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