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