[#24171] Tab key no longer responds from UV / Image Editor
[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/256.0f;
230         *aspy *= (float)h/256.0f;
231 }
232
233 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
234 {
235         int w, h;
236         
237         ED_image_aspect(ima, aspx, aspy);
238         ED_image_size(ima, &w, &h);
239         
240         *aspx *= (float)w;
241         *aspy *= (float)h;
242 }
243
244 int ED_space_image_show_render(SpaceImage *sima)
245 {
246         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
247 }
248
249 int ED_space_image_show_paint(SpaceImage *sima)
250 {
251         if(ED_space_image_show_render(sima))
252                 return 0;
253         
254         return (sima->flag & SI_DRAWTOOL);
255 }
256
257 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
258 {
259         if(ED_space_image_show_render(sima))
260                 return 0;
261         if(ED_space_image_show_paint(sima))
262                 return 0;
263         
264         if(obedit && obedit->type == OB_MESH) {
265                 EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
266                 int ret;
267                 
268                 ret = EM_texFaceCheck(em);
269                 
270                 BKE_mesh_end_editmesh(obedit->data, em);
271                 return ret;
272         }
273         
274         return 0;
275 }
276
277 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
278 {
279         if(ED_space_image_show_render(sima))
280                 return 0;
281         
282         if(ED_space_image_show_paint(sima))
283                 if(obedit && obedit->type == OB_MESH) {
284                         EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
285                         int ret;
286                         
287                         ret = EM_texFaceCheck(em);
288                         
289                         BKE_mesh_end_editmesh(obedit->data, em);
290                         return ret;
291                 }
292         
293         return 0;
294 }
295
296
297 static void image_scopes_tag_refresh(ScrArea *sa)
298 {
299         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
300         ARegion *ar;
301
302         /* only while histogram is visible */
303         for (ar=sa->regionbase.first; ar; ar=ar->next) {
304                 if (ar->regiontype == RGN_TYPE_PREVIEW && ar->flag & RGN_FLAG_HIDDEN)
305                         return;
306         }
307
308         sima->scopes.ok=0;
309 }
310
311
312 /* ******************** manage regions ********************* */
313
314 ARegion *image_has_buttons_region(ScrArea *sa)
315 {
316         ARegion *ar, *arnew;
317         
318         for(ar= sa->regionbase.first; ar; ar= ar->next)
319                 if(ar->regiontype==RGN_TYPE_UI)
320                         return ar;
321         
322         /* add subdiv level; after header */
323         for(ar= sa->regionbase.first; ar; ar= ar->next)
324                 if(ar->regiontype==RGN_TYPE_HEADER)
325                         break;
326         
327         /* is error! */
328         if(ar==NULL) return NULL;
329         
330         arnew= MEM_callocN(sizeof(ARegion), "buttons for image");
331         
332         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
333         arnew->regiontype= RGN_TYPE_UI;
334         arnew->alignment= RGN_ALIGN_LEFT;
335         
336         arnew->flag = RGN_FLAG_HIDDEN;
337         
338         return arnew;
339 }
340
341 ARegion *image_has_scope_region(ScrArea *sa)
342 {
343         ARegion *ar, *arnew;
344         
345         for(ar= sa->regionbase.first; ar; ar= ar->next)
346                 if(ar->regiontype==RGN_TYPE_PREVIEW)
347                         return ar;
348         
349         /* add subdiv level; after buttons */
350         for(ar= sa->regionbase.first; ar; ar= ar->next)
351                 if(ar->regiontype==RGN_TYPE_UI)
352                         break;
353         
354         /* is error! */
355         if(ar==NULL) return NULL;
356         
357         arnew= MEM_callocN(sizeof(ARegion), "scopes for image");
358         
359         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
360         arnew->regiontype= RGN_TYPE_PREVIEW;
361         arnew->alignment= RGN_ALIGN_RIGHT;
362         
363         arnew->flag = RGN_FLAG_HIDDEN;
364
365         image_scopes_tag_refresh(sa);
366         
367         return arnew;
368 }
369
370 /* ******************** default callbacks for image space ***************** */
371
372 static SpaceLink *image_new(const bContext *C)
373 {
374         ARegion *ar;
375         SpaceImage *simage;
376         
377         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
378         simage->spacetype= SPACE_IMAGE;
379         simage->zoom= 1;
380         simage->lock= 1;
381         
382         simage->iuser.ok= 1;
383         simage->iuser.fie_ima= 2;
384         simage->iuser.frames= 100;
385         
386         scopes_new(&simage->scopes);
387
388         /* header */
389         ar= MEM_callocN(sizeof(ARegion), "header for image");
390         
391         BLI_addtail(&simage->regionbase, ar);
392         ar->regiontype= RGN_TYPE_HEADER;
393         ar->alignment= RGN_ALIGN_BOTTOM;
394         
395         /* buttons/list view */
396         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
397         
398         BLI_addtail(&simage->regionbase, ar);
399         ar->regiontype= RGN_TYPE_UI;
400         ar->alignment= RGN_ALIGN_LEFT;
401         ar->flag = RGN_FLAG_HIDDEN;
402         
403         /* scopes */
404         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
405         
406         BLI_addtail(&simage->regionbase, ar);
407         ar->regiontype= RGN_TYPE_PREVIEW;
408         ar->alignment= RGN_ALIGN_RIGHT;
409         ar->flag = RGN_FLAG_HIDDEN;
410
411         /* main area */
412         ar= MEM_callocN(sizeof(ARegion), "main area for image");
413         
414         BLI_addtail(&simage->regionbase, ar);
415         ar->regiontype= RGN_TYPE_WINDOW;
416         
417         return (SpaceLink *)simage;
418 }
419
420 /* not spacelink itself */
421 static void image_free(SpaceLink *sl)
422 {       
423         SpaceImage *simage= (SpaceImage*) sl;
424         
425         if(simage->cumap)
426                 curvemapping_free(simage->cumap);
427         scopes_free(&simage->scopes);
428 }
429
430
431 /* spacetype; init callback, add handlers */
432 static void image_init(struct wmWindowManager *wm, ScrArea *sa)
433 {
434         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
435
436         /* add drop boxes */
437         WM_event_add_dropbox_handler(&sa->handlers, lb);
438         
439 }
440
441 static SpaceLink *image_duplicate(SpaceLink *sl)
442 {
443         SpaceImage *simagen= MEM_dupallocN(sl);
444         
445         /* clear or remove stuff from old */
446         if(simagen->cumap)
447                 simagen->cumap= curvemapping_copy(simagen->cumap);
448         
449         return (SpaceLink *)simagen;
450 }
451
452 void image_operatortypes(void)
453 {
454         WM_operatortype_append(IMAGE_OT_view_all);
455         WM_operatortype_append(IMAGE_OT_view_pan);
456         WM_operatortype_append(IMAGE_OT_view_selected);
457         WM_operatortype_append(IMAGE_OT_view_zoom);
458         WM_operatortype_append(IMAGE_OT_view_zoom_in);
459         WM_operatortype_append(IMAGE_OT_view_zoom_out);
460         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
461
462         WM_operatortype_append(IMAGE_OT_new);
463         WM_operatortype_append(IMAGE_OT_open);
464         WM_operatortype_append(IMAGE_OT_replace);
465         WM_operatortype_append(IMAGE_OT_reload);
466         WM_operatortype_append(IMAGE_OT_save);
467         WM_operatortype_append(IMAGE_OT_save_as);
468         WM_operatortype_append(IMAGE_OT_save_sequence);
469         WM_operatortype_append(IMAGE_OT_pack);
470         WM_operatortype_append(IMAGE_OT_unpack);
471
472         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
473
474         WM_operatortype_append(IMAGE_OT_sample);
475         WM_operatortype_append(IMAGE_OT_sample_line);
476         WM_operatortype_append(IMAGE_OT_curves_point_set);
477
478         WM_operatortype_append(IMAGE_OT_record_composite);
479
480         WM_operatortype_append(IMAGE_OT_toolbox);
481         WM_operatortype_append(IMAGE_OT_properties);
482         WM_operatortype_append(IMAGE_OT_scopes);
483 }
484
485 void image_keymap(struct wmKeyConfig *keyconf)
486 {
487         wmKeyMap *keymap= WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
488         wmKeyMapItem *kmi;
489         
490         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
491         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
492         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
493         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
494         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
495         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
496         WM_keymap_add_item(keymap, "IMAGE_OT_scopes", PKEY, KM_PRESS, 0, 0);
497
498         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
499         
500         keymap= WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
501         
502         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
503         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
504         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
505         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
506         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
507
508         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
509         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
510         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
511         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
512         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
513         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
514
515         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
516         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
517         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
518         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
519         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
520         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
521         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
522
523         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
524         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
525         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
526
527         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
528
529         /* toggle editmode is handy to have while UV unwrapping */
530         kmi= WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
531         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
532         RNA_boolean_set(kmi->ptr, "toggle", 1);
533 }
534
535 /* dropboxes */
536 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
537 {
538         if(drag->type==WM_DRAG_PATH)
539                 if(ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK))      /* rule might not work? */
540                         return 1;
541         return 0;
542 }
543
544 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
545 {
546         /* copy drag path to properties */
547         RNA_string_set(drop->ptr, "filepath", drag->path);
548 }
549
550 /* area+region dropbox definition */
551 static void image_dropboxes(void)
552 {
553         ListBase *lb= WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
554         
555         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
556 }
557
558
559
560 static void image_refresh(const bContext *C, ScrArea *sa)
561 {
562         SpaceImage *sima= CTX_wm_space_image(C);
563         Object *obedit= CTX_data_edit_object(C);
564         Image *ima;
565
566         ima= ED_space_image(sima);
567
568         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
569                 BKE_image_user_calc_frame(&sima->iuser, CTX_data_scene(C)->r.cfra, 0);
570         
571         /* check if we have to set the image from the editmesh */
572         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
573         else if(obedit && obedit->type == OB_MESH) {
574                 Mesh *me= (Mesh*)obedit->data;
575                 EditMesh *em= BKE_mesh_get_editmesh(me);
576                 MTFace *tf;
577                 
578                 if(em && EM_texFaceCheck(em)) {
579                         sima->image= ima= NULL;
580                         
581                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
582                         
583                         if(tf && (tf->mode & TF_TEX)) {
584                                 /* don't need to check for pin here, see above */
585                                 sima->image= ima= tf->tpage;
586                                 
587                                 if(sima->flag & SI_EDITTILE);
588                                 else sima->curtile= tf->tile;
589                         }
590                 }
591
592                 BKE_mesh_end_editmesh(obedit->data, em);
593         }
594 }
595
596 static void image_listener(ScrArea *sa, wmNotifier *wmn)
597 {
598         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
599         
600         /* context changes */
601         switch(wmn->category) {
602                 case NC_SCENE:
603                         switch(wmn->data) {
604                                 case ND_FRAME:
605                                         image_scopes_tag_refresh(sa);
606                                         ED_area_tag_refresh(sa);
607                                         ED_area_tag_redraw(sa);                                 
608                                         break;
609                                 case ND_MODE:
610                                 case ND_RENDER_RESULT:
611                                 case ND_COMPO_RESULT:
612                                         if (ED_space_image_show_render(sima))
613                                                 image_scopes_tag_refresh(sa);
614                                         ED_area_tag_refresh(sa);
615                                         ED_area_tag_redraw(sa);                                 
616                                         break;
617                         }
618                         break;
619                 case NC_IMAGE:
620                         if (wmn->reference == sima->image || !wmn->reference) {
621                                 image_scopes_tag_refresh(sa);
622                                 ED_area_tag_refresh(sa);
623                                 ED_area_tag_redraw(sa);
624                         }
625                         break;
626                 case NC_SPACE:  
627                         if(wmn->data == ND_SPACE_IMAGE) {
628                                 image_scopes_tag_refresh(sa);
629                                 ED_area_tag_redraw(sa);
630                         }
631                         break;
632                 case NC_GEOM:
633                         switch(wmn->data) {
634                                 case ND_DATA:
635                                 case ND_SELECT:
636                                         ED_area_tag_refresh(sa);
637                                         ED_area_tag_redraw(sa);
638                                         break;
639                         }
640                 case NC_OBJECT:
641                         switch(wmn->data) {
642                                 case ND_TRANSFORM:
643                                         if(sima->lock && (sima->flag & SI_DRAWSHADOW)) {
644                                                 ED_area_tag_refresh(sa);
645                                                 ED_area_tag_redraw(sa);
646                                         }
647                                         break;
648                         }
649         }
650 }
651
652 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
653 {
654         SpaceImage *sima= CTX_wm_space_image(C);
655
656         if(CTX_data_dir(member)) {
657                 static const char *dir[] = {"edit_image", NULL};
658                 CTX_data_dir_set(result, dir);
659         }
660         else if(CTX_data_equals(member, "edit_image")) {
661                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
662                 return 1;
663         }
664
665         return 0;
666 }
667
668 /************************** main region ***************************/
669
670 /* sets up the fields of the View2D from zoom and offset */
671 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
672 {
673         Image *ima= ED_space_image(sima);
674         float x1, y1, w, h;
675         int width, height, winx, winy;
676         
677 #if 0
678         if(image_preview_active(curarea, &width, &height));
679         else
680 #endif
681         ED_space_image_size(sima, &width, &height);
682
683         w= width;
684         h= height;
685         
686         if(ima)
687                 h *= ima->aspy/ima->aspx;
688
689         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
690         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
691                 
692         ar->v2d.tot.xmin= 0;
693         ar->v2d.tot.ymin= 0;
694         ar->v2d.tot.xmax= w;
695         ar->v2d.tot.ymax= h;
696         
697         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
698         ar->v2d.mask.xmax= winx;
699         ar->v2d.mask.ymax= winy;
700
701         /* which part of the image space do we see? */
702         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2.0f;
703         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2.0f;
704
705         x1-= sima->zoom*sima->xof;
706         y1-= sima->zoom*sima->yof;
707         
708         /* relative display right */
709         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
710         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
711         
712         /* relative display left */
713         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
714         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
715         
716         /* normalize 0.0..1.0 */
717         ar->v2d.cur.xmin /= w;
718         ar->v2d.cur.xmax /= w;
719         ar->v2d.cur.ymin /= h;
720         ar->v2d.cur.ymax /= h;
721 }
722
723 /* add handlers, stuff you only do once or on area/region changes */
724 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
725 {
726         wmKeyMap *keymap;
727         
728         // image space manages own v2d
729         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
730
731         /* image paint polls for mode */
732         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
733         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
734
735         keymap= WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
736         WM_event_add_keymap_handler(&ar->handlers, keymap);
737         
738         /* own keymaps */
739         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
740         WM_event_add_keymap_handler(&ar->handlers, keymap);
741         keymap= WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
742         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
743
744 }
745
746 static void image_main_area_draw(const bContext *C, ARegion *ar)
747 {
748         /* draw entirely, view changes should be handled here */
749         SpaceImage *sima= CTX_wm_space_image(C);
750         Object *obedit= CTX_data_edit_object(C);
751         Scene *scene= CTX_data_scene(C);
752         View2D *v2d= &ar->v2d;
753         //View2DScrollers *scrollers;
754         float col[3];
755         
756         /* clear and setup matrix */
757         UI_GetThemeColor3fv(TH_BACK, col);
758         glClearColor(col[0], col[1], col[2], 0.0);
759         glClear(GL_COLOR_BUFFER_BIT);
760
761         /* put scene context variable in iuser */
762         sima->iuser.scene= scene;
763
764         /* we set view2d from own zoom and offset each time */
765         image_main_area_set_view2d(sima, ar);
766         
767         /* we draw image in pixelspace */
768         draw_image_main(sima, ar, scene);
769
770         /* and uvs in 0.0-1.0 space */
771         UI_view2d_view_ortho(C, v2d);
772         draw_uvedit_main(sima, ar, scene, obedit);
773
774         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
775                 
776         /* Grease Pencil too (in addition to UV's) */
777         draw_image_grease_pencil((bContext *)C, 1); 
778
779         UI_view2d_view_restore(C);
780
781         /* draw Grease Pencil - screen space only */
782         draw_image_grease_pencil((bContext *)C, 0);
783         
784         /* scrollers? */
785         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
786         UI_view2d_scrollers_draw(C, v2d, scrollers);
787         UI_view2d_scrollers_free(scrollers);*/
788 }
789
790 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
791 {
792         /* context changes */
793         switch(wmn->category) {
794                 case NC_SCREEN:
795                         if (wmn->data==ND_GPENCIL)
796                                 ED_region_tag_redraw(ar);
797                 break;
798         }
799 }
800
801 /* *********************** buttons region ************************ */
802
803 /* add handlers, stuff you only do once or on area/region changes */
804 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
805 {
806         wmKeyMap *keymap;
807
808         ED_region_panels_init(wm, ar);
809         
810         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
811         WM_event_add_keymap_handler(&ar->handlers, keymap);
812 }
813
814 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
815 {
816         ED_region_panels(C, ar, 1, NULL, -1);
817 }
818
819 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
820 {
821         /* context changes */
822         switch(wmn->category) {
823                 case NC_SCREEN:
824                         if (wmn->data==ND_GPENCIL)
825                                 ED_region_tag_redraw(ar);
826                         break;
827                 case NC_BRUSH:
828                         if(wmn->action==NA_EDITED)
829                                 ED_region_tag_redraw(ar);
830                         break;
831         }
832 }
833
834 /* *********************** scopes region ************************ */
835
836 /* add handlers, stuff you only do once or on area/region changes */
837 static void image_scope_area_init(wmWindowManager *wm, ARegion *ar)
838 {
839         wmKeyMap *keymap;
840         
841         ED_region_panels_init(wm, ar);
842         
843         keymap= WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
844         WM_event_add_keymap_handler(&ar->handlers, keymap);
845 }
846
847 static void image_scope_area_draw(const bContext *C, ARegion *ar)
848 {
849         SpaceImage *sima= CTX_wm_space_image(C);
850         Scene *scene= CTX_data_scene(C);
851         void *lock;
852         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
853         if(ibuf) {
854                 scopes_update(&sima->scopes, ibuf, scene->r.color_mgt_flag & R_COLOR_MANAGEMENT );
855         }
856         ED_space_image_release_buffer(sima, lock);
857         
858         ED_region_panels(C, ar, 1, NULL, -1);
859 }
860
861 static void image_scope_area_listener(ARegion *ar, wmNotifier *wmn)
862 {
863         /* context changes */
864         switch(wmn->category) {
865                 case NC_SCENE:
866                         switch(wmn->data) {
867                                 case ND_MODE:
868                                 case ND_RENDER_RESULT:
869                                 case ND_COMPO_RESULT:
870                                         ED_region_tag_redraw(ar);
871                                         break;
872                         }
873                         break;
874                 case NC_IMAGE:
875                         ED_region_tag_redraw(ar);
876                         break;
877                 case NC_NODE:
878                         ED_region_tag_redraw(ar);
879                         break;
880                         
881         }
882 }
883
884 /************************* header region **************************/
885
886 /* add handlers, stuff you only do once or on area/region changes */
887 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
888 {
889         ED_region_header_init(ar);
890 }
891
892 static void image_header_area_draw(const bContext *C, ARegion *ar)
893 {
894         ED_region_header(C, ar);
895 }
896
897 static void image_header_area_listener(ARegion *ar, wmNotifier *wmn)
898 {
899         /* context changes */
900         switch(wmn->category) {
901                 case NC_SCENE:
902                         switch(wmn->data) {
903                                 case ND_MODE:
904                                 case ND_TOOLSETTINGS:
905                                         ED_region_tag_redraw(ar);
906                                         break;
907                         }
908                         break;
909                 case NC_GEOM:
910                         switch(wmn->data) {
911                                 case ND_DATA:
912                                 case ND_SELECT:
913                                         ED_region_tag_redraw(ar);
914                                         break;
915                         }
916                         
917         }
918 }
919
920 /**************************** spacetype *****************************/
921
922 /* only called once, from space/spacetypes.c */
923 void ED_spacetype_image(void)
924 {
925         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
926         ARegionType *art;
927         
928         st->spaceid= SPACE_IMAGE;
929         strncpy(st->name, "Image", BKE_ST_MAXNAME);
930         
931         st->new= image_new;
932         st->free= image_free;
933         st->init= image_init;
934         st->duplicate= image_duplicate;
935         st->operatortypes= image_operatortypes;
936         st->keymap= image_keymap;
937         st->dropboxes= image_dropboxes;
938         st->refresh= image_refresh;
939         st->listener= image_listener;
940         st->context= image_context;
941         
942         /* regions: main window */
943         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
944         art->regionid = RGN_TYPE_WINDOW;
945         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
946         art->init= image_main_area_init;
947         art->draw= image_main_area_draw;
948         art->listener= image_main_area_listener;
949
950         BLI_addhead(&st->regiontypes, art);
951         
952         /* regions: listview/buttons */
953         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
954         art->regionid = RGN_TYPE_UI;
955         art->prefsizex= 220; // XXX
956         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
957         art->listener= image_buttons_area_listener;
958         art->init= image_buttons_area_init;
959         art->draw= image_buttons_area_draw;
960         BLI_addhead(&st->regiontypes, art);
961
962         image_buttons_register(art);
963         
964         /* regions: statistics/scope buttons */
965         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
966         art->regionid = RGN_TYPE_PREVIEW;
967         art->prefsizex= 220; // XXX
968         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
969         art->listener= image_scope_area_listener;
970         art->init= image_scope_area_init;
971         art->draw= image_scope_area_draw;
972         BLI_addhead(&st->regiontypes, art);
973
974         /* regions: header */
975         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
976         art->regionid = RGN_TYPE_HEADER;
977         art->prefsizey= HEADERY;
978         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
979         art->listener= image_header_area_listener;
980         art->init= image_header_area_init;
981         art->draw= image_header_area_draw;
982         
983         BLI_addhead(&st->regiontypes, art);
984         
985         BKE_spacetype_register(st);
986 }
987