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