Orange <-> bf-blender conflict, with 2.40 saved files the version-patch to
[blender.git] / source / blender / src / headerbuttons.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 <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include <sys/types.h>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BMF_Api.h"
46 #include "BIF_language.h"
47 #ifdef INTERNATIONAL
48 #include "FTF_Api.h"
49 #endif
50
51 #include "BLI_blenlib.h"
52 #include "BLI_arithb.h"
53 #include "BLI_storage_types.h"
54
55 #include "IMB_imbuf_types.h"
56 #include "IMB_imbuf.h"
57
58 #include "DNA_ID.h"
59 #include "DNA_action_types.h"
60 #include "DNA_armature_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_group_types.h"
64 #include "DNA_image_types.h"
65 #include "DNA_ipo_types.h"
66 #include "DNA_key_types.h"
67 #include "DNA_lamp_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_material_types.h"
70 #include "DNA_mesh_types.h"
71 #include "DNA_meta_types.h"
72 #include "DNA_object_types.h"
73 #include "DNA_oops_types.h"
74 #include "DNA_packedFile_types.h"
75 #include "DNA_scene_types.h"
76 #include "DNA_screen_types.h"
77 #include "DNA_sequence_types.h"
78 #include "DNA_sound_types.h"
79 #include "DNA_space_types.h"
80 #include "DNA_texture_types.h"
81 #include "DNA_text_types.h"
82 #include "DNA_userdef_types.h"
83 #include "DNA_view2d_types.h"
84 #include "DNA_view3d_types.h"
85 #include "DNA_world_types.h"
86 #include "DNA_constraint_types.h"
87
88 #include "BKE_utildefines.h"
89
90 #include "BKE_constraint.h"
91 #include "BKE_action.h"
92 #include "BKE_armature.h"
93 #include "BKE_blender.h"
94 #include "BKE_curve.h"
95 #include "BKE_depsgraph.h"
96 #include "BKE_exotic.h"
97 #include "BKE_global.h"
98 #include "BKE_image.h"
99 #include "BKE_ipo.h"
100 #include "BKE_key.h"
101 #include "BKE_lattice.h"
102 #include "BKE_library.h"
103 #include "BKE_main.h"
104 #include "BKE_material.h"
105 #include "BKE_mball.h"
106 #include "BKE_mesh.h"
107 #include "BKE_object.h"
108 #include "BKE_packedFile.h"
109 #include "BKE_sca.h"
110 #include "BKE_scene.h"
111 #include "BKE_texture.h"
112 #include "BKE_text.h"
113 #include "BKE_world.h"
114
115 #include "BLO_readfile.h"
116 #include "BLO_writefile.h"
117
118 #include "BIF_drawimage.h"
119 #include "BIF_drawoops.h"
120 #include "BIF_drawscene.h"
121 #include "BIF_drawtext.h"
122 #include "BIF_editaction.h"
123 #include "BIF_editarmature.h"
124 #include "BIF_editfont.h"
125 #include "BIF_editlattice.h"
126 #include "BIF_editconstraint.h"
127 #include "BIF_editmesh.h"
128 #include "BIF_editmesh.h"
129 #include "BIF_editsima.h"
130 #include "BIF_editsound.h"
131 #include "BIF_gl.h"
132 #include "BIF_imasel.h"
133 #include "BIF_interface.h"
134 #include "BIF_mainqueue.h"
135 #include "BIF_mywindow.h"
136 #include "BIF_poseobject.h"
137 #include "BIF_renderwin.h"
138 #include "BIF_resources.h"
139 #include "BIF_screen.h"
140 #include "BIF_space.h"
141 #include "BIF_toets.h"
142 #include "BIF_toolbox.h"
143 #include "BIF_usiblender.h"
144 #include "BIF_previewrender.h"
145 #include "BIF_writeimage.h"
146 #include "BIF_butspace.h"
147
148 #include "BPI_script.h"
149
150 #include "BSE_edit.h"
151 #include "BSE_filesel.h"
152 #include "BSE_headerbuttons.h"
153 #include "BSE_view.h"
154 #include "BSE_sequence.h"
155 #include "BSE_editipo.h"
156 #include "BSE_drawipo.h"
157
158 #include "BDR_drawmesh.h"
159 #include "BDR_vpaint.h"
160 #include "BDR_editface.h"
161 #include "BDR_editobject.h"
162 #include "BDR_editcurve.h"
163 #include "BDR_editmball.h"
164
165 #include "BPY_extern.h"
166 #include "BPY_menus.h"
167
168 #include "mydevice.h"
169 #include "blendef.h"
170 #include "interface.h"
171 #include "nla.h"        /* __NLA : To be removed later */
172 #include "butspace.h"  // test_idbutton
173
174 #include "BIF_poseobject.h"
175
176 #include "SYS_System.h"
177
178  /* WATCH IT:  always give all headerbuttons for same window the same name
179         *                       event B_REDR is a standard redraw
180         *
181         */
182
183 char *windowtype_pup(void)
184 {
185         static char string[1024];
186
187         strcpy(string, "Window type:%t"); //14
188         strcat(string, "|3D View %x1"); //30
189
190         strcat(string, "|%l"); // 33
191
192         strcat(string, "|Ipo Curve Editor %x2"); //54
193         strcat(string, "|Action Editor %x12"); //73
194         strcat(string, "|NLA Editor %x13"); //94
195
196         strcat(string, "|%l"); //97
197
198         strcat(string, "|UV/Image Editor %x6"); //117
199
200         strcat(string, "|Video Sequence Editor %x8"); //143
201         strcat(string, "|Timeline %x15"); //163
202         strcat(string, "|Audio Window %x11"); //163
203         strcat(string, "|Text Editor %x9"); //179
204
205         strcat(string, "|%l"); //192
206
207
208         strcat(string, "|User Preferences %x7"); //213
209         strcat(string, "|Outliner %x3"); //232
210         strcat(string, "|Buttons Window %x4"); //251
211         strcat(string, "|Node Editor %x16");
212
213         strcat(string, "|%l"); //254
214
215         strcat(string, "|Image Browser %x10"); //273
216         strcat(string, "|File Browser %x5"); //290
217
218         strcat(string, "|%l"); //293
219
220         strcat(string, "|Scripts Window %x14"); //313
221
222         return (string);
223 }
224
225 int GetButStringLength(char *str) {
226         int rt;
227
228         rt= BIF_GetStringWidth(G.font, str, (U.transopts & USER_TR_BUTTONS));
229
230         return rt + 15;
231 }
232
233 /* ********************** GLOBAL ****************************** */
234
235 int std_libbuttons(uiBlock *block, short xco, short yco,
236                                                         int pin, short *pinpoin, int browse, short id_code, short special, ID *id,
237                                                         ID *parid, short *menupoin, int users, int lib,
238                                                         int del, int autobut, int keepbut)
239 {
240         ListBase *lb;
241         uiBut *but;
242         int len, oldcol, add_addbutton=0;
243         char *str=NULL, str1[10];
244
245         uiBlockBeginAlign(block);
246         oldcol= uiBlockGetCol(block);
247
248         if(id && pin) {
249                 uiDefIconButS(block, ICONTOG, pin, ICON_PIN_DEHLT, xco,yco,XIC,YIC, pinpoin, 0, 0, 0, 0, "Keeps this view displaying the current data regardless of what object is selected");
250                 xco+= XIC;
251         }
252         /* browse menu */
253         if(browse) {
254                 char *extrastr= NULL;
255                 
256                 if(ELEM(id_code, ID_MA, ID_TE)) add_addbutton= 1;
257                         
258                 lb= wich_libbase(G.main, id_code);
259                 
260                 if(id && id->us>1) uiBlockSetCol(block, TH_BUT_SETTING1);
261
262                 if (pin && *pinpoin) {
263                         uiBlockSetCol(block, TH_BUT_SETTING2);
264                 }
265                 
266                 if ELEM7( id_code, ID_SCE, ID_SCR, ID_MA, ID_TE, ID_WO, ID_IP, ID_AC) extrastr= "ADD NEW %x 32767";
267                 else if (id_code==ID_TXT) extrastr= "OPEN NEW %x 32766 |ADD NEW %x 32767";
268                 else if (id_code==ID_SO) extrastr= "OPEN NEW %x 32766";
269                 
270                 uiSetButLock(G.scene->id.lib!=0, "Can't edit library data");
271                 if( id_code==ID_SCE || id_code==ID_SCR ) uiClearButLock();
272                 
273                 if(curarea->spacetype==SPACE_BUTS)
274                         uiSetButLock(id_code!=ID_SCR && G.obedit!=0 && G.buts->mainb==CONTEXT_EDITING, NULL);
275                 
276                 if(parid) uiSetButLock(parid->lib!=0, "Can't edit library data");
277
278                 if (lb) {
279                         if( id_code==ID_IP)
280                                 IPOnames_to_pupstring(&str, NULL, extrastr, lb, id, menupoin, G.sipo->blocktype);
281                         else
282                                 IDnames_to_pupstring(&str, NULL, extrastr, lb, id, menupoin);
283                 }
284                 
285                 uiDefButS(block, MENU, browse, str, xco,yco,XIC,YIC, menupoin, 0, 0, 0, 0, "Browses existing choices or adds NEW");
286                 xco+= XIC;
287                 
288                 uiClearButLock();
289         
290                 MEM_freeN(str);
291         }
292
293         uiBlockSetCol(block, oldcol);
294
295         if(id) {        /* text button with name */
296         
297                 /* name */
298                 if(id->us>1) uiBlockSetCol(block, TH_BUT_SETTING1);
299                 /* Pinned data ? */
300                 if (pin && *pinpoin) {
301                         uiBlockSetCol(block, TH_BUT_SETTING2);
302                 }
303                 /* Redalert overrides pin color */
304                 if(id->us<=0) uiBlockSetCol(block, TH_REDALERT);
305
306                 uiSetButLock(id->lib!=0, "Can't edit library data");
307                 
308                 if(GS(id->name)==ID_SCE) strcpy(str1, "SCE:");
309                 else if(GS(id->name)==ID_SCE) strcpy(str1, "SCR:");
310                 else if(GS(id->name)==ID_MA) {
311                         if( ((Material *)id)->use_nodes )
312                                 strcpy(str1, "NT:");
313                         else
314                                 strcpy(str1, "MA:");
315                 }
316                 else {
317                         str1[0]= id->name[0];
318                         str1[1]= id->name[1];
319                         str1[2]= ':';
320                         str1[3]= 0;
321                 }
322                 
323                 if( GS(id->name)==ID_IP) len= 110;
324                 else if(yco) len= 140;  // comes from button panel
325                 else len= 120;
326                 
327                 but= uiDefBut(block, TEX, B_IDNAME, str1,xco, yco, (short)len, YIC, id->name+2, 0.0, 19.0, 0, 0, "Displays current Datablock name. Click to change.");
328                 uiButSetFunc(but, test_idbutton_cb, id->name, NULL);
329
330                 uiClearButLock();
331
332                 xco+= len;
333                 
334                 if(id->lib) {
335                         
336                         if(parid && parid->lib) uiDefIconBut(block, BUT, 0, ICON_DATALIB,xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Displays name of the current Indirect Library Datablock. Click to change.");
337                         else uiDefIconBut(block, BUT, lib, ICON_PARLIB, xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Displays current Library Datablock name. Click to make local.");
338                         
339                         xco+= XIC;
340                 }
341                 
342                 
343                 if(users && id->us>1) {
344                         uiSetButLock (pin && *pinpoin, "Can't make pinned data single-user");
345                         
346                         sprintf(str1, "%d", id->us);
347                         if(id->us<10) {
348                                 
349                                 uiDefBut(block, BUT, users, str1, xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Displays number of users of this data. Click to make a single-user copy.");
350                                 xco+= XIC;
351                         }
352                         else {
353                                 uiDefBut(block, BUT, users, str1, xco, yco, XIC+10, YIC, 0, 0, 0, 0, 0, "Displays number of users of this data. Click to make a single-user copy.");
354                                 xco+= XIC+10;
355                         }
356                         
357                         uiClearButLock();
358                         
359                 }
360                 
361                 if(del) {
362
363                         uiSetButLock (pin && *pinpoin, "Can't unlink pinned data");
364                         if(parid && parid->lib);
365                         else {
366                                 uiDefIconBut(block, BUT, del, ICON_X, xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Deletes link to this Datablock");
367                                 xco+= XIC;
368                         }
369
370                         uiClearButLock();
371                 }
372
373                 if(autobut) {
374                         if(parid && parid->lib);
375                         else {
376                                 uiDefIconBut(block, BUT, autobut, ICON_AUTO,xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Generates an automatic name");
377                                 xco+= XIC;
378                         }
379                         
380                         
381                 }
382                 if(keepbut) {
383                         uiDefBut(block, BUT, keepbut, "F", xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Saves this datablock even if it has no users");  
384                         xco+= XIC;
385                 }
386         }
387         else if(add_addbutton) {        /* "add new" button */
388                 uiBlockSetCol(block, oldcol);
389                 uiDefButS(block, TOG, browse, "Add New" ,xco, yco, 110, YIC, menupoin, (float)*menupoin, 32767.0, 0, 0, "Add new data block");
390                 xco+= 110;
391         }
392         //xco+=XIC;
393         
394         uiBlockSetCol(block, oldcol);
395         uiBlockEndAlign(block);
396
397         return xco;
398 }
399
400
401 /* results in fully updated anim system */
402 static void do_update_for_newframe(int mute, int events)
403 {
404         extern void audiostream_scrub(unsigned int frame);      /* seqaudio.c */
405         
406         if(events) {
407                 allqueue(REDRAWALL, 0);
408         }
409         
410         /* this function applies the changes too */
411         scene_update_for_newframe(G.scene, screen_view3d_layers()); /* BKE_scene.h */
412
413         if ( (CFRA>1) && (!mute) && (G.scene->audio.flag & AUDIO_SCRUB)) audiostream_scrub( CFRA );
414 }
415
416 void update_for_newframe(void)
417 {
418         do_update_for_newframe(0, 1);
419 }
420
421 void update_for_newframe_muted(void)
422 {
423         do_update_for_newframe(1, 1);
424 }
425
426 /* used by new animated UI playback */
427 void update_for_newframe_nodraw(int nosound)
428 {
429         do_update_for_newframe(nosound, 0);
430 }
431
432
433 static void show_splash(void)
434 {
435         extern char datatoc_splash_jpg[];
436         extern int datatoc_splash_jpg_size;
437         char *string = NULL;
438
439 #ifdef NAN_BUILDINFO
440         char buffer[1024];
441         extern char * build_date;
442         extern char * build_time;
443         extern char * build_platform;
444         extern char * build_type;
445
446         string = &buffer[0];
447         sprintf(string,"Built on %s %s     Version %s %s", build_date, build_time, build_platform, build_type);
448 #endif
449
450         splash((void *)datatoc_splash_jpg, datatoc_splash_jpg_size, string);
451 }
452
453
454 /* Functions for user preferences fileselect windows */
455
456 /* yafray: export dir select */
457 static void filesel_u_yfexportdir(char *name)
458 {
459         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
460         BLI_split_dirfile(name, dir, file);
461
462         strcpy(U.yfexportdir, dir);
463         allqueue(REDRAWALL, 0);
464 }
465
466 static void filesel_u_fontdir(char *name)
467 {
468         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
469         BLI_split_dirfile(name, dir, file);
470
471         strcpy(U.fontdir, dir);
472         allqueue(REDRAWALL, 0);
473 }
474
475 static void filesel_u_textudir(char *name)
476 {
477         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
478         BLI_split_dirfile(name, dir, file);
479
480         strcpy(U.textudir, dir);
481         allqueue(REDRAWALL, 0);
482 }
483
484 static void filesel_u_plugtexdir(char *name)
485 {
486         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
487         BLI_split_dirfile(name, dir, file);
488
489         strcpy(U.plugtexdir, dir);
490         allqueue(REDRAWALL, 0);
491 }
492
493 static void filesel_u_plugseqdir(char *name)
494 {
495         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
496         BLI_split_dirfile(name, dir, file);
497
498         strcpy(U.plugseqdir, dir);
499         allqueue(REDRAWALL, 0);
500 }
501
502 static void filesel_u_renderdir(char *name)
503 {
504         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
505         BLI_split_dirfile(name, dir, file);
506
507         strcpy(U.renderdir, dir);
508         allqueue(REDRAWALL, 0);
509 }
510
511 static void filesel_u_pythondir(char *name)
512 {
513         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
514         BLI_split_dirfile(name, dir, file);
515
516         strcpy(U.pythondir, dir);
517         allqueue(REDRAWALL, 0);
518 }
519
520 static void filesel_u_sounddir(char *name)
521 {
522         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
523         BLI_split_dirfile(name, dir, file);
524
525         strcpy(U.sounddir, dir);
526         allqueue(REDRAWALL, 0);
527 }
528
529 static void filesel_u_tempdir(char *name)
530 {
531         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
532         BLI_split_dirfile(name, dir, file);
533
534         strcpy(U.tempdir, dir);
535         allqueue(REDRAWALL, 0);
536 }
537
538 /* END Functions for user preferences fileselect windows */
539
540
541 void do_global_buttons(unsigned short event)
542 {
543         ListBase *lb;
544         Object *ob;
545         Material *ma;
546         MTex *mtex;
547         Ipo *ipo;
548         Lamp *la;
549         World *wrld;
550         bAction *act;
551         ID *id, *idtest, *from=NULL;
552         ScrArea *sa;
553         int nr= 1;
554         char buf[FILE_MAXDIR+FILE_MAXFILE];
555
556         ob= OBACT;
557
558         id= NULL;       /* id at null for texbrowse */
559
560
561         switch(event) {
562         
563         case B_NEWFRAME:
564                 scrarea_queue_winredraw(curarea);
565                 scrarea_queue_headredraw(curarea);
566
567                 update_for_newframe();
568                 break;          
569         case B_REDR:
570                 scrarea_queue_winredraw(curarea);
571                 scrarea_queue_headredraw(curarea);
572                 break;
573         case B_REDRCURW3D:
574                 allqueue(REDRAWVIEW3D, 0);
575                 scrarea_queue_winredraw(curarea);
576                 scrarea_queue_headredraw(curarea);
577                 break;
578         case B_EDITBROWSE:
579                 if(ob==0) return;
580                 if(ob->id.lib) return;
581                 id= ob->data;
582                 if(id==0) return;
583
584                 if(G.buts->menunr== -2) {
585                         activate_databrowse((ID *)G.buts->lockpoin, GS(id->name), 0, B_EDITBROWSE, &G.buts->menunr, do_global_buttons);
586                         return;
587                 }
588                 if(G.buts->menunr < 0) return;
589                 
590                 lb= wich_libbase(G.main, GS(id->name));
591                 idtest= lb->first;
592                 while(idtest) {
593                         if(nr==G.buts->menunr) {
594                                 if(idtest!=id) {
595                                         id->us--;
596                                         id_us_plus(idtest);
597                                         
598                                         ob->data= idtest;
599                                         
600                                         test_object_materials(idtest);
601                                         
602                                         if( GS(idtest->name)==ID_CU ) {
603                                                 test_curve_type(ob);
604                                         }
605                                         else if( ob->type==OB_ARMATURE) {
606                                                 armature_rebuild_pose(ob, ob->data);
607                                         }
608                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
609                                         
610                                         allqueue(REDRAWBUTSEDIT, 0);
611                                         allqueue(REDRAWVIEW3D, 0);
612                                         allqueue(REDRAWACTION,0);
613                                         allqueue(REDRAWIPO, 0);
614                                         allqueue(REDRAWNLA,0);
615                                 }
616                                 break;
617                         }
618                         nr++;
619                         idtest= idtest->next;
620                 }
621
622                 break;
623         case B_MESHBROWSE:
624                 if(ob==0) return;
625                 if(ob->id.lib) return;
626                 
627                 id= ob->data;
628                 if(id==0) id= G.main->mesh.first;
629                 if(id==0) return;
630                 
631                 if(G.buts->menunr== -2) {
632                         activate_databrowse((ID *)G.buts->lockpoin, GS(id->name), 0, B_MESHBROWSE, &G.buts->menunr, do_global_buttons);
633                         return;
634                 }
635                 if(G.buts->menunr < 0) return;
636                 
637
638                 idtest= G.main->mesh.first;
639                 while(idtest) {
640                         if(nr==G.buts->menunr) {
641                                 set_mesh(ob, (Mesh *)idtest);
642                                 
643                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
644                                         
645                                 BIF_undo_push("Browse Mesh");
646                                 allqueue(REDRAWBUTSEDIT, 0);
647                                 allqueue(REDRAWVIEW3D, 0);
648                                 allqueue(REDRAWACTION,0);
649                                 allqueue(REDRAWIPO, 0);
650
651                                 break;
652                         }
653                         nr++;
654                         idtest= idtest->next;
655                 }
656
657                 break;
658         case B_MATBROWSE:
659                 if(G.buts->menunr== -2) {
660                         activate_databrowse((ID *)G.buts->lockpoin, ID_MA, 0, B_MATBROWSE, &G.buts->menunr, do_global_buttons);
661                         return;
662                 }
663                 
664                 if(G.buts->menunr < 0) return;
665                 
666                 if(G.buts->pin) {
667                         
668                 }
669                 else {
670                         
671                         ma= give_current_material(ob, ob->actcol);
672                         nr= 1;
673                         
674                         id= (ID *)ma;
675                         
676                         idtest= G.main->mat.first;
677                         while(idtest) {
678                                 if(nr==G.buts->menunr) {
679                                         break;
680                                 }
681                                 nr++;
682                                 idtest= idtest->next;
683                         }
684                         if(idtest==0) { /* new mat */
685                                 if(id)  idtest= (ID *)copy_material((Material *)id);
686                                 else {
687                                         idtest= (ID *)add_material("Material");
688                                 }
689                                 idtest->us--;
690                         }
691                         if(idtest!=id) {
692                                 assign_material(ob, (Material *)idtest, ob->actcol);
693                                 
694                                 BIF_undo_push("Browse Material");
695                                 allqueue(REDRAWBUTSSHADING, 0);
696                                 allqueue(REDRAWIPO, 0);
697                                 allqueue(REDRAWNODE, 0);
698                                 BIF_preview_changed(ID_MA);
699                         }
700                         
701                 }
702                 break;
703         case B_MATDELETE:
704                 if(G.buts->pin) {
705                         
706                 }
707                 else {
708                         ma= give_current_material(ob, ob->actcol);
709                         if(ma) {
710                                 assign_material(ob, 0, ob->actcol);
711                                 BIF_undo_push("Unlink Material");
712                                 allqueue(REDRAWBUTSSHADING, 0);
713                                 allqueue(REDRAWIPO, 0);
714                                 allqueue(REDRAWOOPS, 0);
715                                 BIF_preview_changed(ID_MA);
716                         }
717                 }
718                 break;
719         case B_TEXDELETE:
720                 if(G.buts->pin) {
721                         
722                 }
723                 else {
724                         if(G.buts->texfrom==0) {        /* from mat */
725                                 ma= give_current_material(ob, ob->actcol);
726                                 ma= get_active_matlayer(ma);
727                                 if(ma) {
728                                         mtex= ma->mtex[ ma->texact ];
729                                         if(mtex) {
730                                                 if(mtex->tex) mtex->tex->id.us--;
731                                                 MEM_freeN(mtex);
732                                                 ma->mtex[ ma->texact ]= NULL;
733                                                 allqueue(REDRAWBUTSSHADING, 0);
734                                                 allqueue(REDRAWIPO, 0);
735                                                 BIF_preview_changed(ID_MA);
736                                         }
737                                 }
738                         }
739                         else if(G.buts->texfrom==1) { /* from world */
740                                 wrld= G.scene->world;
741                                 if(wrld) {
742                                         mtex= wrld->mtex[ wrld->texact ];
743                                         if(mtex) {
744                                                 if(mtex->tex) mtex->tex->id.us--;
745                                                 MEM_freeN(mtex);
746                                                 wrld->mtex[ wrld->texact ]= NULL;
747                                                 allqueue(REDRAWBUTSSHADING, 0);
748                                                 allqueue(REDRAWIPO, 0);
749                                                 BIF_preview_changed(ID_WO);
750                                         }
751                                 }
752                         }
753                         else {  /* from lamp */
754                                 la= ob->data;
755                                 if(la && ob->type==OB_LAMP) { /* to be sure */
756                                         mtex= la->mtex[ la->texact ];
757                                         if(mtex) {
758                                                 if(mtex->tex) mtex->tex->id.us--;
759                                                 MEM_freeN(mtex);
760                                                 la->mtex[ la->texact ]= NULL;
761                                                 allqueue(REDRAWBUTSSHADING, 0);
762                                                 allqueue(REDRAWIPO, 0);
763                                                 BIF_preview_changed(ID_LA);
764                                         }
765                                 }
766                         }
767                         BIF_undo_push("Unlink Texture");
768                 }
769                 break;
770         case B_EXTEXBROWSE: 
771         case B_TEXBROWSE:
772
773                 if(G.buts->texnr== -2) {
774                         
775                         id= G.buts->lockpoin;
776                         if(event==B_EXTEXBROWSE) {
777                                 id= NULL;
778                                 ma= give_current_material(ob, ob->actcol);
779                                 ma= get_active_matlayer(ma);
780                                 if(ma) {
781                                         mtex= ma->mtex[ ma->texact ];
782                                         if(mtex) id= (ID *)mtex->tex;
783                                 }
784                         }
785                         
786                         activate_databrowse(id, ID_TE, 0, B_TEXBROWSE, &G.buts->texnr, do_global_buttons);
787                         return;
788                 }
789                 if(G.buts->texnr < 0) break;
790                 
791                 if(G.buts->pin) {
792                         
793                 }
794                 else {
795                         id= NULL;
796                         
797                         ma= give_current_material(ob, ob->actcol);
798                         ma= get_active_matlayer(ma);
799                         if(ma) {
800                                 mtex= ma->mtex[ ma->texact ];
801                                 if(mtex) id= (ID *)mtex->tex;
802                         }
803
804                         idtest= G.main->tex.first;
805                         while(idtest) {
806                                 if(nr==G.buts->texnr) {
807                                         break;
808                                 }
809                                 nr++;
810                                 idtest= idtest->next;
811                         }
812                         if(idtest==0) { /* new tex */
813                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
814                                 else idtest= (ID *)add_texture("Tex");
815                                 idtest->us--;
816                         }
817                         if(idtest!=id && ma) {
818                                 
819                                 if( ma->mtex[ma->texact]==0) ma->mtex[ma->texact]= add_mtex();
820                                 
821                                 ma->mtex[ ma->texact ]->tex= (Tex *)idtest;
822                                 id_us_plus(idtest);
823                                 if(id) id->us--;
824                                 
825                                 BIF_undo_push("Browse Texture");
826                                 allqueue(REDRAWBUTSSHADING, 0);
827                                 allqueue(REDRAWIPO, 0);
828                                 allqueue(REDRAWOOPS, 0);
829                                 BIF_preview_changed(ID_MA);
830                         }
831                 }
832                 break;
833         case B_ACTIONDELETE:
834                 act=ob->action;
835                 
836                 if (act)
837                         act->id.us--;
838                 ob->action=NULL;
839                 if(ob->pose) {          // clear flag, also used for draw colors
840                         bPoseChannel *pchan;
841                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next)
842                                 pchan->flag= 0;
843                 }
844                 BIF_undo_push("Unlink Action");
845                 
846                 allqueue(REDRAWVIEW3D, 0);
847                 allqueue(REDRAWACTION, 0);
848                 allqueue(REDRAWNLA, 0);
849                 allqueue(REDRAWIPO, 0);
850                 break;
851         case B_ACTIONBROWSE:
852                 if (!ob)
853                         break;
854                 act=ob->action;
855                 id= (ID *)act;
856
857                 if (G.saction->actnr== -2){
858                                 activate_databrowse((ID *)G.saction->action, ID_AC,  0, B_ACTIONBROWSE, &G.saction->actnr, do_global_buttons);
859                         return;
860                 }
861
862                 if(G.saction->actnr < 0) break;
863
864                 /*      See if we have selected a valid action */
865                 for (idtest= G.main->action.first; idtest; idtest= idtest->next) {
866                                 if(nr==G.saction->actnr) {
867                                         break;
868                                 }
869                                 nr++;
870                         
871                 }
872
873                 if(G.saction->pin) {
874                         G.saction->action= (bAction *)idtest;
875                         allqueue(REDRAWACTION, 0);
876                 }
877                 else {
878
879                         /* Store current action */
880                         if (!idtest){
881                                 if (act)
882                                         idtest= (ID *)copy_action(act);
883                                 else 
884                                         idtest=(ID *)add_empty_action(ob->type==OB_ARMATURE?ID_PO:ID_OB);
885                                 idtest->us--;
886                         }
887                         
888                         
889                         if(idtest!=id && ob) {
890                                 act= (bAction *)idtest;
891                                 
892                                 ob->action= act;
893                                 id_us_plus(idtest);
894                                 
895                                 if(id) id->us--;
896                                 
897                                 // Update everything
898                                 BIF_undo_push("Browse Action");
899                                 do_global_buttons (B_NEWFRAME);
900                                 allqueue(REDRAWVIEW3D, 0);
901                                 allqueue(REDRAWNLA, 0);
902                                 allqueue(REDRAWACTION, 0);
903                                 allqueue(REDRAWHEADERS, 0); 
904                         }
905                 }
906                 
907                 break;
908         case B_IPOBROWSE:
909
910                 ipo= G.sipo->ipo;
911                 from= G.sipo->from;
912                 id= (ID *)ipo;
913                 if(from==NULL) return;
914
915                 if(G.sipo->menunr== -2) {
916                         activate_databrowse((ID *)G.sipo->ipo, ID_IP, GS(from->name), B_IPOBROWSE, &G.sipo->menunr, do_global_buttons);
917                         return;
918                 }
919
920                 if(G.sipo->menunr < 0) break;
921
922                 idtest= G.main->ipo.first;
923                 while(idtest) {
924                         if( ((Ipo *)idtest)->blocktype == G.sipo->blocktype) {
925                                 if(nr==G.sipo->menunr) {
926                                         break;
927                                 }
928                                 nr++;
929                         }
930                         idtest= idtest->next;
931                 }
932
933                 if(G.sipo->pin) {
934                         if(idtest) {
935                                 G.sipo->ipo= (Ipo *)idtest;
936                                 allspace(REMAKEIPO, 0);         // in fact it should only do this one, but there is no function for it
937                         }
938                 }
939                 else {
940                         // assign the ipo to ...
941
942                         if(idtest==0) {
943                                 if(ipo) idtest= (ID *)copy_ipo(ipo);
944                                 else {
945                                         nr= G.sipo->blocktype;
946                                         if(nr==ID_OB) idtest= (ID *)add_ipo("ObIpo", ID_OB);
947                                         else if(nr==ID_CO) idtest= (ID *)add_ipo("CoIpo", ID_CO);
948                                         else if(nr==ID_PO) idtest= (ID *)add_ipo("ActIpo", nr);
949                                         else if(nr==ID_MA) idtest= (ID *)add_ipo("MatIpo", nr);
950                                         else if(nr==ID_TE) idtest= (ID *)add_ipo("TexIpo", nr);
951                                         else if(nr==ID_SEQ) idtest= (ID *)add_ipo("MatSeq", nr);
952                                         else if(nr==ID_CU) idtest= (ID *)add_ipo("CuIpo", nr);
953                                         else if(nr==ID_KE) idtest= (ID *)add_ipo("KeyIpo", nr);
954                                         else if(nr==ID_WO) idtest= (ID *)add_ipo("WoIpo", nr);
955                                         else if(nr==ID_LA) idtest= (ID *)add_ipo("LaIpo", nr);
956                                         else if(nr==ID_CA) idtest= (ID *)add_ipo("CaIpo", nr);
957                                         else if(nr==ID_SO) idtest= (ID *)add_ipo("SndIpo", nr);
958                                         else error("Warn bugtracker!");
959                                 }
960                                 idtest->us--;
961                         }
962                         if(idtest!=id && from) {
963                                 spaceipo_assign_ipo(G.sipo, (Ipo *)idtest);
964                                                                         
965                                 BIF_undo_push("Browse Ipo");
966                         }
967                 }
968                 break;
969         case B_IPODELETE:
970                 ipo= G.sipo->ipo;
971                 from= G.sipo->from;
972                 
973                 spaceipo_assign_ipo(G.sipo, NULL);
974                 
975                 editipo_changed(G.sipo, 1); /* doredraw */
976                 
977                 BIF_undo_push("Unlink Ipo");
978                 
979                 break;
980         case B_WORLDBROWSE:
981
982                 if(G.buts->menunr==-2) {
983                         activate_databrowse((ID *)G.scene->world, ID_WO, 0, B_WORLDBROWSE, &G.buts->menunr, do_global_buttons);
984                         break;
985                 }
986
987                 if(G.buts->menunr < 0) break;
988                 /* no lock */
989                         
990                 wrld= G.scene->world;
991                 nr= 1;
992                 
993                 id= (ID *)wrld;
994                 
995                 idtest= G.main->world.first;
996                 while(idtest) {
997                         if(nr==G.buts->menunr) {
998                                 break;
999                         }
1000                         nr++;
1001                         idtest= idtest->next;
1002                 }
1003                 if(idtest==0) { /* new world */
1004                         if(id) idtest= (ID *)copy_world((World *)id);
1005                         else idtest= (ID *)add_world("World");
1006                         idtest->us--;
1007                 }
1008                 if(idtest!=id) {
1009                         G.scene->world= (World *)idtest;
1010                         id_us_plus(idtest);
1011                         if(id) id->us--;
1012                         
1013                         BIF_undo_push("Browse World");
1014                         allqueue(REDRAWBUTSSHADING, 0);
1015                         allqueue(REDRAWIPO, 0);
1016                         allqueue(REDRAWOOPS, 0);
1017                         BIF_preview_changed(ID_WO);
1018                 }
1019                 break;
1020         case B_WORLDDELETE:
1021                 if(G.scene->world) {
1022                         G.scene->world->id.us--;
1023                         G.scene->world= NULL;
1024
1025                         BIF_undo_push("Unlink World");
1026                         allqueue(REDRAWBUTSSHADING, 0);
1027                         allqueue(REDRAWIPO, 0);
1028                 }
1029                 
1030                 break;
1031         case B_WTEXBROWSE:
1032
1033                 if(G.buts->texnr== -2) {
1034                         id= NULL;
1035                         wrld= G.scene->world;
1036                         if(wrld) {
1037                                 mtex= wrld->mtex[ wrld->texact ];
1038                                 if(mtex) id= (ID *)mtex->tex;
1039                         }
1040
1041                         activate_databrowse((ID *)id, ID_TE, 0, B_WTEXBROWSE, &G.buts->texnr, do_global_buttons);
1042                         return;
1043                 }
1044                 if(G.buts->texnr < 0) break;
1045
1046                 if(G.buts->pin) {
1047                         
1048                 }
1049                 else {
1050                         id= NULL;
1051                         
1052                         wrld= G.scene->world;
1053                         if(wrld) {
1054                                 mtex= wrld->mtex[ wrld->texact ];
1055                                 if(mtex) id= (ID *)mtex->tex;
1056                         }
1057
1058                         idtest= G.main->tex.first;
1059                         while(idtest) {
1060                                 if(nr==G.buts->texnr) {
1061                                         break;
1062                                 }
1063                                 nr++;
1064                                 idtest= idtest->next;
1065                         }
1066                         if(idtest==0) { /* new tex */
1067                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
1068                                 else idtest= (ID *)add_texture("Tex");
1069                                 idtest->us--;
1070                         }
1071                         if(idtest!=id && wrld) {
1072                                 
1073                                 if( wrld->mtex[wrld->texact]==0) {
1074                                         wrld->mtex[wrld->texact]= add_mtex();
1075                                         wrld->mtex[wrld->texact]->texco= TEXCO_VIEW;
1076                                 }
1077                                 wrld->mtex[ wrld->texact ]->tex= (Tex *)idtest;
1078                                 id_us_plus(idtest);
1079                                 if(id) id->us--;
1080                                 
1081                                 BIF_undo_push("Texture browse");
1082                                 allqueue(REDRAWBUTSSHADING, 0);
1083                                 allqueue(REDRAWIPO, 0);
1084                                 allqueue(REDRAWOOPS, 0);
1085                                 BIF_preview_changed(ID_WO);
1086                         }
1087                 }
1088                 break;
1089         case B_LAMPBROWSE:
1090                 /* no lock */
1091                 if(ob==0) return;
1092                 if(ob->type!=OB_LAMP) return;
1093
1094                 if(G.buts->menunr== -2) {
1095                         activate_databrowse((ID *)G.buts->lockpoin, ID_LA, 0, B_LAMPBROWSE, &G.buts->menunr, do_global_buttons);
1096                         return;
1097                 }
1098                 if(G.buts->menunr < 0) break;
1099                 
1100                 la= ob->data;
1101                 nr= 1;
1102                 id= (ID *)la;
1103                 
1104                 idtest= G.main->lamp.first;
1105                 while(idtest) {
1106                         if(nr==G.buts->menunr) {
1107                                 break;
1108                         }
1109                         nr++;
1110                         idtest= idtest->next;
1111                 }
1112                 if(idtest==0) { /* no new lamp */
1113                         return;
1114                 }
1115                 if(idtest!=id) {
1116                         ob->data= (Lamp *)idtest;
1117                         id_us_plus(idtest);
1118                         if(id) id->us--;
1119                         
1120                         BIF_undo_push("Lamp browse");
1121                         allqueue(REDRAWBUTSSHADING, 0);
1122                         allqueue(REDRAWVIEW3D, 0);
1123                         allqueue(REDRAWIPO, 0);
1124                         allqueue(REDRAWOOPS, 0);
1125                         BIF_preview_changed(ID_LA);
1126                 }
1127                 break;
1128         
1129         case B_LTEXBROWSE:
1130
1131                 if(ob==0) return;
1132                 if(ob->type!=OB_LAMP) return;
1133
1134                 if(G.buts->texnr== -2) {
1135                         id= NULL;
1136                         la= ob->data;
1137                         mtex= la->mtex[ la->texact ];
1138                         if(mtex) id= (ID *)mtex->tex;
1139
1140                         activate_databrowse(id, ID_TE, 0, B_LTEXBROWSE, &G.buts->texnr, do_global_buttons);
1141                         return;
1142                 }
1143                 if(G.buts->texnr < 0) break;
1144
1145                 if(G.buts->pin) {
1146                         
1147                 }
1148                 else {
1149                         id= NULL;
1150                         
1151                         la= ob->data;
1152                         mtex= la->mtex[ la->texact ];
1153                         if(mtex) id= (ID *)mtex->tex;
1154
1155                         idtest= G.main->tex.first;
1156                         while(idtest) {
1157                                 if(nr==G.buts->texnr) {
1158                                         break;
1159                                 }
1160                                 nr++;
1161                                 idtest= idtest->next;
1162                         }
1163                         if(idtest==0) { /* new tex */
1164                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
1165                                 else idtest= (ID *)add_texture("Tex");
1166                                 idtest->us--;
1167                         }
1168                         if(idtest!=id && la) {
1169                                 
1170                                 if( la->mtex[la->texact]==0) {
1171                                         la->mtex[la->texact]= add_mtex();
1172                                         la->mtex[la->texact]->texco= TEXCO_GLOB;
1173                                 }
1174                                 la->mtex[ la->texact ]->tex= (Tex *)idtest;
1175                                 id_us_plus(idtest);
1176                                 if(id) id->us--;
1177                                 
1178                                 BIF_undo_push("Texture Browse");
1179                                 allqueue(REDRAWBUTSSHADING, 0);
1180                                 allqueue(REDRAWIPO, 0);
1181                                 allqueue(REDRAWOOPS, 0);
1182                                 BIF_preview_changed(ID_LA);
1183                         }
1184                 }
1185                 break;
1186         
1187         case B_IMAGEDELETE:
1188                 G.sima->image= NULL;
1189                 image_changed(G.sima, 0);
1190                 BIF_undo_push("Unlink Image");
1191                 allqueue(REDRAWIMAGE, 0);
1192                 break;
1193         
1194         case B_AUTOMATNAME:
1195                 automatname(G.buts->lockpoin);
1196
1197                 BIF_undo_push("Auto name");
1198                 allqueue(REDRAWBUTSSHADING, 0);
1199                 allqueue(REDRAWOOPS, 0);
1200                 break;          
1201         case B_AUTOTEXNAME:
1202                 if(G.buts->mainb==CONTEXT_SHADING) {
1203                         if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_TEX) {
1204                                 autotexname(G.buts->lockpoin);
1205                         }
1206                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_MAT) {
1207                                 ma= G.buts->lockpoin;
1208                                 if(ma->mtex[ ma->texact]) autotexname(ma->mtex[ma->texact]->tex);
1209                         }
1210                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_WORLD) {
1211                                 wrld= G.buts->lockpoin;
1212                                 if(wrld->mtex[ wrld->texact]) autotexname(wrld->mtex[wrld->texact]->tex);
1213                         }
1214                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_LAMP) {
1215                                 la= G.buts->lockpoin;
1216                                 if(la->mtex[ la->texact]) autotexname(la->mtex[la->texact]->tex);
1217                         }
1218                         BIF_undo_push("Auto name");
1219                         allqueue(REDRAWBUTSSHADING, 0);
1220                         allqueue(REDRAWOOPS, 0);
1221                 }
1222                 break;
1223
1224         case B_RESETAUTOSAVE:
1225                 reset_autosave();
1226                 allqueue(REDRAWINFO, 0);
1227                 break;
1228         case B_SOUNDTOGGLE:
1229                 SYS_WriteCommandLineInt(SYS_GetSystem(), "noaudio", (U.gameflags & USER_DISABLE_SOUND));
1230                 break;
1231         case B_SHOWSPLASH:
1232                                 show_splash();
1233                 break;
1234         case B_MIPMAPCHANGED:
1235                 set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
1236                 allqueue(REDRAWVIEW3D, 0);
1237                 break;
1238         case B_NEWSPACE:
1239                 newspace(curarea, curarea->butspacetype);
1240                 break;
1241         case B_LOADTEMP:        /* is button from space.c */
1242                 BIF_read_autosavefile();
1243                 break;
1244
1245         case B_USERPREF:
1246                 allqueue(REDRAWINFO, 0);
1247                 break;
1248
1249         case B_DRAWINFO:        /* is button from space.c  *info* */
1250                 allqueue(REDRAWVIEW3D, 0);
1251                 break;
1252
1253         case B_PLAINMENUS:     /* is button from space.c  *info* */
1254                 reset_toolbox();
1255                 break;
1256
1257         case B_FLIPINFOMENU:    /* is button from space.c  *info* */
1258                 scrarea_queue_headredraw(curarea);
1259                 break;
1260
1261 #if 0
1262 //#ifdef _WIN32 // FULLSCREEN
1263         case B_FLIPFULLSCREEN:
1264                 if(U.uiflag & USER_FLIPFULLSCREEN)
1265                         U.uiflag &= ~USER_FLIPFULLSCREEN;
1266                 else
1267                         U.uiflag |= USER_FLIPFULLSCREEN;
1268                 mainwindow_toggle_fullscreen((U.uiflag & USER_FLIPFULLSCREEN));
1269                 break;
1270 #endif
1271
1272         /* Fileselect windows for user preferences file paths */
1273
1274         /* yafray: xml export dir. select */
1275         case B_YAFRAYDIRFILESEL:        /* space.c */
1276                 if(curarea->spacetype==SPACE_INFO) {
1277                         sa= closest_bigger_area();
1278                         areawinset(sa->win);
1279                 }
1280
1281                 activate_fileselect(FILE_SPECIAL, "SELECT YFEXPORT PATH", U.yfexportdir, filesel_u_yfexportdir);
1282                 break;
1283
1284         case B_FONTDIRFILESEL:  /* is button from space.c  *info* */
1285                 if(curarea->spacetype==SPACE_INFO) {
1286                         sa= closest_bigger_area();
1287                         areawinset(sa->win);
1288                 }
1289
1290                 activate_fileselect(FILE_SPECIAL, "SELECT FONT PATH", U.fontdir, filesel_u_fontdir);
1291                 break;
1292
1293         case B_TEXTUDIRFILESEL:         /* is button from space.c  *info* */
1294                 if(curarea->spacetype==SPACE_INFO) {
1295                         sa= closest_bigger_area();
1296                         areawinset(sa->win);
1297                 }
1298
1299                 activate_fileselect(FILE_SPECIAL, "SELECT TEXTURE PATH", U.textudir, filesel_u_textudir);
1300                 break;
1301         
1302         case B_PLUGTEXDIRFILESEL:               /* is button form space.c  *info* */
1303                 if(curarea->spacetype==SPACE_INFO) {
1304                         sa= closest_bigger_area();
1305                         areawinset(sa->win);
1306                 }
1307
1308                 activate_fileselect(FILE_SPECIAL, "SELECT TEX PLUGIN PATH", U.plugtexdir, filesel_u_plugtexdir);
1309                 break;
1310         
1311         case B_PLUGSEQDIRFILESEL:               /* is button from space.c  *info* */
1312                 if(curarea->spacetype==SPACE_INFO) {
1313                         sa= closest_bigger_area();
1314                         areawinset(sa->win);
1315                 }
1316
1317                 activate_fileselect(FILE_SPECIAL, "SELECT SEQ PLUGIN PATH", U.plugseqdir, filesel_u_plugseqdir);
1318                 break;
1319         
1320         case B_RENDERDIRFILESEL:        /* is button from space.c  *info* */
1321                 if(curarea->spacetype==SPACE_INFO) {
1322                         sa= closest_bigger_area();
1323                         areawinset(sa->win);
1324                 }
1325
1326                 activate_fileselect(FILE_SPECIAL, "SELECT RENDER PATH", U.renderdir, filesel_u_renderdir);
1327                 break;
1328
1329         case B_PYMENUEVAL: /* is button from space.c *info* */
1330                 BPyMenu_RemoveAllEntries(); /* free old data */
1331                 if (BPyMenu_Init(1) == -1) /* re-eval scripts registration in menus */
1332                         error("Invalid scripts dir: check console");
1333                 break;
1334         case B_PYTHONDIRFILESEL:        /* is button from space.c  *info* */
1335                 if(curarea->spacetype==SPACE_INFO) {
1336                         sa= closest_bigger_area();
1337                         areawinset(sa->win);
1338                 }
1339
1340                 activate_fileselect(FILE_SPECIAL, "SELECT SCRIPT PATH", U.pythondir, filesel_u_pythondir);
1341                 break;
1342
1343         case B_SOUNDDIRFILESEL:         /* is button from space.c  *info* */
1344                 if(curarea->spacetype==SPACE_INFO) {
1345                         sa= closest_bigger_area();
1346                         areawinset(sa->win);
1347                 }
1348
1349                 activate_fileselect(FILE_SPECIAL, "SELECT SOUND PATH", U.sounddir, filesel_u_sounddir);
1350                 break;
1351
1352         case B_TEMPDIRFILESEL:  /* is button from space.c  *info* */
1353                 if(curarea->spacetype==SPACE_INFO) {
1354                         sa= closest_bigger_area();
1355                         areawinset(sa->win);
1356                 }
1357
1358                 activate_fileselect(FILE_SPECIAL, "SELECT TEMP FILE PATH", U.tempdir, filesel_u_tempdir);
1359                 break;
1360
1361         /* END Fileselect windows for user preferences file paths */
1362
1363 #ifdef INTERNATIONAL
1364         case B_LOADUIFONT:      /* is button from space.c  *info* */
1365                 if(curarea->spacetype==SPACE_INFO) {
1366                         sa= closest_bigger_area();
1367                         areawinset(sa->win);
1368                 }
1369                 BLI_make_file_string("/", buf, U.fontdir, U.fontname);
1370                 activate_fileselect(FILE_SPECIAL, "LOAD UI FONT", buf, set_interface_font);
1371                 break;
1372
1373         case B_SETLANGUAGE:             /* is button from space.c  *info* */
1374                 lang_setlanguage();
1375                 allqueue(REDRAWALL, 0);
1376                 break;
1377
1378         case B_SETFONTSIZE:             /* is button from space.c  *info* */
1379                 refresh_interface_font();
1380                 FTF_SetSize(U.fontsize); 
1381                 allqueue(REDRAWALL, 0);
1382                 break;
1383                 
1384         case B_SETTRANSBUTS:    /* is button from space.c  *info* */
1385                 allqueue(REDRAWALL, 0);
1386                 break;
1387
1388         case B_RESTOREFONT:             /* is button from space.c  *info* */
1389                 U.fontsize= 0;
1390                 start_interface_font();
1391                 allqueue(REDRAWALL, 0);
1392                 break;
1393                 
1394         case B_USETEXTUREFONT:          /* is button from space.c  *info* */
1395                 refresh_interface_font();
1396                 allqueue(REDRAWALL, 0);
1397                 break;
1398
1399         case B_DOLANGUIFONT:    /* is button from space.c  *info* */
1400                 if(U.transopts & USER_DOTRANSLATE)
1401                         start_interface_font();
1402                 else
1403                         G.ui_international = FALSE;
1404                 allqueue(REDRAWALL, 0);
1405                 break;
1406 #endif
1407                 
1408         case B_FULL:
1409                 if(curarea->spacetype!=SPACE_INFO) {
1410                         area_fullscreen();
1411                 }
1412                 break;  
1413
1414         case B_IDNAME:
1415                         /* changing a metaballs name, sadly enough,
1416                          * can require it to be updated because its
1417                          * basis might have changed... -zr
1418                          */
1419                 if (ob && ob->type==OB_MBALL)
1420                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1421                         
1422                 /* redraw because name has changed: new pup */
1423                 scrarea_queue_headredraw(curarea);
1424                 allqueue(REDRAWINFO, 1);
1425                 allqueue(REDRAWOOPS, 1);
1426                 allqueue(REDRAWACTION, 1);
1427                 allqueue(REDRAWNLA, 1);
1428                 /* name scene also in set PUPmenu */
1429                 allqueue(REDRAWBUTSALL, 0);
1430                 allqueue(REDRAWHEADERS, 0);
1431
1432                 break;
1433         
1434         case B_KEEPDATA:
1435                 /* keep datablock. similar to pressing FKEY in a fileselect window
1436                  * maybe we can move that stuff to a seperate function? -- sg
1437                  */
1438                 if (curarea->spacetype==SPACE_BUTS) {
1439                         id= (ID *)G.buts->lockpoin;
1440                 } else if(curarea->spacetype==SPACE_IPO) {
1441                         id = (ID *)G.sipo->ipo;
1442                 } /* similar for other spacetypes ? */
1443                 if (id) {
1444                         if( id->flag & LIB_FAKEUSER) {
1445                                 id->flag -= LIB_FAKEUSER;
1446                                 id->us--;
1447                         } else {
1448                                 id->flag |= LIB_FAKEUSER;
1449                                 id->us++;
1450                         }
1451                 }
1452                 allqueue(REDRAWHEADERS, 0);
1453
1454                 break;
1455
1456         }
1457 }
1458
1459
1460 void do_global_buttons2(short event)
1461 {
1462         Base *base;
1463         Object *ob;
1464         Material *ma;
1465         MTex *mtex;
1466         Mesh *me;
1467         Curve *cu;
1468         MetaBall *mb;
1469         Ipo *ipo;
1470         Lamp *la;
1471         Lattice *lt;
1472         World *wrld;
1473         ID *idfrom; 
1474         bAction *act;
1475
1476         /* general:  Single User is allowed when from==LOCAL 
1477          *                       Make Local is allowed when (from==LOCAL && id==LIB)
1478          */
1479                 
1480         if(event<B_LOCAL_ALONE) return;
1481
1482         ob= OBACT;
1483
1484         switch(event) {
1485                 
1486         case B_LAMPALONE:
1487                 if(ob && ob->id.lib==0) {
1488                         la= ob->data;
1489                         if(la->id.us>1) {
1490                                 if(okee("Single user")) {
1491                                         ob->data= copy_lamp(la);
1492                                         la->id.us--;
1493                                 }
1494                         }
1495                 }
1496                 break;
1497         case B_LAMPLOCAL:
1498                 if(ob && ob->id.lib==0) {
1499                         la= ob->data;
1500                         if(la->id.lib) {
1501                                 if(okee("Make local")) {
1502                                         make_local_lamp(la);
1503                                 }
1504                         }
1505                 }
1506                 break;
1507         
1508         case B_ARMLOCAL:
1509                 if (ob&&ob->id.lib==0){
1510                         bArmature *arm=ob->data;
1511                         if (arm->id.lib){
1512                                 if(okee("Make local")) {
1513                                         make_local_armature(arm);
1514                                 }
1515                         }
1516                 }
1517                 break;
1518         case B_ARMALONE:
1519                 if(ob && ob->id.lib==0) {
1520                         bArmature *arm=ob->data;
1521                         if(arm->id.us>1) {
1522                                 if(okee("Single user")) {
1523                                         ob->data= copy_armature(arm);
1524                                         armature_rebuild_pose(ob, ob->data);
1525                                         arm->id.us--;
1526                                 }
1527                         }
1528                 }
1529                 break;
1530         case B_ACTLOCAL:
1531                 if(ob && ob->id.lib==0) {
1532                         act= ob->action;
1533                         if(act->id.lib) {
1534                                 if(okee("Make local")) {
1535                                         make_local_action(act);
1536                                         allqueue(REDRAWACTION,0);
1537                                 }
1538                         }
1539                 }
1540                 break;
1541         case B_ACTALONE:
1542                 if(ob && ob->id.lib==0) {
1543                         act= ob->action;
1544                 
1545                         if(act->id.us>1) {
1546                                 if(okee("Single user")) {
1547                                         ob->action=copy_action(act);
1548                                         act->id.us--;
1549                                         allqueue(REDRAWACTION, 0);
1550                                 }
1551                         }
1552                 }
1553                 break;
1554
1555         case B_CAMERAALONE:
1556                 if(ob && ob->id.lib==0) {
1557                         Camera *ca= ob->data;
1558                         if(ca->id.us>1) {
1559                                 if(okee("Single user")) {
1560                                         ob->data= copy_camera(ca);
1561                                         ca->id.us--;
1562                                 }
1563                         }
1564                 }
1565                 break;
1566         case B_CAMERALOCAL:
1567                 if(ob && ob->id.lib==0) {
1568                         Camera *ca= ob->data;
1569                         if(ca->id.lib) {
1570                                 if(okee("Make local")) {
1571                                         make_local_camera(ca);
1572                                 }
1573                         }
1574                 }
1575                 break;
1576         case B_WORLDALONE:
1577                 wrld= G.scene->world;
1578                 if(wrld->id.us>1) {
1579                         if(okee("Single user")) {
1580                                 G.scene->world= copy_world(wrld);
1581                                 wrld->id.us--;
1582                         }
1583                 }
1584                 break;
1585         case B_WORLDLOCAL:
1586                 wrld= G.scene->world;
1587                 if(wrld && wrld->id.lib) {
1588                         if(okee("Make local")) {
1589                                 make_local_world(wrld);
1590                         }
1591                 }
1592                 break;
1593
1594         case B_LATTALONE:
1595                 if(ob && ob->id.lib==0) {
1596                         lt= ob->data;
1597                         if(lt->id.us>1) {
1598                                 if(okee("Single user")) {
1599                                         ob->data= copy_lattice(lt);
1600                                         lt->id.us--;
1601                                 }
1602                         }
1603                 }
1604                 break;
1605         case B_LATTLOCAL:
1606                 if(ob && ob->id.lib==0) {
1607                         lt= ob->data;
1608                         if(lt->id.lib) {
1609                                 if(okee("Make local")) {
1610                                         make_local_lattice(lt);
1611                                 }
1612                         }
1613                 }
1614                 break;
1615         
1616         case B_MATALONE:
1617                 if(ob==0) return;
1618                 ma= give_current_material(ob, ob->actcol);
1619                 idfrom= material_from(ob, ob->actcol);
1620                 if(idfrom && idfrom->lib==0) {
1621                         if(ma->id.us>1) {
1622                                 if(okee("Single user")) {
1623                                         ma= copy_material(ma);
1624                                         ma->id.us= 0;
1625                                         assign_material(ob, ma, ob->actcol);
1626                                 }
1627                         }
1628                 }
1629                 break;
1630         case B_MATLOCAL:
1631                 if(ob==0) return;
1632                 idfrom= material_from(ob, ob->actcol);
1633                 if(idfrom->lib==0) {
1634                         ma= give_current_material(ob, ob->actcol);
1635                         if(ma && ma->id.lib) {
1636                                 if(okee("Make local")) {
1637                                         MaterialLayer *ml;
1638                                         
1639                                         make_local_material(ma);
1640                                         for(ml= ma->layers.first; ml; ml= ml->next) {
1641                                                 if(ml->mat)
1642                                                         make_local_material(ml->mat);
1643                                         }
1644                                 }
1645                         }
1646                 }
1647                 break;
1648
1649         case B_MESHLOCAL:
1650                 if(ob && ob->id.lib==0) {
1651                         me= ob->data;
1652                         if(me && me->id.lib) {
1653                                 if(okee("Make local")) {
1654                                         make_local_mesh(me);
1655                                         make_local_key( me->key );
1656
1657                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);                           
1658                                 }
1659                         }
1660                 }
1661                 break;
1662
1663         case B_MBALLALONE:
1664                 if(ob && ob->id.lib==0) {
1665                         mb= ob->data;
1666                         if(mb->id.us>1) {
1667                                 if(okee("Single user")) {
1668                                         ob->data= copy_mball(mb);
1669                                         mb->id.us--;
1670                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1671                                 }
1672                         }
1673                 }
1674                 break;
1675         case B_MBALLLOCAL:
1676                 if(ob && ob->id.lib==0) {
1677                         mb= ob->data;
1678                         if(mb->id.lib) {
1679                                 if(okee("Make local")) {
1680                                         make_local_mball(mb);
1681                                 }
1682                         }
1683                 }
1684                 break;
1685
1686         case B_CURVEALONE:
1687                 if(ob && ob->id.lib==0) {
1688                         cu= ob->data;
1689                         if(cu->id.us>1) {
1690                                 if(okee("Single user")) {
1691                                         ob->data= copy_curve(cu);
1692                                         cu->id.us--;
1693                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1694                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1695                                 }
1696                         }
1697                 }
1698                 break;
1699         case B_CURVELOCAL:
1700                 if(ob && ob->id.lib==0) {
1701                         cu= ob->data;
1702                         if(cu->id.lib) {
1703                                 if(okee("Make local")) {
1704                                         make_local_curve(cu);
1705                                         make_local_key( cu->key );
1706                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1707                                 }
1708                         }
1709                 }
1710                 break;
1711                 
1712         case B_TEXALONE:
1713                 if(G.buts->texfrom==0) {        /* from mat */
1714                         if(ob==0) return;
1715                         ma= give_current_material(ob, ob->actcol);
1716                         ma= get_active_matlayer(ma);
1717                         if(ma && ma->id.lib==0) {
1718                                 mtex= ma->mtex[ ma->texact ];
1719                                 if(mtex->tex && mtex->tex->id.us>1) {
1720                                         if(okee("Single user")) {
1721                                                 mtex->tex->id.us--;
1722                                                 mtex->tex= copy_texture(mtex->tex);
1723                                         }
1724                                 }
1725                         }
1726                 }
1727                 else if(G.buts->texfrom==1) { /* from world */
1728                         wrld= G.scene->world;
1729                         if(wrld->id.lib==0) {
1730                                 mtex= wrld->mtex[ wrld->texact ];
1731                                 if(mtex->tex && mtex->tex->id.us>1) {
1732                                         if(okee("Single user")) {
1733                                                 mtex->tex->id.us--;
1734                                                 mtex->tex= copy_texture(mtex->tex);
1735                                         }
1736                                 }
1737                         }
1738                 }
1739                 else if(G.buts->texfrom==2) { /* from lamp */
1740                         if(ob==0 || ob->type!=OB_LAMP) return;
1741                         la= ob->data;
1742                         if(la->id.lib==0) {
1743                                 mtex= la->mtex[ la->texact ];
1744                                 if(mtex->tex && mtex->tex->id.us>1) {
1745                                         if(okee("Single user")) {
1746                                                 mtex->tex->id.us--;
1747                                                 mtex->tex= copy_texture(mtex->tex);
1748                                         }
1749                                 }
1750                         }
1751                 }
1752                 break;
1753         case B_TEXLOCAL:
1754                 if(G.buts->texfrom==0) {        /* from mat */
1755                         if(ob==0) return;
1756                         ma= give_current_material(ob, ob->actcol);
1757                         ma= get_active_matlayer(ma);
1758                         if(ma && ma->id.lib==0) {
1759                                 mtex= ma->mtex[ ma->texact ];
1760                                 if(mtex->tex && mtex->tex->id.lib) {
1761                                         if(okee("Make local")) {
1762                                                 make_local_texture(mtex->tex);
1763                                         }
1764                                 }
1765                         }
1766                 }
1767                 else if(G.buts->texfrom==1) { /* from world */
1768                         wrld= G.scene->world;
1769                         if(wrld->id.lib==0) {
1770                                 mtex= wrld->mtex[ wrld->texact ];
1771                                 if(mtex->tex && mtex->tex->id.lib) {
1772                                         if(okee("Make local")) {
1773                                                 make_local_texture(mtex->tex);
1774                                         }
1775                                 }
1776                         }
1777                 }
1778                 else if(G.buts->texfrom==2) { /* from lamp */
1779                         if(ob==0 || ob->type!=OB_LAMP) return;
1780                         la= ob->data;
1781                         if(la->id.lib==0) {
1782                                 mtex= la->mtex[ la->texact ];
1783                                 if(mtex->tex && mtex->tex->id.lib) {
1784                                         if(okee("Make local")) {
1785                                                 make_local_texture(mtex->tex);
1786                                         }
1787                                 }
1788                         }
1789                 }
1790                 break;
1791         
1792         case B_IPOALONE:
1793                 ipo= G.sipo->ipo;
1794                 idfrom= G.sipo->from;
1795                 
1796                 if(idfrom && idfrom->lib==NULL) {
1797                         if(ipo->id.us>1) {
1798                                 if(okee("Single user")) {
1799                                         ipo= copy_ipo(ipo);
1800                                         ipo->id.us= 0;  /* assign_ipo adds users, copy_ipo sets to 1 */
1801                                         spaceipo_assign_ipo(G.sipo, ipo);
1802                                         allqueue(REDRAWIPO, 0);
1803                                 }
1804                         }
1805                 }
1806                 break;
1807         case B_IPOLOCAL:
1808                 ipo= G.sipo->ipo;
1809                 idfrom= G.sipo->from;
1810                 
1811                 if(idfrom && idfrom->lib==0) {
1812                         if(ipo->id.lib) {
1813                                 if(okee("Make local")) {
1814                                         make_local_ipo(ipo);
1815                                         allqueue(REDRAWIPO, 0);
1816                                 }
1817                         }
1818                 }
1819                 break;
1820
1821         case B_OBALONE:
1822                 if(G.scene->id.lib==0) {
1823                         if(ob->id.us>1) {
1824                                 if(okee("Single user")) {
1825                                         base= FIRSTBASE;
1826                                         while(base) {
1827                                                 if(base->object==ob) {
1828                                                         base->object= copy_object(ob);
1829                                                         ob->id.us--;
1830                                                         allqueue(REDRAWVIEW3D, 0);
1831                                                         break;
1832                                                 }
1833                                                 base= base->next;
1834                                         }
1835                                 }
1836                         }
1837                 }
1838                 break;
1839         case B_OBLOCAL:
1840                 if(G.scene->id.lib==0) {
1841                         if(ob->id.lib) {
1842                                 if(okee("Make local")) {
1843                                         make_local_object(ob);
1844                                         allqueue(REDRAWVIEW3D, 0);
1845                                 }
1846                         }
1847                 }
1848                 break;
1849         case B_MESHALONE:
1850                 if(ob && ob->id.lib==0) {
1851                         
1852                         me= ob->data;
1853                         
1854                         if(me && me->id.us>1) {
1855                                 if(okee("Single user")) {
1856                                         Mesh *men= copy_mesh(me);
1857                                         men->id.us= 0;
1858                                         
1859                                         set_mesh(ob, men);
1860                                         
1861                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1862                                         
1863                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1864                                 }
1865                         }
1866                 }
1867                 break;
1868         }
1869         
1870         BIF_undo_push("Make single user or local");
1871         allqueue(REDRAWBUTSALL, 0);
1872         allqueue(REDRAWOOPS, 0);
1873 }
1874
1875 /* ******************** GENERAL ********************** */
1876
1877 void do_headerbuttons(short event)
1878 {
1879
1880         if(event<=50) do_global_buttons2(event);
1881         else if(event<=100) do_global_buttons(event);
1882         else if(event<200) do_view3d_buttons(event);
1883         else if(event<250) do_ipo_buttons(event);
1884         else if(event<300) do_oops_buttons(event);
1885         else if(event<350) do_info_buttons(event);
1886         else if(event<400) do_image_buttons(event);
1887         else if(event<450) do_buts_buttons(event);
1888         else if(event<500) do_imasel_buttons(event);
1889         else if(event<525) do_text_buttons(event);
1890         else if(event<550) do_script_buttons(event);
1891         else if(event<600) do_file_buttons(event);
1892         else if(event<650) do_seq_buttons(event);
1893         else if(event<700) do_sound_buttons(event);
1894         else if(event<750) do_action_buttons(event);
1895         else if(event<800) do_time_buttons(curarea, event);
1896         else if(event<850) do_nla_buttons(event);
1897         else if(event<900) do_node_buttons(curarea, event);
1898         else if(event>=REDRAWVIEW3D) allqueue(event, 0);
1899 }
1900