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