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