64c79f0924ceff4aabac3a6b7fd282169c57de97
[blender.git] / source / blender / src / space.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  * - hier het initialiseren en vrijgeven van SPACE data
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifdef WIN32
42 #include "BLI_winstuff.h"
43 #endif
44
45 #include "MEM_guardedalloc.h"
46
47 #include "IMB_imbuf_types.h"
48 #include "IMB_imbuf.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52 #include "BLI_editVert.h"
53 #include "BLI_linklist.h"
54
55 #include "DNA_action_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_image_types.h"
58 #include "DNA_ipo_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_scene_types.h"
62 #include "DNA_screen_types.h"
63 #include "DNA_sequence_types.h"
64 #include "DNA_sound_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_userdef_types.h"
67 #include "DNA_view2d_types.h"
68 #include "DNA_view3d_types.h"
69
70 #include "BKE_blender.h"
71 #include "BKE_curve.h"
72 #include "BKE_displist.h"
73 #include "BKE_global.h"
74 #include "BKE_ipo.h"
75 #include "BKE_main.h"
76 #include "BKE_scene.h"
77 #include "BKE_utildefines.h"
78
79 #include "BIF_buttons.h"
80 #include "BIF_drawimage.h"
81 #include "BIF_drawseq.h"
82 #include "BIF_drawtext.h"
83 #include "BIF_editarmature.h"
84 #include "BIF_editfont.h"
85 #include "BIF_editika.h"
86 #include "BIF_editkey.h"
87 #include "BIF_editlattice.h"
88 #include "BIF_editmesh.h"
89 #include "BIF_editoops.h"
90 #include "BIF_editseq.h"
91 #include "BIF_editsima.h"
92 #include "BIF_editsound.h"
93 #include "BIF_editview.h"
94 #include "BIF_gl.h"
95 #include "BIF_imasel.h"
96 #include "BIF_interface.h"
97 #include "BIF_mywindow.h"
98 #include "BIF_oops.h"
99 #include "BIF_resources.h"
100 #include "BIF_screen.h"
101 #include "BIF_space.h"
102 #include "BIF_spacetypes.h"
103 #include "BIF_toets.h"
104 #include "BIF_toolbox.h"
105 #include "BIF_usiblender.h"
106 #include "BIF_previewrender.h"
107
108 #include "BSE_edit.h"
109 #include "BSE_view.h"
110 #include "BSE_editipo.h"
111 #include "BSE_drawipo.h"
112 #include "BSE_drawview.h"
113 #include "BSE_drawnla.h"
114 #include "BSE_filesel.h"
115 #include "BSE_headerbuttons.h"
116 #include "BSE_editnla_types.h"
117
118 #include "BDR_vpaint.h"
119 #include "BDR_editmball.h"
120 #include "BDR_editobject.h"
121 #include "BDR_editcurve.h"
122 #include "BDR_editface.h"
123 #include "BDR_drawmesh.h"
124
125 #include "BLO_readfile.h" /* for BLO_blendhandle_close */
126
127 #include "interface.h"
128 #include "mydevice.h"
129 #include "blendef.h"
130 #include "datatoc.h"
131
132 #include "BPY_extern.h" // Blender Python library
133
134 #include "TPT_DependKludge.h"
135 #ifdef NAN_TPT
136 #include "BSE_trans_types.h"
137 #include "IMG_Api.h"
138 #endif /* NAN_TPT */
139
140 #include "SYS_System.h" /* for the user def menu ... should move elsewhere. */
141
142 extern void StartKetsjiShell(ScrArea *area, char* startscenename, struct Main* maggie, int always_use_expand_framing);
143
144 /**
145  * When the mipmap setting changes, we want to redraw the view right
146  * away to reflect this setting.
147  */
148 void space_mipmap_button_function(int event);
149
150
151 unsigned short convert_for_nonumpad(unsigned short event);
152 void free_soundspace(SpaceSound *ssound);
153
154 /* ************* SPACE: VIEW3D  ************* */
155
156 /*  extern void drawview3d(); BSE_drawview.h */
157
158
159 void copy_view3d_lock(short val)
160 {
161         bScreen *sc;
162         int bit;
163         
164         /* van G.scene naar andere views kopieeren */
165         sc= G.main->screen.first;
166         
167         while(sc) {
168                 if(sc->scene==G.scene) {
169                         ScrArea *sa= sc->areabase.first;
170                         while(sa) {
171                                 SpaceLink *sl= sa->spacedata.first;
172                                 while(sl) {
173                                         if(sl->spacetype==SPACE_OOPS && val==REDRAW) {
174                                                 if(sa->win) scrarea_queue_winredraw(sa);
175                                         }
176                                         else if(sl->spacetype==SPACE_VIEW3D) {
177                                                 View3D *vd= (View3D*) sl;
178                                                 if(vd->scenelock && vd->localview==0) {
179                                                         vd->lay= G.scene->lay;
180                                                         vd->camera= G.scene->camera;
181                                                         
182                                                         if(vd->camera==0 && vd->persp>1) vd->persp= 1;
183                                                         
184                                                         if( (vd->lay & vd->layact) == 0) {
185                                                                 bit= 0;
186                                                                 while(bit<32) {
187                                                                         if(vd->lay & (1<<bit)) {
188                                                                                 vd->layact= 1<<bit;
189                                                                                 break;
190                                                                         }
191                                                                         bit++;
192                                                                 }
193                                                         }
194                                                         
195                                                         if(val==REDRAW && vd==sa->spacedata.first) {
196                                                                 scrarea_queue_redraw(sa);
197                                                         }
198                                                 }
199                                         }
200                                         sl= sl->next;
201                                 }
202                                 sa= sa->next;
203                         }
204                 }
205                 sc= sc->id.next;
206         }
207 }
208
209 void handle_view3d_lock()
210 {
211         if (G.vd != NULL) {
212                 if(G.vd->localview==0 && G.vd->scenelock && curarea->spacetype==SPACE_VIEW3D) {
213
214                         /* naar scene kopieeren */
215                         G.scene->lay= G.vd->lay;
216                         G.scene->camera= G.vd->camera;
217         
218                         copy_view3d_lock(REDRAW);
219                 }
220         }
221 }
222
223 void space_set_commmandline_options(void) {
224         SYS_SystemHandle syshandle;
225         int a;
226                 
227         if ( (syshandle = SYS_GetSystem()) ) {
228                 /* User defined settings */
229                 a= (U.gameflags & USERDEF_VERTEX_ARRAYS);
230                 SYS_WriteCommandLineInt(syshandle, "vertexarrays", a);
231
232                 a= (U.gameflags & USERDEF_DISABLE_SOUND);
233                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
234
235                 a= (U.gameflags & USERDEF_DISABLE_MIPMAP);
236                 set_mipmap(!a);
237                 SYS_WriteCommandLineInt(syshandle, "nomipmap", a);
238
239                 /* File specific settings: */
240                 /* Only test the first one. These two are switched
241                  * simultaneously. */
242                 a= (G.fileflags & G_FILE_SHOW_FRAMERATE);
243                 SYS_WriteCommandLineInt(syshandle, "show_framerate", a);
244                 SYS_WriteCommandLineInt(syshandle, "show_profile", a);
245
246                 /* When in wireframe mode, always draw debug props. */
247                 if (G.vd) {
248                         a = ( (G.fileflags & G_FILE_SHOW_DEBUG_PROPS) 
249                                   || (G.vd->drawtype == OB_WIRE)          
250                                   || (G.vd->drawtype == OB_SOLID)         );
251                         SYS_WriteCommandLineInt(syshandle, "show_properties", a);
252                 }
253
254                 a= (G.fileflags & G_FILE_ENABLE_ALL_FRAMES);
255                 SYS_WriteCommandLineInt(syshandle, "fixedtime", a);
256         }
257 }
258
259         /**
260          * These two routines imported from the gameengine, 
261          * I suspect a lot of the resetting stuff is cruft
262          * and can be removed, but it should be checked.
263          */
264 static void SaveState(void)
265 {
266         glPushAttrib(GL_ALL_ATTRIB_BITS);
267
268         init_realtime_GL();
269         init_gl_stuff();
270
271         if(G.scene->camera==0 || G.scene->camera->type!=OB_CAMERA)
272                 error("no (correct) camera");
273
274         waitcursor(1);
275 }
276
277 static void RestoreState(void)
278 {
279         curarea->win_swap = 0;
280         curarea->head_swap=0;
281         allqueue(REDRAWVIEW3D, 0);
282         allqueue(REDRAWBUTSALL, 0);
283         reset_slowparents();
284         waitcursor(0);
285         G.qual= 0;
286         glPopAttrib();
287 }
288
289 static LinkNode *save_and_reset_all_scene_cfra(void)
290 {
291         LinkNode *storelist= NULL;
292         Scene *sc;
293         
294         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
295                 BLI_linklist_prepend(&storelist, (void*) sc->r.cfra);
296                 sc->r.cfra= 1;
297
298                 set_scene_bg(sc);
299         }
300         
301         BLI_linklist_reverse(&storelist);
302         
303         return storelist;
304 }
305
306 static void restore_all_scene_cfra(LinkNode *storelist) {
307         LinkNode *sc_store= storelist;
308         Scene *sc;
309         
310         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
311                 int stored_cfra= (int) sc_store->link;
312                 
313                 sc->r.cfra= stored_cfra;
314                 set_scene_bg(sc);
315                 
316                 sc_store= sc_store->next;
317         }
318         
319         BLI_linklist_free(storelist, NULL);
320 }
321
322 void start_game(void)
323 {
324 #if GAMEBLENDER == 1
325         Scene *sc, *startscene = G.scene;
326         LinkNode *scene_cfra_store;
327
328                 /* XXX, silly code -  the game engine can
329                  * access any scene through logic, so we try 
330                  * to make sure each scene has a valid camera, 
331                  * just in case the game engine tries to use it.
332                  * 
333                  * Better would be to make a better routine
334                  * in the game engine for finding the camera.
335                  *  - zr
336                  */
337         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
338                 if (!sc->camera) {
339                         Base *base;
340         
341                         for (base= sc->base.first; base; base= base->next)
342                                 if (base->object->type==OB_CAMERA)
343                                         break;
344                         
345                         sc->camera= base?base->object:NULL;
346                 }
347         }
348
349         /* these two lines make sure front and backbuffer are equal. for swapbuffers */
350         markdirty_all();
351         screen_swapbuffers();
352
353         /* can start from header */
354         mywinset(curarea->win);
355     
356         scene_cfra_store= save_and_reset_all_scene_cfra();
357         
358         BPY_end_python();
359
360         sound_stop_all_sounds();
361
362         /* Before jumping into Ketsji, we configure some settings. */
363         space_set_commmandline_options();
364
365         SaveState();
366         StartKetsjiShell(curarea, startscene->id.name+2, G.main, 1);
367         RestoreState();
368
369         BPY_start_python();
370
371         restore_all_scene_cfra(scene_cfra_store);
372         set_scene_bg(startscene);
373         
374         if (G.flags & G_FLAGS_AUTOPLAY)
375                 exit_usiblender();
376
377                 /* groups could have changed ipo */
378         allqueue(REDRAWNLA, 0);
379         allqueue(REDRAWACTION, 0);
380         allspace(REMAKEIPO, 0);
381         allqueue(REDRAWIPO, 0);
382 #endif
383 }
384
385 void changeview3d()
386 {
387         
388         setwinmatrixview3d(0);  /* 0= geen pick rect */
389         
390 }
391
392         /* Callable from editmode and faceselect mode from the
393          * moment, would be nice (and is easy) to generalize
394          * to any mode.
395          */
396 static void align_view_to_selected(View3D *v3d)
397 {
398         int nr= pupmenu("Align view%t|To selection (top)%x2|To selection (front)%x1|To selection (side)%x0");
399
400         if (nr!=-1) {
401                 int axis= nr;
402
403                 if (G.obedit && (G.obedit->type == OB_MESH)) {
404                         editmesh_align_view_to_selected(v3d, axis);
405                         addqueue(v3d->area->win, REDRAW, 1);
406                 } else if (G.f & G_FACESELECT) {
407                         Object *obact= OBACT;
408                         if (obact && obact->type==OB_MESH) {
409                                 Mesh *me= obact->data;
410
411                                 if (me->tface) {
412                                         faceselect_align_view_to_selected(v3d, me, axis);
413                                         addqueue(v3d->area->win, REDRAW, 1);
414                                 }
415                         }
416                 }
417         }
418 }
419
420 void winqread3d(unsigned short event, short val, char ascii)
421 {
422         View3D *v3d= curarea->spacedata.first;
423         Object *ob;
424         float *curs;
425         int doredraw= 0, pupval;
426         
427         if(curarea->win==0) return;     /* hier komtie vanuit sa->headqread() */
428         if(event==MOUSEY) return;
429         
430         if(val) {
431
432                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
433
434                 /* TEXTEDITING?? */
435                 if(G.obedit && G.obedit->type==OB_FONT) {
436                         switch(event) {
437                         
438                         case LEFTMOUSE:
439                                 mouse_cursor();
440                                 break;
441                         case MIDDLEMOUSE:
442                                 if(U.flag & VIEWMOVE) {
443                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
444                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
445                                         else viewmove(1);
446                                 }
447                                 else {
448                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
449                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
450                                         else viewmove(0);
451                                 }
452                         case UKEY:
453                                 if(G.qual & LR_ALTKEY) {
454                                         remake_editText();
455                                         doredraw= 1;
456                                 } else {
457                                         do_textedit(event, val, ascii);
458                                 }
459                                 break;
460                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
461                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
462                         case PADENTER:
463                                 persptoetsen(event);
464                                 doredraw= 1;
465                                 break;
466                                 
467                         default:
468                                 do_textedit(event, val, ascii);
469                                 break;
470                         }
471                 }
472                 else {
473                         switch(event) {
474                         
475                         case BACKBUFDRAW:
476                                 backdrawview3d(1);
477                                 break;
478                                 
479                         case LEFTMOUSE:
480                                 if (G.obedit || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
481                                         mouse_cursor();
482                                 }
483                                 else if (G.f & G_VERTEXPAINT) {
484                                         vertex_paint();
485                                 }
486                                 else if (G.f & G_WEIGHTPAINT){
487                                         weight_paint();
488                                 }
489                                 else if (G.f & G_TEXTUREPAINT) {
490                                         face_draw();
491                                 }
492                                 break;
493                         case MIDDLEMOUSE:
494                                 if(U.flag & VIEWMOVE) {
495                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
496                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
497                                         else viewmove(1);
498                                 }
499                                 else {
500                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
501                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
502                                         else viewmove(0);
503                                 }
504                                 break;
505                         case RIGHTMOUSE:
506                                 if(G.obedit && (G.qual & LR_CTRLKEY)==0) {
507                                         if(G.obedit->type==OB_MESH) mouse_mesh();
508                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) mouse_nurb();
509                                         else if(G.obedit->type==OB_MBALL) mouse_mball();
510                                         else if(G.obedit->type==OB_LATTICE) mouse_lattice();
511                                         else if(G.obedit->type==OB_ARMATURE) mouse_armature();
512                                 }
513                                 else if(G.obpose) { 
514                                         if (G.obpose->type==OB_ARMATURE) mousepose_armature();
515                                 }
516                                 else if( G.qual & LR_CTRLKEY ) mouse_select();
517                                 else if(G.f & G_FACESELECT) face_select();
518                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) sample_vpaint();
519                                 else
520                                         mouse_select();
521                                 break;
522                         
523                         case ONEKEY:
524                                 do_layer_buttons(0); break;
525                         case TWOKEY:
526                                 do_layer_buttons(1); break;
527                         case THREEKEY:
528                                 do_layer_buttons(2); break;
529                         case FOURKEY:
530                                 do_layer_buttons(3); break;
531                         case FIVEKEY:
532                                 do_layer_buttons(4); break;
533                         case SIXKEY:
534                                 do_layer_buttons(5); break;
535                         case SEVENKEY:
536                                 do_layer_buttons(6); break;
537                         case EIGHTKEY:
538                                 do_layer_buttons(7); break;
539                         case NINEKEY:
540                                 do_layer_buttons(8); break;
541                         case ZEROKEY:
542                                 do_layer_buttons(9); break;
543                         case MINUSKEY:
544                                 do_layer_buttons(10); break;
545                         case EQUALKEY:
546                                 do_layer_buttons(11); break;
547                         case ACCENTGRAVEKEY:
548                                 do_layer_buttons(-1); break;
549                                 
550                         case AKEY:
551                                 if(G.qual & LR_CTRLKEY) apply_object();
552                                 else if(G.qual & LR_SHIFTKEY) {
553                                         tbox_setmain(0);
554                                         toolbox();
555                                 }
556                                 else {
557                                         if(G.obedit) {
558                                                 if(G.obedit->type==OB_MESH) deselectall_mesh();
559                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) deselectall_nurb();
560                                                 else if(G.obedit->type==OB_MBALL) deselectall_mball();
561                                                 else if(G.obedit->type==OB_LATTICE) deselectall_Latt();
562                                                 else if(G.obedit->type==OB_ARMATURE) deselectall_armature();
563                                         }
564                                         else if (G.obpose){
565                                                 switch (G.obpose->type){
566                                                 case OB_ARMATURE:
567                                                         deselectall_posearmature(1);
568                                                         break;
569                                                 }
570                                         }
571                                         else {
572                                                 if(G.f & G_FACESELECT) deselectall_tface();
573                                                 else deselectall();
574                                         }
575                                 }
576                                 break;
577                         case BKEY:
578                                 if(G.qual & LR_SHIFTKEY) set_render_border();
579                                 else borderselect();
580                                 break;
581                         case CKEY:
582                                 if(G.qual & LR_CTRLKEY) {
583                                         copymenu();
584                                 }
585                                 else if(G.qual & LR_ALTKEY) {
586                                         convertmenu();  /* editobject.c */
587                                 }
588                                 else if(G.qual & LR_SHIFTKEY) {
589                                         view3d_home(1);
590                                         curs= give_cursor();
591                                         curs[0]=curs[1]=curs[2]= 0.0;
592                                         scrarea_queue_winredraw(curarea);
593                                 }
594                                 else if(G.obedit!=0 && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
595                                         makecyclicNurb();
596                                         makeDispList(G.obedit);
597                                         allqueue(REDRAWVIEW3D, 0);
598                                 }
599                                 else {
600                                         curs= give_cursor();
601                                         G.vd->ofs[0]= -curs[0];
602                                         G.vd->ofs[1]= -curs[1];
603                                         G.vd->ofs[2]= -curs[2];
604                                         scrarea_queue_winredraw(curarea);
605                                 }
606                         
607                                 break;
608                         case DKEY:
609                                 if(G.qual & LR_SHIFTKEY) {
610                                         if(G.obedit) {
611                                                 if(G.obedit->type==OB_MESH) adduplicate_mesh();
612                                                 else if(G.obedit->type==OB_ARMATURE) adduplicate_armature();
613                                                 else if(G.obedit->type==OB_MBALL) adduplicate_mball();
614                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) adduplicate_nurb();
615                                         }
616                                         else if(G.obpose){
617                                                 error ("Duplicate not possible in posemode.");
618                                         }
619                                         else adduplicate(0);
620                                 }
621                                 else if(G.qual & LR_ALTKEY) {
622                                         if(G.obpose) error ("Duplicate not possible in posemode.");
623                                         else
624                                         if(G.obedit==0) adduplicate(0);
625                                 }
626                                 else if(G.qual & LR_CTRLKEY) {
627                                         imagestodisplist();
628                                 }
629                                 else {
630                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4");
631                                         if(pupval>0) {
632                                                 G.vd->drawtype= pupval;
633                                                 doredraw= 1;
634                                         
635                                         }
636                                 }
637                                 
638                                 break;
639                         case EKEY:
640                                 if(G.obedit) {
641                                         if(G.obedit->type==OB_MESH) extrude_mesh();
642                                         else if(G.obedit->type==OB_CURVE) addvert_Nurb('e');
643                                         else if(G.obedit->type==OB_SURF) extrude_nurb();
644                                         else if(G.obedit->type==OB_ARMATURE) extrude_armature();
645                                 }
646                                 else {
647                                         ob= OBACT;
648                                         if(ob && ob->type==OB_IKA) if(okee("extrude IKA")) extrude_ika(ob, 1);
649                                 }
650                                 break;
651                         case FKEY:
652                                 if(G.obedit) {
653                                         if(G.obedit->type==OB_MESH) {
654                                                 if(G.qual & LR_SHIFTKEY) fill_mesh();
655                                                 else if(G.qual & LR_ALTKEY) beauty_fill();
656                                                 else if(G.qual & LR_CTRLKEY) edge_flip();
657                                                 else addedgevlak_mesh();
658                                         }
659                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
660                                 }
661                                 else if(G.qual & LR_CTRLKEY) sort_faces();
662                                 else if(G.qual & LR_SHIFTKEY) fly();
663                                 else {
664                                                 set_faceselect();
665                                         }
666                                 
667                                 break;
668                         case GKEY:
669                                 /* RMGRP if(G.qual & LR_CTRLKEY) add_selected_to_group();
670                                 else if(G.qual & LR_ALTKEY) rem_selected_from_group();
671                                 else if(G.qual & LR_SHIFTKEY) group_menu();
672                                 else */ 
673                                 if(G.qual & LR_ALTKEY) clear_object('g');
674                                 else
675                                         transform('g');
676                                 break;
677                         case HKEY:
678                                 if(G.obedit) {
679                                         if(G.obedit->type==OB_MESH) {
680                                                 if(G.qual & LR_ALTKEY) reveal_mesh();
681                                                 else hide_mesh(G.qual & LR_SHIFTKEY);
682                                         }
683                                         else if(G.obedit->type== OB_SURF) {
684                                                 if(G.qual & LR_ALTKEY) revealNurb();
685                                                 else hideNurb(G.qual & LR_SHIFTKEY);
686                                         }
687                                         else if(G.obedit->type==OB_CURVE) {
688                                         
689                                                 if(G.qual & LR_CTRLKEY) autocalchandlesNurb_all(1);     /* flag=1, selected */
690                                                 else if(G.qual & LR_SHIFTKEY) sethandlesNurb(1);
691                                                 else sethandlesNurb(3);
692                                                 
693                                                 makeDispList(G.obedit);
694                                                 
695                                                 allqueue(REDRAWVIEW3D, 0);
696                                         }
697                                 }
698                                 else if(G.f & G_FACESELECT) hide_tface();
699                                 
700                                 break;
701                         case IKEY:
702                                 break;
703                                 
704                         case JKEY:
705                                 if(G.qual & LR_CTRLKEY) {
706                                         if( (ob= OBACT) ) {
707                                                 if(ob->type == OB_MESH) join_mesh();
708                                                 else if(ob->type == OB_CURVE) join_curve(OB_CURVE);
709                                                 else if(ob->type == OB_SURF) join_curve(OB_SURF);
710                                                 else if(ob->type == OB_ARMATURE) join_armature ();
711                                         }
712                                         else if (G.obedit && ELEM(G.obedit->type, OB_CURVE, OB_SURF)) addsegment_nurb();
713                                 } else if(G.obedit) {
714                                         if(G.obedit->type==OB_MESH) {
715                                                 join_triangles();
716                                         }
717                                 }
718
719                                 break;
720                         case KKEY:
721                                 if(G.obedit) {
722                                         if(G.obedit->type==OB_SURF) printknots();
723                                 }
724                                 else {
725                                         if(G.qual & LR_SHIFTKEY) {
726                                                 if(G.f & G_FACESELECT) clear_vpaint_selectedfaces();
727                                                 else if(G.f & G_VERTEXPAINT) clear_vpaint();
728                                                 else select_select_keys();
729                                         }
730                                         else if(G.qual & LR_CTRLKEY) make_skeleton();
731 /*                                      else if(G.qual & LR_ALTKEY) delete_skeleton(); */
732                                         else set_ob_ipoflags();
733                                 }
734                                 
735                                 break;
736                         case LKEY:
737                                 if(G.obedit) {
738                                         if(G.obedit->type==OB_MESH) selectconnected_mesh();
739                                         if(G.obedit->type==OB_ARMATURE) selectconnected_armature();
740                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) selectconnected_nurb();
741                                 }
742                                 else if(G.obpose) {
743                                         if(G.obpose->type==OB_ARMATURE) selectconnected_posearmature();
744                                 }
745                                 else {
746                                 
747                                         if(G.qual & LR_SHIFTKEY) selectlinks();
748                                         else if(G.qual & LR_CTRLKEY) linkmenu();
749                                         else if(G.f & G_FACESELECT) select_linked_tfaces();
750                                         else make_local();
751                                 }
752                                 break;
753                         case MKEY:
754                                 movetolayer();
755                                 break;
756                         case NKEY:
757                                 if(G.obedit) {
758                                         switch (G.obedit->type){
759                                         case OB_ARMATURE:
760                                                 if (okee("Recalc bone roll angles")) auto_align_armature();
761                                                 break;
762                                         case OB_MESH: 
763                                                 if(G.qual & LR_SHIFTKEY) {
764                                                         if(okee("Recalc normals inside")) righthandfaces(2);
765                                                 }
766                                                 else {
767                                                         if(okee("Recalc normals outside")) righthandfaces(1);
768                                                 }
769                                                 break;
770                                         }
771                                         allqueue(REDRAWVIEW3D, 0);
772                                 }
773                                 break;
774                         case OKEY:
775                                 if(G.qual & LR_ALTKEY) clear_object('o');
776                                 else if(G.obedit) {
777                                         extern int prop_mode;
778
779                                         if (G.qual & LR_SHIFTKEY) prop_mode= !prop_mode;
780                                         else G.f ^= G_PROPORTIONAL;
781
782                                         allqueue(REDRAWHEADERS, 0);
783                                 }
784                                 break;
785                         case PKEY:
786                                 
787                                 if(G.obedit) {
788                                         if(G.qual) {
789                                                 if(G.qual & LR_CTRLKEY) make_parent();
790                                         }
791                                         else if(G.obedit->type==OB_MESH) separate_mesh();
792                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) separate_nurb();
793                                 }
794                                 else if(G.qual & LR_CTRLKEY) make_parent();
795                                 else if(G.qual & LR_ALTKEY) clear_parent();
796                                 else {
797                         start_game();
798                                 }
799                                 break;
800                         case RKEY:
801                                 if(G.obedit==0 && (G.f & G_FACESELECT)) rotate_uv_tface();
802                                 else if(G.qual & LR_ALTKEY) clear_object('r');
803                                 else if(G.qual & LR_SHIFTKEY) selectrow_nurb();
804                                 else transform('r');
805                                 break;
806                         case SKEY:
807                                 if(G.qual & LR_ALTKEY) {
808                                         if(G.obedit) transform('N');    /* scale by vertex normal */
809                                         else clear_object('s');
810                                 }
811                                 else if(G.qual & LR_SHIFTKEY) snapmenu();
812                                 else if(G.qual & LR_CTRLKEY) {
813                                         if(G.obedit) transform('S');
814                                 }
815                                 else transform('s');
816                                 break;
817                         case TKEY:
818                                 if(G.qual & LR_CTRLKEY) {
819                                         if(G.obedit) {
820                                                 if(G.obedit->type==OB_MESH) {
821                                                         convert_to_triface(0);
822                                                         allqueue(REDRAWVIEW3D, 0);
823                                                         countall();
824                                                         makeDispList(G.obedit);
825                                                 }
826                                         }
827                                         else make_track();
828                                 }
829                                 else if(G.qual & LR_ALTKEY) {
830                                         if(G.obedit && G.obedit->type==OB_CURVE) clear_tilt();
831                                         else clear_track();
832                                 }
833                                 else {
834                                         if(G.obedit) transform('t');
835                                         else texspace_edit();
836                                 }
837                                 
838                                 break;
839                         case UKEY:
840                                 if(G.obedit) {
841                                         if(G.obedit->type==OB_MESH) remake_editMesh();
842                                         else if(G.obedit->type==OB_ARMATURE) remake_editArmature();
843                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) remake_editNurb();
844                                         else if(G.obedit->type==OB_LATTICE) remake_editLatt();
845                                 }
846                                 else if(G.f & G_FACESELECT) uv_autocalc_tface();
847                                 else if(G.f & G_WEIGHTPAINT) wpaint_undo();
848                                 else if(G.f & G_VERTEXPAINT) vpaint_undo();
849                                 else single_user();
850                                 
851                                 break;
852                         case VKEY:
853                                 if(G.qual==LR_SHIFTKEY) {
854                                         if (G.obedit && G.obedit->type==OB_MESH) {
855                                                 align_view_to_selected(v3d);
856                                         } else if (G.f & G_FACESELECT) {
857                                                 align_view_to_selected(v3d);
858                                         }
859                                 } else {
860                                         if(G.obedit) {
861                                                 if(G.obedit->type==OB_CURVE) {
862                                                         sethandlesNurb(2);
863                                                         makeDispList(G.obedit);
864                                                         allqueue(REDRAWVIEW3D, 0);
865                                                 }
866                                         }
867                                         else if(G.qual & LR_ALTKEY) image_aspect();
868                                         else set_vpaint();
869                                 }
870                                 break;
871                         case WKEY:
872                                 if(G.qual & LR_SHIFTKEY) {
873                                         transform('w');
874                                 }
875                                 else if(G.qual & LR_ALTKEY) {
876                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
877                                 }
878                                 else if(G.qual & LR_CTRLKEY) {
879                                         if(G.obedit) {
880                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
881                                                         switchdirectionNurb2();
882                                                 }
883                                         }
884                                 }
885                                 else special_editmenu();
886                                 
887                                 break;
888                         case XKEY:
889                         case DELKEY:
890                                 if(G.obedit) {
891                                         if(G.obedit->type==OB_MESH) delete_mesh();
892                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) delNurb();
893                                         else if(G.obedit->type==OB_MBALL) delete_mball();
894                                         else if (G.obedit->type==OB_ARMATURE) delete_armature();
895                                 }
896                                 else delete_obj(0);
897                                 break;
898                         case YKEY:
899                                 if(G.obedit) {
900                                         if(G.obedit->type==OB_MESH) split_mesh();
901                                 }
902                                 break;
903                         case ZKEY:
904                                 if(G.qual & LR_CTRLKEY) {
905                                         reshadeall_displist();
906                                         G.vd->drawtype= OB_SHADED;
907                                 }
908                                 else if(G.qual & LR_SHIFTKEY) {
909                                         if(G.vd->drawtype== OB_SHADED) G.vd->drawtype= OB_WIRE;
910                                         else G.vd->drawtype= OB_SHADED;
911                                 }
912                                 else if(G.qual & LR_ALTKEY) {
913                                         if(G.vd->drawtype== OB_TEXTURE) G.vd->drawtype= OB_SOLID;
914                                         else G.vd->drawtype= OB_TEXTURE;
915                                 }
916                                 else {
917                                         if(G.vd->drawtype==OB_SOLID || G.vd->drawtype==OB_SHADED) G.vd->drawtype= OB_WIRE;
918                                         else G.vd->drawtype= OB_SOLID;
919                                 }
920                                 
921                                 
922                                 scrarea_queue_headredraw(curarea);
923                                 scrarea_queue_winredraw(curarea);
924                                 break;
925                                 
926                         
927                         case HOMEKEY:
928                                 view3d_home(0);
929                                 break;
930                         case COMMAKEY:
931                                 G.vd->around= V3D_CENTRE;
932                                 scrarea_queue_headredraw(curarea);
933                                 break;
934                                 
935                         case PERIODKEY:
936                                 G.vd->around= V3D_CURSOR;
937                                 scrarea_queue_headredraw(curarea);
938                                 break;
939                         
940                         case PADSLASHKEY:
941                                 if(G.vd->localview) {
942                                         G.vd->localview= 0;
943                                         endlocalview(curarea);
944                                 }
945                                 else {
946                                         G.vd->localview= 1;
947                                         initlocalview();
948                                 }
949                                 scrarea_queue_headredraw(curarea);
950                                 break;
951                         case PADASTERKEY:       /* '*' */
952                                 ob= OBACT;
953                                 if(ob) {
954                                         obmat_to_viewmat(ob);
955                                         if(G.vd->persp==2) G.vd->persp= 1;
956                                         scrarea_queue_winredraw(curarea);
957                                 }
958                                 break;
959                         case PADPERIOD: /* '.' */
960                                 centreview();
961                                 break;
962                         
963                         case PAGEUPKEY:
964                                 if(G.qual & LR_CTRLKEY) movekey_obipo(1);
965                                 else nextkey_obipo(1);  /* in editipo.c */
966                                 break;
967
968                         case PAGEDOWNKEY:
969                                 if(G.qual & LR_CTRLKEY) movekey_obipo(-1);
970                                 else nextkey_obipo(-1);
971                                 break;
972                                 
973                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
974                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
975                         case PADMINUS: case PADPLUSKEY: case PADENTER:
976                                 persptoetsen(event);
977                                 doredraw= 1;
978                                 break;
979                         
980                         case ESCKEY:
981                                 if (G.vd->flag & V3D_DISPIMAGE) {
982                                         G.vd->flag &= ~V3D_DISPIMAGE;
983                                         doredraw= 1;
984                                 }
985                                 break;
986                         }
987                 }
988         }
989         
990         if(doredraw) {
991                 scrarea_queue_winredraw(curarea);
992                 scrarea_queue_headredraw(curarea);
993         }
994 }
995
996 void initview3d(ScrArea *sa)
997 {
998         View3D *vd;
999         
1000         vd= MEM_callocN(sizeof(View3D), "initview3d");
1001         BLI_addhead(&sa->spacedata, vd);        /* addhead! niet addtail */
1002
1003         vd->spacetype= SPACE_VIEW3D;
1004         vd->viewquat[0]= 1.0;
1005         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0;
1006         vd->persp= 1;
1007         vd->drawtype= OB_WIRE;
1008         vd->view= 7;
1009         vd->dist= 10.0;
1010         vd->lens= 35.0;
1011         vd->near= 0.01;
1012         vd->far= 500.0;
1013         vd->grid= 1.0;
1014         vd->gridlines= 16;
1015         vd->lay= vd->layact= 1;
1016         if(G.scene) {
1017                 vd->lay= vd->layact= G.scene->lay;
1018                 vd->camera= G.scene->camera;
1019         }
1020         vd->scenelock= 1;
1021 }
1022
1023
1024 /* ******************** SPACE: IPO ********************** */
1025
1026 void changeview2d()
1027 {
1028         if(G.v2d==0) return;
1029
1030         test_view2d(G.v2d, curarea->winx, curarea->winy);
1031         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1032 }
1033
1034 void winqreadipo(unsigned short event, short val, char ascii)
1035 {
1036         SpaceIpo *sipo= curarea->spacedata.first;
1037         View2D *v2d= &sipo->v2d;
1038         float dx, dy;
1039         int cfra, doredraw= 0;
1040         short mval[2];
1041         
1042         if(curarea->win==0) return;
1043
1044         if(val) {
1045                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1046
1047                 switch(event) {
1048                 case UI_BUT_EVENT:
1049                         if(val>0) do_ipowin_buts(val-1);
1050                         break;
1051                 case LEFTMOUSE:
1052                         if( in_ipo_buttons() ) {
1053                                 do_ipo_selectbuttons();
1054                                 doredraw= 1;
1055                         }                       
1056                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
1057                         else {
1058                                 do {
1059                                         getmouseco_areawin(mval);
1060                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
1061                                         
1062                                         cfra= (int)dx;
1063                                         if(cfra< 1) cfra= 1;
1064                                         
1065                                         if( cfra!=CFRA ) {
1066                                                 CFRA= cfra;
1067                                                 update_for_newframe();
1068                                                 force_draw_plus(SPACE_VIEW3D);
1069                                                 force_draw_plus(SPACE_ACTION);
1070                                                 force_draw_plus(SPACE_BUTS);    /* To make constraint sliders redraw */
1071                                         }
1072                                 
1073                                 } while(get_mbut()&L_MOUSE);
1074                         }
1075                         
1076                         break;
1077                 case MIDDLEMOUSE:
1078                         if(in_ipo_buttons()) {
1079                                 scroll_ipobuts();
1080                         }
1081                         else view2dmove();      /* in drawipo.c */
1082                         break;
1083                 case RIGHTMOUSE:
1084                         mouse_select_ipo();
1085                         allqueue (REDRAWACTION, 0);
1086                         allqueue(REDRAWNLA, 0);
1087                         break;
1088                 case PADPLUSKEY:
1089
1090                         dx= 0.1154*(v2d->cur.xmax-v2d->cur.xmin);
1091                         dy= 0.1154*(v2d->cur.ymax-v2d->cur.ymin);
1092                         if(val==SPACE_BUTS) {
1093                                 dx/=2.0; dy/= 2.0;
1094                         }
1095                         v2d->cur.xmin+= dx;
1096                         v2d->cur.xmax-= dx;
1097                         v2d->cur.ymin+= dy;
1098                         v2d->cur.ymax-= dy;
1099                         test_view2d(G.v2d, curarea->winx, curarea->winy);
1100                         doredraw= 1;
1101                         break;
1102                 case PADMINUS:
1103                         dx= 0.15*(v2d->cur.xmax-v2d->cur.xmin);
1104                         dy= 0.15*(v2d->cur.ymax-v2d->cur.ymin);
1105                         if(val==SPACE_BUTS) {
1106                                 dx/=2.0; dy/= 2.0;
1107                         }
1108                         v2d->cur.xmin-= dx;
1109                         v2d->cur.xmax+= dx;
1110                         v2d->cur.ymin-= dy;
1111                         v2d->cur.ymax+= dy;
1112                         test_view2d(G.v2d, curarea->winx, curarea->winy);
1113                         doredraw= 1;
1114                         break;
1115                 case PAGEUPKEY:
1116                         if(G.qual & LR_CTRLKEY) movekey_ipo(1);
1117                         else nextkey_ipo(1);
1118                         break;
1119                 case PAGEDOWNKEY:
1120                         if(G.qual & LR_CTRLKEY) movekey_ipo(-1);
1121                         else nextkey_ipo(-1);
1122                         break;
1123                 case HOMEKEY:
1124                         do_ipo_buttons(B_IPOHOME);
1125                         break;
1126                         
1127                 case AKEY:
1128                         if(in_ipo_buttons()) {
1129                                 swap_visible_editipo();
1130                         }
1131                         else swap_selectall_editipo();
1132                         allspace (REMAKEIPO, 0);
1133                         allqueue (REDRAWNLA, 0);
1134                         allqueue (REDRAWACTION, 0);
1135                         break;
1136                 case BKEY:
1137                         borderselect_ipo();
1138                         break;
1139                 case CKEY:
1140                         move_to_frame();
1141                         break;
1142                 case DKEY:
1143                         if(G.qual & LR_SHIFTKEY) add_duplicate_editipo();
1144                         break;
1145                 case GKEY:
1146                         transform_ipo('g');
1147                         break;
1148                 case HKEY:
1149                         if(G.qual & LR_SHIFTKEY) sethandles_ipo(HD_AUTO);
1150                         else sethandles_ipo(HD_ALIGN);
1151                         break;
1152                 case JKEY:
1153                         join_ipo();
1154                         break;
1155                 case KKEY:
1156                         if(G.sipo->showkey) {
1157                                 G.sipo->showkey= 0;
1158                                 swap_selectall_editipo();       /* sel all */
1159                         }
1160                         else G.sipo->showkey= 1;
1161                         free_ipokey(&G.sipo->ipokey);
1162                         if(G.sipo->ipo) G.sipo->ipo->showkey= G.sipo->showkey;
1163
1164                         scrarea_queue_headredraw(curarea);
1165                         allqueue(REDRAWVIEW3D, 0);
1166                         doredraw= 1;
1167                         break;
1168                 case RKEY:
1169                         ipo_record();
1170                         break;
1171                 case SKEY:
1172                         if(G.qual & LR_SHIFTKEY) ipo_snapmenu();
1173                         else transform_ipo('s');
1174                         break;
1175                 case TKEY:
1176                         set_ipotype();
1177                         break;
1178                 case VKEY:
1179                         sethandles_ipo(HD_VECT);
1180                         break;
1181                 case XKEY:
1182                 case DELKEY:
1183                         if(G.qual & LR_SHIFTKEY) delete_key();
1184                         else del_ipo();
1185                         break;
1186                 }
1187         }
1188
1189         if(doredraw) scrarea_queue_winredraw(curarea);
1190 }
1191
1192 void initipo(ScrArea *sa)
1193 {
1194         SpaceIpo *sipo;
1195         
1196         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
1197         BLI_addhead(&sa->spacedata, sipo);
1198
1199         sipo->spacetype= SPACE_IPO;
1200         /* sipo space loopt van (0,-?) tot (??,?) */
1201         sipo->v2d.tot.xmin= 0.0;
1202         sipo->v2d.tot.ymin= -10.0;
1203         sipo->v2d.tot.xmax= G.scene->r.efra;
1204         sipo->v2d.tot.ymax= 10.0;
1205
1206         sipo->v2d.cur= sipo->v2d.tot;
1207
1208         sipo->v2d.min[0]= 0.01;
1209         sipo->v2d.min[1]= 0.01;
1210
1211         sipo->v2d.max[0]= 15000.0;
1212         sipo->v2d.max[1]= 10000.0;
1213         
1214         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
1215         sipo->v2d.keeptot= 0;
1216
1217         sipo->blocktype= ID_OB;
1218 }
1219
1220 /* ******************** SPACE: INFO ********************** */
1221
1222 void space_mipmap_button_function(int event) {
1223         set_mipmap(!(U.gameflags & USERDEF_DISABLE_MIPMAP));
1224
1225         allqueue(REDRAWVIEW3D, 0);
1226 }
1227
1228 void space_sound_button_function(int event)
1229 {
1230         int a;
1231         SYS_SystemHandle syshandle;
1232
1233         if ((syshandle = SYS_GetSystem()))
1234         {
1235                 a = (U.gameflags & USERDEF_DISABLE_SOUND);
1236                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
1237         }
1238 }
1239
1240 void drawinfospace(void)
1241 {
1242         uiBlock *block;
1243         float fac;
1244         int dx;
1245         char naam[32];
1246
1247         if(curarea->win==0) return;
1248
1249         glClearColor(0.5, 0.5, 0.5, 0.0); 
1250         glClear(GL_COLOR_BUFFER_BIT);
1251
1252         fac= ((float)curarea->winx)/1280.0;
1253         myortho2(0.0, 1280.0, 0.0, curarea->winy/fac);
1254         
1255         sprintf(naam, "infowin %d", curarea->win);
1256         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSSX, UI_HELV, curarea->win);
1257         
1258         uiBlockSetCol(block, BUTBLUE);
1259         uiDefButS(block, TOG|BIT|0, B_RESETAUTOSAVE, "Auto Temp Save", 45,32,126,20, &(U.flag), 0, 0, 0, 0, "Enables/Disables the automatic temp. file saving");
1260         uiBlockSetCol(block, BUTGREY);
1261         uiDefBut(block, TEX, 0, "Dir:", 45,10,127,20, U.tempdir, 1.0, 63.0, 0, 0, "The directory for temp. files");
1262         uiDefButI(block, NUM, B_RESETAUTOSAVE, "Time:", 174,32,91,20, &(U.savetime), 1.0, 60.0, 0, 0, "The time in minutes to wait between temp. saves");
1263         uiBlockSetCol(block, BUTSALMON);
1264         uiDefBut(block, BUT, B_LOADTEMP, "Load Temp", 174,10,90,20, 0, 0, 0, 0, 0, "Loads the most recently saved temp file");
1265
1266         uiBlockSetCol(block, BUTGREY);
1267         uiDefButS(block, NUM, 0, "Versions:", 281,10,86,42, &U.versions, 0.0, 32.0, 0, 0, "The number of old versions to maintain when saving");
1268
1269         uiBlockSetCol(block, BUTYELLOW);
1270         uiDefButI(block, TOG|BIT|USERDEF_VERTEX_ARRAYS_BIT, 0, "Vertex arrays",
1271                          389,54,86,20, &(U.gameflags), 0, 0, 0, 0,
1272                          "Toggle between vertex arrays on (less reliable) and off (more reliable)");
1273         uiDefButI(block, TOG|BIT|USERDEF_DISABLE_SOUND_BIT, B_SOUNDTOGGLE, "No sound",
1274                          478,54,86,20, &(U.gameflags), 0, 0, 0, 0,
1275                          "Toggle between sound on and sound off");
1276         
1277         uiDefButI(block, TOG|BIT|USERDEF_DISABLE_MIPMAP_BIT, B_MIPMAPCHANGED, "No Mipmaps",
1278                                    569,54,78,20, &(U.gameflags), 0, 0, 0, 0,
1279                                    "Toggle between Mipmap textures on (beautiful) and off (fast)");
1280         
1281         uiBlockSetCol(block, BUTGREEN);
1282         uiDefButS(block, TOG|BIT|4, 0, "Scene Global",
1283                          389,32,86,20, &(U.flag), 0, 0, 0, 0,
1284                          "Forces the current Scene to be displayed in all Screens");
1285         uiDefButS(block, TOG|BIT|5, 0, "TrackBall",
1286                          389,10,86,20, &(U.flag), 0, 0, 0, 0,
1287                          "Switches between trackball and turntable view rotation methods (MiddleMouse)");
1288         uiDefButS(block, TOG|BIT|12, 0, "2-Mouse",
1289                          478,10,86,20, &(U.flag), 0, 0, 0, 0,
1290                          "Maps ALT+LeftMouse to MiddleMouse button");
1291         uiDefButS(block, TOG|BIT|8, 0, "Mat on Obj",
1292                          569,9,78,20, &(U.flag), 0, 0, 0, 0,
1293                          "Sets whether Material data is linked to Obj or ObjData");
1294         uiDefButS(block, TOG|BIT|9, B_U_CAPSLOCK, "NoCapsLock",
1295                          478,32,86,20, &(U.flag), 0, 0, 0, 0,
1296                          "Deactives the CapsLock button (only applies to text input)");
1297         uiDefButS(block, TOG|BIT|10, 0, "Viewmove",
1298                          569,32,78,20, &(U.flag), 0, 0, 0, 0,
1299                          "Sets the default action for the middle mouse button");
1300
1301         uiDefButS(block, TOG|BIT|13, 0, "noNumpad",
1302                          653,10,76,20, &(U.flag), 0, 0, 0, 0, 
1303                          "For laptops: keys 1 to 0 become numpad keys");
1304         uiDefButS(block, TOG|BIT|11, 0, "ToolTips",
1305                          653,32,76,20, &(U.flag), 0, 0, 0, 0,
1306                          "Enables/Disables tooltips");
1307         
1308 //      uiDefButS(block, ICONTOG|BIT|14, 0, ICON(),     733,10,50,42, &(U.flag), 0, 0, 0, 0, "Automatic keyframe insertion");
1309         uiDefButS(block, TOG|BIT|14, 0, "KeyAC",        733,32,50,20, &(U.flag), 0, 0, 0, 0, "Automatic keyframe insertion for actions");
1310         uiDefButS(block, TOG|BIT|15, 0, "KeyOB",        733,10,50,20, &(U.flag), 0, 0, 0, 0, "Automatic keyframe insertion for objects");
1311
1312         uiDefButS(block, TOG|BIT|1, 0, "Grab Grid",     788,32,106,20, &(U.flag), 0, 0, 0, 0, "Changes default step mode for grabbing");
1313         uiDefButS(block, TOG|BIT|2, 0, "Rot",           842,10,52,20, &(U.flag), 0, 0, 0, 0, "Changes default step mode for rotation");
1314         uiDefButS(block, TOG|BIT|3, 0, "Size",          788,10,52,20, &(U.flag), 0, 0, 0, 0, "Changes default step mode for scaling");
1315
1316         uiDefButS(block, TOG|BIT|0, 0, "Dupli Mesh",    902,32,90,20, &(U.dupflag), 0, 0, 0, 0, "Causes Mesh data to be duplicated with Shift+D");
1317         uiDefButS(block, TOG|BIT|9, 0, "Armature",      902,10,90,20, &(U.dupflag), 0, 0, 0, 0, "Causes Armature data to be duplicated with Shift+D");
1318
1319         uiDefButS(block, TOG|BIT|1, 0, "Curve",         995,32,50,20, &(U.dupflag), 0, 0, 0, 0, "Causes Curve data to be duplicated with Shift+D");
1320         uiDefButS(block, TOG|BIT|2, 0, "Surf",          995,10,50,20, &(U.dupflag), 0, 0, 0, 0, "Causes Surface data to be duplicated with Shift+D");
1321         uiDefButS(block, TOG|BIT|3, 0, "Text",          1048,32,50,20, &(U.dupflag), 0, 0, 0, 0, "Causes Text data to be duplicated with Shift+D");
1322         uiDefButS(block, TOG|BIT|4, 0, "MBall",         1048,10,50,20, &(U.dupflag), 0, 0, 0, 0, "Causes Metaball data to be duplicated with Shift+D");
1323         uiDefButS(block, TOG|BIT|5, 0, "Lamp",          1101,32,50,20, &(U.dupflag), 0, 0, 0, 0, "Causes Lamp data to be duplicated with Shift+D");
1324         uiDefButS(block, TOG|BIT|6, 0, "Ipo",                   1101,10,50,20, &(U.dupflag), 0, 0, 0, 0, "Causes Ipo data to be duplicated with Shift+D");
1325         uiDefButS(block, TOG|BIT|7, 0, "Material",      1153,32,70,20, &(U.dupflag), 0, 0, 0, 0, "Causes Material data to be duplicated with Shift+D");
1326         uiDefButS(block, TOG|BIT|8, 0, "Texture",               1153,10,70,20, &(U.dupflag), 0, 0, 0, 0, "Causes Texture data to be duplicated with Shift+D");
1327
1328         
1329         
1330         uiBlockSetCol(block, BUTGREY);
1331         dx= (1280-90)/6;
1332
1333
1334 #define _XPOS_ 45
1335 #define _YPOS_ 80
1336 #define _BUTH_ 20
1337 #define _RULESPACE_ 2
1338         uiDefBut(block, TEX, 0, "Python:",
1339                          _XPOS_,_YPOS_-_BUTH_-_RULESPACE_,(short)dx,_BUTH_, U.pythondir, 1.0, 63.0, 0, 0,
1340                          "The default directory for Python scripts");
1341         uiDefBut(block, TEX, 0, "Fonts:",
1342                          _XPOS_,_YPOS_,(short)dx,_BUTH_, U.fontdir, 1.0, 63.0, 0, 0,
1343                          "The default directory to search when loading fonts");
1344         uiDefBut(block, TEX, 0, "Render:",
1345                          (short)(_XPOS_+dx),_YPOS_,(short)dx,_BUTH_, U.renderdir, 1.0, 63.0, 0, 0,
1346                          "The default directory to choose for rendering");
1347         uiDefBut(block, TEX, 0, "Textures:",
1348                          (short)(_XPOS_+2*dx),_YPOS_,(short)dx,_BUTH_, U.textudir, 1.0, 63.0, 0, 0,
1349                          "The default directory to search when loading textures");
1350         uiDefBut(block, TEX, 0, "TexPlugin:",
1351                          (short)(_XPOS_+3*dx),_YPOS_,(short)dx,_BUTH_, U.plugtexdir, 1.0, 63.0, 0, 0,
1352                          "The default directory to search when loading texture plugins");
1353         uiDefBut(block, TEX, 0, "SeqPlugin:",
1354                          (short)(_XPOS_+4*dx),_YPOS_,(short)dx,_BUTH_, U.plugseqdir, 1.0, 63.0, 0, 0,
1355                          "The default directory to search when loading sequence plugins");
1356         uiDefBut(block, TEX, 0, "Sounds:",
1357                          (short)(_XPOS_+5*dx),_YPOS_,(short)dx,_BUTH_, U.sounddir, 1.0, 63.0, 0, 0,
1358                          "The default directory to search when loading sounds");
1359 #undef _XPOS_
1360 #undef _YPOS_
1361 #undef _BUTH_
1362 #undef _RULESPACE_
1363         uiDrawBlock(block);
1364 }
1365
1366 void winqreadinfospace(unsigned short event, short val, char ascii)
1367 {
1368         if(val) {
1369                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1370
1371                 switch(event) {
1372                 case UI_BUT_EVENT:
1373                         do_global_buttons(val);
1374                         
1375                         break;  
1376                 }
1377         }
1378 }
1379
1380 void init_infospace(ScrArea *sa)
1381 {
1382         SpaceInfo *sinfo;
1383         
1384         sinfo= MEM_callocN(sizeof(SpaceInfo), "initinfo");
1385         BLI_addhead(&sa->spacedata, sinfo);
1386 }
1387
1388 /* ******************** SPACE: BUTS ********************** */
1389
1390 extern void drawbutspace(void); /* buttons.c */
1391
1392 void changebutspace(void)
1393 {
1394         if(G.v2d==0) return;
1395         
1396         test_view2d(G.v2d, curarea->winx, curarea->winy);
1397         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1398 }
1399
1400 void winqreadbutspace(unsigned short event, short val, char ascii)
1401 {
1402         SpaceButs *sbuts= curarea->spacedata.first;
1403         ScrArea *sa, *sa3d;
1404         int doredraw= 0;
1405
1406         if(val) {
1407                 
1408                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1409
1410                 switch(event) {
1411                 case UI_BUT_EVENT:
1412                         do_blenderbuttons(val);
1413                         break;
1414                         
1415                 case MIDDLEMOUSE:
1416                         view2dmove();   /* in drawipo.c */
1417                         break;
1418                 case PADPLUSKEY:
1419                 case PADMINUS:
1420                         val= SPACE_BUTS;
1421                         winqreadipo(event, val, 0);
1422                         break;
1423                 case RENDERPREVIEW:
1424                         BIF_previewrender(sbuts);
1425                         break;
1426                 
1427                 case HOMEKEY:
1428                         do_buts_buttons(B_BUTSHOME);
1429                         break;
1430
1431
1432                 /* if only 1 view, also de persp, excluding arrowkeys */
1433                 case PAD0: case PAD1: case PAD3:
1434                 case PAD5: case PAD7: case PAD9:
1435                 case PADENTER: case ZKEY: case PKEY:
1436                         sa3d= 0;
1437                         sa= G.curscreen->areabase.first;
1438                         while(sa) {
1439                                 if(sa->spacetype==SPACE_VIEW3D) {
1440                                         if(sa3d) return;
1441                                         sa3d= sa;
1442                                 }
1443                                 sa= sa->next;
1444                         }
1445                         if(sa3d) {
1446                                 sa= curarea;
1447                                 areawinset(sa3d->win);
1448                                 
1449                                 if(event==PKEY) start_game();
1450                                 else if(event==ZKEY) winqread3d(event, val, 0);
1451                                 else persptoetsen(event);
1452                                 
1453                                 scrarea_queue_winredraw(sa3d);
1454                                 scrarea_queue_headredraw(sa3d);
1455                                 areawinset(sa->win);
1456                         }
1457                 }
1458         }
1459
1460         if(doredraw) scrarea_queue_winredraw(curarea);
1461 }
1462
1463 void set_rects_butspace(SpaceButs *buts)
1464 {
1465         /* buts space loopt van (0,0) tot (1280, 228) */
1466
1467         buts->v2d.tot.xmin= 0.0;
1468         buts->v2d.tot.ymin= 0.0;
1469         buts->v2d.tot.xmax= 1279.0;
1470         buts->v2d.tot.ymax= 228.0;
1471         
1472         buts->v2d.min[0]= 256.0;
1473         buts->v2d.min[1]= 42.0;
1474
1475         buts->v2d.max[0]= 1600.0;
1476         buts->v2d.max[1]= 450.0;
1477         
1478         buts->v2d.minzoom= 0.5;
1479         buts->v2d.maxzoom= 1.41;
1480         
1481         buts->v2d.scroll= 0;
1482         buts->v2d.keepaspect= 1;
1483         buts->v2d.keepzoom= 1;
1484         buts->v2d.keeptot= 1;
1485         
1486 }
1487
1488 void test_butspace(void)
1489 {
1490         ScrArea *area= curarea;
1491         int blocksmin= uiBlocksGetYMin(&area->uiblocks)-10.0;
1492         
1493         G.buts->v2d.tot.ymin= MIN2(0.0, blocksmin-10.0);
1494 }
1495
1496 void init_butspace(ScrArea *sa)
1497 {
1498         SpaceButs *buts;
1499         
1500         buts= MEM_callocN(sizeof(SpaceButs), "initbuts");
1501         BLI_addhead(&sa->spacedata, buts);
1502
1503         buts->spacetype= SPACE_BUTS;
1504         buts->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
1505
1506         /* set_rects doet alleen defaults, zodat na het filezen de cur niet verandert */
1507         set_rects_butspace(buts);
1508         buts->v2d.cur= buts->v2d.tot;
1509 }
1510
1511 void extern_set_butspace(int fkey)
1512 {
1513         ScrArea *sa;
1514         SpaceButs *sbuts;
1515         
1516         /* als een ftoets ingedrukt: de dichtsbijzijnde buttonwindow wordt gezet */
1517         if(curarea->spacetype==SPACE_BUTS) sa= curarea;
1518         else {
1519                 /* area vinden */
1520                 sa= G.curscreen->areabase.first;
1521                 while(sa) {
1522                         if(sa->spacetype==SPACE_BUTS) break;
1523                         sa= sa->next;
1524                 }
1525         }
1526         
1527         if(sa==0) return;
1528         
1529         if(sa!=curarea) areawinset(sa->win);
1530         
1531         sbuts= sa->spacedata.first;
1532         
1533         if(fkey==F4KEY) sbuts->mainb= BUTS_LAMP;
1534         else if(fkey==F5KEY) sbuts->mainb= BUTS_MAT;
1535         else if(fkey==F6KEY) sbuts->mainb= BUTS_TEX;
1536         else if(fkey==F7KEY) sbuts->mainb= BUTS_ANIM;
1537         else if(fkey==F8KEY) sbuts->mainb= BUTS_GAME;
1538         else if(fkey==F9KEY) sbuts->mainb= BUTS_EDIT;
1539         else if(fkey==F10KEY) sbuts->mainb= BUTS_RENDER;
1540
1541         scrarea_queue_headredraw(sa);
1542         scrarea_queue_winredraw(sa);
1543         BIF_preview_changed(sbuts);
1544 }
1545
1546 /* ******************** SPACE: SEQUENCE ********************** */
1547
1548 /*  extern void drawseqspace(); BIF_drawseq.h */
1549
1550 void winqreadsequence(unsigned short event, short val, char ascii)
1551 {
1552         SpaceSeq *sseq= curarea->spacedata.first;
1553         View2D *v2d= &sseq->v2d;
1554         extern Sequence *last_seq;
1555         float dx, dy;
1556         int doredraw= 0, cfra, first;
1557         short mval[2];
1558         
1559         if(curarea->win==0) return;
1560
1561         if(val) {
1562                 
1563                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1564
1565                 switch(event) {
1566                 case LEFTMOUSE:
1567                         if(sseq->mainb || view2dmove()==0) {
1568                                 
1569                                 first= 1;               
1570                                 set_special_seq_update(1);
1571
1572                                 do {
1573                                         getmouseco_areawin(mval);
1574                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
1575                                         
1576                                         cfra= (int)dx;
1577                                         if(cfra< 1) cfra= 1;
1578                                         /* else if(cfra> EFRA) cfra= EFRA; */
1579                                         
1580                                         if( cfra!=CFRA || first ) {
1581                                                 first= 0;
1582                                 
1583                                                 CFRA= cfra;
1584                                                 force_draw();
1585                                         }
1586                                 
1587                                 } while(get_mbut()&L_MOUSE);
1588                                 
1589                                 set_special_seq_update(0);
1590                                 
1591                                 update_for_newframe();
1592                         }
1593                         break;
1594                 case MIDDLEMOUSE:
1595                         if(sseq->mainb) break;
1596                         view2dmove();   /* in drawipo.c */
1597                         break;
1598                 case RIGHTMOUSE:
1599                         if(sseq->mainb) break;
1600                         mouse_select_seq();
1601                         break;
1602                 case PADPLUSKEY:
1603                         if(sseq->mainb) {
1604                                 sseq->zoom++;
1605                                 if(sseq->zoom>8) sseq->zoom= 8;
1606                         }
1607                         else {
1608                                 if(G.qual) {
1609                                         if(G.qual & LR_SHIFTKEY) insert_gap(25, CFRA);
1610                                         else if(G.qual & LR_ALTKEY) insert_gap(250, CFRA);
1611                                         allqueue(REDRAWSEQ, 0);
1612                                 }
1613                                 else {
1614                                         dx= 0.1154*(v2d->cur.xmax-v2d->cur.xmin);
1615                                         v2d->cur.xmin+= dx;
1616                                         v2d->cur.xmax-= dx;
1617                                         test_view2d(G.v2d, curarea->winx, curarea->winy);
1618                                 }
1619                         }
1620                         doredraw= 1;
1621                         break;
1622                 case PADMINUS:
1623                         if(sseq->mainb) {
1624                                 sseq->zoom--;
1625                                 if(sseq->zoom<1) sseq->zoom= 1;
1626                         }
1627                         else {
1628                                 if(G.qual) {
1629                                         if(G.qual & LR_SHIFTKEY) no_gaps();
1630                                 }
1631                                 else {
1632                                         dx= 0.15*(v2d->cur.xmax-v2d->cur.xmin);
1633                                         v2d->cur.xmin-= dx;
1634                                         v2d->cur.xmax+= dx;
1635                                         test_view2d(G.v2d, curarea->winx, curarea->winy);
1636                                 }
1637                         }
1638                         doredraw= 1;
1639                         break;
1640                 case HOMEKEY:
1641                         do_seq_buttons(B_SEQHOME);
1642                         break;
1643                 case PADPERIOD: 
1644                         if(last_seq) {
1645                                 CFRA= last_seq->startdisp;
1646                                 v2d->cur.xmin= last_seq->startdisp- (last_seq->len/20);
1647                                 v2d->cur.xmax= last_seq->enddisp+ (last_seq->len/20);
1648                                 update_for_newframe();
1649                         }
1650                         break;
1651                         
1652                 case AKEY:
1653                         if(sseq->mainb) break;
1654                         if(G.qual & LR_SHIFTKEY) {
1655                                 add_sequence(0);
1656                         }
1657                         else swap_select_seq();
1658                         break;
1659                 case BKEY:
1660                         if(sseq->mainb) break;
1661                         borderselect_seq();
1662                         break;
1663                 case CKEY:
1664                         if(last_seq && (last_seq->flag & (SEQ_LEFTSEL+SEQ_RIGHTSEL))) {
1665                                 if(last_seq->flag & SEQ_LEFTSEL) CFRA= last_seq->startdisp;
1666                                 else CFRA= last_seq->enddisp-1;
1667                                 
1668                                 dx= CFRA-(v2d->cur.xmax+v2d->cur.xmin)/2;
1669                                 v2d->cur.xmax+= dx;
1670                                 v2d->cur.xmin+= dx;
1671                                 update_for_newframe();
1672                         }
1673                         else change_sequence();
1674                         break;
1675                 case DKEY:
1676                         if(sseq->mainb) break;
1677                         if(G.qual & LR_SHIFTKEY) add_duplicate_seq();
1678                         break;
1679                 case EKEY:
1680                         break;
1681                 case FKEY:
1682                         set_filter_seq();
1683                         break;
1684                 case GKEY:
1685                         if(sseq->mainb) break;
1686                         transform_seq('g');
1687                         break;
1688                 case MKEY:
1689                         if(G.qual & LR_ALTKEY) un_meta();
1690                         else make_meta();
1691                         break;
1692                 case SKEY:
1693                         if(G.qual & LR_SHIFTKEY) seq_snapmenu();
1694                         break;
1695                 case TKEY:
1696                         touch_seq_files();
1697                         break;
1698                 case XKEY:
1699                 case DELKEY:
1700                         if(sseq->mainb) break;
1701                         del_seq();
1702                         break;
1703                 }
1704         }
1705
1706         if(doredraw) scrarea_queue_winredraw(curarea);
1707 }
1708
1709
1710 void init_seqspace(ScrArea *sa)
1711 {
1712         SpaceSeq *sseq;
1713         
1714         sseq= MEM_callocN(sizeof(SpaceSeq), "initseqspace");
1715         BLI_addhead(&sa->spacedata, sseq);
1716
1717         sseq->spacetype= SPACE_SEQ;
1718         sseq->zoom= 1;
1719         
1720         /* seq space loopt van (0,8) tot (250, 0) */
1721
1722         sseq->v2d.tot.xmin= 0.0;
1723         sseq->v2d.tot.ymin= 0.0;
1724         sseq->v2d.tot.xmax= 250.0;
1725         sseq->v2d.tot.ymax= 8.0;
1726         
1727         sseq->v2d.cur= sseq->v2d.tot;
1728
1729         sseq->v2d.min[0]= 10.0;
1730         sseq->v2d.min[1]= 4.0;
1731
1732         sseq->v2d.max[0]= 32000.0;
1733         sseq->v2d.max[1]= MAXSEQ;
1734         
1735         sseq->v2d.minzoom= 0.1;
1736         sseq->v2d.maxzoom= 10.0;
1737         
1738         sseq->v2d.scroll= L_SCROLL+B_SCROLL;
1739         sseq->v2d.keepaspect= 0;
1740         sseq->v2d.keepzoom= 0;
1741         sseq->v2d.keeptot= 0;
1742 }
1743
1744 /* ******************** SPACE: ACTION ********************** */
1745 extern void drawactionspace(void);
1746 extern void winqreadactionspace(unsigned short, short, char ascii);
1747
1748 void init_actionspace(ScrArea *sa)
1749 {
1750         SpaceAction *saction;
1751         
1752         saction= MEM_callocN(sizeof(SpaceAction), "initactionspace");
1753         BLI_addhead(&sa->spacedata, saction);
1754
1755         saction->spacetype= SPACE_ACTION;
1756
1757         saction->v2d.tot.xmin= 1.0;
1758         saction->v2d.tot.ymin=  0.0;
1759         saction->v2d.tot.xmax= 1000.0;
1760         saction->v2d.tot.ymax= 1000.0;
1761         
1762         saction->v2d.cur.xmin= -5.0;
1763         saction->v2d.cur.ymin= 0.0;
1764         saction->v2d.cur.xmax= 65.0;
1765         saction->v2d.cur.ymax= 1000.0;
1766
1767
1768         saction->v2d.min[0]= 0.0;
1769         saction->v2d.min[1]= 0.0;
1770
1771         saction->v2d.max[0]= 1000.0;
1772         saction->v2d.max[1]= 1000.0;
1773         
1774         saction->v2d.minzoom= 0.1;
1775         saction->v2d.maxzoom= 10;
1776
1777         saction->v2d.scroll= R_SCROLL+B_SCROLL;
1778         saction->v2d.keepaspect= 0;
1779         saction->v2d.keepzoom= V2D_LOCKZOOM_Y;
1780         saction->v2d.keeptot= 0;
1781         
1782 }
1783
1784 void free_actionspace(SpaceAction *saction)
1785 {
1786         /* don't free saction itself */
1787         
1788         /* __PINFAKE */
1789 /*      if (saction->flag & SACTION_PIN)
1790                 if (saction->action)
1791                         saction->action->id.us --;
1792
1793 */      /* end PINFAKE */
1794 }
1795
1796
1797 /* ******************** SPACE: FILE ********************** */
1798
1799 void init_filespace(ScrArea *sa)
1800 {
1801         SpaceFile *sfile;
1802         
1803         sfile= MEM_callocN(sizeof(SpaceFile), "initfilespace");
1804         BLI_addhead(&sa->spacedata, sfile);
1805
1806         sfile->dir[0]= '/';
1807         sfile->type= FILE_UNIX;
1808
1809         sfile->spacetype= SPACE_FILE;
1810 }
1811
1812 void init_textspace(ScrArea *sa)
1813 {
1814         SpaceText *st;
1815         
1816         st= MEM_callocN(sizeof(SpaceText), "inittextspace");
1817         BLI_addhead(&sa->spacedata, st);
1818
1819         st->spacetype= SPACE_TEXT;      
1820         
1821         st->text= NULL;
1822         st->flags= 0;
1823         
1824         st->font_id= 5;
1825         st->lheight= 12;
1826         
1827         st->top= 0;
1828 }
1829
1830 void init_imaselspace(ScrArea *sa)
1831 {
1832         SpaceImaSel *simasel;
1833         
1834         simasel= MEM_callocN(sizeof(SpaceImaSel), "initimaselspace");
1835         BLI_addhead(&sa->spacedata, simasel);
1836
1837         simasel->spacetype= SPACE_IMASEL;
1838         
1839         simasel->mode = 7;
1840         strcpy (simasel->dir,  U.textudir);     /* TON */
1841         strcpy (simasel->file, "");
1842         strcpy(simasel->fole, simasel->file);
1843         strcpy(simasel->dor,  simasel->dir);
1844
1845         simasel->first_sel_ima  =  0;
1846         simasel->hilite_ima         =  0;
1847         simasel->firstdir               =  0;
1848         simasel->firstfile              =  0;
1849         simasel->cmap           =  0;
1850         simasel->returnfunc     =  0;
1851         
1852         simasel->title[0]       =  0;
1853         
1854         clear_ima_dir(simasel);
1855         
1856         // simasel->cmap= IMB_loadiffmem((int*)datatoc_cmap_tga, IB_rect|IB_cmap);
1857         simasel->cmap= IMB_ibImageFromMemory((int *)datatoc_cmap_tga, datatoc_cmap_tga_size, IB_rect|IB_cmap);
1858         if (!simasel->cmap) {
1859                 error("in console");
1860                 printf("Image select cmap file not found \n");
1861         }
1862 }
1863
1864 /* ******************** SPACE: SOUND ********************** */
1865
1866 extern void drawsoundspace(void);
1867 extern void winqreadsoundspace(unsigned short, short, char ascii);
1868
1869 void init_soundspace(ScrArea *sa)
1870 {
1871         SpaceSound *ssound;
1872         
1873         ssound= MEM_callocN(sizeof(SpaceSound), "initsoundspace");
1874         BLI_addhead(&sa->spacedata, ssound);
1875
1876         ssound->spacetype= SPACE_SOUND;
1877         
1878         /* sound space loopt van (0,8) tot (250, 0) */
1879
1880         ssound->v2d.tot.xmin= -4.0;
1881         ssound->v2d.tot.ymin= -4.0;
1882         ssound->v2d.tot.xmax= 250.0;
1883         ssound->v2d.tot.ymax= 255.0;
1884         
1885         ssound->v2d.cur.xmin= -4.0;
1886         ssound->v2d.cur.ymin= -4.0;
1887         ssound->v2d.cur.xmax= 50.0;
1888         ssound->v2d.cur.ymax= 255.0;
1889
1890         ssound->v2d.min[0]= 1.0;
1891         ssound->v2d.min[1]= 259.0;
1892
1893         ssound->v2d.max[0]= 32000.0;
1894         ssound->v2d.max[1]= 259;
1895         
1896         ssound->v2d.minzoom= 0.1;
1897         ssound->v2d.maxzoom= 10.0;
1898         
1899         ssound->v2d.scroll= B_SCROLL;
1900         ssound->v2d.keepaspect= 0;
1901         ssound->v2d.keepzoom= 0;
1902         ssound->v2d.keeptot= 0;
1903         
1904 }
1905
1906 void free_soundspace(SpaceSound *ssound)
1907 {
1908         /* don't free ssound itself */
1909         
1910         
1911 }
1912
1913 /* ******************** SPACE: IMAGE ********************** */
1914
1915 /*  extern void drawimagespace(); BIF_drawimage.h */
1916
1917 void winqreadimagespace(unsigned short event, short val, char ascii)
1918 {
1919         SpaceImage *sima= curarea->spacedata.first;
1920         View2D *v2d= &sima->v2d;
1921         int width, height;
1922 #ifdef NAN_TPT
1923         IMG_BrushPtr brush;
1924         IMG_CanvasPtr canvas;
1925         int rowBytes;
1926         short xy_prev[2], xy_curr[2];
1927         float uv_prev[2], uv_curr[2];
1928         extern VPaint Gvp;
1929 #endif /* NAN_TPT */    
1930         if(val==0) return;
1931
1932         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1933         
1934         if (sima->flag & SI_DRAWTOOL) {
1935 #ifdef NAN_TPT
1936                 /* Draw tool is active */
1937                 switch(event) {
1938                         case LEFTMOUSE:
1939                                 /* Paranoia checks */
1940                                 if (!sima) break;
1941                                 if (!sima->image) break;
1942                                 if (!sima->image->ibuf) break;
1943                                 if (sima->image->packedfile) {
1944                                         error("Painting in packed images not supported");
1945                                         break;
1946                                 }
1947                         
1948                                 brush = IMG_BrushCreate(Gvp.size, Gvp.size, Gvp.r, Gvp.g, Gvp.b, Gvp.a);
1949                                 /* MAART: skipx is not set most of the times. Make a guess. */
1950                                 rowBytes = sima->image->ibuf->skipx ? sima->image->ibuf->skipx : sima->image->ibuf->x * 4;
1951                                 canvas = IMG_CanvasCreateFromPtr(sima->image->ibuf->rect, sima->image->ibuf->x, sima->image->ibuf->y, rowBytes);
1952
1953                                 getmouseco_areawin(xy_prev);
1954                                 while (get_mbut() & L_MOUSE) {
1955                                         getmouseco_areawin(xy_curr);
1956                                         /* Check if mouse position changed */
1957                                         if ((xy_prev[0] != xy_curr[0]) || (xy_prev[1] != xy_curr[1])) {
1958                                                 /* Convert mouse coordinates to u,v and draw */
1959                                                 areamouseco_to_ipoco(v2d, xy_prev, &uv_prev[0], &uv_prev[1]);
1960                                                 areamouseco_to_ipoco(v2d, xy_curr, &uv_curr[0], &uv_curr[1]);
1961                                                 IMG_CanvasDrawLineUV(canvas, brush, uv_prev[0], uv_prev[1], uv_curr[0], uv_curr[1]);
1962                                                 if (G.sima->lock) {
1963                                                         /* Make OpenGL aware of a changed texture */
1964                                                         free_realtime_image(sima->image);
1965                                                         /* Redraw this view and the 3D view */
1966                                                         force_draw_plus(SPACE_VIEW3D);
1967                                                 }
1968                                                 else {
1969                                                         /* Redraw only this view */
1970                                                         force_draw();
1971                                                 }
1972                                                 xy_prev[0] = xy_curr[0];
1973                                                 xy_prev[1] = xy_curr[1];
1974                                         }
1975                                 }
1976                                 /* Set the dirty bit in the image so that it is clear that it has been modified. */
1977                                 sima->image->ibuf->userflags |= IB_BITMAPDIRTY;
1978                                 if (!G.sima->lock) {
1979                                         /* Make OpenGL aware of a changed texture */
1980                                         free_realtime_image(sima->image);
1981                                         /* Redraw this view and the 3D view */
1982                                         force_draw_plus(SPACE_VIEW3D);
1983                                 }
1984                                 IMG_BrushDispose(brush);
1985                                 IMG_CanvasDispose(canvas);
1986                                 allqueue(REDRAWHEADERS, 0);
1987                                 break;
1988                 }
1989 #endif /* NAN_TPT */
1990         }
1991         else {
1992                 /* Draw tool is inactive */
1993                 switch(event) {
1994                         case LEFTMOUSE:
1995                                 if(G.qual & LR_SHIFTKEY) mouseco_to_curtile();
1996                                 else gesture();
1997                                 break;
1998                         case MIDDLEMOUSE:
1999                                 image_viewmove();
2000                                 break;
2001                         case RIGHTMOUSE:
2002                                 mouse_select_sima();
2003                                 break;
2004                         case AKEY:
2005                                 select_swap_tface_uv();
2006                                 break;
2007                         case BKEY:
2008                                 borderselect_sima();
2009                                 break;
2010                         case GKEY:
2011                                 transform_tface_uv('g');
2012                                 break;
2013                         case NKEY:
2014                                 if(G.qual & LR_CTRLKEY) replace_names_but();
2015                                 break;
2016                         case RKEY:
2017                                 transform_tface_uv('r');
2018                                 break;
2019                         case SKEY:
2020                                 transform_tface_uv('s');
2021                                 break;
2022                 }
2023         }
2024
2025         /* Events handled always (whether the draw tool is active or not) */
2026         switch (event) {
2027                 case MIDDLEMOUSE:
2028                         image_viewmove();
2029                         break;
2030                 case PADPLUSKEY:
2031                         sima->zoom *= 2;
2032                         scrarea_queue_winredraw(curarea);
2033                         break;
2034                 case HOMEKEY:
2035                         image_home();
2036                         break;
2037                 case PADMINUS:
2038                         sima->zoom /= 2;
2039                         /* Check if the image will still be visible after zooming out */
2040                         if (sima->zoom < 1) {
2041                                 calc_image_view(G.sima, 'p');
2042                                 if (sima->image) {
2043                                         if (sima->image->ibuf) {
2044                                                 width = sima->image->ibuf->x * sima->zoom;
2045                                                 height = sima->image->ibuf->y * sima->zoom;
2046                                                 if ((width < 4) && (height < 4)) {
2047                                                         /* Image will become too small, reset value */
2048                                                         sima->zoom *= 2;
2049                                                 }
2050                                         }
2051                                 }
2052                         }
2053                         scrarea_queue_winredraw(curarea);
2054                         break;                  
2055         }
2056 }
2057
2058
2059 void init_imagespace(ScrArea *sa)
2060 {
2061         SpaceImage *sima;
2062         
2063         sima= MEM_callocN(sizeof(SpaceImage), "initimaspace");
2064         BLI_addhead(&sa->spacedata, sima);
2065
2066         sima->spacetype= SPACE_IMAGE;
2067         sima->zoom= 1;
2068 }
2069
2070
2071 /* ******************** SPACE: IMASEL ********************** */
2072
2073 extern void drawimasel(void);
2074 extern void winqreadimasel(unsigned short, short, char ascii);
2075
2076
2077 /* alles naar imasel.c */
2078
2079
2080 /* ******************** SPACE: OOPS ********************** */
2081
2082 extern void drawoopsspace(void);
2083
2084 void winqreadoopsspace(unsigned short event, short val, char ascii)
2085 {
2086         SpaceOops *soops= curarea->spacedata.first;
2087         View2D *v2d= &soops->v2d;
2088         float dx, dy;
2089
2090         if(val==0) return;
2091
2092         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2093
2094         switch(event) {
2095         case LEFTMOUSE:
2096                 gesture();
2097                 break;
2098         case MIDDLEMOUSE:
2099                 view2dmove();   /* in drawipo.c */
2100                 break;
2101         case RIGHTMOUSE:
2102                 mouse_select_oops();
2103                 break;
2104         case PADPLUSKEY:
2105         
2106                 dx= 0.1154*(v2d->cur.xmax-v2d->cur.xmin);
2107                 dy= 0.1154*(v2d->cur.ymax-v2d->cur.ymin);
2108                 v2d->cur.xmin+= dx;
2109                 v2d->cur.xmax-= dx;
2110                 v2d->cur.ymin+= dy;
2111                 v2d->cur.ymax-= dy;
2112                 test_view2d(G.v2d, curarea->winx, curarea->winy);
2113                 scrarea_queue_winredraw(curarea);
2114                 break;
2115         
2116         case PADMINUS:
2117
2118                 dx= 0.15*(v2d->cur.xmax-v2d->cur.xmin);
2119                 dy= 0.15*(v2d->cur.ymax-v2d->cur.ymin);
2120                 v2d->cur.xmin-= dx;
2121                 v2d->cur.xmax+= dx;
2122                 v2d->cur.ymin-= dy;
2123                 v2d->cur.ymax+= dy;
2124                 test_view2d(G.v2d, curarea->winx, curarea->winy);
2125                 scrarea_queue_winredraw(curarea);
2126                 break;
2127                 
2128         case HOMEKEY:   
2129                 do_oops_buttons(B_OOPSHOME);
2130                 break;
2131                 
2132         case AKEY:
2133                 swap_select_all_oops();
2134                 scrarea_queue_winredraw(curarea);
2135                 break;
2136         case BKEY:
2137                 borderselect_oops();
2138                 break;
2139         case GKEY:
2140                 transform_oops('g');
2141                 break;
2142         case LKEY:
2143                 if(G.qual & LR_SHIFTKEY) select_backlinked_oops();
2144                 else select_linked_oops();
2145                 break;
2146         case SKEY:
2147                 
2148                 if(G.qual & LR_ALTKEY) shrink_oops();
2149                 else if(G.qual & LR_SHIFTKEY) shuffle_oops();
2150                 else transform_oops('s');
2151                 break;
2152
2153         case ONEKEY:
2154                 do_layer_buttons(0); break;
2155         case TWOKEY:
2156                 do_layer_buttons(1); break;
2157         case THREEKEY:
2158                 do_layer_buttons(2); break;
2159         case FOURKEY:
2160                 do_layer_buttons(3); break;
2161         case FIVEKEY:
2162                 do_layer_buttons(4); break;
2163         case SIXKEY:
2164                 do_layer_buttons(5); break;
2165         case SEVENKEY:
2166                 do_layer_buttons(6); break;
2167         case EIGHTKEY:
2168                 do_layer_buttons(7); break;
2169         case NINEKEY:
2170                 do_layer_buttons(8); break;
2171         case ZEROKEY:
2172                 do_layer_buttons(9); break;
2173         case MINUSKEY:
2174                 do_layer_buttons(10); break;
2175         case EQUALKEY:
2176                 do_layer_buttons(11); break;
2177         case ACCENTGRAVEKEY:
2178                 do_layer_buttons(-1); break;
2179         
2180         }
2181 }
2182
2183 void init_v2d_oops(View2D *v2d)
2184 {
2185         v2d->tot.xmin= -28.0;
2186         v2d->tot.xmax= 28.0;
2187         v2d->tot.ymin= -28.0;
2188         v2d->tot.ymax= 28.0;
2189         
2190         v2d->cur= v2d->tot;
2191
2192         v2d->min[0]= 10.0;
2193         v2d->min[1]= 4.0;
2194
2195         v2d->max[0]= 320.0;
2196         v2d->max[1]= 320.0;
2197         
2198         v2d->minzoom= 0.01;
2199         v2d->maxzoom= 2.0;
2200         
2201         /* v2d->scroll= L_SCROLL+B_SCROLL; */
2202         v2d->scroll= 0;
2203         v2d->keepaspect= 1;
2204         v2d->keepzoom= 0;
2205         v2d->keeptot= 0;
2206         
2207 }
2208
2209 void init_oopsspace(ScrArea *sa)
2210 {
2211         SpaceOops *soops;
2212         
2213         soops= MEM_callocN(sizeof(SpaceOops), "initoopsspace");
2214         BLI_addhead(&sa->spacedata, soops);
2215
2216         soops->visiflag= OOPS_OB+OOPS_MA+OOPS_ME+OOPS_TE+OOPS_CU+OOPS_IP;
2217         
2218         soops->spacetype= SPACE_OOPS;
2219         init_v2d_oops(&soops->v2d);
2220 }
2221
2222 /* ******************** SPACE: PAINT ********************** */
2223
2224
2225 /* ******************** SPACE: Text ********************** */
2226
2227 extern void drawtextspace(void);
2228 extern void winqreadtextspace(unsigned short, short, char ascii);
2229
2230 /* ******************** SPACE: ALGEMEEN ********************** */
2231
2232 void newspace(ScrArea *sa, int type)
2233 {
2234         if(type>=0) {
2235                 if(sa->spacetype != type) {
2236                         SpaceLink *sl;
2237                         
2238                         sa->spacetype= type;
2239                         sa->headbutofs= 0;
2240                         
2241                         uiFreeBlocks(&sa->uiblocks);
2242                         wich_cursor(sa);
2243                         
2244                         if (sa->headwin) addqueue(sa->headwin, CHANGED, 1);
2245                         scrarea_queue_headredraw(sa);
2246
2247                         addqueue(sa->win, CHANGED, 1);
2248                         scrarea_queue_winredraw(sa);
2249
2250                         areawinset(sa->win);
2251
2252                         bwin_clear_viewmat(sa->win);
2253                         
2254                         for (sl= sa->spacedata.first; sl; sl= sl->next)
2255                                 if(sl->spacetype==type)
2256                                         break;
2257
2258                         if (sl) {                       
2259                                 BLI_remlink(&sa->spacedata, sl);
2260                                 BLI_addhead(&sa->spacedata, sl);
2261                         } else {
2262                                 if(type==SPACE_VIEW3D)
2263                                         initview3d(sa);
2264                                 else if(type==SPACE_IPO)
2265                                         initipo(sa);
2266                                 else if(type==SPACE_INFO)
2267                                         init_infospace(sa);
2268                                 else if(type==SPACE_BUTS)
2269                                         init_butspace(sa);
2270                                 else if(type==SPACE_FILE)
2271                                         init_filespace(sa);
2272                                 else if(type==SPACE_SEQ)
2273                                         init_seqspace(sa);
2274                                 else if(type==SPACE_IMAGE)
2275                                         init_imagespace(sa);
2276                                 else if(type==SPACE_IMASEL)
2277                                         init_imaselspace(sa);
2278                                 else if(type==SPACE_OOPS)
2279                                         init_oopsspace(sa);
2280                                 else if(type==SPACE_ACTION)
2281                                         init_actionspace(sa);
2282                                 else if(type==SPACE_TEXT)
2283                                         init_textspace(sa);
2284                                 else if(type==SPACE_SOUND)
2285                                         init_soundspace(sa);
2286                                 else if(type==SPACE_NLA)
2287                                         init_nlaspace(sa);
2288
2289                                 sl= sa->spacedata.first;
2290                                 sl->area= sa;
2291                         }
2292                 }
2293         }
2294
2295                 
2296         /* uitzondering: filespace */
2297         if(curarea->spacetype==SPACE_FILE) {
2298                 SpaceFile *sfile= curarea->spacedata.first;
2299                 
2300                 if(sfile->type==FILE_MAIN) {
2301                         freefilelist(sfile);
2302                 } else {
2303                         sfile->type= FILE_UNIX;
2304                 }
2305                 
2306                 sfile->returnfunc= 0;
2307                 sfile->title[0]= 0;
2308                 if(sfile->filelist) test_flags_file(sfile);
2309         }
2310         /* uitzondering: imasel space */
2311         else if(curarea->spacetype==SPACE_IMASEL) {
2312                 SpaceImaSel *simasel= curarea->spacedata.first;
2313                 simasel->returnfunc= 0;
2314                 simasel->title[0]= 0;
2315         }
2316 }
2317
2318 void freespacelist(ListBase *lb)
2319 {
2320         SpaceLink *sl;
2321
2322         for (sl= lb->first; sl; sl= sl->next) {
2323                 if(sl->spacetype==SPACE_FILE) {
2324                         SpaceFile *sfile= (SpaceFile*) sl;
2325                         if(sfile->libfiledata)  
2326                                 BLO_blendhandle_close(sfile->libfiledata);
2327                 }
2328                 else if(sl->spacetype==SPACE_BUTS) {
2329                         SpaceButs *buts= (SpaceButs*) sl;
2330                         if(buts->rect) MEM_freeN(buts->rect);
2331                         if(G.buts==buts) G.buts= 0;
2332                 }
2333                 else if(sl->spacetype==SPACE_IPO) {
2334                         SpaceIpo *si= (SpaceIpo*) sl;
2335                         if(si->editipo) MEM_freeN(si->editipo);
2336                         free_ipokey(&si->ipokey);
2337                         if(G.sipo==si) G.sipo= 0;
2338                 }
2339                 else if(sl->spacetype==SPACE_VIEW3D) {
2340                         View3D *vd= (View3D*) sl;
2341                         if(vd->bgpic) {
2342                                 if(vd->bgpic->rect) MEM_freeN(vd->bgpic->rect);
2343                                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
2344                                 MEM_freeN(vd->bgpic);
2345                         }
2346                         if(vd->localvd) MEM_freeN(vd->localvd);
2347                         if(G.vd==vd) G.vd= 0;
2348                 }
2349                 else if(sl->spacetype==SPACE_OOPS) {
2350                         free_oopspace((SpaceOops *)sl);
2351                 }
2352                 else if(sl->spacetype==SPACE_IMASEL) {
2353                         free_imasel((SpaceImaSel *)sl);
2354                 }
2355                 else if(sl->spacetype==SPACE_ACTION) {
2356                         free_actionspace((SpaceAction*)sl);
2357                 }
2358                 else if(sl->spacetype==SPACE_NLA){
2359 /*                      free_nlaspace((SpaceNla*)sl);   */
2360                 }
2361                 else if(sl->spacetype==SPACE_TEXT) {
2362                         free_textspace((SpaceText *)sl);
2363                 }
2364                 else if(sl->spacetype==SPACE_SOUND) {
2365                         free_soundspace((SpaceSound *)sl);
2366                 }
2367         }
2368
2369         BLI_freelistN(lb);
2370 }
2371
2372 void duplicatespacelist(ScrArea *newarea, ListBase *lb1, ListBase *lb2)
2373 {
2374         SpaceLink *sl;
2375
2376         duplicatelist(lb1, lb2);
2377         
2378         /* lb1 is kopie van lb2, van lb2 geven we de filelist vrij */
2379         
2380         sl= lb2->first;
2381         while(sl) {
2382                 if(sl->spacetype==SPACE_FILE) {
2383                         SpaceFile *sfile= (SpaceFile*) sl;
2384                         sfile->libfiledata= 0;
2385                         sfile->filelist= 0;
2386                 }
2387                 else if(sl->spacetype==SPACE_OOPS) {
2388                         SpaceOops *so= (SpaceOops *)sl;
2389                         so->oops.first= so->oops.last= 0;
2390                 }
2391                 else if(sl->spacetype==SPACE_IMASEL) {
2392                         check_imasel_copy((SpaceImaSel *) sl);
2393                 }
2394                 else if(sl->spacetype==SPACE_TEXT) {
2395                 }
2396                 /* __PINFAKE */
2397 /*              else if(sfile->spacetype==SPACE_ACTION) {
2398                         SpaceAction *sa= (SpaceAction *)sfile;
2399                         if (sa->flag & SACTION_PIN)
2400                                 if (sa->action)
2401                                         sa->action->id.us++;
2402
2403                 }
2404 */              /* end PINFAKE */
2405
2406                 sl= sl->next;
2407         }
2408         
2409         sl= lb1->first;
2410         while(sl) {
2411                 sl->area= newarea;
2412
2413                 if(sl->spacetype==SPACE_BUTS) {
2414                         SpaceButs *buts= (SpaceButs *)sl;
2415                         buts->rect= 0;
2416                 }
2417                 else if(sl->spacetype==SPACE_IPO) {
2418                         SpaceIpo *si= (SpaceIpo *)sl;
2419                         si->editipo= 0;
2420                         si->ipokey.first= si->ipokey.last= 0;
2421                 }
2422                 else if(sl->spacetype==SPACE_VIEW3D) {
2423                         View3D *vd= (View3D *)sl;
2424                         if(vd->bgpic) {
2425                                 vd->bgpic= MEM_dupallocN(vd->bgpic);
2426                                 vd->bgpic->rect= 0;
2427                                 if(vd->bgpic->ima) vd->bgpic->ima->id.us++;
2428                         }
2429                 }
2430                 sl= sl->next;
2431         }
2432
2433         /* nog een keer: van oude View3D de localview restoren (ivm full) */
2434         sl= lb2->first;
2435         while(sl) {
2436                 if(sl->spacetype==SPACE_VIEW3D) {
2437                         View3D *v3d= (View3D*) sl;
2438                         if(v3d->localvd) {
2439                                 restore_localviewdata(v3d);
2440                                 v3d->localvd= 0;
2441                                 v3d->localview= 0;
2442                                 v3d->lay &= 0xFFFFFF;
2443                         }
2444                 }
2445                 sl= sl->next;
2446         }
2447 }
2448
2449 /* wordt overal aangeroepen */
2450 void allqueue(unsigned short event, short val)
2451 {
2452         ScrArea *sa;
2453         View3D *v3d;
2454         SpaceButs *buts;
2455         SpaceFile *sfile;
2456
2457         sa= G.curscreen->areabase.first;
2458         while(sa) {
2459                 if(event==REDRAWALL) {
2460                         scrarea_queue_winredraw(sa);
2461                         scrarea_queue_headredraw(sa);
2462                 }
2463                 else if(sa->win != val) {
2464                         switch(event) {
2465                                 
2466                         case REDRAWHEADERS:
2467                                 scrarea_queue_headredraw(sa);
2468                                 break;
2469                         case REDRAWVIEW3D:
2470                                 if(sa->spacetype==SPACE_VIEW3D) {
2471                                         scrarea_queue_winredraw(sa);
2472                                         if(val) scrarea_queue_headredraw(sa);
2473                                 }
2474                                 break;
2475                         case REDRAWVIEW3D_Z:
2476                                 if(sa->spacetype==SPACE_VIEW3D) {
2477                                         v3d= sa->spacedata.first;
2478                                         if(v3d->drawtype==OB_SOLID) {
2479                                                 scrarea_queue_winredraw(sa);
2480                                                 if(val) scrarea_queue_headredraw(sa);
2481                                         }
2482                                 }
2483                                 break;
2484                         case REDRAWVIEWCAM:
2485                                 if(sa->spacetype==SPACE_VIEW3D) {
2486                                         v3d= sa->spacedata.first;
2487                                         if(v3d->persp>1) scrarea_queue_winredraw(sa);
2488                                 }
2489                                 break;
2490                         case REDRAWINFO:
2491                                 if(sa->spacetype==SPACE_INFO) {
2492                                         scrarea_queue_headredraw(sa);
2493                                 }
2494                                 break;
2495                         case REDRAWIMAGE:
2496                                 if(sa->spacetype==SPACE_IMAGE) {
2497                                         scrarea_queue_winredraw(sa);
2498                                         scrarea_queue_headredraw(sa);
2499                                 }
2500                                 break;
2501                         case REDRAWIPO:
2502                                 if(sa->spacetype==SPACE_IPO) {
2503                                         SpaceIpo *si;
2504                                         scrarea_queue_winredraw(sa);
2505                                         scrarea_queue_headredraw(sa);
2506                                         if(val) {
2507                                                 si= sa->spacedata.first;
2508                                                 if (!G.sipo->pin)                                                       
2509                                                         si->blocktype= val;
2510                                         }
2511                                 }
2512                                 else if(sa->spacetype==SPACE_OOPS) {
2513                                         scrarea_queue_winredraw(sa);
2514                                 }
2515                                 
2516                                 break;
2517                                 
2518                         case REDRAWBUTSALL:
2519                                 if(sa->spacetype==SPACE_BUTS) {
2520                                         scrarea_queue_winredraw(sa);
2521                                         scrarea_queue_headredraw(sa);
2522                                 }
2523                                 break;
2524                         case REDRAWBUTSHEAD:
2525                                 if(sa->spacetype==SPACE_BUTS) {
2526                                         scrarea_queue_headredraw(sa);
2527                                 }
2528                                 break;
2529                         case REDRAWBUTSVIEW:
2530                                 if(sa->spacetype==SPACE_BUTS) {
2531                                         buts= sa->spacedata.first;
2532                                         if(buts->mainb==BUTS_VIEW) {
2533                                                 scrarea_queue_winredraw(sa);
2534                                                 scrarea_queue_headredraw(sa);
2535                                         }
2536                                 }
2537                                 break;
2538                         case REDRAWBUTSLAMP:
2539                                 if(sa->spacetype==SPACE_BUTS) {
2540                                         buts= sa->spacedata.first;
2541                                         if(buts->mainb==BUTS_LAMP) {
2542                                                 scrarea_queue_winredraw(sa);
2543                                                 scrarea_queue_headredraw(sa);
2544                                         }
2545                                 }
2546                                 break;
2547                         case REDRAWBUTSMAT:
2548                                 if(sa->spacetype==SPACE_BUTS) {
2549                                         buts= sa->spacedata.first;
2550                                         if(buts->mainb==BUTS_MAT) {
2551                                                 scrarea_queue_winredraw(sa);
2552                                                 scrarea_queue_headredraw(sa);
2553                                         }
2554                                 }
2555                                 break;
2556                         case REDRAWBUTSTEX:
2557                                 if(sa->spacetype==SPACE_BUTS) {
2558                                         buts= sa->spacedata.first;
2559                                         if(buts->mainb==BUTS_TEX) {
2560                                                 scrarea_queue_winredraw(sa);
2561                                                 scrarea_queue_headredraw(sa);
2562                                         }
2563                                 }
2564                                 break;
2565                         case REDRAWBUTSANIM:
2566                                 if(sa->spacetype==SPACE_BUTS) {
2567                                         buts= sa->spacedata.first;
2568                                         if(buts->mainb==BUTS_ANIM) {
2569                                                 scrarea_queue_winredraw(sa);
2570                                                 scrarea_queue_headredraw(sa);
2571                                         }
2572                                 }
2573                                 break;
2574                         case REDRAWBUTSWORLD:
2575                                 if(sa->spacetype==SPACE_BUTS) {
2576                                         buts= sa->spacedata.first;
2577                                         if(buts->mainb==BUTS_WORLD) {
2578                                                 scrarea_queue_winredraw(sa);
2579                                                 scrarea_queue_headredraw(sa);
2580                                         }
2581                                 }
2582                                 break;
2583                         case REDRAWBUTSRENDER:
2584                                 if(sa->spacetype==SPACE_BUTS) {
2585                                         buts= sa->spacedata.first;
2586                                         if(buts->mainb==BUTS_RENDER) {
2587                                                 scrarea_queue_winredraw(sa);
2588                                                 scrarea_queue_headredraw(sa);
2589                                         }
2590                                 }
2591                                 break;
2592                         case REDRAWBUTSEDIT:
2593                                 if(sa->spacetype==SPACE_BUTS) {
2594                                         buts= sa->spacedata.first;
2595                                         if(buts->mainb==BUTS_EDIT) {
2596                                                 scrarea_queue_winredraw(sa);
2597                                                 scrarea_queue_headredraw(sa);
2598                                         }
2599                                 }
2600                                 break;
2601                         case REDRAWBUTSGAME:
2602                                 if(sa->spacetype==SPACE_BUTS) {
2603                                         buts= sa->spacedata.first;
2604                                         if ELEM(buts->mainb, BUTS_GAME, BUTS_FPAINT) {
2605                                                 scrarea_queue_winredraw(sa);
2606                                                 scrarea_queue_headredraw(sa);
2607                                         }
2608                                 }
2609                                 break;
2610                         case REDRAWBUTSRADIO:
2611                                 if(sa->spacetype==SPACE_BUTS) {
2612                                         buts= sa->spacedata.first;
2613                                         if(buts->mainb==BUTS_RADIO) {
2614                                                 scrarea_queue_winredraw(sa);
2615                                                 scrarea_queue_headredraw(sa);
2616                                         }
2617                                 }
2618                                 break;
2619                         case REDRAWBUTSSCRIPT:
2620                                 if(sa->spacetype==SPACE_BUTS) {
2621                                         buts= sa->spacedata.first;
2622                                         if(buts->mainb==BUTS_SCRIPT) {
2623                                                 scrarea_queue_winredraw(sa);
2624                                                 scrarea_queue_headredraw(sa);
2625                                         }
2626                                 }
2627                                 break;
2628                         case REDRAWBUTSSOUND:
2629                                 if(sa->spacetype==SPACE_BUTS) {
2630                                         buts= sa->spacedata.first;
2631                                         if(buts->mainb==BUTS_SOUND) {
2632                                                 scrarea_queue_winredraw(sa);
2633                                                 scrarea_queue_headredraw(sa);
2634                                         }
2635                                 }
2636                                 break;
2637                         case REDRAWBUTSCONSTRAINT:
2638                                 if(sa->spacetype==SPACE_BUTS) {
2639                                         buts= sa->spacedata.first;
2640                                         if(buts->mainb==BUTS_CONSTRAINT) {
2641                                                 scrarea_queue_winredraw(sa);
2642                                                 scrarea_queue_headredraw(sa);
2643                                         }
2644                                 }
2645                                 break;                          
2646                         case REDRAWDATASELECT:
2647                                 if(sa->spacetype==SPACE_FILE) {
2648                                         sfile= sa->spacedata.first;
2649                                         if(sfile->type==FILE_MAIN) {
2650                                                 freefilelist(sfile);
2651                                                 scrarea_queue_winredraw(sa);
2652                                         }
2653                                 }
2654                                 else if(sa->spacetype==SPACE_OOPS) {
2655                                         scrarea_queue_winredraw(sa);
2656                                 }
2657                                 break;
2658                         case REDRAWSEQ:
2659                                 if(sa->spacetype==SPACE_SEQ) {
2660                                         addqueue(sa->win, CHANGED, 1);
2661                                         scrarea_queue_winredraw(sa);
2662                                         scrarea_queue_headredraw(sa);
2663                                 }
2664                                 break;
2665                         case REDRAWOOPS:
2666                                 if(sa->spacetype==SPACE_OOPS) {
2667                                         scrarea_queue_winredraw(sa);
2668                                 }
2669                                 break;
2670                         case REDRAWNLA:
2671                                 if(sa->spacetype==SPACE_NLA) {
2672                                         scrarea_queue_headredraw(sa);
2673                                         scrarea_queue_winredraw(sa);
2674                                 }
2675                         case REDRAWACTION:
2676                                 if(sa->spacetype==SPACE_ACTION) {
2677                                         scrarea_queue_headredraw(sa);
2678                                         scrarea_queue_winredraw(sa);
2679                                 }
2680                                 break;
2681                         case REDRAWTEXT:
2682                                 if(sa->spacetype==SPACE_TEXT) {
2683                                         scrarea_queue_winredraw(sa);
2684                                 }
2685                                 break;
2686                         case REDRAWSOUND:
2687                                 if(sa->spacetype==SPACE_SOUND) {
2688                                         scrarea_queue_headredraw(sa);
2689                                         scrarea_queue_winredraw(sa);
2690                                 }
2691                                 break;
2692                         }
2693                 }
2694                 sa= sa->next;
2695         }
2696 }
2697
2698 void allspace(unsigned short event, short val)
2699 {
2700         bScreen *sc;
2701
2702         sc= G.main->screen.first;
2703         while(sc) {
2704                 ScrArea *sa= sc->areabase.first;
2705                 while(sa) {
2706                         SpaceLink *sl= sa->spacedata.first;
2707                         while(sl) {
2708                                 switch(event) {
2709                                 case REMAKEALLIPO:
2710                                         {
2711                                                 Ipo *ipo;
2712                                                 IpoCurve *icu;
2713                                                 
2714                                                 /* Go to each ipo */
2715                                                 for (ipo=G.main->ipo.first; ipo; ipo=ipo->id.next){
2716                                                         for (icu = ipo->curve.first; icu; icu=icu->next){
2717                                                                 sort_time_ipocurve(icu);
2718                                                                 testhandles_ipocurve(icu);
2719                                                         }
2720                                                 }
2721                                         }
2722                                         break;
2723                                 case REMAKEIPO:
2724                                         if(sl->spacetype==SPACE_IPO) {
2725                                                 SpaceIpo *si= (SpaceIpo *)sl;
2726                                                 {
2727                                                         if(si->editipo) MEM_freeN(si->editipo);
2728                                                         si->editipo= 0;
2729                                                         free_ipokey(&si->ipokey);
2730                                                 }
2731                                         }
2732                                         break;
2733                                                                                 
2734                                 case OOPS_TEST:
2735                                         if(sl->spacetype==SPACE_OOPS) {
2736                                                 SpaceOops *so= (SpaceOops *)sl;
2737                                                 so->flag |= SO_TESTBLOCKS;
2738                                         }
2739                                         break;
2740                                 }
2741
2742                                 sl= sl->next;
2743                         }
2744                         sa= sa->next;
2745                 }
2746                 sc= sc->id.next;
2747         }
2748 }
2749
2750
2751 void force_draw()
2752 {
2753         /* alle area's die (ongeveer) zelfde laten zien als curarea */
2754         ScrArea *tempsa, *sa;
2755
2756         scrarea_do_windraw(curarea);
2757         
2758         tempsa= curarea;
2759         sa= G.curscreen->areabase.first;
2760         while(sa) {
2761                 if(sa!=tempsa && sa->spacetype==tempsa->spacetype) {
2762                         if(sa->spacetype==SPACE_VIEW3D) {
2763                                 if( ((View3D *)sa->spacedata.first)->lay & ((View3D *)tempsa->spacedata.first)->lay) {
2764                                         areawinset(sa->win);
2765                                         scrarea_do_windraw(sa);
2766                                 }
2767                         }
2768                         else if(sa->spacetype==SPACE_IPO) {
2769                                 areawinset(sa->win);
2770                                 scrarea_do_windraw(sa);
2771                         }
2772                         else if(sa->spacetype==SPACE_SEQ) {
2773                                 areawinset(sa->win);
2774                                 scrarea_do_windraw(sa);
2775                         }
2776                         else if(sa->spacetype==SPACE_ACTION) {
2777                                 areawinset(sa->win);
2778                                 scrarea_do_windraw(sa);
2779                         }
2780                 }
2781                 sa= sa->next;
2782         }
2783         if(curarea!=tempsa) areawinset(tempsa->win);
2784         
2785         screen_swapbuffers();
2786
2787 }
2788
2789 void force_draw_plus(int type)
2790 {
2791         /* alle area's die (ongeveer) zelfde laten zien als curarea EN areas van 'type' */
2792         ScrArea *tempsa, *sa;
2793
2794         scrarea_do_windraw(curarea); 
2795
2796         tempsa= curarea;
2797         sa= G.curscreen->areabase.first;
2798         while(sa) {
2799                 if(sa!=tempsa && (sa->spacetype==tempsa->spacetype || sa->spacetype==type)) {
2800                         if(ELEM5(sa->spacetype, SPACE_VIEW3D, SPACE_IPO, SPACE_SEQ, SPACE_BUTS, SPACE_ACTION)) {
2801                                 areawinset(sa->win);
2802                                 scrarea_do_windraw(sa);
2803                         }
2804                 }
2805                 sa= sa->next;
2806         }
2807         if(curarea!=tempsa) areawinset(tempsa->win);
2808
2809         screen_swapbuffers();
2810 }
2811
2812 void force_draw_all(void)
2813 {
2814         /* alle area's die (ongeveer) zelfde laten zien als curarea EN areas van 'type' */
2815         ScrArea *tempsa, *sa;
2816
2817         drawscreen();
2818
2819         tempsa= curarea;
2820         sa= G.curscreen->areabase.first;
2821         while(sa) {
2822                 if(sa->headwin) {
2823                         scrarea_do_headdraw(sa);
2824                         scrarea_do_headchange(sa);
2825                 }
2826                 if(sa->win) {
2827                         scrarea_do_windraw(sa);
2828                 }
2829                 sa= sa->next;
2830         }
2831         if(curarea!=tempsa) areawinset(tempsa->win);
2832
2833         screen_swapbuffers();
2834 }
2835
2836 /***/
2837
2838 SpaceType *spaceaction_get_type(void)
2839 {
2840         static SpaceType *st= NULL;
2841         
2842         if (!st) {
2843                 st= spacetype_new("Action");
2844                 spacetype_set_winfuncs(st, drawactionspace, changeview2d, winqreadactionspace);
2845         }
2846
2847         return st;
2848 }
2849 SpaceType *spacebuts_get_type(void)
2850 {
2851         static SpaceType *st= NULL;
2852         
2853         if (!st) {
2854                 st= spacetype_new("Buts");
2855                 spacetype_set_winfuncs(st, drawbutspace, changebutspace, winqreadbutspace);
2856         }
2857
2858         return st;
2859 }
2860 SpaceType *spacefile_get_type(void)
2861 {
2862         static SpaceType *st= NULL;
2863         
2864         if (!st) {
2865                 st= spacetype_new("File");
2866                 spacetype_set_winfuncs(st, drawfilespace, NULL, winqreadfilespace);
2867         }
2868
2869         return st;
2870 }
2871 SpaceType *spaceimage_get_type(void)
2872 {
2873         static SpaceType *st= NULL;
2874         
2875         if (!st) {
2876                 st= spacetype_new("Image");
2877                 spacetype_set_winfuncs(st, drawimagespace, NULL, winqreadimagespace);
2878         }
2879
2880         return st;
2881 }
2882 SpaceType *spaceimasel_get_type(void)
2883 {
2884         static SpaceType *st= NULL;
2885         
2886         if (!st) {
2887                 st= spacetype_new("Imasel");
2888                 spacetype_set_winfuncs(st, drawimasel, NULL, winqreadimasel);
2889         }
2890
2891         return st;
2892 }
2893 SpaceType *spaceinfo_get_type(void)
2894 {
2895         static SpaceType *st= NULL;
2896         
2897         if (!st) {
2898                 st= spacetype_new("Info");
2899                 spacetype_set_winfuncs(st, drawinfospace, NULL, winqreadinfospace);
2900         }
2901
2902         return st;
2903 }
2904 SpaceType *spaceipo_get_type(void)
2905 {
2906         static SpaceType *st= NULL;
2907         
2908         if (!st) {
2909                 st= spacetype_new("Ipo");
2910                 spacetype_set_winfuncs(st, drawipo, changeview2d, winqreadipo);
2911         }
2912
2913         return st;
2914 }
2915 SpaceType *spacenla_get_type(void)
2916 {
2917         static SpaceType *st= NULL;
2918         
2919         if (!st) {
2920                 st= spacetype_new("Nla");
2921                 spacetype_set_winfuncs(st, drawnlaspace, changeview2d, winqreadnlaspace);
2922         }
2923
2924         return st;
2925 }
2926 SpaceType *spaceoops_get_type(void)
2927 {
2928         static SpaceType *st= NULL;
2929         
2930         if (!st) {
2931                 st= spacetype_new("Oops");
2932                 spacetype_set_winfuncs(st, drawoopsspace, changeview2d, winqreadoopsspace);
2933         }
2934
2935         return st;
2936 }
2937 SpaceType *spaceseq_get_type(void)
2938 {
2939         static SpaceType *st= NULL;
2940         
2941         if (!st) {
2942                 st= spacetype_new("Sequence");
2943                 spacetype_set_winfuncs(st, drawseqspace, changeview2d, winqreadsequence);
2944         }
2945
2946         return st;
2947 }
2948 SpaceType *spacesound_get_type(void)
2949 {
2950         static SpaceType *st= NULL;
2951         
2952         if (!st) {
2953                 st= spacetype_new("Sound");
2954                 spacetype_set_winfuncs(st, drawsoundspace, NULL, winqreadsoundspace);
2955         }
2956
2957         return st;
2958 }
2959 SpaceType *spacetext_get_type(void)
2960 {
2961         static SpaceType *st= NULL;
2962         
2963         if (!st) {
2964                 st= spacetype_new("Text");
2965                 spacetype_set_winfuncs(st, drawtextspace, NULL, winqreadtextspace);
2966         }
2967
2968         return st;
2969 }
2970 SpaceType *spaceview3d_get_type(void)
2971 {
2972         static SpaceType *st= NULL;
2973         
2974         if (!st) {
2975                 st= spacetype_new("View3D");
2976                 spacetype_set_winfuncs(st, drawview3d, changeview3d, winqread3d);
2977         }
2978
2979         return st;
2980 }