Drag and drop 2.5 integration! Finally, slashdot regulars can use
[blender.git] / source / blender / editors / space_image / space_image.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 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, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
520
521         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
522         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
523         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
524
525         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
526 }
527
528 /* dropboxes */
529 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
530 {
531         if(drag->type==WM_DRAG_PATH)
532                 if(ELEM(drag->icon, ICON_FILE_IMAGE, ICON_FILE_BLANK))  /* rule might not work? */
533                         return 1;
534         return 0;
535 }
536
537 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
538 {
539         /* copy drag path to properties */
540         RNA_string_set(drop->ptr, "path", drag->path);
541 }
542
543 /* area+region dropbox definition */
544 static void image_dropboxes(void)
545 {
546         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
547         
548         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
549 }
550
551
552
553 static void image_refresh(const bContext *C, ScrArea *sa)
554 {
555         SpaceImage *sima= CTX_wm_space_image(C);
556         Object *obedit= CTX_data_edit_object(C);
557         Image *ima;
558
559         ima= ED_space_image(sima);
560
561         /* check if we have to set the image from the editmesh */
562         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
563         else if(obedit && obedit->type == OB_MESH) {
564                 Mesh *me= (Mesh*)obedit->data;
565                 EditMesh *em= BKE_mesh_get_editmesh(me);
566                 MTFace *tf;
567                 
568                 if(em && EM_texFaceCheck(em)) {
569                         sima->image= ima= NULL;
570                         
571                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
572                         
573                         if(tf && (tf->mode & TF_TEX)) {
574                                 /* don't need to check for pin here, see above */
575                                 sima->image= ima= tf->tpage;
576                                 
577                                 if(sima->flag & SI_EDITTILE);
578                                 else sima->curtile= tf->tile;
579                         }
580                 }
581
582                 BKE_mesh_end_editmesh(obedit->data, em);
583         }
584 }
585
586 static void image_listener(ScrArea *sa, wmNotifier *wmn)
587 {
588         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
589         
590         /* context changes */
591         switch(wmn->category) {
592                 case NC_SCENE:
593                         switch(wmn->data) {
594                                 case ND_MODE:
595                                 case ND_RENDER_RESULT:
596                                 case ND_COMPO_RESULT:
597                                         if (ED_space_image_show_render(sima))
598                                                 image_histogram_tag_refresh(sa);
599                                         ED_area_tag_refresh(sa);
600                                         ED_area_tag_redraw(sa);
601                                         break;
602                         }
603                         break;
604                 case NC_IMAGE:
605                         if (wmn->reference == sima->image) {
606                                 image_histogram_tag_refresh(sa);
607                                 ED_area_tag_refresh(sa);
608                                 ED_area_tag_redraw(sa);
609                         }
610                         break;
611                 case NC_SPACE:  
612                         if(wmn->data == ND_SPACE_IMAGE)
613                                 ED_area_tag_redraw(sa);
614                         break;
615                 case NC_GEOM:
616                         switch(wmn->data) {
617                                 case ND_DATA:
618                                 case ND_SELECT:
619                                         ED_area_tag_refresh(sa);
620                                         ED_area_tag_redraw(sa);
621                                         break;
622                         }
623         }
624 }
625
626 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
627 {
628         SpaceImage *sima= CTX_wm_space_image(C);
629
630         if(CTX_data_dir(member)) {
631                 static const char *dir[] = {"edit_image", NULL};
632                 CTX_data_dir_set(result, dir);
633         }
634         else if(CTX_data_equals(member, "edit_image")) {
635                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
636                 return 1;
637         }
638
639         return 0;
640 }
641
642 /************************** main region ***************************/
643
644 /* sets up the fields of the View2D from zoom and offset */
645 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
646 {
647         Image *ima= ED_space_image(sima);
648         float x1, y1, w, h;
649         int width, height, winx, winy;
650         
651 #if 0
652         if(image_preview_active(curarea, &width, &height));
653         else
654 #endif
655         ED_space_image_size(sima, &width, &height);
656
657         w= width;
658         h= height;
659         
660         if(ima)
661                 h *= ima->aspy/ima->aspx;
662
663         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
664         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
665                 
666         ar->v2d.tot.xmin= 0;
667         ar->v2d.tot.ymin= 0;
668         ar->v2d.tot.xmax= w;
669         ar->v2d.tot.ymax= h;
670         
671         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
672         ar->v2d.mask.xmax= winx;
673         ar->v2d.mask.ymax= winy;
674
675         /* which part of the image space do we see? */
676         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
677         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
678
679         x1-= sima->zoom*sima->xof;
680         y1-= sima->zoom*sima->yof;
681         
682         /* relative display right */
683         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
684         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
685         
686         /* relative display left */
687         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
688         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
689         
690         /* normalize 0.0..1.0 */
691         ar->v2d.cur.xmin /= w;
692         ar->v2d.cur.xmax /= w;
693         ar->v2d.cur.ymin /= h;
694         ar->v2d.cur.ymax /= h;
695 }
696
697 /* add handlers, stuff you only do once or on area/region changes */
698 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
699 {
700         wmKeyMap *keymap;
701         
702         // image space manages own v2d
703         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
704
705         /* image paint polls for mode */
706         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
707         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
708
709         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
710         WM_event_add_keymap_handler(&ar->handlers, keymap);
711         
712         /* own keymaps */
713         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
714         WM_event_add_keymap_handler(&ar->handlers, keymap);
715         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
716         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
717
718 }
719
720 static void image_main_area_draw(const bContext *C, ARegion *ar)
721 {
722         /* draw entirely, view changes should be handled here */
723         SpaceImage *sima= CTX_wm_space_image(C);
724         Object *obedit= CTX_data_edit_object(C);
725         Scene *scene= CTX_data_scene(C);
726         View2D *v2d= &ar->v2d;
727         //View2DScrollers *scrollers;
728         float col[3];
729         
730         /* clear and setup matrix */
731         UI_GetThemeColor3fv(TH_BACK, col);
732         glClearColor(col[0], col[1], col[2], 0.0);
733         glClear(GL_COLOR_BUFFER_BIT);
734
735         /* put scene context variable in iuser */
736         sima->iuser.scene= scene;
737
738         /* we set view2d from own zoom and offset each time */
739         image_main_area_set_view2d(sima, ar);
740         
741         /* we draw image in pixelspace */
742         draw_image_main(sima, ar, scene);
743
744         /* and uvs in 0.0-1.0 space */
745         UI_view2d_view_ortho(C, v2d);
746         draw_uvedit_main(sima, ar, scene, obedit);
747
748         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
749                 
750         /* Grease Pencil too (in addition to UV's) */
751         draw_image_grease_pencil((bContext *)C, 1); 
752
753         UI_view2d_view_restore(C);
754
755         /* draw Grease Pencil - screen space only */
756         draw_image_grease_pencil((bContext *)C, 0);
757
758         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
759         
760         /* scrollers? */
761         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
762         UI_view2d_scrollers_draw(C, v2d, scrollers);
763         UI_view2d_scrollers_free(scrollers);*/
764 }
765
766 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
767 {
768         /* context changes */
769         switch(wmn->category) {
770                 /* nothing yet */
771         }
772 }
773
774 /* *********************** buttons region ************************ */
775
776 /* add handlers, stuff you only do once or on area/region changes */
777 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
778 {
779         wmKeyMap *keymap;
780
781         ED_region_panels_init(wm, ar);
782         
783         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
784         WM_event_add_keymap_handler(&ar->handlers, keymap);
785 }
786
787 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
788 {
789         ED_region_panels(C, ar, 1, NULL, -1);
790 }
791
792 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
793 {
794         /* context changes */
795         switch(wmn->category) {
796                 case NC_BRUSH:
797                         if(wmn->action==NA_EDITED)
798                                 ED_region_tag_redraw(ar);
799                         break;
800         }
801 }
802
803 /* *********************** scopes region ************************ */
804
805 /* add handlers, stuff you only do once or on area/region changes */
806 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
807 {
808         wmKeyMap *keymap;
809         
810         ED_region_panels_init(wm, ar);
811         
812         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
813         WM_event_add_keymap_handler(&ar->handlers, keymap);
814 }
815
816 static void image_scope_area_draw(const bContext *C, ARegion *ar)
817 {
818         ED_region_panels(C, ar, 1, NULL, -1);
819 }
820
821 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
822 {
823         /* context changes */
824         switch(wmn->category) {
825         }
826 }
827
828 /************************* header region **************************/
829
830 /* add handlers, stuff you only do once or on area/region changes */
831 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
832 {
833         ED_region_header_init(ar);
834 }
835
836 static void image_header_area_draw(const bContext *C, ARegion *ar)
837 {
838         ED_region_header(C, ar);
839 }
840
841 /**************************** spacetype *****************************/
842
843 /* only called once, from space/spacetypes.c */
844 void ED_spacetype_image(void)
845 {
846         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
847         ARegionType *art;
848         
849         st->spaceid= SPACE_IMAGE;
850         strncpy(st->name, "Image", BKE_ST_MAXNAME);
851         
852         st->new= image_new;
853         st->free= image_free;
854         st->init= image_init;
855         st->duplicate= image_duplicate;
856         st->operatortypes= image_operatortypes;
857         st->keymap= image_keymap;
858         st->dropboxes= image_dropboxes;
859         st->refresh= image_refresh;
860         st->listener= image_listener;
861         st->context= image_context;
862         
863         /* regions: main window */
864         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
865         art->regionid = RGN_TYPE_WINDOW;
866         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
867         art->init= image_main_area_init;
868         art->draw= image_main_area_draw;
869         art->listener= image_main_area_listener;
870
871         BLI_addhead(&st->regiontypes, art);
872         
873         /* regions: listview/buttons */
874         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
875         art->regionid = RGN_TYPE_UI;
876         art->prefsizex= 220; // XXX
877         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
878         art->listener= image_buttons_area_listener;
879         art->init= image_buttons_area_init;
880         art->draw= image_buttons_area_draw;
881         BLI_addhead(&st->regiontypes, art);
882
883         image_buttons_register(art);
884         
885         /* regions: statistics/scope buttons */
886         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
887         art->regionid = RGN_TYPE_PREVIEW;
888         art->minsizex= 220; // XXX
889         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
890         art->listener= image_scope_area_listener;
891         art->init= image_scope_area_init;
892         art->draw= image_scope_area_draw;
893         BLI_addhead(&st->regiontypes, art);
894
895         /* regions: header */
896         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
897         art->regionid = RGN_TYPE_HEADER;
898         art->prefsizey= HEADERY;
899         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
900         art->init= image_header_area_init;
901         art->draw= image_header_area_draw;
902         
903         BLI_addhead(&st->regiontypes, art);
904         
905         BKE_spacetype_register(st);
906 }
907