Merged changes in the trunk up to revision 54110.
[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;
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_record_composite);
257
258         WM_operatortype_append(IMAGE_OT_properties);
259         WM_operatortype_append(IMAGE_OT_scopes);
260 }
261
262 static void image_keymap(struct wmKeyConfig *keyconf)
263 {
264         wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
265         wmKeyMapItem *kmi;
266         int i;
267         
268         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
269         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
270         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
271         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
272         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
273         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
274         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", TKEY, KM_PRESS, 0, 0);
275
276         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
277         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", TRUE);
278         
279         keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
280         
281         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
282         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
283         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
284         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
285         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
286
287         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
288         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
289
290         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
291         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
292         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
293         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
294         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
295         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
296         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEPAN, 0, KM_CTRL, 0);
297
298         /* ctrl now works as well, shift + numpad works as arrow keys on Windows */
299         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 8.0f);
300         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 4.0f);
301         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 2.0f);
302         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
303         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
304         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
305
306         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
307         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
308         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
309         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
310
311         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
312         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
313         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
314
315         /* toggle editmode is handy to have while UV unwrapping */
316         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
317         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
318         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
319
320         /* fast switch to render slots */
321         for (i = 0; i < MAX2(IMA_MAX_RENDER_SLOT, 9); i++) {
322                 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY + i, KM_PRESS, 0, 0);
323                 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slot");
324                 RNA_int_set(kmi->ptr, "value", i);
325         }
326
327         /* pivot */
328         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, 0, 0);
329         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
330         RNA_string_set(kmi->ptr, "value", "CENTER");
331
332         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, KM_CTRL, 0);
333         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
334         RNA_string_set(kmi->ptr, "value", "MEDIAN");
335
336         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, 0, 0);
337         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
338         RNA_string_set(kmi->ptr, "value", "CURSOR");
339 }
340
341 /* dropboxes */
342 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
343 {
344         if (drag->type == WM_DRAG_PATH)
345                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
346                         return 1;
347         return 0;
348 }
349
350 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
351 {
352         /* copy drag path to properties */
353         RNA_string_set(drop->ptr, "filepath", drag->path);
354 }
355
356 /* area+region dropbox definition */
357 static void image_dropboxes(void)
358 {
359         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
360         
361         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
362 }
363
364 /**
365  * \note take care not to get into feedback loop here,
366  *       calling composite job causes viewer to refresh.
367  */
368 static void image_refresh(const bContext *C, ScrArea *sa)
369 {
370         Scene *scene = CTX_data_scene(C);
371         SpaceImage *sima = sa->spacedata.first;
372         Object *obedit = CTX_data_edit_object(C);
373         Image *ima;
374
375         ima = ED_space_image(sima);
376
377         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
378         
379         /* check if we have to set the image from the editmesh */
380         if (ima && (ima->source == IMA_SRC_VIEWER && sima->mode == SI_MODE_MASK)) {
381                 if (sima->lock == FALSE && G.moving) {
382                         /* pass */
383                 }
384                 else {
385                         if (scene->nodetree) {
386                                 Mask *mask = ED_space_image_get_mask(sima);
387                                 if (mask) {
388                                         ED_node_composite_job(C, scene->nodetree, scene);
389                                 }
390                         }
391                 }
392         }
393         else if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) {
394                 /* pass */
395         }
396         else if (obedit && obedit->type == OB_MESH) {
397                 Mesh *me = (Mesh *)obedit->data;
398                 struct BMEditMesh *em = me->edit_btmesh;
399                 int sloppy = TRUE; /* partially selected face is ok */
400                 int selected = !(scene->toolsettings->uv_flag & UV_SYNC_SELECTION); /* only selected active face? */
401
402                 if (BKE_scene_use_new_shading_nodes(scene)) {
403                         /* new shading system, get image from material */
404                         BMFace *efa = BM_active_face_get(em->bm, sloppy, selected);
405
406                         if (efa) {
407                                 Image *node_ima;
408                                 ED_object_get_active_image(obedit, efa->mat_nr + 1, &node_ima, NULL, NULL);
409
410                                 if (node_ima)
411                                         sima->image = node_ima;
412                         }
413                 }
414                 else {
415                         /* old shading system, we set texface */
416                         MTexPoly *tf;
417                         
418                         if (em && EDBM_mtexpoly_check(em)) {
419                                 tf = EDBM_mtexpoly_active_get(em, NULL, sloppy, selected);
420
421                                 if (tf) {
422                                         /* don't need to check for pin here, see above */
423                                         sima->image = tf->tpage;
424                                         
425                                         if ((sima->flag & SI_EDITTILE) == 0) {
426                                                 sima->curtile = tf->tile;
427                                         }
428                                 }
429                         }
430                 }
431         }
432 }
433
434 static void image_listener(ScrArea *sa, wmNotifier *wmn)
435 {
436         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
437         
438         /* context changes */
439         switch (wmn->category) {
440                 case NC_SCENE:
441                         switch (wmn->data) {
442                                 case ND_FRAME:
443                                         image_scopes_tag_refresh(sa);
444                                         ED_area_tag_refresh(sa);
445                                         ED_area_tag_redraw(sa);
446                                         break;
447                                 case ND_MODE:
448                                         if (wmn->subtype == NS_EDITMODE_MESH)
449                                                 ED_area_tag_refresh(sa);
450                                         ED_area_tag_redraw(sa);
451                                         break;
452                                 case ND_RENDER_RESULT:
453                                 case ND_RENDER_OPTIONS:
454                                 case ND_COMPO_RESULT:
455                                         if (ED_space_image_show_render(sima))
456                                                 image_scopes_tag_refresh(sa);
457                                         ED_area_tag_redraw(sa);
458                                         break;
459                         }
460                         break;
461                 case NC_IMAGE:
462                         if (wmn->reference == sima->image || !wmn->reference) {
463                                 image_scopes_tag_refresh(sa);
464                                 ED_area_tag_refresh(sa);
465                                 ED_area_tag_redraw(sa);
466                         }
467                         break;
468                 case NC_SPACE:
469                         if (wmn->data == ND_SPACE_IMAGE) {
470                                 image_scopes_tag_refresh(sa);
471                                 ED_area_tag_redraw(sa);
472                         }
473                         break;
474                 case NC_MASK:
475                 {
476                         // Scene *scene = wmn->window->screen->scene;
477                         /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima) but we cant get the scene */
478                         if (sima->mode == SI_MODE_MASK) {
479                                 switch (wmn->data) {
480                                         case ND_SELECT:
481                                                 ED_area_tag_redraw(sa);
482                                                 break;
483                                         case ND_DATA:
484                                         case ND_DRAW:
485                                                 /* causes node-recalc */
486                                                 ED_area_tag_redraw(sa);
487                                                 ED_area_tag_refresh(sa);
488                                                 break;
489                                 }
490                                 switch (wmn->action) {
491                                         case NA_SELECTED:
492                                                 ED_area_tag_redraw(sa);
493                                                 break;
494                                         case NA_EDITED:
495                                                 /* causes node-recalc */
496                                                 ED_area_tag_redraw(sa);
497                                                 ED_area_tag_refresh(sa);
498                                                 break;
499                                 }
500                         }
501                         break;
502                 }
503                 case NC_GEOM:
504                         switch (wmn->data) {
505                                 case ND_DATA:
506                                 case ND_SELECT:
507                                         image_scopes_tag_refresh(sa);
508                                         ED_area_tag_refresh(sa);
509                                         ED_area_tag_redraw(sa);
510                                         break;
511                         }
512                 case NC_OBJECT:
513                 {
514                         Object *ob = (Object *)wmn->reference;
515                         switch (wmn->data) {
516                                 case ND_TRANSFORM:
517                                 case ND_MODIFIER:
518                                         if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
519                                                 ED_area_tag_refresh(sa);
520                                                 ED_area_tag_redraw(sa);
521                                         }
522                                         break;
523                         }
524                 }
525         }
526 }
527
528 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
529
530 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
531 {
532         SpaceImage *sima = CTX_wm_space_image(C);
533
534         if (CTX_data_dir(member)) {
535                 CTX_data_dir_set(result, image_context_dir);
536         }
537         else if (CTX_data_equals(member, "edit_image")) {
538                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
539                 return 1;
540         }
541         else if (CTX_data_equals(member, "edit_mask")) {
542                 Mask *mask = ED_space_image_get_mask(sima);
543                 if (mask) {
544                         CTX_data_id_pointer_set(result, &mask->id);
545                 }
546                 return TRUE;
547         }
548         return 0;
549 }
550
551 /************************** main region ***************************/
552
553 /* sets up the fields of the View2D from zoom and offset */
554 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
555 {
556         Image *ima = ED_space_image(sima);
557         float x1, y1, w, h;
558         int width, height, winx, winy;
559         
560 #if 0
561         if (image_preview_active(curarea, &width, &height)) {}
562         else
563 #endif
564         ED_space_image_get_size(sima, &width, &height);
565
566         w = width;
567         h = height;
568         
569         if (ima)
570                 h *= ima->aspy / ima->aspx;
571
572         winx = BLI_rcti_size_x(&ar->winrct) + 1;
573         winy = BLI_rcti_size_y(&ar->winrct) + 1;
574                 
575         ar->v2d.tot.xmin = 0;
576         ar->v2d.tot.ymin = 0;
577         ar->v2d.tot.xmax = w;
578         ar->v2d.tot.ymax = h;
579         
580         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
581         ar->v2d.mask.xmax = winx;
582         ar->v2d.mask.ymax = winy;
583
584         /* which part of the image space do we see? */
585         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
586         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
587
588         x1 -= sima->zoom * sima->xof;
589         y1 -= sima->zoom * sima->yof;
590         
591         /* relative display right */
592         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
593         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
594         
595         /* relative display left */
596         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
597         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
598         
599         /* normalize 0.0..1.0 */
600         ar->v2d.cur.xmin /= w;
601         ar->v2d.cur.xmax /= w;
602         ar->v2d.cur.ymin /= h;
603         ar->v2d.cur.ymax /= h;
604 }
605
606 /* add handlers, stuff you only do once or on area/region changes */
607 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
608 {
609         wmKeyMap *keymap;
610         
611         // image space manages own v2d
612         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
613
614         /* mask polls mode */
615         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
616         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
617
618         /* image paint polls for mode */
619         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
620         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
621
622         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
623         WM_event_add_keymap_handler(&ar->handlers, keymap);
624         
625         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
626         WM_event_add_keymap_handler(&ar->handlers, keymap);
627
628         /* own keymaps */
629         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
630         WM_event_add_keymap_handler(&ar->handlers, keymap);
631         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
632         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
633
634 }
635
636 static void image_main_area_draw(const bContext *C, ARegion *ar)
637 {
638         /* draw entirely, view changes should be handled here */
639         SpaceImage *sima = CTX_wm_space_image(C);
640         Object *obact = CTX_data_active_object(C);
641         Object *obedit = CTX_data_edit_object(C);
642         Mask *mask = NULL;
643         Scene *scene = CTX_data_scene(C);
644         View2D *v2d = &ar->v2d;
645         //View2DScrollers *scrollers;
646         float col[3];
647         
648         /* XXX not supported yet, disabling for now */
649         scene->r.scemode &= ~R_COMP_CROP;
650         
651         /* clear and setup matrix */
652         UI_GetThemeColor3fv(TH_BACK, col);
653         glClearColor(col[0], col[1], col[2], 0.0);
654         glClear(GL_COLOR_BUFFER_BIT);
655
656         /* put scene context variable in iuser */
657         sima->iuser.scene = scene;
658
659         /* we set view2d from own zoom and offset each time */
660         image_main_area_set_view2d(sima, ar);
661
662         /* we draw image in pixelspace */
663         draw_image_main(C, ar);
664
665         /* and uvs in 0.0-1.0 space */
666         UI_view2d_view_ortho(v2d);
667         draw_uvedit_main(sima, ar, scene, obedit, obact);
668
669         /* check for mask (delay draw) */
670         if (ED_space_image_show_uvedit(sima, obedit)) {
671                 /* pass */
672         }
673         else if (sima->mode == SI_MODE_MASK) {
674                 mask = ED_space_image_get_mask(sima);
675                 draw_image_cursor(sima, ar);
676         }
677
678         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
679
680         if (sima->flag & SI_SHOW_GPENCIL) {
681                 /* Grease Pencil too (in addition to UV's) */
682                 draw_image_grease_pencil((bContext *)C, TRUE);
683         }
684
685         /* sample line */
686         draw_image_sample_line(sima);
687
688         UI_view2d_view_restore(C);
689
690         if (sima->flag & SI_SHOW_GPENCIL) {
691                 /* draw Grease Pencil - screen space only */
692                 draw_image_grease_pencil((bContext *)C, FALSE);
693         }
694
695         if (mask) {
696                 Image *image = ED_space_image(sima);
697                 int width, height, show_viewer;
698                 float aspx, aspy;
699
700                 show_viewer = (image && image->source == IMA_SRC_VIEWER);
701
702                 if (show_viewer) {
703                         /* ED_space_image_get* will acquire image buffer which requires
704                          * lock here by the same reason why lock is needed in draw_image_main
705                          */
706                         BLI_lock_thread(LOCK_DRAW_IMAGE);
707                 }
708
709                 ED_space_image_get_size(sima, &width, &height);
710                 ED_space_image_get_aspect(sima, &aspx, &aspy);
711
712                 if (show_viewer)
713                         BLI_unlock_thread(LOCK_DRAW_IMAGE);
714
715                 ED_mask_draw_region(mask, ar,
716                                     sima->mask_info.draw_flag, sima->mask_info.draw_type,
717                                     width, height,
718                                     aspx, aspy,
719                                     TRUE, FALSE,
720                                     NULL, C);
721
722                 ED_mask_draw_frames(mask, ar, CFRA, mask->sfra, mask->efra);
723
724                 draw_image_cursor(sima, ar);
725         }
726
727         /* scrollers? */
728 #if 0
729         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
730         UI_view2d_scrollers_draw(C, v2d, scrollers);
731         UI_view2d_scrollers_free(scrollers);
732 #endif
733 }
734
735 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
736 {
737         /* context changes */
738         switch (wmn->category) {
739                 case NC_GPENCIL:
740                         if (wmn->action == NA_EDITED)
741                                 ED_region_tag_redraw(ar);
742                         break;
743         }
744 }
745
746 /* *********************** buttons region ************************ */
747
748 /* add handlers, stuff you only do once or on area/region changes */
749 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
750 {
751         wmKeyMap *keymap;
752
753         ED_region_panels_init(wm, ar);
754         
755         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
756         WM_event_add_keymap_handler(&ar->handlers, keymap);
757 }
758
759 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
760 {
761         ED_region_panels(C, ar, 1, NULL, -1);
762 }
763
764 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
765 {
766         /* context changes */
767         switch (wmn->category) {
768                 case NC_GPENCIL:
769                         if (wmn->data == ND_DATA)
770                                 ED_region_tag_redraw(ar);
771                         break;
772                 case NC_BRUSH:
773                         if (wmn->action == NA_EDITED)
774                                 ED_region_tag_redraw(ar);
775                         break;
776                 case NC_TEXTURE:
777                 case NC_MATERIAL:
778                         /* sending by texture render job and needed to properly update displaying
779                          * brush texture icon */
780                         ED_region_tag_redraw(ar);
781                         break;
782         }
783 }
784
785 /* *********************** scopes region ************************ */
786
787 /* add handlers, stuff you only do once or on area/region changes */
788 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
789 {
790         wmKeyMap *keymap;
791         
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