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