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