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