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