mask now draws in the image view, misc minor edits.
[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                         switch (wmn->data) {
434                                 case ND_SELECT:
435                                 case ND_DATA:
436                                 case ND_DRAW:
437                                         ED_area_tag_redraw(sa);
438                                         break;
439                         }
440                         switch (wmn->action) {
441                                 case NA_SELECTED:
442                                         ED_area_tag_redraw(sa);
443                                         break;
444                                 case NA_EDITED:
445                                         ED_area_tag_redraw(sa);
446                                         break;
447                         }
448                         break;
449                 case NC_GEOM:
450                         switch (wmn->data) {
451                                 case ND_DATA:
452                                 case ND_SELECT:
453                                         image_scopes_tag_refresh(sa);
454                                         ED_area_tag_refresh(sa);
455                                         ED_area_tag_redraw(sa);
456                                         break;
457                         }
458                 case NC_OBJECT:
459                 {
460                         Object *ob = (Object *)wmn->reference;
461                         switch (wmn->data) {
462                                 case ND_TRANSFORM:
463                                 case ND_MODIFIER:
464                                         if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
465                                                 ED_area_tag_refresh(sa);
466                                                 ED_area_tag_redraw(sa);
467                                         }
468                                         break;
469                         }
470                 }
471         }
472 }
473
474 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
475
476 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
477 {
478         SpaceImage *sima = CTX_wm_space_image(C);
479
480         if (CTX_data_dir(member)) {
481                 CTX_data_dir_set(result, image_context_dir);
482         }
483         else if (CTX_data_equals(member, "edit_image")) {
484                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
485                 return 1;
486         }
487         else if (CTX_data_equals(member, "edit_mask")) {
488                 Mask *mask = ED_space_image_get_mask(sima);
489                 if (mask) {
490                         CTX_data_id_pointer_set(result, &mask->id);
491                 }
492                 return TRUE;
493         }
494         return 0;
495 }
496
497 /************************** main region ***************************/
498
499 /* sets up the fields of the View2D from zoom and offset */
500 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
501 {
502         Image *ima = ED_space_image(sima);
503         float x1, y1, w, h;
504         int width, height, winx, winy;
505         
506 #if 0
507         if (image_preview_active(curarea, &width, &height)) ;
508         else
509 #endif
510         ED_space_image_size(sima, &width, &height);
511
512         w = width;
513         h = height;
514         
515         if (ima)
516                 h *= ima->aspy / ima->aspx;
517
518         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
519         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
520                 
521         ar->v2d.tot.xmin = 0;
522         ar->v2d.tot.ymin = 0;
523         ar->v2d.tot.xmax = w;
524         ar->v2d.tot.ymax = h;
525         
526         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
527         ar->v2d.mask.xmax = winx;
528         ar->v2d.mask.ymax = winy;
529
530         /* which part of the image space do we see? */
531         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
532         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
533
534         x1 -= sima->zoom * sima->xof;
535         y1 -= sima->zoom * sima->yof;
536         
537         /* relative display right */
538         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
539         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
540         
541         /* relative display left */
542         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
543         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
544         
545         /* normalize 0.0..1.0 */
546         ar->v2d.cur.xmin /= w;
547         ar->v2d.cur.xmax /= w;
548         ar->v2d.cur.ymin /= h;
549         ar->v2d.cur.ymax /= h;
550 }
551
552 /* add handlers, stuff you only do once or on area/region changes */
553 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
554 {
555         wmKeyMap *keymap;
556         
557         // image space manages own v2d
558         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
559
560         /* image paint polls for mode */
561         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
562         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
563
564         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
565         WM_event_add_keymap_handler(&ar->handlers, keymap);
566         
567         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
568         WM_event_add_keymap_handler(&ar->handlers, keymap);
569
570         /* own keymaps */
571         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
572         WM_event_add_keymap_handler(&ar->handlers, keymap);
573         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
574         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
575
576 }
577
578 static void image_main_area_draw(const bContext *C, ARegion *ar)
579 {
580         /* draw entirely, view changes should be handled here */
581         SpaceImage *sima = CTX_wm_space_image(C);
582         Object *obact = CTX_data_active_object(C);
583         Object *obedit = CTX_data_edit_object(C);
584         Scene *scene = CTX_data_scene(C);
585         View2D *v2d = &ar->v2d;
586         //View2DScrollers *scrollers;
587         float col[3];
588         
589         /* XXX not supported yet, disabling for now */
590         scene->r.scemode &= ~R_COMP_CROP;
591         
592         /* clear and setup matrix */
593         UI_GetThemeColor3fv(TH_BACK, col);
594         glClearColor(col[0], col[1], col[2], 0.0);
595         glClear(GL_COLOR_BUFFER_BIT);
596
597         /* put scene context variable in iuser */
598         sima->iuser.scene = scene;
599
600         /* we set view2d from own zoom and offset each time */
601         image_main_area_set_view2d(sima, ar);
602
603         /* we draw image in pixelspace */
604         draw_image_main(sima, ar, scene);
605
606         /* and uvs in 0.0-1.0 space */
607         UI_view2d_view_ortho(v2d);
608         draw_uvedit_main(sima, ar, scene, obedit, obact);
609
610         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
611
612         /* Grease Pencil too (in addition to UV's) */
613         draw_image_grease_pencil((bContext *)C, 1); 
614
615         /* sample line */
616         draw_image_sample_line(sima);
617
618         UI_view2d_view_restore(C);
619
620         /* draw Grease Pencil - screen space only */
621         draw_image_grease_pencil((bContext *)C, 0);
622
623         {
624                 Mask *mask = ED_space_image_get_mask(sima);
625                 if (mask) {
626                         int width, height;
627                         ED_mask_size(C, &width, &height);
628                         ED_mask_draw_region(mask, ar,
629                                             sima->mask_info.draw_flag, sima->mask_info.draw_type,
630                                             width, height,
631                                             TRUE, FALSE,
632                                             NULL, C);
633                 }
634         }
635
636         /* scrollers? */
637 #if 0
638         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
639         UI_view2d_scrollers_draw(C, v2d, scrollers);
640         UI_view2d_scrollers_free(scrollers);
641 #endif
642 }
643
644 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
645 {
646         /* context changes */
647         switch (wmn->category) {
648                 case NC_SCREEN:
649                         if (wmn->data == ND_GPENCIL)
650                                 ED_region_tag_redraw(ar);
651                         break;
652         }
653 }
654
655 /* *********************** buttons region ************************ */
656
657 /* add handlers, stuff you only do once or on area/region changes */
658 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
659 {
660         wmKeyMap *keymap;
661
662         ED_region_panels_init(wm, ar);
663         
664         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
665         WM_event_add_keymap_handler(&ar->handlers, keymap);
666 }
667
668 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
669 {
670         ED_region_panels(C, ar, 1, NULL, -1);
671 }
672
673 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
674 {
675         /* context changes */
676         switch (wmn->category) {
677                 case NC_SCREEN:
678                         if (wmn->data == ND_GPENCIL)
679                                 ED_region_tag_redraw(ar);
680                         break;
681                 case NC_BRUSH:
682                         if (wmn->action == NA_EDITED)
683                                 ED_region_tag_redraw(ar);
684                         break;
685                 case NC_TEXTURE:
686                 case NC_MATERIAL:
687                         /* sending by texture render job and needed to properly update displaying
688                          * brush texture icon */
689                         ED_region_tag_redraw(ar);
690                         break;
691         }
692 }
693
694 /* *********************** scopes region ************************ */
695
696 /* add handlers, stuff you only do once or on area/region changes */
697 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
698 {
699         wmKeyMap *keymap;
700         
701         ED_region_panels_init(wm, ar);
702         
703         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
704         WM_event_add_keymap_handler(&ar->handlers, keymap);
705 }
706
707 static void image_scope_area_draw(const bContext *C, ARegion *ar)
708 {
709         SpaceImage *sima = CTX_wm_space_image(C);
710         Scene *scene = CTX_data_scene(C);
711         void *lock;
712         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
713         if (ibuf) {
714                 if (!sima->scopes.ok) {
715                         BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
716                 }
717                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
718         }
719         ED_space_image_release_buffer(sima, lock);
720         
721         ED_region_panels(C, ar, 1, NULL, -1);
722 }
723
724 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
725 {
726         /* context changes */
727         switch (wmn->category) {
728                 case NC_SCENE:
729                         switch (wmn->data) {
730                                 case ND_MODE:
731                                 case ND_RENDER_RESULT:
732                                 case ND_COMPO_RESULT:
733                                         ED_region_tag_redraw(ar);
734                                         break;
735                         }
736                         break;
737                 case NC_IMAGE:
738                         ED_region_tag_redraw(ar);
739                         break;
740                 case NC_NODE:
741                         ED_region_tag_redraw(ar);
742                         break;
743                         
744         }
745 }
746
747 /************************* header region **************************/
748
749 /* add handlers, stuff you only do once or on area/region changes */
750 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
751 {
752         ED_region_header_init(ar);
753 }
754
755 static void image_header_area_draw(const bContext *C, ARegion *ar)
756 {
757         ED_region_header(C, ar);
758 }
759
760 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
761 {
762         /* context changes */
763         switch (wmn->category) {
764                 case NC_SCENE:
765                         switch (wmn->data) {
766                                 case ND_MODE:
767                                 case ND_TOOLSETTINGS:
768                                         ED_region_tag_redraw(ar);
769                                         break;
770                         }
771                         break;
772                 case NC_GEOM:
773                         switch (wmn->data) {
774                                 case ND_DATA:
775                                 case ND_SELECT:
776                                         ED_region_tag_redraw(ar);
777                                         break;
778                         }
779                         
780         }
781 }
782
783 /**************************** spacetype *****************************/
784
785 /* only called once, from space/spacetypes.c */
786 void ED_spacetype_image(void)
787 {
788         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
789         ARegionType *art;
790         
791         st->spaceid = SPACE_IMAGE;
792         strncpy(st->name, "Image", BKE_ST_MAXNAME);
793         
794         st->new = image_new;
795         st->free = image_free;
796         st->init = image_init;
797         st->duplicate = image_duplicate;
798         st->operatortypes = image_operatortypes;
799         st->keymap = image_keymap;
800         st->dropboxes = image_dropboxes;
801         st->refresh = image_refresh;
802         st->listener = image_listener;
803         st->context = image_context;
804         
805         /* regions: main window */
806         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
807         art->regionid = RGN_TYPE_WINDOW;
808         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
809         art->init = image_main_area_init;
810         art->draw = image_main_area_draw;
811         art->listener = image_main_area_listener;
812
813         BLI_addhead(&st->regiontypes, art);
814         
815         /* regions: listview/buttons */
816         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
817         art->regionid = RGN_TYPE_UI;
818         art->prefsizex = 220; // XXX
819         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
820         art->listener = image_buttons_area_listener;
821         art->init = image_buttons_area_init;
822         art->draw = image_buttons_area_draw;
823         BLI_addhead(&st->regiontypes, art);
824
825         image_buttons_register(art);
826         ED_uvedit_buttons_register(art);
827         
828         /* regions: statistics/scope buttons */
829         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
830         art->regionid = RGN_TYPE_PREVIEW;
831         art->prefsizex = 220; // XXX
832         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
833         art->listener = image_scope_area_listener;
834         art->init = image_scope_area_init;
835         art->draw = image_scope_area_draw;
836         BLI_addhead(&st->regiontypes, art);
837
838         /* regions: header */
839         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
840         art->regionid = RGN_TYPE_HEADER;
841         art->prefsizey = HEADERY;
842         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
843         art->listener = image_header_area_listener;
844         art->init = image_header_area_init;
845         art->draw = image_header_area_draw;
846         
847         BLI_addhead(&st->regiontypes, art);
848         
849         BKE_spacetype_register(st);
850 }
851