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