97fee2238a02d0aa5c75502af26e37817b0926f6
[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         /* OCIO_TODO: use default view transform here when OCIO is completely integrated
391         *             and proper versioning stuff is added.
392         *             for now use NONE to be compatible with all current files
393         */
394         BLI_strncpy(simage->view_transform, "NONE", sizeof(simage->view_transform));
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 *simage = (SpaceImage *) sl;
459         SpaceImage *simagen = MEM_dupallocN(sl);
460         
461         /* clear or remove stuff from old */
462         if (simagen->cumap)
463                 simagen->cumap = curvemapping_copy(simagen->cumap);
464
465         scopes_new(&simagen->scopes);
466
467         BLI_strncpy(simagen->view_transform, simage->view_transform, sizeof(simage->view_transform));
468
469         return (SpaceLink *)simagen;
470 }
471
472 static void image_operatortypes(void)
473 {
474         WM_operatortype_append(IMAGE_OT_view_all);
475         WM_operatortype_append(IMAGE_OT_view_pan);
476         WM_operatortype_append(IMAGE_OT_view_selected);
477         WM_operatortype_append(IMAGE_OT_view_zoom);
478         WM_operatortype_append(IMAGE_OT_view_zoom_in);
479         WM_operatortype_append(IMAGE_OT_view_zoom_out);
480         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
481         WM_operatortype_append(IMAGE_OT_view_ndof);
482
483         WM_operatortype_append(IMAGE_OT_new);
484         WM_operatortype_append(IMAGE_OT_open);
485         WM_operatortype_append(IMAGE_OT_match_movie_length);
486         WM_operatortype_append(IMAGE_OT_replace);
487         WM_operatortype_append(IMAGE_OT_reload);
488         WM_operatortype_append(IMAGE_OT_save);
489         WM_operatortype_append(IMAGE_OT_save_as);
490         WM_operatortype_append(IMAGE_OT_save_sequence);
491         WM_operatortype_append(IMAGE_OT_pack);
492         WM_operatortype_append(IMAGE_OT_unpack);
493         
494         WM_operatortype_append(IMAGE_OT_invert);
495
496         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
497
498         WM_operatortype_append(IMAGE_OT_sample);
499         WM_operatortype_append(IMAGE_OT_sample_line);
500         WM_operatortype_append(IMAGE_OT_curves_point_set);
501
502         WM_operatortype_append(IMAGE_OT_record_composite);
503
504         WM_operatortype_append(IMAGE_OT_properties);
505         WM_operatortype_append(IMAGE_OT_scopes);
506 }
507
508 static void image_keymap(struct wmKeyConfig *keyconf)
509 {
510         wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
511         wmKeyMapItem *kmi;
512         int i;
513         
514         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
515         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
516         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
517         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
518         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
519         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
520         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", TKEY, KM_PRESS, 0, 0);
521
522         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
523         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", TRUE);
524         
525         keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
526         
527         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
528         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
529         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
530         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
531         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
532
533         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
534         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
535
536         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
537         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
538         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
539         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
540         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
541         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
542
543         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
544         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
545         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
546         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
547         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
548         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
549         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
550
551         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
552         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
553         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
554
555         /* toggle editmode is handy to have while UV unwrapping */
556         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
557         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
558         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
559
560         /* fast switch to render slots */
561         for (i = 0; i < MAX2(IMA_MAX_RENDER_SLOT, 9); i++) {
562                 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY + i, KM_PRESS, 0, 0);
563                 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slot");
564                 RNA_int_set(kmi->ptr, "value", i);
565         }
566 }
567
568 /* dropboxes */
569 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
570 {
571         if (drag->type == WM_DRAG_PATH)
572                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
573                         return 1;
574         return 0;
575 }
576
577 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
578 {
579         /* copy drag path to properties */
580         RNA_string_set(drop->ptr, "filepath", drag->path);
581 }
582
583 /* area+region dropbox definition */
584 static void image_dropboxes(void)
585 {
586         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
587         
588         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
589 }
590
591
592
593 static void image_refresh(const bContext *C, ScrArea *UNUSED(sa))
594 {
595         Scene *scene = CTX_data_scene(C);
596         SpaceImage *sima = CTX_wm_space_image(C);
597         Object *obedit = CTX_data_edit_object(C);
598         Image *ima;
599
600         ima = ED_space_image(sima);
601
602         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
603         
604         /* check if we have to set the image from the editmesh */
605         if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) ;
606         else if (obedit && obedit->type == OB_MESH) {
607                 Mesh *me = (Mesh *)obedit->data;
608                 struct BMEditMesh *em = me->edit_btmesh;
609                 int sloppy = 1; /* partially selected face is ok */
610
611                 if (BKE_scene_use_new_shading_nodes(scene)) {
612                         /* new shading system, get image from material */
613                         BMFace *efa = BM_active_face_get(em->bm, sloppy);
614
615                         if (efa) {
616                                 Image *node_ima;
617                                 ED_object_get_active_image(obedit, efa->mat_nr + 1, &node_ima, NULL, NULL);
618
619                                 if (node_ima)
620                                         sima->image = node_ima;
621                         }
622                 }
623                 else {
624                         /* old shading system, we set texface */
625                         MTexPoly *tf;
626                         
627                         if (em && EDBM_mtexpoly_check(em)) {
628                                 sima->image = NULL;
629                                 
630                                 tf = EDBM_mtexpoly_active_get(em, NULL, TRUE); /* partially selected face is ok */
631                                 
632                                 if (tf) {
633                                         /* don't need to check for pin here, see above */
634                                         sima->image = tf->tpage;
635                                         
636                                         if (sima->flag & SI_EDITTILE) ;
637                                         else sima->curtile = tf->tile;
638                                 }
639                         }
640                 }
641         }
642 }
643
644 static void image_listener(ScrArea *sa, wmNotifier *wmn)
645 {
646         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
647         
648         /* context changes */
649         switch (wmn->category) {
650                 case NC_SCENE:
651                         switch (wmn->data) {
652                                 case ND_FRAME:
653                                         image_scopes_tag_refresh(sa);
654                                         ED_area_tag_refresh(sa);
655                                         ED_area_tag_redraw(sa);                                 
656                                         break;
657                                 case ND_MODE:
658                                 case ND_RENDER_RESULT:
659                                 case ND_COMPO_RESULT:
660                                         if (ED_space_image_show_render(sima))
661                                                 image_scopes_tag_refresh(sa);
662                                         ED_area_tag_refresh(sa);
663                                         ED_area_tag_redraw(sa);                                 
664                                         break;
665                         }
666                         break;
667                 case NC_IMAGE:
668                         if (wmn->reference == sima->image || !wmn->reference) {
669                                 image_scopes_tag_refresh(sa);
670                                 ED_area_tag_refresh(sa);
671                                 ED_area_tag_redraw(sa);
672                         }
673                         break;
674                 case NC_SPACE:  
675                         if (wmn->data == ND_SPACE_IMAGE) {
676                                 image_scopes_tag_refresh(sa);
677                                 ED_area_tag_redraw(sa);
678                         }
679                         break;
680                 case NC_GEOM:
681                         switch (wmn->data) {
682                                 case ND_DATA:
683                                 case ND_SELECT:
684                                         image_scopes_tag_refresh(sa);
685                                         ED_area_tag_refresh(sa);
686                                         ED_area_tag_redraw(sa);
687                                         break;
688                         }
689                 case NC_OBJECT:
690                 {
691                         Object *ob = (Object *)wmn->reference;
692                         switch (wmn->data) {
693                                 case ND_TRANSFORM:
694                                 case ND_MODIFIER:
695                                         if (ob && (ob->mode & OB_MODE_EDIT) && sima->lock && (sima->flag & SI_DRAWSHADOW)) {
696                                                 ED_area_tag_refresh(sa);
697                                                 ED_area_tag_redraw(sa);
698                                         }
699                                         break;
700                         }
701                 }
702         }
703 }
704
705 const char *image_context_dir[] = {"edit_image", NULL};
706
707 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
708 {
709         SpaceImage *sima = CTX_wm_space_image(C);
710
711         if (CTX_data_dir(member)) {
712                 CTX_data_dir_set(result, image_context_dir);
713         }
714         else if (CTX_data_equals(member, "edit_image")) {
715                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
716                 return 1;
717         }
718
719         return 0;
720 }
721
722 /************************** main region ***************************/
723
724 /* sets up the fields of the View2D from zoom and offset */
725 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
726 {
727         Image *ima = ED_space_image(sima);
728         float x1, y1, w, h;
729         int width, height, winx, winy;
730         
731 #if 0
732         if (image_preview_active(curarea, &width, &height)) ;
733         else
734 #endif
735         ED_space_image_size(sima, &width, &height);
736
737         w = width;
738         h = height;
739         
740         if (ima)
741                 h *= ima->aspy / ima->aspx;
742
743         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
744         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
745                 
746         ar->v2d.tot.xmin = 0;
747         ar->v2d.tot.ymin = 0;
748         ar->v2d.tot.xmax = w;
749         ar->v2d.tot.ymax = h;
750         
751         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
752         ar->v2d.mask.xmax = winx;
753         ar->v2d.mask.ymax = winy;
754
755         /* which part of the image space do we see? */
756         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
757         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
758
759         x1 -= sima->zoom * sima->xof;
760         y1 -= sima->zoom * sima->yof;
761         
762         /* relative display right */
763         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
764         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
765         
766         /* relative display left */
767         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
768         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
769         
770         /* normalize 0.0..1.0 */
771         ar->v2d.cur.xmin /= w;
772         ar->v2d.cur.xmax /= w;
773         ar->v2d.cur.ymin /= h;
774         ar->v2d.cur.ymax /= h;
775 }
776
777 /* add handlers, stuff you only do once or on area/region changes */
778 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
779 {
780         wmKeyMap *keymap;
781         
782         // image space manages own v2d
783         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
784
785         /* image paint polls for mode */
786         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
787         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
788
789         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
790         WM_event_add_keymap_handler(&ar->handlers, keymap);
791         
792         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
793         WM_event_add_keymap_handler(&ar->handlers, keymap);
794
795         /* own keymaps */
796         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
797         WM_event_add_keymap_handler(&ar->handlers, keymap);
798         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
799         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
800
801 }
802
803 static void image_main_area_draw(const bContext *C, ARegion *ar)
804 {
805         /* draw entirely, view changes should be handled here */
806         SpaceImage *sima = CTX_wm_space_image(C);
807         Object *obact = CTX_data_active_object(C);
808         Object *obedit = CTX_data_edit_object(C);
809         Scene *scene = CTX_data_scene(C);
810         View2D *v2d = &ar->v2d;
811         //View2DScrollers *scrollers;
812         float col[3];
813         
814         /* XXX not supported yet, disabling for now */
815         scene->r.scemode &= ~R_COMP_CROP;
816         
817         /* clear and setup matrix */
818         UI_GetThemeColor3fv(TH_BACK, col);
819         glClearColor(col[0], col[1], col[2], 0.0);
820         glClear(GL_COLOR_BUFFER_BIT);
821
822         /* put scene context variable in iuser */
823         sima->iuser.scene = scene;
824
825         /* we set view2d from own zoom and offset each time */
826         image_main_area_set_view2d(sima, ar);
827         
828         /* we draw image in pixelspace */
829         draw_image_main(C, ar);
830
831         /* and uvs in 0.0-1.0 space */
832         UI_view2d_view_ortho(v2d);
833         draw_uvedit_main(sima, ar, scene, obedit, obact);
834
835         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
836
837         /* Grease Pencil too (in addition to UV's) */
838         draw_image_grease_pencil((bContext *)C, 1); 
839
840         /* sample line */
841         draw_image_sample_line(sima);
842
843         UI_view2d_view_restore(C);
844
845         /* draw Grease Pencil - screen space only */
846         draw_image_grease_pencil((bContext *)C, 0);
847
848         /* scrollers? */
849 #if 0
850         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
851         UI_view2d_scrollers_draw(C, v2d, scrollers);
852         UI_view2d_scrollers_free(scrollers);
853 #endif
854 }
855
856 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
857 {
858         /* context changes */
859         switch (wmn->category) {
860                 case NC_SCREEN:
861                         if (wmn->data == ND_GPENCIL)
862                                 ED_region_tag_redraw(ar);
863                         break;
864         }
865 }
866
867 /* *********************** buttons region ************************ */
868
869 /* add handlers, stuff you only do once or on area/region changes */
870 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
871 {
872         wmKeyMap *keymap;
873
874         ED_region_panels_init(wm, ar);
875         
876         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
877         WM_event_add_keymap_handler(&ar->handlers, keymap);
878 }
879
880 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
881 {
882         ED_region_panels(C, ar, 1, NULL, -1);
883 }
884
885 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
886 {
887         /* context changes */
888         switch (wmn->category) {
889                 case NC_SCREEN:
890                         if (wmn->data == ND_GPENCIL)
891                                 ED_region_tag_redraw(ar);
892                         break;
893                 case NC_BRUSH:
894                         if (wmn->action == NA_EDITED)
895                                 ED_region_tag_redraw(ar);
896                         break;
897                 case NC_TEXTURE:
898                 case NC_MATERIAL:
899                         /* sending by texture render job and needed to properly update displaying
900                          * brush texture icon */
901                         ED_region_tag_redraw(ar);
902                         break;
903         }
904 }
905
906 /* *********************** scopes region ************************ */
907
908 /* add handlers, stuff you only do once or on area/region changes */
909 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
910 {
911         wmKeyMap *keymap;
912         
913         ED_region_panels_init(wm, ar);
914         
915         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
916         WM_event_add_keymap_handler(&ar->handlers, keymap);
917 }
918
919 static void image_scope_area_draw(const bContext *C, ARegion *ar)
920 {
921         SpaceImage *sima = CTX_wm_space_image(C);
922         Scene *scene = CTX_data_scene(C);
923         void *lock;
924         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
925         if (ibuf) {
926                 if (!sima->scopes.ok) {
927                         BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
928                 }
929                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT);
930         }
931         ED_space_image_release_buffer(sima, lock);
932         
933         ED_region_panels(C, ar, 1, NULL, -1);
934 }
935
936 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
937 {
938         /* context changes */
939         switch (wmn->category) {
940                 case NC_SCENE:
941                         switch (wmn->data) {
942                                 case ND_MODE:
943                                 case ND_RENDER_RESULT:
944                                 case ND_COMPO_RESULT:
945                                         ED_region_tag_redraw(ar);
946                                         break;
947                         }
948                         break;
949                 case NC_IMAGE:
950                         ED_region_tag_redraw(ar);
951                         break;
952                 case NC_NODE:
953                         ED_region_tag_redraw(ar);
954                         break;
955                         
956         }
957 }
958
959 /************************* header region **************************/
960
961 /* add handlers, stuff you only do once or on area/region changes */
962 static void image_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
963 {
964         ED_region_header_init(ar);
965 }
966
967 static void image_header_area_draw(const bContext *C, ARegion *ar)
968 {
969         ED_region_header(C, ar);
970 }
971
972 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
973 {
974         /* context changes */
975         switch (wmn->category) {
976                 case NC_SCENE:
977                         switch (wmn->data) {
978                                 case ND_MODE:
979                                 case ND_TOOLSETTINGS:
980                                         ED_region_tag_redraw(ar);
981                                         break;
982                         }
983                         break;
984                 case NC_GEOM:
985                         switch (wmn->data) {
986                                 case ND_DATA:
987                                 case ND_SELECT:
988                                         ED_region_tag_redraw(ar);
989                                         break;
990                         }
991                         
992         }
993 }
994
995 /**************************** spacetype *****************************/
996
997 /* only called once, from space/spacetypes.c */
998 void ED_spacetype_image(void)
999 {
1000         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
1001         ARegionType *art;
1002         
1003         st->spaceid = SPACE_IMAGE;
1004         strncpy(st->name, "Image", BKE_ST_MAXNAME);
1005         
1006         st->new = image_new;
1007         st->free = image_free;
1008         st->init = image_init;
1009         st->duplicate = image_duplicate;
1010         st->operatortypes = image_operatortypes;
1011         st->keymap = image_keymap;
1012         st->dropboxes = image_dropboxes;
1013         st->refresh = image_refresh;
1014         st->listener = image_listener;
1015         st->context = image_context;
1016         
1017         /* regions: main window */
1018         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1019         art->regionid = RGN_TYPE_WINDOW;
1020         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1021         art->init = image_main_area_init;
1022         art->draw = image_main_area_draw;
1023         art->listener = image_main_area_listener;
1024
1025         BLI_addhead(&st->regiontypes, art);
1026         
1027         /* regions: listview/buttons */
1028         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1029         art->regionid = RGN_TYPE_UI;
1030         art->prefsizex = 220; // XXX
1031         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1032         art->listener = image_buttons_area_listener;
1033         art->init = image_buttons_area_init;
1034         art->draw = image_buttons_area_draw;
1035         BLI_addhead(&st->regiontypes, art);
1036
1037         image_buttons_register(art);
1038         ED_uvedit_buttons_register(art);
1039         
1040         /* regions: statistics/scope buttons */
1041         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1042         art->regionid = RGN_TYPE_PREVIEW;
1043         art->prefsizex = 220; // XXX
1044         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1045         art->listener = image_scope_area_listener;
1046         art->init = image_scope_area_init;
1047         art->draw = image_scope_area_draw;
1048         BLI_addhead(&st->regiontypes, art);
1049
1050         /* regions: header */
1051         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1052         art->regionid = RGN_TYPE_HEADER;
1053         art->prefsizey = HEADERY;
1054         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1055         art->listener = image_header_area_listener;
1056         art->init = image_header_area_init;
1057         art->draw = image_header_area_draw;
1058         
1059         BLI_addhead(&st->regiontypes, art);
1060         
1061         BKE_spacetype_register(st);
1062 }
1063