Merging r47667 through r47668 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_image / space_image.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_image/space_image.c
28  *  \ingroup spimage
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_rand.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_image.h"
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_mesh.h"
53 #include "BKE_scene.h"
54 #include "BKE_screen.h"
55 #include "BKE_tessmesh.h"
56
57 #include "IMB_imbuf_types.h"
58
59 #include "ED_image.h"
60 #include "ED_mesh.h"
61 #include "ED_space_api.h"
62 #include "ED_screen.h"
63 #include "ED_uvedit.h"
64
65 #include "BIF_gl.h"
66
67 #include "RNA_access.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "UI_resources.h"
73 #include "UI_view2d.h"
74
75 #include "image_intern.h"
76
77 /**************************** common state *****************************/
78
79 /* note; image_panel_properties() uses pointer to sima->image directly */
80 Image *ED_space_image(SpaceImage *sima)
81 {
82         return sima->image;
83 }
84
85 /* called to assign images to UV faces */
86 void ED_space_image_set(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
87 {
88         /* context may be NULL, so use global */
89         ED_uvedit_assign_image(G.main, scene, obedit, ima, sima->image);
90         
91         /* change the space ima after because uvedit_face_visible_test uses the space ima
92          * to check if the face is displayed in UV-localview */
93         sima->image = ima;
94         
95         if (ima == NULL || ima->type == IMA_TYPE_R_RESULT || ima->type == IMA_TYPE_COMPOSITE)
96                 sima->flag &= ~SI_DRAWTOOL;
97         
98         if (sima->image)
99                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
100         
101         if (sima->image && sima->image->id.us == 0)
102                 sima->image->id.us = 1;
103         
104         if (obedit)
105                 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
106
107         WM_main_add_notifier(NC_SPACE | ND_SPACE_IMAGE, NULL);
108 }
109
110 ImBuf *ED_space_image_acquire_buffer(SpaceImage *sima, void **lock_r)
111 {
112         ImBuf *ibuf;
113         
114         if (sima && sima->image) {
115 #if 0
116                 if (sima->image->type == IMA_TYPE_R_RESULT && BIF_show_render_spare())
117                         return BIF_render_spare_imbuf();
118                 else
119 #endif
120                 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, lock_r);
121                 
122                 if (ibuf && (ibuf->rect || ibuf->rect_float))
123                         return ibuf;
124         }
125         
126         return NULL;
127 }
128
129 void ED_space_image_release_buffer(SpaceImage *sima, void *lock)
130 {
131         if (sima && sima->image)
132                 BKE_image_release_ibuf(sima->image, lock);
133 }
134
135 int ED_space_image_has_buffer(SpaceImage *sima)
136 {
137         ImBuf *ibuf;
138         void *lock;
139         int has_buffer;
140         
141         ibuf = ED_space_image_acquire_buffer(sima, &lock);
142         has_buffer = (ibuf != NULL);
143         ED_space_image_release_buffer(sima, lock);
144         
145         return has_buffer;
146 }
147
148 void ED_image_size(Image *ima, int *width, int *height)
149 {
150         ImBuf *ibuf = NULL;
151         void *lock;
152         
153         if (ima)
154                 ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
155         
156         if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
157                 *width = ibuf->x;
158                 *height = ibuf->y;
159         }
160         else {
161                 *width = 256;
162                 *height = 256;
163         }
164         
165         if (ima)
166                 BKE_image_release_ibuf(ima, lock);
167 }
168
169 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
170 {
171         Scene *scene = sima->iuser.scene;
172         ImBuf *ibuf;
173         void *lock;
174         
175         ibuf = ED_space_image_acquire_buffer(sima, &lock);
176         
177         if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
178                 *width = ibuf->x;
179                 *height = ibuf->y;
180         }
181         else if (sima->image && sima->image->type == IMA_TYPE_R_RESULT && scene) {
182                 /* not very important, just nice */
183                 *width = (scene->r.xsch * scene->r.size) / 100;
184                 *height = (scene->r.ysch * scene->r.size) / 100;
185
186                 if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
187                         *width *= (scene->r.border.xmax - scene->r.border.xmin);
188                         *height *= (scene->r.border.ymax - scene->r.border.ymin);
189                 }
190
191         }
192         /* I know a bit weak... but preview uses not actual image size */
193         // XXX else if (image_preview_active(sima, width, height));
194         else {
195                 *width = 256;
196                 *height = 256;
197         }
198         
199         ED_space_image_release_buffer(sima, lock);
200 }
201
202 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
203 {
204         *aspx = *aspy = 1.0;
205         
206         if ((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
207             (ima->aspx == 0.0f || ima->aspy == 0.0f))
208         {
209                 return;
210         }
211         
212         /* x is always 1 */
213         *aspy = ima->aspy / ima->aspx;
214 }
215
216 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
217 {
218         ED_image_aspect(ED_space_image(sima), aspx, aspy);
219 }
220
221 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
222 {
223         int width, height;
224         
225         ED_space_image_size(sima, &width, &height);
226         
227         *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1) / (float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin) * width);
228         *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1) / (float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin) * height);
229 }
230
231 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
232 {
233         int w, h;
234         
235         ED_space_image_aspect(sima, aspx, aspy);
236         ED_space_image_size(sima, &w, &h);
237
238         *aspx *= (float)w;
239         *aspy *= (float)h;
240         
241         if (*aspx < *aspy) {
242                 *aspy = *aspy / *aspx;
243                 *aspx = 1.0f;
244         }
245         else {
246                 *aspx = *aspx / *aspy;
247                 *aspy = 1.0f;
248         }
249 }
250
251 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
252 {
253         int w, h;
254         
255         ED_image_aspect(ima, aspx, aspy);
256         ED_image_size(ima, &w, &h);
257         
258         *aspx *= (float)w;
259         *aspy *= (float)h;
260 }
261
262 int ED_space_image_show_render(SpaceImage *sima)
263 {
264         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
265 }
266
267 int ED_space_image_show_paint(SpaceImage *sima)
268 {
269         if (ED_space_image_show_render(sima))
270                 return 0;
271         
272         return (sima->flag & SI_DRAWTOOL);
273 }
274
275 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
276 {
277         if (sima && (ED_space_image_show_render(sima) || ED_space_image_show_paint(sima)))
278                 return 0;
279
280         if (obedit && obedit->type == OB_MESH) {
281                 struct BMEditMesh *em = BMEdit_FromObject(obedit);
282                 int ret;
283                 
284                 ret = EDBM_mtexpoly_check(em);
285                 
286                 return ret;
287         }
288         
289         return 0;
290 }
291
292 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
293 {
294         if (ED_space_image_show_render(sima))
295                 return 0;
296         
297         if (ED_space_image_show_paint(sima))
298                 if (obedit && obedit->type == OB_MESH) {
299                         struct BMEditMesh *em = BMEdit_FromObject(obedit);
300                         int ret;
301                         
302                         ret = EDBM_mtexpoly_check(em);
303                         
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         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
333         if (ar) return ar;
334         
335         /* add subdiv level; after header */
336         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
337
338         /* is error! */
339         if (ar == NULL) return NULL;
340         
341         arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
342         
343         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
344         arnew->regiontype = RGN_TYPE_UI;
345         arnew->alignment = RGN_ALIGN_LEFT;
346         
347         arnew->flag = RGN_FLAG_HIDDEN;
348         
349         return arnew;
350 }
351
352 ARegion *image_has_scope_region(ScrArea *sa)
353 {
354         ARegion *ar, *arnew;
355
356         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
357         if (ar) return ar;
358
359         /* add subdiv level; after buttons */
360         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
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         scopes_new(&simagen->scopes);
459
460         return (SpaceLink *)simagen;
461 }
462
463 static void image_operatortypes(void)
464 {
465         WM_operatortype_append(IMAGE_OT_view_all);
466         WM_operatortype_append(IMAGE_OT_view_pan);
467         WM_operatortype_append(IMAGE_OT_view_selected);
468         WM_operatortype_append(IMAGE_OT_view_zoom);
469         WM_operatortype_append(IMAGE_OT_view_zoom_in);
470         WM_operatortype_append(IMAGE_OT_view_zoom_out);
471         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
472         WM_operatortype_append(IMAGE_OT_view_ndof);
473
474         WM_operatortype_append(IMAGE_OT_new);
475         WM_operatortype_append(IMAGE_OT_open);
476         WM_operatortype_append(IMAGE_OT_match_movie_length);
477         WM_operatortype_append(IMAGE_OT_replace);
478         WM_operatortype_append(IMAGE_OT_reload);
479         WM_operatortype_append(IMAGE_OT_save);
480         WM_operatortype_append(IMAGE_OT_save_as);
481         WM_operatortype_append(IMAGE_OT_save_sequence);
482         WM_operatortype_append(IMAGE_OT_pack);
483         WM_operatortype_append(IMAGE_OT_unpack);
484         
485         WM_operatortype_append(IMAGE_OT_invert);
486
487         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
488
489         WM_operatortype_append(IMAGE_OT_sample);
490         WM_operatortype_append(IMAGE_OT_sample_line);
491         WM_operatortype_append(IMAGE_OT_curves_point_set);
492
493         WM_operatortype_append(IMAGE_OT_record_composite);
494
495         WM_operatortype_append(IMAGE_OT_properties);
496         WM_operatortype_append(IMAGE_OT_scopes);
497 }
498
499 static void image_keymap(struct wmKeyConfig *keyconf)
500 {
501         wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
502         wmKeyMapItem *kmi;
503         int i;
504         
505         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
506         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
507         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
508         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
509         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
510         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
511         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", TKEY, KM_PRESS, 0, 0);
512
513         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
514         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", TRUE);
515         
516         keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
517         
518         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
519         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
520         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
521         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
522         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
523
524         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
525         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 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", TRUE);
550
551         /* fast switch to render slots */
552         for (i = 0; i < MAX2(IMA_MAX_RENDER_SLOT, 9); i++) {
553                 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY+i, KM_PRESS, 0, 0);
554                 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slot");
555                 RNA_int_set(kmi->ptr, "value", i);
556         }
557 }
558
559 /* dropboxes */
560 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
561 {
562         if (drag->type == WM_DRAG_PATH)
563                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
564                         return 1;
565         return 0;
566 }
567
568 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
569 {
570         /* copy drag path to properties */
571         RNA_string_set(drop->ptr, "filepath", drag->path);
572 }
573
574 /* area+region dropbox definition */
575 static void image_dropboxes(void)
576 {
577         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
578         
579         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
580 }
581
582
583
584 static void image_refresh(const bContext *C, ScrArea *UNUSED(sa))
585 {
586         Scene *scene = CTX_data_scene(C);
587         SpaceImage *sima = CTX_wm_space_image(C);
588         Object *obedit = CTX_data_edit_object(C);
589         Image *ima;
590
591         ima = ED_space_image(sima);
592
593         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
594         
595         /* check if we have to set the image from the editmesh */
596         if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) ;
597         else if (obedit && obedit->type == OB_MESH) {
598                 Mesh *me = (Mesh *)obedit->data;
599                 struct BMEditMesh *em = me->edit_btmesh;
600                 int sloppy = 1; /* partially selected face is ok */
601
602                 if (BKE_scene_use_new_shading_nodes(scene)) {
603                         /* new shading system, get image from material */
604                         BMFace *efa = BM_active_face_get(em->bm, sloppy);
605
606                         if (efa) {
607                                 Image *node_ima;
608                                 ED_object_get_active_image(obedit, efa->mat_nr + 1, &node_ima, NULL, NULL);
609
610                                 if (node_ima)
611                                         sima->image = node_ima;
612                         }
613                 }
614                 else {
615                         /* old shading system, we set texface */
616                         MTexPoly *tf;
617                         
618                         if (em && EDBM_mtexpoly_check(em)) {
619                                 sima->image = NULL;
620                                 
621                                 tf = EDBM_mtexpoly_active_get(em, NULL, TRUE); /* partially selected face is ok */
622                                 
623                                 if (tf) {
624                                         /* don't need to check for pin here, see above */
625                                         sima->image = tf->tpage;
626                                         
627                                         if (sima->flag & SI_EDITTILE) ;
628                                         else sima->curtile = tf->tile;
629                                 }
630                         }
631                 }
632         }
633 }
634
635 static void image_listener(ScrArea *sa, wmNotifier *wmn)
636 {
637         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
638         
639         /* context changes */
640         switch (wmn->category) {
641                 case NC_SCENE:
642                         switch (wmn->data) {
643                                 case ND_FRAME:
644                                         image_scopes_tag_refresh(sa);
645                                         ED_area_tag_refresh(sa);
646                                         ED_area_tag_redraw(sa);                                 
647                                         break;
648                                 case ND_MODE:
649                                 case ND_RENDER_RESULT:
650                                 case ND_COMPO_RESULT:
651                                         if (ED_space_image_show_render(sima))
652                                                 image_scopes_tag_refresh(sa);
653                                         ED_area_tag_refresh(sa);
654                                         ED_area_tag_redraw(sa);                                 
655                                         break;
656                         }
657                         break;
658                 case NC_IMAGE:
659                         if (wmn->reference == sima->image || !wmn->reference) {
660                                 image_scopes_tag_refresh(sa);
661                                 ED_area_tag_refresh(sa);
662                                 ED_area_tag_redraw(sa);
663                         }
664                         break;
665                 case NC_SPACE:  
666                         if (wmn->data == ND_SPACE_IMAGE) {
667                                 image_scopes_tag_refresh(sa);
668                                 ED_area_tag_redraw(sa);
669                         }
670                         break;
671                 case NC_GEOM:
672                         switch (wmn->data) {
673                                 case ND_DATA:
674                                 case ND_SELECT:
675                                         image_scopes_tag_refresh(sa);
676                                         ED_area_tag_refresh(sa);
677                                         ED_area_tag_redraw(sa);
678                                         break;
679                         }
680                 case NC_OBJECT:
681                 {
682                         Object *ob = (Object *)wmn->reference;
683                         switch (wmn->data) {
684                                 case ND_TRANSFORM:
685                                 case ND_MODIFIER:
686                                         if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
687                                                 ED_area_tag_refresh(sa);
688                                                 ED_area_tag_redraw(sa);
689                                         }
690                                         break;
691                         }
692                 }
693         }
694 }
695
696 const char *image_context_dir[] = {"edit_image", NULL};
697
698 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
699 {
700         SpaceImage *sima = CTX_wm_space_image(C);
701
702         if (CTX_data_dir(member)) {
703                 CTX_data_dir_set(result, image_context_dir);
704         }
705         else if (CTX_data_equals(member, "edit_image")) {
706                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
707                 return 1;
708         }
709
710         return 0;
711 }
712
713 /************************** main region ***************************/
714
715 /* sets up the fields of the View2D from zoom and offset */
716 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
717 {
718         Image *ima = ED_space_image(sima);
719         float x1, y1, w, h;
720         int width, height, winx, winy;
721         
722 #if 0
723         if (image_preview_active(curarea, &width, &height)) ;
724         else
725 #endif
726         ED_space_image_size(sima, &width, &height);
727
728         w = width;
729         h = height;
730         
731         if (ima)
732                 h *= ima->aspy / ima->aspx;
733
734         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
735         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
736                 
737         ar->v2d.tot.xmin = 0;
738         ar->v2d.tot.ymin = 0;
739         ar->v2d.tot.xmax = w;
740         ar->v2d.tot.ymax = h;
741         
742         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
743         ar->v2d.mask.xmax = winx;
744         ar->v2d.mask.ymax = winy;
745
746         /* which part of the image space do we see? */
747         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
748         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
749
750         x1 -= sima->zoom * sima->xof;
751         y1 -= sima->zoom * sima->yof;
752         
753         /* relative display right */
754         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
755         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
756         
757         /* relative display left */
758         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
759         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
760         
761         /* normalize 0.0..1.0 */
762         ar->v2d.cur.xmin /= w;
763         ar->v2d.cur.xmax /= w;
764         ar->v2d.cur.ymin /= h;
765         ar->v2d.cur.ymax /= h;
766 }
767
768 /* add handlers, stuff you only do once or on area/region changes */
769 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
770 {
771         wmKeyMap *keymap;
772         
773         // image space manages own v2d
774         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
775
776         /* image paint polls for mode */
777         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
778         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
779
780         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
781         WM_event_add_keymap_handler(&ar->handlers, keymap);
782         
783         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
784         WM_event_add_keymap_handler(&ar->handlers, keymap);
785
786         /* own keymaps */
787         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
788         WM_event_add_keymap_handler(&ar->handlers, keymap);
789         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
790         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
791
792 }
793
794 static void image_main_area_draw(const bContext *C, ARegion *ar)
795 {
796         /* draw entirely, view changes should be handled here */
797         SpaceImage *sima = CTX_wm_space_image(C);
798         Object *obact = CTX_data_active_object(C);
799         Object *obedit = CTX_data_edit_object(C);
800         Scene *scene = CTX_data_scene(C);
801         View2D *v2d = &ar->v2d;
802         //View2DScrollers *scrollers;
803         float col[3];
804         
805         /* XXX not supported yet, disabling for now */
806         scene->r.scemode &= ~R_COMP_CROP;
807         
808         /* clear and setup matrix */
809         UI_GetThemeColor3fv(TH_BACK, col);
810         glClearColor(col[0], col[1], col[2], 0.0);
811         glClear(GL_COLOR_BUFFER_BIT);
812
813         /* put scene context variable in iuser */
814         sima->iuser.scene = scene;
815
816         /* we set view2d from own zoom and offset each time */
817         image_main_area_set_view2d(sima, ar);
818         
819         /* we draw image in pixelspace */
820         draw_image_main(sima, ar, scene);
821
822         /* and uvs in 0.0-1.0 space */
823         UI_view2d_view_ortho(v2d);
824         draw_uvedit_main(sima, ar, scene, obedit, obact);
825
826         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
827
828         /* Grease Pencil too (in addition to UV's) */
829         draw_image_grease_pencil((bContext *)C, 1); 
830
831         /* sample line */
832         draw_image_sample_line(sima);
833
834         UI_view2d_view_restore(C);
835
836         /* draw Grease Pencil - screen space only */
837         draw_image_grease_pencil((bContext *)C, 0);
838
839         /* scrollers? */
840 #if 0
841         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
842         UI_view2d_scrollers_draw(C, v2d, scrollers);
843         UI_view2d_scrollers_free(scrollers);
844 #endif
845 }
846
847 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
848 {
849         /* context changes */
850         switch (wmn->category) {
851                 case NC_SCREEN:
852                         if (wmn->data == ND_GPENCIL)
853                                 ED_region_tag_redraw(ar);
854                         break;
855         }
856 }
857
858 /* *********************** buttons region ************************ */
859
860 /* add handlers, stuff you only do once or on area/region changes */
861 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
862 {
863         wmKeyMap *keymap;
864
865         ED_region_panels_init(wm, ar);
866         
867         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
868         WM_event_add_keymap_handler(&ar->handlers, keymap);
869 }
870
871 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
872 {
873         ED_region_panels(C, ar, 1, NULL, -1);
874 }
875
876 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
877 {
878         /* context changes */
879         switch (wmn->category) {
880                 case NC_SCREEN:
881                         if (wmn->data == ND_GPENCIL)
882                                 ED_region_tag_redraw(ar);
883                         break;
884                 case NC_BRUSH:
885                         if (wmn->action == NA_EDITED)
886                                 ED_region_tag_redraw(ar);
887                         break;
888                 case NC_TEXTURE:
889                 case NC_MATERIAL:
890                         /* sending by texture render job and needed to properly update displaying
891                          * brush texture icon */
892                         ED_region_tag_redraw(ar);
893                         break;
894         }
895 }
896
897 /* *********************** scopes region ************************ */
898
899 /* add handlers, stuff you only do once or on area/region changes */
900 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
901 {
902         wmKeyMap *keymap;
903         
904         ED_region_panels_init(wm, ar);
905         
906         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
907         WM_event_add_keymap_handler(&ar->handlers, keymap);
908 }
909
910 static void image_scope_area_draw(const bContext *C, ARegion *ar)
911 {
912         SpaceImage *sima = CTX_wm_space_image(C);
913         Scene *scene = CTX_data_scene(C);
914         void *lock;
915         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
916         if (ibuf) {
917                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
918         }
919         ED_space_image_release_buffer(sima, lock);
920         
921         ED_region_panels(C, ar, 1, NULL, -1);
922 }
923
924 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
925 {
926         /* context changes */
927         switch (wmn->category) {
928                 case NC_SCENE:
929                         switch (wmn->data) {
930                                 case ND_MODE:
931                                 case ND_RENDER_RESULT:
932                                 case ND_COMPO_RESULT:
933                                         ED_region_tag_redraw(ar);
934                                         break;
935                         }
936                         break;
937                 case NC_IMAGE:
938                         ED_region_tag_redraw(ar);
939                         break;
940                 case NC_NODE:
941                         ED_region_tag_redraw(ar);
942                         break;
943                         
944         }
945 }
946
947 /************************* header region **************************/
948
949 /* add handlers, stuff you only do once or on area/region changes */
950 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
951 {
952         ED_region_header_init(ar);
953 }
954
955 static void image_header_area_draw(const bContext *C, ARegion *ar)
956 {
957         ED_region_header(C, ar);
958 }
959
960 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
961 {
962         /* context changes */
963         switch (wmn->category) {
964                 case NC_SCENE:
965                         switch (wmn->data) {
966                                 case ND_MODE:
967                                 case ND_TOOLSETTINGS:
968                                         ED_region_tag_redraw(ar);
969                                         break;
970                         }
971                         break;
972                 case NC_GEOM:
973                         switch (wmn->data) {
974                                 case ND_DATA:
975                                 case ND_SELECT:
976                                         ED_region_tag_redraw(ar);
977                                         break;
978                         }
979                         
980         }
981 }
982
983 /**************************** spacetype *****************************/
984
985 /* only called once, from space/spacetypes.c */
986 void ED_spacetype_image(void)
987 {
988         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
989         ARegionType *art;
990         
991         st->spaceid = SPACE_IMAGE;
992         strncpy(st->name, "Image", BKE_ST_MAXNAME);
993         
994         st->new = image_new;
995         st->free = image_free;
996         st->init = image_init;
997         st->duplicate = image_duplicate;
998         st->operatortypes = image_operatortypes;
999         st->keymap = image_keymap;
1000         st->dropboxes = image_dropboxes;
1001         st->refresh = image_refresh;
1002         st->listener = image_listener;
1003         st->context = image_context;
1004         
1005         /* regions: main window */
1006         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1007         art->regionid = RGN_TYPE_WINDOW;
1008         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1009         art->init = image_main_area_init;
1010         art->draw = image_main_area_draw;
1011         art->listener = image_main_area_listener;
1012
1013         BLI_addhead(&st->regiontypes, art);
1014         
1015         /* regions: listview/buttons */
1016         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1017         art->regionid = RGN_TYPE_UI;
1018         art->prefsizex = 220; // XXX
1019         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1020         art->listener = image_buttons_area_listener;
1021         art->init = image_buttons_area_init;
1022         art->draw = image_buttons_area_draw;
1023         BLI_addhead(&st->regiontypes, art);
1024
1025         image_buttons_register(art);
1026         ED_uvedit_buttons_register(art);
1027         
1028         /* regions: statistics/scope buttons */
1029         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1030         art->regionid = RGN_TYPE_PREVIEW;
1031         art->prefsizex = 220; // XXX
1032         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1033         art->listener = image_scope_area_listener;
1034         art->init = image_scope_area_init;
1035         art->draw = image_scope_area_draw;
1036         BLI_addhead(&st->regiontypes, art);
1037
1038         /* regions: header */
1039         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1040         art->regionid = RGN_TYPE_HEADER;
1041         art->prefsizey = HEADERY;
1042         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1043         art->listener = image_header_area_listener;
1044         art->init = image_header_area_init;
1045         art->draw = image_header_area_draw;
1046         
1047         BLI_addhead(&st->regiontypes, art);
1048         
1049         BKE_spacetype_register(st);
1050 }
1051