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