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