Fix #23208: REGION_DRAW_POST_PIXEL callbacks only work in 3d view and
[blender.git] / source / blender / editors / space_image / space_image.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_meshdata_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_math.h"
40 #include "BLI_editVert.h"
41 #include "BLI_rand.h"
42
43 #include "BKE_colortools.h"
44 #include "BKE_context.h"
45 #include "BKE_image.h"
46 #include "BKE_mesh.h"
47 #include "BKE_screen.h"
48 #include "BKE_utildefines.h"
49
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
52
53 #include "ED_mesh.h"
54 #include "ED_space_api.h"
55 #include "ED_screen.h"
56 #include "ED_uvedit.h"
57
58 #include "BIF_gl.h"
59 #include "BIF_glutil.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 /* note; image_panel_properties() uses pointer to sima->image directly */
74 Image *ED_space_image(SpaceImage *sima)
75 {
76         return sima->image;
77 }
78
79 /* called to assign images to UV faces */
80 void ED_space_image_set(bContext *C, SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
81 {
82         ED_uvedit_assign_image(scene, obedit, ima, sima->image);
83         
84         /* change the space ima after because uvedit_face_visible uses the space ima
85          * to check if the face is displayed in UV-localview */
86         sima->image= ima;
87         
88         if(ima == NULL || ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
89                 sima->flag &= ~SI_DRAWTOOL;
90         
91         if(sima->image)
92                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
93         
94         if(sima->image && sima->image->id.us==0)
95                 sima->image->id.us= 1;
96         
97         if(C) {
98                 if(obedit)
99                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
100                 
101                 ED_area_tag_redraw(CTX_wm_area(C));
102                 
103         }
104 }
105
106 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
107 {
108         ImBuf *ibuf;
109         
110         if(sima && sima->image) {
111 #if 0
112                 if(sima->image->type==IMA_TYPE_R_RESULT && BIF_show_render_spare())
113                         return BIF_render_spare_imbuf();
114                 else
115 #endif
116                         ibuf= BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
117                 
118                 if(ibuf && (ibuf->rect || ibuf->rect_float))
119                         return ibuf;
120         }
121         
122         return NULL;
123 }
124
125 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
126 {
127         if(sima && sima->image)
128                 BKE_image_release_ibuf(sima->image, lock);
129 }
130
131 int ED_space_image_has_buffer(SpaceImage *sima)
132 {
133         ImBuf *ibuf;
134         void *lock;
135         int has_buffer;
136         
137         ibuf= ED_space_image_acquire_buffer(sima, &lock);
138         has_buffer= (ibuf != NULL);
139         ED_space_image_release_buffer(sima, lock);
140         
141         return has_buffer;
142 }
143
144 void ED_image_size(Image *ima, int *width, int *height)
145 {
146         ImBuf *ibuf= NULL;
147         void *lock;
148         
149         if(ima)
150                 ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
151         
152         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
153                 *width= ibuf->x;
154                 *height= ibuf->y;
155         }
156         else {
157                 *width= 256;
158                 *height= 256;
159         }
160         
161         if(ima)
162                 BKE_image_release_ibuf(ima, lock);
163 }
164
165 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
166 {
167         Scene *scene= sima->iuser.scene;
168         ImBuf *ibuf;
169         void *lock;
170         
171         ibuf= ED_space_image_acquire_buffer(sima, &lock);
172         
173         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
174                 *width= ibuf->x;
175                 *height= ibuf->y;
176         }
177         else if(sima->image && sima->image->type==IMA_TYPE_R_RESULT && scene) {
178                 /* not very important, just nice */
179                 *width= (scene->r.xsch*scene->r.size)/100;
180                 *height= (scene->r.ysch*scene->r.size)/100;
181
182                 if((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
183                         *width *= (scene->r.border.xmax - scene->r.border.xmin);
184                         *height *= (scene->r.border.ymax - scene->r.border.ymin);
185                 }
186
187         }
188         /* I know a bit weak... but preview uses not actual image size */
189         // XXX else if(image_preview_active(sima, width, height));
190         else {
191                 *width= 256;
192                 *height= 256;
193         }
194         
195         ED_space_image_release_buffer(sima, lock);
196 }
197
198 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
199 {
200         *aspx= *aspy= 1.0;
201         
202         if((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
203            (ima->aspx==0.0 || ima->aspy==0.0))
204                 return;
205         
206         /* x is always 1 */
207         *aspy = ima->aspy/ima->aspx;
208 }
209
210 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
211 {
212         ED_image_aspect(ED_space_image(sima), aspx, aspy);
213 }
214
215 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
216 {
217         int width, height;
218         
219         ED_space_image_size(sima, &width, &height);
220         
221         *zoomx= (float)(ar->winrct.xmax - ar->winrct.xmin + 1)/(float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin)*width);
222         *zoomy= (float)(ar->winrct.ymax - ar->winrct.ymin + 1)/(float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin)*height);
223 }
224
225 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
226 {
227         int w, h;
228         
229         ED_space_image_aspect(sima, aspx, aspy);
230         ED_space_image_size(sima, &w, &h);
231         
232         *aspx *= (float)w/256.0f;
233         *aspy *= (float)h/256.0f;
234 }
235
236 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
237 {
238         int w, h;
239         
240         ED_image_aspect(ima, aspx, aspy);
241         ED_image_size(ima, &w, &h);
242         
243         *aspx *= (float)w;
244         *aspy *= (float)h;
245 }
246
247 int ED_space_image_show_render(SpaceImage *sima)
248 {
249         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
250 }
251
252 int ED_space_image_show_paint(SpaceImage *sima)
253 {
254         if(ED_space_image_show_render(sima))
255                 return 0;
256         
257         return (sima->flag & SI_DRAWTOOL);
258 }
259
260 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
261 {
262         if(ED_space_image_show_render(sima))
263                 return 0;
264         if(ED_space_image_show_paint(sima))
265                 return 0;
266         
267         if(obedit && obedit->type == OB_MESH) {
268                 EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
269                 int ret;
270                 
271                 ret = EM_texFaceCheck(em);
272                 
273                 BKE_mesh_end_editmesh(obedit->data, em);
274                 return ret;
275         }
276         
277         return 0;
278 }
279
280 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
281 {
282         if(ED_space_image_show_render(sima))
283                 return 0;
284         
285         if(ED_space_image_show_paint(sima))
286                 if(obedit && obedit->type == OB_MESH) {
287                         EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
288                         int ret;
289                         
290                         ret = EM_texFaceCheck(em);
291                         
292                         BKE_mesh_end_editmesh(obedit->data, em);
293                         return ret;
294                 }
295         
296         return 0;
297 }
298
299
300 static void image_scopes_tag_refresh(ScrArea *sa)
301 {
302         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
303         ARegion *ar;
304
305         /* only while histogram is visible */
306         for (ar=sa->regionbase.first; ar; ar=ar->next) {
307                 if (ar->regiontype == RGN_TYPE_PREVIEW && ar->flag & RGN_FLAG_HIDDEN)
308                         return;
309         }
310
311         sima->scopes.ok=0;
312 }
313
314
315 /* ******************** manage regions ********************* */
316
317 ARegion *image_has_buttons_region(ScrArea *sa)
318 {
319         ARegion *ar, *arnew;
320         
321         for(ar= sa->regionbase.first; ar; ar= ar->next)
322                 if(ar->regiontype==RGN_TYPE_UI)
323                         return ar;
324         
325         /* add subdiv level; after header */
326         for(ar= sa->regionbase.first; ar; ar= ar->next)
327                 if(ar->regiontype==RGN_TYPE_HEADER)
328                         break;
329         
330         /* is error! */
331         if(ar==NULL) return NULL;
332         
333         arnew= MEM_callocN(sizeof(ARegion), "buttons for image");
334         
335         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
336         arnew->regiontype= RGN_TYPE_UI;
337         arnew->alignment= RGN_ALIGN_LEFT;
338         
339         arnew->flag = RGN_FLAG_HIDDEN;
340         
341         return arnew;
342 }
343
344 ARegion *image_has_scope_region(ScrArea *sa)
345 {
346         ARegion *ar, *arnew;
347         
348         for(ar= sa->regionbase.first; ar; ar= ar->next)
349                 if(ar->regiontype==RGN_TYPE_PREVIEW)
350                         return ar;
351         
352         /* add subdiv level; after buttons */
353         for(ar= sa->regionbase.first; ar; ar= ar->next)
354                 if(ar->regiontype==RGN_TYPE_UI)
355                         break;
356         
357         /* is error! */
358         if(ar==NULL) return NULL;
359         
360         arnew= MEM_callocN(sizeof(ARegion), "scopes for image");
361         
362         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
363         arnew->regiontype= RGN_TYPE_PREVIEW;
364         arnew->alignment= RGN_ALIGN_RIGHT;
365         
366         arnew->flag = RGN_FLAG_HIDDEN;
367
368         image_scopes_tag_refresh(sa);
369         
370         return arnew;
371 }
372
373 /* ******************** default callbacks for image space ***************** */
374
375 static SpaceLink *image_new(const bContext *C)
376 {
377         ARegion *ar;
378         SpaceImage *simage;
379         
380         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
381         simage->spacetype= SPACE_IMAGE;
382         simage->zoom= 1;
383         simage->lock= 1;
384         
385         simage->iuser.ok= 1;
386         simage->iuser.fie_ima= 2;
387         simage->iuser.frames= 100;
388         
389         scopes_new(&simage->scopes);
390
391         /* header */
392         ar= MEM_callocN(sizeof(ARegion), "header for image");
393         
394         BLI_addtail(&simage->regionbase, ar);
395         ar->regiontype= RGN_TYPE_HEADER;
396         ar->alignment= RGN_ALIGN_BOTTOM;
397         
398         /* buttons/list view */
399         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
400         
401         BLI_addtail(&simage->regionbase, ar);
402         ar->regiontype= RGN_TYPE_UI;
403         ar->alignment= RGN_ALIGN_LEFT;
404         ar->flag = RGN_FLAG_HIDDEN;
405         
406         /* scopes */
407         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
408         
409         BLI_addtail(&simage->regionbase, ar);
410         ar->regiontype= RGN_TYPE_PREVIEW;
411         ar->alignment= RGN_ALIGN_RIGHT;
412         ar->flag = RGN_FLAG_HIDDEN;
413
414         /* main area */
415         ar= MEM_callocN(sizeof(ARegion), "main area for image");
416         
417         BLI_addtail(&simage->regionbase, ar);
418         ar->regiontype= RGN_TYPE_WINDOW;
419         
420         return (SpaceLink *)simage;
421 }
422
423 /* not spacelink itself */
424 static void image_free(SpaceLink *sl)
425 {       
426         SpaceImage *simage= (SpaceImage*) sl;
427         
428         if(simage->cumap)
429                 curvemapping_free(simage->cumap);
430         scopes_free(&simage->scopes);
431 }
432
433
434 /* spacetype; init callback, add handlers */
435 static void image_init(struct wmWindowManager *wm, ScrArea *sa)
436 {
437         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
438
439         /* add drop boxes */
440         WM_event_add_dropbox_handler(&sa->handlers, lb);
441         
442 }
443
444 static SpaceLink *image_duplicate(SpaceLink *sl)
445 {
446         SpaceImage *simagen= MEM_dupallocN(sl);
447         
448         /* clear or remove stuff from old */
449         if(simagen->cumap)
450                 simagen->cumap= curvemapping_copy(simagen->cumap);
451         
452         return (SpaceLink *)simagen;
453 }
454
455 void image_operatortypes(void)
456 {
457         WM_operatortype_append(IMAGE_OT_view_all);
458         WM_operatortype_append(IMAGE_OT_view_pan);
459         WM_operatortype_append(IMAGE_OT_view_selected);
460         WM_operatortype_append(IMAGE_OT_view_zoom);
461         WM_operatortype_append(IMAGE_OT_view_zoom_in);
462         WM_operatortype_append(IMAGE_OT_view_zoom_out);
463         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
464
465         WM_operatortype_append(IMAGE_OT_new);
466         WM_operatortype_append(IMAGE_OT_open);
467         WM_operatortype_append(IMAGE_OT_replace);
468         WM_operatortype_append(IMAGE_OT_reload);
469         WM_operatortype_append(IMAGE_OT_save);
470         WM_operatortype_append(IMAGE_OT_save_as);
471         WM_operatortype_append(IMAGE_OT_save_sequence);
472         WM_operatortype_append(IMAGE_OT_pack);
473         WM_operatortype_append(IMAGE_OT_unpack);
474
475         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
476
477         WM_operatortype_append(IMAGE_OT_sample);
478         WM_operatortype_append(IMAGE_OT_sample_line);
479         WM_operatortype_append(IMAGE_OT_curves_point_set);
480
481         WM_operatortype_append(IMAGE_OT_record_composite);
482
483         WM_operatortype_append(IMAGE_OT_toolbox);
484         WM_operatortype_append(IMAGE_OT_properties);
485         WM_operatortype_append(IMAGE_OT_scopes);
486 }
487
488 void image_keymap(struct wmKeyConfig *keyconf)
489 {
490         wmKeyMap *keymap= WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
491         
492         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
493         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
494         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
495         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
496         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
497         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
498         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", PKEY, KM_PRESS, 0, 0);
499
500         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
501         
502         keymap= WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
503         
504         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
505         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
506         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
507         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
508
509         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
510         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
511         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
512         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
513         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
514         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
515
516         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
517         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
518         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
519         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
520         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
521         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
522         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
523
524         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
525         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
526         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
527
528         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
529 }
530
531 /* dropboxes */
532 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
533 {
534         if(drag->type==WM_DRAG_PATH)
535                 if(ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK))      /* rule might not work? */
536                         return 1;
537         return 0;
538 }
539
540 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
541 {
542         /* copy drag path to properties */
543         RNA_string_set(drop->ptr, "filepath", drag->path);
544 }
545
546 /* area+region dropbox definition */
547 static void image_dropboxes(void)
548 {
549         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
550         
551         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
552 }
553
554
555
556 static void image_refresh(const bContext *C, ScrArea *sa)
557 {
558         SpaceImage *sima= CTX_wm_space_image(C);
559         Object *obedit= CTX_data_edit_object(C);
560         Image *ima;
561
562         ima= ED_space_image(sima);
563
564         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
565                 BKE_image_user_calc_frame(&sima->iuser, CTX_data_scene(C)->r.cfra, 0);
566         
567         /* check if we have to set the image from the editmesh */
568         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
569         else if(obedit && obedit->type == OB_MESH) {
570                 Mesh *me= (Mesh*)obedit->data;
571                 EditMesh *em= BKE_mesh_get_editmesh(me);
572                 MTFace *tf;
573                 
574                 if(em && EM_texFaceCheck(em)) {
575                         sima->image= ima= NULL;
576                         
577                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
578                         
579                         if(tf && (tf->mode & TF_TEX)) {
580                                 /* don't need to check for pin here, see above */
581                                 sima->image= ima= tf->tpage;
582                                 
583                                 if(sima->flag & SI_EDITTILE);
584                                 else sima->curtile= tf->tile;
585                         }
586                 }
587
588                 BKE_mesh_end_editmesh(obedit->data, em);
589         }
590 }
591
592 static void image_listener(ScrArea *sa, wmNotifier *wmn)
593 {
594         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
595         
596         /* context changes */
597         switch(wmn->category) {
598                 case NC_SCENE:
599                         switch(wmn->data) {
600                                 case ND_FRAME:
601                                         image_scopes_tag_refresh(sa);
602                                         ED_area_tag_refresh(sa);
603                                         ED_area_tag_redraw(sa);                                 
604                                         break;
605                                 case ND_MODE:
606                                 case ND_RENDER_RESULT:
607                                 case ND_COMPO_RESULT:
608                                         if (ED_space_image_show_render(sima))
609                                                 image_scopes_tag_refresh(sa);
610                                         ED_area_tag_refresh(sa);
611                                         ED_area_tag_redraw(sa);                                 
612                                         break;
613                         }
614                         break;
615                 case NC_IMAGE:
616                         if (wmn->reference == sima->image || !wmn->reference) {
617                                 image_scopes_tag_refresh(sa);
618                                 ED_area_tag_refresh(sa);
619                                 ED_area_tag_redraw(sa);
620                         }
621                         break;
622                 case NC_SPACE:  
623                         if(wmn->data == ND_SPACE_IMAGE) {
624                                 image_scopes_tag_refresh(sa);
625                                 ED_area_tag_redraw(sa);
626                         }
627                         break;
628                 case NC_GEOM:
629                         switch(wmn->data) {
630                                 case ND_DATA:
631                                 case ND_SELECT:
632                                         ED_area_tag_refresh(sa);
633                                         ED_area_tag_redraw(sa);
634                                         break;
635                         }
636                 case NC_OBJECT:
637                         switch(wmn->data) {
638                                 case ND_TRANSFORM:
639                                         if(sima->lock && (sima->flag & SI_DRAWSHADOW)) {
640                                                 ED_area_tag_refresh(sa);
641                                                 ED_area_tag_redraw(sa);
642                                         }
643                                         break;
644                         }
645         }
646 }
647
648 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
649 {
650         SpaceImage *sima= CTX_wm_space_image(C);
651
652         if(CTX_data_dir(member)) {
653                 static const char *dir[] = {"edit_image", NULL};
654                 CTX_data_dir_set(result, dir);
655         }
656         else if(CTX_data_equals(member, "edit_image")) {
657                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
658                 return 1;
659         }
660
661         return 0;
662 }
663
664 /************************** main region ***************************/
665
666 /* sets up the fields of the View2D from zoom and offset */
667 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
668 {
669         Image *ima= ED_space_image(sima);
670         float x1, y1, w, h;
671         int width, height, winx, winy;
672         
673 #if 0
674         if(image_preview_active(curarea, &width, &height));
675         else
676 #endif
677         ED_space_image_size(sima, &width, &height);
678
679         w= width;
680         h= height;
681         
682         if(ima)
683                 h *= ima->aspy/ima->aspx;
684
685         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
686         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
687                 
688         ar->v2d.tot.xmin= 0;
689         ar->v2d.tot.ymin= 0;
690         ar->v2d.tot.xmax= w;
691         ar->v2d.tot.ymax= h;
692         
693         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
694         ar->v2d.mask.xmax= winx;
695         ar->v2d.mask.ymax= winy;
696
697         /* which part of the image space do we see? */
698         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
699         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
700
701         x1-= sima->zoom*sima->xof;
702         y1-= sima->zoom*sima->yof;
703         
704         /* relative display right */
705         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
706         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
707         
708         /* relative display left */
709         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
710         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
711         
712         /* normalize 0.0..1.0 */
713         ar->v2d.cur.xmin /= w;
714         ar->v2d.cur.xmax /= w;
715         ar->v2d.cur.ymin /= h;
716         ar->v2d.cur.ymax /= h;
717 }
718
719 /* add handlers, stuff you only do once or on area/region changes */
720 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
721 {
722         wmKeyMap *keymap;
723         
724         // image space manages own v2d
725         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
726
727         /* image paint polls for mode */
728         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
729         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
730
731         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
732         WM_event_add_keymap_handler(&ar->handlers, keymap);
733         
734         /* own keymaps */
735         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
736         WM_event_add_keymap_handler(&ar->handlers, keymap);
737         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
738         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
739
740 }
741
742 static void image_main_area_draw(const bContext *C, ARegion *ar)
743 {
744         /* draw entirely, view changes should be handled here */
745         SpaceImage *sima= CTX_wm_space_image(C);
746         Object *obedit= CTX_data_edit_object(C);
747         Scene *scene= CTX_data_scene(C);
748         View2D *v2d= &ar->v2d;
749         //View2DScrollers *scrollers;
750         float col[3];
751         
752         /* clear and setup matrix */
753         UI_GetThemeColor3fv(TH_BACK, col);
754         glClearColor(col[0], col[1], col[2], 0.0);
755         glClear(GL_COLOR_BUFFER_BIT);
756
757         /* put scene context variable in iuser */
758         sima->iuser.scene= scene;
759
760         /* we set view2d from own zoom and offset each time */
761         image_main_area_set_view2d(sima, ar);
762         
763         /* we draw image in pixelspace */
764         draw_image_main(sima, ar, scene);
765
766         /* and uvs in 0.0-1.0 space */
767         UI_view2d_view_ortho(C, v2d);
768         draw_uvedit_main(sima, ar, scene, obedit);
769
770         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
771                 
772         /* Grease Pencil too (in addition to UV's) */
773         draw_image_grease_pencil((bContext *)C, 1); 
774
775         UI_view2d_view_restore(C);
776
777         /* draw Grease Pencil - screen space only */
778         draw_image_grease_pencil((bContext *)C, 0);
779         
780         /* scrollers? */
781         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
782         UI_view2d_scrollers_draw(C, v2d, scrollers);
783         UI_view2d_scrollers_free(scrollers);*/
784 }
785
786 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
787 {
788         /* context changes */
789         switch(wmn->category) {
790                 case NC_SCREEN:
791                         if (wmn->data==ND_GPENCIL)
792                                 ED_region_tag_redraw(ar);
793                 break;
794         }
795 }
796
797 /* *********************** buttons region ************************ */
798
799 /* add handlers, stuff you only do once or on area/region changes */
800 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
801 {
802         wmKeyMap *keymap;
803
804         ED_region_panels_init(wm, ar);
805         
806         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
807         WM_event_add_keymap_handler(&ar->handlers, keymap);
808 }
809
810 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
811 {
812         ED_region_panels(C, ar, 1, NULL, -1);
813 }
814
815 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
816 {
817         /* context changes */
818         switch(wmn->category) {
819                 case NC_SCREEN:
820                         if (wmn->data==ND_GPENCIL)
821                                 ED_region_tag_redraw(ar);
822                         break;
823                 case NC_BRUSH:
824                         if(wmn->action==NA_EDITED)
825                                 ED_region_tag_redraw(ar);
826                         break;
827         }
828 }
829
830 /* *********************** scopes region ************************ */
831
832 /* add handlers, stuff you only do once or on area/region changes */
833 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
834 {
835         wmKeyMap *keymap;
836         
837         ED_region_panels_init(wm, ar);
838         
839         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
840         WM_event_add_keymap_handler(&ar->handlers, keymap);
841 }
842
843 static void image_scope_area_draw(const bContext *C, ARegion *ar)
844 {
845         SpaceImage *sima= CTX_wm_space_image(C);
846         Scene *scene= CTX_data_scene(C);
847         void *lock;
848         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
849         if(ibuf) {
850                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT );
851         }
852         ED_space_image_release_buffer(sima, lock);
853         
854         ED_region_panels(C, ar, 1, NULL, -1);
855 }
856
857 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
858 {
859         /* context changes */
860         switch(wmn->category) {
861                 case NC_SCENE:
862                         switch(wmn->data) {
863                                 case ND_MODE:
864                                 case ND_RENDER_RESULT:
865                                 case ND_COMPO_RESULT:
866                                         ED_region_tag_redraw(ar);
867                                         break;
868                         }
869                         break;
870                 case NC_IMAGE:
871                         ED_region_tag_redraw(ar);
872                         break;
873                 case NC_NODE:
874                         ED_region_tag_redraw(ar);
875                         break;
876                         
877         }
878 }
879
880 /************************* header region **************************/
881
882 /* add handlers, stuff you only do once or on area/region changes */
883 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
884 {
885         ED_region_header_init(ar);
886 }
887
888 static void image_header_area_draw(const bContext *C, ARegion *ar)
889 {
890         ED_region_header(C, ar);
891 }
892
893 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
894 {
895         /* context changes */
896         switch(wmn->category) {
897                 case NC_SCENE:
898                         switch(wmn->data) {
899                                 case ND_MODE:
900                                 case ND_TOOLSETTINGS:
901                                         ED_region_tag_redraw(ar);
902                                         break;
903                         }
904                         break;
905                 case NC_GEOM:
906                         switch(wmn->data) {
907                                 case ND_DATA:
908                                 case ND_SELECT:
909                                         ED_region_tag_redraw(ar);
910                                         break;
911                         }
912                         
913         }
914 }
915
916 /**************************** spacetype *****************************/
917
918 /* only called once, from space/spacetypes.c */
919 void ED_spacetype_image(void)
920 {
921         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
922         ARegionType *art;
923         
924         st->spaceid= SPACE_IMAGE;
925         strncpy(st->name, "Image", BKE_ST_MAXNAME);
926         
927         st->new= image_new;
928         st->free= image_free;
929         st->init= image_init;
930         st->duplicate= image_duplicate;
931         st->operatortypes= image_operatortypes;
932         st->keymap= image_keymap;
933         st->dropboxes= image_dropboxes;
934         st->refresh= image_refresh;
935         st->listener= image_listener;
936         st->context= image_context;
937         
938         /* regions: main window */
939         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
940         art->regionid = RGN_TYPE_WINDOW;
941         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
942         art->init= image_main_area_init;
943         art->draw= image_main_area_draw;
944         art->listener= image_main_area_listener;
945
946         BLI_addhead(&st->regiontypes, art);
947         
948         /* regions: listview/buttons */
949         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
950         art->regionid = RGN_TYPE_UI;
951         art->prefsizex= 220; // XXX
952         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
953         art->listener= image_buttons_area_listener;
954         art->init= image_buttons_area_init;
955         art->draw= image_buttons_area_draw;
956         BLI_addhead(&st->regiontypes, art);
957
958         image_buttons_register(art);
959         
960         /* regions: statistics/scope buttons */
961         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
962         art->regionid = RGN_TYPE_PREVIEW;
963         art->minsizex= 220; // XXX
964         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
965         art->listener= image_scope_area_listener;
966         art->init= image_scope_area_init;
967         art->draw= image_scope_area_draw;
968         BLI_addhead(&st->regiontypes, art);
969
970         /* regions: header */
971         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
972         art->regionid = RGN_TYPE_HEADER;
973         art->prefsizey= HEADERY;
974         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
975         art->listener= image_header_area_listener;
976         art->init= image_header_area_init;
977         art->draw= image_header_area_draw;
978         
979         BLI_addhead(&st->regiontypes, art);
980         
981         BKE_spacetype_register(st);
982 }
983