2.5 - Constraints Editing + Keyframe Drawing Tweaks
[blender-staging.git] / source / blender / editors / space_buttons / space_buttons.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_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_userdef_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_colortools.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47
48 #include "ED_space_api.h"
49 #include "ED_screen.h"
50
51 #include "BIF_gl.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58 #include "UI_view2d.h"
59
60 #include "ED_previewrender.h"
61
62 #include "buttons_intern.h"     // own include
63
64 /* ******************** default callbacks for buttons space ***************** */
65
66 static SpaceLink *buttons_new(const bContext *C)
67 {
68         ARegion *ar;
69         SpaceButs *sbuts;
70         
71         sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
72         sbuts->spacetype= SPACE_BUTS;
73         sbuts->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
74         sbuts->align= BUT_AUTO;
75
76         /* header */
77         ar= MEM_callocN(sizeof(ARegion), "header for buts");
78         
79         BLI_addtail(&sbuts->regionbase, ar);
80         ar->regiontype= RGN_TYPE_HEADER;
81         ar->alignment= RGN_ALIGN_BOTTOM;
82         
83 #if 0
84         /* context area */
85         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
86         BLI_addtail(&sbuts->regionbase, ar);
87         ar->regiontype= RGN_TYPE_CHANNELS;
88         ar->alignment= RGN_ALIGN_TOP;
89 #endif
90
91         /* main area */
92         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
93         
94         BLI_addtail(&sbuts->regionbase, ar);
95         ar->regiontype= RGN_TYPE_WINDOW;
96         
97         return (SpaceLink *)sbuts;
98 }
99
100 /* not spacelink itself */
101 static void buttons_free(SpaceLink *sl)
102 {       
103         SpaceButs *sbuts= (SpaceButs*) sl;
104         
105         if(sbuts->ri) { 
106                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
107                 MEM_freeN(sbuts->ri);
108         }
109
110         if(sbuts->path)
111                 MEM_freeN(sbuts->path);
112 }
113
114 /* spacetype; init callback */
115 static void buttons_init(struct wmWindowManager *wm, ScrArea *sa)
116 {
117         SpaceButs *sbuts= sa->spacedata.first;
118
119         /* auto-align based on size */
120         if(sbuts->align == BUT_AUTO || !sbuts->align) {
121                 if(sa->winx > sa->winy)
122                         sbuts->align= BUT_HORIZONTAL;
123                 else
124                         sbuts->align= BUT_VERTICAL;
125         }
126 }
127
128 static SpaceLink *buttons_duplicate(SpaceLink *sl)
129 {
130         SpaceButs *sbutsn= MEM_dupallocN(sl);
131         
132         /* clear or remove stuff from old */
133         sbutsn->ri= NULL;
134         sbutsn->path= NULL;
135         
136         return (SpaceLink *)sbutsn;
137 }
138
139 /* add handlers, stuff you only do once or on area/region changes */
140 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
141 {
142         ED_region_panels_init(wm, ar);
143 }
144
145 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
146 {
147         /* draw entirely, view changes should be handled here */
148         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
149         int vertical= (sbuts->align == BUT_VERTICAL);
150
151         buttons_context_compute(C, sbuts);
152
153         if(sbuts->mainb == BCONTEXT_SCENE)
154                 ED_region_panels(C, ar, vertical, "scene");
155         else if(sbuts->mainb == BCONTEXT_WORLD)
156                 ED_region_panels(C, ar, vertical, "world");
157         else if(sbuts->mainb == BCONTEXT_OBJECT)
158                 ED_region_panels(C, ar, vertical, "object");
159         else if(sbuts->mainb == BCONTEXT_DATA)
160                 ED_region_panels(C, ar, vertical, "data");
161         else if(sbuts->mainb == BCONTEXT_MATERIAL)
162                 ED_region_panels(C, ar, vertical, "material");
163         else if(sbuts->mainb == BCONTEXT_TEXTURE)
164                 ED_region_panels(C, ar, vertical, "texture");
165         else if(sbuts->mainb == BCONTEXT_PARTICLE)
166                 ED_region_panels(C, ar, vertical, "particle");
167         else if(sbuts->mainb == BCONTEXT_PHYSICS)
168                 ED_region_panels(C, ar, vertical, "physics");
169         else if(sbuts->mainb == BCONTEXT_BONE)
170                 ED_region_panels(C, ar, vertical, "bone");
171         else if(sbuts->mainb == BCONTEXT_MODIFIER)
172                 ED_region_panels(C, ar, vertical, "modifier");
173         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
174                 ED_region_panels(C, ar, vertical, "constraint");
175
176     sbuts->re_align= 0;
177         sbuts->mainbo= sbuts->mainb;
178 }
179
180 void buttons_operatortypes(void)
181 {
182         WM_operatortype_append(OBJECT_OT_group_add);
183         WM_operatortype_append(OBJECT_OT_group_remove);
184
185         WM_operatortype_append(OBJECT_OT_material_slot_add);
186         WM_operatortype_append(OBJECT_OT_material_slot_remove);
187         WM_operatortype_append(OBJECT_OT_material_slot_assign);
188         WM_operatortype_append(OBJECT_OT_material_slot_select);
189         WM_operatortype_append(OBJECT_OT_material_slot_deselect);
190
191         WM_operatortype_append(MATERIAL_OT_new);
192         WM_operatortype_append(TEXTURE_OT_new);
193         WM_operatortype_append(WORLD_OT_new);
194
195         WM_operatortype_append(OBJECT_OT_particle_system_add);
196         WM_operatortype_append(OBJECT_OT_particle_system_remove);
197
198         WM_operatortype_append(PARTICLE_OT_new);
199         WM_operatortype_append(PARTICLE_OT_new_keyed_target);
200         WM_operatortype_append(PARTICLE_OT_remove_keyed_target);
201         WM_operatortype_append(PARTICLE_OT_keyed_target_move_up);
202         WM_operatortype_append(PARTICLE_OT_keyed_target_move_down);
203
204         WM_operatortype_append(SCENE_OT_render_layer_add);
205         WM_operatortype_append(SCENE_OT_render_layer_remove);
206 }
207
208 void buttons_keymap(struct wmWindowManager *wm)
209 {
210         
211 }
212
213 //#define PY_HEADER
214 /* add handlers, stuff you only do once or on area/region changes */
215 static void buttons_header_area_init(wmWindowManager *wm, ARegion *ar)
216 {
217 #ifdef PY_HEADER
218         ED_region_header_init(ar);
219 #else
220         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
221 #endif
222 }
223
224 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
225 {
226 #ifdef PY_HEADER
227         ED_region_header(C, ar);
228 #else
229
230         float col[3];
231         
232         /* clear */
233         if(ED_screen_area_active(C))
234                 UI_GetThemeColor3fv(TH_HEADER, col);
235         else
236                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
237         
238         glClearColor(col[0], col[1], col[2], 0.0);
239         glClear(GL_COLOR_BUFFER_BIT);
240         
241         /* set view2d view matrix for scrolling (without scrollers) */
242         UI_view2d_view_ortho(C, &ar->v2d);
243         
244         buttons_header_buttons(C, ar);
245 #endif  
246
247         /* restore view matrix? */
248         UI_view2d_view_restore(C);
249 }
250
251 #if 0
252 /* add handlers, stuff you only do once or on area/region changes */
253 static void buttons_context_area_init(wmWindowManager *wm, ARegion *ar)
254 {
255         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
256 }
257
258 #define CONTEXTY        30
259
260 static void buttons_context_area_draw(const bContext *C, ARegion *ar)
261 {
262         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
263         uiStyle *style= U.uistyles.first;
264         uiBlock *block;
265         uiLayout *layout;
266         View2D *v2d= &ar->v2d;
267         float col[3];
268         int x, y, w, h;
269
270         buttons_context_compute(C, sbuts);
271
272         w= v2d->cur.xmax - v2d->cur.xmin;
273         h= v2d->cur.ymax - v2d->cur.ymin;
274         UI_view2d_view_ortho(C, v2d);
275
276         /* create UI */
277         block= uiBeginBlock(C, ar, "buttons_context", UI_EMBOSS);
278         layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_PANEL,
279                 style->panelspace, h - (h-UI_UNIT_Y)/2, w, 20, style);
280
281         buttons_context_draw(C, layout);
282
283         uiBlockLayoutResolve(C, block, &x, &y);
284         uiEndBlock(C, block);
285
286         /* draw */
287         UI_SetTheme(SPACE_BUTS, RGN_TYPE_WINDOW); /* XXX */
288
289         UI_GetThemeColor3fv(TH_BACK, col);
290         glClearColor(col[0], col[1], col[2], 0.0);
291         glClear(GL_COLOR_BUFFER_BIT);
292
293         UI_view2d_totRect_set(v2d, x, -y);
294         UI_view2d_view_ortho(C, v2d);
295
296         uiDrawBlock(C, block);
297
298         /* restore view matrix */
299         UI_view2d_view_restore(C);
300 }
301 #endif
302
303 /* reused! */
304 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
305 {
306         SpaceButs *sbuts= sa->spacedata.first;
307
308         /* context changes */
309         switch(wmn->category) {
310                 case NC_SCENE:
311                         switch(wmn->data) {
312                                 case ND_FRAME:
313                                 case ND_MODE:
314                                         ED_area_tag_redraw(sa);
315                                         break;
316                                         
317                                 case ND_OB_ACTIVE:
318                                         ED_area_tag_redraw(sa);
319                                         sbuts->preview= 1;
320                                         break;
321                         }
322                         break;
323                 case NC_OBJECT:
324                         switch(wmn->data) {
325                                 case ND_TRANSFORM:
326                                 case ND_BONE_ACTIVE:
327                                 case ND_BONE_SELECT:
328                                 case ND_GEOM_SELECT:
329                                 case ND_CONSTRAINT:
330                                         ED_area_tag_redraw(sa);
331                                         break;
332                                 case ND_SHADING:
333                                 case ND_SHADING_DRAW:
334                                         /* currently works by redraws... if preview is set, it (re)starts job */
335                                         sbuts->preview= 1;
336                                         break;
337                         }
338                         break;
339                 case NC_MATERIAL:
340                         ED_area_tag_redraw(sa);
341                         
342                         switch(wmn->data) {
343                                 case ND_SHADING:
344                                 case ND_SHADING_DRAW:
345                                         /* currently works by redraws... if preview is set, it (re)starts job */
346                                         sbuts->preview= 1;
347                                         break;
348                         }                                       
349                         break;
350                 case NC_WORLD:
351                         ED_area_tag_redraw(sa);
352                         sbuts->preview= 1;
353                 case NC_LAMP:
354                         ED_area_tag_redraw(sa);
355                         sbuts->preview= 1;
356                 case NC_TEXTURE:
357                         ED_area_tag_redraw(sa);
358                         sbuts->preview= 1;
359         }
360
361         if(wmn->data == ND_KEYS)
362                 ED_area_tag_redraw(sa);
363 }
364
365 /* only called once, from space/spacetypes.c */
366 void ED_spacetype_buttons(void)
367 {
368         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
369         ARegionType *art;
370         
371         st->spaceid= SPACE_BUTS;
372         
373         st->new= buttons_new;
374         st->free= buttons_free;
375         st->init= buttons_init;
376         st->duplicate= buttons_duplicate;
377         st->operatortypes= buttons_operatortypes;
378         st->keymap= buttons_keymap;
379         st->listener= buttons_area_listener;
380         st->context= buttons_context;
381         
382         /* regions: main window */
383         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
384         art->regionid = RGN_TYPE_WINDOW;
385         art->init= buttons_main_area_init;
386         art->draw= buttons_main_area_draw;
387         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
388         BLI_addhead(&st->regiontypes, art);
389
390         buttons_context_register(art);
391         
392         /* regions: header */
393         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
394         art->regionid = RGN_TYPE_HEADER;
395         art->minsizey= HEADERY;
396         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
397         
398         art->init= buttons_header_area_init;
399         art->draw= buttons_header_area_draw;
400         BLI_addhead(&st->regiontypes, art);
401
402 #if 0
403         /* regions: context */
404         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
405         art->regionid = RGN_TYPE_CHANNELS;
406         art->minsizey= CONTEXTY;
407         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
408         art->init= buttons_context_area_init;
409         art->draw= buttons_context_area_draw;;
410         art->listener= buttons_area_listener;
411         
412         BLI_addhead(&st->regiontypes, art);
413 #endif
414         
415         BKE_spacetype_register(st);
416 }
417