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