The occosional warning cleanup;
[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                 else if(G.buts->mainb==CONTEXT_EDITING) {
1295                         SculptData *sd= &G.scene->sculptdata;
1296                         if(sd && sd->texact != -1) {
1297                                 if(sd->mtex[sd->texact]) autotexname(sd->mtex[sd->texact]->tex);
1298
1299                                 BIF_undo_push("Auto name");
1300                                 allqueue(REDRAWBUTSEDIT, 0);
1301                                 allqueue(REDRAWOOPS, 0);
1302                         }
1303                 }
1304                 break;
1305
1306         case B_RESETAUTOSAVE:
1307                 reset_autosave();
1308                 allqueue(REDRAWINFO, 0);
1309                 break;
1310         case B_SOUNDTOGGLE:
1311                 SYS_WriteCommandLineInt(SYS_GetSystem(), "noaudio", (U.gameflags & USER_DISABLE_SOUND));
1312                 break;
1313         case B_SHOWSPLASH:
1314                                 show_splash();
1315                 break;
1316         case B_MIPMAPCHANGED:
1317                 set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
1318                 allqueue(REDRAWVIEW3D, 0);
1319                 break;
1320         case B_NEWSPACE:
1321                 newspace(curarea, curarea->butspacetype);
1322                 break;
1323         case B_LOADTEMP:        /* is button from space.c */
1324                 BIF_read_autosavefile();
1325                 break;
1326
1327         case B_USERPREF:
1328                 allqueue(REDRAWINFO, 0);
1329                 break;
1330
1331         case B_DRAWINFO:        /* is button from space.c  *info* */
1332                 allqueue(REDRAWVIEW3D, 0);
1333                 break;
1334
1335         case B_PLAINMENUS:     /* is button from space.c  *info* */
1336                 reset_toolbox();
1337                 break;
1338
1339         case B_FLIPINFOMENU:    /* is button from space.c  *info* */
1340                 scrarea_queue_headredraw(curarea);
1341                 break;
1342
1343 #if 0
1344 //#ifdef _WIN32 // FULLSCREEN
1345         case B_FLIPFULLSCREEN:
1346                 if(U.uiflag & USER_FLIPFULLSCREEN)
1347                         U.uiflag &= ~USER_FLIPFULLSCREEN;
1348                 else
1349                         U.uiflag |= USER_FLIPFULLSCREEN;
1350                 mainwindow_toggle_fullscreen((U.uiflag & USER_FLIPFULLSCREEN));
1351                 break;
1352 #endif
1353
1354         /* Fileselect windows for user preferences file paths */
1355
1356         /* yafray: xml export dir. select */
1357         case B_YAFRAYDIRFILESEL:        /* space.c */
1358                 if(curarea->spacetype==SPACE_INFO) {
1359                         sa= closest_bigger_area();
1360                         areawinset(sa->win);
1361                 }
1362
1363                 activate_fileselect(FILE_SPECIAL, "SELECT YFEXPORT PATH", U.yfexportdir, filesel_u_yfexportdir);
1364                 break;
1365
1366         case B_FONTDIRFILESEL:  /* is button from space.c  *info* */
1367                 if(curarea->spacetype==SPACE_INFO) {
1368                         sa= closest_bigger_area();
1369                         areawinset(sa->win);
1370                 }
1371
1372                 activate_fileselect(FILE_SPECIAL, "SELECT FONT PATH", U.fontdir, filesel_u_fontdir);
1373                 break;
1374
1375         case B_TEXTUDIRFILESEL:         /* is button from space.c  *info* */
1376                 if(curarea->spacetype==SPACE_INFO) {
1377                         sa= closest_bigger_area();
1378                         areawinset(sa->win);
1379                 }
1380
1381                 activate_fileselect(FILE_SPECIAL, "SELECT TEXTURE PATH", U.textudir, filesel_u_textudir);
1382                 break;
1383         
1384         case B_PLUGTEXDIRFILESEL:               /* is button form space.c  *info* */
1385                 if(curarea->spacetype==SPACE_INFO) {
1386                         sa= closest_bigger_area();
1387                         areawinset(sa->win);
1388                 }
1389
1390                 activate_fileselect(FILE_SPECIAL, "SELECT TEX PLUGIN PATH", U.plugtexdir, filesel_u_plugtexdir);
1391                 break;
1392         
1393         case B_PLUGSEQDIRFILESEL:               /* is button from space.c  *info* */
1394                 if(curarea->spacetype==SPACE_INFO) {
1395                         sa= closest_bigger_area();
1396                         areawinset(sa->win);
1397                 }
1398
1399                 activate_fileselect(FILE_SPECIAL, "SELECT SEQ PLUGIN PATH", U.plugseqdir, filesel_u_plugseqdir);
1400                 break;
1401         
1402         case B_RENDERDIRFILESEL:        /* is button from space.c  *info* */
1403                 if(curarea->spacetype==SPACE_INFO) {
1404                         sa= closest_bigger_area();
1405                         areawinset(sa->win);
1406                 }
1407
1408                 activate_fileselect(FILE_SPECIAL, "SELECT RENDER PATH", U.renderdir, filesel_u_renderdir);
1409                 break;
1410
1411         case B_PYMENUEVAL: /* is button from space.c *info* */
1412                 BPyMenu_RemoveAllEntries(); /* free old data */
1413                 if (BPyMenu_Init(1) == -1) /* re-eval scripts registration in menus */
1414                         error("Invalid scripts dir: check console");
1415                 break;
1416         case B_PYTHONDIRFILESEL:        /* is button from space.c  *info* */
1417                 if(curarea->spacetype==SPACE_INFO) {
1418                         sa= closest_bigger_area();
1419                         areawinset(sa->win);
1420                 }
1421
1422                 activate_fileselect(FILE_SPECIAL, "SELECT SCRIPT PATH", U.pythondir, filesel_u_pythondir);
1423                 break;
1424
1425         case B_SOUNDDIRFILESEL:         /* is button from space.c  *info* */
1426                 if(curarea->spacetype==SPACE_INFO) {
1427                         sa= closest_bigger_area();
1428                         areawinset(sa->win);
1429                 }
1430
1431                 activate_fileselect(FILE_SPECIAL, "SELECT SOUND PATH", U.sounddir, filesel_u_sounddir);
1432                 break;
1433
1434         case B_TEMPDIRFILESEL:  /* is button from space.c  *info* */
1435                 if(curarea->spacetype==SPACE_INFO) {
1436                         sa= closest_bigger_area();
1437                         areawinset(sa->win);
1438                 }
1439
1440                 activate_fileselect(FILE_SPECIAL, "SELECT TEMP FILE PATH", U.tempdir, filesel_u_tempdir);
1441                 break;
1442
1443         /* END Fileselect windows for user preferences file paths */
1444
1445 #ifdef INTERNATIONAL
1446         case B_LOADUIFONT:      /* is button from space.c  *info* */
1447                 if(curarea->spacetype==SPACE_INFO) {
1448                         sa= closest_bigger_area();
1449                         areawinset(sa->win);
1450                 }
1451                 BLI_make_file_string("/", buf, U.fontdir, U.fontname);
1452                 activate_fileselect(FILE_SPECIAL, "LOAD UI FONT", buf, set_interface_font);
1453                 break;
1454
1455         case B_SETLANGUAGE:             /* is button from space.c  *info* */
1456                 lang_setlanguage();
1457                 allqueue(REDRAWALL, 0);
1458                 break;
1459
1460         case B_SETFONTSIZE:             /* is button from space.c  *info* */
1461                 refresh_interface_font();
1462                 FTF_SetSize(U.fontsize); 
1463                 allqueue(REDRAWALL, 0);
1464                 break;
1465                 
1466         case B_SETTRANSBUTS:    /* is button from space.c  *info* */
1467                 allqueue(REDRAWALL, 0);
1468                 break;
1469
1470         case B_RESTOREFONT:             /* is button from space.c  *info* */
1471                 U.fontsize= 0;
1472                 start_interface_font();
1473                 allqueue(REDRAWALL, 0);
1474                 break;
1475                 
1476         case B_USETEXTUREFONT:          /* is button from space.c  *info* */
1477                 refresh_interface_font();
1478                 allqueue(REDRAWALL, 0);
1479                 break;
1480
1481         case B_DOLANGUIFONT:    /* is button from space.c  *info* */
1482                 if(U.transopts & USER_DOTRANSLATE)
1483                         start_interface_font();
1484                 else
1485                         G.ui_international = FALSE;
1486                 allqueue(REDRAWALL, 0);
1487                 break;
1488 #endif
1489                 
1490         case B_FULL:
1491                 if(curarea->spacetype!=SPACE_INFO) {
1492                         area_fullscreen();
1493                 }
1494                 break;  
1495
1496         case B_IDNAME:
1497                         /* changing a metaballs name, sadly enough,
1498                          * can require it to be updated because its
1499                          * basis might have changed... -zr
1500                          */
1501                 if (ob && ob->type==OB_MBALL) {
1502                         DAG_scene_sort(G.scene);
1503                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1504                 }
1505                 /* redraw because name has changed: new pup */
1506                 scrarea_queue_headredraw(curarea);
1507                 allqueue(REDRAWINFO, 1);
1508                 allqueue(REDRAWOOPS, 1);
1509                 allqueue(REDRAWACTION, 1);
1510                 allqueue(REDRAWNLA, 1);
1511                 /* name scene also in set PUPmenu */
1512                 allqueue(REDRAWBUTSALL, 0);
1513                 allqueue(REDRAWHEADERS, 0);
1514
1515                 break;
1516         
1517         case B_KEEPDATA:
1518                 /* keep datablock. similar to pressing FKEY in a fileselect window
1519                  * maybe we can move that stuff to a seperate function? -- sg
1520                  */
1521                 if (curarea->spacetype==SPACE_BUTS) {
1522                         id= (ID *)G.buts->lockpoin;
1523                 } else if(curarea->spacetype==SPACE_IPO) {
1524                         id = (ID *)G.sipo->ipo;
1525                 } else if(curarea->spacetype==SPACE_NODE) {
1526                         id = ((SpaceNode *)curarea->spacedata.first)->id;
1527                 } /* similar for other spacetypes ? */
1528                 if (id) {
1529                         if( id->flag & LIB_FAKEUSER) {
1530                                 id->flag -= LIB_FAKEUSER;
1531                                 id->us--;
1532                         } else {
1533                                 id->flag |= LIB_FAKEUSER;
1534                                 id->us++;
1535                         }
1536                 }
1537                 allqueue(REDRAWHEADERS, 0);
1538
1539                 break;
1540
1541         }
1542 }
1543
1544
1545 void do_global_buttons2(short event)
1546 {
1547         Base *base;
1548         Object *ob;
1549         Material *ma;
1550         MTex *mtex;
1551         Mesh *me;
1552         Curve *cu;
1553         MetaBall *mb;
1554         Ipo *ipo;
1555         Lamp *la;
1556         Lattice *lt;
1557         World *wrld;
1558         ID *idfrom; 
1559         bAction *act;
1560         Brush *br;
1561
1562         /* general:  Single User is allowed when from==LOCAL 
1563          *                       Make Local is allowed when (from==LOCAL && id==LIB)
1564          */
1565                 
1566         if(event<B_LOCAL_ALONE) return;
1567
1568         ob= OBACT;
1569
1570         switch(event) {
1571                 
1572         case B_LAMPALONE:
1573                 if(ob && ob->id.lib==0) {
1574                         la= ob->data;
1575                         if(la->id.us>1) {
1576                                 if(okee("Single user")) {
1577                                         ob->data= copy_lamp(la);
1578                                         la->id.us--;
1579                                 }
1580                         }
1581                 }
1582                 break;
1583         case B_LAMPLOCAL:
1584                 if(ob && ob->id.lib==0) {
1585                         la= ob->data;
1586                         if(la->id.lib) {
1587                                 if(okee("Make local")) {
1588                                         make_local_lamp(la);
1589                                 }
1590                         }
1591                 }
1592                 break;
1593         
1594         case B_ARMLOCAL:
1595                 if (ob&&ob->id.lib==0){
1596                         bArmature *arm=ob->data;
1597                         if (arm->id.lib){
1598                                 if(okee("Make local")) {
1599                                         make_local_armature(arm);
1600                                 }
1601                         }
1602                 }
1603                 break;
1604         case B_ARMALONE:
1605                 if(ob && ob->id.lib==0) {
1606                         bArmature *arm=ob->data;
1607                         if(arm->id.us>1) {
1608                                 if(okee("Single user")) {
1609                                         ob->data= copy_armature(arm);
1610                                         armature_rebuild_pose(ob, ob->data);
1611                                         arm->id.us--;
1612                                 }
1613                         }
1614                 }
1615                 break;
1616         case B_ACTLOCAL:
1617                 if(ob && ob->id.lib==0) {
1618                         act= ob->action;
1619                         if(act->id.lib) {
1620                                 if(okee("Make local")) {
1621                                         make_local_action(act);
1622                                         allqueue(REDRAWACTION,0);
1623                                 }
1624                         }
1625                 }
1626                 break;
1627         case B_ACTALONE:
1628                 if(ob && ob->id.lib==0) {
1629                         act= ob->action;
1630                 
1631                         if(act->id.us>1) {
1632                                 if(okee("Single user")) {
1633                                         ob->action=copy_action(act);
1634                                         act->id.us--;
1635                                         allqueue(REDRAWACTION, 0);
1636                                 }
1637                         }
1638                 }
1639                 break;
1640
1641         case B_CAMERAALONE:
1642                 if(ob && ob->id.lib==0) {
1643                         Camera *ca= ob->data;
1644                         if(ca->id.us>1) {
1645                                 if(okee("Single user")) {
1646                                         ob->data= copy_camera(ca);
1647                                         ca->id.us--;
1648                                 }
1649                         }
1650                 }
1651                 break;
1652         case B_CAMERALOCAL:
1653                 if(ob && ob->id.lib==0) {
1654                         Camera *ca= ob->data;
1655                         if(ca->id.lib) {
1656                                 if(okee("Make local")) {
1657                                         make_local_camera(ca);
1658                                 }
1659                         }
1660                 }
1661                 break;
1662         case B_WORLDALONE:
1663                 wrld= G.scene->world;
1664                 if(wrld->id.us>1) {
1665                         if(okee("Single user")) {
1666                                 G.scene->world= copy_world(wrld);
1667                                 wrld->id.us--;
1668                         }
1669                 }
1670                 break;
1671         case B_WORLDLOCAL:
1672                 wrld= G.scene->world;
1673                 if(wrld && wrld->id.lib) {
1674                         if(okee("Make local")) {
1675                                 make_local_world(wrld);
1676                         }
1677                 }
1678                 break;
1679
1680         case B_LATTALONE:
1681                 if(ob && ob->id.lib==0) {
1682                         lt= ob->data;
1683                         if(lt->id.us>1) {
1684                                 if(okee("Single user")) {
1685                                         ob->data= copy_lattice(lt);
1686                                         lt->id.us--;
1687                                 }
1688                         }
1689                 }
1690                 break;
1691         case B_LATTLOCAL:
1692                 if(ob && ob->id.lib==0) {
1693                         lt= ob->data;
1694                         if(lt->id.lib) {
1695                                 if(okee("Make local")) {
1696                                         make_local_lattice(lt);
1697                                 }
1698                         }
1699                 }
1700                 break;
1701         
1702         case B_MATALONE:
1703                 if(ob==0) return;
1704                 ma= give_current_material(ob, ob->actcol);
1705                 idfrom= material_from(ob, ob->actcol);
1706                 if(idfrom && idfrom->lib==0) {
1707                         if(ma->id.us>1) {
1708                                 if(okee("Single user")) {
1709                                         ma= copy_material(ma);
1710                                         ma->id.us= 0;
1711                                         assign_material(ob, ma, ob->actcol);
1712                                 }
1713                         }
1714                 }
1715                 break;
1716         case B_MATLOCAL:
1717                 if(ob==0) return;
1718                 idfrom= material_from(ob, ob->actcol);
1719                 if(idfrom->lib==0) {
1720                         ma= give_current_material(ob, ob->actcol);
1721                         if(ma && ma->id.lib) {
1722                                 if(okee("Make local")) {
1723                                         make_local_material(ma);
1724                                 }
1725                         }
1726                 }
1727                 break;
1728
1729         case B_MESHLOCAL:
1730                 if(ob && ob->id.lib==0) {
1731                         me= ob->data;
1732                         if(me && me->id.lib) {
1733                                 if(okee("Make local")) {
1734                                         make_local_mesh(me);
1735                                         make_local_key( me->key );
1736
1737                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);                           
1738                                 }
1739                         }
1740                 }
1741                 break;
1742
1743         case B_MBALLALONE:
1744                 if(ob && ob->id.lib==0) {
1745                         mb= ob->data;
1746                         if(mb->id.us>1) {
1747                                 if(okee("Single user")) {
1748                                         ob->data= copy_mball(mb);
1749                                         mb->id.us--;
1750                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1751                                 }
1752                         }
1753                 }
1754                 break;
1755         case B_MBALLLOCAL:
1756                 if(ob && ob->id.lib==0) {
1757                         mb= ob->data;
1758                         if(mb->id.lib) {
1759                                 if(okee("Make local")) {
1760                                         make_local_mball(mb);
1761                                 }
1762                         }
1763                 }
1764                 break;
1765
1766         case B_CURVEALONE:
1767                 if(ob && ob->id.lib==0) {
1768                         cu= ob->data;
1769                         if(cu->id.us>1) {
1770                                 if(okee("Single user")) {
1771                                         ob->data= copy_curve(cu);
1772                                         cu->id.us--;
1773                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1774                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1775                                 }
1776                         }
1777                 }
1778                 break;
1779         case B_CURVELOCAL:
1780                 if(ob && ob->id.lib==0) {
1781                         cu= ob->data;
1782                         if(cu->id.lib) {
1783                                 if(okee("Make local")) {
1784                                         make_local_curve(cu);
1785                                         make_local_key( cu->key );
1786                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1787                                 }
1788                         }
1789                 }
1790                 break;
1791                 
1792         case B_TEXALONE:
1793                 if(G.buts->texfrom==0) {        /* from mat */
1794                         if(ob==0) return;
1795                         ma= give_current_material(ob, ob->actcol);
1796                         ma= editnode_get_active_material(ma);
1797                         if(ma && ma->id.lib==0) {
1798                                 mtex= ma->mtex[ ma->texact ];
1799                                 if(mtex->tex && mtex->tex->id.us>1) {
1800                                         if(okee("Single user")) {
1801                                                 mtex->tex->id.us--;
1802                                                 mtex->tex= copy_texture(mtex->tex);
1803                                         }
1804                                 }
1805                         }
1806                 }
1807                 else if(G.buts->texfrom==1) { /* from world */
1808                         wrld= G.scene->world;
1809                         if(wrld->id.lib==0) {
1810                                 mtex= wrld->mtex[ wrld->texact ];
1811                                 if(mtex->tex && mtex->tex->id.us>1) {
1812                                         if(okee("Single user")) {
1813                                                 mtex->tex->id.us--;
1814                                                 mtex->tex= copy_texture(mtex->tex);
1815                                         }
1816                                 }
1817                         }
1818                 }
1819                 else if(G.buts->texfrom==2) { /* from lamp */
1820                         if(ob==0 || ob->type!=OB_LAMP) return;
1821                         la= ob->data;
1822                         if(la->id.lib==0) {
1823                                 mtex= la->mtex[ la->texact ];
1824                                 if(mtex->tex && mtex->tex->id.us>1) {
1825                                         if(okee("Single user")) {
1826                                                 mtex->tex->id.us--;
1827                                                 mtex->tex= copy_texture(mtex->tex);
1828                                         }
1829                                 }
1830                         }
1831                 }
1832                 else if(G.buts->texfrom==3) { /* from brush */
1833                         br= G.scene->toolsettings->imapaint.brush;
1834                         if(br==0) return;
1835                         if(br->id.lib==0) {
1836                                 mtex= br->mtex[ br->texact ];
1837                                 if(mtex->tex && mtex->tex->id.us>1) {
1838                                         if(okee("Single user")) {
1839                                                 mtex->tex->id.us--;
1840                                                 mtex->tex= copy_texture(mtex->tex);
1841                                                 allqueue(REDRAWIMAGE, 0);
1842                                         }
1843                                 }
1844                         }
1845                 }
1846                 break;
1847         case B_TEXLOCAL:
1848                 if(G.buts->texfrom==0) {        /* from mat */
1849                         if(ob==0) return;
1850                         ma= give_current_material(ob, ob->actcol);
1851                         ma= editnode_get_active_material(ma);
1852                         if(ma && ma->id.lib==0) {
1853                                 mtex= ma->mtex[ ma->texact ];
1854                                 if(mtex->tex && mtex->tex->id.lib) {
1855                                         if(okee("Make local")) {
1856                                                 make_local_texture(mtex->tex);
1857                                         }
1858                                 }
1859                         }
1860                 }
1861                 else if(G.buts->texfrom==1) { /* from world */
1862                         wrld= G.scene->world;
1863                         if(wrld->id.lib==0) {
1864                                 mtex= wrld->mtex[ wrld->texact ];
1865                                 if(mtex->tex && mtex->tex->id.lib) {
1866                                         if(okee("Make local")) {
1867                                                 make_local_texture(mtex->tex);
1868                                         }
1869                                 }
1870                         }
1871                 }
1872                 else if(G.buts->texfrom==2) { /* from lamp */
1873                         if(ob==0 || ob->type!=OB_LAMP) return;
1874                         la= ob->data;
1875                         if(la->id.lib==0) {
1876                                 mtex= la->mtex[ la->texact ];
1877                                 if(mtex->tex && mtex->tex->id.lib) {
1878                                         if(okee("Make local")) {
1879                                                 make_local_texture(mtex->tex);
1880                                         }
1881                                 }
1882                         }
1883                 }
1884                 else if(G.buts->texfrom==3) { /* from brush */
1885                         br= G.scene->toolsettings->imapaint.brush;
1886                         if(br==0) return;
1887                         if(br->id.lib==0) {
1888                                 mtex= br->mtex[ br->texact ];
1889                                 if(mtex->tex && mtex->tex->id.lib) {
1890                                         if(okee("Make local")) {
1891                                                 make_local_texture(mtex->tex);
1892                                                 allqueue(REDRAWIMAGE, 0);
1893                                         }
1894                                 }
1895                         }
1896                 }
1897                 break;
1898         
1899         case B_IPOALONE:
1900                 ipo= G.sipo->ipo;
1901                 idfrom= G.sipo->from;
1902                 
1903                 if(idfrom && idfrom->lib==NULL) {
1904                         if(ipo->id.us>1) {
1905                                 if(okee("Single user")) {
1906                                         ipo= copy_ipo(ipo);
1907                                         ipo->id.us= 0;  /* assign_ipo adds users, copy_ipo sets to 1 */
1908                                         spaceipo_assign_ipo(G.sipo, ipo);
1909                                         allqueue(REDRAWIPO, 0);
1910                                 }
1911                         }
1912                 }
1913                 break;
1914         case B_IPOLOCAL:
1915                 ipo= G.sipo->ipo;
1916                 idfrom= G.sipo->from;
1917                 
1918                 if(idfrom && idfrom->lib==0) {
1919                         if(ipo->id.lib) {
1920                                 if(okee("Make local")) {
1921                                         make_local_ipo(ipo);
1922                                         allqueue(REDRAWIPO, 0);
1923                                 }
1924                         }
1925                 }
1926                 break;
1927
1928         case B_OBALONE:
1929                 if(G.scene->id.lib==0) {
1930                         if(ob->id.us>1) {
1931                                 if(okee("Single user")) {
1932                                         base= FIRSTBASE;
1933                                         while(base) {
1934                                                 if(base->object==ob) {
1935                                                         base->object= copy_object(ob);
1936                                                         ob->id.us--;
1937                                                         allqueue(REDRAWVIEW3D, 0);
1938                                                         break;
1939                                                 }
1940                                                 base= base->next;
1941                                         }
1942                                 }
1943                         }
1944                 }
1945                 break;
1946         case B_OBLOCAL:
1947                 if(G.scene->id.lib==0) {
1948                         if(ob->id.lib) {
1949                                 if(okee("Make local")) {
1950                                         make_local_object(ob);
1951                                         allqueue(REDRAWVIEW3D, 0);
1952                                 }
1953                         }
1954                 }
1955                 break;
1956         case B_MESHALONE:
1957                 if(ob && ob->id.lib==0) {
1958                         
1959                         me= ob->data;
1960                         
1961                         if(me && me->id.us>1) {
1962                                 if(okee("Single user")) {
1963                                         Mesh *men= copy_mesh(me);
1964                                         men->id.us= 0;
1965                                         
1966                                         set_mesh(ob, men);
1967                                         
1968                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1969                                         
1970                                         if(ob==G.obedit) allqueue(REDRAWVIEW3D, 0);
1971                                 }
1972                         }
1973                 }
1974                 break;
1975         }
1976         
1977         BIF_undo_push("Make single user or local");
1978         allqueue(REDRAWBUTSALL, 0);
1979         allqueue(REDRAWOOPS, 0);
1980 }
1981
1982 /* ******************** GENERAL ********************** */
1983
1984 void do_headerbuttons(short event)
1985 {
1986
1987         if(event<=50) do_global_buttons2(event);
1988         else if(event<=100) do_global_buttons(event);
1989         else if(event<200) do_view3d_buttons(event);
1990         else if(event<250) do_ipo_buttons(event);
1991         else if(event<300) do_oops_buttons(event);
1992         else if(event<350) do_info_buttons(event);
1993         else if(event<400) do_image_buttons(event);
1994         else if(event<450) do_buts_buttons(event);
1995         else if(event<500) do_imasel_buttons(event);
1996         else if(event<525) do_text_buttons(event);
1997         else if(event<550) do_script_buttons(event);
1998         else if(event<600) do_file_buttons(event);
1999         else if(event<650) do_seq_buttons(event);
2000         else if(event<700) do_sound_buttons(event);
2001         else if(event<750) do_action_buttons(event);
2002         else if(event<800) do_time_buttons(curarea, event);
2003         else if(event<850) do_nla_buttons(event);
2004         else if(event<900) do_node_buttons(curarea, event);
2005         else if(event>=REDRAWVIEW3D) allqueue(event, 0);
2006 }
2007