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