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