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