2.5: ID datablock button back, previously known as std_libbuttons. The
[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 /* ******************** default callbacks for image space ***************** */
77
78 static SpaceLink *image_new(const bContext *C)
79 {
80         ARegion *ar;
81         SpaceImage *simage;
82         
83         simage= MEM_callocN(sizeof(SpaceImage), "initimage");
84         simage->spacetype= SPACE_IMAGE;
85         simage->zoom= 1;
86         
87         simage->iuser.ok= 1;
88         simage->iuser.fie_ima= 2;
89         simage->iuser.frames= 100;
90         
91         /* header */
92         ar= MEM_callocN(sizeof(ARegion), "header for image");
93         
94         BLI_addtail(&simage->regionbase, ar);
95         ar->regiontype= RGN_TYPE_HEADER;
96         ar->alignment= RGN_ALIGN_BOTTOM;
97         
98         /* main area */
99         ar= MEM_callocN(sizeof(ARegion), "main area for image");
100         
101         BLI_addtail(&simage->regionbase, ar);
102         ar->regiontype= RGN_TYPE_WINDOW;
103         
104         /* channel list region XXX */
105
106         
107         return (SpaceLink *)simage;
108 }
109
110 /* not spacelink itself */
111 static void image_free(SpaceLink *sl)
112 {       
113         SpaceImage *simage= (SpaceImage*) sl;
114         
115         if(simage->cumap)
116                 curvemapping_free(simage->cumap);
117 //      if(simage->gpd)
118 // XXX          free_gpencil_data(simage->gpd);
119         
120 }
121
122
123 /* spacetype; init callback */
124 static void image_init(struct wmWindowManager *wm, ScrArea *sa)
125 {
126
127 }
128
129 static SpaceLink *image_duplicate(SpaceLink *sl)
130 {
131         SpaceImage *simagen= MEM_dupallocN(sl);
132         
133         /* clear or remove stuff from old */
134         if(simagen->cumap)
135                 simagen->cumap= curvemapping_copy(simagen->cumap);
136         
137         return (SpaceLink *)simagen;
138 }
139
140 void image_operatortypes(void)
141 {
142         WM_operatortype_append(IMAGE_OT_view_all);
143         WM_operatortype_append(IMAGE_OT_view_pan);
144         WM_operatortype_append(IMAGE_OT_view_selected);
145         WM_operatortype_append(IMAGE_OT_view_zoom);
146         WM_operatortype_append(IMAGE_OT_view_zoom_in);
147         WM_operatortype_append(IMAGE_OT_view_zoom_out);
148         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
149
150         WM_operatortype_append(IMAGE_OT_toolbox);
151 }
152
153 void image_keymap(struct wmWindowManager *wm)
154 {
155         ListBase *keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);
156         
157         WM_keymap_add_item(keymap, "IMAGE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
158         WM_keymap_add_item(keymap, "IMAGE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
159         WM_keymap_add_item(keymap, "IMAGE_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
160
161         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
162         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
163         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
164         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
165         WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
166
167         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
168         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
169         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
170         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
171         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
172         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
173         RNA_float_set(WM_keymap_add_item(keymap, "IMAGE_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
174
175         WM_keymap_add_item(keymap, "IMAGE_OT_toolbox", SPACEKEY, KM_PRESS, 0, 0);
176 }
177
178 static void image_refresh(const bContext *C, ScrArea *sa)
179 {
180         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
181         Object *obedit= CTX_data_edit_object(C);
182         Image *ima;
183
184         ima= get_space_image(sima);
185
186         /* check if we have to set the image from the editmesh */
187         if(ima && (ima->source==IMA_SRC_VIEWER || sima->pin));
188         else if(obedit && obedit->type == OB_MESH) {
189                 Mesh *me= (Mesh*)obedit->data;
190                 EditMesh *em= me->edit_mesh;
191                 MTFace *tf;
192                 
193                 if(EM_texFaceCheck(em)) {
194                         sima->image= ima= NULL;
195                         
196                         tf = EM_get_active_mtface(em, NULL, NULL, 1); /* partially selected face is ok */
197                         
198                         if(tf && (tf->mode & TF_TEX)) {
199                                 /* don't need to check for pin here, see above */
200                                 sima->image= ima= tf->tpage;
201                                 
202                                 if(sima->flag & SI_EDITTILE);
203                                 else sima->curtile= tf->tile;
204                                 
205                                 if(ima) {
206                                         if(tf->mode & TF_TILES)
207                                                 ima->tpageflag |= IMA_TILES;
208                                         else
209                                                 ima->tpageflag &= ~IMA_TILES;
210                                 }
211                         }
212                 }
213         }
214 }
215
216 static void image_listener(ScrArea *sa, wmNotifier *wmn)
217 {
218         /* context changes */
219         switch(wmn->category) {
220                 case NC_SCENE:
221                         switch(wmn->data) {
222                                 case ND_MODE:
223                                 case ND_RENDER_RESULT:
224                                 case ND_COMPO_RESULT:
225                                         ED_area_tag_refresh(sa);
226                                         ED_area_tag_redraw(sa);
227                                         break;
228                         }
229                         break;
230         }
231 }
232
233 static int image_context(const bContext *C, bContextDataMember member, bContextDataResult *result)
234 {
235         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
236
237         if(member == CTX_DATA_EDIT_IMAGE) {
238                 CTX_data_pointer_set(result, get_space_image(sima));
239                 return 1;
240         }
241         else if(member == CTX_DATA_EDIT_IMAGE_BUFFER) {
242                 CTX_data_pointer_set(result, get_space_image_buffer(sima));
243                 return 1;
244         }
245
246         return 0;
247 }
248
249 /************************** main region ***************************/
250
251 /* sets up the fields of the View2D from zoom and offset */
252 static void image_main_area_set_view2d(SpaceImage *sima, ARegion *ar)
253 {
254         Image *ima= get_space_image(sima);
255         float x1, y1, w, h;
256         int width, height, winx, winy;
257         
258 #if 0
259         if(image_preview_active(curarea, &xim, &yim));
260         else if(sima->image) {
261                 ImBuf *ibuf= imagewindow_get_ibuf(sima);
262                 float xuser_asp, yuser_asp;
263                 
264                 image_pixel_aspect(sima->image, &xuser_asp, &yuser_asp);
265                 if(ibuf) {
266                         xim= ibuf->x * xuser_asp;
267                         yim= ibuf->y * yuser_asp;
268                 }
269                 else if( sima->image->type==IMA_TYPE_R_RESULT ) {
270                         /* not very important, just nice */
271                         xim= (G.scene->r.xsch*G.scene->r.size)/100;
272                         yim= (G.scene->r.ysch*G.scene->r.size)/100;
273                 }
274         }
275 #endif
276
277         get_space_image_size(sima, &width, &height);
278
279         w= width;
280         h= height;
281         
282         if(ima)
283                 h *= ima->aspy/ima->aspx;
284
285         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
286         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
287                 
288         ar->v2d.tot.xmin= 0;
289         ar->v2d.tot.ymin= 0;
290         ar->v2d.tot.xmax= w;
291         ar->v2d.tot.ymax= h;
292         
293         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
294         ar->v2d.mask.xmax= winx;
295         ar->v2d.mask.ymax= winy;
296
297         /* which part of the image space do we see? */
298         /* same calculation as in lrectwrite: area left and down*/
299         x1= ar->winrct.xmin+(winx-sima->zoom*w)/2;
300         y1= ar->winrct.ymin+(winy-sima->zoom*h)/2;
301
302         x1-= sima->zoom*sima->xof;
303         y1-= sima->zoom*sima->yof;
304         
305         /* relative display right */
306         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sima->zoom);
307         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sima->zoom);
308         
309         /* relative display left */
310         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sima->zoom);
311         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sima->zoom);
312         
313         /* normalize 0.0..1.0 */
314         ar->v2d.cur.xmin /= w;
315         ar->v2d.cur.xmax /= w;
316         ar->v2d.cur.ymin /= h;
317         ar->v2d.cur.ymax /= h;
318 }
319
320 /* add handlers, stuff you only do once or on area/region changes */
321 static void image_main_area_init(wmWindowManager *wm, ARegion *ar)
322 {
323         ListBase *keymap;
324         
325         // image space manages own v2d
326         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
327         
328         /* own keymap */
329         keymap= WM_keymap_listbase(wm, "Image", SPACE_IMAGE, 0);
330         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
331 }
332
333 static void image_main_area_draw(const bContext *C, ARegion *ar)
334 {
335         /* draw entirely, view changes should be handled here */
336         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
337         Object *obedit= CTX_data_edit_object(C);
338         Scene *scene= CTX_data_scene(C);
339         View2D *v2d= &ar->v2d;
340         //View2DScrollers *scrollers;
341         float col[3];
342         
343         /* clear and setup matrix */
344         UI_GetThemeColor3fv(TH_BACK, col);
345         glClearColor(col[0], col[1], col[2], 0.0);
346         glClear(GL_COLOR_BUFFER_BIT);
347         
348         /* we set view2d from own zoom and offset each time */
349         image_main_area_set_view2d(sima, ar);
350                 
351         /* we draw image in pixelspace */
352         draw_image_main(sima, ar, scene);
353
354         /* and uvs in 0.0-1.0 space */
355         UI_view2d_view_ortho(C, v2d);
356         draw_uvedit_main(sima, ar, scene, obedit);
357         UI_view2d_view_restore(C);
358         
359         /* scrollers? */
360         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
361         UI_view2d_scrollers_draw(C, v2d, scrollers);
362         UI_view2d_scrollers_free(scrollers);*/
363 }
364
365 static void image_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
366 {
367         ListBase *keymap;
368         
369         keymap= WM_keymap_listbase(wm, "UVEdit", 0, 0);
370         if(stype==NS_EDITMODE_MESH)
371                 WM_event_add_keymap_handler(&ar->handlers, keymap);
372         else
373                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
374 }
375
376 static void image_main_area_listener(ARegion *ar, wmNotifier *wmn)
377 {
378         /* context changes */
379         switch(wmn->category) {
380                 case NC_SCENE:
381                         switch(wmn->data) {
382                                 case ND_MODE:
383                                         image_modal_keymaps(wmn->wm, ar, wmn->subtype);
384                                         break;
385                         }
386                         break;
387                 case NC_OBJECT:
388                         switch(wmn->data) {
389                                 case ND_GEOM_SELECT:
390                                         ED_region_tag_redraw(ar);
391                                         break;
392                         }
393         }
394 }
395
396 /************************* header region **************************/
397
398 /* add handlers, stuff you only do once or on area/region changes */
399 static void image_header_area_init(wmWindowManager *wm, ARegion *ar)
400 {
401         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
402 }
403
404 static void image_header_area_draw(const bContext *C, ARegion *ar)
405 {
406         float col[3];
407         
408         /* clear */
409         if(ED_screen_area_active(C))
410                 UI_GetThemeColor3fv(TH_HEADER, col);
411         else
412                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
413         
414         glClearColor(col[0], col[1], col[2], 0.0);
415         glClear(GL_COLOR_BUFFER_BIT);
416         
417         /* set view2d view matrix for scrolling (without scrollers) */
418         UI_view2d_view_ortho(C, &ar->v2d);
419         
420         image_header_buttons(C, ar);
421         
422         /* restore view matrix? */
423         UI_view2d_view_restore(C);
424 }
425
426 /**************************** spacetype *****************************/
427
428 /* only called once, from space/spacetypes.c */
429 void ED_spacetype_image(void)
430 {
431         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype image");
432         ARegionType *art;
433         
434         st->spaceid= SPACE_IMAGE;
435         
436         st->new= image_new;
437         st->free= image_free;
438         st->init= image_init;
439         st->duplicate= image_duplicate;
440         st->operatortypes= image_operatortypes;
441         st->keymap= image_keymap;
442         st->refresh= image_refresh;
443         st->listener= image_listener;
444         st->context= image_context;
445         
446         /* regions: main window */
447         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
448         art->regionid = RGN_TYPE_WINDOW;
449         art->keymapflag= ED_KEYMAP_FRAMES;
450         art->init= image_main_area_init;
451         art->draw= image_main_area_draw;
452         art->listener= image_main_area_listener;
453         art->keymapflag= 0;
454
455         BLI_addhead(&st->regiontypes, art);
456         
457         /* regions: header */
458         art= MEM_callocN(sizeof(ARegionType), "spacetype image region");
459         art->regionid = RGN_TYPE_HEADER;
460         art->minsizey= HEADERY;
461         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
462         
463         art->init= image_header_area_init;
464         art->draw= image_header_area_draw;
465         
466         BLI_addhead(&st->regiontypes, art);
467         
468         
469         BKE_spacetype_register(st);
470 }
471
472 /**************************** common state *****************************/
473
474 Image *get_space_image(SpaceImage *sima)
475 {
476         return sima->image;
477 }
478
479 /* called to assign images to UV faces */
480 void set_space_image(SpaceImage *sima, Scene *scene, Object *obedit, Image *ima)
481 {
482         ED_uvedit_assign_image(scene, obedit, ima, sima->image);
483
484         /* change the space ima after because uvedit_face_visible uses the space ima
485          * to check if the face is displayed in UV-localview */
486         sima->image= ima;
487
488         if(ima == NULL || ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
489                 sima->flag &= ~SI_DRAWTOOL;
490
491         if(sima->image)
492                 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
493 }
494
495 ImBuf *get_space_image_buffer(SpaceImage *sima)
496 {
497         ImBuf *ibuf;
498
499         if(sima->image) {
500 #if 0
501                 if(sima->image->type==IMA_TYPE_R_RESULT && BIF_show_render_spare())
502                         return BIF_render_spare_imbuf();
503                 else
504 #endif
505                         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
506
507                 if(ibuf && (ibuf->rect || ibuf->rect_float))
508                         return ibuf;
509         }
510
511         return NULL;
512 }
513
514 void get_space_image_size(SpaceImage *sima, int *width, int *height)
515 {
516         ImBuf *ibuf;
517
518         ibuf= get_space_image_buffer(sima);
519
520         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
521                 *width= ibuf->x;
522                 *height= ibuf->y;
523         }
524         /* I know a bit weak... but preview uses not actual image size */
525         // XXX else if(image_preview_active(sima, width, height));
526         else {
527                 *width= 256;
528                 *height= 256;
529         }
530 }
531
532 void get_space_image_aspect(SpaceImage *sima, float *aspx, float *aspy)
533 {
534         Image *ima;
535
536         ima= get_space_image(sima);
537
538         *aspx= *aspy= 1.0;
539
540         if((ima == NULL) || (ima->type == IMA_TYPE_R_RESULT) || (ima->type == IMA_TYPE_COMPOSITE) ||
541            (ima->tpageflag & IMA_TILES) || (ima->aspx==0.0 || ima->aspy==0.0))
542                 return;
543
544         /* x is always 1 */
545         *aspy = ima->aspy/ima->aspx;
546 }
547
548 void get_space_image_zoom(SpaceImage *sima, ARegion *ar, float *zoomx, float *zoomy)
549 {
550         int width, height;
551
552         get_space_image_size(sima, &width, &height);
553
554         *zoomx= (float)(ar->winrct.xmax - ar->winrct.xmin)/(float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin)*width);
555         *zoomy= (float)(ar->winrct.ymax - ar->winrct.ymin)/(float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin)*height);
556 }
557
558 void get_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
559 {
560         int w, h;
561
562         get_space_image_aspect(sima, aspx, aspy);
563         get_space_image_size(sima, &w, &h);
564
565         *aspx *= (float)w;
566         *aspy *= (float)h;
567 }
568
569 int get_space_image_show_render(SpaceImage *sima)
570 {
571         return (sima->image && ELEM(sima->image->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE));
572 }
573
574 int get_space_image_show_paint(SpaceImage *sima)
575 {
576         if(get_space_image_show_render(sima))
577                 return 0;
578
579         return (sima->flag & SI_DRAWTOOL);
580 }
581
582 int get_space_image_show_uvedit(SpaceImage *sima, Object *obedit)
583 {
584         if(get_space_image_show_render(sima))
585                 return 0;
586         if(get_space_image_show_paint(sima))
587                 return 0;
588
589         if(obedit && obedit->type == OB_MESH)
590                 return EM_texFaceCheck(((Mesh*)obedit->data)->edit_mesh);
591
592         return 0;
593 }
594
595 int get_space_image_show_uvshadow(SpaceImage *sima, Object *obedit)
596 {
597         if(get_space_image_show_render(sima))
598                 return 0;
599
600         if(get_space_image_show_paint(sima))
601                 if(obedit && obedit->type == OB_MESH)
602                         return EM_texFaceCheck(((Mesh*)obedit->data)->edit_mesh);
603
604         return 0;
605 }
606
607 /* Exported Functions */
608
609 Image *ED_space_image(SpaceImage *sima)
610 {
611         return get_space_image(sima);
612 }
613
614 void ED_space_image_size(SpaceImage *sima, int *width, int *height)
615 {
616         get_space_image_size(sima, width, height);
617 }
618
619 void ED_space_image_uv_aspect(SpaceImage *sima, float *aspx, float *aspy)
620 {
621         get_space_image_uv_aspect(sima, aspx, aspy);
622 }
623
624