4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
32 #include "DNA_color_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_oops_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_vec_types.h"
41 #include "DNA_windowmanager_types.h"
43 #include "MEM_guardedalloc.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_arithb.h"
49 #include "BKE_colortools.h"
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
52 #include "BKE_texture.h"
53 #include "BKE_utildefines.h"
55 #include "ED_space_api.h"
56 #include "ED_screen.h"
62 #include "BIF_glutil.h"
64 #include "UI_interface.h"
66 #include "UI_resources.h"
67 #include "UI_view2d.h"
69 #include "RNA_access.h"
70 #include "RNA_types.h"
72 #include "outliner_intern.h"
74 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
75 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
78 #define COLUMN_WIDTH 150
80 typedef void (*uiTableCellFunc)(void *userdata, int row, int col, struct rcti *rct, struct uiBlock *block);
82 typedef struct uiTable {
86 uiTableCellFunc cellfunc;
90 uiTable *UI_table_create(int rows, int cols, rcti *rct, uiTableCellFunc cellfunc, void *userdata)
94 table= MEM_callocN(sizeof(uiTable), "uiTable");
96 table->cellfunc= cellfunc;
99 table->userdata= userdata;
104 void UI_table_free(uiTable *table)
109 void UI_table_draw(const bContext *C, uiTable *table)
111 ARegion *ar= CTX_wm_region(C);
120 block= uiBeginBlock(C, ar, "table outliner", UI_EMBOSST, UI_HELV);
122 for(y=rct->ymax, row=0; y>rct->ymin; y-=ROW_HEIGHT, row++) {
124 UI_ThemeColorShade(TH_BACK, 6);
125 glRecti(v2d->cur.xmin, y-ROW_HEIGHT, v2d->cur.xmax, y);
128 if(row >= table->rows)
131 for(col=0; col<table->cols; col++) {
132 cellrct.xmin= rct->xmin+COLUMN_WIDTH*col + 1;
133 cellrct.xmax= rct->xmin+COLUMN_WIDTH*(col+1);
134 cellrct.ymin= y-ROW_HEIGHT;
137 table->cellfunc(table->userdata, row, col, &cellrct, block);
141 UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
143 for(col=0; col<table->cols; col++)
144 fdrawline(rct->xmin+COLUMN_WIDTH*(col+1), rct->ymin, rct->xmin+COLUMN_WIDTH*(col+1), rct->ymax);
146 uiEndBlock(C, block);
151 /* ************************ main outliner area region *********************** */
153 typedef struct CellRNA {
160 CollectionPropertyIterator iter;
163 static void rna_back_cb(bContext *C, void *arg_unused, void *arg_unused2)
165 SpaceOops *soutliner= (SpaceOops*)CTX_wm_space_data(C);
168 newpath= RNA_path_back(soutliner->rnapath);
169 if(soutliner->rnapath)
170 MEM_freeN(soutliner->rnapath);
171 soutliner->rnapath= newpath;
174 static void rna_pointer_cb(bContext *C, void *arg_prop, void *arg_index)
176 SpaceOops *soutliner= (SpaceOops*)CTX_wm_space_data(C);
177 PropertyRNA *prop= arg_prop;
179 int index= GET_INT_FROM_POINTER(arg_index);;
181 newpath= RNA_path_append(soutliner->rnapath, NULL, prop, index, NULL);
182 if(soutliner->rnapath)
183 MEM_freeN(soutliner->rnapath);
184 soutliner->rnapath= newpath;
187 static void rna_label(CellRNA *cell, rcti *rct, uiBlock *block)
189 PropertySubType subtype;
192 char *vectoritem[4]= {"x", "y", "z", "w"};
193 char *quatitem[4]= {"w", "x", "y", "z"};
194 char *coloritem[4]= {"r", "g", "b", "a"};
199 type= RNA_property_type(&cell->ptr, prop);
200 subtype= RNA_property_subtype(&cell->ptr, prop);
201 arraylength= RNA_property_array_length(&cell->ptr, prop);
203 if(cell->index == -1) {
204 uiDefBut(block, LABEL, 0, (char*)RNA_property_ui_name(&cell->ptr, prop), rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin, 0, 0, 0, 0, 0, (char*)RNA_property_ui_description(&cell->ptr, prop));
206 else if (type != PROP_COLLECTION) {
207 if(arraylength == 4 && subtype == PROP_ROTATION)
208 sprintf(item, " %s", quatitem[cell->index]);
209 else if(arraylength <= 4 && (subtype == PROP_VECTOR || subtype == PROP_ROTATION))
210 sprintf(item, " %s", vectoritem[cell->index]);
211 else if(arraylength <= 4 && subtype == PROP_COLOR)
212 sprintf(item, " %s", coloritem[cell->index]);
214 sprintf(item, " %d", cell->index+1);
216 uiDefBut(block, LABEL, 0, item, rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin, 0, 0, 0, 0, 0, "");
220 static void rna_collection_but(CellRNA *cell, rcti *rct, uiBlock *block)
224 PropertyRNA *nameprop;
225 char name[256]= "", *nameptr= name;
227 RNA_property_collection_lookup_int(&cell->ptr, cell->prop, cell->index, &lookup);
230 nameprop= RNA_struct_name_property(&lookup);
233 nameptr= RNA_property_string_get_alloc(&lookup, nameprop, name, sizeof(name));
235 if(!nameprop || strlen(nameptr) == 0)
236 sprintf(nameptr, "%d", cell->index+1);
239 but= uiDefBut(block, BUT, 0, nameptr, rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin, 0, 0, 0, 0, 0, "");
240 uiButSetFlag(but, UI_TEXT_LEFT);
245 uiButSetFunc(but, rna_pointer_cb, cell->prop, SET_INT_IN_POINTER(cell->index));
248 static uiBut *rna_auto_but(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int index, int x1, int y1, int x2, int y2)
251 const char *propname= RNA_property_identifier(ptr, prop);
253 switch(RNA_property_type(ptr, prop)) {
257 length= RNA_property_array_length(ptr, prop);
260 value= RNA_property_boolean_get_array(ptr, prop, index);
262 value= RNA_property_boolean_get(ptr, prop);
264 but= uiDefButR(block, TOG, 0, (value)? "True": "False", x1, y1, x2, y2, ptr, propname, index, 0, 0, -1, -1, NULL);
269 but= uiDefButR(block, NUM, 0, "", x1, y1, x2, y2, ptr, propname, index, 0, 0, -1, -1, NULL);
272 but= uiDefButR(block, MENU, 0, NULL, x1, y1, x2, y2, ptr, propname, index, 0, 0, -1, -1, NULL);
275 but= uiDefButR(block, TEX, 0, "", x1, y1, x2, y2, ptr, propname, index, 0, 0, -1, -1, NULL);
279 PropertyRNA *nameprop;
280 char name[256]= "", *nameptr= name;
282 RNA_property_pointer_get(ptr, prop, &pptr);
285 nameprop= RNA_struct_name_property(&pptr);
286 if(pptr.type && nameprop)
287 nameptr= RNA_property_string_get_alloc(&pptr, nameprop, name, sizeof(name));
289 strcpy(nameptr, "->");
292 but= uiDefButR(block, BUT, 0, nameptr, x1, y1, x2, y2, ptr, propname, index, 0, 0, 0, 0, NULL);
293 uiButSetFlag(but, UI_TEXT_LEFT);
308 static void rna_but(CellRNA *cell, rcti *rct, uiBlock *block)
313 int arraylength, index;
316 type= RNA_property_type(&cell->ptr, prop);
317 arraylength= RNA_property_array_length(&cell->ptr, prop);
319 if(type == PROP_COLLECTION) {
320 /* item in a collection */
322 rna_collection_but(cell, rct, block);
326 index= (arraylength)? cell->index: 0;
329 but= rna_auto_but(block, &cell->ptr, prop, index, rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin);
331 if(type == PROP_POINTER)
332 uiButSetFunc(but, rna_pointer_cb, prop, SET_INT_IN_POINTER(0));
337 static void rna_path_but(CellRNA *cell, rcti *rct, uiBlock *block)
341 but= uiDefBut(block, BUT, 0, (cell->space->rnapath)? "..": ".", rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin, 0, 0, 0, 0, 0, "");
342 uiButSetFlag(but, UI_TEXT_LEFT);
343 uiButSetFunc(but, rna_back_cb, cell->space, NULL);
346 static void rna_table_cell_func(void *userdata, int row, int col, rcti *rct, uiBlock *block)
348 CellRNA *cell= userdata;
355 rna_path_but(cell, rct, block);
360 /* set next property for new row */
361 if(row != cell->lastrow) {
365 type= RNA_property_type(&cell->ptr, cell->prop);
366 if(type == PROP_COLLECTION)
367 length= RNA_property_collection_length(&cell->ptr, cell->prop);
369 length= RNA_property_array_length(&cell->ptr, cell->prop);
371 /* verify if we need to go to the next property */
372 if(type == PROP_COLLECTION && cell->index < length);
373 else if(length && cell->index < length);
375 RNA_property_collection_next(&cell->iter);
376 cell->prop= cell->iter.ptr.data;
382 cell->prop= cell->iter.ptr.data;
391 rna_label(cell, rct, block);
393 rna_but(cell, rct, block);
396 static void outliner_main_area_init(wmWindowManager *wm, ARegion *ar)
398 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
401 static void outliner_main_area_draw(const bContext *C, ARegion *ar)
406 PropertyRNA *prop, *iterprop;
409 int rows, cols, awidth, aheight, width, height;
410 SpaceOops *soutliner= (SpaceOops*)CTX_wm_space_data(C);
411 View2D *v2d= &ar->v2d;
412 View2DScrollers *scrollers;
415 UI_GetThemeColor3fv(TH_BACK, col);
416 glClearColor(col[0], col[1], col[2], 0.0);
417 glClear(GL_COLOR_BUFFER_BIT);
419 awidth= width= ar->winx;
420 aheight= height= ar->winy;
423 cell.space= soutliner;
425 RNA_main_pointer_create(CTX_data_main(C), &cell.ptr);
428 /* solve RNA path or reset if fails */
429 if(soutliner->rnapath) {
430 if(!RNA_path_resolve(&cell.ptr, soutliner->rnapath, &newptr, &prop)) {
432 printf("RNA outliner: failed resolving path. (%s)\n", soutliner->rnapath);
435 if(newptr.data && newptr.type) {
439 MEM_freeN(soutliner->rnapath);
440 soutliner->rnapath= NULL;
444 /* compute number of rows and columns */
448 iterprop= RNA_struct_iterator_property(&cell.ptr);
449 RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
451 for(; cell.iter.valid; RNA_property_collection_next(&cell.iter)) {
452 prop= cell.iter.ptr.data;
454 rows += 1 + RNA_property_array_length(&cell.ptr, prop);
455 if(RNA_property_type(&cell.ptr, prop) == PROP_COLLECTION)
456 rows += RNA_property_collection_length(&cell.ptr, prop);
459 RNA_property_collection_end(&cell.iter);
461 /* determine extents of data
462 * - height must be at least the height of the mask area
463 * - width is columns + 1, as otherwise, part of last column
464 * will be obscured by scrollers
466 if ((rows*ROW_HEIGHT) > height)
467 height= rows * ROW_HEIGHT;
468 width= (cols + 1) * COLUMN_WIDTH;
470 /* update size of tot-rect (extents of data/viewable area) */
471 UI_view2d_totRect_set(v2d, width, height);
478 /* set matrix for 2d-view controls */
479 UI_view2d_view_ortho(C, v2d);
481 /* create and draw table */
482 table= UI_table_create(rows, 2, &rct, rna_table_cell_func, &cell);
484 RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
485 UI_table_draw(C, table);
486 RNA_property_collection_end(&cell.iter);
488 UI_table_free(table);
490 /* reset view matrix */
491 UI_view2d_view_restore(C);
494 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
495 UI_view2d_scrollers_draw(C, v2d, scrollers);
496 UI_view2d_scrollers_free(scrollers);
499 static void outliner_main_area_free(ARegion *ar)
503 /* ************************ header outliner area region *********************** */
505 /* add handlers, stuff you only do once or on area/region changes */
506 static void outliner_header_area_init(wmWindowManager *wm, ARegion *ar)
508 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
511 static void outliner_header_area_draw(const bContext *C, ARegion *ar)
515 if(ED_screen_area_active(C))
516 UI_GetThemeColor3fv(TH_HEADER, col);
518 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
520 glClearColor(col[0], col[1], col[2], 0.0);
521 glClear(GL_COLOR_BUFFER_BIT);
523 /* set view2d view matrix for scrolling (without scrollers) */
524 UI_view2d_view_ortho(C, &ar->v2d);
526 outliner_header_buttons(C, ar);
529 static void outliner_header_area_free(ARegion *ar)
533 /* ******************** default callbacks for outliner space ***************** */
535 static SpaceLink *outliner_new(void)
538 SpaceOops *soutliner;
540 soutliner= MEM_callocN(sizeof(SpaceOops), "initoutliner");
543 ar= MEM_callocN(sizeof(ARegion), "header for outliner");
545 BLI_addtail(&soutliner->regionbase, ar);
546 ar->regiontype= RGN_TYPE_HEADER;
547 ar->alignment= RGN_ALIGN_BOTTOM;
550 ar= MEM_callocN(sizeof(ARegion), "main area for outliner");
552 BLI_addtail(&soutliner->regionbase, ar);
553 ar->regiontype= RGN_TYPE_WINDOW;
555 ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM_O);
556 ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
557 ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
558 ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
559 ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
561 return (SpaceLink*)soutliner;
564 static void free_oops(Oops *oops) /* also oops itself */
566 BLI_freelistN(&oops->link);
570 static void outliner_free_tree(ListBase *lb)
574 TreeElement *te= lb->first;
576 outliner_free_tree(&te->subtree);
582 /* not spacelink itself */
583 static void outliner_free(SpaceLink *sl)
585 SpaceOops *soutliner= (SpaceOops*)sl;
588 if(soutliner->rnapath) {
589 MEM_freeN(soutliner->rnapath);
590 soutliner->rnapath= NULL;
593 while( (oops= soutliner->oops.first) ) {
594 BLI_remlink(&soutliner->oops, oops);
598 outliner_free_tree(&soutliner->tree);
599 if(soutliner->treestore) {
600 if(soutliner->treestore->data) MEM_freeN(soutliner->treestore->data);
601 MEM_freeN(soutliner->treestore);
606 /* spacetype; init callback */
607 static void outliner_init(wmWindowManager *wm, ScrArea *sa)
612 static SpaceLink *outliner_duplicate(SpaceLink *sl)
614 SpaceOops *soutliner= (SpaceOops *)sl;
615 SpaceOops *soutlinern= MEM_dupallocN(soutliner);
617 if(soutlinern->rnapath)
618 soutlinern->rnapath= MEM_dupallocN(soutlinern->rnapath);
620 soutlinern->oops.first= soutlinern->oops.last= NULL;
621 soutlinern->tree.first= soutlinern->tree.last= NULL;
622 soutlinern->treestore= NULL;
624 return (SpaceLink *)soutlinern;
627 /* only called once, from space_api/spacetypes.c */
628 void ED_spacetype_outliner(void)
630 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype time");
633 st->spaceid= SPACE_OOPS;
634 strncpy(st->name, "Outliner", BKE_ST_MAXNAME);
636 st->new= outliner_new;
637 st->free= outliner_free;
638 st->init= outliner_init;
639 st->duplicate= outliner_duplicate;
640 st->operatortypes= outliner_operatortypes;
641 st->keymap= outliner_keymap;
643 /* regions: main window */
644 art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
645 art->regionid = RGN_TYPE_WINDOW;
646 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
648 art->init= outliner_main_area_init;
649 art->draw= outliner_main_area_draw;
650 art->free= outliner_main_area_free;
651 BLI_addhead(&st->regiontypes, art);
653 /* regions: header */
654 art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
655 art->regionid = RGN_TYPE_HEADER;
656 art->minsizey= HEADERY;
657 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
659 art->init= outliner_header_area_init;
660 art->draw= outliner_header_area_draw;
661 art->free= outliner_header_area_free;
662 BLI_addhead(&st->regiontypes, art);
664 BKE_spacetype_register(st);