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