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