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