added showlinenrs variable to spacetext
[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         st->showlinenrs= 0;
1827         
1828         st->top= 0;
1829 }
1830
1831 void init_imaselspace(ScrArea *sa)
1832 {
1833         SpaceImaSel *simasel;
1834         
1835         simasel= MEM_callocN(sizeof(SpaceImaSel), "initimaselspace");
1836         BLI_addhead(&sa->spacedata, simasel);
1837
1838         simasel->spacetype= SPACE_IMASEL;
1839         
1840         simasel->mode = 7;
1841         strcpy (simasel->dir,  U.textudir);     /* TON */
1842         strcpy (simasel->file, "");
1843         strcpy(simasel->fole, simasel->file);
1844         strcpy(simasel->dor,  simasel->dir);
1845
1846         simasel->first_sel_ima  =  0;
1847         simasel->hilite_ima         =  0;
1848         simasel->firstdir               =  0;
1849         simasel->firstfile              =  0;
1850         simasel->cmap           =  0;
1851         simasel->returnfunc     =  0;
1852         
1853         simasel->title[0]       =  0;
1854         
1855         clear_ima_dir(simasel);
1856         
1857         // simasel->cmap= IMB_loadiffmem((int*)datatoc_cmap_tga, IB_rect|IB_cmap);
1858         simasel->cmap= IMB_ibImageFromMemory((int *)datatoc_cmap_tga, datatoc_cmap_tga_size, IB_rect|IB_cmap);
1859         if (!simasel->cmap) {
1860                 error("in console");
1861                 printf("Image select cmap file not found \n");
1862         }
1863 }
1864
1865 /* ******************** SPACE: SOUND ********************** */
1866
1867 extern void drawsoundspace(void);
1868 extern void winqreadsoundspace(unsigned short, short, char ascii);
1869
1870 void init_soundspace(ScrArea *sa)
1871 {
1872         SpaceSound *ssound;
1873         
1874         ssound= MEM_callocN(sizeof(SpaceSound), "initsoundspace");
1875         BLI_addhead(&sa->spacedata, ssound);
1876
1877         ssound->spacetype= SPACE_SOUND;
1878         
1879         /* sound space loopt van (0,8) tot (250, 0) */
1880
1881         ssound->v2d.tot.xmin= -4.0;
1882         ssound->v2d.tot.ymin= -4.0;
1883         ssound->v2d.tot.xmax= 250.0;
1884         ssound->v2d.tot.ymax= 255.0;
1885         
1886         ssound->v2d.cur.xmin= -4.0;
1887         ssound->v2d.cur.ymin= -4.0;
1888         ssound->v2d.cur.xmax= 50.0;
1889         ssound->v2d.cur.ymax= 255.0;
1890
1891         ssound->v2d.min[0]= 1.0;
1892         ssound->v2d.min[1]= 259.0;
1893
1894         ssound->v2d.max[0]= 32000.0;
1895         ssound->v2d.max[1]= 259;
1896         
1897         ssound->v2d.minzoom= 0.1;
1898         ssound->v2d.maxzoom= 10.0;
1899         
1900         ssound->v2d.scroll= B_SCROLL;
1901         ssound->v2d.keepaspect= 0;
1902         ssound->v2d.keepzoom= 0;
1903         ssound->v2d.keeptot= 0;
1904         
1905 }
1906
1907 void free_soundspace(SpaceSound *ssound)
1908 {
1909         /* don't free ssound itself */
1910         
1911         
1912 }
1913
1914 /* ******************** SPACE: IMAGE ********************** */
1915
1916 /*  extern void drawimagespace(); BIF_drawimage.h */
1917
1918 void winqreadimagespace(unsigned short event, short val, char ascii)
1919 {
1920         SpaceImage *sima= curarea->spacedata.first;
1921         View2D *v2d= &sima->v2d;
1922         int width, height;
1923 #ifdef NAN_TPT
1924         IMG_BrushPtr brush;
1925         IMG_CanvasPtr canvas;
1926         int rowBytes;
1927         short xy_prev[2], xy_curr[2];
1928         float uv_prev[2], uv_curr[2];
1929         extern VPaint Gvp;
1930 #endif /* NAN_TPT */    
1931         if(val==0) return;
1932
1933         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1934         
1935         if (sima->flag & SI_DRAWTOOL) {
1936 #ifdef NAN_TPT
1937                 /* Draw tool is active */
1938                 switch(event) {
1939                         case LEFTMOUSE:
1940                                 /* Paranoia checks */
1941                                 if (!sima) break;
1942                                 if (!sima->image) break;
1943                                 if (!sima->image->ibuf) break;
1944                                 if (sima->image->packedfile) {
1945                                         error("Painting in packed images not supported");
1946                                         break;
1947                                 }
1948                         
1949                                 brush = IMG_BrushCreate(Gvp.size, Gvp.size, Gvp.r, Gvp.g, Gvp.b, Gvp.a);
1950                                 /* MAART: skipx is not set most of the times. Make a guess. */
1951                                 rowBytes = sima->image->ibuf->skipx ? sima->image->ibuf->skipx : sima->image->ibuf->x * 4;
1952                                 canvas = IMG_CanvasCreateFromPtr(sima->image->ibuf->rect, sima->image->ibuf->x, sima->image->ibuf->y, rowBytes);
1953
1954                                 getmouseco_areawin(xy_prev);
1955                                 while (get_mbut() & L_MOUSE) {
1956                                         getmouseco_areawin(xy_curr);
1957                                         /* Check if mouse position changed */
1958                                         if ((xy_prev[0] != xy_curr[0]) || (xy_prev[1] != xy_curr[1])) {
1959                                                 /* Convert mouse coordinates to u,v and draw */
1960                                                 areamouseco_to_ipoco(v2d, xy_prev, &uv_prev[0], &uv_prev[1]);
1961                                                 areamouseco_to_ipoco(v2d, xy_curr, &uv_curr[0], &uv_curr[1]);
1962                                                 IMG_CanvasDrawLineUV(canvas, brush, uv_prev[0], uv_prev[1], uv_curr[0], uv_curr[1]);
1963                                                 if (G.sima->lock) {
1964                                                         /* Make OpenGL aware of a changed texture */
1965                                                         free_realtime_image(sima->image);
1966                                                         /* Redraw this view and the 3D view */
1967                                                         force_draw_plus(SPACE_VIEW3D);
1968                                                 }
1969                                                 else {
1970                                                         /* Redraw only this view */
1971                                                         force_draw();
1972                                                 }
1973                                                 xy_prev[0] = xy_curr[0];
1974                                                 xy_prev[1] = xy_curr[1];
1975                                         }
1976                                 }
1977                                 /* Set the dirty bit in the image so that it is clear that it has been modified. */
1978                                 sima->image->ibuf->userflags |= IB_BITMAPDIRTY;
1979                                 if (!G.sima->lock) {
1980                                         /* Make OpenGL aware of a changed texture */
1981                                         free_realtime_image(sima->image);
1982                                         /* Redraw this view and the 3D view */
1983                                         force_draw_plus(SPACE_VIEW3D);
1984                                 }
1985                                 IMG_BrushDispose(brush);
1986                                 IMG_CanvasDispose(canvas);
1987                                 allqueue(REDRAWHEADERS, 0);
1988                                 break;
1989                 }
1990 #endif /* NAN_TPT */
1991         }
1992         else {
1993                 /* Draw tool is inactive */
1994                 switch(event) {
1995                         case LEFTMOUSE:
1996                                 if(G.qual & LR_SHIFTKEY) mouseco_to_curtile();
1997                                 else gesture();
1998                                 break;
1999                         case MIDDLEMOUSE:
2000                                 image_viewmove();
2001                                 break;
2002                         case RIGHTMOUSE:
2003                                 mouse_select_sima();
2004                                 break;
2005                         case AKEY:
2006                                 select_swap_tface_uv();
2007                                 break;
2008                         case BKEY:
2009                                 borderselect_sima();
2010                                 break;
2011                         case GKEY:
2012                                 transform_tface_uv('g');
2013                                 break;
2014                         case NKEY:
2015                                 if(G.qual & LR_CTRLKEY) replace_names_but();
2016                                 break;
2017                         case RKEY:
2018                                 transform_tface_uv('r');
2019                                 break;
2020                         case SKEY:
2021                                 transform_tface_uv('s');
2022                                 break;
2023                 }
2024         }
2025
2026         /* Events handled always (whether the draw tool is active or not) */
2027         switch (event) {
2028                 case MIDDLEMOUSE:
2029                         image_viewmove();
2030                         break;
2031                 case PADPLUSKEY:
2032                         sima->zoom *= 2;
2033                         scrarea_queue_winredraw(curarea);
2034                         break;
2035                 case HOMEKEY:
2036                         image_home();
2037                         break;
2038                 case PADMINUS:
2039                         sima->zoom /= 2;
2040                         /* Check if the image will still be visible after zooming out */
2041                         if (sima->zoom < 1) {
2042                                 calc_image_view(G.sima, 'p');
2043                                 if (sima->image) {
2044                                         if (sima->image->ibuf) {
2045                                                 width = sima->image->ibuf->x * sima->zoom;
2046                                                 height = sima->image->ibuf->y * sima->zoom;
2047                                                 if ((width < 4) && (height < 4)) {
2048                                                         /* Image will become too small, reset value */
2049                                                         sima->zoom *= 2;
2050                                                 }
2051                                         }
2052                                 }
2053                         }
2054                         scrarea_queue_winredraw(curarea);
2055                         break;                  
2056         }
2057 }
2058
2059
2060 void init_imagespace(ScrArea *sa)
2061 {
2062         SpaceImage *sima;
2063         
2064         sima= MEM_callocN(sizeof(SpaceImage), "initimaspace");
2065         BLI_addhead(&sa->spacedata, sima);
2066
2067         sima->spacetype= SPACE_IMAGE;
2068         sima->zoom= 1;
2069 }
2070
2071
2072 /* ******************** SPACE: IMASEL ********************** */
2073
2074 extern void drawimasel(void);
2075 extern void winqreadimasel(unsigned short, short, char ascii);
2076
2077
2078 /* alles naar imasel.c */
2079
2080
2081 /* ******************** SPACE: OOPS ********************** */
2082
2083 extern void drawoopsspace(void);
2084
2085 void winqreadoopsspace(unsigned short event, short val, char ascii)
2086 {
2087         SpaceOops *soops= curarea->spacedata.first;
2088         View2D *v2d= &soops->v2d;
2089         float dx, dy;
2090
2091         if(val==0) return;
2092
2093         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2094
2095         switch(event) {
2096         case LEFTMOUSE:
2097                 gesture();
2098                 break;
2099         case MIDDLEMOUSE:
2100                 view2dmove();   /* in drawipo.c */
2101                 break;
2102         case RIGHTMOUSE:
2103                 mouse_select_oops();
2104                 break;
2105         case PADPLUSKEY:
2106         
2107                 dx= 0.1154*(v2d->cur.xmax-v2d->cur.xmin);
2108                 dy= 0.1154*(v2d->cur.ymax-v2d->cur.ymin);
2109                 v2d->cur.xmin+= dx;
2110                 v2d->cur.xmax-= dx;
2111                 v2d->cur.ymin+= dy;
2112                 v2d->cur.ymax-= dy;
2113                 test_view2d(G.v2d, curarea->winx, curarea->winy);
2114                 scrarea_queue_winredraw(curarea);
2115                 break;
2116         
2117         case PADMINUS:
2118
2119                 dx= 0.15*(v2d->cur.xmax-v2d->cur.xmin);
2120                 dy= 0.15*(v2d->cur.ymax-v2d->cur.ymin);
2121                 v2d->cur.xmin-= dx;
2122                 v2d->cur.xmax+= dx;
2123                 v2d->cur.ymin-= dy;
2124                 v2d->cur.ymax+= dy;
2125                 test_view2d(G.v2d, curarea->winx, curarea->winy);
2126                 scrarea_queue_winredraw(curarea);
2127                 break;
2128                 
2129         case HOMEKEY:   
2130                 do_oops_buttons(B_OOPSHOME);
2131                 break;
2132                 
2133         case AKEY:
2134                 swap_select_all_oops();
2135                 scrarea_queue_winredraw(curarea);
2136                 break;
2137         case BKEY:
2138                 borderselect_oops();
2139                 break;
2140         case GKEY:
2141                 transform_oops('g');
2142                 break;
2143         case LKEY:
2144                 if(G.qual & LR_SHIFTKEY) select_backlinked_oops();
2145                 else select_linked_oops();
2146                 break;
2147         case SKEY:
2148                 
2149                 if(G.qual & LR_ALTKEY) shrink_oops();
2150                 else if(G.qual & LR_SHIFTKEY) shuffle_oops();
2151                 else transform_oops('s');
2152                 break;
2153
2154         case ONEKEY:
2155                 do_layer_buttons(0); break;
2156         case TWOKEY:
2157                 do_layer_buttons(1); break;
2158         case THREEKEY:
2159                 do_layer_buttons(2); break;
2160         case FOURKEY:
2161                 do_layer_buttons(3); break;
2162         case FIVEKEY:
2163                 do_layer_buttons(4); break;
2164         case SIXKEY:
2165                 do_layer_buttons(5); break;
2166         case SEVENKEY:
2167                 do_layer_buttons(6); break;
2168         case EIGHTKEY:
2169                 do_layer_buttons(7); break;
2170         case NINEKEY:
2171                 do_layer_buttons(8); break;
2172         case ZEROKEY:
2173                 do_layer_buttons(9); break;
2174         case MINUSKEY:
2175                 do_layer_buttons(10); break;
2176         case EQUALKEY:
2177                 do_layer_buttons(11); break;
2178         case ACCENTGRAVEKEY:
2179                 do_layer_buttons(-1); break;
2180         
2181         }
2182 }
2183
2184 void init_v2d_oops(View2D *v2d)
2185 {
2186         v2d->tot.xmin= -28.0;
2187         v2d->tot.xmax= 28.0;
2188         v2d->tot.ymin= -28.0;
2189         v2d->tot.ymax= 28.0;
2190         
2191         v2d->cur= v2d->tot;
2192
2193         v2d->min[0]= 10.0;
2194         v2d->min[1]= 4.0;
2195
2196         v2d->max[0]= 320.0;
2197         v2d->max[1]= 320.0;
2198         
2199         v2d->minzoom= 0.01;
2200         v2d->maxzoom= 2.0;
2201         
2202         /* v2d->scroll= L_SCROLL+B_SCROLL; */
2203         v2d->scroll= 0;
2204         v2d->keepaspect= 1;
2205         v2d->keepzoom= 0;
2206         v2d->keeptot= 0;
2207         
2208 }
2209
2210 void init_oopsspace(ScrArea *sa)
2211 {
2212         SpaceOops *soops;
2213         
2214         soops= MEM_callocN(sizeof(SpaceOops), "initoopsspace");
2215         BLI_addhead(&sa->spacedata, soops);
2216
2217         soops->visiflag= OOPS_OB+OOPS_MA+OOPS_ME+OOPS_TE+OOPS_CU+OOPS_IP;
2218         
2219         soops->spacetype= SPACE_OOPS;
2220         init_v2d_oops(&soops->v2d);
2221 }
2222
2223 /* ******************** SPACE: PAINT ********************** */
2224
2225
2226 /* ******************** SPACE: Text ********************** */
2227
2228 extern void drawtextspace(void);
2229 extern void winqreadtextspace(unsigned short, short, char ascii);
2230
2231 /* ******************** SPACE: ALGEMEEN ********************** */
2232
2233 void newspace(ScrArea *sa, int type)
2234 {
2235         if(type>=0) {
2236                 if(sa->spacetype != type) {
2237                         SpaceLink *sl;
2238                         
2239                         sa->spacetype= type;
2240                         sa->headbutofs= 0;
2241                         
2242                         uiFreeBlocks(&sa->uiblocks);
2243                         wich_cursor(sa);
2244                         
2245                         if (sa->headwin) addqueue(sa->headwin, CHANGED, 1);
2246                         scrarea_queue_headredraw(sa);
2247
2248                         addqueue(sa->win, CHANGED, 1);
2249                         scrarea_queue_winredraw(sa);
2250
2251                         areawinset(sa->win);
2252
2253                         bwin_clear_viewmat(sa->win);
2254                         
2255                         for (sl= sa->spacedata.first; sl; sl= sl->next)
2256                                 if(sl->spacetype==type)
2257                                         break;
2258
2259                         if (sl) {                       
2260                                 BLI_remlink(&sa->spacedata, sl);
2261                                 BLI_addhead(&sa->spacedata, sl);
2262                         } else {
2263                                 if(type==SPACE_VIEW3D)
2264                                         initview3d(sa);
2265                                 else if(type==SPACE_IPO)
2266                                         initipo(sa);
2267                                 else if(type==SPACE_INFO)
2268                                         init_infospace(sa);
2269                                 else if(type==SPACE_BUTS)
2270                                         init_butspace(sa);
2271                                 else if(type==SPACE_FILE)
2272                                         init_filespace(sa);
2273                                 else if(type==SPACE_SEQ)
2274                                         init_seqspace(sa);
2275                                 else if(type==SPACE_IMAGE)
2276                                         init_imagespace(sa);
2277                                 else if(type==SPACE_IMASEL)
2278                                         init_imaselspace(sa);
2279                                 else if(type==SPACE_OOPS)
2280                                         init_oopsspace(sa);
2281                                 else if(type==SPACE_ACTION)
2282                                         init_actionspace(sa);
2283                                 else if(type==SPACE_TEXT)
2284                                         init_textspace(sa);
2285                                 else if(type==SPACE_SOUND)
2286                                         init_soundspace(sa);
2287                                 else if(type==SPACE_NLA)
2288                                         init_nlaspace(sa);
2289
2290                                 sl= sa->spacedata.first;
2291                                 sl->area= sa;
2292                         }
2293                 }
2294         }
2295
2296                 
2297         /* uitzondering: filespace */
2298         if(curarea->spacetype==SPACE_FILE) {
2299                 SpaceFile *sfile= curarea->spacedata.first;
2300                 
2301                 if(sfile->type==FILE_MAIN) {
2302                         freefilelist(sfile);
2303                 } else {
2304                         sfile->type= FILE_UNIX;
2305                 }
2306                 
2307                 sfile->returnfunc= 0;
2308                 sfile->title[0]= 0;
2309                 if(sfile->filelist) test_flags_file(sfile);
2310         }
2311         /* uitzondering: imasel space */
2312         else if(curarea->spacetype==SPACE_IMASEL) {
2313                 SpaceImaSel *simasel= curarea->spacedata.first;
2314                 simasel->returnfunc= 0;
2315                 simasel->title[0]= 0;
2316         }
2317 }
2318
2319 void freespacelist(ListBase *lb)
2320 {
2321         SpaceLink *sl;
2322
2323         for (sl= lb->first; sl; sl= sl->next) {
2324                 if(sl->spacetype==SPACE_FILE) {
2325                         SpaceFile *sfile= (SpaceFile*) sl;
2326                         if(sfile->libfiledata)  
2327                                 BLO_blendhandle_close(sfile->libfiledata);
2328                 }
2329                 else if(sl->spacetype==SPACE_BUTS) {
2330                         SpaceButs *buts= (SpaceButs*) sl;
2331                         if(buts->rect) MEM_freeN(buts->rect);
2332                         if(G.buts==buts) G.buts= 0;
2333                 }
2334                 else if(sl->spacetype==SPACE_IPO) {
2335                         SpaceIpo *si= (SpaceIpo*) sl;
2336                         if(si->editipo) MEM_freeN(si->editipo);
2337                         free_ipokey(&si->ipokey);
2338                         if(G.sipo==si) G.sipo= 0;
2339                 }
2340                 else if(sl->spacetype==SPACE_VIEW3D) {
2341                         View3D *vd= (View3D*) sl;
2342                         if(vd->bgpic) {
2343                                 if(vd->bgpic->rect) MEM_freeN(vd->bgpic->rect);
2344                                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
2345                                 MEM_freeN(vd->bgpic);
2346                         }
2347                         if(vd->localvd) MEM_freeN(vd->localvd);
2348                         if(G.vd==vd) G.vd= 0;
2349                 }
2350                 else if(sl->spacetype==SPACE_OOPS) {
2351                         free_oopspace((SpaceOops *)sl);
2352                 }
2353                 else if(sl->spacetype==SPACE_IMASEL) {
2354                         free_imasel((SpaceImaSel *)sl);
2355                 }
2356                 else if(sl->spacetype==SPACE_ACTION) {
2357                         free_actionspace((SpaceAction*)sl);
2358                 }
2359                 else if(sl->spacetype==SPACE_NLA){
2360 /*                      free_nlaspace((SpaceNla*)sl);   */
2361                 }
2362                 else if(sl->spacetype==SPACE_TEXT) {
2363                         free_textspace((SpaceText *)sl);
2364                 }
2365                 else if(sl->spacetype==SPACE_SOUND) {
2366                         free_soundspace((SpaceSound *)sl);
2367                 }
2368         }
2369
2370         BLI_freelistN(lb);
2371 }
2372
2373 void duplicatespacelist(ScrArea *newarea, ListBase *lb1, ListBase *lb2)
2374 {
2375         SpaceLink *sl;
2376
2377         duplicatelist(lb1, lb2);
2378         
2379         /* lb1 is kopie van lb2, van lb2 geven we de filelist vrij */
2380         
2381         sl= lb2->first;
2382         while(sl) {
2383                 if(sl->spacetype==SPACE_FILE) {
2384                         SpaceFile *sfile= (SpaceFile*) sl;
2385                         sfile->libfiledata= 0;
2386                         sfile->filelist= 0;
2387                 }
2388                 else if(sl->spacetype==SPACE_OOPS) {
2389                         SpaceOops *so= (SpaceOops *)sl;
2390                         so->oops.first= so->oops.last= 0;
2391                 }
2392                 else if(sl->spacetype==SPACE_IMASEL) {
2393                         check_imasel_copy((SpaceImaSel *) sl);
2394                 }
2395                 else if(sl->spacetype==SPACE_TEXT) {
2396                 }
2397                 /* __PINFAKE */
2398 /*              else if(sfile->spacetype==SPACE_ACTION) {
2399                         SpaceAction *sa= (SpaceAction *)sfile;
2400                         if (sa->flag & SACTION_PIN)
2401                                 if (sa->action)
2402                                         sa->action->id.us++;
2403
2404                 }
2405 */              /* end PINFAKE */
2406
2407                 sl= sl->next;
2408         }
2409         
2410         sl= lb1->first;
2411         while(sl) {
2412                 sl->area= newarea;
2413
2414                 if(sl->spacetype==SPACE_BUTS) {
2415                         SpaceButs *buts= (SpaceButs *)sl;
2416                         buts->rect= 0;
2417                 }
2418                 else if(sl->spacetype==SPACE_IPO) {
2419                         SpaceIpo *si= (SpaceIpo *)sl;
2420                         si->editipo= 0;
2421                         si->ipokey.first= si->ipokey.last= 0;
2422                 }
2423                 else if(sl->spacetype==SPACE_VIEW3D) {
2424                         View3D *vd= (View3D *)sl;
2425                         if(vd->bgpic) {
2426                                 vd->bgpic= MEM_dupallocN(vd->bgpic);
2427                                 vd->bgpic->rect= 0;
2428                                 if(vd->bgpic->ima) vd->bgpic->ima->id.us++;
2429                         }
2430                 }
2431                 sl= sl->next;
2432         }
2433
2434         /* nog een keer: van oude View3D de localview restoren (ivm full) */
2435         sl= lb2->first;
2436         while(sl) {
2437                 if(sl->spacetype==SPACE_VIEW3D) {
2438                         View3D *v3d= (View3D*) sl;
2439                         if(v3d->localvd) {
2440                                 restore_localviewdata(v3d);
2441                                 v3d->localvd= 0;
2442                                 v3d->localview= 0;
2443                                 v3d->lay &= 0xFFFFFF;
2444                         }
2445                 }
2446                 sl= sl->next;
2447         }
2448 }
2449
2450 /* wordt overal aangeroepen */
2451 void allqueue(unsigned short event, short val)
2452 {
2453         ScrArea *sa;
2454         View3D *v3d;
2455         SpaceButs *buts;
2456         SpaceFile *sfile;
2457
2458         sa= G.curscreen->areabase.first;
2459         while(sa) {
2460                 if(event==REDRAWALL) {
2461                         scrarea_queue_winredraw(sa);
2462                         scrarea_queue_headredraw(sa);
2463                 }
2464                 else if(sa->win != val) {
2465                         switch(event) {
2466                                 
2467                         case REDRAWHEADERS:
2468                                 scrarea_queue_headredraw(sa);
2469                                 break;
2470                         case REDRAWVIEW3D:
2471                                 if(sa->spacetype==SPACE_VIEW3D) {
2472                                         scrarea_queue_winredraw(sa);
2473                                         if(val) scrarea_queue_headredraw(sa);
2474                                 }
2475                                 break;
2476                         case REDRAWVIEW3D_Z:
2477                                 if(sa->spacetype==SPACE_VIEW3D) {
2478                                         v3d= sa->spacedata.first;
2479                                         if(v3d->drawtype==OB_SOLID) {
2480                                                 scrarea_queue_winredraw(sa);
2481                                                 if(val) scrarea_queue_headredraw(sa);
2482                                         }
2483                                 }
2484                                 break;
2485                         case REDRAWVIEWCAM:
2486                                 if(sa->spacetype==SPACE_VIEW3D) {
2487                                         v3d= sa->spacedata.first;
2488                                         if(v3d->persp>1) scrarea_queue_winredraw(sa);
2489                                 }
2490                                 break;
2491                         case REDRAWINFO:
2492                                 if(sa->spacetype==SPACE_INFO) {
2493                                         scrarea_queue_headredraw(sa);
2494                                 }
2495                                 break;
2496                         case REDRAWIMAGE:
2497                                 if(sa->spacetype==SPACE_IMAGE) {
2498                                         scrarea_queue_winredraw(sa);
2499                                         scrarea_queue_headredraw(sa);
2500                                 }
2501                                 break;
2502                         case REDRAWIPO:
2503                                 if(sa->spacetype==SPACE_IPO) {
2504                                         SpaceIpo *si;
2505                                         scrarea_queue_winredraw(sa);
2506                                         scrarea_queue_headredraw(sa);
2507                                         if(val) {
2508                                                 si= sa->spacedata.first;
2509                                                 if (!G.sipo->pin)                                                       
2510                                                         si->blocktype= val;
2511                                         }
2512                                 }
2513                                 else if(sa->spacetype==SPACE_OOPS) {
2514                                         scrarea_queue_winredraw(sa);
2515                                 }
2516                                 
2517                                 break;
2518                                 
2519                         case REDRAWBUTSALL:
2520                                 if(sa->spacetype==SPACE_BUTS) {
2521                                         scrarea_queue_winredraw(sa);
2522                                         scrarea_queue_headredraw(sa);
2523                                 }
2524                                 break;
2525                         case REDRAWBUTSHEAD:
2526                                 if(sa->spacetype==SPACE_BUTS) {
2527                                         scrarea_queue_headredraw(sa);
2528                                 }
2529                                 break;
2530                         case REDRAWBUTSVIEW:
2531                                 if(sa->spacetype==SPACE_BUTS) {
2532                                         buts= sa->spacedata.first;
2533                                         if(buts->mainb==BUTS_VIEW) {
2534                                                 scrarea_queue_winredraw(sa);
2535                                                 scrarea_queue_headredraw(sa);
2536                                         }
2537                                 }
2538                                 break;
2539                         case REDRAWBUTSLAMP:
2540                                 if(sa->spacetype==SPACE_BUTS) {
2541                                         buts= sa->spacedata.first;
2542                                         if(buts->mainb==BUTS_LAMP) {
2543                                                 scrarea_queue_winredraw(sa);
2544                                                 scrarea_queue_headredraw(sa);
2545                                         }
2546                                 }
2547                                 break;
2548                         case REDRAWBUTSMAT:
2549                                 if(sa->spacetype==SPACE_BUTS) {
2550                                         buts= sa->spacedata.first;
2551                                         if(buts->mainb==BUTS_MAT) {
2552                                                 scrarea_queue_winredraw(sa);
2553                                                 scrarea_queue_headredraw(sa);
2554                                         }
2555                                 }
2556                                 break;
2557                         case REDRAWBUTSTEX:
2558                                 if(sa->spacetype==SPACE_BUTS) {
2559                                         buts= sa->spacedata.first;
2560                                         if(buts->mainb==BUTS_TEX) {
2561                                                 scrarea_queue_winredraw(sa);
2562                                                 scrarea_queue_headredraw(sa);
2563                                         }
2564                                 }
2565                                 break;
2566                         case REDRAWBUTSANIM:
2567                                 if(sa->spacetype==SPACE_BUTS) {
2568                                         buts= sa->spacedata.first;
2569                                         if(buts->mainb==BUTS_ANIM) {
2570                                                 scrarea_queue_winredraw(sa);
2571                                                 scrarea_queue_headredraw(sa);
2572                                         }
2573                                 }
2574                                 break;
2575                         case REDRAWBUTSWORLD:
2576                                 if(sa->spacetype==SPACE_BUTS) {
2577                                         buts= sa->spacedata.first;
2578                                         if(buts->mainb==BUTS_WORLD) {
2579                                                 scrarea_queue_winredraw(sa);
2580                                                 scrarea_queue_headredraw(sa);
2581                                         }
2582                                 }
2583                                 break;
2584                         case REDRAWBUTSRENDER:
2585                                 if(sa->spacetype==SPACE_BUTS) {
2586                                         buts= sa->spacedata.first;
2587                                         if(buts->mainb==BUTS_RENDER) {
2588                                                 scrarea_queue_winredraw(sa);
2589                                                 scrarea_queue_headredraw(sa);
2590                                         }
2591                                 }
2592                                 break;
2593                         case REDRAWBUTSEDIT:
2594                                 if(sa->spacetype==SPACE_BUTS) {
2595                                         buts= sa->spacedata.first;
2596                                         if(buts->mainb==BUTS_EDIT) {
2597                                                 scrarea_queue_winredraw(sa);
2598                                                 scrarea_queue_headredraw(sa);
2599                                         }
2600                                 }
2601                                 break;
2602                         case REDRAWBUTSGAME:
2603                                 if(sa->spacetype==SPACE_BUTS) {
2604                                         buts= sa->spacedata.first;
2605                                         if ELEM(buts->mainb, BUTS_GAME, BUTS_FPAINT) {
2606                                                 scrarea_queue_winredraw(sa);
2607                                                 scrarea_queue_headredraw(sa);
2608                                         }
2609                                 }
2610                                 break;
2611                         case REDRAWBUTSRADIO:
2612                                 if(sa->spacetype==SPACE_BUTS) {
2613                                         buts= sa->spacedata.first;
2614                                         if(buts->mainb==BUTS_RADIO) {
2615                                                 scrarea_queue_winredraw(sa);
2616                                                 scrarea_queue_headredraw(sa);
2617                                         }
2618                                 }
2619                                 break;
2620                         case REDRAWBUTSSCRIPT:
2621                                 if(sa->spacetype==SPACE_BUTS) {
2622                                         buts= sa->spacedata.first;
2623                                         if(buts->mainb==BUTS_SCRIPT) {
2624                                                 scrarea_queue_winredraw(sa);
2625                                                 scrarea_queue_headredraw(sa);
2626                                         }
2627                                 }
2628                                 break;
2629                         case REDRAWBUTSSOUND:
2630                                 if(sa->spacetype==SPACE_BUTS) {
2631                                         buts= sa->spacedata.first;
2632                                         if(buts->mainb==BUTS_SOUND) {
2633                                                 scrarea_queue_winredraw(sa);
2634                                                 scrarea_queue_headredraw(sa);
2635                                         }
2636                                 }
2637                                 break;
2638                         case REDRAWBUTSCONSTRAINT:
2639                                 if(sa->spacetype==SPACE_BUTS) {
2640                                         buts= sa->spacedata.first;
2641                                         if(buts->mainb==BUTS_CONSTRAINT) {
2642                                                 scrarea_queue_winredraw(sa);
2643                                                 scrarea_queue_headredraw(sa);
2644                                         }
2645                                 }
2646                                 break;                          
2647                         case REDRAWDATASELECT:
2648                                 if(sa->spacetype==SPACE_FILE) {
2649                                         sfile= sa->spacedata.first;
2650                                         if(sfile->type==FILE_MAIN) {
2651                                                 freefilelist(sfile);
2652                                                 scrarea_queue_winredraw(sa);
2653                                         }
2654                                 }
2655                                 else if(sa->spacetype==SPACE_OOPS) {
2656                                         scrarea_queue_winredraw(sa);
2657                                 }
2658                                 break;
2659                         case REDRAWSEQ:
2660                                 if(sa->spacetype==SPACE_SEQ) {
2661                                         addqueue(sa->win, CHANGED, 1);
2662                                         scrarea_queue_winredraw(sa);
2663                                         scrarea_queue_headredraw(sa);
2664                                 }
2665                                 break;
2666                         case REDRAWOOPS:
2667                                 if(sa->spacetype==SPACE_OOPS) {
2668                                         scrarea_queue_winredraw(sa);
2669                                 }
2670                                 break;
2671                         case REDRAWNLA:
2672                                 if(sa->spacetype==SPACE_NLA) {
2673                                         scrarea_queue_headredraw(sa);
2674                                         scrarea_queue_winredraw(sa);
2675                                 }
2676                         case REDRAWACTION:
2677                                 if(sa->spacetype==SPACE_ACTION) {
2678                                         scrarea_queue_headredraw(sa);
2679                                         scrarea_queue_winredraw(sa);
2680                                 }
2681                                 break;
2682                         case REDRAWTEXT:
2683                                 if(sa->spacetype==SPACE_TEXT) {
2684                                         scrarea_queue_winredraw(sa);
2685                                 }
2686                                 break;
2687                         case REDRAWSOUND:
2688                                 if(sa->spacetype==SPACE_SOUND) {
2689                                         scrarea_queue_headredraw(sa);
2690                                         scrarea_queue_winredraw(sa);
2691                                 }
2692                                 break;
2693                         }
2694                 }
2695                 sa= sa->next;
2696         }
2697 }
2698
2699 void allspace(unsigned short event, short val)
2700 {
2701         bScreen *sc;
2702
2703         sc= G.main->screen.first;
2704         while(sc) {
2705                 ScrArea *sa= sc->areabase.first;
2706                 while(sa) {
2707                         SpaceLink *sl= sa->spacedata.first;
2708                         while(sl) {
2709                                 switch(event) {
2710                                 case REMAKEALLIPO:
2711                                         {
2712                                                 Ipo *ipo;
2713                                                 IpoCurve *icu;
2714                                                 
2715                                                 /* Go to each ipo */
2716                                                 for (ipo=G.main->ipo.first; ipo; ipo=ipo->id.next){
2717                                                         for (icu = ipo->curve.first; icu; icu=icu->next){
2718                                                                 sort_time_ipocurve(icu);
2719                                                                 testhandles_ipocurve(icu);
2720                                                         }
2721                                                 }
2722                                         }
2723                                         break;
2724                                 case REMAKEIPO:
2725                                         if(sl->spacetype==SPACE_IPO) {
2726                                                 SpaceIpo *si= (SpaceIpo *)sl;
2727                                                 {
2728                                                         if(si->editipo) MEM_freeN(si->editipo);
2729                                                         si->editipo= 0;
2730                                                         free_ipokey(&si->ipokey);
2731                                                 }
2732                                         }
2733                                         break;
2734                                                                                 
2735                                 case OOPS_TEST:
2736                                         if(sl->spacetype==SPACE_OOPS) {
2737                                                 SpaceOops *so= (SpaceOops *)sl;
2738                                                 so->flag |= SO_TESTBLOCKS;
2739                                         }
2740                                         break;
2741                                 }
2742
2743                                 sl= sl->next;
2744                         }
2745                         sa= sa->next;
2746                 }
2747                 sc= sc->id.next;
2748         }
2749 }
2750
2751
2752 void force_draw()
2753 {
2754         /* alle area's die (ongeveer) zelfde laten zien als curarea */
2755         ScrArea *tempsa, *sa;
2756
2757         scrarea_do_windraw(curarea);
2758         
2759         tempsa= curarea;
2760         sa= G.curscreen->areabase.first;
2761         while(sa) {
2762                 if(sa!=tempsa && sa->spacetype==tempsa->spacetype) {
2763                         if(sa->spacetype==SPACE_VIEW3D) {
2764                                 if( ((View3D *)sa->spacedata.first)->lay & ((View3D *)tempsa->spacedata.first)->lay) {
2765                                         areawinset(sa->win);
2766                                         scrarea_do_windraw(sa);
2767                                 }
2768                         }
2769                         else if(sa->spacetype==SPACE_IPO) {
2770                                 areawinset(sa->win);
2771                                 scrarea_do_windraw(sa);
2772                         }
2773                         else if(sa->spacetype==SPACE_SEQ) {
2774                                 areawinset(sa->win);
2775                                 scrarea_do_windraw(sa);
2776                         }
2777                         else if(sa->spacetype==SPACE_ACTION) {
2778                                 areawinset(sa->win);
2779                                 scrarea_do_windraw(sa);
2780                         }
2781                 }
2782                 sa= sa->next;
2783         }
2784         if(curarea!=tempsa) areawinset(tempsa->win);
2785         
2786         screen_swapbuffers();
2787
2788 }
2789
2790 void force_draw_plus(int type)
2791 {
2792         /* alle area's die (ongeveer) zelfde laten zien als curarea EN areas van 'type' */
2793         ScrArea *tempsa, *sa;
2794
2795         scrarea_do_windraw(curarea); 
2796
2797         tempsa= curarea;
2798         sa= G.curscreen->areabase.first;
2799         while(sa) {
2800                 if(sa!=tempsa && (sa->spacetype==tempsa->spacetype || sa->spacetype==type)) {
2801                         if(ELEM5(sa->spacetype, SPACE_VIEW3D, SPACE_IPO, SPACE_SEQ, SPACE_BUTS, SPACE_ACTION)) {
2802                                 areawinset(sa->win);
2803                                 scrarea_do_windraw(sa);
2804                         }
2805                 }
2806                 sa= sa->next;
2807         }
2808         if(curarea!=tempsa) areawinset(tempsa->win);
2809
2810         screen_swapbuffers();
2811 }
2812
2813 void force_draw_all(void)
2814 {
2815         /* alle area's die (ongeveer) zelfde laten zien als curarea EN areas van 'type' */
2816         ScrArea *tempsa, *sa;
2817
2818         drawscreen();
2819
2820         tempsa= curarea;
2821         sa= G.curscreen->areabase.first;
2822         while(sa) {
2823                 if(sa->headwin) {
2824                         scrarea_do_headdraw(sa);
2825                         scrarea_do_headchange(sa);
2826                 }
2827                 if(sa->win) {
2828                         scrarea_do_windraw(sa);
2829                 }
2830                 sa= sa->next;
2831         }
2832         if(curarea!=tempsa) areawinset(tempsa->win);
2833
2834         screen_swapbuffers();
2835 }
2836
2837 /***/
2838
2839 SpaceType *spaceaction_get_type(void)
2840 {
2841         static SpaceType *st= NULL;
2842         
2843         if (!st) {
2844                 st= spacetype_new("Action");
2845                 spacetype_set_winfuncs(st, drawactionspace, changeview2d, winqreadactionspace);
2846         }
2847
2848         return st;
2849 }
2850 SpaceType *spacebuts_get_type(void)
2851 {
2852         static SpaceType *st= NULL;
2853         
2854         if (!st) {
2855                 st= spacetype_new("Buts");
2856                 spacetype_set_winfuncs(st, drawbutspace, changebutspace, winqreadbutspace);
2857         }
2858
2859         return st;
2860 }
2861 SpaceType *spacefile_get_type(void)
2862 {
2863         static SpaceType *st= NULL;
2864         
2865         if (!st) {
2866                 st= spacetype_new("File");
2867                 spacetype_set_winfuncs(st, drawfilespace, NULL, winqreadfilespace);
2868         }
2869
2870         return st;
2871 }
2872 SpaceType *spaceimage_get_type(void)
2873 {
2874         static SpaceType *st= NULL;
2875         
2876         if (!st) {
2877                 st= spacetype_new("Image");
2878                 spacetype_set_winfuncs(st, drawimagespace, NULL, winqreadimagespace);
2879         }
2880
2881         return st;
2882 }
2883 SpaceType *spaceimasel_get_type(void)
2884 {
2885         static SpaceType *st= NULL;
2886         
2887         if (!st) {
2888                 st= spacetype_new("Imasel");
2889                 spacetype_set_winfuncs(st, drawimasel, NULL, winqreadimasel);
2890         }
2891
2892         return st;
2893 }
2894 SpaceType *spaceinfo_get_type(void)
2895 {
2896         static SpaceType *st= NULL;
2897         
2898         if (!st) {
2899                 st= spacetype_new("Info");
2900                 spacetype_set_winfuncs(st, drawinfospace, NULL, winqreadinfospace);
2901         }
2902
2903         return st;
2904 }
2905 SpaceType *spaceipo_get_type(void)
2906 {
2907         static SpaceType *st= NULL;
2908         
2909         if (!st) {
2910                 st= spacetype_new("Ipo");
2911                 spacetype_set_winfuncs(st, drawipo, changeview2d, winqreadipo);
2912         }
2913
2914         return st;
2915 }
2916 SpaceType *spacenla_get_type(void)
2917 {
2918         static SpaceType *st= NULL;
2919         
2920         if (!st) {
2921                 st= spacetype_new("Nla");
2922                 spacetype_set_winfuncs(st, drawnlaspace, changeview2d, winqreadnlaspace);
2923         }
2924
2925         return st;
2926 }
2927 SpaceType *spaceoops_get_type(void)
2928 {
2929         static SpaceType *st= NULL;
2930         
2931         if (!st) {
2932                 st= spacetype_new("Oops");
2933                 spacetype_set_winfuncs(st, drawoopsspace, changeview2d, winqreadoopsspace);
2934         }
2935
2936         return st;
2937 }
2938 SpaceType *spaceseq_get_type(void)
2939 {
2940         static SpaceType *st= NULL;
2941         
2942         if (!st) {
2943                 st= spacetype_new("Sequence");
2944                 spacetype_set_winfuncs(st, drawseqspace, changeview2d, winqreadsequence);
2945         }
2946
2947         return st;
2948 }
2949 SpaceType *spacesound_get_type(void)
2950 {
2951         static SpaceType *st= NULL;
2952         
2953         if (!st) {
2954                 st= spacetype_new("Sound");
2955                 spacetype_set_winfuncs(st, drawsoundspace, NULL, winqreadsoundspace);
2956         }
2957
2958         return st;
2959 }
2960 SpaceType *spacetext_get_type(void)
2961 {
2962         static SpaceType *st= NULL;
2963         
2964         if (!st) {
2965                 st= spacetype_new("Text");
2966                 spacetype_set_winfuncs(st, drawtextspace, NULL, winqreadtextspace);
2967         }
2968
2969         return st;
2970 }
2971 SpaceType *spaceview3d_get_type(void)
2972 {
2973         static SpaceType *st= NULL;
2974         
2975         if (!st) {
2976                 st= spacetype_new("View3D");
2977                 spacetype_set_winfuncs(st, drawview3d, changeview3d, winqread3d);
2978         }
2979
2980         return st;
2981 }