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, NULL, 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         PropertyType type;
282         int length;
283
284         /* path button */
285         if(row == 0) {
286                 if(col == 0)
287                         rna_path_but(cell, rct, block);
288
289                 return;
290         }
291
292         /* set next property for new row */
293         if(row != cell->lastrow) {
294                 if(cell->prop) {
295                         cell->index++;
296
297                         type= RNA_property_type(cell->prop, &cell->ptr);
298                         if(type == PROP_COLLECTION)
299                                 length= RNA_property_collection_length(cell->prop, &cell->ptr);
300                         else
301                                 length= RNA_property_array_length(cell->prop, &cell->ptr);
302
303                         /* verify if we need to go to the next property */
304                         if(type == PROP_COLLECTION && cell->index < length);
305                         else if(length && cell->index < length);
306                         else {
307                                 RNA_property_collection_next(cell->iterprop, &cell->iter);
308                                 cell->prop= cell->iter.ptr.data;
309                                 cell->index= -1;
310                         }
311                 }
312                 else {
313                         /* initialize */
314                         cell->prop= cell->iter.ptr.data;
315                         cell->index= -1;
316                 }
317
318                 cell->lastrow= row;
319         }
320
321         /* make button */
322         if(col == 0)
323                 rna_label(cell, rct, block);
324         else if(col == 1)
325                 rna_but(cell, rct, block);
326 }
327
328 static void outliner_main_area_draw(const bContext *C, ARegion *ar)
329 {
330         uiTable *table;
331         rcti rct;
332         CellRNA cell;
333         PropertyRNA *prop;
334         PointerRNA newptr;
335         float col[3];
336         int rows, cols, width, height;
337         SpaceOops *soutliner= C->area->spacedata.first;
338
339         /* clear */
340         UI_GetThemeColor3fv(TH_BACK, col);
341         glClearColor(col[0], col[1], col[2], 0.0);
342         glClear(GL_COLOR_BUFFER_BIT);
343
344         width= ar->winrct.xmax - ar->winrct.xmin;
345         height= ar->winrct.ymax - ar->winrct.ymin;
346         
347         /* create table */
348         rct.xmin= 0;
349         rct.ymin= 0;
350         rct.xmax= width;
351         rct.ymax= height;
352
353         cell.space= soutliner;
354         cell.lastrow= -1;
355         RNA_pointer_main_get(G.main, &cell.ptr);
356         cell.prop= NULL;
357
358         /* solve RNA path or reset if fails */
359         if(soutliner->rnapath) {
360                 if(!RNA_path_resolve(&cell.ptr, soutliner->rnapath, &newptr, &prop)) {
361                         newptr.data= NULL;
362                         printf("RNA outliner: failed resolving path. (%s)\n", soutliner->rnapath);
363                 }
364
365                 if(newptr.data && newptr.type) {
366                         cell.ptr= newptr;
367                 }
368                 else {
369                         MEM_freeN(soutliner->rnapath);
370                         soutliner->rnapath= NULL;
371                 }
372         }
373
374         /* compute number of rows and columns */
375         rows= 1;
376         cols= 2;
377
378         cell.iterprop= RNA_struct_iterator_property(&cell.ptr);
379         RNA_property_collection_begin(cell.iterprop, &cell.iter, &cell.ptr);
380
381         for(; cell.iter.valid; RNA_property_collection_next(cell.iterprop, &cell.iter)) {
382                 prop= cell.iter.ptr.data;
383
384                 rows += 1 + RNA_property_array_length(prop, &cell.ptr);
385                 if(RNA_property_type(prop, &cell.ptr) == PROP_COLLECTION)
386                         rows += RNA_property_collection_length(prop, &cell.ptr);
387         }
388
389         RNA_property_collection_end(cell.iterprop, &cell.iter);
390
391         /* create and draw table */
392         table= UI_table_create(rows, 2, &rct, rna_table_cell_func, &cell);
393
394         RNA_property_collection_begin(cell.iterprop, &cell.iter, &cell.ptr);
395         UI_table_draw(C->window, ar, table);
396         RNA_property_collection_end(cell.iterprop, &cell.iter);
397
398         UI_table_free(table);
399 }
400
401 static void outliner_main_area_free(ARegion *ar)
402 {
403         uiFreeBlocks(&ar->uiblocks);
404 }
405
406 /* ************************ header outliner area region *********************** */
407
408 static void outliner_header_area_draw(const bContext *C, ARegion *ar)
409 {
410         SpaceOops *soutliner= C->area->spacedata.first;
411         float col[3];
412         int width, height;
413         rctf bbox;
414         char *path;
415
416         path= (soutliner->rnapath)? soutliner->rnapath: "Main";
417
418         /* clear */
419         UI_GetThemeColor3fv(TH_BACK, col);
420         glClearColor(MAX2(col[0]-0.3f, 0.0f), MAX2(col[1]-0.3f, 0.0f), MAX2(col[2]-0.3f, 0.0f), 0.0);
421         glClear(GL_COLOR_BUFFER_BIT);
422
423         width= ar->winrct.xmax - ar->winrct.xmin;
424         height= ar->winrct.ymax - ar->winrct.ymin;
425
426         /* header text */
427         UI_GetBoundingBox(UI_HELV, path, 0, &bbox);
428
429         glColor3f(1.0f, 1.0f, 1.0f);
430         UI_SetScale(1.0);
431         UI_RasterPos(0.5f*(width - (bbox.xmax - bbox.xmin)), 0.5f*(height - (bbox.ymax - bbox.ymin)));
432         UI_DrawString(UI_HELV, path, 0);
433 }
434
435 static void outliner_header_area_free(ARegion *ar)
436 {
437         uiFreeBlocks(&ar->uiblocks);
438 }
439
440 /* ******************** default callbacks for outliner space ***************** */
441
442 static SpaceLink *outliner_new(void)
443 {
444         SpaceOops *soutliner;
445
446         soutliner= MEM_callocN(sizeof(SpaceOops), "initoutliner");
447
448         return (SpaceLink*)soutliner;
449 }
450
451 /* not spacelink itself */
452 static void outliner_free(SpaceLink *sl)
453 {
454         SpaceOops *soutliner= (SpaceOops*)sl;
455
456         if(soutliner->rnapath) {
457                 MEM_freeN(soutliner->rnapath);
458                 soutliner->rnapath= NULL;
459         }
460 }
461
462 /* spacetype; init callback */
463 static void outliner_init(wmWindowManager *wm, ScrArea *sa)
464 {
465         ARegion *ar;
466         
467         /* link area to SpaceXXX struct */
468
469         /* add handlers to area */
470         /* define how many regions, the order and types */
471         
472         /* add types to regions */
473         for(ar= sa->regionbase.first; ar; ar= ar->next) {
474                 if(ar->regiontype == RGN_TYPE_WINDOW) {
475                         static ARegionType mainart={NULL, NULL, NULL, NULL, NULL};
476
477                         mainart.draw= outliner_main_area_draw;
478                         mainart.free= outliner_main_area_free;
479
480                         ar->type= &mainart;
481
482                         WM_event_add_keymap_handler(&ar->handlers, &wm->uikeymap);
483                 }
484                 else if(ar->regiontype == RGN_TYPE_HEADER) {
485                         static ARegionType headerart={NULL, NULL, NULL, NULL, NULL};
486
487                         headerart.draw= outliner_header_area_draw;
488                         headerart.free= outliner_header_area_free;
489
490                         ar->type= &headerart;
491
492                         WM_event_add_keymap_handler(&ar->handlers, &wm->uikeymap);
493                 }
494                 else {
495                         static ARegionType headerart={NULL, NULL, NULL, NULL, NULL};
496                         ar->type= &headerart;
497                 }
498         }
499 }
500
501 /* spacetype; context changed */
502 static void outliner_refresh(bContext *C, ScrArea *sa)
503 {
504         
505 }
506
507 static SpaceLink *outliner_duplicate(SpaceLink *sl)
508 {
509         SpaceOops *soutliner= (SpaceOops *)sl;
510         SpaceOops *soutlinern= MEM_dupallocN(soutliner);
511         
512         return (SpaceLink *)soutlinern;
513 }
514
515 /* only called once, from screen/spacetypes.c */
516 void ED_spacetype_outliner(void)
517 {
518         static SpaceType st;
519         
520         st.spaceid= SPACE_OOPS;
521         
522         st.new= outliner_new;
523         st.free= outliner_free;
524         st.init= outliner_init;
525         st.refresh= outliner_refresh;
526         st.duplicate= outliner_duplicate;
527         st.operatortypes= outliner_operatortypes;
528         st.keymap= outliner_keymap;
529         
530         BKE_spacetype_register(&st);
531 }
532