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