aaa8f64d11a8a318b0f2c2b565ea057c47d6f71d
[blender.git] / source / blender / src / buttons_editing.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <time.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef WIN32
39 #ifndef snprintf
40 #define snprintf _snprintf
41 #endif
42 #endif
43
44 #include "MEM_guardedalloc.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_scene_types.h"
48
49 #include "DNA_action_types.h"
50 #include "DNA_armature_types.h"
51 #include "DNA_camera_types.h"
52 #include "DNA_constraint_types.h"
53 #include "DNA_curve_types.h"
54 #include "DNA_effect_types.h"
55 #include "DNA_group_types.h"
56 #include "DNA_image_types.h"
57 #include "DNA_key_types.h"
58 #include "DNA_lamp_types.h"
59 #include "DNA_lattice_types.h"
60 #include "DNA_material_types.h"
61 #include "DNA_meta_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_meshdata_types.h"
64 #include "DNA_modifier_types.h"
65 #include "DNA_object_types.h"
66 #include "DNA_object_force.h"
67 #include "DNA_radio_types.h"
68 #include "DNA_screen_types.h"
69 #include "DNA_texture_types.h"
70 #include "DNA_userdef_types.h"
71 #include "DNA_vfont_types.h"
72 #include "DNA_view3d_types.h"
73 #include "DNA_world_types.h"
74 #include "DNA_packedFile_types.h"
75
76 #include "BKE_curve.h"
77 #include "BKE_depsgraph.h"
78 #include "BKE_global.h"
79 #include "BKE_key.h"
80 #include "BKE_library.h"
81 #include "BKE_main.h"
82 #include "BKE_modifier.h"
83 #include "BKE_packedFile.h"
84 #include "BKE_scene.h"
85
86 #include "BLI_blenlib.h"
87 #include "BLI_arithb.h"
88 #include "BLI_vfontdata.h"
89 #include "BLI_editVert.h"
90 #include "BLI_dynstr.h"
91
92 #include "BSE_filesel.h"
93
94 #include "BIF_gl.h"
95 #include "BIF_editarmature.h"
96 #include "BIF_editconstraint.h"
97 #include "BIF_editdeform.h"
98 #include "BIF_editfont.h"
99 #include "BIF_editkey.h"
100 #include "BIF_editmesh.h"
101 #include "BIF_interface.h"
102 #include "BIF_meshtools.h"
103 #include "BIF_mywindow.h"
104 #include "BIF_poseobject.h"
105 #include "BIF_renderwin.h"
106 #include "BIF_resources.h"
107 #include "BIF_screen.h"
108 #include "BIF_scrarea.h"
109 #include "BIF_space.h"
110 #include "BIF_toets.h"
111 #include "BIF_toolbox.h"
112 #include "BIF_previewrender.h"
113 #include "BIF_butspace.h"
114
115 #include "mydevice.h"
116 #include "blendef.h"
117
118 #include "BKE_action.h"
119 #include "BKE_anim.h"
120 #include "BKE_armature.h"
121 #include "BKE_constraint.h"
122 #include "BKE_curve.h"
123 #include "BKE_displist.h"
124 #include "BKE_DerivedMesh.h"
125 #include "BKE_effect.h"
126 #include "BKE_font.h"
127 #include "BKE_image.h"
128 #include "BKE_ipo.h"
129 #include "BKE_lattice.h"
130 #include "BKE_material.h"
131 #include "BKE_mball.h"
132 #include "BKE_mesh.h"
133 #include "BKE_object.h"
134 #include "BKE_texture.h"
135 #include "BKE_utildefines.h"
136
137 #include "BDR_drawobject.h"
138 #include "BDR_editcurve.h"
139 #include "BDR_editface.h"
140 #include "BDR_editobject.h"
141 #include "BDR_vpaint.h"
142 #include "BDR_unwrapper.h"
143
144 #include "BSE_drawview.h"
145 #include "BSE_editipo.h"
146 #include "BSE_edit.h"
147 #include "BSE_filesel.h"
148 #include "BSE_headerbuttons.h"
149 #include "BSE_trans_types.h"
150 #include "BSE_view.h"
151 #include "BSE_buttons.h"
152 #include "BSE_seqaudio.h"
153
154 #include "RE_renderconverter.h"         // make_sticky
155
156 #include "butspace.h" // own module
157
158 static float editbutweight=1.0;
159 float editbutvweight=1;
160 float uv_calc_radius= 1.0, uv_calc_cubesize= 1.0;
161 short uv_calc_mapdir= 1, uv_calc_mapalign= 1, facesel_draw_edges= 0;
162
163 extern ListBase editNurb;
164
165 /* *************************** Unicode Character Groups ****************** */
166 unicodect uctabname[125] = {
167         {"All", "All", 0x0000, 0xffff},
168         {"Basic Latin", "Basic Latin", 0x0000, 0x007f},
169         {"Latin 1 Supp", "Latin-1 Supplement", 0x0080, 0x00ff}, 
170
171         {"Latin Ext. A.", "Latin Extended-A", 0x0100, 0x017F},
172         {"Latin Ext. B.", "Latin Extended-B", 0x0180,0x024F}, 
173         {"Latin Ext. Add.", "Latin Extended Additional", 0x1e00, 0x1eff},
174
175         {"IPA Ext", "IPA Extensions", 0x0250, 0x02AF},
176         {"Spacing Mod.", "Spacing Modifier Letters", 0x02b0, 0x02ff},
177
178         {"Comb. Dia.", "Combining Diacritical Marks", 0x0300, 0x036F},
179         {"Greek, Coptic", "Greek and Coptic", 0x0370, 0x03ff},
180         {"Greek Ext.", "Greek Extended", 0x1f00, 0x1fff},
181
182         {"Cyrillic", "Cyrillic", 0x0400, 0x04ff},
183         {"Cyrillic Supp.", "Cyrillic Supplementary", 0x0500, 0x052f},
184
185         {"Armenian", "Armenian", 0x0530, 0x058f},
186         {"Hebrew", "Hebrew", 0x0590, 0x05ff},
187
188         
189         {"Arabic", "Arabic", 0x0600, 0x06ff},
190         {"Syriac", "Syriac", 0x0700, 0x074f},
191
192         {"Thaana", "Thaana", 0x0780, 0x07bf},
193         {"Devanagari", "Devanagari", 0x0900, 0x097f},
194
195         {"Bengali", "Bengali", 0x0980, 0x09ff},
196         {"Gurmukhi", "Gurmukhi", 0x0a00, 0x0a7f},
197
198         {"Gujarati", "Gujarati", 0x0a80, 0x0aff},
199         {"Oriya", "Oriya", 0x0b00, 0x0b7f},
200
201         {"Tamil", "Tamil", 0x0b80, 0x0bff},
202         {"Tegulu", "Tegulu", 0x0c00, 0x0c7f},
203
204         {"Kannada", "Kannada", 0x0c80, 0x0cff},
205         {"Malayalam", "Malayalam", 0x0d00, 0x0d7f},
206
207         {"Sinhala", "Sinhala", 0x0d80, 0x0dff},
208         {"Thai", "Thai", 0x0e00, 0x0e7f},
209
210         {"Lao", "Lao", 0x0e80, 0x0eff},
211         {"Tibetan", "Tibetan", 0x0f00, 0x0fff},
212
213         {"Myanmar", "Myanmar", 0x1000, 0x109f},
214         {"Georgian", "Georgian", 0x10a0, 0x10ff},
215
216         {"Ethiopic", "Ethiopic", 0x1200, 0x137f},
217
218         {"Cherokee", "Cherokee", 0x13a0, 0x13ff},
219         {"Unif. Canadian", "Unified Canadian Aboriginal Syllabics", 0x1400, 0x167f},
220
221         {"Ogham", "Ogham", 0x1680, 0x169f},
222         {"Runic", "Runic", 0x16a0, 0x16ff},
223
224         {"Tagalog", "Tagalog", 0x1700, 0x171f},
225         {"Hanunoo", "Hanunoo", 0x1720, 0x173f},
226
227         {"Buhid", "Buhid", 0x1740, 0x175f},
228         {"Tagbanwa", "Tagbanwa", 0x1760, 0x177f},
229
230         {"Khmer", "Khmer", 0x1780, 0x17ff},
231         {"Khmer Symb", "Khmer Symbols", 0x19e0, 0x19ff},
232
233         {"Mongolian", "Mongolian", 0x1800, 0x18af},
234
235         {"Limbu", "Limbu", 0x1900, 0x194f},
236         {"Tai Le", "Tai Le", 0x1950, 0x197f},
237
238         {"Phon. Ext.", "Phonetic Extensions", 0x1d00, 0x1d7f},
239
240
241         {"Gen. Punct.", "General Punctutation", 0x2000, 0x206f},
242         {"Super, Sub", "Superscripts and Subscripts", 0x2070, 0x209f},
243
244         {"Curr. Symb.", "Currency Symbols", 0x20a0, 0x20cf},
245         {"Comb. Diacrit.", "Combining Diacritical Marks for Symbols", 0x20d0, 0x20ff},
246
247         {"Letter Symb", "Letterlike Symbols", 0x2100, 0x214f},
248         {"Numb. Forms", "Number Forms", 0x2150, 0x218f},
249
250         {"Arrows", "Arrows", 0x2190, 0x21ff},
251         {"Math Oper.", "Mathematical Operators", 0x2200, 0x22ff},
252
253         {"Misc. Tech.", "Miscellaneous Technical", 0x2300, 0x23ff},
254         {"Ctrl. Pict.", "Control Pictures", 0x2400, 0x243f},
255
256         {"OCR", "Optical Character Recognition", 0x2440, 0x245f},
257         {"Enc. Alpha", "Enclosed Alphanumerics", 0x2460, 0x24ff},
258
259         {"Bow Drawing", "Box Drawing", 0x2500, 0x257f},
260         {"BLock Elem.", "Block Elements", 0x2580, 0x259f},
261
262         {"Geom. Shapes", "Geometric Shapes", 0x25a0, 0x25ff},
263         {"Misc. Symb.", "Miscellaneous Symbols", 0x2600, 0x26ff},
264
265         {"Dingbats", "Dingbats", 0x2700, 0x27bf},
266         {"Misc. Math A", "Miscellaneous Mathematical Symbols-A", 0x27c0, 0x27ef},
267
268         {"Supp. Arrows-A", "Supplemental Arrows-A", 0x27f0, 0x27ff},
269         {"Braille Pat.", "Braille Patterns", 0x2800, 0x28ff},
270
271         {"Supp. Arrows-B", "Supplemental Arrows-B", 0x2900, 0x297f},
272         {"Misc. Math B", "Miscellaneous Mathematical Symbols-B", 0x2980, 0x29ff},
273
274         {"Supp. Math Op.", "Supplemental Mathematical Operators", 0x2a00, 0x2aff},
275         {"Misc. Symb.", "Miscellaneous Symbols and Arrows", 0x2b00, 0x2bff},
276
277         {"Kangxi Rad.", "Kangxi Radicals", 0x2f00, 0x2fdf},
278
279         {"Ideographic", "Ideographic Description Characters", 0x2ff0, 0x2fff},
280
281         {"Hiragana", "Hiragana", 0x3040, 0x309f},
282         {"Katakana", "Katakana", 0x30a0, 0x30ff},
283         {"Katakana Ext.", "Katakana Phonetic Extensions", 0x31f0, 0x31ff},
284
285         {"Bopomofo", "Bopomofo", 0x3100, 0x312f},
286         {"Bopomofo Ext.", "Bopomofo Extended", 0x31a0, 0x31bf},
287
288         {"Hangul", "Hangul Jamo", 0x1100, 0x11ff},
289         {"Hangul Comp.", "Hangul Compatibility Jamo", 0x3130, 0x318f},
290         {"Hangul Syll.", "Hangul Syllables", 0xac00, 0xd7af},
291
292         {"Kanbun", "Kanbun", 0x3190, 0x319f},
293
294
295
296         {"Yijing Hex.", "Yijing Hexagram Symbols", 0x4dc0, 0x4dff},
297
298         {"Yi Syllables", "Yi Syllables", 0xa000, 0xa48f},
299         {"Yi Radicals", "Yi Radicals", 0xa490, 0xa4cf},
300
301         {"High Surr.", "High Surrogate Area", 0xd800, 0xdbff},
302
303         {"Low Surr.", "Low Surrogates", 0xdc00, 0xdfff},
304         {"Priv. Use Area", "Private Use Area", 0xe000, 0xf8ff},
305
306         {"CJK Rad. Supp.", "CJK Radicals Supplement", 0x2e80, 0x2eff},
307         {"CJK Ideographs", "CJK Unified Ideographs", 0x4e00, 0x9faf},
308         {"CJK Ideog. Ext. A", "CJK Unified Ideographs Extension A", 0x3400, 0x4dbf},
309         {"CJK Ideog. Ext. B", "CJK Unified Ideographs Extension B", 0x20000, 0x2a6df},
310         {"CJK Symbols.", "CJK Symbols and Punctuation", 0x3000, 0x303f},
311         {"Enclosed CJK", "Enclosed CJK Letters and Months", 0x3200, 0x32ff},
312         {"CJK Comp.", "CJK Compatibility", 0x3300, 0x33ff},
313         {"CJK Comp. Ideog.", "CJK Compatibility Ideographs", 0xf900, 0xfaff},
314         {"CJK Comp. Forms", "CJK Compatibility Forms", 0xfe30, 0xfe4f},
315         {"CJK Comp. Supp.", "CJK Compatibility Ideographs Supplement", 0x2f800, 0x2fa1f},
316
317         {"Alpha. Pres. Forms", "Alphabetic Presentation Forms", 0xfb00, 0xfb4f},
318
319         {"Arabic Pres. A", "Arabic Presentation Forms-A", 0xfb50, 0xfdff},
320         {"Arabic Pres. B", "Arabic Presentation Forms-B", 0xfe70, 0xfeff},
321
322         {"Var. Sel.", "Variation Selectors", 0xfe00, 0xfe0f},
323
324         {"Comb. Half", "Combining Half Marks", 0xfe20, 0xfe2f},
325
326         {"Sml. From Var.", "Small Form Variants", 0xfe50, 0xfe6f},
327
328         {"Half, Full Forms", "Halfwidth and Fullwidth Forms", 0xff00, 0xffef},
329         {"Specials", "Specials", 0xfff0, 0xffff},
330
331         {"Lin. B Syllab.", "Linear B Syllabary", 0x10000, 0x1007f},
332         {"Lin. B Idog.", "Linear B Ideograms", 0x10080, 0x100ff},
333
334         {"Aegean Num.", "Aegean Numbers", 0x10100, 0x1013f},
335         {"Old Italic", "Old Italic", 0x10300, 0x1032f},
336
337         {"Gothic", "Gothic", 0x10330, 0x1034f},
338         {"Ugaritic", "Ugaritic", 0x10380, 0x1039f},
339
340         {"Deseret", "Deseret", 0x10400, 0x1044f},
341         {"Shavian", "Shavian", 0x10450, 0x1047f},
342
343         {"Osmanya", "Osmanya", 0x10480, 0x104af},
344         {"Cypriot Syll", "Cypriot Syllabary", 0x10800, 0x1083f},
345
346         {"Bysantine Mus.", "Bysantine Musical Symbols", 0x1d000, 0x1d0ff},
347         {"Music Symb.", "Musical Symbols", 0x1d100, 0x1d1ff},
348
349         {"Tai Xuan Symb", "Tai Xuan Jing Symbols", 0x1d300, 0x1d35f},
350         {"Math. Alpha Symb.", "Mathematical Alpanumeric Symbols", 0x1d400, 0x1d7ff},
351
352
353         {"Tags", "Tags", 0xe0000, 0xe007f},
354         {"Var. Supp", "Variation Selectors Supplement", 0xe0100, 0xe01ef},
355
356         {"Supp. Priv. A", "Supplementary Private Use Area-A", 0xf0000, 0xffffd},
357         {"Supp. Priv. B", "Supplementary Private Use Area-B", 0x100000, 0x10fffd}
358 };
359
360
361 /* *************************** static functions prototypes ****************** */
362 VFont *exist_vfont(char *str);
363
364 /* *************** */
365
366 void do_common_editbuts(unsigned short event) // old name, is a mix of object and editing events.... 
367 {
368         EditMesh *em = G.editMesh;
369         EditFace *efa;
370         Base *base;
371         Object *ob= OBACT;
372         Nurb *nu;
373         Curve *cu;
374         BezTriple *bezt;
375         BPoint *bp;
376         unsigned int local;
377         int a, bit, index= -1;
378
379         switch(event) {
380                 
381         case B_MATWICH:
382                 if(G.obedit && G.obedit->actcol>0) {
383                         if(G.obedit->type == OB_MESH) {
384                                 efa= em->faces.first;
385                                 while(efa) {
386                                         if(efa->f & SELECT) {
387                                                 if(index== -1) index= efa->mat_nr;
388                                                 else if(index!=efa->mat_nr) {
389                                                         error("Mixed colors");
390                                                         return;
391                                                 }
392                                         }
393                                         efa= efa->next;
394                                 }
395                         }
396                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
397                                 nu= editNurb.first;
398                                 while(nu) {
399                                         if( isNurbsel(nu) ) {
400                                                 if(index== -1) index= nu->mat_nr;
401                                                 else if(index!=nu->mat_nr) {
402                                                         error("Mixed colors");
403                                                         return;
404                                                 }
405                                         }
406                                         nu= nu->next;
407                                 }                               
408                         }
409                         if(index>=0) {
410                                 G.obedit->actcol= index+1;
411                                 scrarea_queue_winredraw(curarea);
412                         }
413                 }
414                 break;
415         case B_MATNEW:
416                 new_material_to_objectdata(ob);
417                 scrarea_queue_winredraw(curarea);
418                 BIF_undo_push("New material");
419                 allqueue(REDRAWBUTSSHADING, 0);
420                 allqueue(REDRAWVIEW3D_Z, 0);
421                 allqueue(REDRAWOOPS, 0);
422                 break;
423         case B_MATDEL:
424                 delete_material_index();
425                 scrarea_queue_winredraw(curarea);
426                 BIF_undo_push("Delete material index");
427                 allqueue(REDRAWBUTSSHADING, 0);
428                 allqueue(REDRAWVIEW3D_Z, 0);
429                 allqueue(REDRAWOOPS, 0);
430                 break;
431         case B_MATASS:
432                 if(G.obedit && G.obedit->actcol>0) {
433                         if(G.obedit->type == OB_MESH) {
434                                 efa= em->faces.first;
435                                 while(efa) {
436                                         if(efa->f & SELECT)
437                                                 efa->mat_nr= G.obedit->actcol-1;
438                                         efa= efa->next;
439                                 }
440                         }
441                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
442                                 nu= editNurb.first;
443                                 while(nu) {
444                                         if( isNurbsel(nu) )
445                                                 nu->mat_nr= nu->charidx= G.obedit->actcol-1;
446                                         nu= nu->next;
447                                 }
448                         }
449                         else if (G.obedit->type == OB_FONT) {
450                         if (mat_to_sel()) {
451                                 allqueue(REDRAWVIEW3D, 0);
452                         }
453                         }
454                         allqueue(REDRAWVIEW3D_Z, 0);
455                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
456                         BIF_undo_push("Assign material index");
457                 }
458                 break;
459         case B_MATSEL:
460         case B_MATDESEL:
461                 if(G.obedit) {
462                         if(G.obedit->type == OB_MESH) {
463                                 if (event==B_MATSEL) {
464                                         editmesh_select_by_material(G.obedit->actcol-1);
465                                 } else {
466                                         editmesh_deselect_by_material(G.obedit->actcol-1);
467                                 }
468                                 allqueue(REDRAWVIEW3D, 0);
469                         }
470                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
471                                 nu= editNurb.first;
472                                 while(nu) {
473                                         if(nu->mat_nr==G.obedit->actcol-1) {
474                                                 if(nu->bezt) {
475                                                         a= nu->pntsu;
476                                                         bezt= nu->bezt;
477                                                         while(a--) {
478                                                                 if(bezt->hide==0) {
479                                                                         if(event==B_MATSEL) {
480                                                                                 bezt->f1 |= 1;
481                                                                                 bezt->f2 |= 1;
482                                                                                 bezt->f3 |= 1;
483                                                                         }
484                                                                         else {
485                                                                                 bezt->f1 &= ~1;
486                                                                                 bezt->f2 &= ~1;
487                                                                                 bezt->f3 &= ~1;
488                                                                         }
489                                                                 }
490                                                                 bezt++;
491                                                         }
492                                                 }
493                                                 else if(nu->bp) {
494                                                         a= nu->pntsu*nu->pntsv;
495                                                         bp= nu->bp;
496                                                         while(a--) {
497                                                                 if(bp->hide==0) {
498                                                                         if(event==B_MATSEL) bp->f1 |= 1;
499                                                                         else bp->f1 &= ~1;
500                                                                 }
501                                                                 bp++;
502                                                         }
503                                                 }
504                                         }
505                                         nu= nu->next;
506                                 }
507                                 BIF_undo_push("Select material index");
508                                 allqueue(REDRAWVIEW3D, 0);
509                         }
510                 }
511                 break;
512         case B_HIDE:
513                 if(G.obedit) {
514                         if(G.obedit->type == OB_MESH) hide_mesh(0);
515                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) hideNurb(0);
516                 }
517                 break;
518         case B_REVEAL:
519                 if(G.obedit) {
520                         if(G.obedit->type == OB_MESH) reveal_mesh();
521                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) revealNurb();
522                 }
523                 else if(G.f & G_FACESELECT) reveal_tface();
524                 
525                 break;
526         case B_SELSWAP:
527                 if(G.obedit) {
528                         if(G.obedit->type == OB_MESH) selectswap_mesh();
529                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) selectswapNurb();
530                 }
531                 break;
532         case B_AUTOTEX:
533                 if(ob && G.obedit==0) {
534                         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) tex_space_curve(ob->data);
535                 }
536                 break;
537         case B_DOCENTRE:
538                 docentre(0);
539                 break;
540         case B_DOCENTRENEW:
541                 docentre_new();
542                 break;
543         case B_DOCENTRECURSOR:
544                 docentre_cursor();
545                 break;
546         case B_SETSMOOTH:
547         case B_SETSOLID:
548                 if(G.obedit) {
549                         if(G.obedit->type == OB_MESH) {
550                                 mesh_set_smooth_faces((event==B_SETSMOOTH));
551                         }
552                         else {
553                                 nurb_set_smooth((event==B_SETSMOOTH));
554                         }
555                 }
556                 else if(G.vd) {
557                         base= FIRSTBASE;
558                         while(base) {
559                                 if(TESTBASELIB(base)) {
560                                         if(base->object->type==OB_MESH) {
561                                                 mesh_set_smooth_flag(base->object, (event==B_SETSMOOTH));
562                                         }
563                                         else if ELEM(base->object->type, OB_SURF, OB_CURVE) {
564                                                 cu= base->object->data;
565                                                 nu= cu->nurb.first;
566                                                 while(nu) {
567                                                         if(event==B_SETSMOOTH) nu->flag |= ME_SMOOTH;
568                                                         else nu->flag &= ~ME_SMOOTH;
569                                                         nu= nu->next;
570                                                 }
571                                                 makeDispListCurveTypes(base->object, 0);
572                                         }
573                                 }
574                                 base= base->next;
575                         }
576                         allqueue(REDRAWVIEW3D, 0);
577                         
578                         if(event == B_SETSMOOTH) BIF_undo_push("Set Smooth");
579                         else BIF_undo_push("Set Solid");
580                 }
581                 break;
582         case B_CHANGEDEP:
583                 DAG_scene_sort(G.scene); // makes new dag
584                 if(ob) ob->recalc |= OB_RECALC;
585                 allqueue(REDRAWVIEW3D, 0);
586                 break;
587         
588         case B_ADDKEY:
589                 insert_shapekey(ob);
590                 break;
591         case B_SETKEY:
592                 ob->shapeflag |= OB_SHAPE_TEMPLOCK;
593                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
594                 allqueue(REDRAWVIEW3D, 0);
595                 allqueue(REDRAWIPO, 0);
596                 allqueue(REDRAWBUTSEDIT, 0);
597                 break;
598         case B_LOCKKEY:
599                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
600                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
601                 allqueue(REDRAWVIEW3D, 0);
602                 allqueue(REDRAWIPO, 0);
603                 allqueue(REDRAWBUTSEDIT, 0);
604                 break;
605         case B_NEXTKEY:
606         {
607                 Key *key= ob_get_key(ob);
608                 if(ob->shapenr == BLI_countlist(&key->block))
609                    ob->shapenr= 1;
610                 else ob->shapenr++;
611                 do_common_editbuts(B_SETKEY);
612                 break;
613         }
614         case B_PREVKEY:
615         {
616                 Key *key= ob_get_key(ob);
617                 if(ob->shapenr <= 1)
618                         ob->shapenr= BLI_countlist(&key->block);
619                 else ob->shapenr--;
620                 do_common_editbuts(B_SETKEY);
621                 break;
622         }
623         case B_NAMEKEY:
624                 allspace(REMAKEIPO, 0);
625         allqueue (REDRAWIPO, 0);
626                 break;
627         case B_DELKEY:
628                 delete_key(OBACT);
629                 break;
630                 
631                 
632         default:
633                 if(event>=B_OBLAY && event<=B_OBLAY+31) {
634                         local= BASACT->lay & 0xFF000000;
635                         BASACT->lay -= local;
636                         if(BASACT->lay==0 || (G.qual & LR_SHIFTKEY)==0) {
637                                 bit= event-B_OBLAY;
638                                 BASACT->lay= 1<<bit;
639                                 scrarea_queue_winredraw(curarea);
640                         }
641                         BASACT->lay += local;
642                         /* optimal redraw */
643                         if( (ob->lay & G.vd->lay) && (BASACT->lay & G.vd->lay) );
644                         else if( (ob->lay & G.vd->lay)==0 && (BASACT->lay & G.vd->lay)==0 );
645                         else allqueue(REDRAWVIEW3D, 0);
646                         
647                         ob->lay= BASACT->lay;
648                 }
649         }
650
651 }
652
653 /* *************************** MESH  ******************************** */
654
655 static void editing_panel_mesh_type(Object *ob, Mesh *me)
656 {
657         uiBlock *block;
658         float val;
659
660         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mesh_type", UI_EMBOSS, UI_HELV, curarea->win);
661         if( uiNewPanel(curarea, block, "Mesh", "Editing", 320, 0, 318, 204)==0) return;
662
663         uiBlockBeginAlign(block);
664         uiDefButBitS(block, TOG, ME_AUTOSMOOTH, REDRAWVIEW3D, "Auto Smooth",10,180,154,19, &me->flag, 0, 0, 0, 0, "Treats all set-smoothed faces with angles less than Degr: as 'smooth' during render");
665         uiDefButS(block, NUM, B_DIFF, "Degr:",                          10,160,154,19, &me->smoothresh, 1, 80, 0, 0, "Defines maximum angle between face normals that 'Auto Smooth' will operate on");
666
667         uiBlockBeginAlign(block);
668         uiBlockSetCol(block, TH_AUTO);
669
670         if(me->mcol) val= 1.0; else val= 0.0;
671         uiDefBut(block, LABEL, 0, "VertCol",                            10,50,70,20, 0, val, 0, 0, 0, "");
672         if(me->mcol==NULL) {
673                 uiDefBut(block, BUT, B_MAKEVERTCOL, "Make",             80,50,84,19, 0, 0, 0, 0, 0, "Enables vertex colour painting on active Mesh");
674         }
675         else uiDefBut(block, BUT, B_DELVERTCOL, "Delete",       80,50,84,19, 0, 0, 0, 0, 0, "Deletes vertex colours on active Mesh");
676
677         if(me->tface) val= 1.0; else val= 0.0;
678         uiDefBut(block, LABEL, 0, "TexFace",                            10,30,70,20, 0, val, 0, 0, 0, "");
679         if(me->tface==NULL) {
680                 uiDefBut(block, BUT, B_MAKE_TFACES, "Make",             80,30,84,19, 0, 0, 0, 0, 0, "Enables the active Mesh's faces for UV coordinate mapping");
681         }
682         else uiDefBut(block, BUT, B_DEL_TFACES, "Delete",       80,30,84,19, 0, 0, 0, 0, 0, "Deletes UV coordinates for active Mesh's faces");
683
684         if(me->msticky) val= 1.0; else val= 0.0;
685         uiDefBut(block, LABEL, 0, "Sticky",                             10,10,70,20, 0, val, 0, 0, 0, "");
686         if(me->msticky==NULL) {
687                 uiDefBut(block, BUT, B_MAKESTICKY, "Make",              80,10,84,19, 0, 0, 0, 0, 0, "Creates Sticky coordinates for the active Mesh from the current camera view background picture");
688         }
689         else uiDefBut(block, BUT, B_DELSTICKY, "Delete",        80,10,84,19, 0, 0, 0, 0, 0, "Deletes Sticky texture coordinates");
690
691         uiBlockEndAlign(block);
692
693         uiDefIDPoinBut(block, test_meshpoin_but, 0, "TexMesh: ",        175,124,230,19, &me->texcomesh, "Enter the name of a Meshblock");
694
695         if(me->key) {
696                 uiBlockBeginAlign(block);
697                 uiDefButS(block, NUM, B_DIFF, "Slurph:",                                175,95,95,19, &(me->key->slurph), -500.0, 500.0, 0, 0, "");
698                 uiDefButS(block, TOG, B_RELKEY, "Relative Keys",                175,75,95,19, &me->key->type, 0, 0, 0, 0, "");
699         }
700
701         uiBlockBeginAlign(block);
702         uiDefBut(block, BUT, B_SLOWERDRAW,"SlowerDraw",                 175,30,95,19, 0, 0, 0, 0, 0, "Displays the active object with all possible edges shown");
703         uiDefBut(block, BUT, B_FASTERDRAW,"FasterDraw",                 175,10,95,19, 0, 0, 0, 0, 0, "Displays the active object faster by omitting some edges when drawing");
704
705         uiBlockBeginAlign(block);
706         uiDefBut(block, BUT,B_DOCENTRE, "Centre",                                       275, 95, 130, 19, 0, 0, 0, 0, 0, "Shifts object data to be centered about object's origin");
707         uiDefBut(block, BUT,B_DOCENTRENEW, "Centre New",                        275, 75, 130, 19, 0, 0, 0, 0, 0, "Shifts object's origin to center of object data");
708         uiDefBut(block, BUT,B_DOCENTRECURSOR, "Centre Cursor",          275, 55, 130, 19, 0, 0, 0, 0, 0, "Shifts object's origin to cursor location");
709
710         uiBlockBeginAlign(block);
711         uiDefButBitS(block, TOG, ME_TWOSIDED, REDRAWVIEW3D, "Double Sided",     275,30,130,19, &me->flag, 0, 0, 0, 0, "Render/display the mesh as double or single sided");
712         uiDefButBitS(block, TOG, ME_NOPUNOFLIP, REDRAWVIEW3D, "No V.Normal Flip",275,10,130,19, &me->flag, 0, 0, 0, 0, "Disables flipping of vertexnormals during render");
713         uiBlockEndAlign(block);
714
715 }
716
717 /* *************************** MODIFIERS ******************************** */
718
719 void do_modifier_panels(unsigned short event)
720 {
721         Object *ob = OBACT;
722
723         switch(event) {
724         case B_MODIFIER_REDRAW:
725                 allqueue(REDRAWBUTSEDIT, 0);
726                 allqueue(REDRAWOOPS, 0);
727                 break;
728
729         case B_MODIFIER_RECALC:
730                 ob->softflag |= OB_SB_RESET;
731                 allqueue(REDRAWBUTSEDIT, 0);
732                 allqueue(REDRAWVIEW3D, 0);
733                 allqueue(REDRAWIMAGE, 0);
734                 allqueue(REDRAWOOPS, 0);
735                 countall();
736                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
737                 break;
738         }
739 }
740
741 static void modifiers_add(void *ob_v, int type)
742 {
743         Object *ob = ob_v;
744         ModifierTypeInfo *mti = modifierType_getInfo(type);
745         
746         if (mti->flags&eModifierTypeFlag_RequiresOriginalData) {
747                 ModifierData *md = ob->modifiers.first;
748
749                 while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) {
750                         md = md->next;
751                 }
752
753                 BLI_insertlinkbefore(&ob->modifiers, md, modifier_new(type));
754         } else {
755                 BLI_addtail(&ob->modifiers, modifier_new(type));
756         }
757
758         BIF_undo_push("Add modifier");
759 }
760
761 static uiBlock *modifiers_add_menu(void *ob_v)
762 {
763         Object *ob = ob_v;
764         uiBlock *block;
765         int i, yco=0;
766         
767         block= uiNewBlock(&curarea->uiblocks, "modifier_add_menu", UI_EMBOSSP, UI_HELV, curarea->win);
768         uiBlockSetButmFunc(block, modifiers_add, ob);
769
770         for (i=eModifierType_None+1; i<NUM_MODIFIER_TYPES; i++) {
771                 ModifierTypeInfo *mti = modifierType_getInfo(i);
772
773                         /* Only allow adding through appropriate other interfaces */
774                 if (ELEM(i, eModifierType_Softbody, eModifierType_Hook)) continue;
775                         
776                 if (    (mti->flags&eModifierTypeFlag_AcceptsCVs) || 
777                                 (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) {
778                         uiDefBut(block, BUTM, B_MODIFIER_RECALC, mti->name,             0, yco-=20, 160, 19, NULL, 0, 0, 1, i, "");
779                 }
780         }
781         
782         uiTextBoundsBlock(block, 50);
783         uiBlockSetDirection(block, UI_DOWN);
784
785         return block;
786 }
787
788 static void modifiers_del(void *ob_v, void *md_v)
789 {
790         Object *ob = ob_v;
791         ModifierData *md;
792
793                 /* It seems on rapid delete it is possible to
794                  * get called twice on same modifier, so make
795                  * sure it is in list.
796                  */
797         for (md=ob->modifiers.first; md; md=md->next)
798                 if (md==md_v)
799                         break;
800         
801         if (!md)
802                 return;
803
804         BLI_remlink(&ob->modifiers, md_v);
805
806         modifier_free(md_v);
807
808         BIF_undo_push("Del modifier");
809 }
810
811 static void modifiers_moveUp(void *ob_v, void *md_v)
812 {
813         Object *ob = ob_v;
814         ModifierData *md = md_v;
815
816         if (md->prev) {
817                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
818
819                 if (mti->type!=eModifierTypeType_OnlyDeform) {
820                         ModifierTypeInfo *nmti = modifierType_getInfo(md->prev->type);
821
822                         if (nmti->flags&eModifierTypeFlag_RequiresOriginalData) {
823                                 error("Cannot move above a modifier requiring original data.");
824                                 return;
825                         }
826                 }
827
828                 BLI_remlink(&ob->modifiers, md);
829                 BLI_insertlink(&ob->modifiers, md->prev->prev, md);
830         }
831
832         BIF_undo_push("Move modifier");
833 }
834
835 static void modifiers_moveDown(void *ob_v, void *md_v)
836 {
837         Object *ob = ob_v;
838         ModifierData *md = md_v;
839
840         if (md->next) {
841                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
842
843                 if (mti->flags&eModifierTypeFlag_RequiresOriginalData) {
844                         ModifierTypeInfo *nmti = modifierType_getInfo(md->next->type);
845
846                         if (nmti->type!=eModifierTypeType_OnlyDeform) {
847                                 error("Cannot move beyond a non-deforming modifier.");
848                                 return;
849                         }
850                 }
851
852                 BLI_remlink(&ob->modifiers, md);
853                 BLI_insertlink(&ob->modifiers, md->next, md);
854         }
855
856         BIF_undo_push("Move modifier");
857 }
858
859 static void modifier_testLatticeObj(char *name, ID **idpp)
860 {
861         ID *id;
862
863         for (id= G.main->object.first; id; id= id->next) {
864                 if( strcmp(name, id->name+2)==0 ) {
865                         if (((Object *)id)->type != OB_LATTICE) {
866                                 error ("Lattice deform object must be a lattice");
867                                 break;
868                         } 
869                         *idpp= id;
870                         return;
871                 }
872         }
873         *idpp= 0;
874 }
875
876 static void modifier_testCurveObj(char *name, ID **idpp)
877 {
878         ID *id;
879
880         for (id= G.main->object.first; id; id= id->next) {
881                 if( strcmp(name, id->name+2)==0 ) {
882                         if (((Object *)id)->type != OB_CURVE) {
883                                 error ("Curve deform object must be a curve");
884                                 break;
885                         } 
886                         *idpp= id;
887                         return;
888                 }
889         }
890         *idpp= 0;
891 }
892
893 static void modifier_testMeshObj(char *name, ID **idpp)
894 {
895         ID *id;
896
897         for (id= G.main->object.first; id; id= id->next) {
898                 if( strcmp(name, id->name+2)==0 ) {
899                         if (((Object *)id)->type != OB_MESH) {
900                                 error ("Boolean modifier object must be a mesh");
901                                 break;
902                         } 
903                         *idpp= id;
904                         return;
905                 }
906         }
907         *idpp= 0;
908 }
909
910 static void modifier_testArmatureObj(char *name, ID **idpp)
911 {
912         ID *id;
913
914         for (id= G.main->object.first; id; id= id->next) {
915                 if( strcmp(name, id->name+2)==0 ) {
916                         if (((Object *)id)->type != OB_ARMATURE) {
917                                 error ("Armature deform object must be an armature");
918                                 break;
919                         } 
920                         *idpp= id;
921                         return;
922                 }
923         }
924         *idpp= 0;
925 }
926
927 static void modifiers_applyModifier(void *obv, void *mdv)
928 {
929         Object *ob = obv;
930         ModifierData *md = mdv;
931         DerivedMesh *dm;
932         DispListMesh *dlm;
933         Mesh *me = ob->data;
934         int converted = 0;
935
936         if (G.obedit) {
937                 error("Modifiers cannot be applied in editmode");
938                 return;
939         } else if (((ID*) ob->data)->us>1) {
940                 error("Modifiers cannot be applied to multi-user data");
941                 return;
942         }
943
944         if (md!=ob->modifiers.first) {
945                 if (!okee("Modifier is not first"))
946                         return;
947         }
948
949         if (ob->type==OB_MESH) {
950                 dm = mesh_create_derived_for_modifier(ob, md);
951                 if (!dm) {
952                         error("Modifier is disabled or returned error, skipping apply");
953                         return;
954                 }
955
956                 dlm= dm->convertToDispListMesh(dm, 0);
957
958                 if ((!me->tface || dlm->tface) || okee("Applying will delete mesh UVs and vertex colors")) {
959                         if ((!me->mcol || dlm->mcol) || okee("Applying will delete mesh vertex colors")) {
960                                 if (dlm->totvert==me->totvert || okee("Applying will delete mesh sticky, keys, and vertex groups")) {
961                                         displistmesh_to_mesh(dlm, me);
962                                         converted = 1;
963                                 }
964                         }
965                 }
966
967                 if (!converted) {
968                         displistmesh_free(dlm);
969                 }
970                 dm->release(dm);
971         } 
972         else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
973                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
974                 Curve *cu = ob->data;
975                 int numVerts;
976                 float (*vertexCos)[3];
977
978                 if (!okee("Apply will only change CV points, not tesselated/bevel vertices"))
979                         return;
980
981                 if (!(md->mode&eModifierMode_Realtime) || (mti->isDisabled && mti->isDisabled(md))) {
982                         error("Modifier is disabled, skipping apply");
983                         return;
984                 }
985
986                 vertexCos = curve_getVertexCos(cu, &cu->nurb, &numVerts);
987                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts);
988                 curve_applyVertexCos(cu, &cu->nurb, vertexCos);
989                 MEM_freeN(vertexCos);
990
991                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
992         }
993         else {
994                 error("Cannot apply modifier for this object type");
995                 return;
996         }
997
998         if (converted) {
999                 BLI_remlink(&ob->modifiers, md);
1000                 modifier_free(md);
1001
1002                 BIF_undo_push("Apply modifier");
1003         }
1004 }
1005
1006 static void modifiers_copyModifier(void *ob_v, void *md_v)
1007 {
1008         Object *ob = ob_v;
1009         ModifierData *md = md_v;
1010         ModifierData *nmd = modifier_new(md->type);
1011
1012         modifier_copyData(md, nmd);
1013
1014         BLI_insertlink(&ob->modifiers, md, nmd);
1015
1016         BIF_undo_push("Copy modifier");
1017 }
1018
1019 static void modifiers_setOnCage(void *ob_v, void *md_v)
1020 {
1021         Object *ob = ob_v;
1022         ModifierData *md;
1023         
1024         for (md=ob->modifiers.first; md; md=md->next)
1025                 if (md!=md_v)
1026                         md->mode &= ~eModifierMode_OnCage;
1027
1028         md = md_v;
1029         md->mode ^= eModifierMode_OnCage;
1030 }
1031
1032 static void modifiers_clearHookOffset(void *ob_v, void *md_v)
1033 {
1034         Object *ob = ob_v;
1035         ModifierData *md = md_v;
1036         HookModifierData *hmd = (HookModifierData*) md;
1037         
1038         if (hmd->object) {
1039                 Mat4Invert(hmd->object->imat, hmd->object->obmat);
1040                 Mat4MulSerie(hmd->parentinv, hmd->object->imat, ob->obmat, NULL, NULL, NULL, NULL, NULL, NULL);
1041                 BIF_undo_push("Clear hook");
1042         }
1043 }
1044
1045 static void modifiers_cursorHookCenter(void *ob_v, void *md_v)
1046 {
1047         Object *ob = ob_v;
1048         ModifierData *md = md_v;
1049         HookModifierData *hmd = (HookModifierData*) md;
1050
1051         if (G.vd) {
1052                 float *curs = give_cursor();
1053                 float bmat[3][3], imat[3][3];
1054
1055                 where_is_object(ob);
1056         
1057                 Mat3CpyMat4(bmat, ob->obmat);
1058                 Mat3Inv(imat, bmat);
1059
1060                 curs= give_cursor();
1061                 hmd->cent[0]= curs[0]-ob->obmat[3][0];
1062                 hmd->cent[1]= curs[1]-ob->obmat[3][1];
1063                 hmd->cent[2]= curs[2]-ob->obmat[3][2];
1064                 Mat3MulVecfl(imat, hmd->cent);
1065
1066                 BIF_undo_push("Hook cursor center");
1067         }
1068 }
1069
1070 static void modifiers_selectHook(void *ob_v, void *md_v)
1071 {
1072         ModifierData *md = md_v;
1073         HookModifierData *hmd = (HookModifierData*) md;
1074
1075         hook_select(hmd);
1076 }
1077
1078 static void modifiers_reassignHook(void *ob_v, void *md_v)
1079 {
1080         ModifierData *md = md_v;
1081         HookModifierData *hmd = (HookModifierData*) md;
1082         float cent[3];
1083         int *indexar, tot, ok;
1084         char name[32];
1085                 
1086         ok= hook_getIndexArray(&tot, &indexar, name, cent);
1087
1088         if (!ok) {
1089                 error("Requires selected vertices or active Vertex Group");
1090         } else {
1091                 if (hmd->indexar) {
1092                         MEM_freeN(hmd->indexar);
1093                 }
1094
1095                 VECCOPY(hmd->cent, cent);
1096                 hmd->indexar = indexar;
1097                 hmd->totindex = tot;
1098         }
1099 }
1100
1101 static void modifiers_convertToReal(void *ob_v, void *md_v)
1102 {
1103         Object *ob = ob_v;
1104         ModifierData *md = md_v;
1105         ModifierData *nmd = modifier_new(md->type);
1106
1107         modifier_copyData(md, nmd);
1108         nmd->mode &= ~eModifierMode_Virtual;
1109
1110         BLI_addhead(&ob->modifiers, nmd);
1111
1112         ob->partype = PAROBJECT;
1113
1114         BIF_undo_push("Modifier convert to real");
1115 }
1116
1117 static void draw_modifier(uiBlock *block, Object *ob, ModifierData *md, int *xco, int *yco, int index, int cageIndex, int lastCageIndex)
1118 {
1119         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1120         uiBut *but;
1121         int isVirtual = md->mode&eModifierMode_Virtual;
1122         int x = *xco, y = *yco, color = md->error?TH_REDALERT:TH_BUT_NEUTRAL;
1123         int editing = (G.obedit==ob);
1124     short height=26, width = 295, buttonWidth = width-120-10;
1125         char str[128];
1126
1127         /* rounded header */
1128         uiBlockSetCol(block, color);
1129                 /* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
1130         uiDefBut(block, ROUNDBOX, 0, "", x-10, y-4, width, 25, NULL, 7.0, 0.0, 
1131                          (!isVirtual && (md->mode&eModifierMode_Expanded))?3:15, 20, ""); 
1132         uiBlockSetCol(block, TH_AUTO);
1133         
1134         /* open/close icon */
1135         if (!isVirtual) {
1136                 uiBlockSetEmboss(block, UI_EMBOSSN);
1137                 uiDefIconButBitI(block, ICONTOG, eModifierMode_Expanded, B_MODIFIER_REDRAW, VICON_DISCLOSURE_TRI_RIGHT, x-10, y-2, 20, 20, &md->mode, 0.0, 0.0, 0.0, 0.0, "Collapse/Expand Modifier");
1138         }
1139
1140         uiBlockSetEmboss(block, UI_EMBOSS);
1141         
1142         if (isVirtual) {
1143                 sprintf(str, "%s parent deform", md->name);
1144                 uiDefBut(block, LABEL, 0, str, x+10, y-1, width-110, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Modifier name"); 
1145
1146                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Make Real", x+width-100, y, 80, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Convert virtual modifier to a real modifier");
1147                 uiButSetFunc(but, modifiers_convertToReal, ob, md);
1148         } else {
1149                 uiBlockBeginAlign(block);
1150                 uiDefBut(block, TEX, B_MODIFIER_REDRAW, "", x+10, y-1, buttonWidth-60, 19, md->name, 0.0, sizeof(md->name)-1, 0.0, 0.0, "Modifier name"); 
1151
1152                         /* Softbody not allowed in this situation, enforce! */
1153                 if (md->type!=eModifierType_Softbody || !(ob->pd && ob->pd->deflect)) {
1154                         uiDefIconButBitI(block, TOG, eModifierMode_Render, B_MODIFIER_RECALC, ICON_SCENE, x+10+buttonWidth-60, y-1, 19, 19,&md->mode, 0, 0, 1, 0, "Enable modifier during rendering");
1155                         uiDefIconButBitI(block, TOG, eModifierMode_Realtime, B_MODIFIER_RECALC, VICON_VIEW3D, x+10+buttonWidth-40, y-1, 19, 19,&md->mode, 0, 0, 1, 0, "Enable modifier during interactive display");
1156                         if (mti->flags&eModifierTypeFlag_SupportsEditmode) {
1157                                 uiDefIconButBitI(block, TOG, eModifierMode_Editmode, B_MODIFIER_RECALC, VICON_EDIT, x+10+buttonWidth-20, y-1, 19, 19,&md->mode, 0, 0, 1, 0, "Enable modifier during Editmode (only if enabled for display)");
1158                         }
1159                 }
1160                 uiBlockEndAlign(block);
1161
1162                 uiBlockSetEmboss(block, UI_EMBOSSR);
1163
1164                 if (ob->type==OB_MESH && modifier_couldBeCage(md) && index<=lastCageIndex) {
1165                         int icon, color;
1166
1167                         if (index==cageIndex) {
1168                                 color = TH_BUT_SETTING;
1169                                 icon = VICON_EDITMODE_HLT;
1170                         } else if (index<cageIndex) {
1171                                 color = TH_BUT_NEUTRAL;
1172                                 icon = VICON_EDITMODE_DEHLT;
1173                         } else {
1174                                 color = TH_BUT_NEUTRAL;
1175                                 icon = ICON_BLANK1;
1176                         }
1177                         uiBlockSetCol(block, color);
1178                         but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, icon, x+width-105, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Apply modifier to editing cage during Editmode");
1179                         uiButSetFunc(but, modifiers_setOnCage, ob, md);
1180                         uiBlockSetCol(block, TH_AUTO);
1181                 }
1182
1183                 uiBlockSetCol(block, TH_BUT_ACTION);
1184
1185                 but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, VICON_MOVE_UP, x+width-75, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Move modifier up in stack");
1186                 uiButSetFunc(but, modifiers_moveUp, ob, md);
1187
1188                 but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, VICON_MOVE_DOWN, x+width-75+20, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Move modifier down in stack");
1189                 uiButSetFunc(but, modifiers_moveDown, ob, md);
1190                 
1191                 uiBlockSetEmboss(block, UI_EMBOSSN);
1192
1193                 but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, VICON_X, x+width-70+40, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Delete modifier");
1194                 uiButSetFunc(but, modifiers_del, ob, md);
1195                 uiBlockSetCol(block, TH_AUTO);
1196         }
1197
1198         uiBlockSetEmboss(block, UI_EMBOSS);
1199
1200         if (isVirtual || !(md->mode&eModifierMode_Expanded)) {
1201                 y -= 18;
1202         } else {
1203                 int cy = y - 8;
1204                 int lx = x + width - 60 - 15;
1205
1206                 if (md->type==eModifierType_Subsurf) {
1207                         height = 86;
1208                 } else if (md->type==eModifierType_Lattice) {
1209                         height = 46;
1210                 } else if (md->type==eModifierType_Curve) {
1211                         height = 46;
1212                 } else if (md->type==eModifierType_Build) {
1213                         height = 86;
1214                 } else if (md->type==eModifierType_Mirror) {
1215                         height = 46;
1216                 } else if (md->type==eModifierType_Decimate) {
1217                         height = 46;
1218                 } else if (md->type==eModifierType_Wave) {
1219                         height = 200;
1220                 } else if (md->type==eModifierType_Armature) {
1221                         height = 46;
1222                 } else if (md->type==eModifierType_Hook) {
1223                         HookModifierData *hmd = (HookModifierData*) md;
1224                         height = 86;
1225                         if (editing)
1226                                 height += 20;
1227                         if(hmd->indexar==NULL)
1228                                 height += 20;
1229                 } else if (md->type==eModifierType_Softbody) {
1230                         height = 26;
1231                 } else if (md->type==eModifierType_Boolean) {
1232                         height = 46;
1233                 }
1234
1235                                                         /* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
1236                 uiDefBut(block, ROUNDBOX, 0, "", x-10, y-height-2, width, height-2, NULL, 5.0, 0.0, 12, 40, ""); 
1237
1238                 y -= 18;
1239
1240                 if (!isVirtual) {
1241                         uiBlockBeginAlign(block);
1242                         but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Apply",  lx,(cy-=19),60,19, 0, 0, 0, 0, 0, "Apply the current modifier and remove from the stack");
1243                         uiButSetFunc(but, modifiers_applyModifier, ob, md);
1244                         if (md->type!=eModifierType_Softbody) {
1245                                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Copy",   lx,(cy-=19),60,19, 0, 0, 0, 0, 0, "Duplicate the current modifier at the same position in the stack");
1246                                 uiButSetFunc(but, modifiers_copyModifier, ob, md);
1247                         }
1248                         uiBlockEndAlign(block);
1249                 }
1250
1251                 lx = x + 10;
1252                 cy = y + 10 - 1;
1253                 uiBlockBeginAlign(block);
1254                 if (md->type==eModifierType_Subsurf) {
1255                         SubsurfModifierData *smd = (SubsurfModifierData*) md;
1256                         char subsurfmenu[]="Subsurf Type%t|Catmull-Clark%x0|Simple Subdiv.%x1";
1257                         uiDefButS(block, MENU, B_MODIFIER_RECALC, subsurfmenu,          lx,(cy-=19),buttonWidth,19, &smd->subdivType, 0, 0, 0, 0, "Selects type of subdivision algorithm.");
1258                         uiDefButS(block, NUM, B_MODIFIER_RECALC, "Levels:",             lx, (cy-=19), buttonWidth,19, &smd->levels, 1, 6, 0, 0, "Number subdivisions to perform");
1259                         uiDefButS(block, NUM, B_MODIFIER_RECALC, "Render Levels:",              lx, (cy-=19), buttonWidth,19, &smd->renderLevels, 1, 6, 0, 0, "Number subdivisions to perform when rendering");
1260
1261                         /* Disabled until non-EM DerivedMesh implementation is complete */
1262
1263                         /*
1264                         uiDefButBitS(block, TOG, eSubsurfModifierFlag_Incremental, B_MODIFIER_RECALC, "Incremental", lx, (cy-=19),90,19,&smd->flags, 0, 0, 0, 0, "Use incremental calculation, even outside of mesh mode");
1265                         uiDefButBitS(block, TOG, eSubsurfModifierFlag_DebugIncr, B_MODIFIER_RECALC, "Debug", lx+90, cy,buttonWidth-90,19,&smd->flags, 0, 0, 0, 0, "Visualize the subsurf incremental calculation, for debugging effect of other modifiers");
1266                         */
1267
1268                         uiDefButBitS(block, TOG, eSubsurfModifierFlag_ControlEdges, B_MODIFIER_RECALC, "Optimal Draw", lx, (cy-=19), buttonWidth,19,&smd->flags, 0, 0, 0, 0, "Skip drawing/rendering of interior subdivided edges");
1269                 } else if (md->type==eModifierType_Lattice) {
1270                         LatticeModifierData *lmd = (LatticeModifierData*) md;
1271                         uiDefIDPoinBut(block, modifier_testLatticeObj, B_CHANGEDEP, "Ob: ",     lx, (cy-=19), buttonWidth,19, &lmd->object, "Lattice object to deform with");
1272                 } else if (md->type==eModifierType_Curve) {
1273                         CurveModifierData *cmd = (CurveModifierData*) md;
1274                         uiDefIDPoinBut(block, modifier_testCurveObj, B_CHANGEDEP, "Ob: ", lx, (cy-=19), buttonWidth,19, &cmd->object, "Curve object to deform with");
1275                 } else if (md->type==eModifierType_Build) {
1276                         BuildModifierData *bmd = (BuildModifierData*) md;
1277                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Start:", lx, (cy-=19), buttonWidth,19, &bmd->start, 1.0, MAXFRAMEF, 100, 0, "Specify the start frame of the effect");
1278                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Length:", lx, (cy-=19), buttonWidth,19, &bmd->length, 1.0, MAXFRAMEF, 100, 0, "Specify the total time the build effect requires");
1279                         uiDefButI(block, TOG, B_MODIFIER_RECALC, "Randomize", lx, (cy-=19), buttonWidth,19, &bmd->randomize, 0, 0, 1, 0, "Randomize the faces or edges during build.");
1280                         uiDefButI(block, NUM, B_MODIFIER_RECALC, "Seed:", lx, (cy-=19), buttonWidth,19, &bmd->seed, 1.0, MAXFRAMEF, 100, 0, "Specify the seed for random if used.");
1281                 } else if (md->type==eModifierType_Mirror) {
1282                         MirrorModifierData *mmd = (MirrorModifierData*) md;
1283                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Merge Limit:", lx, (cy-=19), buttonWidth,19, &mmd->tolerance, 0.0, 1, 0, 0, "Distance from axis within which mirrored vertices are merged");
1284                         uiDefButI(block, ROW, B_MODIFIER_RECALC, "X",   lx, (cy-=19), 20,19, &mmd->axis, 1, 0, 0, 0, "Specify the axis to mirror about");
1285                         uiDefButI(block, ROW, B_MODIFIER_RECALC, "Y",   lx+20, cy, 20,19, &mmd->axis, 1, 1, 0, 0, "Specify the axis to mirror about");
1286                         uiDefButI(block, ROW, B_MODIFIER_RECALC, "Z",   lx+40, cy, 20,19, &mmd->axis, 1, 2, 0, 0, "Specify the axis to mirror about");
1287                 } else if (md->type==eModifierType_Decimate) {
1288                         DecimateModifierData *dmd = (DecimateModifierData*) md;
1289                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Percent:",    lx,(cy-=19),buttonWidth,19, &dmd->percent, 0.0, 1.0, 0, 0, "Defines the percentage of triangles to reduce to");
1290                         sprintf(str, "Face Count: %d", dmd->faceCount);
1291                         uiDefBut(block, LABEL, 1, str,  lx, (cy-=19), 160,19, NULL, 0.0, 0.0, 0, 0, "Displays the current number of faces in the decimated mesh");
1292                 } else if (md->type==eModifierType_Wave) {
1293                         WaveModifierData *wmd = (WaveModifierData*) md;
1294                         uiDefButBitS(block, TOG, WAV_X, B_MODIFIER_RECALC, "X",         lx,(cy-=19),45,19, &wmd->flag, 0, 0, 0, 0, "Enable X axis motion");
1295                         uiDefButBitS(block, TOG, WAV_Y, B_MODIFIER_RECALC, "Y",         lx+45,cy,45,19, &wmd->flag, 0, 0, 0, 0, "Enable Y axis motion");
1296                         uiDefButBitS(block, TOG, WAV_CYCL, B_MODIFIER_RECALC, "Cycl",   lx+90,cy,buttonWidth-90,19, &wmd->flag, 0, 0, 0, 0, "Enable cyclic wave effect");
1297                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Time sta:",   lx,(cy-=19),buttonWidth,19, &wmd->timeoffs, -1000.0, 1000.0, 100, 0, "Specify startingframe of the wave");
1298                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Lifetime:",   lx,(cy-=19),buttonWidth,19, &wmd->lifetime,  -1000.0, 1000.0, 100, 0, "Specify the lifespan of the wave");
1299                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Damptime:",   lx,(cy-=19),buttonWidth,19, &wmd->damp,  -1000.0, 1000.0, 100, 0, "Specify the dampingtime of the wave");
1300                         cy -= 19;
1301                         uiBlockBeginAlign(block);
1302                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Sta x:",              lx,(cy-=19),113,19, &wmd->startx, -100.0, 100.0, 100, 0, "Starting position for the X axis");
1303                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Sta y:",              lx+115,cy,105,19, &wmd->starty, -100.0, 100.0, 100, 0, "Starting position for the Y axis");
1304                         uiBlockBeginAlign(block);
1305                         uiDefButF(block, NUMSLI, B_MODIFIER_RECALC, "Speed:",   lx,(cy-=19),220,19, &wmd->speed, -2.0, 2.0, 0, 0, "Specify the wave speed");
1306                         uiDefButF(block, NUMSLI, B_MODIFIER_RECALC, "Heigth:",  lx,(cy-=19),220,19, &wmd->height, -2.0, 2.0, 0, 0, "Specify the amplitude of the wave");
1307                         uiDefButF(block, NUMSLI, B_MODIFIER_RECALC, "Width:",   lx,(cy-=19),220,19, &wmd->width, 0.0, 5.0, 0, 0, "Specify the width of the wave");
1308                         uiDefButF(block, NUMSLI, B_MODIFIER_RECALC, "Narrow:",  lx,(cy-=19),220,19, &wmd->narrow, 0.0, 10.0, 0, 0, "Specify how narrow the wave follows");
1309                 } else if (md->type==eModifierType_Armature) {
1310                         ArmatureModifierData *amd = (ArmatureModifierData*) md;
1311                         uiDefIDPoinBut(block, modifier_testArmatureObj, B_CHANGEDEP, "Ob: ", lx, (cy-=19), buttonWidth,19, &amd->object, "Armature object to deform with");
1312                 } else if (md->type==eModifierType_Hook) {
1313                         HookModifierData *hmd = (HookModifierData*) md;
1314                         uiDefButF(block, NUM, B_MODIFIER_RECALC, "Falloff: ",           lx, (cy-=19), buttonWidth,19, &hmd->falloff, 0.0, 100.0, 100, 0, "If not zero, the distance from hook where influence ends");
1315                         uiDefButF(block, NUMSLI, B_MODIFIER_RECALC, "Force: ",          lx, (cy-=19), buttonWidth,19, &hmd->force, 0.0, 1.0, 100, 0, "Set relative force of hook");
1316                         uiDefIDPoinBut(block, test_obpoin_but, B_CHANGEDEP, "Ob: ", lx, (cy-=19), buttonWidth,19, &hmd->object, "Parent Object for hook, also recalculates and clears offset"); 
1317                         if(hmd->indexar==NULL)
1318                                 uiDefBut(block, TEX, B_MODIFIER_RECALC, "VGroup: ",             lx, (cy-=19), buttonWidth,19, &hmd->name, 0.0, 31.0, 0, 0, "Vertex Group name");
1319                         uiBlockBeginAlign(block);
1320                         but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Reset",          lx, (cy-=19), 80,19,                    NULL, 0.0, 0.0, 0, 0, "Recalculate and clear offset (transform) of hook");
1321                         uiButSetFunc(but, modifiers_clearHookOffset, ob, md);
1322                         but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Recenter",       lx+80, cy, buttonWidth-80,19,   NULL, 0.0, 0.0, 0, 0, "Sets hook center to cursor position");
1323                         uiButSetFunc(but, modifiers_cursorHookCenter, ob, md);
1324
1325                         if (editing) {
1326                                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Select",                 lx, (cy-=19), 80,19, NULL, 0.0, 0.0, 0, 0, "Selects effected vertices on mesh");
1327                                 uiButSetFunc(but, modifiers_selectHook, ob, md);
1328                                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Reassign",               lx+80, cy, buttonWidth-80,19, NULL, 0.0, 0.0, 0, 0, "Reassigns selected vertices to hook");
1329                                 uiButSetFunc(but, modifiers_reassignHook, ob, md);
1330                         }
1331                 } else if (md->type==eModifierType_Softbody) {
1332                         uiDefBut(block, LABEL, 1, "See Softbody panel.",        lx, (cy-=19), buttonWidth,19, NULL, 0.0, 0.0, 0, 0, "");
1333                 } else if (md->type==eModifierType_Boolean) {
1334                         BooleanModifierData *bmd = (BooleanModifierData*) md;
1335                         uiDefButI(block, MENU, B_MODIFIER_RECALC, "Operation%t|Intersect%x0|Union%x1|Difference%x2",    lx,(cy-=19),buttonWidth,19, &bmd->operation, 0.0, 1.0, 0, 0, "Boolean operation to perform");
1336                         uiDefIDPoinBut(block, modifier_testMeshObj, B_CHANGEDEP, "Ob: ", lx, (cy-=19), buttonWidth,19, &bmd->object, "Mesh object to use for boolean operation");
1337                 }
1338                 uiBlockEndAlign(block);
1339
1340                 y-=height;
1341         }
1342
1343         if (md->error) {
1344                 char str[512];
1345
1346                 y -= 20;
1347
1348                 uiBlockSetCol(block, color);
1349                                         /* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
1350                 uiDefBut(block, ROUNDBOX, 0, "", x-10, y, width, 20, NULL, 5.0, 0.0, 15, 40, ""); 
1351                 uiBlockSetCol(block, TH_AUTO);
1352
1353                 sprintf(str, "Modifier Error: %s", md->error);
1354                 uiDefBut(block, LABEL, B_NOP, str, x+15, y+15, width-35, 19, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1355         }
1356
1357         y -= 3+6;
1358
1359         *xco = x;
1360         *yco = y;
1361 }
1362
1363 static void editing_panel_modifiers(Object *ob)
1364 {
1365         ModifierData *md;
1366         uiBlock *block;
1367         char str[64];
1368         int xco, yco, i, lastCageIndex, cageIndex = modifiers_getCageIndex(ob, &lastCageIndex);
1369
1370         block= uiNewBlock(&curarea->uiblocks, "editing_panel_modifiers", UI_EMBOSS, UI_HELV, curarea->win);
1371         if( uiNewPanel(curarea, block, "Modifiers", "Editing", 640, 0, 318, 204)==0) return;
1372
1373         uiNewPanelHeight(block, 204);
1374
1375         uiDefBlockBut(block, modifiers_add_menu, ob, "Add Modifier", 0, 190, 130, 20, "Add a new modifier");
1376
1377         sprintf(str, "To: %s", ob->id.name+2);
1378         uiDefBut(block, LABEL, 1, str,  140, 190, 150, 20, NULL, 0.0, 0.0, 0, 0, "Object whose modifier stack is being edited");
1379
1380         xco = 0;
1381         yco = 160;
1382
1383         md = modifiers_getVirtualModifierList(ob);
1384
1385         for (i=0; md; i++, md=md->next) {
1386                 draw_modifier(block, ob, md, &xco, &yco, i, cageIndex, lastCageIndex);
1387                 if (md->mode&eModifierMode_Virtual) i--;
1388         }
1389         
1390         if(yco < 0) uiNewPanelHeight(block, 204-yco);
1391 }
1392
1393 static char *make_key_menu(Key *key)
1394 {
1395         KeyBlock *kb;
1396         int index= 1;
1397         char *str, item[64];
1398
1399         for (kb = key->block.first; kb; kb=kb->next, index++);
1400         str= MEM_mallocN(index*40, "key string");
1401         str[0]= 0;
1402         
1403         index= 1;
1404         for (kb = key->block.first; kb; kb=kb->next, index++) {
1405                 sprintf (item,  "|%s%%x%d", kb->name, index);
1406                 strcat(str, item);
1407         }
1408         
1409         return str;
1410 }
1411
1412 static void editing_panel_shapes(Object *ob)
1413 {
1414         uiBlock *block;
1415         Key *key= NULL;
1416         KeyBlock *kb;
1417         int icon;
1418         char *strp;
1419         
1420         block= uiNewBlock(&curarea->uiblocks, "editing_panel_shapes", UI_EMBOSS, UI_HELV, curarea->win);
1421         uiNewPanelTabbed("Modifiers", "Editing");
1422         if( uiNewPanel(curarea, block, "Shapes", "Editing", 640, 0, 318, 204)==0) return;
1423         
1424         uiDefBut(block, BUT, B_ADDKEY, "Add Shape Key" ,        10, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "Add new Shape Key");
1425                 
1426         key= ob_get_key(ob);
1427         if(key==NULL) 
1428                 return;
1429         
1430         uiDefButS(block, TOG, B_RELKEY, "Relative",             170, 180,140,20, &key->type, 0, 0, 0, 0, "Makes Shape Keys relative");
1431
1432         kb= BLI_findlink(&key->block, ob->shapenr-1);
1433         if(kb==NULL) {
1434                 ob->shapenr= 1;
1435                 kb= key->block.first;
1436         }
1437
1438         uiBlockBeginAlign(block);
1439         if(ob->shapeflag & OB_SHAPE_LOCK) icon= ICON_PIN_HLT; else icon= ICON_PIN_DEHLT;
1440         uiDefIconButBitC(block, TOG, OB_SHAPE_LOCK, B_LOCKKEY, icon, 10,150,25,20, &ob->shapeflag, 0, 0, 0, 0, "Always show the current Shape for this Object");
1441         uiSetButLock(G.obedit==ob, "Unable to perform in EditMode");
1442         uiDefIconBut(block, BUT, B_PREVKEY, ICON_TRIA_LEFT,             35,150,20,20, NULL, 0, 0, 0, 0, "Previous Shape Key");
1443         strp= make_key_menu(key);
1444         uiDefButC(block, MENU, B_SETKEY, strp,                                  55,150,20,20, &ob->shapenr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
1445         MEM_freeN(strp);
1446         uiDefIconBut(block, BUT, B_NEXTKEY, ICON_TRIA_RIGHT,    75,150,20,20, NULL, 0, 0, 0, 0, "Next Shape Key");
1447         uiClearButLock();
1448         uiDefBut(block, TEX, B_NAMEKEY, "",                                             95, 150, 190, 20, kb->name, 0.0, 31.0, 0, 0, "Current Shape Key name");
1449         uiDefIconBut(block, BUT, B_DELKEY, ICON_X,                              285,150,25,20, 0, 0, 0, 0, 0, "Deletes current Shape Key");
1450         uiBlockEndAlign(block);
1451
1452         if(key->type && (ob->shapeflag & OB_SHAPE_LOCK)==0 && ob->shapenr!=1) {
1453                 uiBlockBeginAlign(block);
1454                 make_rvk_slider(block, key, ob->shapenr-1,                      10, 120, 150, 20, "Key value, when used it inserts an animation curve point");
1455                 uiDefButF(block, NUM, B_REDR, "Min ",                           160,120, 75, 20, &kb->slidermin, -10.0, 10.0, 100, 1, "Minumum for slider");
1456                 uiDefButF(block, NUM, B_REDR, "Max ",                           235,120, 75, 20, &kb->slidermax, -10.0, 10.0, 100, 1, "Maximum for slider");
1457         }
1458 }
1459
1460 /* *************************** FONT ******************************** */
1461
1462 static short give_vfontnr(VFont *vfont)
1463 {
1464         VFont *vf;
1465         short nr= 1;
1466
1467         vf= G.main->vfont.first;
1468         while(vf) {
1469                 if(vf==vfont) return nr;
1470                 nr++;
1471                 vf= vf->id.next;
1472         }
1473         return -1;
1474 }
1475
1476 static VFont *give_vfontpointer(int nr) /* nr= button */
1477 {
1478         VFont *vf;
1479         short tel= 1;
1480
1481         vf= G.main->vfont.first;
1482         while(vf) {
1483                 if(tel==nr) return vf;
1484                 tel++;
1485                 vf= vf->id.next;
1486         }
1487         return G.main->vfont.first;
1488 }
1489
1490 VFont *exist_vfont(char *str)
1491 {
1492         VFont *vf;
1493
1494         vf= G.main->vfont.first;
1495         while(vf) {
1496                 if(strcmp(vf->name, str)==0) return vf;
1497                 vf= vf->id.next;
1498         }
1499         return 0;
1500 }
1501
1502 static char *give_vfontbutstr(void)
1503 {
1504         VFont *vf;
1505         int len= 0;
1506         char *str, di[FILE_MAXDIR], fi[FILE_MAXFILE];
1507
1508         vf= G.main->vfont.first;
1509         while(vf) {
1510                 strcpy(di, vf->name);
1511                 BLI_splitdirstring(di, fi);
1512                 len+= strlen(fi)+4;
1513                 vf= vf->id.next;
1514         }
1515
1516         str= MEM_callocN(len+21, "vfontbutstr");
1517         strcpy(str, "FONTS %t");
1518         vf= G.main->vfont.first;
1519         while(vf) {
1520
1521                 if(vf->id.us==0) strcat(str, "|0 ");
1522                 else strcat(str, "|   ");
1523
1524                 strcpy(di, vf->name);
1525                 BLI_splitdirstring(di, fi);
1526
1527                 strcat(str, fi);
1528                 vf= vf->id.next;
1529         }
1530         return str;
1531 }
1532
1533 static void load_buts_vfont(char *name)
1534 {
1535         VFont *vf;
1536         Curve *cu;
1537
1538         if(OBACT && OBACT->type==OB_FONT) cu= OBACT->data;
1539         else return;
1540
1541         vf= exist_vfont(name);
1542         if(vf==0) {
1543                 vf= load_vfont(name);
1544                 if(vf==0) return;
1545         }
1546         else id_us_plus((ID *)vf);
1547         
1548         switch(cu->curinfo.flag & CU_STYLE) {
1549                 case CU_BOLD:
1550                         if(cu->vfontb) cu->vfontb->id.us--;
1551                         cu->vfontb= vf;
1552                         break;
1553                 case CU_ITALIC:
1554                         if(cu->vfonti) cu->vfonti->id.us--;             
1555                         cu->vfonti= vf;
1556                         break;                                          
1557                 case (CU_BOLD|CU_ITALIC):
1558                         if(cu->vfontbi) cu->vfontbi->id.us--;
1559                         cu->vfontbi= vf;
1560                         break;
1561                 default:
1562                         if(cu->vfont) cu->vfont->id.us--;
1563                         cu->vfont= vf;
1564                         break;                                          
1565         }       
1566
1567         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1568         BIF_undo_push("Load vector font");
1569         allqueue(REDRAWVIEW3D, 0);
1570         allqueue(REDRAWBUTSEDIT, 0);
1571 }
1572
1573 static void set_unicode_text_fs(char *file)
1574 {
1575         if (file > 0) paste_unicodeText(file); 
1576 }
1577
1578 void do_fontbuts(unsigned short event)
1579 {
1580         Curve *cu;
1581         VFont *vf;
1582         Object *ob;
1583         ScrArea *sa;
1584         char str[80];
1585         int ctevt;
1586         char *ctmenu;
1587         DynStr *ds;
1588         int i, style=0;
1589
1590         ob= OBACT;
1591
1592         switch(event) {
1593         case B_MAKEFONT:
1594                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1595                 allqueue(REDRAWVIEW3D, 0);
1596                 break;
1597
1598         case B_STYLETOSELU:     
1599         case B_STYLETOSELB:
1600         case B_STYLETOSELI:
1601                 switch (event) {
1602                         case B_STYLETOSELU: style = CU_UNDERLINE; break;
1603                         case B_STYLETOSELB: style = CU_BOLD; break;                     
1604                         case B_STYLETOSELI: style = CU_ITALIC; break;
1605                 }
1606                 if (style_to_sel(style, ((Curve*)ob->data)->curinfo.flag & style)) {
1607                         text_to_curve(ob, 0);
1608                         makeDispListCurveTypes(ob, 0);
1609                         allqueue(REDRAWVIEW3D, 0);
1610                 }
1611                 allqueue(REDRAWBUTSEDIT, 0);
1612                 break;          
1613                 
1614         case B_FASTFONT:
1615                 if (G.obedit) {
1616                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1617                         allqueue(REDRAWVIEW3D, 0);
1618                 }
1619                 break;
1620         case B_INSTB:
1621                 cu= ob->data;
1622                 if (cu->totbox < 256) {
1623                         for (i = cu->totbox; i>cu->actbox; i--) cu->tb[i]= cu->tb[i-1];
1624                         cu->tb[cu->actbox]= cu->tb[cu->actbox-1];
1625                         cu->actbox++;
1626                         cu->totbox++;
1627                         allqueue(REDRAWBUTSEDIT, 0);
1628                         allqueue(REDRAWVIEW3D, 0);
1629                         text_to_curve(ob, 0);
1630                         makeDispListCurveTypes(ob, 0);
1631                 }
1632                 else {
1633                         error("Do you really need that many text frames?");
1634                 }
1635                 break;
1636         case B_DELTB:
1637                 cu= ob->data;
1638                 if (cu->totbox > 1) {
1639                         for (i = cu->actbox-1; i < cu->totbox; i++) cu->tb[i]= cu->tb[i+1];
1640                         cu->totbox--;
1641                         cu->actbox--;
1642                         allqueue(REDRAWBUTSEDIT, 0);
1643                         allqueue(REDRAWVIEW3D, 0);
1644                         text_to_curve(ob, 0);
1645                         makeDispListCurveTypes(ob, 0);
1646                 }
1647                 break;
1648         case B_TOUPPER:
1649                 to_upper();
1650                 break;
1651         case B_LOADFONT:
1652                 vf= give_vfontpointer(G.buts->texnr);
1653                 if(vf && vf->id.prev!=vf->id.next) strcpy(str, vf->name);
1654                 else strcpy(str, U.fontdir);
1655
1656                 sa= closest_bigger_area();
1657                 areawinset(sa->win);
1658
1659                 activate_fileselect(FILE_SPECIAL, "SELECT FONT", str, load_buts_vfont);
1660
1661                 break;
1662         case B_PACKFONT:
1663                 if (ob) {
1664                         cu= ob->data;
1665                         if(cu && cu->vfont) {
1666                                 if (cu->vfont->packedfile) {
1667                                         if (G.fileflags & G_AUTOPACK) {
1668                                                 if (okee("Disable AutoPack ?")) {
1669                                                         G.fileflags &= ~G_AUTOPACK;
1670                                                 }
1671                                         }
1672
1673                                         if ((G.fileflags & G_AUTOPACK) == 0) {
1674                                                 if (unpackVFont(cu->vfont, PF_ASK) == RET_OK) {
1675                                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1676                                                         allqueue(REDRAWVIEW3D, 0);
1677                                                 }
1678                                         }
1679                                 } else {
1680                                         cu->vfont->packedfile = newPackedFile(cu->vfont->name);
1681                                 }
1682                         }
1683                 }
1684                 allqueue(REDRAWHEADERS, 0);
1685                 allqueue(REDRAWBUTSEDIT, 0);
1686                 break;
1687
1688         case B_LOAD3DTEXT:
1689                 if (!G.obedit) { error("Only in editmode!"); return; }
1690                 if (G.obedit->type != OB_FONT) return;  
1691                 activate_fileselect(FILE_SPECIAL, "Open Text File", G.sce, load_3dtext_fs);
1692                 break;
1693                 
1694         case B_LOREM:
1695                 if (!G.obedit) { error("Only in editmode!"); return; }
1696                 if (G.obedit->type != OB_FONT) return;  
1697                 add_lorem();
1698                 
1699                 break;          
1700
1701         case B_SETFONT:
1702                 if(ob) {
1703                         cu= ob->data;
1704
1705                         vf= give_vfontpointer(G.buts->texnr);
1706                         if(vf) {
1707                                 id_us_plus((ID *)vf);
1708
1709                                 switch(cu->curinfo.flag & CU_STYLE) {
1710                                         case CU_BOLD:
1711                                                 cu->vfontb->id.us--;
1712                                                 cu->vfontb= vf;
1713                                                 break;
1714                                         case CU_ITALIC:
1715                                                 cu->vfonti->id.us--;
1716                                                 cu->vfonti= vf;
1717                                                 break;                                          
1718                                         case (CU_BOLD|CU_ITALIC):
1719                                                 cu->vfontbi->id.us--;
1720                                                 cu->vfontbi= vf;
1721                                                 break;
1722                                         default:
1723                                                 cu->vfont->id.us--;
1724                                                 cu->vfont= vf;
1725                                                 break;                                          
1726                                 }
1727                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1728
1729                                 BIF_undo_push("Set vector font");
1730                                 allqueue(REDRAWVIEW3D, 0);
1731                                 allqueue(REDRAWBUTSEDIT, 0);
1732                         }
1733                 }
1734                 break;
1735                 
1736         case B_SETCHAR:
1737                 G.charmin = 0x0000;
1738                 G.charmax = 0xffff;
1739                 if(G.charstart < 0)
1740                         G.charstart = 0;        
1741                 if(G.charstart > (0xffff - 12*6))
1742                         G.charstart = 0xffff - (12*6);
1743                 allqueue(REDRAWBUTSEDIT, 0);
1744                 break;
1745                 
1746         case B_SETUPCHAR:
1747                 G.charstart = G.charstart - (12*6);
1748                 if(G.charstart < 0)
1749                         G.charstart = 0;        
1750                 if(G.charstart < G.charmin)
1751                         G.charstart = G.charmin;
1752                 allqueue(REDRAWBUTSEDIT, 0);
1753                 break;
1754                 
1755         case B_SETCAT:
1756                 // Create new dynamic string
1757                 ds = BLI_dynstr_new();
1758                 
1759                 // Fill the dynamic string with entries
1760                 for(i=0;i<104;i++)
1761                 {
1762                         BLI_dynstr_append(ds, "|");
1763                         BLI_dynstr_append(ds, uctabname[i].name);
1764                 }
1765                 
1766                 // Create the menu string from dyn string
1767                 ctmenu = BLI_dynstr_get_cstring(ds);
1768                 
1769                 // Call the popup menu
1770                 ctevt = pupmenu_col(ctmenu, 40);
1771                 G.charstart = uctabname[ctevt-1].start;
1772                 G.charmin = uctabname[ctevt-1].start;
1773                 G.charmax = uctabname[ctevt-1].end;
1774
1775                 // Free all data
1776                 BLI_dynstr_free(ds);
1777                 MEM_freeN(ctmenu);
1778
1779                 // And refresh
1780                 allqueue(REDRAWVIEW3D, 0);
1781                 allqueue(REDRAWBUTSEDIT, 0);
1782                 
1783                 break;  
1784                 
1785         case B_SETDOWNCHAR:
1786                 G.charstart = G.charstart + (12*6);
1787                 if(G.charstart > (0xffff - 12*6))
1788                         G.charstart = 0xffff - (12*6);
1789                 if(G.charstart > G.charmax - 12*6)
1790                         G.charstart = G.charmax - 12*6;
1791                 allqueue(REDRAWBUTSEDIT, 0);
1792                 break;
1793                 
1794         case B_SETUNITEXT:
1795                 sa= closest_bigger_area();
1796                 areawinset(sa->win);
1797
1798                 if(ob==G.obedit) {
1799                         activate_fileselect(FILE_SPECIAL, "Open Text File", G.sce, set_unicode_text_fs);
1800                 }
1801                 break;          
1802                 
1803         case B_TEXTONCURVE:
1804                 if(ob) {
1805                         cu= ob->data;
1806                         if(cu->textoncurve && cu->textoncurve->type!=OB_CURVE) {
1807                                 error("Only Curve Objects");
1808                                 cu->textoncurve= 0;
1809                                 allqueue(REDRAWBUTSEDIT, 0);
1810                         }
1811                         DAG_scene_sort(G.scene); // makes new dag
1812                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1813                         allqueue(REDRAWVIEW3D, 0);
1814                 }
1815         }
1816 }
1817
1818 static void editing_panel_char_type(Object *ob, Curve *cu)
1819 {
1820         uiBlock *block;
1821
1822         block= uiNewBlock(&curarea->uiblocks, "editing_panel_char_type", UI_EMBOSS, UI_HELV, curarea->win);
1823         uiNewPanelTabbed("Font", "Editing");
1824         if(uiNewPanel(curarea, block, "Char", "Editing", 640, 0, 318, 204)==0) 
1825                 return;
1826
1827         // Set the selected font
1828         G.selfont = cu->vfont;
1829         
1830         uiDefIconBut(block, BUT, B_SETUNITEXT, ICON_TEXT,       0,210,20,20, 0, 0, 0, 0, 0, "Load Unicode Text file");
1831
1832         // Unicode categorization selection button
1833         uiDefBut(block, BUT, B_SETCAT, "Unicode Table", 22,210,226,20, 0, 0, 0, 0, 0, "Select Unicode Table");
1834         uiDefButI(block, NUM, /*B_SETUPCHAR*/ 0, "", 250,210,50,20, &G.charstart, 0, 0xffff, 0, 0, "UT");
1835
1836         // Character selection button
1837         uiDefBut(block, CHARTAB, B_SETCHAR, "", 0, 0, 264, 200, 0, 0, 0, 0, 0, "Select character");
1838
1839         // Buttons to change the max, min
1840         uiDefButI(block, BUT, B_SETUPCHAR, "U", 280, 185, 15, 15, &G.charstart, 0, 0xffff, 0, 0, "Scroll character table up");
1841         uiDefButI(block, BUT, B_SETDOWNCHAR, "D", 280, 0, 15, 15, &G.charstart, 0, 0xffff, 0, 0, "Scroll character table down");
1842 }
1843
1844 static void editing_panel_font_type(Object *ob, Curve *cu)
1845 {
1846         uiBlock *block;
1847         char *strp;
1848         static int packdummy = 0;
1849         char str[32];
1850
1851         block= uiNewBlock(&curarea->uiblocks, "editing_panel_font_type", UI_EMBOSS, UI_HELV, curarea->win);
1852         if(uiNewPanel(curarea, block, "Font", "Editing", 640, 0, 470, 204)==0) return;
1853
1854         switch(cu->curinfo.flag & CU_STYLE) {
1855                 case CU_BOLD:
1856                         G.buts->texnr= give_vfontnr(cu->vfontb);
1857                         break;
1858                 case CU_ITALIC:
1859                         G.buts->texnr= give_vfontnr(cu->vfonti);
1860                         break;                                          
1861                 case (CU_BOLD|CU_ITALIC):
1862                         G.buts->texnr= give_vfontnr(cu->vfontbi);
1863                         break;
1864                 default:
1865                         G.buts->texnr= give_vfontnr(cu->vfont);
1866                         break;                                          
1867         }       
1868
1869         strp= give_vfontbutstr();
1870 //      vfd= cu->vfont->data;
1871
1872         uiDefBut(block, BUT,B_LOADFONT, "Load", 480,188,68,20, 0, 0, 0, 0, 0, "Load a new font");
1873         uiDefButS(block, MENU, B_SETFONT, strp, 550,188,220,20, &G.buts->texnr, 0, 0, 0, 0, "Change font for object");
1874
1875         if (cu->vfont->packedfile) {
1876                 packdummy = 1;
1877         } else {
1878                 packdummy = 0;
1879         }
1880         uiDefIconButI(block, TOG|BIT|0, B_PACKFONT, ICON_PACKAGE,       772,188,20,20, &packdummy, 0, 0, 0, 0, "Pack/Unpack this font");
1881
1882         /* This doesn't work anyway */
1883 //      uiDefBut(block, LABEL, 0, vfd->name,  480, 165,314,20, 0, 0, 0, 0, 0, "Postscript name of the font");
1884
1885         uiDefBut(block, BUT, B_LOAD3DTEXT, "Insert Text", 480, 165, 90, 20, 0, 0, 0, 0, 0, "Insert text file at cursor");
1886         uiDefBut(block, BUT, B_LOREM, "Lorem", 575, 165, 70, 20, 0, 0, 0, 0, 0, "Insert a paragraph of Lorem Ipsum at cursor"); 
1887         uiDefButC(block, TOG|BIT|2,B_STYLETOSELU, "U",          727,165,20,20, &(cu->curinfo.flag), 0,0, 0, 0, "");     
1888         uiBlockBeginAlign(block);
1889         uiDefButBitC(block, TOG, CU_BOLD, B_STYLETOSELB, "B",           752,165,20,20, &(cu->curinfo.flag), 0,0, 0, 0, "");
1890         uiDefButBitC(block, TOG, CU_ITALIC, B_STYLETOSELI, "i",         772,165,20,20, &(cu->curinfo.flag), 0, 0, 0, 0, "");    
1891         uiBlockEndAlign(block);
1892
1893         MEM_freeN(strp);
1894
1895         uiBlockBeginAlign(block);
1896         uiDefButS(block, ROW,B_MAKEFONT, "Left",                480,135,47,20, &cu->spacemode, 0.0,0.0, 0, 0, "Left align the text from the object centre");
1897         uiDefButS(block, ROW,B_MAKEFONT, "Center",              527,135,47,20, &cu->spacemode, 0.0,1.0, 0, 0, "Middle align the text from the object centre");
1898         uiDefButS(block, ROW,B_MAKEFONT, "Right",               574,135,47,20, &cu->spacemode, 0.0,2.0, 0, 0, "Right align the text from the object centre");
1899         uiDefButS(block, ROW,B_MAKEFONT, "Justify",             621,135,47,20, &cu->spacemode, 0.0,3.0, 0, 0, "Fill completed lines to maximum textframe width by expanding whitespace");
1900         uiDefButS(block, ROW,B_MAKEFONT, "Flush",               668,135,47,20, &cu->spacemode, 0.0,4.0, 0, 0, "Fill every line to maximum textframe width, distributing space among all characters");   
1901         uiDefBut(block, BUT, B_TOUPPER, "ToUpper",              715,135,78,20, 0, 0, 0, 0, 0, "Toggle between upper and lower case in editmode");
1902         uiBlockEndAlign(block);
1903         uiDefButBitS(block, TOG, CU_FAST, B_FASTFONT, "Fast Edit",              715,105,78,20, &cu->flag, 0, 0, 0, 0, "Don't fill polygons while editing");     
1904
1905         uiDefIDPoinBut(block, test_obpoin_but, B_TEXTONCURVE, "TextOnCurve:",   480,105,220,19, &cu->textoncurve, "Apply a deforming curve to the text");
1906         uiDefBut(block, TEX,REDRAWVIEW3D, "Ob Family:", 480,84,220,19, cu->family, 0.0, 20.0, 0, 0, "Blender uses font from selfmade objects");
1907
1908         uiBlockBeginAlign(block);
1909         uiDefButF(block, NUM,B_MAKEFONT, "Size:",               480,56,155,20, &cu->fsize, 0.1,10.0, 10, 0, "Size of the text");
1910         uiDefButF(block, NUM,B_MAKEFONT, "Linedist:",   640,56,155,20, &cu->linedist, 0.0,10.0, 10, 0, "Distance between text lines");
1911         uiDefButF(block, NUM,B_MAKEFONT, "Word spacing:",       795,56,155,20, &cu->wordspace, 0.0,10.0, 10, 0, "Distance factor between words");               
1912         uiDefButF(block, NUM,B_MAKEFONT, "Spacing:",    480,34,155,20, &cu->spacing, 0.0,10.0, 10, 0, "Spacing of individual characters");
1913         uiDefButF(block, NUM,B_MAKEFONT, "X offset:",   640,34,155,20, &cu->xof, -50.0,50.0, 10, 0, "Horizontal position from object centre");
1914         uiDefButF(block, NUM,B_MAKEFONT, "UL position:",        795,34,155,20, &cu->ulpos, -0.2,0.8, 10, 0, "Vertical position of underline");                  
1915         uiDefButF(block, NUM,B_MAKEFONT, "Shear:",              480,12,155,20, &cu->shear, -1.0,1.0, 10, 0, "Italic angle of the characters");
1916         uiDefButF(block, NUM,B_MAKEFONT, "Y offset:",   640,12,155,20, &cu->yof, -50.0,50.0, 10, 0, "Vertical position from object centre");
1917         uiDefButF(block, NUM,B_MAKEFONT, "UL height:",  795,12,155,20, &cu->ulheight, 0.01,0.5, 10, 0, "Thickness of underline");                               
1918         uiBlockEndAlign(block); 
1919         
1920         sprintf(str, "%d TextFrame: ", cu->totbox);
1921         uiBlockBeginAlign(block);
1922         uiDefButI(block, NUM, REDRAWVIEW3D, str, 805, 188, 145, 20, &cu->actbox, 1.0, cu->totbox, 0, 10, "Textbox to show settings for");
1923         uiDefBut(block, BUT,B_INSTB, "Insert", 805, 168, 72, 20, 0, 0, 0, 0, 0, "Insert a new text frame after the current one");
1924         uiDefBut(block, BUT,B_DELTB, "Delete", 877, 168, 73, 20, 0, 0, 0, 0, 0, "Delete current text frame and shift the others up");   
1925         uiDefButF(block, NUM,B_MAKEFONT, "X:", 805, 148, 72, 20, &(cu->tb[cu->actbox-1].x), -50.0, 50.0, 10, 0, "Horizontal offset of text frame");
1926         uiDefButF(block, NUM,B_MAKEFONT, "Y:", 877, 148, 73, 20, &(cu->tb[cu->actbox-1].y), -50.0, 50.0, 10, 0, "Horizontal offset of text frame");     
1927         uiDefButF(block, NUM,B_MAKEFONT, "Width:", 805, 128, 145, 20, &(cu->tb[cu->actbox-1].w), 0.0, 50.0, 10, 0, "Horizontal offset of text frame");
1928         uiDefButF(block, NUM,B_MAKEFONT, "Height:", 805, 108, 145, 20, &(cu->tb[cu->actbox-1].h), 0.0, 50.0, 10, 0, "Horizontal offset of text frame");         
1929         uiBlockEndAlign(block);
1930 }
1931
1932
1933 /* *************************** CURVE ******************************** */
1934
1935
1936 void do_curvebuts(unsigned short event)
1937 {
1938         extern Nurb *lastnu;
1939         extern ListBase editNurb;  /* from editcurve */
1940         Object *ob;
1941         Curve *cu;
1942         Nurb *nu;
1943
1944         ob= OBACT;
1945         if(ob==0) return;
1946
1947         switch(event) {
1948
1949         case B_CONVERTPOLY:
1950         case B_CONVERTBEZ:
1951         case B_CONVERTBSPL:
1952         case B_CONVERTCARD:
1953         case B_CONVERTNURB:
1954                 if(G.obedit) {
1955                         setsplinetype(event-B_CONVERTPOLY);
1956                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1957                         allqueue(REDRAWVIEW3D, 0);
1958                 }
1959                 break;
1960         case B_UNIFU:
1961         case B_ENDPU:
1962         case B_BEZU:
1963         case B_UNIFV:
1964         case B_ENDPV:
1965         case B_BEZV:
1966                 if(G.obedit) {
1967                         nu= editNurb.first;
1968                         while(nu) {
1969                                 if(isNurbsel(nu)) {
1970                                         if((nu->type & 7)==CU_NURBS) {
1971                                                 if(event<B_UNIFV) {
1972                                                         nu->flagu &= 1;
1973                                                         nu->flagu += ((event-B_UNIFU)<<1);
1974                                                         makeknots(nu, 1, nu->flagu>>1);
1975                                                 }
1976                                                 else if(nu->pntsv>1) {
1977                                                         nu->flagv &= 1;
1978                                                         nu->flagv += ((event-B_UNIFV)<<1);
1979                                                         makeknots(nu, 2, nu->flagv>>1);
1980                                                 }
1981                                         }
1982                                 }
1983                                 nu= nu->next;
1984                         }
1985                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1986                         allqueue(REDRAWVIEW3D, 0);
1987                 }
1988                 break;
1989         case B_SETWEIGHT:
1990                 if(G.obedit) {
1991                         weightflagNurb(1, editbutweight, 0);
1992                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1993                         allqueue(REDRAWVIEW3D, 0);
1994                 }
1995                 break;
1996         case B_SETW1:
1997                 editbutweight= 1.0;
1998                 scrarea_queue_winredraw(curarea);
1999                 break;
2000         case B_SETW2:
2001                 editbutweight= sqrt(2.0)/4.0;
2002                 scrarea_queue_winredraw(curarea);
2003                 break;
2004         case B_SETW3:
2005                 editbutweight= 0.25;
2006                 scrarea_queue_winredraw(curarea);
2007                 break;
2008         case B_SETW4:
2009                 editbutweight= sqrt(0.5);
2010                 scrarea_queue_winredraw(curarea);
2011                 break;
2012         case B_SETORDER:
2013                 if(G.obedit) {
2014                         nu= lastnu;
2015                         if(nu && (nu->type & 7)==CU_NURBS ) {
2016                                 if(nu->orderu>nu->pntsu) {
2017                                         nu->orderu= nu->pntsu;
2018                                         scrarea_queue_winredraw(curarea);
2019                                 }
2020                                 makeknots(nu, 1, nu->flagu>>1);
2021                                 if(nu->orderv>nu->pntsv) {
2022                                         nu->orderv= nu->pntsv;
2023                                         scrarea_queue_winredraw(curarea);
2024                                 }
2025                                 makeknots(nu, 2, nu->flagv>>1);
2026                         }
2027                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2028                         allqueue(REDRAWVIEW3D, 0);
2029                 }
2030                 break;
2031         case B_SUBSURFTYPE:
2032                 /* fallthrough */
2033         case B_MAKEDISP:
2034                 if(G.vd) {
2035                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2036                         allqueue(REDRAWVIEW3D, 0);
2037                         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
2038                 }
2039                 break;
2040
2041         case B_SUBDIVCURVE:
2042                 subdivideNurb();
2043                 break;
2044         case B_SPINNURB:
2045                 if( (G.obedit==NULL) || (G.obedit->type!=OB_SURF) || (G.vd==NULL) ||
2046                         ((G.obedit->lay & G.vd->lay) == 0) ) return;
2047                 spinNurb(0, 0);
2048                 countall();
2049                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2050                 allqueue(REDRAWVIEW3D, 0);
2051                 break;
2052         case B_CU3D:        /* allow 3D curve */
2053                 if(G.obedit) {
2054                         cu= G.obedit->data;
2055                         nu= editNurb.first;
2056                         while(nu) {
2057                                 nu->type &= ~CU_2D;
2058                                 if((cu->flag & CU_3D)==0) nu->type |= CU_2D;
2059                                 test2DNurb(nu);
2060                                 nu= nu->next;
2061                         }
2062                 }
2063                 if(ob->type==OB_CURVE) {
2064                         cu= ob->data;
2065                         nu= cu->nurb.first;
2066                         while(nu) {
2067                                 nu->type &= ~CU_2D;
2068                                 if((cu->flag & CU_3D)==0) nu->type |= CU_2D;
2069                                 test2DNurb(nu);
2070                                 nu= nu->next;
2071                         }
2072                 }
2073                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2074                 allqueue(REDRAWVIEW3D, 0);
2075                 break;
2076         case B_SETRESOLU:
2077                 if(ob->type==OB_CURVE) {
2078                         cu= ob->data;
2079                         if(ob==G.obedit) nu= editNurb.first;
2080                         else nu= cu->nurb.first;
2081
2082                         while(nu) {
2083                                 nu->resolu= cu->resolu;
2084                                 nu= nu->next;
2085                         }
2086                 }
2087
2088                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2089                 allqueue(REDRAWVIEW3D, 0);
2090
2091                 break;
2092         }
2093 }
2094
2095 static void editing_panel_curve_tools(Object *ob, Curve *cu)
2096 {
2097         Nurb *nu;
2098         extern ListBase editNurb;  /* from editcurve */
2099         extern Nurb *lastnu;
2100         uiBlock *block;
2101         short *sp;
2102
2103         block= uiNewBlock(&curarea->uiblocks, "editing_panel_curve_tools", UI_EMBOSS, UI_HELV, curarea->win);
2104         if(uiNewPanel(curarea, block, "Curve Tools", "Editing", 640, 0, 318, 204)==0) return;
2105
2106         uiDefBut(block, LABEL, 0, "Make Knots",562,173,102, 18, 0, 0, 0, 0, 0, "");
2107
2108         if(ob->type==OB_CURVE) {
2109                 uiDefBut(block, LABEL, 0, "Convert",    463,173,72, 18, 0, 0, 0, 0, 0, "");
2110                 uiBlockBeginAlign(block);
2111                 uiDefBut(block, BUT,B_CONVERTPOLY,"Poly",               467,152,72, 18, 0, 0, 0, 0, 0, "Converts selected into regular Polygon vertices");
2112                 uiDefBut(block, BUT,B_CONVERTBEZ,"Bezier",              467,132,72, 18, 0, 0, 0, 0, 0, "Converts selected to Bezier triples");
2113                 uiDefBut(block, BUT,B_CONVERTNURB,"Nurb",               467,112,72, 18, 0, 0, 0, 0, 0, "Converts selected to Nurbs Points");
2114         }
2115         uiBlockBeginAlign(block);
2116         uiDefBut(block, BUT,B_UNIFU,"Uniform U",        565,152,102, 18, 0, 0, 0, 0, 0, "Nurbs only; interpolated result doesn't go to end points in U");
2117         uiDefBut(block, BUT,B_UNIFV,"V",                        670,152,50, 18, 0, 0, 0, 0, 0, "Nurbs only; interpolated result doesn't go to end points in V");
2118         uiDefBut(block, BUT,B_ENDPU,"Endpoint U",       565,132,102, 18, 0, 0, 0, 0, 0, "Nurbs only; interpolated result is forced to end points in U");
2119         uiDefBut(block, BUT,B_ENDPV,"V",                        670,132,50, 18, 0, 0, 0, 0, 0, "Nurbs only; interpolated result is forced to end points in V");
2120         uiDefBut(block, BUT,B_BEZU,"Bezier U",          565,112,102, 18, 0, 0, 0, 0, 0, "Nurbs only; make knots array mimic a Bezier in U");
2121         uiDefBut(block, BUT,B_BEZV,"V",                         670,112,50, 18, 0, 0, 0, 0, 0, "Nurbs only; make knots array mimic a Bezier in V");
2122         uiBlockEndAlign(block);
2123
2124         uiDefBut(block, BUT,B_SETWEIGHT,"Set Weight",   465,11,95,49, 0, 0, 0, 0, 0, "Nurbs only; set weight for select points");
2125
2126         uiBlockBeginAlign(block);
2127         uiDefButF(block, NUM,0,"Weight:",               565,36,102,22, &editbutweight, 0.01, 100.0, 10, 0, "The weight you can assign");
2128         uiDefBut(block, BUT,B_SETW1,"1.0",              670,36,50,22, 0, 0, 0, 0, 0, "");
2129         uiDefBut(block, BUT,B_SETW2,"sqrt(2)/4",565,11,55,20, 0, 0, 0, 0, 0, "");
2130         uiDefBut(block, BUT,B_SETW3,"0.25",             620,11,45,20, 0, 0, 0, 0, 0, "");
2131         uiDefBut(block, BUT,B_SETW4,"sqrt(0.5)",665,11,55,20, 0, 0, 0, 0, 0, "");
2132         uiBlockEndAlign(block);
2133
2134         if(ob==G.obedit) {
2135                 nu= lastnu;
2136                 if(nu==NULL) nu= editNurb.first;
2137                 if(nu) {
2138                         uiBlockBeginAlign(block);
2139                         sp= &(nu->orderu);
2140                         uiDefButS(block, NUM, B_SETORDER, "Order U:", 565,90,102, 19, sp, 2.0, 6.0, 0, 0, "Nurbs only; the amount of control points involved");
2141                         sp= &(nu->orderv);
2142                         uiDefButS(block, NUM, B_SETORDER, "V:",         670,90,50, 19, sp, 2.0, 6.0, 0, 0, "Nurbs only; the amount of control points involved");
2143                         sp= &(nu->resolu);
2144                         uiDefButS(block, NUM, B_MAKEDISP, "Resol U:", 565,70,102, 19, sp, 1.0, 1024.0, 0, 0, "The amount of new points interpolated per control vertex pair");
2145                         sp= &(nu->resolv);
2146                         uiDefButS(block, NUM, B_MAKEDISP, "V:",         670,70,50, 19, sp, 1.0, 1024.0, 0, 0, "The amount of new points interpolated per control vertex pair");
2147                 }
2148         }
2149
2150
2151 }
2152
2153 static void editing_panel_curve_tools1(Object *ob, Curve *cu)
2154 {
2155         uiBlock *block;
2156
2157         block= uiNewBlock(&curarea->uiblocks, "editing_panel_curve_tools1", UI_EMBOSS, UI_HELV, curarea->win);
2158         if(uiNewPanel(curarea, block, "Curve Tools1", "Editing", 960, 0, 318, 204)==0) return;
2159
2160         uiDefBut(block, BUT, B_SUBDIVCURVE, "Subdivide", 400,180,150,20, 0, 0, 0, 0, 0, "Subdivide selected");
2161         if(ob->type==OB_SURF) {
2162                 uiDefBut(block, BUT, B_SPINNURB, "Spin",         400,160,150,20, 0, 0, 0, 0, 0, "Spin selected 360 degrees");
2163         }
2164         uiBlockBeginAlign(block);
2165         uiDefBut(block, BUT,B_HIDE,             "Hide",                 400,120,150,18, 0, 0, 0, 0, 0, "Hides selected faces");
2166         uiDefBut(block, BUT,B_REVEAL,   "Reveal",               400,100,150,18, 0, 0, 0, 0, 0, "Reveals selected faces");
2167         uiDefBut(block, BUT,B_SELSWAP,  "Select Swap",  400,80,150,18, 0, 0, 0, 0, 0, "Selects unselected faces, and deselects selected faces");
2168         uiBlockEndAlign(block);
2169
2170         uiDefButF(block, NUM,   REDRAWVIEW3D, "NSize:", 400, 40, 150, 19, &G.scene->editbutsize, 0.001, 1.0, 10, 0, "Normal size for drawing");
2171 }
2172
2173 /* for curve, surf and font! */
2174 static void editing_panel_curve_type(Object *ob, Curve *cu)
2175 {
2176         uiBlock *block;
2177
2178         block= uiNewBlock(&curarea->uiblocks, "editing_panel_curve_type", UI_EMBOSS, UI_HELV, curarea->win);
2179         if(uiNewPanel(curarea, block, "Curve and Surface", "Editing", 320, 0, 318, 204)==0) return;
2180
2181         uiDefButBitS(block, TOG, CU_UV_ORCO, 0, "UV Orco",                                      600,160,150,19, &cu->flag, 0, 0, 0, 0, "Forces to use UV coordinates for texture mapping 'orco'");
2182         if(ob->type==OB_SURF)
2183                 uiDefButBitS(block, TOG, CU_NOPUNOFLIP, REDRAWVIEW3D, "No Puno Flip",   600,140,150,19, &cu->flag, 0, 0, 0, 0, "Don't flip vertex normals while render");
2184
2185         uiBlockBeginAlign(block);
2186         uiDefBut(block, BUT,B_DOCENTRE, "Centre",                                       600, 115, 55, 19, 0, 0, 0, 0, 0, "Shifts object data to be centered about object's origin");
2187         uiDefBut(block, BUT,B_DOCENTRENEW, "Centre New",                        655, 115, 95, 19, 0, 0, 0, 0, 0, "Shifts object's origin to center of object data");
2188         uiDefBut(block, BUT,B_DOCENTRECURSOR, "Centre Cursor",          600, 95, 150, 19, 0, 0, 0, 0, 0, "Shifts object's origin to cursor location");
2189         uiBlockEndAlign(block);
2190
2191         if(cu->key) {
2192                 /* uiDefButS(block, NUM, B_DIFF, "Slurph:",                     600,25,140,19, &(cu->key->slurph), -500.0, 500.0,0,0); ,""*/
2193                 uiDefButS(block, TOG, B_RELKEY, "Relative Keys",        600, 72,150,19, &cu->key->type, 0, 0, 0, 0, "");
2194         }
2195
2196
2197         if(ob->type!=OB_SURF) {
2198         
2199                 if(ob->type==OB_CURVE) {
2200                         extern float prlen;             // buttons_object.c, should be moved....
2201                         char str[32];
2202                         
2203                         sprintf(str, "%.4f", prlen);
2204                         uiDefBut(block, BUT, B_PRINTLEN,                "PrintLen",     600,135,75,19, 0, 0, 0, 0, 0, "");
2205                         uiDefBut(block, LABEL, 0, str,                                          675,135,75,19, 0, 1.0, 0, 0, 0, "");
2206                         
2207                         uiBlockBeginAlign(block);
2208                         uiDefButS(block, NUM, B_RECALCPATH, "PathLen:",                 600,50,150,19, &cu->pathlen, 1.0, MAXFRAMEF, 0, 0, "If no speed Ipo was set, the amount of frames of the path");
2209                         uiDefButBitS(block, TOG, CU_PATH, B_RECALCPATH, "CurvePath",    600,30,75,19 , &cu->flag, 0, 0, 0, 0, "Enables curve to become translation path");
2210                         uiDefButBitS(block, TOG, CU_FOLLOW, REDRAWVIEW3D, "CurveFollow",675,30,75,19, &cu->flag, 0, 0, 0, 0, "Makes curve path children to rotate along path");
2211                         uiDefButBitS(block, TOG, CU_STRETCH, B_CURVECHECK, "CurveStretch", 600,10,150,19, &cu->flag, 0, 0, 0, 0, "Option for curve-deform: makes deformed child to stretch along entire path");
2212                         uiDefButBitS(block, TOG, CU_OFFS_PATHDIST, REDRAWVIEW3D, "PathDist Offs", 600,-10,150,19, &cu->flag, 0, 0, 0, 0, "Children will use TimeOffs value as path distance offset");
2213
2214                         uiBlockEndAlign(block);
2215                 }
2216
2217                 uiBlockBeginAlign(block);
2218                 uiDefButS(block, NUM, B_MAKEDISP, "DefResolU:", 760,160,120,19, &cu->resolu, 1.0, 1024.0, 0, 0, "Default resolution");
2219                 uiDefBut(block, BUT, B_SETRESOLU, "Set",                880,160,30,19, 0, 0, 0, 0, 0, "Set resolution for interpolation");
2220
2221                 uiBlockBeginAlign(block);
2222                 uiDefButF(block, NUM, B_MAKEDISP, "Width:",             760,90,150,19, &cu->width, 0.0, 2.0, 1, 0, "Make interpolated result thinner or fatter");
2223                 uiDefButF(block, NUM, B_MAKEDISP, "Extrude:",           760,70,150,19, &cu->ext1, 0.0, 5.0, 10, 0, "Curve extrusion size when not using a bevel object");
2224                 uiDefButF(block, NUM, B_MAKEDISP, "Bevel Depth:",               760,50,150,19, &cu->ext2, 0.0, 2.0, 1, 0, "Bevel depth when not using a bevel object");
2225                 uiDefButS(block, NUM, B_MAKEDISP, "BevResol:",  760,30,150,19, &cu->bevresol, 0.0, 10.0, 0, 0, "Bevel resolution when depth is non-zero and not using a bevel object");
2226                 uiDefIDPoinBut(block, test_obcurpoin_but, B_CHANGEDEP, "BevOb:",                760,10,150,19, &cu->bevobj, "Curve object name that defines the bevel shape");
2227                 uiDefIDPoinBut(block, test_obcurpoin_but, B_CHANGEDEP, "TaperOb:",              760,-10,150,19, &cu->taperobj, "Curve object name that defines the taper (width)");
2228
2229                 uiBlockBeginAlign(block);
2230                 uiBlockSetCol(block, TH_BUT_SETTING1);
2231                 uiDefButBitS(block, TOG, CU_BACK, B_MAKEDISP, "Back",   760,130,50,19, &cu->flag, 0, 0, 0, 0, "Draw filled back for curves");
2232                 uiDefButBitS(block, TOG, CU_FRONT, B_MAKEDISP, "Front",810,130,50,19, &cu->flag, 0, 0, 0, 0, "Draw filled front for curves");
2233                 uiDefButBitS(block, TOG, CU_3D, B_CU3D, "3D",           860,130,50,19, &cu->flag, 0, 0, 0, 0, "Allow Curve Object to be 3d, it doesn't fill then");
2234         }
2235 }
2236
2237
2238 /* *************************** CAMERA ******************************** */
2239
2240
2241 static void editing_panel_camera_type(Object *ob, Camera *cam)
2242 {
2243         uiBlock *block;
2244         float grid=0.0;
2245
2246         if(G.vd) grid= G.vd->grid;
2247         if(grid<1.0) grid= 1.0;
2248
2249         block= uiNewBlock(&curarea->uiblocks, "editing_panel_camera_type", UI_EMBOSS, UI_HELV, curarea->win);
2250         if(uiNewPanel(curarea, block, "Camera", "Editing", 320, 0, 318, 204)==0) return;
2251
2252         if(cam->type==CAM_ORTHO)
2253                 uiDefButF(block, NUM,REDRAWVIEW3D, "Scale:", 470,178,160,20, &cam->ortho_scale, 0.01, 1000.0, 50, 0, "Specify the ortho scaling of the used camera");
2254         else
2255                 uiDefButF(block, NUM,REDRAWVIEW3D, "Lens:", 470,178,160,20, &cam->lens, 1.0, 250.0, 100, 0, "Specify the lens of the camera");
2256
2257         uiBlockBeginAlign(block);
2258         uiDefButF(block, NUM,REDRAWVIEW3D, "ClipSta:", 470,147,160,20, &cam->clipsta, 0.001*grid, 100.0*grid, 10, 0, "Specify the startvalue of the the field of view");
2259         uiDefButF(block, NUM,REDRAWVIEW3D, "ClipEnd:", 470,125,160,20, &cam->clipend, 1.0, 5000.0*grid, 100, 0, "Specify the endvalue of the the field of view");
2260         uiBlockEndAlign(block);
2261
2262         uiDefButF(block, NUM,REDRAWVIEW3D, "DrawSize:", 470,90,160,20, &cam->drawsize, 0.1*grid, 10.0, 10, 0, "Specify the drawsize of the camera");
2263
2264         uiDefButS(block, TOG, REDRAWVIEW3D, "Ortho",            470,29,61,60, &cam->type, 0, 0, 0, 0, "Render orthogonally");
2265         uiBlockBeginAlign(block);
2266         uiDefButBitS(block, TOG, CAM_SHOWLIMITS, REDRAWVIEW3D, "ShowLimits", 533,59,97,30, &cam->flag, 0, 0, 0, 0, "Draw the field of view");
2267         uiDefButBitS(block, TOG, CAM_SHOWMIST, REDRAWVIEW3D, "Show Mist",  533,29,97,30, &cam->flag, 0, 0, 0, 0, "Draw a line that indicates the mist area");
2268         uiBlockEndAlign(block);
2269 }
2270
2271 /* yafray: extra camera panel to set Depth-of-Field parameters */
2272 static void editing_panel_camera_yafraydof(Object *ob, Camera *cam)
2273 {
2274         uiBlock *block;
2275         char *mst1, *mst2;
2276
2277         block= uiNewBlock(&curarea->uiblocks, "editing_panel_camera_yafraydof", UI_EMBOSS, UI_HELV, curarea->win);
2278         uiNewPanelTabbed("Camera", "Editing");
2279         if(uiNewPanel(curarea, block, "Yafray DoF", "Editing", 320, 0, 318, 204)==0) return;
2280
2281         uiDefButF(block, NUM, REDRAWVIEW3D, "DoFDist:", 10, 147, 180, 20, &cam->YF_dofdist, 0.0, 5000.0, 50, 0, "Sets distance to point of focus (use camera 'ShowLimits' to make visible in 3Dview)");
2282         uiDefButF(block, NUM, B_DIFF, "Aperture:", 10, 125, 180, 20, &cam->YF_aperture, 0.0, 2.0, 1, 0, "Sets lens aperture, the larger, the more blur (use small values, 0 is no DoF)");
2283
2284         uiDefButBitS(block, TOG, CAM_YF_NO_QMC, B_DIFF, "Random sampling", 10, 90, 180, 20, &cam->flag, 0, 0, 0, 0, "Use noisy random Lens sampling instead of QMC");
2285
2286         uiDefBut(block, LABEL, 0, "Bokeh", 10, 60, 180, 19, 0, 0.0, 0.0, 0, 0, "");
2287         mst1 = "Bokeh Type%t|Disk1%x0|Disk2%x1|Triangle%x2|Square%x3|Pentagon%x4|Hexagon%x5|Ring%x6";
2288         uiDefButS(block, MENU, B_REDR, mst1, 10, 40, 89, 20, &cam->YF_bkhtype, 0.0, 0.0, 0, 0, "Sets Bokeh type");
2289         
2290         if ((cam->YF_bkhtype!=0) && (cam->YF_bkhtype!=6)) {
2291                 mst2 = "Bokeh Bias%t|Uniform%x0|Center%x1|Edge%x2";
2292                 uiDefButS(block, MENU, B_REDR, mst2, 100, 40, 90, 20, &cam->YF_bkhbias, 0.0, 0.0, 0, 0, "Sets Bokeh bias");
2293                 if (cam->YF_bkhtype>1)
2294                         uiDefButF(block, NUM, B_DIFF, "Rotation:", 10, 15, 180, 20, &cam->YF_bkhrot, 0.0, 360.0, 100, 0, "Shape rotation amount in degrees");
2295         }
2296
2297 }
2298
2299 /* **************************** CAMERA *************************** */
2300
2301 void do_cambuts(unsigned short event)
2302 {
2303         Object *ob;
2304         Camera *cam;
2305         
2306         ob= OBACT;
2307         if (ob==0) return;
2308         cam= ob->data;
2309
2310         switch(event) {
2311         case 0:
2312                 ;
2313                 break;
2314         }
2315 }
2316
2317 /* *************************** MBALL ******************************** */
2318
2319 void do_mballbuts(unsigned short event)
2320 {
2321         switch(event) {
2322         case B_RECALCMBALL:
2323                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
2324                 allqueue(REDRAWVIEW3D, 0);
2325                 break;
2326         }
2327 }
2328
2329 static void editing_panel_mball_type(Object *ob, MetaBall *mb)
2330 {
2331         uiBlock *block;
2332
2333         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mball_type", UI_EMBOSS, UI_HELV, curarea->win);
2334         if(uiNewPanel(curarea, block, "MetaBall", "Editing", 320, 0, 318, 204)==0) return;
2335
2336         ob= find_basis_mball(ob);
2337         mb= ob->data;
2338
2339         uiBlockBeginAlign(block);
2340         uiDefButF(block, NUM, B_RECALCMBALL, "Wiresize:", 470,178,250,19, &mb->wiresize, 0.05, 1.0, 1, 0, "Polygonization resolution in 3d window");
2341         uiDefButF(block, NUM, B_NOP, "Rendersize:", 470,158,250,19, &mb->rendersize, 0.05, 1.0, 1, 0, "Polygonization resolution in rendering");
2342         uiDefButF(block, NUM, B_RECALCMBALL, "Threshold:", 470,138,250,19, &mb->thresh, 0.0001, 5.0, 1, 0, "Defines influence of meta elements");
2343
2344         uiBlockBeginAlign(block);
2345         uiBlockSetCol(block, TH_BUT_SETTING1);
2346         uiDefBut(block, LABEL, 0, "Update:", 471,108,120,19, 0, 0, 0, 0, 0, "");
2347         uiDefButS(block, ROW, B_DIFF, "Always", 471, 85, 120, 19, &mb->flag, 0.0, 0.0, 0, 0, "While editing, always updates");
2348         uiDefButS(block, ROW, B_DIFF, "Half Res", 471, 65, 120, 19, &mb->flag, 0.0, 1.0, 0, 0, "While editing, updates in half resolution");
2349         uiDefButS(block, ROW, B_DIFF, "Fast", 471, 45, 120, 19, &mb->flag, 0.0, 2.0, 0, 0, "While editing, updates without polygonization");
2350         uiDefButS(block, ROW, B_DIFF, "Never", 471, 25, 120, 19, &mb->flag, 0.0, 3.0, 0, 0, "While editing, doesn't update");
2351
2352 }
2353
2354 static void editing_panel_mball_tools(Object *ob, MetaBall *mb)
2355 {
2356         extern MetaElem *lastelem;
2357         uiBlock *block;
2358
2359         block= uiNewBlock(&curarea->uiblocks, "editing_panel_mball_tools", UI_EMBOSS, UI_HELV, curarea->win);
2360         if( uiNewPanel(curarea, block, "MetaBall tools", "Editing", 640, 0, 318, 204)==0) return;
2361
2362         if(ob==G.obedit && lastelem) {
2363                 uiBlockBeginAlign(block);
2364                 uiDefButF(block, NUM, B_RECALCMBALL, "Stiffness:", 750,178,250,19, &lastelem->s, 0.0, 10.0, 1, 0, "Stiffness for active meta");
2365                 if(lastelem->type!=MB_BALL)
2366                 uiDefButF(block, NUM, B_RECALCMBALL, "dx:", 750,158,250,19, &lastelem->expx, 0.0, 20.0, 1, 0, "X size for active meta");
2367                 if((lastelem->type!=MB_BALL)&&(lastelem->type!=MB_TUBE))
2368                 uiDefButF(block, NUM, B_RECALCMBALL, "dy:", 750,138,250,19, &lastelem->expy, 0.0, 20.0, 1, 0, "Y size for active meta");
2369
2370                 if((lastelem->type==MB_CUBE)||(lastelem->type==MB_ELIPSOID))
2371                 uiDefButF(block, NUM, B_RECALCMBALL, "dz:", 750,118,250,19, &lastelem->expz, 0.0, 20.0, 1, 0, "Z size for active meta");
2372                 uiBlockEndAlign(block);
2373
2374                 uiDefButS(block, ROW, B_RECALCMBALL, "Ball", 753,83,60,19, &lastelem->type, 1.0, 0.0, 0, 0, "Draw active meta as Ball");
2375                 uiBlockBeginAlign(block);
2376                 uiDefButS(block, ROW, B_RECALCMBALL, "Tube", 753,62,60,19, &lastelem->type, 1.0, 4.0, 0, 0, "Draw active meta as Ball");
2377                 uiDefButS(block, ROW, B_RECALCMBALL, "Plane", 814,62,60,19, &lastelem->type, 1.0, 5.0, 0, 0, "Draw active meta as Plane");
2378                 uiDefButS(block, ROW, B_RECALCMBALL, "Elipsoid", 876,62,60,19, &lastelem->type, 1.0, 6.0, 0, 0, "Draw active meta as Ellipsoid");
2379                 uiDefButS(block, ROW, B_RECALCMBALL, "Cube", 938,62,60,19, &lastelem->type, 1.0, 7.0, 0, 0, "Draw active meta as Cube");
2380                 uiBlockEndAlign(block);
2381
2382                 uiBlockBeginAlign(block);
2383                 uiDefButBitS(block, TOG, MB_NEGATIVE, B_RECALCMBALL, "Negative",753,16,125,19, &lastelem->flag, 0, 0, 0, 0, "Make active meta creating holes");
2384                 uiDefButBitS(block, TOG, MB_HIDE, B_RECALCMBALL, "Hide",878,16,125,19, &lastelem->flag, 0, 0, 0, 0, "Make active meta invisible");
2385                 uiBlockEndAlign(block);
2386
2387         }
2388
2389 }
2390
2391
2392 /* *************************** LATTICE ******************************** */
2393
2394 void do_latticebuts(unsigned short event)
2395 {
2396         Object *ob;
2397         Lattice *lt;
2398
2399         ob= OBACT;
2400         if(ob->type!=OB_LATTICE) return;
2401
2402         switch(event) {
2403         case B_REGULARLAT:
2404                 if(ob) {
2405                         lt = ob->data;
2406                         if(ob==G.obedit) resizelattice(editLatt, lt->opntsu, lt->opntsv, lt->opntsw, NULL);
2407                         else resizelattice(ob->data, lt->opntsu, lt->opntsv, lt->opntsw, NULL);
2408                         ob->softflag |= OB_SB_REDO;
2409                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2410                         allqueue(REDRAWVIEW3D, 0);
2411                 }
2412         case B_RESIZELAT:
2413                 if(ob) {
2414                         lt = ob->data;
2415                         resizelattice(ob->data, lt->opntsu, lt->opntsv, lt->opntsw, ob);
2416                         ob->softflag |= OB_SB_REDO;
2417                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2418                         allqueue(REDRAWVIEW3D, 0);
2419                 }
2420                 break;
2421         case B_DRAWLAT:
2422                 allqueue(REDRAWVIEW3D, 0);
2423                 break;
2424         case B_LATTCHANGED:
2425
2426                 lt= ob->data;
2427                 if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
2428
2429                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2430
2431                 allqueue(REDRAWVIEW3D, 0);
2432
2433                 break;
2434         }
2435 }
2436
2437 static void editing_panel_lattice_type(Object *ob, Lattice *lt)
2438 {
2439         uiBlock *block;
2440
2441         block= uiNewBlock(&curarea->uiblocks, "editing_panel_lattice_type", UI_EMBOSS, UI_HELV, curarea->win);
2442         if(uiNewPanel(curarea, block, "Lattice", "Editing", 320, 0, 318, 204)==0) return;
2443
2444
2445         uiSetButLock(lt->key!=0, "Not with VertexKeys");
2446         uiSetButLock(ob==G.obedit, "Unable to perform function in EditMode");
2447
2448         uiBlockBeginAlign(block);
2449
2450         lt->opntsu = lt->pntsu;
2451         lt->opntsv = lt->pntsv;
2452         lt->opntsw = lt->pntsw;
2453
2454         uiDefButS(block, NUM, B_RESIZELAT,      "U:",                           469, 178,100,19, &lt->opntsu, 1.0, 64.0, 0, 0, "Points in U direction");
2455         uiDefButC(block, ROW, B_LATTCHANGED,            "Lin",          572, 178, 40, 19, &lt->typeu, 1.0, (float)KEY_LINEAR, 0, 0, "Set Linear interpolation");
2456         uiDefButC(block, ROW, B_LATTCHANGED,            "Card",         613, 178, 40, 19, &lt->typeu, 1.0, (float)KEY_CARDINAL, 0, 0, "Set Cardinal interpolation");
2457         uiDefButC(block, ROW, B_LATTCHANGED,            "B",            652, 178, 40, 19, &lt->typeu, 1.0, (float)KEY_BSPLINE, 0, 0, "Set B-spline interpolation");
2458
2459         uiDefButS(block, NUM, B_RESIZELAT,      "V:",                           469, 156,100,19, &lt->opntsv, 1.0, 64.0, 0, 0, "Points in V direction");
2460         uiDefButC(block, ROW, B_LATTCHANGED,            "Lin",          572, 156, 40, 19, &lt->typev, 2.0, (float)KEY_LINEAR, 0, 0, "Set Linear interpolation");
2461         uiDefButC(block, ROW, B_LATTCHANGED,            "Card",         613, 156, 40, 19, &lt->typev, 2.0, (float)KEY_CARDINAL, 0, 0, "Set Cardinal interpolation");
2462         uiDefButC(block, ROW, B_LATTCHANGED,            "B",            652, 156, 40, 19, &lt->typev, 2.0, (float)KEY_BSPLINE, 0, 0, "Set B-spline interpolation");
2463
2464         uiDefButS(block, NUM, B_RESIZELAT,      "W:",                           469, 134,100,19, &lt->opntsw, 1.0, 64.0, 0, 0, "Points in W direction");
2465         uiDefButC(block, ROW, B_LATTCHANGED,            "Lin",          572, 134, 40, 19, &lt->typew, 3.0, (float)KEY_LINEAR, 0, 0, "Set Linear interpolation");
2466         uiDefButC(block, ROW, B_LATTCHANGED,            "Card",         613, 134, 40, 19, &lt->typew, 3.0, (float)KEY_CARDINAL, 0, 0, "Set Cardinal interpolation");
2467         uiDefButC(block, ROW, B_LATTCHANGED,            "B",            652, 134, 40, 19, &lt->typew, 3.0, (float)KEY_BSPLINE, 0, 0, "Set B-spline interpolation");
2468
2469         uiBlockEndAlign(block);
2470
2471         uiDefBut(block, BUT, B_REGULARLAT,      "Make Regular",         469,98,102,31, 0, 0, 0, 0, 0, "Make Lattice regular");
2472
2473         uiClearButLock();
2474         uiDefButBitS(block, TOG, LT_OUTSIDE, B_LATTCHANGED, "Outside",  571,98,122,31, &lt->flag, 0, 0, 0, 0, "Only draw, and take into account, the outer vertices");
2475
2476         if(lt->key) {
2477                 uiDefButS(block, NUM, B_DIFF, "Slurph:",                        469,60,120,19, &(lt->key->slurph), -500.0, 500.0, 0, 0, "Set time value to denote 'slurph' (sequential delay) vertices with key framing");
2478                 uiDefButS(block, TOG, B_RELKEY, "Relative Keys",        469,40,120,19, &lt->key->type, 0, 0, 0, 0, "Use relative keys (instead of absolute)");
2479         }
2480
2481 }
2482
2483 /* *************************** ARMATURE ******************************** */
2484
2485 void do_armbuts(unsigned short event)
2486 {
2487         switch(event) {
2488         case B_ARM_RECALCDATA:
2489                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
2490                 allqueue(REDRAWVIEW3D, 1);
2491                 allqueue(REDRAWBUTSEDIT, 0);
2492         }
2493 }
2494
2495 static int editbone_to_parnr (EditBone *bone)
2496 {
2497         EditBone *ebone;
2498         int     index;
2499
2500         for (ebone=G.edbo.first, index=0; ebone; ebone=ebone->next, index++){
2501                 if (ebone==bone)
2502                         return index;
2503         }
2504
2505         return -1;
2506 }
2507
2508
2509 /* the "IK" button in editbuttons */
2510 static void attach_bone_to_parent_cb(void *bonev, void *arg2_unused)
2511 {
2512         EditBone *ebone= bonev;
2513         
2514         if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
2515                 /* Attach this bone to its parent */
2516                 VECCOPY(ebone->head, ebone->parent->tail);
2517         }
2518 }
2519
2520 static void parnr_to_editbone(EditBone *bone)
2521 {
2522         if (bone->parNr == -1){
2523                 bone->parent = NULL;
2524                 bone->flag &= ~BONE_CONNECTED;
2525         }
2526         else{
2527                 bone->parent = BLI_findlink(&G.edbo, bone->parNr);
2528                 attach_bone_to_parent_cb(bone, NULL);
2529         }
2530 }
2531
2532 static void parnr_to_editbone_cb(void *bonev, void *arg2_unused)
2533 {
2534         EditBone *curBone= bonev;
2535         parnr_to_editbone(curBone);
2536 }
2537
2538 static void build_bonestring (char *string, EditBone *bone)
2539 {
2540         EditBone *curBone;
2541         EditBone *pBone;
2542         int             skip=0;
2543         int             index, numbones, i;
2544         char (*qsort_ptr)[32] = NULL;
2545
2546         sprintf (string, "Parent%%t| %%x%d", -1);       /* That space is there
2547                                                                                                  * for a reason
2548                                                                                                  */
2549
2550         numbones = BLI_countlist(&G.edbo);
2551
2552         /*
2553          * This will hold the bone names temporarily so we can sort them
2554          */
2555         if (numbones > 0)
2556                 qsort_ptr = MEM_callocN (numbones * sizeof (qsort_ptr[0]),
2557                                                                  "qsort_ptr");
2558
2559         numbones = 0;
2560         for (curBone = G.edbo.first, index=0; curBone;
2561                  curBone=curBone->next, index++){
2562                 /* Make sure this is a valid child */
2563                 if (curBone != bone){
2564                         skip=0;
2565                         for (pBone=curBone->parent; pBone; pBone=pBone->parent){
2566                                 if (pBone==bone){
2567                                         skip=1;
2568                                         break;
2569                                 }
2570                         }
2571
2572                         if (skip)
2573                                 continue;
2574
2575                         sprintf (qsort_ptr[numbones], "|%s%%x%d", curBone->name, index);
2576                         numbones++;
2577                 }
2578         }
2579         qsort (qsort_ptr, numbones, sizeof (qsort_ptr[0]),
2580                    ( int (*)(const void *, const void *) ) strcmp);
2581
2582         for (i=0; i < numbones; ++i) {
2583                 sprintf (string, "%s%s", string, qsort_ptr[i]);
2584         }
2585
2586         if (qsort_ptr)
2587                 MEM_freeN(qsort_ptr);
2588 }
2589
2590 /* assumes armature editmode */
2591 /* exported to drawview.c via BIF_butspace.h */
2592 void validate_editbonebutton_cb(void *bonev, void *namev)
2593 {
2594         EditBone *eBone= bonev;
2595         char oldname[32], newname[32];
2596         
2597         /* need to be on the stack */
2598         BLI_strncpy(newname, eBone->name, 32);
2599         BLI_strncpy(oldname, (char *)namev, 32);
2600         /* restore */
2601         BLI_strncpy(eBone->name, oldname, 32);
2602         
2603         armature_bone_rename(G.obedit->data, oldname, newname); // editarmature.c
2604         allqueue(REDRAWALL, 0);
2605 }
2606
2607 /* assumes armature posemode */
2608 static void validate_posebonebutton_cb(void *bonev, void *namev)
2609 {
2610         Bone *bone= bonev;
2611         Object *ob= OBACT;
2612         char oldname[32], newname[32];
2613         
2614         /* need to be on the stack */
2615         BLI_strncpy(newname, bone->name, 32);
2616         BLI_strncpy(oldname, (char *)namev, 32);
2617         /* restore */
2618         BLI_strncpy(bone->name, oldname, 32);
2619         
2620         armature_bone_rename(ob->data, oldname, newname); // editarmature.c
2621         allqueue(REDRAWALL, 0);
2622 }
2623
2624 static void editing_panel_armature_type(Object *ob, bArmature *arm)
2625 {
2626         uiBlock         *block;
2627
2628         block= uiNewBlock(&curarea->uiblocks, "editing_panel_armature_type", UI_EMBOSS, UI_HELV, curarea->win);
2629         if(uiNewPanel(curarea, block, "Armature", "Editing", 320, 0, 318, 204)==0) return;
2630
2631         uiDefBut(block, LABEL, 0, "Editing Options", 10,180,150,20, 0, 0, 0, 0, 0, "");
2632         uiBlockBeginAlign(block);
2633         uiDefButBitI(block, TOG, ARM_MIRROR_EDIT, B_DIFF, "X-Axis Mirror Edit", 10, 160,150,20, &arm->flag, 0, 0, 0, 0, "Enable X-axis mirrored editing");
2634         uiDefButBitC(block, TOG, OB_DRAWXRAY,REDRAWVIEW3D, "X-Ray",                             160,160,150,20, &ob->dtx, 0, 0, 0, 0, "Draw armature in front of solid objects");
2635         uiBlockEndAlign(block);
2636         
2637         uiDefBut(block, LABEL, 0, "Display Options", 10,140,150,20, 0, 0, 0, 0, 0, "");
2638         uiBlockBeginAlign(block);
2639         uiDefButI(block, ROW, REDRAWVIEW3D, "Octahedron", 10, 120,90,20, &arm->drawtype, 0, ARM_OCTA, 0, 0, "Draw bones as octahedra");
2640         uiDefButI(block, ROW, REDRAWVIEW3D, "Stick",    100, 120,55,20, &arm->drawtype, 0, ARM_LINE, 0, 0, "Draw bones as simple 2d lines with dots");
2641         uiDefButI(block, ROW, REDRAWVIEW3D, "B-Bone",   155, 120,70,20, &arm->drawtype, 0, ARM_B_BONE, 0, 0, "Draw bones as boxes, showing subdivision and b-splines");
2642         uiDefButI(block, ROW, REDRAWVIEW3D, "Envelope", 225, 120,85,20, &arm->drawtype, 0, ARM_ENVELOPE, 0, 0, "Draw bones as extruded spheres, showing deformation influence volume");
2643
2644         uiDefButBitI(block, TOG, ARM_DRAWAXES, REDRAWVIEW3D, "Draw Axes", 10, 100,150,20, &arm->flag, 0, 0, 0, 0, "Draw bone axes");
2645         uiDefButBitI(block, TOG, ARM_DRAWNAMES, REDRAWVIEW3D, "Draw Names", 160,100,150,20, &arm->flag, 0, 0, 0, 0, "Draw bone names");
2646         uiBlockEndAlign(block);
2647         
2648         uiDefBut(block, LABEL, 0, "Deform Options", 10,80,150,20, 0, 0, 0, 0, 0, "");
2649         uiBlockBeginAlign(block);
2650         uiDefButBitI(block, TOG, ARM_DEF_VGROUP, B_ARM_RECALCDATA, "Vertex Groups",     10, 60,150,20, &arm->deformflag, 0, 0, 0, 0, "Enable VertexGroups defining deform");
2651         uiDefButBitI(block, TOG, ARM_DEF_ENVELOPE, B_ARM_RECALCDATA, "Envelopes",       160,60,150,20, &arm->deformflag, 0, 0, 0, 0, "Enable Bone Envelopes defining deform");
2652         uiDefButBitI(block, TOG, ARM_RESTPOS, B_ARM_RECALCDATA,"Rest Position",         10,40,150,20, &arm->flag, 0, 0, 0, 0, "Show armature rest position, no posing possible");
2653         uiDefButBitI(block, TOG, ARM_DELAYDEFORM, REDRAWVIEW3D, "Delay Deform",         160,40,150,20, &arm->flag, 0, 0, 0, 0, "Don't deform children when manipulating bones in pose mode");
2654         
2655 }
2656
2657
2658 static void editing_panel_armature_bones(Object *ob, bArmature *arm)
2659 {
2660         uiBlock         *block;
2661         uiBut           *but;
2662         EditBone        *curBone;
2663         char            *boneString=NULL;
2664         int                     bx=148, by=180;
2665         int                     index;
2666
2667         /* Draw the bone name block */
2668
2669         block= uiNewBlock(&curarea->uiblocks, "editing_panel_armature_bones", UI_EMBOSS, UI_HELV, curarea->win);
2670         if(uiNewPanel(curarea, block, "Armature Bones", "Editing", 640, 0, 318, 204)==0) return;
2671
2672         /* this is a variable height panel, newpanel doesnt force new size on existing panels */
2673         /* so first we make it default height */
2674         uiNewPanelHeight(block, 204);
2675
2676
2677         uiDefBut(block, LABEL, 0, "Selected Bones", bx,by,158,18, 0, 0, 0, 0, 0, "Only show in Armature Editmode");
2678         by-=20;
2679         for (curBone=G.edbo.first, index=0; curBone; curBone=curBone->next, index++){
2680                 if (curBone->flag & (BONE_SELECTED)) {
2681
2682                         /*      Bone naming button */
2683                         but=uiDefBut(block, TEX, REDRAWVIEW3D, "BO:", bx-10,by,117,18, &curBone->name, 0, 24, 0, 0, "Change the bone name");
2684                         uiButSetFunc(but, validate_editbonebutton_cb, curBone, NULL);
2685
2686                         uiDefBut(block, LABEL, 0, "child of", bx+107,by,73,18, NULL, 0.0, 0.0, 0.0, 0.0, "");
2687
2688                         boneString = MEM_mallocN((BLI_countlist(&G.edbo) * 64)+64, "Bone str");
2689                         build_bonestring (boneString, curBone);
2690
2691                         curBone->parNr = editbone_to_parnr(curBone->parent);
2692                         but = uiDefButI(block, MENU,REDRAWVIEW3D, boneString, bx+180,by,120,18, &curBone->parNr, 0.0, 0.0, 0.0, 0.0, "Parent");
2693                         /* last arg NULL means button will put old string there */
2694                         uiButSetFunc(but, parnr_to_editbone_cb, curBone, NULL);
2695
2696                         MEM_freeN(boneString);
2697
2698                         /* Connect to parent flag */
2699                         if (curBone->parent){
2700                                 but=uiDefButBitI(block, TOG, BONE_CONNECTED, B_ARM_RECALCDATA, "Con", bx+300,by,32,18, &curBone->flag, 0.0, 0.0, 0.0, 0.0, "Connect this Bone to Parent");
2701                                 uiButSetFunc(but, attach_bone_to_parent_cb, curBone, NULL);
2702                         }
2703
2704                         /* Segment, dist and weight buttons */
2705                         uiBlockBeginAlign(block);
2706                         uiDefButS(block, NUM, B_ARM_RECALCDATA, "Segm: ", bx-10,by-19,117,18, &curBone->segments, 1.0, 32.0, 0.0, 0.0, "Subdivisions for B-bones");
2707                         uiDefButF(block, NUM,B_ARM_RECALCDATA, "Dist:", bx+110, by-19, 105, 18, &curBone->dist, 0.0, 1000.0, 10.0, 0.0, "Bone deformation distance");
2708                         uiDefButF(block, NUM,B_ARM_RECALCDATA, "Weight:", bx+223, by-19,110, 18, &curBone->weight, 0.0F, 1000.0F, 10.0F, 0.0F, "Bone deformation weight");
2709
2710                         /* bone types */
2711                         uiDefButBitI(block, TOG, BONE_HINGE, B_ARM_RECALCDATA, "Hinge",         bx-10,by-38,85,18, &curBone->flag, 1.0, 32.0, 0.0, 0.0, "Don't inherit rotation or scale from parent Bone");
2712                         uiDefButBitI(block, TOGN, BONE_NO_DEFORM, B_ARM_RECALCDATA, "Deform",   bx+75, by-38, 85, 18, &curBone->flag, 0.0, 0.0, 0.0, 0.0, "Indicate if Bone deforms geometry");
2713                         uiDefButBitI(block, TOG, BONE_MULT_VG_ENV, B_ARM_RECALCDATA, "Mult", bx+160,by-38,85,18, &curBone->flag, 1.0, 32.0, 0.0, 0.0, "Multiply Bone Envelope with VertexGroup");
2714                         uiDefButBitI(block, TOG, BONE_HIDDEN_A, REDRAWVIEW3D, "Hide",   bx+245,by-38,88,18, &curBone->flag, 0, 0, 0, 0, "Toggles display of this bone in Edit Mode");
2715                         
2716                         uiBlockEndAlign(block);
2717                         by-=60;
2718                         
2719                         if(by < -200) break;    // for time being... extreme long panels are very slow
2720                 }
2721         }
2722
2723         if(by<0) {
2724                 uiNewPanelHeight(block, 204 - by);
2725         }
2726
2727 }
2728
2729 static void editing_panel_pose_bones(Object *ob, bArmature *arm)
2730 {
2731         uiBlock         *block;
2732         uiBut           *but;
2733         bPoseChannel *pchan;
2734         Bone            *curBone;
2735         int                     bx=148, by=180;
2736         int                     index, zerodof, zerolimit;
2737         
2738         /* Draw the bone name block */
2739         
2740         block= uiNewBlock(&curarea->uiblocks, "editing_panel_pose_bones", UI_EMBOSS, UI_HELV, curarea->win);
2741         if(uiNewPanel(curarea, block, "Armature Bones", "Editing", 640, 0, 318, 204)==0) return;
2742         
2743         /* this is a variable height panel, newpanel doesnt force new size on existing panels */
2744         /* so first we make it default height */
2745         uiNewPanelHeight(block, 204);
2746         
2747         uiDefBut(block, LABEL, 0, "Selected Bones", bx,by,158,18, 0, 0, 0, 0, 0, "Only show in Armature Editmode/Posemode");
2748         by-=20;
2749         for (pchan=ob->pose->chanbase.first, index=0; pchan; pchan=pchan->next, index++){
2750                 curBone= pchan->bone;
2751                 if (curBone->flag & (BONE_SELECTED)) {
2752
2753                         /*      Bone naming button */
2754                         uiBlockBeginAlign(block);
2755                         but=uiDefBut(block, TEX, REDRAWVIEW3D, "BO:", bx-10,by,117,18, &curBone->name, 0, 24, 0, 0, "Change the bone name");
2756                         uiButSetFunc(but, validate_posebonebutton_cb, curBone, NULL);
2757                         
2758                         /* Dist and weight buttons */
2759                         uiDefButF(block, NUM,B_ARM_RECALCDATA, "Dist:", bx+107, by, 105, 18, &curBone->dist, 0.0, 1000.0, 10.0, 0.0, "Bone deformation distance");
2760                         uiDefButF(block, NUM,B_ARM_RECALCDATA, "Weight:", bx+220, by,  110, 18, &curBone->weight, 0.0F, 1000.0F, 10.0F, 0.0F, "Bone deformation weight");
2761                         
2762                         
2763                         /* Segment, ease in/out buttons */
2764                         uiBlockBeginAlign(block);
2765                         uiDefButS(block, NUM, B_ARM_RECALCDATA, "Segm: ",  bx-10,by-19,117,19, &curBone->segments, 1.0, 32.0, 0.0, 0.0, "Subdivisions for B-bones");
2766                         uiDefButF(block, NUM,B_ARM_RECALCDATA, "In:",  bx+107, by-19,105, 19, &curBone->ease1, 0.0, 2.0, 10.0, 0.0, "First length of Bezier handle");
2767                         uiDefButF(block, NUM,B_ARM_RECALCDATA, "Out:",  bx+220, by-19, 110, 19, &curBone->ease2, 0.0, 2.0, 10.0, 0.0, "Second length of Bezier handle");
2768                         
2769                         /* bone types */
2770                         uiDefButBitI(block, TOG, BONE_HINGE, B_ARM_RECALCDATA, "Hinge", bx-10,by-38,85,18, &curBone->flag, 1.0, 32.0, 0.0, 0.0, "Don't inherit rotation or scale from parent Bone");
2771                         uiDefButBitI(block, TOGN, BONE_NO_DEFORM, B_ARM_RECALCDATA, "Deform",   bx+75, by-38, 85, 18, &curBone->flag, 0.0, 0.0, 0.0, 0.0, "Indicate if Bone deforms geometry");
2772                         uiDefButBitI(block, TOG, BONE_MULT_VG_ENV, B_ARM_RECALCDATA, "Mult", bx+160,by-38,85,18, &curBone->flag, 1.0, 32.0, 0.0, 0.0, "Multiply Bone Envelope with VertexGroup");
2773                         uiDefButBitI(block, TOG, BONE_HIDDEN_P, REDRAWVIEW3D, "Hide",   bx+245,by-38,88,18, &curBone->flag, 0, 0, 0, 0, "Toggles display of this bone in Pose Mode");
2774                         uiBlockEndAlign(block);
2775                         
2776                         /* DOFs only for IK chains */
2777                         zerodof = 1;
2778                         zerolimit = 1;
2779                         if(pose_channel_in_IK_chain(ob, pchan)) {
2780                                 
2781                                 uiBlockBeginAlign(block);
2782                                 uiDefButBitS(block, TOG, BONE_IK_NO_XDOF, B_ARM_RECALCDATA, "Lock X Rot", bx-10,by-60,114,19, &pchan->ikflag, 0.0, 0.0, 0.0, 0.0, "Disable X DoF for IK");
2783                                 if ((pchan->ikflag & BONE_IK_NO_XDOF)==0) {
2784                                         uiDefButF(block, NUM, B_ARM_RECALCDATA, "Stiff X:", bx-10, by-80, 114, 19, &pchan->stiffness[0], 0.0, 0.99, 1.0, 0.0, "Resistance to bending for X axis");
2785                                         uiDefButBitS(block, TOG, BONE_IK_XLIMIT, B_ARM_RECALCDATA, "Limit X", bx-10,by-100,114,19, &pchan->ikflag, 0.0, 0.0, 0.0, 0.0, "Limit rotation over X axis");
2786                                         if ((pchan->ikflag & BONE_IK_XLIMIT)) {
2787                                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Min X:", bx-10, by-120, 114, 19, &pchan->limitmin[0], -180.0, 0.0, 1000, 1, "Minimum X limit");
2788                                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Max X:", bx-10, by-140, 114, 19, &pchan->limitmax[0], 0.0, 180.0f, 1000, 1, "Maximum X limit");
2789                                                 zerolimit = 0;
2790                                         }
2791                                         zerodof = 0;
2792                                 }
2793                                 uiBlockEndAlign(block);
2794                                 
2795                                 uiBlockBeginAlign(block);
2796                                 uiDefButBitS(block, TOG, BONE_IK_NO_YDOF, B_ARM_RECALCDATA, "Lock Y Rot", bx+104,by-60,113,19, &pchan->ikflag, 0.0, 0.0, 0.0, 0.0, "Disable Y DoF for IK");
2797                                 if ((pchan->ikflag & BONE_IK_NO_YDOF)==0) {
2798                                         uiDefButF(block, NUM, B_ARM_RECALCDATA, "Stiff Y:", bx+104, by-80, 114, 19, &pchan->stiffness[1], 0.0, 0.99, 1.0, 0.0, "Resistance to twisting over Y axis");
2799                                         uiDefButBitS(block, TOG, BONE_IK_YLIMIT, B_ARM_RECALCDATA, "Limit Y", bx+104,by-100,113,19, &pchan->ikflag, 0.0, 0.0, 0.0, 0.0, "Limit rotation over Y axis");
2800                                         if ((pchan->ikflag & BONE_IK_YLIMIT)) {
2801                                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Min Y:", bx+104, by-120, 113, 19, &pchan->limitmin[1], -180.0, 0.0, 1000, 1, "Minimum Y limit");
2802                                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Max Y:", bx+104, by-140, 113, 19, &pchan->limitmax[1], 0.0, 180.0, 1000, 1, "Maximum Y limit");
2803                                                 zerolimit = 0;
2804                                         }
2805                                         zerodof = 0;
2806                                 }
2807                                 uiBlockEndAlign(block);
2808                                 
2809                                 uiBlockBeginAlign(block);
2810                                 uiDefButBitS(block, TOG, BONE_IK_NO_ZDOF, B_ARM_RECALCDATA, "Lock Z Rot", bx+217,by-60,113,19, &pchan->ikflag, 0.0, 0.0, 0.0, 0.0, "Disable Z DoF for IK");
2811                                 if ((pchan->ikflag & BONE_IK_NO_ZDOF)==0) {
2812                                         uiDefButF(block, NUM, B_ARM_RECALCDATA, "Stiff Z:", bx+217, by-80, 114, 19, &pchan->stiffness[2], 0.0, 0.99, 1.0, 0.0, "Resistance to bending for Z axis");
2813                                         uiDefButBitS(block, TOG, BONE_IK_ZLIMIT, B_ARM_RECALCDATA, "Limit Z", bx+217,by-100,113,19, &pchan->ikflag, 0.0, 0.0, 0.0, 0.0, "Limit rotation over Z axis");
2814                                         if ((pchan->ikflag & BONE_IK_ZLIMIT)) {
2815                                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Min Z:", bx+217, by-120, 113, 19, &pchan->limitmin[2], -180.0, 0.0, 1000, 1, "Minimum Z limit");
2816                                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Max Z:", bx+217, by-140, 113, 19, &pchan->limitmax[2], 0.0, 180.0, 1000, 1, "Maximum Z limit");
2817                                                 zerolimit = 0;
2818                                         }
2819                                         zerodof = 0;
2820                                 }
2821                                 uiBlockEndAlign(block);
2822                                 
2823                                 by -= (zerodof)? 82: (zerolimit)? 122: 162;
2824
2825                                 uiBlockBeginAlign(block);
2826                                 uiDefButF(block, NUM, B_ARM_RECALCDATA, "Stretch:", bx-10, by, 113, 19, &pchan->ikstretch, 0.0, 1.0, 1.0, 0.0, "Allow scaling of the bone for IK");
2827                                 uiBlockEndAlign(block);
2828
2829                                 by -= 20;
2830                         }
2831                         else {
2832                                 uiDefBut(block, LABEL, 0, "(DoF options only for IK chains)", bx-10,by-60, 300, 20, 0, 0, 0, 0, 0, "");
2833
2834                                 by -= 82;
2835                         }
2836                         
2837                                 
2838                         if(by < -200) break;    // for time being... extreme long panels are very slow
2839                 }
2840         }
2841         
2842         if(by<0) {
2843                 uiNewPanelHeight(block, 204 - by);
2844         }
2845         
2846 }
2847
2848
2849 /* *************************** MESH ******************************** */
2850
2851 void do_meshbuts(unsigned short event)
2852 {
2853         Object *ob;
2854         Mesh *me;
2855         float fac;
2856         short randfac;
2857
2858         ob= OBACT;
2859         if(ob && ob->type==OB_MESH) {
2860
2861                 me= get_mesh(ob);
2862                 if(me==0) return;
2863
2864                 switch(event) {
2865                 case B_NEWVGROUP:
2866                         add_defgroup (G.obedit);
2867                         scrarea_queue_winredraw(curarea);
2868                         allqueue(REDRAWOOPS, 0);
2869                         break;
2870                 case B_DELVGROUP:
2871                         del_defgroup (G.obedit);
2872                         allqueue (REDRAWVIEW3D, 1);
2873                         allqueue(REDRAWOOPS, 0);
2874                         BIF_undo_push("Delete vertex group");
2875                         break;
2876                 case B_ASSIGNVGROUP:
2877                         assign_verts_defgroup ();
2878                         allqueue (REDRAWVIEW3D, 1);
2879                         BIF_undo_push("Assign to vertex group");
2880                         break;
2881                 case B_REMOVEVGROUP:
2882                         remove_verts_defgroup (0);
2883                         allqueue (REDRAWVIEW3D, 1);
2884                         allqueue(REDRAWOOPS, 0);
2885                         BIF_undo_push("Remove from vertex group");
2886                         break;
2887                 case B_SELVGROUP:
2888                         sel_verts_defgroup(1);
2889                         allqueue (REDRAWVIEW3D, 1);
2890                         allqueue(REDRAWOOPS, 0);
2891                         break;
2892                 case B_DESELVGROUP:
2893                         sel_verts_defgroup(0);
2894                         allqueue (REDRAWVIEW3D, 1);
2895                         allqueue(REDRAWOOPS, 0);
2896                         break;
2897                 case B_DELSTICKY:
2898
2899                         if(me->msticky) MEM_freeN(me->msticky);
2900                         me->msticky= NULL;
2901                         allqueue(REDRAWBUTSEDIT, 0);
2902                         break;
2903                 case B_MAKESTICKY:
2904                         RE_make_sticky();
2905                         allqueue(REDRAWBUTSEDIT, 0);
2906                         break;
2907                 
2908                 case B_MAKEVERTCOL:
2909                         make_vertexcol();
2910                         break;
2911                 case B_DELVERTCOL:
2912                         if(me->mcol) MEM_freeN(me->mcol);
2913                         me->mcol= NULL;
2914                         G.f &= ~G_VERTEXPAINT;
2915                         freedisplist(&(ob->disp));
2916                         allqueue(REDRAWBUTSEDIT, 0);
2917                         allqueue(REDRAWVIEW3D, 0);
2918                         break;
2919
2920                 case B_MAKE_TFACES:
2921                         make_tfaces(me);
2922                         allqueue(REDRAWBUTSEDIT, 0);
2923                         break;
2924
2925                 case B_DEL_TFACES:
2926                         if(me->tface) MEM_freeN(me->tface);
2927                         me->tface= 0;
2928                         G.f &= ~G_FACESELECT;
2929                         allqueue(REDRAWBUTSEDIT, 0);
2930                         allqueue(REDRAWVIEW3D, 0);
2931                         allqueue(REDRAWIMAGE, 0);
2932                         break;
2933
2934                 case B_FLIPNORM:
2935                         if(G.obedit) {
2936                                 flip_editnormals();
2937                         }
2938                         allqueue(REDRAWVIEW3D, 0);
2939                         break;
2940
2941                 case B_SLOWERDRAW:
2942                         slowerdraw();
2943                         break;
2944                 case B_FASTERDRAW:
2945                         fasterdraw();
2946                         break;
2947                 }
2948         }
2949
2950         if(G.obedit==0 || (G.obedit->type!=OB_MESH)) return;
2951
2952         switch(event) {
2953         case B_SPIN:
2954                 if( select_area(SPACE_VIEW3D)) spin_mesh(G.scene->toolsettings->step, G.scene->toolsettings->degr, 0, 0);
2955                 break;
2956         case B_SPINDUP:
2957                 if( select_area(SPACE_VIEW3D)) spin_mesh(G.scene->toolsettings->step, G.scene->toolsettings->degr, 0, 1);
2958                 break;
2959         case B_EXTR:
2960                 G.f |= G_DISABLE_OK;
2961                 if( select_area(SPACE_VIEW3D)) extrude_mesh();
2962                 G.f -= G_DISABLE_OK;
2963                 break;
2964         case B_SCREW:
2965                 if( select_area(SPACE_VIEW3D)) screw_mesh(G.scene->toolsettings->step, G.scene->toolsettings->turn);
2966                 break;
2967         case B_EXTREP:
2968                 if( select_area(SPACE_VIEW3D)) extrude_repeat_mesh(G.scene->toolsettings->step, G.scene->toolsettings->extr_offs);
2969                 break;
2970         case B_SPLIT:
2971                 G.f |= G_DISABLE_OK;
2972                 split_mesh();
2973                 G.f -= G_DISABLE_OK;
2974                 break;
2975         case B_REMDOUB:
2976                 notice("Removed: %d", removedoublesflag(1, G.scene->toolsettings->doublimit));
2977                 allqueue(REDRAWVIEW3D, 0);
2978                 BIF_undo_push("Rem Doubles");
2979                 break;
2980         case B_SUBDIV:
2981                 waitcursor(1);
2982                 esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag & B_BEAUTY,1,0);
2983                 countall();
2984                 waitcursor(0);
2985                 allqueue(REDRAWVIEW3D, 0);
2986                 BIF_undo_push("Subdivide");
2987                 break;
2988         case B_FRACSUBDIV:
2989                 randfac= 10;
2990                 if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2991                 waitcursor(1);
2992                 fac= -( (float)randfac )/100;
2993                 esubdivideflag(1, fac, G.scene->toolsettings->editbutflag & B_BEAUTY,1,0);
2994                 countall();
2995                 waitcursor(0);
2996                 allqueue(REDRAWVIEW3D, 0);
2997                 BIF_undo_push("Fractal Subdivide");
2998                 break;
2999         case B_XSORT: