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