Slight simplification of windowtype_pup()...
[blender.git] / source / blender / src / headerbuttons.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include <sys/types.h>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BMF_Api.h"
46 #include "BIF_language.h"
47 #ifdef INTERNATIONAL
48 #include "FTF_Api.h"
49 #endif
50
51 #include "BLI_blenlib.h"
52 #include "BLI_arithb.h"
53 #include "BLI_storage_types.h"
54
55 #include "IMB_imbuf_types.h"
56 #include "IMB_imbuf.h"
57
58 #include "DNA_ID.h"
59 #include "DNA_action_types.h"
60 #include "DNA_armature_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_group_types.h"
64 #include "DNA_image_types.h"
65 #include "DNA_ipo_types.h"
66 #include "DNA_key_types.h"
67 #include "DNA_lamp_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_material_types.h"
70 #include "DNA_mesh_types.h"
71 #include "DNA_meta_types.h"
72 #include "DNA_object_types.h"
73 #include "DNA_oops_types.h"
74 #include "DNA_packedFile_types.h"
75 #include "DNA_scene_types.h"
76 #include "DNA_screen_types.h"
77 #include "DNA_sequence_types.h"
78 #include "DNA_sound_types.h"
79 #include "DNA_space_types.h"
80 #include "DNA_texture_types.h"
81 #include "DNA_text_types.h"
82 #include "DNA_userdef_types.h"
83 #include "DNA_view2d_types.h"
84 #include "DNA_view3d_types.h"
85 #include "DNA_world_types.h"
86 #include "DNA_constraint_types.h"
87
88 #include "BKE_utildefines.h"
89
90 #include "BKE_constraint.h"
91 #include "BKE_action.h"
92 #include "BKE_armature.h"
93 #include "BKE_blender.h"
94 #include "BKE_curve.h"
95 #include "BKE_depsgraph.h"
96 #include "BKE_exotic.h"
97 #include "BKE_global.h"
98 #include "BKE_image.h"
99 #include "BKE_ipo.h"
100 #include "BKE_key.h"
101 #include "BKE_lattice.h"
102 #include "BKE_library.h"
103 #include "BKE_main.h"
104 #include "BKE_material.h"
105 #include "BKE_mball.h"
106 #include "BKE_mesh.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 "BPI_script.h"
149
150 #include "BSE_edit.h"
151 #include "BSE_filesel.h"
152 #include "BSE_headerbuttons.h"
153 #include "BSE_node.h"
154 #include "BSE_view.h"
155 #include "BSE_sequence.h"
156 #include "BSE_editipo.h"
157 #include "BSE_drawipo.h"
158
159 #include "BDR_drawmesh.h"
160 #include "BDR_vpaint.h"
161 #include "BDR_editface.h"
162 #include "BDR_editobject.h"
163 #include "BDR_editcurve.h"
164 #include "BDR_editmball.h"
165
166 #include "BPY_extern.h"
167 #include "BPY_menus.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(ELEM(id_code, ID_MA, ID_TE)) 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 ELEM7( id_code, ID_SCE, ID_SCR, ID_MA, ID_TE, ID_WO, ID_IP, ID_AC) 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, "Can't edit library data");
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, "Can't edit library data");
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
280                                 IDnames_to_pupstring(&str, NULL, extrastr, lb, id, menupoin);
281                 }
282                 
283                 uiDefButS(block, MENU, browse, str, xco,yco,XIC,YIC, menupoin, 0, 0, 0, 0, "Browses existing choices or adds NEW");
284                 xco+= XIC;
285                 
286                 uiClearButLock();
287         
288                 MEM_freeN(str);
289         }
290
291         uiBlockSetCol(block, oldcol);
292
293         if(id) {        /* text button with name */
294         
295                 /* name */
296                 if(id->us>1) uiBlockSetCol(block, TH_BUT_SETTING1);
297                 /* Pinned data ? */
298                 if (pin && *pinpoin) {
299                         uiBlockSetCol(block, TH_BUT_SETTING2);
300                 }
301                 /* Redalert overrides pin color */
302                 if(id->us<=0) uiBlockSetCol(block, TH_REDALERT);
303
304                 uiSetButLock(id->lib!=0, "Can't edit library data");
305                 
306                 if(GS(id->name)==ID_SCE) strcpy(str1, "SCE:");
307                 else if(GS(id->name)==ID_SCE) strcpy(str1, "SCR:");
308                 else if(GS(id->name)==ID_MA) {
309                         if( ((Material *)id)->use_nodes )
310                                 strcpy(str1, "NT:");
311                         else
312                                 strcpy(str1, "MA:");
313                 }
314                 else {
315                         str1[0]= id->name[0];
316                         str1[1]= id->name[1];
317                         str1[2]= ':';
318                         str1[3]= 0;
319                 }
320                 
321                 if( GS(id->name)==ID_IP) len= 110;
322                 else if(yco) len= 140;  // comes from button panel
323                 else len= 120;
324                 
325                 but= uiDefBut(block, TEX, B_IDNAME, str1,xco, yco, (short)len, YIC, id->name+2, 0.0, 19.0, 0, 0, "Displays current Datablock name. Click to change.");
326                 uiButSetFunc(but, test_idbutton_cb, id->name, NULL);
327
328                 uiClearButLock();
329
330                 xco+= len;
331                 
332                 if(id->lib) {
333                         
334                         if(parid && parid->lib) uiDefIconBut(block, BUT, 0, ICON_DATALIB,xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Displays name of the current Indirect Library Datablock. Click to change.");
335                         else uiDefIconBut(block, BUT, lib, ICON_PARLIB, xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Displays current Library Datablock name. Click to make local.");
336                         
337                         xco+= XIC;
338                 }
339                 
340                 
341                 if(users && id->us>1) {
342                         uiSetButLock (pin && *pinpoin, "Can't make pinned data single-user");
343                         
344                         sprintf(str1, "%d", id->us);
345                         if(id->us<10) {
346                                 
347                                 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.");
348                                 xco+= XIC;
349                         }
350                         else {
351                                 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.");
352                                 xco+= XIC+10;
353                         }
354                         
355                         uiClearButLock();
356                         
357                 }
358                 
359                 if(del) {
360
361                         uiSetButLock (pin && *pinpoin, "Can't unlink pinned data");
362                         if(parid && parid->lib);
363                         else {
364                                 uiDefIconBut(block, BUT, del, ICON_X, xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Deletes link to this Datablock");
365                                 xco+= XIC;
366                         }
367
368                         uiClearButLock();
369                 }
370
371                 if(autobut) {
372                         if(parid && parid->lib);
373                         else {
374                                 uiDefIconBut(block, BUT, autobut, ICON_AUTO,xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Generates an automatic name");
375                                 xco+= XIC;
376                         }
377                         
378                         
379                 }
380                 if(keepbut) {
381                         uiDefBut(block, BUT, keepbut, "F", xco,yco,XIC,YIC, 0, 0, 0, 0, 0, "Saves this datablock even if it has no users");  
382                         xco+= XIC;
383                 }
384         }
385         else if(add_addbutton) {        /* "add new" button */
386                 uiBlockSetCol(block, oldcol);
387                 uiDefButS(block, TOG, browse, "Add New" ,xco, yco, 110, YIC, menupoin, (float)*menupoin, 32767.0, 0, 0, "Add new data block");
388                 xco+= 110;
389         }
390         //xco+=XIC;
391         
392         uiBlockSetCol(block, oldcol);
393         uiBlockEndAlign(block);
394
395         return xco;
396 }
397
398
399 /* results in fully updated anim system */
400 static void do_update_for_newframe(int mute, int events)
401 {
402         extern void audiostream_scrub(unsigned int frame);      /* seqaudio.c */
403         
404         if(events) {
405                 allqueue(REDRAWALL, 0);
406         }
407         
408         /* this function applies the changes too */
409         scene_update_for_newframe(G.scene, screen_view3d_layers()); /* BKE_scene.h */
410
411         if ( (CFRA>1) && (!mute) && (G.scene->audio.flag & AUDIO_SCRUB)) audiostream_scrub( CFRA );
412         BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
413
414 }
415
416 void update_for_newframe(void)
417 {
418         do_update_for_newframe(0, 1);
419 }
420
421 void update_for_newframe_muted(void)
422 {
423         do_update_for_newframe(1, 1);
424 }
425
426 /* used by new animated UI playback */
427 void update_for_newframe_nodraw(int nosound)
428 {
429         do_update_for_newframe(nosound, 0);
430 }
431
432
433 static void show_splash(void)
434 {
435         extern char datatoc_splash_jpg[];
436         extern int datatoc_splash_jpg_size;
437         char *string = NULL;
438
439 #ifdef NAN_BUILDINFO
440         char buffer[1024];
441         extern char * build_date;
442         extern char * build_time;
443         extern char * build_platform;
444         extern char * build_type;
445
446         string = &buffer[0];
447         sprintf(string,"Built on %s %s     Version %s %s", build_date, build_time, build_platform, build_type);
448 #endif
449
450         splash((void *)datatoc_splash_jpg, datatoc_splash_jpg_size, string);
451 }
452
453
454 /* Functions for user preferences fileselect windows */
455
456 /* yafray: export dir select */
457 static void filesel_u_yfexportdir(char *name)
458 {
459         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
460         BLI_split_dirfile(name, dir, file);
461
462         strcpy(U.yfexportdir, dir);
463         allqueue(REDRAWALL, 0);
464 }
465
466 static void filesel_u_fontdir(char *name)
467 {
468         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
469         BLI_split_dirfile(name, dir, file);
470
471         strcpy(U.fontdir, dir);
472         allqueue(REDRAWALL, 0);
473 }
474
475 static void filesel_u_textudir(char *name)
476 {
477         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
478         BLI_split_dirfile(name, dir, file);
479
480         strcpy(U.textudir, dir);
481         allqueue(REDRAWALL, 0);
482 }
483
484 static void filesel_u_plugtexdir(char *name)
485 {
486         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
487         BLI_split_dirfile(name, dir, file);
488
489         strcpy(U.plugtexdir, dir);
490         allqueue(REDRAWALL, 0);
491 }
492
493 static void filesel_u_plugseqdir(char *name)
494 {
495         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
496         BLI_split_dirfile(name, dir, file);
497
498         strcpy(U.plugseqdir, dir);
499         allqueue(REDRAWALL, 0);
500 }
501
502 static void filesel_u_renderdir(char *name)
503 {
504         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
505         BLI_split_dirfile(name, dir, file);
506
507         strcpy(U.renderdir, dir);
508         allqueue(REDRAWALL, 0);
509 }
510
511 static void filesel_u_pythondir(char *name)
512 {
513         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
514         BLI_split_dirfile(name, dir, file);
515
516         strcpy(U.pythondir, dir);
517         allqueue(REDRAWALL, 0);
518 }
519
520 static void filesel_u_sounddir(char *name)
521 {
522         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
523         BLI_split_dirfile(name, dir, file);
524
525         strcpy(U.sounddir, dir);
526         allqueue(REDRAWALL, 0);
527 }
528
529 static void filesel_u_tempdir(char *name)
530 {
531         char dir[FILE_MAXDIR], file[FILE_MAXFILE];
532         BLI_split_dirfile(name, dir, file);
533
534         strcpy(U.tempdir, dir);
535         allqueue(REDRAWALL, 0);
536 }
537
538 /* END Functions for user preferences fileselect windows */
539
540
541 void do_global_buttons(unsigned short event)
542 {
543         ListBase *lb;
544         Object *ob;
545         Material *ma;
546         MTex *mtex;
547         Ipo *ipo;
548         Lamp *la;
549         World *wrld;
550         bAction *act;
551         ID *id, *idtest, *from=NULL;
552         ScrArea *sa;
553         int nr= 1;
554         char buf[FILE_MAXDIR+FILE_MAXFILE];
555
556         ob= OBACT;
557
558         id= NULL;       /* id at null for texbrowse */
559
560
561         switch(event) {
562         
563         case B_NEWFRAME:
564                 scrarea_queue_winredraw(curarea);
565                 scrarea_queue_headredraw(curarea);
566
567                 update_for_newframe();
568                 break;          
569         case B_REDR:
570                 scrarea_queue_winredraw(curarea);
571                 scrarea_queue_headredraw(curarea);
572                 break;
573         case B_REDRCURW3D:
574                 allqueue(REDRAWVIEW3D, 0);
575                 scrarea_queue_winredraw(curarea);
576                 scrarea_queue_headredraw(curarea);
577                 break;
578         case B_EDITBROWSE:
579                 if(ob==0) return;
580                 if(ob->id.lib) return;
581                 id= ob->data;
582                 if(id==0) return;
583
584                 if(G.buts->menunr== -2) {
585                         activate_databrowse((ID *)G.buts->lockpoin, GS(id->name), 0, B_EDITBROWSE, &G.buts->menunr, do_global_buttons);
586                         return;
587                 }
588                 if(G.buts->menunr < 0) return;
589                 
590                 lb= wich_libbase(G.main, GS(id->name));
591                 idtest= lb->first;
592                 while(idtest) {
593                         if(nr==G.buts->menunr) {
594                                 if(idtest!=id) {
595                                         id->us--;
596                                         id_us_plus(idtest);
597                                         
598                                         ob->data= idtest;
599                                         
600                                         test_object_materials(idtest);
601                                         
602                                         if( GS(idtest->name)==ID_CU ) {
603                                                 test_curve_type(ob);
604                                         }
605                                         else if( ob->type==OB_ARMATURE) {
606                                                 armature_rebuild_pose(ob, ob->data);
607                                         }
608                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
609                                         
610                                         allqueue(REDRAWBUTSEDIT, 0);
611                                         allqueue(REDRAWVIEW3D, 0);
612                                         allqueue(REDRAWACTION,0);
613                                         allqueue(REDRAWIPO, 0);
614                                         allqueue(REDRAWNLA,0);
615                                 }
616                                 break;
617                         }
618                         nr++;
619                         idtest= idtest->next;
620                 }
621
622                 break;
623         case B_MESHBROWSE:
624                 if(ob==0) return;
625                 if(ob->id.lib) return;
626                 
627                 id= ob->data;
628                 if(id==0) id= G.main->mesh.first;
629                 if(id==0) return;
630                 
631                 if(G.buts->menunr== -2) {
632                         activate_databrowse((ID *)G.buts->lockpoin, GS(id->name), 0, B_MESHBROWSE, &G.buts->menunr, do_global_buttons);
633                         return;
634                 }
635                 if(G.buts->menunr < 0) return;
636                 
637
638                 idtest= G.main->mesh.first;
639                 while(idtest) {
640                         if(nr==G.buts->menunr) {
641                                 set_mesh(ob, (Mesh *)idtest);
642                                 
643                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
644                                         
645                                 BIF_undo_push("Browse Mesh");
646                                 allqueue(REDRAWBUTSEDIT, 0);
647                                 allqueue(REDRAWVIEW3D, 0);
648                                 allqueue(REDRAWACTION,0);
649                                 allqueue(REDRAWIPO, 0);
650
651                                 break;
652                         }
653                         nr++;
654                         idtest= idtest->next;
655                 }
656
657                 break;
658         case B_MATBROWSE:
659                 if(G.buts->menunr== -2) {
660                         activate_databrowse((ID *)G.buts->lockpoin, ID_MA, 0, B_MATBROWSE, &G.buts->menunr, do_global_buttons);
661                         return;
662                 }
663                 
664                 if(G.buts->menunr < 0) return;
665                 
666                 if(G.buts->pin) {
667                         
668                 }
669                 else {
670                         
671                         ma= give_current_material(ob, ob->actcol);
672                         nr= 1;
673                         
674                         id= (ID *)ma;
675                         
676                         idtest= G.main->mat.first;
677                         while(idtest) {
678                                 if(nr==G.buts->menunr) {
679                                         break;
680                                 }
681                                 nr++;
682                                 idtest= idtest->next;
683                         }
684                         if(idtest==0) { /* new mat */
685                                 if(id)  idtest= (ID *)copy_material((Material *)id);
686                                 else {
687                                         idtest= (ID *)add_material("Material");
688                                 }
689                                 idtest->us--;
690                         }
691                         if(idtest!=id) {
692                                 assign_material(ob, (Material *)idtest, ob->actcol);
693                                 
694                                 BIF_undo_push("Browse Material");
695                                 allqueue(REDRAWBUTSSHADING, 0);
696                                 allqueue(REDRAWIPO, 0);
697                                 allqueue(REDRAWNODE, 0);
698                                 BIF_preview_changed(ID_MA);
699                         }
700                         
701                 }
702                 break;
703         case B_MATDELETE:
704                 if(G.buts->pin) {
705                         
706                 }
707                 else {
708                         ma= give_current_material(ob, ob->actcol);
709                         if(ma) {
710                                 assign_material(ob, 0, ob->actcol);
711                                 BIF_undo_push("Unlink Material");
712                                 allqueue(REDRAWBUTSSHADING, 0);
713                                 allqueue(REDRAWIPO, 0);
714                                 allqueue(REDRAWOOPS, 0);
715                                 BIF_preview_changed(ID_MA);
716                         }
717                 }
718                 break;
719         case B_TEXDELETE:
720                 if(G.buts->pin) {
721                         
722                 }
723                 else {
724                         if(G.buts->texfrom==0) {        /* from mat */
725                                 ma= give_current_material(ob, ob->actcol);
726                                 ma= editnode_get_active_material(ma);
727                                 if(ma) {
728                                         mtex= ma->mtex[ ma->texact ];
729                                         if(mtex) {
730                                                 if(mtex->tex) mtex->tex->id.us--;
731                                                 MEM_freeN(mtex);
732                                                 ma->mtex[ ma->texact ]= NULL;
733                                                 allqueue(REDRAWBUTSSHADING, 0);
734                                                 allqueue(REDRAWIPO, 0);
735                                                 BIF_preview_changed(ID_MA);
736                                         }
737                                 }
738                         }
739                         else if(G.buts->texfrom==1) { /* from world */
740                                 wrld= G.scene->world;
741                                 if(wrld) {
742                                         mtex= wrld->mtex[ wrld->texact ];
743                                         if(mtex) {
744                                                 if(mtex->tex) mtex->tex->id.us--;
745                                                 MEM_freeN(mtex);
746                                                 wrld->mtex[ wrld->texact ]= NULL;
747                                                 allqueue(REDRAWBUTSSHADING, 0);
748                                                 allqueue(REDRAWIPO, 0);
749                                                 BIF_preview_changed(ID_WO);
750                                         }
751                                 }
752                         }
753                         else {  /* from lamp */
754                                 la= ob->data;
755                                 if(la && ob->type==OB_LAMP) { /* to be sure */
756                                         mtex= la->mtex[ la->texact ];
757                                         if(mtex) {
758                                                 if(mtex->tex) mtex->tex->id.us--;
759                                                 MEM_freeN(mtex);
760                                                 la->mtex[ la->texact ]= NULL;
761                                                 allqueue(REDRAWBUTSSHADING, 0);
762                                                 allqueue(REDRAWIPO, 0);
763                                                 BIF_preview_changed(ID_LA);
764                                         }
765                                 }
766                         }
767                         BIF_undo_push("Unlink Texture");
768                 }
769                 break;
770         case B_EXTEXBROWSE: 
771         case B_TEXBROWSE:
772
773                 if(G.buts->texnr== -2) {
774                         
775                         id= G.buts->lockpoin;
776                         if(event==B_EXTEXBROWSE) {
777                                 id= NULL;
778                                 ma= give_current_material(ob, ob->actcol);
779                                 ma= editnode_get_active_material(ma);
780                                 if(ma) {
781                                         mtex= ma->mtex[ ma->texact ];
782                                         if(mtex) id= (ID *)mtex->tex;
783                                 }
784                         }
785                         
786                         activate_databrowse(id, ID_TE, 0, B_TEXBROWSE, &G.buts->texnr, do_global_buttons);
787                         return;
788                 }
789                 if(G.buts->texnr < 0) break;
790                 
791                 if(G.buts->pin) {
792                         
793                 }
794                 else {
795                         id= NULL;
796                         
797                         ma= give_current_material(ob, ob->actcol);
798                         ma= editnode_get_active_material(ma);
799                         if(ma) {
800                                 mtex= ma->mtex[ ma->texact ];
801                                 if(mtex) id= (ID *)mtex->tex;
802                         }
803
804                         idtest= G.main->tex.first;
805                         while(idtest) {
806                                 if(nr==G.buts->texnr) {
807                                         break;
808                                 }
809                                 nr++;
810                                 idtest= idtest->next;
811                         }
812                         if(idtest==0) { /* new tex */
813                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
814                                 else idtest= (ID *)add_texture("Tex");
815                                 idtest->us--;
816                         }
817                         if(idtest!=id && ma) {
818                                 
819                                 if( ma->mtex[ma->texact]==0) ma->mtex[ma->texact]= add_mtex();
820                                 
821                                 ma->mtex[ ma->texact ]->tex= (Tex *)idtest;
822                                 id_us_plus(idtest);
823                                 if(id) id->us--;
824                                 
825                                 BIF_undo_push("Browse Texture");
826                                 allqueue(REDRAWBUTSSHADING, 0);
827                                 allqueue(REDRAWIPO, 0);
828                                 allqueue(REDRAWOOPS, 0);
829                                 BIF_preview_changed(ID_MA);
830                         }
831                 }
832                 break;
833         case B_ACTIONDELETE:
834                 act=ob->action;
835                 
836                 if (act)
837                         act->id.us--;
838                 ob->action=NULL;
839                 if(ob->pose) {          // clear flag, also used for draw colors
840                         bPoseChannel *pchan;
841                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next)
842                                 pchan->flag= 0;
843                 }
844                 BIF_undo_push("Unlink Action");
845                 
846                 allqueue(REDRAWVIEW3D, 0);
847                 allqueue(REDRAWACTION, 0);
848                 allqueue(REDRAWNLA, 0);
849                 allqueue(REDRAWIPO, 0);
850                 break;
851         case B_ACTIONBROWSE:
852                 if (!ob)
853                         break;
854                 act=ob->action;
855                 id= (ID *)act;
856
857                 if (G.saction->actnr== -2){
858                                 activate_databrowse((ID *)G.saction->action, ID_AC,  0, B_ACTIONBROWSE, &G.saction->actnr, do_global_buttons);
859                         return;
860                 }
861
862                 if(G.saction->actnr < 0) break;
863
864                 /*      See if we have selected a valid action */
865                 for (idtest= G.main->action.first; idtest; idtest= idtest->next) {
866                                 if(nr==G.saction->actnr) {
867                                         break;
868                                 }
869                                 nr++;
870                         
871                 }
872
873                 if(G.saction->pin) {
874                         G.saction->action= (bAction *)idtest;
875                         allqueue(REDRAWACTION, 0);
876                 }
877                 else {
878
879                         /* Store current action */
880                         if (!idtest){
881                                 if (act)
882                                         idtest= (ID *)copy_action(act);
883                                 else 
884                                         idtest=(ID *)add_empty_action(ob->type==OB_ARMATURE?ID_PO:ID_OB);
885                                 idtest->us--;
886                         }
887                         
888                         
889                         if(idtest!=id && ob) {
890                                 act= (bAction *)idtest;
891                                 
892                                 ob->action= act;
893                                 id_us_plus(idtest);
894                                 
895                                 if(id) id->us--;
896                                 
897                                 // Update everything
898                                 BIF_undo_push("Browse Action");
899                                 do_global_buttons (B_NEWFRAME);
900                                 allqueue(REDRAWVIEW3D, 0);
901                                 allqueue(REDRAWNLA, 0);
902                                 allqueue(REDRAWACTION, 0);
903                                 allqueue(REDRAWHEADERS, 0); 
904                         }
905                 }
906                 
907                 break;
908         case B_IPOBROWSE:
909
910                 ipo= G.sipo->ipo;
911                 from= G.sipo->from;
912                 id= (ID *)ipo;
913                 if(from==NULL) return;
914
915                 if(G.sipo->menunr== -2) {
916                         activate_databrowse((ID *)G.sipo->ipo, ID_IP, GS(from->name), B_IPOBROWSE, &G.sipo->menunr, do_global_buttons);
917                         return;
918                 }
919
920                 if(G.sipo->menunr < 0) break;
921
922                 idtest= G.main->ipo.first;
923                 while(idtest) {
924                         if( ((Ipo *)idtest)->blocktype == G.sipo->blocktype) {
925                                 if(nr==G.sipo->menunr) {
926                                         break;
927                                 }
928                                 nr++;
929                         }
930                         idtest= idtest->next;
931                 }
932
933                 if(G.sipo->pin) {
934                         if(idtest) {
935                                 G.sipo->ipo= (Ipo *)idtest;
936                                 allspace(REMAKEIPO, 0);         // in fact it should only do this one, but there is no function for it
937                         }
938                 }
939                 else {
940                         // assign the ipo to ...
941
942                         if(idtest==0) {
943                                 if(ipo) idtest= (ID *)copy_ipo(ipo);
944                                 else {
945                                         nr= G.sipo->blocktype;
946                                         if(nr==ID_OB) idtest= (ID *)add_ipo("ObIpo", ID_OB);
947                                         else if(nr==ID_CO) idtest= (ID *)add_ipo("CoIpo", ID_CO);
948                                         else if(nr==ID_PO) idtest= (ID *)add_ipo("ActIpo", nr);
949                                         else if(nr==ID_MA) idtest= (ID *)add_ipo("MatIpo", nr);
950                                         else if(nr==ID_TE) idtest= (ID *)add_ipo("TexIpo", nr);
951                                         else if(nr==ID_SEQ) idtest= (ID *)add_ipo("MatSeq", nr);
952                                         else if(nr==ID_CU) idtest= (ID *)add_ipo("CuIpo", nr);
953                                         else if(nr==ID_KE) idtest= (ID *)add_ipo("KeyIpo", nr);
954                                         else if(nr==ID_WO) idtest= (ID *)add_ipo("WoIpo", nr);
955                                         else if(nr==ID_LA) idtest= (ID *)add_ipo("LaIpo", nr);
956                                         else if(nr==ID_CA) idtest= (ID *)add_ipo("CaIpo", nr);
957                                         else if(nr==ID_SO) idtest= (ID *)add_ipo("SndIpo", nr);
958                                         else error("Warn bugtracker!");
959                                 }
960                                 idtest->us--;
961                         }
962                         if(idtest!=id && from) {
963                                 spaceipo_assign_ipo(G.sipo, (Ipo *)idtest);
964                                                                         
965                                 BIF_undo_push("Browse Ipo");
966                         }
967                 }
968                 break;
969         case B_IPODELETE:
970                 ipo= G.sipo->ipo;
971                 from= G.sipo->from;
972                 
973                 spaceipo_assign_ipo(G.sipo, NULL);
974                 
975                 editipo_changed(G.sipo, 1); /* doredraw */
976                 
977                 BIF_undo_push("Unlink Ipo");
978                 
979                 break;
980         case B_WORLDBROWSE:
981
982                 if(G.buts->menunr==-2) {
983                         activate_databrowse((ID *)G.scene->world, ID_WO, 0, B_WORLDBROWSE, &G.buts->menunr, do_global_buttons);
984                         break;
985                 }
986
987                 if(G.buts->menunr < 0) break;
988                 /* no lock */
989                         
990                 wrld= G.scene->world;
991                 nr= 1;
992                 
993                 id= (ID *)wrld;
994                 
995                 idtest= G.main->world.first;
996                 while(idtest) {
997                         if(nr==G.buts->menunr) {
998                                 break;
999                         }
1000                         nr++;
1001                         idtest= idtest->next;
1002                 }
1003                 if(idtest==0) { /* new world */
1004                         if(id) idtest= (ID *)copy_world((World *)id);
1005                         else idtest= (ID *)add_world("World");
1006                         idtest->us--;
1007                 }
1008                 if(idtest!=id) {
1009                         G.scene->world= (World *)idtest;
1010                         id_us_plus(idtest);
1011                         if(id) id->us--;
1012                         
1013                         BIF_undo_push("Browse World");
1014                         allqueue(REDRAWBUTSSHADING, 0);
1015                         allqueue(REDRAWIPO, 0);
1016                         allqueue(REDRAWOOPS, 0);
1017                         BIF_preview_changed(ID_WO);
1018                 }
1019                 break;
1020         case B_WORLDDELETE:
1021                 if(G.scene->world) {
1022                         G.scene->world->id.us--;
1023                         G.scene->world= NULL;
1024
1025                         BIF_undo_push("Unlink World");
1026                         allqueue(REDRAWBUTSSHADING, 0);
1027                         allqueue(REDRAWIPO, 0);
1028                 }
1029                 
1030                 break;
1031         case B_WTEXBROWSE:
1032
1033                 if(G.buts->texnr== -2) {
1034                         id= NULL;
1035                         wrld= G.scene->world;
1036                         if(wrld) {
1037                                 mtex= wrld->mtex[ wrld->texact ];
1038                                 if(mtex) id= (ID *)mtex->tex;
1039                         }
1040
1041                         activate_databrowse((ID *)id, ID_TE, 0, B_WTEXBROWSE, &G.buts->texnr, do_global_buttons);
1042                         return;
1043                 }
1044                 if(G.buts->texnr < 0) break;
1045
1046                 if(G.buts->pin) {
1047                         
1048                 }
1049                 else {
1050                         id= NULL;
1051                         
1052                         wrld= G.scene->world;
1053                         if(wrld) {
1054                                 mtex= wrld->mtex[ wrld->texact ];
1055                                 if(mtex) id= (ID *)mtex->tex;
1056                         }
1057
1058                         idtest= G.main->tex.first;
1059                         while(idtest) {
1060                                 if(nr==G.buts->texnr) {
1061                                         break;
1062                                 }
1063                                 nr++;
1064                                 idtest= idtest->next;
1065                         }
1066                         if(idtest==0) { /* new tex */
1067                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
1068                                 else idtest= (ID *)add_texture("Tex");
1069                                 idtest->us--;
1070                         }
1071                         if(idtest!=id && wrld) {
1072                                 
1073                                 if( wrld->mtex[wrld->texact]==0) {
1074                                         wrld->mtex[wrld->texact]= add_mtex();
1075                                         wrld->mtex[wrld->texact]->texco= TEXCO_VIEW;
1076                                 }
1077                                 wrld->mtex[ wrld->texact ]->tex= (Tex *)idtest;
1078                                 id_us_plus(idtest);
1079                                 if(id) id->us--;
1080                                 
1081                                 BIF_undo_push("Texture browse");
1082                                 allqueue(REDRAWBUTSSHADING, 0);
1083                                 allqueue(REDRAWIPO, 0);
1084                                 allqueue(REDRAWOOPS, 0);
1085                                 BIF_preview_changed(ID_WO);
1086                         }
1087                 }
1088                 break;
1089         case B_LAMPBROWSE:
1090                 /* no lock */
1091                 if(ob==0) return;
1092                 if(ob->type!=OB_LAMP) return;
1093
1094                 if(G.buts->menunr== -2) {
1095                         activate_databrowse((ID *)G.buts->lockpoin, ID_LA, 0, B_LAMPBROWSE, &G.buts->menunr, do_global_buttons);
1096                         return;
1097                 }
1098                 if(G.buts->menunr < 0) break;
1099                 
1100                 la= ob->data;
1101                 nr= 1;
1102                 id= (ID *)la;
1103                 
1104                 idtest= G.main->lamp.first;
1105                 while(idtest) {
1106                         if(nr==G.buts->menunr) {
1107                                 break;
1108                         }
1109                         nr++;
1110                         idtest= idtest->next;
1111                 }
1112                 if(idtest==0) { /* no new lamp */
1113                         return;
1114                 }
1115                 if(idtest!=id) {
1116                         ob->data= (Lamp *)idtest;
1117                         id_us_plus(idtest);
1118                         if(id) id->us--;
1119                         
1120                         BIF_undo_push("Lamp browse");
1121                         allqueue(REDRAWBUTSSHADING, 0);
1122                         allqueue(REDRAWVIEW3D, 0);
1123                         allqueue(REDRAWIPO, 0);
1124                         allqueue(REDRAWOOPS, 0);
1125                         BIF_preview_changed(ID_LA);
1126                 }
1127                 break;
1128         
1129         case B_LTEXBROWSE:
1130
1131                 if(ob==0) return;
1132                 if(ob->type!=OB_LAMP) return;
1133
1134                 if(G.buts->texnr== -2) {
1135                         id= NULL;
1136                         la= ob->data;
1137                         mtex= la->mtex[ la->texact ];
1138                         if(mtex) id= (ID *)mtex->tex;
1139
1140                         activate_databrowse(id, ID_TE, 0, B_LTEXBROWSE, &G.buts->texnr, do_global_buttons);
1141                         return;
1142                 }
1143                 if(G.buts->texnr < 0) break;
1144
1145                 if(G.buts->pin) {
1146                         
1147                 }
1148                 else {
1149                         id= NULL;
1150                         
1151                         la= ob->data;
1152                         mtex= la->mtex[ la->texact ];
1153                         if(mtex) id= (ID *)mtex->tex;
1154
1155                         idtest= G.main->tex.first;
1156                         while(idtest) {
1157                                 if(nr==G.buts->texnr) {
1158                                         break;
1159                                 }
1160                                 nr++;
1161                                 idtest= idtest->next;
1162                         }
1163                         if(idtest==0) { /* new tex */
1164                                 if(id)  idtest= (ID *)copy_texture((Tex *)id);
1165                                 else idtest= (ID *)add_texture("Tex");
1166                                 idtest->us--;
1167                         }
1168                         if(idtest!=id && la) {
1169                                 
1170                                 if( la->mtex[la->texact]==0) {
1171                                         la->mtex[la->texact]= add_mtex();
1172                                         la->mtex[la->texact]->texco= TEXCO_GLOB;
1173                                 }
1174                                 la->mtex[ la->texact ]->tex= (Tex *)idtest;
1175                                 id_us_plus(idtest);
1176                                 if(id) id->us--;
1177                                 
1178                                 BIF_undo_push("Texture Browse");
1179                                 allqueue(REDRAWBUTSSHADING, 0);
1180                                 allqueue(REDRAWIPO, 0);
1181                                 allqueue(REDRAWOOPS, 0);
1182                                 BIF_preview_changed(ID_LA);
1183                         }
1184                 }
1185                 break;
1186         
1187         case B_IMAGEDELETE:
1188                 G.sima->image= NULL;
1189                 image_changed(G.sima, 0);
1190                 BIF_undo_push("Unlink Image");
1191                 allqueue(REDRAWIMAGE, 0);
1192                 break;
1193         
1194         case B_AUTOMATNAME:
1195                 automatname(G.buts->lockpoin);
1196
1197                 BIF_undo_push("Auto name");
1198                 allqueue(REDRAWBUTSSHADING, 0);
1199                 allqueue(REDRAWOOPS, 0);
1200                 break;          
1201         case B_AUTOTEXNAME:
1202                 if(G.buts->mainb==CONTEXT_SHADING) {
1203                         if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_TEX) {
1204                                 autotexname(G.buts->lockpoin);
1205                         }
1206                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_MAT) {
1207                                 ma= G.buts->lockpoin;
1208                                 if(ma->mtex[ ma->texact]) autotexname(ma->mtex[ma->texact]->tex);
1209                         }
1210                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_WORLD) {
1211                                 wrld= G.buts->lockpoin;
1212                                 if(wrld->mtex[ wrld->texact]) autotexname(wrld->mtex[wrld->texact]->tex);
1213                         }
1214                         else if(G.buts->tab[CONTEXT_SHADING]==TAB_SHADING_LAMP) {
1215                                 la= G.buts->lockpoin;
1216                                 if(la->mtex[ la->texact]) autotexname(la->mtex[la->texact]->tex);
1217                         }
1218                         BIF_undo_push("Auto name");
1219                         allqueue(REDRAWBUTSSHADING, 0);
1220                         allqueue(REDRAWOOPS, 0);
1221                 }
1222                 break;
1223
1224         case B_RESETAUTOSAVE:
1225                 reset_autosave();
1226                 allqueue(REDRAWINFO, 0);
1227                 break;
1228         case B_SOUNDTOGGLE:
1229                 SYS_WriteCommandLineInt(SYS_GetSystem(), "noaudio", (U.gameflags & USER_DISABLE_SOUND));
1230                 break;
1231         case B_SHOWSPLASH:
1232                                 show_splash();
1233                 break;
1234         case B_MIPMAPCHANGED:
1235                 set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
1236                 allqueue(REDRAWVIEW3D, 0);
1237                 break;
1238         case B_NEWSPACE:
1239                 newspace(curarea, curarea->butspacetype);
1240                 break;
1241         case B_LOADTEMP:        /* is button from space.c */
1242                 BIF_read_autosavefile();
1243                 break;
1244
1245         case B_USERPREF:
1246                 allqueue(REDRAWINFO, 0);
1247                 break;
1248
1249         case B_DRAWINFO:        /* is button from space.c  *info* */
1250                 allqueue(REDRAWVIEW3D, 0);
1251                 break;
1252
1253         case B_PLAINMENUS:     /* is button from space.c  *info* */
1254                 reset_toolbox();
1255                 break;
1256
1257         case B_FLIPINFOMENU:    /* is button from space.c  *info* */
1258                 scrarea_queue_headredraw(curarea);
1259                 break;
1260
1261 #if 0
1262 //#ifdef _WIN32 // FULLSCREEN
1263         case B_FLIPFULLSCREEN:
1264                 if(U.uiflag & USER_FLIPFULLSCREEN)
1265                         U.uiflag &= ~USER_FLIPFULLSCREEN;
1266                 else
1267                         U.uiflag |= USER_FLIPFULLSCREEN;
1268                 mainwindow_toggle_fullscreen((U.uiflag & USER_FLIPFULLSCREEN));
1269                 break;
1270 #endif
1271
1272         /* Fileselect windows for user preferences file paths */
1273
1274         /* yafray: xml export dir. select */
1275         case B_YAFRAYDIRFILESEL:        /* space.c */
1276                 if(curarea->spacetype==SPACE_INFO) {
1277                         sa= closest_bigger_area();
1278                         areawinset(sa->win);
1279                 }
1280
1281                 activate_fileselect(FILE_SPECIAL, "SELECT YFEXPORT PATH", U.yfexportdir, filesel_u_yfexportdir);
1282                 break;
1283
1284         case B_FONTDIRFILESEL:  /* is button from space.c  *info* */
1285                 if(curarea->spacetype==SPACE_INFO) {
1286                         sa= closest_bigger_area();
1287                         areawinset(sa->win);
1288                 }
1289
1290                 activate_fileselect(FILE_SPECIAL, "SELECT FONT PATH", U.fontdir, filesel_u_fontdir);
1291                 break;
1292
1293         case B_TEXTUDIRFILESEL:         /* is button from space.c  *info* */
1294                 if(curarea->spacetype==SPACE_INFO) {
1295                         sa= closest_bigger_area();
1296                         areawinset(sa->win);
1297                 }
1298
1299                 activate_fileselect(FILE_SPECIAL, "SELECT TEXTURE PATH", U.textudir, filesel_u_textudir);
1300                 break;
1301         
1302         case B_PLUGTEXDIRFILESEL:               /* is button form space.c  *info* */
1303                 if(curarea->spacetype==SPACE_INFO) {
1304                         sa= closest_bigger_area();
1305                         areawinset(sa->win);
1306                 }
1307
1308                 activate_fileselect(FILE_SPECIAL, "SELECT TEX PLUGIN PATH", U.plugtexdir, filesel_u_plugtexdir);
1309                 break;
1310         
1311         case B_PLUGSEQDIRFILESEL:               /* is button from space.c  *info* */
1312                 if(curarea->spacetype==SPACE_INFO) {
1313                         sa= closest_bigger_area();
1314                         areawinset(sa->win);
1315                 }
1316
1317                 activate_fileselect(FILE_SPECIAL, "SELECT SEQ PLUGIN PATH", U.plugseqdir, filesel_u_plugseqdir);
1318                 break;
1319         
1320         case B_RENDERDIRFILESEL:        /* is button from space.c  *info* */
1321                 if(curarea->spacetype==SPACE_INFO) {
1322                         sa= closest_bigger_area();
1323                         areawinset(sa->win);
1324                 }
1325
1326                 activate_fileselect(FILE_SPECIAL, "SELECT RENDER PATH", U.renderdir, filesel_u_renderdir);
1327                 break;
1328
1329         case B_PYMENUEVAL: /* is button from space.c *info* */
1330                 BPyMenu_RemoveAllEntries(); /* free old data */
1331                 if (BPyMenu_Init(1) == -1) /* re-eval scripts registration in menus */
1332                         error("Invalid scripts dir: check console");
1333                 break;
1334         case B_PYTHONDIRFILESEL:        /* is button from space.c  *info* */
1335                 if(curarea->spacetype==SPACE_INFO) {
1336                         sa= closest_bigger_area();
1337                         areawinset(sa->win);
1338                 }
1339
1340                 activate_fileselect(FILE_SPECIAL, "SELECT SCRIPT PATH", U.pythondir, filesel_u_pythondir);
1341                 break;
1342
1343         case B_SOUNDDIRFILESEL:         /* is button from space.c  *info* */
1344                 if(curarea->spacetype==SPACE_INFO) {
1345                         sa= closest_bigger_area();
1346                         areawinset(sa->win);
1347                 }
1348
1349                 activate_fileselect(FILE_SPECIAL, "SELECT SOUND PATH", U.sounddir, filesel_u_sounddir);
1350                 break;
1351
1352         case B_TEMPDIRFILESEL:  /* is button from space.c  *info* */
1353                 if(curarea->spacetype==SPACE_INFO) {
1354                         sa= closest_bigger_area();
1355                         areawinset(sa->win);
1356                 }
1357
1358                 activate_fileselect(FILE_SPECIAL, "SELECT TEMP FILE PATH", U.tempdir, filesel_u_tempdir);
1359                 break;
1360
1361         /* END Fileselect windows for user preferences file paths */
1362
1363 #ifdef INTERNATIONAL
1364         case B_LOADUIFONT:      /* is button from space.c  *info* */
1365                 if(curarea->spacetype==SPACE_INFO) {
1366                         sa= closest_bigger_area();
1367                         areawinset(sa->win);
1368                 }
1369                 BLI_make_file_string("/", buf, U.fontdir, U.fontname);
1370                 activate_fileselect(FILE_SPECIAL, "LOAD UI FONT", buf, set_interface_font);
1371                 break;
1372
1373         case B_SETLANGUAGE:             /* is button from space.c  *info* */
1374                 lang_setlanguage();
1375                 allqueue(REDRAWALL, 0);
1376                 break;
1377
1378         case B_SETFONTSIZE:             /* is button from space.c  *info* */
1379                 refresh_interface_font();
1380                 FTF_SetSize(U.fontsize); 
1381                 allqueue(REDRAWALL, 0);
1382                 break;
1383                 
1384         case B_SETTRANSBUTS:    /* is button from space.c  *info* */
1385                 allqueue(REDRAWALL, 0);
1386                 break;
1387
1388         case B_RESTOREFONT:             /* is button from space.c  *info* */
1389                 U.fontsize= 0;
1390                 start_interface_font();
1391                 allqueue(REDRAWALL, 0);
1392                 break;
1393                 
1394         case B_USETEXTUREFONT:          /* is button from space.c  *info* */
1395                 refresh_interface_font();
1396                 allqueue(REDRAWALL, 0);
1397                 break;
1398
1399         case B_DOLANGUIFONT:    /* is button from space.c  *info* */
1400                 if(U.transopts & USER_DOTRANSLATE)
1401                         start_interface_font();
1402                 else
1403                         G.ui_international = FALSE;
1404                 allqueue(REDRAWALL, 0);
1405                 break;
1406 #endif
1407                 
1408         case B_FULL:
1409                 if(curarea->spacetype!=SPACE_INFO) {
1410                         area_fullscreen();
1411                 }
1412                 break;  
1413
1414         case B_IDNAME:
1415                         /* changing a metaballs name, sadly enough,
1416                          * can require it to be updated because its
1417                          * basis might have changed... -zr
1418                          */
1419                 if (ob && ob->type==OB_MBALL)
1420                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1421                         
1422                 /* redraw because name has changed: new pup */
1423                 scrarea_queue_headredraw(curarea);
1424                 allqueue(REDRAWINFO, 1);
1425                 allqueue(REDRAWOOPS, 1);
1426                 allqueue(REDRAWACTION, 1);
1427                 allqueue(REDRAWNLA, 1);
1428                 /* name scene also in set PUPmenu */
1429                 allqueue(REDRAWBUTSALL, 0);
1430                 allqueue(REDRAWHEADERS, 0);
1431
1432                 break;
1433         
1434         case B_KEEPDATA:
1435                 /* keep datablock. similar to pressing FKEY in a fileselect window
1436                  * maybe we can move that stuff to a seperate function? -- sg
1437                  */
1438                 if (curarea->spacetype==SPACE_BUTS) {
1439                         id= (ID *)G.buts->lockpoin;
1440                 } else if(curarea->spacetype==SPACE_IPO) {
1441                         id = (ID *)G.sipo->ipo;
1442                 } else if(curarea->spacetype==SPACE_NODE) {
1443                         id = ((SpaceNode *)curarea->spacedata.first)->id;
1444                 } /* similar for other spacetypes ? */
1445                 if (id) {
1446                         if( id->flag & LIB_FAKEUSER) {
1447                                 id->flag -= LIB_FAKEUSER;
1448                                 id->us--;
1449                         } else {
1450                                 id->flag |= LIB_FAKEUSER;
1451                                 id->us++;
1452                         }
1453                 }
1454                 allqueue(REDRAWHEADERS, 0);
1455
1456                 break;
1457
1458         }
1459 }
1460
1461
1462 void do_global_buttons2(short event)
1463 {
1464         Base *base;
1465         Object *ob;
1466         Material *ma;
1467         MTex *mtex;
1468         Mesh *me;
1469         Curve *cu;
1470         MetaBall *mb;
1471         Ipo *ipo;
1472         Lamp *la;
1473         Lattice *lt;
1474         World *wrld;
1475         ID *idfrom; 
1476         bAction *act;
1477
1478         /* general:  Single User is allowed when from==LOCAL 
1479          *                       Make Local is allowed when (from==LOCAL && id==LIB)
1480          */
1481                 
1482         if(event<B_LOCAL_ALONE) return;
1483
1484         ob= OBACT;
1485
1486         switch(event) {
1487                 
1488         case B_LAMPALONE:
1489                 if(ob && ob->id.lib==0) {
1490                         la= ob->data;
1491                         if(la->id.us>1) {
1492                                 if(okee("Single user")) {
1493                                         ob->data= copy_lamp(la);
1494                                         la->id.us--;
1495                                 }
1496                         }
1497                 }
1498                 break;
1499         case B_LAMPLOCAL:
1500                 if(ob && ob->id.lib==0) {
1501                         la= ob->data;
1502                         if(la->id.lib) {
1503                                 if(okee("Make local")) {
1504                                         make_local_lamp(la);
1505                                 }
1506                         }
1507                 }
1508                 break;
1509         
1510         case B_ARMLOCAL:
1511                 if (ob&&ob->id.lib==0){
1512                         bArmature *arm=ob->data;
1513                         if (arm->id.lib){
1514                                 if(okee("Make local")) {
1515                                         make_local_armature(arm);
1516                                 }
1517                         }
1518                 }
1519                 break;
1520         case B_ARMALONE:
1521                 if(ob && ob->id.lib==0) {
1522                         bArmature *arm=ob->data;
1523                         if(arm->id.us>1) {
1524                                 if(okee("Single user")) {
1525                                         ob->data= copy_armature(arm);
1526                                         armature_rebuild_pose(ob, ob->data);
1527                                         arm->id.us--;
1528                                 }
1529                         }
1530                 }
1531                 break;
1532         case B_ACTLOCAL:
1533                 if(ob && ob->id.lib==0) {
1534                         act= ob->action;
1535                         if(act->id.lib) {
1536                                 if(okee("Make local")) {
1537                                         make_local_action(act);
1538                                         allqueue(REDRAWACTION,0);
1539                                 }
1540                         }
1541                 }
1542                 break;
1543         case B_ACTALONE:
1544                 if(ob && ob->id.lib==0) {
1545                         act= ob->action;
1546                 
1547                         if(act->id.us>1) {
1548                                 if(okee("Single user")) {
1549                                         ob->action=copy_action(act);
1550                                         act->id.us--;
1551                                         allqueue(REDRAWACTION, 0);
1552                                 }
1553                         }
1554                 }
1555                 break;
1556
1557         case B_CAMERAALONE:
1558                 if(ob && ob->id.lib==0) {
1559                         Camera *ca= ob->data;
1560                         if(ca->id.us>1) {
1561                                 if(okee("Single user")) {
1562                                         ob->data= copy_camera(ca);
1563                                         ca->id.us--;
1564                                 }
1565                         }
1566                 }
1567                 break;
1568         case B_CAMERALOCAL:
1569                 if(ob && ob->id.lib==0) {
1570                         Camera *ca= ob->data;
1571                         if(ca->id.lib) {
1572                                 if(okee("Make local")) {
1573                                         make_local_camera(ca);
1574                                 }
1575                         }
1576                 }
1577                 break;
1578         case B_WORLDALONE:
1579                 wrld= G.scene->world;
1580                 if(wrld->id.us>1) {
1581                         if(okee("Single user")) {
1582                                 G.scene->world= copy_world(wrld);
1583                                 wrld->id.us--;
1584                         }
1585                 }
1586                 break;
1587         case B_WORLDLOCAL:
1588                 wrld= G.scene->world;
1589                 if(wrld && wrld->id.lib) {
1590                         if(okee("Make local")) {
1591                                 make_local_world(wrld);
1592                         }
1593                 }
1594                 break;
1595
1596         case B_LATTALONE:
1597                 if(ob && ob->id.lib==0) {
1598                         lt= ob->data;
1599                         if(lt->id.us>1) {
1600                                 if(okee("Single user")) {
1601                                         ob->data= copy_lattice(lt);
1602                                         lt->id.us--;
1603                                 }
1604                         }
1605                 }
1606                 break;
1607         case B_LATTLOCAL:
1608                 if(ob && ob->id.lib==0) {
1609                         lt= ob->data;
1610                         if(lt->id.lib) {
1611                                 if(okee("Make local")) {
1612                                         make_local_lattice(lt);
1613                                 }
1614                         }
1615                 }
1616                 break;
1617         
1618         case B_MATALONE:
1619                 if(ob==0) return;
1620                 ma= give_current_material(ob, ob->actcol);
1621                 idfrom= material_from(ob, ob->actcol);
1622                 if(idfrom && idfrom->lib==0) {
1623                         if(ma->id.us>1) {
1624                                 if(okee("Single user")) {
1625                                         ma= copy_material(ma);
1626                                         ma->id.us= 0;
1627                                         assign_material(ob, ma, ob->actcol);
1628                                 }
1629                         }
1630                 }
1631                 break;
1632         case B_MATLOCAL:
1633                 if(ob==0) return;
1634                 idfrom= material_from(ob, ob->actcol);
1635                 if(idfrom->lib==0) {
1636                         ma= give_current_material(ob, ob->actcol);
1637                         if(ma && ma->id.lib) {
1638                                 if(okee("Make local")) {
1639                                         make_local_material(ma);
1640                                 }
1641                         }
1642                 }
1643                 break;
1644
1645         case B_MESHLOCAL:
1646                 if(ob && ob->id.lib==0) {
1647                         me= ob->data;
1648                         if(me && me->id.lib) {
1649                                 if(okee("Make local")) {
1650                                         make_local_mesh(me);
1651                                         make_local_key( me->key );
1652
1653                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);                           
1654                                 }
1655                         }
1656                 }
1657                 break;
1658
1659         case B_MBALLALONE:
1660                 if(ob && ob->id.lib==0) {
1661                         mb= ob->data;
1662                         if(mb->id.us>1) {
1663                                 if(okee("Single user")) {
1664                                         ob->data= copy_mball(mb);
1665                                         mb->id.us--;
1666                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1667                                 }
1668                         }
1669                 }
1670                 break;
1671         case B_MBALLLOCAL:
1672                 if(ob && ob->id.lib==0) {
1673                         mb= ob->data;
1674                         if(mb->id.lib) {
1675                                 if(okee("Make local")) {
1676                                         make_local_mball(mb);
1677                                 }
1678                         }
1679                 }
1680                 break;
1681
1682         case B_CURVEALONE:
1683                 if(ob && ob->id.lib==0) {
1684                         cu= ob->data;
1685                         if(cu->id.us>1) {
1686                                 if(okee("Single user")) {
1687                                         ob->data= copy_curve(cu);
1688                                         cu->id.us--;
1689                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1690                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1691                                 }
1692                         }
1693                 }
1694                 break;
1695         case B_CURVELOCAL:
1696                 if(ob && ob->id.lib==0) {
1697                         cu= ob->data;
1698                         if(cu->id.lib) {
1699                                 if(okee("Make local")) {
1700                                         make_local_curve(cu);
1701                                         make_local_key( cu->key );
1702                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1703                                 }
1704                         }
1705                 }
1706                 break;
1707                 
1708         case B_TEXALONE:
1709                 if(G.buts->texfrom==0) {        /* from mat */
1710                         if(ob==0) return;
1711                         ma= give_current_material(ob, ob->actcol);
1712                         ma= editnode_get_active_material(ma);
1713                         if(ma && ma->id.lib==0) {
1714                                 mtex= ma->mtex[ ma->texact ];
1715                                 if(mtex->tex && mtex->tex->id.us>1) {
1716                                         if(okee("Single user")) {
1717                                                 mtex->tex->id.us--;
1718                                                 mtex->tex= copy_texture(mtex->tex);
1719                                         }
1720                                 }
1721                         }
1722                 }
1723                 else if(G.buts->texfrom==1) { /* from world */
1724                         wrld= G.scene->world;
1725                         if(wrld->id.lib==0) {
1726                                 mtex= wrld->mtex[ wrld->texact ];
1727                                 if(mtex->tex && mtex->tex->id.us>1) {
1728                                         if(okee("Single user")) {
1729                                                 mtex->tex->id.us--;
1730                                                 mtex->tex= copy_texture(mtex->tex);
1731                                         }
1732                                 }
1733                         }
1734                 }
1735                 else if(G.buts->texfrom==2) { /* from lamp */
1736                         if(ob==0 || ob->type!=OB_LAMP) return;
1737                         la= ob->data;
1738                         if(la->id.lib==0) {
1739                                 mtex= la->mtex[ la->texact ];
1740                                 if(mtex->tex && mtex->tex->id.us>1) {
1741                                         if(okee("Single user")) {
1742                                                 mtex->tex->id.us--;
1743                                                 mtex->tex= copy_texture(mtex->tex);
1744                                         }
1745                                 }
1746                         }
1747                 }
1748                 break;
1749         case B_TEXLOCAL:
1750                 if(G.buts->texfrom==0) {        /* from mat */
1751                         if(ob==0) return;
1752                         ma= give_current_material(ob, ob->actcol);
1753                         ma= editnode_get_active_material(ma);
1754                         if(ma && ma->id.lib==0) {
1755                                 mtex= ma->mtex[ ma->texact ];
1756                                 if(mtex->tex && mtex->tex->id.lib) {
1757                                         if(okee("Make local")) {
1758                                                 make_local_texture(mtex->tex);
1759                                         }
1760                                 }
1761                         }
1762                 }
1763                 else if(G.buts->texfrom==1) { /* from world */
1764                         wrld= G.scene->world;
1765                         if(wrld->id.lib==0) {
1766                                 mtex= wrld->mtex[ wrld->texact ];
1767                                 if(mtex->tex && mtex->tex->id.lib) {
1768                                         if(okee("Make local")) {
1769                                                 make_local_texture(mtex->tex);
1770                                         }
1771                                 }
1772                         }
1773                 }
1774                 else if(G.buts->texfrom==2) { /* from lamp */
1775                         if(ob==0 || ob->type!=OB_LAMP) return;
1776                         la= ob->data;
1777                         if(la->id.lib==0) {
1778                                 mtex= la->mtex[ la->texact ];
1779                                 if(mtex->tex && mtex->tex->id.lib) {
1780                                         if(okee("Make local")) {
1781                                                 make_local_texture(mtex->tex);
1782                                         }
1783                                 }
1784                         }
1785                 }
1786                 break;
1787         
1788         case B_IPOALONE:
1789                 ipo= G.sipo->ipo;
1790                 idfrom= G.sipo->from;
1791                 
1792                 if(idfrom && idfrom->lib==NULL) {
1793                         if(ipo->id.us>1) {
1794                                 if(okee("Single user")) {
1795                                         ipo= copy_ipo(ipo);
1796                                         ipo->id.us= 0;  /* assign_ipo adds users, copy_ipo sets to 1 */
1797                                         spaceipo_assign_ipo(G.sipo, ipo);
1798                                         allqueue(REDRAWIPO, 0);
1799                                 }
1800                         }
1801                 }
1802                 break;
1803         case B_IPOLOCAL:
1804                 ipo= G.sipo->ipo;
1805                 idfrom= G.sipo->from;
1806                 
1807                 if(idfrom && idfrom->lib==0) {
1808                         if(ipo->id.lib) {
1809                                 if(okee("Make local")) {
1810                                         make_local_ipo(ipo);
1811                                         allqueue(REDRAWIPO, 0);
1812                                 }
1813                         }
1814                 }
1815                 break;
1816
1817         case B_OBALONE:
1818                 if(G.scene->id.lib==0) {
1819                         if(ob->id.us>1) {
1820                                 if(okee("Single user")) {
1821                                         base= FIRSTBASE;
1822                                         while(base) {
1823                                                 if(base->object==ob) {
1824                                                         base->object= copy_object(ob);
1825                                                         ob->id.us--;
1826                                                         allqueue(REDRAWVIEW3D, 0);
1827                                                         break;
1828                                                 }
1829                                                 base= base->next;
1830                                         }
1831                                 }
1832                         }
1833                 }
1834                 break;
1835         case B_OBLOCAL:
1836                 if(G.scene->id.lib==0) {
1837                         if(ob->id.lib) {
1838                                 if(okee("Make local")) {
1839                                         make_local_object(ob);
1840                                         allqueue(REDRAWVIEW3D, 0);
1841                                 }
1842                         }
1843                 }
1844                 break;
1845         case B_MESHALONE:
1846                 if(ob && ob->id.lib==0) {
1847                         
1848                         me= ob->data;
1849                         
1850                         if(me && me->id.us>1) {
1851                                 if(okee("Single user")) {
1852                                         Mesh *men= copy_mesh(me);
1853                                         men->id.us= 0;
1854                                         
1855                                         set_mesh(ob, men);
1856                                         
1857                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1858                                         
1859                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1860                                 }
1861                         }
1862                 }
1863                 break;
1864         }
1865         
1866         BIF_undo_push("Make single user or local");
1867         allqueue(REDRAWBUTSALL, 0);
1868         allqueue(REDRAWOOPS, 0);
1869 }
1870
1871 /* ******************** GENERAL ********************** */
1872
1873 void do_headerbuttons(short event)
1874 {
1875
1876         if(event<=50) do_global_buttons2(event);
1877         else if(event<=100) do_global_buttons(event);
1878         else if(event<200) do_view3d_buttons(event);
1879         else if(event<250) do_ipo_buttons(event);
1880         else if(event<300) do_oops_buttons(event);
1881         else if(event<350) do_info_buttons(event);
1882         else if(event<400) do_image_buttons(event);
1883         else if(event<450) do_buts_buttons(event);
1884         else if(event<500) do_imasel_buttons(event);
1885         else if(event<525) do_text_buttons(event);
1886         else if(event<550) do_script_buttons(event);
1887         else if(event<600) do_file_buttons(event);
1888         else if(event<650) do_seq_buttons(event);
1889         else if(event<700) do_sound_buttons(event);
1890         else if(event<750) do_action_buttons(event);
1891         else if(event<800) do_time_buttons(curarea, event);
1892         else if(event<850) do_nla_buttons(event);
1893         else if(event<900) do_node_buttons(curarea, event);
1894         else if(event>=REDRAWVIEW3D) allqueue(event, 0);
1895 }
1896