Added rename/delete popups to the file and imageselect windows.
[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 #include <math.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #ifndef WIN32
45 #include <unistd.h>
46 #else
47 #include <io.h>
48 #include "BLI_winstuff.h"
49 #endif   
50
51 #include <fcntl.h>
52 #include "MEM_guardedalloc.h"
53
54 #include "BMF_Api.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_arithb.h"
58 #include "BLI_editVert.h"
59
60 #include "DNA_image_types.h"
61 #include "DNA_object_types.h"
62 #include "DNA_screen_types.h"
63 #include "DNA_view3d_types.h"
64
65 #include "BKE_plugin_types.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_global.h"
68 #include "BKE_main.h"
69
70 #include "BIF_gl.h"
71 #include "BIF_graphics.h"
72 #include "BIF_mainqueue.h"
73 #include "BIF_interface.h"
74 #include "BIF_toolbox.h"
75 #include "BIF_mywindow.h"
76 #include "BIF_editarmature.h"
77 #include "BIF_editfont.h"
78 #include "BIF_editmesh.h"
79 #include "BIF_editseq.h"
80 #include "BIF_editlattice.h"
81 #include "BIF_editsima.h"
82 #include "BIF_editoops.h"
83 #include "BIF_imasel.h"
84 #include "BIF_screen.h"
85 #include "BIF_tbcallback.h"
86 #include "BIF_editnla.h"
87
88 #include "BDR_editobject.h"
89 #include "BDR_editcurve.h"
90 #include "BDR_editmball.h"
91
92 #include "BSE_editipo.h"
93 #include "BSE_buttons.h"
94 #include "BSE_filesel.h"
95
96 #include "IMB_imbuf.h"
97
98 #include "mydevice.h"
99 #include "blendef.h"
100
101 #include "interface.h"
102 #include "render.h"
103
104 /*   ********  NOTES  ***********       *****************************
105         
106         - Toolbox items zelf invullen
107         - de colormap kleuren staan met comments in de bgntoolbox()
108         - de funktie extern_qread eventueel vervangen
109         - let op de benaming van bijzondere toetsen (NumL etc) 
110         - meelinken: Button.c,  ivm rawcodes
111         
112         *****************************   *****************************
113 */      
114
115
116 static int tbx1, tbx2, tby1, tby2, tbfontyofs, tbmain=0;
117 static int tbmemx=TBOXX/2, tbmemy=(TBOXEL-0.5)*TBOXH, tboldwin, addmode= 0;
118 static int oldcursor;
119
120         /* variabelen per item */
121 static char *tbstr, *tbstr1, *keystr;   
122 static void (*tbfunc)(int);
123 static int tbval;
124
125 /* *********** PC PATCH ************* */
126
127 void ColorFunc(int i)
128 {
129         if(i==TBOXBLACK) glColor3ub(0, 0, 0);
130         else if(i==TBOXWHITE) glColor3ub(240, 240, 240);
131         else if(i==TBOXGREY) glColor3ub(160, 160, 160);
132         else glColor3ub(0, 0, 0);
133 }
134
135 /* ********************* PYTHON TOOLBOX CALLBACK ************************* */
136
137 #ifdef PY_TOOLBOX 
138 /* see bpython/intern/py_toolbox.c */
139
140 /* moved to BIF_toolbox.h */
141 /* typedef char** (*tbox_callback)(int, int); */
142
143 TBcallback *callback_dummy(int level, int entry)
144 {
145         return NULL;
146 }       
147
148 /* callback func ptr for py_toolbox */
149 Tbox_callbackfunc g_toolbox_menucallback = &callback_dummy;
150
151 void tboxSetCallback(Tbox_callbackfunc f)
152 {
153         g_toolbox_menucallback = f;
154 }
155
156 #endif
157
158 /* ********************* TOOLBOX ITEMS ************************* */
159
160 void tbox_setinfo(int x, int y)
161 {
162         /* afhankelijk van tbmain worden vars gezet */
163         tbstr= 0;
164         tbstr1= 0;
165         tbfunc= 0;
166         tbval= 0;
167         keystr = NULL;
168
169 /* main menu entries: defined in BIF_toolbox.h */
170
171         if(x==0) {
172                 switch(y) {
173                         case TBOX_MAIN_FILE:            tbstr= "FILE";          break;
174                         case TBOX_MAIN_EDIT:            tbstr= "EDIT";          break;
175                         case TBOX_MAIN_ADD:             
176                                 if (addmode==OB_MESH) tbstr= "  MESH";
177                                 else if(addmode==OB_CURVE) tbstr= "  CURVE";
178                                 else if(addmode==OB_SURF) tbstr= "  SURF";
179                                 else tbstr= "ADD";
180                                 break;
181                         case TBOX_MAIN_OBJECT1:         tbstr= "OBJECT";        break;
182                         case TBOX_MAIN_OBJECT2:         tbstr= "OBJECT";        break;
183                         case TBOX_MAIN_MESH:            tbstr= "MESH";          break;
184                         case TBOX_MAIN_CURVE:           tbstr= "CURVE";         break;
185                         case TBOX_MAIN_KEY:                     tbstr= "KEY";           break;
186                         case TBOX_MAIN_RENDER:          tbstr= "RENDER";        break;
187                         case TBOX_MAIN_VIEW:            tbstr= "VIEW";          break;
188                         case TBOX_MAIN_SEQ:             tbstr= "SEQUENCE";      break;
189 #ifdef PY_TOOLBOX
190                         case TBOX_MAIN_PYTOOL:          
191                         {
192                                 if (g_toolbox_menucallback(0, 0)) // valid callback?
193                                         tbstr= "PYTOOL";        
194                                 break;
195                         }
196 #endif
197                 }
198         }
199         
200 /* TOPICS */
201         else {
202                 
203                 
204 /* FILE TOPICS */
205                 if(tbmain==TBOX_MAIN_FILE) {
206                         switch(y) {
207                                 case 0: tbstr= "New";                           tbstr1= "c|x";          keystr= "Ctrl X";       break;
208                                 case 1: tbstr= "Open";                          tbstr1= "F1";           keystr= "F1";           break;
209                                 case 2: tbstr= "Reopen Last";           tbstr1= "c|o";          keystr= "Ctrl O";       break;
210                                 case 3: tbstr= "Append";                        tbstr1= "shift+F1";     keystr= "Shift F1";     break;
211                                 case 4: tbstr= "";                                      tbstr1= "";                     keystr= "";                     break;
212                                 case 5: tbstr= "Save As";                       tbstr1= "F2";           keystr= "F2";           break;
213                                 case 6: tbstr= "Save";                          tbstr1= "c|w";          keystr= "Ctrl W";       break;
214                                 case 7: tbstr= "";                                      tbstr1= "";                     keystr= "";                     break;
215                                 case 8: tbstr= "Save Image";            tbstr1= "F3";           keystr= "F3";           break;
216                                 case 9: tbstr= "Save VRML";                     tbstr1= "c|F2";         keystr= "Ctrl F2";      break;
217                                 case 10: tbstr= "Save DXF";                     tbstr1= "shift+F2";     keystr= "Shift F2";     break;
218                                 case 11: tbstr= "Save VideoScape";      tbstr1= "a|w";          keystr= "Alt W";        break;
219                                 case 12: tbstr= "Save UserPrefs";                                       tbstr1= "c|u";                  keystr= "Ctrl U";                       break;
220                                 case 13: tbstr= "Quit";                         tbstr1= "q";            keystr= "Q";            break;
221                         }
222                 }
223
224 /* EDIT TOPICS */
225                 if(tbmain==TBOX_MAIN_EDIT) {
226                         switch(y) {
227                                 case 0: tbstr= "(De)Select All";        tbstr1= "a";    keystr= "A";            break;
228                                 case 1: tbstr= "Border Select";         tbstr1= "b";    keystr= "B";            break;
229                                 case 2: tbstr= "Select Linked";                                 tbstr1= "l";            keystr= "L";                    break;
230                                 case 3: tbstr= "Hide Selected";                                 tbstr1= "h";            keystr= "H";                    break;
231                                 case 4: tbstr= "Duplicate";                     tbstr1= "D";    keystr= "Shift D";      break;
232                                 case 5: tbstr= "Delete";                        tbstr1= "x";    keystr= "X";            break;
233                                 case 6: tbstr= "Edit Mode";                     tbstr1= "Tab";  keystr= "Tab";          break;
234                                 case 7: tbstr= "Grabber";                       tbstr1= "g";    keystr= "G";            break;
235                                 case 8: tbstr= "Rotate";                        tbstr1= "r";    keystr= "R";            break;
236                                 case 9: tbstr= "Scale";                         tbstr1= "s";    keystr= "S";            break;
237                                 case 10: tbstr= "Shrink/Fatten";                                        tbstr1= "a|s";          keystr= "Alt S";                        break;
238                                 case 11: tbstr= "Shear";                        tbstr1= "c|s";  keystr= "Ctrl S";       break;
239                                 case 12: tbstr= "Warp/Bend";            tbstr1= "W";    keystr= "Shift W";      break;
240                                 case 13: tbstr= "Snap Menu";            tbstr1= "S";    keystr= "Shift S";      break;
241                         }
242                 }
243
244 /* ADD TOPICS */
245                 if(tbmain==TBOX_MAIN_ADD) {
246
247                         if(addmode==0) {
248                                 switch(y) {
249                                         case 0: tbstr= "Mesh";          tbstr1= ">>";   keystr= ">>";   tbval=OB_MESH;                                                                          break;
250                                         case 1: tbstr= "Curve";         tbstr1= ">>";   keystr= ">>";   tbval=OB_CURVE; ;                                                                       break;
251                                         case 2: tbstr= "Surface";       tbstr1= ">>";   keystr= ">>";   tbval=OB_SURF;                                                                          break;
252                                         case 3: tbstr= "Text";          tbstr1= "";             keystr= "";             tbval=OB_FONT;          tbfunc= add_primitiveFont;              break;
253                                         case 4: tbstr= "MetaBall";      tbstr1= "";             keystr= "";             tbval=OB_MBALL;         tbfunc= add_primitiveMball;             break;
254                                         case 5: tbstr= "Empty";         tbstr1= "A";    keystr= "";             tbval=OB_EMPTY;                                                                         break;
255                                         case 6: tbstr= "";                      tbstr1= "";             keystr= "";             tbval=0;                                                                                        break;
256                                         case 7: tbstr= "Camera";        tbstr1= "A";    keystr= "";             tbval=OB_CAMERA;                                                                        break;
257                                         case 8: tbstr= "Lamp";          tbstr1= "A";    keystr= "";             tbval=OB_LAMP;                                                                          break;
258                                         case 9: tbstr= "Armature";      tbstr1= "";             keystr= "";             tbval=OB_ARMATURE;      tbfunc=add_primitiveArmature;   break;
259                                         case 10: tbstr= "";                     tbstr1= "";             keystr= "";             tbval=0;                                                                                        break;
260                                         case 11: tbstr= "Lattice";      tbstr1= "A";    keystr= "";             tbval=OB_LATTICE;                                                                       break;
261                                         case 12: tbstr= "";                     tbstr1= "";             keystr= "";             tbval=0;                                                                                        break;
262                                         case 13: tbstr= "";                     tbstr1= "";             keystr= "";             tbval=0;                                                                                        break;
263                                 }
264                                 if(tbstr1 && tbstr1[0]=='A') tbfunc= (void (*)(int) )add_object_draw;
265                         }
266                         else if(addmode==OB_MESH) {             
267                                 switch(y) {
268                                         case 0: tbstr= ">Plane";        tbstr1= "A";    keystr= "";             tbval=0;        break;
269                                         case 1: tbstr= ">Cube";         tbstr1= "A";    keystr= "";             tbval=1;        break;
270                                         case 2: tbstr= ">Circle";       tbstr1= "A";    keystr= "";             tbval=4;        break;
271                                         case 3: tbstr= ">UVsphere";     tbstr1= "A";    keystr= "";             tbval=11;       break;
272                                         case 4: tbstr= ">Icosphere";tbstr1= "A";        keystr= "";             tbval=12;       break;
273                                         case 5: tbstr= ">Cylinder";     tbstr1= "A";    keystr= "";             tbval=5;        break;
274                                         case 6: tbstr= ">Tube";         tbstr1= "A";    keystr= "";             tbval=6;        break;
275                                         case 7: tbstr= ">Cone";         tbstr1= "A";    keystr= "";             tbval=7;        break;
276                                         case 8: tbstr= ">";                     tbstr1= "";             keystr= "";                                     break;
277                                         case 9: tbstr= ">Grid";         tbstr1= "A";    keystr= "";             tbval=10;       break;
278                                         case 13: tbstr= ">Monkey";      tbstr1= "A";    keystr= "";             tbval=13;       break;
279                                 }
280                                 if(tbstr1 && tbstr1[0]=='A') tbfunc= add_primitiveMesh;
281                         }
282                         else if(addmode==OB_SURF) {
283                                 switch(y) {
284                                         case 0: tbstr= ">Curve";        tbstr1= "A";    keystr= "";             tbval=0; break;
285                                         case 1: tbstr= ">Circle";       tbstr1= "A";    keystr= "";             tbval=1; break;
286                                         case 2: tbstr= ">Surface";      tbstr1= "A";    keystr= "";             tbval=2; break;
287                                         case 3: tbstr= ">Tube";         tbstr1= "A";    keystr= "";             tbval=3; break;
288                                         case 4: tbstr= ">Sphere";       tbstr1= "A";    keystr= "";             tbval=4; break;
289                                         case 5: tbstr= ">Donut";        tbstr1= "A";    keystr= "";             tbval=5; break;
290                                 }
291                                 if(tbstr1 && tbstr1[0]=='A') tbfunc= add_primitiveNurb;
292                         }
293 /*                      else if (addmode==OB_ARMATURE){
294                                 switch(y) {
295                                         case 0: tbstr= ">Bone";         tbstr1= "A";    keystr= "";             tbval=0; break;
296                                         case 1: tbstr= ">Hand";         tbstr1= "A";    keystr= "";             tbval=1; break;
297                                         case 2: tbstr= ">Biped";        tbstr1= "A";    keystr= "";             tbval=2; break;
298                                 }
299                                 if(tbstr1 && tbstr1[0]=='A') tbfunc= add_primitiveArmature;     
300                         }
301 */
302                         else if(addmode==OB_CURVE) {
303                                 switch(y) {
304                                         case 0: tbstr= ">Bezier Curve";         tbstr1= "A";    keystr= "";     tbval=10;       break;
305                                         case 1: tbstr= ">Bezier Circle";        tbstr1= "A";    keystr= "";     tbval=11;       break;
306                                         case 2: tbstr= ">";                                     tbstr1= "";             keystr= "";                             break;
307                                         case 3: tbstr= ">Nurbs Curve";          tbstr1= "A";    keystr= "";     tbval=40;       break;
308                                         case 4: tbstr= ">Nurbs Circle";         tbstr1= "A";    keystr= "";     tbval=41;       break;
309                                         case 5: tbstr= ">";                                     tbstr1= "";             keystr= "";                             break;
310                                         case 6: tbstr= ">Path";                         tbstr1= "A";    keystr= "";     tbval=46;       break;
311                                 }
312                                 if(tbstr1 && tbstr1[0]=='A') tbfunc= add_primitiveCurve;
313                         }
314 /*                      else if(addmode==OB_MBALL) {
315                                 switch(y) {
316                                         case 0: tbstr= "Ball";          tbstr1= "A";    tbval=1; break;
317                                         case 1: tbstr= "";                      tbstr1= "";             break;
318                                         case 2: tbstr= "";                      tbstr1= "";             break;
319                                         case 3: tbstr= "";                      tbstr1= "";             break;
320                                         case 4: tbstr= "";                      tbstr1= "";             break;
321                                         case 5: tbstr= "";                      tbstr1= "";             break;
322                                         case 6: tbstr= "";                      tbstr1= "";             break;
323                                         case 7: tbstr= "";                      tbstr1= "";             break;
324                                         case 8: tbstr= "";                      tbstr1= "";             break;
325                                         case 9: tbstr= "";                      tbstr1= "";             break;
326                                         case 10: tbstr= "";                     tbstr1= "";             break;
327                                         case 11: tbstr= "Duplicate";tbstr1= "D";        break;
328                                 }
329                                 if(tbstr1 && tbstr1[0]=='A') tbfunc= add_primitiveMball;
330                         }*/
331                 }
332                 
333 /* OB TOPICS 1 */
334                 else if(tbmain==TBOX_MAIN_OBJECT1) {
335                         switch(y) {
336                                 case 0: tbstr= "Clear Size";            tbstr1= "a|s";  keystr= "Alt S";        break;
337                                 case 1: tbstr= "Clear Rotation";        tbstr1= "a|r";  keystr= "Alt R";        break;
338                                 case 2: tbstr= "Clear Location";        tbstr1= "a|g";  keystr= "Alt G";        break;
339                                 case 3: tbstr= "Clear Origin";          tbstr1= "a|o";  keystr= "Alt O";        break;
340                                 case 4: tbstr= "Make Parent";           tbstr1= "c|p";  keystr= "Ctrl P";       break;
341                                 case 5: tbstr= "Clear Parent";          tbstr1= "a|p";  keystr= "Alt P";        break;
342 /*      Unkown what tbstr1 should be...
343                                 case 6: tbstr= "MkVert Parent";         tbstr1= "c|a|p";        keystr= "Ctrl Alt P";   break;
344 */
345                                 case 7: tbstr= "Make Track";            tbstr1= "c|t";  keystr= "Ctrl T";       break;
346                                 case 8: tbstr= "Clear Track";           tbstr1= "a|t";  keystr= "Alt T";        break;
347 /*                              case 9: tbstr= "";                                      tbstr1= "";             keystr= "";                     break; */
348                                 case 10: tbstr= "Image Displist";       tbstr1= "c|d";  keystr= "Ctrl D";       break;
349                                 case 11: tbstr= "Image Aspect";         tbstr1= "a|v";  keystr= "Alt V";        break;
350                                 case 12: tbstr= "Vect Paint";           tbstr1= "v";    keystr= "V";    break;
351                         }
352                 }
353                 
354 /* OB TOPICS 2 */
355                 else if(tbmain==TBOX_MAIN_OBJECT2) {
356                         switch(y) {
357                                 case 0: tbstr= "Edit Mode";                     tbstr1= "Tab";  keystr= "Tab";                  break;
358                                 case 1: tbstr= "Move To Layer";         tbstr1= "m";    keystr= "M";                    break;
359                                 case 2: tbstr= "Delete";                        tbstr1= "x";    keystr= "X";                    break;
360                                 case 3: tbstr= "Delete All";            tbstr1= "c|x";  keystr= "Ctrl X";               break;
361                                 case 4: tbstr= "Apply Size/Rot";        tbstr1= "c|a";  keystr= "Ctrl A";               break;
362                                 case 5: tbstr= "Apply Deform";          tbstr1= "c|A";  keystr= "Ctrl Shift A"; break;
363                                 case 6: tbstr= "Join";                          tbstr1= "c|j";  keystr= "Ctrl J";               break;
364                                 case 7: tbstr= "Make Local";            tbstr1= "l";    keystr= "L";                    break;
365                                 case 8: tbstr= "Select Linked";         tbstr1= "L";    keystr= "Shift L";              break;
366                                 case 9: tbstr= "Make Links";            tbstr1= "c|l";  keystr= "Ctrl L";               break;
367                                 case 10: tbstr= "Copy Menu";            tbstr1= "c|c";  keystr= "Ctrl C";               break;
368                                 case 11: tbstr= "Convert Menu";         tbstr1= "a|c";  keystr= "Alt C";                break;
369                                 case 12: tbstr= "Boolean Op";           tbstr1= "w";    keystr= "W";            break;
370                         }
371                 }
372
373 /* mesh TOPICS */
374                 else if(tbmain==TBOX_MAIN_MESH) {
375                         switch(y) {
376                                 case 0: tbstr= "Select Linked";         tbstr1= "l";    keystr= "L";            break;
377                                 case 1: tbstr= "Deselect Linked";       tbstr1= "L";    keystr= "Shift L";      break;
378                                 case 2: tbstr= "Extrude";                       tbstr1= "e";    keystr= "E";            break;
379                                 case 3: tbstr= "Delete Menu";           tbstr1= "x";    keystr= "X";            break;
380                                 case 4: tbstr= "Make edge/face";        tbstr1= "f";    keystr= "F";            break;
381                                 case 5: tbstr= "Fill";                          tbstr1= "F";    keystr= "Shift F";      break;
382                                 case 6: tbstr= "Split";                         tbstr1= "y";    keystr= "Y";            break;
383                                 case 7: tbstr= "Undo/reload";           tbstr1= "u";    keystr= "U";            break;
384                                 case 8: tbstr= "Calc Normals";          tbstr1= "c|n";  keystr= "Ctrl N";       break;
385                                 case 9: tbstr= "Separate";                      tbstr1= "p";    keystr= "P";            break;
386                                 case 10: tbstr= "Write Videosc";        tbstr1= "a|w";  keystr= "Alt W";        break;
387 /*                              case 11: tbstr= "";                                     tbstr1= "";             keystr= "";                     break; */
388                         }
389                 }
390         
391 /* CURVE TOPICS */
392                 else if(tbmain==TBOX_MAIN_CURVE) {
393                         switch(y) {
394                                 case 0: tbstr= "Select Linked";         tbstr1= "l";    keystr= "L";            break;
395                                 case 1: tbstr= "Deselect Linked";       tbstr1= "L";    keystr= "Shift L";      break;
396                                 case 2: tbstr= "Extrude";                       tbstr1= "e";    keystr= "E";            break;
397                                 case 3: tbstr= "Delete Menu";           tbstr1= "x";    keystr= "X";            break;
398                                 case 4: tbstr= "Make Segment";          tbstr1= "f";    keystr= "F";            break;
399                                 case 5: tbstr= "Cyclic";                        tbstr1= "c";    keystr= "C";            break;
400 /*                              case 6: tbstr= "";                                      tbstr1= "";             keystr= "";                     break; */
401                                 case 7: tbstr= "Select Row";            tbstr1= "R";    keystr= "Shift R";      break;
402                                 case 8: tbstr= "Calc Handle";           tbstr1= "h";    keystr= "H";            break;
403                                 case 9: tbstr= "Auto Handle";           tbstr1= "H";    keystr= "Shift H";      break;
404                                 case 10: tbstr= "Vect Handle";          tbstr1= "v";    keystr= "V";            break;
405                                 case 11: tbstr= "Specials";                     tbstr1= "w";    keystr= "W";            break;
406                         }
407                 }
408         
409 /* KEY TOPICS */
410                 else if(tbmain==TBOX_MAIN_KEY) {
411                         switch(y) {
412                                 case 0: tbstr= "Insert";        tbstr1= "i";            keystr= "I";            break;
413                                 case 1: tbstr= "Show";          tbstr1= "k";            keystr= "K";            break;
414                                 case 2: tbstr= "Next";          tbstr1= "PageUp";       keystr= "PgUp";         break;
415                                 case 3: tbstr= "Prev";          tbstr1= "PageDn";       keystr= "PgDn";         break;
416                                 case 4: tbstr= "Show+Sel";      tbstr1= "K";            keystr= "Shift K";      break;
417 /*                              case 5: tbstr= "";                      tbstr1= "";                     keystr= "";                     break;
418                                 case 6: tbstr= "";                      tbstr1= "";                     keystr= "";                     break;
419                                 case 7: tbstr= "";                      tbstr1= "";                     keystr= "";                     break;
420                                 case 8: tbstr= "";                      tbstr1= "";                     keystr= "";                     break;
421                                 case 9: tbstr= "";                      tbstr1= "";                     keystr= "";                     break;
422                                 case 10: tbstr= "";                     tbstr1= "";                     keystr= "";                     break;
423                                 case 11: tbstr= "";                     tbstr1= "";                     keystr= "";                     break; */
424                         }
425                 }
426 /* SEQUENCER TOPICS */
427                 else if(tbmain==TBOX_MAIN_SEQ) {
428                         switch(y) {
429                                 case 0: tbstr= "Add Strip"; tbstr1= "A";  keystr= "Shift A";          break;
430                                 case 1: tbstr= "Change Str"; tbstr1= "c";  keystr= "C";          break;
431                                 case 2: tbstr= "Delete Str";                              tbstr1= "x";             keystr= "X";                     break;
432                                 case 3: tbstr= "Make Meta";    tbstr1= "m";    keystr= "M";      break;
433                                 case 4: tbstr= "Str Params";    tbstr1= "n";    keystr= "N";            break;
434                         }
435                 }
436
437 /* RENDER TOPICS */
438                 else if(tbmain==TBOX_MAIN_RENDER) {
439                         switch(y) {
440                                 case 0: tbstr= "Render Window"; tbstr1= "F11";  keystr= "F11";          break;
441                                 case 1: tbstr= "Render";                tbstr1= "F12";  keystr= "F12";          break;
442                                 case 2: tbstr= "Set Border";    tbstr1= "B";    keystr= "Shift B";      break;
443                                 case 3: tbstr= "Image Zoom";    tbstr1= "z";    keystr= "Z";            break;
444 /*                              case 4: tbstr= "";                              tbstr1= "";             keystr= "";                     break;
445                                 case 5: tbstr= "";                              tbstr1= "";             keystr= "";                     break;
446                                 case 6: tbstr= "";                              tbstr1= "";             keystr= "";                     break;
447                                 case 7: tbstr= "";                              tbstr1= "";             keystr= "";                     break;
448                                 case 8: tbstr= "";                              tbstr1= "";             keystr= "";                     break;
449                                 case 9: tbstr= "";                              tbstr1= "";             keystr= "";                     break;
450                                 case 10: tbstr= "";                             tbstr1= "";             keystr= "";                     break;
451                                 case 11: tbstr= "";                             tbstr1= "";             keystr= "";                     break; */
452                         }
453                 }
454         
455 /* VIEW TOPICS */
456                 else if(tbmain==TBOX_MAIN_VIEW) {
457                         switch(y) {
458 /*                              case 0: tbstr= "";              tbstr1= "";     break;
459                                 case 1: tbstr= "";              tbstr1= "";     break;
460                                 case 2: tbstr= "";              tbstr1= "";     break;
461                                 case 3: tbstr= "";              tbstr1= "";     break; */
462                                 case 4: tbstr= "Centre";                tbstr1= "c";    keystr= "C";            break;
463                                 case 5: tbstr= "Home";                  tbstr1= "C";    keystr= "Shift C";      break;
464 /*                              case 6: tbstr= "";              tbstr1= "";     break;
465                                 case 7: tbstr= "";              tbstr1= "";     break;
466                                 case 8: tbstr= "";              tbstr1= "";     break;*/
467                                 case 9: tbstr= "Z-Buffer";              tbstr1= "z";    keystr= "Z";            break;
468 /*                              case 10: tbstr= "";             tbstr1= "";     break;
469                                 case 11: tbstr= "";             tbstr1= "";     break;*/
470                         }
471                 }
472 #ifdef PY_TOOLBOX
473                 else if(tbmain==TBOX_MAIN_PYTOOL) {
474                         TBcallback *t= g_toolbox_menucallback(0, y); // call python menu constructor
475                         if (t) { 
476                                 tbstr = t->desc; 
477                                 keystr = t->key;
478                                 tbfunc = t->cb;
479                                 tbval = t->val;
480                         }
481                 }
482 #endif
483         }
484 }
485
486 /* ******************** INIT ************************** */
487
488 void bgnpupdraw(int startx, int starty, int endx, int endy)
489 {
490         #if defined(__sgi) || defined(__sun__)
491         /* this is a dirty patch: XgetImage gets sometimes the backbuffer */
492         my_get_frontbuffer_image(0, 0, 1, 1);
493         my_put_frontbuffer_image();
494         #endif
495
496         tboldwin= mywinget();
497
498         mywinset(G.curscreen->mainwin);
499         
500         /* pietsje groter, 1 pixel aan de rand */
501         
502         glReadBuffer(GL_FRONT);
503         glDrawBuffer(GL_FRONT);
504
505         glFinish();
506
507         my_get_frontbuffer_image(startx-1, starty-4, endx-startx+5, endy-starty+6);
508
509         oldcursor= get_cursor();
510         set_cursor(CURSOR_STD);
511         
512         tbfontyofs= (TBOXH-11)/2 +2;    /* toolbox, hier stond ooit getheigh */
513 }
514
515 void endpupdraw(void)
516 {
517         glFinish();
518         my_put_frontbuffer_image();
519         
520         if(tboldwin) {
521                 mywinset(tboldwin);
522                 set_cursor(oldcursor);
523         }
524
525         glReadBuffer(GL_BACK);
526         glDrawBuffer(GL_BACK);
527 }
528
529 /* ********************************************** */
530
531 void asciitoraw(int ch, unsigned short *event, unsigned short *qual)
532 {
533         if( isalpha(ch)==0 ) return;
534         
535         if( isupper(ch) ) {
536                 *qual= LEFTSHIFTKEY;
537                 ch= tolower(ch);
538         }
539         
540         switch(ch) {
541         case 'a': *event= AKEY; break;
542         case 'b': *event= BKEY; break;
543         case 'c': *event= CKEY; break;
544         case 'd': *event= DKEY; break;
545         case 'e': *event= EKEY; break;
546         case 'f': *event= FKEY; break;
547         case 'g': *event= GKEY; break;
548         case 'h': *event= HKEY; break;
549         case 'i': *event= IKEY; break;
550         case 'j': *event= JKEY; break;
551         case 'k': *event= KKEY; break;
552         case 'l': *event= LKEY; break;
553         case 'm': *event= MKEY; break;
554         case 'n': *event= NKEY; break;
555         case 'o': *event= OKEY; break;
556         case 'p': *event= PKEY; break;
557         case 'q': *event= QKEY; break;
558         case 'r': *event= RKEY; break;
559         case 's': *event= SKEY; break;
560         case 't': *event= TKEY; break;
561         case 'u': *event= UKEY; break;
562         case 'v': *event= VKEY; break;
563         case 'w': *event= WKEY; break;
564         case 'x': *event= XKEY; break;
565         case 'y': *event= YKEY; break;
566         case 'z': *event= ZKEY; break;
567         }
568 }
569
570 void tbox_execute(void)
571 {
572         /* als tbfunc: functie aanroepen */
573         /* als tbstr1 is een string: value tbval in queue stopen */
574         unsigned short event=0;
575         unsigned short qual1=0, qual2=0;
576
577         if(tbfunc) {
578                 tbfunc(tbval);
579         }
580         else if(tbstr1) {
581                 if(strcmp(tbstr1, "Tab")==0) {
582                         event= TABKEY;
583                 }
584                 else if(strcmp(tbstr1, "PageUp")==0) {
585                         event= PAGEUPKEY;
586                 }
587                 else if(strcmp(tbstr1, "PageDn")==0) {
588                         event= PAGEDOWNKEY;
589                 }
590                 else if(strcmp(tbstr1, "shift+F1")==0) {
591                         qual1= LEFTSHIFTKEY;
592                         event= F1KEY;
593                 }
594                 else if(strcmp(tbstr1, "shift+F2")==0) {
595                         qual1= LEFTSHIFTKEY;
596                         event= F2KEY;
597                 }
598                 /* ctrl-s (Shear): switch into editmode ### */
599                 else if(strcmp(tbstr1, "c|s")==0) {
600                         if (!G.obedit) {
601                                 enter_editmode();
602                                 /* ### put these into a deselectall_gen() */
603                                 if(G.obedit->type==OB_MESH) deselectall_mesh();
604                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) deselectall_nurb();
605                                 else if(G.obedit->type==OB_MBALL) deselectall_mball();
606                                 else if(G.obedit->type==OB_LATTICE) deselectall_Latt();
607                                 /* ### */
608                         }
609                         qual1 = LEFTCTRLKEY;
610                         event = SKEY;
611                 }
612                 else if(strcmp(tbstr1, "W")==0) {
613                         if (!G.obedit) {
614                                 enter_editmode();
615                                 /* ### put these into a deselectall_gen() */
616                                 if(G.obedit->type==OB_MESH) deselectall_mesh();
617                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) deselectall_nurb();
618                                 else if(G.obedit->type==OB_MBALL) deselectall_mball();
619                                 else if(G.obedit->type==OB_LATTICE) deselectall_Latt();
620                                 /* ### */
621                         }
622                         qual1 = LEFTSHIFTKEY;
623                         event = WKEY;
624                 }
625
626                 else if(strlen(tbstr1)<4 || (strlen(tbstr1)==4 && tbstr1[2]=='F')) {
627                                 
628                         if(tbstr1[1]=='|') {
629                                 if(tbstr1[0]=='c') qual1= LEFTCTRLKEY;
630                                 else if(tbstr1[0]=='a') qual1= LEFTALTKEY;
631                                 
632                                 if (tbstr1[2]=='F') {
633                                         switch(tbstr1[3]) {
634                                         case '1': event= F1KEY; break;
635                                         case '2': event= F2KEY; break;
636                                         case '3': event= F3KEY; break;
637                                         case '4': event= F4KEY; break;
638                                         case '5': event= F5KEY; break;
639                                         case '6': event= F6KEY; break;
640                                         case '7': event= F7KEY; break;
641                                         case '8': event= F8KEY; break;
642                                         case '9': event= F9KEY; break;
643                                         }
644                                 }
645                                 else asciitoraw(tbstr1[2], &event, &qual2);
646                         }
647                         else if(tbstr1[1]==0) {
648                                 asciitoraw(tbstr1[0], &event, &qual2);
649                         }
650                         else if(tbstr1[0]=='F') {
651                                 event= atoi(tbstr1+1);
652                                 switch(event) {
653                                         case 1: event= F1KEY; break;
654                                         case 2: event= F2KEY; break;
655                                         case 3: event= F3KEY; break;
656                                         case 4: event= F4KEY; break;
657                                         case 5: event= F5KEY; break;
658                                         case 6: event= F6KEY; break;
659                                         case 7: event= F7KEY; break;
660                                         case 8: event= F8KEY; break;
661                                         case 9: event= F9KEY; break;
662                                         case 10: event= F10KEY; break;
663                                         case 11: event= F11KEY; break;
664                                         case 12: event= F12KEY; break;
665                                 }
666                         }
667                 }
668                 
669                 if(event) {
670                         if(qual1) mainqenter(qual1, 1);
671                         if(qual2) mainqenter(qual2, 1);
672                         mainqenter(event, 1);
673                         mainqenter(event, 0);
674                         mainqenter(EXECUTE, 1);
675                         if(qual1) mainqenter(qual1, 0);
676                         if(qual2) mainqenter(qual2, 0);
677                 }
678         }
679         
680 }
681
682 void tbox_getmouse(mval)
683 short *mval;
684 {
685
686         getmouseco_sc(mval);
687
688 }
689
690 void tbox_setmain(int val)
691 {
692         tbmain= val;
693
694         if(tbmain==0 && G.obedit) {
695                 addmode= G.obedit->type;
696         }
697 }
698
699 void bgntoolbox(void)
700 {
701         short xmax, ymax, mval[2];
702         
703         xmax = G.curscreen->sizex;
704         ymax = G.curscreen->sizey;
705
706         tbox_getmouse(mval);
707         
708         if(mval[0]<95) mval[0]= 95;
709         if(mval[0]>xmax-95) mval[0]= xmax-95;
710
711         warp_pointer(mval[0], mval[1]);
712
713         tbx1= mval[0]-tbmemx;
714         tby1= mval[1]-tbmemy;
715         if(tbx1<10) tbx1= 10;
716         if(tby1<10) tby1= 10;
717         
718         tbx2= tbx1+TBOXX;
719         tby2= tby1+TBOXY;
720         if(tbx2>xmax) {
721                 tbx2= xmax-10;
722                 tbx1= tbx2-TBOXX;
723         }
724         if(tby2>ymax) {
725                 tby2= ymax-10;
726                 tby1= tby2-TBOXY;
727         }
728
729         bgnpupdraw(tbx1, tby1, tbx2, tby2);
730 }
731
732 void endtoolbox(void)
733 {
734         short mval[2];
735         
736         tbox_getmouse(mval);
737         if(mval[0]>tbx1 && mval[0]<tbx2)
738                 if(mval[1]>tby1 && mval[1]<tby2) {
739                         tbmemx= mval[0]-(tbx1);
740                         tbmemy= mval[1]-(tby1);
741         }
742         
743         endpupdraw();
744 }
745
746
747 void tbox_embossbox(short x1, short y1, short x2, short y2, short type) 
748 /* type: 0=menu, 1=menusel, 2=topic, 3=topicsel */
749 {
750         
751         if(type==0) {
752                 glColor3ub(160, 160, 160);
753                 glRects(x1+1, y1+1, x2-1, y2-1);
754         }
755         if(type==1) {
756                 glColor3ub(50, 50, 100);
757                 glRects(x1+1, y1+1, x2-1, y2-1);
758         }
759         if(type==2) {
760                 glColor3ub(190, 190, 190);
761                 glRects(x1+1, y1+1, x2-1, y2-1);
762         }
763         if(type==3) {
764                 cpack(0xc07070);
765                 glRects(x1+1, y1+1, x2-1, y2-1);
766         }
767         
768         if(type & 1) cpack(0xFFFFFF);
769         else cpack(0x0);
770 }
771
772
773 void tbox_drawelem_body(x, y, type)
774 {
775         int x1 = 0, y1, x2 = 0, y2;
776         
777         if(x==0) {
778                 x1= tbx1; x2= tbx1+TBOXXL;
779         }
780         else if(x==1) {
781                 x1= tbx1+TBOXXL;
782                 x2= x1+ TBOXXR-1;
783         }
784         
785         y1= tby1+ (TBOXEL-y-1)*TBOXH;
786         y2= y1+TBOXH-1;
787         
788         tbox_embossbox(x1, y1, x2, y2, type);
789         
790 }
791
792 void tbox_drawelem_text(x, y, type)
793 {
794         int x1 = 0, y1, x2 = 0, y2, len1, len2;
795         
796         if(x==0) {
797                 x1= tbx1; x2= tbx1+TBOXXL;
798         }
799         else if(x==1) {
800                 x1= tbx1+TBOXXL;
801                 x2= x1+ TBOXXR-1;
802         }
803         
804         y1= tby1+ (TBOXEL-y-1)*TBOXH;
805         y2= y1+TBOXH-1;
806         
807         if(type==0 || type==2) {
808                 ColorFunc(TBOXBLACK);
809         }
810         else {
811                 glColor3ub(240, 240, 240);
812         }
813         
814         /* tekst */
815         tbox_setinfo(x, y);
816         if(tbstr && tbstr[0]) {
817                 len1= 5+BMF_GetStringWidth(G.font, tbstr);
818 //              if(tbstr1) len2= 5+BMF_GetStringWidth(G.font, tbstr1); else len2= 0;
819                 if(keystr) len2= 5+BMF_GetStringWidth(G.font, keystr); else len2= 0;
820                 
821                 while(len1>0 && (len1+len2+5>x2-x1) ) {
822                         tbstr[strlen(tbstr)-1]= 0;
823                         len1= BMF_GetStringWidth(G.font, tbstr);
824                 }
825                 
826                 glRasterPos2i(x1+5, y1+tbfontyofs);
827                 BMF_DrawString(G.font, tbstr);
828                 
829 //              if(tbstr1 && tbstr1[0]) {
830                 if(keystr && keystr[0]) {
831                         if(type & 1) {
832                                 ColorFunc(TBOXBLACK);
833         
834                                 glRecti(x2-len2-2,  y1+2,  x2-3,  y2-2);
835                                 ColorFunc(TBOXWHITE);
836                                 glRasterPos2i(x2-len2,  y1+tbfontyofs);
837 //                              BMF_DrawString(G.font, tbstr1);
838                                 BMF_DrawString(G.font, keystr);
839                         }
840                         else {
841                                 ColorFunc(TBOXBLACK);
842                                 glRasterPos2i(x2-len2,  y1+tbfontyofs);
843 //                              BMF_DrawString(G.font, tbstr1);
844                                 BMF_DrawString(G.font, keystr);
845                         }
846                 }
847         }
848 }
849
850
851 void tbox_drawelem(x, y, type)
852 int x, y, type; 
853 {
854         /* type: 0=menu, 1=menusel, 2=topic, 3=topicsel */
855
856         tbox_drawelem_body(x, y, type);
857         tbox_drawelem_text(x, y, type);
858         
859 }
860
861 void tbox_getactive(x, y)
862 int *x, *y;
863 {
864         short mval[2];
865         
866         tbox_getmouse(mval);
867         
868         mval[0]-=tbx1;
869         if(mval[0]<TBOXXL) *x= 0;
870         else *x= 1;
871         
872         *y= mval[1]-tby1;
873         *y/= TBOXH;
874         *y= TBOXEL- *y-1;
875         if(*y<0) *y= 0;
876         if(*y>TBOXEL-1) *y= TBOXEL-1;
877         
878 }
879
880 void drawtoolbox(void)
881 {
882         int x, y, actx, acty, type;
883
884         tbox_getactive(&actx, &acty);
885
886         /* de background */
887         for(x=0; x<2; x++) {
888                 
889                 for(y=0; y<TBOXEL; y++) {
890                         
891                         if(x==0) type= 0; 
892                         else type= 2;
893                         
894                         if(actx==x && acty==y) type++;
895                         if(type==0) {
896                                 if(tbmain==y) type= 1;
897                         }
898                         
899                         tbox_drawelem_body(x, y, type);
900                         
901                 }
902         }
903
904         /* de text */
905         for(x=0; x<2; x++) {
906                 
907                 for(y=0; y<TBOXEL; y++) {
908                         
909                         if(x==0) type= 0; 
910                         else type= 2;
911                         
912                         if(actx==x && acty==y) type++;
913                         if(type==0) {
914                                 if(tbmain==y) type= 1;
915                         }
916                         
917                         tbox_drawelem_text(x, y, type);
918                         
919                 }
920         }
921         glFinish();             /* for geforce, to show it in the frontbuffer */
922
923 }
924
925
926 void toolbox(void)
927 {
928         int actx, acty, y;
929         unsigned short event;
930         short val, mval[2], xo= -1, yo=0;
931         
932         bgntoolbox();
933         glColor3ub(0xB0, 0xB0, 0xB0);
934         uiDrawMenuBox((float)tbx1, (float)tby1-1, (float)tbx2, (float)tby2);
935         drawtoolbox();
936         
937         /* 
938          *      De aktieve window wordt in queue terug gestopt.
939          */
940
941         while(1) {
942                 event= extern_qread(&val);
943                 if(event) {
944                         switch(event) {
945                                 case LEFTMOUSE: case MIDDLEMOUSE: case RIGHTMOUSE: case RETKEY: case PADENTER:
946                                         if(val==1) {
947                                                 tbox_getactive(&actx, &acty);
948                                                 tbox_setinfo(actx, acty);
949                                                 
950                                                 if(event==RIGHTMOUSE) {
951                                                         if(addmode) {
952                                                                 addmode= 0;
953                                                                 drawtoolbox();
954                                                         }
955                                                 }
956                                                 else if(tbstr1 && tbstr1[0]=='>') {
957                                                         addmode= tbval;
958                                                         drawtoolbox();
959                                                 }
960                                                 else {
961                                                         endtoolbox();
962                                                         tbox_execute();
963                                                         return;
964                                                 }
965                                         }
966                                         break;
967                                 case ESCKEY:
968                                         /* altkeys: om conflicten met overdraw en stow/push/pop te voorkomen */
969 #ifndef MAART
970 /* Temporary for making screen dumps (Alt+PrtSc) */
971                                 case LEFTALTKEY:
972                                 case RIGHTALTKEY:
973 #endif /* MAART */
974                                         if(val) endtoolbox();
975                                         return;
976                         }
977                 }
978                 
979                 tbox_getmouse(mval);
980                 if(mval[0]<tbx1-10 || mval[0]>tbx2+10 || mval[1]<tby1-10 || mval[1]>tby2+10) break;
981                 
982                 tbox_getactive(&actx, &acty);
983                 
984                 /* muisafhandeling en redraw */
985                 if(xo!=actx || yo!=acty) {
986                         if(actx==0) {
987                                 if (acty==0) addmode=0;
988                                 
989                                 tbox_drawelem(0, tbmain, 0);
990                                 tbox_drawelem(0, acty, 1);
991                                 
992                                 tbmain= acty;
993                                 addmode= 0;
994                                 for(y=0; y<TBOXEL; y++) tbox_drawelem(1, y, 2);
995                         }
996                         else if(xo> -1) {
997                                 if(xo==0) tbox_drawelem(xo, yo, 1);
998                                 else tbox_drawelem(xo, yo, 2);
999                                 tbox_drawelem(actx, acty, 3);
1000                         }
1001                         
1002                         glFinish();             /* for geforce, to show it in the frontbuffer */
1003                         
1004                         xo= actx;
1005                         yo= acty;
1006                 }
1007         }
1008
1009         endtoolbox();
1010 }
1011
1012 /* ************************************  */
1013
1014 static int vconfirm(char *title, char *itemfmt, va_list ap)
1015 {
1016         char *s, buf[512];
1017
1018         s= buf;
1019         if (title) s+= sprintf(s, "%s%%t|", title);
1020         vsprintf(s, itemfmt, ap);
1021         
1022         return (pupmenu(buf)>=0);
1023 }
1024
1025 static int confirm(char *title, char *itemfmt, ...)
1026 {
1027         va_list ap;
1028         int ret;
1029         
1030         va_start(ap, itemfmt);
1031         ret= vconfirm(title, itemfmt, ap);
1032         va_end(ap);
1033         
1034         return ret;
1035 }
1036
1037 int okee(char *str, ...)
1038 {
1039         va_list ap;
1040         int ret;
1041         
1042         va_start(ap, str);
1043         ret= vconfirm("OK?", str, ap);
1044         va_end(ap);
1045         
1046         return ret;
1047 }
1048
1049 void notice(char *str, ...)
1050 {
1051         va_list ap;
1052         
1053         va_start(ap, str);
1054         vconfirm(NULL, str, ap);
1055         va_end(ap);
1056 }
1057
1058 void error(char *fmt, ...)
1059 {
1060         va_list ap;
1061         char nfmt[256];
1062
1063         sprintf(nfmt, "ERROR: %s", fmt);
1064
1065         va_start(ap, fmt);
1066         if (G.background || !G.curscreen) {
1067                 vprintf(nfmt, ap);
1068                 printf("\n");
1069         } else {
1070                 vconfirm(NULL, nfmt, ap);
1071         }
1072         va_end(ap);
1073 }
1074
1075 int saveover(char *file)
1076 {
1077         return (!BLI_exists(file) || confirm("SAVE OVER", file));
1078 }
1079
1080 /* ****************** EXTRAATJE **************** */
1081
1082 short button(short *var, short min, short max, char *str)
1083 {
1084         uiBlock *block;
1085         ListBase listb={0, 0};
1086         short x1,y1;
1087         short mval[2], ret=0;
1088
1089         if(min>max) min= max;
1090
1091         getmouseco_sc(mval);
1092         
1093         if(mval[0]<150) mval[0]=150;
1094         if(mval[1]<30) mval[1]=30;
1095         if(mval[0]>G.curscreen->sizex) mval[0]= G.curscreen->sizex-10;
1096         if(mval[1]>G.curscreen->sizey) mval[1]= G.curscreen->sizey-10;
1097
1098         block= uiNewBlock(&listb, "button", UI_EMBOSSX, UI_HELV, G.curscreen->mainwin);
1099         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|
1100                                    UI_BLOCK_ENTER_OK);
1101
1102         x1=mval[0]-150; 
1103         y1=mval[1]-20; 
1104         
1105         uiDefButS(block, NUM, 0, str,   (short)(x1+5),(short)(y1+10),125,20, var,(float)min,(float)max, 0, 0, "");
1106         uiDefBut(block, BUT, 1, "OK",   (short)(x1+136),(short)(y1+10),25,20, NULL, 0, 0, 0, 0, "");
1107
1108         uiBoundsBlock(block, 5);
1109
1110         ret= uiDoBlocks(&listb, 0);
1111
1112         if(ret==UI_RETURN_OK) return 1;
1113         return 0;
1114 }
1115
1116 short sbutton(char *var, float min, float max, char *str)
1117 {
1118         uiBlock *block;
1119         ListBase listb={0, 0};
1120         short x1,y1;
1121         short mval[2], ret=0;
1122
1123         if(min>max) min= max;
1124
1125         getmouseco_sc(mval);
1126         
1127         if(mval[0]<150) mval[0]=150;
1128         if(mval[1]<30) mval[1]=30;
1129         if(mval[0]>G.curscreen->sizex) mval[0]= G.curscreen->sizex-10;
1130         if(mval[1]>G.curscreen->sizey) mval[1]= G.curscreen->sizey-10;
1131
1132         block= uiNewBlock(&listb, "button", UI_EMBOSSX, UI_HELV, G.curscreen->mainwin);
1133         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
1134
1135         x1=mval[0]-150; 
1136         y1=mval[1]-20; 
1137         
1138         uiDefButC(block, TEX, 0, str,   x1+5,y1+10,125,20, var,(float)min,(float)max, 0, 0, "");
1139         uiDefBut(block, BUT, 1, "OK",   x1+136,y1+10,25,20, NULL, 0, 0, 0, 0, "");
1140
1141         uiBoundsBlock(block, 5);
1142
1143         ret= uiDoBlocks(&listb, 0);
1144
1145         if(ret==UI_RETURN_OK) return 1;
1146         return 0;
1147 }
1148
1149 short fbutton(float *var, float min, float max, char *str)
1150 {
1151         uiBlock *block;
1152         ListBase listb={0, 0};
1153         short x1,y1;
1154         short mval[2], ret=0;
1155
1156         if(min>max) min= max;
1157
1158         getmouseco_sc(mval);
1159         
1160         if(mval[0]<150) mval[0]=150;
1161         if(mval[1]<30) mval[1]=30;
1162         if(mval[0]>G.curscreen->sizex) mval[0]= G.curscreen->sizex-10;
1163         if(mval[1]>G.curscreen->sizey) mval[1]= G.curscreen->sizey-10;
1164
1165         block= uiNewBlock(&listb, "button", UI_EMBOSSX, UI_HELV, G.curscreen->mainwin);
1166         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
1167
1168         x1=mval[0]-150; 
1169         y1=mval[1]-20; 
1170         
1171         uiDefButF(block, NUM, 0, str,(short)(x1+5),(short)(y1+10),125,20, var, min, max, 0, 0, "");
1172         uiDefBut(block, BUT, 1, "OK",(short)(x1+136),(short)(y1+10), 35, 20, NULL, 0, 0, 0, 0, "");
1173
1174         uiBoundsBlock(block, 2);
1175
1176         ret= uiDoBlocks(&listb, 0);
1177
1178         if(ret==UI_RETURN_OK) return 1;
1179         return 0;
1180 }
1181
1182 int movetolayer_buts(unsigned int *lay)
1183 {
1184         uiBlock *block;
1185         ListBase listb={0, 0};
1186         int dx, dy, a, x1, y1, sizex=160, sizey=30;
1187         short pivot[2], mval[2], ret=0;
1188         
1189         if(G.vd->localview) {
1190                 error("Not in localview ");
1191                 return ret;
1192         }
1193
1194         getmouseco_sc(mval);
1195
1196         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
1197         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
1198         
1199         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
1200                 warp_pointer(pivot[0], pivot[1]);
1201
1202         mywinset(G.curscreen->mainwin);
1203         
1204         x1= pivot[0]-sizex+10; 
1205         y1= pivot[1]-sizey/2; 
1206
1207         block= uiNewBlock(&listb, "button", UI_EMBOSSX, UI_HELV, G.curscreen->mainwin);
1208         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
1209         
1210         dx= (sizex-5)/12;
1211         dy= sizey/2;
1212         
1213         for(a=0; a<10; a++) {
1214                 uiDefButI(block, TOGR|BIT|a, 0, "",(short)(x1+a*dx),(short)(y1+dy),(short)dx,(short)dy, lay, 0, 0, 0, 0, "");
1215                 if(a==4) x1+= 5;
1216         }
1217         x1-= 5;
1218
1219         for(a=0; a<10; a++) {
1220                 uiDefButI(block, TOGR|BIT|(a+10), 0, "",(short)(x1+a*dx),(short)y1,(short)dx,(short)dy, lay, 0, 0, 0, 0, "");
1221                 if(a==4) x1+= 5;
1222         }
1223         x1-= 5;
1224         
1225         uiDefBut(block, BUT, 1, "OK", (short)(x1+10*dx+10), (short)y1, (short)(3*dx), (short)(2*dy), NULL, 0, 0, 0, 0, "");
1226
1227         uiBoundsBlock(block, 2);
1228
1229         ret= uiDoBlocks(&listb, 0);
1230
1231         if(ret==UI_RETURN_OK) return 1;
1232         return 0;
1233 }
1234
1235 /* ********************** CLEVER_NUMBUTS ******************** */
1236
1237 #define MAXNUMBUTS      24
1238
1239 VarStruct numbuts[MAXNUMBUTS];
1240 void *numbpoin[MAXNUMBUTS];
1241 int numbdata[MAXNUMBUTS];
1242
1243 void draw_numbuts_tip(char *str, int x1, int y1, int x2, int y2)
1244 {
1245         static char *last=0;    /* avoid ugly updates! */
1246         int temp;
1247         
1248         if(str==last) return;
1249         last= str;
1250         if(str==0) return;
1251
1252         glColor3ub(160, 160, 160); /* MGREY */
1253         glRecti(x1+4,  y2-36,  x2-4,  y2-16);
1254
1255         cpack(0x0);
1256
1257         temp= 0;
1258         while( BMF_GetStringWidth(G.fonts, str+temp)>(x2 - x1-24)) temp++;
1259         glRasterPos2i(x1+16, y2-30);
1260         BMF_DrawString(G.fonts, str+temp);
1261 }
1262
1263 int do_clever_numbuts(char *name, int tot, int winevent)
1264 {
1265         ListBase listb= {NULL, NULL};
1266         uiBlock *block;
1267         VarStruct *varstr;
1268         int a, sizex, sizey, x1, y2;
1269         short mval[2], event;
1270         
1271         if(tot<=0 || tot>MAXNUMBUTS) return 0;
1272
1273         getmouseco_sc(mval);
1274
1275         /* size */
1276         sizex= 235;
1277         sizey= 30+20*(tot+1);
1278         
1279         /* midden */
1280         if(mval[0]<sizex/2) mval[0]=sizex/2;
1281         if(mval[1]<sizey/2) mval[1]=sizey/2;
1282         if(mval[0]>G.curscreen->sizex -sizex/2) mval[0]= G.curscreen->sizex -sizex/2;
1283         if(mval[1]>G.curscreen->sizey -sizey/2) mval[1]= G.curscreen->sizey -sizey/2;
1284
1285         mywinset(G.curscreen->mainwin);
1286         
1287         x1= mval[0]-sizex/2; 
1288         y2= mval[1]+sizey/2;
1289         
1290         block= uiNewBlock(&listb, "numbuts", UI_EMBOSSX, UI_HELV, G.curscreen->mainwin);
1291         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_ENTER_OK);
1292         
1293         /* LET OP: TEX BUTTON UITZONDERING */
1294         /* WAARSCHUWING: ALLEEN EEN ENKELE BITJES-BUTTON MOGELIJK: ER WORDT OP KOPIEDATA GEWERKT! */
1295
1296         uiDefBut(block, LABEL, 0, name, (short)(x1+15), (short)(y2-35), (short)(sizex-60), 19, 0, 1.0, 0.0, 0, 0, ""); 
1297
1298         if(name[0]=='A' && name[7]=='O') {
1299                 y2 -= 20;
1300                 uiDefBut(block, LABEL, 0, "Rotations in degrees!",      (short)(x1+15), (short)(y2-35), (short)(sizex-60), 19, 0, 0.0, 0.0, 0, 0, "");
1301         }
1302         
1303         varstr= &numbuts[0];
1304         for(a=0; a<tot; a++, varstr++) {
1305                 if(varstr->type==TEX) {
1306                         uiDefBut(block, TEX, 0, varstr->name,(short)(x1+15),(short)(y2-55-20*a),(short)(sizex-60), 19, numbpoin[a], varstr->min, varstr->max, 0, 0, varstr->tip);
1307                 }
1308                 else  {
1309                         uiDefBut(block, varstr->type, 0,        varstr->name,(short)(x1+15),(short)(y2-55-20*a), (short)(sizex-60), 19, &(numbdata[a]), varstr->min, varstr->max, 100, 0, varstr->tip);
1310                 }
1311         }
1312
1313         uiDefBut(block, BUT, 4000, "OK", (short)(x1+sizex-40),(short)(y2-35-20*a), 25, (short)(sizey-50), 0, 0, 0, 0, 0, "OK: Assign Values");
1314
1315         uiBoundsBlock(block, 5);
1316
1317         event= uiDoBlocks(&listb, 0);
1318
1319         areawinset(curarea->win);
1320         
1321         if(event & UI_RETURN_OK) {
1322                 
1323                 varstr= &numbuts[0];
1324                 for(a=0; a<tot; a++, varstr++) {
1325                         if(varstr->type==TEX);
1326                         else if ELEM( (varstr->type & BUTPOIN), FLO, INT ) memcpy(numbpoin[a], numbdata+a, 4);
1327                         else if((varstr->type & BUTPOIN)==SHO ) *((short *)(numbpoin[a]))= *( (short *)(numbdata+a));
1328                         
1329                         if( strncmp(varstr->name, "Rot", 3)==0 ) {
1330                                 float *fp;
1331                                 
1332                                 fp= numbpoin[a];
1333                                 fp[0]= M_PI*fp[0]/180.0;
1334                         }
1335                 }
1336                 
1337                 if(winevent) {
1338                         ScrArea *sa;
1339                 
1340                         sa= G.curscreen->areabase.first;
1341                         while(sa) {
1342                                 if(sa->spacetype==curarea->spacetype) addqueue(sa->win, winevent, 1);
1343                                 sa= sa->next;
1344                         }
1345                 }
1346                 
1347                 return 1;
1348         }
1349         return 0;
1350 }
1351
1352 void add_numbut(int nr, int type, char *str, float min, float max, void *poin, char *tip)
1353 {
1354         if(nr>=MAXNUMBUTS) return;
1355
1356         numbuts[nr].type= type;
1357         strcpy(numbuts[nr].name, str);
1358         numbuts[nr].min= min;
1359         numbuts[nr].max= max;
1360         if(tip) 
1361                 strcpy(numbuts[nr].tip, tip);
1362         else
1363                 strcpy(numbuts[nr].tip, "");
1364         
1365         
1366         /* LET OP: TEX BUTTON UITZONDERING */
1367         
1368         numbpoin[nr]= poin;
1369         
1370         if ELEM( (type & BUTPOIN), FLO, INT ) memcpy(numbdata+nr, poin, 4);
1371         if((type & BUTPOIN)==SHO ) *((short *)(numbdata+nr))= *( (short *)poin);
1372         
1373         if( strncmp(numbuts[nr].name, "Rot", 3)==0 ) {
1374                 float *fp;
1375                 
1376                 fp= (float *)(numbdata+nr);
1377                 fp[0]= 180.0*fp[0]/M_PI;
1378         }
1379
1380 }
1381
1382 void clever_numbuts(void)
1383 {
1384         Object *ob;
1385         float lim;
1386         char str[128];
1387         
1388         if(curarea->spacetype==SPACE_VIEW3D) {
1389                 lim= 1000.0*MAX2(1.0, G.vd->grid);
1390
1391                 if(G.obpose){
1392                         if (G.obpose->type == OB_ARMATURE) clever_numbuts_posearmature();
1393                 }
1394                 else if(G.obedit==0) {
1395                         ob= OBACT;
1396                         if(ob==0) return;
1397
1398                         add_numbut(0, NUM|FLO, "LocX:", -lim, lim, ob->loc, 0);
1399                         add_numbut(1, NUM|FLO, "LocY:", -lim, lim, ob->loc+1, 0);
1400                         add_numbut(2, NUM|FLO, "LocZ:", -lim, lim, ob->loc+2, 0);
1401                         
1402                         add_numbut(3, NUM|FLO, "RotX:", -10.0*lim, 10.0*lim, ob->rot, 0);
1403                         add_numbut(4, NUM|FLO, "RotY:", -10.0*lim, 10.0*lim, ob->rot+1, 0);
1404                         add_numbut(5, NUM|FLO, "RotZ:", -10.0*lim, 10.0*lim, ob->rot+2, 0);
1405                         
1406                         add_numbut(6, NUM|FLO, "SizeX:", -lim, lim, ob->size, 0);
1407                         add_numbut(7, NUM|FLO, "SizeY:", -lim, lim, ob->size+1, 0);
1408                         add_numbut(8, NUM|FLO, "SizeZ:", -lim, lim, ob->size+2, 0);
1409                         
1410                         sprintf(str, "Active Object: %s", ob->id.name+2);
1411                         do_clever_numbuts(str, 9, REDRAW);
1412
1413                 }
1414                 else if(G.obedit->type==OB_MESH) clever_numbuts_mesh();
1415                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) clever_numbuts_curve();
1416                 else if (G.obedit->type==OB_ARMATURE) clever_numbuts_armature();
1417         }
1418         else if(curarea->spacetype==SPACE_NLA){
1419                 clever_numbuts_nla();
1420         }
1421         else if(curarea->spacetype==SPACE_IPO) {
1422                 clever_numbuts_ipo();
1423         }
1424         else if(curarea->spacetype==SPACE_SEQ) {
1425                 clever_numbuts_seq();
1426         }
1427         else if(curarea->spacetype==SPACE_IMAGE) {
1428                 clever_numbuts_sima();
1429         }
1430         else if(curarea->spacetype==SPACE_IMASEL) {
1431                 clever_numbuts_imasel();
1432         }
1433         else if(curarea->spacetype==SPACE_BUTS){
1434                 clever_numbuts_buts();
1435         }
1436         else if(curarea->spacetype==SPACE_OOPS) {
1437                 clever_numbuts_oops();
1438         }
1439 //      else if(curarea->spacetype==SPACE_ACTION){
1440 //              stupid_damn_numbuts_action();
1441 //      }
1442         else if(curarea->spacetype==SPACE_FILE) {
1443                 clever_numbuts_filesel();
1444         }
1445 }
1446
1447
1448 void replace_names_but(void)
1449 {
1450         Image *ima= G.main->image.first;
1451         short len, tot=0;
1452         char old[64], new[64], temp[80];
1453         
1454         strcpy(old, "/");
1455         strcpy(new, "/");
1456         
1457         add_numbut(0, TEX, "Old:", 0, 63, old, 0);
1458         add_numbut(1, TEX, "New:", 0, 63, new, 0);
1459
1460         if (do_clever_numbuts("Replace image name", 2, REDRAW) ) {
1461                 
1462                 len= strlen(old);
1463                 
1464                 while(ima) {
1465                         
1466                         if(strncmp(old, ima->name, len)==0) {
1467                                 
1468                                 strcpy(temp, new);
1469                                 strcat(temp, ima->name+len);
1470                                 BLI_strncpy(ima->name, temp, sizeof(ima->name));
1471                                 
1472                                 if(ima->ibuf) IMB_freeImBuf(ima->ibuf);
1473                                 ima->ibuf= 0;
1474                                 ima->ok= 1;
1475                                 
1476                                 tot++;
1477                         }
1478                         
1479                         ima= ima->id.next;
1480                 }
1481
1482                 notice("Replaced %d names", tot);
1483         }
1484         
1485 }