View2D: Scrollbar tweaks
[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 + 1;
352         aheight= height= ar->winrct.ymax - ar->winrct.ymin + 1;
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         width= (cols + 1) * COLUMN_WIDTH;
398         
399         /* need to validate view2d after updating size of tot */
400         v2d->tot.xmin= 0;
401         v2d->tot.xmax= width;
402         v2d->tot.ymax= 0;
403         v2d->tot.ymin= -height;
404         UI_view2d_enforce_status(v2d, awidth, aheight);
405         
406         rct.xmin= 0;
407         rct.ymin= -height;
408         rct.xmax= width;
409         rct.ymax= 0;
410         
411         /* set matrix for 2d-view controls */
412         UI_view2d_view_ortho(C, v2d);
413         
414         /* create and draw table */
415         table= UI_table_create(rows, 2, &rct, rna_table_cell_func, &cell);
416
417         RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
418         UI_table_draw(C->window, ar, table);
419         RNA_property_collection_end(&cell.iter);
420
421         UI_table_free(table);
422         
423         /* reset view matrix */
424         UI_view2d_view_restore(C);
425         
426         /* scrollers */
427         scrollers= UI_view2d_calc_scrollers(C, v2d, 0, 0, 0, 0);
428         UI_view2d_draw_scrollers(C, v2d, scrollers);
429         UI_view2d_free_scrollers(scrollers);
430 }
431
432 static void outliner_main_area_free(ARegion *ar)
433 {
434         uiFreeBlocks(&ar->uiblocks);
435 }
436
437 /* ************************ header outliner area region *********************** */
438
439 static void outliner_header_area_draw(const bContext *C, ARegion *ar)
440 {
441         SpaceOops *soutliner= C->area->spacedata.first;
442         float col[3];
443         int width, height;
444         rctf bbox;
445         char *path;
446
447         path= (soutliner->rnapath)? soutliner->rnapath: "Main";
448
449         /* clear */
450         UI_GetThemeColor3fv(TH_BACK, col);
451         glClearColor(MAX2(col[0]-0.3f, 0.0f), MAX2(col[1]-0.3f, 0.0f), MAX2(col[2]-0.3f, 0.0f), 0.0);
452         glClear(GL_COLOR_BUFFER_BIT);
453
454         width= ar->winrct.xmax - ar->winrct.xmin;
455         height= ar->winrct.ymax - ar->winrct.ymin;
456
457         /* header text */
458         UI_GetBoundingBox(UI_HELV, path, 0, &bbox);
459
460         glColor3f(1.0f, 1.0f, 1.0f);
461         UI_SetScale(1.0);
462         UI_RasterPos(0.5f*(width - (bbox.xmax - bbox.xmin)), 0.5f*(height - (bbox.ymax - bbox.ymin)));
463         UI_DrawString(UI_HELV, path, 0);
464 }
465
466 static void outliner_header_area_free(ARegion *ar)
467 {
468         uiFreeBlocks(&ar->uiblocks);
469 }
470
471 /* ******************** default callbacks for outliner space ***************** */
472
473 static SpaceLink *outliner_new(void)
474 {
475         SpaceOops *soutliner;
476
477         soutliner= MEM_callocN(sizeof(SpaceOops), "initoutliner");
478
479         return (SpaceLink*)soutliner;
480 }
481
482 /* not spacelink itself */
483 static void outliner_free(SpaceLink *sl)
484 {
485         SpaceOops *soutliner= (SpaceOops*)sl;
486
487         if(soutliner->rnapath) {
488                 MEM_freeN(soutliner->rnapath);
489                 soutliner->rnapath= NULL;
490         }
491 }
492
493 /* spacetype; init callback */
494 static void outliner_init(wmWindowManager *wm, ScrArea *sa)
495 {
496         ARegion *ar;
497         
498         /* link area to SpaceXXX struct */
499
500         /* add handlers to area */
501         /* define how many regions, the order and types */
502         
503         /* add types to regions */
504         for(ar= sa->regionbase.first; ar; ar= ar->next) {
505                 if(ar->regiontype == RGN_TYPE_WINDOW) {
506                         static ARegionType mainart={NULL, NULL, NULL, NULL, NULL};
507
508                         mainart.draw= outliner_main_area_draw;
509                         mainart.free= outliner_main_area_free;
510
511                         ar->type= &mainart;
512
513                         WM_event_add_keymap_handler(&ar->handlers, &wm->uikeymap);
514                         WM_event_add_keymap_handler(&ar->handlers, &wm->view2dkeymap);
515                 }
516                 else if(ar->regiontype == RGN_TYPE_HEADER) {
517                         static ARegionType headerart={NULL, NULL, NULL, NULL, NULL};
518
519                         headerart.draw= outliner_header_area_draw;
520                         headerart.free= outliner_header_area_free;
521
522                         ar->type= &headerart;
523
524                         WM_event_add_keymap_handler(&ar->handlers, &wm->uikeymap);
525                         WM_event_add_keymap_handler(&ar->handlers, &wm->view2dkeymap);
526                 }
527                 else {
528                         static ARegionType headerart={NULL, NULL, NULL, NULL, NULL};
529                         ar->type= &headerart;
530                 }
531         }
532 }
533
534 /* spacetype; context changed */
535 static void outliner_refresh(bContext *C, ScrArea *sa)
536 {
537         
538 }
539
540 static SpaceLink *outliner_duplicate(SpaceLink *sl)
541 {
542         SpaceOops *soutliner= (SpaceOops *)sl;
543         SpaceOops *soutlinern= MEM_dupallocN(soutliner);
544
545         if(soutlinern->rnapath)
546                 soutlinern->rnapath= MEM_dupallocN(soutlinern->rnapath);
547         
548         return (SpaceLink *)soutlinern;
549 }
550
551 /* only called once, from screen/spacetypes.c */
552 void ED_spacetype_outliner(void)
553 {
554         static SpaceType st;
555         
556         st.spaceid= SPACE_OOPS;
557         
558         st.new= outliner_new;
559         st.free= outliner_free;
560         st.init= outliner_init;
561         st.refresh= outliner_refresh;
562         st.duplicate= outliner_duplicate;
563         st.operatortypes= outliner_operatortypes;
564         st.keymap= outliner_keymap;
565         
566         BKE_spacetype_register(&st);
567 }
568