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