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