55feb4c030557f7368674d97a201dd2bd3d86199
[blender-staging.git] / source / blender / src / toolbox.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #define PY_TOOLBOX 1
31
32 #include <math.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdarg.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifndef WIN32
43 #include <unistd.h>
44 #else
45 #include <io.h>
46 #endif   
47
48 #include <fcntl.h>
49 #include "MEM_guardedalloc.h"
50
51 #include "BMF_Api.h"
52 #include "BIF_language.h"
53 #include "BIF_resources.h"
54
55 #include "DNA_group_types.h"
56 #include "DNA_image_types.h"
57 #include "DNA_lamp_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_node_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_userdef_types.h"
65 #include "DNA_view3d_types.h"
66 #include "DNA_camera_types.h"
67
68 #include "BLI_blenlib.h"
69 #include "BLI_arithb.h"
70
71 #include "BKE_displist.h"
72 #include "BKE_depsgraph.h"
73 #include "BKE_global.h"
74 #include "BKE_image.h"
75 #include "BKE_library.h"
76 #include "BKE_mesh.h"
77 #include "BKE_node.h"
78 #include "BKE_main.h"
79 #include "BKE_plugin_types.h"
80 #include "BKE_utildefines.h"
81
82 #include "BIF_editnla.h"
83 #include "BIF_editarmature.h"
84 #include "BIF_editdeform.h"
85 #include "BIF_editfont.h"
86 #include "BIF_editmesh.h"
87 #include "BIF_editseq.h"
88 #include "BIF_editlattice.h"
89 #include "BIF_editsima.h"
90 #include "BIF_editoops.h"
91 #include "BIF_editview.h"
92 #include "BIF_gl.h"
93 #include "BIF_graphics.h"
94 #include "BIF_imasel.h"
95 #include "BIF_interface.h"
96 #include "BIF_keyframing.h"
97 #include "BIF_mainqueue.h"
98 #include "BIF_mywindow.h"
99 #include "BIF_renderwin.h"
100 #include "BIF_screen.h"
101 #include "BIF_space.h"
102 #include "BIF_toolbox.h"
103 #include "BIF_tbcallback.h"
104 #include "BIF_transform.h"
105
106 #include "BDR_editobject.h"
107 #include "BDR_editcurve.h"
108 #include "BDR_editmball.h"
109
110 #include "BSE_drawipo.h"
111 #include "BSE_edit.h"
112 #include "BSE_editipo.h"
113 #include "BSE_filesel.h"
114 #include "BSE_headerbuttons.h"
115 #include "BSE_node.h"
116
117 #include "IMB_imbuf.h"
118
119 #include "blendef.h"
120 #include "butspace.h"
121 #include "mydevice.h"
122
123 /* bpymenu */
124 #include "BPY_extern.h"
125 #include "BPY_menus.h"
126
127 #include "BLO_sys_types.h" // for intptr_t support
128
129 void asciitoraw(int ch, unsigned short *event, unsigned short *qual)
130 {
131         if( isupper(ch) ) {
132                 *qual= LEFTSHIFTKEY;
133                 ch= tolower(ch);
134         }
135         
136         switch(ch) {
137         case 'a': *event= AKEY; break;
138         case 'b': *event= BKEY; break;
139         case 'c': *event= CKEY; break;
140         case 'd': *event= DKEY; break;
141         case 'e': *event= EKEY; break;
142         case 'f': *event= FKEY; break;
143         case 'g': *event= GKEY; break;
144         case 'h': *event= HKEY; break;
145         case 'i': *event= IKEY; break;
146         case 'j': *event= JKEY; break;
147         case 'k': *event= KKEY; break;
148         case 'l': *event= LKEY; break;
149         case 'm': *event= MKEY; break;
150         case 'n': *event= NKEY; break;
151         case 'o': *event= OKEY; break;
152         case 'p': *event= PKEY; break;
153         case 'q': *event= QKEY; break;
154         case 'r': *event= RKEY; break;
155         case 's': *event= SKEY; break;
156         case 't': *event= TKEY; break;
157         case 'u': *event= UKEY; break;
158         case 'v': *event= VKEY; break;
159         case 'w': *event= WKEY; break;
160         case 'x': *event= XKEY; break;
161         case 'y': *event= YKEY; break;
162         case 'z': *event= ZKEY; break;
163         }
164 }
165
166 /* ************************************  */
167
168 /* this va_ stuff allows printf() style codes in these menus */
169
170 static int vconfirm(char *title, char *itemfmt, va_list ap)
171 {
172         char *s, buf[512];
173
174         s= buf;
175         if (title) s+= sprintf(s, "%s%%t|", title);
176         vsprintf(s, itemfmt, ap);
177         
178         return (pupmenu(buf)>=0);
179 }
180
181 static int confirm(char *title, char *itemfmt, ...)
182 {
183         va_list ap;
184         int ret;
185         
186         va_start(ap, itemfmt);
187         ret= vconfirm(title, itemfmt, ap);
188         va_end(ap);
189         
190         return ret;
191 }
192
193 int okee(char *str, ...)
194 {
195         va_list ap;
196         int ret;
197         char titlestr[256];
198         
199         sprintf(titlestr, "OK? %%i%d", ICON_HELP);
200         
201         va_start(ap, str);
202         ret= vconfirm(titlestr, str, ap);
203         va_end(ap);
204         
205         return ret;
206 }
207
208 void notice(char *str, ...)
209 {
210         va_list ap;
211         
212         va_start(ap, str);
213         vconfirm(NULL, str, ap);
214         va_end(ap);
215 }
216
217 void error(char *fmt, ...)
218 {
219         va_list ap;
220         char nfmt[256];
221         char titlestr[256];
222         
223         sprintf(titlestr, "Error %%i%d", ICON_ERROR);
224         
225         sprintf(nfmt, "%s", fmt);
226         
227         va_start(ap, fmt);
228         if (G.background || !G.curscreen) {
229                 vprintf(nfmt, ap);
230                 printf("\n");
231         } else {
232                 vconfirm(titlestr, nfmt, ap);
233         }
234         va_end(ap);
235 }
236
237 void error_libdata(void)
238 {
239         error(ERROR_LIBDATA_MESSAGE);
240 }
241
242 int saveover(char *file)
243 {
244         size_t len= strlen(file);
245         
246         if(len==0) 
247                 return 0;
248         
249         if(BLI_exists(file)==0)
250                 return 1;
251         
252         if( file[len-1]=='/' || file[len-1]=='\\' ) {
253                 error("Cannot overwrite a directory");
254                 return 0;
255         }
256                 
257         return confirm("Save over", file);
258 }
259
260 /* ****************** EXTRA STUFF **************** */
261
262 short button(short *var, short min, short max, char *str)
263 {
264         uiBlock *block;
265         ListBase listb={0, 0};
266         short x1,y1;
267         short mval[2], ret=0;
268
269         if(min>max) min= max;
270
271         getmouseco_sc(mval);
272         
273         if(mval[0]<150) mval[0]=150;
274         if(mval[1]<30) mval[1]=30;
275         if(mval[0]>G.curscreen->sizex) mval[0]= G.curscreen->sizex-10;
276         if(mval[1]>G.curscreen->sizey) mval[1]= G.curscreen->sizey-10;
277
278         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
279         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_ENTER_OK);
280
281         x1=mval[0]-150; 
282         y1=mval[1]-20; 
283         
284         uiDefButS(block, NUM, 0, str,   (short)(x1+5),(short)(y1+10),125,20, var,(float)min,(float)max, 0, 0, "");
285         uiDefBut(block, BUT, 32767, "OK",       (short)(x1+136),(short)(y1+10),25,20, NULL, 0, 0, 0, 0, "");
286
287         uiBoundsBlock(block, 5);
288
289         ret= uiDoBlocks(&listb, 0, 0);
290
291         if(ret==UI_RETURN_OK) return 1;
292         return 0;
293 }
294
295 short sbutton(char *var, short min, short max, char *str)
296 {
297         uiBlock *block;
298         ListBase listb={0, 0};
299         short x1,y1;
300         short mval[2], ret=0;
301         char *editvar = NULL; /* dont edit the original text, incase we cancel the popup */
302         
303         if(min>max) min= max;
304
305         getmouseco_sc(mval);
306         
307         if(mval[0]<250) mval[0]=250;
308         if(mval[1]<30) mval[1]=30;
309         if(mval[0]>G.curscreen->sizex) mval[0]= G.curscreen->sizex-10;
310         if(mval[1]>G.curscreen->sizey) mval[1]= G.curscreen->sizey-10;
311
312         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
313         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_ENTER_OK);
314
315         x1=mval[0]-250; 
316         y1=mval[1]-20; 
317         
318         editvar = MEM_callocN(max+1, "sbutton");
319         BLI_strncpy(editvar, var, max);
320         
321         uiDefButC(block, TEX, 32766, str,       x1+5,y1+10,225,20, editvar,(float)min,(float)max, 0, 0, "");
322         uiDefBut(block, BUT, 32767, "OK",       x1+236,y1+10,25,20, NULL, 0, 0, 0, 0, "");
323
324         uiBoundsBlock(block, 5);
325         
326         mainqenter_ext(BUT_ACTIVATE, 32766, 0); /* note, button id '32766' is asking for errors some day! */
327         ret= uiDoBlocks(&listb, 0, 0);
328
329         if(ret==UI_RETURN_OK) {
330                 BLI_strncpy(var, editvar, max);
331                 MEM_freeN(editvar);
332                 return 1;
333         }
334         MEM_freeN(editvar);
335         return 0;
336         
337 }
338
339 short fbutton(float *var, float min, float max, float a1, float a2, char *str)
340 {
341         uiBlock *block;
342         ListBase listb={0, 0};
343         short x1,y1;
344         short mval[2], ret=0;
345
346         if(min>max) min= max;
347
348         getmouseco_sc(mval);
349         
350         if(mval[0]<150) mval[0]=150;
351         if(mval[1]<30) mval[1]=30;
352         if(mval[0]>G.curscreen->sizex) mval[0]= G.curscreen->sizex-10;
353         if(mval[1]>G.curscreen->sizey) mval[1]= G.curscreen->sizey-10;
354
355         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
356         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
357
358         x1=mval[0]-150; 
359         y1=mval[1]-20; 
360         
361         uiDefButF(block, NUM, 0, str,(short)(x1+5),(short)(y1+10),125,20, var, min, max, a1, a2, "");
362         uiDefBut(block, BUT, 32767, "OK",(short)(x1+136),(short)(y1+10), 35, 20, NULL, 0, 0, 0, 0, "");
363
364         uiBoundsBlock(block, 2);
365
366         ret= uiDoBlocks(&listb, 0, 0);
367
368         if(ret==UI_RETURN_OK) return 1;
369         return 0;
370 }
371
372 int movetolayer_buts(unsigned int *lay, char *title)
373 {
374         uiBlock *block;
375         ListBase listb={0, 0};
376         int dx, dy, a, x1, y1, sizex=160, sizey=30;
377         short pivot[2], mval[2], ret=0;
378         
379         if(G.vd->localview) {
380                 error("Not in localview ");
381                 return ret;
382         }
383
384         getmouseco_sc(mval);
385
386         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
387         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
388         
389         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
390                 warp_pointer(pivot[0], pivot[1]);
391
392         mywinset(G.curscreen->mainwin);
393         
394         x1= pivot[0]-sizex+10; 
395         y1= pivot[1]-sizey/2; 
396
397         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
398         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
399         
400         dx= (sizex-5)/12;
401         dy= sizey/2;
402         
403         if(title)
404                 uiDefBut(block, LABEL, 0, title, (short)(x1), (short)y1+30, sizex, 20, NULL, 1, 0, 0, 0, "");
405         
406         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
407         uiBlockBeginAlign(block);
408         for(a=0; a<5; a++) 
409                 uiDefButBitI(block, TOGR, 1<<a, 0, "",(short)(x1+a*dx),(short)(y1+dy),(short)dx,(short)dy, (int *)lay, 0, 0, 0, 0, "");
410         for(a=0; a<5; a++) 
411                 uiDefButBitI(block, TOGR, 1<<(a+10), 0, "",(short)(x1+a*dx),(short)y1,(short)dx,(short)dy, (int *)lay, 0, 0, 0, 0, "");
412         x1+= 5;
413         
414         uiBlockBeginAlign(block);
415         for(a=5; a<10; a++) 
416                 uiDefButBitI(block, TOGR, 1<<a, 0, "",(short)(x1+a*dx),(short)(y1+dy),(short)dx,(short)dy, (int *)lay, 0, 0, 0, 0, "");
417         for(a=5; a<10; a++) 
418                 uiDefButBitI(block, TOGR, 1<<(a+10), 0, "",(short)(x1+a*dx),(short)y1,(short)dx,(short)dy, (int *)lay, 0, 0, 0, 0, "");
419         uiBlockEndAlign(block);
420
421         x1-= 5;
422         uiDefBut(block, BUT, 32767, "OK", (short)(x1+10*dx+10), (short)y1, (short)(3*dx), (short)(2*dy), NULL, 0, 0, 0, 0, "");
423
424         uiBoundsBlock(block, 2);
425
426         ret= uiDoBlocks(&listb, 0, 0);
427
428         if(ret==UI_RETURN_OK) return 1;
429         return 0;
430 }
431
432 /* armature or bone */
433 int movetolayer_short_buts(short *lay, char *title)
434 {
435         uiBlock *block;
436         ListBase listb={0, 0};
437         int dx, dy, a, x1, y1, sizex=120, sizey=30;
438         short pivot[2], mval[2], ret=0;
439         
440         getmouseco_sc(mval);
441         
442         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
443         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
444         
445         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
446                 warp_pointer(pivot[0], pivot[1]);
447         
448         mywinset(G.curscreen->mainwin);
449         
450         x1= pivot[0]-sizex+10; 
451         y1= pivot[1]-sizey/2; 
452         
453         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
454         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
455         
456         dx= (sizex-5)/10;
457         dy= sizey/2;
458         
459         if(title)
460                 uiDefBut(block, LABEL, 0, title, (short)(x1), (short)y1+30, sizex, 20, NULL, 1, 0, 0, 0, "");
461
462         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
463         uiBlockBeginAlign(block);
464         for(a=0; a<8; a++) 
465                 uiDefButBitS(block, TOGR, 1<<a, 0, "",(short)(x1+a*dx),(short)(y1+dy),(short)dx,(short)dy, lay, 0, 0, 0, 0, "");
466         for(a=0; a<8; a++) 
467                 uiDefButBitS(block, TOGR, 1<<(a+8), 0, "",(short)(x1+a*dx),(short)y1,(short)dx,(short)dy, lay, 0, 0, 0, 0, "");
468         
469         uiBlockEndAlign(block);
470         
471         x1-= 5;
472         uiDefBut(block, BUT, 32767, "OK", (short)(x1+8*dx+10), (short)y1, (short)(3*dx), (short)(2*dy), NULL, 0, 0, 0, 0, "");
473         
474         uiBoundsBlock(block, 2);
475         
476         ret= uiDoBlocks(&listb, 0, 0);
477         
478         if(ret==UI_RETURN_OK) return 1;
479         return 0;
480 }
481
482
483 /* ********************** CLEVER_NUMBUTS ******************** */
484
485 #define MAXNUMBUTS      120
486 #define MAXNUMBUTROWS   8
487
488 VarStruct numbuts[MAXNUMBUTS];
489 void *numbpoin[MAXNUMBUTS];
490 int numbdata[MAXNUMBUTS];
491
492 void draw_numbuts_tip(char *str, int x1, int y1, int x2, int y2)
493 {
494         static char *last=0;    /* avoid ugly updates! */
495         int temp;
496         
497         if(str==last) return;
498         last= str;
499         if(str==0) return;
500
501         glColor3ub(160, 160, 160); /* MGREY */
502         glRecti(x1+4,  y2-36,  x2-4,  y2-16);
503
504         cpack(0x0);
505
506         temp= 0;
507         while( BIF_GetStringWidth(G.fonts, str+temp, (U.transopts & USER_TR_BUTTONS))>(x2 - x1-24)) temp++;
508         glRasterPos2i(x1+16, y2-30);
509         BIF_DrawString(G.fonts, str+temp, (U.transopts & USER_TR_BUTTONS));
510 }
511
512 int do_clever_numbuts(char *name, int tot, int winevent)
513 {
514         ListBase listb= {NULL, NULL};
515         uiBlock *block;
516         VarStruct *varstr;
517         int a, sizex, sizey, x1, y2, width, colunms=1, xi=0, yi=0;
518         short mval[2], event;
519         
520         /* Clear all events so tooltips work, this is not ideal and
521         only needed because calls from the menu still have some events
522         left over when do_clever_numbuts is called.
523         Calls from keyshortcuts do not have this problem.*/
524         ScrArea *sa;
525         BWinEvent temp_bevt;
526         for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
527                 if(sa->win) {
528                         while( bwin_qread( sa->win, &temp_bevt ) ) {}
529                 }
530                 if(sa->headwin) {
531                         while( bwin_qread( sa->headwin, &temp_bevt ) ) {}
532                 }
533         }
534         /* Done clearing events */
535         
536         if(tot<=0 || tot>MAXNUMBUTS) return 0;
537         
538         /* if we have too many buttons then have more then 1 column */
539         colunms= (int)ceil((double)tot / (double)MAXNUMBUTROWS);
540         
541         getmouseco_sc(mval);
542
543         /* size */
544         sizex= 175;
545         sizey= 30+20*(MIN2(MAXNUMBUTROWS, tot)+1);
546         width= (sizex*colunms)+60;
547         
548         /* center */
549         if(mval[0]<width/2) mval[0]=width/2;
550         if(mval[1]<sizey/2) mval[1]=sizey/2;
551         if(mval[0]>G.curscreen->sizex -width/2) mval[0]= G.curscreen->sizex -width/2;
552         if(mval[1]>G.curscreen->sizey -sizey/2) mval[1]= G.curscreen->sizey -sizey/2;
553
554         mywinset(G.curscreen->mainwin);
555         
556         x1= mval[0]-width/2; 
557         y2= mval[1]+sizey/2;
558         
559         block= uiNewBlock(&listb, "numbuts", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
560         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_ENTER_OK);
561         
562         /* WATCH IT: TEX BUTTON EXCEPTION */
563         /* WARNING: ONLY A SINGLE BIT-BUTTON POSSIBLE: WE WORK AT COPIED DATA! */
564         
565         BIF_ThemeColor(TH_MENU_TEXT); /* makes text readable on dark theme */
566         
567         uiDefBut(block, LABEL, 0, name, (short)(x1+15), (short)(y2-35), (short)(width-60), 19, 0, 1.0, 0.0, 0, 0, ""); 
568         
569         /*
570         if(name[0]=='A' && name[7]=='O') {
571                 y2 -= 20;
572                 uiDefBut(block, LABEL, 0, "Rotations in degrees!",      (short)(x1+15), (short)(y2-35), (short)(sizex-60), 19, 0, 0.0, 0.0, 0, 0, "");
573         }*/
574         
575         uiBlockBeginAlign(block);
576         varstr= &numbuts[0];
577         for(a=0; a<tot; a++, varstr++) {
578                 
579                 if(varstr->type==TEX) {
580                         uiDefBut(block, TEX, 0, varstr->name,(short)((x1+15) + (sizex*xi)),(short)(y2-55- 20*yi),(short)(sizex), 19, numbpoin[a], varstr->min, varstr->max, 0, 0, varstr->tip);
581                 } else if(varstr->type==COL) {
582                         uiDefButF(block, COL, 0, "",(short)((x1+15) + (sizex*xi)),(short)(y2-55- 20*yi),(short)(sizex), 19, numbpoin[a], varstr->min, varstr->max, 0, 0, "");
583                 } else  {
584                         if(varstr->type==LABEL) {/* dont include the label when rounding the buttons */
585                                 uiBlockEndAlign(block);
586                         
587                                 /* using the tip for the name, this is incorrect lets us get around the 16 char limit of name */
588                                 /* Changed from the line below to use the tip since the tip isnt used for a label */
589                                 uiDefBut(block, varstr->type, 0, varstr->tip,(short)((x1+15) + (sizex*xi)),(short)(y2-55-20*yi), (short)(sizex), 19, &(numbdata[a]), varstr->min, varstr->max, 100, 0, "");
590                         } else {
591                                 uiDefBut(block, varstr->type, 0, varstr->name,(short)((x1+15) + (sizex*xi)),(short)(y2-55-20*yi), (short)(sizex), 19, &(numbdata[a]), varstr->min, varstr->max, 100, 0, varstr->tip);
592                         }
593                         
594                         if(varstr->type==LABEL)
595                                 uiBlockBeginAlign(block);
596                 }
597                 
598                 /* move to the next column */
599                 yi++;
600                 if (yi>=MAXNUMBUTROWS) {
601                         yi=0;
602                         xi++;
603                         uiBlockEndAlign(block);
604                         uiBlockBeginAlign(block);
605                 }
606         }
607         uiBlockEndAlign(block);
608
609         uiDefBut(block, BUT, 4000, "OK", (short)(x1+width-40),(short)(y2-35-20*MIN2(MAXNUMBUTROWS,a)), 25, (short)(sizey-50), 0, 0, 0, 0, 0, "OK: Assign Values");
610         
611         uiBoundsBlock(block, 5);
612
613         event= uiDoBlocks(&listb, 0, 0);
614
615         areawinset(curarea->win);
616         
617         if(event & UI_RETURN_OK) {
618                 
619                 varstr= &numbuts[0];
620                 for(a=0; a<tot; a++, varstr++) {
621                         if(varstr->type==TEX);
622                         else if ELEM( (varstr->type & BUTPOIN), FLO, INT ) memcpy(numbpoin[a], numbdata+a, 4);
623                         else if((varstr->type & BUTPOIN)==SHO ) *((short *)(numbpoin[a]))= *( (short *)(numbdata+a));
624                 }
625                 
626                 if(winevent) {
627                         ScrArea *sa;
628                 
629                         sa= G.curscreen->areabase.first;
630                         while(sa) {
631                                 if(sa->spacetype==curarea->spacetype) addqueue(sa->win, winevent, 1);
632                                 sa= sa->next;
633                         }
634                 }
635                 
636                 return 1;
637         }
638         return 0;
639 }
640
641 void add_numbut(int nr, int type, char *str, float min, float max, void *poin, char *tip)
642 {
643         int tip_max = sizeof(numbuts[nr].tip);
644         int name_max = sizeof(numbuts[nr].name);
645
646         if(nr >= MAXNUMBUTS || (nr < 0)) return;
647
648         numbuts[nr].type= type;
649         
650         numbuts[nr].min= min;
651         numbuts[nr].max= max;
652         
653         if (type==LABEL) {
654                 /* evil use it tooltip for the label string to get around the 16 char limit of "name" */
655                 if (str) {
656                         strncpy(numbuts[nr].tip, str, tip_max);
657                         numbuts[nr].tip[tip_max-1] = 0;
658                 } else {
659                         strcpy(numbuts[nr].tip, "");
660                 }
661         } else {
662                 /* for all other types */
663                 if (str) {
664                         strncpy(numbuts[nr].name, str, name_max);
665                         numbuts[nr].name[name_max-1] = 0;
666                 } else {
667                         strcpy(numbuts[nr].name, "");
668                 }
669                 if (tip) {
670                         strncpy(numbuts[nr].tip, tip, tip_max);
671                         numbuts[nr].tip[tip_max-1] = 0;
672                 } else {
673                         strcpy(numbuts[nr].tip, "");
674                 }
675         }
676         
677         /*WATCH: TEX BUTTON EXCEPTION */
678         
679         numbpoin[nr]= poin;
680         
681         if ELEM( (type & BUTPOIN), FLO, INT ) memcpy(numbdata+nr, poin, 4);
682         if((type & BUTPOIN)==SHO ) *((short *)(numbdata+nr))= *( (short *)poin);
683         
684         /* if( strncmp(numbuts[nr].name, "Rot", 3)==0 ) {
685                 float *fp;
686                 
687                 fp= (float *)(numbdata+nr);
688                 fp[0]= 180.0*fp[0]/M_PI;
689         } */
690
691 }
692
693 void clever_numbuts(void)
694 {
695         
696         if(curarea->spacetype==SPACE_VIEW3D) {
697                 // panel now
698         }
699         else if(curarea->spacetype==SPACE_NLA){
700                 // panel now
701         }
702         else if(curarea->spacetype==SPACE_IPO) {
703                 // panel now
704         }
705         else if(curarea->spacetype==SPACE_SEQ) {
706                 // panel now
707         }
708         else if(curarea->spacetype==SPACE_IMAGE) {
709                 // panel now
710         }
711         else if(curarea->spacetype==SPACE_IMASEL) {
712                 clever_numbuts_imasel();
713         }
714         else if(curarea->spacetype==SPACE_OOPS) {
715                 clever_numbuts_oops();
716         }
717         else if(curarea->spacetype==SPACE_ACTION){
718                 // in its own queue
719         }
720         else if(curarea->spacetype==SPACE_FILE) {
721                 clever_numbuts_filesel();
722         }
723 }
724
725
726 void replace_names_but(void)
727 {
728         Image *ima= G.main->image.first;
729         short len, tot=0;
730         char old[64], new[64], temp[80];
731         
732         strcpy(old, "/");
733         strcpy(new, "/");
734         
735         add_numbut(0, TEX, "Old:", 0, 63, old, 0);
736         add_numbut(1, TEX, "New:", 0, 63, new, 0);
737
738         if (do_clever_numbuts("Replace image name", 2, REDRAW) ) {
739                 
740                 len= strlen(old);
741                 
742                 while(ima) {
743                         
744                         if(strncmp(old, ima->name, len)==0) {
745                                 
746                                 strcpy(temp, new);
747                                 strcat(temp, ima->name+len);
748                                 BLI_strncpy(ima->name, temp, sizeof(ima->name));
749                                 
750                                 BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
751                                 
752                                 tot++;
753                         }
754                         
755                         ima= ima->id.next;
756                 }
757
758                 notice("Replaced %d names", tot);
759         }
760         
761 }
762
763
764 /* ********************** NEW TOOLBOX ********************** */
765
766 ListBase tb_listb= {NULL, NULL};
767
768 #define TB_TAB  256
769 #define TB_ALT  512
770 #define TB_CTRL 1024
771 #define TB_PAD  2048
772 #define TB_SHIFT 4096
773
774 static void tb_do_hotkey(void *arg, int event)
775 {
776         unsigned short i, key=0;
777         unsigned short qual[] = { 0,0,0,0 };
778         
779         if(event & TB_CTRL) {
780                 qual[0] = LEFTCTRLKEY;
781                 event &= ~TB_CTRL;
782         }
783         if(event & TB_ALT) {
784                 qual[1] = LEFTALTKEY;
785                 event &= ~TB_ALT;
786         }
787         if(event & TB_SHIFT) {
788                 qual[2] = LEFTSHIFTKEY;
789                 event &= ~TB_SHIFT;
790         }
791         
792         if(event & TB_TAB) key= TABKEY;
793         else if(event & TB_PAD) {
794                 event &= ~TB_PAD;
795                 switch(event) {
796                 case '-': key= PADMINUS; break;
797                 case '+': key= PADPLUSKEY; break;
798                 case '0': key= PAD0; break;
799                 case '5': key= PAD5; break;
800                 case '/': key= PADSLASHKEY; break;
801                 case '.': key= PADPERIOD; break;
802                 case '*': key= PADASTERKEY; break;
803                 case 'h': key= HOMEKEY; break;
804                 case 'u': key= PAGEUPKEY; break;
805                 case 'd': key= PAGEDOWNKEY; break;
806                 }
807         }
808         else if (isalpha(event))
809                 asciitoraw(event, &key, &qual[3]);
810         else if (event == '~')
811                 key = ACCENTGRAVEKEY;
812
813         for (i=0;i<4;i++)
814         {
815                 if(qual[i]) mainqenter(qual[i], 1);
816         }
817         mainqenter(key, 1);
818         mainqenter(key, 0);
819         mainqenter(EXECUTE, 1);
820
821         for (i=0;i<4;i++)
822         {
823                 if(qual[i]) mainqenter(qual[i], 0);
824         }
825 }
826
827 /* *************Select ********** */
828
829 static TBitem tb_object_select_layer1_5[]= {
830 {       0, "1",         1, NULL},
831 {       0, "2",         2, NULL},
832 {       0, "3",         3, NULL},
833 {       0, "4",         4, NULL},
834 {       0, "5",         5, NULL},
835 {  -1, "",              0, do_view3d_select_object_layermenu}};
836
837 static TBitem tb_object_select_layer6_10[]= {
838 {       0, "6",         6, NULL},
839 {       0, "7",         7, NULL},
840 {       0, "8",         8, NULL},
841 {       0, "9",         9, NULL},
842 {       0, "10",        10, NULL},
843 {  -1, "",              0, do_view3d_select_object_layermenu}};
844
845 static TBitem tb_object_select_layer11_15[]= {
846 {       0, "11",        11, NULL},
847 {       0, "12",        12, NULL},
848 {       0, "13",        13, NULL},
849 {       0, "14",        14, NULL},
850 {       0, "15",        15, NULL},
851 {  -1, "",              0, do_view3d_select_object_layermenu}};
852
853 static TBitem tb_object_select_layer16_20[]= {
854 {       0, "16",        16, NULL},
855 {       0, "17",        17, NULL},
856 {       0, "18",        18, NULL},
857 {       0, "19",        19, NULL},
858 {       0, "20",        20, NULL},
859 {  -1, "",              0, do_view3d_select_object_layermenu}};
860
861 static TBitem tb_object_select_layer[]= {
862 {       0, "Layers 1-5",        0,              tb_object_select_layer1_5},
863 {       0, "Layers 6-10",       0,              tb_object_select_layer6_10},
864 {       0, "Layers 11-15",      0,              tb_object_select_layer11_15},
865 {       0, "Layers 16-20",      0,              tb_object_select_layer16_20},
866 {  -1, "",                      0, tb_do_hotkey}};
867
868 static TBitem tb_object_select_type[]= {
869 {       0, "Mesh",              1, NULL},
870 {       0, "Curve",     2, NULL},
871 {       0, "Surface",   3, NULL},
872 {       0, "Meta",              4, NULL},
873 {       0, "SEPR",              0, NULL},
874 {       0, "Armature",  5, NULL},
875 {       0, "Lattice",   6, NULL},
876 {       0, "Text",              7, NULL},
877 {       0, "Empty",     8, NULL},
878 {       0, "SEPR",              0, NULL},
879 {       0, "Camera",    9, NULL},
880 {       0, "Lamp",              10, NULL},
881 {  -1, "",                      0, do_view3d_select_object_typemenu}};
882
883 static TBitem tb_object_select_linked[]= {
884 {       0, "Object Ipo|Shift L, 1",     1, NULL},
885 {       0, "ObData|Shift L, 2",         2, NULL},
886 {       0, "Material|Shift L, 3",       3, NULL},
887 {       0, "Texture|Shift L, 4",        4, NULL},
888 {  -1, "",                      0, do_view3d_select_object_linkedmenu}};
889
890 static TBitem tb_object_select_grouped[]= {
891 {       0, "Children|Shift G, 1",       1, NULL},
892 {       0, "Immediate Children|Shift G, 2",     2, NULL},
893 {       0, "Parent|Shift G, 3",         3, NULL},
894 {       0, "Siblings (Shared Parent)|Shift G, 4",       4, NULL},
895 {       0, "Objects of Same Type|Shift G, 5",   5, NULL},
896 {       0, "Objects on Shared Layers|Shift G, 6",       6, NULL},
897 {       0, "Objects in Same Group|Shift G, 7",  7, NULL},
898 {       0, "Object Hooks|Shift G, 8",   8, NULL},
899 {       0, "Object PassIndex|Shift G, 9",       9, NULL},
900 {       0, "Object Color|Shift G, 0",   9, NULL},
901 {       0, "Game Properties|Shift G, Alt+1",    9, NULL},
902 {  -1, "",                      0, do_view3d_select_object_groupedmenu}};
903
904 static TBitem tb_object_select[]= {
905 {       0, "Border Select|B",   0, NULL},
906 {       0, "SEPR",                              0, NULL},
907 {       0, "Select/Deselect All|A",     1, NULL},
908 {       0, "Inverse",                   2, NULL},
909 {       0, "Random",                    3, NULL},
910 {       0, "Select All by Layer",       0,              tb_object_select_layer},
911 {       0, "Select All by Type",        0,              tb_object_select_type},
912 {       0, "SEPR",                              0, NULL},
913 {       0, "Linked",    0,      tb_object_select_linked},
914 {       0, "Grouped",   0,      tb_object_select_grouped},
915 {  -1, "",                      0, do_view3d_select_objectmenu}};
916
917 static TBitem tb_face_select[]= {
918 {       0, "Border Select|B",           0, NULL},
919 {       0, "SEPR",                                      0, NULL},
920 {       0, "Select/Deselect All|A", 2, NULL},
921 {       0, "Inverse",                           3, NULL},
922 {       0, "Same UV",                           4, NULL},
923 {       0, "SEPR",                                      0, NULL},
924 {       0, "Linked Faces|Ctrl L",       5, NULL},
925 {  -1, "",                                              0, do_view3d_select_faceselmenu}};
926
927 static TBitem tb_mesh_select[]= {
928 {       0, "Border Select|B",               0, NULL},
929 {       0, "SEPR",                          0, NULL},
930 {       0, "Select/Deselect All|A",              2, NULL},
931 {       0, "Inverse|Ctrl I",                       3, NULL},
932 {       0, "SEPR",                          0, NULL},
933 {       0, "Random...",                             5, NULL},
934 {       0, "Non-Manifold|Shift Ctrl Alt M", 9, NULL},
935 {       0, "Sharp Edges|Shift Ctrl Alt S", 14, NULL},
936 {       0, "Linked Flat Faces|Shift Ctrl Alt F", 15, NULL},
937 {       0, "Triangles|Shift Ctrl Alt 3",    11, NULL},
938 {       0, "Quads|Shift Ctrl Alt 4",        12, NULL},
939 {       0, "Non-Triangles/Quads|Shift Ctrl Alt 5", 13, NULL},
940 {       0, "Similar to Selection|Shift G", 21, NULL},
941 {       0, "SEPR",                          0, NULL},
942 {       0, "More|Ctrl NumPad +",            7, NULL},
943 {       0, "Less|Ctrl NumPad -",            8, NULL},
944 {       0, "SEPR",                          0, NULL},
945 {       0, "Linked Vertices|Ctrl L",        4, NULL},
946 {       0, "Vertex Path|W Alt 7",               16, NULL},
947 {       0, "Edge Loop|Ctrl E 6",                17, NULL},
948 {       0, "Edge Ring|Ctrl E 7",                18, NULL},
949 {       0, "SEPR",                          0, NULL},
950 {       0, "Loop to Region|Ctrl E 8",           19, NULL},
951 {       0, "Region to Loop|Ctrl E 9",           20, NULL},
952 {  -1, "",                      0, do_view3d_select_meshmenu}};
953
954
955 static TBitem tb_curve_select[]= {
956 {       0, "Border Select|B",   0, NULL},
957 {       0, "SEPR",                              0, NULL},
958 {       0, "(De)select All|A",  2, NULL},
959 {       0, "Inverse",                   3, NULL},
960 {       0, "Random...",                 13, NULL},
961 {       0, "Every Nth",                 14, NULL},
962 {       0, "Row|Shift R",                       5, NULL}, /* shouldn't be visible in case of bezier curves*/
963 {       0, "SEPR",                              0, NULL},
964 {       0, "(De)select First",  7, NULL},
965 {       0, "(De)select Last",   8, NULL},
966 {       0, "Select Next",               11, NULL},
967 {       0, "Select Previous",   12, NULL},
968 {       0, "SEPR",                              0, NULL},
969 {       0, "More|Ctrl NumPad +",        9, NULL},
970 {       0, "Less|Ctrl NumPad -",        10, NULL},
971 {  -1, "",                              0, do_view3d_select_curvemenu}};
972
973 static TBitem tb_mball_select[]= {
974 {       0, "Border Select|B",   0, NULL},
975 {       0, "SEPR",              0, NULL},
976 {       0, "(De)select All|A",  2, NULL},
977 {       0, "Inverse",           3, NULL},
978 {       0, "SEPR",              0, NULL},
979 {       0, "Random...",         4, NULL},
980 {  -1, "",                      0, do_view3d_select_metaballmenu}};
981
982 static TBitem tb__select[]= {
983 {       0, "Border Select|B",   'b', NULL},
984 {       0, "(De)select All|A",  'a', NULL},
985 {  -1, "",                      0, tb_do_hotkey}};
986
987
988 /* *************Edit ********** */
989
990 static TBitem tb_edit[]= {
991 {       0, "Exit Editmode|Tab",         TB_TAB, NULL},
992 {  -1, "",                      0, tb_do_hotkey}};
993
994 static TBitem tb_curve_edit_seg[]= {
995 {       0, "Subdivide|W, 1",            0, NULL},
996 {       0, "Switch Direction|W, 2",     1, NULL},
997 {  -1, "",                      0, do_view3d_edit_curve_segmentsmenu}};
998
999 static TBitem tb_curve_edit_cv[]= {
1000 {       0, "Tilt|T",    't', NULL},
1001 {       0, "Clear Tilt|Alt T",                  TB_ALT|'t', NULL},
1002 {       0, "Separate|P",        'p', NULL},
1003 {       0, "SEPR",                                                              0, NULL},
1004 {       0, "Automatic|Shift H",                 'H', NULL},
1005 {       0, "Toggle Free/Aligned|H",     'h', NULL},
1006 {       0, "Vector|V",                                  'v', NULL},
1007 {       0, "SEPR",                                                              0, NULL},
1008 {       0, "Make Vertex Parent|Ctrl P", TB_CTRL|'p', NULL},
1009 {       0, "Add Hook|Ctrl H",                   TB_CTRL|'h', NULL},
1010 {  -1, "",                      0, tb_do_hotkey}};
1011
1012
1013 static TBitem tb_curve_edit[]= {
1014 {       0, "Exit Editmode|Tab",         TB_TAB, NULL},
1015 {       0, "SEPR",                                                              0, NULL},
1016 {       0, "Extrude|E",                 'e',            NULL},
1017 {       0, "Duplicate|Shift D", 'D',            NULL},
1018 {       0, "Make Segment|F",    'f',            NULL},
1019 {       0, "Toggle Cyclic|C",   'c',            NULL},
1020 {       0, "Delete...|X",               'x',            NULL},
1021 {       0, "SEPR",                                                              0, NULL},
1022 {       0, "Control Points",    0,              tb_curve_edit_cv},
1023 {       0, "Segments",  0,              tb_curve_edit_seg},
1024 {  -1, "",                      0, tb_do_hotkey}};
1025
1026
1027 static TBitem tb_mesh_edit_vertex[]= {
1028 {       0, "Merge...|Alt M",            5, NULL},
1029 {       0, "Rip|V",                                     7, NULL},
1030 {       0, "Split|Y",                           4,              NULL},
1031 {       0, "Separate|P",                        3,              NULL},
1032 {       0, "SEPR",                                      0, NULL},
1033 {       0, "Smooth|W, Alt 1",                   2, NULL},
1034 {       0, "Remove Doubles|W, 6",                       1, NULL},
1035 {       0, "SEPR",                                      0, NULL},
1036 {       0, "Make Vertex Parent|Ctrl P",         0, NULL},
1037 {       0, "Add Hook|Ctrl H",           6, NULL},
1038 {  -1, "",                      0, do_view3d_edit_mesh_verticesmenu}};
1039
1040 static TBitem tb_mesh_edit_edge[]= {
1041 {       0, "Make Edge/Face|F",                  5,              NULL},
1042 {       0, "Collapse|Alt M", 14,                        NULL},
1043 {       0, "SEPR",                                              0, NULL},
1044 {       0, "Bevel|W, Alt 2",                                    6,              NULL},
1045 {       0, "Loop Subdivide|Ctrl R",             4,              NULL},
1046 {       0, "Knife Subdivide...|Shift K",        3,              NULL},
1047 {       0, "SEPR",                                                              0, NULL},
1048 {       0, "Subdivide|W, 1",                    2,              NULL},
1049 {       0, "Subdivide Fractal|W, 2",    1,              NULL},
1050 {       0, "Subdivide Smooth|W, 3",             0,              NULL},
1051 {       0, "SEPR",                                                              0, NULL},
1052 {       0, "Mark Seam|Ctrl E",                  7,              NULL},
1053 {       0, "Clear Seam|Ctrl E",                 8,              NULL},
1054 {       0, "SEPR",                                                              0, NULL},
1055 {       0, "Crease SubSurf|Shift E",    9,              NULL},
1056 {       0, "SEPR",                                                              0, NULL},
1057 {       0, "Rotate Edge CW|Ctrl E",     10,             NULL},
1058 {       0, "Rotate Edge CCW|Ctrl E",    11,             NULL},
1059 {       0, "SEPR",                                                              0, NULL},
1060 {       0, "Slide Edge|Ctrl E", 12,             NULL},
1061 {       0, "Delete Edge Loop|X",        13,             NULL},
1062 {  -1, "",                      0, do_view3d_edit_mesh_edgesmenu}};
1063
1064 static TBitem tb_mesh_edit_face[]= {
1065 {       0, "Make Edge/Face|F",                  5,              NULL},
1066 {       0, "Fill|Shift F",                              0,              NULL},
1067 {       0, "Beautify Fill|Alt F",                       1,              NULL},
1068 {       0, "SEPR",                                      0, NULL},
1069 {       0, "Convert to Triangles|Ctrl T",       2,              NULL},
1070 {       0, "Convert to Quads|Alt J",            3,              NULL},
1071 {       0, "Flip Triangle Edges|Ctrl Shift F",  4,              NULL},
1072 {       0, "Set Smooth|Ctrl F, 3",      6,              NULL},
1073 {       0, "Set Solid|Ctrl F, 4",       7,              NULL},
1074 {  -1, "",                      0, do_view3d_edit_mesh_facesmenu}};
1075
1076
1077 static TBitem tb_mesh_edit_normal[]= {
1078 {       0, "Recalculate Outside|Ctrl N",        2,              NULL},
1079 {       0, "Recalculate Inside|Ctrl Shift N",   1,              NULL},
1080 {       0, "SEPR",                                      0, NULL},
1081 {       0, "Flip|Ctrl F, 1",                            0,              NULL},
1082 {  -1, "",                      0, do_view3d_edit_mesh_normalsmenu}};
1083
1084 static TBitem tb_mesh_edit[]= {
1085 {       0, "Exit Editmode|Tab",         TB_TAB, NULL},
1086 {       0, "Undo|Ctrl Z",                       'u',            NULL},
1087 {       0, "Redo|Ctrl Shift Z",                 'U',            NULL},
1088 {       0, "SEPR",                              0,                      NULL},
1089 {       0, "Extrude|E",                 'e',            NULL},
1090 {       0, "Duplicate|Shift D", 'D',            NULL},
1091 {       0, "Delete...|X",               'x',            NULL},
1092 {       0, "SEPR",                              0,                      NULL},
1093 {       0, "Vertices",          0,              tb_mesh_edit_vertex},
1094 {       0, "Edges",             0,              tb_mesh_edit_edge},
1095 {       0, "Faces",             0,              tb_mesh_edit_face},
1096 {       0, "Normals",           0,              tb_mesh_edit_normal},
1097 {  -1, "",                      0, tb_do_hotkey}};
1098
1099
1100 static TBitem tb_object_ipo[]= {
1101 {       0, "Show/Hide",         'k', NULL},
1102 {       0, "Select Next",       TB_PAD|'u', NULL},
1103 {       0, "Select Prev",       TB_PAD|'d', NULL},
1104 {  -1, "",                      0, tb_do_hotkey}};
1105
1106
1107 static TBitem tb_object_edit[]= {
1108 {       0, "Enter Editmode|Tab",        TB_TAB, NULL},
1109 {       0, "SEPR",                                                              0, NULL},
1110 {       0, "Duplicate|Shift D",                 'D',            NULL},
1111 {       0, "Duplicate Linked|Alt D",    TB_ALT|'d', NULL},
1112 {       0, "Delete|X",                                  'x',            NULL},
1113 {       0, "SEPR",                                                              0, NULL},
1114 {       0, "Object Keys",       0, tb_object_ipo},
1115 {  -1, "",                      0, tb_do_hotkey}};
1116
1117
1118 /* ************* Type  ********** */
1119
1120 static TBitem tb_obdata_hide[]= {
1121 {       0, "Show Hidden|Alt H",                 TB_ALT|'h',             NULL},
1122 {       0, "Hide Selected|H",                   'h',            NULL},
1123 {       0, "Hide Deselected|Shift H",   'H',            NULL},
1124 {  -1, "",                      0, tb_do_hotkey}};
1125
1126 static void tb_do_mesh(void *arg, int event){
1127         switch(event) {
1128         case 1: common_insertkey(); break;
1129         case 2: G.f ^= G_DRAWEDGES; break;
1130         case 3: G.f ^= G_DRAWFACES; break;
1131         case 4: G.f ^= G_DRAWNORMALS; break;
1132         case 5: flip_subdivison(-1); break;
1133         }
1134         addqueue(curarea->win, REDRAW, 1);
1135 }
1136
1137 static TBitem tb_mesh[]= {
1138 {       0, "Insert Keyframe|I",                 1,              NULL},
1139 {       0, "SEPR",                                              0, NULL},
1140 {       0, "Show/Hide Edges",                   2,              NULL},
1141 {       0, "Show/Hide Faces",                   3,              NULL},
1142 {       0, "Show/Hide Normals",                 4,              NULL},
1143 {       0, "SEPR",                                              0,      NULL},
1144 {       0, "Subdivision Surface",               5,              NULL},
1145 {       0, "SEPR",                                              0, NULL},
1146 {       0, "Show/Hide Vertices",        0,              tb_obdata_hide},
1147 {  -1, "",                      0, tb_do_mesh}};
1148
1149 static TBitem tb_curve_hide[]= {
1150 {       0, "Show Hidden|Alt H",                 10,             NULL},
1151 {       0, "Hide Selected|Alt Ctrl H",                  11,             NULL},
1152 {  -1, "",                      0, do_view3d_edit_curve_showhidemenu}};
1153
1154
1155 static TBitem tb_curve[]= {
1156 {       0, "Insert Keyframe|I",                 'i',            NULL},
1157 {       0, "SEPR",                                              0, NULL},
1158 {       0, "Show/Hide Points",  0,              tb_curve_hide},
1159 {  -1, "",                      0, tb_do_hotkey}};
1160
1161 static TBitem tb_obdata[]= {
1162 {       0, "Duplicate|Shift D",                 'D',            NULL},
1163 {       0, "Delete|X",                                  'x',            NULL},
1164 {  -1, "",                      0, tb_do_hotkey}};
1165
1166 static TBitem tb_object_parent[]= {
1167 {       0, "Make Parent...|Ctrl P",             TB_CTRL|'p', NULL},
1168 {       0, "Clear Parent...|Alt P",             TB_ALT|'p', NULL},
1169 {  -1, "",                      0, tb_do_hotkey}};
1170
1171 static TBitem tb_object_track[]= {
1172 {       0, "Make Track|Ctrl T",                 TB_CTRL|'t', NULL},
1173 {       0, "Clear Track|Alt T",                 TB_ALT|'t', NULL},
1174 {  -1, "",                      0, tb_do_hotkey}};
1175
1176 static TBitem tb_object[]= {
1177 {       0, "Insert Keyframe|I",                 'i',            NULL},
1178 {       0, "SEPR",                                                              0, NULL},
1179 {       0, "Make Links...|Ctrl L",              TB_CTRL|'l', NULL},
1180 {       0, "Make Single User...|U",     'u',            NULL},
1181 {       0, "Copy Attributes...|Ctrl C", TB_CTRL|'c', NULL},
1182 {       0, "SEPR",                                                              0, NULL},
1183 {       0, "Parent",    0,              tb_object_parent},
1184 {       0, "Track",     0,              tb_object_track},
1185 {       0, "SEPR",                                                              0, NULL},
1186 {       0, "Boolean Operation|W",       'w', NULL},
1187 {       0, "Join Objects...|Ctrl J",    TB_CTRL|'j', NULL},
1188 {       0, "Convert Object Type...|Alt C",      TB_ALT|'c', NULL},
1189 {       0, "SEPR",                                                              0, NULL},
1190 {       0, "Move to Layer...|M",                'm', NULL},
1191 {  -1, "",                      0, tb_do_hotkey}};
1192
1193
1194 /* *************VIEW ********** */
1195
1196 static void tb_do_view_dt(void *arg, int event){
1197         G.vd->drawtype= event;
1198         addqueue(curarea->win, REDRAW, 1);
1199 }
1200
1201 static TBitem tb_view_dt[]= {
1202 {       ICON_BBOX, "Bounding Box",      1, NULL},
1203 {       ICON_WIRE, "Wireframe|Z",       2, NULL},
1204 {       ICON_SOLID, "Solid|Z",          3, NULL},
1205 {       ICON_SMOOTH, "Shaded|Shift Z",          4, NULL},
1206 {       ICON_POTATO, "Textured|Alt Z",  5, NULL},
1207 {  -1, "",                      0, tb_do_view_dt}};
1208
1209 static TBitem tb_view_alignview[]= {
1210 {       0, "Center View to Cursor|C",           'c', NULL},
1211 {       0, "Align Active Camera to View|Ctrl Alt NumPad 0",
1212 TB_CTRL|TB_ALT|TB_PAD|'0', NULL}, 
1213 {       0, "Align View to Selected|NumPad *",           TB_PAD|'*', NULL},
1214 {  -1, "",                      0, tb_do_hotkey}};
1215
1216 static TBitem tb_view[]= {
1217 {       0, "Viewport Shading",                  0, tb_view_dt},
1218 {       0, "SEPR",                                              0, NULL},
1219 {       0, "Ortho/Perspective|NumPad 5",        TB_PAD|'5', NULL},
1220 {       0, "Local/Global View|NumPad /",        TB_PAD|'/', NULL},
1221 {       0, "SEPR",                                              0, NULL},
1222 {       0, "Show All Layers|Shift ~",  TB_SHIFT|'~', NULL},
1223 {       0, "SEPR",                                              0, NULL},
1224 {       0, "Align View",                        0, tb_view_alignview},
1225 {       0, "SEPR",              0, NULL},
1226 {       0, "View Selected|NumPad .",    TB_PAD|'.', NULL},
1227 {       0, "View All|Home",             TB_PAD|'h', NULL},
1228 {       0, "SEPR",              0, NULL},
1229 {       0, "Play Back Animation|Alt A", TB_ALT|'a', NULL},
1230 {       0, "Camera Fly Mode|Shift F", TB_SHIFT|'f', NULL},
1231 {  -1, "",                      0, tb_do_hotkey}};
1232
1233
1234 /* *************TRANSFORM ********** */
1235
1236 static TBitem tb_transform_moveaxis[]= {
1237 {       0, "X Global|G, X",     0, NULL},
1238 {       0, "Y Global|G, Y",     1, NULL},
1239 {       0, "Z Global|G, Z",     2, NULL},
1240 {       0, "SEPR",                                      0, NULL},
1241 {       0, "X Local|G, X, X",   3, NULL},
1242 {       0, "Y Local|G, Y, Y",   4, NULL},
1243 {       0, "Z Local|G, Z, Z",   5, NULL},
1244 {  -1, "",                      0, do_view3d_transform_moveaxismenu}};
1245
1246 static TBitem tb_transform_rotateaxis[]= {
1247 {       0, "X Global|R, X",     0, NULL},
1248 {       0, "Y Global|R, Y",     1, NULL},
1249 {       0, "Z Global|R, Z",     2, NULL},
1250 {       0, "SEPR",                                      0, NULL},
1251 {       0, "X Local|R, X, X",   3, NULL},
1252 {       0, "Y Local|R, Y, Y",   4, NULL},
1253 {       0, "Z Local|R, Z, Z",   5, NULL},
1254 {  -1, "",                      0, do_view3d_transform_rotateaxismenu}};
1255
1256 static TBitem tb_transform_scaleaxis[]= {
1257 {       0, "X Global|S, X",     0, NULL},
1258 {       0, "Y Global|S, Y",     1, NULL},
1259 {       0, "Z Global|S, Z",     2, NULL},
1260 {       0, "SEPR",                                      0, NULL},
1261 {       0, "X Local|S, X, X",   3, NULL},
1262 {       0, "Y Local|S, Y, Y",   4, NULL},
1263 {       0, "Z Local|S, Z, Z",   5, NULL},
1264 {  -1, "",                      0, do_view3d_transform_scaleaxismenu}};
1265
1266 static void tb_do_transform_clearapply(void *arg, int event)
1267 {
1268         Object *ob;
1269         ob= OBACT;
1270         
1271         switch(event)
1272         {
1273             case 0: /* clear location */
1274                         clear_object('g');
1275                         break;
1276                 case 1: /* clear rotation */
1277                         clear_object('r');
1278                         break;
1279                 case 2: /* clear scale */
1280                         clear_object('s');
1281                         break;
1282                 case 3: /* apply scale/rotation */
1283                         apply_objects_locrot();
1284                         break;
1285                 case 4: /* apply scale/rotation */
1286                         apply_objects_visual_tx();
1287                         break;
1288                 case 5: /* apply deformation */
1289                         object_apply_deform(ob);
1290                         break;
1291                 case 6: /* make duplicates real */
1292                         if (ob->transflag & OB_DUPLI) make_duplilist_real();
1293                         else error("The active object does not have dupliverts");
1294                         break;
1295         }
1296 }
1297
1298 static TBitem tb_transform_clearapply[]= {
1299 {       0, "Clear Location|Alt G",              0, NULL},
1300 {       0, "Clear Rotation|Alt R",              1, NULL},
1301 {       0, "Clear Scale|Alt S",                 2, NULL},
1302 {       0, "SEPR",                                      0, NULL},
1303 {       0, "Apply Scale/Rotation to ObData|Ctrl A, 1", 3, NULL},
1304 {       0, "Apply Visual Transform|Ctrl A, 2", 4, NULL},
1305 {       0, "Apply Deformation|Shift Ctrl A", 5, NULL},
1306 {       0, "Make Duplicates Real|Shift Ctrl A", 6, NULL},
1307 {  -1, "",                      0, tb_do_transform_clearapply}};
1308
1309 static TBitem tb_transform_snap[]= {
1310 {       0, "Selection -> Grid|Shift S, 1",              1, NULL},
1311 {       0, "Selection -> Cursor|Shift S, 2",    2, NULL},
1312 {       0, "Selection -> Center|Shift S, 3", 3, NULL},
1313 {       0, "Cursor -> Selection|Shift S, 4", 4, NULL},
1314 {       0, "Cursor -> Grid|Shift S, 5",                 5, NULL},
1315 {       0, "Cursor -> Active|Shift S, 6",               6, NULL},
1316 {  -1, "",                      0, do_view3d_edit_snapmenu}};
1317
1318 static void tb_do_transform(void *arg, int event)
1319 {
1320         switch(event)
1321         {
1322                 case 0: /* Grab/move */
1323                         initTransform(TFM_TRANSLATION, CTX_NONE);
1324                         Transform();
1325                         break;
1326                 case 1: /* Rotate */
1327                         initTransform(TFM_ROTATION, CTX_NONE);
1328                         Transform();
1329                         break;
1330                 case 2: /* Scale */
1331                         initTransform(TFM_RESIZE,CTX_NONE);
1332                         Transform();
1333                         break;
1334                 case 3: /* transform properties */
1335                         add_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_UNSTOW);
1336                         break;
1337                 case 4: /* snap */
1338                         snapmenu();
1339                         break;
1340                 case 5: /* Shrink/Fatten Along Normals */
1341                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1342                         Transform();
1343                         break;
1344                 case 6: /* Shear */
1345                         initTransform(TFM_SHEAR, CTX_NONE);
1346                         Transform();
1347                         break;
1348                 case 7: /* Warp */
1349                         initTransform(TFM_WARP, CTX_NONE);
1350                         Transform();
1351                         break;
1352                 case 8: /* proportional edit (toggle) */
1353                         if(G.scene->proportional) G.scene->proportional= 0;
1354                         else G.scene->proportional= 1;
1355                         break;
1356                 case 10:
1357                         docenter(0);
1358                         break;
1359                 case 11:
1360                         docenter_new();
1361                         break;
1362                 case 12:
1363                         docenter_cursor();
1364                         break;
1365         }
1366         allqueue(REDRAWVIEW3D, 0);
1367 }
1368
1369 static TBitem tb_transform_object_mirror[]= {
1370 {       0, "X Local|Ctrl M, 1",         1, NULL},
1371 {       0, "Y Local|Ctrl M, 2",         2, NULL},
1372 {       0, "Z Local|Ctrl M, 3",         3, NULL},
1373 {  -1, "",                      0, do_view3d_object_mirrormenu}};
1374
1375 static TBitem tb_transform[]= {
1376 {       0, "Grab/Move|G",       0, NULL},
1377 {       0, "Grab/Move on Axis| ",       0, tb_transform_moveaxis},
1378 {       0, "Rotate|R",          1, NULL},
1379 {       0, "Rotate on Axis",    0, tb_transform_rotateaxis},
1380 {       0, "Scale|S",           2, NULL},
1381 {       0, "Scale on Axis",     0, tb_transform_scaleaxis},
1382 {       0, "SEPR",                                      0, NULL},
1383 {       0, "ObData to Center",          10, NULL},
1384 {       0, "Center New",                        11, NULL},
1385 {       0, "Center Cursor",                     12, NULL},
1386 {       0, "SEPR",                                      0, NULL},
1387 {       ICON_MENU_PANEL, "Properties|N", 3, NULL},
1388 {       0, "SEPR",                                      0, NULL},
1389 {       0, "Mirror",    0, tb_transform_object_mirror},
1390 {       0, "SEPR",                                      0, NULL},
1391 {       0, "Snap",              0, tb_transform_snap},
1392 {       0, "SEPR",                                      0, NULL},
1393 {       0, "Clear/Apply",       0, tb_transform_clearapply},
1394 {  -1, "",                      0, tb_do_transform}};
1395
1396 static TBitem tb_transform_edit_mirror[]= {
1397 {       0, "X Global|Ctrl M, 1",        1, NULL},
1398 {       0, "Y Global|Ctrl M, 2",        2, NULL},
1399 {       0, "Z Global|Ctrl M, 3",        3, NULL},
1400 {       0, "SEPR",                                      0, NULL},
1401 {       0, "X Local|Ctrl M, 4",         4, NULL},
1402 {       0, "Y Local|Ctrl M, 5",         5, NULL},
1403 {       0, "Z Local|Ctrl M, 6",         6, NULL},
1404 {       0, "SEPR",                                      0, NULL},
1405 {       0, "X View|Ctrl M, 7",  7, NULL},
1406 {       0, "Y View|Ctrl M, 8",  8, NULL},
1407 {       0, "Z View|Ctrl M, 9",  9, NULL},
1408 {  -1, "",                      0, do_view3d_edit_mirrormenu}};
1409
1410 static TBitem tb_transform_editmode1[]= {
1411 {       0, "Grab/Move|G",       0, NULL},
1412 {       0, "Grab/Move on Axis| ",       0, tb_transform_moveaxis},
1413 {       0, "Rotate|R",          1, NULL},
1414 {       0, "Rotate on Axis",    0, tb_transform_rotateaxis},
1415 {       0, "Scale|S",           2, NULL},
1416 {       0, "Scale on Axis",     0, tb_transform_scaleaxis},
1417 {       0, "SEPR",                                      0, NULL},
1418 {       0, "Shrink/Fatten|Alt S", 5, NULL},
1419 {       0, "Shear|Ctrl S", 6, NULL},
1420 {       0, "Warp|Shift W",      7, NULL},
1421 {       0, "SEPR",                                      0, NULL},
1422 {       0, "ObData to Center",          10, NULL},
1423 {       0, "SEPR",                                      0, NULL},
1424 {       ICON_MENU_PANEL, "Properties|N", 3, NULL},
1425 {       0, "SEPR",                                      0, NULL},
1426 {       0, "Mirror",    0, tb_transform_edit_mirror},
1427 {       0, "SEPR",                                      0, NULL},
1428 {       0, "Snap",              0, tb_transform_snap},
1429 {       0, "SEPR",                                      0, NULL},
1430 {       0, "Proportional Edit|O",       8,              NULL},
1431 {  -1, "",                      0, tb_do_transform}};
1432
1433
1434 static TBitem tb_transform_editmode2[]= {
1435 {       0, "Grab/Move|G",       0, NULL},
1436 {       0, "Grab/Move on Axis| ",       0, tb_transform_moveaxis},
1437 {       0, "Rotate|R",          1, NULL},
1438 {       0, "Rotate on Axis",    0, tb_transform_rotateaxis},
1439 {       0, "Scale|S",           2, NULL},
1440 {       0, "Scale on Axis",     0, tb_transform_scaleaxis},
1441 {       0, "SEPR",                                      0, NULL},
1442 {       ICON_MENU_PANEL, "Properties|N", 3, NULL},
1443 {       0, "Snap",              0, tb_transform_snap},
1444 {  -1, "",                      0, tb_do_transform}};
1445
1446
1447 /* *************ADD ********** */
1448
1449 static TBitem addmenu_curve[]= {
1450 {       0, "Bezier Curve",      0, NULL},
1451 {       0, "Bezier Circle", 1, NULL},
1452 {       0, "NURBS Curve",       2, NULL},
1453 {       0, "NURBS Circle",      3, NULL},
1454 {       0, "Path",                      4, NULL},
1455 {  -1, "",                      0, do_info_add_curvemenu}};
1456
1457 static TBitem addmenu_surf[]= {
1458 {       0, "NURBS Curve",       0, NULL},
1459 {       0, "NURBS Circle",      1, NULL},
1460 {       0, "NURBS Surface", 2, NULL},
1461 {       0, "NURBS Tube",        3, NULL},
1462 {       0, "NURBS Sphere",      4, NULL},
1463 {       0, "NURBS Donut",       5, NULL},
1464 {  -1, "",                      0, do_info_add_surfacemenu}};
1465
1466 static TBitem addmenu_meta[]= {
1467 {       0, "Meta Ball",         0, NULL},
1468 {       0, "Meta Tube",         1, NULL},
1469 {       0, "Meta Plane",        2, NULL},
1470 {       0, "Meta Ellipsoid", 3, NULL},
1471 {       0, "Meta Cube",         4, NULL},
1472 {  -1, "",                      0, do_info_add_metamenu}};
1473
1474 static TBitem addmenu_lamp[]= {
1475 {       0, "Lamp",      0, NULL},
1476 {       0, "Sun",       1, NULL},
1477 {       0, "Spot",      2, NULL},
1478 {       0, "Hemi", 3, NULL},
1479 {       0, "Area",      4, NULL},
1480 {  -1, "",                      0, do_info_add_lampmenu}};
1481
1482 static TBitem addmenu_YF_lamp[]= {
1483 {       0, "Lamp",      0, NULL},
1484 {       0, "Sun",       1, NULL},
1485 {       0, "Spot",      2, NULL},
1486 {       0, "Hemi", 3, NULL},
1487 {       0, "Area",      4, NULL},
1488 {       0, "Photon",    5, NULL},
1489 {  -1, "",                      0, do_info_add_lampmenu}};
1490
1491
1492 static TBitem addmenu_armature[]= {
1493 {       0, "Bone",      8, NULL},
1494 {  -1, "",                      0, do_info_addmenu}};
1495
1496 /* dynamic items */
1497 #define TB_ADD_MESH             0
1498 #define TB_ADD_GROUP    7
1499 #define TB_ADD_LAMP             10
1500
1501 static TBitem tb_add[]= {
1502 {       0, "Mesh",              0, NULL},
1503 {       0, "Curve",     1, addmenu_curve},
1504 {       0, "Surface",   2, addmenu_surf},
1505 {       0, "Meta",      3, addmenu_meta},
1506 {       0, "Text",              4, NULL},
1507 {       0, "Empty",     5, NULL},
1508 {       0, "SEPR",              0, NULL},
1509 {       0, "Group",     10, NULL},
1510 {       0, "SEPR",              0, NULL},
1511 {       0, "Camera",    6, NULL},
1512 {       0, "Lamp",              7, addmenu_lamp},
1513 {       0, "SEPR",              0, NULL},
1514 {       0, "Armature",  8, NULL},
1515 {       0, "Lattice",   9, NULL},
1516 {  -1, "",                      0, do_info_addmenu}};
1517
1518 static TBitem tb_empty[]= {
1519 {       0, "Nothing...",        0, NULL},
1520 {  -1, "",              0, NULL}};
1521
1522
1523 /* *************RENDER ********** */
1524
1525 static void tb_do_render(void *arg, int event){
1526         switch(event)
1527         {
1528                 case 1: /* set render border */
1529                         set_render_border();
1530                         break;
1531                 case 2: /* render */
1532                         BIF_do_render(0);
1533                         break;
1534                 case 3: /* render anim */
1535                         BIF_do_render(1);
1536                         break;
1537                 case 4: /* passepartout */
1538                 {
1539                         Camera *ca= NULL;
1540                         if(G.vd->camera==NULL) return;
1541                         
1542                         if(G.vd->camera->type==OB_CAMERA)
1543                                 ca= G.vd->camera->data;
1544                         else return;
1545                                 
1546                         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT))
1547                                 ca->flag &= ~CAM_SHOWPASSEPARTOUT;
1548                         else
1549                                 ca->flag |= CAM_SHOWPASSEPARTOUT;
1550                         allqueue(REDRAWVIEW3D, 0);
1551                 }
1552                         break;
1553                 case 5: /*preview render */
1554                         toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
1555                         scrarea_queue_winredraw(curarea);
1556                         break;
1557         }
1558 }
1559
1560 static TBitem tb_render[]= {
1561         {       0, "Passepartout",                      4, NULL},
1562         {       0, "Set Border|Shift B",                1, NULL},
1563         {       0, "SEPR",                              0, NULL},
1564         {       0, "Render|F12",                        2, NULL},
1565         {       0, "Anim|Ctrl F12",                     3, NULL},
1566         {       0, "Preview|Shift P",                   5, NULL},
1567         {  -1, "",                      0, tb_do_render}};
1568
1569 /* ************************* NODES *********************** */
1570
1571
1572 /* dynamic items */
1573
1574 static TBitem tb_node_addsh[]= {
1575         {       0, "Input",                     1, NULL},
1576         {       0, "Output",            2, NULL},
1577         {       0, "Color",                     3, NULL},
1578         {       0, "Vector",            4, NULL},
1579         {       0, "Convertor", 5, NULL},
1580         {       0, "Group",             6, NULL},
1581         {       0, "Dynamic",   7, NULL},
1582         {  -1, "",                      0, NULL}};
1583
1584 static TBitem tb_node_addcomp[]= {
1585         {       0, "Input",                     1, NULL},
1586         {       0, "Output",            2, NULL},
1587         {       0, "Color",                     3, NULL},
1588         {       0, "Vector",            4, NULL},
1589         {       0, "Filter",            5, NULL},
1590         {       0, "Convertor", 6, NULL},
1591         {       0, "Matte",             7, NULL},
1592         {       0, "Distort",   8, NULL},
1593         {       0, "Group",             9, NULL},
1594         {       0, "Dynamic",   10, NULL},
1595         {       -1, "",                 0, NULL}};
1596
1597 /* do_node_addmenu() in header_node.c, prototype in BSE_headerbuttons.h */
1598
1599 /* dynamic toolbox sublevel */
1600 static TBitem *node_add_sublevel(ListBase *storage, bNodeTree *ntree, int nodeclass)
1601 {
1602         static TBitem _addmenu[]= { {   0, " ",         0, NULL}, {  -1, "",                    0, NULL}};
1603         Link *link;
1604         TBitem *addmenu;
1605         int tot= 0, a;
1606         
1607         if(ntree) {
1608                 if(nodeclass==NODE_CLASS_GROUP) {
1609                         bNodeTree *ngroup= G.main->nodetree.first;
1610                         for(; ngroup; ngroup= ngroup->id.next)
1611                                 if(ngroup->type==ntree->type)
1612                                         tot++;
1613                 }
1614                 else {
1615                         bNodeType *ntype = ntree->alltypes.first;
1616                         while(ntype) {
1617                                 if(ntype->nclass == nodeclass) {
1618                                         tot++;
1619                                 }
1620                                 ntype= ntype->next;
1621                         }
1622                 }
1623         }       
1624         if(tot==0) {
1625                 return _addmenu;
1626         }
1627         
1628         link= MEM_callocN(sizeof(Link) + sizeof(TBitem)*(tot+1), "types menu");
1629         BLI_addtail(storage, link);
1630         addmenu= (TBitem *)(link+1);
1631         
1632         if(nodeclass==NODE_CLASS_GROUP) {
1633                 bNodeTree *ngroup= G.main->nodetree.first;
1634                 for(tot=0, a=0; ngroup; ngroup= ngroup->id.next, tot++) {
1635                         if(ngroup->type==ntree->type) {
1636                                 addmenu[a].name= ngroup->id.name+2;
1637                                 addmenu[a].retval= NODE_GROUP_MENU+tot; /* so we can use BLI_findlink() */
1638                                 a++;
1639                         }
1640                 }
1641         }
1642         else {
1643                 bNodeType *type= ntree->alltypes.first;
1644                 int script=0;
1645                 for(a=0; type; type= type->next) {
1646                         if( type->nclass == nodeclass ) {
1647                                 if(type->type == NODE_DYNAMIC) {
1648                                         if(type->id)
1649                                                 addmenu[a].name= type->id->name+2;
1650                                         else
1651                                                 addmenu[a].name= type->name;
1652                                         addmenu[a].retval= NODE_DYNAMIC_MENU+script;
1653                                         script++;
1654                                 } else {
1655                                         addmenu[a].name= type->name;
1656                                         addmenu[a].retval= type->type;
1657                                 }
1658                                 a++;
1659                         }
1660                 }
1661         }
1662         
1663         addmenu[a].icon= -1;    /* end signal */
1664         addmenu[a].name= "";
1665         addmenu[a].retval= a;
1666         addmenu[a].poin= do_node_addmenu;
1667         
1668         return addmenu;
1669 }
1670
1671
1672 static TBitem tb_node_node[]= {
1673         {       0, "Duplicate|Shift D", TB_SHIFT|'d',           NULL},
1674         {       0, "Delete|X", 'x',             NULL},
1675         {       0, "SEPR",              0, NULL},
1676         {       0, "Make Link|F", 'f', NULL},
1677         {       0, "SEPR",              0, NULL},
1678         {       0, "Make Group|Ctrl G", TB_CTRL|'g',            NULL},
1679         {       0, "Ungroup|Alt G", TB_ALT|'g',                 NULL},
1680         {       0, "Edit Group|Tab", TB_TAB, NULL},
1681         {       0, "SEPR",              0, NULL},
1682         {       0, "Hide/Unhide|H", 'h', NULL},
1683         {       0, "Rename|Ctrl R", TB_CTRL|'r',                NULL},
1684         {       0, "SEPR",              0, NULL},
1685         {       0, "Read Saved Render Results|R", 'r', NULL},
1686         {       0, "Show Cyclic Dependencies|C", 'c', NULL},
1687         {  -1, "",                      0, tb_do_hotkey}};
1688
1689 static TBitem tb_node_select[]= {
1690         {       0, "Select/Deselect All|A",     'a', NULL},
1691         {       0, "Border Select|B",   'b', NULL},
1692         {  -1, "",                      0, tb_do_hotkey}};
1693
1694 static TBitem tb_node_transform[]= {
1695         {       0, "Grab/Move|G", 'g',          NULL},
1696         {  -1, "",                      0, tb_do_hotkey}};
1697
1698 static TBitem tb_node_view[]= {
1699         {       0, "Zoom In|NumPad +",  TB_PAD|'+', NULL},
1700         {       0, "Zoom Out|NumPad -", TB_PAD|'-', NULL},
1701         {       0, "View All|Home",     TB_PAD|'h', NULL},
1702         {  -1, "",                      0, tb_do_hotkey}};
1703
1704
1705 /* *********************************************** */
1706
1707 static uiBlock *tb_makemenu(void *arg)
1708 {
1709         static int counter=0;
1710         TBitem *item= arg, *itemt;
1711         uiBlock *block;
1712         int xco= 0, yco= 0;
1713         char str[10];
1714         
1715         if(arg==NULL) return NULL;
1716         
1717         sprintf(str, "tb %d", counter++);
1718         block= uiNewBlock(&tb_listb, str, UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
1719         uiBlockSetCol(block, TH_MENU_ITEM);
1720
1721         // last item has do_menu func, has to be stored in each button
1722         itemt= item;
1723         while(itemt->icon != -1) itemt++;
1724         uiBlockSetButmFunc(block, itemt->poin, NULL);
1725
1726         // now make the buttons
1727         while(item->icon != -1) {
1728
1729                 if(strcmp(item->name, "SEPR")==0) {
1730                         uiDefBut(block, SEPR, 0, "", xco, yco-=6, 50, 6, NULL, 0.0, 0.0, 0, 0, "");
1731                 }
1732                 else if(item->icon) {
1733                         uiDefIconTextBut(block, BUTM, 1, item->icon, item->name, xco, yco-=20, 80, 19, NULL, 0.0, 0.0, 0, item->retval, "");
1734                 }
1735                 else if(item->poin) {
1736                         uiDefIconTextBlockBut(block, tb_makemenu, item->poin, ICON_RIGHTARROW_THIN, item->name, 0, yco-=20, 80, 19, "");
1737                 }
1738                 else {
1739                         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, item->name, xco, yco-=20, 80, 19, NULL, 0.0, 0.0, 0, item->retval, "");
1740                 }
1741                 
1742                 if(yco <= -600) {
1743                         yco = 0;
1744                         xco += 80;
1745                 }
1746                 
1747                 item++;
1748         }
1749         
1750         uiTextBoundsBlock(block, 60);
1751         
1752         /* direction is also set in the function that calls this */
1753         if(U.uiflag & USER_PLAINMENUS)
1754                 uiBlockSetDirection(block, UI_RIGHT);
1755         else
1756                 uiBlockSetDirection(block, UI_RIGHT|UI_CENTER);
1757
1758         return block;
1759 }
1760
1761 static int tb_mainx= 1234, tb_mainy= 0;
1762 static void store_main(void *arg1, void *arg2)
1763 {
1764         tb_mainx= (intptr_t)arg1;
1765         tb_mainy= (intptr_t)arg2;
1766 }
1767
1768 static void do_group_addmenu(void *arg, int event)
1769 {
1770         Object *ob;
1771         
1772         if(event<0) return;
1773         
1774         add_object_draw(OB_EMPTY);
1775         ob= OBACT;
1776         
1777         ob->dup_group= BLI_findlink(&G.main->group, event);
1778         if(ob->dup_group) {
1779                 rename_id(&ob->id, ob->dup_group->id.name+2);
1780
1781                 id_us_plus((ID *)ob->dup_group);
1782                 ob->transflag |= OB_DUPLIGROUP;
1783                 DAG_scene_sort(G.scene);
1784         }
1785 }
1786
1787 /* helper for create group menu */
1788 static void tag_groups_for_toolbox(void)
1789 {
1790         Group *group;
1791         GroupObject *go;
1792         
1793         for(group= G.main->group.first; group; group= group->id.next)
1794                 group->id.flag |= LIB_DOIT;
1795         
1796         for(group= G.main->group.first; group; group= group->id.next) {
1797                 if(group->id.flag & LIB_DOIT)
1798                         for(go= group->gobject.first; go; go= go->next) 
1799                                 if(go->ob && go->ob->dup_group)
1800                                         go->ob->dup_group->id.flag &= ~LIB_DOIT;
1801         }
1802 }
1803
1804 /* helper for create group menu */
1805 /* note that group id.flag was set */
1806 static int count_group_libs(void)
1807 {
1808         Group *group;
1809         Library *lib;
1810         int tot= 0;
1811         
1812         for(lib= G.main->library.first; lib; lib= lib->id.next)
1813                 lib->id.flag |= LIB_DOIT;
1814         
1815         for(group= G.main->group.first; group; group= group->id.next) {
1816                 if(group->id.flag & LIB_DOIT) {
1817                         if(group->id.lib && (group->id.lib->id.flag & LIB_DOIT)) {
1818                                 group->id.lib->id.flag &= ~LIB_DOIT;
1819                                 tot++;
1820                         }
1821                 }
1822         }
1823         return tot;
1824 }
1825
1826 /* dynamic toolbox sublevel */
1827 static TBitem *create_group_sublevel(ListBase *storage, Library *lib)
1828 {
1829         static TBitem addmenu[]= { {    0, "No Groups",         0, NULL}, {  -1, "",                    0, NULL}};
1830         Link *link;
1831         TBitem *groupmenu, *gm;
1832         Group *group;
1833         int a;
1834         int tot= BLI_countlist(&G.main->group);
1835         
1836         if(tot==0) {
1837                 return addmenu;
1838         }
1839         
1840         /* build menu, we insert a Link before the array of TBitems */
1841         link= MEM_callocN(sizeof(Link) + sizeof(TBitem)*(tot+1), "group menu lib");
1842         BLI_addtail(storage, link);
1843         gm= groupmenu= (TBitem *)(link+1);
1844         for(a=0, group= G.main->group.first; group; group= group->id.next, a++) {
1845                 if(group->id.lib==lib && (group->id.flag & LIB_DOIT)) {
1846                         gm->name= group->id.name+2;
1847                         gm->retval= a;
1848                         gm++;
1849                 }
1850         }
1851         gm->icon= -1;   /* end signal */
1852         gm->name= "";
1853         gm->retval= a;
1854         gm->poin= do_group_addmenu;
1855         
1856         return groupmenu;
1857 }
1858
1859 static TBitem *create_group_all_sublevels(ListBase *storage)
1860 {
1861         Library *lib;
1862         Group *group;
1863         Link *link;
1864         TBitem *groupmenu, *gm;
1865         int a;
1866         int totlevel= 0;
1867         int totlocal= 0;
1868         
1869         /* we add totlevel + local groups entries */
1870         
1871         /* let's skip group-in-group */
1872         tag_groups_for_toolbox();
1873         
1874         /* this call checks for skipped group-in-groups */
1875         totlevel= count_group_libs();
1876         
1877         for(group= G.main->group.first; group; group= group->id.next)
1878                 if(group->id.flag & LIB_DOIT)
1879                         if(group->id.lib==NULL)
1880                                 totlocal++;
1881         
1882         if(totlocal+totlevel==0)
1883                 return create_group_sublevel(storage, NULL);
1884         
1885         /* build menu, we insert a Link before the array of TBitems */
1886         link= MEM_callocN(sizeof(Link) + sizeof(TBitem)*(totlocal+totlevel+1), "group menu");
1887         BLI_addtail(storage, link);
1888         gm= groupmenu= (TBitem *)(link+1);
1889         
1890         /* first all levels. libs with groups are not tagged */
1891         for(lib= G.main->library.first; lib; lib= lib->id.next) {
1892                 if(!(lib->id.flag & LIB_DOIT)) {
1893                         char *str;
1894                         /* do some tricks to get .blend file name without extension */
1895                         link= MEM_callocN(sizeof(Link) + 128, "string");
1896                         BLI_addtail(storage, link);
1897                         str= (char *)(link+1);
1898                         BLI_strncpy(str, BLI_last_slash(lib->filename)+1, 128);
1899                         if(strlen(str)>6) str[strlen(str)-6]= 0;
1900                         gm->name= str;
1901                         gm->retval= -1;
1902                         gm->poin= create_group_sublevel(storage, lib);
1903                         gm++;
1904                 }
1905         }
1906         /* remaining groups */
1907         for(a=0, group= G.main->group.first; group; group= group->id.next, a++) {
1908                 if(group->id.lib==NULL && (group->id.flag & LIB_DOIT)) {
1909                         gm->name= group->id.name+2;
1910                         gm->retval= a;
1911                         gm++;
1912                 }
1913         }
1914         gm->icon= -1;   /* end signal */
1915         gm->name= "";
1916         gm->retval= a;
1917         gm->poin= do_group_addmenu;
1918         
1919         return groupmenu;
1920 }
1921
1922 static TBitem *create_mesh_sublevel(ListBase *storage)
1923 {
1924         Link *link;
1925         TBitem *meshmenu, *mm;
1926         int totmenu= 10, totpymenu=0, a=0;
1927         
1928         /* Python Menu */
1929         BPyMenu *pym;
1930         
1931         /* count the python menu items*/
1932         for (pym = BPyMenuTable[PYMENU_ADDMESH]; pym; pym = pym->next, totpymenu++) {}
1933         if (totpymenu) totmenu += totpymenu+1; /* add 1 for the seperator */
1934         
1935         link= MEM_callocN(sizeof(Link) + sizeof(TBitem)*(totmenu+1), "mesh menu");
1936         BLI_addtail(storage, link);
1937         mm= meshmenu= (TBitem *)(link+1);
1938         
1939         mm->icon = 0; mm->retval= a; mm->name = "Plane";                mm++; a++;
1940         mm->icon = 0; mm->retval= a; mm->name = "Cube";                 mm++; a++;
1941         mm->icon = 0; mm->retval= a; mm->name = "Circle";               mm++; a++;
1942         mm->icon = 0; mm->retval= a; mm->name = "UVsphere";     mm++; a++;
1943         mm->icon = 0; mm->retval= a; mm->name = "Icosphere";    mm++; a++;
1944         mm->icon = 0; mm->retval= a; mm->name = "Cylinder";     mm++; a++; a++;
1945         mm->icon = 0; mm->retval= a; mm->name = "Cone";                 mm++; a++; 
1946         mm->icon = 0; mm->retval= 0; mm->name = "SEPR";                 mm++;
1947         mm->icon = 0; mm->retval= a; mm->name = "Grid";                 mm++; a++;
1948         mm->icon = 0; mm->retval= a; mm->name = "Monkey";               mm++; a++;
1949         /* a == 10 */
1950         
1951         if (totpymenu) {
1952                 int i=0;
1953                 mm->icon = 0; mm->retval= 0; mm->name = "SEPR";         mm++;
1954                 
1955                 /* note that we account for the 10 previous entries with i+4: */
1956                 for (pym = BPyMenuTable[PYMENU_ADDMESH]; pym; pym = pym->next, i++) {
1957                         mm->icon = ICON_PYTHON;
1958                         mm->retval= i+20;
1959                         mm->name = pym->name;   
1960                         mm++; a++;
1961                 }
1962         }
1963         
1964         /* terminate the menu */
1965         mm->icon= -1; mm->retval= a; mm->name= ""; mm->poin= do_info_add_meshmenu;
1966         
1967         return meshmenu;
1968 }
1969
1970
1971
1972 void toolbox_n(void)
1973 {
1974         uiBlock *block;
1975         uiBut *but;
1976         ListBase storage= {NULL, NULL};
1977         TBitem *menu1=NULL, *menu2=NULL, *menu3=NULL; 
1978         TBitem *menu4=NULL, *menu5=NULL, *menu6=NULL;
1979         TBitem *menu7=NULL;
1980         int dx=0;
1981         short event, mval[2], tot=0;
1982         char *str1=NULL, *str2=NULL, *str3=NULL, *str4=NULL, *str5=NULL, *str6=NULL, *str7=NULL;
1983         
1984         /* temporal too... when this flag is (was) saved, it should initialize OK */
1985         if(tb_mainx==1234) {
1986                 if(U.uiflag & USER_PLAINMENUS) {
1987                         tb_mainx= -32;
1988                         tb_mainy= -5;
1989                 } else {
1990                         tb_mainx= 0;
1991                         tb_mainy= -5;
1992                 }
1993         }
1994         
1995         /* save present mouse position */
1996         toolbox_mousepos(mval, 1);
1997
1998         mywinset(G.curscreen->mainwin); // we go to screenspace
1999         
2000         block= uiNewBlock(&tb_listb, "toolbox", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
2001         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
2002         uiBlockSetCol(block, TH_MENU_ITEM);
2003         
2004         /* select context for main items */
2005         if(curarea->spacetype==SPACE_VIEW3D) {
2006
2007                 /* dynamic menu entries */
2008                 tb_add[TB_ADD_GROUP].poin= create_group_all_sublevels(&storage);
2009                 tb_add[TB_ADD_MESH].poin= create_mesh_sublevel(&storage);
2010                 
2011                 /* static */
2012                 if (G.scene->r.renderer==R_YAFRAY)
2013                         tb_add[TB_ADD_LAMP].poin= addmenu_YF_lamp;
2014                 else
2015                         tb_add[TB_ADD_LAMP].poin= addmenu_lamp;
2016                 
2017                 if(U.uiflag & USER_PLAINMENUS) {
2018                         menu1= tb_add; str1= "Add";
2019                         menu2= tb_object_edit; str2= "Edit";
2020                         menu3= tb_object_select; str3= "Select";
2021                         menu4= tb_transform; str4= "Transform";
2022                         menu5= tb_object; str5= "Object";
2023                         menu6= tb_view; str6= "View";
2024                         menu7= tb_render; str7= "Render";
2025
2026                         dx= 96;
2027                         tot= 7;
2028                 } else {
2029                         /* 3x2 layout menu */
2030                         menu1= tb_object; str1= "Object";
2031                         menu2= tb_add; str2= "Add";
2032                         menu3= tb_object_select; str3= "Select";
2033                         menu4= tb_object_edit; str4= "Edit";
2034                         menu5= tb_transform; str5= "Transform";
2035                         menu6= tb_view; str6= "View";
2036
2037                         dx= 64;
2038                         tot= 6;
2039                 }
2040                 
2041                 if(G.obedit) {
2042                         if(U.uiflag & USER_PLAINMENUS) {
2043                                 switch(G.obedit->type){
2044                                 case OB_MESH:
2045                                         menu1= create_mesh_sublevel(&storage);
2046                                         menu2= tb_mesh_edit;
2047                                         menu3= tb_mesh_select;
2048                                         menu4= tb_transform_editmode1;
2049                                         menu5= tb_mesh; str5= "Mesh";
2050                                 break;
2051                                 case OB_CURVE:
2052                                         menu1= addmenu_curve;
2053                                         menu2= tb_curve_edit;
2054                                         menu3= tb_curve_select;
2055                                         menu4= tb_transform_editmode1;
2056                                         menu5= tb_curve; str5= "Curve";
2057                                 break;
2058                                 case OB_SURF:
2059                                         menu1= addmenu_surf;
2060                                         menu2= tb_curve_edit;
2061                                         menu3= tb_curve_select;
2062                                         menu4= tb_transform_editmode1;
2063                                         menu5= tb_curve; str5= "Surface";
2064                                 break;
2065                                 case OB_MBALL:
2066                                         menu1= addmenu_meta;
2067                                         menu2= tb_edit;
2068                                         menu3= tb_mball_select;
2069                                         menu4= tb_transform_editmode2;
2070                                         menu5= tb_obdata; str5= "Meta";
2071                                 break;
2072                                 case OB_ARMATURE:
2073                                         menu1= addmenu_armature;
2074                                         menu2= tb_edit;
2075                                         menu3= tb__select;
2076                                         menu4= tb_transform_editmode2;
2077                                         menu5= tb_obdata;str5= "Armature";
2078                                 break;
2079                                 case OB_LATTICE:
2080                                         menu1= tb_empty;
2081                                         menu2= tb_edit;
2082                                         menu3= tb__select;
2083                                         menu4= tb_transform_editmode1;
2084                                         menu5= tb_empty;str5= "Lattice";
2085                                 break;
2086                                 }
2087                         } else {
2088                                 if(G.obedit->type==OB_MESH) {
2089                                         menu1= tb_mesh; str1= "Mesh";
2090                                         menu2= create_mesh_sublevel(&storage); 
2091                                         menu3= tb_mesh_select;
2092                                         menu4= tb_mesh_edit; 
2093                                         menu5= tb_transform_editmode1;
2094                                 }
2095                                 else if(G.obedit->type==OB_CURVE) {
2096                                         menu1= tb_curve; str1= "Curve";
2097                                         menu2= addmenu_curve;
2098                                         menu3= tb_curve_select;
2099                                         menu4= tb_curve_edit;
2100                                         menu5= tb_transform_editmode1;
2101                                 }
2102                                 else if(G.obedit->type==OB_SURF) {
2103                                         menu1= tb_curve; str1= "Surface";
2104                                         menu2= addmenu_surf; 
2105                                         menu3= tb_curve_select;
2106                                         menu4= tb_curve_edit;
2107                                         menu5= tb_transform_editmode1;
2108                                 }
2109                                 else if(G.obedit->type==OB_MBALL) {
2110                                         menu1= tb_obdata; str1= "Meta";
2111                                         menu2= addmenu_meta;
2112                                         menu3= tb__select;
2113                                         menu4= tb_edit;
2114                                         menu5= tb_transform_editmode2;
2115                                 }
2116                                 else if(G.obedit->type==OB_ARMATURE) {
2117                                         menu1= tb_obdata;str1= "Armature";
2118                                         menu2= addmenu_armature;
2119                                         menu3= tb__select;
2120                                         menu4= tb_edit;
2121                                         menu5= tb_transform_editmode2;
2122                                 }
2123                                 else if(G.obedit->type==OB_LATTICE) {
2124                                         menu1= tb_empty;str1= "Lattice";
2125                                         menu2= tb_empty;
2126                                         menu3= tb__select;
2127                                         menu4= tb_edit;
2128                                         menu5= tb_transform_editmode1;
2129                                 }
2130                         }
2131                 }
2132                 else if (FACESEL_PAINT_TEST) {
2133                         menu3 = tb_face_select;
2134                 }
2135         }
2136         else if(curarea->spacetype==SPACE_NODE) {
2137                 SpaceNode *snode= curarea->spacedata.first;
2138                 
2139                 if(snode->treetype==NTREE_COMPOSIT)
2140                         menu1= tb_node_addcomp; 
2141                 else
2142                         menu1= tb_node_addsh; 
2143                 str1= "Add";
2144                 menu2= tb_node_node; str2= "Node";
2145                 menu3= tb_node_select; str3= "Select";
2146                 menu4= tb_node_transform; str4= "Transform";
2147                 menu5= tb_node_view; str5= "View";
2148                 
2149                 if(snode->treetype==NTREE_SHADER) {
2150                         menu1[0].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_INPUT);
2151                         menu1[1].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OUTPUT);
2152                         menu1[2].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_COLOR);
2153                         menu1[3].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_VECTOR);
2154                         menu1[4].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_CONVERTOR);
2155                         menu1[5].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_GROUP);
2156                         menu1[6].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_DYNAMIC);
2157                 }
2158                 else if(snode->treetype==NTREE_COMPOSIT) {
2159                         menu1[0].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_INPUT);
2160                         menu1[1].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OUTPUT);
2161                         menu1[2].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_COLOR);
2162                         menu1[3].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_VECTOR);
2163                         menu1[4].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_FILTER);
2164                         menu1[5].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_CONVERTOR);
2165                         menu1[6].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_MATTE);
2166                         menu1[7].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_DISTORT);
2167                         menu1[8].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_GROUP);
2168                         menu1[9].poin= node_add_sublevel(&storage, snode->nodetree, NODE_CLASS_OP_DYNAMIC);
2169
2170                 }
2171                 
2172                 dx= 96;
2173                 tot= 5;
2174                 
2175         }
2176         
2177         getmouseco_sc(mval);
2178         
2179         /* create the main buttons menu */
2180         if(tot==6) {
2181         
2182                 /* check if it fits */
2183                 if(mval[0]-1.5*dx+tb_mainx < 6) mval[0]= 6 + 1.5*dx -tb_mainx;
2184                 else if(mval[0]+1.5*dx+tb_mainx > G.curscreen->sizex-6) 
2185                         mval[0]= G.curscreen->sizex-6-1.5*dx-tb_mainx;
2186
2187                 if(mval[1]-20+tb_mainy < 6) mval[1]= 6+20 -tb_mainy;
2188                 else if(mval[1]+20+tb_mainy > G.curscreen->sizey-6) 
2189                         mval[1]= G.curscreen->sizey-6-20-tb_mainy;
2190         
2191                 but=uiDefBlockBut(block, tb_makemenu, menu1, str1,      mval[0]-(1.5*dx)+tb_mainx,mval[1]+tb_mainy, dx, 19, "");
2192                 uiButSetFlag(but, UI_MAKE_TOP|UI_MAKE_RIGHT);
2193                 uiButSetFunc(but, store_main, (void *)(intptr_t)dx, (void *)(intptr_t)-5);
2194
2195                 but=uiDefBlockBut(block, tb_makemenu, menu2, str2,      mval[0]-(0.5*dx)+tb_mainx,mval[1]+tb_mainy, dx, 19, "");
2196                 uiButSetFlag(but, UI_MAKE_TOP);
2197                 uiButSetFunc(but, store_main, (void *)(intptr_t)0, (void *)(intptr_t)-5);
2198
2199                 but=uiDefBlockBut(block, tb_makemenu, menu3, str3,      mval[0]+(0.5*dx)+tb_mainx,mval[1]+tb_mainy, dx, 19, "");
2200                 uiButSetFlag(but, UI_MAKE_TOP|UI_MAKE_LEFT);
2201                 uiButSetFunc(but, store_main, (void *)(intptr_t)-dx, (void *)(intptr_t)-5);
2202
2203                 but=uiDefBlockBut(block, tb_makemenu, menu4, str4,      mval[0]-(1.5*dx)+tb_mainx,mval[1]+tb_mainy-20, dx, 19, "");
2204                 uiButSetFlag(but, UI_MAKE_DOWN|UI_MAKE_RIGHT);
2205                 uiButSetFunc(but, store_main, (void *)(intptr_t)dx, (void *)(intptr_t)5);
2206
2207                 but=uiDefBlockBut(block, tb_makemenu, menu5, str5,      mval[0]-(0.5*dx)+tb_mainx,mval[1]+tb_mainy-20, dx, 19, "");
2208                 uiButSetFlag(but, UI_MAKE_DOWN);
2209                 uiButSetFunc(but, store_main, (void *)(intptr_t)0, (void *)(intptr_t)5);
2210
2211                 but=uiDefBlockBut(block, tb_makemenu, menu6, str6,      mval[0]+(0.5*dx)+tb_mainx,mval[1]+tb_mainy-20, dx, 19, "");
2212                 uiButSetFlag(but, UI_MAKE_DOWN|UI_MAKE_LEFT);
2213                 uiButSetFunc(but, store_main, (void *)(intptr_t)-dx, (void *)(intptr_t)5);
2214         } else if (tot==5 || tot==7) {
2215                 /* check if it fits, dubious */
2216                 if(mval[0]-0.25*dx+tb_mainx < 6) mval[0]= 6 + 0.25*dx -tb_mainx;
2217                 else if(mval[0]+0.25*dx+tb_mainx > G.curscreen->sizex-6)
2218                 mval[0]= G.curscreen->sizex-6-0.25*dx-tb_mainx;
2219
2220                 if(mval[1]-20+tb_mainy < 6) mval[1]= 6+20 -tb_mainy;
2221                 else if(mval[1]+20+tb_mainy > G.curscreen->sizey-6)
2222                         mval[1]= G.curscreen->sizey-6-20-tb_mainy;
2223
2224                 but=uiDefIconTextBlockBut(block, tb_makemenu, menu1, ICON_RIGHTARROW_THIN, str1, mval[0]+tb_mainx,mval[1]+tb_mainy, dx, 19, "");
2225                 uiButSetFlag(but, UI_MAKE_RIGHT);
2226                 uiButSetFunc(but, store_main, (void *)-32, (void *)-5);
2227
2228                 but=uiDefIconTextBlockBut(block, tb_makemenu, menu2, ICON_RIGHTARROW_THIN, str2, mval[0]+tb_mainx,mval[1]+tb_mainy-20, dx, 19, "");
2229                 uiButSetFlag(but, UI_MAKE_RIGHT);
2230                 uiButSetFunc(but, store_main, (void *)-32, (void *)15);
2231
2232                 but=uiDefIconTextBlockBut(block, tb_makemenu, menu3, ICON_RIGHTARROW_THIN, str3, mval[0]+tb_mainx,mval[1]+tb_mainy-40, dx, 19, "");
2233                 uiButSetFlag(but, UI_MAKE_RIGHT);
2234                 uiButSetFunc(but, store_main, (void *)-32, (void *)35);
2235
2236                 but=uiDefIconTextBlockBut(block, tb_makemenu, menu4, ICON_RIGHTARROW_THIN, str4, mval[0]+tb_mainx,mval[1]+tb_mainy-60, dx, 19, "");
2237                 uiButSetFlag(but, UI_MAKE_RIGHT);
2238                 uiButSetFunc(but, store_main, (void *)-32, (void *)55);
2239
2240                 but=uiDefIconTextBlockBut(block, tb_makemenu, menu5, ICON_RIGHTARROW_THIN, str5, mval[0]+tb_mainx,mval[1]+tb_mainy-80, dx, 19, "");
2241                 uiButSetFlag(but, UI_MAKE_RIGHT);
2242                 uiButSetFunc(but, store_main, (void *)-32, (void *)75);
2243                 
2244                 if(tot>5) {
2245                         but=uiDefIconTextBlockBut(block, tb_makemenu, menu6, ICON_RIGHTARROW_THIN, str6, mval[0]+tb_mainx,mval[1]+tb_mainy-100, dx, 19, "");
2246                         uiButSetFlag(but, UI_MAKE_RIGHT);
2247                         uiButSetFunc(but, store_main, (void *)-32, (void *)95);
2248                 }
2249                 if(tot>6) {
2250                         but=uiDefIconTextBlockBut(block, tb_makemenu, menu7, ICON_RIGHTARROW_THIN, str7, mval[0]+tb_mainx,mval[1]+tb_mainy-120, dx, 19, "");
2251                         uiButSetFlag(but, UI_MAKE_RIGHT);
2252                         uiButSetFunc(but, store_main, (void *)-32, (void *)105);
2253                 }
2254         }
2255         
2256         uiBoundsBlock(block, 2);
2257         event= uiDoBlocks(&tb_listb, 0, 1);
2258         
2259         /* free all dynamic entries... */
2260         BLI_freelistN(&storage);
2261         
2262         mywinset(curarea->win);
2263 }
2264
2265 void toolbox_n_add(void)
2266 {
2267         reset_toolbox();
2268         toolbox_n();
2269 }
2270
2271 void reset_toolbox(void)
2272 {
2273         if(U.uiflag & USER_PLAINMENUS) {
2274                 tb_mainx= -32;
2275                 tb_mainy= -5;
2276         } else {
2277                 tb_mainx= 0;
2278                 tb_mainy= -5;
2279         }
2280 }
2281
2282 /* general toolbox for python access */
2283 void toolbox_generic( TBitem *generic_menu )
2284 {
2285         uiBlock *block;
2286         uiBut *but;
2287         TBitem *menu;
2288         int dx=96, first=1, len;
2289         short event, mval[2];
2290         intptr_t ypos = -5;
2291         
2292         tb_mainx= -32;
2293         tb_mainy= -5;
2294         
2295         mywinset(G.curscreen->mainwin); // we go to screenspace
2296         
2297         block= uiNewBlock(&tb_listb, "toolbox", UI_EMBOSSP, UI_HELV, G.curscreen->mainwin);
2298         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
2299         uiBlockSetCol(block, TH_MENU_ITEM);
2300         
2301         getmouseco_sc(mval);
2302         
2303         menu= generic_menu;
2304         while(menu->icon != -1) menu++;
2305         uiBlockSetButmFunc(block, menu->poin, NULL);
2306         
2307         /* Add the menu */
2308         for (menu = generic_menu; menu->icon != -1; menu++) {
2309                 if (first && (len=strlen(menu->name)) > 2 && menu->name[len-2]=='%' && menu->name[len-1]=='t') {
2310                         menu->name[len-2] = '\0';
2311                         uiSetCurFont(block, UI_HELVB);
2312                         uiDefIconTextBut(block, LABEL, 0, ICON_BLANK1, menu->name, mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 19, NULL, 0.0, 0.0, 0, 0, "");
2313                         uiSetCurFont(block, UI_HELV);
2314                         ypos-=20;
2315                 } else if(strcmp(menu->name, "SEPR")==0) {
2316                         uiDefBut(block, SEPR, 0, "", mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 6, NULL, 0.0, 0.0, 0, 0, "");
2317                         ypos-=6;
2318                 } else {
2319                         if (menu->poin) {
2320                                 but=uiDefIconTextBlockBut(block, tb_makemenu, menu->poin, ICON_RIGHTARROW_THIN, menu->name, mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 19, "");
2321                                 uiButSetFlag(but, UI_MAKE_RIGHT);
2322                         
2323                                 uiButSetFunc(but, store_main, (void *)+32, (void *)ypos);
2324                         } else {
2325                                 /* TODO - add icon support */
2326                                 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, menu->name, mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 19, NULL, 0.0, 0.0, 0, menu->retval, "");
2327                         }
2328                         ypos-=20;
2329                 }
2330                 first= 0;
2331         }
2332         
2333         uiBlockSetButmFunc(block, menu->poin, NULL);
2334         
2335         uiBoundsBlock(block, 2);
2336         event= uiDoBlocks(&tb_listb, 0, 1);
2337         
2338         mywinset(curarea->win);
2339         
2340         reset_toolbox();
2341 }
2342
2343 /* save or restore mouse position when entering/exiting menus */
2344 void toolbox_mousepos( short *mpos, int save )
2345 {
2346         static short initpos[2];
2347         static int tog;
2348         
2349         if (save) {
2350                 getmouseco_areawin(mpos);
2351                 initpos[0]= mpos[0];
2352                 initpos[1]= mpos[1];
2353                 tog=1;
2354         } else {
2355                 if (tog) {
2356                         mpos[0]= initpos[0];
2357                         mpos[1]= initpos[1];
2358                 } else {
2359                         getmouseco_areawin(mpos);
2360                 }
2361                 tog= 0;
2362         }
2363 }