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