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