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