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