ecc25d473d89156b67af1ef0830ddc074312c304
[blender.git] / source / blender / editors / space_outliner / space_outliner.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
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"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_arithb.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_colortools.h"
50 #include "BKE_global.h"
51 #include "BKE_screen.h"
52 #include "BKE_texture.h"
53 #include "BKE_utildefines.h"
54
55 #include "ED_area.h"
56 #include "ED_screen.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "BIF_gl.h"
62 #include "BIF_glutil.h"
63
64 #include "UI_interface.h"
65 #include "UI_text.h"
66 #include "UI_resources.h"
67 #include "UI_view2d.h"
68
69 #include "RNA_access.h"
70 #include "RNA_types.h"
71
72 #include "outliner_intern.h"
73
74 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
75 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
76
77 #define ROW_HEIGHT              19
78 #define COLUMN_WIDTH    150
79
80 typedef void (*uiTableCellFunc)(void *userdata, int row, int col, struct rcti *rct, struct uiBlock *block);
81
82 typedef struct uiTable {
83         rcti rct;
84         int rows, cols;
85
86         uiTableCellFunc cellfunc;
87         void *userdata;
88 } uiTable;
89
90 uiTable *UI_table_create(int rows, int cols, rcti *rct, uiTableCellFunc cellfunc, void *userdata)
91 {
92         uiTable *table;
93
94         table= MEM_callocN(sizeof(uiTable), "uiTable");
95         table->rct= *rct;
96         table->cellfunc= cellfunc;
97         table->rows= rows;
98         table->cols= cols;
99         table->userdata= userdata;
100
101         return table;
102 }
103
104 void UI_table_free(uiTable *table)
105 {
106         MEM_freeN(table);
107 }
108
109 void UI_table_draw(const bContext *C, uiTable *table)
110 {
111         uiBlock *block;
112         View2D *v2d;
113         rcti *rct, cellrct;
114         int y, row, col;
115         
116         v2d= &C->region->v2d;
117         rct= &table->rct;
118         
119         block= uiBeginBlock(C, C->region, "table outliner", UI_EMBOSST, UI_HELV);
120         
121         for(y=rct->ymax, row=0; y>rct->ymin; y-=ROW_HEIGHT, row++) {
122                 if(row%2 == 0) {
123                         UI_ThemeColorShade(TH_BACK, 6);
124                         glRecti(v2d->cur.xmin, y-ROW_HEIGHT, v2d->cur.xmax, y);
125                 }
126
127                 if(row >= table->rows)
128                         continue;
129
130                 for(col=0; col<table->cols; col++) {
131                         cellrct.xmin= rct->xmin+COLUMN_WIDTH*col + 1;
132                         cellrct.xmax= rct->xmin+COLUMN_WIDTH*(col+1);
133                         cellrct.ymin= y-ROW_HEIGHT;
134                         cellrct.ymax= y;
135
136                         table->cellfunc(table->userdata, row, col, &cellrct, block);
137                 }
138         }
139
140         UI_ThemeColorShadeAlpha(TH_BACK, -15, -200);
141
142         for(col=0; col<table->cols; col++)
143                 fdrawline(rct->xmin+COLUMN_WIDTH*(col+1), rct->ymin, rct->xmin+COLUMN_WIDTH*(col+1), rct->ymax);
144
145         uiEndBlock(C, block);
146         uiDrawBlock(block);
147 }
148
149
150 /* ************************ main outliner area region *********************** */
151
152 typedef struct CellRNA {
153         SpaceOops *space;
154         StructRNA *srna;
155         PropertyRNA *prop;
156         PointerRNA ptr;
157         int lastrow, index;
158
159         CollectionPropertyIterator iter;
160 } CellRNA;
161
162 static void rna_back_cb(bContext *C, void *arg_unused, void *arg_unused2)
163 {
164         SpaceOops *soutliner= C->area->spacedata.first;
165         char *newpath;
166
167         newpath= RNA_path_back(soutliner->rnapath);
168         if(soutliner->rnapath)
169                 MEM_freeN(soutliner->rnapath);
170         soutliner->rnapath= newpath;
171 }
172
173 static void rna_pointer_cb(bContext *C, void *arg_prop, void *arg_index)
174 {
175         SpaceOops *soutliner= C->area->spacedata.first;
176         PropertyRNA *prop= arg_prop;
177         char *newpath;
178         int index= GET_INT_FROM_POINTER(arg_index);;
179
180         newpath= RNA_path_append(soutliner->rnapath, NULL, prop, index, NULL);
181         if(soutliner->rnapath)
182                 MEM_freeN(soutliner->rnapath);
183         soutliner->rnapath= newpath;
184 }
185
186 static void rna_label(CellRNA *cell, rcti *rct, uiBlock *block)
187 {
188         PropertySubType subtype;
189         PropertyType type;
190         PropertyRNA *prop;
191         char *vectoritem[4]= {"x", "y", "z", "w"};
192         char *quatitem[4]= {"w", "x", "y", "z"};
193         char *coloritem[4]= {"r", "g", "b", "a"};
194         char item[32];
195         int arraylength;
196
197         prop= cell->prop;
198         type= RNA_property_type(&cell->ptr, prop);
199         subtype= RNA_property_subtype(&cell->ptr, prop);
200         arraylength= RNA_property_array_length(&cell->ptr, prop);
201
202         if(cell->index == -1) {
203                 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));
204         }
205         else if (type != PROP_COLLECTION) {
206                 if(arraylength == 4 && subtype == PROP_ROTATION)
207                         sprintf(item, "    %s", quatitem[cell->index]);
208                 else if(arraylength <= 4 && (subtype == PROP_VECTOR || subtype == PROP_ROTATION))
209                         sprintf(item, "    %s", vectoritem[cell->index]);
210                 else if(arraylength <= 4 && subtype == PROP_COLOR)
211                         sprintf(item, "    %s", coloritem[cell->index]);
212                 else
213                         sprintf(item, "    %d", cell->index+1);
214
215                 uiDefBut(block, LABEL, 0, item, rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin, 0, 0, 0, 0, 0, "");
216         }
217 }
218
219 static void rna_collection_but(CellRNA *cell, rcti *rct, uiBlock *block)
220 {
221         uiBut *but;
222         PointerRNA lookup;
223         PropertyRNA *nameprop;
224         char name[256]= "", *nameptr= name;
225
226         RNA_property_collection_lookup_int(&cell->ptr, cell->prop, cell->index, &lookup);
227
228         if(lookup.data) {
229                 nameprop= RNA_struct_name_property(&lookup);
230
231                 if(nameprop)
232                         nameptr= RNA_property_string_get_alloc(&lookup, nameprop, name, sizeof(name));
233                 else
234                         sprintf(nameptr, "%d", cell->index+1);
235         }
236
237         but= uiDefBut(block, BUT, 0, nameptr, rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin, 0, 0, 0, 0, 0, "");
238         uiButSetFlag(but, UI_TEXT_LEFT);
239
240         if(nameptr != name)
241                 MEM_freeN(nameptr);
242
243         uiButSetFunc(but, rna_pointer_cb, cell->prop, SET_INT_IN_POINTER(cell->index));
244 }
245
246 static void rna_but(CellRNA *cell, rcti *rct, uiBlock *block)
247 {
248         uiBut *but;
249         PropertyRNA *prop;
250         PropertyType type;
251         int arraylength, index;
252
253         prop= cell->prop;
254         type= RNA_property_type(&cell->ptr, prop);
255         arraylength= RNA_property_array_length(&cell->ptr, prop);
256
257         if(type == PROP_COLLECTION) {
258                 /* item in a collection */
259                 if(cell->index >= 0)
260                         rna_collection_but(cell, rct, block);
261         }
262         else {
263                 /* other cases */
264                 index= (arraylength)? cell->index: 0;
265
266                 if(index >= 0) {
267                         but= uiDefRNABut(block, 0, &cell->ptr, prop, index, rct->xmin, rct->ymin, rct->xmax-rct->xmin, rct->ymax-rct->ymin);
268
269                         if(type == PROP_POINTER)
270                                 uiButSetFunc(but, rna_pointer_cb, prop, SET_INT_IN_POINTER(0));
271                 }
272         }
273 }
274
275 static void rna_path_but(CellRNA *cell, rcti *rct, uiBlock *block)
276 {
277         uiBut *but;
278
279         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, "");
280         uiButSetFlag(but, UI_TEXT_LEFT);
281         uiButSetFunc(but, rna_back_cb, cell->space, NULL);
282 }
283
284 static void rna_table_cell_func(void *userdata, int row, int col, rcti *rct, uiBlock *block)
285 {
286         CellRNA *cell= userdata;
287         PropertyType type;
288         int length;
289
290         /* path button */
291         if(row == 0) {
292                 if(col == 0)
293                         rna_path_but(cell, rct, block);
294
295                 return;
296         }
297
298         /* set next property for new row */
299         if(row != cell->lastrow) {
300                 if(cell->prop) {
301                         cell->index++;
302
303                         type= RNA_property_type(&cell->ptr, cell->prop);
304                         if(type == PROP_COLLECTION)
305                                 length= RNA_property_collection_length(&cell->ptr, cell->prop);
306                         else
307                                 length= RNA_property_array_length(&cell->ptr, cell->prop);
308
309                         /* verify if we need to go to the next property */
310                         if(type == PROP_COLLECTION && cell->index < length);
311                         else if(length && cell->index < length);
312                         else {
313                                 RNA_property_collection_next(&cell->iter);
314                                 cell->prop= cell->iter.ptr.data;
315                                 cell->index= -1;
316                         }
317                 }
318                 else {
319                         /* initialize */
320                         cell->prop= cell->iter.ptr.data;
321                         cell->index= -1;
322                 }
323
324                 cell->lastrow= row;
325         }
326
327         /* make button */
328         if(col == 0)
329                 rna_label(cell, rct, block);
330         else if(col == 1)
331                 rna_but(cell, rct, block);
332 }
333
334 static void outliner_main_area_init(wmWindowManager *wm, ARegion *ar)
335 {
336         UI_view2d_size_update(&ar->v2d, ar->winx, ar->winy);
337
338 }
339
340 static void outliner_main_area_draw(const bContext *C, ARegion *ar)
341 {
342         uiTable *table;
343         rcti rct;
344         CellRNA cell;
345         PropertyRNA *prop, *iterprop;
346         PointerRNA newptr;
347         float col[3];
348         int rows, cols, awidth, aheight, width, height;
349         SpaceOops *soutliner= C->area->spacedata.first;
350         View2D *v2d= &ar->v2d;
351         View2DScrollers *scrollers;
352
353         /* clear */
354         UI_GetThemeColor3fv(TH_BACK, col);
355         glClearColor(col[0], col[1], col[2], 0.0);
356         glClear(GL_COLOR_BUFFER_BIT);
357         
358         awidth= width= ar->winx;
359         aheight= height= ar->winy;
360         
361         /* create table */
362         cell.space= soutliner;
363         cell.lastrow= -1;
364         RNA_main_pointer_create(G.main, &cell.ptr);
365         cell.prop= NULL;
366
367         /* solve RNA path or reset if fails */
368         if(soutliner->rnapath) {
369                 if(!RNA_path_resolve(&cell.ptr, soutliner->rnapath, &newptr, &prop)) {
370                         newptr.data= NULL;
371                         printf("RNA outliner: failed resolving path. (%s)\n", soutliner->rnapath);
372                 }
373
374                 if(newptr.data && newptr.type) {
375                         cell.ptr= newptr;
376                 }
377                 else {
378                         MEM_freeN(soutliner->rnapath);
379                         soutliner->rnapath= NULL;
380                 }
381         }
382
383         /* compute number of rows and columns */
384         rows= 1;
385         cols= 2;
386
387         iterprop= RNA_struct_iterator_property(&cell.ptr);
388         RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
389
390         for(; cell.iter.valid; RNA_property_collection_next(&cell.iter)) {
391                 prop= cell.iter.ptr.data;
392
393                 rows += 1 + RNA_property_array_length(&cell.ptr, prop);
394                 if(RNA_property_type(&cell.ptr, prop) == PROP_COLLECTION)
395                         rows += RNA_property_collection_length(&cell.ptr, prop);
396         }
397
398         RNA_property_collection_end(&cell.iter);
399         
400         /* determine extents of data
401          *      - height must be at least the height of the mask area
402          *      - width is columns + 1, as otherwise, part of last column 
403          *        will be obscured by scrollers
404          */
405         if ((rows*ROW_HEIGHT) > height)
406                 height= rows * ROW_HEIGHT;
407         width= (cols + 1) * COLUMN_WIDTH;
408         
409         /* update size of tot-rect (extents of data/viewable area) */
410         UI_view2d_totRect_set(v2d, width, height);
411         
412         rct.xmin= 0;
413         rct.ymin= -height;
414         rct.xmax= width;
415         rct.ymax= 0;
416         
417         /* set matrix for 2d-view controls */
418         UI_view2d_view_ortho(C, v2d);
419         
420         /* create and draw table */
421         table= UI_table_create(rows, 2, &rct, rna_table_cell_func, &cell);
422
423         RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
424         UI_table_draw(C, table);
425         RNA_property_collection_end(&cell.iter);
426
427         UI_table_free(table);
428         
429         /* reset view matrix */
430         UI_view2d_view_restore(C);
431         
432         /* scrollers */
433         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
434         UI_view2d_scrollers_draw(C, v2d, scrollers);
435         UI_view2d_scrollers_free(scrollers);
436 }
437
438 static void outliner_main_area_free(ARegion *ar)
439 {
440 }
441
442 /* ************************ header outliner area region *********************** */
443
444
445 static void outliner_header_area_draw(const bContext *C, ARegion *ar)
446 {
447         SpaceOops *soutliner= C->area->spacedata.first;
448         float col[3];
449         int width, height;
450         rctf bbox;
451         char *path;
452
453         path= (soutliner->rnapath)? soutliner->rnapath: "Main";
454
455         if(ED_screen_area_active(C))
456                 UI_GetThemeColor3fv(TH_HEADER, col);
457         else
458                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
459         
460         glClearColor(col[0], col[1], col[2], 0.0);
461         glClear(GL_COLOR_BUFFER_BIT);
462
463         width= ar->winrct.xmax - ar->winrct.xmin;
464         height= ar->winrct.ymax - ar->winrct.ymin;
465
466         /* header text */
467         UI_GetBoundingBox(UI_HELV, path, 0, &bbox);
468
469         glColor3f(1.0f, 1.0f, 1.0f);
470         UI_SetScale(1.0);
471         UI_RasterPos(0.5f*(width - (bbox.xmax - bbox.xmin)), 0.5f*(height - (bbox.ymax - bbox.ymin)));
472         UI_DrawString(UI_HELV, path, 0);
473 }
474
475 static void outliner_header_area_free(ARegion *ar)
476 {
477 }
478
479 /* ******************** default callbacks for outliner space ***************** */
480
481 static SpaceLink *outliner_new(void)
482 {
483         ARegion *ar;
484         SpaceOops *soutliner;
485
486         soutliner= MEM_callocN(sizeof(SpaceOops), "initoutliner");
487
488         /* header */
489         ar= MEM_callocN(sizeof(ARegion), "header for outliner");
490         
491         BLI_addtail(&soutliner->regionbase, ar);
492         ar->regiontype= RGN_TYPE_HEADER;
493         ar->alignment= RGN_ALIGN_BOTTOM;
494         UI_view2d_header_default(&ar->v2d);
495         
496         /* main area */
497         ar= MEM_callocN(sizeof(ARegion), "main area for outliner");
498         
499         BLI_addtail(&soutliner->regionbase, ar);
500         ar->regiontype= RGN_TYPE_WINDOW;
501         
502         ar->v2d.scroll |= V2D_SCROLL_RIGHT;
503         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
504         ar->v2d.keepzoom |= (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y);
505         ar->v2d.keepaspect= 1;
506         ar->v2d.keepzoom= 1;
507         ar->v2d.keeptot= 2;     /* XXX make define */
508         
509         return (SpaceLink*)soutliner;
510 }
511
512 /* not spacelink itself */
513 static void outliner_free(SpaceLink *sl)
514 {
515         SpaceOops *soutliner= (SpaceOops*)sl;
516
517         if(soutliner->rnapath) {
518                 MEM_freeN(soutliner->rnapath);
519                 soutliner->rnapath= NULL;
520         }
521 }
522
523 /* spacetype; init callback */
524 static void outliner_init(wmWindowManager *wm, ScrArea *sa)
525 {
526         
527 }
528
529 static SpaceLink *outliner_duplicate(SpaceLink *sl)
530 {
531         SpaceOops *soutliner= (SpaceOops *)sl;
532         SpaceOops *soutlinern= MEM_dupallocN(soutliner);
533
534         if(soutlinern->rnapath)
535                 soutlinern->rnapath= MEM_dupallocN(soutlinern->rnapath);
536         
537         return (SpaceLink *)soutlinern;
538 }
539
540 /* only called once, from space_api/spacetypes.c */
541 void ED_spacetype_outliner(void)
542 {
543         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype time");
544         ARegionType *art;
545         
546         st->spaceid= SPACE_OOPS;
547         strncpy(st->name, "Outliner", BKE_ST_MAXNAME);
548         
549         st->new= outliner_new;
550         st->free= outliner_free;
551         st->init= outliner_init;
552         st->duplicate= outliner_duplicate;
553         st->operatortypes= outliner_operatortypes;
554         st->keymap= outliner_keymap;
555         
556         /* regions: main window */
557         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
558         art->regionid = RGN_TYPE_WINDOW;
559         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
560         
561         art->init= outliner_main_area_init;
562         art->draw= outliner_main_area_draw;
563         art->free= outliner_main_area_free;
564         BLI_addhead(&st->regiontypes, art);
565         
566         /* regions: header */
567         art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
568         art->regionid = RGN_TYPE_HEADER;
569         art->minsizey= HEADERY;
570         art->keymapflag= ED_KEYMAP_UI;
571         
572         art->draw= outliner_header_area_draw;
573         art->free= outliner_header_area_free;
574         BLI_addhead(&st->regiontypes, art);
575         
576         BKE_spacetype_register(st);
577 }
578