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