Drag and drop 2.5 integration! Finally, slashdot regulars can use
[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_math.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_render.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->align= BUT_AUTO;
74
75         /* header */
76         ar= MEM_callocN(sizeof(ARegion), "header for buts");
77         
78         BLI_addtail(&sbuts->regionbase, ar);
79         ar->regiontype= RGN_TYPE_HEADER;
80         ar->alignment= RGN_ALIGN_BOTTOM;
81         
82 #if 0
83         /* context area */
84         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
85         BLI_addtail(&sbuts->regionbase, ar);
86         ar->regiontype= RGN_TYPE_CHANNELS;
87         ar->alignment= RGN_ALIGN_TOP;
88 #endif
89
90         /* main area */
91         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
92         
93         BLI_addtail(&sbuts->regionbase, ar);
94         ar->regiontype= RGN_TYPE_WINDOW;
95         
96         return (SpaceLink *)sbuts;
97 }
98
99 /* not spacelink itself */
100 static void buttons_free(SpaceLink *sl)
101 {       
102         SpaceButs *sbuts= (SpaceButs*) sl;
103         
104         if(sbuts->ri) { 
105                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
106                 MEM_freeN(sbuts->ri);
107         }
108
109         if(sbuts->path)
110                 MEM_freeN(sbuts->path);
111 }
112
113 /* spacetype; init callback */
114 static void buttons_init(struct wmWindowManager *wm, ScrArea *sa)
115 {
116         SpaceButs *sbuts= sa->spacedata.first;
117
118         /* auto-align based on size */
119         if(sbuts->align == BUT_AUTO || !sbuts->align) {
120                 if(sa->winx > sa->winy)
121                         sbuts->align= BUT_HORIZONTAL;
122                 else
123                         sbuts->align= BUT_VERTICAL;
124         }
125 }
126
127 static SpaceLink *buttons_duplicate(SpaceLink *sl)
128 {
129         SpaceButs *sbutsn= MEM_dupallocN(sl);
130         
131         /* clear or remove stuff from old */
132         sbutsn->ri= NULL;
133         sbutsn->path= NULL;
134         
135         return (SpaceLink *)sbutsn;
136 }
137
138 /* add handlers, stuff you only do once or on area/region changes */
139 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
140 {
141         wmKeyMap *keymap;
142
143         ED_region_panels_init(wm, ar);
144
145         keymap= WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
146         WM_event_add_keymap_handler(&ar->handlers, keymap);
147 }
148
149 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
150 {
151         /* draw entirely, view changes should be handled here */
152         SpaceButs *sbuts= CTX_wm_space_buts(C);
153         int vertical= (sbuts->align == BUT_VERTICAL);
154
155         buttons_context_compute(C, sbuts);
156
157         if(sbuts->mainb == BCONTEXT_SCENE)
158                 ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
159         else if(sbuts->mainb == BCONTEXT_RENDER)
160                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
161         else if(sbuts->mainb == BCONTEXT_WORLD)
162                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
163         else if(sbuts->mainb == BCONTEXT_OBJECT)
164                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
165         else if(sbuts->mainb == BCONTEXT_DATA)
166                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
167         else if(sbuts->mainb == BCONTEXT_MATERIAL)
168                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
169         else if(sbuts->mainb == BCONTEXT_TEXTURE)
170                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
171         else if(sbuts->mainb == BCONTEXT_PARTICLE)
172                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
173         else if(sbuts->mainb == BCONTEXT_PHYSICS)
174                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
175         else if(sbuts->mainb == BCONTEXT_BONE)
176                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
177         else if(sbuts->mainb == BCONTEXT_MODIFIER)
178                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
179         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
180                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
181         else if(sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
182                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
183
184     sbuts->re_align= 0;
185         sbuts->mainbo= sbuts->mainb;
186 }
187
188 void buttons_operatortypes(void)
189 {
190         WM_operatortype_append(BUTTONS_OT_toolbox);
191         WM_operatortype_append(BUTTONS_OT_file_browse);
192 }
193
194 void buttons_keymap(struct wmKeyConfig *keyconf)
195 {
196         wmKeyMap *keymap= WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
197         
198         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
199 }
200
201 //#define PY_HEADER
202 /* add handlers, stuff you only do once or on area/region changes */
203 static void buttons_header_area_init(wmWindowManager *wm, ARegion *ar)
204 {
205 #ifdef PY_HEADER
206         ED_region_header_init(ar);
207 #else
208         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
209 #endif
210 }
211
212 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
213 {
214 #ifdef PY_HEADER
215         ED_region_header(C, ar);
216 #else
217
218         float col[3];
219         
220         /* clear */
221         if(ED_screen_area_active(C))
222                 UI_GetThemeColor3fv(TH_HEADER, col);
223         else
224                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
225         
226         glClearColor(col[0], col[1], col[2], 0.0);
227         glClear(GL_COLOR_BUFFER_BIT);
228         
229         /* set view2d view matrix for scrolling (without scrollers) */
230         UI_view2d_view_ortho(C, &ar->v2d);
231         
232         buttons_header_buttons(C, ar);
233 #endif  
234
235         /* restore view matrix? */
236         UI_view2d_view_restore(C);
237 }
238
239 /* draw a certain button set only if properties area is currently
240  * showing that button set, to reduce unnecessary drawing. */
241 static void buttons_area_redraw(ScrArea *sa, short buttons)
242 {
243         SpaceButs *sbuts= sa->spacedata.first;
244         
245         /* if the area's current button set is equal to the one to redraw */
246         if(sbuts->mainb == buttons)
247                 ED_area_tag_redraw(sa);
248 }
249
250 /* reused! */
251 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
252 {
253         SpaceButs *sbuts= sa->spacedata.first;
254
255         /* context changes */
256         switch(wmn->category) {
257                 case NC_SCENE:
258                         switch(wmn->data) {
259                                 case ND_FRAME:
260                                 case ND_RENDER_OPTIONS:
261                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
262                                         break;
263                                 case ND_OB_ACTIVE:
264                                         ED_area_tag_redraw(sa);
265                                         sbuts->preview= 1;
266                                         break;
267                                 case ND_KEYINGSET:
268                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
269                                         break;
270                                 case ND_MODE:
271                                 case ND_LAYER:
272                                 default:
273                                         ED_area_tag_redraw(sa);
274                                         break;
275                         }
276                         break;
277                 case NC_OBJECT:
278                         switch(wmn->data) {
279                                 case ND_TRANSFORM:
280                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
281                                         break;
282                                 case ND_POSE:
283                                 case ND_BONE_ACTIVE:
284                                 case ND_BONE_SELECT:
285                                         buttons_area_redraw(sa, BCONTEXT_BONE);
286                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
287                                         break;
288                                 case ND_MODIFIER:
289                                         if(wmn->action == NA_RENAME)
290                                                 ED_area_tag_redraw(sa);
291                                         else
292                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
293                                         break;
294                                 case ND_CONSTRAINT:
295                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
296                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
297                                         break;
298                                 case ND_PARTICLE_DATA:
299                                         buttons_area_redraw(sa, BCONTEXT_PARTICLE);
300                                         break;
301                                 case ND_DRAW:
302                                 case ND_SHADING:
303                                 case ND_SHADING_DRAW:
304                                         /* currently works by redraws... if preview is set, it (re)starts job */
305                                         sbuts->preview= 1;
306                                         break;
307                                 default:
308                                         /* Not all object RNA props have a ND_ notifier (yet) */
309                                         ED_area_tag_redraw(sa);
310                                         break;
311                         }
312                         break;
313                 case NC_GEOM:
314                         switch(wmn->data) {
315                                 case ND_SELECT:
316                                 case ND_DATA:
317                                         ED_area_tag_redraw(sa);
318                                         break;
319                         }
320                         break;
321                 case NC_MATERIAL:
322                         ED_area_tag_redraw(sa);
323                         switch(wmn->data) {
324                                 case ND_SHADING:
325                                 case ND_SHADING_DRAW:
326                                 case ND_NODES:
327                                         /* currently works by redraws... if preview is set, it (re)starts job */
328                                         sbuts->preview= 1;
329                                         break;
330                         }                                       
331                         break;
332                 case NC_WORLD:
333                         buttons_area_redraw(sa, BCONTEXT_WORLD);
334                         sbuts->preview= 1;
335                         break;
336                 case NC_LAMP:
337                         buttons_area_redraw(sa, BCONTEXT_DATA);
338                         sbuts->preview= 1;
339                         break;
340                 case NC_BRUSH:
341                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
342                         sbuts->preview= 1;
343                         break;
344                 case NC_TEXTURE:
345                         ED_area_tag_redraw(sa);
346                         sbuts->preview= 1;
347                         break;
348                 case NC_SPACE:
349                         if(wmn->data == ND_SPACE_PROPERTIES)
350                                 ED_area_tag_redraw(sa);
351                         break;
352                 case NC_ID:
353                         if(wmn->action == NA_RENAME)
354                                 ED_area_tag_redraw(sa);
355                         break;
356                 case NC_ANIMATION:
357                         switch(wmn->data) {
358                                 case ND_KEYFRAME_EDIT:
359                                         ED_area_tag_redraw(sa);
360                                         break;
361                         }
362         }
363
364         if(wmn->data == ND_KEYS)
365                 ED_area_tag_redraw(sa);
366 }
367
368 /* only called once, from space/spacetypes.c */
369 void ED_spacetype_buttons(void)
370 {
371         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
372         ARegionType *art;
373         
374         st->spaceid= SPACE_BUTS;
375         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
376         
377         st->new= buttons_new;
378         st->free= buttons_free;
379         st->init= buttons_init;
380         st->duplicate= buttons_duplicate;
381         st->operatortypes= buttons_operatortypes;
382         st->keymap= buttons_keymap;
383         st->listener= buttons_area_listener;
384         st->context= buttons_context;
385         
386         /* regions: main window */
387         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
388         art->regionid = RGN_TYPE_WINDOW;
389         art->init= buttons_main_area_init;
390         art->draw= buttons_main_area_draw;
391         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
392         BLI_addhead(&st->regiontypes, art);
393
394         buttons_context_register(art);
395         
396         /* regions: header */
397         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
398         art->regionid = RGN_TYPE_HEADER;
399         art->prefsizey= BUTS_HEADERY;
400         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
401         
402         art->init= buttons_header_area_init;
403         art->draw= buttons_header_area_draw;
404         BLI_addhead(&st->regiontypes, art);
405
406         BKE_spacetype_register(st);
407 }
408