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