patch [#20342] Notifier patch for modifier rename and particle system rename
[blender.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, "Buttons Generic", 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, "Buttons Generic", 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 /* reused! */
240 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
241 {
242         SpaceButs *sbuts= sa->spacedata.first;
243
244         /* context changes */
245         switch(wmn->category) {
246                 case NC_SCENE:
247                         /* lazy general redraw tag here, in case more than 1 propertie window is opened 
248                         Not all RNA props have a ND_sub notifier(yet) */
249                         ED_area_tag_redraw(sa);
250                         switch(wmn->data) {
251                                 case ND_FRAME:
252                                 case ND_MODE:
253                                 case ND_RENDER_OPTIONS:
254                                 case ND_KEYINGSET:
255                                 case ND_LAYER:
256                                         ED_area_tag_redraw(sa);
257                                         break;
258                                         
259                                 case ND_OB_ACTIVE:
260                                         ED_area_tag_redraw(sa);
261                                         sbuts->preview= 1;
262                                         break;
263                         }
264                         break;
265                 case NC_OBJECT:
266                         ED_area_tag_redraw(sa);
267                         /* lazy general redraw tag here, in case more than 1 propertie window is opened 
268                         Not all RNA props have a ND_ notifier(yet) */
269                         switch(wmn->data) {
270                                 case ND_TRANSFORM:
271                                 case ND_BONE_ACTIVE:
272                                 case ND_BONE_SELECT:
273                                 case ND_MODIFIER:
274                                         if(wmn->action == NA_RENAME)
275                                                 ED_area_tag_redraw(sa);
276                                         break;
277                                 case ND_CONSTRAINT:
278                                         ED_area_tag_redraw(sa);
279                                         break;
280                                 case ND_DRAW:
281                                 case ND_SHADING:
282                                 case ND_SHADING_DRAW:
283                                         /* currently works by redraws... if preview is set, it (re)starts job */
284                                         sbuts->preview= 1;
285                                         break;
286                         }
287                         break;
288                 case NC_GEOM:
289                         switch(wmn->data) {
290                                 case ND_SELECT:
291                                 case ND_DATA:
292                                         ED_area_tag_redraw(sa);
293                                         break;
294                         }
295                         break;
296                 case NC_MATERIAL:
297                         ED_area_tag_redraw(sa);
298                         switch(wmn->data) {
299                                 case ND_SHADING:
300                                 case ND_SHADING_DRAW:
301                                         /* currently works by redraws... if preview is set, it (re)starts job */
302                                         sbuts->preview= 1;
303                                         break;
304                         }                                       
305                         break;
306                 case NC_WORLD:
307                 case NC_LAMP:
308                 case NC_TEXTURE:
309                         ED_area_tag_redraw(sa);
310                         sbuts->preview= 1;
311                         break;
312                 case NC_SPACE:
313                         if(wmn->data == ND_SPACE_PROPERTIES)
314                                 ED_area_tag_redraw(sa);
315                         break;
316                 case NC_ID:
317                         if(wmn->action == NA_RENAME)
318                                 ED_area_tag_redraw(sa);
319                         break;
320         }
321
322         if(wmn->data == ND_KEYS)
323                 ED_area_tag_redraw(sa);
324 }
325
326 /* only called once, from space/spacetypes.c */
327 void ED_spacetype_buttons(void)
328 {
329         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
330         ARegionType *art;
331         
332         st->spaceid= SPACE_BUTS;
333         
334         st->new= buttons_new;
335         st->free= buttons_free;
336         st->init= buttons_init;
337         st->duplicate= buttons_duplicate;
338         st->operatortypes= buttons_operatortypes;
339         st->keymap= buttons_keymap;
340         st->listener= buttons_area_listener;
341         st->context= buttons_context;
342         
343         /* regions: main window */
344         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
345         art->regionid = RGN_TYPE_WINDOW;
346         art->init= buttons_main_area_init;
347         art->draw= buttons_main_area_draw;
348         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
349         BLI_addhead(&st->regiontypes, art);
350
351         buttons_context_register(art);
352         
353         /* regions: header */
354         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
355         art->regionid = RGN_TYPE_HEADER;
356         art->minsizey= BUTS_HEADERY;
357         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
358         
359         art->init= buttons_header_area_init;
360         art->draw= buttons_header_area_draw;
361         BLI_addhead(&st->regiontypes, art);
362
363         BKE_spacetype_register(st);
364 }
365