Fix T53810: Crash removing a scene used in render
[blender-staging.git] / source / blender / editors / space_image / space_image.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_image/space_image.c
28  *  \ingroup spimage
29  */
30
31 #include "DNA_gpencil_types.h"
32 #include "DNA_mesh_types.h"
33 #include "DNA_mask_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_image_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_threads.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_image.h"
48 #include "BKE_library.h"
49 #include "BKE_scene.h"
50 #include "BKE_screen.h"
51
52 #include "IMB_imbuf_types.h"
53
54 #include "ED_image.h"
55 #include "ED_mask.h"
56 #include "ED_mesh.h"
57 #include "ED_node.h"
58 #include "ED_render.h"
59 #include "ED_space_api.h"
60 #include "ED_screen.h"
61 #include "ED_uvedit.h"
62
63 #include "BIF_gl.h"
64
65 #include "RNA_access.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "UI_resources.h"
71 #include "UI_interface.h"
72 #include "UI_view2d.h"
73
74 #include "image_intern.h"
75
76 /**************************** common state *****************************/
77
78 static void image_scopes_tag_refresh(ScrArea *sa)
79 {
80         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
81         ARegion *ar;
82
83         /* only while histogram is visible */
84         for (ar = sa->regionbase.first; ar; ar = ar->next) {
85                 if (ar->regiontype == RGN_TYPE_TOOLS && ar->flag & RGN_FLAG_HIDDEN)
86                         return;
87         }
88
89         sima->scopes.ok = 0;
90 }
91
92 static void image_user_refresh_scene(const bContext *C, SpaceImage *sima)
93 {
94         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT) {
95                 /* for render result, try to use the currently rendering scene */
96                 Scene *render_scene = ED_render_job_get_current_scene(C);
97                 if (render_scene) {
98                         sima->iuser.scene = render_scene;
99                         return;
100                 }
101         }
102         sima->iuser.scene = CTX_data_scene(C);
103 }
104
105 /* ******************** manage regions ********************* */
106
107 ARegion *image_has_buttons_region(ScrArea *sa)
108 {
109         ARegion *ar, *arnew;
110
111         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
112         if (ar) return ar;
113         
114         /* add subdiv level; after header */
115         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
116
117         /* is error! */
118         if (ar == NULL) return NULL;
119         
120         arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
121         
122         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
123         arnew->regiontype = RGN_TYPE_UI;
124         arnew->alignment = RGN_ALIGN_RIGHT;
125         
126         arnew->flag = RGN_FLAG_HIDDEN;
127         
128         return arnew;
129 }
130
131 ARegion *image_has_tools_region(ScrArea *sa)
132 {
133         ARegion *ar, *arnew;
134
135         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
136         if (ar) return ar;
137
138         /* add subdiv level; after buttons */
139         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
140
141         /* is error! */
142         if (ar == NULL) return NULL;
143         
144         arnew = MEM_callocN(sizeof(ARegion), "scopes for image");
145         
146         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
147         arnew->regiontype = RGN_TYPE_TOOLS;
148         arnew->alignment = RGN_ALIGN_LEFT;
149         
150         arnew->flag = RGN_FLAG_HIDDEN;
151
152         image_scopes_tag_refresh(sa);
153         
154         return arnew;
155 }
156
157 /* ******************** default callbacks for image space ***************** */
158
159 static SpaceLink *image_new(const bContext *UNUSED(C))
160 {
161         ARegion *ar;
162         SpaceImage *simage;
163         
164         simage = MEM_callocN(sizeof(SpaceImage), "initimage");
165         simage->spacetype = SPACE_IMAGE;
166         simage->zoom = 1.0f;
167         simage->lock = true;
168         simage->flag = SI_SHOW_GPENCIL | SI_USE_ALPHA;
169
170         simage->iuser.ok = true;
171         simage->iuser.fie_ima = 2;
172         simage->iuser.frames = 100;
173         simage->iuser.flag = IMA_SHOW_STEREO;
174
175         scopes_new(&simage->scopes);
176         simage->sample_line_hist.height = 100;
177
178         /* header */
179         ar = MEM_callocN(sizeof(ARegion), "header for image");
180         
181         BLI_addtail(&simage->regionbase, ar);
182         ar->regiontype = RGN_TYPE_HEADER;
183         ar->alignment = RGN_ALIGN_BOTTOM;
184         
185         /* buttons/list view */
186         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
187         
188         BLI_addtail(&simage->regionbase, ar);
189         ar->regiontype = RGN_TYPE_UI;
190         ar->alignment = RGN_ALIGN_RIGHT;
191         ar->flag = RGN_FLAG_HIDDEN;
192         
193         /* scopes/uv sculpt/paint */
194         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
195         
196         BLI_addtail(&simage->regionbase, ar);
197         ar->regiontype = RGN_TYPE_TOOLS;
198         ar->alignment = RGN_ALIGN_LEFT;
199         ar->flag = RGN_FLAG_HIDDEN;
200
201         /* main area */
202         ar = MEM_callocN(sizeof(ARegion), "main area for image");
203         
204         BLI_addtail(&simage->regionbase, ar);
205         ar->regiontype = RGN_TYPE_WINDOW;
206         
207         return (SpaceLink *)simage;
208 }
209
210 /* not spacelink itself */
211 static void image_free(SpaceLink *sl)
212 {       
213         SpaceImage *simage = (SpaceImage *) sl;
214
215         scopes_free(&simage->scopes);
216 }
217
218
219 /* spacetype; init callback, add handlers */
220 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
221 {
222         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
223
224         /* add drop boxes */
225         WM_event_add_dropbox_handler(&sa->handlers, lb);
226         
227 }
228
229 static SpaceLink *image_duplicate(SpaceLink *sl)
230 {
231         SpaceImage *simagen = MEM_dupallocN(sl);
232         
233         /* clear or remove stuff from old */
234
235         scopes_new(&simagen->scopes);
236
237         return (SpaceLink *)simagen;
238 }
239
240 static void image_operatortypes(void)
241 {
242         WM_operatortype_append(IMAGE_OT_view_all);
243         WM_operatortype_append(IMAGE_OT_view_pan);
244         WM_operatortype_append(IMAGE_OT_view_selected);
245         WM_operatortype_append(IMAGE_OT_view_zoom);
246         WM_operatortype_append(IMAGE_OT_view_zoom_in);
247         WM_operatortype_append(IMAGE_OT_view_zoom_out);
248         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
249         WM_operatortype_append(IMAGE_OT_view_zoom_border);
250 #ifdef WITH_INPUT_NDOF
251         WM_operatortype_append(IMAGE_OT_view_ndof);
252 #endif
253
254         WM_operatortype_append(IMAGE_OT_new);
255         WM_operatortype_append(IMAGE_OT_open);
256         WM_operatortype_append(IMAGE_OT_match_movie_length);
257         WM_operatortype_append(IMAGE_OT_replace);
258         WM_operatortype_append(IMAGE_OT_reload);
259         WM_operatortype_append(IMAGE_OT_save);
260         WM_operatortype_append(IMAGE_OT_save_as);
261         WM_operatortype_append(IMAGE_OT_save_sequence);
262         WM_operatortype_append(IMAGE_OT_pack);
263         WM_operatortype_append(IMAGE_OT_unpack);
264         
265         WM_operatortype_append(IMAGE_OT_invert);
266
267         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
268
269         WM_operatortype_append(IMAGE_OT_sample);
270         WM_operatortype_append(IMAGE_OT_sample_line);
271         WM_operatortype_append(IMAGE_OT_curves_point_set);
272
273         WM_operatortype_append(IMAGE_OT_properties);
274         WM_operatortype_append(IMAGE_OT_toolshelf);
275
276         WM_operatortype_append(IMAGE_OT_change_frame);
277
278         WM_operatortype_append(IMAGE_OT_read_renderlayers);
279         WM_operatortype_append(IMAGE_OT_render_border);
280         WM_operatortype_append(IMAGE_OT_clear_render_border);
281 }
282
283 static void image_keymap(struct wmKeyConfig *keyconf)
284 {
285         wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
286         wmKeyMapItem *kmi;
287         int i;
288         
289         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
290         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
291         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
292         WM_keymap_add_item(keymap, "IMAGE_OT_read_renderlayers", RKEY, KM_PRESS, KM_CTRL, 0);
293         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
294         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
295         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
296         WM_keymap_add_item(keymap, "IMAGE_OT_toolshelf", TKEY, KM_PRESS, 0, 0);
297
298         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
299         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", true);
300         
301         keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
302         
303         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
304
305         kmi = WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, KM_SHIFT, 0);
306         RNA_boolean_set(kmi->ptr, "fit_view", true);
307
308         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
309         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
310         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
311         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
312
313 #ifdef WITH_INPUT_NDOF
314         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
315         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
316 #endif
317
318         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
319         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
320         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
321         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
322         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
323         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
324         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEPAN, 0, KM_CTRL, 0);
325         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_border", BKEY, KM_PRESS, KM_SHIFT, 0);
326
327         /* ctrl now works as well, shift + numpad works as arrow keys on Windows */
328         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 8.0f);
329         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 4.0f);
330         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 2.0f);
331         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
332         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
333         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
334
335         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
336         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
337         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
338         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
339
340         WM_keymap_add_item(keymap, "IMAGE_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
341
342         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
343         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
344         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
345
346         /* toggle editmode is handy to have while UV unwrapping */
347         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
348         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
349         RNA_boolean_set(kmi->ptr, "toggle", true);
350
351         /* fast switch to render slots */
352         for (i = 0; i < MIN2(IMA_MAX_RENDER_SLOT, 9); i++) {
353                 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY + i, KM_PRESS, 0, 0);
354                 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slots.active_index");
355                 RNA_int_set(kmi->ptr, "value", i);
356         }
357
358         /* pivot */
359         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, 0, 0);
360         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
361         RNA_string_set(kmi->ptr, "value", "CENTER");
362
363         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, KM_CTRL, 0);
364         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
365         RNA_string_set(kmi->ptr, "value", "MEDIAN");
366
367         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, 0, 0);
368         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
369         RNA_string_set(kmi->ptr, "value", "CURSOR");
370
371         /* render border */
372         WM_keymap_add_item(keymap, "IMAGE_OT_render_border", BKEY, KM_PRESS, KM_CTRL, 0);
373         WM_keymap_add_item(keymap, "IMAGE_OT_clear_render_border", BKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
374 }
375
376 /* dropboxes */
377 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
378 {
379         if (drag->type == WM_DRAG_PATH)
380                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE, ICON_FILE_BLANK)) /* rule might not work? */
381                         return 1;
382         return 0;
383 }
384
385 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
386 {
387         /* copy drag path to properties */
388         RNA_string_set(drop->ptr, "filepath", drag->path);
389 }
390
391 /* area+region dropbox definition */
392 static void image_dropboxes(void)
393 {
394         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
395         
396         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
397 }
398
399 /**
400  * \note take care not to get into feedback loop here,
401  *       calling composite job causes viewer to refresh.
402  */
403 static void image_refresh(const bContext *C, ScrArea *sa)
404 {
405         Scene *scene = CTX_data_scene(C);
406         SpaceImage *sima = sa->spacedata.first;
407         Object *obedit = CTX_data_edit_object(C);
408         Image *ima;
409
410         ima = ED_space_image(sima);
411
412         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
413         
414         /* check if we have to set the image from the editmesh */
415         if (ima && (ima->source == IMA_SRC_VIEWER && sima->mode == SI_MODE_MASK)) {
416                 if (scene->nodetree) {
417                         Mask *mask = ED_space_image_get_mask(sima);
418                         if (mask) {
419                                 ED_node_composite_job(C, scene->nodetree, scene);
420                         }
421                 }
422         }
423         else if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) {
424                 /* pass */
425         }
426         else if (obedit && obedit->type == OB_MESH) {
427                 Mesh *me = (Mesh *)obedit->data;
428                 struct BMEditMesh *em = me->edit_btmesh;
429                 bool sloppy = true; /* partially selected face is ok */
430                 bool selected = !(scene->toolsettings->uv_flag & UV_SYNC_SELECTION); /* only selected active face? */
431
432                 if (BKE_scene_use_new_shading_nodes(scene)) {
433                         /* new shading system does not alter image */
434                 }
435                 else {
436                         /* old shading system, we set texface */
437                         MTexPoly *tf;
438                         
439                         if (em && EDBM_mtexpoly_check(em)) {
440                                 tf = EDBM_mtexpoly_active_get(em, NULL, sloppy, selected);
441
442                                 if (tf) {
443                                         /* don't need to check for pin here, see above */
444                                         sima->image = tf->tpage;
445                                         
446                                         if ((sima->flag & SI_EDITTILE) == 0) {
447                                                 sima->curtile = tf->tile;
448                                         }
449                                 }
450                         }
451                 }
452         }
453 }
454
455 static void image_listener(bScreen *sc, ScrArea *sa, wmNotifier *wmn)
456 {
457         Scene *scene = sc->scene;
458         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
459         
460         /* context changes */
461         switch (wmn->category) {
462                 case NC_WINDOW:
463                         /* notifier comes from editing color space */
464                         image_scopes_tag_refresh(sa);
465                         ED_area_tag_redraw(sa);
466                         break;
467                 case NC_SCENE:
468                         switch (wmn->data) {
469                                 case ND_FRAME:
470                                         image_scopes_tag_refresh(sa);
471                                         ED_area_tag_refresh(sa);
472                                         ED_area_tag_redraw(sa);
473                                         break;
474                                 case ND_MODE:
475                                         if (wmn->subtype == NS_EDITMODE_MESH)
476                                                 ED_area_tag_refresh(sa);
477                                         ED_area_tag_redraw(sa);
478                                         break;
479                                 case ND_RENDER_RESULT:
480                                 case ND_RENDER_OPTIONS:
481                                 case ND_COMPO_RESULT:
482                                         if (ED_space_image_show_render(sima))
483                                                 image_scopes_tag_refresh(sa);
484                                         ED_area_tag_redraw(sa);
485                                         break;
486                         }
487                         break;
488                 case NC_IMAGE:
489                         if (wmn->reference == sima->image || !wmn->reference) {
490                                 if (wmn->action != NA_PAINTING) {
491                                         image_scopes_tag_refresh(sa);
492                                         ED_area_tag_refresh(sa);
493                                         ED_area_tag_redraw(sa);
494                                 }
495                         }
496                         break;
497                 case NC_SPACE:
498                         if (wmn->data == ND_SPACE_IMAGE) {
499                                 image_scopes_tag_refresh(sa);
500                                 ED_area_tag_redraw(sa);
501                         }
502                         break;
503                 case NC_MASK:
504                 {
505                         // Scene *scene = wmn->window->screen->scene;
506                         /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima) but we cant get the scene */
507                         if (sima->mode == SI_MODE_MASK) {
508                                 switch (wmn->data) {
509                                         case ND_SELECT:
510                                                 ED_area_tag_redraw(sa);
511                                                 break;
512                                         case ND_DATA:
513                                         case ND_DRAW:
514                                                 /* causes node-recalc */
515                                                 ED_area_tag_redraw(sa);
516                                                 ED_area_tag_refresh(sa);
517                                                 break;
518                                 }
519                                 switch (wmn->action) {
520                                         case NA_SELECTED:
521                                                 ED_area_tag_redraw(sa);
522                                                 break;
523                                         case NA_EDITED:
524                                                 /* causes node-recalc */
525                                                 ED_area_tag_redraw(sa);
526                                                 ED_area_tag_refresh(sa);
527                                                 break;
528                                 }
529                         }
530                         break;
531                 }
532                 case NC_GEOM:
533                 {
534                         switch (wmn->data) {
535                                 case ND_DATA:
536                                 case ND_SELECT:
537                                         image_scopes_tag_refresh(sa);
538                                         ED_area_tag_refresh(sa);
539                                         ED_area_tag_redraw(sa);
540                                         break;
541                         }
542                         break;
543                 }
544                 case NC_OBJECT:
545                 {
546                         switch (wmn->data) {
547                                 case ND_TRANSFORM:
548                                 case ND_MODIFIER:
549                                 {
550                                         Object *ob = OBACT;
551                                         if (ob && (ob == wmn->reference) && (ob->mode & OB_MODE_EDIT)) {
552                                                 if (sima->lock && (sima->flag & SI_DRAWSHADOW)) {
553                                                         ED_area_tag_refresh(sa);
554                                                         ED_area_tag_redraw(sa);
555                                                 }
556                                         }
557                                         break;
558                                 }
559                         }
560
561                         break;
562                 }
563                 case NC_ID:
564                 {
565                         if (wmn->action == NA_RENAME) {
566                                 ED_area_tag_redraw(sa);
567                         }
568                         break;
569                 }
570                 case NC_WM:
571                         if (wmn->data == ND_UNDO) {
572                                 ED_area_tag_redraw(sa);
573                                 ED_area_tag_refresh(sa);
574                         }
575                         break;
576         }
577 }
578
579 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
580
581 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
582 {
583         SpaceImage *sima = CTX_wm_space_image(C);
584
585         if (CTX_data_dir(member)) {
586                 CTX_data_dir_set(result, image_context_dir);
587         }
588         else if (CTX_data_equals(member, "edit_image")) {
589                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
590                 return 1;
591         }
592         else if (CTX_data_equals(member, "edit_mask")) {
593                 Mask *mask = ED_space_image_get_mask(sima);
594                 if (mask) {
595                         CTX_data_id_pointer_set(result, &mask->id);
596                 }
597                 return true;
598         }
599         return 0;
600 }
601
602 /************************** main region ***************************/
603
604 /* sets up the fields of the View2D from zoom and offset */
605 static void image_main_region_set_view2d(SpaceImage *sima, ARegion *ar)
606 {
607         Image *ima = ED_space_image(sima);
608         float x1, y1, w, h;
609         int width, height, winx, winy;
610         
611 #if 0
612         if (image_preview_active(curarea, &width, &height)) {}
613         else
614 #endif
615         ED_space_image_get_size(sima, &width, &height);
616
617         w = width;
618         h = height;
619         
620         if (ima)
621                 h *= ima->aspy / ima->aspx;
622
623         winx = BLI_rcti_size_x(&ar->winrct) + 1;
624         winy = BLI_rcti_size_y(&ar->winrct) + 1;
625                 
626         ar->v2d.tot.xmin = 0;
627         ar->v2d.tot.ymin = 0;
628         ar->v2d.tot.xmax = w;
629         ar->v2d.tot.ymax = h;
630         
631         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
632         ar->v2d.mask.xmax = winx;
633         ar->v2d.mask.ymax = winy;
634
635         /* which part of the image space do we see? */
636         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
637         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
638
639         x1 -= sima->zoom * sima->xof;
640         y1 -= sima->zoom * sima->yof;
641         
642         /* relative display right */
643         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
644         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
645         
646         /* relative display left */
647         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
648         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
649         
650         /* normalize 0.0..1.0 */
651         ar->v2d.cur.xmin /= w;
652         ar->v2d.cur.xmax /= w;
653         ar->v2d.cur.ymin /= h;
654         ar->v2d.cur.ymax /= h;
655 }
656
657 /* add handlers, stuff you only do once or on area/region changes */
658 static void image_main_region_init(wmWindowManager *wm, ARegion *ar)
659 {
660         wmKeyMap *keymap;
661         
662         // image space manages own v2d
663         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
664
665         /* mask polls mode */
666         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
667         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
668
669         /* image paint polls for mode */
670         keymap = WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
671         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
672
673         keymap = WM_keymap_find(wm->defaultconf, "Paint Curve", 0, 0);
674         WM_event_add_keymap_handler(&ar->handlers, keymap);
675
676         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
677         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
678
679         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
680         WM_event_add_keymap_handler(&ar->handlers, keymap);
681         
682         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
683         WM_event_add_keymap_handler(&ar->handlers, keymap);
684
685         /* own keymaps */
686         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
687         WM_event_add_keymap_handler(&ar->handlers, keymap);
688         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
689         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
690
691 }
692
693 static void image_main_region_draw(const bContext *C, ARegion *ar)
694 {
695         /* draw entirely, view changes should be handled here */
696         SpaceImage *sima = CTX_wm_space_image(C);
697         Object *obact = CTX_data_active_object(C);
698         Object *obedit = CTX_data_edit_object(C);
699         Mask *mask = NULL;
700         bool curve = false;
701         Scene *scene = CTX_data_scene(C);
702         View2D *v2d = &ar->v2d;
703         //View2DScrollers *scrollers;
704         float col[3];
705
706         /* XXX not supported yet, disabling for now */
707         scene->r.scemode &= ~R_COMP_CROP;
708         
709         /* clear and setup matrix */
710         UI_GetThemeColor3fv(TH_BACK, col);
711         glClearColor(col[0], col[1], col[2], 0.0);
712         glClear(GL_COLOR_BUFFER_BIT);
713
714         image_user_refresh_scene(C, sima);
715
716         /* we set view2d from own zoom and offset each time */
717         image_main_region_set_view2d(sima, ar);
718
719         /* we draw image in pixelspace */
720         draw_image_main(C, ar);
721
722         /* and uvs in 0.0-1.0 space */
723         UI_view2d_view_ortho(v2d);
724
725         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
726
727         ED_uvedit_draw_main(sima, ar, scene, obedit, obact);
728
729         /* check for mask (delay draw) */
730         if (ED_space_image_show_uvedit(sima, obedit)) {
731                 /* pass */
732         }
733         else if (sima->mode == SI_MODE_MASK) {
734                 mask = ED_space_image_get_mask(sima);
735         }
736         else if (ED_space_image_paint_curve(C)) {
737                 curve = true;
738         }
739
740         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
741
742         if (sima->flag & SI_SHOW_GPENCIL) {
743                 /* Grease Pencil too (in addition to UV's) */
744                 draw_image_grease_pencil((bContext *)C, true);
745         }
746
747         /* sample line */
748         draw_image_sample_line(sima);
749
750         UI_view2d_view_restore(C);
751
752         if (sima->flag & SI_SHOW_GPENCIL) {
753                 /* draw Grease Pencil - screen space only */
754                 draw_image_grease_pencil((bContext *)C, false);
755         }
756
757         if (mask) {
758                 Image *image = ED_space_image(sima);
759                 int width, height, show_viewer;
760                 float aspx, aspy;
761
762                 show_viewer = (image && image->source == IMA_SRC_VIEWER);
763
764                 if (show_viewer) {
765                         /* ED_space_image_get* will acquire image buffer which requires
766                          * lock here by the same reason why lock is needed in draw_image_main
767                          */
768                         BLI_lock_thread(LOCK_DRAW_IMAGE);
769                 }
770
771                 ED_space_image_get_size(sima, &width, &height);
772                 ED_space_image_get_aspect(sima, &aspx, &aspy);
773
774                 if (show_viewer)
775                         BLI_unlock_thread(LOCK_DRAW_IMAGE);
776
777                 ED_mask_draw_region(mask, ar,
778                                     sima->mask_info.draw_flag,
779                                     sima->mask_info.draw_type,
780                                     sima->mask_info.overlay_mode,
781                                     width, height,
782                                     aspx, aspy,
783                                     true, false,
784                                     NULL, C);
785
786                 UI_view2d_view_ortho(v2d);
787                 ED_image_draw_cursor(ar, sima->cursor);
788                 UI_view2d_view_restore(C);
789         }
790         else if (curve) {
791                 UI_view2d_view_ortho(v2d);
792                 ED_image_draw_cursor(ar, sima->cursor);
793                 UI_view2d_view_restore(C);
794         }
795
796         draw_image_cache(C, ar);
797
798         /* scrollers? */
799 #if 0
800         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
801         UI_view2d_scrollers_draw(C, v2d, scrollers);
802         UI_view2d_scrollers_free(scrollers);
803 #endif
804 }
805
806 static void image_main_region_listener(bScreen *UNUSED(sc), ScrArea *sa, ARegion *ar, wmNotifier *wmn)
807 {
808         /* context changes */
809         switch (wmn->category) {
810                 case NC_GPENCIL:
811                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
812                                 ED_region_tag_redraw(ar);
813                         else if (wmn->data & ND_GPENCIL_EDITMODE)
814                                 ED_region_tag_redraw(ar);
815                         break;
816                 case NC_IMAGE:
817                         if (wmn->action == NA_PAINTING)
818                                 ED_region_tag_redraw(ar);
819                         break;
820                 case NC_MATERIAL:
821                         if (wmn->data == ND_SHADING_LINKS) {
822                                 SpaceImage *sima = sa->spacedata.first;
823
824                                 if (sima->iuser.scene && (sima->iuser.scene->toolsettings->uv_flag & UV_SHOW_SAME_IMAGE))
825                                         ED_region_tag_redraw(ar);
826                         }
827                         break;
828         }
829 }
830
831 /* *********************** buttons region ************************ */
832
833 /* add handlers, stuff you only do once or on area/region changes */
834 static void image_buttons_region_init(wmWindowManager *wm, ARegion *ar)
835 {
836         wmKeyMap *keymap;
837
838         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
839         ED_region_panels_init(wm, ar);
840         
841         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
842         WM_event_add_keymap_handler(&ar->handlers, keymap);
843 }
844
845 static void image_buttons_region_draw(const bContext *C, ARegion *ar)
846 {
847         ED_region_panels(C, ar, NULL, -1, true);
848 }
849
850 static void image_buttons_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
851 {
852         /* context changes */
853         switch (wmn->category) {
854                 case NC_TEXTURE:
855                 case NC_MATERIAL:
856                         /* sending by texture render job and needed to properly update displaying
857                          * brush texture icon */
858                         ED_region_tag_redraw(ar);
859                         break;
860                 case NC_SCENE:
861                         switch (wmn->data) {
862                                 case ND_MODE:
863                                 case ND_RENDER_RESULT:
864                                 case ND_COMPO_RESULT:
865                                         ED_region_tag_redraw(ar);
866                                         break;
867                         }
868                         break;
869                 case NC_IMAGE:
870                         if (wmn->action != NA_PAINTING)
871                                 ED_region_tag_redraw(ar);
872                         break;
873                 case NC_NODE:
874                         ED_region_tag_redraw(ar);
875                         break;
876                 case NC_GPENCIL:
877                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
878                                 ED_region_tag_redraw(ar);
879                         break;
880         }
881 }
882
883 /* *********************** scopes region ************************ */
884
885 /* add handlers, stuff you only do once or on area/region changes */
886 static void image_tools_region_init(wmWindowManager *wm, ARegion *ar)
887 {
888         wmKeyMap *keymap;
889         
890         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
891         ED_region_panels_init(wm, ar);
892         
893         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
894         WM_event_add_keymap_handler(&ar->handlers, keymap);
895 }
896
897 static void image_tools_region_draw(const bContext *C, ARegion *ar)
898 {
899         SpaceImage *sima = CTX_wm_space_image(C);
900         Scene *scene = CTX_data_scene(C);
901         void *lock;
902         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
903         /* XXX performance regression if name of scopes category changes! */
904         PanelCategoryStack *category = UI_panel_category_active_find(ar, "Scopes");
905
906         /* only update scopes if scope category is active */
907         if (category) {
908                 if (ibuf) {
909                         if (!sima->scopes.ok) {
910                                 BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, &scene->view_settings, &scene->display_settings);
911                         }
912                         if (sima->image->flag & IMA_VIEW_AS_RENDER)
913                                 ED_space_image_scopes_update(C, sima, ibuf, true);
914                         else
915                                 ED_space_image_scopes_update(C, sima, ibuf, false);
916                 }
917         }
918         ED_space_image_release_buffer(sima, ibuf, lock);
919         
920         ED_region_panels(C, ar, NULL, -1, true);
921 }
922
923 static void image_tools_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
924 {
925         /* context changes */
926         switch (wmn->category) {
927                 case NC_GPENCIL:
928                         if (wmn->data == ND_DATA || ELEM(wmn->action, NA_EDITED, NA_SELECTED))
929                                 ED_region_tag_redraw(ar);
930                         break;
931                 case NC_BRUSH:
932                         /* NA_SELECTED is used on brush changes */
933                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
934                                 ED_region_tag_redraw(ar);
935                         break;
936                 case NC_SCENE:
937                         switch (wmn->data) {
938                                 case ND_MODE:
939                                 case ND_RENDER_RESULT:
940                                 case ND_COMPO_RESULT:
941                                         ED_region_tag_redraw(ar);
942                                         break;
943                         }
944                         break;
945                 case NC_IMAGE:
946                         if (wmn->action != NA_PAINTING)
947                                 ED_region_tag_redraw(ar);
948                         break;
949                 case NC_NODE:
950                         ED_region_tag_redraw(ar);
951                         break;
952                         
953         }
954 }
955
956 /************************* header region **************************/
957
958 /* add handlers, stuff you only do once or on area/region changes */
959 static void image_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
960 {
961         ED_region_header_init(ar);
962 }
963
964 static void image_header_region_draw(const bContext *C, ARegion *ar)
965 {
966         ScrArea *sa = CTX_wm_area(C);
967         SpaceImage *sima = sa->spacedata.first;
968
969         image_user_refresh_scene(C, sima);
970
971         ED_region_header(C, ar);
972 }
973
974 static void image_header_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
975 {
976         /* context changes */
977         switch (wmn->category) {
978                 case NC_SCENE:
979                         switch (wmn->data) {
980                                 case ND_MODE:
981                                 case ND_TOOLSETTINGS:
982                                         ED_region_tag_redraw(ar);
983                                         break;
984                         }
985                         break;
986                 case NC_GEOM:
987                         switch (wmn->data) {
988                                 case ND_DATA:
989                                 case ND_SELECT:
990                                         ED_region_tag_redraw(ar);
991                                         break;
992                         }
993                         break;
994         }
995 }
996
997 static void image_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
998 {
999         SpaceImage *simg = (SpaceImage *)slink;
1000
1001         if (!ELEM(GS(old_id->name), ID_IM, ID_GD, ID_MSK)) {
1002                 return;
1003         }
1004
1005         if ((ID *)simg->image == old_id) {
1006                 simg->image = (Image *)new_id;
1007                 id_us_ensure_real(new_id);
1008         }
1009
1010         if ((ID *)simg->gpd == old_id) {
1011                 simg->gpd = (bGPdata *)new_id;
1012                 id_us_min(old_id);
1013                 id_us_plus(new_id);
1014         }
1015
1016         if ((ID *)simg->mask_info.mask == old_id) {
1017                 simg->mask_info.mask = (Mask *)new_id;
1018                 id_us_ensure_real(new_id);
1019         }
1020 }
1021
1022 /**************************** spacetype *****************************/
1023
1024 /* only called once, from space/spacetypes.c */
1025 void ED_spacetype_image(void)
1026 {
1027         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
1028         ARegionType *art;
1029         
1030         st->spaceid = SPACE_IMAGE;
1031         strncpy(st->name, "Image", BKE_ST_MAXNAME);
1032         
1033         st->new = image_new;
1034         st->free = image_free;
1035         st->init = image_init;
1036         st->duplicate = image_duplicate;
1037         st->operatortypes = image_operatortypes;
1038         st->keymap = image_keymap;
1039         st->dropboxes = image_dropboxes;
1040         st->refresh = image_refresh;
1041         st->listener = image_listener;
1042         st->context = image_context;
1043         st->id_remap = image_id_remap;
1044
1045         /* regions: main window */
1046         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1047         art->regionid = RGN_TYPE_WINDOW;
1048         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1049         art->init = image_main_region_init;
1050         art->draw = image_main_region_draw;
1051         art->listener = image_main_region_listener;
1052
1053         BLI_addhead(&st->regiontypes, art);
1054         
1055         /* regions: listview/buttons */
1056         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1057         art->regionid = RGN_TYPE_UI;
1058         art->prefsizex = 220; // XXX
1059         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1060         art->listener = image_buttons_region_listener;
1061         art->init = image_buttons_region_init;
1062         art->draw = image_buttons_region_draw;
1063         BLI_addhead(&st->regiontypes, art);
1064
1065         ED_uvedit_buttons_register(art);
1066         image_buttons_register(art);
1067
1068         /* regions: statistics/scope buttons */
1069         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1070         art->regionid = RGN_TYPE_TOOLS;
1071         art->prefsizex = 220; // XXX
1072         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1073         art->listener = image_tools_region_listener;
1074         art->init = image_tools_region_init;
1075         art->draw = image_tools_region_draw;
1076         BLI_addhead(&st->regiontypes, art);
1077
1078         /* regions: header */
1079         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1080         art->regionid = RGN_TYPE_HEADER;
1081         art->prefsizey = HEADERY;
1082         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1083         art->listener = image_header_region_listener;
1084         art->init = image_header_region_init;
1085         art->draw = image_header_region_draw;
1086         
1087         BLI_addhead(&st->regiontypes, art);
1088         
1089         BKE_spacetype_register(st);
1090 }
1091