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