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