svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / editors / space_image / image_header.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_object_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_customdata_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_editVert.h"
44
45 #include "BKE_context.h"
46 #include "BKE_customdata.h"
47 #include "BKE_image.h"
48 #include "BKE_mesh.h"
49 #include "BKE_screen.h"
50 #include "BKE_utildefines.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "ED_image.h"
56 #include "ED_mesh.h"
57 #include "ED_screen.h"
58 #include "ED_types.h"
59 #include "ED_util.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h"
66 #include "BIF_transform.h"
67
68 #include "UI_interface.h"
69 #include "UI_resources.h"
70 #include "UI_view2d.h"
71
72 #include "RNA_access.h"
73
74 #include "RE_pipeline.h"
75
76 #include "image_intern.h"
77
78 /* ************************ header area region *********************** */
79
80 #define B_NOP                           -1
81 #define B_REDR                          1
82 #define B_SIMAGEPAINTTOOL       4
83 #define B_SIMA_USE_ALPHA        5
84 #define B_SIMA_SHOW_ALPHA       6
85 #define B_SIMA_SHOW_ZBUF        7
86 #define B_SIMA_RECORD           8
87 #define B_SIMA_PLAY                     9
88
89 static void image_view_viewnavmenu(bContext *C, uiLayout *layout, void *arg_unused)
90 {
91         int a;
92         
93         uiItemO(layout, NULL, 0, "IMAGE_OT_view_zoom_in");
94         uiItemO(layout, NULL, 0, "IMAGE_OT_view_zoom_out");
95
96         uiItemS(layout);
97
98         for(a=0; a<7; a++) {
99                 const int ratios[7][2] = {{1, 8}, {1, 4}, {1, 2}, {1, 1}, {2, 1}, {4, 1}, {8, 1}};
100                 char namestr[128];
101
102                 sprintf(namestr, "Zoom %d:%d", ratios[a][0], ratios[a][1]);
103                 uiItemFloatO(layout, namestr, 0, "IMAGE_OT_view_zoom_ratio", "ratio", (float)ratios[a][0]/(float)ratios[a][1]);
104         }
105 }
106
107 #if 0
108 static void do_viewmenu(bContext *C, void *arg, int event)
109 {
110         add_blockhandler(curarea, IMAGE_HANDLER_VIEW_PROPERTIES, UI_PNL_UNSTOW);
111         add_blockhandler(curarea, IMAGE_HANDLER_PROPERTIES, UI_PNL_UNSTOW);
112         add_blockhandler(curarea, IMAGE_HANDLER_PAINT, UI_PNL_UNSTOW);
113         add_blockhandler(curarea, IMAGE_HANDLER_CURVES, UI_PNL_UNSTOW);
114
115         toggle_blockhandler(curarea, IMAGE_HANDLER_PREVIEW, 0);
116         scrarea_queue_winredraw(curarea);
117
118         add_blockhandler(curarea, IMAGE_HANDLER_GAME_PROPERTIES, UI_PNL_UNSTOW);
119         add_blockhandler(curarea, IMAGE_HANDLER_GREASEPENCIL, UI_PNL_UNSTOW);
120
121         allqueue(REDRAWIMAGE, 0);
122         allqueue(REDRAWVIEW3D, 0);
123 }
124 #endif
125
126 static void image_viewmenu(bContext *C, uiLayout *layout, void *arg_unused)
127 {
128         bScreen *sc= CTX_wm_screen(C);
129         ScrArea *sa= CTX_wm_area(C);
130         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
131         PointerRNA spaceptr, uvptr;
132         int show_paint, show_render, show_uvedit;
133
134         /* retrieve state */
135         RNA_pointer_create(&sc->id, &RNA_SpaceImageEditor, sima, &spaceptr);
136         RNA_pointer_create(&sc->id, &RNA_SpaceUVEditor, sima, &uvptr);
137
138         show_render= ED_space_image_show_render(sima);
139         show_paint= ED_space_image_show_paint(sima);
140         show_uvedit= ED_space_image_show_uvedit(sima, CTX_data_edit_object(C));
141         
142         /* create menu */
143         uiItemO(layout, NULL, ICON_MENU_PANEL, "IMAGE_OT_properties");
144         //if(show_render) uiItemO(layout, NULL, ICON_MENU_PANEL, "IMAGE_OT_toggle_compositing_preview_panel"); // Compositing Preview...|Shift P
145
146         uiItemS(layout);
147
148         uiItemR(layout, NULL, 0, &spaceptr, "update_automatically", 0, 0, 0);
149         // XXX if(show_uvedit) uiItemR(layout, NULL, 0, &uvptr, "local_view", 0, 0, 0); // "UV Local View", Numpad /
150
151         uiItemS(layout);
152
153         uiItemMenuF(layout, "View Navigation", 0, image_view_viewnavmenu);
154         if(show_uvedit) uiItemO(layout, NULL, 0, "IMAGE_OT_view_selected");
155         uiItemO(layout, NULL, 0, "IMAGE_OT_view_all");
156
157         if(sa->full) uiItemO(layout, NULL, 0, "SCREEN_OT_screen_full_area"); // "Tile Window", Ctrl UpArrow
158         else uiItemO(layout, NULL, 0, "SCREEN_OT_screen_full_area"); // "Maximize Window", Ctr DownArrow
159 }
160
161 static void image_selectmenu(bContext *C, uiLayout *layout, void *arg_unused)
162 {
163         uiItemO(layout, NULL, 0, "UV_OT_select_border");
164         uiItemBooleanO(layout, "Border Select Pinned", 0, "UV_OT_select_border", "pinned", 1); // Border Select Pinned|Shift B
165
166         uiItemS(layout);
167         
168         uiItemO(layout, NULL, 0, "UV_OT_select_all_toggle");
169         uiItemO(layout, NULL, 0, "UV_OT_select_invert");
170         uiItemO(layout, NULL, 0, "UV_OT_unlink_selection");
171         
172         uiItemS(layout);
173
174         uiItemO(layout, NULL, 0, "UV_OT_select_pinned");
175         uiItemO(layout, NULL, 0, "UV_OT_select_linked");
176 }
177
178 #if 0
179 static void do_image_imagemenu(void *arg, int event)
180 {
181         /* events >=20 are registered bpython scripts */
182 #ifndef DISABLE_PYTHON
183         if (event >= 20) BPY_menu_do_python(PYMENU_IMAGE, event - 20);
184 #endif  
185 }
186 #endif
187
188 static void image_imagemenu(bContext *C, uiLayout *layout, void *arg_unused)
189 {
190         bScreen *sc= CTX_wm_screen(C);
191         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
192         PointerRNA spaceptr, imaptr;
193         Image *ima;
194         ImBuf *ibuf;
195         int show_render;
196         
197         /* retrieve state */
198         ima= ED_space_image(sima);
199         ibuf= ED_space_image_buffer(sima);
200
201         show_render= ED_space_image_show_render(sima);
202
203         RNA_pointer_create(&sc->id, &RNA_SpaceImageEditor, sima, &spaceptr);
204
205         /* create menu */
206         uiItemO(layout, NULL, 0, "IMAGE_OT_new"); // New...
207         uiItemO(layout, NULL, 0, "IMAGE_OT_open"); // Open...
208
209         if(ima) {
210                 if(!show_render) {
211                         uiItemO(layout, NULL, 0, "IMAGE_OT_replace"); // Replace...
212                         uiItemO(layout, NULL, 0, "IMAGE_OT_reload"); // Reload...
213                 }
214                 uiItemO(layout, NULL, 0, "IMAGE_OT_save"); // Save
215                 uiItemO(layout, NULL, 0, "IMAGE_OT_save_as"); // Save As...
216                 if(ima->source == IMA_SRC_SEQUENCE)
217                         uiItemO(layout, NULL, 0, "IMAGE_OT_save_sequence"); // Save Changed Sequence Images
218
219                 if(!show_render) {
220                         uiItemS(layout);
221
222                         if(ima->packedfile) uiItemO(layout, NULL, 0, "IMAGE_OT_unpack"); // Unpack Image...
223                         else uiItemO(layout, NULL, 0, "IMAGE_OT_pack"); // Pack Image
224
225                         /* only for dirty && specific image types : XXX poll? */
226                         if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
227                                 if(ELEM(ima->source, IMA_SRC_FILE, IMA_SRC_GENERATED) && ima->type != IMA_TYPE_MULTILAYER)
228                                         uiItemBooleanO(layout, "Pack As PNG", 0, "IMAGE_OT_pack", "as_png", 1); // Pack Image As PNG
229
230                         uiItemS(layout);
231
232                         uiItemR(layout, NULL, 0, &spaceptr, "image_painting", 0, 0, 0);
233                         
234                         /* move to realtime properties panel */
235                         RNA_id_pointer_create(&ima->id, &imaptr);
236                         uiItemMenuEnumR(layout, NULL, 0, &imaptr, "mapping");
237                 }
238         }
239
240 #if 0
241 #ifndef DISABLE_PYTHON
242         {
243                 BPyMenu *pym;
244                 int i = 0;
245
246                 /* note that we acount for the N previous entries with i+20: */
247                 for (pym = BPyMenuTable[PYMENU_IMAGE]; pym; pym = pym->next, i++) {
248
249                         uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
250                                          NULL, 0.0, 0.0, 1, i+20, 
251                                          pym->tooltip?pym->tooltip:pym->filename);
252                 }
253         }
254 #endif
255 #endif
256 }
257
258 static void image_uvs_showhidemenu(bContext *C, uiLayout *layout, void *arg_unused)
259 {
260         uiItemO(layout, NULL, 0, "UV_OT_reveal");
261         uiItemO(layout, NULL, 0, "UV_OT_hide");
262         uiItemBooleanO(layout, "Hide Unselected", 0, "UV_OT_hide", "unselected", 1);
263 }
264
265 static void image_uvs_transformmenu(bContext *C, uiLayout *layout, void *arg_unused)
266 {
267         uiItemEnumO(layout, NULL, 0, "TFM_OT_transform", "mode", TFM_TRANSLATION);
268         uiItemEnumO(layout, NULL, 0, "TFM_OT_transform", "mode", TFM_ROTATION);
269         uiItemEnumO(layout, NULL, 0, "TFM_OT_transform", "mode", TFM_RESIZE);
270 }
271
272 static void image_uvs_mirrormenu(bContext *C, uiLayout *layout, void *arg_unused)
273 {
274         uiItemEnumO(layout, NULL, 0, "UV_OT_mirror", "axis", 'x'); // "X Axis", M, 1
275         uiItemEnumO(layout, NULL, 0, "UV_OT_mirror", "axis", 'y'); // "Y Axis", M, 2
276 }
277
278 static void image_uvs_weldalignmenu(bContext *C, uiLayout *layout, void *arg_unused)
279 {
280         uiItemO(layout, NULL, 0, "UV_OT_weld"); // W, 1
281         uiItemsEnumO(layout, "UV_OT_align", "axis"); // W, 2/3/4
282 }
283
284 #if 0
285 #ifndef DISABLE_PYTHON
286 static void do_image_uvs_scriptsmenu(void *arg, int event)
287 {
288         BPY_menu_do_python(PYMENU_UV, event);
289
290         allqueue(REDRAWIMAGE, 0);
291 }
292
293 static void image_uvs_scriptsmenu (void *args_unused)
294 {
295         uiBlock *block;
296         BPyMenu *pym;
297         int i= 0;
298         short yco = 20, menuwidth = 120;
299         
300         block= uiNewBlock(&curarea->uiblocks, "image_uvs_scriptsmenu", UI_EMBOSSP);
301         uiBlockSetButmFunc(block, do_image_uvs_scriptsmenu, NULL);
302         
303         /* note that we acount for the N previous entries with i+20: */
304         for (pym = BPyMenuTable[PYMENU_UV]; pym; pym = pym->next, i++) {
305                 
306                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
307                                                  NULL, 0.0, 0.0, 1, i, 
308                                                  pym->tooltip?pym->tooltip:pym->filename);
309         }
310         
311         uiBlockSetDirection(block, UI_RIGHT);
312         uiTextBoundsBlock(block, 60);
313         
314         return block;
315 }
316 #endif /* DISABLE_PYTHON */
317 #endif
318
319 static void image_uvsmenu(bContext *C, uiLayout *layout, void *arg_unused)
320 {
321         bScreen *sc= CTX_wm_screen(C);
322         Scene *scene= CTX_data_scene(C);
323         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
324         PointerRNA uvptr, sceneptr;
325         Image *ima;
326         ImBuf *ibuf;
327         
328         /* retrieve state */
329         ima= ED_space_image(sima);
330         ibuf= ED_space_image_buffer(sima);
331
332         RNA_pointer_create(&sc->id, &RNA_SpaceUVEditor, sima, &uvptr);
333         RNA_id_pointer_create(&scene->id, &sceneptr);
334
335         /* create menu */
336         uiItemR(layout, NULL, 0, &uvptr, "snap_to_pixels", 0, 0, 0);
337         uiItemR(layout, NULL, 0, &uvptr, "constrain_to_image_bounds", 0, 0, 0);
338
339         uiItemS(layout);
340
341         uiItemR(layout, NULL, 0, &uvptr, "live_unwrap", 0, 0, 0);
342         uiItemO(layout, NULL, 0, "UV_OT_unwrap");
343         uiItemBooleanO(layout, "Unpin", 0, "UV_OT_pin", "clear", 1);
344         uiItemO(layout, NULL, 0, "UV_OT_pin");
345
346         uiItemS(layout);
347
348         uiItemO(layout, NULL, 0, "UV_OT_pack_islands");
349         uiItemO(layout, NULL, 0, "UV_OT_average_islands_scale");
350         uiItemO(layout, NULL, 0, "UV_OT_minimize_stretch");
351         uiItemO(layout, NULL, 0, "UV_OT_stitch");
352
353         uiItemS(layout);
354
355         uiItemMenuF(layout, "Transform", 0, image_uvs_transformmenu);
356         uiItemMenuF(layout, "Mirror", 0, image_uvs_mirrormenu);
357         uiItemMenuF(layout, "Weld/Align", 0, image_uvs_weldalignmenu);
358
359         uiItemS(layout);
360
361         uiItemR(layout, NULL, 0, &sceneptr, "proportional_editing", 0, 0, 0);
362         uiItemMenuEnumR(layout, NULL, 0, &sceneptr, "proportional_editing_falloff");
363
364         uiItemS(layout);
365
366         uiItemMenuF(layout, "Show/Hide Faces", 0, image_uvs_showhidemenu);
367
368 #if 0
369 #ifndef DISABLE_PYTHON
370         uiItemS(layout);
371
372         uiItemMenuF(layout, "Scripts", image_uvs_scriptsmenu);
373 #endif
374 #endif
375 }
376
377 static void image_menu_uvlayers(Object *obedit, char *menustr, int *active)
378 {
379         Mesh *me= (Mesh*)obedit->data;
380         EditMesh *em= BKE_mesh_get_editmesh(me);
381         CustomDataLayer *layer;
382         int i, count = 0;
383
384         menustr[0]= '\0';
385
386         for(i=0; i<em->fdata.totlayer; i++) {
387                 layer = &em->fdata.layers[i];
388
389                 if(layer->type == CD_MTFACE) {
390                         menustr += sprintf(menustr, "%s%%x%d|", layer->name, count);
391                         count++;
392                 }
393         }
394
395         *active= CustomData_get_active_layer(&em->fdata, CD_MTFACE);
396
397         BKE_mesh_end_editmesh(me, em);
398 }
399
400 static void do_image_buttons(bContext *C, void *arg, int event)
401 {
402         switch(event) {
403                 case B_REDR:
404                         ED_area_tag_redraw(CTX_wm_area(C));
405                         break;
406         }
407
408 #if 0
409         ToolSettings *settings= G.scene->toolsettings;
410         ID *id, *idtest;
411         int nr;
412
413         if(curarea->win==0) return;
414
415         if(event<=100) {
416                 if(event<=50) do_global_buttons2(event);
417                 else do_global_buttons(event);
418                 return;
419         }
420         
421         switch(event) {
422         case B_SIMABROWSE:      
423                 if(sima->imanr== -2) {
424                         if(G.qual & LR_CTRLKEY) {
425                                 activate_databrowse_imasel((ID *)sima->image, ID_IM, 0, B_SIMABROWSE,
426                                                                                         &sima->imanr, do_image_buttons);
427                         } else {
428                                 activate_databrowse((ID *)sima->image, ID_IM, 0, B_SIMABROWSE,
429                                                                                         &sima->imanr, do_image_buttons);
430                         }
431                         return;
432                 }
433                 if(sima->imanr < 0) break;
434         
435                 nr= 1;
436                 id= (ID *)sima->image;
437
438                 idtest= BLI_findlink(&G.main->image, sima->imanr-1);
439                 if(idtest==NULL) { /* no new */
440                         return;
441                 }
442         
443                 if(idtest!=id) {
444                         sima->image= (Image *)idtest;
445                         if(idtest->us==0) idtest->us= 1;
446                         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
447                         allqueue(REDRAWIMAGE, 0);
448                 }
449                 /* also when image is the same: assign! 0==no tileflag: */
450                 image_changed(sima, (Image *)idtest);
451                 BIF_undo_push("Assign image UV");
452
453                 break;
454         case B_SIMAGETILE:
455                 image_set_tile(sima, 1);                /* 1: only tileflag */
456                 allqueue(REDRAWVIEW3D, 0);
457                 allqueue(REDRAWIMAGE, 0);
458                 break;
459         case B_SIMA3DVIEWDRAW:
460                 allqueue(REDRAWVIEW3D, 0);
461                 break;
462         case B_SIMA_REDR_IMA_3D:
463                 allqueue(REDRAWVIEW3D, 0);
464                 allqueue(REDRAWIMAGE, 0);
465                 break;
466
467         case B_SIMAPACKIMA:
468                 pack_image_sima();
469                 break;
470                 
471         case B_SIMA_REPACK:
472                 BKE_image_memorypack(sima->image);
473                 allqueue(REDRAWIMAGE, 0);
474                 break;
475                 
476         case B_SIMA_USE_ALPHA:
477                 sima->flag &= ~(SI_SHOW_ALPHA|SI_SHOW_ZBUF);
478                 scrarea_queue_winredraw(curarea);
479                 scrarea_queue_headredraw(curarea);
480                 break;
481         case B_SIMA_SHOW_ALPHA:
482                 sima->flag &= ~(SI_USE_ALPHA|SI_SHOW_ZBUF);
483                 scrarea_queue_winredraw(curarea);
484                 scrarea_queue_headredraw(curarea);
485                 break;
486         case B_SIMA_SHOW_ZBUF:
487                 sima->flag &= ~(SI_SHOW_ALPHA|SI_USE_ALPHA);
488                 scrarea_queue_winredraw(curarea);
489                 scrarea_queue_headredraw(curarea);
490                 break;
491         case B_SIMARELOAD:
492                 reload_image_sima();
493                 break;
494         case B_SIMAGELOAD:
495                 open_image_sima(0);
496                 break;
497         case B_SIMANAME:
498                 if(sima->image) {
499                         Image *ima;
500                         char str[FILE_MAXDIR+FILE_MAXFILE];
501                         
502                         /* name in ima has been changed by button! */
503                         BLI_strncpy(str, sima->image->name, sizeof(str));
504                         ima= BKE_add_image_file(str);
505                         if(ima) {
506                                 BKE_image_signal(ima, &sima->iuser, IMA_SIGNAL_RELOAD);
507                                 image_changed(sima, ima);
508                         }
509                         BIF_undo_push("Load image");
510                         allqueue(REDRAWIMAGE, 0);
511                 }
512                 break;
513         case B_SIMAMULTI:
514                 if(sima && sima->image) {
515                         BKE_image_multilayer_index(sima->image->rr, &sima->iuser);
516                         allqueue(REDRAWIMAGE, 0);
517                 }
518                 break;
519         case B_TRANS_IMAGE:
520                 image_editvertex_buts(NULL);
521                 break;
522         case B_CURSOR_IMAGE:
523                 image_editcursor_buts(NULL);
524                 break;
525                 
526         case B_TWINANIM:
527         {
528                 Image *ima;
529                 int nr;
530                 
531                 ima = sima->image;
532                 if (ima) {
533                         if(ima->flag & IMA_TWINANIM) {
534                                 nr= ima->xrep*ima->yrep;
535                                 if(ima->twsta>=nr) ima->twsta= 1;
536                                 if(ima->twend>=nr) ima->twend= nr-1;
537                                 if(ima->twsta>ima->twend) ima->twsta= 1;
538                         }
539
540                         allqueue(REDRAWIMAGE, 0);
541                         allqueue(REDRAWVIEW3D, 0);
542                 }
543                 break;
544         }       
545         case B_SIMACLONEBROWSE:
546                 if(settings->imapaint.brush) {
547                         Brush *brush= settings->imapaint.brush;
548                 
549                         if(sima->menunr== -2) {
550                                 if(G.qual & LR_CTRLKEY) {
551                                         activate_databrowse_imasel((ID *)brush->clone.image, ID_IM, 0, B_SIMACLONEBROWSE,
552                                                                                                 &sima->menunr, do_image_buttons);
553                                 } else {
554                                         activate_databrowse((ID *)brush->clone.image, ID_IM, 0, B_SIMACLONEBROWSE,
555                                                                                                 &sima->menunr, do_image_buttons);
556                                 }
557                                 break;
558                         }
559                         if(sima->menunr < 0) break;
560
561                         if(brush_clone_image_set_nr(brush, sima->menunr))
562                                 allqueue(REDRAWIMAGE, 0);
563                 }
564                 break;
565                 
566         case B_SIMACLONEDELETE:
567                 if (settings->imapaint.brush)
568                         if (brush_clone_image_delete(settings->imapaint.brush))
569                                 allqueue(REDRAWIMAGE, 0);
570                 break;
571                 
572         case B_SIMABRUSHCHANGE:
573                 allqueue(REDRAWIMAGE, 0);
574                 allqueue(REDRAWBUTSEDIT, 0);
575                 break;
576                 
577         case B_SIMACURVES:
578                 curvemapping_do_ibuf(sima->cumap, imagewindow_get_ibuf(sima));
579                 allqueue(REDRAWIMAGE, 0);
580                 break;
581                 
582         case B_SIMARANGE:
583                 curvemapping_set_black_white(sima->cumap, NULL, NULL);
584                 curvemapping_do_ibuf(sima->cumap, imagewindow_get_ibuf(sima));
585                 allqueue(REDRAWIMAGE, 0);
586                 break;
587                 
588         case B_SIMABRUSHBROWSE:
589                 if(sima->menunr==-2) {
590                         activate_databrowse((ID*)settings->imapaint.brush, ID_BR, 0, B_SIMABRUSHBROWSE, &sima->menunr, do_global_buttons);
591                         break;
592                 }
593                 else if(sima->menunr < 0) break;
594                 
595                 if(brush_set_nr(&settings->imapaint.brush, sima->menunr)) {
596                         BIF_undo_push("Browse Brush");
597                         allqueue(REDRAWBUTSEDIT, 0);
598                         allqueue(REDRAWIMAGE, 0);
599                 }
600                 break;
601         case B_SIMABRUSHDELETE:
602                 if(brush_delete(&settings->imapaint.brush)) {
603                         BIF_undo_push("Unlink Brush");
604                         allqueue(REDRAWIMAGE, 0);
605                         allqueue(REDRAWBUTSEDIT, 0);
606                 }
607                 break;
608         case B_KEEPDATA:
609                 brush_toggled_fake_user(settings->imapaint.brush);
610                 allqueue(REDRAWIMAGE, 0);
611                 allqueue(REDRAWBUTSEDIT, 0);
612                 break;
613         case B_SIMABRUSHLOCAL:
614                 if(settings->imapaint.brush && settings->imapaint.brush->id.lib) {
615                         if(okee("Make local")) {
616                                 make_local_brush(settings->imapaint.brush);
617                                 allqueue(REDRAWIMAGE, 0);
618                                 allqueue(REDRAWBUTSEDIT, 0);
619                         }
620                 }
621                 break;
622         case B_SIMABTEXBROWSE:
623                 if(settings->imapaint.brush) {
624                         Brush *brush= settings->imapaint.brush;
625                         
626                         if(sima->menunr==-2) {
627                                 MTex *mtex= brush->mtex[brush->texact];
628                                 ID *id= (ID*)((mtex)? mtex->tex: NULL);
629                                 if(G.qual & LR_CTRLKEY) {
630                                         activate_databrowse_imasel(id, ID_TE, 0, B_SIMABTEXBROWSE, &sima->menunr, do_image_buttons);
631                                 } else {
632                                         activate_databrowse(id, ID_TE, 0, B_SIMABTEXBROWSE, &sima->menunr, do_image_buttons);
633                                 }
634                                 break;
635                         }
636                         else if(sima->menunr < 0) break;
637                         
638                         if(brush_texture_set_nr(brush, sima->menunr)) {
639                                 BIF_undo_push("Browse Brush Texture");
640                                 allqueue(REDRAWBUTSSHADING, 0);
641                                 allqueue(REDRAWBUTSEDIT, 0);
642                                 allqueue(REDRAWIMAGE, 0);
643                         }
644                 }
645                 break;
646         case B_SIMABTEXDELETE:
647                 if(settings->imapaint.brush) {
648                         if (brush_texture_delete(settings->imapaint.brush)) {
649                                 BIF_undo_push("Unlink Brush Texture");
650                                 allqueue(REDRAWBUTSSHADING, 0);
651                                 allqueue(REDRAWBUTSEDIT, 0);
652                                 allqueue(REDRAWIMAGE, 0);
653                         }
654                 }
655                 break;
656         case B_SIMA_PLAY:
657                 play_anim(0);
658                 break;
659         case B_SIMA_RECORD:
660                 imagespace_composite_flipbook(curarea);
661                 break;
662         }
663 #endif
664 }
665
666 #if 0
667 static void do_image_buttons_set_uvlayer_callback(void *act, void *data)
668 {
669         CustomData_set_layer_active(&G.editMesh->fdata, CD_MTFACE, *((int *)act));
670         
671         BIF_undo_push("Set Active UV Texture");
672         allqueue(REDRAWVIEW3D, 0);
673         allqueue(REDRAWBUTSEDIT, 0);
674         allqueue(REDRAWIMAGE, 0);
675 }
676 #endif
677
678 static void sima_idpoin_handle(bContext *C, ID *id, int event)
679 {
680         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
681         Scene *scene= CTX_data_scene(C);
682         Object *obedit= CTX_data_edit_object(C);
683
684         switch(event) {
685                 case UI_ID_BROWSE:
686                 case UI_ID_DELETE:
687                         ED_space_image_set(C, sima, scene, obedit, (Image*)id);
688                         ED_undo_push(C, "Assign Image UV");
689                         break;
690                 case UI_ID_RENAME:
691                         break;
692                 case UI_ID_ADD_NEW:
693                         WM_operator_name_call(C, "IMAGE_OT_new", WM_OP_INVOKE_REGION_WIN, NULL);
694                         break;
695                 case UI_ID_OPEN:
696                         WM_operator_name_call(C, "IMAGE_OT_open", WM_OP_INVOKE_REGION_WIN, NULL);
697                         break;
698                 case UI_ID_PIN:
699                         ED_area_tag_refresh(CTX_wm_area(C));
700                         break;
701         }
702 }
703
704 void image_header_buttons(const bContext *C, ARegion *ar)
705 {
706         bScreen *sc= CTX_wm_screen(C);
707         ScrArea *sa= CTX_wm_area(C);
708         Scene *scene= CTX_data_scene(C);
709         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
710         Image *ima;
711         ImBuf *ibuf;
712         uiBlock *block;
713         uiBut *but;
714         PointerRNA spaceptr, uvptr, sceneptr;
715         int xco, yco= 3, show_uvedit, show_render, show_paint, pinflag;
716
717         /* retrieve state */
718         ima= ED_space_image(sima);
719         ibuf= ED_space_image_buffer(sima);
720
721         show_render= ED_space_image_show_render(sima);
722         show_paint= ED_space_image_show_paint(sima);
723         show_uvedit= ED_space_image_show_uvedit(sima, CTX_data_edit_object(C));
724
725         RNA_pointer_create(&sc->id, &RNA_SpaceImageEditor, sima, &spaceptr);
726         RNA_pointer_create(&sc->id, &RNA_SpaceUVEditor, sima, &uvptr);
727         RNA_id_pointer_create(&scene->id, &sceneptr);
728         
729         /* create block */
730         block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
731         uiBlockSetHandleFunc(block, do_image_buttons, NULL);
732         
733         xco= ED_area_header_standardbuttons(C, block, yco);
734         
735         /* create pulldown menus */
736         if((sa->flag & HEADER_NO_PULLDOWN)==0) {
737                 char *menuname;
738                 int xmax;
739                 
740                 xmax= GetButStringLength("View");
741                 uiDefMenuBut(block, image_viewmenu, NULL, "View", xco, yco, xmax-3, 20, "");
742                 xco+= xmax;
743                 
744                 if(show_uvedit) {
745                         xmax= GetButStringLength("Select");
746                         uiDefMenuBut(block, image_selectmenu, NULL, "Select", xco, yco, xmax-3, 20, "");
747                         xco+= xmax;
748                 }
749                 
750                 menuname= (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))? "Image*": "Image";
751                 xmax= GetButStringLength(menuname);
752                 uiDefMenuBut(block, image_imagemenu, NULL, menuname, xco, yco, xmax-3, 20, "");
753                 xco+= xmax;
754
755                 if(show_uvedit) {
756                         xmax= GetButStringLength("UVs");
757                         uiDefMenuBut(block, image_uvsmenu, NULL, "UVs", xco, yco, xmax-3, 20, "");
758                         xco+= xmax;
759                 }
760         }
761
762         uiBlockSetEmboss(block, UI_EMBOSS);
763
764         /* image select */
765
766         pinflag= (show_render)? 0: UI_ID_PIN;
767         xco= uiDefIDPoinButs(block, CTX_data_main(C), NULL, (ID*)sima->image, ID_IM, &sima->pin, xco, yco,
768                 sima_idpoin_handle, UI_ID_BROWSE|UI_ID_BROWSE_RENDER|UI_ID_RENAME|UI_ID_ADD_NEW|UI_ID_OPEN|UI_ID_DELETE|pinflag);
769         xco += 8;
770
771         if(ima && !ELEM3(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE, IMA_SRC_VIEWER) && ima->ok) {
772                 /* XXX this should not be a static var */
773                 static int headerbuttons_packdummy;
774                 
775                 headerbuttons_packdummy = 0;
776
777                 if (ima->packedfile) {
778                         headerbuttons_packdummy = 1;
779                 }
780                 if (ima->packedfile && ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
781                         uiDefIconButBitI(block, TOG, 1, 0 /* XXX B_SIMA_REPACK */, ICON_UGLYPACKAGE,    xco,yco,XIC,YIC, &headerbuttons_packdummy, 0, 0, 0, 0, "Re-Pack this image as PNG");
782                 else
783                         uiDefIconButBitI(block, TOG, 1, 0 /* XXX B_SIMAPACKIMA */, ICON_PACKAGE,        xco,yco,XIC,YIC, &headerbuttons_packdummy, 0, 0, 0, 0, "Pack/Unpack this image");
784                         
785                 xco+= XIC+8;
786         }
787         
788         /* uv editing */
789         if(show_uvedit) {
790                 /* pivot */
791                 uiDefIconTextButS(block, ICONTEXTROW, B_NOP, ICON_ROTATE,
792                                 "Pivot: %t|Bounding Box Center %x0|Median Point %x3|2D Cursor %x1",
793                                 xco,yco,XIC+10,YIC, &ar->v2d.around, 0, 3.0, 0, 0,
794                                 "Rotation/Scaling Pivot (Hotkeys: Comma, Shift Comma, Period)");
795                 xco+= XIC + 18;
796                 
797                 /* selection modes */
798                 uiDefIconButBitS(block, TOG, UV_SYNC_SELECTION, B_REDR, ICON_EDIT, xco,yco,XIC,YIC, &scene->toolsettings->uv_flag, 0, 0, 0, 0, "Sync UV and Mesh Selection");
799                 xco+= XIC+8;
800
801                 if(scene->toolsettings->uv_flag & UV_SYNC_SELECTION) {
802                         uiBlockBeginAlign(block);
803                         
804                         uiDefIconButBitS(block, TOG, SCE_SELECT_VERTEX, B_REDR, ICON_VERTEXSEL,
805                                 xco,yco,XIC,YIC, &scene->toolsettings->selectmode, 1.0, 0.0, 0, 0, "Vertex select mode");
806                         uiDefIconButBitS(block, TOG, SCE_SELECT_EDGE, B_REDR, ICON_EDGESEL,
807                                 xco+=XIC,yco,XIC,YIC, &scene->toolsettings->selectmode, 1.0, 0.0, 0, 0, "Edge select mode");
808                         uiDefIconButBitS(block, TOG, SCE_SELECT_FACE, B_REDR, ICON_FACESEL,
809                                 xco+=XIC,yco,XIC,YIC, &scene->toolsettings->selectmode, 1.0, 0.0, 0, 0, "Face select mode");
810
811                         uiBlockEndAlign(block);
812                 }
813                 else {
814                         uiBlockBeginAlign(block);
815
816                         uiDefIconButS(block, ROW, B_REDR, ICON_VERTEXSEL,
817                                 xco,yco,XIC,YIC, &scene->toolsettings->uv_selectmode, 1.0, UV_SELECT_VERTEX, 0, 0, "Vertex select mode");
818                         uiDefIconButS(block, ROW, B_REDR, ICON_EDGESEL,
819                                 xco+=XIC,yco,XIC,YIC, &scene->toolsettings->uv_selectmode, 1.0, UV_SELECT_EDGE, 0, 0, "Edge select mode");
820                         uiDefIconButS(block, ROW, B_REDR, ICON_FACESEL,
821                                 xco+=XIC,yco,XIC,YIC, &scene->toolsettings->uv_selectmode, 1.0, UV_SELECT_FACE, 0, 0, "Face select mode");
822                         uiDefIconButS(block, ROW, B_REDR, ICON_LINKEDSEL,
823                                 xco+=XIC,yco,XIC,YIC, &scene->toolsettings->uv_selectmode, 1.0, UV_SELECT_ISLAND, 0, 0, "Island select mode");
824
825                         uiBlockEndAlign(block);
826
827                         /* would use these if const's could go in strings 
828                          * SI_STICKY_LOC SI_STICKY_DISABLE SI_STICKY_VERTEX */
829                         but = uiDefIconTextButC(block, ICONTEXTROW, B_REDR, ICON_STICKY_UVS_LOC,
830                                         "Sticky UV Selection: %t|Disable%x1|Shared Location%x0|Shared Vertex%x2",
831                                         xco+=XIC+10,yco,XIC+10,YIC, &(sima->sticky), 0, 3.0, 0, 0,
832                                         "Sticky UV Selection (Hotkeys: Shift C, Alt C, Ctrl C)");
833                 }
834
835                 xco+= XIC + 16;
836                 
837                 /* snap options, identical to options in 3d view header */
838                 uiBlockBeginAlign(block);
839
840                 if (scene->toolsettings->snap_flag & SCE_SNAP) {
841                         uiDefIconButBitS(block, TOG, SCE_SNAP, B_REDR, ICON_SNAP_GEO,xco,yco,XIC,YIC, &scene->toolsettings->snap_flag, 0, 0, 0, 0, "Use Snap or Grid (Shift Tab).");
842                         xco+= XIC;
843                         uiDefButS(block, MENU, B_NOP, "Mode%t|Closest%x0|Center%x1|Median%x2",xco,yco,70,YIC, &scene->toolsettings->snap_target, 0, 0, 0, 0, "Snap Target Mode.");
844                         xco+= 70;
845                 }
846                 else {
847                         uiDefIconButBitS(block, TOG, SCE_SNAP, B_REDR, ICON_SNAP_GEAR,xco,yco,XIC,YIC, &scene->toolsettings->snap_flag, 0, 0, 0, 0, "Snap while Ctrl is held during transform (Shift Tab).");   
848                         xco+= XIC;
849                 }
850
851                 uiBlockEndAlign(block);
852                 xco+= 8;
853
854                 /* uv layers */
855                 {
856                         Object *obedit= CTX_data_edit_object(C);
857                         char menustr[34*MAX_MTFACE];
858                         static int act;
859                         
860                         image_menu_uvlayers(obedit, menustr, &act);
861
862                         but = uiDefButI(block, MENU, B_NOP, menustr ,xco,yco,85,YIC, &act, 0, 0, 0, 0, "Active UV Layer for editing.");
863                         // uiButSetFunc(but, do_image_buttons_set_uvlayer_callback, &act, NULL);
864                         
865                         xco+= 85;
866                 }
867
868                 xco+= 8;
869         }
870         
871         if(ima) {
872                 RenderResult *rr;
873         
874                 /* render layers and passes */
875                 rr= BKE_image_get_renderresult(scene, ima);
876                 if(rr) {
877                         uiBlockBeginAlign(block);
878 #if 0
879                         uiblock_layer_pass_buttons(block, rr, &sima->iuser, B_REDR, xco, 0, 160);
880 #endif
881                         uiBlockEndAlign(block);
882                         xco+= 166;
883                 }
884
885                 /* painting */
886                 uiDefIconButR(block, TOG, B_REDR, ICON_TPAINT_HLT, xco,yco,XIC,YIC, &spaceptr, "image_painting", 0, 0, 0, 0, 0, NULL);
887                 xco+= XIC+8;
888
889                 /* image draw options */
890                 uiBlockBeginAlign(block);
891                 uiDefIconButR(block, ROW, B_REDR, ICON_IMAGE_RGB, xco,yco,XIC,YIC, &spaceptr, "draw_channels", 0, 0, 0, 0, 0, NULL);
892                 xco+= XIC;
893                 if(ibuf==NULL || ibuf->channels==4) {
894                         uiDefIconButR(block, ROW, B_REDR, ICON_IMAGE_RGB_ALPHA, xco,yco,XIC,YIC, &spaceptr, "draw_channels", 0, 0, SI_USE_ALPHA, 0, 0, NULL);
895                         xco+= XIC;
896                         uiDefIconButR(block, ROW, B_REDR, ICON_IMAGE_ALPHA, xco,yco,XIC,YIC, &spaceptr, "draw_channels", 0, 0, SI_SHOW_ALPHA, 0, 0, NULL);
897                         xco+= XIC;
898                 }
899                 if(ibuf) {
900                         if(ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1)) {
901                                 uiDefIconButR(block, ROW, B_REDR, ICON_IMAGE_ZDEPTH, xco,yco,XIC,YIC, &spaceptr, "draw_channels", 0, 0, SI_SHOW_ZBUF, 0, 0, NULL);
902                                 xco+= XIC;
903                         }
904                 }               
905 #ifdef WITH_LCMS
906                 uiDefIconButR(block, ROW, B_REDR, ICON_IMAGE_ALPHA, xco,yco,XIC,YIC, &spaceptr, "draw_channels", 0, 0, SI_COLOR_CORRECTION, 0, 0, NULL);
907                 xco+= XIC;
908 #endif
909                 xco+= 8;
910
911                 /* record & play */
912                 uiBlockBeginAlign(block);
913                 if(ima->type==IMA_TYPE_COMPOSITE) {
914                         uiDefIconButO(block, BUT, "IMAGE_OT_record_composite", WM_OP_INVOKE_REGION_WIN, ICON_REC, xco, yco, XIC, YIC, NULL); // Record Composite
915                         xco+= XIC;
916                 }
917                 if((ima->type==IMA_TYPE_COMPOSITE) || ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
918 //XXX                   uiDefIconButO(block, BUT, "IMAGE_OT_play_composite", WM_OP_INVOKE_REGION_WIN, ICON_PLAY, xco, yco, XIC, YIC, NULL); // PLAY
919                         xco+= XIC;
920                 }
921                 uiBlockEndAlign(block);
922                 xco+= 8;
923
924         }
925         
926         /* draw lock */
927         uiDefIconButR(block, ICONTOG, 0, ICON_UNLOCKED, xco,yco,XIC,YIC, &spaceptr, "update_automatically", 0, 0, 0, 0, 0, NULL);
928
929         /* always as last  */
930         UI_view2d_totRect_set(&ar->v2d, xco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);
931         
932         uiEndBlock(C, block);
933         uiDrawBlock(C, block);
934 }
935
936 /********************** toolbox operator *********************/
937
938 static int toolbox_invoke(bContext *C, wmOperator *op, wmEvent *event)
939 {
940         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
941         Object *obedit= CTX_data_edit_object(C);
942         uiPopupMenu *pup;
943         uiLayout *layout;
944         int show_uvedit;
945
946         show_uvedit= ED_space_image_show_uvedit(sima, obedit);
947
948         pup= uiPupMenuBegin(C, "Toolbox", 0);
949         layout= uiPupMenuLayout(pup);
950
951         uiItemMenuF(layout, "View", 0, image_viewmenu);
952         if(show_uvedit) uiItemMenuF(layout, "Select", 0, image_selectmenu);
953         uiItemMenuF(layout, "Image", 0, image_imagemenu);
954         if(show_uvedit) uiItemMenuF(layout, "UVs", 0, image_uvsmenu);
955
956         uiPupMenuEnd(C, pup);
957
958         return OPERATOR_CANCELLED;
959 }
960
961 void IMAGE_OT_toolbox(wmOperatorType *ot)
962 {
963         /* identifiers */
964         ot->name= "Toolbox";
965         ot->idname= "IMAGE_OT_toolbox";
966         
967         /* api callbacks */
968         ot->invoke= toolbox_invoke;
969         ot->poll= space_image_main_area_poll;
970 }
971