View2D - Initial commit of Pan-View Operator
[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
344         /* clear */
345         UI_GetThemeColor3fv(TH_BACK, col);
346         glClearColor(col[0], col[1], col[2], 0.0);
347         glClear(GL_COLOR_BUFFER_BIT);
348
349         // XXX width should be depend on max length of items (like height)...
350         awidth= width= ar->winrct.xmax - ar->winrct.xmin;
351         aheight= height= ar->winrct.ymax - ar->winrct.ymin;
352         
353         /* create table */
354         cell.space= soutliner;
355         cell.lastrow= -1;
356         RNA_main_pointer_create(G.main, &cell.ptr);
357         cell.prop= NULL;
358
359         /* solve RNA path or reset if fails */
360         if(soutliner->rnapath) {
361                 if(!RNA_path_resolve(&cell.ptr, soutliner->rnapath, &newptr, &prop)) {
362                         newptr.data= NULL;
363                         printf("RNA outliner: failed resolving path. (%s)\n", soutliner->rnapath);
364                 }
365
366                 if(newptr.data && newptr.type) {
367                         cell.ptr= newptr;
368                 }
369                 else {
370                         MEM_freeN(soutliner->rnapath);
371                         soutliner->rnapath= NULL;
372                 }
373         }
374
375         /* compute number of rows and columns */
376         rows= 1;
377         cols= 2;
378
379         iterprop= RNA_struct_iterator_property(&cell.ptr);
380         RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
381
382         for(; cell.iter.valid; RNA_property_collection_next(&cell.iter)) {
383                 prop= cell.iter.ptr.data;
384
385                 rows += 1 + RNA_property_array_length(&cell.ptr, prop);
386                 if(RNA_property_type(&cell.ptr, prop) == PROP_COLLECTION)
387                         rows += RNA_property_collection_length(&cell.ptr, prop);
388         }
389
390         RNA_property_collection_end(&cell.iter);
391
392         if ((rows*ROW_HEIGHT) > height)
393                 height= rows * ROW_HEIGHT;
394         
395         /* need to validate view2d after updating size of tot */
396         v2d->tot.xmin= 0;
397         v2d->tot.xmax= width;
398         v2d->tot.ymax= 0;
399         v2d->tot.ymin= -height;
400         UI_view2d_enforce_status(v2d, awidth, aheight);
401         
402         rct.xmin= 0;
403         rct.ymin= -height;
404         rct.xmax= width;
405         rct.ymax= 0;
406         
407         /* set matrix for 2d-view controls */
408         UI_view2d_ortho(C, v2d);
409         
410         /* create and draw table */
411         table= UI_table_create(rows, 2, &rct, rna_table_cell_func, &cell);
412
413         RNA_property_collection_begin(&cell.ptr, iterprop, &cell.iter);
414         UI_table_draw(C->window, ar, table);
415         RNA_property_collection_end(&cell.iter);
416
417         UI_table_free(table);
418 }
419
420 static void outliner_main_area_free(ARegion *ar)
421 {
422         uiFreeBlocks(&ar->uiblocks);
423 }
424
425 /* ************************ header outliner area region *********************** */
426
427 static void outliner_header_area_draw(const bContext *C, ARegion *ar)
428 {
429         SpaceOops *soutliner= C->area->spacedata.first;
430         float col[3];
431         int width, height;
432         rctf bbox;
433         char *path;
434
435         path= (soutliner->rnapath)? soutliner->rnapath: "Main";
436
437         /* clear */
438         UI_GetThemeColor3fv(TH_BACK, col);
439         glClearColor(MAX2(col[0]-0.3f, 0.0f), MAX2(col[1]-0.3f, 0.0f), MAX2(col[2]-0.3f, 0.0f), 0.0);
440         glClear(GL_COLOR_BUFFER_BIT);
441
442         width= ar->winrct.xmax - ar->winrct.xmin;
443         height= ar->winrct.ymax - ar->winrct.ymin;
444
445         /* header text */
446         UI_GetBoundingBox(UI_HELV, path, 0, &bbox);
447
448         glColor3f(1.0f, 1.0f, 1.0f);
449         UI_SetScale(1.0);
450         UI_RasterPos(0.5f*(width - (bbox.xmax - bbox.xmin)), 0.5f*(height - (bbox.ymax - bbox.ymin)));
451         UI_DrawString(UI_HELV, path, 0);
452 }
453
454 static void outliner_header_area_free(ARegion *ar)
455 {
456         uiFreeBlocks(&ar->uiblocks);
457 }
458
459 /* ******************** default callbacks for outliner space ***************** */
460
461 static SpaceLink *outliner_new(void)
462 {
463         SpaceOops *soutliner;
464
465         soutliner= MEM_callocN(sizeof(SpaceOops), "initoutliner");
466
467         return (SpaceLink*)soutliner;
468 }
469
470 /* not spacelink itself */
471 static void outliner_free(SpaceLink *sl)
472 {
473         SpaceOops *soutliner= (SpaceOops*)sl;
474
475         if(soutliner->rnapath) {
476                 MEM_freeN(soutliner->rnapath);
477                 soutliner->rnapath= NULL;
478         }
479 }
480
481 /* spacetype; init callback */
482 static void outliner_init(wmWindowManager *wm, ScrArea *sa)
483 {
484         ARegion *ar;
485         
486         /* link area to SpaceXXX struct */
487
488         /* add handlers to area */
489         /* define how many regions, the order and types */
490         
491         /* add types to regions */
492         for(ar= sa->regionbase.first; ar; ar= ar->next) {
493                 if(ar->regiontype == RGN_TYPE_WINDOW) {
494                         static ARegionType mainart={NULL, NULL, NULL, NULL, NULL};
495
496                         mainart.draw= outliner_main_area_draw;
497                         mainart.free= outliner_main_area_free;
498
499                         ar->type= &mainart;
500
501                         WM_event_add_keymap_handler(&ar->handlers, &wm->uikeymap);
502                         WM_event_add_keymap_handler(&ar->handlers, &wm->view2dkeymap);
503                 }
504                 else if(ar->regiontype == RGN_TYPE_HEADER) {
505                         static ARegionType headerart={NULL, NULL, NULL, NULL, NULL};
506
507                         headerart.draw= outliner_header_area_draw;
508                         headerart.free= outliner_header_area_free;
509
510                         ar->type= &headerart;
511
512                         WM_event_add_keymap_handler(&ar->handlers, &wm->uikeymap);
513                         WM_event_add_keymap_handler(&ar->handlers, &wm->view2dkeymap);
514                 }
515                 else {
516                         static ARegionType headerart={NULL, NULL, NULL, NULL, NULL};
517                         ar->type= &headerart;
518                 }
519         }
520 }
521
522 /* spacetype; context changed */
523 static void outliner_refresh(bContext *C, ScrArea *sa)
524 {
525         
526 }
527
528 static SpaceLink *outliner_duplicate(SpaceLink *sl)
529 {
530         SpaceOops *soutliner= (SpaceOops *)sl;
531         SpaceOops *soutlinern= MEM_dupallocN(soutliner);
532
533         if(soutlinern->rnapath)
534                 soutlinern->rnapath= MEM_dupallocN(soutlinern->rnapath);
535         
536         return (SpaceLink *)soutlinern;
537 }
538
539 /* only called once, from screen/spacetypes.c */
540 void ED_spacetype_outliner(void)
541 {
542         static SpaceType st;
543         
544         st.spaceid= SPACE_OOPS;
545         
546         st.new= outliner_new;
547         st.free= outliner_free;
548         st.init= outliner_init;
549         st.refresh= outliner_refresh;
550         st.duplicate= outliner_duplicate;
551         st.operatortypes= outliner_operatortypes;
552         st.keymap= outliner_keymap;
553         
554         BKE_spacetype_register(&st);
555 }
556