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