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