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