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