0510bc7e4cb9c8f7375c19edf4c76f4fe18d0556
[blender-staging.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_image_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_editVert.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_image.h"
50 #include "BKE_mesh.h"
51 #include "BKE_screen.h"
52 #include "BKE_utildefines.h"
53
54 #include "IMB_imbuf.h"
55 #include "IMB_imbuf_types.h"
56
57 #include "ED_gpencil.h"
58 #include "ED_image.h"
59 #include "ED_mesh.h"
60 #include "ED_space_api.h"
61 #include "ED_screen.h"
62 #include "ED_uvedit.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h"
66
67 #include "RNA_access.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "image_intern.h"
77
78 /**************************** common state *****************************/
79
80 /* note; image_panel_properties() uses pointer to sima->image directly */
81 Image *ED_space_image(SpaceImage *sima)
82 {
83         return sima->image;
84 }
85
86 /* called to assign images to UV faces */
87 void ED_space_image_set(bContext *C, SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
88 {
89         ED_uvedit_assign_image(scene, obedit, ima, sima->image);
90         
91         /* change the space ima after because uvedit_face_visible uses the space ima
92          * to check if the face is displayed in UV-localview */
93         sima->image= ima;
94         
95         if(ima == NULL || ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
96                 sima->flag &= ~SI_DRAWTOOL;
97         
98         if(sima->image)
99                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
100         
101         if(sima->image && sima->image->id.us==0)
102                 sima->image->id.us= 1;
103         
104         if(C) {
105                 if(obedit)
106                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
107                 
108                 ED_area_tag_redraw(CTX_wm_area(C));
109         }
110 }
111
112 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
113 {
114         ImBuf *ibuf;
115         
116         if(sima && sima->image) {
117 #if 0
118                 if(sima->image->type==IMA_TYPE_R_RESULT && BIF_show_render_spare())
119                         return BIF_render_spare_imbuf();
120                 else
121 #endif
122                         ibuf= BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
123                 
124                 if(ibuf && (ibuf->rect || ibuf->rect_float))
125                         return ibuf;
126         }
127         
128         return NULL;
129 }
130
131 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
132 {
133         if(sima && sima->image)
134                 BKE_image_release_ibuf(sima->image, lock);
135 }
136
137 int ED_space_image_has_buffer(SpaceImage *sima)
138 {
139         ImBuf *ibuf;
140         void *lock;
141         int has_buffer;
142         
143         ibuf= ED_space_image_acquire_buffer(sima, &lock);
144         has_buffer= (ibuf != NULL);
145         ED_space_image_release_buffer(sima, lock);
146         
147         return has_buffer;
148 }
149
150 void ED_image_size(Image *ima, int *width, int *height)
151 {
152         ImBuf *ibuf= NULL;
153         void *lock;
154         
155         if(ima)
156                 ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
157         
158         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
159                 *width= ibuf->x;
160                 *height= ibuf->y;
161         }
162         else {
163                 *width= 256;
164                 *height= 256;
165         }
166         
167         if(ima)
168                 BKE_image_release_ibuf(ima, lock);
169 }
170
171 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
172 {
173         Scene *scene= sima->iuser.scene;
174         ImBuf *ibuf;
175         void *lock;
176         
177         ibuf= ED_space_image_acquire_buffer(sima, &lock);
178         
179         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
180                 *width= ibuf->x;
181                 *height= ibuf->y;
182         }
183         else if(sima->image && sima->image->type==IMA_TYPE_R_RESULT && scene) {
184                 /* not very important, just nice */
185                 *width= (scene->r.xsch*scene->r.size)/100;
186                 *height= (scene->r.ysch*scene->r.size)/100;
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
301 static void image_histogram_tag_refresh(ScrArea *sa)
302 {
303         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
304         ARegion *ar;
305         
306         /* only while histogram is visible */
307         for (ar=sa->regionbase.first; ar; ar=ar->next) {
308                 if (ar->regiontype == RGN_TYPE_PREVIEW && ar->flag & RGN_FLAG_HIDDEN)
309                         return;
310         }
311         
312         sima->hist.ok=0;
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_histogram_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         /* header */
390         ar= MEM_callocN(sizeof(ARegion), "header for image");
391         
392         BLI_addtail(&simage->regionbase, ar);
393         ar->regiontype= RGN_TYPE_HEADER;
394         ar->alignment= RGN_ALIGN_BOTTOM;
395         
396         /* buttons/list view */
397         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
398         
399         BLI_addtail(&simage->regionbase, ar);
400         ar->regiontype= RGN_TYPE_UI;
401         ar->alignment= RGN_ALIGN_LEFT;
402         ar->flag = RGN_FLAG_HIDDEN;
403         
404         /* scopes */
405         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
406         
407         BLI_addtail(&simage->regionbase, ar);
408         ar->regiontype= RGN_TYPE_PREVIEW;
409         ar->alignment= RGN_ALIGN_RIGHT;
410         ar->flag = RGN_FLAG_HIDDEN;
411         
412         /* main area */
413         ar= MEM_callocN(sizeof(ARegion), "main area for image");
414         
415         BLI_addtail(&simage->regionbase, ar);
416         ar->regiontype= RGN_TYPE_WINDOW;
417         
418         return (SpaceLink *)simage;
419 }
420
421 /* not spacelink itself */
422 static void image_free(SpaceLink *sl)
423 {       
424         SpaceImage *simage= (SpaceImage*) sl;
425         
426         if(simage->cumap)
427                 curvemapping_free(simage->cumap);
428 //      if(simage->gpd)
429 // XXX          free_gpencil_data(simage->gpd);
430         
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_curves_point_set);
479
480         WM_operatortype_append(IMAGE_OT_record_composite);
481
482         WM_operatortype_append(IMAGE_OT_toolbox);
483         WM_operatortype_append(IMAGE_OT_properties);
484         WM_operatortype_append(IMAGE_OT_scopes);
485 }
486
487 void image_keymap(struct wmKeyConfig *keyconf)
488 {
489         wmKeyMap *keymap= WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
490         
491         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
492         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
493         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
494         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
495         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
496         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
497         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", PKEY, KM_PRESS, 0, 0);
498
499         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
500         
501         keymap= WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
502         
503         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
504         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
505         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
506         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
507
508         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
509         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
510         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
511         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
512         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
513         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
514
515         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
516         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
517         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
518         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
519         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
520         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
521         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
522
523         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
524         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
525         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
526
527         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
528 }
529
530 /* dropboxes */
531 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
532 {
533         if(drag->type==WM_DRAG_PATH)
534                 if(ELEM(drag->icon, ICON_FILE_IMAGE, ICON_FILE_BLANK))  /* rule might not work? */
535                         return 1;
536         return 0;
537 }
538
539 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
540 {
541         /* copy drag path to properties */
542         RNA_string_set(drop->ptr, "path", drag->path);
543 }
544
545 /* area+region dropbox definition */
546 static void image_dropboxes(void)
547 {
548         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
549         
550         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
551 }
552
553
554
555 static void image_refresh(const bContext *C, ScrArea *sa)
556 {
557         SpaceImage *sima= CTX_wm_space_image(C);
558         Object *obedit= CTX_data_edit_object(C);
559         Image *ima;
560
561         ima= ED_space_image(sima);
562
563         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
564                 BKE_image_user_calc_frame(&sima->iuser, CTX_data_scene(C)->r.cfra, 0);
565         
566         /* check if we have to set the image from the editmesh */
567         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
568         else if(obedit && obedit->type == OB_MESH) {
569                 Mesh *me= (Mesh*)obedit->data;
570                 EditMesh *em= BKE_mesh_get_editmesh(me);
571                 MTFace *tf;
572                 
573                 if(em && EM_texFaceCheck(em)) {
574                         sima->image= ima= NULL;
575                         
576                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
577                         
578                         if(tf && (tf->mode & TF_TEX)) {
579                                 /* don't need to check for pin here, see above */
580                                 sima->image= ima= tf->tpage;
581                                 
582                                 if(sima->flag & SI_EDITTILE);
583                                 else sima->curtile= tf->tile;
584                         }
585                 }
586
587                 BKE_mesh_end_editmesh(obedit->data, em);
588         }
589 }
590
591 static void image_listener(ScrArea *sa, wmNotifier *wmn)
592 {
593         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
594         
595         /* context changes */
596         switch(wmn->category) {
597                 case NC_SCENE:
598                         switch(wmn->data) {
599                                 case ND_FRAME:
600                                 case ND_MODE:
601                                 case ND_RENDER_RESULT:
602                                 case ND_COMPO_RESULT:
603                                         if (ED_space_image_show_render(sima))
604                                                 image_histogram_tag_refresh(sa);
605                                         ED_area_tag_refresh(sa);
606                                         ED_area_tag_redraw(sa);                                 
607                                         break;
608                         }
609                         break;
610                 case NC_IMAGE:
611                         if (wmn->reference == sima->image || !wmn->reference) {
612                                 image_histogram_tag_refresh(sa);
613                                 ED_area_tag_refresh(sa);
614                                 ED_area_tag_redraw(sa);
615                         }
616                         break;
617                 case NC_SPACE:  
618                         if(wmn->data == ND_SPACE_IMAGE)
619                                 ED_area_tag_redraw(sa);
620                         break;
621                 case NC_GEOM:
622                         switch(wmn->data) {
623                                 case ND_DATA:
624                                 case ND_SELECT:
625                                         ED_area_tag_refresh(sa);
626                                         ED_area_tag_redraw(sa);
627                                         break;
628                         }
629         }
630 }
631
632 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
633 {
634         SpaceImage *sima= CTX_wm_space_image(C);
635
636         if(CTX_data_dir(member)) {
637                 static const char *dir[] = {"edit_image", NULL};
638                 CTX_data_dir_set(result, dir);
639         }
640         else if(CTX_data_equals(member, "edit_image")) {
641                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
642                 return 1;
643         }
644
645         return 0;
646 }
647
648 /************************** main region ***************************/
649
650 /* sets up the fields of the View2D from zoom and offset */
651 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
652 {
653         Image *ima= ED_space_image(sima);
654         float x1, y1, w, h;
655         int width, height, winx, winy;
656         
657 #if 0
658         if(image_preview_active(curarea, &width, &height));
659         else
660 #endif
661         ED_space_image_size(sima, &width, &height);
662
663         w= width;
664         h= height;
665         
666         if(ima)
667                 h *= ima->aspy/ima->aspx;
668
669         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
670         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
671                 
672         ar->v2d.tot.xmin= 0;
673         ar->v2d.tot.ymin= 0;
674         ar->v2d.tot.xmax= w;
675         ar->v2d.tot.ymax= h;
676         
677         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
678         ar->v2d.mask.xmax= winx;
679         ar->v2d.mask.ymax= winy;
680
681         /* which part of the image space do we see? */
682         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
683         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
684
685         x1-= sima->zoom*sima->xof;
686         y1-= sima->zoom*sima->yof;
687         
688         /* relative display right */
689         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
690         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
691         
692         /* relative display left */
693         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
694         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
695         
696         /* normalize 0.0..1.0 */
697         ar->v2d.cur.xmin /= w;
698         ar->v2d.cur.xmax /= w;
699         ar->v2d.cur.ymin /= h;
700         ar->v2d.cur.ymax /= h;
701 }
702
703 /* add handlers, stuff you only do once or on area/region changes */
704 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
705 {
706         wmKeyMap *keymap;
707         
708         // image space manages own v2d
709         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
710
711         /* image paint polls for mode */
712         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
713         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
714
715         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
716         WM_event_add_keymap_handler(&ar->handlers, keymap);
717         
718         /* own keymaps */
719         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
720         WM_event_add_keymap_handler(&ar->handlers, keymap);
721         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
722         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
723
724 }
725
726 static void image_main_area_draw(const bContext *C, ARegion *ar)
727 {
728         /* draw entirely, view changes should be handled here */
729         SpaceImage *sima= CTX_wm_space_image(C);
730         Object *obedit= CTX_data_edit_object(C);
731         Scene *scene= CTX_data_scene(C);
732         View2D *v2d= &ar->v2d;
733         //View2DScrollers *scrollers;
734         float col[3];
735         
736         /* clear and setup matrix */
737         UI_GetThemeColor3fv(TH_BACK, col);
738         glClearColor(col[0], col[1], col[2], 0.0);
739         glClear(GL_COLOR_BUFFER_BIT);
740
741         /* put scene context variable in iuser */
742         sima->iuser.scene= scene;
743
744         /* we set view2d from own zoom and offset each time */
745         image_main_area_set_view2d(sima, ar);
746         
747         /* we draw image in pixelspace */
748         draw_image_main(sima, ar, scene);
749
750         /* and uvs in 0.0-1.0 space */
751         UI_view2d_view_ortho(C, v2d);
752         draw_uvedit_main(sima, ar, scene, obedit);
753
754         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
755                 
756         /* Grease Pencil too (in addition to UV's) */
757         draw_image_grease_pencil((bContext *)C, 1); 
758
759         UI_view2d_view_restore(C);
760
761         /* draw Grease Pencil - screen space only */
762         draw_image_grease_pencil((bContext *)C, 0);
763
764         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
765         
766         /* scrollers? */
767         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
768         UI_view2d_scrollers_draw(C, v2d, scrollers);
769         UI_view2d_scrollers_free(scrollers);*/
770 }
771
772 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
773 {
774         /* context changes */
775         switch(wmn->category) {
776                 /* nothing yet */
777         }
778 }
779
780 /* *********************** buttons region ************************ */
781
782 /* add handlers, stuff you only do once or on area/region changes */
783 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
784 {
785         wmKeyMap *keymap;
786
787         ED_region_panels_init(wm, ar);
788         
789         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
790         WM_event_add_keymap_handler(&ar->handlers, keymap);
791 }
792
793 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
794 {
795         ED_region_panels(C, ar, 1, NULL, -1);
796 }
797
798 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
799 {
800         /* context changes */
801         switch(wmn->category) {
802                 case NC_BRUSH:
803                         if(wmn->action==NA_EDITED)
804                                 ED_region_tag_redraw(ar);
805                         break;
806         }
807 }
808
809 /* *********************** scopes region ************************ */
810
811 /* add handlers, stuff you only do once or on area/region changes */
812 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
813 {
814         wmKeyMap *keymap;
815         
816         ED_region_panels_init(wm, ar);
817         
818         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
819         WM_event_add_keymap_handler(&ar->handlers, keymap);
820 }
821
822 static void image_scope_area_draw(const bContext *C, ARegion *ar)
823 {
824         SpaceImage *sima= CTX_wm_space_image(C);
825         void *lock;
826         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
827         if(ibuf)
828         histogram_update(&sima->hist, ibuf);
829         ED_space_image_release_buffer(sima, lock);
830         
831         ED_region_panels(C, ar, 1, NULL, -1);
832 }
833
834 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
835 {
836         /* context changes */
837         switch(wmn->category) {
838                 case NC_SCENE:
839                         switch(wmn->data) {
840                                 case ND_MODE:
841                                 case ND_RENDER_RESULT:
842                                 case ND_COMPO_RESULT:
843                                         ED_region_tag_redraw(ar);
844                                         break;
845                         }
846                         break;
847                 case NC_IMAGE:
848                         ED_region_tag_redraw(ar);
849                         break;
850                 case NC_NODE:
851                         ED_region_tag_redraw(ar);
852                         break;
853                         
854         }
855 }
856
857 /************************* header region **************************/
858
859 /* add handlers, stuff you only do once or on area/region changes */
860 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
861 {
862         ED_region_header_init(ar);
863 }
864
865 static void image_header_area_draw(const bContext *C, ARegion *ar)
866 {
867         ED_region_header(C, ar);
868 }
869
870 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
871 {
872         /* context changes */
873         switch(wmn->category) {
874                 case NC_SCENE:
875                         switch(wmn->data) {
876                                 case ND_MODE:
877                                 case ND_TOOLSETTINGS:
878                                         ED_region_tag_redraw(ar);
879                                         break;
880                         }
881                         break;
882                 case NC_GEOM:
883                         switch(wmn->data) {
884                                 case ND_DATA:
885                                 case ND_SELECT:
886                                         ED_region_tag_redraw(ar);
887                                         break;
888                         }
889                         
890         }
891 }
892
893 /**************************** spacetype *****************************/
894
895 /* only called once, from space/spacetypes.c */
896 void ED_spacetype_image(void)
897 {
898         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
899         ARegionType *art;
900         
901         st->spaceid= SPACE_IMAGE;
902         strncpy(st->name, "Image", BKE_ST_MAXNAME);
903         
904         st->new= image_new;
905         st->free= image_free;
906         st->init= image_init;
907         st->duplicate= image_duplicate;
908         st->operatortypes= image_operatortypes;
909         st->keymap= image_keymap;
910         st->dropboxes= image_dropboxes;
911         st->refresh= image_refresh;
912         st->listener= image_listener;
913         st->context= image_context;
914         
915         /* regions: main window */
916         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
917         art->regionid = RGN_TYPE_WINDOW;
918         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
919         art->init= image_main_area_init;
920         art->draw= image_main_area_draw;
921         art->listener= image_main_area_listener;
922
923         BLI_addhead(&st->regiontypes, art);
924         
925         /* regions: listview/buttons */
926         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
927         art->regionid = RGN_TYPE_UI;
928         art->prefsizex= 220; // XXX
929         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
930         art->listener= image_buttons_area_listener;
931         art->init= image_buttons_area_init;
932         art->draw= image_buttons_area_draw;
933         BLI_addhead(&st->regiontypes, art);
934
935         image_buttons_register(art);
936         
937         /* regions: statistics/scope buttons */
938         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
939         art->regionid = RGN_TYPE_PREVIEW;
940         art->minsizex= 220; // XXX
941         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
942         art->listener= image_scope_area_listener;
943         art->init= image_scope_area_init;
944         art->draw= image_scope_area_draw;
945         BLI_addhead(&st->regiontypes, art);
946
947         /* regions: header */
948         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
949         art->regionid = RGN_TYPE_HEADER;
950         art->prefsizey= HEADERY;
951         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
952         art->listener= image_header_area_listener;
953         art->init= image_header_area_init;
954         art->draw= image_header_area_draw;
955         
956         BLI_addhead(&st->regiontypes, art);
957         
958         BKE_spacetype_register(st);
959 }
960