b01f653837cffd839fd47443f08313332fd73785
[blender-staging.git] / source / blender / editors / space_buttons / space_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_buttons/space_buttons.c
28  *  \ingroup spbuttons
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_utildefines.h"
40
41 #include "BKE_context.h"
42 #include "BKE_screen.h"
43
44 #include "ED_space_api.h"
45 #include "ED_screen.h"
46
47 #include "BIF_gl.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "UI_resources.h"
53 #include "UI_view2d.h"
54
55 #include "ED_render.h"
56
57 #include "buttons_intern.h"  /* own include */
58
59 /* ******************** default callbacks for buttons space ***************** */
60
61 static SpaceLink *buttons_new(const bContext *UNUSED(C))
62 {
63         ARegion *ar;
64         SpaceButs *sbuts;
65         
66         sbuts = MEM_callocN(sizeof(SpaceButs), "initbuts");
67         sbuts->spacetype = SPACE_BUTS;
68         sbuts->align = BUT_VERTICAL;
69
70         /* header */
71         ar = MEM_callocN(sizeof(ARegion), "header for buts");
72         
73         BLI_addtail(&sbuts->regionbase, ar);
74         ar->regiontype = RGN_TYPE_HEADER;
75         ar->alignment = RGN_ALIGN_TOP;
76         
77 #if 0
78         /* context area */
79         ar = MEM_callocN(sizeof(ARegion), "context area for buts");
80         BLI_addtail(&sbuts->regionbase, ar);
81         ar->regiontype = RGN_TYPE_CHANNELS;
82         ar->alignment = RGN_ALIGN_TOP;
83 #endif
84
85         /* main area */
86         ar = MEM_callocN(sizeof(ARegion), "main area for buts");
87         
88         BLI_addtail(&sbuts->regionbase, ar);
89         ar->regiontype = RGN_TYPE_WINDOW;
90
91         return (SpaceLink *)sbuts;
92 }
93
94 /* not spacelink itself */
95 static void buttons_free(SpaceLink *sl)
96 {       
97         SpaceButs *sbuts = (SpaceButs *) sl;
98
99         if (sbuts->path)
100                 MEM_freeN(sbuts->path);
101         
102         if (sbuts->texuser) {
103                 ButsContextTexture *ct = sbuts->texuser;
104                 BLI_freelistN(&ct->users);
105                 MEM_freeN(ct);
106         }
107 }
108
109 /* spacetype; init callback */
110 static void buttons_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
111 {
112         SpaceButs *sbuts = sa->spacedata.first;
113
114         /* auto-align based on size */
115         if (sbuts->align == BUT_AUTO || !sbuts->align) {
116                 if (sa->winx > sa->winy)
117                         sbuts->align = BUT_HORIZONTAL;
118                 else
119                         sbuts->align = BUT_VERTICAL;
120         }
121 }
122
123 static SpaceLink *buttons_duplicate(SpaceLink *sl)
124 {
125         SpaceButs *sbutsn = MEM_dupallocN(sl);
126         
127         /* clear or remove stuff from old */
128         sbutsn->path = NULL;
129         sbutsn->texuser = NULL;
130         
131         return (SpaceLink *)sbutsn;
132 }
133
134 /* add handlers, stuff you only do once or on area/region changes */
135 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
136 {
137         wmKeyMap *keymap;
138
139         ED_region_panels_init(wm, ar);
140
141         keymap = WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
142         WM_event_add_keymap_handler(&ar->handlers, keymap);
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 = CTX_wm_space_buts(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", sbuts->mainb);
155         else if (sbuts->mainb == BCONTEXT_RENDER)
156                 ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
157         else if (sbuts->mainb == BCONTEXT_RENDER_LAYER)
158                 ED_region_panels(C, ar, vertical, "render_layer", sbuts->mainb);
159         else if (sbuts->mainb == BCONTEXT_WORLD)
160                 ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
161         else if (sbuts->mainb == BCONTEXT_OBJECT)
162                 ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
163         else if (sbuts->mainb == BCONTEXT_DATA)
164                 ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
165         else if (sbuts->mainb == BCONTEXT_MATERIAL)
166                 ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
167         else if (sbuts->mainb == BCONTEXT_TEXTURE)
168                 ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
169         else if (sbuts->mainb == BCONTEXT_PARTICLE)
170                 ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
171         else if (sbuts->mainb == BCONTEXT_PHYSICS)
172                 ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
173         else if (sbuts->mainb == BCONTEXT_BONE)
174                 ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
175         else if (sbuts->mainb == BCONTEXT_MODIFIER)
176                 ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
177         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
178                 ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
179         else if (sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
180                 ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
181
182         sbuts->re_align = 0;
183         sbuts->mainbo = sbuts->mainb;
184 }
185
186 static void buttons_operatortypes(void)
187 {
188         WM_operatortype_append(BUTTONS_OT_toolbox);
189         WM_operatortype_append(BUTTONS_OT_file_browse);
190         WM_operatortype_append(BUTTONS_OT_directory_browse);
191 }
192
193 static void buttons_keymap(struct wmKeyConfig *keyconf)
194 {
195         wmKeyMap *keymap = WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
196         
197         WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
198 }
199
200 /* add handlers, stuff you only do once or on area/region changes */
201 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
202 {
203         ED_region_header_init(ar);
204 }
205
206 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
207 {
208         SpaceButs *sbuts = CTX_wm_space_buts(C);
209
210         /* Needed for RNA to get the good values! */
211         buttons_context_compute(C, sbuts);
212
213         ED_region_header(C, ar);
214 }
215
216 /* draw a certain button set only if properties area is currently
217  * showing that button set, to reduce unnecessary drawing. */
218 static void buttons_area_redraw(ScrArea *sa, short buttons)
219 {
220         SpaceButs *sbuts = sa->spacedata.first;
221         
222         /* if the area's current button set is equal to the one to redraw */
223         if (sbuts->mainb == buttons)
224                 ED_area_tag_redraw(sa);
225 }
226
227 /* reused! */
228 static void buttons_area_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn)
229 {
230         SpaceButs *sbuts = sa->spacedata.first;
231
232         /* context changes */
233         switch (wmn->category) {
234                 case NC_SCENE:
235                         switch (wmn->data) {
236                                 case ND_RENDER_OPTIONS:
237                                         buttons_area_redraw(sa, BCONTEXT_RENDER);
238                                         buttons_area_redraw(sa, BCONTEXT_RENDER_LAYER);
239                                         break;
240                                 case ND_FRAME:
241                                         /* any buttons area can have animated properties so redraw all */
242                                         ED_area_tag_redraw(sa);
243                                         sbuts->preview = 1;
244                                         break;
245                                 case ND_OB_ACTIVE:
246                                         ED_area_tag_redraw(sa);
247                                         sbuts->preview = 1;
248                                         break;
249                                 case ND_KEYINGSET:
250                                         buttons_area_redraw(sa, BCONTEXT_SCENE);
251                                         break;
252                                 case ND_RENDER_RESULT:
253                                         break;
254                                 case ND_MODE:
255                                 case ND_LAYER:
256                                 default:
257                                         ED_area_tag_redraw(sa);
258                                         break;
259                         }
260                         break;
261                 case NC_OBJECT:
262                         switch (wmn->data) {
263                                 case ND_TRANSFORM:
264                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
265                                         buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
266                                         break;
267                                 case ND_POSE:
268                                         buttons_area_redraw(sa, BCONTEXT_DATA);
269                                 case ND_BONE_ACTIVE:
270                                 case ND_BONE_SELECT:
271                                         buttons_area_redraw(sa, BCONTEXT_BONE);
272                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
273                                         buttons_area_redraw(sa, BCONTEXT_DATA);
274                                         break;
275                                 case ND_MODIFIER:
276                                         if (wmn->action == NA_RENAME)
277                                                 ED_area_tag_redraw(sa);
278                                         else
279                                                 buttons_area_redraw(sa, BCONTEXT_MODIFIER);
280                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
281                                         break;
282                                 case ND_CONSTRAINT:
283                                         buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
284                                         buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
285                                         break;
286                                 case ND_PARTICLE:
287                                         if (wmn->action == NA_EDITED)
288                                                 buttons_area_redraw(sa, BCONTEXT_PARTICLE);
289                                         sbuts->preview = 1;
290                                         break;
291                                 case ND_DRAW:
292                                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
293                                         buttons_area_redraw(sa, BCONTEXT_DATA);
294                                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
295                                 case ND_SHADING:
296                                 case ND_SHADING_DRAW:
297                                 case ND_SHADING_LINKS:
298                                 case ND_SHADING_PREVIEW:
299                                         /* currently works by redraws... if preview is set, it (re)starts job */
300                                         sbuts->preview = 1;
301                                         break;
302                                 default:
303                                         /* Not all object RNA props have a ND_ notifier (yet) */
304                                         ED_area_tag_redraw(sa);
305                                         break;
306                         }
307                         break;
308                 case NC_GEOM:
309                         switch (wmn->data) {
310                                 case ND_SELECT:
311                                 case ND_DATA:
312                                         ED_area_tag_redraw(sa);
313                                         break;
314                         }
315                         break;
316                 case NC_MATERIAL:
317                         ED_area_tag_redraw(sa);
318                         switch (wmn->data) {
319                                 case ND_SHADING:
320                                 case ND_SHADING_DRAW:
321                                 case ND_SHADING_LINKS:
322                                 case ND_SHADING_PREVIEW:
323                                 case ND_NODES:
324                                         /* currently works by redraws... if preview is set, it (re)starts job */
325                                         sbuts->preview = 1;
326                                         break;
327                         }
328                         break;
329                 case NC_WORLD:
330                         buttons_area_redraw(sa, BCONTEXT_WORLD);
331                         sbuts->preview = 1;
332                         break;
333                 case NC_LAMP:
334                         buttons_area_redraw(sa, BCONTEXT_DATA);
335                         sbuts->preview = 1;
336                         break;
337                 case NC_GROUP:
338                         buttons_area_redraw(sa, BCONTEXT_OBJECT);
339                         break;
340                 case NC_BRUSH:
341                         buttons_area_redraw(sa, BCONTEXT_TEXTURE);
342                         sbuts->preview = 1;
343                         break;
344                 case NC_TEXTURE:
345                 case NC_IMAGE:
346                         if (wmn->action != NA_PAINTING) {
347                                 ED_area_tag_redraw(sa);
348                                 sbuts->preview = 1;
349                         }
350                         break;
351                 case NC_SPACE:
352                         if (wmn->data == ND_SPACE_PROPERTIES)
353                                 ED_area_tag_redraw(sa);
354                         break;
355                 case NC_ID:
356                         if (wmn->action == NA_RENAME)
357                                 ED_area_tag_redraw(sa);
358                         break;
359                 case NC_ANIMATION:
360                         switch (wmn->data) {
361                                 case ND_KEYFRAME:
362                                         if (wmn->action == NA_EDITED)
363                                                 ED_area_tag_redraw(sa);
364                                         break;
365                         }
366                         break;
367                 case NC_NODE:
368                         if (wmn->action == NA_SELECTED) {
369                                 ED_area_tag_redraw(sa);
370                                 /* new active node, update texture preview */
371                                 if (sbuts->mainb == BCONTEXT_TEXTURE)
372                                         sbuts->preview = 1;
373                         }
374                         break;
375                 /* Listener for preview render, when doing an global undo. */
376                 case NC_WINDOW:
377                         ED_area_tag_redraw(sa);
378                         sbuts->preview = 1;
379                         break;
380 #ifdef WITH_FREESTYLE
381                 case NC_LINESTYLE:
382                         ED_area_tag_redraw(sa);
383                         sbuts->preview = 1;
384                         break;
385 #endif
386         }
387
388         if (wmn->data == ND_KEYS)
389                 ED_area_tag_redraw(sa);
390 }
391
392 /* only called once, from space/spacetypes.c */
393 void ED_spacetype_buttons(void)
394 {
395         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype buttons");
396         ARegionType *art;
397         
398         st->spaceid = SPACE_BUTS;
399         strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
400         
401         st->new = buttons_new;
402         st->free = buttons_free;
403         st->init = buttons_init;
404         st->duplicate = buttons_duplicate;
405         st->operatortypes = buttons_operatortypes;
406         st->keymap = buttons_keymap;
407         st->listener = buttons_area_listener;
408         st->context = buttons_context;
409         
410         /* regions: main window */
411         art = MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
412         art->regionid = RGN_TYPE_WINDOW;
413         art->init = buttons_main_area_init;
414         art->draw = buttons_main_area_draw;
415         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
416         BLI_addhead(&st->regiontypes, art);
417
418         buttons_context_register(art);
419         
420         /* regions: header */
421         art = MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
422         art->regionid = RGN_TYPE_HEADER;
423         art->prefsizey = HEADERY;
424         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
425         
426         art->init = buttons_header_area_init;
427         art->draw = buttons_header_area_draw;
428         BLI_addhead(&st->regiontypes, art);
429
430         BKE_spacetype_register(st);
431 }
432