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