3c9c46c1c75e0327ecacb0ffdd72a1058e321056
[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_get_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         /* mask polls mode */
561         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
562         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
563
564         /* image paint polls for mode */
565         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
566         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
567
568         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
569         WM_event_add_keymap_handler(&ar->handlers, keymap);
570         
571         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
572         WM_event_add_keymap_handler(&ar->handlers, keymap);
573
574         /* own keymaps */
575         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
576         WM_event_add_keymap_handler(&ar->handlers, keymap);
577         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
578         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
579
580 }
581
582 static void image_main_area_draw(const bContext *C, ARegion *ar)
583 {
584         /* draw entirely, view changes should be handled here */
585         SpaceImage *sima = CTX_wm_space_image(C);
586         Object *obact = CTX_data_active_object(C);
587         Object *obedit = CTX_data_edit_object(C);
588         Scene *scene = CTX_data_scene(C);
589         View2D *v2d = &ar->v2d;
590         //View2DScrollers *scrollers;
591         float col[3];
592         
593         /* XXX not supported yet, disabling for now */
594         scene->r.scemode &= ~R_COMP_CROP;
595         
596         /* clear and setup matrix */
597         UI_GetThemeColor3fv(TH_BACK, col);
598         glClearColor(col[0], col[1], col[2], 0.0);
599         glClear(GL_COLOR_BUFFER_BIT);
600
601         /* put scene context variable in iuser */
602         sima->iuser.scene = scene;
603
604         /* we set view2d from own zoom and offset each time */
605         image_main_area_set_view2d(sima, ar);
606
607         /* we draw image in pixelspace */
608         draw_image_main(sima, ar, scene);
609
610         /* and uvs in 0.0-1.0 space */
611         UI_view2d_view_ortho(v2d);
612         draw_uvedit_main(sima, ar, scene, obedit, obact);
613
614         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
615
616         /* Grease Pencil too (in addition to UV's) */
617         draw_image_grease_pencil((bContext *)C, 1); 
618
619         /* sample line */
620         draw_image_sample_line(sima);
621
622         UI_view2d_view_restore(C);
623
624         /* draw Grease Pencil - screen space only */
625         draw_image_grease_pencil((bContext *)C, 0);
626
627         {
628                 Mask *mask = ED_space_image_get_mask(sima);
629                 if (mask) {
630                         int width, height;
631                         ED_mask_size(C, &width, &height);
632                         ED_mask_draw_region(mask, ar,
633                                             sima->mask_info.draw_flag, sima->mask_info.draw_type,
634                                             width, height,
635                                             TRUE, FALSE,
636                                             NULL, C);
637                 }
638         }
639
640         /* scrollers? */
641 #if 0
642         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
643         UI_view2d_scrollers_draw(C, v2d, scrollers);
644         UI_view2d_scrollers_free(scrollers);
645 #endif
646 }
647
648 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
649 {
650         /* context changes */
651         switch (wmn->category) {
652                 case NC_SCREEN:
653                         if (wmn->data == ND_GPENCIL)
654                                 ED_region_tag_redraw(ar);
655                         break;
656         }
657 }
658
659 /* *********************** buttons region ************************ */
660
661 /* add handlers, stuff you only do once or on area/region changes */
662 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
663 {
664         wmKeyMap *keymap;
665
666         ED_region_panels_init(wm, ar);
667         
668         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
669         WM_event_add_keymap_handler(&ar->handlers, keymap);
670 }
671
672 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
673 {
674         ED_region_panels(C, ar, 1, NULL, -1);
675 }
676
677 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
678 {
679         /* context changes */
680         switch (wmn->category) {
681                 case NC_SCREEN:
682                         if (wmn->data == ND_GPENCIL)
683                                 ED_region_tag_redraw(ar);
684                         break;
685                 case NC_BRUSH:
686                         if (wmn->action == NA_EDITED)
687                                 ED_region_tag_redraw(ar);
688                         break;
689                 case NC_TEXTURE:
690                 case NC_MATERIAL:
691                         /* sending by texture render job and needed to properly update displaying
692                          * brush texture icon */
693                         ED_region_tag_redraw(ar);
694                         break;
695         }
696 }
697
698 /* *********************** scopes region ************************ */
699
700 /* add handlers, stuff you only do once or on area/region changes */
701 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
702 {
703         wmKeyMap *keymap;
704         
705         ED_region_panels_init(wm, ar);
706         
707         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
708         WM_event_add_keymap_handler(&ar->handlers, keymap);
709 }
710
711 static void image_scope_area_draw(const bContext *C, ARegion *ar)
712 {
713         SpaceImage *sima = CTX_wm_space_image(C);
714         Scene *scene = CTX_data_scene(C);
715         void *lock;
716         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
717         if (ibuf) {
718                 if (!sima->scopes.ok) {
719                         BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
720                 }
721                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
722         }
723         ED_space_image_release_buffer(sima, lock);
724         
725         ED_region_panels(C, ar, 1, NULL, -1);
726 }
727
728 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
729 {
730         /* context changes */
731         switch (wmn->category) {
732                 case NC_SCENE:
733                         switch (wmn->data) {
734                                 case ND_MODE:
735                                 case ND_RENDER_RESULT:
736                                 case ND_COMPO_RESULT:
737                                         ED_region_tag_redraw(ar);
738                                         break;
739                         }
740                         break;
741                 case NC_IMAGE:
742                         ED_region_tag_redraw(ar);
743                         break;
744                 case NC_NODE:
745                         ED_region_tag_redraw(ar);
746                         break;
747                         
748         }
749 }
750
751 /************************* header region **************************/
752
753 /* add handlers, stuff you only do once or on area/region changes */
754 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
755 {
756         ED_region_header_init(ar);
757 }
758
759 static void image_header_area_draw(const bContext *C, ARegion *ar)
760 {
761         ED_region_header(C, ar);
762 }
763
764 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
765 {
766         /* context changes */
767         switch (wmn->category) {
768                 case NC_SCENE:
769                         switch (wmn->data) {
770                                 case ND_MODE:
771                                 case ND_TOOLSETTINGS:
772                                         ED_region_tag_redraw(ar);
773                                         break;
774                         }
775                         break;
776                 case NC_GEOM:
777                         switch (wmn->data) {
778                                 case ND_DATA:
779                                 case ND_SELECT:
780                                         ED_region_tag_redraw(ar);
781                                         break;
782                         }
783                         
784         }
785 }
786
787 /**************************** spacetype *****************************/
788
789 /* only called once, from space/spacetypes.c */
790 void ED_spacetype_image(void)
791 {
792         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
793         ARegionType *art;
794         
795         st->spaceid = SPACE_IMAGE;
796         strncpy(st->name, "Image", BKE_ST_MAXNAME);
797         
798         st->new = image_new;
799         st->free = image_free;
800         st->init = image_init;
801         st->duplicate = image_duplicate;
802         st->operatortypes = image_operatortypes;
803         st->keymap = image_keymap;
804         st->dropboxes = image_dropboxes;
805         st->refresh = image_refresh;
806         st->listener = image_listener;
807         st->context = image_context;
808         
809         /* regions: main window */
810         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
811         art->regionid = RGN_TYPE_WINDOW;
812         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
813         art->init = image_main_area_init;
814         art->draw = image_main_area_draw;
815         art->listener = image_main_area_listener;
816
817         BLI_addhead(&st->regiontypes, art);
818         
819         /* regions: listview/buttons */
820         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
821         art->regionid = RGN_TYPE_UI;
822         art->prefsizex = 220; // XXX
823         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
824         art->listener = image_buttons_area_listener;
825         art->init = image_buttons_area_init;
826         art->draw = image_buttons_area_draw;
827         BLI_addhead(&st->regiontypes, art);
828
829         image_buttons_register(art);
830         ED_uvedit_buttons_register(art);
831         
832         /* regions: statistics/scope buttons */
833         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
834         art->regionid = RGN_TYPE_PREVIEW;
835         art->prefsizex = 220; // XXX
836         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
837         art->listener = image_scope_area_listener;
838         art->init = image_scope_area_init;
839         art->draw = image_scope_area_draw;
840         BLI_addhead(&st->regiontypes, art);
841
842         /* regions: header */
843         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
844         art->regionid = RGN_TYPE_HEADER;
845         art->prefsizey = HEADERY;
846         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
847         art->listener = image_header_area_listener;
848         art->init = image_header_area_init;
849         art->draw = image_header_area_draw;
850         
851         BLI_addhead(&st->regiontypes, art);
852         
853         BKE_spacetype_register(st);
854 }
855