code cleanup: use const events for modal and invoke operators.
[blender.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_SCENE:
439                         switch (wmn->data) {
440                                 case ND_FRAME:
441                                         image_scopes_tag_refresh(sa);
442                                         ED_area_tag_refresh(sa);
443                                         ED_area_tag_redraw(sa);
444                                         break;
445                                 case ND_MODE:
446                                         if (wmn->subtype == NS_EDITMODE_MESH)
447                                                 ED_area_tag_refresh(sa);
448                                         ED_area_tag_redraw(sa);
449                                         break;
450                                 case ND_RENDER_RESULT:
451                                 case ND_RENDER_OPTIONS:
452                                 case ND_COMPO_RESULT:
453                                         if (ED_space_image_show_render(sima))
454                                                 image_scopes_tag_refresh(sa);
455                                         ED_area_tag_redraw(sa);
456                                         break;
457                         }
458                         break;
459                 case NC_IMAGE:
460                         if (wmn->reference == sima->image || !wmn->reference) {
461                                 image_scopes_tag_refresh(sa);
462                                 ED_area_tag_refresh(sa);
463                                 ED_area_tag_redraw(sa);
464                         }
465                         break;
466                 case NC_SPACE:
467                         if (wmn->data == ND_SPACE_IMAGE) {
468                                 image_scopes_tag_refresh(sa);
469                                 ED_area_tag_redraw(sa);
470                         }
471                         break;
472                 case NC_MASK:
473                 {
474                         // Scene *scene = wmn->window->screen->scene;
475                         /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima) but we cant get the scene */
476                         if (sima->mode == SI_MODE_MASK) {
477                                 switch (wmn->data) {
478                                         case ND_SELECT:
479                                                 ED_area_tag_redraw(sa);
480                                                 break;
481                                         case ND_DATA:
482                                         case ND_DRAW:
483                                                 /* causes node-recalc */
484                                                 ED_area_tag_redraw(sa);
485                                                 ED_area_tag_refresh(sa);
486                                                 break;
487                                 }
488                                 switch (wmn->action) {
489                                         case NA_SELECTED:
490                                                 ED_area_tag_redraw(sa);
491                                                 break;
492                                         case NA_EDITED:
493                                                 /* causes node-recalc */
494                                                 ED_area_tag_redraw(sa);
495                                                 ED_area_tag_refresh(sa);
496                                                 break;
497                                 }
498                         }
499                         break;
500                 }
501                 case NC_GEOM:
502                         switch (wmn->data) {
503                                 case ND_DATA:
504                                 case ND_SELECT:
505                                         image_scopes_tag_refresh(sa);
506                                         ED_area_tag_refresh(sa);
507                                         ED_area_tag_redraw(sa);
508                                         break;
509                         }
510                 case NC_OBJECT:
511                 {
512                         Object *ob = (Object *)wmn->reference;
513                         switch (wmn->data) {
514                                 case ND_TRANSFORM:
515                                 case ND_MODIFIER:
516                                         if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
517                                                 ED_area_tag_refresh(sa);
518                                                 ED_area_tag_redraw(sa);
519                                         }
520                                         break;
521                         }
522                 }
523         }
524 }
525
526 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
527
528 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
529 {
530         SpaceImage *sima = CTX_wm_space_image(C);
531
532         if (CTX_data_dir(member)) {
533                 CTX_data_dir_set(result, image_context_dir);
534         }
535         else if (CTX_data_equals(member, "edit_image")) {
536                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
537                 return 1;
538         }
539         else if (CTX_data_equals(member, "edit_mask")) {
540                 Mask *mask = ED_space_image_get_mask(sima);
541                 if (mask) {
542                         CTX_data_id_pointer_set(result, &mask->id);
543                 }
544                 return TRUE;
545         }
546         return 0;
547 }
548
549 /************************** main region ***************************/
550
551 /* sets up the fields of the View2D from zoom and offset */
552 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
553 {
554         Image *ima = ED_space_image(sima);
555         float x1, y1, w, h;
556         int width, height, winx, winy;
557         
558 #if 0
559         if (image_preview_active(curarea, &width, &height)) {}
560         else
561 #endif
562         ED_space_image_get_size(sima, &width, &height);
563
564         w = width;
565         h = height;
566         
567         if (ima)
568                 h *= ima->aspy / ima->aspx;
569
570         winx = BLI_rcti_size_x(&ar->winrct) + 1;
571         winy = BLI_rcti_size_y(&ar->winrct) + 1;
572                 
573         ar->v2d.tot.xmin = 0;
574         ar->v2d.tot.ymin = 0;
575         ar->v2d.tot.xmax = w;
576         ar->v2d.tot.ymax = h;
577         
578         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
579         ar->v2d.mask.xmax = winx;
580         ar->v2d.mask.ymax = winy;
581
582         /* which part of the image space do we see? */
583         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
584         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
585
586         x1 -= sima->zoom * sima->xof;
587         y1 -= sima->zoom * sima->yof;
588         
589         /* relative display right */
590         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
591         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
592         
593         /* relative display left */
594         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
595         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
596         
597         /* normalize 0.0..1.0 */
598         ar->v2d.cur.xmin /= w;
599         ar->v2d.cur.xmax /= w;
600         ar->v2d.cur.ymin /= h;
601         ar->v2d.cur.ymax /= h;
602 }
603
604 /* add handlers, stuff you only do once or on area/region changes */
605 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
606 {
607         wmKeyMap *keymap;
608         
609         // image space manages own v2d
610         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
611
612         /* mask polls mode */
613         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
614         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
615
616         /* image paint polls for mode */
617         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
618         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
619
620         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
621         WM_event_add_keymap_handler(&ar->handlers, keymap);
622         
623         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
624         WM_event_add_keymap_handler(&ar->handlers, keymap);
625
626         /* own keymaps */
627         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
628         WM_event_add_keymap_handler(&ar->handlers, keymap);
629         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
630         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
631
632 }
633
634 static void image_main_area_draw(const bContext *C, ARegion *ar)
635 {
636         /* draw entirely, view changes should be handled here */
637         SpaceImage *sima = CTX_wm_space_image(C);
638         Object *obact = CTX_data_active_object(C);
639         Object *obedit = CTX_data_edit_object(C);
640         Mask *mask = NULL;
641         Scene *scene = CTX_data_scene(C);
642         View2D *v2d = &ar->v2d;
643         //View2DScrollers *scrollers;
644         float col[3];
645         
646         /* XXX not supported yet, disabling for now */
647         scene->r.scemode &= ~R_COMP_CROP;
648         
649         /* clear and setup matrix */
650         UI_GetThemeColor3fv(TH_BACK, col);
651         glClearColor(col[0], col[1], col[2], 0.0);
652         glClear(GL_COLOR_BUFFER_BIT);
653
654         /* put scene context variable in iuser */
655         sima->iuser.scene = scene;
656
657         /* we set view2d from own zoom and offset each time */
658         image_main_area_set_view2d(sima, ar);
659
660         /* we draw image in pixelspace */
661         draw_image_main(C, ar);
662
663         /* and uvs in 0.0-1.0 space */
664         UI_view2d_view_ortho(v2d);
665         draw_uvedit_main(sima, ar, scene, obedit, obact);
666
667         /* check for mask (delay draw) */
668         if (ED_space_image_show_uvedit(sima, obedit)) {
669                 /* pass */
670         }
671         else if (sima->mode == SI_MODE_MASK) {
672                 mask = ED_space_image_get_mask(sima);
673                 draw_image_cursor(sima, ar);
674         }
675
676         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
677
678         if (sima->flag & SI_SHOW_GPENCIL) {
679                 /* Grease Pencil too (in addition to UV's) */
680                 draw_image_grease_pencil((bContext *)C, TRUE);
681         }
682
683         /* sample line */
684         draw_image_sample_line(sima);
685
686         UI_view2d_view_restore(C);
687
688         if (sima->flag & SI_SHOW_GPENCIL) {
689                 /* draw Grease Pencil - screen space only */
690                 draw_image_grease_pencil((bContext *)C, FALSE);
691         }
692
693         if (mask) {
694                 Image *image = ED_space_image(sima);
695                 int width, height, show_viewer;
696                 float aspx, aspy;
697
698                 show_viewer = (image && image->source == IMA_SRC_VIEWER);
699
700                 if (show_viewer) {
701                         /* ED_space_image_get* will acquire image buffer which requires
702                          * lock here by the same reason why lock is needed in draw_image_main
703                          */
704                         BLI_lock_thread(LOCK_DRAW_IMAGE);
705                 }
706
707                 ED_space_image_get_size(sima, &width, &height);
708                 ED_space_image_get_aspect(sima, &aspx, &aspy);
709
710                 if (show_viewer)
711                         BLI_unlock_thread(LOCK_DRAW_IMAGE);
712
713                 ED_mask_draw_region(mask, ar,
714                                     sima->mask_info.draw_flag, sima->mask_info.draw_type,
715                                     width, height,
716                                     aspx, aspy,
717                                     TRUE, FALSE,
718                                     NULL, C);
719
720                 ED_mask_draw_frames(mask, ar, CFRA, mask->sfra, mask->efra);
721
722                 draw_image_cursor(sima, ar);
723         }
724
725         /* scrollers? */
726 #if 0
727         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
728         UI_view2d_scrollers_draw(C, v2d, scrollers);
729         UI_view2d_scrollers_free(scrollers);
730 #endif
731 }
732
733 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
734 {
735         /* context changes */
736         switch (wmn->category) {
737                 case NC_GPENCIL:
738                         if (wmn->action == NA_EDITED)
739                                 ED_region_tag_redraw(ar);
740                         break;
741         }
742 }
743
744 /* *********************** buttons region ************************ */
745
746 /* add handlers, stuff you only do once or on area/region changes */
747 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
748 {
749         wmKeyMap *keymap;
750
751         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
752         ED_region_panels_init(wm, ar);
753         
754         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
755         WM_event_add_keymap_handler(&ar->handlers, keymap);
756 }
757
758 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
759 {
760         ED_region_panels(C, ar, 1, NULL, -1);
761 }
762
763 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
764 {
765         /* context changes */
766         switch (wmn->category) {
767                 case NC_GPENCIL:
768                         if (wmn->data == ND_DATA)
769                                 ED_region_tag_redraw(ar);
770                         break;
771                 case NC_BRUSH:
772                         if (wmn->action == NA_EDITED)
773                                 ED_region_tag_redraw(ar);
774                         break;
775                 case NC_TEXTURE:
776                 case NC_MATERIAL:
777                         /* sending by texture render job and needed to properly update displaying
778                          * brush texture icon */
779                         ED_region_tag_redraw(ar);
780                         break;
781         }
782 }
783
784 /* *********************** scopes region ************************ */
785
786 /* add handlers, stuff you only do once or on area/region changes */
787 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
788 {
789         wmKeyMap *keymap;
790         
791         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
792         ED_region_panels_init(wm, ar);
793         
794         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
795         WM_event_add_keymap_handler(&ar->handlers, keymap);
796 }
797
798 static void image_scope_area_draw(const bContext *C, ARegion *ar)
799 {
800         SpaceImage *sima = CTX_wm_space_image(C);
801         Scene *scene = CTX_data_scene(C);
802         void *lock;
803         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
804         if (ibuf) {
805                 if (!sima->scopes.ok) {
806                         BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, &scene->view_settings, &scene->display_settings);
807                 }
808                 scopes_update(&sima->scopes, ibuf, &scene->view_settings, &scene->display_settings);
809         }
810         ED_space_image_release_buffer(sima, ibuf, lock);
811         
812         ED_region_panels(C, ar, 1, NULL, -1);
813 }
814
815 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
816 {
817         /* context changes */
818         switch (wmn->category) {
819                 case NC_SCENE:
820                         switch (wmn->data) {
821                                 case ND_MODE:
822                                 case ND_RENDER_RESULT:
823                                 case ND_COMPO_RESULT:
824                                         ED_region_tag_redraw(ar);
825                                         break;
826                         }
827                         break;
828                 case NC_IMAGE:
829                         ED_region_tag_redraw(ar);
830                         break;
831                 case NC_NODE:
832                         ED_region_tag_redraw(ar);
833                         break;
834                         
835         }
836 }
837
838 /************************* header region **************************/
839
840 /* add handlers, stuff you only do once or on area/region changes */
841 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
842 {
843         ED_region_header_init(ar);
844 }
845
846 static void image_header_area_draw(const bContext *C, ARegion *ar)
847 {
848         ED_region_header(C, ar);
849 }
850
851 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
852 {
853         /* context changes */
854         switch (wmn->category) {
855                 case NC_SCENE:
856                         switch (wmn->data) {
857                                 case ND_MODE:
858                                 case ND_TOOLSETTINGS:
859                                         ED_region_tag_redraw(ar);
860                                         break;
861                         }
862                         break;
863                 case NC_GEOM:
864                         switch (wmn->data) {
865                                 case ND_DATA:
866                                 case ND_SELECT:
867                                         ED_region_tag_redraw(ar);
868                                         break;
869                         }
870                         
871         }
872 }
873
874 /**************************** spacetype *****************************/
875
876 /* only called once, from space/spacetypes.c */
877 void ED_spacetype_image(void)
878 {
879         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
880         ARegionType *art;
881         
882         st->spaceid = SPACE_IMAGE;
883         strncpy(st->name, "Image", BKE_ST_MAXNAME);
884         
885         st->new = image_new;
886         st->free = image_free;
887         st->init = image_init;
888         st->duplicate = image_duplicate;
889         st->operatortypes = image_operatortypes;
890         st->keymap = image_keymap;
891         st->dropboxes = image_dropboxes;
892         st->refresh = image_refresh;
893         st->listener = image_listener;
894         st->context = image_context;
895         
896         /* regions: main window */
897         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
898         art->regionid = RGN_TYPE_WINDOW;
899         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
900         art->init = image_main_area_init;
901         art->draw = image_main_area_draw;
902         art->listener = image_main_area_listener;
903
904         BLI_addhead(&st->regiontypes, art);
905         
906         /* regions: listview/buttons */
907         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
908         art->regionid = RGN_TYPE_UI;
909         art->prefsizex = 220; // XXX
910         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
911         art->listener = image_buttons_area_listener;
912         art->init = image_buttons_area_init;
913         art->draw = image_buttons_area_draw;
914         BLI_addhead(&st->regiontypes, art);
915
916         image_buttons_register(art);
917         ED_uvedit_buttons_register(art);
918         
919         /* regions: statistics/scope buttons */
920         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
921         art->regionid = RGN_TYPE_PREVIEW;
922         art->prefsizex = 220; // XXX
923         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
924         art->listener = image_scope_area_listener;
925         art->init = image_scope_area_init;
926         art->draw = image_scope_area_draw;
927         BLI_addhead(&st->regiontypes, art);
928
929         /* regions: header */
930         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
931         art->regionid = RGN_TYPE_HEADER;
932         art->prefsizey = HEADERY;
933         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
934         art->listener = image_header_area_listener;
935         art->init = image_header_area_init;
936         art->draw = image_header_area_draw;
937         
938         BLI_addhead(&st->regiontypes, art);
939         
940         BKE_spacetype_register(st);
941 }
942