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