first pass at the uv code. uv editor now mostly uses new bmesh structures for uvs.
[blender-staging.git] / source / blender / editors / space_image / space_image.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_image_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_arithb.h"
44 #include "BLI_editVert.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_image.h"
50 #include "BKE_mesh.h"
51 #include "BKE_screen.h"
52 #include "BKE_utildefines.h"
53 #include "BKE_tessmesh.h"
54
55 #include "IMB_imbuf.h"
56 #include "IMB_imbuf_types.h"
57
58 #include "ED_image.h"
59 #include "ED_mesh.h"
60 #include "ED_space_api.h"
61 #include "ED_screen.h"
62 #include "ED_uvedit.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h"
66
67 #include "RNA_access.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "image_intern.h"
77
78 /* ******************** manage regions ********************* */
79
80 ARegion *image_has_buttons_region(ScrArea *sa)
81 {
82         ARegion *ar, *arnew;
83         
84         for(ar= sa->regionbase.first; ar; ar= ar->next)
85                 if(ar->regiontype==RGN_TYPE_UI)
86                         return ar;
87         
88         /* add subdiv level; after header */
89         for(ar= sa->regionbase.first; ar; ar= ar->next)
90                 if(ar->regiontype==RGN_TYPE_HEADER)
91                         break;
92         
93         /* is error! */
94         if(ar==NULL) return NULL;
95         
96         arnew= MEM_callocN(sizeof(ARegion), "buttons for image");
97         
98         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
99         arnew->regiontype= RGN_TYPE_UI;
100         arnew->alignment= RGN_ALIGN_LEFT;
101         
102         arnew->flag = RGN_FLAG_HIDDEN;
103         
104         return arnew;
105 }
106
107 /* ******************** default callbacks for image space ***************** */
108
109 static SpaceLink *image_new(const bContext *C)
110 {
111         ARegion *ar;
112         SpaceImage *simage;
113         
114         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
115         simage->spacetype= SPACE_IMAGE;
116         simage->zoom= 1;
117         
118         simage->iuser.ok= 1;
119         simage->iuser.fie_ima= 2;
120         simage->iuser.frames= 100;
121         
122         /* header */
123         ar= MEM_callocN(sizeof(ARegion), "header for image");
124         
125         BLI_addtail(&simage->regionbase, ar);
126         ar->regiontype= RGN_TYPE_HEADER;
127         ar->alignment= RGN_ALIGN_BOTTOM;
128         
129         /* buttons/list view */
130         ar= MEM_callocN(sizeof(ARegion), "buttons for image");
131         
132         BLI_addtail(&simage->regionbase, ar);
133         ar->regiontype= RGN_TYPE_UI;
134         ar->alignment= RGN_ALIGN_LEFT;
135         ar->flag = RGN_FLAG_HIDDEN;
136         
137         /* main area */
138         ar= MEM_callocN(sizeof(ARegion), "main area for image");
139         
140         BLI_addtail(&simage->regionbase, ar);
141         ar->regiontype= RGN_TYPE_WINDOW;
142         
143         return (SpaceLink *)simage;
144 }
145
146 /* not spacelink itself */
147 static void image_free(SpaceLink *sl)
148 {       
149         SpaceImage *simage= (SpaceImage*) sl;
150         
151         if(simage->cumap)
152                 curvemapping_free(simage->cumap);
153 //      if(simage->gpd)
154 // XXX          free_gpencil_data(simage->gpd);
155         
156 }
157
158
159 /* spacetype; init callback */
160 static void image_init(struct wmWindowManager *wm, ScrArea *sa)
161 {
162
163 }
164
165 static SpaceLink *image_duplicate(SpaceLink *sl)
166 {
167         SpaceImage *simagen= MEM_dupallocN(sl);
168         
169         /* clear or remove stuff from old */
170         if(simagen->cumap)
171                 simagen->cumap= curvemapping_copy(simagen->cumap);
172         
173         return (SpaceLink *)simagen;
174 }
175
176 void image_operatortypes(void)
177 {
178         WM_operatortype_append(IMAGE_OT_view_all);
179         WM_operatortype_append(IMAGE_OT_view_pan);
180         WM_operatortype_append(IMAGE_OT_view_selected);
181         WM_operatortype_append(IMAGE_OT_view_zoom);
182         WM_operatortype_append(IMAGE_OT_view_zoom_in);
183         WM_operatortype_append(IMAGE_OT_view_zoom_out);
184         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
185
186         WM_operatortype_append(IMAGE_OT_new);
187         WM_operatortype_append(IMAGE_OT_open);
188         WM_operatortype_append(IMAGE_OT_replace);
189         WM_operatortype_append(IMAGE_OT_reload);
190         WM_operatortype_append(IMAGE_OT_save);
191         WM_operatortype_append(IMAGE_OT_save_as);
192         WM_operatortype_append(IMAGE_OT_save_sequence);
193         WM_operatortype_append(IMAGE_OT_pack);
194         WM_operatortype_append(IMAGE_OT_unpack);
195
196         WM_operatortype_append(IMAGE_OT_sample);
197         WM_operatortype_append(IMAGE_OT_curves_point_set);
198
199         WM_operatortype_append(IMAGE_OT_record_composite);
200
201         WM_operatortype_append(IMAGE_OT_toolbox);
202         WM_operatortype_append(IMAGE_OT_properties);
203 }
204
205 void image_keymap(struct wmWindowManager *wm)
206 {
207         ListBase *keymap= WM_keymap_listbase(wm, "Image Generic", SPACE_IMAGE, 0);
208         
209         WM_keymap_add_item(keymap, "IMAGE_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
210         WM_keymap_add_item(keymap, "IMAGE_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
211         WM_keymap_add_item(keymap, "IMAGE_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
212         WM_keymap_add_item(keymap, "IMAGE_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
213         WM_keymap_add_item(keymap, "IMAGE_OT_properties", NKEY, KM_PRESS, 0, 0);
214         
215         keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);
216         
217         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
218         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
219         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
220
221         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
222         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
223         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
224         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
225         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
226
227         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
228         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
229         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
230         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
231         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
232         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
233         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
234
235         WM_keymap_add_item(keymap, "PAINT_OT_image_paint", ACTIONMOUSE, KM_PRESS, 0, 0);
236         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", SELECTMOUSE, KM_PRESS, 0, 0);
237         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SELECTMOUSE, KM_PRESS, 0, 0);
238         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
239         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_RADIALCONTROL_STRENGTH);
240
241         WM_keymap_add_item(keymap, "IMAGE_OT_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
242         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "point", 0);
243         RNA_enum_set(WM_keymap_add_item(keymap, "IMAGE_OT_curves_point_set", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "point", 1);
244
245         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
246 }
247
248 static void image_refresh(const bContext *C, ScrArea *sa)
249 {
250         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
251         Object *obedit= CTX_data_edit_object(C);
252         Image *ima;
253
254         ima= ED_space_image(sima);
255
256         /* check if we have to set the image from the editmesh */
257         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
258         else if(obedit && obedit->type == OB_MESH) {
259                 Mesh *me= (Mesh*)obedit->data;
260                 BMEditMesh *em= me->edit_btmesh;
261                 MTexPoly *tf;
262                 
263                 if(em && EDBM_texFaceCheck(em)) {
264                         sima->image= ima= NULL;
265                         
266                         tf = EDBM_get_active_mtexpoly(em, NULL, 1); /* partially selected face is ok */
267                         
268                         if(tf && (tf->mode & TF_TEX)) {
269                                 /* don't need to check for pin here, see above */
270                                 sima->image= ima= tf->tpage;
271                                 
272                                 if(sima->flag & SI_EDITTILE);
273                                 else sima->curtile= tf->tile;
274                         }
275                 }
276         }
277 }
278
279 static void image_listener(ScrArea *sa, wmNotifier *wmn)
280 {
281         /* context changes */
282         switch(wmn->category) {
283                 case NC_SCENE:
284                         switch(wmn->data) {
285                                 case ND_MODE:
286                                 case ND_RENDER_RESULT:
287                                 case ND_COMPO_RESULT:
288                                         ED_area_tag_refresh(sa);
289                                         ED_area_tag_redraw(sa);
290                                         break;
291                         }
292                         break;
293                 case NC_IMAGE:  
294                         ED_area_tag_redraw(sa);
295                         break;
296                 case NC_OBJECT:
297                         switch(wmn->data) {
298                                 case ND_GEOM_SELECT:
299                                 case ND_GEOM_DATA:
300                                         ED_area_tag_redraw(sa);
301                                         break;
302                         }
303         }
304 }
305
306 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
307 {
308         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
309
310         if(CTX_data_dir(member)) {
311                 static const char *dir[] = {"edit_image", NULL};
312                 CTX_data_dir_set(result, dir);
313         }
314         else if(CTX_data_equals(member, "edit_image")) {
315                 CTX_data_id_pointer_set(result, (ID*)ED_space_image(sima));
316                 return 1;
317         }
318
319         return 0;
320 }
321
322 /************************** main region ***************************/
323
324 /* sets up the fields of the View2D from zoom and offset */
325 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar, Scene *scene)
326 {
327         Image *ima= ED_space_image(sima);
328         float x1, y1, w, h;
329         int width, height, winx, winy;
330         
331 #if 0
332         if(image_preview_active(curarea, &width, &height));
333 #endif
334         if(sima->image) {
335                 ImBuf *ibuf= ED_space_image_buffer(sima);
336                 
337                 if(ibuf) {
338                         width= ibuf->x;
339                         height= ibuf->y;
340                 }
341                 else if(sima->image->type==IMA_TYPE_R_RESULT) {
342                         /* not very important, just nice */
343                         width= (scene->r.xsch*scene->r.size)/100;
344                         height= (scene->r.ysch*scene->r.size)/100;
345                 }
346                 else
347                         ED_space_image_size(sima, &width, &height);
348         }
349         else
350                 ED_space_image_size(sima, &width, &height);
351
352         w= width;
353         h= height;
354         
355         if(ima)
356                 h *= ima->aspy/ima->aspx;
357
358         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
359         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
360                 
361         ar->v2d.tot.xmin= 0;
362         ar->v2d.tot.ymin= 0;
363         ar->v2d.tot.xmax= w;
364         ar->v2d.tot.ymax= h;
365         
366         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
367         ar->v2d.mask.xmax= winx;
368         ar->v2d.mask.ymax= winy;
369
370         /* which part of the image space do we see? */
371         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2;
372         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2;
373
374         x1-= sima->zoom*sima->xof;
375         y1-= sima->zoom*sima->yof;
376         
377         /* relative display right */
378         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
379         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
380         
381         /* relative display left */
382         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
383         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
384         
385         /* normalize 0.0..1.0 */
386         ar->v2d.cur.xmin /= w;
387         ar->v2d.cur.xmax /= w;
388         ar->v2d.cur.ymin /= h;
389         ar->v2d.cur.ymax /= h;
390 }
391
392 /* add handlers, stuff you only do once or on area/region changes */
393 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
394 {
395         ListBase *keymap;
396         
397         // image space manages own v2d
398         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
399
400         /* image paint polls for mode */
401         keymap= WM_keymap_listbase(wm, "ImagePaint", SPACE_IMAGE, 0);
402         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
403
404         /* XXX need context here?
405         keymap= WM_keymap_listbase(wm, "UVEdit", 0, 0);
406         WM_event_add_keymap_handler(&ar->handlers, keymap);*/
407         
408         /* own keymaps */
409         keymap= WM_keymap_listbase(wm, "Image Generic", SPACE_IMAGE, 0);
410         WM_event_add_keymap_handler(&ar->handlers, keymap);
411         keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);
412         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
413 }
414
415 static void image_main_area_draw(const bContext *C, ARegion *ar)
416 {
417         /* draw entirely, view changes should be handled here */
418         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
419         Object *obedit= CTX_data_edit_object(C);
420         Scene *scene= CTX_data_scene(C);
421         View2D *v2d= &ar->v2d;
422         //View2DScrollers *scrollers;
423         float col[3];
424         
425         /* clear and setup matrix */
426         UI_GetThemeColor3fv(TH_BACK, col);
427         glClearColor(col[0], col[1], col[2], 0.0);
428         glClear(GL_COLOR_BUFFER_BIT);
429         
430         /* we set view2d from own zoom and offset each time */
431         image_main_area_set_view2d(sima, ar, scene);
432                 
433         /* we draw image in pixelspace */
434         draw_image_main(sima, ar, scene);
435
436         /* and uvs in 0.0-1.0 space */
437         UI_view2d_view_ortho(C, v2d);
438         draw_uvedit_main(sima, ar, scene, obedit);
439         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST);
440         UI_view2d_view_restore(C);
441         
442         /* scrollers? */
443         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
444         UI_view2d_scrollers_draw(C, v2d, scrollers);
445         UI_view2d_scrollers_free(scrollers);*/
446 }
447
448 static void image_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
449 {
450         ListBase *keymap;
451         
452         keymap= WM_keymap_listbase(wm, "UVEdit", 0, 0);
453
454         if(stype==NS_EDITMODE_MESH)
455                 WM_event_add_keymap_handler(&ar->handlers, keymap);
456         else
457                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
458 }
459
460 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
461 {
462         /* context changes */
463         switch(wmn->category) {
464                 case NC_SCENE:
465                         switch(wmn->data) {
466                                 case ND_MODE:
467                                         image_modal_keymaps(wmn->wm, ar, wmn->subtype);
468                                         break;
469                         }
470                         break;
471         }
472 }
473
474 /* *********************** buttons region ************************ */
475
476 /* add handlers, stuff you only do once or on area/region changes */
477 static void image_buttons_area_init(wmWindowManager *wm, ARegion *ar)
478 {
479         ListBase *keymap;
480
481         ED_region_panels_init(wm, ar);
482         
483         keymap= WM_keymap_listbase(wm, "Image Generic", SPACE_IMAGE, 0);
484         WM_event_add_keymap_handler(&ar->handlers, keymap);
485 }
486
487 static void image_buttons_area_draw(const bContext *C, ARegion *ar)
488 {
489         ED_region_panels(C, ar, 1, NULL);
490 }
491
492 static void image_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
493 {
494         /* context changes */
495         switch(wmn->category) {
496                 
497         }
498 }
499
500 /************************* header region **************************/
501
502 /* add handlers, stuff you only do once or on area/region changes */
503 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
504 {
505 #if 0
506         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
507 #else
508         ED_region_header_init(ar);
509 #endif
510 }
511
512 static void image_header_area_draw(const bContext *C, ARegion *ar)
513 {
514         ED_region_header(C, ar);
515 #if 0
516         float col[3];
517         
518         /* clear */
519         if(ED_screen_area_active(C))
520                 UI_GetThemeColor3fv(TH_HEADER, col);
521         else
522                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
523         
524         glClearColor(col[0], col[1], col[2], 0.0);
525         glClear(GL_COLOR_BUFFER_BIT);
526         
527         /* set view2d view matrix for scrolling (without scrollers) */
528         UI_view2d_view_ortho(C, &ar->v2d);
529         
530         image_header_buttons(C, ar);
531         
532         /* restore view matrix? */
533         UI_view2d_view_restore(C);
534 #endif
535 }
536
537 /**************************** spacetype *****************************/
538
539 /* only called once, from space/spacetypes.c */
540 void ED_spacetype_image(void)
541 {
542         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
543         ARegionType *art;
544         
545         st->spaceid= SPACE_IMAGE;
546         
547         st->new= image_new;
548         st->free= image_free;
549         st->init= image_init;
550         st->duplicate= image_duplicate;
551         st->operatortypes= image_operatortypes;
552         st->keymap= image_keymap;
553         st->refresh= image_refresh;
554         st->listener= image_listener;
555         st->context= image_context;
556         
557         /* regions: main window */
558         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
559         art->regionid = RGN_TYPE_WINDOW;
560         art->keymapflag= ED_KEYMAP_FRAMES;
561         art->init= image_main_area_init;
562         art->draw= image_main_area_draw;
563         art->listener= image_main_area_listener;
564         art->keymapflag= 0;
565
566         BLI_addhead(&st->regiontypes, art);
567         
568         /* regions: listview/buttons */
569         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
570         art->regionid = RGN_TYPE_UI;
571         art->minsizex= 220; // XXX
572         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
573         art->listener= image_buttons_area_listener;
574         art->init= image_buttons_area_init;
575         art->draw= image_buttons_area_draw;
576         BLI_addhead(&st->regiontypes, art);
577
578         image_buttons_register(art);
579
580         /* regions: header */
581         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
582         art->regionid = RGN_TYPE_HEADER;
583         art->minsizey= HEADERY;
584         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
585         art->init= image_header_area_init;
586         art->draw= image_header_area_draw;
587         
588         BLI_addhead(&st->regiontypes, art);
589         
590         BKE_spacetype_register(st);
591 }
592
593 /**************************** common state *****************************/
594
595 /* note; image_panel_properties() uses pointer to sima->image directly */
596 Image *ED_space_image(SpaceImage *sima)
597 {
598         return sima->image;
599 }
600
601 /* called to assign images to UV faces */
602 void ED_space_image_set(bContext *C, SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
603 {
604         ED_uvedit_assign_image(scene, obedit, ima, sima->image);
605
606         /* change the space ima after because uvedit_face_visible uses the space ima
607          * to check if the face is displayed in UV-localview */
608         sima->image= ima;
609
610         if(ima == NULL || ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
611                 sima->flag &= ~SI_DRAWTOOL;
612
613         if(sima->image)
614                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
615
616         if(sima->image && sima->image->id.us==0)
617                 sima->image->id.us= 1;
618
619         if(C) {
620                 if(obedit)
621                         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
622
623                 ED_area_tag_redraw(CTX_wm_area(C));
624         }
625 }
626
627 ImBuf *ED_space_image_buffer(SpaceImage *sima)
628 {
629         ImBuf *ibuf;
630
631         if(sima->image) {
632 #if 0
633                 if(sima->image->type==IMA_TYPE_R_RESULT && BIF_show_render_spare())
634                         return BIF_render_spare_imbuf();
635                 else
636 #endif
637                         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
638
639                 if(ibuf && (ibuf->rect || ibuf->rect_float))
640                         return ibuf;
641         }
642
643         return NULL;
644 }
645
646 void ED_image_size(Image *ima, int *width, int *height)
647 {
648         ImBuf *ibuf;
649
650         ibuf= (ima)? BKE_image_get_ibuf(ima, NULL): NULL;
651
652         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
653                 *width= ibuf->x;
654                 *height= ibuf->y;
655         }
656         else {
657                 *width= 256;
658                 *height= 256;
659         }
660 }
661
662 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
663 {
664         ImBuf *ibuf;
665
666         ibuf= ED_space_image_buffer(sima);
667
668         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
669                 *width= ibuf->x;
670                 *height= ibuf->y;
671         }
672         /* I know a bit weak... but preview uses not actual image size */
673         // XXX else if(image_preview_active(sima, width, height));
674         else {
675                 *width= 256;
676                 *height= 256;
677         }
678 }
679
680 void ED_image_aspect(Image *ima, float *aspx, float *aspy)
681 {
682         *aspx= *aspy= 1.0;
683
684         if((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
685            (ima->aspx==0.0 || ima->aspy==0.0))
686                 return;
687
688         /* x is always 1 */
689         *aspy = ima->aspy/ima->aspx;
690 }
691
692 void ED_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
693 {
694         ED_image_aspect(ED_space_image(sima), aspx, aspy);
695 }
696
697 void ED_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
698 {
699         int width, height;
700
701         ED_space_image_size(sima, &width, &height);
702
703         *zoomx= (float)(ar->winrct.xmax - ar->winrct.xmin)/(float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin)*width);
704         *zoomy= (float)(ar->winrct.ymax - ar->winrct.ymin)/(float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin)*height);
705 }
706
707 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
708 {
709         int w, h;
710
711         ED_space_image_aspect(sima, aspx, aspy);
712         ED_space_image_size(sima, &w, &h);
713
714         *aspx *= (float)w/256.0f;
715         *aspy *= (float)h/256.0f;
716 }
717
718 void ED_image_uv_aspect(Image *ima, float *aspx, float *aspy)
719 {
720         int w, h;
721
722         ED_image_aspect(ima, aspx, aspy);
723         ED_image_size(ima, &w, &h);
724
725         *aspx *= (float)w;
726         *aspy *= (float)h;
727 }
728
729 int ED_space_image_show_render(SpaceImage *sima)
730 {
731         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
732 }
733
734 int ED_space_image_show_paint(SpaceImage *sima)
735 {
736         if(ED_space_image_show_render(sima))
737                 return 0;
738
739         return (sima->flag & SI_DRAWTOOL);
740 }
741
742 int ED_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
743 {
744         if(ED_space_image_show_render(sima))
745                 return 0;
746         if(ED_space_image_show_paint(sima))
747                 return 0;
748
749         if(obedit && obedit->type == OB_MESH) {
750                 BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
751
752                 return EDBM_texFaceCheck(em);
753         }
754
755         return 0;
756 }
757
758 int ED_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
759 {
760         if(ED_space_image_show_render(sima))
761                 return 0;
762
763         if(ED_space_image_show_paint(sima))
764                 if(obedit && obedit->type == OB_MESH) {
765                         BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
766
767                         return EDBM_texFaceCheck(em);
768                 }
769
770         return 0;
771 }
772