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