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