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