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