Merge branch 'master' into blender28
[blender.git] / source / blender / editors / space_image / space_image.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_image/space_image.c
28  *  \ingroup spimage
29  */
30
31 #include "DNA_gpencil_types.h"
32 #include "DNA_mesh_types.h"
33 #include "DNA_mask_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_image_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_threads.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_editmesh.h"
48 #include "BKE_image.h"
49 #include "BKE_layer.h"
50 #include "BKE_library.h"
51 #include "BKE_scene.h"
52 #include "BKE_screen.h"
53 #include "BKE_material.h"
54
55 #include "DEG_depsgraph.h"
56
57 #include "IMB_imbuf_types.h"
58
59 #include "ED_image.h"
60 #include "ED_mask.h"
61 #include "ED_mesh.h"
62 #include "ED_node.h"
63 #include "ED_render.h"
64 #include "ED_space_api.h"
65 #include "ED_screen.h"
66 #include "ED_uvedit.h"
67 #include "ED_transform.h"
68
69 #include "BIF_gl.h"
70
71 #include "RNA_access.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "UI_resources.h"
77 #include "UI_interface.h"
78 #include "UI_view2d.h"
79
80 #include "image_intern.h"
81
82 /**************************** common state *****************************/
83
84 static void image_scopes_tag_refresh(ScrArea *sa)
85 {
86         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
87         ARegion *ar;
88
89         /* only while histogram is visible */
90         for (ar = sa->regionbase.first; ar; ar = ar->next) {
91                 if (ar->regiontype == RGN_TYPE_TOOLS && ar->flag & RGN_FLAG_HIDDEN)
92                         return;
93         }
94
95         sima->scopes.ok = 0;
96 }
97
98
99 /* ******************** manage regions ********************* */
100
101 ARegion *image_has_buttons_region(ScrArea *sa)
102 {
103         ARegion *ar, *arnew;
104
105         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
106         if (ar) return ar;
107         
108         /* add subdiv level; after header */
109         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
110
111         /* is error! */
112         if (ar == NULL) return NULL;
113         
114         arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
115         
116         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
117         arnew->regiontype = RGN_TYPE_UI;
118         arnew->alignment = RGN_ALIGN_RIGHT;
119         
120         arnew->flag = RGN_FLAG_HIDDEN;
121         
122         return arnew;
123 }
124
125 ARegion *image_has_tools_region(ScrArea *sa)
126 {
127         ARegion *ar, *arnew;
128
129         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
130         if (ar) return ar;
131
132         /* add subdiv level; after buttons */
133         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
134
135         /* is error! */
136         if (ar == NULL) return NULL;
137         
138         arnew = MEM_callocN(sizeof(ARegion), "scopes for image");
139         
140         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
141         arnew->regiontype = RGN_TYPE_TOOLS;
142         arnew->alignment = RGN_ALIGN_LEFT;
143         
144         arnew->flag = RGN_FLAG_HIDDEN;
145
146         image_scopes_tag_refresh(sa);
147         
148         return arnew;
149 }
150
151 /* ******************** default callbacks for image space ***************** */
152
153 static SpaceLink *image_new(const bContext *UNUSED(C))
154 {
155         ARegion *ar;
156         SpaceImage *simage;
157         
158         simage = MEM_callocN(sizeof(SpaceImage), "initimage");
159         simage->spacetype = SPACE_IMAGE;
160         simage->zoom = 1.0f;
161         simage->lock = true;
162         simage->flag = SI_SHOW_GPENCIL | SI_USE_ALPHA;
163
164         simage->iuser.ok = true;
165         simage->iuser.fie_ima = 2;
166         simage->iuser.frames = 100;
167         simage->iuser.flag = IMA_SHOW_STEREO;
168
169         scopes_new(&simage->scopes);
170         simage->sample_line_hist.height = 100;
171
172         /* header */
173         ar = MEM_callocN(sizeof(ARegion), "header for image");
174         
175         BLI_addtail(&simage->regionbase, ar);
176         ar->regiontype = RGN_TYPE_HEADER;
177         ar->alignment = RGN_ALIGN_BOTTOM;
178         
179         /* buttons/list view */
180         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
181         
182         BLI_addtail(&simage->regionbase, ar);
183         ar->regiontype = RGN_TYPE_UI;
184         ar->alignment = RGN_ALIGN_RIGHT;
185         ar->flag = RGN_FLAG_HIDDEN;
186         
187         /* scopes/uv sculpt/paint */
188         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
189         
190         BLI_addtail(&simage->regionbase, ar);
191         ar->regiontype = RGN_TYPE_TOOLS;
192         ar->alignment = RGN_ALIGN_LEFT;
193         ar->flag = RGN_FLAG_HIDDEN;
194
195         /* main area */
196         ar = MEM_callocN(sizeof(ARegion), "main area for image");
197         
198         BLI_addtail(&simage->regionbase, ar);
199         ar->regiontype = RGN_TYPE_WINDOW;
200         
201         return (SpaceLink *)simage;
202 }
203
204 /* not spacelink itself */
205 static void image_free(SpaceLink *sl)
206 {       
207         SpaceImage *simage = (SpaceImage *) sl;
208
209         scopes_free(&simage->scopes);
210 }
211
212
213 /* spacetype; init callback, add handlers */
214 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
215 {
216         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
217
218         /* add drop boxes */
219         WM_event_add_dropbox_handler(&sa->handlers, lb);
220         
221 }
222
223 static SpaceLink *image_duplicate(SpaceLink *sl)
224 {
225         SpaceImage *simagen = MEM_dupallocN(sl);
226         
227         /* clear or remove stuff from old */
228
229         scopes_new(&simagen->scopes);
230
231         return (SpaceLink *)simagen;
232 }
233
234 static void image_operatortypes(void)
235 {
236         WM_operatortype_append(IMAGE_OT_view_all);
237         WM_operatortype_append(IMAGE_OT_view_pan);
238         WM_operatortype_append(IMAGE_OT_view_selected);
239         WM_operatortype_append(IMAGE_OT_view_zoom);
240         WM_operatortype_append(IMAGE_OT_view_zoom_in);
241         WM_operatortype_append(IMAGE_OT_view_zoom_out);
242         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
243         WM_operatortype_append(IMAGE_OT_view_zoom_border);
244 #ifdef WITH_INPUT_NDOF
245         WM_operatortype_append(IMAGE_OT_view_ndof);
246 #endif
247
248         WM_operatortype_append(IMAGE_OT_new);
249         WM_operatortype_append(IMAGE_OT_open);
250         WM_operatortype_append(IMAGE_OT_match_movie_length);
251         WM_operatortype_append(IMAGE_OT_replace);
252         WM_operatortype_append(IMAGE_OT_reload);
253         WM_operatortype_append(IMAGE_OT_save);
254         WM_operatortype_append(IMAGE_OT_save_as);
255         WM_operatortype_append(IMAGE_OT_save_sequence);
256         WM_operatortype_append(IMAGE_OT_pack);
257         WM_operatortype_append(IMAGE_OT_unpack);
258         
259         WM_operatortype_append(IMAGE_OT_invert);
260
261         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
262
263         WM_operatortype_append(IMAGE_OT_sample);
264         WM_operatortype_append(IMAGE_OT_sample_line);
265         WM_operatortype_append(IMAGE_OT_curves_point_set);
266
267         WM_operatortype_append(IMAGE_OT_properties);
268         WM_operatortype_append(IMAGE_OT_toolshelf);
269
270         WM_operatortype_append(IMAGE_OT_change_frame);
271
272         WM_operatortype_append(IMAGE_OT_read_renderlayers);
273         WM_operatortype_append(IMAGE_OT_render_border);
274         WM_operatortype_append(IMAGE_OT_clear_render_border);
275 }
276
277 static void image_keymap(struct wmKeyConfig *keyconf)
278 {
279         wmKeyMap *keymap = WM_keymap_find(keyconf, "Image Generic", SPACE_IMAGE, 0);
280         wmKeyMapItem *kmi;
281         int i;
282         
283         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
284         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
285         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
286         WM_keymap_add_item(keymap, "IMAGE_OT_read_renderlayers", RKEY, KM_PRESS, KM_CTRL, 0);
287         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
288         WM_keymap_add_item(keymap, "IMAGE_OT_save_as", F3KEY, KM_PRESS, 0, 0);
289         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
290         WM_keymap_add_item(keymap, "IMAGE_OT_toolshelf", TKEY, KM_PRESS, 0, 0);
291
292         WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, 0, 0);
293         RNA_boolean_set(WM_keymap_add_item(keymap, "IMAGE_OT_cycle_render_slot", JKEY, KM_PRESS, KM_ALT, 0)->ptr, "reverse", true);
294         
295         keymap = WM_keymap_find(keyconf, "Image", SPACE_IMAGE, 0);
296         
297         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
298
299         kmi = WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, KM_SHIFT, 0);
300         RNA_boolean_set(kmi->ptr, "fit_view", true);
301
302         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
303         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
304         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
305         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MOUSEPAN, 0, 0, 0);
306
307 #ifdef WITH_INPUT_NDOF
308         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0); // or view selected?
309         WM_keymap_add_item(keymap, "IMAGE_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
310 #endif
311
312         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
313         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
314         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
315         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
316         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
317         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
318         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MOUSEPAN, 0, KM_CTRL, 0);
319         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_border", BKEY, KM_PRESS, KM_SHIFT, 0);
320
321         /* ctrl now works as well, shift + numpad works as arrow keys on Windows */
322         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 8.0f);
323         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 4.0f);
324         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_CTRL, 0)->ptr, "ratio", 2.0f);
325         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
326         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
327         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
328
329         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
330         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
331         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
332         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
333
334         WM_keymap_add_item(keymap, "IMAGE_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
335
336         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
337         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
338         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
339
340         /* toggle editmode is handy to have while UV unwrapping */
341         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
342         RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
343         RNA_boolean_set(kmi->ptr, "toggle", true);
344
345         /* fast switch to render slots */
346         for (i = 0; i < MIN2(IMA_MAX_RENDER_SLOT, 9); i++) {
347                 kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY + i, KM_PRESS, 0, 0);
348                 RNA_string_set(kmi->ptr, "data_path", "space_data.image.render_slots.active_index");
349                 RNA_int_set(kmi->ptr, "value", i);
350         }
351
352         /* pivot */
353         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, 0, 0);
354         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
355         RNA_string_set(kmi->ptr, "value", "CENTER");
356
357         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, KM_CTRL, 0);
358         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
359         RNA_string_set(kmi->ptr, "value", "MEDIAN");
360
361         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, 0, 0);
362         RNA_string_set(kmi->ptr, "data_path", "space_data.pivot_point");
363         RNA_string_set(kmi->ptr, "value", "CURSOR");
364
365         /* render border */
366         WM_keymap_add_item(keymap, "IMAGE_OT_render_border", BKEY, KM_PRESS, KM_CTRL, 0);
367         WM_keymap_add_item(keymap, "IMAGE_OT_clear_render_border", BKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
368 }
369
370 /* dropboxes */
371 static int image_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
372 {
373         if (drag->type == WM_DRAG_PATH)
374                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE, ICON_FILE_BLANK)) /* rule might not work? */
375                         return 1;
376         return 0;
377 }
378
379 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
380 {
381         /* copy drag path to properties */
382         RNA_string_set(drop->ptr, "filepath", drag->path);
383 }
384
385 /* area+region dropbox definition */
386 static void image_dropboxes(void)
387 {
388         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
389         
390         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
391 }
392
393 /**
394  * \note take care not to get into feedback loop here,
395  *       calling composite job causes viewer to refresh.
396  */
397 static void image_refresh(const bContext *C, ScrArea *sa)
398 {
399         Scene *scene = CTX_data_scene(C);
400         SpaceImage *sima = sa->spacedata.first;
401         Object *obedit = CTX_data_edit_object(C);
402         Image *ima;
403
404         ima = ED_space_image(sima);
405
406         BKE_image_user_check_frame_calc(&sima->iuser, scene->r.cfra, 0);
407         
408         /* check if we have to set the image from the editmesh */
409         if (ima && (ima->source == IMA_SRC_VIEWER && sima->mode == SI_MODE_MASK)) {
410                 if (scene->nodetree) {
411                         Mask *mask = ED_space_image_get_mask(sima);
412                         if (mask) {
413                                 ED_node_composite_job(C, scene->nodetree, scene);
414                         }
415                 }
416         }
417         else if (ima && (ima->source == IMA_SRC_VIEWER || sima->pin)) {
418                 /* pass */
419         }
420         else if (obedit && obedit->type == OB_MESH) {
421                 Mesh *me = (Mesh *)obedit->data;
422                 struct BMEditMesh *em = me->edit_btmesh;
423                 bool sloppy = true; /* partially selected face is ok */
424                 bool selected = !(scene->toolsettings->uv_flag & UV_SYNC_SELECTION); /* only selected active face? */
425
426                 if (BKE_scene_use_new_shading_nodes(scene)) {
427                         /* new shading system does not alter image */
428                 }
429                 else {
430                         /* old shading system, we set texface */
431                         if (em && EDBM_uv_check(em)) {
432                                 BMFace *efa = BM_mesh_active_face_get(em->bm, sloppy, selected);
433
434                                 if (efa) {
435                                         /* don't need to check for pin here, see above */
436                                         Image *image = BKE_object_material_edit_image_get(obedit, efa->mat_nr);
437
438                                         sima->image = image;
439                                 }
440                         }
441                 }
442         }
443 }
444
445 static void image_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn, const Scene *scene)
446 {
447         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
448         
449         /* context changes */
450         switch (wmn->category) {
451                 case NC_WINDOW:
452                         /* notifier comes from editing color space */
453                         image_scopes_tag_refresh(sa);
454                         ED_area_tag_redraw(sa);
455                         break;
456                 case NC_SCENE:
457                         switch (wmn->data) {
458                                 case ND_FRAME:
459                                         image_scopes_tag_refresh(sa);
460                                         ED_area_tag_refresh(sa);
461                                         ED_area_tag_redraw(sa);
462                                         break;
463                                 case ND_MODE:
464                                         if (wmn->subtype == NS_EDITMODE_MESH)
465                                                 ED_area_tag_refresh(sa);
466                                         ED_area_tag_redraw(sa);
467                                         break;
468                                 case ND_RENDER_RESULT:
469                                 case ND_RENDER_OPTIONS:
470                                 case ND_COMPO_RESULT:
471                                         if (ED_space_image_show_render(sima))
472                                                 image_scopes_tag_refresh(sa);
473                                         ED_area_tag_redraw(sa);
474                                         break;
475                         }
476                         break;
477                 case NC_IMAGE:
478                         if (wmn->reference == sima->image || !wmn->reference) {
479                                 if (wmn->action != NA_PAINTING) {
480                                         image_scopes_tag_refresh(sa);
481                                         ED_area_tag_refresh(sa);
482                                         ED_area_tag_redraw(sa);
483                                 }
484                         }
485                         break;
486                 case NC_SPACE:
487                         if (wmn->data == ND_SPACE_IMAGE) {
488                                 image_scopes_tag_refresh(sa);
489                                 ED_area_tag_redraw(sa);
490                         }
491                         break;
492                 case NC_MASK:
493                 {
494                         // Scene *scene = wmn->window->screen->scene;
495                         /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima) but we cant get the scene */
496                         if (sima->mode == SI_MODE_MASK) {
497                                 switch (wmn->data) {
498                                         case ND_SELECT:
499                                                 ED_area_tag_redraw(sa);
500                                                 break;
501                                         case ND_DATA:
502                                         case ND_DRAW:
503                                                 /* causes node-recalc */
504                                                 ED_area_tag_redraw(sa);
505                                                 ED_area_tag_refresh(sa);
506                                                 break;
507                                 }
508                                 switch (wmn->action) {
509                                         case NA_SELECTED:
510                                                 ED_area_tag_redraw(sa);
511                                                 break;
512                                         case NA_EDITED:
513                                                 /* causes node-recalc */
514                                                 ED_area_tag_redraw(sa);
515                                                 ED_area_tag_refresh(sa);
516                                                 break;
517                                 }
518                         }
519                         break;
520                 }
521                 case NC_GEOM:
522                 {
523                         switch (wmn->data) {
524                                 case ND_DATA:
525                                 case ND_SELECT:
526                                         image_scopes_tag_refresh(sa);
527                                         ED_area_tag_refresh(sa);
528                                         ED_area_tag_redraw(sa);
529                                         break;
530                         }
531                         break;
532                 }
533                 case NC_OBJECT:
534                 {
535                         switch (wmn->data) {
536                                 case ND_TRANSFORM:
537                                 case ND_MODIFIER:
538                                 {
539                                         SceneLayer *sl = BKE_scene_layer_context_active_PLACEHOLDER(scene);
540                                         Object *ob = OBACT_NEW(sl);
541                                         if (ob && (ob == wmn->reference) && (ob->mode & OB_MODE_EDIT)) {
542                                                 if (sima->lock && (sima->flag & SI_DRAWSHADOW)) {
543                                                         ED_area_tag_refresh(sa);
544                                                         ED_area_tag_redraw(sa);
545                                                 }
546                                         }
547                                         break;
548                                 }
549                         }
550
551                         break;
552                 }
553                 case NC_ID:
554                 {
555                         if (wmn->action == NA_RENAME) {
556                                 ED_area_tag_redraw(sa);
557                         }
558                         break;
559                 }
560                 case NC_WM:
561                         if (wmn->data == ND_UNDO) {
562                                 ED_area_tag_redraw(sa);
563                                 ED_area_tag_refresh(sa);
564                         }
565                         break;
566         }
567 }
568
569 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
570
571 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
572 {
573         SpaceImage *sima = CTX_wm_space_image(C);
574
575         if (CTX_data_dir(member)) {
576                 CTX_data_dir_set(result, image_context_dir);
577         }
578         else if (CTX_data_equals(member, "edit_image")) {
579                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
580                 return 1;
581         }
582         else if (CTX_data_equals(member, "edit_mask")) {
583                 Mask *mask = ED_space_image_get_mask(sima);
584                 if (mask) {
585                         CTX_data_id_pointer_set(result, &mask->id);
586                 }
587                 return true;
588         }
589         return 0;
590 }
591
592 static void IMAGE_WGT_manipulator2d(wmManipulatorGroupType *wgt)
593 {
594         wgt->name = "UV Transform Manipulator";
595         wgt->idname = "IMAGE_WGT_manipulator2d";
596
597         wgt->flag |= WM_MANIPULATORGROUPTYPE_PERSISTENT;
598
599         wgt->poll = ED_widgetgroup_manipulator2d_poll;
600         wgt->setup = ED_widgetgroup_manipulator2d_setup;
601         wgt->refresh = ED_widgetgroup_manipulator2d_refresh;
602         wgt->draw_prepare = ED_widgetgroup_manipulator2d_draw_prepare;
603 }
604
605 static void image_widgets(void)
606 {
607         wmManipulatorMapType *mmap_type = WM_manipulatormaptype_ensure(
608                 &(const struct wmManipulatorMapType_Params){SPACE_IMAGE, RGN_TYPE_WINDOW});
609
610         WM_manipulatorgrouptype_append_and_link(mmap_type, IMAGE_WGT_manipulator2d);
611 }
612
613 /************************** main region ***************************/
614
615 /* sets up the fields of the View2D from zoom and offset */
616 static void image_main_region_set_view2d(SpaceImage *sima, ARegion *ar)
617 {
618         Image *ima = ED_space_image(sima);
619         float x1, y1, w, h;
620         int width, height, winx, winy;
621         
622 #if 0
623         if (image_preview_active(curarea, &width, &height)) {}
624         else
625 #endif
626         ED_space_image_get_size(sima, &width, &height);
627
628         w = width;
629         h = height;
630         
631         if (ima)
632                 h *= ima->aspy / ima->aspx;
633
634         winx = BLI_rcti_size_x(&ar->winrct) + 1;
635         winy = BLI_rcti_size_y(&ar->winrct) + 1;
636                 
637         ar->v2d.tot.xmin = 0;
638         ar->v2d.tot.ymin = 0;
639         ar->v2d.tot.xmax = w;
640         ar->v2d.tot.ymax = h;
641         
642         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
643         ar->v2d.mask.xmax = winx;
644         ar->v2d.mask.ymax = winy;
645
646         /* which part of the image space do we see? */
647         x1 = ar->winrct.xmin + (winx - sima->zoom * w) / 2.0f;
648         y1 = ar->winrct.ymin + (winy - sima->zoom * h) / 2.0f;
649
650         x1 -= sima->zoom * sima->xof;
651         y1 -= sima->zoom * sima->yof;
652         
653         /* relative display right */
654         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
655         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
656         
657         /* relative display left */
658         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
659         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
660         
661         /* normalize 0.0..1.0 */
662         ar->v2d.cur.xmin /= w;
663         ar->v2d.cur.xmax /= w;
664         ar->v2d.cur.ymin /= h;
665         ar->v2d.cur.ymax /= h;
666 }
667
668 /* add handlers, stuff you only do once or on area/region changes */
669 static void image_main_region_init(wmWindowManager *wm, ARegion *ar)
670 {
671         wmKeyMap *keymap;
672         
673         // image space manages own v2d
674         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
675
676         /* manipulators */
677         if (ar->manipulator_map == NULL) {
678                 const struct wmManipulatorMapType_Params wmap_params = {
679                         .spaceid = SPACE_IMAGE,
680                         .regionid = RGN_TYPE_WINDOW,
681                 };
682                 ar->manipulator_map = WM_manipulatormap_new_from_type(&wmap_params);
683         }
684         WM_manipulatormap_add_handlers(ar, ar->manipulator_map);
685
686         /* mask polls mode */
687         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
688         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
689
690         /* image paint polls for mode */
691         keymap = WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
692         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
693
694         keymap = WM_keymap_find(wm->defaultconf, "Paint Curve", 0, 0);
695         WM_event_add_keymap_handler(&ar->handlers, keymap);
696
697         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
698         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
699
700         keymap = WM_keymap_find(wm->defaultconf, "UV Editor", 0, 0);
701         WM_event_add_keymap_handler(&ar->handlers, keymap);
702         
703         keymap = WM_keymap_find(wm->defaultconf, "UV Sculpt", 0, 0);
704         WM_event_add_keymap_handler(&ar->handlers, keymap);
705
706         /* own keymaps */
707         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
708         WM_event_add_keymap_handler(&ar->handlers, keymap);
709         keymap = WM_keymap_find(wm->defaultconf, "Image", SPACE_IMAGE, 0);
710         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
711 }
712
713 static void image_main_region_draw(const bContext *C, ARegion *ar)
714 {
715         /* draw entirely, view changes should be handled here */
716         SpaceImage *sima = CTX_wm_space_image(C);
717         Object *obact = CTX_data_active_object(C);
718         Object *obedit = CTX_data_edit_object(C);
719         Depsgraph *depsgraph = CTX_data_depsgraph(C);
720         Mask *mask = NULL;
721         bool curve = false;
722         Scene *scene = CTX_data_scene(C);
723         SceneLayer *sl = CTX_data_scene_layer(C);
724         View2D *v2d = &ar->v2d;
725         //View2DScrollers *scrollers;
726         float col[3];
727
728         /* XXX not supported yet, disabling for now */
729         scene->r.scemode &= ~R_COMP_CROP;
730         
731         /* clear and setup matrix */
732         UI_GetThemeColor3fv(TH_BACK, col);
733         glClearColor(col[0], col[1], col[2], 0.0);
734         glClear(GL_COLOR_BUFFER_BIT);
735
736         /* put scene context variable in iuser */
737         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT) {
738                 /* for render result, try to use the currently rendering scene */
739                 Scene *render_scene = ED_render_job_get_current_scene(C);
740                 if (render_scene)
741                         sima->iuser.scene = render_scene;
742                 else
743                         sima->iuser.scene = scene;
744         }
745         else
746                 sima->iuser.scene = scene;
747
748         /* we set view2d from own zoom and offset each time */
749         image_main_region_set_view2d(sima, ar);
750
751         /* we draw image in pixelspace */
752         draw_image_main(C, ar);
753
754         /* and uvs in 0.0-1.0 space */
755         UI_view2d_view_ortho(v2d);
756
757         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
758
759         ED_uvedit_draw_main(sima, ar, scene, sl, obedit, obact, depsgraph);
760
761         /* check for mask (delay draw) */
762         if (ED_space_image_show_uvedit(sima, obedit)) {
763                 /* pass */
764         }
765         else if (sima->mode == SI_MODE_MASK) {
766                 mask = ED_space_image_get_mask(sima);
767         }
768         else if (ED_space_image_paint_curve(C)) {
769                 curve = true;
770         }
771
772         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
773
774         if (sima->flag & SI_SHOW_GPENCIL) {
775                 /* Grease Pencil too (in addition to UV's) */
776                 draw_image_grease_pencil((bContext *)C, true);
777         }
778
779         /* sample line */
780         draw_image_sample_line(sima);
781
782         UI_view2d_view_restore(C);
783
784         if (sima->flag & SI_SHOW_GPENCIL) {
785                 /* draw Grease Pencil - screen space only */
786                 draw_image_grease_pencil((bContext *)C, false);
787         }
788
789         if (mask) {
790                 Image *image = ED_space_image(sima);
791                 int width, height, show_viewer;
792                 float aspx, aspy;
793
794                 show_viewer = (image && image->source == IMA_SRC_VIEWER);
795
796                 if (show_viewer) {
797                         /* ED_space_image_get* will acquire image buffer which requires
798                          * lock here by the same reason why lock is needed in draw_image_main
799                          */
800                         BLI_lock_thread(LOCK_DRAW_IMAGE);
801                 }
802
803                 ED_space_image_get_size(sima, &width, &height);
804                 ED_space_image_get_aspect(sima, &aspx, &aspy);
805
806                 if (show_viewer)
807                         BLI_unlock_thread(LOCK_DRAW_IMAGE);
808
809                 ED_mask_draw_region(mask, ar,
810                                     sima->mask_info.draw_flag,
811                                     sima->mask_info.draw_type,
812                                     sima->mask_info.overlay_mode,
813                                     width, height,
814                                     aspx, aspy,
815                                     true, false,
816                                     NULL, C);
817
818                 UI_view2d_view_ortho(v2d);
819                 ED_image_draw_cursor(ar, sima->cursor);
820                 UI_view2d_view_restore(C);
821         }
822         else if (curve) {
823                 UI_view2d_view_ortho(v2d);
824                 ED_image_draw_cursor(ar, sima->cursor);
825                 UI_view2d_view_restore(C);
826         }
827
828         WM_manipulatormap_draw(ar->manipulator_map, C, WM_MANIPULATORMAP_DRAWSTEP_2D);
829
830         draw_image_cache(C, ar);
831
832         /* scrollers? */
833 #if 0
834         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
835         UI_view2d_scrollers_draw(C, v2d, scrollers);
836         UI_view2d_scrollers_free(scrollers);
837 #endif
838 }
839
840 static void image_main_region_listener(
841         bScreen *UNUSED(sc), ScrArea *sa, ARegion *ar,
842         wmNotifier *wmn, const Scene *UNUSED(scene))
843 {
844         /* context changes */
845         switch (wmn->category) {
846                 case NC_GEOM:
847                         if (ELEM(wmn->data, ND_DATA, ND_SELECT))
848                                 WM_manipulatormap_tag_refresh(ar->manipulator_map);
849                         break;
850                 case NC_GPENCIL:
851                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
852                                 ED_region_tag_redraw(ar);
853                         else if (wmn->data & ND_GPENCIL_EDITMODE)
854                                 ED_region_tag_redraw(ar);
855                         break;
856                 case NC_IMAGE:
857                         if (wmn->action == NA_PAINTING)
858                                 ED_region_tag_redraw(ar);
859                         WM_manipulatormap_tag_refresh(ar->manipulator_map);
860                         break;
861                 case NC_MATERIAL:
862                         if (wmn->data == ND_SHADING_LINKS) {
863                                 SpaceImage *sima = sa->spacedata.first;
864
865                                 if (sima->iuser.scene && (sima->iuser.scene->toolsettings->uv_flag & UV_SHOW_SAME_IMAGE))
866                                         ED_region_tag_redraw(ar);
867                         }
868                         break;
869                 case NC_SCREEN:
870                         if (ELEM(wmn->data, ND_LAYER)) {
871                                 ED_region_tag_redraw(ar);
872                         }
873                         break;
874         }
875 }
876
877 /* *********************** buttons region ************************ */
878
879 /* add handlers, stuff you only do once or on area/region changes */
880 static void image_buttons_region_init(wmWindowManager *wm, ARegion *ar)
881 {
882         wmKeyMap *keymap;
883
884         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
885         ED_region_panels_init(wm, ar);
886         
887         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
888         WM_event_add_keymap_handler(&ar->handlers, keymap);
889 }
890
891 static void image_buttons_region_draw(const bContext *C, ARegion *ar)
892 {
893         ED_region_panels(C, ar, NULL, -1, true);
894 }
895
896 static void image_buttons_region_listener(
897         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
898         wmNotifier *wmn, const Scene *UNUSED(scene))
899 {
900         /* context changes */
901         switch (wmn->category) {
902                 case NC_TEXTURE:
903                 case NC_MATERIAL:
904                         /* sending by texture render job and needed to properly update displaying
905                          * brush texture icon */
906                         ED_region_tag_redraw(ar);
907                         break;
908                 case NC_SCENE:
909                         switch (wmn->data) {
910                                 case ND_MODE:
911                                 case ND_RENDER_RESULT:
912                                 case ND_COMPO_RESULT:
913                                         ED_region_tag_redraw(ar);
914                                         break;
915                         }
916                         break;
917                 case NC_IMAGE:
918                         if (wmn->action != NA_PAINTING)
919                                 ED_region_tag_redraw(ar);
920                         break;
921                 case NC_NODE:
922                         ED_region_tag_redraw(ar);
923                         break;
924                 case NC_GPENCIL:
925                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
926                                 ED_region_tag_redraw(ar);
927                         break;
928         }
929 }
930
931 /* *********************** scopes region ************************ */
932
933 /* add handlers, stuff you only do once or on area/region changes */
934 static void image_tools_region_init(wmWindowManager *wm, ARegion *ar)
935 {
936         wmKeyMap *keymap;
937         
938         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
939         ED_region_panels_init(wm, ar);
940         
941         keymap = WM_keymap_find(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
942         WM_event_add_keymap_handler(&ar->handlers, keymap);
943 }
944
945 static void image_tools_region_draw(const bContext *C, ARegion *ar)
946 {
947         SpaceImage *sima = CTX_wm_space_image(C);
948         Scene *scene = CTX_data_scene(C);
949         void *lock;
950         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
951         /* XXX performance regression if name of scopes category changes! */
952         PanelCategoryStack *category = UI_panel_category_active_find(ar, "Scopes");
953
954         /* only update scopes if scope category is active */
955         if (category) {
956                 if (ibuf) {
957                         if (!sima->scopes.ok) {
958                                 BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, &scene->view_settings, &scene->display_settings);
959                         }
960                         if (sima->image->flag & IMA_VIEW_AS_RENDER)
961                                 ED_space_image_scopes_update(C, sima, ibuf, true);
962                         else
963                                 ED_space_image_scopes_update(C, sima, ibuf, false);
964                 }
965         }
966         ED_space_image_release_buffer(sima, ibuf, lock);
967         
968         ED_region_panels(C, ar, NULL, -1, true);
969 }
970
971 static void image_tools_region_listener(
972         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
973         wmNotifier *wmn, const Scene *UNUSED(scene))
974 {
975         /* context changes */
976         switch (wmn->category) {
977                 case NC_GPENCIL:
978                         if (wmn->data == ND_DATA || ELEM(wmn->action, NA_EDITED, NA_SELECTED))
979                                 ED_region_tag_redraw(ar);
980                         break;
981                 case NC_BRUSH:
982                         /* NA_SELECTED is used on brush changes */
983                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
984                                 ED_region_tag_redraw(ar);
985                         break;
986                 case NC_SCENE:
987                         switch (wmn->data) {
988                                 case ND_MODE:
989                                 case ND_RENDER_RESULT:
990                                 case ND_COMPO_RESULT:
991                                         ED_region_tag_redraw(ar);
992                                         break;
993                         }
994                         break;
995                 case NC_IMAGE:
996                         if (wmn->action != NA_PAINTING)
997                                 ED_region_tag_redraw(ar);
998                         break;
999                 case NC_NODE:
1000                         ED_region_tag_redraw(ar);
1001                         break;
1002                         
1003         }
1004 }
1005
1006 /************************* header region **************************/
1007
1008 /* add handlers, stuff you only do once or on area/region changes */
1009 static void image_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1010 {
1011         ED_region_header_init(ar);
1012 }
1013
1014 static void image_header_region_draw(const bContext *C, ARegion *ar)
1015 {
1016         ED_region_header(C, ar);
1017 }
1018
1019 static void image_header_region_listener(
1020         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
1021         wmNotifier *wmn, const Scene *UNUSED(scene))
1022 {
1023         /* context changes */
1024         switch (wmn->category) {
1025                 case NC_SCENE:
1026                         switch (wmn->data) {
1027                                 case ND_MODE:
1028                                 case ND_TOOLSETTINGS:
1029                                         ED_region_tag_redraw(ar);
1030                                         break;
1031                         }
1032                         break;
1033                 case NC_GEOM:
1034                         switch (wmn->data) {
1035                                 case ND_DATA:
1036                                 case ND_SELECT:
1037                                         ED_region_tag_redraw(ar);
1038                                         break;
1039                         }
1040                         break;
1041         }
1042 }
1043
1044 static void image_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
1045 {
1046         SpaceImage *simg = (SpaceImage *)slink;
1047
1048         if (!ELEM(GS(old_id->name), ID_IM, ID_GD, ID_MSK)) {
1049                 return;
1050         }
1051
1052         if ((ID *)simg->image == old_id) {
1053                 simg->image = (Image *)new_id;
1054                 id_us_ensure_real(new_id);
1055         }
1056
1057         if ((ID *)simg->gpd == old_id) {
1058                 simg->gpd = (bGPdata *)new_id;
1059                 id_us_min(old_id);
1060                 id_us_plus(new_id);
1061         }
1062
1063         if ((ID *)simg->mask_info.mask == old_id) {
1064                 simg->mask_info.mask = (Mask *)new_id;
1065                 id_us_ensure_real(new_id);
1066         }
1067 }
1068
1069 /**************************** spacetype *****************************/
1070
1071 /* only called once, from space/spacetypes.c */
1072 void ED_spacetype_image(void)
1073 {
1074         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
1075         ARegionType *art;
1076         
1077         st->spaceid = SPACE_IMAGE;
1078         strncpy(st->name, "Image", BKE_ST_MAXNAME);
1079         
1080         st->new = image_new;
1081         st->free = image_free;
1082         st->init = image_init;
1083         st->duplicate = image_duplicate;
1084         st->operatortypes = image_operatortypes;
1085         st->keymap = image_keymap;
1086         st->dropboxes = image_dropboxes;
1087         st->refresh = image_refresh;
1088         st->listener = image_listener;
1089         st->context = image_context;
1090         st->manipulators = image_widgets;
1091         st->id_remap = image_id_remap;
1092
1093         /* regions: main window */
1094         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1095         art->regionid = RGN_TYPE_WINDOW;
1096         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1097         art->init = image_main_region_init;
1098         art->draw = image_main_region_draw;
1099         art->listener = image_main_region_listener;
1100
1101         BLI_addhead(&st->regiontypes, art);
1102         
1103         /* regions: listview/buttons */
1104         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1105         art->regionid = RGN_TYPE_UI;
1106         art->prefsizex = 220; // XXX
1107         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1108         art->listener = image_buttons_region_listener;
1109         art->init = image_buttons_region_init;
1110         art->draw = image_buttons_region_draw;
1111         BLI_addhead(&st->regiontypes, art);
1112
1113         ED_uvedit_buttons_register(art);
1114         image_buttons_register(art);
1115
1116         /* regions: statistics/scope buttons */
1117         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1118         art->regionid = RGN_TYPE_TOOLS;
1119         art->prefsizex = 220; // XXX
1120         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1121         art->listener = image_tools_region_listener;
1122         art->init = image_tools_region_init;
1123         art->draw = image_tools_region_draw;
1124         BLI_addhead(&st->regiontypes, art);
1125
1126         /* regions: header */
1127         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1128         art->regionid = RGN_TYPE_HEADER;
1129         art->prefsizey = HEADERY;
1130         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1131         art->listener = image_header_region_listener;
1132         art->init = image_header_region_init;
1133         art->draw = image_header_region_draw;
1134         
1135         BLI_addhead(&st->regiontypes, art);
1136         
1137         BKE_spacetype_register(st);
1138 }
1139