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