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