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