a653838c0735954a80d303c859efbe7f2e0b6091
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_sample);
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         keymap= WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
498         
499         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
500         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
501         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
502         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
503
504         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
505         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
506         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
507         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
508         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
509         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
510
511         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
512         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
513         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
514         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
515         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
516         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
517         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
518
519         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
520         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
521         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
522
523         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
524 }
525
526 /* dropboxes */
527 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
528 {
529         if(drag->type==WM_DRAG_PATH)
530                 if(ELEM(drag->icon, ICON_FILE_IMAGE, ICON_FILE_BLANK))  /* rule might not work? */
531                         return 1;
532         return 0;
533 }
534
535 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
536 {
537         /* copy drag path to properties */
538         RNA_string_set(drop->ptr, "path", drag->path);
539 }
540
541 /* area+region dropbox definition */
542 static void image_dropboxes(void)
543 {
544         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
545         
546         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
547 }
548
549
550
551 static void image_refresh(const bContext *C, ScrArea *sa)
552 {
553         SpaceImage *sima= CTX_wm_space_image(C);
554         Object *obedit= CTX_data_edit_object(C);
555         Image *ima;
556
557         ima= ED_space_image(sima);
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_MODE:
593                                 case ND_RENDER_RESULT:
594                                 case ND_COMPO_RESULT:
595                                         if (ED_space_image_show_render(sima))
596                                                 image_histogram_tag_refresh(sa);
597                                         ED_area_tag_refresh(sa);
598                                         ED_area_tag_redraw(sa);
599                                         break;
600                         }
601                         break;
602                 case NC_IMAGE:
603                         if (wmn->reference == sima->image || !wmn->reference) {
604                                 image_histogram_tag_refresh(sa);
605                                 ED_area_tag_refresh(sa);
606                                 ED_area_tag_redraw(sa);
607                         }
608                         break;
609                 case NC_SPACE:  
610                         if(wmn->data == ND_SPACE_IMAGE)
611                                 ED_area_tag_redraw(sa);
612                         break;
613                 case NC_GEOM:
614                         switch(wmn->data) {
615                                 case ND_DATA:
616                                 case ND_SELECT:
617                                         ED_area_tag_refresh(sa);
618                                         ED_area_tag_redraw(sa);
619                                         break;
620                         }
621         }
622 }
623
624 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
625 {
626         SpaceImage *sima= CTX_wm_space_image(C);
627
628         if(CTX_data_dir(member)) {
629                 static const char *dir[] = {"edit_image", NULL};
630                 CTX_data_dir_set(result, dir);
631         }
632         else if(CTX_data_equals(member, "edit_image")) {
633                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
634                 return 1;
635         }
636
637         return 0;
638 }
639
640 /************************** main region ***************************/
641
642 /* sets up the fields of the View2D from zoom and offset */
643 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
644 {
645         Image *ima= ED_space_image(sima);
646         float x1, y1, w, h;
647         int width, height, winx, winy;
648         
649 #if 0
650         if(image_preview_active(curarea, &width, &height));
651         else
652 #endif
653         ED_space_image_size(sima, &width, &height);
654
655         w= width;
656         h= height;
657         
658         if(ima)
659                 h *= ima->aspy/ima->aspx;
660
661         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
662         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
663                 
664         ar->v2d.tot.xmin= 0;
665         ar->v2d.tot.ymin= 0;
666         ar->v2d.tot.xmax= w;
667         ar->v2d.tot.ymax= h;
668         
669         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
670         ar->v2d.mask.xmax= winx;
671         ar->v2d.mask.ymax= winy;
672
673         /* which part of the image space do we see? */
674         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
675         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
676
677         x1-= sima->zoom*sima->xof;
678         y1-= sima->zoom*sima->yof;
679         
680         /* relative display right */
681         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
682         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
683         
684         /* relative display left */
685         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
686         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
687         
688         /* normalize 0.0..1.0 */
689         ar->v2d.cur.xmin /= w;
690         ar->v2d.cur.xmax /= w;
691         ar->v2d.cur.ymin /= h;
692         ar->v2d.cur.ymax /= h;
693 }
694
695 /* add handlers, stuff you only do once or on area/region changes */
696 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
697 {
698         wmKeyMap *keymap;
699         
700         // image space manages own v2d
701         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
702
703         /* image paint polls for mode */
704         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
705         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
706
707         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
708         WM_event_add_keymap_handler(&ar->handlers, keymap);
709         
710         /* own keymaps */
711         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
712         WM_event_add_keymap_handler(&ar->handlers, keymap);
713         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
714         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
715
716 }
717
718 static void image_main_area_draw(const bContext *C, ARegion *ar)
719 {
720         /* draw entirely, view changes should be handled here */
721         SpaceImage *sima= CTX_wm_space_image(C);
722         Object *obedit= CTX_data_edit_object(C);
723         Scene *scene= CTX_data_scene(C);
724         View2D *v2d= &ar->v2d;
725         //View2DScrollers *scrollers;
726         float col[3];
727         
728         /* clear and setup matrix */
729         UI_GetThemeColor3fv(TH_BACK, col);
730         glClearColor(col[0], col[1], col[2], 0.0);
731         glClear(GL_COLOR_BUFFER_BIT);
732
733         /* put scene context variable in iuser */
734         sima->iuser.scene= scene;
735
736         /* we set view2d from own zoom and offset each time */
737         image_main_area_set_view2d(sima, ar);
738         
739         /* we draw image in pixelspace */
740         draw_image_main(sima, ar, scene);
741
742         /* and uvs in 0.0-1.0 space */
743         UI_view2d_view_ortho(C, v2d);
744         draw_uvedit_main(sima, ar, scene, obedit);
745
746         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
747                 
748         /* Grease Pencil too (in addition to UV's) */
749         draw_image_grease_pencil((bContext *)C, 1); 
750
751         UI_view2d_view_restore(C);
752
753         /* draw Grease Pencil - screen space only */
754         draw_image_grease_pencil((bContext *)C, 0);
755
756         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
757         
758         /* scrollers? */
759         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
760         UI_view2d_scrollers_draw(C, v2d, scrollers);
761         UI_view2d_scrollers_free(scrollers);*/
762 }
763
764 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
765 {
766         /* context changes */
767         switch(wmn->category) {
768                 /* nothing yet */
769         }
770 }
771
772 /* *********************** buttons region ************************ */
773
774 /* add handlers, stuff you only do once or on area/region changes */
775 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
776 {
777         wmKeyMap *keymap;
778
779         ED_region_panels_init(wm, ar);
780         
781         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
782         WM_event_add_keymap_handler(&ar->handlers, keymap);
783 }
784
785 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
786 {
787         ED_region_panels(C, ar, 1, NULL, -1);
788 }
789
790 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
791 {
792         /* context changes */
793         switch(wmn->category) {
794                 case NC_BRUSH:
795                         if(wmn->action==NA_EDITED)
796                                 ED_region_tag_redraw(ar);
797                         break;
798         }
799 }
800
801 /* *********************** scopes region ************************ */
802
803 /* add handlers, stuff you only do once or on area/region changes */
804 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
805 {
806         wmKeyMap *keymap;
807         
808         ED_region_panels_init(wm, ar);
809         
810         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
811         WM_event_add_keymap_handler(&ar->handlers, keymap);
812 }
813
814 static void image_scope_area_draw(const bContext *C, ARegion *ar)
815 {
816         SpaceImage *sima= CTX_wm_space_image(C);
817         void *lock;
818         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
819         if(ibuf)
820         histogram_update(&sima->hist, ibuf);
821         ED_space_image_release_buffer(sima, lock);
822         
823         ED_region_panels(C, ar, 1, NULL, -1);
824 }
825
826 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
827 {
828         /* context changes */
829         switch(wmn->category) {
830                 case NC_SCENE:
831                         switch(wmn->data) {
832                                 case ND_MODE:
833                                 case ND_RENDER_RESULT:
834                                 case ND_COMPO_RESULT:
835                                         ED_region_tag_redraw(ar);
836                                         break;
837                         }
838                         break;
839                 case NC_IMAGE:
840                         ED_region_tag_redraw(ar);
841                         break;
842                 case NC_NODE:
843                         ED_region_tag_redraw(ar);
844                         break;
845                         
846         }
847 }
848
849 /************************* header region **************************/
850
851 /* add handlers, stuff you only do once or on area/region changes */
852 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
853 {
854         ED_region_header_init(ar);
855 }
856
857 static void image_header_area_draw(const bContext *C, ARegion *ar)
858 {
859         ED_region_header(C, ar);
860 }
861
862 /**************************** spacetype *****************************/
863
864 /* only called once, from space/spacetypes.c */
865 void ED_spacetype_image(void)
866 {
867         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
868         ARegionType *art;
869         
870         st->spaceid= SPACE_IMAGE;
871         strncpy(st->name, "Image", BKE_ST_MAXNAME);
872         
873         st->new= image_new;
874         st->free= image_free;
875         st->init= image_init;
876         st->duplicate= image_duplicate;
877         st->operatortypes= image_operatortypes;
878         st->keymap= image_keymap;
879         st->dropboxes= image_dropboxes;
880         st->refresh= image_refresh;
881         st->listener= image_listener;
882         st->context= image_context;
883         
884         /* regions: main window */
885         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
886         art->regionid = RGN_TYPE_WINDOW;
887         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
888         art->init= image_main_area_init;
889         art->draw= image_main_area_draw;
890         art->listener= image_main_area_listener;
891
892         BLI_addhead(&st->regiontypes, art);
893         
894         /* regions: listview/buttons */
895         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
896         art->regionid = RGN_TYPE_UI;
897         art->prefsizex= 220; // XXX
898         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
899         art->listener= image_buttons_area_listener;
900         art->init= image_buttons_area_init;
901         art->draw= image_buttons_area_draw;
902         BLI_addhead(&st->regiontypes, art);
903
904         image_buttons_register(art);
905         
906         /* regions: statistics/scope buttons */
907         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
908         art->regionid = RGN_TYPE_PREVIEW;
909         art->minsizex= 220; // XXX
910         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
911         art->listener= image_scope_area_listener;
912         art->init= image_scope_area_init;
913         art->draw= image_scope_area_draw;
914         BLI_addhead(&st->regiontypes, art);
915
916         /* regions: header */
917         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
918         art->regionid = RGN_TYPE_HEADER;
919         art->prefsizey= HEADERY;
920         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
921         art->init= image_header_area_init;
922         art->draw= image_header_area_draw;
923         
924         BLI_addhead(&st->regiontypes, art);
925         
926         BKE_spacetype_register(st);
927 }
928