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