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