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