Huge commit, but not much features... had to shuffle a lot of code around.
[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= NULL;
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= NULL;       /* 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                                 set_mesh(ob, (Mesh *)idtest);
744                                 
745                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
746                                         
747                                 BIF_undo_push("Browse Mesh");
748                                 allqueue(REDRAWBUTSEDIT, 0);
749                                 allqueue(REDRAWVIEW3D, 0);
750                                 allqueue(REDRAWACTION,0);
751                                 allqueue(REDRAWIPO, 0);
752
753                                 break;
754                         }
755                         nr++;
756                         idtest= idtest->next;
757                 }
758
759                 break;
760         case B_MATBROWSE:
761                 if(G.buts->menunr== -2) {
762                         activate_databrowse((ID *)G.buts->lockpoin, ID_MA, 0, B_MATBROWSE, &G.buts->menunr, do_global_buttons);
763                         return;
764                 }
765                 
766                 if(G.buts->menunr < 0) return;
767                 
768                 if(G.buts->pin) {
769                         
770                 }
771                 else {
772                         
773                         ma= give_current_material(ob, ob->actcol);
774                         nr= 1;
775                         
776                         id= (ID *)ma;
777                         
778                         idtest= G.main->mat.first;
779                         while(idtest) {
780                                 if(nr==G.buts->menunr) {
781                                         break;
782                                 }
783                                 nr++;
784                                 idtest= idtest->next;
785                         }
786                         if(idtest==0) { /* new mat */
787                                 if(id)  idtest= (ID *)copy_material((Material *)id);
788                                 else {
789                                         idtest= (ID *)add_material("Material");
790                                 }
791                                 idtest->us--;
792                         }
793                         if(idtest!=id) {
794                                 assign_material(ob, (Material *)idtest, ob->actcol);
795                                 
796                                 BIF_undo_push("Browse Material");
797                                 allqueue(REDRAWBUTSSHADING, 0);
798                                 allqueue(REDRAWIPO, 0);
799                                 BIF_preview_changed(G.buts);
800                         }
801                         
802                 }
803                 break;
804         case B_MATDELETE:
805                 if(G.buts->pin) {
806                         
807                 }
808                 else {
809                         ma= give_current_material(ob, ob->actcol);
810                         if(ma) {
811                                 assign_material(ob, 0, ob->actcol);
812                                 BIF_undo_push("Unlink Material");
813                                 allqueue(REDRAWBUTSSHADING, 0);
814                                 allqueue(REDRAWIPO, 0);
815                                 allqueue(REDRAWOOPS, 0);
816                                 BIF_preview_changed(G.buts);
817                         }
818                 }
819                 break;
820         case B_TEXDELETE:
821                 if(G.buts->pin) {
822                         
823                 }
824                 else {
825                         if(G.buts->texfrom==0) {        /* from mat */
826                                 ma= give_current_material(ob, ob->actcol);
827                                 if(ma) {
828                                         mtex= ma->mtex[ ma->texact ];
829                                         if(mtex) {
830                                                 if(mtex->tex) mtex->tex->id.us--;
831                                                 MEM_freeN(mtex);
832                                                 ma->mtex[ ma->texact ]= NULL;
833                                                 allqueue(REDRAWBUTSSHADING, 0);
834                                                 allqueue(REDRAWIPO, 0);
835                                                 BIF_preview_changed(G.buts);
836                                         }
837                                 }
838                         }
839                         else if(G.buts->texfrom==1) { /* from world */
840                                 wrld= G.scene->world;
841                                 if(wrld) {
842                                         mtex= wrld->mtex[ wrld->texact ];
843                                         if(mtex) {
844                                                 if(mtex->tex) mtex->tex->id.us--;
845                                                 MEM_freeN(mtex);
846                                                 wrld->mtex[ wrld->texact ]= NULL;
847                                                 allqueue(REDRAWBUTSSHADING, 0);
848                                                 allqueue(REDRAWIPO, 0);
849                                                 BIF_preview_changed(G.buts);
850                                         }
851                                 }
852                         }
853                         else {  /* from lamp */
854                                 la= ob->data;
855                                 if(la && ob->type==OB_LAMP) { /* to be sure */
856                                         mtex= la->mtex[ la->texact ];
857                                         if(mtex) {
858                                                 if(mtex->tex) mtex->tex->id.us--;
859                                                 MEM_freeN(mtex);
860                                                 la->mtex[ la->texact ]= NULL;
861                                                 allqueue(REDRAWBUTSSHADING, 0);
862                                                 allqueue(REDRAWIPO, 0);
863                                                 BIF_preview_changed(G.buts);
864                                         }
865                                 }
866                         }
867                         BIF_undo_push("Unlink Texture");
868                 }
869                 break;
870         case B_EXTEXBROWSE: 
871         case B_TEXBROWSE:
872
873                 if(G.buts->texnr== -2) {
874                         
875                         id= G.buts->lockpoin;
876                         if(event==B_EXTEXBROWSE) {
877                                 id= NULL;
878                                 ma= give_current_material(ob, ob->actcol);
879                                 if(ma) {
880                                         mtex= ma->mtex[ ma->texact ];
881                                         if(mtex) id= (ID *)mtex->tex;
882                                 }
883                         }
884                         
885                         activate_databrowse(id, ID_TE, 0, B_TEXBROWSE, &G.buts->texnr, do_global_buttons);
886                         return;
887                 }
888                 if(G.buts->texnr < 0) break;
889                 
890                 if(G.buts->pin) {
891                         
892                 }
893                 else {
894                         id= NULL;
895                         
896                         ma= give_current_material(ob, ob->actcol);
897                         if(ma) {
898                                 mtex= ma->mtex[ ma->texact ];
899                                 if(mtex) id= (ID *)mtex->tex;
900                         }
901
902                         idtest= G.main->tex.first;
903                         while(idtest) {
904                                 if(nr==G.buts->texnr) {
905                                         break;
906                                 }
907                                 nr++;
908                                 idtest= idtest->next;
909                         }
910                         if(idtest==0) { /* new tex */
911                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
912                                 else idtest= (ID *)add_texture("Tex");
913                                 idtest->us--;
914                         }
915                         if(idtest!=id && ma) {
916                                 
917                                 if( ma->mtex[ma->texact]==0) ma->mtex[ma->texact]= add_mtex();
918                                 
919                                 ma->mtex[ ma->texact ]->tex= (Tex *)idtest;
920                                 id_us_plus(idtest);
921                                 if(id) id->us--;
922                                 
923                                 BIF_undo_push("Browse Texture");
924                                 allqueue(REDRAWBUTSSHADING, 0);
925                                 allqueue(REDRAWIPO, 0);
926                                 allqueue(REDRAWOOPS, 0);
927                                 BIF_preview_changed(G.buts);
928                         }
929                 }
930                 break;
931         case B_ACTIONDELETE:
932                 act=ob->action;
933                 
934                 if (act)
935                         act->id.us--;
936                 ob->action=NULL;
937                 if(ob->pose) {          // clear flag, also used for draw colors
938                         bPoseChannel *pchan;
939                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next)
940                                 pchan->flag= 0;
941                 }
942                 BIF_undo_push("Unlink Action");
943                 
944                 allqueue(REDRAWVIEW3D, 0);
945                 allqueue(REDRAWACTION, 0);
946                 allqueue(REDRAWNLA, 0);
947                 allqueue(REDRAWIPO, 0);
948                 break;
949         case B_ACTIONBROWSE:
950                 if (!ob)
951                         break;
952                 act=ob->action;
953                 id= (ID *)act;
954
955                 if (G.saction->actnr== -2){
956                                 activate_databrowse((ID *)G.saction->action, ID_AC,  0, B_ACTIONBROWSE, &G.saction->actnr, do_global_buttons);
957                         return;
958                 }
959
960                 if(G.saction->actnr < 0) break;
961
962                 /*      See if we have selected a valid action */
963                 for (idtest= G.main->action.first; idtest; idtest= idtest->next) {
964                                 if(nr==G.saction->actnr) {
965                                         break;
966                                 }
967                                 nr++;
968                         
969                 }
970
971                 if(G.saction->pin) {
972                         G.saction->action= (bAction *)idtest;
973                         allqueue(REDRAWACTION, 0);
974                 }
975                 else {
976
977                         /* Store current action */
978                         if (!idtest){
979                                 if (act)
980                                         idtest= (ID *)copy_action(act);
981                                 else 
982                                         idtest=(ID *)add_empty_action();
983                                 idtest->us--;
984                         }
985                         
986                         
987                         if(idtest!=id && ob) {
988                                 act= (bAction *)idtest;
989                                 
990                                 ob->action= act;
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); /* constraint channel is no ID data... */
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 bugtracker!");
1060                                 }
1061                                 idtest->us--;
1062                         }
1063                         if(idtest!=id && from) {
1064                                 ipo= (Ipo *)idtest;
1065                 
1066                                 if (ipo->blocktype==IPO_CO){
1067                                         bConstraintChannel *chan= get_active_constraint_channel((Object*)from);
1068                                         if(chan) {
1069                                                 chan->ipo = ipo;
1070                                                 id_us_plus(idtest);
1071                                                 allqueue(REDRAWVIEW3D, 0);
1072                                                 allqueue(REDRAWACTION, 0);
1073                                                 allqueue(REDRAWNLA, 0);
1074                                         }
1075                                 }
1076                                 else if(ipo->blocktype==ID_OB) {
1077                                         ( (Object *)from)->ipo= ipo;
1078                                         id_us_plus(idtest);
1079                                         allqueue(REDRAWVIEW3D, 0);
1080                                 }
1081                                 else if(ipo->blocktype==ID_AC) {
1082                                         bActionChannel *chan;
1083                                         chan = get_hilighted_action_channel ((bAction*)from);
1084                                         if (!chan){
1085                                                 error ("Create an action channel first");
1086                                                 return;
1087                                         }
1088                                         chan->ipo=ipo;
1089                                         id_us_plus(idtest);
1090                                         allqueue(REDRAWNLA, 0);
1091                                         allqueue(REDRAWACTION, 0);
1092                                 }
1093                                 else if(ipo->blocktype==ID_MA) {
1094                                         ( (Material *)from)->ipo= ipo;
1095                                         id_us_plus(idtest);
1096                                         allqueue(REDRAWBUTSSHADING, 0);
1097                                 }
1098                                 else if(ipo->blocktype==ID_TE) {
1099                                         ( (Tex *)from)->ipo= ipo;
1100                                         id_us_plus(idtest);
1101                                         allqueue(REDRAWBUTSSHADING, 0);
1102                                 }
1103                                 else if(ipo->blocktype==ID_SEQ) {
1104                                         seq= (Sequence *)from;
1105                                         if((seq->type & SEQ_EFFECT)||(seq->type == SEQ_SOUND)) {
1106                                                 id_us_plus(idtest);
1107                                                 seq->ipo= ipo;
1108                                         }
1109                                 }
1110                                 else if(ipo->blocktype==ID_CU) {
1111                                         ( (Curve *)from)->ipo= ipo;
1112                                         id_us_plus(idtest);
1113                                         allqueue(REDRAWVIEW3D, 0);
1114                                 }
1115                                 else if(ipo->blocktype==ID_KE) {
1116                                         ( (Key *)from)->ipo= ipo;
1117                                         
1118                                         id_us_plus(idtest);
1119                                         allqueue(REDRAWVIEW3D, 0);
1120                                         
1121                                 }
1122                                 else if(ipo->blocktype==ID_WO) {
1123                                         ( (World *)from)->ipo= ipo;
1124                                         id_us_plus(idtest);
1125                                         allqueue(REDRAWBUTSSHADING, 0);
1126                                 }
1127                                 else if(ipo->blocktype==ID_LA) {
1128                                         ( (Lamp *)from)->ipo= ipo;
1129                                         id_us_plus(idtest);
1130                                         allqueue(REDRAWBUTSSHADING, 0);
1131                                 }
1132                                 else if(ipo->blocktype==ID_CA) {
1133                                         ( (Camera *)from)->ipo= ipo;
1134                                         id_us_plus(idtest);
1135                                         allqueue(REDRAWBUTSEDIT, 0);
1136                                 }
1137                                 else if(ipo->blocktype==ID_SO) {
1138                                         ( (bSound *)from)->ipo= ipo;
1139                                         id_us_plus(idtest);
1140                                         allqueue(REDRAWBUTSEDIT, 0);
1141                                 }
1142                                 else
1143                                         printf("error in browse ipo \n");
1144                                 
1145                                 if(id) id->us--;
1146                                 
1147                                 BIF_undo_push("Browse Ipo");
1148                                 scrarea_queue_winredraw(curarea);
1149                                 scrarea_queue_headredraw(curarea);
1150                                 allqueue(REDRAWIPO, 0);
1151                         }
1152                 }
1153                 break;
1154         case B_IPODELETE:
1155                 ipo= get_ipo_to_edit(&from);
1156                 if(from==0) return;
1157                 
1158                 ipo->id.us--;
1159                 
1160                 if(ipo->blocktype==ID_OB) ( (Object *)from)->ipo= NULL;
1161                 else if(ipo->blocktype==ID_MA) ( (Material *)from)->ipo= NULL;
1162                 else if(ipo->blocktype==ID_TE) ( (Tex *)from)->ipo= NULL;
1163                 else if(ipo->blocktype==ID_SEQ) ( (Sequence *)from)->ipo= NULL;
1164                 else if(ipo->blocktype==ID_CU) ( (Curve *)from)->ipo= NULL;
1165                 else if(ipo->blocktype==ID_KE) ( (Key *)from)->ipo= NULL;
1166                 else if(ipo->blocktype==ID_WO) ( (World *)from)->ipo= NULL;
1167                 else if(ipo->blocktype==ID_LA) ( (Lamp *)from)->ipo= NULL;
1168                 else if(ipo->blocktype==ID_WO) ( (World *)from)->ipo= NULL;
1169                 else if(ipo->blocktype==ID_CA) ( (Camera *)from)->ipo= NULL;
1170                 else if(ipo->blocktype==ID_SO) ( (bSound *)from)->ipo= NULL;
1171                 else if(ipo->blocktype==ID_AC) {
1172                         bAction *act = (bAction*) from;
1173                         bActionChannel *chan = 
1174                                 get_hilighted_action_channel((bAction*)from);
1175                         BLI_freelinkN (&act->chanbase, chan);
1176                 }
1177                 else if(ipo->blocktype==IPO_CO) {
1178                         bConstraintChannel *chan= get_active_constraint_channel((Object*)from);
1179                         if(chan) chan->ipo= NULL;
1180                 }
1181                 else error("Warn bugtracker!");
1182                 
1183                 editipo_changed(G.sipo, 1); /* doredraw */
1184                 BIF_undo_push("Unlink Ipo");
1185                 allqueue(REDRAWIPO, 0);
1186                 allqueue(REDRAWNLA, 0);
1187                 allqueue (REDRAWACTION, 0);
1188                 
1189                 break;
1190         case B_WORLDBROWSE:
1191
1192                 if(G.buts->menunr==-2) {
1193                         activate_databrowse((ID *)G.scene->world, ID_WO, 0, B_WORLDBROWSE, &G.buts->menunr, do_global_buttons);
1194                         break;
1195                 }
1196
1197                 if(G.buts->menunr < 0) break;
1198                 /* no lock */
1199                         
1200                 wrld= G.scene->world;
1201                 nr= 1;
1202                 
1203                 id= (ID *)wrld;
1204                 
1205                 idtest= G.main->world.first;
1206                 while(idtest) {
1207                         if(nr==G.buts->menunr) {
1208                                 break;
1209                         }
1210                         nr++;
1211                         idtest= idtest->next;
1212                 }
1213                 if(idtest==0) { /* new world */
1214                         if(id) idtest= (ID *)copy_world((World *)id);
1215                         else idtest= (ID *)add_world("World");
1216                         idtest->us--;
1217                 }
1218                 if(idtest!=id) {
1219                         G.scene->world= (World *)idtest;
1220                         id_us_plus(idtest);
1221                         if(id) id->us--;
1222                         
1223                         BIF_undo_push("Browse World");
1224                         allqueue(REDRAWBUTSSHADING, 0);
1225                         allqueue(REDRAWIPO, 0);
1226                         allqueue(REDRAWOOPS, 0);
1227                         BIF_preview_changed(G.buts);
1228                 }
1229                 break;
1230         case B_WORLDDELETE:
1231                 if(G.scene->world) {
1232                         G.scene->world->id.us--;
1233                         G.scene->world= NULL;
1234
1235                         BIF_undo_push("Unlink World");
1236                         allqueue(REDRAWBUTSSHADING, 0);
1237                         allqueue(REDRAWIPO, 0);
1238                 }
1239                 
1240                 break;
1241         case B_WTEXBROWSE:
1242
1243                 if(G.buts->texnr== -2) {
1244                         id= NULL;
1245                         wrld= G.scene->world;
1246                         if(wrld) {
1247                                 mtex= wrld->mtex[ wrld->texact ];
1248                                 if(mtex) id= (ID *)mtex->tex;
1249                         }
1250
1251                         activate_databrowse((ID *)id, ID_TE, 0, B_WTEXBROWSE, &G.buts->texnr, do_global_buttons);
1252                         return;
1253                 }
1254                 if(G.buts->texnr < 0) break;
1255
1256                 if(G.buts->pin) {
1257                         
1258                 }
1259                 else {
1260                         id= NULL;
1261                         
1262                         wrld= G.scene->world;
1263                         if(wrld) {
1264                                 mtex= wrld->mtex[ wrld->texact ];
1265                                 if(mtex) id= (ID *)mtex->tex;
1266                         }
1267
1268                         idtest= G.main->tex.first;
1269                         while(idtest) {
1270                                 if(nr==G.buts->texnr) {
1271                                         break;
1272                                 }
1273                                 nr++;
1274                                 idtest= idtest->next;
1275                         }
1276                         if(idtest==0) { /* new tex */
1277                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
1278                                 else idtest= (ID *)add_texture("Tex");
1279                                 idtest->us--;
1280                         }
1281                         if(idtest!=id && wrld) {
1282                                 
1283                                 if( wrld->mtex[wrld->texact]==0) {
1284                                         wrld->mtex[wrld->texact]= add_mtex();
1285                                         wrld->mtex[wrld->texact]->texco= TEXCO_VIEW;
1286                                 }
1287                                 wrld->mtex[ wrld->texact ]->tex= (Tex *)idtest;
1288                                 id_us_plus(idtest);
1289                                 if(id) id->us--;
1290                                 
1291                                 BIF_undo_push("Texture browse");
1292                                 allqueue(REDRAWBUTSSHADING, 0);
1293                                 allqueue(REDRAWIPO, 0);
1294                                 allqueue(REDRAWOOPS, 0);
1295                                 BIF_preview_changed(G.buts);
1296                         }
1297                 }
1298                 break;
1299         case B_LAMPBROWSE:
1300                 /* no lock */
1301                 if(ob==0) return;
1302                 if(ob->type!=OB_LAMP) return;
1303
1304                 if(G.buts->menunr== -2) {
1305                         activate_databrowse((ID *)G.buts->lockpoin, ID_LA, 0, B_LAMPBROWSE, &G.buts->menunr, do_global_buttons);
1306                         return;
1307                 }
1308                 if(G.buts->menunr < 0) break;
1309                 
1310                 la= ob->data;
1311                 nr= 1;
1312                 id= (ID *)la;
1313                 
1314                 idtest= G.main->lamp.first;
1315                 while(idtest) {
1316                         if(nr==G.buts->menunr) {
1317                                 break;
1318                         }
1319                         nr++;
1320                         idtest= idtest->next;
1321                 }
1322                 if(idtest==0) { /* no new lamp */
1323                         return;
1324                 }
1325                 if(idtest!=id) {
1326                         ob->data= (Lamp *)idtest;
1327                         id_us_plus(idtest);
1328                         if(id) id->us--;
1329                         
1330                         BIF_undo_push("Lamp browse");
1331                         allqueue(REDRAWBUTSSHADING, 0);
1332                         allqueue(REDRAWVIEW3D, 0);
1333                         allqueue(REDRAWIPO, 0);
1334                         allqueue(REDRAWOOPS, 0);
1335                         BIF_preview_changed(G.buts);
1336                 }
1337                 break;
1338         
1339         case B_LTEXBROWSE:
1340
1341                 if(ob==0) return;
1342                 if(ob->type!=OB_LAMP) return;
1343
1344                 if(G.buts->texnr== -2) {
1345                         id= NULL;
1346                         la= ob->data;
1347                         mtex= la->mtex[ la->texact ];
1348                         if(mtex) id= (ID *)mtex->tex;
1349
1350                         activate_databrowse(id, ID_TE, 0, B_LTEXBROWSE, &G.buts->texnr, do_global_buttons);
1351                         return;
1352                 }
1353                 if(G.buts->texnr < 0) break;
1354
1355                 if(G.buts->pin) {
1356                         
1357                 }
1358                 else {
1359                         id= NULL;
1360                         
1361                         la= ob->data;
1362                         mtex= la->mtex[ la->texact ];
1363                         if(mtex) id= (ID *)mtex->tex;
1364
1365                         idtest= G.main->tex.first;
1366                         while(idtest) {
1367                                 if(nr==G.buts->texnr) {
1368                                         break;
1369                                 }
1370                                 nr++;
1371                                 idtest= idtest->next;
1372                         }
1373                         if(idtest==0) { /* new tex */
1374                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
1375                                 else idtest= (ID *)add_texture("Tex");
1376                                 idtest->us--;
1377                         }
1378                         if(idtest!=id && la) {
1379                                 
1380                                 if( la->mtex[la->texact]==0) {
1381                                         la->mtex[la->texact]= add_mtex();
1382                                         la->mtex[la->texact]->texco= TEXCO_GLOB;
1383                                 }
1384                                 la->mtex[ la->texact ]->tex= (Tex *)idtest;
1385                                 id_us_plus(idtest);
1386                                 if(id) id->us--;
1387                                 
1388                                 BIF_undo_push("Texture Browse");
1389                                 allqueue(REDRAWBUTSSHADING, 0);
1390                                 allqueue(REDRAWIPO, 0);
1391                                 allqueue(REDRAWOOPS, 0);
1392                                 BIF_preview_changed(G.buts);
1393                         }
1394                 }
1395                 break;
1396         
1397         case B_IMAGEDELETE:
1398                 G.sima->image= NULL;
1399                 image_changed(G.sima, 0);
1400                 BIF_undo_push("Unlink Image");
1401                 allqueue(REDRAWIMAGE, 0);
1402                 break;
1403         
1404         case B_AUTOMATNAME:
1405                 automatname(G.buts->lockpoin);
1406
1407                 BIF_undo_push("Auto name");
1408                 allqueue(REDRAWBUTSSHADING, 0);
1409                 allqueue(REDRAWOOPS, 0);
1410                 break;          
1411         case B_AUTOTEXNAME:
1412                 if(G.buts->mainb==CONTEXT_SHADING) {
1413                         if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_TEX) {
1414                                 autotexname(G.buts->lockpoin);
1415                         }
1416                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_MAT) {
1417                                 ma= G.buts->lockpoin;
1418                                 if(ma->mtex[ ma->texact]) autotexname(ma->mtex[ma->texact]->tex);
1419                         }
1420                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_WORLD) {
1421                                 wrld= G.buts->lockpoin;
1422                                 if(wrld->mtex[ wrld->texact]) autotexname(wrld->mtex[wrld->texact]->tex);
1423                         }
1424                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_LAMP) {
1425                                 la= G.buts->lockpoin;
1426                                 if(la->mtex[ la->texact]) autotexname(la->mtex[la->texact]->tex);
1427                         }
1428                         BIF_undo_push("Auto name");
1429                         allqueue(REDRAWBUTSSHADING, 0);
1430                         allqueue(REDRAWOOPS, 0);
1431                 }
1432                 break;
1433
1434         case B_RESETAUTOSAVE:
1435                 reset_autosave();
1436                 allqueue(REDRAWINFO, 0);
1437                 break;
1438         case B_SOUNDTOGGLE:
1439                 SYS_WriteCommandLineInt(SYS_GetSystem(), "noaudio", (U.gameflags & USER_DISABLE_SOUND));
1440                 break;
1441         case B_SHOWSPLASH:
1442                                 show_splash();
1443                 break;
1444         case B_MIPMAPCHANGED:
1445                 set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
1446                 allqueue(REDRAWVIEW3D, 0);
1447                 break;
1448         case B_NEWSPACE:
1449                 newspace(curarea, curarea->butspacetype);
1450                 break;
1451         case B_LOADTEMP:        /* is button from space.c */
1452                 BIF_read_autosavefile();
1453                 break;
1454
1455         case B_USERPREF:
1456                 allqueue(REDRAWINFO, 0);
1457                 break;
1458
1459         case B_DRAWINFO:        /* is button from space.c  *info* */
1460                 allqueue(REDRAWVIEW3D, 0);
1461                 break;
1462
1463         case B_FLIPINFOMENU:    /* is button from space.c  *info* */
1464                 scrarea_queue_headredraw(curarea);
1465                 break;
1466
1467 #if 0
1468 //#ifdef _WIN32 // FULLSCREEN
1469         case B_FLIPFULLSCREEN:
1470                 if(U.uiflag & USER_FLIPFULLSCREEN)
1471                         U.uiflag &= ~USER_FLIPFULLSCREEN;
1472                 else
1473                         U.uiflag |= USER_FLIPFULLSCREEN;
1474                 mainwindow_toggle_fullscreen((U.uiflag & USER_FLIPFULLSCREEN));
1475                 break;
1476 #endif
1477
1478         /* Fileselect windows for user preferences file paths */
1479
1480         /* yafray: xml export dir. select */
1481         case B_YAFRAYDIRFILESEL:        /* space.c */
1482                 if(curarea->spacetype==SPACE_INFO) {
1483                         sa= closest_bigger_area();
1484                         areawinset(sa->win);
1485                 }
1486
1487                 activate_fileselect(FILE_SPECIAL, "SELECT YFEXPORT PATH", U.yfexportdir, filesel_u_yfexportdir);
1488                 break;
1489
1490         case B_FONTDIRFILESEL:  /* is button from space.c  *info* */
1491                 if(curarea->spacetype==SPACE_INFO) {
1492                         sa= closest_bigger_area();
1493                         areawinset(sa->win);
1494                 }
1495
1496                 activate_fileselect(FILE_SPECIAL, "SELECT FONT PATH", U.fontdir, filesel_u_fontdir);
1497                 break;
1498
1499         case B_TEXTUDIRFILESEL:         /* is button from space.c  *info* */
1500                 if(curarea->spacetype==SPACE_INFO) {
1501                         sa= closest_bigger_area();
1502                         areawinset(sa->win);
1503                 }
1504
1505                 activate_fileselect(FILE_SPECIAL, "SELECT TEXTURE PATH", U.textudir, filesel_u_textudir);
1506                 break;
1507         
1508         case B_PLUGTEXDIRFILESEL:               /* is button form space.c  *info* */
1509                 if(curarea->spacetype==SPACE_INFO) {
1510                         sa= closest_bigger_area();
1511                         areawinset(sa->win);
1512                 }
1513
1514                 activate_fileselect(FILE_SPECIAL, "SELECT TEX PLUGIN PATH", U.plugtexdir, filesel_u_plugtexdir);
1515                 break;
1516         
1517         case B_PLUGSEQDIRFILESEL:               /* is button from space.c  *info* */
1518                 if(curarea->spacetype==SPACE_INFO) {
1519                         sa= closest_bigger_area();
1520                         areawinset(sa->win);
1521                 }
1522
1523                 activate_fileselect(FILE_SPECIAL, "SELECT SEQ PLUGIN PATH", U.plugseqdir, filesel_u_plugseqdir);
1524                 break;
1525         
1526         case B_RENDERDIRFILESEL:        /* is button from space.c  *info* */
1527                 if(curarea->spacetype==SPACE_INFO) {
1528                         sa= closest_bigger_area();
1529                         areawinset(sa->win);
1530                 }
1531
1532                 activate_fileselect(FILE_SPECIAL, "SELECT RENDER PATH", U.renderdir, filesel_u_renderdir);
1533                 break;
1534
1535         case B_PYMENUEVAL: /* is button from space.c *info* */
1536                 BPyMenu_RemoveAllEntries(); /* free old data */
1537                 if (BPyMenu_Init(1) == -1) /* re-eval scripts registration in menus */
1538                         error("Invalid scripts dir: check console");
1539                 break;
1540         case B_PYTHONDIRFILESEL:        /* is button from space.c  *info* */
1541                 if(curarea->spacetype==SPACE_INFO) {
1542                         sa= closest_bigger_area();
1543                         areawinset(sa->win);
1544                 }
1545
1546                 activate_fileselect(FILE_SPECIAL, "SELECT SCRIPT PATH", U.pythondir, filesel_u_pythondir);
1547                 break;
1548
1549         case B_SOUNDDIRFILESEL:         /* is button from space.c  *info* */
1550                 if(curarea->spacetype==SPACE_INFO) {
1551                         sa= closest_bigger_area();
1552                         areawinset(sa->win);
1553                 }
1554
1555                 activate_fileselect(FILE_SPECIAL, "SELECT SOUND PATH", U.sounddir, filesel_u_sounddir);
1556                 break;
1557
1558         case B_TEMPDIRFILESEL:  /* is button from space.c  *info* */
1559                 if(curarea->spacetype==SPACE_INFO) {
1560                         sa= closest_bigger_area();
1561                         areawinset(sa->win);
1562                 }
1563
1564                 activate_fileselect(FILE_SPECIAL, "SELECT TEMP FILE PATH", U.tempdir, filesel_u_tempdir);
1565                 break;
1566
1567         /* END Fileselect windows for user preferences file paths */
1568
1569 #ifdef INTERNATIONAL
1570         case B_LOADUIFONT:      /* is button from space.c  *info* */
1571                 if(curarea->spacetype==SPACE_INFO) {
1572                         sa= closest_bigger_area();
1573                         areawinset(sa->win);
1574                 }
1575                 BLI_make_file_string("/", buf, U.fontdir, U.fontname);
1576                 activate_fileselect(FILE_SPECIAL, "LOAD UI FONT", buf, set_interface_font);
1577                 break;
1578
1579         case B_SETLANGUAGE:             /* is button from space.c  *info* */
1580                 lang_setlanguage();
1581                 allqueue(REDRAWALL, 0);
1582                 break;
1583
1584         case B_SETFONTSIZE:             /* is button from space.c  *info* */
1585                 refresh_interface_font();
1586                 FTF_SetSize(U.fontsize); 
1587                 allqueue(REDRAWALL, 0);
1588                 break;
1589                 
1590         case B_SETTRANSBUTS:    /* is button from space.c  *info* */
1591                 allqueue(REDRAWALL, 0);
1592                 break;
1593
1594         case B_RESTOREFONT:             /* is button from space.c  *info* */
1595                 U.fontsize= 0;
1596                 start_interface_font();
1597                 allqueue(REDRAWALL, 0);
1598                 break;
1599                 
1600         case B_USETEXTUREFONT:          /* is button from space.c  *info* */
1601                 refresh_interface_font();
1602                 allqueue(REDRAWALL, 0);
1603                 break;
1604
1605         case B_DOLANGUIFONT:    /* is button from space.c  *info* */
1606                 if(U.transopts & USER_DOTRANSLATE)
1607                         start_interface_font();
1608                 else
1609                         G.ui_international = FALSE;
1610                 allqueue(REDRAWALL, 0);
1611                 break;
1612 #endif
1613                 
1614         case B_FULL:
1615                 if(curarea->spacetype!=SPACE_INFO) {
1616                         area_fullscreen();
1617                 }
1618                 break;  
1619
1620         case B_IDNAME:
1621                         /* changing a metaballs name, sadly enough,
1622                          * can require it to be updated because its
1623                          * basis might have changed... -zr
1624                          */
1625                 if (ob && ob->type==OB_MBALL)
1626                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1627                         
1628                 /* redraw because name has changed: new pup */
1629                 scrarea_queue_headredraw(curarea);
1630                 allqueue(REDRAWINFO, 1);
1631                 allqueue(REDRAWOOPS, 1);
1632                 /* name scene also in set PUPmenu */
1633                 allqueue(REDRAWBUTSALL, 0);
1634                 allqueue(REDRAWHEADERS, 0);
1635
1636                 break;
1637         
1638         case B_KEEPDATA:
1639                 /* keep datablock. similar to pressing FKEY in a fileselect window
1640                  * maybe we can move that stuff to a seperate function? -- sg
1641                  */
1642                 if (curarea->spacetype==SPACE_BUTS) {
1643                         id= (ID *)G.buts->lockpoin;
1644                 } else if(curarea->spacetype==SPACE_IPO) {
1645                         id = (ID *)G.sipo->ipo;
1646                 } /* similar for other spacetypes ? */
1647                 if (id) {
1648                         if( id->flag & LIB_FAKEUSER) {
1649                                 id->flag -= LIB_FAKEUSER;
1650                                 id->us--;
1651                         } else {
1652                                 id->flag |= LIB_FAKEUSER;
1653                                 id->us++;
1654                         }
1655                 }
1656                 allqueue(REDRAWHEADERS, 0);
1657
1658                 break;
1659
1660         }
1661 }
1662
1663
1664 void do_global_buttons2(short event)
1665 {
1666         Base *base;
1667         Object *ob;
1668         Material *ma;
1669         MTex *mtex;
1670         Mesh *me;
1671         Curve *cu;
1672         MetaBall *mb;
1673         Ipo *ipo;
1674         Lamp *la;
1675         Lattice *lt;
1676         World *wrld;
1677         ID *idfrom; 
1678         bAction *act;
1679
1680         /* general:  Single User is allowed when from==LOCAL 
1681          *                       Make Local is allowed when (from==LOCAL && id==LIB)
1682          */
1683                 
1684         if(event<B_LOCAL_ALONE) return;
1685
1686         ob= OBACT;
1687
1688         switch(event) {
1689                 
1690         case B_LAMPALONE:
1691                 if(ob && ob->id.lib==0) {
1692                         la= ob->data;
1693                         if(la->id.us>1) {
1694                                 if(okee("Single user")) {
1695                                         ob->data= copy_lamp(la);
1696                                         la->id.us--;
1697                                 }
1698                         }
1699                 }
1700                 break;
1701         case B_LAMPLOCAL:
1702                 if(ob && ob->id.lib==0) {
1703                         la= ob->data;
1704                         if(la->id.lib) {
1705                                 if(okee("Make local")) {
1706                                         make_local_lamp(la);
1707                                 }
1708                         }
1709                 }
1710                 break;
1711         
1712         case B_ARMLOCAL:
1713                 if (ob&&ob->id.lib==0){
1714                         bArmature *arm=ob->data;
1715                         if (arm->id.lib){
1716                                 if(okee("Make local")) {
1717                                         make_local_armature(arm);
1718                                 }
1719                         }
1720                 }
1721                 break;
1722         case B_ARMALONE:
1723                 if(ob && ob->id.lib==0) {
1724                         bArmature *arm=ob->data;
1725                         if(arm->id.us>1) {
1726                                 if(okee("Single user")) {
1727                                         ob->data= copy_armature(arm);
1728                                         arm->id.us--;
1729                                 }
1730                         }
1731                 }
1732                 break;
1733         case B_ACTLOCAL:
1734                 if(ob && ob->id.lib==0) {
1735                         act= ob->action;
1736                         if(act->id.lib) {
1737                                 if(okee("Make local")) {
1738                                         make_local_action(act);
1739                                         allqueue(REDRAWACTION,0);
1740                                 }
1741                         }
1742                 }
1743                 break;
1744         case B_ACTALONE:
1745                 if(ob && ob->id.lib==0) {
1746                         act= ob->action;
1747                 
1748                         if(act->id.us>1) {
1749                                 if(okee("Single user")) {
1750                                         ob->action=copy_action(act);
1751                                         act->id.us--;
1752                                         allqueue(REDRAWACTION, 0);
1753                                 }
1754                         }
1755                 }
1756                 break;
1757
1758         case B_CAMERAALONE:
1759                 if(ob && ob->id.lib==0) {
1760                         Camera *ca= ob->data;
1761                         if(ca->id.us>1) {
1762                                 if(okee("Single user")) {
1763                                         ob->data= copy_camera(ca);
1764                                         ca->id.us--;
1765                                 }
1766                         }
1767                 }
1768                 break;
1769         case B_CAMERALOCAL:
1770                 if(ob && ob->id.lib==0) {
1771                         Camera *ca= ob->data;
1772                         if(ca->id.lib) {
1773                                 if(okee("Make local")) {
1774                                         make_local_camera(ca);
1775                                 }
1776                         }
1777                 }
1778                 break;
1779         case B_WORLDALONE:
1780                 wrld= G.scene->world;
1781                 if(wrld->id.us>1) {
1782                         if(okee("Single user")) {
1783                                 G.scene->world= copy_world(wrld);
1784                                 wrld->id.us--;
1785                         }
1786                 }
1787                 break;
1788         case B_WORLDLOCAL:
1789                 wrld= G.scene->world;
1790                 if(wrld && wrld->id.lib) {
1791                         if(okee("Make local")) {
1792                                 make_local_world(wrld);
1793                         }
1794                 }
1795                 break;
1796
1797         case B_LATTALONE:
1798                 if(ob && ob->id.lib==0) {
1799                         lt= ob->data;
1800                         if(lt->id.us>1) {
1801                                 if(okee("Single user")) {
1802                                         ob->data= copy_lattice(lt);
1803                                         lt->id.us--;
1804                                 }
1805                         }
1806                 }
1807                 break;
1808         case B_LATTLOCAL:
1809                 if(ob && ob->id.lib==0) {
1810                         lt= ob->data;
1811                         if(lt->id.lib) {
1812                                 if(okee("Make local")) {
1813                                         make_local_lattice(lt);
1814                                 }
1815                         }
1816                 }
1817                 break;
1818         
1819         case B_MATALONE:
1820                 if(ob==0) return;
1821                 ma= give_current_material(ob, ob->actcol);
1822                 idfrom= material_from(ob, ob->actcol);
1823                 if(idfrom && idfrom->lib==0) {
1824                         if(ma->id.us>1) {
1825                                 if(okee("Single user")) {
1826                                         ma= copy_material(ma);
1827                                         ma->id.us= 0;
1828                                         assign_material(ob, ma, ob->actcol);
1829                                 }
1830                         }
1831                 }
1832                 break;
1833         case B_MATLOCAL:
1834                 if(ob==0) return;
1835                 idfrom= material_from(ob, ob->actcol);
1836                 if(idfrom->lib==0) {
1837                         ma= give_current_material(ob, ob->actcol);
1838                         if(ma && ma->id.lib) {
1839                                 if(okee("Make local")) {
1840                                         make_local_material(ma);
1841                                 }
1842                         }
1843                 }
1844                 break;
1845
1846         case B_MESHLOCAL:
1847                 if(ob && ob->id.lib==0) {
1848                         me= ob->data;
1849                         if(me && me->id.lib) {
1850                                 if(okee("Make local")) {
1851                                         make_local_mesh(me);
1852                                         make_local_key( me->key );
1853
1854                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);                           
1855                                 }
1856                         }
1857                 }
1858                 break;
1859
1860         case B_MBALLALONE:
1861                 if(ob && ob->id.lib==0) {
1862                         mb= ob->data;
1863                         if(mb->id.us>1) {
1864                                 if(okee("Single user")) {
1865                                         ob->data= copy_mball(mb);
1866                                         mb->id.us--;
1867                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1868                                 }
1869                         }
1870                 }
1871                 break;
1872         case B_MBALLLOCAL:
1873                 if(ob && ob->id.lib==0) {
1874                         mb= ob->data;
1875                         if(mb->id.lib) {
1876                                 if(okee("Make local")) {
1877                                         make_local_mball(mb);
1878                                 }
1879                         }
1880                 }
1881                 break;
1882
1883         case B_CURVEALONE:
1884                 if(ob && ob->id.lib==0) {
1885                         cu= ob->data;
1886                         if(cu->id.us>1) {
1887                                 if(okee("Single user")) {
1888                                         ob->data= copy_curve(cu);
1889                                         cu->id.us--;
1890                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1891                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1892                                 }
1893                         }
1894                 }
1895                 break;
1896         case B_CURVELOCAL:
1897                 if(ob && ob->id.lib==0) {
1898                         cu= ob->data;
1899                         if(cu->id.lib) {
1900                                 if(okee("Make local")) {
1901                                         make_local_curve(cu);
1902                                         make_local_key( cu->key );
1903                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1904                                 }
1905                         }
1906                 }
1907                 break;
1908                 
1909         case B_TEXALONE:
1910                 if(G.buts->texfrom==0) {        /* from mat */
1911                         if(ob==0) return;
1912                         ma= give_current_material(ob, ob->actcol);
1913                         if(ma && ma->id.lib==0) {
1914                                 mtex= ma->mtex[ ma->texact ];
1915                                 if(mtex->tex && mtex->tex->id.us>1) {
1916                                         if(okee("Single user")) {
1917                                                 mtex->tex->id.us--;
1918                                                 mtex->tex= copy_texture(mtex->tex);
1919                                         }
1920                                 }
1921                         }
1922                 }
1923                 else if(G.buts->texfrom==1) { /* from world */
1924                         wrld= G.scene->world;
1925                         if(wrld->id.lib==0) {
1926                                 mtex= wrld->mtex[ wrld->texact ];
1927                                 if(mtex->tex && mtex->tex->id.us>1) {
1928                                         if(okee("Single user")) {
1929                                                 mtex->tex->id.us--;
1930                                                 mtex->tex= copy_texture(mtex->tex);
1931                                         }
1932                                 }
1933                         }
1934                 }
1935                 else if(G.buts->texfrom==2) { /* from lamp */
1936                         if(ob==0 || ob->type!=OB_LAMP) return;
1937                         la= ob->data;
1938                         if(la->id.lib==0) {
1939                                 mtex= la->mtex[ la->texact ];
1940                                 if(mtex->tex && mtex->tex->id.us>1) {
1941                                         if(okee("Single user")) {
1942                                                 mtex->tex->id.us--;
1943                                                 mtex->tex= copy_texture(mtex->tex);
1944                                         }
1945                                 }
1946                         }
1947                 }
1948                 break;
1949         case B_TEXLOCAL:
1950                 if(G.buts->texfrom==0) {        /* from mat */
1951                         if(ob==0) return;
1952                         ma= give_current_material(ob, ob->actcol);
1953                         if(ma && ma->id.lib==0) {
1954                                 mtex= ma->mtex[ ma->texact ];
1955                                 if(mtex->tex && mtex->tex->id.lib) {
1956                                         if(okee("Make local")) {
1957                                                 make_local_texture(mtex->tex);
1958                                         }
1959                                 }
1960                         }
1961                 }
1962                 else if(G.buts->texfrom==1) { /* from world */
1963                         wrld= G.scene->world;
1964                         if(wrld->id.lib==0) {
1965                                 mtex= wrld->mtex[ wrld->texact ];
1966                                 if(mtex->tex && mtex->tex->id.lib) {
1967                                         if(okee("Make local")) {
1968                                                 make_local_texture(mtex->tex);
1969                                         }
1970                                 }
1971                         }
1972                 }
1973                 else if(G.buts->texfrom==2) { /* from lamp */
1974                         if(ob==0 || ob->type!=OB_LAMP) return;
1975                         la= ob->data;
1976                         if(la->id.lib==0) {
1977                                 mtex= la->mtex[ la->texact ];
1978                                 if(mtex->tex && mtex->tex->id.lib) {
1979                                         if(okee("Make local")) {
1980                                                 make_local_texture(mtex->tex);
1981                                         }
1982                                 }
1983                         }
1984                 }
1985                 break;
1986         
1987         case B_IPOALONE:
1988                 ipo= get_ipo_to_edit(&idfrom);
1989                 
1990                 if(idfrom && idfrom->lib==0) {
1991                         if(ipo->id.us>1) {
1992                                 if(okee("Single user")) {
1993                                         if(ipo->blocktype==ID_OB) ((Object *)idfrom)->ipo= copy_ipo(ipo);
1994                                         else if(ipo->blocktype==ID_MA) ((Material *)idfrom)->ipo= copy_ipo(ipo);
1995                                         else if(ipo->blocktype==ID_TE) ((Tex *)idfrom)->ipo= copy_ipo(ipo);
1996                                         else if(ipo->blocktype==ID_SEQ) ((Sequence *)idfrom)->ipo= copy_ipo(ipo);
1997                                         else if(ipo->blocktype==ID_CU) ((Curve *)idfrom)->ipo= copy_ipo(ipo);
1998                                         else if(ipo->blocktype==ID_KE) ((Key *)idfrom)->ipo= copy_ipo(ipo);
1999                                         else if(ipo->blocktype==ID_LA) ((Lamp *)idfrom)->ipo= copy_ipo(ipo);
2000                                         else if(ipo->blocktype==ID_WO) ((World *)idfrom)->ipo= copy_ipo(ipo);
2001                                         else if(ipo->blocktype==ID_CA) ((Camera *)idfrom)->ipo= copy_ipo(ipo);
2002                                         else if(ipo->blocktype==ID_SO) ((bSound *)idfrom)->ipo= copy_ipo(ipo);
2003                                         else if(ipo->blocktype==ID_AC) 
2004                                                 get_hilighted_action_channel((bAction *)idfrom)->ipo= copy_ipo(ipo);
2005                                         else if(ipo->blocktype==IPO_CO) 
2006                                                 get_active_constraint_channel((Object*)idfrom)->ipo= copy_ipo(ipo);
2007                                         else error("Warn bugtracker!");
2008                                         
2009                                         ipo->id.us--;
2010                                         allqueue(REDRAWIPO, 0);
2011                                 }
2012                         }
2013                 }
2014                 break;
2015         case B_IPOLOCAL:
2016                 ipo= get_ipo_to_edit(&idfrom);
2017                 
2018                 if(idfrom && idfrom->lib==0) {
2019                         if(ipo->id.lib) {
2020                                 if(okee("Make local")) {
2021                                         make_local_ipo(ipo);
2022                                         allqueue(REDRAWIPO, 0);
2023                                 }
2024                         }
2025                 }
2026                 break;
2027
2028         case B_OBALONE:
2029                 if(G.scene->id.lib==0) {
2030                         if(ob->id.us>1) {
2031                                 if(okee("Single user")) {
2032                                         base= FIRSTBASE;
2033                                         while(base) {
2034                                                 if(base->object==ob) {
2035                                                         base->object= copy_object(ob);
2036                                                         ob->id.us--;
2037                                                         allqueue(REDRAWVIEW3D, 0);
2038                                                         break;
2039                                                 }
2040                                                 base= base->next;
2041                                         }
2042                                 }
2043                         }
2044                 }
2045                 break;
2046         case B_OBLOCAL:
2047                 if(G.scene->id.lib==0) {
2048                         if(ob->id.lib) {
2049                                 if(okee("Make local")) {
2050                                         make_local_object(ob);
2051                                         allqueue(REDRAWVIEW3D, 0);
2052                                 }
2053                         }
2054                 }
2055                 break;
2056         case B_MESHALONE:
2057                 if(ob && ob->id.lib==0) {
2058                         
2059                         me= ob->data;
2060                         
2061                         if(me && me->id.us>1) {
2062                                 if(okee("Single user")) {
2063                                         Mesh *men= copy_mesh(me);
2064                                         men->id.us= 0;
2065                                         
2066                                         set_mesh(ob, men);
2067                                         
2068                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2069                                         
2070                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
2071                                 }
2072                         }
2073                 }
2074                 break;
2075         }
2076         
2077         BIF_undo_push("Make single user or local");
2078         allqueue(REDRAWBUTSALL, 0);
2079         allqueue(REDRAWOOPS, 0);
2080 }
2081
2082 /* ******************** GENERAL ********************** */
2083
2084 void do_headerbuttons(short event)
2085 {
2086
2087         if(event<=50) do_global_buttons2(event);
2088         else if(event<=100) do_global_buttons(event);
2089         else if(event<200) do_view3d_buttons(event);
2090         else if(event<250) do_ipo_buttons(event);
2091         else if(event<300) do_oops_buttons(event);
2092         else if(event<350) do_info_buttons(event);
2093         else if(event<400) do_image_buttons(event);
2094         else if(event<450) do_buts_buttons(event);
2095         else if(event<500) do_imasel_buttons(event);
2096         else if(event<525) do_text_buttons(event);
2097         else if(event<550) do_script_buttons(event);
2098         else if(event<600) do_file_buttons(event);
2099         else if(event<650) do_seq_buttons(event);
2100         else if(event<700) do_sound_buttons(event);
2101         else if(event<750) do_action_buttons(event);
2102         else if(event<800) do_time_buttons(curarea, event);
2103         else if(event<900) do_nla_buttons(event);
2104         else if(event>=REDRAWVIEW3D) allqueue(event, 0);
2105 }
2106