Two in one:
[blender.git] / source / blender / src / header_image.c
1 /**
2  * header_image.c oct-2003
3  *
4  * Functions to draw the "UV/Image Editor" window header
5  * and handle user events sent to it.
6  * 
7  * $Id$
8  *
9  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version. The Blender
15  * Foundation also sells licenses for use in proprietary software under
16  * the Blender License.  See http://www.blender.org/BL/ for information
17  * about this.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
27  *
28  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
29  * All rights reserved.
30  *
31  * The Original Code is: all of this file.
32  *
33  * Contributor(s): none yet.
34  *
35  * ***** END GPL/BL DUAL LICENSE BLOCK *****
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include "DNA_brush_types.h"
46 #include "DNA_ID.h"
47 #include "DNA_image_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_texture_types.h"
52 #include "DNA_userdef_types.h"
53
54 #include "BLI_blenlib.h"
55
56 #include "BDR_drawmesh.h"
57 #include "BDR_unwrapper.h"
58
59 #include "BKE_brush.h"
60 #include "BKE_colortools.h"
61 #include "BKE_global.h"
62 #include "BKE_image.h"
63 #include "BKE_main.h"
64 #include "BKE_utildefines.h"
65
66 #include "BIF_butspace.h"
67 #include "BIF_drawimage.h"
68 #include "BIF_editsima.h"
69 #include "BIF_interface.h"
70 #include "BIF_resources.h"
71 #include "BIF_screen.h"
72 #include "BIF_space.h"
73 #include "BIF_transform.h"
74 #include "BIF_toolbox.h"
75
76 #include "BSE_drawview.h"
77 #include "BSE_filesel.h"
78 #include "BSE_headerbuttons.h"
79 #include "BSE_trans_types.h"
80
81 #include "BPY_extern.h"
82 #include "BPY_menus.h"
83
84 #include "IMB_imbuf_types.h"
85
86 #include "RE_pipeline.h"
87
88 #include "blendef.h"
89 #include "butspace.h"
90 #include "mydevice.h"
91
92 void do_image_buttons(unsigned short event)
93 {
94         ToolSettings *settings= G.scene->toolsettings;
95         ID *id, *idtest;
96         int nr;
97
98         if(curarea->win==0) return;
99
100         if(event<=100) {
101                 if(event<=50) do_global_buttons2(event);
102                 else do_global_buttons(event);
103                 return;
104         }
105         
106         switch(event) {
107         case B_SIMAGEHOME:
108                 image_home();
109                 break;
110
111         case B_SIMABROWSE:      
112                 if(G.sima->imanr== -2) {
113                         activate_databrowse((ID *)G.sima->image, ID_IM, 0, B_SIMABROWSE,
114                                                                                         &G.sima->imanr, do_image_buttons);
115                         return;
116                 }
117                 if(G.sima->imanr < 0) break;
118         
119                 nr= 1;
120                 id= (ID *)G.sima->image;
121
122                 idtest= BLI_findlink(&G.main->image, G.sima->imanr-1);
123                 if(idtest==NULL) { /* no new */
124                         return;
125                 }
126         
127                 if(idtest!=id) {
128                         G.sima->image= (Image *)idtest;
129                         if(idtest->us==0) idtest->us= 1;
130                         BKE_image_signal(G.sima->image, &G.sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
131                         allqueue(REDRAWIMAGE, 0);
132                 }
133                 /* also when image is the same: assign! 0==no tileflag: */
134                 image_changed(G.sima, 0);
135                 BIF_undo_push("Assign image UV");
136
137                 break;
138                 
139         case B_SIMAGEDRAW:
140                 if(G.f & G_FACESELECT) {
141                         make_repbind(G.sima->image);
142                         image_changed(G.sima, 1);
143                 }
144                 /* XXX might be another event needed for this? */
145                 if(G.sima->image)
146                         if(ELEM(G.sima->image->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
147                                 if(G.sima->iuser.flag & IMA_ANIM_ALWAYS)
148                                         BKE_image_user_calc_imanr(&G.sima->iuser, G.scene->r.cfra, 0);
149                 allqueue(REDRAWVIEW3D, 0);
150                 allqueue(REDRAWIMAGE, 0);
151                 break;
152                 
153         case B_SIMAGEDRAW1:
154                 image_changed(G.sima, 2);               /* 2: only tileflag */
155                 allqueue(REDRAWVIEW3D, 0);
156                 allqueue(REDRAWIMAGE, 0);
157                 break;
158                 
159         case B_SIMAGEPAINTTOOL:
160                 if(G.sima->flag & SI_DRAWTOOL)
161                         /* add new brush if none exists */
162                         brush_check_exists(&G.scene->toolsettings->imapaint.brush);
163                 allqueue(REDRAWIMAGE, 0);
164                 allqueue(REDRAWVIEW3D, 0);
165                 break;
166
167         case B_SIMAPACKIMA:
168                 pack_image_sima();
169                 break;
170                 
171         case B_SIMA_REPACK:
172                 BKE_image_memorypack(G.sima->image);
173                 allqueue(REDRAWIMAGE, 0);
174                 break;
175                 
176         case B_SIMA_USE_ALPHA:
177                 G.sima->flag &= ~(SI_SHOW_ALPHA|SI_SHOW_ZBUF);
178                 scrarea_queue_winredraw(curarea);
179                 scrarea_queue_headredraw(curarea);
180                 break;
181         case B_SIMA_SHOW_ALPHA:
182                 G.sima->flag &= ~(SI_USE_ALPHA|SI_SHOW_ZBUF);
183                 scrarea_queue_winredraw(curarea);
184                 scrarea_queue_headredraw(curarea);
185                 break;
186         case B_SIMA_SHOW_ZBUF:
187                 G.sima->flag &= ~(SI_SHOW_ALPHA|SI_USE_ALPHA);
188                 scrarea_queue_winredraw(curarea);
189                 scrarea_queue_headredraw(curarea);
190                 break;
191         case B_SIMARELOAD:
192                 reload_image_sima();
193                 break;
194         case B_SIMAGELOAD:
195                 open_image_sima(0);
196                 break;
197         case B_SIMANAME:
198                 if(G.sima->image) {
199                         Image *ima;
200                         char str[FILE_MAXDIR+FILE_MAXFILE];
201                         
202                         /* name in ima has been changed by button! */
203                         BLI_strncpy(str, G.sima->image->name, sizeof(str));
204                         ima= BKE_add_image_file(str);
205                         if(ima) {
206                                 
207                                 G.sima->image= ima;
208                                 
209                                 BKE_image_signal(ima, &G.sima->iuser, IMA_SIGNAL_RELOAD);
210                                 image_changed(G.sima, 0);
211                                 
212                         }
213                         BIF_undo_push("Load image");
214                         allqueue(REDRAWIMAGE, 0);
215                 }
216                 break;
217         case B_SIMAMULTI:
218                 if(G.sima && G.sima->image) {
219                         BKE_image_multilayer_index(G.sima->image->rr, &G.sima->iuser);
220                         allqueue(REDRAWIMAGE, 0);
221                 }
222                 break;
223         case B_TRANS_IMAGE:
224                 image_editvertex_buts(NULL);
225                 break;
226                 
227         case B_TWINANIM:
228         {
229                 Image *ima;
230                 int nr;
231                 
232                 ima = G.sima->image;
233                 if (ima) {
234                         if(ima->flag & IMA_TWINANIM) {
235                                 nr= ima->xrep*ima->yrep;
236                                 if(ima->twsta>=nr) ima->twsta= 1;
237                                 if(ima->twend>=nr) ima->twend= nr-1;
238                                 if(ima->twsta>ima->twend) ima->twsta= 1;
239                                 allqueue(REDRAWIMAGE, 0);
240                         }
241                 }
242                 break;
243         }       
244         case B_SIMACLONEBROWSE:
245                 if (settings->imapaint.brush)
246                         if (brush_clone_image_set_nr(settings->imapaint.brush, G.sima->menunr))
247                                 allqueue(REDRAWIMAGE, 0);
248                 break;
249                 
250         case B_SIMACLONEDELETE:
251                 if (settings->imapaint.brush)
252                         if (brush_clone_image_delete(settings->imapaint.brush))
253                                 allqueue(REDRAWIMAGE, 0);
254                 break;
255                 
256         case B_SIMABRUSHCHANGE:
257                 allqueue(REDRAWIMAGE, 0);
258                 allqueue(REDRAWBUTSEDIT, 0);
259                 break;
260                 
261         case B_SIMACURVES:
262                 curvemapping_do_ibuf(G.sima->cumap, imagewindow_get_ibuf(G.sima));
263                 allqueue(REDRAWIMAGE, 0);
264                 break;
265                 
266         case B_SIMARANGE:
267                 curvemapping_set_black_white(G.sima->cumap, NULL, NULL);
268                 curvemapping_do_ibuf(G.sima->cumap, imagewindow_get_ibuf(G.sima));
269                 allqueue(REDRAWIMAGE, 0);
270                 break;
271                 
272         case B_SIMABRUSHBROWSE:
273                 if(G.sima->menunr==-2) {
274                         activate_databrowse((ID*)settings->imapaint.brush, ID_BR, 0, B_SIMABRUSHBROWSE, &G.sima->menunr, do_global_buttons);
275                         break;
276                 }
277                 else if(G.sima->menunr < 0) break;
278                 
279                 if(brush_set_nr(&settings->imapaint.brush, G.sima->menunr)) {
280                         BIF_undo_push("Browse Brush");
281                         allqueue(REDRAWBUTSEDIT, 0);
282                         allqueue(REDRAWIMAGE, 0);
283                 }
284                 break;
285         case B_SIMABRUSHDELETE:
286                 if(brush_delete(&settings->imapaint.brush)) {
287                         BIF_undo_push("Unlink Brush");
288                         allqueue(REDRAWIMAGE, 0);
289                         allqueue(REDRAWBUTSEDIT, 0);
290                 }
291                 break;
292         case B_KEEPDATA:
293                 brush_toggle_fake_user(settings->imapaint.brush);
294                 allqueue(REDRAWIMAGE, 0);
295                 allqueue(REDRAWBUTSEDIT, 0);
296                 break;
297         case B_SIMABRUSHLOCAL:
298                 if(settings->imapaint.brush && settings->imapaint.brush->id.lib) {
299                         if(okee("Make local")) {
300                                 make_local_brush(settings->imapaint.brush);
301                                 allqueue(REDRAWIMAGE, 0);
302                                 allqueue(REDRAWBUTSEDIT, 0);
303                         }
304                 }
305                 break;
306         case B_SIMABTEXBROWSE:
307                 if(settings->imapaint.brush) {
308                         Brush *brush= settings->imapaint.brush;
309                         
310                         if(G.sima->menunr==-2) {
311                                 MTex *mtex= brush->mtex[brush->texact];
312                                 ID *id= (ID*)((mtex)? mtex->tex: NULL);
313                                 activate_databrowse(id, ID_TE, 0, B_SIMABTEXBROWSE, &G.sima->menunr, do_global_buttons);
314                                 break;
315                         }
316                         else if(G.sima->menunr < 0) break;
317                         
318                         if(brush_texture_set_nr(brush, G.sima->menunr)) {
319                                 BIF_undo_push("Browse Brush Texture");
320                                 allqueue(REDRAWBUTSSHADING, 0);
321                                 allqueue(REDRAWBUTSEDIT, 0);
322                                 allqueue(REDRAWIMAGE, 0);
323                         }
324                 }
325                 break;
326         case B_SIMABTEXDELETE:
327                 if(settings->imapaint.brush) {
328                         if (brush_texture_delete(settings->imapaint.brush)) {
329                                 BIF_undo_push("Unlink Brush Texture");
330                                 allqueue(REDRAWBUTSSHADING, 0);
331                                 allqueue(REDRAWBUTSEDIT, 0);
332                                 allqueue(REDRAWIMAGE, 0);
333                         }
334                 }
335                 break;
336         case B_SIMA_PLAY:
337                 play_anim(0);
338                 break;
339         case B_SIMA_RECORD:
340                 imagespace_composite_flipbook(curarea);
341                 break;
342         }
343 }
344
345 static void do_image_view_viewnavmenu(void *arg, int event)
346 {
347         switch(event) {
348         case 1: /* Zoom In */
349                 image_viewzoom(PADPLUSKEY, 0);
350                 break;
351         case 2: /* Zoom Out */
352                 image_viewzoom(PADMINUS, 0);
353                 break;
354         case 3: /* Zoom 8:1 */
355                 image_viewzoom(PAD8, 0);
356                 break;
357         case 4: /* Zoom 4:1 */
358                 image_viewzoom(PAD4, 0);
359                 break;
360         case 5: /* Zoom 2:1 */
361                 image_viewzoom(PAD2, 0);
362                 break;
363         case 6: /* Zoom 1:1 */
364                 image_viewzoom(PAD1, 0);
365                 break;
366         case 7: /* Zoom 1:2 */
367                 image_viewzoom(PAD2, 1);
368                 break;
369         case 8: /* Zoom 1:4 */
370                 image_viewzoom(PAD4, 1);
371                 break;
372         case 9: /* Zoom 1:8 */
373                 image_viewzoom(PAD8, 1);
374                 break;
375         }
376         allqueue(REDRAWIMAGE, 0);
377 }
378
379 static uiBlock *image_view_viewnavmenu(void *arg_unused)
380 {
381 /*              static short tog=0; */
382         uiBlock *block;
383         short yco= 0, menuwidth=120;
384         
385         block= uiNewBlock(&curarea->uiblocks, "image_view_viewnavmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
386         uiBlockSetButmFunc(block, do_image_view_viewnavmenu, NULL);
387         
388         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom In|NumPad +", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
389         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom Out|NumPad -",      0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
390
391         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
392
393         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 1:8|Shift+NumPad 8", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 3, "");
394         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 1:4|Shift+NumPad 4", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
395         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 1:2|Shift+NumPad 2", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
396         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 1:1|NumPad 1", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
397         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 2:1|NumPad 2", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
398         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 4:1|NumPad 4", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 8, "");
399         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Zoom 8:1|NumPad 8", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 9, "");
400
401         uiBlockSetDirection(block, UI_RIGHT);
402         uiTextBoundsBlock(block, 50);
403         return block;
404 }
405
406 static void do_image_viewmenu(void *arg, int event)
407 {
408
409         switch(event) {
410         case 0: /* Update Automatically */
411                 if(G.sima->lock) G.sima->lock = 0;
412                 else G.sima->lock = 1;
413                 break;
414         case 1: /* View All */
415                 do_image_buttons(B_SIMAGEHOME);
416                 break;
417         case 2: /* Maximize Window */
418                 /* using event B_FULL */
419                 break;
420         case 5: /* Draw Shadow Mesh */
421                 if(G.sima->flag & SI_DRAWSHADOW)
422                         G.sima->flag &= ~SI_DRAWSHADOW;
423                 else
424                         G.sima->flag |= SI_DRAWSHADOW;
425                 allqueue(REDRAWIMAGE, 0);
426                 break;
427         case 6: /* Draw Faces */
428                 if(G.f & G_DRAWFACES)
429                         G.f &= ~G_DRAWFACES;
430                 else
431                         G.f |= G_DRAWFACES;
432                 allqueue(REDRAWIMAGE, 0);
433                 break;
434         case 7: /* Properties  Panel */
435                 add_blockhandler(curarea, IMAGE_HANDLER_PROPERTIES, UI_PNL_UNSTOW);
436                 break;
437         case 8: /* Paint Panel... */
438                 add_blockhandler(curarea, IMAGE_HANDLER_PAINT, UI_PNL_UNSTOW);
439                 break;
440         case 9:
441                 image_viewcentre();
442         case 10: /* Display Normalized Coordinates */
443                 G.sima->flag ^= SI_COORDFLOATS;
444                 allqueue(REDRAWIMAGE, 0);
445                 break;
446         case 11: /* Curves Panel... */
447                 add_blockhandler(curarea, IMAGE_HANDLER_CURVES, UI_PNL_UNSTOW);
448                 break;
449         case 12: /* composite preview */
450                 toggle_blockhandler(curarea, IMAGE_HANDLER_PREVIEW, 0);
451                 scrarea_queue_winredraw(curarea);
452                 break;
453         case 13: /* Realtime Panel... */
454                 add_blockhandler(curarea, IMAGE_HANDLER_GAME_PROPERTIES, UI_PNL_UNSTOW);
455                 break;
456                 
457         }
458         allqueue(REDRAWVIEW3D, 0);
459 }
460
461 static uiBlock *image_viewmenu(void *arg_unused)
462 {
463 /*      static short tog=0; */
464         uiBlock *block;
465         short yco= 0, menuwidth=120;
466         
467         block= uiNewBlock(&curarea->uiblocks, "image_viewmenu", UI_EMBOSSP, UI_HELV, curarea->headwin);
468         uiBlockSetButmFunc(block, do_image_viewmenu, NULL);
469
470         uiDefIconTextBut(block, BUTM, 1, ICON_MENU_PANEL, "Properties...",      0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
471         uiDefIconTextBut(block, BUTM, 1, ICON_MENU_PANEL, "Real-time Properties...",    0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 13, "");
472         uiDefIconTextBut(block, BUTM, 1, ICON_MENU_PANEL, "Paint Tool...|C",    0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 8, "");
473         uiDefIconTextBut(block, BUTM, 1, ICON_MENU_PANEL, "Curves Tool...",     0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 11, "");
474         uiDefIconTextBut(block, BUTM, 1, ICON_MENU_PANEL, "Composite Preview...|Shift P",       0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 12, "");
475
476         if(G.sima->flag & SI_COORDFLOATS) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Display Normalized Coordinates|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 10, "");
477         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Display Normalized Coordinates|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 10, "");
478         if(G.f & G_DRAWFACES) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Draw Faces", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
479         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Draw Faces|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
480         if(G.sima->flag & SI_DRAWSHADOW) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Draw Shadow Mesh", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
481         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Draw Shadow Mesh|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
482
483         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
484         uiDefIconTextBlockBut(block, image_view_viewnavmenu, NULL, ICON_RIGHTARROW_THIN, "View Navigation", 0, yco-=20, 120, 19, "");
485
486         if(G.sima->lock) {
487                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Update Automatically|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 0, "");
488         } else {
489                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Update Automatically|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 0, "");
490         } 
491
492         uiDefBut(block, SEPR, 0, "",                                    0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
493         
494         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "View Selected|NumPad .",                 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 9, "");
495         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "View All|Home", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
496                 
497         if(!curarea->full) uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Maximize Window|Ctrl UpArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
498         else uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Tile Window|Ctrl DownArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
499         
500         if(curarea->headertype==HEADERTOP) {
501                 uiBlockSetDirection(block, UI_DOWN);
502         }
503         else {
504                 uiBlockSetDirection(block, UI_TOP);
505                 uiBlockFlipOrder(block);
506         }
507
508         uiTextBoundsBlock(block, 50);
509         
510         return block;
511 }
512
513 static void do_image_selectmenu(void *arg, int event)
514 {
515         switch(event)
516         {
517         case 0: /* Border Select */
518                 borderselect_sima(UV_SELECT_ALL);
519                 break;
520         case 8: /* Border Select Pinned */
521                 borderselect_sima(UV_SELECT_PINNED);
522                 break;
523         case 1: /* Select/Deselect All */
524                 select_swap_tface_uv();
525                 break;
526         case 2: /* Unlink Selection */
527                 unlink_selection();
528                 break;
529         case 3: /* Linked UVs */
530                 select_linked_tface_uv(2);
531                 break;
532         case 4: /* Toggle Local UVs Stick to Vertex in Mesh */
533                 if(G.sima->flag & SI_LOCALSTICKY)
534                         G.sima->flag &= ~SI_LOCALSTICKY;
535                 else {
536                         G.sima->flag |= SI_LOCALSTICKY;
537                         G.sima->flag &= ~SI_STICKYUVS;
538                 }
539                 allqueue(REDRAWIMAGE, 0);
540                 break;  
541         case 5: /* Toggle UVs Stick to Vertex in Mesh */
542                 if(G.sima->flag & SI_STICKYUVS) {
543                         G.sima->flag &= ~SI_STICKYUVS;
544                         G.sima->flag |= SI_LOCALSTICKY;
545                 }
546                 else {
547                         G.sima->flag |= SI_STICKYUVS;
548                         G.sima->flag &= ~SI_LOCALSTICKY;
549                 }
550                 allqueue(REDRAWIMAGE, 0);
551                 break;  
552         case 6: /* Toggle Active Face Select */
553                 if(G.sima->flag & SI_SELACTFACE)
554                         G.sima->flag &= ~SI_SELACTFACE;
555                 else
556                         G.sima->flag |= SI_SELACTFACE;
557                 allqueue(REDRAWIMAGE, 0);
558                 break;
559         case 7: /* Pinned UVs */
560                 select_pinned_tface_uv();
561                 break;
562         }
563 }
564
565 static uiBlock *image_selectmenu(void *arg_unused)
566 {
567         uiBlock *block;
568         short yco= 0, menuwidth=120;
569
570         block= uiNewBlock(&curarea->uiblocks, "image_selectmenu", UI_EMBOSSP, UI_HELV, curarea->headwin);
571         uiBlockSetButmFunc(block, do_image_selectmenu, NULL);
572
573         if(G.sima->flag & SI_SELACTFACE) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Active Face Select|C", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
574         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Active Face Select|C", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
575
576         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
577
578         if(G.sima->flag & SI_LOCALSTICKY) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Stick Local UVs to Mesh Vertex|Shift C", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
579         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Stick Local UVs to Mesh Vertex|Shift C", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
580
581         if(G.sima->flag & SI_STICKYUVS) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Stick UVs to Mesh Vertex|Ctrl C", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
582         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Stick UVs to Mesh Vertex|Ctrl C", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
583
584         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
585
586         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Border Select|B", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 0, "");
587         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Border Select Pinned|Shift B", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 8, "");
588
589         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
590
591         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Select/Deselect All|A", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
592
593         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Unlink Selection|Alt L", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
594         
595         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
596
597         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Pinned UVs|Shift P", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
598         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Linked UVs|Ctrl L", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 3, "");
599
600         if(curarea->headertype==HEADERTOP) {
601                 uiBlockSetDirection(block, UI_DOWN);
602         }
603         else {
604                 uiBlockSetDirection(block, UI_TOP);
605                 uiBlockFlipOrder(block);
606         }
607
608         uiTextBoundsBlock(block, 50);
609
610         return block;
611 }
612
613 static void do_image_image_rtmappingmenu(void *arg, int event)
614 {
615         switch(event) {
616         case 0: /* UV Co-ordinates */
617                 G.sima->image->flag &= ~IMA_REFLECT;
618                 break;
619         case 1: /* Reflection */
620                 G.sima->image->flag |= IMA_REFLECT;
621                 break;
622         }
623
624         allqueue(REDRAWVIEW3D, 0);
625 }
626
627 static uiBlock *image_image_rtmappingmenu(void *arg_unused)
628 {
629         uiBlock *block;
630         short yco = 20, menuwidth = 120;
631
632         block= uiNewBlock(&curarea->uiblocks, "image_image_rtmappingmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
633         uiBlockSetButmFunc(block, do_image_image_rtmappingmenu, NULL);
634         
635         if (G.sima->image->flag & IMA_REFLECT) {
636                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "UV Co-ordinates",        0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 0, "");
637                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Reflection",               0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
638         } else {
639                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "UV Co-ordinates",  0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 0, "");
640                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Reflection",             0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
641         }
642
643         uiBlockSetDirection(block, UI_RIGHT);
644         uiTextBoundsBlock(block, 60);
645         return block;
646 }
647
648 static void do_image_imagemenu(void *arg, int event)
649 {
650         /* events >=20 are registered bpython scripts */
651         if (event >= 20) BPY_menu_do_python(PYMENU_IMAGE, event - 20);
652         
653         switch(event)
654         {
655         case 0:
656                 open_image_sima((G.qual==LR_CTRLKEY));
657                 break;
658         case 1:
659                 replace_image_sima((G.qual==LR_CTRLKEY));
660                 break;
661         case 2:
662                 pack_image_sima();
663                 break;
664         case 4: /* Texture Painting */
665                 if(G.sima->flag & SI_DRAWTOOL) G.sima->flag &= ~SI_DRAWTOOL;
666                 else G.sima->flag |= SI_DRAWTOOL;
667                 break;
668         case 5:
669                 save_as_image_sima();
670                 break;
671         case 6:
672                 reload_image_sima();
673                 break;
674         case 7:
675                 new_image_sima();
676                 break;
677         case 8:
678                 save_image_sima();
679                 break;
680         case 9:
681                 save_image_sequence_sima();
682                 break;
683         case 10:
684                 BKE_image_memorypack(G.sima->image);
685                 allqueue(REDRAWIMAGE, 0);
686                 break;
687         }
688 }
689
690 static uiBlock *image_imagemenu(void *arg_unused)
691 {
692         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
693         uiBlock *block;
694         short yco= 0, menuwidth=150;
695         BPyMenu *pym;
696         int i = 0;
697
698         block= uiNewBlock(&curarea->uiblocks, "image_imagemenu", UI_EMBOSSP, UI_HELV, curarea->headwin);
699         uiBlockSetButmFunc(block, do_image_imagemenu, NULL);
700
701         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "New...|Alt N", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
702         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Open...|Alt O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 0, "");
703         
704         if (G.sima->image) {
705                 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Replace...", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
706                 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Reload|Alt R", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
707                 uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, ""); 
708
709                 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Save|Alt S", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 8, "");
710                 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Save As...", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
711                 if(G.sima->image->source==IMA_SRC_SEQUENCE)
712                         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Save Changed Images", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 9, "");
713                 uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, ""); 
714                 
715                 if (G.sima->image->packedfile) {
716                         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Unpack Image...", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
717                 } else {
718                         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Pack Image", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
719                 }
720                 
721                 /* only for dirty && specific image types */
722                 if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
723                         if( ELEM(G.sima->image->source, IMA_SRC_FILE, IMA_SRC_GENERATED))
724                                 if(G.sima->image->type!=IMA_TYPE_MULTILAYER)
725                                         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Pack Image as PNG", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 10, "");
726                 
727                 uiDefBut(block, SEPR, 0, "",        0, yco-=7, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
728
729                 if(G.sima->flag & SI_DRAWTOOL) {
730                         uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Texture Painting", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
731                 } else {
732                         uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Texture Painting", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
733                 }
734                 
735                 uiDefBut(block, SEPR, 0, "",        0, yco-=7, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
736                 
737                 uiDefIconTextBlockBut(block, image_image_rtmappingmenu, NULL, ICON_RIGHTARROW_THIN, "Realtime Texture Mapping", 0, yco-=20, 120, 19, "");
738                 // uiDefIconTextBut(block, BUTM, 1, ICON_MENU_PANEL, "Realtime Texture Animation|",             0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
739         }
740         
741         /* note that we acount for the N previous entries with i+20: */
742         for (pym = BPyMenuTable[PYMENU_IMAGE]; pym; pym = pym->next, i++) {
743
744                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
745                                  NULL, 0.0, 0.0, 1, i+20, 
746                                  pym->tooltip?pym->tooltip:pym->filename);
747         }
748         
749         if(curarea->headertype==HEADERTOP) {
750                 uiBlockSetDirection(block, UI_DOWN);
751         }
752         else {
753                 uiBlockSetDirection(block, UI_TOP);
754                 uiBlockFlipOrder(block);
755         }
756
757         uiTextBoundsBlock(block, 80);
758
759         return block;
760 }
761
762 static void do_image_uvs_showhidemenu(void *arg, int event)
763 {
764         switch(event) {
765         case 4: /* show hidden faces */
766                 reveal_tface_uv();
767                 break;
768         case 5: /* hide selected faces */
769                 hide_tface_uv(0);
770                 break;
771         case 6: /* hide deselected faces */
772                 hide_tface_uv(1);
773                 break;
774         }
775         allqueue(REDRAWVIEW3D, 0);
776 }
777
778 static uiBlock *image_uvs_showhidemenu(void *arg_unused)
779 {
780         uiBlock *block;
781         short yco = 20, menuwidth = 120;
782
783         block= uiNewBlock(&curarea->uiblocks, "image_uvs_showhidemenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
784         uiBlockSetButmFunc(block, do_image_uvs_showhidemenu, NULL);
785         
786         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Show Hidden Faces|Alt H",                0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 4, "");
787         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Hide Selected Faces|H",          0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 5, "");
788         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Hide Deselected Faces|Shift H",          0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 6, "");
789
790         uiBlockSetDirection(block, UI_RIGHT);
791         uiTextBoundsBlock(block, 60);
792         return block;
793 }
794
795 static void do_image_uvs_propfalloffmenu(void *arg, int event)
796 {
797         G.scene->prop_mode= event;
798         allqueue(REDRAWVIEW3D, 1);
799 }
800
801 static uiBlock *image_uvs_propfalloffmenu(void *arg_unused)
802 {
803         uiBlock *block;
804         short yco = 20, menuwidth = 120;
805
806         block= uiNewBlock(&curarea->uiblocks, "image_uvs_propfalloffmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
807         uiBlockSetButmFunc(block, do_image_uvs_propfalloffmenu, NULL);
808         
809         if (G.scene->prop_mode==PROP_SMOOTH) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Smooth|Shift O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_SMOOTH, "");
810         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Smooth|Shift O",    0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_SMOOTH, "");
811         if (G.scene->prop_mode==PROP_SPHERE) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Sphere|Shift O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_SPHERE, "");
812         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Sphere|Shift O",    0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_SPHERE, "");
813         if (G.scene->prop_mode==PROP_ROOT) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Root|Shift O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_ROOT, "");
814         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Root|Shift O",      0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_ROOT, "");
815         if (G.scene->prop_mode==PROP_SHARP) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Sharp|Shift O",        0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_SHARP, "");
816         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Sharp|Shift O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_SHARP, "");
817         if (G.scene->prop_mode==PROP_LIN) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Linear|Shift O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_LIN, "");
818         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Linear|Shift O",    0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_LIN, "");
819         if (G.scene->prop_mode==PROP_CONST) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Constant|Shift O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_CONST, "");
820         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Constant|Shift O",  0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, PROP_CONST, "");
821                 
822         uiBlockSetDirection(block, UI_RIGHT);
823         uiTextBoundsBlock(block, 60);
824         return block;
825 }
826
827 static void do_image_uvs_transformmenu(void *arg, int event)
828 {
829         switch(event) {
830         case 0: /* Grab */
831                 initTransform(TFM_TRANSLATION, CTX_NONE);
832                 Transform();
833                 break;
834         case 1: /* Rotate */
835                 initTransform(TFM_ROTATION, CTX_NONE);
836                 Transform();
837                 break;
838         case 2: /* Scale */
839                 initTransform(TFM_RESIZE, CTX_NONE);
840                 Transform();
841                 break;
842         }
843 }
844
845 static uiBlock *image_uvs_transformmenu(void *arg_unused)
846 {
847         uiBlock *block;
848         short yco = 20, menuwidth = 120;
849
850         block= uiNewBlock(&curarea->uiblocks, "image_uvs_transformmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
851         uiBlockSetButmFunc(block, do_image_uvs_transformmenu, NULL);
852         
853         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Grab/Move|G", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 0, "");
854         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Rotate|R", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
855         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Scale|S", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
856
857         uiBlockSetDirection(block, UI_RIGHT);
858         uiTextBoundsBlock(block, 60);
859         return block;
860 }
861
862 static void do_image_uvs_mirrormenu(void *arg, int event)
863 {
864         switch(event) {
865         case 0: /* X axis */
866                 mirror_tface_uv('x');
867                 break;
868         case 1: /* Y axis */
869                 mirror_tface_uv('y');
870                 break;
871         }
872         
873         BIF_undo_push("Mirror UV");
874 }
875
876 static uiBlock *image_uvs_mirrormenu(void *arg_unused)
877 {
878         uiBlock *block;
879         short yco = 20, menuwidth = 120;
880
881         block= uiNewBlock(&curarea->uiblocks, "image_uvs_mirrormenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
882         uiBlockSetButmFunc(block, do_image_uvs_mirrormenu, NULL);
883         
884         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "X Axis|M, 1", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 0, "");
885         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Y Axis|M, 2", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
886
887         uiBlockSetDirection(block, UI_RIGHT);
888         uiTextBoundsBlock(block, 60);
889         return block;
890 }
891
892 static void do_image_uvs_weldalignmenu(void *arg, int event)
893 {
894         switch(event) {
895         case 0: /* Weld */
896                 weld_align_tface_uv('w');
897                 break;
898         case 1: /* Align X */
899                 weld_align_tface_uv('x');
900                 break;
901         case 2: /* Align Y */
902                 weld_align_tface_uv('y');
903                 break;
904         }
905         
906         if(event==0) BIF_undo_push("Weld UV");
907         else if(event==1 || event==2) BIF_undo_push("Align UV");
908 }
909
910 static uiBlock *image_uvs_weldalignmenu(void *arg_unused)
911 {
912         uiBlock *block;
913         short yco = 20, menuwidth = 120;
914
915         block= uiNewBlock(&curarea->uiblocks, "image_uvs_weldalignmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
916         uiBlockSetButmFunc(block, do_image_uvs_weldalignmenu, NULL);
917         
918         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Weld|W, 1", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 0, "");
919         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Align X|W, 2", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
920         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Align Y|W, 3", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
921
922         uiBlockSetDirection(block, UI_RIGHT);
923         uiTextBoundsBlock(block, 60);
924         return block;
925 }
926
927 static void do_image_uvs_scriptsmenu(void *arg, int event)
928 {
929         BPY_menu_do_python(PYMENU_UV, event);
930
931         allqueue(REDRAWIMAGE, 0);
932 }
933
934 static uiBlock *image_uvs_scriptsmenu (void *args_unused)
935 {
936         uiBlock *block;
937         BPyMenu *pym;
938         int i= 0;
939         short yco = 20, menuwidth = 120;
940         
941         block= uiNewBlock(&curarea->uiblocks, "image_uvs_scriptsmenu", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
942         uiBlockSetButmFunc(block, do_image_uvs_scriptsmenu, NULL);
943         
944         /* note that we acount for the N previous entries with i+20: */
945         for (pym = BPyMenuTable[PYMENU_UV]; pym; pym = pym->next, i++) {
946                 
947                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
948                                                  NULL, 0.0, 0.0, 1, i, 
949                                                  pym->tooltip?pym->tooltip:pym->filename);
950         }
951         
952         uiBlockSetDirection(block, UI_RIGHT);
953         uiTextBoundsBlock(block, 60);
954         
955         return block;
956 }
957
958 static void do_image_uvsmenu(void *arg, int event)
959 {
960
961         switch(event) {
962         case 1: /* UVs Constrained Rectangular */
963                 if(G.sima->flag & SI_BE_SQUARE) G.sima->flag &= ~SI_BE_SQUARE;
964                 else G.sima->flag |= SI_BE_SQUARE;
965                 break;
966         case 2: /* UVs Clipped to Image Size */
967                 if(G.sima->flag & SI_CLIP_UV) G.sima->flag &= ~SI_CLIP_UV;
968                 else G.sima->flag |= SI_CLIP_UV;
969                 break;
970         case 3: /* Limit Stitch UVs */
971                 stitch_uv_tface(1);
972                 break;
973         case 4: /* Stitch UVs */
974                 stitch_uv_tface(0);
975                 break;
976         case 5: /* Proportional Edit (toggle) */
977                 if(G.scene->proportional)
978                         G.scene->proportional= 0;
979                 else
980                         G.scene->proportional= 1;
981                 break;
982         case 7: /* UVs Snap to Pixel */
983                 G.sima->flag ^= SI_PIXELSNAP;
984                 break;
985     case 8:
986                 pin_tface_uv(1);
987                 break;
988     case 9:
989                 pin_tface_uv(0);
990                 break;
991     case 10:
992                 unwrap_lscm(0);
993                 break;
994         case 11:
995                 if(G.sima->flag & SI_LIVE_UNWRAP) G.sima->flag &= ~SI_LIVE_UNWRAP;
996                 else G.sima->flag |= SI_LIVE_UNWRAP;
997                 break;
998         case 12:
999                 minimize_stretch_tface_uv();
1000                 break;
1001         }
1002 }
1003
1004 static uiBlock *image_uvsmenu(void *arg_unused)
1005 {
1006         uiBlock *block;
1007         short yco= 0, menuwidth=120;
1008
1009         block= uiNewBlock(&curarea->uiblocks, "image_uvsmenu", UI_EMBOSSP, UI_HELV, curarea->headwin);
1010         uiBlockSetButmFunc(block, do_image_uvsmenu, NULL);
1011
1012         // uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Transform Properties...|N", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 0, "");
1013         // uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
1014         if(G.sima->flag & SI_PIXELSNAP) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Snap to Pixels|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
1015         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Snap to Pixels|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 7, "");
1016
1017         if(G.sima->flag & SI_BE_SQUARE) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Quads Constrained Rectangular|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
1018         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Quads Constrained Rectangular|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 1, "");
1019         
1020         if(G.sima->flag & SI_CLIP_UV) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Layout Clipped to Image Size|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
1021         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Layout Clipped to Image Size|", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 2, "");
1022
1023         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
1024
1025         if(G.sima->flag & SI_LIVE_UNWRAP) uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Live Unwrap Transform", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 11, "");
1026         else uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Live Unwrap Transform", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 11, "");
1027         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Unwrap|E", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 10, "");
1028         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Unpin|Alt P", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 9, "");
1029         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Pin|P", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 8, "");
1030
1031         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
1032
1033         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Minimize Stretch|Ctrl V", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 12, "");
1034         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Limit Stitch...|Shift V", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 3, "");
1035         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Stitch|V", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
1036         uiDefIconTextBlockBut(block, image_uvs_transformmenu, NULL, ICON_RIGHTARROW_THIN, "Transform", 0, yco-=20, 120, 19, "");
1037
1038         uiDefBut(block, SEPR, 0, "",                            0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
1039         
1040         uiDefIconTextBlockBut(block, image_uvs_mirrormenu, NULL, ICON_RIGHTARROW_THIN, "Mirror", 0, yco-=20, 120, 19, "");
1041         uiDefIconTextBlockBut(block, image_uvs_weldalignmenu, NULL, ICON_RIGHTARROW_THIN, "Weld/Align", 0, yco-=20, 120, 19, "");
1042         
1043         uiDefBut(block, SEPR, 0, "",                            0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
1044
1045         if(G.scene->proportional)
1046                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_HLT, "Proportional Editing|O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 5, "");
1047         else
1048                 uiDefIconTextBut(block, BUTM, 1, ICON_CHECKBOX_DEHLT, "Proportional Editing|O", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 5, "");
1049
1050         uiDefIconTextBlockBut(block, image_uvs_propfalloffmenu, NULL, ICON_RIGHTARROW_THIN, "Proportional Falloff", 0, yco-=20, 120, 19, "");
1051
1052         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
1053
1054         uiDefIconTextBlockBut(block, image_uvs_showhidemenu, NULL, ICON_RIGHTARROW_THIN, "Show/Hide Faces", 0, yco-=20, menuwidth, 19, "");
1055
1056         uiDefBut(block, SEPR, 0, "", 0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");        
1057         
1058         uiDefIconTextBlockBut(block, image_uvs_scriptsmenu, NULL, ICON_RIGHTARROW_THIN, "Scripts", 0, yco-=20, 120, 19, "");
1059         
1060         if(curarea->headertype==HEADERTOP) {
1061                 uiBlockSetDirection(block, UI_DOWN);
1062         }
1063         else {
1064                 uiBlockSetDirection(block, UI_TOP);
1065                 uiBlockFlipOrder(block);
1066         }
1067
1068         uiTextBoundsBlock(block, 50);
1069
1070         return block;
1071 }
1072
1073 void image_buttons(void)
1074 {
1075         Image *ima;
1076         ImBuf *ibuf;
1077         uiBlock *block;
1078         short xco, xmax;
1079         char naam[256], *menuname;
1080         /* This should not be a static var */
1081         static int headerbuttons_packdummy;
1082
1083         headerbuttons_packdummy = 0;
1084                 
1085         sprintf(naam, "header %d", curarea->headwin);
1086         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->headwin);
1087
1088         if(area_is_active_area(curarea)) uiBlockSetCol(block, TH_HEADER);
1089         else uiBlockSetCol(block, TH_HEADERDESEL);
1090
1091         what_image(G.sima);
1092         ima= G.sima->image;
1093         ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1094
1095         curarea->butspacetype= SPACE_IMAGE;
1096
1097         xco = 8;
1098         uiDefIconTextButC(block, ICONTEXTROW,B_NEWSPACE, ICON_VIEW3D, windowtype_pup(), xco,0,XIC+10,YIC, &(curarea->butspacetype), 1.0, SPACEICONMAX, 0, 0, "Current Window Type. Click for menu of available types.");
1099         xco+= XIC+14;
1100
1101         uiBlockSetEmboss(block, UI_EMBOSSN);
1102         if(curarea->flag & HEADER_NO_PULLDOWN) {
1103                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, ICON_DISCLOSURE_TRI_RIGHT,
1104                                 xco,2,XIC,YIC-2,
1105                                 &(curarea->flag), 0, 0, 0, 0, "Show pulldown menus");
1106         } else {
1107                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, ICON_DISCLOSURE_TRI_DOWN,
1108                                 xco,2,XIC,YIC-2,
1109                                 &(curarea->flag), 0, 0, 0, 0, "Hide pulldown menus");
1110         }
1111         uiBlockSetEmboss(block, UI_EMBOSS);
1112         xco+=XIC;
1113
1114         if((curarea->flag & HEADER_NO_PULLDOWN)==0) {
1115                 
1116                 /* pull down menus */
1117                 uiBlockSetEmboss(block, UI_EMBOSSP);
1118         
1119                 xmax= GetButStringLength("View");
1120                 uiDefPulldownBut(block, image_viewmenu, NULL, "View", xco, -2, xmax-3, 24, "");
1121                 xco+= xmax;
1122                 
1123                 if((G.f & G_FACESELECT) && !(ima && (G.sima->flag & SI_DRAWTOOL))) {
1124                         xmax= GetButStringLength("Select");
1125                         uiDefPulldownBut(block, image_selectmenu, NULL, "Select", xco, -2, xmax-3, 24, "");
1126                         xco+= xmax;
1127                 }
1128                 
1129                 if (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
1130                         menuname= "Image*";
1131                 else
1132                         menuname= "Image";
1133                 xmax= GetButStringLength(menuname);
1134                 uiDefPulldownBut(block, image_imagemenu, NULL, menuname, xco, -2, xmax-3, 24, "");
1135                 xco+= xmax;
1136                 
1137                 if((G.f & G_FACESELECT) && !(ima && (G.sima->flag & SI_DRAWTOOL))) {
1138                         xmax= GetButStringLength("UVs");
1139                         uiDefPulldownBut(block, image_uvsmenu, NULL, "UVs", xco, -2, xmax-3, 24, "");
1140                         xco+= xmax;
1141                 }
1142         }
1143         
1144         /* other buttons: */
1145         uiBlockSetEmboss(block, UI_EMBOSS);
1146
1147         xco= std_libbuttons(block, xco, 0, 0, NULL, B_SIMABROWSE, ID_IM, 0, (ID *)ima, 0, &(G.sima->imanr), 0, 0, B_IMAGEDELETE, 0, 0);
1148
1149         if (ima) {
1150                 RenderResult *rr= BKE_image_get_renderresult(ima);
1151                 
1152                 xco+= 8;
1153         
1154                 if(rr) {
1155                         uiBlockBeginAlign(block);
1156                         uiblock_layer_pass_buttons(block, rr, &G.sima->iuser, B_REDR, xco, 0, 160);
1157                         uiBlockEndAlign(block);
1158                         xco+= 166;
1159                 }
1160                 if( !ELEM3(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE, IMA_SRC_VIEWER) && ima->ok) {
1161
1162                         if (ima->packedfile) {
1163                                 headerbuttons_packdummy = 1;
1164                         }
1165                         if (ima->packedfile && ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
1166                                 uiDefIconButBitI(block, TOG, 1, B_SIMA_REPACK, ICON_UGLYPACKAGE,        xco,0,XIC,YIC, &headerbuttons_packdummy, 0, 0, 0, 0, "Re-Pack this image as PNG");
1167                         else
1168                                 uiDefIconButBitI(block, TOG, 1, B_SIMAPACKIMA, ICON_PACKAGE,    xco,0,XIC,YIC, &headerbuttons_packdummy, 0, 0, 0, 0, "Pack/Unpack this image");
1169                                 
1170                         xco+= XIC+8;
1171                 }
1172                 
1173                 uiDefIconButBitI(block, TOG, SI_DRAWTOOL, B_SIMAGEPAINTTOOL, ICON_TPAINT_HLT, xco,0,XIC,YIC, &G.sima->flag, 0, 0, 0, 0, "Enables painting textures on the image with left mouse button");
1174                 xco+= XIC+8;
1175
1176                 uiBlockBeginAlign(block);
1177                 if(ibuf==NULL || ibuf->channels==4) {
1178                         uiDefIconButBitI(block, TOG, SI_USE_ALPHA, B_SIMA_USE_ALPHA, ICON_TRANSP_HLT, xco,0,XIC,YIC, &G.sima->flag, 0, 0, 0, 0, "Draws image with alpha");
1179                         xco+= XIC;
1180                         uiDefIconButBitI(block, TOG, SI_SHOW_ALPHA, B_SIMA_SHOW_ALPHA, ICON_DOT, xco,0,XIC,YIC, &G.sima->flag, 0, 0, 0, 0, "Draws only alpha");
1181                         xco+= XIC;
1182                 }
1183                 if(ibuf) {
1184                         if(ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1)) {
1185                                 uiDefIconButBitI(block, TOG, SI_SHOW_ZBUF, B_SIMA_SHOW_ZBUF, ICON_SOLID, xco,0,XIC,YIC, &G.sima->flag, 0, 0, 0, 0, "Draws zbuffer values");
1186                                 xco+= XIC;
1187                         }
1188                         else G.sima->flag &= ~SI_SHOW_ZBUF;     /* no confusing display for non-zbuf images */
1189                 }               
1190                 xco+= 8;
1191                 
1192                 uiBlockBeginAlign(block);
1193                 if(ima->type==IMA_TYPE_COMPOSITE) {
1194                         uiDefIconBut(block, BUT, B_SIMA_RECORD, ICON_REC,  xco, 0, XIC, YIC, 0, 0, 0, 0, 0, "Record Composite");
1195                         xco+= XIC;
1196                 }
1197                 if((ima->type==IMA_TYPE_COMPOSITE) || ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
1198                         uiDefIconBut(block, BUT, B_SIMA_PLAY, ICON_PLAY, xco, 0, XIC, YIC, 0, 0, 0, 0, 0, "Play");
1199                         xco+= XIC;
1200                 }
1201                 uiBlockEndAlign(block);
1202                 xco+= 8;
1203         }
1204
1205         /* draw LOCK */
1206         uiDefIconButS(block, ICONTOG, 0, ICON_UNLOCKED, xco,0,XIC,YIC, &(G.sima->lock), 0, 0, 0, 0, "Updates other affected window spaces automatically to reflect changes in real time");
1207
1208         /* Always do this last */
1209         curarea->headbutlen= xco+2*XIC;
1210         
1211         uiDrawBlock(block);
1212 }
1213