Nodes: add cycles shader nodes, these will only be available to render engines
[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_mesh.h"
51 #include "BKE_screen.h"
52
53 #include "IMB_imbuf_types.h"
54
55 #include "ED_image.h"
56 #include "ED_mesh.h"
57 #include "ED_space_api.h"
58 #include "ED_screen.h"
59 #include "ED_uvedit.h"
60
61 #include "BIF_gl.h"
62
63 #include "RNA_access.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "UI_resources.h"
69 #include "UI_view2d.h"
70
71 #include "image_intern.h"
72
73 /**************************** common state *****************************/
74
75 /* note; image_panel_properties() uses pointer to sima->image directly */
76 Image *ED_space_image(SpaceImage *sima)
77 {
78         return sima->image;
79 }
80
81 /* called to assign images to UV faces */
82 void ED_space_image_set(bContext *C, SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
83 {
84         ED_uvedit_assign_image(scene, obedit, ima, sima->image);
85         
86         /* change the space ima after because uvedit_face_visible uses the space ima
87          * to check if the face is displayed in UV-localview */
88         sima->image= ima;
89         
90         if(ima == NULL || ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
91                 sima->flag &= ~SI_DRAWTOOL;
92         
93         if(sima->image)
94                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
95         
96         if(sima->image && sima->image->id.us==0)
97                 sima->image->id.us= 1;
98         
99         if(C) {
100                 if(obedit)
101                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
102                 
103                 ED_area_tag_redraw(CTX_wm_area(C));
104                 
105         }
106 }
107
108 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
109 {
110         ImBuf *ibuf;
111         
112         if(sima && sima->image) {
113 #if 0
114                 if(sima->image->type==IMA_TYPE_R_RESULT && BIF_show_render_spare())
115                         return BIF_render_spare_imbuf();
116                 else
117 #endif
118                         ibuf= BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
119                 
120                 if(ibuf && (ibuf->rect || ibuf->rect_float))
121                         return ibuf;
122         }
123         
124         return NULL;
125 }
126
127 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
128 {
129         if(sima && sima->image)
130                 BKE_image_release_ibuf(sima->image, lock);
131 }
132
133 int ED_space_image_has_buffer(SpaceImage *sima)
134 {
135         ImBuf *ibuf;
136         void *lock;
137         int has_buffer;
138         
139         ibuf= ED_space_image_acquire_buffer(sima, &lock);
140         has_buffer= (ibuf != NULL);
141         ED_space_image_release_buffer(sima, lock);
142         
143         return has_buffer;
144 }
145
146 void ED_image_size(Image *ima, int *width, int *height)
147 {
148         ImBuf *ibuf= NULL;
149         void *lock;
150         
151         if(ima)
152                 ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
153         
154         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
155                 *width= ibuf->x;
156                 *height= ibuf->y;
157         }
158         else {
159                 *width= 256;
160                 *height= 256;
161         }
162         
163         if(ima)
164                 BKE_image_release_ibuf(ima, lock);
165 }
166
167 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
168 {
169         Scene *scene= sima->iuser.scene;
170         ImBuf *ibuf;
171         void *lock;
172         
173         ibuf= ED_space_image_acquire_buffer(sima, &lock);
174         
175         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
176                 *width= ibuf->x;
177                 *height= ibuf->y;
178         }
179         else if(sima->image && sima->image->type==IMA_TYPE_R_RESULT && scene) {
180                 /* not very important, just nice */
181                 *width= (scene->r.xsch*scene->r.size)/100;
182                 *height= (scene->r.ysch*scene->r.size)/100;
183
184                 if((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
185                         *width *= (scene->r.border.xmax - scene->r.border.xmin);
186                         *height *= (scene->r.border.ymax - scene->r.border.ymin);
187                 }
188
189         }
190         /* I know a bit weak... but preview uses not actual image size */
191         // XXX else if(image_preview_active(sima, width, height));
192         else {
193                 *width= 256;
194                 *height= 256;
195         }
196         
197         ED_space_image_release_buffer(sima, lock);
198 }
199
200 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
201 {
202         *aspx= *aspy= 1.0;
203         
204         if((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
205            (ima->aspx==0.0f || ima->aspy==0.0f))
206                 return;
207         
208         /* x is always 1 */
209         *aspy = ima->aspy/ima->aspx;
210 }
211
212 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
213 {
214         ED_image_aspect(ED_space_image(sima), aspx, aspy);
215 }
216
217 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
218 {
219         int width, height;
220         
221         ED_space_image_size(sima, &width, &height);
222         
223         *zoomx= (float)(ar->winrct.xmax - ar->winrct.xmin + 1)/(float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin)*width);
224         *zoomy= (float)(ar->winrct.ymax - ar->winrct.ymin + 1)/(float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin)*height);
225 }
226
227 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
228 {
229         int w, h;
230         
231         ED_space_image_aspect(sima, aspx, aspy);
232         ED_space_image_size(sima, &w, &h);
233
234         *aspx *= (float)w;
235         *aspy *= (float)h;
236         
237         if(*aspx < *aspy) {
238                 *aspy= *aspy / *aspx;
239                 *aspx= 1.0f;
240         }
241         else {
242                 *aspx= *aspx / *aspy;
243                 *aspy= 1.0f;            
244         }
245 }
246
247 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
248 {
249         int w, h;
250         
251         ED_image_aspect(ima, aspx, aspy);
252         ED_image_size(ima, &w, &h);
253         
254         *aspx *= (float)w;
255         *aspy *= (float)h;
256 }
257
258 int ED_space_image_show_render(SpaceImage *sima)
259 {
260         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
261 }
262
263 int ED_space_image_show_paint(SpaceImage *sima)
264 {
265         if(ED_space_image_show_render(sima))
266                 return 0;
267         
268         return (sima->flag & SI_DRAWTOOL);
269 }
270
271 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
272 {
273         if(sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
274                 return 0;
275
276         if(obedit && obedit->type == OB_MESH) {
277                 EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
278                 int ret;
279                 
280                 ret = EM_texFaceCheck(em);
281                 
282                 BKE_mesh_end_editmesh(obedit->data, em);
283                 return ret;
284         }
285         
286         return 0;
287 }
288
289 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
290 {
291         if(ED_space_image_show_render(sima))
292                 return 0;
293         
294         if(ED_space_image_show_paint(sima))
295                 if(obedit && obedit->type == OB_MESH) {
296                         EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
297                         int ret;
298                         
299                         ret = EM_texFaceCheck(em);
300                         
301                         BKE_mesh_end_editmesh(obedit->data, em);
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", 1);
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         SpaceImage *sima= CTX_wm_space_image(C);
576         Object *obedit= CTX_data_edit_object(C);
577         Image *ima;
578
579         ima= ED_space_image(sima);
580
581         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
582                 BKE_image_user_calc_frame(&sima->iuser, CTX_data_scene(C)->r.cfra, 0);
583         
584         /* check if we have to set the image from the editmesh */
585         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
586         else if(obedit && obedit->type == OB_MESH) {
587                 Mesh *me= (Mesh*)obedit->data;
588                 EditMesh *em= BKE_mesh_get_editmesh(me);
589                 MTFace *tf;
590                 
591                 if(em && EM_texFaceCheck(em)) {
592                         sima->image= NULL;
593                         
594                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
595                         
596                         if(tf) {
597                                 /* don't need to check for pin here, see above */
598                                 sima->image= tf->tpage;
599                                 
600                                 if(sima->flag & SI_EDITTILE);
601                                 else sima->curtile= tf->tile;
602                         }
603                 }
604
605                 BKE_mesh_end_editmesh(obedit->data, em);
606         }
607 }
608
609 static void image_listener(ScrArea *sa, wmNotifier *wmn)
610 {
611         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
612         
613         /* context changes */
614         switch(wmn->category) {
615                 case NC_SCENE:
616                         switch(wmn->data) {
617                                 case ND_FRAME:
618                                         image_scopes_tag_refresh(sa);
619                                         ED_area_tag_refresh(sa);
620                                         ED_area_tag_redraw(sa);                                 
621                                         break;
622                                 case ND_MODE:
623                                 case ND_RENDER_RESULT:
624                                 case ND_COMPO_RESULT:
625                                         if (ED_space_image_show_render(sima))
626                                                 image_scopes_tag_refresh(sa);
627                                         ED_area_tag_refresh(sa);
628                                         ED_area_tag_redraw(sa);                                 
629                                         break;
630                         }
631                         break;
632                 case NC_IMAGE:
633                         if (wmn->reference == sima->image || !wmn->reference) {
634                                 image_scopes_tag_refresh(sa);
635                                 ED_area_tag_refresh(sa);
636                                 ED_area_tag_redraw(sa);
637                         }
638                         break;
639                 case NC_SPACE:  
640                         if(wmn->data == ND_SPACE_IMAGE) {
641                                 image_scopes_tag_refresh(sa);
642                                 ED_area_tag_redraw(sa);
643                         }
644                         break;
645                 case NC_GEOM:
646                         switch(wmn->data) {
647                                 case ND_DATA:
648                                 case ND_SELECT:
649                                         image_scopes_tag_refresh(sa);
650                                         ED_area_tag_refresh(sa);
651                                         ED_area_tag_redraw(sa);
652                                         break;
653                         }
654                 case NC_OBJECT:
655                 {
656                         Object *ob= (Object *)wmn->reference;
657                         switch(wmn->data) {
658                                 case ND_TRANSFORM:
659                                 case ND_MODIFIER:
660                                         if(ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
661                                                 ED_area_tag_refresh(sa);
662                                                 ED_area_tag_redraw(sa);
663                                         }
664                                         break;
665                         }
666                 }
667         }
668 }
669
670 const char *image_context_dir[] = {"edit_image", NULL};
671
672 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
673 {
674         SpaceImage *sima= CTX_wm_space_image(C);
675
676         if(CTX_data_dir(member)) {
677                 CTX_data_dir_set(result, image_context_dir);
678         }
679         else if(CTX_data_equals(member, "edit_image")) {
680                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
681                 return 1;
682         }
683
684         return 0;
685 }
686
687 /************************** main region ***************************/
688
689 /* sets up the fields of the View2D from zoom and offset */
690 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
691 {
692         Image *ima= ED_space_image(sima);
693         float x1, y1, w, h;
694         int width, height, winx, winy;
695         
696 #if 0
697         if(image_preview_active(curarea, &width, &height));
698         else
699 #endif
700         ED_space_image_size(sima, &width, &height);
701
702         w= width;
703         h= height;
704         
705         if(ima)
706                 h *= ima->aspy/ima->aspx;
707
708         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
709         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
710                 
711         ar->v2d.tot.xmin= 0;
712         ar->v2d.tot.ymin= 0;
713         ar->v2d.tot.xmax= w;
714         ar->v2d.tot.ymax= h;
715         
716         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
717         ar->v2d.mask.xmax= winx;
718         ar->v2d.mask.ymax= winy;
719
720         /* which part of the image space do we see? */
721         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
722         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
723
724         x1-= sima->zoom*sima->xof;
725         y1-= sima->zoom*sima->yof;
726         
727         /* relative display right */
728         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
729         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
730         
731         /* relative display left */
732         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
733         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
734         
735         /* normalize 0.0..1.0 */
736         ar->v2d.cur.xmin /= w;
737         ar->v2d.cur.xmax /= w;
738         ar->v2d.cur.ymin /= h;
739         ar->v2d.cur.ymax /= h;
740 }
741
742 /* add handlers, stuff you only do once or on area/region changes */
743 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
744 {
745         wmKeyMap *keymap;
746         
747         // image space manages own v2d
748         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
749
750         /* image paint polls for mode */
751         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
752         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
753
754         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
755         WM_event_add_keymap_handler(&ar->handlers, keymap);
756         
757         /* own keymaps */
758         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
759         WM_event_add_keymap_handler(&ar->handlers, keymap);
760         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
761         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
762
763 }
764
765 static void image_main_area_draw(const bContext *C, ARegion *ar)
766 {
767         /* draw entirely, view changes should be handled here */
768         SpaceImage *sima= CTX_wm_space_image(C);
769         Object *obedit= CTX_data_edit_object(C);
770         Scene *scene= CTX_data_scene(C);
771         View2D *v2d= &ar->v2d;
772         //View2DScrollers *scrollers;
773         float col[3];
774         
775         /* XXX not supported yet, disabling for now */
776         scene->r.scemode &= ~R_COMP_CROP;
777         
778         /* clear and setup matrix */
779         UI_GetThemeColor3fv(TH_BACK, col);
780         glClearColor(col[0], col[1], col[2], 0.0);
781         glClear(GL_COLOR_BUFFER_BIT);
782
783         /* put scene context variable in iuser */
784         sima->iuser.scene= scene;
785
786         /* we set view2d from own zoom and offset each time */
787         image_main_area_set_view2d(sima, ar);
788         
789         /* we draw image in pixelspace */
790         draw_image_main(sima, ar, scene);
791
792         /* and uvs in 0.0-1.0 space */
793         UI_view2d_view_ortho(v2d);
794         draw_uvedit_main(sima, ar, scene, obedit);
795
796         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
797                 
798         /* Grease Pencil too (in addition to UV's) */
799         draw_image_grease_pencil((bContext *)C, 1); 
800
801         UI_view2d_view_restore(C);
802
803         /* draw Grease Pencil - screen space only */
804         draw_image_grease_pencil((bContext *)C, 0);
805         
806         /* scrollers? */
807         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
808         UI_view2d_scrollers_draw(C, v2d, scrollers);
809         UI_view2d_scrollers_free(scrollers);*/
810 }
811
812 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
813 {
814         /* context changes */
815         switch(wmn->category) {
816                 case NC_SCREEN:
817                         if (wmn->data==ND_GPENCIL)
818                                 ED_region_tag_redraw(ar);
819                 break;
820         }
821 }
822
823 /* *********************** buttons region ************************ */
824
825 /* add handlers, stuff you only do once or on area/region changes */
826 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
827 {
828         wmKeyMap *keymap;
829
830         ED_region_panels_init(wm, ar);
831         
832         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
833         WM_event_add_keymap_handler(&ar->handlers, keymap);
834 }
835
836 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
837 {
838         ED_region_panels(C, ar, 1, NULL, -1);
839 }
840
841 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
842 {
843         /* context changes */
844         switch(wmn->category) {
845                 case NC_SCREEN:
846                         if (wmn->data==ND_GPENCIL)
847                                 ED_region_tag_redraw(ar);
848                         break;
849                 case NC_BRUSH:
850                         if(wmn->action==NA_EDITED)
851                                 ED_region_tag_redraw(ar);
852                         break;
853         }
854 }
855
856 /* *********************** scopes region ************************ */
857
858 /* add handlers, stuff you only do once or on area/region changes */
859 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
860 {
861         wmKeyMap *keymap;
862         
863         ED_region_panels_init(wm, ar);
864         
865         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
866         WM_event_add_keymap_handler(&ar->handlers, keymap);
867 }
868
869 static void image_scope_area_draw(const bContext *C, ARegion *ar)
870 {
871         SpaceImage *sima= CTX_wm_space_image(C);
872         Scene *scene= CTX_data_scene(C);
873         void *lock;
874         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
875         if(ibuf) {
876                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT );
877         }
878         ED_space_image_release_buffer(sima, lock);
879         
880         ED_region_panels(C, ar, 1, NULL, -1);
881 }
882
883 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
884 {
885         /* context changes */
886         switch(wmn->category) {
887                 case NC_SCENE:
888                         switch(wmn->data) {
889                                 case ND_MODE:
890                                 case ND_RENDER_RESULT:
891                                 case ND_COMPO_RESULT:
892                                         ED_region_tag_redraw(ar);
893                                         break;
894                         }
895                         break;
896                 case NC_IMAGE:
897                         ED_region_tag_redraw(ar);
898                         break;
899                 case NC_NODE:
900                         ED_region_tag_redraw(ar);
901                         break;
902                         
903         }
904 }
905
906 /************************* header region **************************/
907
908 /* add handlers, stuff you only do once or on area/region changes */
909 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
910 {
911         ED_region_header_init(ar);
912 }
913
914 static void image_header_area_draw(const bContext *C, ARegion *ar)
915 {
916         ED_region_header(C, ar);
917 }
918
919 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
920 {
921         /* context changes */
922         switch(wmn->category) {
923                 case NC_SCENE:
924                         switch(wmn->data) {
925                                 case ND_MODE:
926                                 case ND_TOOLSETTINGS:
927                                         ED_region_tag_redraw(ar);
928                                         break;
929                         }
930                         break;
931                 case NC_GEOM:
932                         switch(wmn->data) {
933                                 case ND_DATA:
934                                 case ND_SELECT:
935                                         ED_region_tag_redraw(ar);
936                                         break;
937                         }
938                         
939         }
940 }
941
942 /**************************** spacetype *****************************/
943
944 /* only called once, from space/spacetypes.c */
945 void ED_spacetype_image(void)
946 {
947         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
948         ARegionType *art;
949         
950         st->spaceid= SPACE_IMAGE;
951         strncpy(st->name, "Image", BKE_ST_MAXNAME);
952         
953         st->new= image_new;
954         st->free= image_free;
955         st->init= image_init;
956         st->duplicate= image_duplicate;
957         st->operatortypes= image_operatortypes;
958         st->keymap= image_keymap;
959         st->dropboxes= image_dropboxes;
960         st->refresh= image_refresh;
961         st->listener= image_listener;
962         st->context= image_context;
963         
964         /* regions: main window */
965         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
966         art->regionid = RGN_TYPE_WINDOW;
967         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
968         art->init= image_main_area_init;
969         art->draw= image_main_area_draw;
970         art->listener= image_main_area_listener;
971
972         BLI_addhead(&st->regiontypes, art);
973         
974         /* regions: listview/buttons */
975         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
976         art->regionid = RGN_TYPE_UI;
977         art->prefsizex= 220; // XXX
978         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
979         art->listener= image_buttons_area_listener;
980         art->init= image_buttons_area_init;
981         art->draw= image_buttons_area_draw;
982         BLI_addhead(&st->regiontypes, art);
983
984         image_buttons_register(art);
985         ED_uvedit_buttons_register(art);
986         
987         /* regions: statistics/scope buttons */
988         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
989         art->regionid = RGN_TYPE_PREVIEW;
990         art->prefsizex= 220; // XXX
991         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
992         art->listener= image_scope_area_listener;
993         art->init= image_scope_area_init;
994         art->draw= image_scope_area_draw;
995         BLI_addhead(&st->regiontypes, art);
996
997         /* regions: header */
998         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
999         art->regionid = RGN_TYPE_HEADER;
1000         art->prefsizey= HEADERY;
1001         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
1002         art->listener= image_header_area_listener;
1003         art->init= image_header_area_init;
1004         art->draw= image_header_area_draw;
1005         
1006         BLI_addhead(&st->regiontypes, art);
1007         
1008         BKE_spacetype_register(st);
1009 }
1010