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