Another huge commit!!!
[blender-staging.git] / source / blender / src / buttons.txt
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  * Everything for drawing buttons (and I do mean _everything_).
32  */
33
34
35 /* System includes ----------------------------------------------------- */
36
37 #include <time.h>
38 #include <math.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #ifdef _WIN32
47 #include "BLI_winstuff.h"
48 #else
49 #include <unistd.h>
50 #endif
51 #include "MEM_guardedalloc.h"
52
53 #include "BMF_Api.h"
54
55 #include "IMB_imbuf_types.h"
56 #include "IMB_imbuf.h"
57
58 #include "BLI_blenlib.h"
59 #include "BLI_arithb.h"
60 #include "BLI_editVert.h"
61
62 #include "DNA_action_types.h"
63 #include "DNA_armature_types.h"
64 #include "DNA_camera_types.h"
65 #include "DNA_constraint_types.h"
66 #include "DNA_curve_types.h"
67 #include "DNA_effect_types.h"
68 #include "DNA_group_types.h"
69 #include "DNA_ika_types.h"
70 #include "DNA_image_types.h"
71 #include "DNA_key_types.h"
72 #include "DNA_lamp_types.h"
73 #include "DNA_lattice_types.h"
74 #include "DNA_material_types.h"
75 #include "DNA_meta_types.h"
76 #include "DNA_mesh_types.h"
77 #include "DNA_object_types.h"
78 #include "DNA_packedFile_types.h"
79 #include "DNA_radio_types.h"
80 #include "DNA_scene_types.h"
81 #include "DNA_screen_types.h"
82 #include "DNA_sound_types.h"
83 #include "DNA_space_types.h"
84 #include "DNA_texture_types.h"
85 #include "DNA_userdef_types.h"
86 #include "DNA_vfont_types.h"
87 #include "DNA_view3d_types.h"
88 #include "DNA_world_types.h"
89
90 #include "BKE_anim.h"
91 #include "BKE_armature.h"
92 #include "BKE_constraint.h"
93 #include "BKE_curve.h"
94 #include "BKE_displist.h"
95 #include "BKE_effect.h"
96 #include "BKE_font.h"
97 #include "BKE_global.h"
98 #include "BKE_ika.h"
99 #include "BKE_image.h"
100 #include "BKE_ipo.h"
101 #include "BKE_lattice.h"
102 #include "BKE_library.h"
103 #include "BKE_main.h"
104 #include "BKE_material.h"
105 #include "BKE_mball.h"
106 #include "BKE_mesh.h"
107 #include "BKE_object.h"
108 #include "BKE_packedFile.h"
109 #include "BKE_plugin_types.h"
110 #include "BKE_sound.h"
111 #include "BKE_texture.h"
112 #include "BKE_utildefines.h"
113 #include "BKE_writeavi.h"
114
115 /* Everything from source (BIF, BDR, BSE) ------------------------------ */ 
116
117 #include "BDR_drawobject.h"
118 #include "BDR_editcurve.h"
119 #include "BDR_editface.h"
120 #include "BDR_editobject.h"
121 #include "BDR_vpaint.h"
122
123 #include "BSE_drawview.h"
124 #include "BSE_editipo.h"
125 #include "BSE_edit.h"
126 #include "BSE_filesel.h"
127 #include "BSE_headerbuttons.h"
128 #include "BSE_trans_types.h"
129 #include "BSE_view.h"
130 #include "BSE_buttons.h"
131 #include "BSE_seqaudio.h"
132
133 #include "BIF_gl.h"
134 #include "BIF_editarmature.h"   
135 #include "BIF_editconstraint.h" 
136 #include "BIF_editdeform.h"
137 #include "BIF_editfont.h"
138 #include "BIF_editmesh.h"
139 #include "BIF_editsca.h"
140 #include "BIF_editsound.h"
141 #include "BIF_interface.h"
142 #include "BIF_mywindow.h"
143 #include "BIF_renderwin.h"
144 #include "BIF_resources.h"
145 #include "BIF_screen.h"
146 #include "BIF_scrarea.h"
147 #include "BIF_space.h"
148 #include "BIF_toets.h"
149 #include "BIF_toolbox.h"
150 #include "BIF_previewrender.h"
151 #include "BIF_writeimage.h"
152 #include "BIF_writeavicodec.h"
153 #ifdef WITH_QUICKTIME
154 #include "quicktime_export.h"
155 #endif
156
157 /* 'old' stuff": defines and types ------------------------------------- */
158 #include "blendef.h"
159 #include "interface.h"
160
161 /* old style modules --------------------------------------------------- */
162
163 #include "mydevice.h"
164
165 #include "render.h"
166 #include "radio.h"
167 #include "nla.h"                        /* For __NLA: Do not remove! */
168
169 /* Decimation includes. See LOD_DependKludge.h for enabling Decimation   */
170 #include "LOD_DependKludge.h"
171 #ifdef NAN_DECIMATION
172   #include "LOD_decimation.h"
173 #endif
174
175 /* own include --------------------------------------------------------- */
176 #include "BSE_buttons.h"
177
178 /* some dirt ... let the linker deal with it :( ------------------------ */
179 extern ListBase editNurb;  /* from editcurve */
180 extern VPaint Gvp;         /* from vpaint */
181
182 /* Local vars ---------------------------------------------------------- */
183 short bgpicmode=0, near=1000, far=1000;
184 short degr= 90, step= 9, turn= 1, editbutflag= 1;
185 float hspeed=0.1f, prspeed=0.0f, prlen=0.0f, doublimit= 0.001f;
186 int decim_faces=0;
187
188 #ifdef __NLA
189 float editbutvweight=1;
190 #endif
191 float extr_offs= 1.0, editbutweight=1.0, editbutsize=0.1, cumapsize= 1.0;
192 MTex emptytex;
193 char texstr[15][8]= {"None"  , "Clouds" , "Wood",
194                                          "Marble", "Magic"  , "Blend",
195                                          "Stucci", "Noise"  , "Image",
196                                          "Plugin", "EnvMap" , "",
197                                          ""      , ""       , ""};
198
199
200 /* Local functions ----------------------------------------------------- */
201
202 /* event for buttons (ROW) to indicate the backbuffer isn't OK (ogl) */
203 #define B_DIFF                  1       
204
205 /* *********************** */
206 #define B_VIEWBUTS              1100
207
208 #define B_LOADBGPIC             1001
209 #define B_BLENDBGPIC    1002
210 #define B_BGPICBROWSE   1003
211 #define B_BGPICTEX              1004
212 #define B_BGPICCLEAR    1005
213 #define B_BGPICTEXCLEAR 1006
214
215 /* *********************** */
216 #define B_LAMPBUTS              1200
217
218 #define B_LAMPREDRAW    1101
219 #define B_COLLAMP               1102
220 #define B_TEXCLEARLAMP  1103
221 #define B_SBUFF                 1104
222
223 /* *********************** */
224 #define B_MATBUTS               1300
225
226 #define B_MATCOL                1201
227 #define B_SPECCOL               1202
228 #define B_MIRCOL                1203
229 #define B_ACTCOL                1204
230 #define B_MATFROM               1205
231 #define B_MATPRV                1206
232 #define B_MTEXCOL               1207
233 #define B_TEXCLEAR              1208
234 #define B_MATPRV_DRAW   1209
235 #define B_MTEXPASTE             1210
236 #define B_MTEXCOPY              1211
237 #define B_MATLAY                1212
238
239 /* *********************** */
240 #define B_TEXBUTS               1400
241
242 #define B_TEXTYPE               1301
243 #define B_DEFTEXVAR             1302
244 #define B_LOADTEXIMA    1303
245 #define B_NAMEIMA               1304
246 #define B_TEXCHANNEL    1305
247 #define B_TEXREDR_PRV   1306
248 #define B_TEXIMABROWSE  1307
249 #define B_IMAPTEST              1308
250 #define B_RELOADIMA             1309
251 #define B_LOADPLUGIN    1310
252 #define B_NAMEPLUGIN    1311
253 #define B_COLORBAND             1312
254 #define B_ADDCOLORBAND  1313
255 #define B_DELCOLORBAND  1314
256 #define B_CALCCBAND             1315
257 #define B_CALCCBAND2    1316
258 #define B_DOCOLORBAND   1317
259 #define B_REDRAWCBAND   1318
260 #define B_BANDCOL               1319
261 #define B_LOADTEXIMA1   1320
262 #define B_PLUGBUT               1321
263
264 /* plugbut reserves 24 buttons at least! */
265
266 #define B_ENV_MAKE              1350
267 #define B_ENV_FREE              1351
268 #define B_ENV_DELETE    1352
269 #define B_ENV_SAVE              1353
270 #define B_ENV_OB                1354
271
272 #define B_PACKIMA               1355
273 #define B_TEXSETFRAMES  1356
274
275 #define B_ENV_FREE_ALL  1357
276
277 /* *********************** */
278 #define B_ANIMBUTS              1500
279
280 #define B_RECALCPATH    1401
281 #define B_MUL_IPO               1402
282 #define B_AUTOTIMEOFS   1403
283 #define B_FRAMEMAP              1404
284 #define B_NEWEFFECT             1405
285 #define B_PREVEFFECT    1406
286 #define B_NEXTEFFECT    1407
287 #define B_CHANGEEFFECT  1408
288 #define B_CALCEFFECT    1409
289 #define B_DELEFFECT             1410
290 #define B_RECALCAL              1411
291 #define B_SETSPEED              1412
292 #define B_PRINTSPEED    1413
293 #define B_PRINTLEN              1414
294 #define B_RELKEY                1415
295
296         /* this has MAX_EFFECT settings! Next free define is 1450... */
297 #define B_SELEFFECT     1430    
298
299
300 /* *********************** */
301 #define B_WORLDBUTS             1600
302
303 #define B_TEXCLEARWORLD 1501
304
305 /* *********************** */
306 #define B_RENDERBUTS    1700
307
308 #define B_FS_PIC                1601
309 #define B_FS_BACKBUF    1602
310
311 #define B_FS_FTYPE              1604
312 #define B_DORENDER              1605
313 #define B_DOANIM                1606
314 #define B_PLAYANIM              1607
315 #define B_PR_PAL                1608
316 #define B_PR_FULL               1609
317 #define B_PR_PRV                1610
318 #define B_PR_CDI                1611
319 #define B_PR_PAL169             1612
320 #define B_PR_D2MAC              1613
321 #define B_PR_MPEG               1614
322 #define B_REDRAWDISP    1615
323 #define B_SETBROWSE             1616
324 #define B_CLEARSET              1617
325 #define B_PR_PRESET             1618
326 #define B_PR_PANO               1619
327 #define B_PR_NTSC               1620
328
329 #define B_IS_FTYPE              1622
330 #define B_IS_BACKBUF    1623
331 #define B_PR_PC                 1624
332
333 #define B_PR_PANO360    1627
334 #define B_PR_HALFFIELDS 1628
335 #define B_NEWRENDERPIPE 1629
336 #define B_R_SCALE       1630
337 #define B_G_SCALE       1631
338 #define B_B_SCALE       1632
339 #define B_USE_R_SCALE   1633
340 #define B_USE_G_SCALE   1634
341 #define B_USE_B_SCALE   1635
342 #define B_EDGECOLSLI    1636
343 #define B_GAMMASLI      1637
344
345 #define B_FILETYPEMENU  1638
346 #define B_SELECTCODEC   1639
347 #define B_RTCHANGED             1640
348
349 #ifdef __NLA
350 /* *********************** */
351 enum {
352         B_ARMATUREBUTS  =       1800,
353         B_POSE                  =       1701
354 };
355 #endif
356
357 /* *********************** */
358 #define B_COMMONEDITBUTS        2049
359
360 #define B_MATWICH               2003
361 #define B_MATNEW                2004
362 #define B_MATDEL                2005
363 #define B_MATASS                2006
364 #define B_MATSEL                2007
365 #define B_MATDESEL              2008
366 #define B_HIDE                  2009
367 #define B_REVEAL                2010
368 #define B_SELSWAP               2011
369 #define B_SETSMOOTH             2012
370 #define B_SETSOLID              2013
371 #define B_AUTOTEX               2014
372 #define B_DOCENTRE              2015
373 #define B_DOCENTRENEW   2016
374 #define B_DOCENTRECURSOR        2017
375
376         /* 32 values! */
377 #define B_OBLAY                 2018
378
379 #define B_MESHBUTS              2100
380
381 #define B_FLIPNORM              2050
382 #define B_SPIN                  2051
383 #define B_SPINDUP               2052
384 #define B_EXTR                  2053
385 #define B_SCREW                 2054
386 #define B_EXTREP                2055
387 #define B_SPLIT                 2056
388 #define B_REMDOUB               2057
389 #define B_SUBDIV                2058
390 #define B_FRACSUBDIV    2059
391 #define B_XSORT                 2060
392 #define B_HASH                  2061
393 #define B_DELSTICKY             2062
394 #define B_DELVERTCOL    2063
395 #define B_MAKE_TFACES   2064
396 #define B_TOSPHERE              2065
397 #define B_DEL_TFACES    2066
398 #define B_NEWVGROUP             2067
399 #define B_DELVGROUP             2068
400 #define B_ASSIGNVGROUP  2069
401 #define B_REMOVEVGROUP  2070
402 #define B_SELVGROUP             2071    
403 #define B_DESELVGROUP   2072
404 #define B_DECIM_FACES   2073
405 #define B_DECIM_CANCEL  2074
406 #define B_DECIM_APPLY   2075
407 #define B_AUTOVGROUP    2076
408 #define B_SLOWERDRAW    2077
409 #define B_FASTERDRAW    2078
410 #define B_VERTEXNOISE   2079
411 #define B_VERTEXSMOOTH  2080
412 #define B_MAKESTICKY    2082
413 #define B_MAKEVERTCOL   2083
414
415 /* *********************** */
416 #define B_CURVEBUTS             2200
417
418 #define B_CONVERTPOLY   2101
419 #define B_CONVERTBEZ    2102
420 #define B_CONVERTBSPL   2103
421 #define B_CONVERTCARD   2104
422 #define B_CONVERTNURB   2105
423 #define B_UNIFU                 2106
424 #define B_ENDPU                 2107
425 #define B_BEZU                  2108
426 #define B_UNIFV                 2109
427 #define B_ENDPV                 2110
428 #define B_BEZV                  2111
429 #define B_SETWEIGHT             2112
430 #define B_SETW1                 2113
431 #define B_SETW2                 2114
432 #define B_SETW3                 2115
433 #define B_SETORDER              2116
434 #define B_MAKEDISP              2117
435 #define B_SUBDIVCURVE   2118
436 #define B_SPINNURB              2119
437 #define B_CU3D                  2120
438 #define B_SETRESOLU             2121
439 #define B_SETW4                 2122
440
441
442 /* *********************** */
443 #define B_FONTBUTS              2300
444
445 #define B_MAKEFONT              2201
446 #define B_TOUPPER               2202
447 #define B_SETFONT               2203
448 #define B_LOADFONT              2204
449 #define B_TEXTONCURVE   2205
450 #define B_PACKFONT              2206
451
452 /* *********************** */
453 #define B_IKABUTS               2400
454
455 #define B_IKASETREF             2301
456 #define B_IKARECALC             2302
457
458 /* *********************** */
459 #define B_CAMBUTS               2500
460
461 /* *********************** */
462 #define B_MBALLBUTS             2600
463
464 #define B_RECALCMBALL   2501
465
466 /* *********************** */
467 #define B_LATTBUTS              2700
468
469 #define B_RESIZELAT             2601
470 #define B_DRAWLAT               2602
471 #define B_LATTCHANGED   2603
472
473 /* *********************** */
474 #define B_GAMEBUTS              2800
475
476 /* in editsca.c */
477
478 /* *********************** */
479 #define B_FPAINTBUTS    2900
480
481 #define B_VPCOLSLI              2801
482 #define B_VPGAMMA               2802
483
484 #define B_COPY_TF_MODE  2804
485 #define B_COPY_TF_UV    2805
486 #define B_COPY_TF_COL   2806
487 #define B_REDR_3D_IMA   2807
488 #define B_SET_VCOL              2808
489
490 #define B_COPY_TF_TEX   2814
491 #define B_TFACE_HALO    2815
492 #define B_TFACE_BILLB   2816
493
494 #define B_SHOWTEX               2832
495 #define B_ASSIGNMESH    2833
496
497
498 /* *********************** */
499 #define B_RADIOBUTS             3000
500
501 #define B_RAD_GO                2901
502 #define B_RAD_INIT              2902
503 #define B_RAD_LIMITS    2903
504 #define B_RAD_FAC               2904
505 #define B_RAD_NODELIM   2905
506 #define B_RAD_NODEFILT  2906
507 #define B_RAD_FACEFILT  2907
508 #define B_RAD_ADD               2908
509 #define B_RAD_DELETE    2909
510 #define B_RAD_COLLECT   2910
511 #define B_RAD_SHOOTP    2911
512 #define B_RAD_SHOOTE    2912
513 #define B_RAD_REPLACE   2913
514 #define B_RAD_DRAW              2914
515 #define B_RAD_FREE              2915
516 #define B_RAD_ADDMESH   2916
517
518 /* *********************** */
519 #define B_SCRIPTBUTS    3100
520
521 #define B_SCRIPT_ADD    3001
522 #define B_SCRIPT_DEL    3002
523 #define B_SCRIPT_TYPE   3003
524
525 /* Scene script buttons */
526 #define B_SSCRIPT_ADD   3004
527 #define B_SSCRIPT_DEL   3005
528 #define B_SSCRIPT_TYPE  3006
529
530 /* *********************** */
531 #define B_SOUNDBUTS             3200
532 enum B_SOUND_BUTTONS {
533         B_SOUND_CHANGED = 3101,
534                 B_SOUND_REDRAW,
535                 B_SOUND_VOLUME,
536                 B_SOUND_PANNING,
537                 B_SOUND_PITCH,
538                 B_SOUND_LOAD_SAMPLE,
539                 B_SOUND_MENU_SAMPLE,
540                 B_SOUND_NAME_SAMPLE,
541                 B_SOUND_UNLINK_SAMPLE,
542                 B_SOUND_RELOAD_SAMPLE,
543                 B_SOUND_UNPACK_SAMPLE,
544                 B_SOUND_PLAY_SAMPLE,
545                 B_SOUND_COPY_SOUND,
546                 B_SOUND_LOOPSTART,
547                 B_SOUND_LOOPEND,
548                 B_SOUND_BIDIRECTIONAL,
549                 B_SOUND_RECALC,
550                 B_SOUND_RATECHANGED,
551                 B_SOUND_MIXDOWN
552 };
553
554 /* *********************** */
555 #define B_CONSTRAINTBUTS        3300
556 enum {
557         B_CONSTRAINT_REDRAW = 3201,
558         B_CONSTRAINT_ADD,
559         B_CONSTRAINT_DEL,
560         B_CONSTRAINT_TEST,
561         B_CONSTRAINT_CHANGETYPE,
562         B_CONSTRAINT_CHANGENAME,
563         B_CONSTRAINT_CHANGETARGET
564 };
565
566 /* *********************** */
567 /*  BUTTON BUT: > 4000     */
568 /*  BUTTON 4001-4032: layers */
569
570
571 static char *physics_pup(void)
572 {
573   /* the number needs to match defines in KX_PhysicsBlenderSceneConverter.cpp */
574   return "Physics %t|None %x1|Sumo %x2|"
575          "ODE %x3 |Dynamo %x4|";
576 }
577
578
579 static void draw_buttons_edge(int win, float x1)
580 {
581         float asp, winmat[4][4];
582         int w,h;
583
584         bwin_getsinglematrix(win, winmat);
585         bwin_getsize(win, &w, &h);
586         asp= (float)(2.0/(w*winmat[0][0]));
587
588         glColor3ub(0,0,0);
589         fdrawline(x1, -1000, x1, 2000);
590         glColor3ub(255,255,255);
591         fdrawline(x1+asp, -1000, x1+asp, 2000);
592 }
593
594 static int packdummy = 0;
595
596
597 // Bish made it to here. (reviewing tooltips)
598
599
600 /* *************************** RADIO ******************************** */
601
602 void do_radiobuts(short event)
603 {
604         Radio *rad;
605         int phase;
606         
607         phase= rad_phase();
608         rad= G.scene->radio;
609         
610         switch(event) {
611         case B_RAD_ADD:
612                 add_radio();
613                 allqueue(REDRAWBUTSRADIO, 0);
614                 allqueue(REDRAWVIEW3D, 0);
615                 break;
616         case B_RAD_DELETE:
617                 delete_radio();
618                 allqueue(REDRAWBUTSRADIO, 0);
619                 allqueue(REDRAWVIEW3D, 0);
620                 break;
621         case B_RAD_FREE:
622                 freeAllRad();
623                 allqueue(REDRAWBUTSRADIO, 0);
624                 allqueue(REDRAWVIEW3D, 0);
625                 break;
626         case B_RAD_COLLECT:
627                 rad_collect_meshes();
628                 allqueue(REDRAWBUTSRADIO, 0);
629                 allqueue(REDRAWVIEW3D, 0);
630                 break;
631         case B_RAD_INIT:
632                 if(phase==RAD_PHASE_PATCHES) {
633                         rad_limit_subdivide();
634                         allqueue(REDRAWBUTSRADIO, 0);
635                         allqueue(REDRAWVIEW3D, 0);
636                 }
637                 break;
638         case B_RAD_SHOOTP:
639                 if(phase==RAD_PHASE_PATCHES) {
640                         waitcursor(1);
641                         rad_subdivshootpatch();
642                         allqueue(REDRAWBUTSRADIO, 0);
643                         allqueue(REDRAWVIEW3D, 0);
644                         waitcursor(0);
645                 }
646                 break;
647         case B_RAD_SHOOTE:
648                 if(phase==RAD_PHASE_PATCHES) {
649                         waitcursor(1);
650                         rad_subdivshootelem();
651                         allqueue(REDRAWBUTSRADIO, 0);
652                         allqueue(REDRAWVIEW3D, 0);
653                         waitcursor(0);
654                 }
655                 break;
656         case B_RAD_GO:
657                 if(phase==RAD_PHASE_PATCHES) {
658                         waitcursor(1);
659                         rad_go();
660                         waitcursor(0);
661                         allqueue(REDRAWBUTSRADIO, 0);
662                         allqueue(REDRAWVIEW3D, 0);
663                 }
664                 break;
665         case B_RAD_LIMITS:
666                 rad_setlimits();
667                 allqueue(REDRAWVIEW3D, 0);
668                 allqueue(REDRAWBUTSRADIO, 0);
669                 break;
670         case B_RAD_FAC:
671                 set_radglobal();
672                 if(phase & RAD_PHASE_FACES) make_face_tab();
673                 else make_node_display();
674                 allqueue(REDRAWVIEW3D, 0);
675                 break;
676         case B_RAD_NODELIM:
677                 if(phase & RAD_PHASE_FACES) {
678                         set_radglobal();
679                         removeEqualNodes(rad->nodelim);
680                         make_face_tab();
681                         allqueue(REDRAWVIEW3D, 0);
682                         allqueue(REDRAWBUTSRADIO, 0);
683                 }
684                 break;
685         case B_RAD_NODEFILT:
686                 if(phase & RAD_PHASE_FACES) {
687                         set_radglobal();
688                         filterNodes();
689                         make_face_tab();
690                         allqueue(REDRAWVIEW3D, 0);
691                 }
692                 break;
693         case B_RAD_FACEFILT:
694                 if(phase & RAD_PHASE_FACES) {
695                         filterFaces();
696                         allqueue(REDRAWVIEW3D, 0);
697                 }
698                 break;
699         case B_RAD_DRAW:
700                 set_radglobal();
701                 allqueue(REDRAWVIEW3D, 0);
702                 break;
703         case B_RAD_ADDMESH:
704                 if(phase & RAD_PHASE_FACES) rad_addmesh();
705                 allqueue(REDRAWVIEW3D, 0);
706                 break;
707         case B_RAD_REPLACE:
708                 if(phase & RAD_PHASE_FACES) rad_replacemesh();
709                 allqueue(REDRAWVIEW3D, 0);
710                 break;
711         }
712
713 }
714
715
716 void radiobuts(void)
717 {
718         Radio *rad;
719         uiBlock *block;
720         int flag;
721         char str[128];
722
723         rad= G.scene->radio;
724         if(rad==0) {
725                 add_radio();
726                 rad= G.scene->radio;
727         }
728         
729         sprintf(str, "buttonswin %d", curarea->win);
730         block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
731
732         flag= rad_phase();
733
734         if(flag & RAD_PHASE_PATCHES) {
735                 uiBlockSetCol(block, BUTSALMON);
736                 uiDefBut(block,  BUT, B_RAD_INIT, "Limit Subdivide",    10, 70, 190, 40, NULL, 0, 0, 0, 0, "Subdivide patches");
737         }
738         if(flag & RAD_PHASE_PATCHES) uiBlockSetCol(block, BUTPURPLE);
739         else uiBlockSetCol(block, BUTSALMON);
740         uiDefBut(block,  BUT, B_RAD_COLLECT, "Collect Meshes",  10, 30, 190, 40, NULL, 0, 0, 0, 0, "Convert selected and visible meshes to patches");
741         uiDrawBlock(block);
742
743         if(flag==0) {
744         
745                 sprintf(str, "buttonswin1 %d", curarea->win);
746                 block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
747                 uiAutoBlock(block, 210, 30, 230, 150, UI_BLOCK_ROWS);
748                 uiBlockSetCol(block, BUTGREY);
749                 uiDefButS(block,  NUM, B_RAD_LIMITS, "Hemires:",        0, 0, 10, 10, &rad->hemires, 100.0, 1000.0, 100, 0, "Set the size of a hemicube");
750                 uiDefButS(block,  NUM, 0, "Max Iterations:",            1, 0, 10, 10, &rad->maxiter, 0.0, 10000.0, 0, 0, "Maximum number of radiosity rounds");
751                 uiDefButF(block,  NUM, B_RAD_FAC, "Mult:",                      2, 0, 50, 10, &rad->radfac, 0.001, 250.0, 100, 0, "Mulitply the energy values");
752                 uiDefButF(block,  NUM, B_RAD_FAC, "Gamma:",                     2, 0, 50, 10, &rad->gamma, 0.2, 10.0, 10, 0, "Change the contrast of the energy values");
753                 uiDefButF(block,  NUM, 0, "Convergence:",                       3, 0, 10, 10, &rad->convergence, 0.0, 1.0, 10, 0, "Set the lower threshold of unshot energy");
754                 uiDrawBlock(block);
755         }
756         else {
757         
758         
759                 sprintf(str, "buttonswin1 %d", curarea->win);
760                 block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
761                 uiAutoBlock(block, 210, 30, 230, 150, UI_BLOCK_ROWS);
762                 
763                 uiBlockSetCol(block, BUTGREEN);
764                 uiDefButS(block,  ROW, B_RAD_DRAW, "Wire",                      0, 0, 10, 10, &rad->drawtype, 0.0, 0.0, 0, 0, "Enable wireframe drawmode");
765                 uiDefButS(block,  ROW, B_RAD_DRAW, "Solid",                     0, 0, 10, 10, &rad->drawtype, 0.0, 1.0, 0, 0, "Enable solid drawmode");
766                 uiDefButS(block,  ROW, B_RAD_DRAW, "Gour",                      0, 0, 10, 10, &rad->drawtype, 0.0, 2.0, 0, 0, "Enable Gourad drawmode");
767                 uiBlockSetCol(block, BUTGREY);
768                 uiDefButS(block,  TOG|BIT|0, B_RAD_DRAW, "ShowLim",  1, 0, 10, 10, &rad->flag, 0, 0, 0, 0, "Visualize patch and element limits");
769                 uiDefButS(block,  TOG|BIT|1, B_RAD_DRAW, "Z",           1, 0, 3, 10, &rad->flag, 0, 0, 0, 0, "Draw limits different");
770                 uiBlockSetCol(block, BUTGREY);
771                 uiDefButS(block,  NUM, B_RAD_LIMITS, "ElMax:",          2, 0, 10, 10, &rad->elma, 1.0, 500.0, 0, 0, "Set maximum size of an element");
772                 uiDefButS(block,  NUM, B_RAD_LIMITS, "ElMin:",          2, 0, 10, 10, &rad->elmi, 1.0, 100.0, 0, 0, "Set minimum size of an element");
773                 uiDefButS(block,  NUM, B_RAD_LIMITS, "PaMax:",          3, 0, 10, 10, &rad->pama, 10.0, 1000.0, 0, 0, "Set maximum size of a patch");
774                 uiDefButS(block,  NUM, B_RAD_LIMITS, "PaMin:",          3, 0, 10, 10, &rad->pami, 10.0, 1000.0, 0, 0, "Set minimum size of a patch");
775                 uiDrawBlock(block);
776                 
777                 sprintf(str, "buttonswin2 %d", curarea->win);
778                 block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
779                 uiAutoBlock(block, 450, 30, 180, 150, UI_BLOCK_ROWS);
780                 
781                 if(flag == RAD_PHASE_PATCHES) uiBlockSetCol(block, BUTSALMON);
782                 else uiBlockSetCol(block, BUTGREY);
783                 uiDefBut(block,  BUT, B_RAD_SHOOTE, "Subdiv Shoot Element", 0, 0, 12, 10, NULL, 0, 0, 0, 0, "");
784                 uiDefBut(block,  BUT, B_RAD_SHOOTP, "Subdiv Shoot Patch",       1, 0, 12, 10, NULL, 0, 0, 0, 0, "Detect high energy changes");
785                 uiBlockSetCol(block, BUTGREY);
786                 uiDefButS(block,  NUM, 0, "Max Subdiv Shoot:",                  2, 0, 10, 10, &rad->maxsublamp, 1.0, 250.0, 0, 0, "Set the maximum number of shoot patches that are evaluated");
787                 uiDefButI(block,  NUM, 0, "MaxEl:",                                             3, 0, 10, 10, &rad->maxnode, 1.0, 250000.0, 0, 0, "Set the maximum allowed number of elements");
788                 uiDefButS(block,  NUM, B_RAD_LIMITS, "Hemires:",                4, 0, 10, 10, &rad->hemires, 100.0, 1000.0, 100, 0, "Set the size of a hemicube");
789                 uiDrawBlock(block);
790                 
791                 sprintf(str, "buttonswin3 %d", curarea->win);
792                 block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
793                 uiAutoBlock(block, 640, 30, 200, 150, UI_BLOCK_ROWS);
794                 
795                 uiBlockSetCol(block, BUTGREY);
796                 uiDefButS(block,  NUM, 0, "Max Iterations:",    0, 0, 10, 10, &rad->maxiter, 0.0, 10000.0, 0, 0, "Maximum number of radiosity rounds");
797                 uiDefButF(block,  NUM, 0, "Convergence:",               1, 0, 10, 10, &rad->convergence, 0.0, 1.0, 10, 0, "Set the lower threshold of unshot energy");
798                 uiDefButS(block,  NUM, 0, "SubSh P:",                   2, 0, 10, 10, &rad->subshootp, 0.0, 10.0, 0, 0, "Set the number of times the environment is tested to detect pathes");
799                 uiDefButS(block,  NUM, 0, "SubSh E:",                   2, 0, 10, 10, &rad->subshoote, 0.0, 10.0, 0, 0, "Set the number of times the environment is tested to detect elements");
800                 if(flag == RAD_PHASE_PATCHES) uiBlockSetCol(block, BUTSALMON);
801                 uiDefBut(block,  BUT, B_RAD_GO, "GO",                           3, 0, 10, 15, NULL, 0, 0, 0, 0, "Start the radiosity simulation");
802                 uiDrawBlock(block);
803                 
804                 sprintf(str, "buttonswin4 %d", curarea->win);
805                 block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
806                 uiAutoBlock(block, 850, 30, 200, 150, UI_BLOCK_ROWS);
807         
808                 uiBlockSetCol(block, BUTGREY);
809                 uiDefButF(block,  NUM, B_RAD_FAC, "Mult:",                      0, 0, 50, 17, &rad->radfac, 0.001, 250.0, 100, 0, "Mulitply the energy values");
810                 uiDefButF(block,  NUM, B_RAD_FAC, "Gamma:",                     0, 0, 50, 17, &rad->gamma, 0.2, 10.0, 10, 0, "Change the contrast of the energy values");
811                 if(flag & RAD_PHASE_FACES) uiBlockSetCol(block, BUTSALMON);
812                 else uiBlockSetCol(block, BUTGREY);
813                 uiDefBut(block,  BUT, B_RAD_FACEFILT, "FaceFilter",             1, 0, 10, 10, NULL, 0, 0, 0, 0, "Force an extra smoothing");
814                 if(flag & RAD_PHASE_FACES) uiBlockSetCol(block, BUTSALMON);
815                 else uiBlockSetCol(block, BUTGREY);
816                 uiDefBut(block,  BUT, B_RAD_NODELIM, "RemoveDoubles",   2, 0, 30, 10, NULL, 0.0, 50.0, 0, 0, "Join elements which differ less than 'Lim'");
817                 uiBlockSetCol(block, BUTGREY);
818                 uiDefButS(block,  NUM, 0, "Lim:",                                       2, 0, 10, 10, &rad->nodelim, 0.0, 50.0, 0, 0, "Set the range for removing doubles");
819                 if(flag & RAD_PHASE_FACES) uiBlockSetCol(block, BUTSALMON);
820                 else uiBlockSetCol(block, BUTGREY);
821                 uiDefBut(block,  BUT, B_RAD_NODEFILT, "Element Filter", 3, 0, 10, 10, NULL, 0, 0, 0, 0, "Filter elements to remove aliasing artefacts");
822                 uiDrawBlock(block);
823         
824                 sprintf(str, "buttonswin5 %d", curarea->win);
825                 block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
826                 uiAutoBlock(block, 1060, 30, 190, 150, UI_BLOCK_ROWS);
827         
828                 if(flag & RAD_PHASE_PATCHES) uiBlockSetCol(block, BUTSALMON);
829                 else uiBlockSetCol(block, BUTGREY);
830                 uiDefBut(block,  BUT, B_RAD_FREE, "Free Radio Data",    0, 0, 10, 10, NULL, 0, 0, 0, 0, "Release all memory used by Radiosity");        
831                 if(flag & RAD_PHASE_FACES) uiBlockSetCol(block, BUTSALMON);
832                 else uiBlockSetCol(block, BUTGREY);
833                 uiDefBut(block,  BUT, B_RAD_REPLACE, "Replace Meshes",  1, 0, 10, 10, NULL, 0, 0, 0, 0, "Convert meshes to Mesh objects with vertex colours, changing input-meshes");
834                 uiDefBut(block,  BUT, B_RAD_ADDMESH, "Add new Meshes",  2, 0, 10, 10, NULL, 0, 0, 0, 0, "Convert meshes to Mesh objects with vertex colours, unchanging input-meshes");
835                 uiDrawBlock(block);
836                 
837                 rad_status_str(str);
838                 cpack(0);
839                 glRasterPos2i(210, 189);
840                 BMF_DrawString(uiBlockGetCurFont(block), str);
841         }
842 }
843
844
845
846 /* *************************** SCRIPT ******************************** */
847
848 static void extend_scriptlink(ScriptLink *slink)
849 {
850         void *stmp, *ftmp;
851
852         if (!slink) return;
853                 
854         stmp= slink->scripts;           
855         slink->scripts= MEM_mallocN(sizeof(ID*)*(slink->totscript+1), "scriptlistL");
856         
857         ftmp= slink->flag;              
858         slink->flag= MEM_mallocN(sizeof(short*)*(slink->totscript+1), "scriptlistF");
859         
860         if (slink->totscript) {
861                 memcpy(slink->scripts, stmp, sizeof(ID*)*(slink->totscript));
862                 MEM_freeN(stmp);
863
864                 memcpy(slink->flag, ftmp, sizeof(short)*(slink->totscript));
865                 MEM_freeN(ftmp);
866         }
867
868         slink->scripts[slink->totscript]= NULL;
869         slink->flag[slink->totscript]= SCRIPT_FRAMECHANGED;
870
871         slink->totscript++;
872                                 
873         if(slink->actscript<1) slink->actscript=1;
874 }
875
876 static void delete_scriptlink(ScriptLink *slink)
877 {
878         int i;
879         
880         if (!slink) return;
881         
882         if (slink->totscript>0) {
883                 for (i=slink->actscript-1; i<slink->totscript-1; i++) {
884                         slink->flag[i]= slink->flag[i+1];
885                         slink->scripts[i]= slink->scripts[i+1];
886                 }
887                 
888                 slink->totscript--;
889         }
890                 
891         CLAMP(slink->actscript, 1, slink->totscript);
892                 
893         if (slink->totscript==0) {
894                 if (slink->scripts) MEM_freeN(slink->scripts);
895                 if (slink->flag) MEM_freeN(slink->flag);
896
897                 slink->scripts= NULL;
898                 slink->flag= NULL;
899                 slink->totscript= slink->actscript= 0;                  
900         }
901 }
902
903 void do_scriptbuts(short event)
904 {
905         Object *ob=NULL;
906         ScriptLink *script=NULL;
907         Material *ma;
908         
909         switch (event) {
910         case B_SSCRIPT_ADD:
911                 extend_scriptlink(&G.scene->scriptlink);
912                 break;
913         case B_SSCRIPT_DEL:
914                 delete_scriptlink(&G.scene->scriptlink);
915                 break;
916                 
917         case B_SCRIPT_ADD:
918         case B_SCRIPT_DEL:
919                 ob= OBACT;
920
921                 if (ob && G.buts->scriptblock==ID_OB) {
922                                 script= &ob->scriptlink;
923
924                 } else if (ob && G.buts->scriptblock==ID_MA) {
925                         ma= give_current_material(ob, ob->actcol);
926                         if (ma) script= &ma->scriptlink;
927
928                 } else if (ob && G.buts->scriptblock==ID_CA) {
929                         if (ob->type==OB_CAMERA)
930                                 script= &((Camera *)ob->data)->scriptlink;
931
932                 } else if (ob && G.buts->scriptblock==ID_LA) {
933                         if (ob->type==OB_LAMP)
934                                 script= &((Lamp *)ob->data)->scriptlink;
935
936                 } else if (G.buts->scriptblock==ID_WO) {
937                         if (G.scene->world) 
938                                 script= &(G.scene->world->scriptlink);
939                 }
940                 
941                 if (event==B_SCRIPT_ADD) extend_scriptlink(script);
942                 else delete_scriptlink(script);
943                 
944                 break;
945         default:
946                 break;
947         }
948
949         allqueue(REDRAWBUTSSCRIPT, 0);
950 }
951
952 void draw_scriptlink(uiBlock *block, ScriptLink *script, int sx, int sy, int scene) 
953 {
954         char str[256];
955
956         uiBlockSetCol(block, BUTGREY);
957
958         if (script->totscript) {
959                 strcpy(str, "FrameChanged%x 1|");
960                 strcat(str, "Redraw%x 4|");
961                 if (scene) {
962                         strcat(str, "OnLoad%x 2");
963                 }
964
965                 uiDefButS(block, MENU, 1, str, (short)sx, (short)sy, 148, 19, &script->flag[script->actscript-1], 0, 0, 0, 0, "Script links for the Frame changed event");
966
967                 uiDefIDPoinBut(block, test_scriptpoin_but, 1, "", (short)(sx+150),(short)sy, 98, 19, &script->scripts[script->actscript-1], "Name of Script to link");
968         }
969
970         sprintf(str,"%d Scr:", script->totscript);
971         uiDefButS(block, NUM, REDRAWBUTSSCRIPT, str, (short)(sx+250), (short)sy,98,19, &script->actscript, 1, script->totscript, 0, 0, "Total / Active Script link (LeftMouse + Drag to change)");
972
973         uiBlockSetCol(block, BUTSALMON);
974
975         if (scene) {
976                 if (script->totscript<32767) 
977                         uiDefBut(block, BUT, B_SSCRIPT_ADD, "New", (short)(sx+350), (short)sy, 38, 19, 0, 0, 0, 0, 0, "Add a new Script link");
978                 if (script->totscript) 
979                         uiDefBut(block, BUT, B_SSCRIPT_DEL, "Del", (short)(sx+390), (short)sy, 38, 19, 0, 0, 0, 0, 0, "Delete the current Script link");
980         } else {
981                 if (script->totscript<32767) 
982                         uiDefBut(block, BUT, B_SCRIPT_ADD, "New", (short)(sx+350), (short)sy, 38, 19, 0, 0, 0, 0, 0, "Add a new Script link");
983                 if (script->totscript) 
984                         uiDefBut(block, BUT, B_SCRIPT_DEL, "Del", (short)(sx+390), (short)sy, 38, 19, 0, 0, 0, 0, 0, "Delete the current Script link");
985         }               
986 }
987
988 void scriptbuts(void)
989 {
990         Object *ob=NULL;
991         ScriptLink *script=NULL;
992         Material *ma;
993         uiBlock *block;
994         char str[64];
995         
996         ob= OBACT;
997
998         sprintf(str, "buttonswin %d", curarea->win);
999         block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
1000
1001         if (ob && G.buts->scriptblock==ID_OB) {
1002                 script= &ob->scriptlink;
1003                 
1004         } else if (ob && G.buts->scriptblock==ID_MA) {
1005                 ma= give_current_material(ob, ob->actcol);
1006                 if (ma) script= &ma->scriptlink;
1007                 
1008         } else if (ob && G.buts->scriptblock==ID_CA) {
1009                 if (ob->type==OB_CAMERA)
1010                         script= &((Camera *)ob->data)->scriptlink;
1011                         
1012         } else if (ob && G.buts->scriptblock==ID_LA) {
1013                 if (ob->type==OB_LAMP)
1014                         script= &((Lamp *)ob->data)->scriptlink;
1015
1016         } else if (G.buts->scriptblock==ID_WO) {
1017                 if (G.scene->world)
1018                         script= &(G.scene->world->scriptlink);
1019         }
1020
1021         if (script) draw_scriptlink(block, script, 25, 180, 0);                 
1022         
1023         /* EVENTS */
1024         draw_buttons_edge(curarea->win, 540);
1025
1026         draw_scriptlink(block, &G.scene->scriptlink, 600, 180, 1);
1027
1028         uiDrawBlock(block);
1029 }
1030
1031 /* *************************** IKA ******************************** */
1032 /* is this number used elsewhere? */
1033 /*  static int ika_del_number; */
1034 void do_ikabuts(unsigned short event)
1035 {
1036         Base *base;
1037         Object *ob;
1038         
1039         ob= OBACT;
1040         
1041         switch(event) {
1042         case B_IKASETREF:
1043                 base= FIRSTBASE;
1044                 while(base) {
1045                         if TESTBASELIB(base) {
1046                                 if(base->object->type==OB_IKA) init_defstate_ika(base->object);
1047                         }
1048                         base= base->next;
1049                 }
1050                 break;  
1051         case B_IKARECALC:
1052                 itterate_ika(ob);
1053                 break;
1054         }
1055 }
1056
1057 void ikabuts(void)
1058 {
1059         Ika *ika;
1060         Object *ob;
1061         Limb *li;
1062         Deform *def;
1063         uiBlock *block;
1064         int nr, cury, nlimbs;
1065         char str[32];
1066         
1067         ob= OBACT;
1068         if(ob==0) return;
1069
1070         sprintf(str, "editbuttonswin %d", curarea->win);
1071         block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
1072
1073         ika= ob->data;
1074         
1075         uiBlockSetCol(block, BUTSALMON);
1076         uiDefBut(block, BUT, B_IKASETREF,       "Set Reference",470,180,200,20, 0, 0, 0, 0, 0, "");
1077
1078         uiBlockSetCol(block, BUTGREEN);
1079         uiDefButS(block, TOG|BIT|1, B_DIFF, "Lock XY Plane",    470,140,200,20, &ika->flag, 0.0, 1.0, 0, 0, "New IK option: allows both X and Y axes to rotate");
1080         uiBlockSetCol(block, BUTGREY);
1081         uiDefButF(block, NUM, B_DIFF, "XY constraint ",         470,120,200,20, &ika->xyconstraint, 0.0, 1.0, 100, 0, "Constrain in radians");
1082
1083         uiDefButF(block, NUMSLI, B_DIFF, "Mem ",                                470,80,200,20, &ika->mem, 0.0, 1.0, 0, 0, "");
1084         uiDefButS(block, NUM, B_DIFF, "Iter: ",                         470,60,200,20, &ika->iter, 2.0, 16.0, 0, 0, "");
1085
1086
1087         uiBlockSetCol(block, BUTGREY);
1088
1089         uiDefBut(block, LABEL, 0, "Limb Weight",                        680, 200, 150, 19, 0, 0, 0, 0, 0, "");
1090         cury= 180;
1091         li= ika->limbbase.first;
1092
1093         nlimbs= BLI_countlist(&ika->limbbase);
1094
1095         for (nr = 0; nr < nlimbs; nr++) {
1096                 sprintf(str, "Limb %d:", nr);
1097                 uiDefButF(block, NUM, B_DIFF, str, 680, (short)cury, 150, 19, &li->fac, 0.01, 1.0, 10, 0, "");
1098                 cury-= 20;
1099                 li= li->next;
1100         }
1101
1102         
1103         
1104         uiDefBut(block, LABEL, 0, "Deform Max Dist",    955, 200, 140, 19, 0, 0, 0, 0, 0, "");
1105         uiDefBut(block, LABEL, 0, "Deform Weight",      1095, 200, 140, 19, 0, 0, 0, 0, 0, "");
1106         
1107
1108         cury= 180;
1109         def= ika->def;
1110         for (nr = 0; nr < ika->totdef; nr++) {
1111                 def = ika->def+nr;
1112                 if(def->ob) {
1113                         if(def->ob->type!=OB_IKA) sprintf(str, "%s   :", def->ob->id.name+2);
1114                         else sprintf(str, "%s (%d):", def->ob->id.name+2, def->par1);
1115                 }
1116                 
1117                 uiDefBut(block, LABEL, 0, str,                  855, (short)cury, 100, 19, 0, 0.01, 0.0, 0, 0, "");
1118                 uiDefButF(block, NUM, B_DIFF, "",       955, (short)cury, 140, 19, &def->dist, 0.0, 40.0, 100, 0, "Beyond this distance the Limb doesn't influence deformation. '0.0' is global influence.");
1119                 uiDefButF(block, NUM, B_DIFF, "",       1095,(short)cury, 140, 19, &def->fac, 0.01, 10.0, 10, 0, "");
1120
1121                 cury-= 20;
1122         }
1123         uiDrawBlock(block);
1124 }
1125
1126 /* *************************** TEXTURE ******************************** */
1127
1128 Tex *cur_imatex=0;
1129 int prv_win= 0;
1130
1131 void load_tex_image(char *str)  /* called from fileselect */
1132 {
1133         Image *ima=0;
1134         Tex *tex;
1135         
1136         tex= cur_imatex;
1137         if(tex->type==TEX_IMAGE || tex->type==TEX_ENVMAP) {
1138
1139                 ima= add_image(str);
1140                 if(ima) {
1141                         if(tex->ima) {
1142                                 tex->ima->id.us--;
1143                         }
1144                         tex->ima= ima;
1145
1146                         free_image_buffers(ima);        /* force reading again */
1147                         ima->ok= 1;
1148                 }
1149
1150                 allqueue(REDRAWBUTSTEX, 0);
1151
1152                 BIF_preview_changed(G.buts);
1153         }
1154 }
1155
1156 void load_plugin_tex(char *str) /* called from fileselect */
1157 {
1158         Tex *tex;
1159         
1160         tex= cur_imatex;
1161         if(tex->type!=TEX_PLUGIN) return;
1162         
1163         if(tex->plugin) free_plugin_tex(tex->plugin);
1164         
1165         tex->stype= 0;
1166         tex->plugin= add_plugin_tex(str);
1167
1168         allqueue(REDRAWBUTSTEX, 0);
1169         BIF_preview_changed(G.buts);
1170 }
1171
1172 int vergcband(const void *a1, const void *a2)
1173 {
1174         const CBData *x1=a1, *x2=a2;
1175
1176         if( x1->pos > x2->pos ) return 1;
1177         else if( x1->pos < x2->pos) return -1;
1178         return 0;
1179 }
1180
1181
1182
1183 void save_env(char *name)
1184 {
1185         Tex *tex;
1186         char str[FILE_MAXFILE];
1187         
1188         strcpy(str, name);
1189         BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
1190         tex= G.buts->lockpoin;
1191         
1192         if(tex && GS(tex->id.name)==ID_TE) {
1193                 if(tex->env && tex->env->ok && saveover(str)) {
1194                         waitcursor(1);
1195                         BIF_save_envmap(tex->env, str);
1196                         strcpy(G.ima, name);
1197                         waitcursor(0);
1198                 }
1199         }
1200         
1201 }
1202
1203 void drawcolorband(ColorBand *coba, float x1, float y1, float sizex, float sizey)
1204 {
1205         CBData *cbd;
1206         float v3[2], v1[2], v2[2];
1207         int a;
1208         
1209         if(coba==0) return;
1210         
1211         /* outline */
1212         v1[0]= x1; v1[1]= y1;
1213         glLineWidth((GLfloat)(3));
1214         cpack(0x0);
1215         glBegin(GL_LINE_LOOP);
1216                 glVertex2fv(v1);
1217                 v1[0]+= sizex;
1218                 glVertex2fv(v1);
1219                 v1[1]+= sizey;
1220                 glVertex2fv(v1);
1221                 v1[0]-= sizex;
1222                 glVertex2fv(v1);
1223         glEnd();
1224         glLineWidth((GLfloat)(1));
1225
1226
1227         glShadeModel(GL_SMOOTH);
1228         cbd= coba->data;
1229         
1230         v1[0]= v2[0]= x1;
1231         v1[1]= y1;
1232         v2[1]= y1+sizey;
1233         
1234         glBegin(GL_QUAD_STRIP);
1235         
1236         glColor3fv( &cbd->r );
1237         glVertex2fv(v1); glVertex2fv(v2);
1238         
1239         for(a=0; a<coba->tot; a++, cbd++) {
1240                 
1241                 v1[0]=v2[0]= x1+ cbd->pos*sizex;
1242
1243                 glColor3fv( &cbd->r );
1244                 glVertex2fv(v1); glVertex2fv(v2);
1245         }
1246         
1247         v1[0]=v2[0]= x1+ sizex;
1248         glVertex2fv(v1); glVertex2fv(v2);
1249         
1250         glEnd();
1251         glShadeModel(GL_FLAT);
1252         
1253         /* help lines */
1254         
1255         v1[0]= v2[0]=v3[0]= x1;
1256         v1[1]= y1;
1257         v2[1]= y1+0.5*sizey;
1258         v3[1]= y1+sizey;
1259         
1260         cbd= coba->data;
1261         glBegin(GL_LINES);
1262         for(a=0; a<coba->tot; a++, cbd++) {
1263                 v1[0]=v2[0]=v3[0]= x1+ cbd->pos*sizex;
1264                 
1265                 glColor3ub(0, 0, 0);
1266                 glVertex2fv(v1);
1267                 glVertex2fv(v2);
1268
1269                 if(a==coba->cur) {
1270                         glVertex2f(v1[0]-1, v1[1]);
1271                         glVertex2f(v2[0]-1, v2[1]);
1272                         glVertex2f(v1[0]+1, v1[1]);
1273                         glVertex2f(v2[0]+1, v2[1]);
1274                 }
1275                         
1276                 glColor3ub(255, 255, 255);
1277                 glVertex2fv(v2);
1278                 glVertex2fv(v3);
1279                 
1280                 if(a==coba->cur) {
1281                         glVertex2f(v2[0]-1, v2[1]);
1282                         glVertex2f(v3[0]-1, v3[1]);
1283                         glVertex2f(v2[0]+1, v2[1]);
1284                         glVertex2f(v3[0]+1, v3[1]);
1285                 }
1286         }
1287         glEnd();
1288         
1289         glFlush();
1290 }
1291
1292
1293
1294 void do_texbuts(unsigned short event)
1295 {
1296         Tex *tex;
1297         ImBuf *ibuf;
1298         ScrArea *sa;
1299         ID *id;
1300         CBData *cbd;
1301         float dx;
1302         int a, nr;
1303         short mvalo[2], mval[2];
1304         char *name, str[80];
1305         
1306         tex= G.buts->lockpoin;
1307         
1308         switch(event) {
1309         case B_TEXCHANNEL:
1310                 scrarea_queue_headredraw(curarea);
1311                 BIF_preview_changed(G.buts);
1312                 allqueue(REDRAWBUTSTEX, 0);
1313                 break;
1314         case B_TEXTYPE:
1315                 if(tex==0) return;
1316                 tex->stype= 0;
1317                 allqueue(REDRAWBUTSTEX, 0);
1318                 BIF_preview_changed(G.buts);
1319                 break;
1320         case B_DEFTEXVAR:
1321                 if(tex==0) return;
1322                 default_tex(tex);
1323                 allqueue(REDRAWBUTSTEX, 0);
1324                 BIF_preview_changed(G.buts);
1325                 break;
1326         case B_LOADTEXIMA:
1327         case B_LOADTEXIMA1:
1328                 if(tex==0) return;
1329                 /* globals: temporal store them: we make another area a fileselect */
1330                 cur_imatex= tex;
1331                 prv_win= curarea->win;
1332                 
1333                 sa= closest_bigger_area();
1334                 areawinset(sa->win);
1335                 if(tex->ima) name= tex->ima->name;
1336 #ifdef _WIN32
1337                 else {
1338                         if (strcmp (U.textudir, "/") == 0)
1339                                 name= G.sce;
1340                         else
1341                                 name= U.textudir;
1342                 }
1343 #else
1344                 else name = U.textudir;
1345 #endif
1346                 
1347                 if(event==B_LOADTEXIMA)
1348                         activate_imageselect(FILE_SPECIAL, "SELECT IMAGE", name, load_tex_image);
1349                 else 
1350                         activate_fileselect(FILE_SPECIAL, "SELECT IMAGE", name, load_tex_image);
1351                 
1352                 break;
1353         case B_NAMEIMA:
1354                 if(tex==0) return;
1355                 if(tex->ima) {
1356                         cur_imatex= tex;
1357                         prv_win= curarea->win;
1358                         
1359                         /* name in tex->ima has been changed by button! */
1360                         strcpy(str, tex->ima->name);
1361                         if(tex->ima->ibuf) strcpy(tex->ima->name, tex->ima->ibuf->name);
1362
1363                         load_tex_image(str);
1364                 }
1365                 break;
1366         case B_TEXREDR_PRV:
1367                 allqueue(REDRAWBUTSTEX, 0);
1368                 BIF_preview_changed(G.buts);
1369                 break;
1370         case B_TEXIMABROWSE:
1371                 if(tex) {
1372                         id= (ID*) tex->ima;
1373                         
1374                         if(G.buts->menunr== -2) {
1375                                 activate_databrowse(id, ID_IM, 0, B_TEXIMABROWSE, &G.buts->menunr, do_texbuts);
1376                         } else if (G.buts->menunr>0) {
1377                                 Image *newima= (Image*) BLI_findlink(&G.main->image, G.buts->menunr-1);
1378                                 
1379                                 if (newima && newima!=(Image*) id) {
1380                                         tex->ima= newima;
1381                                         id_us_plus((ID*) newima);
1382                                         if(id) id->us--;
1383                                 
1384                                         allqueue(REDRAWBUTSTEX, 0);
1385                                         BIF_preview_changed(G.buts);
1386                                 }
1387                         }
1388                 }
1389                 break;
1390         case B_IMAPTEST:
1391                 if(tex) {
1392                         if( (tex->imaflag & (TEX_FIELDS+TEX_MIPMAP))== TEX_FIELDS+TEX_MIPMAP ) {
1393                                 error("Cannot combine fields and mipmap");
1394                                 tex->imaflag -= TEX_MIPMAP;
1395                                 allqueue(REDRAWBUTSTEX, 0);
1396                         }
1397                         
1398                         if(tex->ima && tex->ima->ibuf) {
1399                                 ibuf= tex->ima->ibuf;
1400                                 nr= 0;
1401                                 if( !(tex->imaflag & TEX_FIELDS) && (ibuf->flags & IB_fields) ) nr= 1;
1402                                 if( (tex->imaflag & TEX_FIELDS) && !(ibuf->flags & IB_fields) ) nr= 1;
1403                                 if(nr) {
1404                                         IMB_freeImBuf(ibuf);
1405                                         tex->ima->ibuf= 0;
1406                                         tex->ima->ok= 1;
1407                                         BIF_preview_changed(G.buts);
1408                                 }
1409                         }
1410                 }
1411                 break;
1412         case B_RELOADIMA:
1413                 if(tex && tex->ima) {
1414                         // check if there is a newer packedfile
1415
1416                         if (tex->ima->packedfile) {
1417                                 PackedFile *pf;
1418                                 pf = newPackedFile(tex->ima->name);
1419                                 if (pf) {
1420                                         freePackedFile(tex->ima->packedfile);
1421                                         tex->ima->packedfile = pf;
1422                                 } else {
1423                                         error("Image not available. Keeping packed image.");
1424                                 }
1425                         }
1426
1427                         IMB_freeImBuf(tex->ima->ibuf);
1428                         tex->ima->ibuf= 0;
1429                         tex->ima->ok= 1;
1430                         allqueue(REDRAWBUTSTEX, 0);
1431                         allqueue(REDRAWVIEW3D, 0);
1432                         allqueue(REDRAWIMAGE, 0);
1433                         BIF_preview_changed(G.buts);
1434                 }
1435                 break;
1436
1437         case B_TEXSETFRAMES:
1438                 if(tex->ima->anim) tex->frames = IMB_anim_get_duration(tex->ima->anim);
1439                 allqueue(REDRAWBUTSTEX, 0);
1440                 break;
1441
1442         case B_PACKIMA:
1443                 if(tex && tex->ima) {
1444                         if (tex->ima->packedfile) {
1445                                 if (G.fileflags & G_AUTOPACK) {
1446                                         if (okee("Disable AutoPack ?")) {
1447                                                 G.fileflags &= ~G_AUTOPACK;
1448                                         }
1449                                 }
1450                                 
1451                                 if ((G.fileflags & G_AUTOPACK) == 0) {
1452                                         unpackImage(tex->ima, PF_ASK);
1453                                 }
1454                         } else {
1455                                 if (tex->ima->ibuf && (tex->ima->ibuf->userflags & IB_BITMAPDIRTY)) {
1456                                         error("Can't pack painted image. Save image from Image window first.");
1457                                 } else {
1458                                         tex->ima->packedfile = newPackedFile(tex->ima->name);
1459                                 }
1460                         }
1461                         allqueue(REDRAWBUTSTEX, 0);
1462                         allqueue(REDRAWHEADERS, 0);
1463                 }
1464                 break;
1465         case B_LOADPLUGIN:
1466                 if(tex==0) return;
1467
1468                 /* globals: store temporal: we make another area a fileselect */
1469                 cur_imatex= tex;
1470                 prv_win= curarea->win;
1471                         
1472                 sa= closest_bigger_area();
1473                 areawinset(sa->win);
1474                 if(tex->plugin) strcpy(str, tex->plugin->name);
1475                 else {
1476                         strcpy(str, U.plugtexdir);
1477                 }
1478                 activate_fileselect(FILE_SPECIAL, "SELECT PLUGIN", str, load_plugin_tex);
1479                 
1480                 break;
1481
1482         case B_NAMEPLUGIN:
1483                 if(tex==0 || tex->plugin==0) return;
1484                 strcpy(str, tex->plugin->name);
1485                 free_plugin_tex(tex->plugin);
1486                 tex->stype= 0;
1487                 tex->plugin= add_plugin_tex(str);
1488                 allqueue(REDRAWBUTSTEX, 0);
1489                 BIF_preview_changed(G.buts);
1490                 break;
1491         
1492         case B_COLORBAND:
1493                 if(tex==0) return;
1494                 if(tex->coba==0) tex->coba= add_colorband();
1495                 allqueue(REDRAWBUTSTEX, 0);
1496                 BIF_preview_changed(G.buts);
1497                 break;
1498         
1499         case B_ADDCOLORBAND:
1500                 if(tex==0 || tex->coba==0) return;
1501                 
1502                 if(tex->coba->tot < MAXCOLORBAND-1) tex->coba->tot++;
1503                 tex->coba->cur= tex->coba->tot-1;
1504                 
1505                 do_texbuts(B_CALCCBAND);
1506                 
1507                 break;
1508
1509         case B_DELCOLORBAND:
1510                 if(tex==0 || tex->coba==0 || tex->coba->tot<2) return;
1511                 
1512                 for(a=tex->coba->cur; a<tex->coba->tot; a++) {
1513                         tex->coba->data[a]= tex->coba->data[a+1];
1514                 }
1515                 if(tex->coba->cur) tex->coba->cur--;
1516                 tex->coba->tot--;
1517
1518                 allqueue(REDRAWBUTSTEX, 0);
1519                 BIF_preview_changed(G.buts);
1520                 break;
1521
1522         case B_CALCCBAND:
1523         case B_CALCCBAND2:
1524                 if(tex==0 || tex->coba==0 || tex->coba->tot<2) return;
1525                 
1526                 for(a=0; a<tex->coba->tot; a++) tex->coba->data[a].cur= a;
1527                 qsort(tex->coba->data, tex->coba->tot, sizeof(CBData), vergcband);
1528                 for(a=0; a<tex->coba->tot; a++) {
1529                         if(tex->coba->data[a].cur==tex->coba->cur) {
1530                                 if(tex->coba->cur!=a) addqueue(curarea->win, REDRAW, 0);        /* button cur */
1531                                 tex->coba->cur= a;
1532                                 break;
1533                         }
1534                 }
1535                 if(event==B_CALCCBAND2) return;
1536                 
1537                 allqueue(REDRAWBUTSTEX, 0);
1538                 BIF_preview_changed(G.buts);
1539                 
1540                 break;
1541                 
1542         case B_DOCOLORBAND:
1543                 if(tex==0 || tex->coba==0) return;
1544                 
1545                 cbd= tex->coba->data + tex->coba->cur;
1546                 uiGetMouse(mywinget(), mvalo);
1547
1548                 while(get_mbut() & L_MOUSE) {
1549                         uiGetMouse(mywinget(), mval);
1550                         if(mval[0]!=mvalo[0]) {
1551                                 dx= mval[0]-mvalo[0];
1552                                 dx/= 345.0;
1553                                 cbd->pos+= dx;
1554                                 CLAMP(cbd->pos, 0.0, 1.0);
1555
1556                                 glDrawBuffer(GL_FRONT);
1557                                 drawcolorband(tex->coba, 923,81,345,20);
1558                                 /* uiSetButs(B_CALCCBAND, B_CALCCBAND); */
1559                                 glDrawBuffer(GL_BACK);
1560                                 
1561                                 do_texbuts(B_CALCCBAND2);
1562                                 cbd= tex->coba->data + tex->coba->cur;  /* because qsort */
1563                                 
1564                                 mvalo[0]= mval[0];
1565                         }
1566                         BIF_wait_for_statechange();
1567                 }
1568                 allqueue(REDRAWBUTSTEX, 0);
1569                 BIF_preview_changed(G.buts);
1570                 
1571                 break;
1572         
1573         case B_REDRAWCBAND:
1574                 glDrawBuffer(GL_FRONT);
1575                 drawcolorband(tex->coba, 923,81,345,20);
1576                 glDrawBuffer(GL_BACK);
1577                 BIF_preview_changed(G.buts);
1578                 break;
1579         
1580         case B_ENV_DELETE:
1581                 if(tex->env) {
1582                         RE_free_envmap(tex->env);
1583                         tex->env= 0;
1584                         allqueue(REDRAWBUTSTEX, 0);
1585                         BIF_preview_changed(G.buts);
1586                 }
1587                 break;
1588         case B_ENV_FREE:
1589                 if(tex->env) {
1590                         RE_free_envmapdata(tex->env);
1591                         allqueue(REDRAWBUTSTEX, 0);
1592                         BIF_preview_changed(G.buts);
1593                 }
1594                 break;
1595         case B_ENV_FREE_ALL:
1596                 tex= G.main->tex.first;
1597                 while(tex) {
1598                         if(tex->id.us && tex->type==TEX_ENVMAP) {
1599                                 if(tex->env) {
1600                                         if(tex->env->stype!=ENV_LOAD) RE_free_envmapdata(tex->env);
1601                                 }
1602                         }
1603                         tex= tex->id.next;
1604                 }
1605                 allqueue(REDRAWBUTSTEX, 0);
1606                 BIF_preview_changed(G.buts);
1607                 break;
1608         case B_ENV_SAVE:
1609                 if(tex->env && tex->env->ok) {
1610                         sa= closest_bigger_area();
1611                         areawinset(sa->win);
1612                         save_image_filesel_str(str);
1613                         activate_fileselect(FILE_SPECIAL, str, G.ima, save_env);
1614                 }
1615                 break;  
1616         case B_ENV_OB:
1617                 if(tex->env && tex->env->object) {
1618                         BIF_preview_changed(G.buts);
1619                         if ELEM(tex->env->object->type, OB_CAMERA, OB_LAMP) {
1620                                 error("Camera or Lamp not allowed");
1621                                 tex->env->object= 0;
1622                         }
1623                 }
1624                 break;
1625                 
1626         default:
1627                 if(event>=B_PLUGBUT && event<=B_PLUGBUT+23) {
1628                         PluginTex *pit= tex->plugin;
1629                         if(pit && pit->callback) {
1630                                 pit->callback(event - B_PLUGBUT);
1631                                 BIF_preview_changed(G.buts);
1632                         }
1633                 }
1634         }
1635 }
1636
1637 static  void test_idbutton_cb(void *namev, void *arg2_unused)
1638 {
1639         char *name= namev;
1640         test_idbutton(name+2);
1641 }
1642
1643 void texbuts(void)
1644 {
1645         Object *ob;
1646         Material *ma=0;
1647         World *wrld=0;
1648         Lamp *la=0;
1649         ID *id = NULL;
1650         MTex *mtex = NULL;
1651         Tex *tex;
1652         VarStruct *varstr;
1653         PluginTex *pit;
1654         CBData *cbd;
1655         EnvMap *env;
1656         uiBlock *block;
1657         uiBut *but;
1658         int a, xco, yco, loos, dx, dy, ok;
1659         char str[30], *strp;
1660         
1661         sprintf(str, "buttonswin %d", curarea->win);
1662         block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
1663
1664         uiBlockSetCol(block, BUTSALMON);
1665
1666         uiDefButC(block, ROW, B_TEXREDR_PRV, "Mat",             200,172,40,20, &G.buts->texfrom, 3.0, 0.0, 0, 0, "Display the texture of the active material");
1667         uiDefButC(block, ROW, B_TEXREDR_PRV, "World",           240,172,52,20, &G.buts->texfrom, 3.0, 1.0, 0, 0, "Display the texture of the world block");
1668         uiDefButC(block, ROW, B_TEXREDR_PRV, "Lamp",            292,172,46,20, &G.buts->texfrom, 3.0, 2.0, 0, 0, "Display the texture of the lamp");
1669         uiBlockSetCol(block, BUTGREY);
1670         
1671         ok= 0;
1672         
1673         if(G.buts->texfrom==0) {
1674                 ob= OBACT;
1675                 if(ob) {
1676                         id= ob->data;
1677                         if(id) {
1678                                 ma= give_current_material(ob, ob->actcol);
1679                                 if(ma) ok= 1;
1680                         }
1681                 }
1682                 
1683         }
1684         else if(G.buts->texfrom==1) {
1685                 wrld= G.scene->world;
1686                 if(wrld) {
1687                         id= (ID *)wrld;
1688                         ok= 1;
1689                 }
1690         }
1691         else if(G.buts->texfrom==2) {
1692                 ob= OBACT;
1693                 if(ob) {
1694                         if(ob->type==OB_LAMP) {
1695                                 la= ob->data;
1696                                 id= (ID *)la;
1697                                 ok= 1;
1698                         }
1699                 }
1700         }
1701         
1702         if(ok==0) {
1703                 uiDrawBlock(block);
1704                 return;
1705         }
1706         
1707         uiSetButLock(id->lib!=0, "Can't edit library data");
1708
1709         /* CHANNELS */
1710         yco= 140;
1711         for(a= 0; a<8; a++) {
1712                 if(G.buts->texfrom==0) mtex= ma->mtex[a];
1713                 else if(G.buts->texfrom==1) mtex= wrld->mtex[a];
1714                 else if(G.buts->texfrom==2)  mtex= la->mtex[a];
1715                 
1716                 if(mtex && mtex->tex) splitIDname(mtex->tex->id.name+2, str, &loos);
1717                 else strcpy(str, "");
1718                 str[14]= 0;
1719                 if(G.buts->texfrom==0) {
1720                         uiDefButC(block, ROW, B_TEXCHANNEL, str,        200,(short)yco,140,18, &(ma->texact), 0.0, (float)a, 0, 0, "Linked channel");
1721                 }
1722                 else if(G.buts->texfrom==1) {
1723                         uiDefButS(block, ROW, B_TEXCHANNEL, str,        200,(short)yco,140,18, &(wrld->texact), 0.0, (float)a, 0, 0, "");
1724                         if(a==5) break;
1725                 }
1726                 else if(G.buts->texfrom==2) {
1727                         uiDefButS(block, ROW, B_TEXCHANNEL, str,        200,(short)yco,140,18, &(la->texact), 0.0, (float)a, 0, 0, "");
1728                         if(a==5) break;
1729                 }
1730                 yco-= 19;
1731         }
1732         
1733         if(G.buts->texfrom==0) {
1734                 but= uiDefBut(block, TEX, B_IDNAME, "MA:",      200,195,140,20, ma->id.name+2, 0.0, 18.0, 0, 0, "Name of the datablock");
1735                 uiButSetFunc(but, test_idbutton_cb, ma->id.name, NULL);
1736                 mtex= ma->mtex[ ma->texact ];
1737         }
1738         else if(G.buts->texfrom==1) {
1739                 but= uiDefBut(block, TEX, B_IDNAME, "WO:",                                      200,195,140,20, wrld->id.name+2, 0.0, 18.0, 0, 0, "Name of the datablock");
1740                 uiButSetFunc(but, test_idbutton_cb, wrld->id.name, NULL);
1741                 mtex= wrld->mtex[ wrld->texact ];
1742         }
1743         else if(G.buts->texfrom==2) {
1744                 but= uiDefBut(block, TEX, B_IDNAME, "LA:",                                      200,195,140,20, la->id.name+2, 0.0, 18.0, 0, 0, "Name of the datablock");
1745                 uiButSetFunc(but, test_idbutton_cb, la->id.name, NULL);
1746                 mtex= la->mtex[ la->texact ];
1747         }
1748
1749         if(mtex && mtex->tex) {
1750                 tex= mtex->tex;
1751
1752                 uiSetButLock(tex->id.lib!=0, "Can't edit library data");
1753                 xco= 275;
1754                 uiDefButS(block, ROW, B_TEXTYPE, texstr[0],     (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, 0.0, 0, 0, "Default");
1755                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_IMAGE],(short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_IMAGE, 0, 0, "Use image texture");
1756                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_ENVMAP],    (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_ENVMAP, 0, 0, "Use environment maps");
1757                 if(tex->plugin && tex->plugin->doit) strp= tex->plugin->pname; else strp= texstr[TEX_PLUGIN];
1758                 uiDefButS(block, ROW, B_TEXTYPE, strp,                          (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_PLUGIN, 0, 0, "Use plugin");
1759                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_CLOUDS],    (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_CLOUDS, 0, 0, "Use clouds texture");
1760                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_WOOD],      (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_WOOD, 0, 0, "Use wood texture");
1761                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_MARBLE],    (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_MARBLE, 0, 0, "Use marble texture");
1762                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_MAGIC],     (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_MAGIC, 0, 0, "Use magic texture");
1763                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_BLEND],     (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_BLEND, 0, 0, "Use blend texture");
1764                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_STUCCI],    (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_STUCCI, 0, 0, "Use strucci texture");
1765                 uiDefButS(block, ROW, B_TEXTYPE, texstr[TEX_NOISE],     (short)(xco+=75), 195, 75, 20, &tex->type, 1.0, (float)TEX_NOISE, 0, 0, "Use noise texture");
1766                 
1767                 /* TYPES */
1768                 uiBlockSetCol(block, BUTGREEN); 
1769                 switch(tex->type) {
1770                 case TEX_CLOUDS:
1771                         uiDefButS(block, ROW, B_MATPRV, "Default",      350, 170, 75, 18, &tex->stype, 2.0, 0.0, 0, 0, "Use standard noise"); 
1772                         uiDefButS(block, ROW, B_MATPRV, "Color",                425, 170, 75, 18, &tex->stype, 2.0, 1.0, 0, 0, "Let Noise give RGB value"); 
1773                         uiBlockSetCol(block, BUTGREY);  
1774                         uiDefButF(block, NUM, B_MATPRV, "NoiseSize :",  350, 110, 150, 19, &tex->noisesize, 0.0001, 2.0, 10, 0, "Set the dimension of the noise table");
1775                         uiDefButS(block, NUM, B_MATPRV, "NoiseDepth:",  350, 90, 150, 19, &tex->noisedepth, 0.0, 6.0, 0, 0, "Set the depth of the cloud calculation");
1776                         uiBlockSetCol(block, BUTGREEN);
1777                         uiDefButS(block, ROW, B_MATPRV, "Soft noise",           350, 40, 100, 19, &tex->noisetype, 12.0, 0.0, 0, 0, "Use soft noise");
1778                         uiDefButS(block, ROW, B_MATPRV, "Hard noise",           450, 40, 100, 19, &tex->noisetype, 12.0, 1.0, 0, 0, "Use hard noise");
1779                         break;
1780         
1781                 case TEX_WOOD:
1782                         uiDefButS(block, ROW, B_MATPRV, "Bands",                350, 170, 75, 18, &tex->stype, 2.0, 0.0, 0, 0, "Use standard wood texture"); 
1783                         uiDefButS(block, ROW, B_MATPRV, "Rings",                425, 170, 75, 18, &tex->stype, 2.0, 1.0, 0, 0, "Use wood rings"); 
1784                         uiDefButS(block, ROW, B_MATPRV, "BandNoise",    500, 170, 75, 18, &tex->stype, 2.0, 2.0, 0, 0, "Add noise to standard wood"); 
1785                         uiDefButS(block, ROW, B_MATPRV, "RingNoise",    575, 170, 75, 18, &tex->stype, 2.0, 3.0, 0, 0, "Add noise to rings"); 
1786                         uiBlockSetCol(block, BUTGREY);  
1787                         uiDefButF(block, NUM, B_MATPRV, "NoiseSize :",  350, 110, 150, 19, &tex->noisesize, 0.0001, 2.0, 10, 0, "Set the dimension of the noise table");
1788                         uiDefButF(block, NUM, B_MATPRV, "Turbulence:",  350, 90, 150, 19, &tex->turbul, 0.0, 200.0, 10, 0, "Set the turbulence of the bandnoise and ringnoise types");
1789                         uiBlockSetCol(block, BUTGREEN);
1790                         uiDefButS(block, ROW, B_MATPRV, "Soft noise",           350, 40, 100, 19, &tex->noisetype, 12.0, 0.0, 0, 0, "Use soft noise");
1791                         uiDefButS(block, ROW, B_MATPRV, "Hard noise",           450, 40, 100, 19, &tex->noisetype, 12.0, 1.0, 0, 0, "Use hard noise");
1792                         break;
1793         
1794                 case TEX_MARBLE:
1795                         uiDefButS(block, ROW, B_MATPRV, "Soft",         350, 170, 75, 18, &tex->stype, 2.0, 0.0, 0, 0, "Use soft marble"); 
1796                         uiDefButS(block, ROW, B_MATPRV, "Sharp",                425, 170, 75, 18, &tex->stype, 2.0, 1.0, 0, 0, "Use more clearly defined marble"); 
1797                         uiDefButS(block, ROW, B_MATPRV, "Sharper",      500, 170, 75, 18, &tex->stype, 2.0, 2.0, 0, 0, "Use very clear defined marble"); 
1798                         uiBlockSetCol(block, BUTGREY);  
1799                         uiDefButF(block, NUM, B_MATPRV, "NoiseSize :",  350, 110, 150, 19, &tex->noisesize, 0.0001, 2.0, 10, 0, "Set the dimension of the noise table");
1800                         uiDefButS(block, NUM, B_MATPRV, "NoiseDepth:",  350, 90, 150, 19, &tex->noisedepth, 0.0, 6.0, 0, 0, "Set the depth of the marble calculation");
1801                         uiDefButF(block, NUM, B_MATPRV, "Turbulence:",  350, 70, 150, 19, &tex->turbul, 0.0, 200.0, 10, 0, "Set the turbulence of the sine bands");
1802                         uiBlockSetCol(block, BUTGREEN);
1803                         uiDefButS(block, ROW, B_MATPRV, "Soft noise",           350, 40, 100, 19, &tex->noisetype, 12.0, 0.0, 0, 0, "Use soft noise");
1804                         uiDefButS(block, ROW, B_MATPRV, "Hard noise",           450, 40, 100, 19, &tex->noisetype, 12.0, 1.0, 0, 0, "Use hard noise");
1805                         break;
1806         
1807                 case TEX_MAGIC:
1808                         uiBlockSetCol(block, BUTGREY);
1809                         uiDefButF(block, NUM, B_MATPRV, "Size :",                       350, 110, 150, 19, &tex->noisesize, 0.0001, 2.0, 10, 0, "Set the dimension of the pattern");
1810                         uiDefButS(block, NUM, B_MATPRV, "Depth:",                       350, 90, 150, 19, &tex->noisedepth, 0.0, 10.0, 0, 0, "Set the depth of the pattern");
1811                         uiDefButF(block, NUM, B_MATPRV, "Turbulence:",  350, 70, 150, 19, &tex->turbul, 0.0, 200.0, 10, 0, "Set the strength of the pattern");
1812                         break;
1813         
1814                 case TEX_BLEND:
1815                         uiDefButS(block, ROW, B_MATPRV, "Lin",          350, 170, 75, 18, &tex->stype, 2.0, 0.0, 0, 0, "Use a linear progresion"); 
1816                         uiDefButS(block, ROW, B_MATPRV, "Quad",         425, 170, 75, 18, &tex->stype, 2.0, 1.0, 0, 0, "Use a quadratic progression"); 
1817                         uiDefButS(block, ROW, B_MATPRV, "Ease",         500, 170, 75, 18, &tex->stype, 2.0, 2.0, 0, 0, ""); 
1818                         uiDefButS(block, ROW, B_MATPRV, "Diag",         575, 170, 75, 18, &tex->stype, 2.0, 3.0, 0, 0, "Use a diagonal progression");
1819                         uiDefButS(block, ROW, B_MATPRV, "Sphere",               650, 170, 75, 18, &tex->stype, 2.0, 4.0, 0, 0, "Use progression with the shape of a sphere");
1820                         uiDefButS(block, ROW, B_MATPRV, "Halo",         725, 170, 75, 18, &tex->stype, 2.0, 5.0, 0, 0, "Use a quadratic progression with the shape of a sphere");
1821                         
1822                         uiDefButS(block, TOG|BIT|1, B_MATPRV, "Flip XY",        350, 130, 75, 18, &tex->flag, 0, 0, 0, 0, "Flip the direction of the progression a quarter turn");
1823                         break;
1824                         
1825                 case TEX_STUCCI:
1826                         uiDefButS(block, ROW, B_MATPRV, "Plastic",      350, 170, 75, 18, &tex->stype, 2.0, 0.0, 0, 0, "Use standard stucci");
1827                         uiDefButS(block, ROW, B_MATPRV, "Wall In",      425, 170, 75, 18, &tex->stype, 2.0, 1.0, 0, 0, "Set start value"); 
1828                         uiDefButS(block, ROW, B_MATPRV, "Wall Out",     500, 170, 75, 18, &tex->stype, 2.0, 2.0, 0, 0, "Set end value"); 
1829                         uiBlockSetCol(block, BUTGREY);  
1830                         uiDefButF(block, NUM, B_MATPRV, "NoiseSize :",  350, 110, 150, 19, &tex->noisesize, 0.0001, 2.0, 10, 0, "Set the dimension of the noise table");
1831                         uiDefButF(block, NUM, B_MATPRV, "Turbulence:",  350, 90, 150, 19, &tex->turbul, 0.0, 200.0, 10, 0, "Set the depth of the stucci");
1832                         uiBlockSetCol(block, BUTGREEN);
1833                         uiDefButS(block, ROW, B_MATPRV, "Soft noise",           350, 40, 100, 19, &tex->noisetype, 12.0, 0.0, 0, 0, "Use soft noise");
1834                         uiDefButS(block, ROW, B_MATPRV, "Hard noise",           450, 40, 100, 19, &tex->noisetype, 12.0, 1.0, 0, 0, "Use hard noise");
1835         
1836                         break;
1837                         
1838                 case TEX_NOISE:
1839                         break;
1840                         
1841                 case TEX_IMAGE:
1842                         
1843                         break;
1844                 }
1845                 
1846                 uiBlockSetCol(block, BUTSALMON);
1847                 uiDefBut(block, BUT, B_DEFTEXVAR, "Default Vars",       1180,169,93,47, 0, 0, 0, 0, 0, "Return to standard values");
1848                 
1849                 uiBlockSetCol(block, BUTGREY);
1850                 /* SPECIFIC */
1851                 if(tex->type==TEX_IMAGE) {
1852                         uiDefButF(block, NUM, B_REDR, "MinX ",          350,30,140,19, &tex->cropxmin, -10.0, 10.0, 10, 0, "Set minimum X value for cropping");
1853                         uiDefButF(block, NUM, B_REDR, "MaxX ",          350,10,140,19, &tex->cropxmax, -10.0, 10.0, 10, 0, "Set maximum X value for cropping");
1854                         uiDefButF(block, NUM, B_REDR, "MinY ",          494,30,140,19, &tex->cropymin, -10.0, 10.0, 10, 0, "Set minimum Y value for cropping");
1855                         uiDefButF(block, NUM, B_REDR, "MaxY ",          494,10,140,19, &tex->cropymax, -10.0, 10.0, 10, 0, "Set maximum Y value for cropping");
1856         
1857         
1858                         uiDefButS(block, ROW, 0, "Extend",                      350,85,69,19, &tex->extend, 4.0, 1.0, 0, 0, "Extend the colour of the edge");
1859                         uiDefButS(block, ROW, 0, "Clip",                                421,85,59,19, &tex->extend, 4.0, 2.0, 0, 0, "Return alpha 0.0 outside image");
1860                         uiDefButS(block, ROW, 0, "Repeat",                      565,85,68,19, &tex->extend, 4.0, 3.0, 0, 0, "Repeat image horizontally and vertically");
1861                         uiDefButS(block, ROW, 0, "ClipCube",                    482,85,82,19, &tex->extend, 4.0, 4.0, 0, 0, "Return alpha 0.0 outside cubeshaped area around image");
1862         
1863                         uiDefButF(block, NUM, B_MATPRV, "Filter :",     352,109,135,19, &tex->filtersize, 0.1, 25.0, 0, 0, "Set the filter size used by mipmap and interpol");
1864                         
1865                         uiDefButS(block, NUM, B_MATPRV, "Xrepeat:",     350,60,140,19, &tex->xrepeat, 1.0, 512.0, 0, 0, "Set the degree of repetition in the X direction");
1866                         uiDefButS(block, NUM, B_MATPRV, "Yrepeat:",     494,60,140,19, &tex->yrepeat, 1.0, 512.0, 0, 0, "Set the degree of repetition in the Y direction");
1867                         
1868                         uiDefButS(block, NUM, B_MATPRV, "Frames :",     642,110,150,19, &tex->frames, 0.0, 18000.0, 0, 0, "Activate animation option");
1869                         uiDefButS(block, NUM, B_MATPRV, "Offset :",     642,90,150,19, &tex->offset, -9000.0, 9000.0, 0, 0, "Set the number of the first picture of the animation");
1870                         uiDefButS(block, NUM, B_MATPRV, "Fie/Ima:",     642,60,98,19, &tex->fie_ima, 1.0, 200.0, 0, 0, "Set the number of fields per rendered frame");
1871                         uiDefButS(block, NUM, B_MATPRV, "StartFr:",     642,30,150,19, &tex->sfra, 1.0, 9000.0, 0, 0, "Set the start frame of the animation");
1872                         uiDefButS(block, NUM, B_MATPRV, "Len:",         642,10,150,19, &tex->len, 0.0, 9000.0, 0, 0, "Set the length of the animation");
1873         
1874                         uiDefButS(block, NUM, B_MATPRV, "Fra:",         802,70,73,19, &(tex->fradur[0][0]), 0.0, 18000.0, 0, 0, "Montage mode: frame start");
1875                         uiDefButS(block, NUM, B_MATPRV, "",                     879,70,37,19, &(tex->fradur[0][1]), 0.0, 250.0, 0, 0, "Montage mode: amount of displayed frames");
1876                         uiDefButS(block, NUM, B_MATPRV, "Fra:",         802,50,73,19, &(tex->fradur[1][0]), 0.0, 18000.0, 0, 0, "Montage mode: frame start");
1877                         uiDefButS(block, NUM, B_MATPRV, "",                     879,50,37,19, &(tex->fradur[1][1]), 0.0, 250.0, 0, 0, "Montage mode: amount of displayed frames");
1878                         uiDefButS(block, NUM, B_MATPRV, "Fra:",         802,30,73,19, &(tex->fradur[2][0]), 0.0, 18000.0, 0, 0, "Montage mode: frame start");
1879                         uiDefButS(block, NUM, B_MATPRV, "",                     879,30,37,19, &(tex->fradur[2][1]), 0.0, 250.0, 0, 0, "Montage mode: amount of displayed frames");
1880                         uiDefButS(block, NUM, B_MATPRV, "Fra:",         802,10,73,19, &(tex->fradur[3][0]), 0.0, 18000.0, 0, 0, "Montage mode: frame start");
1881                         uiDefButS(block, NUM, B_MATPRV, "",                     879,10,37,19, &(tex->fradur[3][1]), 0.0, 250.0, 0, 0, "Montage mode: amount of displayed frames");
1882         
1883                         uiBlockSetCol(block, BUTGREEN);
1884                         uiDefButS(block, TOG|BIT|6, 0, "Cyclic",                743,60,48,19, &tex->imaflag, 0, 0, 0, 0, "Repeat animation image");
1885                         
1886                         uiBlockSetCol(block, BUTSALMON);
1887                         uiDefBut(block, BUT, B_LOADTEXIMA, "Load Image", 350,137,132,24, 0, 0, 0, 0, 0, "Load image - thumbnail view");
1888                         uiBlockSetCol(block, BUTGREY);
1889                         uiDefBut(block, BUT, B_LOADTEXIMA1, "", 485,137,10,24, 0, 0, 0, 0, 0, "Load image - file view");
1890         
1891                         id= (ID *)tex->ima;
1892                         IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(G.buts->menunr));
1893                         if(strp[0])
1894                                 uiDefButS(block, MENU, B_TEXIMABROWSE, strp, 496,137,23,24, &(G.buts->menunr), 0, 0, 0, 0, "Browse");
1895                         MEM_freeN(strp);
1896         
1897                         if(tex->ima) {
1898                                 uiDefBut(block, TEX, B_NAMEIMA, "",                     520,137,412,24, tex->ima->name, 0.0, 79.0, 0, 0, "Texture name");
1899                                 sprintf(str, "%d", tex->ima->id.us);
1900                                 uiDefBut(block, BUT, 0, str,                                    934,137,23,24, 0, 0, 0, 0, 0, "Number of users");
1901                                 uiDefBut(block, BUT, B_RELOADIMA, "Reload",     986,137,68,24, 0, 0, 0, 0, 0, "Reload");
1902
1903                                 if (tex->ima->packedfile) {
1904                                         packdummy = 1;
1905                                 } else {
1906                                         packdummy = 0;
1907                                 }
1908                                 uiDefIconButI(block, TOG|BIT|0, B_PACKIMA, ICON_PACKAGE,        960,137,24,24, &packdummy, 0, 0, 0, 0, "Pack/Unpack this Image");
1909                         }
1910                         
1911                         uiBlockSetCol(block, BUTGREEN);
1912                         
1913                         uiDefButS(block, TOG|BIT|0, 0, "InterPol",                      350, 170, 75, 18, &tex->imaflag, 0, 0, 0, 0, "Interpolate pixels of the image");
1914                         uiDefButS(block, TOG|BIT|1, B_MATPRV, "UseAlpha",       425, 170, 75, 18, &tex->imaflag, 0, 0, 0, 0, "Use the alpha layer");
1915                         uiDefButS(block, TOG|BIT|5, B_MATPRV, "CalcAlpha",      500, 170, 75, 18, &tex->imaflag, 0, 0, 0, 0, "Calculate an alpha based on the RGB");
1916                         uiDefButS(block, TOG|BIT|2, B_MATPRV, "NegAlpha",       575, 170, 75, 18, &tex->flag, 0, 0, 0, 0, "Reverse the alpha value");
1917                         uiDefButS(block, TOG|BIT|2, B_IMAPTEST, "MipMap",       650, 170, 75, 18, &tex->imaflag, 0, 0, 0, 0, "Generate a series of pictures used for mipmapping");
1918                         uiDefButS(block, TOG|BIT|3, B_IMAPTEST, "Fields",       725, 170, 75, 18, &tex->imaflag, 0, 0, 0, 0, "Work with field images");
1919                         uiDefButS(block, TOG|BIT|4, B_MATPRV, "Rot90",          800, 170, 50, 18, &tex->imaflag, 0, 0, 0, 0, "Rotate image 90 degrees when rendered");
1920                         uiDefButS(block, TOG|BIT|7, B_RELOADIMA, "Movie",       850, 170, 50, 18, &tex->imaflag, 0, 0, 0, 0, "Use a movie for an image");
1921                         uiDefButS(block, TOG|BIT|8, 0, "Anti",                          900, 170, 50, 18, &tex->imaflag, 0, 0, 0, 0, "Use anti-aliasing");
1922                         uiDefButS(block, TOG|BIT|10, 0, "StField",                      950, 170, 50, 18, &tex->imaflag, 0, 0, 0, 0, "");
1923                         
1924                         uiBlockSetCol(block, BUTGREY);
1925         
1926                         /* print amount of frames anim */
1927                         if(tex->ima && tex->ima->anim) {
1928                                 uiDefBut(block, BUT, B_TEXSETFRAMES, "<",      802, 110, 20, 18, 0, 0, 0, 0, 0, "Paste number of frames in Frames: button");
1929                                 sprintf(str, "%d frs  ", IMB_anim_get_duration(tex->ima->anim));
1930                                 uiDefBut(block, LABEL, 0, str,      834, 110, 90, 18, 0, 0, 0, 0, 0, "");
1931                                 sprintf(str, "%d cur  ", tex->ima->lastframe);
1932                                 uiDefBut(block, LABEL, 0, str,      834, 90, 90, 18, 0, 0, 0, 0, 0, "");
1933                         }
1934                         
1935                         
1936                 }
1937                 else if(tex->type==TEX_PLUGIN) {
1938                         if(tex->plugin && tex->plugin->doit) {
1939                                 
1940                                 pit= tex->plugin;
1941         
1942                                 uiBlockSetCol(block, BUTGREEN);
1943                                 for(a=0; a<pit->stypes; a++) {
1944                                         uiDefButS(block, ROW, B_MATPRV, pit->stnames+16*a, (short)(350+75*a), 170, 75, 18, &tex->stype, 2.0, (float)a, 0, 0, "");
1945                                 }
1946                                 
1947                                 uiBlockSetCol(block, BUTGREY);
1948                                 varstr= pit->varstr;
1949                                 if(varstr) {
1950                                         for(a=0; a<pit->vars; a++, varstr++) {
1951                                                 xco= 350 + 140*(a/6);
1952                                                 yco= 110 - 20*(a % 6);
1953                                                 pit->data[a] = varstr->def;
1954                                                 uiDefBut(block, varstr->type, B_PLUGBUT+a, varstr->name, (short)xco,(short)yco,137,19, &(pit->data[a]), varstr->min, varstr->max, 100, 0, varstr->tip);
1955                                         }
1956                                 }
1957                                 uiDefBut(block, TEX, B_NAMEPLUGIN, "",                  520,137,412,24, pit->name, 0.0, 159.0, 0, 0, "Browse");
1958                         }
1959         
1960                         uiBlockSetCol(block, BUTSALMON);
1961                         uiDefBut(block, BUT, B_LOADPLUGIN, "Load Plugin", 350,137,137,24, 0, 0, 0, 0, 0, "");
1962                         
1963                 }
1964                 else if(tex->type==TEX_ENVMAP) {
1965                         
1966                         if(tex->env==0) {
1967                                 tex->env= RE_add_envmap();
1968                                 tex->env->object= OBACT;
1969                         }
1970                         if(tex->env) {
1971                                 env= tex->env;
1972                                 
1973                                 uiBlockSetCol(block, BUTGREEN);
1974                                 uiDefButS(block, ROW, B_REDR,   "Static", 350, 170, 75, 18, &env->stype, 2.0, 0.0, 0, 0, "Calculate map only once");
1975                                 uiDefButS(block, ROW, B_REDR,   "Anim", 425, 170, 75, 18, &env->stype, 2.0, 1.0, 0, 0, "Calculate map each rendering");
1976                                 uiDefButS(block, ROW, B_ENV_FREE, "Load", 500, 170, 75, 18, &env->stype, 2.0, 2.0, 0, 0, "Load map from disk");
1977                                 
1978                                 if(env->stype==ENV_LOAD) {
1979                                         uiBlockSetCol(block, BUTSALMON);
1980                                         uiDefBut(block, BUT, B_LOADTEXIMA, "Load Image", 350,137,132,24, 0, 0, 0, 0, 0, "Load image - thumbnail view");
1981                                         uiBlockSetCol(block, BUTGREY);
1982                                         uiDefBut(block, BUT, B_LOADTEXIMA1, "", 485,137,10,24, 0, 0, 0, 0, 0, "Load image - file view");
1983                                         
1984                                         id= (ID *)tex->ima;
1985                                         IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(G.buts->menunr));
1986                                         if(strp[0])
1987                                                 uiDefButS(block, MENU, B_TEXIMABROWSE, strp, 496,137,23,24, &(G.buts->menunr), 0, 0, 0, 0, "");
1988                                         MEM_freeN(strp);
1989         
1990                                         if(tex->ima) {
1991                                                 uiDefBut(block, TEX, B_NAMEIMA, "",                     520,137,412,24, tex->ima->name, 0.0, 79.0, 0, 0, "");
1992                                                 sprintf(str, "%d", tex->ima->id.us);
1993                                                 uiDefBut(block, BUT, 0, str,                                    934,137,23,24, 0, 0, 0, 0, 0, "");
1994                                                 if (tex->ima->packedfile) {
1995                                                         packdummy = 1;
1996                                                 } else {
1997                                                         packdummy = 0;
1998                                                 }
1999                                                 uiDefIconButI(block, TOG|BIT|0, B_PACKIMA, ICON_PACKAGE,        960,137,24,24, &packdummy, 0, 0, 0, 0, "Pack/Unpack this Image");
2000                                                 uiDefBut(block, BUT, B_RELOADIMA, "Reload",     986,137,68,24, 0, 0, 0, 0, 0, "");
2001                                         }
2002                                 }
2003                                 else {
2004                                         uiBlockSetCol(block, BUTSALMON);
2005                                         uiDefBut(block, BUT, B_ENV_FREE, "Free Data", 350,137,107,24, 0, 0, 0, 0, 0, "Release all images associated with environment map");
2006                                         uiBlockSetCol(block, BUTGREY);
2007                                         uiDefBut(block, BUT, B_ENV_SAVE, "Save EnvMap", 461,137,115,24, 0, 0, 0, 0, 0, "Save environment map");
2008                                         uiBlockSetCol(block, BUTSALMON);
2009                                         uiDefBut(block, BUT, B_ENV_FREE_ALL, "Free all EnvMaps", 600,137,160,24, 0, 0, 0, 0, 0, "Frees all rendered environment maps");
2010                                 }
2011                                 uiBlockSetCol(block, BUTGREY);
2012                                 uiDefIDPoinBut(block, test_obpoin_but, B_ENV_OB, "Ob:",   350,95,166,24, &(env->object), "Object name");
2013                                 uiDefButF(block, NUM, B_MATPRV, "Filter :",                               518,95,120,24, &tex->filtersize, 0.1, 25.0, 0, 0, "Adjust sharpness or blurriness of the reflection"),
2014                                 uiDefButS(block, NUM, B_ENV_FREE, "Depth:",                               640,95,120,24, &env->depth, 0, 5.0, 0, 0, "Number of times a map gets rendered again, for recursive mirror effect"),
2015
2016                                 uiDefButF(block, NUM, REDRAWVIEW3D,     "ClipSta",              350,68,122,24, &env->clipsta, 0.01, 50.0, 100, 0, "Set start value for clipping");
2017                                 uiDefButF(block, NUM, 0,        "ClipEnd",                                      475,68,142,24, &env->clipend, 0.1, 5000.0, 1000, 0, "Set end value for clipping");
2018                                 if(env->stype!=ENV_LOAD) uiDefButS(block, NUM, B_ENV_FREE,      "CubeRes", 620,68,140,24, &env->cuberes, 50, 2048.0, 0, 0, "Set the resolution in pixels");
2019         
2020                                 uiDefBut(block, LABEL, 0, "Don't render layer:",                772,100,140,22, 0, 0.0, 0.0, 0, 0, ""); 
2021                                 xco= 772;
2022                                 dx= 28;
2023                                 dy= 26;
2024                                 for(a=0; a<10; a++) {
2025                                         uiDefButI(block, TOG|BIT|(a+10), 0, "",(short)(xco+a*(dx/2)), 68, (short)(dx/2), (short)(dy/2), &env->notlay, 0, 0, 0, 0, "Render this layer");
2026                                         uiDefButI(block, TOG|BIT|a, 0, "",      (short)(xco+a*(dx/2)), (short)(68+dy/2), (short)(dx/2), (short)(1+dy/2), &env->notlay, 0, 0, 0, 0, "Render this layer");
2027                                         if(a==4) xco+= 5;
2028                                 }
2029         
2030                         }
2031                 }
2032         
2033                 /* COLORBAND */
2034                 uiBlockSetCol(block, BUTSALMON);
2035                 uiDefButS(block, TOG|BIT|0, B_COLORBAND, "Colorband",           923,103,102,20, &tex->flag, 0, 0, 0, 0, "Use colorband");
2036                 if(tex->flag & TEX_COLORBAND) {
2037                         uiDefBut(block, BUT, B_ADDCOLORBAND, "Add",                             1029,103,50,20, 0, 0, 0, 0, 0, "Add new colour to the colorband");
2038                         uiDefBut(block, BUT, B_DELCOLORBAND, "Del",                             1218,104,50,20, 0, 0, 0, 0, 0, "Delete the active colour");
2039                         uiBlockSetCol(block, BUTPURPLE);
2040                         uiDefButS(block, NUM, B_REDR,           "Cur:",                         1082,104,132,20, &tex->coba->cur, 0.0, (float)(tex->coba->tot-1), 0, 0, "The active colour from the colorband");
2041         
2042                         uiDefBut(block, LABEL, B_DOCOLORBAND, "", 923,81,345,20, 0, 0, 0, 0, 0, "Colorband"); /* only for event! */
2043                         
2044                         drawcolorband(tex->coba, 923,81,345,20);
2045                         cbd= tex->coba->data + tex->coba->cur;
2046                         
2047                         uiDefButF(block, NUM, B_CALCCBAND, "Pos",                       923,59,89,20, &cbd->pos, 0.0, 1.0, 10, 0, "Set the position of the active colour");
2048                         uiBlockSetCol(block, BUTGREEN);
2049                         uiDefButS(block, ROW, B_REDRAWCBAND, "E",               1013,59,20,20, &tex->coba->ipotype, 5.0, 1.0, 0, 0, "Interpolation type Ease");
2050                         uiDefButS(block, ROW, B_REDRAWCBAND, "L",               1033,59,20,20, &tex->coba->ipotype, 5.0, 0.0, 0, 0, "Interpolation type Linear");
2051                         uiDefButS(block, ROW, B_REDRAWCBAND, "S",               1053,59,20,20, &tex->coba->ipotype, 5.0, 2.0, 0, 0, "Interpolation type Spline");
2052                         uiBlockSetCol(block, BUTPURPLE);
2053                         uiDefButF(block, COL, B_BANDCOL, "",                                    1076,59,28,20, &(cbd->r), 0, 0, 0, 0, "");
2054                         uiDefButF(block, NUMSLI, B_REDRAWCBAND, "A ",                   1107,58,163,20, &cbd->a, 0.0, 1.0, 0, 0, "Set the alpha value");
2055                         
2056                         uiDefButF(block, NUMSLI, B_REDRAWCBAND, "R ",                   923,37,116,20, &cbd->r, 0.0, 1.0, B_BANDCOL, 0, "Set the red value");
2057                         uiDefButF(block, NUMSLI, B_REDRAWCBAND, "G ",                   1042,37,111,20, &cbd->g, 0.0, 1.0, B_BANDCOL, 0, "Set the green value");
2058                         uiDefButF(block, NUMSLI, B_REDRAWCBAND, "B ",                   1156,36,115,20, &cbd->b, 0.0, 1.0, B_BANDCOL, 0, "Set the blue value");
2059                         
2060                 }
2061         
2062         
2063                 /* RGB-BRICON */
2064                 uiBlockSetCol(block, BUTGREY);
2065                 uiDefButF(block, NUMSLI, B_MATPRV, "Bright",                    923,11,166,20, &tex->bright, 0.0, 2.0, 0, 0, "Set the brightness of the colour or intensity of a texture");
2066                 
2067                 uiDefButF(block, NUMSLI, B_MATPRV, "Contr",                     1093,11,180,20, &tex->contrast, 0.01, 2.0, 0, 0, "Set the contrast of the colour or intensity of a texture");
2068         
2069                 if((tex->flag & TEX_COLORBAND)==0) {
2070                         uiDefButF(block, NUMSLI, B_MATPRV, "R ",                        923,37,116,20, &tex->rfac, 0.0, 2.0, 0, 0, "Set the red value");
2071                         uiDefButF(block, NUMSLI, B_MATPRV, "G ",                        1042,37,111,20, &tex->gfac, 0.0, 2.0, 0, 0, "Set the green value");
2072                         uiDefButF(block, NUMSLI, B_MATPRV, "B ",                        1156,36,115,20, &tex->bfac, 0.0, 2.0, 0, 0, "Set the blue value");
2073                 }
2074         }
2075         
2076         /* PREVIEW RENDER */
2077         
2078         BIF_previewdraw(G.buts);
2079
2080         uiDrawBlock(block);
2081 }
2082
2083 /* ****************************** MATERIAL ************************ */
2084 MTex mtexcopybuf;
2085
2086
2087 void do_matbuts(unsigned short event)
2088 {
2089         static short mtexcopied=0;
2090         Material *ma;
2091         MTex *mtex;
2092
2093         switch(event) {         
2094         case B_ACTCOL:
2095                 scrarea_queue_headredraw(curarea);
2096                 allqueue(REDRAWBUTSMAT, 0);
2097                 allqueue(REDRAWIPO, 0);
2098                 BIF_preview_changed(G.buts);
2099                 break;
2100         case B_MATFROM:
2101
2102                 scrarea_queue_headredraw(curarea);
2103                 allqueue(REDRAWBUTSMAT, 0);
2104                 BIF_previewdraw(G.buts);
2105                 break;
2106         case B_MATPRV:
2107                 /* this event also used by lamp, tex and sky */
2108                 BIF_preview_changed(G.buts);
2109                 break;
2110         case B_MATPRV_DRAW:
2111                 BIF_preview_changed(G.buts);
2112                 allqueue(REDRAWBUTSMAT, 0);
2113                 break;
2114         case B_TEXCLEAR:
2115                 ma= G.buts->lockpoin;
2116                 mtex= ma->mtex[(int) ma->texact ];
2117                 if(mtex) {
2118                         if(mtex->tex) mtex->tex->id.us--;
2119                         MEM_freeN(mtex);
2120                         ma->mtex[ (int) ma->texact ]= 0;
2121                         allqueue(REDRAWBUTSMAT, 0);
2122                         allqueue(REDRAWOOPS, 0);
2123                         BIF_preview_changed(G.buts);
2124                 }
2125                 break;
2126         case B_MTEXCOPY:
2127                 ma= G.buts->lockpoin;
2128                 if(ma && ma->mtex[(int)ma->texact] ) {
2129                         mtex= ma->mtex[(int)ma->texact];
2130                         if(mtex->tex==0) {
2131                                 error("No texture available");
2132                         }
2133                         else {
2134                                 memcpy(&mtexcopybuf, ma->mtex[(int)ma->texact], sizeof(MTex));
2135                                 notice("copied!");
2136                                 mtexcopied= 1;
2137                         }
2138                 }
2139                 break;
2140         case B_MTEXPASTE:
2141                 ma= G.buts->lockpoin;
2142                 if(ma && mtexcopied && mtexcopybuf.tex) {
2143                         if(ma->mtex[(int)ma->texact]==0 ) ma->mtex[(int)ma->texact]= MEM_mallocN(sizeof(MTex), "mtex"); 
2144                         memcpy(ma->mtex[(int)ma->texact], &mtexcopybuf, sizeof(MTex));
2145                         
2146                         id_us_plus((ID *)mtexcopybuf.tex);
2147                         notice("pasted!");
2148                         BIF_preview_changed(G.buts);
2149                         scrarea_queue_winredraw(curarea);
2150                 }
2151                 break;
2152         case B_MATLAY:
2153                 ma= G.buts->lockpoin;
2154                 if(ma && ma->lay==0) {
2155                         ma->lay= 1;
2156                         scrarea_queue_winredraw(curarea);
2157                 }
2158         }
2159 }
2160
2161 void matbuts(void)
2162 {
2163         Object *ob;
2164         Material *ma;
2165         ID *id, *idn;
2166         MTex *mtex;
2167         uiBlock *block;
2168         uiBut *but;
2169         float *colpoin = NULL, min;
2170         int rgbsel = 0, a, loos;
2171         char str[30], *strp;
2172         short xco;
2173         
2174         ob= OBACT;
2175         if(ob==0 || ob->data==0) return;
2176
2177         sprintf(str, "buttonswin %d", curarea->win);
2178         block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
2179
2180         if(ob->actcol==0) ob->actcol= 1;        /* because of TOG|BIT button */
2181         
2182         /* indicate which one is linking a material */
2183         uiBlockSetCol(block, BUTSALMON);
2184         uiDefButS(block, TOG|BIT|(ob->actcol-1), B_MATFROM, "OB",       342, 195, 33, 20, &ob->colbits, 0, 0, 0, 0, "Link material to object");
2185         idn= ob->data;
2186         strncpy(str, idn->name, 2);
2187         str[2]= 0;
2188         uiBlockSetCol(block, BUTGREEN);
2189         uiDefButS(block, TOGN|BIT|(ob->actcol-1), B_MATFROM, str,               380, 195, 33, 20, &ob->colbits, 0, 0, 0, 0, "Show the block the material is linked to");
2190         uiBlockSetCol(block, BUTGREY);
2191         
2192         /* id is the block from which the material is used */
2193         if( BTST(ob->colbits, ob->actcol-1) ) id= (ID *)ob;
2194         else id= ob->data;
2195
2196         sprintf(str, "%d Mat", ob->totcol);
2197         if(ob->totcol) min= 1.0; else min= 0.0;
2198         uiDefButC(block, NUM, B_ACTCOL, str,    415,195,150,20, &(ob->actcol), min, (float)ob->totcol, 0, 0, "Number of materials on object / Active material");
2199         
2200         uiSetButLock(id->lib!=0, "Can't edit library data");
2201         
2202         strncpy(str, id->name, 2);
2203         str[2]= ':'; str[3]= 0;
2204         but= uiDefBut(block, TEX, B_IDNAME, str,                200,195,140,20, id->name+2, 0.0, 18.0, 0, 0, "Show the block the material is linked to");
2205         uiButSetFunc(but, test_idbutton_cb, id->name, NULL);
2206
2207         if(ob->totcol==0) {
2208                 uiDrawBlock(block);
2209                 return;
2210         }
2211         
2212         ma= give_current_material(ob, ob->actcol);
2213         
2214         if(ma==0) {
2215                 uiDrawBlock(block);
2216                 return;
2217         }
2218         uiSetButLock(ma->id.lib!=0, "Can't edit library data");
2219         
2220         uiBlockSetCol(block, BUTGREY);
2221         uiDefButS(block, ROW, REDRAWBUTSMAT, "RGB",                     200,166,35,22, &(ma->colormodel), 1.0, (float)MA_RGB, 0, 0, "Create colour by red, green and blue");
2222         uiDefButS(block, ROW, REDRAWBUTSMAT, "HSV",                     200,143,35,22, &(ma->colormodel), 1.0, (float)MA_HSV, 0, 0, "Mix colour with hue, saturation and value");
2223         uiDefButS(block, TOG|BIT|0, REDRAWBUTSMAT, "DYN",       200,120,35,22, &(ma->dynamode), 0.0, 0.0, 0, 0, "Adjust parameters for dynamics options");
2224         
2225         if(ma->dynamode & MA_DRAW_DYNABUTS) {
2226                 uiDefButF(block, NUMSLI, 0, "Restitut ",                390,168,175,21, &ma->reflect, 0.0, 1.0, 0, 0, "Elasticity of collisions");
2227                 uiDefButF(block, NUMSLI, 0, "Friction ",                390,144,175,21, &ma->friction, 0.0, 100.0, 0, 0,   "Coulomb friction coefficient");
2228
2229                 uiDefButF(block, NUMSLI, 0, "Fh Force ",                390,120,175,21, &ma->fh, 0.0, 1.0, 0, 0, "Upward spring force within the Fh area");
2230                 uiDefButF(block, NUM, 0,         "Fh Damp ",            260,144,120,21, &ma->xyfrict, 0.0, 1.0, 10, 0, "Damping of the Fh spring force");
2231                 uiDefButF(block, NUM, 0, "Fh Dist ",                    260,120,120,21, &ma->fhdist, 0.0, 20.0, 10, 0, "Height of the Fh area");
2232                 uiBlockSetCol(block, BUTGREEN);
2233                 uiDefButS(block, TOG|BIT|1, 0, "Fh Norm",               260,168,120,21, &ma->dynamode, 0.0, 0.0, 0, 0, "Add a horizontal spring force on slopes");
2234                 uiBlockSetCol(block, BUTGREY);
2235         }
2236         else {
2237                 uiDefButF(block, COL, B_MIRCOL, "",             235,143,30,45, &(ma->mirr), 0, 0, 0, 0, "");
2238                 uiDefButF(block, COL, B_SPECCOL, "",    265,143,39,45, &(ma->specr), 0, 0, 0, 0, "");
2239                 uiDefButF(block, COL, B_MATCOL, "",             304,143,39,45, &(ma->r), 0, 0, 0, 0, "");
2240         
2241                 if(ma->mode & MA_HALO) {
2242                         uiDefButC(block, ROW, REDRAWBUTSMAT, "Ring",            235,120,30,22, &(ma->rgbsel), 2.0, 2.0, 0, 0, "Mix the colour of the rings with the RGB sliders");
2243                         uiDefButC(block, ROW, REDRAWBUTSMAT, "Line",            265,120,39,22, &(ma->rgbsel), 2.0, 1.0, 0, 0, "Mix the colour of the lines with the RGB sliders");
2244                         uiDefButC(block, ROW, REDRAWBUTSMAT, "Halo",            304,120,39,22, &(ma->rgbsel), 2.0, 0.0, 0, 0, "Mix the colour of the halo with the RGB sliders");
2245                 }
2246                 else {
2247                         uiDefButC(block, ROW, REDRAWBUTSMAT, "Mir",                     235,120,30,22, &(ma->rgbsel), 2.0, 2.0, 0, 0, "Use mirror colour");
2248                         uiDefButC(block, ROW, REDRAWBUTSMAT, "Spe",                     265,120,39,22, &(ma->rgbsel), 2.0, 1.0, 0, 0, "Set the colour of the specularity");
2249                         uiDefButC(block, ROW, REDRAWBUTSMAT, "Col",                     304,120,39,22, &(ma->rgbsel), 2.0, 0.0, 0, 0, "Set the basic colour of the material");
2250                 }
2251                 if(ma->rgbsel==0) {colpoin= &(ma->r); rgbsel= B_MATCOL;}
2252                 else if(ma->rgbsel==1) {colpoin= &(ma->specr); rgbsel= B_SPECCOL;}
2253                 else if(ma->rgbsel==2) {colpoin= &(ma->mirr); rgbsel= B_MIRCOL;}
2254                 
2255                 if(ma->rgbsel==0 && (ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE) && !(ma->mode & MA_HALO)));
2256                 else if(ma->colormodel==MA_HSV) {
2257                         uiBlockSetCol(block, BUTPURPLE);
2258                         uiDefButF(block, HSVSLI, B_MATPRV, "H ",                        350,168,150,21, colpoin, 0.0, 0.9999, rgbsel, 0, "");
2259                         uiBlockSetCol(block, BUTPURPLE);
2260                         uiDefButF(block, HSVSLI, B_MATPRV, "S ",                        350,144,150,21, colpoin, 0.0001, 1.0, rgbsel, 0, "");
2261                         uiBlockSetCol(block, BUTPURPLE);
2262                         uiDefButF(block, HSVSLI, B_MATPRV, "V ",                        350,120,150,21, colpoin, 0.0001, 1.0, rgbsel, 0, "");
2263                         uiBlockSetCol(block, BUTGREY);
2264                 }
2265                 else {
2266                         uiDefButF(block, NUMSLI, B_MATPRV, "R ",                        350,168,150,21, colpoin, 0.0, 1.0, rgbsel, 0, "");
2267                         uiDefButF(block, NUMSLI, B_MATPRV, "G ",                        350,144,150,21, colpoin+1, 0.0, 1.0, rgbsel, 0, "");
2268                         uiDefButF(block, NUMSLI, B_MATPRV, "B ",                        350,120,150,21, colpoin+2, 0.0, 1.0, rgbsel, 0, "");
2269                 }
2270                 if(!(ma->mode & MA_HALO)) {
2271                         uiBlockSetCol(block, BUTBLUE);
2272                         uiDefButI(block, TOG|BIT|4, B_REDR,     "VCol Light",           505, 168, 60, 21, &(ma->mode), 0, 0, 0, 0, "Add vertex colours as extra light");
2273                         uiDefButI(block, TOG|BIT|7, B_REDR, "VCol Paint",               505, 144, 60, 21, &(ma->mode), 0, 0, 0, 0, "Replace basic colours with vertex colours");
2274                         uiDefButI(block, TOG|BIT|11, B_REDR, "TexFace",                 505, 120, 60, 21, &(ma->mode), 0, 0, 0, 0, "UV-Editor assigned texture gives color and texture info for the faces");
2275                 }
2276         }
2277         if(ma->mode & MA_HALO) {
2278                 uiBlockSetCol(block, BUTGREY);
2279                 uiDefButF(block, NUM, B_MATPRV, "HaloSize: ",           200,90,175,18, &(ma->hasize), 0.0, 100.0, 10, 0, "Set the dimension of the halo");
2280                 uiDefButF(block, NUMSLI, B_MATPRV, "Alpha ",            200,50,175,18, &(ma->alpha), 0.0, 1.0, 0, 0, "Set the degree of coverage");
2281                 uiDefButS(block, NUMSLI, B_MATPRV, "Hard ",                     200,30,175,18, &(ma->har), 1.0, 127.0, 0, 0, "Set the hardness of the halo");
2282                 uiDefButF(block, NUMSLI, B_MATPRV, "Add  ",                     200,10,175,18, &(ma->add), 0.0, 1.0, 0, 0, "Strength of the add effect");
2283                 
2284                 uiDefButS(block, NUM, B_MATPRV, "Rings: ",                      380,90,85,18, &(ma->ringc), 0.0, 24.0, 0, 0, "Set the number of rings rendered over the basic halo");
2285                 uiDefButS(block, NUM, B_MATPRV, "Lines: ",                      465,90,90,18, &(ma->linec), 0.0, 250.0, 0, 0, "Set the number of star shaped lines rendered over the halo");
2286                 uiDefButS(block, NUM, B_MATPRV, "Star: ",                       380,70,85,18, &(ma->starc), 3.0, 50.0, 0, 0, "Set the number of points on the star shaped halo");
2287                 uiDefButC(block, NUM, B_MATPRV, "Seed: ",                       465,70,90,18, &(ma->seed1), 0.0, 255.0, 0, 0, "Use random values for ring dimension and line location");
2288                 
2289                 uiDefButF(block, NUM, B_MATPRV, "FlareSize: ",          380,50,85,18, &(ma->flaresize), 0.1, 25.0, 10, 0, "Set the factor the flare is larger than the halo");
2290                 uiDefButF(block, NUM, B_MATPRV, "Sub Size: ",           465,50,90,18, &(ma->subsize), 0.1, 25.0, 10, 0, "Set the dimension of the subflares, dots and circles");
2291                 uiDefButF(block, NUM, B_MATPRV, "FlareBoost: ",         380,30,175,18, &(ma->flareboost), 0.1, 10.0, 10, 0, "Give the flare extra strength");
2292                 uiDefButC(block, NUM, B_MATPRV, "Fl.seed: ",            380,10,85,18, &(ma->seed2), 0.0, 255.0, 0, 0, "Specify an offset in the seed table");
2293                 uiDefButS(block, NUM, B_MATPRV, "Flares: ",                     465,10,90,18, &(ma->flarec), 1.0, 32.0, 0, 0, "Set the nuber of subflares");
2294
2295                 uiBlockSetCol(block, BUTBLUE);
2296                 
2297                 uiDefButI(block, TOG|BIT|15, B_MATPRV, "Flare",         576, 181, 77, 36, &(ma->mode), 0, 0, 0, 0, "Render halo as a lensflare");
2298                 uiDefButI(block, TOG|BIT|8, B_MATPRV, "Rings",          576, 143, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render rings over basic halo");
2299                 uiDefButI(block, TOG|BIT|9, B_MATPRV, "Lines",          576, 124, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render star shaped lines over the basic halo");
2300                 uiDefButI(block, TOG|BIT|11, B_MATPRV, "Star",          576, 105, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render halo as a star");
2301                 uiDefButI(block, TOG|BIT|5, B_MATPRV_DRAW, "Halo",      576, 86, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render as a halo");
2302                 
2303                 uiDefButI(block, TOG|BIT|12, B_MATPRV, "HaloTex",       576, 67, 77, 18, &(ma->mode), 0, 0, 0, 0, "Give halo a texture");
2304                 uiDefButI(block, TOG|BIT|13, B_MATPRV, "HaloPuno",      576, 48, 77, 18, &(ma->mode), 0, 0, 0, 0, "Use the vertex normal to specify the dimension of the halo");
2305                 uiDefButI(block, TOG|BIT|10, B_MATPRV, "X Alpha",       576, 28, 77, 18, &(ma->mode), 0, 0, 0, 0, "Use extreme alpha");
2306                 uiDefButI(block, TOG|BIT|14, B_MATPRV, "Shaded",        576, 10, 77, 18, &(ma->mode), 0, 0, 0, 0, "Let halo receive light");
2307         }
2308         else {
2309                 char *str1= "Diffuse Shader%t|Lambert %x0|Oren-Nayar %x1|Toon %x2";
2310                 char *str2= "Specular Shader%t|CookTorr %x0|Phong %x1|Blinn %x2|Toon %x3";
2311                 
2312                 /* shader buttons */
2313                 uiBlockSetCol(block, BUTGREY);
2314                 uiDefButS(block, MENU, B_MATPRV_DRAW, str1,             200,90,95,18, &(ma->diff_shader), 0.0, 0.0, 0, 0, "Set a diffuse shader");
2315                 uiDefButS(block, MENU, B_MATPRV_DRAW, str2,             295,90,90,18, &(ma->spec_shader), 0.0, 0.0, 0, 0, "Set a specular shader");
2316
2317                 if(ma->diff_shader==MA_DIFF_ORENNAYAR)
2318                         uiDefButF(block, NUM, B_MATPRV, "Rough:",               200, 70, 95,18, &(ma->roughness), 0.0, 3.14, 0, 0, "Oren Nayar Roughness");
2319                 else if(ma->diff_shader==MA_DIFF_TOON) {
2320                         uiDefButF(block, NUM, B_MATPRV, "Size:",                200, 70, 95,18, &(ma->param[0]), 0.0, 3.14, 0, 0, "Size of diffuse toon area");
2321                         uiDefButF(block, NUM, B_MATPRV, "Smooth:",              200, 50, 95,18, &(ma->param[1]), 0.0, 1.0, 0, 0, "Smoothness of diffuse toon area");
2322                 }
2323                 
2324                 if ELEM3(ma->spec_shader, MA_SPEC_COOKTORR, MA_SPEC_PHONG, MA_SPEC_BLINN) {
2325                         uiDefButS(block, NUM, B_MATPRV, "Hard:",                295, 70, 90,18, &(ma->har), 1.0, 255, 0, 0, "Set the hardness of the specularity");
2326                 }
2327                 if(ma->spec_shader==MA_SPEC_BLINN)
2328                         uiDefButF(block, NUM, B_MATPRV, "Refr:",                295, 50, 90,18, &(ma->refrac), 1.0, 10.0, 0, 0, "Refraction index");
2329                 if(ma->spec_shader==MA_SPEC_TOON) {
2330                         uiDefButF(block, NUM, B_MATPRV, "Size:",                295, 70, 90,18, &(ma->param[2]), 0.0, 1.53, 0, 0, "Size of specular toon area");
2331                         uiDefButF(block, NUM, B_MATPRV, "Smooth:",              295, 50, 90,18, &(ma->param[3]), 0.0, 1.0, 0, 0, "Smoothness of specular toon area");
2332                 }
2333
2334                 uiDefButF(block, NUMSLI, B_MATPRV, "Ref   ",            200,30,185,18, &(ma->ref), 0.0, 1.0, 0, 0, "Set the amount of reflection");
2335                 uiDefButF(block, NUMSLI, B_MATPRV, "Spec ",                     200,10,185,18, &(ma->spec), 0.0, 2.0, 0, 0, "Set the degree of specularity");
2336         
2337                 /* default shading variables */
2338                 uiDefButF(block, NUMSLI, B_MATPRV, "Alpha ",            390,90,175,18, &(ma->alpha), 0.0, 1.0, 0, 0, "Set the amount of coverage, to make materials transparent");
2339                 uiDefButF(block, NUMSLI, B_MATPRV, "SpecTra ",          390,70,175,18, &(ma->spectra), 0.0, 1.0, 0, 0, "Make specular areas opaque");
2340                 uiDefButF(block, NUMSLI, B_MATPRV, "Add  ",                     390,50,175,18, &(ma->add), 0.0, 1.0, 0, 0, "Glow factor for transparant");
2341                 uiDefButF(block, NUMSLI, B_MATPRV, "Emit  ",            390,30,175,18, &(ma->emit), 0.0, 1.0, 0, 0, "Set the amount of emitting light");
2342                 uiDefButF(block, NUMSLI, B_MATPRV, "Amb   ",            390,10,175,18, &(ma->amb), 0.0, 1.0, 0, 0, "Set the amount of global ambient color");
2343         
2344                 uiBlockSetCol(block, BUTBLUE);
2345         
2346                 uiDefButI(block, TOG|BIT|0, 0,  "Traceable",            576,200,77,18, &(ma->mode), 0, 0, 0, 0, "Make material visible for shadow lamps");
2347                 uiDefButI(block, TOG|BIT|1, 0,  "Shadow",                       576,181,77,18, &(ma->mode), 0, 0, 0, 0, "Enable material for shadows");
2348                 uiDefButI(block, TOG|BIT|16, 0, "Radio",                        576, 162, 77,18, &(ma->mode), 0, 0, 0, 0, "Set the material insensitive to mist");
2349                 uiDefButI(block, TOG|BIT|2, B_MATPRV, "Shadeless",      576, 143, 77, 18, &(ma->mode), 0, 0, 0, 0, "Make material insensitive to light or shadow");
2350                 uiDefButI(block, TOG|BIT|3, 0,  "Wire",                         576, 124, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render only the edges of faces");
2351                 uiDefButI(block, TOG|BIT|6, 0,  "ZTransp",                      576, 105, 77, 18, &(ma->mode), 0, 0, 0, 0, "Z-Buffer transparent faces");
2352                 uiDefButI(block, TOG|BIT|5, B_MATPRV_DRAW, "Halo",      576, 86, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render as a halo");
2353                 uiDefButI(block, TOG|BIT|9, 0,  "Env",                          576, 67, 77, 18, &(ma->mode), 0, 0, 0, 0, "Do not render material");
2354                 uiDefButI(block, TOG|BIT|10, 0, "OnlyShadow",           576, 48, 77, 18, &(ma->mode), 0, 0, 0, 0, "Let alpha be determined on the degree of shadow");
2355                 uiDefButI(block, TOG|BIT|14, 0, "No Mist",                      576, 29, 77,18, &(ma->mode), 0, 0, 0, 0, "Set the material insensitive to mist");
2356                 uiDefButI(block, TOG|BIT|8, 0,  "ZInvert",                      576, 10, 77, 18, &(ma->mode), 0, 0, 0, 0, "Render with inverted Z Buffer");
2357                 uiBlockSetCol(block, BUTGREY);
2358                 uiDefButF(block, NUM, 0, "Zoffs:",                                      576, -9, 77,18, &(ma->zoffs), 0.0, 10.0, 0, 0, "Give face an artificial offset");
2359         }
2360         /* PREVIEW RENDER */
2361         
2362         BIF_previewdraw(G.buts);
2363
2364         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATPLANE,              10,195,25,20, &(ma->pr_type), 10, 0, 0, 0, "");
2365         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATSPHERE,             35,195,25,20, &(ma->pr_type), 10, 1, 0, 0, "");
2366         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATCUBE,               60,195,25,20, &(ma->pr_type), 10, 2, 0, 0, "");
2367
2368         uiDefIconButS(block, ICONTOG|BIT|0, B_MATPRV, ICON_TRANSP_HLT,          95,195,25,20, &(ma->pr_back), 0, 0, 0, 0, "");
2369         
2370         uiDefIconBut(block, BUT, B_MATPRV, ICON_EYE,            159,195,30,20, 0, 0, 0, 0, 0, "");
2371
2372         /* TEX CHANNELS */
2373         uiBlockSetCol(block, BUTGREY);
2374         xco= 665;
2375         for(a= 0; a<8; a++) {
2376                 mtex= ma->mtex[a];
2377                 if(mtex && mtex->tex) splitIDname(mtex->tex->id.name+2, str, &loos);
2378                 else strcpy(str, "");
2379                 str[10]= 0;
2380                 uiDefButC(block, ROW, B_MATPRV_DRAW, str,       xco, 195, 63, 20, &(ma->texact), 3.0, (float)a, 0, 0, "");
2381                 xco+= 65;
2382         }
2383         
2384         uiDefIconBut(block, BUT, B_MTEXCOPY, ICON_COPYUP,       (short)xco,195,20,21, 0, 0, 0, 0, 0, "Copy the material settings to the buffer");
2385         uiDefIconBut(block, BUT, B_MTEXPASTE, ICON_PASTEUP,     (short)(xco+20),195,20,21, 0, 0, 0, 0, 0, "Paste the material settings from the buffer");
2386
2387         
2388         uiBlockSetCol(block, BUTGREEN);
2389         uiDefButC(block, TOG, B_MATPRV, "SepT", (short)(xco+40), 195, 40, 20, &(ma->septex), 0, 0, 0, 0, "Render only use active texture channel");
2390         uiBlockSetCol(block, BUTGREY);
2391
2392         mtex= ma->mtex[ ma->texact ];
2393         if(mtex==0) {
2394                 mtex= &emptytex;
2395                 default_mtex(mtex);
2396         }
2397         
2398         /* TEXCO */
2399         uiBlockSetCol(block, BUTGREEN);
2400         uiDefButS(block, ROW, B_MATPRV, "Object",               694,166,49,18, &(mtex->texco), 4.0, (float)TEXCO_OBJECT, 0, 0, "Use linked object's coordinates for texture coordinates");
2401         uiDefIDPoinBut(block, test_obpoin_but, B_MATPRV, "",            745,166,133,18, &(mtex->object), "");
2402         uiDefButS(block, ROW, B_MATPRV, "UV",                   664,166,29,18, &(mtex->texco), 4.0, (float)TEXCO_UV, 0, 0, "Use UV coordinates for texture coordinates");
2403
2404         uiDefButS(block, ROW, B_MATPRV, "Glob",                 665,146,35,18, &(mtex->texco), 4.0, (float)TEXCO_GLOB, 0, 0, "Use global coordinates for the texture coordinates");
2405         uiDefButS(block, ROW, B_MATPRV, "Orco",                 701,146,38,18, &(mtex->texco), 4.0, (float)TEXCO_ORCO, 0, 0, "Use the original coordinates of the mesh");
2406         uiDefButS(block, ROW, B_MATPRV, "Stick",                        739,146,38,18, &(mtex->texco), 4.0, (float)TEXCO_STICKY, 0, 0, "Use mesh sticky coordaintes for the texture coordinates");
2407         uiDefButS(block, ROW, B_MATPRV, "Win",                  779,146,31,18, &(mtex->texco), 4.0, (float)TEXCO_WINDOW, 0, 0, "Use screen coordinates as texture coordinates");
2408         uiDefButS(block, ROW, B_MATPRV, "Nor",                  811,146,32,18, &(mtex->texco), 4.0, (float)TEXCO_NORM, 0, 0, "Use normal vector as texture coordinates");
2409         uiDefButS(block, ROW, B_MATPRV, "Refl",                 844,146,33,18, &(mtex->texco), 4.0, (float)TEXCO_REFL, 0, 0, "Use reflection vector as texture coordinates");
2410         
2411         uiBlockSetCol(block, BUTGREY);
2412         
2413         /* COORDS */
2414         uiDefButC(block, ROW, B_MATPRV, "Flat",                 666,114,48,18, &(mtex->mapping), 5.0, (float)MTEX_FLAT, 0, 0, "Map X and Y coordinates directly");
2415         uiDefButC(block, ROW, B_MATPRV, "Cube",                 717,114,50,18, &(mtex->mapping), 5.0, (float)MTEX_CUBE, 0, 0, "Map using the normal vector");
2416         uiDefButC(block, ROW, B_MATPRV, "Tube",                 666,94,48,18, &(mtex->mapping), 5.0, (float)MTEX_TUBE, 0, 0, "Map with Z as central axis (tube-like)");
2417         uiDefButC(block, ROW, B_MATPRV, "Sphe",                 716,94,50,18, &(mtex->mapping), 5.0, (float)MTEX_SPHERE, 0, 0, "Map with Z as central axis (sphere-like)");
2418
2419         xco= 665;
2420         for(a=0; a<4; a++) {
2421                 if(a==0) strcpy(str, "");
2422                 else if(a==1) strcpy(str, "X");
2423                 else if(a==2) strcpy(str, "Y");
2424                 else strcpy(str, "Z");
2425                 
2426                 uiDefButC(block, ROW, B_MATPRV, str,                    (short)xco, 50, 24, 18, &(mtex->projx), 6.0, (float)a, 0, 0, "");
2427                 uiDefButC(block, ROW, B_MATPRV, str,                    (short)xco, 30, 24, 18, &(mtex->projy), 7.0, (float)a, 0, 0, "");
2428                 uiDefButC(block, ROW, B_MATPRV, str,                    (short)xco, 10, 24, 18, &(mtex->projz), 8.0, (float)a, 0, 0, "");
2429                 xco+= 26;
2430         }
2431         
2432         uiDefButF(block, NUM, B_MATPRV, "ofsX",         778,114,100,18, mtex->ofs, -10.0, 10.0, 10, 0, "Fine tune X coordinate");
2433         uiDefButF(block, NUM, B_MATPRV, "ofsY",         778,94,100,18, mtex->ofs+1, -10.0, 10.0, 10, 0, "Fine tune Y coordinate");
2434         uiDefButF(block, NUM, B_MATPRV, "ofsZ",         778,74,100,18, mtex->ofs+2, -10.0, 10.0, 10, 0, "Fine tune Z coordinate");
2435         uiDefButF(block, NUM, B_MATPRV, "sizeX",        778,50,100,18, mtex->size, -100.0, 100.0, 10, 0, "Set an extra scaling for the texture coordinate");
2436         uiDefButF(block, NUM, B_MATPRV, "sizeY",        778,30,100,18, mtex->size+1, -100.0, 100.0, 10, 0, "Set an extra scaling for the texture coordinate");
2437         uiDefButF(block, NUM, B_MATPRV, "sizeZ",        778,10,100,18, mtex->size+2, -100.0, 100.0, 10, 0, "Set an extra scaling for the texture coordinate");
2438         
2439         /* TEXTUREBLOK SELECT */
2440         if(G.main->tex.first==0)
2441                 id= NULL;
2442         else
2443                 id= (ID*) mtex->tex;
2444         IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->tex), id, &(G.buts->texnr));
2445         uiDefButS(block, MENU, B_EXTEXBROWSE, strp, 900,146,20,19, &(G.buts->texnr), 0, 0, 0, 0, "The name of the texture");
2446         MEM_freeN(strp);
2447
2448         if(id) {
2449                 uiDefBut(block, TEX, B_IDNAME, "TE:",   900,166,163,19, id->name+2, 0.0, 18.0, 0, 0, "The name of the texture block");
2450                 sprintf(str, "%d", id->us);
2451                 uiDefBut(block, BUT, 0, str,                            996,146,21,19, 0, 0, 0, 0, 0, "");
2452                 uiDefIconBut(block, BUT, B_AUTOTEXNAME, ICON_AUTO, 1041,146,21,19, 0, 0, 0, 0, 0, "Auto-assign name to texture");
2453                 if(id->lib) {
2454                         if(ma->id.lib) uiDefIconBut(block, BUT, 0, ICON_DATALIB,        1019,146,21,19, 0, 0, 0, 0, 0, "");
2455                         else uiDefIconBut(block, BUT, 0, ICON_PARLIB,   1019,146,21,19, 0, 0, 0, 0, 0, "");             
2456                 }
2457                 uiBlockSetCol(block, BUTSALMON);
2458                 uiDefBut(block, BUT, B_TEXCLEAR, "Clear", 922, 146, 72, 19, 0, 0, 0, 0, 0, "Erase link to datablock");
2459                 uiBlockSetCol(block, BUTGREY);
2460         }
2461         
2462         /* TEXTURE OUTPUT */
2463         uiDefButS(block, TOG|BIT|1, B_MATPRV, "Stencil",        900,114,52,18, &(mtex->texflag), 0, 0, 0, 0, "Set the mapping to stencil mode");
2464         uiDefButS(block, TOG|BIT|2, B_MATPRV, "Neg",            954,114,38,18, &(mtex->texflag), 0, 0, 0, 0, "Reverse the effect of the texture");
2465         uiDefButS(block, TOG|BIT|0, B_MATPRV, "No RGB", 994,114,69,18, &(mtex->texflag), 0, 0, 0, 0, "Use an RGB texture as an intensity texture");
2466         
2467         uiDefButF(block, COL, B_MTEXCOL, "",                            900,100,163,12, &(mtex->r), 0, 0, 0, 0, "Browse datablocks");
2468
2469         if(ma->colormodel==MA_HSV) {
2470                 uiBlockSetCol(block, BUTPURPLE);
2471                 uiDefButF(block, HSVSLI, B_MATPRV, "H ",                        900,80,163,18, &(mtex->r), 0.0, 0.9999, B_MTEXCOL, 0, "");
2472                 uiBlockSetCol(block, BUTPURPLE);
2473                 uiDefButF(block, HSVSLI, B_MATPRV, "S ",                        900,60,163,18, &(mtex->r), 0.0001, 1.0, B_MTEXCOL, 0, "");
2474                 uiBlockSetCol(block, BUTPURPLE);
2475                 uiDefButF(block, HSVSLI, B_MATPRV, "V ",                        900,40,163,18, &(mtex->r), 0.0001, 1.0, B_MTEXCOL, 0, "");
2476                 uiBlockSetCol(block, BUTGREY);
2477         }
2478         else {
2479                 uiDefButF(block, NUMSLI, B_MATPRV, "R ",                        900,80,163,18, &(mtex->r), 0.0, 1.0, B_MTEXCOL, 0, "Set the amount of red the intensity texture blends with");
2480                 uiDefButF(block, NUMSLI, B_MATPRV, "G ",                        900,60,163,18, &(mtex->g), 0.0, 1.0, B_MTEXCOL, 0, "Set the amount of green the intensity texture blends with");
2481                 uiDefButF(block, NUMSLI, B_MATPRV, "B ",                        900,40,163,18, &(mtex->b), 0.0, 1.0, B_MTEXCOL, 0, "Set the amount of blue the intensity texture blends with");
2482         }
2483         
2484         uiDefButF(block, NUMSLI, B_MATPRV, "DVar ",             900,10,163,18, &(mtex->def_var), 0.0, 1.0, 0, 0, "Set the value the texture blends with the current value");
2485         
2486         /* MAP TO */
2487         uiBlockSetCol(block, BUTGREEN);
2488         uiDefButS(block, TOG|BIT|0, B_MATPRV, "Col",            1087,166,35,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect basic colour of the material");
2489         uiDefButS(block, TOG3|BIT|1, B_MATPRV, "Nor",           1126,166,31,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the rendered normal");
2490         uiDefButS(block, TOG|BIT|2, B_MATPRV, "Csp",            1160,166,34,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the specularity colour");
2491         uiDefButS(block, TOG|BIT|3, B_MATPRV, "Cmir",           1196,166,35,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affext the mirror colour");
2492         uiDefButS(block, TOG3|BIT|4, B_MATPRV, "Ref",           1234,166,31,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the value of the materials reflectivity");
2493         uiDefButS(block, TOG3|BIT|5, B_MATPRV, "Spec",  1087,146,36,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the value of specularity");
2494         uiDefButS(block, TOG3|BIT|8, B_MATPRV, "Hard",  1126,146,44,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the hardness value");
2495         uiDefButS(block, TOG3|BIT|7, B_MATPRV, "Alpha", 1172,146,45,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the alpha value");
2496         uiDefButS(block, TOG3|BIT|6, B_MATPRV, "Emit",  1220,146,45,18, &(mtex->mapto), 0, 0, 0, 0, "Let the texture affect the emit value");
2497         
2498 /*      uiDefButS(block, TOG|BIT|3, B_MATPRV, "Alpha Mix",1087,114,100,18, &(mtex->texflag), 0, 0, 0, 0); ,""*/
2499
2500         uiBlockSetCol(block, BUTGREY);
2501         uiDefButS(block, ROW, B_MATPRV, "Mix",                  1087,94,48,18, &(mtex->blendtype), 9.0, (float)MTEX_BLEND, 0, 0, "The texture blends the values or colour");
2502         uiDefButS(block, ROW, B_MATPRV, "Mul",                  1136,94,44,18, &(mtex->blendtype), 9.0, (float)MTEX_MUL, 0, 0, "The texture multiplies the values or colour");
2503         uiDefButS(block, ROW, B_MATPRV, "Add",                  1182,94,41,18, &(mtex->blendtype), 9.0, (float)MTEX_ADD, 0, 0, "The texture adds the values or colour");
2504         uiDefButS(block, ROW, B_MATPRV, "Sub",                  1226,94,40,18, &(mtex->blendtype), 9.0, (float)MTEX_SUB, 0, 0, "The texture subtracts the values or colour");
2505         
2506         uiDefButF(block, NUMSLI, B_MATPRV, "Col ",              1087,50,179,18, &(mtex->colfac), 0.0, 1.0, 0, 0, "Set the amount the texture affects colour");
2507         uiDefButF(block, NUMSLI, B_MATPRV, "Nor ",              1087,30,179,18, &(mtex->norfac), 0.0, 5.0, 0, 0, "Set the amount the texture affects the normal");
2508         uiDefButF(block, NUMSLI, B_MATPRV, "Var ",              1087,10,179,18, &(mtex->varfac), 0.0, 1.0, 0, 0, "Set the amount the texture affects a value");
2509         
2510         uiDrawBlock(block);
2511 }
2512
2513
2514 /* ************************ SOUND *************************** */
2515 static void load_new_sample(char *str)  /* called from fileselect */
2516 {
2517         char name[FILE_MAXDIR+FILE_MAXFILE];
2518         bSound *sound;
2519         bSample *sample, *newsample;
2520
2521         sound = G.buts->lockpoin;
2522
2523         if (sound) {
2524                 // save values
2525                 sample = sound->sample;
2526                 strcpy(name, sound->sample->name);
2527
2528                 strcpy(sound->name, str);
2529                 sound_set_sample(sound, NULL);
2530                 sound_initialize_sample(sound);
2531
2532                 if (sound->sample->type == SAMPLE_INVALID) {
2533                         error("Not a valid sample: %s", str);
2534
2535                         newsample = sound->sample;
2536
2537                         // restore values
2538                         strcpy(sound->name, name);
2539                         sound_set_sample(sound, sample);
2540
2541                         // remove invalid sample
2542
2543                         sound_free_sample(newsample);
2544                         BLI_remlink(samples, newsample);
2545                         MEM_freeN(newsample);
2546                 }
2547         }
2548
2549         allqueue(REDRAWBUTSSOUND, 0);
2550         if (curarea) BIF_preview_changed(G.buts);
2551 }
2552
2553
2554 void do_soundbuts(unsigned short event)
2555 {
2556         char name[FILE_MAXDIR+FILE_MAXFILE];
2557         bSound *sound;
2558         bSample *sample;
2559         bSound* tempsound;
2560         ID *id;
2561         
2562         sound = G.buts->lockpoin;
2563         
2564         switch(event)
2565         {
2566         case B_SOUND_REDRAW:
2567                 {
2568                         allqueue(REDRAWBUTSSOUND, 0);
2569                         break;
2570                 }
2571         case B_SOUND_LOAD_SAMPLE:
2572                 {
2573                         if (sound) strcpy(name, sound->name);
2574                         else strcpy(name, U.sounddir);
2575                         
2576                         activate_fileselect(FILE_SPECIAL, "SELECT WAV FILE", name, load_new_sample);
2577                         break;
2578                 }
2579         case B_SOUND_PLAY_SAMPLE:
2580                 {
2581                         if (sound)
2582                         {
2583                                 if (sound->sample->type != SAMPLE_INVALID)
2584                                 {
2585                                         sound_play_sound(sound);
2586                                         allqueue(REDRAWBUTSSOUND, 0);
2587                                 }
2588                         }
2589                         break;
2590                 }
2591         case B_SOUND_MENU_SAMPLE:
2592                 {
2593                         if (G.buts->menunr == -2) {
2594                                 if (sound) {
2595                                         activate_databrowse((ID *)sound->sample, ID_SAMPLE, 0, B_SOUND_MENU_SAMPLE, &G.buts->menunr, do_soundbuts);
2596                                 }
2597                         } else if (G.buts->menunr > 0) {
2598                                 sample = BLI_findlink(samples, G.buts->menunr - 1);
2599                                 if (sample && sound) {
2600                                         BLI_strncpy(sound->name, sample->name, sizeof(sound->name));
2601                                         sound_set_sample(sound, sample);
2602                                         do_soundbuts(B_SOUND_REDRAW);
2603                                 }
2604                         }
2605                         
2606                         break;
2607                 }
2608         case B_SOUND_NAME_SAMPLE:
2609                 {
2610                         load_new_sample(sound->name);
2611                         break;
2612                 }
2613         case B_SOUND_UNPACK_SAMPLE:
2614                 if(sound && sound->sample) {
2615                         sample = sound->sample;
2616                         
2617                         if (sample->packedfile) {
2618                                 if (G.fileflags & G_AUTOPACK) {
2619                                         if (okee("Disable AutoPack ?")) {
2620                                                 G.fileflags &= ~G_AUTOPACK;
2621                                         }
2622                                 }
2623                                 
2624                                 if ((G.fileflags & G_AUTOPACK) == 0) {
2625                                         unpackSample(sample, PF_ASK);
2626                                 }
2627                         } else {
2628                                 sound_set_packedfile(sample, newPackedFile(sample->name));
2629                         }
2630                         allqueue(REDRAWHEADERS, 0);
2631                         do_soundbuts(B_SOUND_REDRAW);
2632                 }
2633                 break;
2634         case B_SOUND_COPY_SOUND:
2635                 {
2636                         if (sound)
2637                         {
2638                                 tempsound = sound_make_copy(sound);
2639                                 sound = tempsound;
2640                                 id = &sound->id;
2641                                 G.buts->lockpoin = (bSound*)id;
2642                                 do_soundbuts(B_SOUND_REDRAW);
2643                         }
2644                         break;
2645                 }
2646         case B_SOUND_RECALC:
2647                 {
2648                         waitcursor(1);
2649                         sound = G.main->sound.first;
2650                         while (sound)
2651                         {
2652                                 MEM_freeN(sound->stream);
2653                                 sound->stream = 0;
2654                                 audio_makestream(sound);
2655                                 sound = (bSound *) sound->id.next;
2656                         }
2657                         waitcursor(0);
2658                         allqueue(REDRAWSEQ, 0);
2659                         break;
2660                 }
2661         case B_SOUND_RATECHANGED:
2662                 {
2663                         allqueue(REDRAWBUTSSOUND, 0);
2664                         allqueue(REDRAWSEQ, 0);
2665                         break;
2666                 }               
2667         case B_SOUND_MIXDOWN:
2668                 {
2669                         audio_mixdown();
2670                         break;
2671                 }                               
2672         case B_SOUND_LOOPSTART:
2673                 {
2674 #ifdef SOUND_UNDER_DEVELOPMENT
2675 /*                      if (sound->loopstart > sound->loopend)
2676                                 sound->loopstart = sound->loopend;*/
2677 #endif
2678                         allqueue(REDRAWBUTSSOUND, 0);
2679                         BIF_preview_changed(G.buts);
2680                         break;
2681                 }
2682         case B_SOUND_LOOPEND:
2683                 {
2684 #ifdef SOUND_UNDER_DEVELOPMENT
2685 /*                      if (sound->loopend < sound->loopstart)
2686                                 sound->loopend = sound->loopstart;*/
2687 #endif
2688                         allqueue(REDRAWBUTSSOUND, 0);
2689                         BIF_preview_changed(G.buts);
2690                         break;
2691                 }
2692
2693         default:
2694                 {
2695                         if (G.f & G_DEBUG)
2696                         {
2697                                 printf("do_soundbuts: unhandled event %d\n", event);
2698                         }
2699                         break;
2700                 }
2701         }
2702 }
2703
2704
2705 void soundbuts(void)
2706 {
2707         short xco, yco, xcostart = 20;
2708         bSound *sound;
2709         bSample *sample;
2710         uiBlock *block;
2711         char *strp, str[32];
2712         ID *id;
2713         char ch[20];
2714         char sampleinfo[200];
2715         char mixrateinfo[50];
2716         int mixrate;
2717         
2718         sound = G.buts->lockpoin;
2719         if ((sound) && (sound->flags & SOUND_FLAGS_SEQUENCE)) sound = 0;
2720         yco = 195;
2721
2722         xco = xcostart;
2723         sprintf(str, "buttonswin %d", curarea->win);
2724         block= uiNewBlock(&curarea->uiblocks, str, UI_EMBOSSX, UI_HELV, curarea->win);
2725                 
2726         if (sound)
2727         {
2728                 sound_initialize_sample(sound);
2729
2730                 sample = sound->sample;
2731
2732                 uiSetButLock(sound->id.lib!=0, "Can't edit library data");
2733
2734                 /* sound settings ------------------------------------------------------------------ */
2735
2736                 uiDefBut(block, LABEL, 0, "Game sounds",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2737
2738                 yco -= 30;
2739                 uiBlockSetCol(block, BUTGREEN);
2740                 uiDefBut(block, BUT, B_SOUND_PLAY_SAMPLE, "Play", xco, yco, 195, 24, 0, 0.0, 0, 0, 0,
2741                         "Playback sample using settings below");
2742                 
2743                 uiBlockSetCol(block, BUTGREY);
2744                 xco += 225;
2745
2746                 if (sound->sample && sound->sample->len)
2747                 {
2748                         if (sound->sample->channels == 1)
2749                                 strcpy(ch, "Mono");
2750                         else if (sound->sample->channels == 2)
2751                                 strcpy(ch, "Stereo");
2752                         else
2753                                 strcpy(ch, "Unknown");
2754                         
2755                         uiDefBut(block, LABEL, 0, "Sample: ",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2756                         xco +=55;
2757                         sprintf(sampleinfo, "%s, %d bit, %d Hz, %d samples", ch, sound->sample->bits, sound->sample->rate, (sound->sample->len/(sound->sample->bits/8)/sound->sample->channels));
2758                         uiDefBut(block, LABEL, 0, sampleinfo,xco,yco,295,20, 0, 0, 0, 0, 0, "");
2759                 }
2760                 else
2761                 {
2762                         uiDefBut(block, LABEL, 0, "No sample info available.",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2763                         xco +=55;
2764                 }
2765
2766                 xco += 314;
2767                 uiDefBut(block, BUT, B_SOUND_COPY_SOUND, "Copy sound", 
2768                         xco,yco,95,24, 0, 0, 0, 0, 0, "Make a copy of the current sound");
2769                 /*
2770                 xco += 25;
2771                 if (sample->channels > 1)
2772                 {
2773                         xco += 100;
2774                         uiDefButC(block, ROW, B_SOUND_REDRAW, "Left",   xco, yco, 95, 20, &sound->channels, 1.0, (float)SOUND_CHANNELS_LEFT, 0, 0, "");
2775                         xco += 100;
2776                         uiDefButC(block, ROW, B_SOUND_REDRAW, "Stereo", xco, yco, 95, 20, &sound->channels, 1.0, (float)SOUND_CHANNELS_STEREO, 0, 0, "");
2777                         xco += 100;
2778                         uiDefButC(block, ROW, B_SOUND_REDRAW, "Right",  xco, yco, 95, 20, &sound->channels, 1.0, (float)SOUND_CHANNELS_RIGHT, 0, 0, "");
2779                 }
2780                 */
2781                 
2782                 xco = xcostart;
2783                 yco -= 30;
2784                 uiDefBut(block, BUT, B_SOUND_LOAD_SAMPLE, "Load sample", 
2785                         xco, yco,195,24, 0, 0, 0, 0, 0, "Load a different sample");
2786                 
2787                 uiBlockSetCol(block, BUTGREY);
2788                 
2789                 id= (ID *)sound->sample;
2790                 IDnames_to_pupstring(&strp, NULL, NULL, samples, id, &(G.buts->menunr));
2791                 if (strp[0]) {
2792                         xco += 200;
2793                         uiDefButS(block, MENU, B_SOUND_MENU_SAMPLE, strp,xco,yco,23,24, &(G.buts->menunr), 0, 0, 0, 0, "Select another loaded sample");
2794                 }
2795                 MEM_freeN(strp);
2796                 
2797                 xco += 25;
2798                 uiDefBut(block, TEX, B_SOUND_NAME_SAMPLE, "",xco,yco,412,24, sound->name, 0.0, 79.0, 0, 0, "The sample used by this sound");
2799                 
2800                 sprintf(str, "1");
2801                 // sprintf(str, "%d", tex->ima->id.us);
2802                 xco += 415;
2803                 uiDefBut(block, BUT, B_SOUND_UNLINK_SAMPLE, str,xco,yco,23,24, 0, 0, 0, 0, 0, "The number of users");
2804                 
2805                 if (sound->sample->packedfile)
2806                         packdummy = 1;
2807                 else
2808                         packdummy = 0;
2809                 
2810                 xco += 25;
2811                 uiDefIconButI(block, TOG|BIT|0, B_SOUND_UNPACK_SAMPLE, ICON_PACKAGE,
2812                         xco, yco,24,24, &packdummy, 0, 0, 0, 0,"Pack/Unpack this sample");
2813                 /*
2814                 xco += 25;
2815                 uiDefBut(block, BUT, B_SOUND_RELOAD_SAMPLE, "Reload",xco, yco,68,24, 0, 0, 0, 0, 0, "");
2816                 */
2817                 /* parameters settings ------------------------------------------------------------------ */
2818                 
2819                 xco = xcostart;
2820                 yco -= 45;
2821                 uiDefBut(block, LABEL, 0, "Game sound settings:",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2822
2823                 yco -= 30;
2824                 uiBlockSetCol(block, BUTGREY);
2825                 uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Volume: ",
2826                         xco,yco,195,24,&sound->volume, 0.0, 1.0, 0, 0, "Set the volume of this sound");
2827
2828                 xco += 200;
2829                 uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Pitch: ",
2830                         xco,yco,195,24,&sound->pitch, -12.0, 12.0, 0, 0, "Set the pitch of this sound");
2831
2832                 xco = xcostart;
2833                 yco -= 30;
2834                 uiBlockSetCol(block, BUTSALMON);
2835                 uiDefButI(block, TOG|BIT|SOUND_FLAGS_LOOP_BIT, B_SOUND_REDRAW, "Loop",
2836                         xco, yco, 95, 24, &sound->flags, 0.0, 0.0, 0, 0,"Toggle between looping on/off");
2837
2838                 if (sound->flags & SOUND_FLAGS_LOOP)
2839                 {
2840                         xco += 100;
2841                         uiDefButI(block, TOG|BIT|SOUND_FLAGS_BIDIRECTIONAL_LOOP_BIT, B_SOUND_REDRAW, "Ping Pong",
2842                                 xco, yco, 95, 24, &sound->flags, 0.0, 0.0, 0, 0,"Toggle between A->B and A->B->A looping");
2843                         
2844 #ifdef SOUND_UNDER_DEVELOPMENT
2845 /*                      uiBlockSetCol(block, REDALERT);
2846                         xco += 100;
2847                         uiDefButI(block, NUM, B_SOUND_LOOPSTART, "loopstart: ", xco,yco,195,24,
2848                                 &sound->loopstart, 0, sound->sample->len, 0, 0, "Set the startpoint for the loop of this sound");
2849                         
2850                         xco += 200;
2851                         uiDefButI(block, NUM, B_SOUND_LOOPEND, "loopend: ",xco,yco,195,24,
2852                                 &sound->loopend, 0, sound->sample->len, 0, 0, "Set the endpoint for the loop of this sound");
2853 */
2854 #endif
2855                 }
2856
2857 #ifdef SOUND_UNDER_DEVELOPMENT
2858                 xco = xcostart;
2859                 yco -= 30;
2860                 uiDefButI(block, TOG|BIT|SOUND_FLAGS_PRIORITY_BIT, B_SOUND_REDRAW, "Priority",
2861                         xco, yco, 95, 24, &sound->flags, 0.0, 0.0, 0, 0,"Toggle between high and low priority");
2862 #endif
2863
2864                 /* 2D & 3D settings ------------------------------------------------------------------ */
2865
2866                 uiBlockSetCol(block, BUTGREY);
2867                 if (sound->sample->channels == 1)
2868                 {
2869                         xco = xcostart;
2870                         yco -= 30;
2871                         uiDefButI(block, TOG|BIT|SOUND_FLAGS_3D_BIT, B_SOUND_REDRAW, "3D Sound",
2872                                 xco, yco, 95, 24, &sound->flags, 0, 0, 0, 0, "Turns 3D sound on");
2873                         
2874                         if (sound->flags & SOUND_FLAGS_3D)
2875                         {
2876                                 xco = xcostart;
2877                                 yco -= 30;
2878                                 uiBlockSetCol(block, BUTGREY);
2879                                 uiDefBut(block, LABEL, 0, "3D surround settings:",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2880                                 uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Scale: ",
2881                                         xco,(short)(yco-=30),195,24,&sound->attenuation, 0.0, 5.0, 1.0, 0, "Sets the world-scaling factor for this sound");
2882                                 
2883                                         /*
2884                                         xco += 200;
2885                                         uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Distance: ",
2886                                         xco,yco,195,20,&sound->distance, 0.0, 100.0, 1.0, 0, "Reference distance: sets the distance at which the listener will experience gain");
2887                                         xco -= 200;
2888                                         yco -= 30;
2889                                         uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Minvol: ",
2890                                         xco,yco,195,20,&sound->min_gain, 0.0, 1.0, 1.0, 0, "Minimal volume: sets the lower threshold for the gain of this sound");
2891                                         xco += 200;
2892                                         uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Maxvol: ",
2893                                         xco,yco,195,20,&sound->max_gain, 0.0, 10.0, 1.0, 0, "Maximal volume: sets the upper threshold for the gain of this sound");
2894                                         */
2895                         }
2896                 }
2897
2898                 /* listener settings ------------------------------------------------------------------ */
2899
2900                 draw_buttons_edge(curarea->win, 740);
2901                 
2902                 xco = xcostart + 750;
2903                 yco = 195;
2904                 uiBlockSetCol(block, BUTGREY);
2905                 mixrate = sound_get_mixrate();
2906                 sprintf(mixrateinfo, "Game Mixrate: %d Hz", mixrate);
2907                 uiDefBut(block, LABEL, 0, mixrateinfo, xco,yco,295,20, 0, 0, 0, 0, 0, "");
2908
2909                 yco -= 30;
2910
2911                 uiDefBut(block, LABEL, 0, "Game listener settings:",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2912
2913                 yco -= 30;
2914                 uiDefButF(block, NUMSLI, B_SOUND_CHANGED, "Volume: ",
2915                         xco,yco,195,24,&G.listener->gain, 0.0, 1.0, 1.0, 0, "Sets the maximum volume for the overall sound");
2916                 
2917                 yco -= 30;
2918                 uiDefBut(block, LABEL, 0, "Game Doppler effect settings:",xco,yco,195,20, 0, 0, 0, 0, 0, "");
2919                 /*
2920