svn merge ^/trunk/blender -r43918:43934
[blender.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 #include "BKE_tessmesh.h"
56
57 #include "IMB_imbuf_types.h"
58
59 #include "ED_image.h"
60 #include "ED_mesh.h"
61 #include "ED_space_api.h"
62 #include "ED_screen.h"
63 #include "ED_uvedit.h"
64
65 #include "BIF_gl.h"
66
67 #include "RNA_access.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "UI_resources.h"
73 #include "UI_view2d.h"
74
75 #include "image_intern.h"
76
77 /**************************** common state *****************************/
78
79 /* note; image_panel_properties() uses pointer to sima->image directly */
80 Image *ED_space_image(SpaceImage *sima)
81 {
82         return sima->image;
83 }
84
85 /* called to assign images to UV faces */
86 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
87 {
88         /* context may be NULL, so use global */
89         ED_uvedit_assign_image(G.main, 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(obedit)
105                 WM_main_add_notifier(NC_GEOM|ND_DATA, obedit->data);
106
107         WM_main_add_notifier(NC_SPACE|ND_SPACE_IMAGE, NULL);
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                 struct BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
280                 int ret;
281                 
282                 ret = EDBM_texFaceCheck(em);
283                 
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                         struct BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
298                         int ret;
299                         
300                         ret = EDBM_texFaceCheck(em);
301                         
302                         return ret;
303                 }
304         
305         return 0;
306 }
307
308
309 static void image_scopes_tag_refresh(ScrArea *sa)
310 {
311         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
312         ARegion *ar;
313
314         /* only while histogram is visible */
315         for (ar=sa->regionbase.first; ar; ar=ar->next) {
316                 if (ar->regiontype == RGN_TYPE_PREVIEW && ar->flag & RGN_FLAG_HIDDEN)
317                         return;
318         }
319
320         sima->scopes.ok=0;
321 }
322
323
324 /* ******************** manage regions ********************* */
325
326 ARegion *image_has_buttons_region(ScrArea *sa)
327 {
328         ARegion *ar, *arnew;
329
330         ar= BKE_area_find_region_type(sa, RGN_TYPE_UI);
331         if(ar) return ar;
332         
333         /* add subdiv level; after header */
334         ar= BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
335
336         /* is error! */
337         if(ar==NULL) return NULL;
338         
339         arnew= MEM_callocN(sizeof(ARegion), "buttons for image");
340         
341         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
342         arnew->regiontype= RGN_TYPE_UI;
343         arnew->alignment= RGN_ALIGN_LEFT;
344         
345         arnew->flag = RGN_FLAG_HIDDEN;
346         
347         return arnew;
348 }
349
350 ARegion *image_has_scope_region(ScrArea *sa)
351 {
352         ARegion *ar, *arnew;
353
354         ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
355         if(ar) return ar;
356
357         /* add subdiv level; after buttons */
358         ar= BKE_area_find_region_type(sa, RGN_TYPE_UI);
359
360         /* is error! */
361         if(ar==NULL) return NULL;
362         
363         arnew= MEM_callocN(sizeof(ARegion), "scopes for image");
364         
365         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
366         arnew->regiontype= RGN_TYPE_PREVIEW;
367         arnew->alignment= RGN_ALIGN_RIGHT;
368         
369         arnew->flag = RGN_FLAG_HIDDEN;
370
371         image_scopes_tag_refresh(sa);
372         
373         return arnew;
374 }
375
376 /* ******************** default callbacks for image space ***************** */
377
378 static SpaceLink *image_new(const bContext *UNUSED(C))
379 {
380         ARegion *ar;
381         SpaceImage *simage;
382         
383         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
384         simage->spacetype= SPACE_IMAGE;
385         simage->zoom= 1;
386         simage->lock= 1;
387         
388         simage->iuser.ok= 1;
389         simage->iuser.fie_ima= 2;
390         simage->iuser.frames= 100;
391         
392         scopes_new(&simage->scopes);
393         simage->sample_line_hist.height= 100;
394
395         /* header */
396         ar= MEM_callocN(sizeof(ARegion), "header for image");
397         
398         BLI_addtail(&simage->regionbase, ar);
399         ar->regiontype= RGN_TYPE_HEADER;
400         ar->alignment= RGN_ALIGN_BOTTOM;
401         
402         /* buttons/list view */
403         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
404         
405         BLI_addtail(&simage->regionbase, ar);
406         ar->regiontype= RGN_TYPE_UI;
407         ar->alignment= RGN_ALIGN_LEFT;
408         ar->flag = RGN_FLAG_HIDDEN;
409         
410         /* scopes */
411         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
412         
413         BLI_addtail(&simage->regionbase, ar);
414         ar->regiontype= RGN_TYPE_PREVIEW;
415         ar->alignment= RGN_ALIGN_RIGHT;
416         ar->flag = RGN_FLAG_HIDDEN;
417
418         /* main area */
419         ar= MEM_callocN(sizeof(ARegion), "main area for image");
420         
421         BLI_addtail(&simage->regionbase, ar);
422         ar->regiontype= RGN_TYPE_WINDOW;
423         
424         return (SpaceLink *)simage;
425 }
426
427 /* not spacelink itself */
428 static void image_free(SpaceLink *sl)
429 {       
430         SpaceImage *simage= (SpaceImage*) sl;
431         
432         if(simage->cumap)
433                 curvemapping_free(simage->cumap);
434         scopes_free(&simage->scopes);
435 }
436
437
438 /* spacetype; init callback, add handlers */
439 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
440 {
441         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
442
443         /* add drop boxes */
444         WM_event_add_dropbox_handler(&sa->handlers, lb);
445         
446 }
447
448 static SpaceLink *image_duplicate(SpaceLink *sl)
449 {
450         SpaceImage *simagen= MEM_dupallocN(sl);
451         
452         /* clear or remove stuff from old */
453         if(simagen->cumap)
454                 simagen->cumap= curvemapping_copy(simagen->cumap);
455
456         scopes_new(&simagen->scopes);
457
458         return (SpaceLink *)simagen;
459 }
460
461 static void image_operatortypes(void)
462 {
463         WM_operatortype_append(IMAGE_OT_view_all);
464         WM_operatortype_append(IMAGE_OT_view_pan);
465         WM_operatortype_append(IMAGE_OT_view_selected);
466         WM_operatortype_append(IMAGE_OT_view_zoom);
467         WM_operatortype_append(IMAGE_OT_view_zoom_in);
468         WM_operatortype_append(IMAGE_OT_view_zoom_out);
469         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
470         WM_operatortype_append(IMAGE_OT_view_ndof);
471
472         WM_operatortype_append(IMAGE_OT_new);
473         WM_operatortype_append(IMAGE_OT_open);
474         WM_operatortype_append(IMAGE_OT_replace);
475         WM_operatortype_append(IMAGE_OT_reload);
476         WM_operatortype_append(IMAGE_OT_save);
477         WM_operatortype_append(IMAGE_OT_save_as);
478         WM_operatortype_append(IMAGE_OT_save_sequence);
479         WM_operatortype_append(IMAGE_OT_pack);
480         WM_operatortype_append(IMAGE_OT_unpack);
481         
482         WM_operatortype_append(IMAGE_OT_invert);
483
484         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
485
486         WM_operatortype_append(IMAGE_OT_sample);
487         WM_operatortype_append(IMAGE_OT_sample_line);
488         WM_operatortype_append(IMAGE_OT_curves_point_set);
489
490         WM_operatortype_append(IMAGE_OT_record_composite);
491
492         WM_operatortype_append(IMAGE_OT_properties);
493         WM_operatortype_append(IMAGE_OT_scopes);
494 }
495
496 static void image_keymap(struct wmKeyConfig *keyconf)
497 {
498         wmKeyMap *keymap= WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
499         wmKeyMapItem *kmi;
500         
501         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
502         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
503         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
504         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
505         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
506         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
507         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", TKEY, KM_PRESS, 0, 0);
508
509         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
510         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", TRUE);
511         
512         keymap= WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
513         
514         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
515         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
516         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
517         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
518         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
519
520         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
521         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
522
523         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
524         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
525         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
526         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
527         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
528         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
529
530         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
531         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
532         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
533         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
534         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
535         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
536         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
537
538         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
539         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
540         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
541
542         /* toggle editmode is handy to have while UV unwrapping */
543         kmi= WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
544         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
545         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
546 }
547
548 /* dropboxes */
549 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
550 {
551         if(drag->type==WM_DRAG_PATH)
552                 if(ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK))      /* rule might not work? */
553                         return 1;
554         return 0;
555 }
556
557 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
558 {
559         /* copy drag path to properties */
560         RNA_string_set(drop->ptr, "filepath", drag->path);
561 }
562
563 /* area+region dropbox definition */
564 static void image_dropboxes(void)
565 {
566         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
567         
568         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
569 }
570
571
572
573 static void image_refresh(const bContext *C, ScrArea *UNUSED(sa))
574 {
575         Scene *scene = CTX_data_scene(C);
576         SpaceImage *sima= CTX_wm_space_image(C);
577         Object *obedit= CTX_data_edit_object(C);
578         Image *ima;
579
580         ima= ED_space_image(sima);
581
582         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
583                 BKE_image_user_calc_frame(&sima->iuser, scene->r.cfra, 0);
584         
585         /* check if we have to set the image from the editmesh */
586         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
587         else if(obedit && obedit->type == OB_MESH) {
588                 Mesh *me= (Mesh*)obedit->data;
589                 struct BMEditMesh *em= me->edit_btmesh;
590                 int sloppy= 1; /* partially selected face is ok */
591
592                 if(scene_use_new_shading_nodes(scene)) {
593                         /* new shading system, get image from material */
594                         BMFace *efa = BM_get_actFace(em->bm, sloppy);
595
596                         if(efa) {
597                                 Image *node_ima;
598                                 ED_object_get_active_image(obedit, efa->mat_nr, &node_ima, NULL, NULL);
599
600                                 if(node_ima)
601                                         sima->image= node_ima;
602                         }
603                 }
604                 else {
605                         /* old shading system, we set texface */
606                         MTexPoly *tf;
607                         
608                         if(em && EDBM_texFaceCheck(em)) {
609                                 sima->image= NULL;
610                                 
611                                 tf = EDBM_get_active_mtexpoly(em, NULL, 1); /* partially selected face is ok */
612                                 
613                                 if(tf) {
614                                         /* don't need to check for pin here, see above */
615                                         sima->image= tf->tpage;
616                                         
617                                         if(sima->flag & SI_EDITTILE);
618                                         else sima->curtile= tf->tile;
619                                 }
620                         }
621                 }
622         }
623 }
624
625 static void image_listener(ScrArea *sa, wmNotifier *wmn)
626 {
627         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
628         
629         /* context changes */
630         switch(wmn->category) {
631                 case NC_SCENE:
632                         switch(wmn->data) {
633                                 case ND_FRAME:
634                                         image_scopes_tag_refresh(sa);
635                                         ED_area_tag_refresh(sa);
636                                         ED_area_tag_redraw(sa);                                 
637                                         break;
638                                 case ND_MODE:
639                                 case ND_RENDER_RESULT:
640                                 case ND_COMPO_RESULT:
641                                         if (ED_space_image_show_render(sima))
642                                                 image_scopes_tag_refresh(sa);
643                                         ED_area_tag_refresh(sa);
644                                         ED_area_tag_redraw(sa);                                 
645                                         break;
646                         }
647                         break;
648                 case NC_IMAGE:
649                         if (wmn->reference == sima->image || !wmn->reference) {
650                                 image_scopes_tag_refresh(sa);
651                                 ED_area_tag_refresh(sa);
652                                 ED_area_tag_redraw(sa);
653                         }
654                         break;
655                 case NC_SPACE:  
656                         if(wmn->data == ND_SPACE_IMAGE) {
657                                 image_scopes_tag_refresh(sa);
658                                 ED_area_tag_redraw(sa);
659                         }
660                         break;
661                 case NC_GEOM:
662                         switch(wmn->data) {
663                                 case ND_DATA:
664                                 case ND_SELECT:
665                                         image_scopes_tag_refresh(sa);
666                                         ED_area_tag_refresh(sa);
667                                         ED_area_tag_redraw(sa);
668                                         break;
669                         }
670                 case NC_OBJECT:
671                 {
672                         Object *ob= (Object *)wmn->reference;
673                         switch(wmn->data) {
674                                 case ND_TRANSFORM:
675                                 case ND_MODIFIER:
676                                         if(ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
677                                                 ED_area_tag_refresh(sa);
678                                                 ED_area_tag_redraw(sa);
679                                         }
680                                         break;
681                         }
682                 }
683         }
684 }
685
686 const char *image_context_dir[] = {"edit_image", NULL};
687
688 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
689 {
690         SpaceImage *sima= CTX_wm_space_image(C);
691
692         if(CTX_data_dir(member)) {
693                 CTX_data_dir_set(result, image_context_dir);
694         }
695         else if(CTX_data_equals(member, "edit_image")) {
696                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
697                 return 1;
698         }
699
700         return 0;
701 }
702
703 /************************** main region ***************************/
704
705 /* sets up the fields of the View2D from zoom and offset */
706 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
707 {
708         Image *ima= ED_space_image(sima);
709         float x1, y1, w, h;
710         int width, height, winx, winy;
711         
712 #if 0
713         if(image_preview_active(curarea, &width, &height));
714         else
715 #endif
716         ED_space_image_size(sima, &width, &height);
717
718         w= width;
719         h= height;
720         
721         if(ima)
722                 h *= ima->aspy/ima->aspx;
723
724         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
725         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
726                 
727         ar->v2d.tot.xmin= 0;
728         ar->v2d.tot.ymin= 0;
729         ar->v2d.tot.xmax= w;
730         ar->v2d.tot.ymax= h;
731         
732         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
733         ar->v2d.mask.xmax= winx;
734         ar->v2d.mask.ymax= winy;
735
736         /* which part of the image space do we see? */
737         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
738         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
739
740         x1-= sima->zoom*sima->xof;
741         y1-= sima->zoom*sima->yof;
742         
743         /* relative display right */
744         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
745         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
746         
747         /* relative display left */
748         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
749         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
750         
751         /* normalize 0.0..1.0 */
752         ar->v2d.cur.xmin /= w;
753         ar->v2d.cur.xmax /= w;
754         ar->v2d.cur.ymin /= h;
755         ar->v2d.cur.ymax /= h;
756 }
757
758 /* add handlers, stuff you only do once or on area/region changes */
759 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
760 {
761         wmKeyMap *keymap;
762         
763         // image space manages own v2d
764         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
765
766         /* image paint polls for mode */
767         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
768         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
769
770         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
771         WM_event_add_keymap_handler(&ar->handlers, keymap);
772         
773         keymap= WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
774         WM_event_add_keymap_handler(&ar->handlers, keymap);
775
776         /* own keymaps */
777         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
778         WM_event_add_keymap_handler(&ar->handlers, keymap);
779         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
780         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
781
782 }
783
784 static void image_main_area_draw(const bContext *C, ARegion *ar)
785 {
786         /* draw entirely, view changes should be handled here */
787         SpaceImage *sima= CTX_wm_space_image(C);
788         Object *obact= CTX_data_active_object(C);
789         Object *obedit= CTX_data_edit_object(C);
790         Scene *scene= CTX_data_scene(C);
791         View2D *v2d= &ar->v2d;
792         //View2DScrollers *scrollers;
793         float col[3];
794         
795         /* XXX not supported yet, disabling for now */
796         scene->r.scemode &= ~R_COMP_CROP;
797         
798         /* clear and setup matrix */
799         UI_GetThemeColor3fv(TH_BACK, col);
800         glClearColor(col[0], col[1], col[2], 0.0);
801         glClear(GL_COLOR_BUFFER_BIT);
802
803         /* put scene context variable in iuser */
804         sima->iuser.scene= scene;
805
806         /* we set view2d from own zoom and offset each time */
807         image_main_area_set_view2d(sima, ar);
808         
809         /* we draw image in pixelspace */
810         draw_image_main(sima, ar, scene);
811
812         /* and uvs in 0.0-1.0 space */
813         UI_view2d_view_ortho(v2d);
814         draw_uvedit_main(sima, ar, scene, obedit, obact);
815
816         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
817                 
818         /* Grease Pencil too (in addition to UV's) */
819         draw_image_grease_pencil((bContext *)C, 1); 
820
821         UI_view2d_view_restore(C);
822
823         /* draw Grease Pencil - screen space only */
824         draw_image_grease_pencil((bContext *)C, 0);
825         
826         /* scrollers? */
827         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
828         UI_view2d_scrollers_draw(C, v2d, scrollers);
829         UI_view2d_scrollers_free(scrollers);*/
830 }
831
832 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
833 {
834         /* context changes */
835         switch(wmn->category) {
836                 case NC_SCREEN:
837                         if (wmn->data==ND_GPENCIL)
838                                 ED_region_tag_redraw(ar);
839                 break;
840         }
841 }
842
843 /* *********************** buttons region ************************ */
844
845 /* add handlers, stuff you only do once or on area/region changes */
846 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
847 {
848         wmKeyMap *keymap;
849
850         ED_region_panels_init(wm, ar);
851         
852         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
853         WM_event_add_keymap_handler(&ar->handlers, keymap);
854 }
855
856 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
857 {
858         ED_region_panels(C, ar, 1, NULL, -1);
859 }
860
861 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
862 {
863         /* context changes */
864         switch(wmn->category) {
865                 case NC_SCREEN:
866                         if (wmn->data==ND_GPENCIL)
867                                 ED_region_tag_redraw(ar);
868                         break;
869                 case NC_BRUSH:
870                         if(wmn->action==NA_EDITED)
871                                 ED_region_tag_redraw(ar);
872                         break;
873         }
874 }
875
876 /* *********************** scopes region ************************ */
877
878 /* add handlers, stuff you only do once or on area/region changes */
879 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
880 {
881         wmKeyMap *keymap;
882         
883         ED_region_panels_init(wm, ar);
884         
885         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
886         WM_event_add_keymap_handler(&ar->handlers, keymap);
887 }
888
889 static void image_scope_area_draw(const bContext *C, ARegion *ar)
890 {
891         SpaceImage *sima= CTX_wm_space_image(C);
892         Scene *scene= CTX_data_scene(C);
893         void *lock;
894         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
895         if(ibuf) {
896                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT );
897         }
898         ED_space_image_release_buffer(sima, lock);
899         
900         ED_region_panels(C, ar, 1, NULL, -1);
901 }
902
903 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
904 {
905         /* context changes */
906         switch(wmn->category) {
907                 case NC_SCENE:
908                         switch(wmn->data) {
909                                 case ND_MODE:
910                                 case ND_RENDER_RESULT:
911                                 case ND_COMPO_RESULT:
912                                         ED_region_tag_redraw(ar);
913                                         break;
914                         }
915                         break;
916                 case NC_IMAGE:
917                         ED_region_tag_redraw(ar);
918                         break;
919                 case NC_NODE:
920                         ED_region_tag_redraw(ar);
921                         break;
922                         
923         }
924 }
925
926 /************************* header region **************************/
927
928 /* add handlers, stuff you only do once or on area/region changes */
929 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
930 {
931         ED_region_header_init(ar);
932 }
933
934 static void image_header_area_draw(const bContext *C, ARegion *ar)
935 {
936         ED_region_header(C, ar);
937 }
938
939 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
940 {
941         /* context changes */
942         switch(wmn->category) {
943                 case NC_SCENE:
944                         switch(wmn->data) {
945                                 case ND_MODE:
946                                 case ND_TOOLSETTINGS:
947                                         ED_region_tag_redraw(ar);
948                                         break;
949                         }
950                         break;
951                 case NC_GEOM:
952                         switch(wmn->data) {
953                                 case ND_DATA:
954                                 case ND_SELECT:
955                                         ED_region_tag_redraw(ar);
956                                         break;
957                         }
958                         
959         }
960 }
961
962 /**************************** spacetype *****************************/
963
964 /* only called once, from space/spacetypes.c */
965 void ED_spacetype_image(void)
966 {
967         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
968         ARegionType *art;
969         
970         st->spaceid= SPACE_IMAGE;
971         strncpy(st->name, "Image", BKE_ST_MAXNAME);
972         
973         st->new= image_new;
974         st->free= image_free;
975         st->init= image_init;
976         st->duplicate= image_duplicate;
977         st->operatortypes= image_operatortypes;
978         st->keymap= image_keymap;
979         st->dropboxes= image_dropboxes;
980         st->refresh= image_refresh;
981         st->listener= image_listener;
982         st->context= image_context;
983         
984         /* regions: main window */
985         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
986         art->regionid = RGN_TYPE_WINDOW;
987         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
988         art->init= image_main_area_init;
989         art->draw= image_main_area_draw;
990         art->listener= image_main_area_listener;
991
992         BLI_addhead(&st->regiontypes, art);
993         
994         /* regions: listview/buttons */
995         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
996         art->regionid = RGN_TYPE_UI;
997         art->prefsizex= 220; // XXX
998         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
999         art->listener= image_buttons_area_listener;
1000         art->init= image_buttons_area_init;
1001         art->draw= image_buttons_area_draw;
1002         BLI_addhead(&st->regiontypes, art);
1003
1004         image_buttons_register(art);
1005         ED_uvedit_buttons_register(art);
1006         
1007         /* regions: statistics/scope buttons */
1008         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
1009         art->regionid = RGN_TYPE_PREVIEW;
1010         art->prefsizex= 220; // XXX
1011         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1012         art->listener= image_scope_area_listener;
1013         art->init= image_scope_area_init;
1014         art->draw= image_scope_area_draw;
1015         BLI_addhead(&st->regiontypes, art);
1016
1017         /* regions: header */
1018         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
1019         art->regionid = RGN_TYPE_HEADER;
1020         art->prefsizey= HEADERY;
1021         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
1022         art->listener= image_header_area_listener;
1023         art->init= image_header_area_init;
1024         art->draw= image_header_area_draw;
1025         
1026         BLI_addhead(&st->regiontypes, art);
1027         
1028         BKE_spacetype_register(st);
1029 }
1030