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