8f85a87c23b8c3741c5c372f0d6eb047d459d5e3
[blender.git] / source / blender / src / editscreen.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  * All screen functions that are related to the interface
32  * handling and drawing. Might be split up as well later...
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef WIN32
40 #include "BLI_winstuff.h"
41 #endif   
42
43 #include "nla.h"
44
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>  /* isprint */
48 #include <stdio.h>
49 #include <math.h>
50
51 #include "MEM_guardedalloc.h"
52
53 #include "PIL_time.h"
54
55 #include "BMF_Api.h"
56
57 #include "BLI_blenlib.h"
58 #include "BLI_arithb.h"
59
60 #include "IMB_imbuf_types.h"
61 #include "IMB_imbuf.h"
62
63 #include "DNA_action_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_screen_types.h"
66 #include "DNA_space_types.h"
67 #include "DNA_sound_types.h"
68 #include "DNA_view3d_types.h"
69 #include "DNA_userdef_types.h"
70
71 #include "BLO_writefile.h"
72
73 #include "BKE_utildefines.h"
74 #include "BKE_global.h"
75 #include "BKE_library.h"
76 #include "BKE_main.h"
77 #include "BKE_blender.h"
78 #include "BKE_screen.h"
79
80 #include "BIF_editsound.h"
81 #include "BIF_glutil.h"
82 #include "BIF_gl.h"
83 #include "BIF_graphics.h"
84 #include "BIF_interface.h"
85 #include "BIF_mainqueue.h"
86 #include "BIF_mywindow.h"
87 #include "BIF_renderwin.h"
88 #include "BIF_screen.h"
89 #include "BIF_space.h"
90 #include "BIF_toets.h"
91 #include "BIF_toolbox.h"
92 #include "BIF_usiblender.h"
93 #include "BIF_keyval.h"
94 #include "BIF_resources.h"
95
96 #include "BSE_edit.h"
97 #include "BSE_filesel.h"
98 #include "BSE_headerbuttons.h"
99 #include "BSE_view.h"
100
101 #include "BPY_extern.h"
102 #include "mydevice.h"
103 #include "blendef.h"
104 #include "render.h"
105
106 #include "winlay.h"
107
108 /* TIPS:
109  * 
110  * - WATCH THE EDGES,  VERTICES HAVE TO BE IN ORDER...
111          (lowest pointer first). Otherwise unpredictable effects!
112  * - problem: flags here are not nicely implemented. After usage
113          always reset to zero.
114  */
115
116 static void testareas(void);
117 static void area_autoplayscreen(void);
118 static void wait_for_event(void);
119
120
121 /* ********* Globals *********** */
122
123 static Window *mainwin= NULL;
124 static int prefsizx= 0, prefsizy= 0, prefstax= 0, prefstay= 0, start_maximized= 1;
125 static short dodrawscreen= 1;
126 static ScrArea *areawinar[MAXWIN];
127 static ScrArea *g_activearea= NULL;
128 short winqueue_break= 0;
129 ScrArea *curarea= 0;
130
131 /**********************************************************************/
132
133 static void screen_set_cursor(bScreen *sc) 
134 {
135         if (sc->winakt>3) {
136                 ScrArea *sa= areawinar[sc->winakt];
137
138                 set_cursor(sa->cursor);
139         } else {
140                 set_cursor(CURSOR_STD);
141         }
142 }
143
144 void waitcursor(int val)
145 {
146         if(val) {
147                 set_cursor(CURSOR_WAIT);
148         } else {
149                 screen_set_cursor(G.curscreen);
150         }
151 }
152
153 static int choose_cursor(ScrArea *sa)
154 {
155         if (sa->spacetype==SPACE_VIEW3D) {
156                 if(G.obedit) return CURSOR_EDIT;
157                 else if(G.f & G_VERTEXPAINT) return CURSOR_VPAINT;
158                 else if(G.f & G_WEIGHTPAINT) return CURSOR_VPAINT;
159                 else if(G.f & G_FACESELECT) return CURSOR_FACESEL;
160                 else return CURSOR_STD;
161         } else {
162                 return CURSOR_STD;
163         }
164 }
165
166 void wich_cursor(ScrArea *sa)
167 {
168         sa->cursor= choose_cursor(sa);
169
170         screen_set_cursor(G.curscreen);
171 }
172
173
174 void setcursor_space(int spacetype, short cur)
175 {
176         bScreen *sc;
177         ScrArea *sa;
178
179         for (sc= G.main->screen.first; sc; sc= sc->id.next)
180                 for (sa= sc->areabase.first; sa; sa= sa->next)
181                         if(sa->spacetype==spacetype)
182                                 sa->cursor= cur;
183
184         screen_set_cursor(G.curscreen);
185 }
186
187
188 /* *********  IN/OUT  ************* */
189
190 void getmouseco_sc(short *mval)         /* screen coordinates */
191 {
192         getmouse(mval);
193 }
194
195 /* mouse_cursor called during a script (via Window.QHandle) need
196  * this function for getmouseco_areawin to work: */
197 void set_g_activearea(ScrArea *sa)
198 {
199         if (sa) g_activearea = sa;
200 }
201
202 void getmouseco_areawin(short *mval)            /* internal area coordinates */
203 {
204         getmouseco_sc(mval);
205         
206         if(g_activearea && g_activearea->win) {
207                 mval[0]-= g_activearea->winrct.xmin;
208                 mval[1]-= g_activearea->winrct.ymin;
209         }
210 }
211
212 void getmouseco_headwin(short *mval)            /* internal area coordinates */
213 {
214         getmouseco_sc(mval);
215         
216         if(g_activearea && g_activearea->headwin) {
217                 mval[0]-= g_activearea->headrct.xmin;
218                 mval[1]-= g_activearea->headrct.ymin;
219         }
220 }
221
222 void headerprint(char *str)
223 {
224         if(curarea->headertype) {
225                 areawinset(curarea->headwin);
226                 
227                 headerbox(curarea);
228                 cpack(0x0);
229                 glRasterPos2i(20+curarea->headbutofs,  6);
230                 BMF_DrawString(G.font, str);
231                 
232                 curarea->head_swap= WIN_BACK_OK;
233                 areawinset(curarea->win);
234         }
235         else {
236                 // dunno... thats for later (ton)
237         }
238 }
239
240 /* *********** STUFF ************** */
241
242 static int scredge_is_horizontal(ScrEdge *se)
243 {
244         return (se->v1->vec.y == se->v2->vec.y);
245 }
246
247 static ScrEdge *screen_find_active_scredge(bScreen *sc, short *mval)
248 {
249         ScrEdge *se;
250         
251         for (se= sc->edgebase.first; se; se= se->next) {
252                 if (scredge_is_horizontal(se)) {
253                         short min, max;
254                         min= MIN2(se->v1->vec.x, se->v2->vec.x);
255                         max= MAX2(se->v1->vec.x, se->v2->vec.x);
256                         
257                         if (abs(mval[1]-se->v1->vec.y)<=2 && mval[0] >= min && mval[0]<=max)
258                                 return se;
259                 } 
260                 else {
261                         short min, max;
262                         min= MIN2(se->v1->vec.y, se->v2->vec.y);
263                         max= MAX2(se->v1->vec.y, se->v2->vec.y);
264
265                         if (abs(mval[0]-se->v1->vec.x)<=2 && mval[1] >= min && mval[1]<=max)
266                                 return se;
267                 }
268         }
269         
270         return NULL;
271 }
272
273 void areawinset(short win)
274 {
275         if(win>3) {
276                 curarea= areawinar[win];
277                 if(curarea==0) {
278                         printf("error in areawinar %d ,areawinset\n", win);
279                         return;
280                 }
281                 
282                 BIF_SetTheme(curarea);
283                 
284                 switch(curarea->spacetype) {
285                 case SPACE_VIEW3D:
286                         G.vd= curarea->spacedata.first;
287                         break;
288                 case SPACE_IPO:
289                         G.sipo= curarea->spacedata.first;
290                         G.v2d= &G.sipo->v2d;
291                         break;
292                 case SPACE_BUTS:
293                         G.buts= curarea->spacedata.first;
294                         G.v2d= &G.buts->v2d;
295                         break;
296                 case SPACE_SEQ: {
297                         SpaceSeq *sseq= curarea->spacedata.first;
298                         G.v2d= &sseq->v2d;
299                         break;
300                 }
301                 case SPACE_OOPS:
302                         G.soops= curarea->spacedata.first;
303                         G.v2d= &G.soops->v2d;
304                         break;
305                 case SPACE_IMAGE:
306                         G.sima= curarea->spacedata.first;
307                         G.v2d= &G.sima->v2d;
308                         break;
309                 case SPACE_SOUND:
310                         G.ssound= curarea->spacedata.first;
311                         G.v2d= &G.ssound->v2d;
312                         break;
313                 case SPACE_ACTION:
314                         G.saction= curarea->spacedata.first;
315                         G.v2d= &G.saction->v2d;
316                         break;
317                 case SPACE_NLA:
318                         G.snla= curarea->spacedata.first;
319                         G.v2d= &G.snla->v2d;
320                         break;
321                 default:
322                         break;
323                 }
324         }
325         
326         if(win) mywinset(win);
327 }
328
329 #define SCR_BACK 0.55
330 #define SCR_ROUND 12
331
332 void headerbox(ScrArea *area)
333 {
334         float width= area->winx;
335         int active=0;
336
337         glClearColor(SCR_BACK, SCR_BACK, SCR_BACK, 0.0);
338         glClear(GL_COLOR_BUFFER_BIT);
339
340         active= area_is_active_area(area);
341
342         if(active) BIF_ThemeColor(TH_HEADER);
343         else BIF_ThemeColor(TH_HEADERDESEL);
344
345         /* weird values here... is because of window matrix that centres buttons */
346         if(area->headertype==HEADERTOP) {
347                 uiSetRoundBox(3);
348                 uiRoundBoxEmboss(-0.5+area->headbutofs, -10.0, width-1.5+area->headbutofs, HEADERY-2.0, SCR_ROUND, active);
349         }
350         else {
351                 uiSetRoundBox(12);
352                 uiRoundBoxEmboss(-0.5+area->headbutofs, -3.5, width-1.5+area->headbutofs, HEADERY+10, SCR_ROUND, active);
353         }
354         
355         uiSetRoundBox(15);
356 }
357
358 int area_is_active_area(ScrArea *area)
359 {
360         return (g_activearea && area==g_activearea);
361 }
362
363 void scrarea_do_headdraw(ScrArea *area)
364 {
365         if (area->headertype) {
366                 areawinset(area->headwin);
367         
368                 headerbox(area);
369                 
370                 /* we make scissor test slightly smaller not to destroy rounded headers */
371                 glScissor(area->headrct.xmin+5, area->headrct.ymin, area->winx-10, HEADERY);
372                 
373                 switch(area->spacetype) {
374                 case SPACE_FILE:        file_buttons();         break;
375                 case SPACE_INFO:        info_buttons();         break;
376                 case SPACE_VIEW3D:      view3d_buttons();       break;
377                 case SPACE_IPO:         ipo_buttons();          break;
378                 case SPACE_BUTS:        buts_buttons();         break;
379                 case SPACE_SEQ:         seq_buttons();          break;
380                 case SPACE_IMAGE:       image_buttons();        break;
381                 case SPACE_IMASEL:      imasel_buttons();       break;
382                 case SPACE_OOPS:        oops_buttons();         break;
383                 case SPACE_TEXT:        text_buttons();         break;
384                 case SPACE_SCRIPT:script_buttons();             break;
385                 case SPACE_SOUND:       sound_buttons();        break;
386                 case SPACE_ACTION:      action_buttons();       break;
387                 case SPACE_NLA:         nla_buttons();          break;
388                 }
389
390                 //glScissor(area->winrct.xmin, area->winrct.xmax, area->winx, area->winy);
391                 area->head_swap= WIN_BACK_OK;
392         }
393 }
394 void scrarea_do_headchange(ScrArea *area)
395 {
396         float ofs= area->headbutofs;
397
398         if (area->headertype==HEADERDOWN) {
399                 bwin_ortho2(area->headwin, -0.375+ofs, area->headrct.xmax-area->headrct.xmin-0.375+ofs, -3.375, area->headrct.ymax-area->headrct.ymin-3.375);
400         } else if (area->headertype==HEADERTOP) {
401                 bwin_ortho2(area->headwin, -0.375+ofs, area->headrct.xmax-area->headrct.xmin-0.375+ofs, -2.375, area->headrct.ymax-area->headrct.ymin-2.375);
402         }
403 }
404
405
406 static void openheadwin(ScrArea *sa);
407 static void closeheadwin(ScrArea *sa);
408
409 static void scrarea_change_headertype(ScrArea *sa, int newtype) 
410 {
411         sa->headertype= newtype;
412
413         if (!newtype) {
414                 if (sa->headwin) {
415                         uiFreeBlocksWin(&sa->uiblocks, sa->headwin);
416                         closeheadwin(sa);
417                 }
418         } else {
419                 if (!sa->headwin) {
420                         openheadwin(sa);
421                 }
422         }
423
424         testareas();
425         mainqenter(DRAWEDGES, 1);
426         winqueue_break= 1;
427 }
428
429 static void headmenu(ScrArea *sa)
430 {
431         short val= pupmenu("Header %t|Top%x2|Bottom %x1|No Header %x0");
432         
433         if(val> -1) {
434                 scrarea_change_headertype(sa, val);
435         }
436 }
437
438 static void addqueue_ext(short win, unsigned short event, short val, char ascii)
439 {
440         if (win<4 || !areawinar[win]) {
441                 if(win==0) // other win ids are for mainwin & renderwin
442                         printf("bad call to addqueue: %d (%d, %d)\n", win, event, val);
443         } 
444         else {
445                 BWinEvent evt;
446                 evt.event= event;
447                 evt.val= val;
448                 evt.ascii= ascii;
449                 
450                 bwin_qadd(win, &evt);
451         }
452 }
453
454 void addqueue(short win, unsigned short event, short val)
455 {
456         addqueue_ext(win, event, val, 0);
457 }
458
459 void scrarea_queue_winredraw(ScrArea *area)
460 {
461         addqueue(area->win, REDRAW, 1);
462 }
463 void scrarea_queue_headredraw(ScrArea *area)
464 {
465         if (area->headwin) addqueue(area->headwin, REDRAW, 1);
466 }
467 void scrarea_queue_redraw(ScrArea *area)
468 {
469         scrarea_queue_winredraw(area);
470         scrarea_queue_headredraw(area);
471 }
472
473 static void scrollheader(ScrArea *area);
474 static void scrarea_dispatch_header_events(ScrArea *sa)
475 {
476         ScrArea *tempsa;
477         BWinEvent evt;
478         short do_redraw=0, do_change=0;
479         
480         areawinset(sa->headwin);
481         
482         while(bwin_qread(sa->headwin, &evt)) {
483                 if(evt.val) {
484                         if( uiDoBlocks(&curarea->uiblocks, evt.event)!=UI_NOTHING ) evt.event= 0;
485
486                         switch(evt.event) {
487                         case UI_BUT_EVENT:
488                                 do_headerbuttons(evt.val);
489                                 break;
490                         
491                         case LEFTMOUSE:
492                                 if (G.qual & LR_CTRLKEY) {
493                                         window_lower(mainwin);
494                                 } else {
495                                         window_raise(mainwin);
496                                 }
497                                 break;
498
499                         case MIDDLEMOUSE:
500                                 scrollheader(sa);
501                                 break;
502                         case RIGHTMOUSE:
503                                 headmenu(sa);
504                                 break;
505                         case REDRAW:
506                                 do_redraw= 1;
507                                 break;
508                         case CHANGED:
509                                 sa->head_swap= 0;
510                                 do_change= 1;
511                                 do_redraw= 1;
512                                 break;
513                         default:
514                                 if (winqueue_break == 0) {
515                                         scrarea_do_winhandle(sa, &evt);
516                                         if (winqueue_break == 0) areawinset(sa->headwin);
517                                 }
518                         }
519                         
520                         if(winqueue_break) return;
521                 }
522         }
523
524         /* test: does window still exist? */    
525         tempsa= areawinar[sa->headwin];
526         if(tempsa==0) return;
527         
528         /* this functional separation does not work as well as i expected... */
529         if(do_change) scrarea_do_headchange(sa);
530         if(do_redraw) scrarea_do_headdraw(sa);
531 }
532
533 static void scrarea_dispatch_events(ScrArea *sa)
534 {
535         ScrArea *tempsa;
536         BWinEvent evt;
537         short do_redraw=0, do_change=0;
538         
539         if(sa!=curarea || sa->win!=mywinget()) areawinset(sa->win);
540
541         while(bwin_qread(sa->win, &evt)) {
542                 if(evt.event==REDRAW) {
543                         do_redraw= 1;
544                 }
545                 else if(evt.event==CHANGED) {
546                         sa->win_swap= 0;
547                         do_change= 1;
548                         do_redraw= 1;
549                 }
550                 else {
551                         scrarea_do_winhandle(sa, &evt);
552                 }
553                 
554                 if(winqueue_break) return;
555         }
556
557         /* test: does window still exist */     
558         tempsa= areawinar[sa->win];
559         if(tempsa==0) return;
560
561         if (do_change || do_redraw) {
562                 areawinset(sa->win);
563                 if(do_change)
564                         scrarea_do_winchange(curarea);
565                 if(do_redraw)
566                         scrarea_do_windraw(curarea);
567         }
568 }
569
570 /***/
571
572
573 void markdirty_all()
574 {
575         ScrArea *sa;
576         for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
577                 if(sa->win) {
578                         scrarea_queue_winredraw(sa);
579                         sa->win_swap &= ~WIN_FRONT_OK;
580                 }
581                 if(sa->headwin) {
582                         scrarea_queue_headredraw(sa);
583                         sa->head_swap &= ~WIN_FRONT_OK;
584                 }
585         }
586 }
587
588 /* but no redraw! */
589 void markdirty_all_back(void)
590 {
591         ScrArea *sa;
592         
593         for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
594                 if(sa->win) {
595                         sa->win_swap &= ~WIN_BACK_OK;
596                 }
597                 if(sa->headwin) {
598                         sa->head_swap &= ~WIN_BACK_OK;
599                 }
600         }
601         
602         /* if needed; backbuffer selection redraw */
603         if(G.vd) G.vd->flag |= V3D_NEEDBACKBUFDRAW;
604
605 }
606
607 void markdirty_win_back(short winid)
608 {
609         ScrArea *sa= areawinar[winid];
610         if(sa) {
611                 if(sa->win==winid) sa->win_swap &= ~WIN_BACK_OK;
612                 else sa->head_swap &= ~WIN_BACK_OK;
613         }
614 }
615
616
617 int is_allowed_to_change_screen(bScreen *new)
618 {
619         /* not when curscreen is full
620          * not when obedit && old->scene!=new->scene
621          */
622         
623         if(new==0) return 0;
624         if(G.curscreen->full != SCREENNORMAL) return 0;
625         if(curarea->full) return 0;
626         if(G.obedit) {
627                 if(G.curscreen->scene!=new->scene) return 0;
628         }
629         return 1;
630 }
631
632 void splash(void *data, int datasize, char *string)
633 {
634         extern void uiDrawBoxShadow(unsigned char alpha, float minx, float miny, float maxx, float maxy);
635         ImBuf *bbuf;
636         int oldwin;
637         short val;
638
639         bbuf= IMB_ibImageFromMemory((int *)data, datasize, IB_rect);
640
641         if (bbuf) {
642                 oldwin = mywinget();
643                 mywinset(G.curscreen->mainwin);
644                 
645                 if (string) {
646                         int x, y, maxy;
647                         unsigned int *rect;
648                         
649                         rect = bbuf->rect;
650                         maxy = MIN2(bbuf->y, 18);
651
652                         for (y = 0; y < maxy; y++) {
653                                 for (x = 0; x < bbuf->x; x++) {
654                                         *rect = 0xffffffff;
655                                         rect++;
656                                 }
657                         }
658                 }
659                 glDrawBuffer(GL_FRONT);
660                 
661                 uiDrawBoxShadow(200, (prefsizx-bbuf->x)/2, (prefsizy-bbuf->y)/2, (prefsizx+bbuf->x)/2, (prefsizy+bbuf->y)/2);
662                 
663                 glRasterPos2i((prefsizx-bbuf->x)/2, (prefsizy-bbuf->y)/2);      
664                 glDrawPixels(bbuf->x, bbuf->y, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
665
666                 if (string) {
667                         void *font;
668                         int width;                      
669                         
670                         if (BMF_GetStringWidth(font= G.font, string) > bbuf->x)
671                                 if (BMF_GetStringWidth(font= G.fonts, string) > bbuf->x)
672                                         font= G.fontss;
673                         
674                         width= BMF_GetStringWidth(font, string);
675                                                 
676                         glColor3ub(0, 0, 0);
677                         glRasterPos2i((prefsizx-width)/2, (prefsizy-bbuf->y)/2 + 6);
678                         BMF_DrawString(font, string);
679                 }
680
681                 glFlush();
682                 glDrawBuffer(GL_BACK);
683                 
684                 IMB_freeImBuf(bbuf);
685                 
686                 // flush input buffers ....
687                 // this might break some things
688
689                 while (get_mbut()) {
690                         BIF_wait_for_statechange();
691                 }
692                 while(qtest()) {
693                         extern_qread(&val);
694                 }
695
696                 wait_for_event();
697                 
698                 mywinset(oldwin);
699                 markdirty_all();
700                 mainqenter(DRAWEDGES, 1);
701         }
702 }
703
704 static void moveareas(ScrEdge *edge);
705 static void joinarea(ScrArea *sa, ScrEdge *onedge);
706 static void splitarea_interactive(ScrArea *area, ScrEdge *onedge);
707
708 static void screen_edge_edit_event(ScrArea *actarea, ScrEdge *actedge, short evt, short val) {
709         if (val) {
710                         // don't allow users to edit full screens
711                 if (actarea && actarea->full) {
712                         return;
713                 }
714         
715                 if (evt==LEFTMOUSE) {
716                         moveareas(actedge);
717                 } else if (evt==MIDDLEMOUSE || evt==RIGHTMOUSE) {
718                         int edgeop;
719                 
720                         if (!actarea->headertype) {
721                                 edgeop= pupmenu("Split Area|Join Areas|Add Header");
722                         } else {
723                                 edgeop= pupmenu("Split Area|Join Areas|No Header");
724                         }
725
726                         if (edgeop==1) {
727                                 splitarea_interactive(actarea, actedge);
728                         } else if (edgeop==2) {
729                                 joinarea(actarea, actedge);
730                         } else if (edgeop==3) {
731                                 scrarea_change_headertype(actarea, actarea->headertype?0:HEADERDOWN);
732                         }
733                 }
734         }
735 }
736
737 /***/
738
739 void mywindow_init_mainwin(Window *win, int orx, int ory, int sizex, int sizey);
740 void test_scale_screen(bScreen *);
741
742 static void resize_screens(int x, int y, int w, int h) {
743         prefstax= x;
744         prefstay= y;
745         prefsizx= w;
746         prefsizy= h;
747
748         test_scale_screen(G.curscreen);
749         testareas();
750 }
751
752 static void init_mainwin(void)
753 {
754         int orx, ory, sizex, sizey;
755         
756         glEnable(GL_SCISSOR_TEST);
757
758         window_get_position(mainwin, &orx, &ory);
759         window_get_size(mainwin, &sizex, &sizey);
760
761                 /* XXX, temporary stupid fix for minimize at windows */
762         if (!sizex && !sizey) {
763                 return;
764         }
765
766         mywindow_init_mainwin(mainwin, orx, ory, sizex, sizey);
767         resize_screens(orx, ory, sizex, sizey);
768 }
769
770 /***/
771
772 static short afterqueue[MAXQUEUE][3];
773 static int nafterqitems= 0;
774
775 void addafterqueue(short win, unsigned short evt, short val)
776 {
777         if (nafterqitems<MAXQUEUE) {
778                 afterqueue[nafterqitems][0]= win;
779                 afterqueue[nafterqitems][1]= evt;
780                 afterqueue[nafterqitems][2]= val;
781                 nafterqitems++;
782         }
783 }
784
785 static void append_afterqueue(void)
786 {
787         while (nafterqitems) {
788                 short win= afterqueue[nafterqitems-1][0];
789                 unsigned short evt= afterqueue[nafterqitems-1][1];
790                 short val= afterqueue[nafterqitems-1][2];
791
792                 addqueue(win, evt, val);
793                 
794                 nafterqitems--;
795         }
796 }
797
798 /* check for event in afterqueue, used in force_draw in space.c */
799 int afterqtest(short win, unsigned short evt)
800 {
801         int a;
802         
803         for(a=0; a<nafterqitems; a++) {
804                 if(afterqueue[a][0]==win && afterqueue[a][1]==evt) return 1;
805         }
806         return 0;
807 }
808
809
810 static char ext_load_str[256]= {0, 0};
811 void add_readfile_event(char *filename)
812 {       
813         mainqenter(LOAD_FILE, 1);
814         strcpy(ext_load_str, filename);
815         BLI_convertstringcode(ext_load_str, G.sce, G.scene->r.cfra);
816 }
817
818 static short ext_reshape= 0, ext_redraw=0, ext_inputchange=0, ext_mousemove=0;
819
820 static void flush_extqd_events(void) {
821         if (ext_inputchange) {
822                 mainqenter(INPUTCHANGE, ext_inputchange);
823         } else if (ext_reshape) {
824                 mainqenter(RESHAPE, ext_redraw);
825         } else if (ext_redraw) {
826                 mainqenter(REDRAW, ext_redraw);
827         } else if (ext_mousemove) {
828                 short mouse[2];
829                 
830                 getmouseco_sc(mouse);
831                 
832                 mainqenter(MOUSEX, mouse[0]);
833                 mainqenter(MOUSEY, mouse[1]);
834         }
835         
836         ext_inputchange= ext_reshape= ext_redraw= ext_mousemove= 0;
837 }
838
839 unsigned short qtest(void)
840 {
841         if (!mainqtest()) {
842                 winlay_process_events(0);
843         }
844         
845         return mainqtest();
846 }
847
848         /* return true if events are waiting anywhere */
849 int anyqtest(void)
850 {
851         ScrArea *sa;
852
853         if (nafterqitems || qtest()) return 1;
854
855         for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
856                 if (bwin_qtest(sa->win)) return 1;
857                 if (sa->headwin && bwin_qtest(sa->headwin)) return 1;
858         }
859
860         return 0;
861 }
862
863 static void wait_for_event(void)
864 {
865         while (!mainqtest()) {
866                 winlay_process_events(1);
867         }
868 }
869
870 unsigned short screen_qread(short *val, char *ascii)
871 {
872         unsigned short event;
873
874         wait_for_event();
875
876         event= mainqread(val, ascii);
877         
878         if(event==RIGHTSHIFTKEY || event==LEFTSHIFTKEY) {
879                 if(*val) G.qual |= LR_SHIFTKEY;
880                 else G.qual &= ~LR_SHIFTKEY;
881         }
882         else if(event==RIGHTALTKEY || event==LEFTALTKEY) {
883                 if(*val) G.qual |= LR_ALTKEY;
884                 else G.qual &= ~LR_ALTKEY;
885         }
886         else if(event==RIGHTCTRLKEY || event==LEFTCTRLKEY) {
887                 if(*val) G.qual |= LR_CTRLKEY;
888                 else G.qual &= ~LR_CTRLKEY;
889         }
890         else if(event==COMMANDKEY) {            // OSX
891                 if(*val) G.qual |= LR_COMMANDKEY;
892                 else G.qual &= ~LR_COMMANDKEY;
893         }
894
895         return event;
896 }
897
898 unsigned short extern_qread_ext(short *val, char *ascii)
899 {
900         /* stores last INPUTCHANGE and last REDRAW */
901         unsigned short event;
902         
903         event= screen_qread(val, ascii);
904         if(event==RESHAPE) ext_reshape= *val;
905         else if(event==REDRAW) ext_redraw= *val;
906         else if(event==INPUTCHANGE) ext_inputchange= *val;
907         else if(event==MOUSEY || event==MOUSEX) ext_mousemove= 1;
908         else if((G.qual & (LR_CTRLKEY|LR_ALTKEY)) && event==F3KEY) {
909                 if(*val) BIF_screendump(0);
910         }
911
912         return event;
913 }
914 unsigned short extern_qread(short *val)
915 {
916         char ascii;
917         return extern_qread_ext(val, &ascii);
918 }
919
920 int blender_test_break(void)
921 {
922         if (!G.background) {
923                 static double ltime= 0;
924                 double curtime= PIL_check_seconds_timer();
925
926                         /* only check for breaks every 10 milliseconds
927                          * if we get called more often.
928                          */
929                 if ((curtime-ltime)>.001) {
930                         ltime= curtime;
931
932                         while(qtest()) {
933                                 short val;
934                                 if (extern_qread(&val) == ESCKEY) {
935                                         G.afbreek= 1;
936                                 }
937                         }
938                 }
939         }
940
941         return (G.afbreek==1);
942 }
943
944 void reset_autosave(void) {
945         window_set_timer(mainwin, U.savetime*60*1000, AUTOSAVE_FILE);
946 }
947
948 static void screen_dispatch_events(void) {
949         int events_remaining= 1;
950         ScrArea *sa;
951
952         window_make_active(mainwin); // added it here instead of screenmain (ton)
953
954         while (events_remaining) {
955                 events_remaining= 0;
956                                 
957                 winqueue_break= 0;
958                 for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
959                                 /* first check header, then rest. Header sometimes has initialization code */
960                         if (sa->headwin && bwin_qtest(sa->headwin)) {
961                                 scrarea_dispatch_header_events(sa);
962                                 events_remaining= 1;
963                         }
964                         if (winqueue_break) break;
965
966                         if (bwin_qtest(sa->win)) {
967                                 scrarea_dispatch_events(sa);
968                                 events_remaining= 1;
969                         }
970                         if (winqueue_break) break;
971                 }
972
973                 if (winqueue_break) break;
974         }
975
976         if (dodrawscreen) {
977                 drawscreen();
978                 dodrawscreen= 0;
979         }
980         
981         screen_swapbuffers();
982 }
983
984 static ScrArea *screen_find_area_for_pt(bScreen *sc, short *mval) 
985 {
986         ScrArea *sa;
987         
988         /* hotspot area of 1 pixel extra */
989         
990         for (sa= sc->areabase.first; sa; sa= sa->next) {
991                 if( sa->totrct.xmin + 1 < mval[0] )
992                         if( sa->totrct.ymin + 1 < mval[1] )
993                                 if( sa->totrct.xmax - 1 > mval[0] )
994                                         if( sa->totrct.ymax - 1 > mval[1] )
995                                                 return sa;
996         }
997         return NULL;
998 }
999
1000 /* ugly yah, will disappear on better event system */
1001 /* is called from interface.c after button events */
1002 static char delayed_undo_name[64];
1003 void screen_delayed_undo_push(char *name)
1004 {
1005         strncpy(delayed_undo_name, name, 63);
1006         mainqenter(UNDOPUSH, 1);
1007 }
1008
1009 void screenmain(void)
1010 {
1011         int has_input= 1;
1012         int firsttime = 1;
1013         int onload_script = 0;
1014
1015         window_make_active(mainwin);
1016         
1017         while (1) {
1018                 unsigned short event;
1019                 short val, towin;
1020                 char ascii;
1021
1022                 flush_extqd_events();
1023                 if (nafterqitems && !qtest()) {
1024                         append_afterqueue();
1025                         event= val= ascii= 0;
1026                 } else {
1027                         event= screen_qread(&val, &ascii);
1028                 }
1029                 
1030                 // window_make_active(mainwin); // (only for inputchange, removed, (ton))
1031
1032                 if (event==INPUTCHANGE) {
1033                         window_make_active(mainwin);
1034                         has_input= val;
1035                 }
1036                 
1037                         /* If the main window is active, find the current active ScrArea
1038                          * underneath the mouse cursor, updating the headers & cursor for
1039                          * the appropriate internal window if things have changed.
1040                          * 
1041                          * If the main window is not active, deactivate the internal 
1042                          * window.
1043                          */
1044                 if (has_input || g_activearea==NULL || G.curscreen->winakt) {
1045                         ScrArea *newactarea;
1046                         int newactwin;
1047                         short mval[2];
1048
1049                         getmouseco_sc(mval);
1050                         newactarea= screen_find_area_for_pt(G.curscreen, mval);                 
1051
1052                         if (newactarea) {
1053                                 if (BLI_in_rcti(&newactarea->headrct, mval[0], mval[1])) {
1054                                         newactwin= newactarea->headwin;
1055                                 } else {
1056                                         newactwin= newactarea->win;
1057                                 }
1058                         } else {
1059                                 newactwin= 0;
1060                         }
1061
1062                         if (newactarea && (newactarea != g_activearea)) {
1063                                 if (g_activearea) scrarea_queue_headredraw(g_activearea);
1064                                 scrarea_queue_headredraw(newactarea);
1065                                 set_cursor(newactarea->cursor);
1066                                 g_activearea= newactarea;
1067                         }
1068                         /* when you move mouse from header to window, buttons can remain hilited otherwise */
1069                         if(newactwin != G.curscreen->winakt) {
1070                                 if (g_activearea) scrarea_queue_headredraw(g_activearea);
1071                         }
1072                         G.curscreen->winakt= newactwin;
1073                         
1074                         if (G.curscreen->winakt) {
1075                                 areawinset(G.curscreen->winakt);
1076                                 set_cursor(choose_cursor(g_activearea));
1077                         }
1078                 } 
1079                 else {
1080                         if (g_activearea) {
1081                                 scrarea_queue_headredraw(g_activearea);
1082                         }
1083                         g_activearea= NULL;
1084                         G.curscreen->winakt= 0;
1085                 }
1086
1087                 towin= 0;
1088                 if (event==WINCLOSE) {
1089                         exit_usiblender();
1090                 } 
1091                 else if (event==DRAWEDGES) {
1092                         dodrawscreen= 1;
1093                 }
1094                 else if (event==RESHAPE) {
1095                         init_mainwin();
1096                         markdirty_all();
1097                         dodrawscreen= 1;
1098                 }
1099                 else if (event==REDRAW) {
1100                         markdirty_all();
1101                         dodrawscreen= 1;
1102                 }
1103                 else if( event==UNDOPUSH) {
1104                         BIF_undo_push(delayed_undo_name);
1105                 }
1106                 else if (event==AUTOSAVE_FILE) {
1107                         BIF_write_autosave();
1108                 }
1109                 else if (event==LOAD_FILE) {
1110                         BIF_read_file(ext_load_str);
1111                         sound_initialize_sounds();
1112                 }
1113                 else if ((event==ONLOAD_SCRIPT) && BPY_has_onload_script()) {
1114                         /* event queued in setup_app_data() in blender.c, where G.f is checked */
1115                         onload_script = 1;
1116                         firsttime = 1; /* see last 'if' in this function */
1117                 }
1118                 else {
1119                         towin= 1;
1120                 }
1121
1122                 if (!g_activearea) {
1123                         towin= 0;
1124                 }
1125                 else if (!G.curscreen->winakt) {
1126                         ScrEdge *actedge;
1127                         short mval[2];
1128
1129                         getmouseco_sc(mval);
1130                         actedge= screen_find_active_scredge(G.curscreen, mval);
1131
1132                         if (actedge) {
1133                                 if (scredge_is_horizontal(actedge)) {
1134                                         set_cursor(CURSOR_Y_MOVE);
1135                                 } else {
1136                                         set_cursor(CURSOR_X_MOVE);
1137                                 }
1138
1139                                 screen_edge_edit_event(g_activearea, actedge, event, val);
1140                         } else {
1141                                 set_cursor(CURSOR_STD);
1142                         }
1143                         
1144                         towin= 0;
1145                 }
1146                 else if (event==QKEY) {
1147                         if((G.obedit && G.obedit->type==OB_FONT && g_activearea->spacetype==SPACE_VIEW3D)||g_activearea->spacetype==SPACE_TEXT||g_activearea->spacetype==SPACE_SCRIPT);
1148                         else {
1149                                 if(val) {
1150                                         if(okee("Quit Blender"))
1151                                                 exit_usiblender();
1152                                 }
1153                                 towin= 0;
1154                         }
1155                 }
1156                 else if (event==ZKEY) {
1157                         if(val && G.qual==(LR_ALTKEY|LR_SHIFTKEY|LR_CTRLKEY)) {
1158                                 extern void set_debug_swapbuffers_ovveride(bScreen *sc, int mode);
1159
1160                                 int which= pupmenu("Swapbuffers%t|Simple|Debug|DebugSwap|Redraw|Default|KillSwap");
1161                                         
1162                                 switch (which) {
1163                                 case 1: set_debug_swapbuffers_ovveride(G.curscreen, 's'); break;
1164                                 case 2: set_debug_swapbuffers_ovveride(G.curscreen, 'd'); break;
1165                                 case 3: set_debug_swapbuffers_ovveride(G.curscreen, 'f'); break;
1166                                 case 4: set_debug_swapbuffers_ovveride(G.curscreen, 'r'); break;
1167                                 case 5: set_debug_swapbuffers_ovveride(G.curscreen, 0); break;
1168                                 case 6: 
1169                                         if (g_activearea) {
1170                                                 g_activearea->head_swap= 0;
1171                                                 g_activearea->win_swap= 0;
1172                                         }
1173                                         break;
1174                                 }
1175                                 towin= 0;
1176                         }
1177                 }
1178                 else if (event==SPACEKEY) {
1179                         if((G.obedit && G.obedit->type==OB_FONT && g_activearea->spacetype==SPACE_VIEW3D)||g_activearea->spacetype==SPACE_TEXT||g_activearea->spacetype==SPACE_SCRIPT);
1180                         else {
1181                                 if(val) toolbox_n();
1182                                 towin= 0;
1183                         }
1184                 }
1185                 else if(ELEM(event, LEFTARROWKEY, RIGHTARROWKEY)) {
1186                         if(val && (G.qual & LR_CTRLKEY)) {
1187                                 bScreen *sc= (event==LEFTARROWKEY)?G.curscreen->id.prev:G.curscreen->id.next;
1188                                 if(is_allowed_to_change_screen(sc)) setscreen(sc);
1189                                 g_activearea= NULL;
1190                                 towin= 0;
1191                         }
1192                 }
1193                 else if(ELEM(event, UPARROWKEY, DOWNARROWKEY)) {
1194                         if(val && (G.qual & LR_CTRLKEY)) {
1195                                 area_fullscreen();
1196                                 g_activearea= NULL;
1197                                 towin= 0;
1198                         }
1199                 }
1200
1201                 if (towin && event) {
1202                         if (blenderqread(event, val))
1203                                 addqueue_ext(G.curscreen->winakt, event, val, ascii);
1204                 }
1205
1206                         /* only process subwindow queue's once the
1207                          * main queue has been emptyied.
1208                          */
1209                 event= qtest();
1210                 if (event==0 || event==EXECUTE) {
1211                         screen_dispatch_events();
1212                 }
1213
1214                 /* Bizar hack. The event queue has mutated... */
1215                 if ( (firsttime) && (event == 0) ) {
1216
1217                         if (onload_script) {
1218                                 /* OnLoad scriptlink */
1219                                 BPY_do_pyscript(&G.scene->id, SCRIPT_ONLOAD);
1220                                 onload_script = 0;
1221                         }
1222                         else if (G.fileflags & G_FILE_AUTOPLAY) {
1223                                 // SET AUTOPLAY in G.flags for
1224                                 // other fileloads
1225
1226                                 G.flags |= G_FLAGS_AUTOPLAY;
1227                                 area_autoplayscreen();
1228
1229                                 // Let The Games Begin
1230                                 // fake a 'p' keypress
1231
1232                                 mainqenter(PKEY, 1);
1233                         } else {
1234                                 extern char datatoc_splash_jpg[];
1235                                 extern int datatoc_splash_jpg_size;
1236
1237                                 //if (! ((G.main->versionfile >= G.version)
1238                                 //       || G.save_over)) {
1239                                         splash((void *)datatoc_splash_jpg,
1240                                                datatoc_splash_jpg_size, NULL);
1241                                 //}
1242                         }
1243                         firsttime = 0;
1244                 }
1245         }
1246 }
1247
1248 #ifdef _WIN32   // FULLSCREEN
1249 void mainwindow_toggle_fullscreen(int fullscreen){
1250         if (fullscreen) U.uiflag |= USER_FLIPFULLSCREEN;
1251         else U.uiflag &= ~USER_FLIPFULLSCREEN;
1252
1253         window_toggle_fullscreen(mainwin, fullscreen);
1254 }
1255 #endif
1256
1257 void mainwindow_raise(void) {
1258         window_raise(mainwin);
1259 }
1260
1261 void mainwindow_make_active(void) {
1262         window_make_active(mainwin);
1263 }
1264
1265 void mainwindow_close(void) {
1266         window_destroy(mainwin);
1267         mainwin= NULL;
1268 }
1269
1270 void mainwindow_set_filename_to_title(char *filename) {
1271         char str[FILE_MAXDIR + FILE_MAXFILE];
1272         char dir[FILE_MAXDIR];
1273         char file[FILE_MAXFILE];
1274
1275         BLI_split_dirfile(filename, dir, file);
1276
1277         if(BLI_streq(file, ".B.blend") || filename[0] =='\0')
1278                 sprintf(str, "Blender");
1279         else
1280                 sprintf(str, "Blender [%s]", filename);
1281
1282         window_set_title(mainwin, str);
1283 }
1284
1285 /* *********  AREAS  ************* */
1286
1287 void setprefsize(int stax, int stay, int sizx, int sizy)
1288 {
1289         int scrwidth, scrheight;
1290         
1291         winlay_get_screensize(&scrwidth, &scrheight);
1292                 
1293         if(stax<0) stax= 0;
1294         if(stay<0) stay= 0;
1295         if(sizx<320) sizx= 320;
1296         if(sizy<256) sizy= 256;
1297
1298         if(stax+sizx>scrwidth) {
1299                 fprintf(stderr," width prob \n");
1300                 sizx= scrwidth-stax;
1301                 }
1302
1303         if(stay+sizy>scrheight) {
1304                 fprintf(stderr," height prob \n");
1305                 sizy= scrheight-stay;
1306                 }
1307
1308 /* #ifdef __APPLE__ */
1309 /*      if(stay+sizy>scrheight-22) sizy= scrheight-22-stay;     // 22 = topbar  */
1310 /* #endif*/
1311
1312         if(sizx<320 || sizy<256) {
1313                 printf("ERROR: illegal prefsize\n");
1314                 return;
1315         }
1316         
1317         prefstax= stax;
1318         prefstay= stay;
1319         prefsizx= sizx;
1320         prefsizy= sizy;
1321
1322         start_maximized= 0;
1323 }
1324
1325
1326 static ScrVert *screen_addvert(bScreen *sc, short x, short y)
1327 {
1328         ScrVert *sv= MEM_callocN(sizeof(ScrVert), "addscrvert");
1329         sv->vec.x= x;
1330         sv->vec.y= y;
1331         
1332         BLI_addtail(&sc->vertbase, sv);
1333         return sv;
1334 }
1335
1336 static void sortscrvert(ScrVert **v1, ScrVert **v2)
1337 {
1338         ScrVert *tmp;
1339         
1340         if ((long)*v1 > (long)*v2) {
1341                 tmp= *v1;
1342                 *v1= *v2;
1343                 *v2= tmp;       
1344         }
1345 }
1346
1347 static ScrEdge *screen_addedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
1348 {
1349         ScrEdge *se= MEM_callocN(sizeof(ScrEdge), "addscredge");
1350
1351         sortscrvert(&v1, &v2);
1352         se->v1= v1;
1353         se->v2= v2;
1354         
1355         BLI_addtail(&sc->edgebase, se);
1356         return se;
1357 }
1358
1359 static ScrEdge *screen_findedge(bScreen *sc, ScrVert *v1, ScrVert *v2)
1360 {
1361         ScrEdge *se;
1362
1363         sortscrvert(&v1, &v2);
1364         for (se= sc->edgebase.first; se; se= se->next)
1365                 if(se->v1==v1 && se->v2==v2)
1366                         return se;
1367
1368         return NULL;
1369 }
1370
1371 static void removedouble_scrverts(void)
1372 {
1373         ScrVert *v1, *verg;
1374         ScrEdge *se;
1375         ScrArea *sa;
1376         
1377         verg= G.curscreen->vertbase.first;
1378         while(verg) {
1379                 if(verg->newv==0) {     /* !!! */
1380                         v1= verg->next;
1381                         while(v1) {
1382                                 if(v1->newv==0) {       /* !?! */
1383                                         if(v1->vec.x==verg->vec.x && v1->vec.y==verg->vec.y) {
1384                                                 /* printf("doublevert\n"); */
1385                                                 v1->newv= verg;
1386                                         }
1387                                 }
1388                                 v1= v1->next;
1389                         }
1390                 }
1391                 verg= verg->next;
1392         }
1393         
1394         /* replace pointers in edges and faces */
1395         se= G.curscreen->edgebase.first;
1396         while(se) {
1397                 if(se->v1->newv) se->v1= se->v1->newv;
1398                 if(se->v2->newv) se->v2= se->v2->newv;
1399                 /* edges changed: so.... */
1400                 sortscrvert(&(se->v1), &(se->v2));
1401                 se= se->next;
1402         }
1403         sa= G.curscreen->areabase.first;
1404         while(sa) {
1405                 if(sa->v1->newv) sa->v1= sa->v1->newv;
1406                 if(sa->v2->newv) sa->v2= sa->v2->newv;
1407                 if(sa->v3->newv) sa->v3= sa->v3->newv;
1408                 if(sa->v4->newv) sa->v4= sa->v4->newv;
1409                 sa= sa->next;
1410         }
1411         
1412         /* remove */
1413         verg= G.curscreen->vertbase.first;
1414         while(verg) {
1415                 v1= verg->next;
1416                 if(verg->newv) {
1417                         BLI_remlink(&G.curscreen->vertbase, verg);
1418                         MEM_freeN(verg);
1419                 }
1420                 verg= v1;
1421         }
1422         
1423 }
1424
1425 static void removenotused_scrverts(void)
1426 {
1427         ScrVert *sv, *svn;
1428         ScrEdge *se;
1429
1430         /* we assume edges are ok */
1431         
1432         se= G.curscreen->edgebase.first;
1433         while(se) {
1434                 se->v1->flag= 1;
1435                 se->v2->flag= 1;
1436                 se= se->next;
1437         }
1438         
1439         sv= G.curscreen->vertbase.first;
1440         while(sv) {
1441                 svn= sv->next;
1442                 if(sv->flag==0) {
1443                         BLI_remlink(&G.curscreen->vertbase, sv);
1444                         MEM_freeN(sv);
1445                 }
1446                 else sv->flag= 0;
1447                 sv= svn;
1448         }
1449 }
1450
1451 static void removedouble_scredges(void)
1452 {
1453         ScrEdge *verg, *se, *sn;
1454         
1455         /* compare */
1456         verg= G.curscreen->edgebase.first;
1457         while(verg) {
1458                 se= verg->next;
1459                 while(se) {
1460                         sn= se->next;
1461                         if(verg->v1==se->v1 && verg->v2==se->v2) {
1462                                 BLI_remlink(&G.curscreen->edgebase, se);
1463                                 MEM_freeN(se);
1464                         }
1465                         se= sn;
1466                 }
1467                 verg= verg->next;
1468         }
1469 }
1470
1471 static void removenotused_scredges(void)
1472 {
1473         ScrEdge *se, *sen;
1474         ScrArea *sa;
1475         int a=0;
1476         
1477         /* sets flags when edge is used in area */
1478         sa= G.curscreen->areabase.first;
1479         while(sa) {
1480                 se= screen_findedge(G.curscreen, sa->v1, sa->v2);
1481                 if(se==0) printf("error: area %d edge 1 bestaat niet\n", a);
1482                 else se->flag= 1;
1483                 se= screen_findedge(G.curscreen, sa->v2, sa->v3);
1484                 if(se==0) printf("error: area %d edge 2 bestaat niet\n", a);
1485                 else se->flag= 1;
1486                 se= screen_findedge(G.curscreen, sa->v3, sa->v4);
1487                 if(se==0) printf("error: area %d edge 3 bestaat niet\n", a);
1488                 else se->flag= 1;
1489                 se= screen_findedge(G.curscreen, sa->v4, sa->v1);
1490                 if(se==0) printf("error: area %d edge 4 bestaat niet\n", a);
1491                 else se->flag= 1;
1492                 sa= sa->next;
1493                 a++;
1494         }
1495         se= G.curscreen->edgebase.first;
1496         while(se) {
1497                 sen= se->next;
1498                 if(se->flag==0) {
1499                         BLI_remlink(&G.curscreen->edgebase, se);
1500                         MEM_freeN(se);
1501                 }
1502                 else se->flag= 0;
1503                 se= sen;
1504         }
1505 }
1506
1507 void calc_arearcts(ScrArea *sa)
1508 {
1509
1510         if(sa->v1->vec.x>0) sa->totrct.xmin= sa->v1->vec.x+1;
1511         else sa->totrct.xmin= sa->v1->vec.x;
1512         if(sa->v4->vec.x<G.curscreen->sizex-1) sa->totrct.xmax= sa->v4->vec.x-1;
1513         else sa->totrct.xmax= sa->v4->vec.x;
1514         
1515         if(sa->v1->vec.y>0) sa->totrct.ymin= sa->v1->vec.y+1;
1516         else sa->totrct.ymin= sa->v1->vec.y;
1517         if(sa->v2->vec.y<G.curscreen->sizey-1) sa->totrct.ymax= sa->v2->vec.y-1;
1518         else sa->totrct.ymax= sa->v2->vec.y;
1519         
1520         sa->winrct= sa->totrct;
1521         sa->headrct= sa->totrct;
1522         if(sa->headertype) {
1523                 if(sa->headertype==HEADERDOWN) {
1524                         sa->headrct.ymax= sa->headrct.ymin+HEADERY;
1525                         sa->winrct.ymin= sa->headrct.ymax+1;
1526                 }
1527                 else if(sa->headertype==HEADERTOP) {
1528                         sa->headrct.ymin= sa->headrct.ymax-HEADERY;
1529                         sa->winrct.ymax= sa->headrct.ymin-1;
1530                 }
1531         }
1532         else {
1533                 sa->headrct.ymax= sa->headrct.ymin;
1534         }
1535         if(sa->winrct.ymin>sa->winrct.ymax) sa->winrct.ymin= sa->winrct.ymax;
1536         
1537         /* for speedup */
1538         sa->winx= sa->winrct.xmax-sa->winrct.xmin+1;
1539         sa->winy= sa->winrct.ymax-sa->winrct.ymin+1;
1540 }
1541
1542 static void openheadwin(ScrArea *sa)
1543 {
1544         sa->headwin= myswinopen(G.curscreen->mainwin,
1545                 sa->headrct.xmin, sa->headrct.xmax, sa->headrct.ymin, sa->headrct.ymax);
1546
1547         glMatrixMode(GL_MODELVIEW);
1548         
1549         areawinar[sa->headwin]= sa;     /* oterwise addqueue does not work */
1550         addqueue(sa->headwin, CHANGED, 1);
1551 }
1552
1553 static void openareawin(ScrArea *sa)
1554 {
1555         sa->win= myswinopen(G.curscreen->mainwin, 
1556                 sa->winrct.xmin, sa->winrct.xmax, sa->winrct.ymin, sa->winrct.ymax);
1557
1558         areawinar[sa->win]= sa; /* otherwise addqueue does not work */
1559         addqueue(sa->win, CHANGED, 1);
1560 }
1561
1562 static void closeheadwin(ScrArea *sa)
1563 {
1564         if(sa->headwin) mywinclose(sa->headwin);
1565         sa->headwin= 0;
1566 }
1567
1568 static void closeareawin(ScrArea *sa)
1569 {
1570         uiFreeBlocksWin(&sa->uiblocks, sa->win);
1571         
1572         if(sa->win) mywinclose(sa->win);
1573         sa->win= 0;
1574 }
1575
1576 static void del_area(ScrArea *sa)
1577 {
1578         closeareawin(sa);
1579         closeheadwin(sa);
1580
1581         freespacelist(&sa->spacedata);
1582         
1583         uiFreeBlocks(&sa->uiblocks);
1584         uiFreePanels(&sa->panels);
1585         
1586         if(sa==curarea) curarea= 0;
1587         if(sa==g_activearea) g_activearea= 0;
1588 }
1589
1590 /* sa2 to sa1 */
1591 static void copy_areadata(ScrArea *sa1, ScrArea *sa2)
1592 {
1593         Panel *pa1, *pa2, *patab;
1594         
1595         sa1->headertype= sa2->headertype;
1596         sa1->spacetype= sa2->spacetype;
1597         Mat4CpyMat4(sa1->winmat, sa2->winmat);
1598         
1599         freespacelist(&sa1->spacedata);
1600         duplicatespacelist(sa1, &sa1->spacedata, &sa2->spacedata);
1601
1602         BLI_freelistN(&sa1->panels);
1603         duplicatelist(&sa1->panels, &sa2->panels);
1604         
1605         /* copy pointers */
1606         pa1= sa1->panels.first;
1607         while(pa1) {
1608                 
1609                 patab= sa1->panels.first;
1610                 pa2= sa2->panels.first;
1611                 while(patab) {
1612                         if( pa1->paneltab == pa2) {
1613                                 pa1->paneltab = patab;
1614                                 break;
1615                         }
1616                         patab= patab->next;
1617                         pa2= pa2->next;
1618                 }
1619                 pa1= pa1->next;
1620         }
1621 }
1622
1623 static ScrArea *screen_addarea(bScreen *sc, ScrVert *v1, ScrVert *v2, ScrVert *v3, ScrVert *v4, short headertype, short spacetype)
1624 {
1625         ScrArea *sa= MEM_callocN(sizeof(ScrArea), "addscrarea");
1626         sa->cursor= CURSOR_STD;
1627         sa->v1= v1;
1628         sa->v2= v2;
1629         sa->v3= v3;
1630         sa->v4= v4;
1631         sa->headertype= headertype;
1632         sa->spacetype= spacetype;
1633
1634         calc_arearcts(sa);
1635
1636         if (sa->headertype) openheadwin(sa);
1637         openareawin(sa);
1638
1639         BLI_addtail(&sc->areabase, sa);
1640         return sa;
1641 }
1642
1643 static int rcti_eq(rcti *a, rcti *b) {
1644         return ((a->xmin==b->xmin && a->xmax==b->xmax) &&
1645                         (a->ymin==b->ymin && a->ymax==b->ymax));
1646 }
1647
1648 static void testareas(void)
1649 {
1650         ScrArea *sa, *next;
1651
1652         /* test for header, if removed, or moved */
1653         /* test for window, if removed, or moved */
1654         
1655         sa= G.curscreen->areabase.first;
1656         while(sa) {
1657                 rcti oldhr= sa->headrct;
1658                 rcti oldwr= sa->winrct;
1659                 
1660                 next= sa->next;
1661                 
1662                 calc_arearcts(sa);
1663                 
1664                 /* ilegally scaled down area.... */
1665                 if(sa->totrct.xmin>=sa->totrct.xmax || sa->totrct.ymin>=sa->totrct.ymax) {
1666                         del_area(sa);
1667                         BLI_remlink(&G.curscreen->areabase, sa);
1668                         MEM_freeN(sa);
1669                         printf("Warning, removed zero sized window from screen %s\n", G.curscreen->id.name+2);
1670                 }
1671                 else {
1672                                 /* test header */
1673                         if (sa->headwin) {
1674                                 if (!rcti_eq(&oldhr, &sa->headrct)) {
1675                                         mywinposition(sa->headwin, sa->headrct.xmin, sa->headrct.xmax, sa->headrct.ymin, sa->headrct.ymax);
1676                                         addqueue(sa->headwin, CHANGED, 1);
1677                                 }
1678                                         
1679                                 if(sa->headbutlen<sa->winx) {
1680                                         sa->headbutofs= 0;
1681                                         addqueue(sa->headwin, CHANGED, 1);
1682                                 }
1683                                 else if(sa->headbutofs+sa->winx > sa->headbutlen) {
1684                                         sa->headbutofs= sa->headbutlen-sa->winx;
1685                                         addqueue(sa->headwin, CHANGED, 1);
1686                                 }
1687                         }
1688
1689                         if (!rcti_eq(&oldwr, &sa->winrct)) {
1690                                 mywinposition(sa->win, sa->winrct.xmin, sa->winrct.xmax, sa->winrct.ymin, sa->winrct.ymax);
1691                                 addqueue(sa->win, CHANGED, 1);
1692                         }
1693                 }
1694                 sa= next;
1695         }
1696         
1697                 /* remake global windowarray */
1698         memset(areawinar, 0, sizeof(void *)*MAXWIN);
1699         for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1700                 areawinar[sa->headwin]= sa;
1701                 areawinar[sa->win]= sa;
1702         }
1703         
1704                 /* test if winakt is OK */      
1705         if( areawinar[G.curscreen->winakt]==0) G.curscreen->winakt= 0;
1706 }
1707
1708 static ScrArea *test_edge_area(ScrArea *sa, ScrEdge *se)
1709 {
1710         /* test if edge is in area, if not, 
1711            then find an area that has it */
1712   
1713         ScrEdge *se1=0, *se2=0, *se3=0, *se4=0;
1714         
1715         if(sa) {
1716                 se1= screen_findedge(G.curscreen, sa->v1, sa->v2);
1717                 se2= screen_findedge(G.curscreen, sa->v2, sa->v3);
1718                 se3= screen_findedge(G.curscreen, sa->v3, sa->v4);
1719                 se4= screen_findedge(G.curscreen, sa->v4, sa->v1);
1720         }
1721         if(se1!=se && se2!=se && se3!=se && se4!=se) {
1722                 
1723                 sa= G.curscreen->areabase.first;
1724                 while(sa) {
1725                         /* a bit optimise? */
1726                         if(se->v1==sa->v1 || se->v1==sa->v2 || se->v1==sa->v3 || se->v1==sa->v4) {
1727                                 se1= screen_findedge(G.curscreen, sa->v1, sa->v2);
1728                                 se2= screen_findedge(G.curscreen, sa->v2, sa->v3);
1729                                 se3= screen_findedge(G.curscreen, sa->v3, sa->v4);
1730                                 se4= screen_findedge(G.curscreen, sa->v4, sa->v1);
1731                                 if(se1==se || se2==se || se3==se || se4==se) return sa;
1732                         }
1733                         sa= sa->next;
1734                 }
1735         }
1736
1737         return sa;      /* is null when not find */
1738 }
1739
1740 ScrArea *closest_bigger_area(void)
1741 {
1742         ScrArea *sa, *big=0;
1743         float cent[3], vec[3],len, len1, len2, len3, dist=1000;
1744         short mval[2];
1745         
1746         getmouseco_sc(mval);
1747         
1748         cent[0]= mval[0];
1749         cent[1]= mval[1];
1750         cent[2]= vec[2]= 0;
1751
1752         sa= G.curscreen->areabase.first;
1753         while(sa) {
1754                 if(sa!=curarea) {
1755                         if(sa->winy>=curarea->winy) {
1756                         
1757                                 /* mimimum of the 4 corners */
1758                                 vec[0]= sa->v1->vec.x; vec[1]= sa->v1->vec.y;
1759                                 len= VecLenf(vec, cent);
1760                                 vec[0]= sa->v2->vec.x; vec[1]= sa->v2->vec.y;
1761                                 len1= VecLenf(vec, cent);
1762                                 vec[0]= sa->v3->vec.x; vec[1]= sa->v3->vec.y;
1763                                 len2= VecLenf(vec, cent);
1764                                 vec[0]= sa->v4->vec.x; vec[1]= sa->v4->vec.y;
1765                                 len3= VecLenf(vec, cent);
1766                                 
1767                                 len= MIN4(len, len1, len2, len3);
1768                                 
1769                                 /* plus centre */
1770                                 vec[0]= (sa->v2->vec.x+sa->v3->vec.x)/2;
1771                                 vec[1]= (sa->v1->vec.y+sa->v2->vec.y)/2;
1772
1773                                 len+= 0.5*VecLenf(vec, cent);
1774                                 
1775                                 /* min size */
1776                                 len-= sa->winy+sa->winx;
1777                                 
1778                                 if(len<dist) {
1779                                         dist= len;
1780                                         big= sa;
1781                                 }
1782                         }
1783                 }
1784                 sa= sa->next;
1785         }
1786         
1787         if(big) return big;
1788         else return curarea;
1789 }
1790
1791 /* ************ SCREEN MANAGEMENT ************** */
1792
1793 static int statechanged= 0;
1794 void BIF_wait_for_statechange(void)
1795 {
1796         if (!statechanged) {
1797                         /* Safety, don't wait more than 0.1 seconds */
1798                 double stime= PIL_check_seconds_timer();
1799                 while (!statechanged) {
1800                         winlay_process_events(1);
1801                         if ((PIL_check_seconds_timer()-stime)>0.1) break;
1802                 }
1803                 statechanged= 0;
1804         }
1805         else PIL_sleep_ms(3);   /* statechanged can be set '1' while holding mousebutton, causing locks */
1806
1807 }
1808 void getmouse(short *mval)
1809 {
1810         winlay_process_events(0);
1811         window_get_mouse(mainwin, mval);
1812 }
1813 short get_qual(void)
1814 {
1815         winlay_process_events(0);
1816         return window_get_qual(mainwin);
1817 }
1818 short get_mbut(void)
1819 {
1820         winlay_process_events(0);
1821         return window_get_mbut(mainwin);
1822 }
1823
1824 void add_to_mainqueue(Window *win, void *user_data, short evt, short val, char ascii)
1825 {
1826
1827         statechanged= 1;
1828
1829         /*  accept the extended ascii set (ton) */
1830         if( !val || ascii<32 ) {
1831                 ascii= '\0';
1832         }
1833
1834         mainqenter_ext(evt, val, ascii);
1835 }
1836
1837 /* ScrVert ordering in a ScrArea:
1838
1839 2---------3
1840 |         |
1841 |         |
1842 1---------4
1843   
1844 */
1845
1846 static bScreen *addscreen(char *name)           /* use setprefsize() if you want something else than a full windpw */
1847 {
1848         /* this function sets variabele G.curscreen,
1849          * that global is about used everywhere!
1850          */
1851         bScreen *sc;
1852         ScrVert *sv1, *sv2, *sv3, *sv4;
1853         short startx, starty, endx, endy;       
1854         
1855         sc= G.curscreen= alloc_libblock(&G.main->screen, ID_SCR, name);
1856
1857         if (!prefsizx) {
1858                 prefstax= 0;
1859                 prefstay= 0;
1860                 
1861                 winlay_get_screensize(&prefsizx, &prefsizy);
1862         }
1863
1864         startx= prefstax;
1865         starty= prefstay;
1866         endx= prefstax+prefsizx-1;
1867         endy= prefstay+prefsizy-1;
1868
1869         sc->startx= startx;     sc->starty= starty;
1870         sc->endx= endx; sc->endy= endy;
1871         sc->sizex= sc->endx-sc->startx+1;
1872         sc->sizey= sc->endy-sc->starty+1;
1873         
1874         sc->scene= G.scene;
1875         
1876         if (!mainwin) {
1877 #ifdef _WIN32   // FULLSCREEN
1878                 if (G.windowstate == G_WINDOWSTATE_FULLSCREEN)
1879                         mainwin= window_open("Blender", sc->startx, sc->starty, sc->sizex, sc->sizey, G_WINDOWSTATE_FULLSCREEN);
1880                 else
1881                         mainwin= window_open("Blender", sc->startx, sc->starty, sc->sizex, sc->sizey, start_maximized);
1882 #else
1883                 mainwin= window_open("Blender", sc->startx, sc->starty, sc->sizex, sc->sizey, start_maximized);
1884 #endif
1885                 
1886                 if (!mainwin) {
1887                         printf("ERROR: Unable to open Blender window\n");
1888                         exit(1);
1889                 }
1890                 
1891                 window_set_handler(mainwin, add_to_mainqueue, NULL);
1892                 init_mainwin();
1893                 mywinset(1);
1894         
1895                 /* for visual speed, but still needed? */
1896                 glClearColor(.55, .55, .55, 0.0);
1897                 glClear(GL_COLOR_BUFFER_BIT);
1898                 window_swap_buffers(mainwin);
1899                 
1900                 warp_pointer(sc->sizex/2,  sc->sizey/2);
1901                 
1902                 mainqenter(REDRAW, 1);
1903         }
1904
1905         sc->mainwin= 1;
1906         
1907         sv1= screen_addvert(sc, 0, 0);
1908         sv2= screen_addvert(sc, 0, sc->endy-sc->starty);
1909         sv3= screen_addvert(sc, sc->sizex-1, sc->sizey-1);
1910         sv4= screen_addvert(sc, sc->sizex-1, 0);
1911         
1912         screen_addedge(sc, sv1, sv2);
1913         screen_addedge(sc, sv2, sv3);
1914         screen_addedge(sc, sv3, sv4);
1915         screen_addedge(sc, sv4, sv1);
1916
1917         screen_addarea(sc, sv1, sv2, sv3, sv4, HEADERDOWN, SPACE_INFO);
1918         
1919         G.curscreen= sc;
1920
1921         return sc;
1922 }
1923
1924 void setscreen(bScreen *sc)
1925 {
1926         bScreen *sc1;
1927         ScrArea *sa;
1928         short mval[2];
1929         
1930         if(sc->full) {                          /* find associated full */
1931                 sc1= G.main->screen.first;
1932                 while(sc1) {
1933                         sa= sc1->areabase.first;
1934                         if(sa->full==sc) {
1935                                 sc= sc1;
1936                                 break;
1937                         }
1938                         sc1= sc1->id.next;
1939                 }
1940                 if(sc1==0) printf("setscreen error\n");
1941         }
1942
1943         /* de-activate G.curscreen */
1944         if (G.curscreen && G.curscreen != sc) {
1945                 sa= G.curscreen->areabase.first;
1946                 while(sa) {
1947                         if(sa->win) mywinclose(sa->win);
1948                         sa->win= 0;
1949                         if(sa->headwin) mywinclose(sa->headwin);
1950                         sa->headwin= 0;
1951                         
1952                         uiFreeBlocks(&sa->uiblocks);
1953                         
1954                         sa= sa->next;
1955                 }               
1956         }
1957         else if(G.curscreen) markdirty_all();   /* at least redraw */
1958
1959         if (G.curscreen != sc) {
1960                 mywinset(sc->mainwin);
1961         }
1962         
1963         G.curscreen= sc;
1964
1965         for (sa= sc->areabase.first; sa; sa= sa->next) {
1966                         /* XXX, fixme zr */
1967 /*              if (sa->win || sa->headwin) */
1968 /*                      printf("error in setscreen (win): %d, %d\n", sa->win, sa->headwin); */
1969                 if (!sa->win)
1970                         openareawin(sa);
1971                 if (!sa->headwin && sa->headertype)
1972                         openheadwin(sa);
1973         }
1974
1975         /* recalculate winakt */
1976         getmouseco_sc(mval);
1977
1978         test_scale_screen(sc);
1979         testareas();
1980         
1981         for(sa= sc->areabase.first; sa; sa= sa->next) {
1982                 SpaceLink *sl;
1983
1984                 for(sl= sa->spacedata.first; sl; sl= sl->next) {
1985                         sl->area= sa;
1986
1987                         if(sl->spacetype==SPACE_OOPS) {
1988                                 SpaceOops *soops= (SpaceOops *) sl;
1989
1990                                 /* patch for old files */
1991                                 if(soops->v2d.cur.xmin==soops->v2d.cur.xmax) {
1992                                         init_v2d_oops(sa, soops);
1993                                 }
1994                         }
1995                         else if(sl->spacetype==SPACE_BUTS) {
1996                                 SpaceButs *sbuts= (SpaceButs *)sl;
1997                                 sbuts->re_align= 1;             // force an align call, maybe new panels were added, also for after file reading
1998                         }
1999                 }
2000                 
2001                 sa->cursor= CURSOR_STD;
2002         }
2003         
2004         G.scene= sc->scene;
2005         countall();
2006         
2007         G.curscreen->winakt= 0;
2008         curarea= sc->areabase.first;
2009         
2010         mainqenter(DRAWEDGES, 1);
2011         dodrawscreen= 1;                /* patch! even gets lost,,,? */
2012
2013         winqueue_break= 1;              /* means leave queue everywhere */
2014 }
2015
2016 static void splitarea(ScrArea *sa, char dir, float fac);
2017
2018 void area_fullscreen(void)      /* with curarea */
2019 {
2020         /* this function toggles: if area is full then the parent will be restored */
2021         bScreen *sc, *oldscreen;
2022         ScrArea *newa, *old;
2023         short headertype, fulltype;
2024         
2025         if(curarea->full) {
2026                 sc= curarea->full;      /* the old screen */
2027                 fulltype = sc->full;
2028
2029                 // refuse to go out of SCREENAUTOPLAY as long as G_FLAGS_AUTOPLAY
2030                 // is set
2031
2032                 if (fulltype != SCREENAUTOPLAY || (G.flags & G_FLAGS_AUTOPLAY) == 0) {
2033                         sc->full= 0;
2034                 
2035                         /* find old area */
2036                         old= sc->areabase.first;
2037                         while(old) {
2038                                 if(old->full) break;
2039                                 old= old->next;
2040                         }
2041                         if(old==0) {error("something wrong in areafullscreen"); return;}
2042                 
2043                         if (fulltype == SCREENAUTOPLAY) {
2044                                 // in autoplay screens the headers are disabled by 
2045                                 // default. So use the old headertype instead
2046                                 headertype = old->headertype;
2047                         } else {
2048                                 // normal fullscreen. Use current headertype
2049                                 headertype = curarea->headertype;
2050                         }
2051
2052                         copy_areadata(old, curarea);
2053                         old->headertype = headertype;
2054
2055                         old->full= 0;
2056                 
2057                         unlink_screen(G.curscreen);
2058                         free_libblock(&G.main->screen, G.curscreen);
2059                         G.curscreen= NULL;
2060
2061                         setscreen(sc);
2062                 }
2063                 
2064         }
2065         else {
2066                 /* is there only 1 area? */
2067                 if(G.curscreen->areabase.first==G.curscreen->areabase.last) return;
2068                 if(curarea->spacetype==SPACE_INFO) return;
2069                 
2070                 G.curscreen->full = SCREENFULL;
2071                 
2072                 old= curarea;           
2073                 oldscreen= G.curscreen;
2074                 sc= addscreen("temp");          /* this sets G.curscreen */
2075
2076                 splitarea( (ScrArea *)sc->areabase.first, 'h', 0.99);
2077                 newa= sc->areabase.first;
2078                 newspace(newa->next, SPACE_INFO);
2079                 
2080                 curarea= old;
2081                 G.curscreen= oldscreen; /* needed because of setscreen */
2082                 
2083                 /* copy area */
2084                 copy_areadata(newa, curarea);
2085                 
2086                 curarea->full= oldscreen;
2087                 newa->full= oldscreen;
2088                 newa->next->full= oldscreen;
2089                 
2090                 setscreen(sc);
2091                 wich_cursor(newa);
2092         }
2093 }
2094
2095 static void area_autoplayscreen(void)
2096 {
2097         bScreen *sc, *oldscreen;
2098         ScrArea *newa, *old, *sa;
2099         
2100         if (curarea->full) {
2101                 area_fullscreen();
2102         }
2103
2104         if (curarea->full == NULL) { 
2105                 sa = G.curscreen->areabase.first;
2106                 while (sa) {
2107                         if (sa->spacetype == SPACE_VIEW3D) {
2108                                 break;
2109                         }
2110                         sa= sa->next;
2111                 }
2112
2113                 if (sa) {
2114                         areawinset(sa->win);
2115                         G.curscreen->full = SCREENAUTOPLAY;
2116                         
2117                         old= curarea;           
2118                         oldscreen= G.curscreen;
2119                         sc= addscreen("temp");          /* this sets G.curscreen */
2120         
2121                         newa= sc->areabase.first;
2122                         
2123                         curarea= old;
2124                         G.curscreen= oldscreen; /* because of setscreen */
2125                         
2126                         /* copy area settings */
2127                         copy_areadata(newa, curarea);
2128                         newa->headertype= 0;
2129                         
2130                         curarea->full= oldscreen;
2131                         newa->full= oldscreen;
2132         
2133                         setscreen(sc);
2134                         wich_cursor(newa);
2135                 }
2136         }
2137 }
2138
2139 static void copy_screen(bScreen *to, bScreen *from)
2140 {
2141         ScrVert *s1, *s2;
2142         ScrEdge *se;
2143         ScrArea *sa, *saf;
2144
2145         /* free 'to' */
2146         free_screen(to);
2147         winqueue_break= 1;      /* leave queues everywhere */
2148         
2149         duplicatelist(&to->vertbase, &from->vertbase);
2150         duplicatelist(&to->edgebase, &from->edgebase);
2151         duplicatelist(&to->areabase, &from->areabase);
2152         
2153         s1= from->vertbase.first;
2154         s2= to->vertbase.first;
2155         while(s1) {
2156                 s1->newv= s2;
2157                 s2= s2->next;
2158                 s1= s1->next;
2159         }
2160         se= to->edgebase.first;
2161         while(se) {
2162                 se->v1= se->v1->newv;
2163                 se->v2= se->v2->newv;
2164                 sortscrvert(&(se->v1), &(se->v2));
2165                 se= se->next;
2166         }
2167
2168         sa= to->areabase.first;
2169         saf= from->areabase.first;
2170         while(sa) {
2171                 sa->v1= sa->v1->newv;
2172                 sa->v2= sa->v2->newv;
2173                 sa->v3= sa->v3->newv;
2174                 sa->v4= sa->v4->newv;
2175                 sa->win= 0;
2176                 sa->headwin= 0;
2177                 
2178                 sa->spacedata.first= sa->spacedata.last= NULL;
2179                 sa->uiblocks.first= sa->uiblocks.last= NULL;
2180                 sa->panels.first= sa->panels.last= NULL;
2181                 copy_areadata(sa, saf);
2182                 
2183                 sa= sa->next;
2184                 saf= saf->next;
2185         }
2186         
2187         /* put at zero (needed?) */
2188         s1= from->vertbase.first;
2189         while(s1) {
2190                 s1->newv= 0;
2191                 s1= s1->next;
2192         }
2193 }
2194
2195 void duplicate_screen(void)
2196 {
2197         bScreen *sc, *oldscreen;
2198         
2199         if(G.curscreen->full != SCREENNORMAL) return;
2200         
2201         /* make new screen: */
2202
2203         oldscreen= G.curscreen;
2204         sc= addscreen(oldscreen->id.name+2);    /* this sets G.curscreen */
2205         copy_screen(sc, oldscreen);
2206
2207         G.curscreen= oldscreen;
2208         setscreen(sc);
2209
2210 }
2211
2212
2213 /* ************ END SCREEN MANAGEMENT ************** */
2214 /* ************  JOIN/SPLIT/MOVE ************** */
2215
2216 static void joinarea(ScrArea *sa, ScrEdge *onedge)
2217 {
2218         ScrArea *sa2;
2219         ScrArea *up=0, *down=0, *right=0, *left=0;
2220         ScrEdge *setest;
2221         short val=0;
2222         
2223         sa= test_edge_area(sa, onedge);
2224         if (sa==0) return;
2225
2226         /* which edges? */
2227         /* find directions with same edge */
2228         sa2= G.curscreen->areabase.first;
2229         while(sa2) {
2230                 if(sa2 != sa) {
2231                         setest= screen_findedge(G.curscreen, sa2->v1, sa2->v2);
2232                         if(onedge==setest) right= sa2;
2233                         setest= screen_findedge(G.curscreen, sa2->v2, sa2->v3);
2234                         if(onedge==setest) down= sa2;
2235                         setest= screen_findedge(G.curscreen, sa2->v3, sa2->v4);
2236                         if(onedge==setest) left= sa2;
2237                         setest= screen_findedge(G.curscreen, sa2->v4, sa2->v1);
2238                         if(onedge==setest) up= sa2;
2239                 }
2240                 sa2= sa2->next;
2241         }
2242         
2243         sa2= 0;
2244         setest= 0;
2245         
2246         if(left) val++;
2247         if(up) val++;
2248         if(right) val++;
2249         if(down) val++;
2250         
2251         if(val==0) return;
2252         else if(val==1) {
2253                 if(left) sa2= left;
2254                 else if(up) sa2= up;
2255                 else if(right) sa2= right;
2256                 else if(down) sa2= down;
2257         }
2258         
2259         
2260         if(sa2) {
2261                 /* new area is old sa */
2262                 if(sa2==left) {
2263                         sa->v1= sa2->v1;
2264                         sa->v2= sa2->v2;
2265                         screen_addedge(G.curscreen, sa->v2, sa->v3);
2266                         screen_addedge(G.curscreen, sa->v1, sa->v4);
2267                 }
2268                 else if(sa2==up) {
2269                         sa->v2= sa2->v2;
2270                         sa->v3= sa2->v3;
2271                         screen_addedge(G.curscreen, sa->v1, sa->v2);
2272                         screen_addedge(G.curscreen, sa->v3, sa->v4);
2273                 }
2274                 else if(sa2==right) {
2275                         sa->v3= sa2->v3;
2276                         sa->v4= sa2->v4;
2277                         screen_addedge(G.curscreen, sa->v2, sa->v3);
2278                         screen_addedge(G.curscreen, sa->v1, sa->v4);
2279                 }
2280                 else if(sa2==down) {
2281                         sa->v1= sa2->v1;
2282                         sa->v4= sa2->v4;
2283                         screen_addedge(G.curscreen, sa->v1, sa->v2);
2284                         screen_addedge(G.curscreen, sa->v3, sa->v4);
2285                 }
2286         
2287                 /* remove edge and area */
2288                 /* remlink(&G.curscreen->edgebase, setest); */
2289                 /* freeN(setest); */
2290                 del_area(sa2);
2291                 BLI_remlink(&G.curscreen->areabase, sa2);
2292                 MEM_freeN(sa2);
2293                 
2294                 removedouble_scredges();
2295                 removenotused_scredges();
2296                 removenotused_scrverts();       /* as last */
2297                 
2298                 testareas();
2299                 mainqenter(DRAWEDGES, 1);
2300                         /* test cursor en inputwindow */
2301                 mainqenter(MOUSEY, -1);         
2302         }
2303 }
2304
2305 static short testsplitpoint(ScrArea *sa, char dir, float fac)
2306 /* return 0: no split possible */
2307 /* else return (integer) screencoordinate split point */
2308 {
2309         short x, y;
2310         
2311         /* area big enough? */
2312         if(sa->v4->vec.x- sa->v1->vec.x <= 2*AREAMINX) return 0;
2313         if(sa->v2->vec.y- sa->v1->vec.y <= 2*AREAMINY) return 0;
2314
2315         /* to be sure */
2316         if(fac<0.0) fac= 0.0;
2317         if(fac>1.0) fac= 1.0;
2318         
2319         if(dir=='h') {
2320                 y= sa->v1->vec.y+ fac*(sa->v2->vec.y- sa->v1->vec.y);
2321                 
2322                 if(sa->v2->vec.y==G.curscreen->sizey-1 && sa->v2->vec.y- y < HEADERY) 
2323                         y= sa->v2->vec.y- HEADERY;
2324
2325                 else if(sa->v1->vec.y==0 && y- sa->v1->vec.y < HEADERY)
2326                         y= sa->v1->vec.y+ HEADERY;
2327
2328                 else if(y- sa->v1->vec.y < AREAMINY) y= sa->v1->vec.y+ AREAMINY;
2329                 else if(sa->v2->vec.y- y < AREAMINY) y= sa->v2->vec.y- AREAMINY;
2330                 else y-= (y % AREAGRID);
2331
2332                 return y;
2333         }
2334         else {
2335                 x= sa->v1->vec.x+ fac*(sa->v4->vec.x- sa->v1->vec.x);
2336                 if(x- sa->v1->vec.x < AREAMINX) x= sa->v1->vec.x+ AREAMINX;
2337                 else if(sa->v4->vec.x- x < AREAMINX) x= sa->v4->vec.x- AREAMINX;
2338                 else x-= (x % AREAGRID);
2339
2340                 return x;
2341         }
2342 }
2343
2344 static void splitarea(ScrArea *sa, char dir, float fac)
2345 {
2346         bScreen *sc;
2347         ScrArea *newa=NULL;
2348         ScrVert *sv1, *sv2;
2349         short split;
2350         
2351         if(sa==0) return;
2352         
2353         split= testsplitpoint(sa, dir, fac);
2354         if(split==0) return;
2355         
2356         sc= G.curscreen;
2357         
2358         areawinset(sa->win);
2359         
2360         if(dir=='h') {
2361                 /* new vertices */
2362                 sv1= screen_addvert(sc, sa->v1->vec.x, split);
2363                 sv2= screen_addvert(sc, sa->v4->vec.x, split);
2364                 
2365                 /* new edges */
2366                 screen_addedge(sc, sa->v1, sv1);
2367                 screen_addedge(sc, sv1, sa->v2);
2368                 screen_addedge(sc, sa->v3, sv2);
2369                 screen_addedge(sc, sv2, sa->v4);
2370                 screen_addedge(sc, sv1, sv2);
2371                 
2372                 /* new areas: top */
2373                 newa= screen_addarea(sc, sv1, sa->v2, sa->v3, sv2, sa->headertype, sa->spacetype);
2374                 copy_areadata(newa, sa);
2375
2376                 /* area below */
2377                 sa->v2= sv1;
2378                 sa->v3= sv2;
2379                 
2380         }
2381         else {
2382                 /* new vertices */
2383                 sv1= screen_addvert(sc, split, sa->v1->vec.y);
2384                 sv2= screen_addvert(sc, split, sa->v2->vec.y);
2385                 
2386                 /* new edges */
2387                 screen_addedge(sc, sa->v1, sv1);
2388                 screen_addedge(sc, sv1, sa->v4);
2389                 screen_addedge(sc, sa->v2, sv2);
2390                 screen_addedge(sc, sv2, sa->v3);
2391                 screen_addedge(sc, sv1, sv2);
2392                 
2393                 /* new areas: left */
2394                 newa= screen_addarea(sc, sa->v1, sa->v2, sv2, sv1, sa->headertype, sa->spacetype);
2395                 copy_areadata(newa, sa);
2396
2397                 /* area right */
2398                 sa->v1= sv1;            
2399                 sa->v2= sv2;
2400         }
2401         
2402         if(sa->spacetype==SPACE_BUTS) {
2403                 addqueue(sa->win, UI_BUT_EVENT, B_BUTSHOME);
2404                 addqueue(newa->win, UI_BUT_EVENT, B_BUTSHOME);
2405         }
2406         
2407         /* remove double vertices en edges */
2408         removedouble_scrverts();
2409         removedouble_scredges();
2410         removenotused_scredges();
2411         
2412         mainqenter(DRAWEDGES, 1);
2413         dodrawscreen= 1;                /* patch! event gets lost,,,? */
2414         testareas();
2415 }
2416
2417 static void scrarea_draw_splitpoint(ScrArea *sa, char dir, float fac)
2418 {
2419         int split= testsplitpoint(sa, dir, fac);
2420
2421         if (split) {
2422                 if(dir=='h') {
2423                         sdrawXORline(sa->totrct.xmin, split, sa->totrct.xmax, split);
2424                         sdrawXORline(sa->totrct.xmin, split-1, sa->totrct.xmax, split-1);
2425                 } else {
2426                         sdrawXORline(split, sa->totrct.ymin, split, sa->totrct.ymax);
2427                         sdrawXORline(split-1, sa->totrct.ymin, split-1, sa->totrct.ymax);
2428                 }
2429         }
2430 }
2431
2432 static void splitarea_interactive(ScrArea *area, ScrEdge *onedge)
2433 {
2434         ScrArea *sa= area;
2435         float fac= 0.0;
2436         unsigned short event;
2437         short ok= 0, val, split = 0, mval[2], mvalo[2], first= 1;
2438         char dir;
2439         
2440         if(sa->win==0) return;
2441         if(sa->full) return;
2442
2443         dir= scredge_is_horizontal(onedge)?'v':'h';
2444         
2445         mywinset(G.curscreen->mainwin);
2446         /* should already have a good matrix */
2447         glReadBuffer(GL_FRONT);
2448         glDrawBuffer(GL_FRONT);
2449
2450         /* keep track of grid and minsize */
2451         while(ok==0) {
2452                 getmouseco_sc(mval);
2453                 
2454                 if (first || mval[0]!=mvalo[0] || mval[1]!=mvalo[1]) {
2455                         if (!first) {
2456                                 scrarea_draw_splitpoint(sa, dir, fac);
2457                         }
2458
2459                         if(dir=='h') {
2460                                 fac= mval[1]- (sa->v1->vec.y);
2461                                 fac/= sa->v2->vec.y- sa->v1->vec.y;
2462                         } else {
2463                                 fac= mval[0]- sa->v1->vec.x;
2464                                 fac/= sa->v4->vec.x- sa->v1->vec.x;
2465                         }
2466
2467                         split= testsplitpoint(sa, dir, fac);
2468                         if (split) {
2469                                 scrarea_draw_splitpoint(sa, dir, fac);
2470                         } else {
2471                                 ok= -1;
2472                         }
2473
2474                         mvalo[0]= mval[0];
2475                         mvalo[1]= mval[1];
2476                         first= 0;                       
2477                 }
2478                 
2479                 event= extern_qread(&val);
2480                 if(val && event==LEFTMOUSE) {
2481                         if(dir=='h') {
2482                                 fac= split- (sa->v1->vec.y);
2483                                 fac/= sa->v2->vec.y- sa->v1->vec.y;
2484                         }
2485                         else {
2486                                 fac= split- sa->v1->vec.x;
2487                                 fac/= sa->v4->vec.x- sa->v1->vec.x;
2488                         }
2489                         ok= 1;
2490                 }
2491                 if(val && (event==ESCKEY || event==RIGHTMOUSE)) {
2492                         ok= -1;
2493                 }
2494                 glFlush();
2495         }
2496
2497         if (!first) {
2498                 scrarea_draw_splitpoint(sa, dir, fac);
2499                 glFlush();
2500         }
2501         glReadBuffer(GL_BACK);
2502         glDrawBuffer(GL_BACK);
2503
2504         if(ok==1) {
2505                 splitarea(sa, dir, fac);
2506                 mainqenter(DRAWEDGES, 1);
2507                 dodrawscreen= 1;                /* patch! event gets lost,,,? */
2508         }
2509 }
2510
2511 View3D *find_biggest_view3d(void)
2512 {
2513         ScrArea *sa= find_biggest_area_of_type(SPACE_VIEW3D);
2514         
2515         if (sa) {
2516                 return (View3D*) sa->spacedata.first;
2517         } else {
2518                 return NULL;
2519         }
2520 }
2521
2522 ScrArea *find_biggest_area_of_type(int spacecode)
2523 {
2524         ScrArea *sa, *biggest= NULL;
2525         int bigsize= 0;
2526         
2527         for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
2528                 if (spacecode==0 || sa->spacetype==spacecode) {
2529                         int x= sa->v3->vec.x - sa->v1->vec.x;
2530                         int y= sa->v3->vec.y - sa->v1->vec.y;
2531                         int size= x*x + y*y;
2532                 
2533                         if (!biggest || size>bigsize) {
2534                                 biggest= sa;
2535                                 bigsize= size;
2536                         }
2537                 }
2538         }
2539         
2540         return biggest;
2541 }
2542
2543 ScrArea *find_biggest_area(void)
2544 {
2545         return find_biggest_area_of_type(0);
2546 }
2547
2548 static void select_connected_scredge(bScreen *sc, ScrEdge *edge)
2549 {
2550         ScrEdge *se;
2551         ScrVert *sv;
2552         int oneselected;
2553         char dir;
2554         
2555         /* select connected, only in the right direction */
2556         /* 'dir' is the direction of EDGE */
2557
2558         if(edge->v1->vec.x==edge->v2->vec.x) dir= 'v';
2559         else dir= 'h';
2560
2561         sv= sc->vertbase.first;
2562         while(sv) {
2563                 sv->flag= 0;
2564                 sv= sv->next;
2565         }
2566
2567         edge->v1->flag= 1;
2568         edge->v2->flag= 1;
2569
2570         oneselected= 1;
2571         while(oneselected) {
2572                 se= sc->edgebase.first;
2573                 oneselected= 0;
2574                 while(se) {
2575                         if(se->v1->flag + se->v2->flag==1) {
2576                                 if(dir=='h') if(se->v1->vec.y==se->v2->vec.y) {
2577                                         se->v1->flag= se->v2->flag= 1;
2578                                         oneselected= 1;
2579                                 }
2580                                 if(dir=='v') if(se->v1->vec.x==se->v2->vec.x) {
2581                                         se->v1->flag= se->v2->flag= 1;
2582                                         oneselected= 1;
2583                                 }
2584                         }
2585                         se= se->next;
2586                 }
2587         }
2588 }
2589
2590 void test_scale_screen(bScreen *sc)
2591 /* test if screen vertices should be scaled */
2592 /* also check offset */
2593 {
2594         ScrVert *sv=0;
2595         ScrEdge *se;
2596         ScrArea *sa, *san;
2597         int yval;
2598         float facx, facy, tempf, min[2], max[2];
2599
2600         sc->startx= prefstax;
2601         sc->starty= prefstay;
2602         sc->endx= prefstax+prefsizx-1;
2603         sc->endy= prefstay+prefsizy-1;
2604
2605         /* calculate size */
2606         sv= sc->vertbase.first;
2607         min[0]= min[1]= 0.0;
2608         max[0]= sc->sizex;
2609         max[1]= sc->sizey;
2610         while(sv) {
2611                 min[0]= MIN2(min[0], sv->vec.x);
2612                 min[1]= MIN2(min[1], sv->vec.y);
2613                 max[0]= MAX2(max[0], sv->vec.x);
2614                 max[1]= MAX2(max[1], sv->vec.y);
2615                 sv= sv->next;
2616         }
2617
2618         /* always make 0.0 left under */
2619         sv= sc->vertbase.first;
2620         while(sv) {
2621                 sv->vec.x -= min[0];
2622                 sv->vec.y -= min[1];
2623                 sv= sv->next;
2624         }
2625         
2626
2627         sc->sizex= max[0]-min[0];
2628         sc->sizey= max[1]-min[1];
2629
2630         if(sc->sizex!= prefsizx || sc->sizey!= prefsizy) {
2631                 facx= prefsizx;
2632                 facx/= (float)sc->sizex;
2633                 facy= prefsizy;
2634                 facy/= (float)sc->sizey;
2635
2636                 /* make sure it fits! */
2637                 sv= sc->vertbase.first;
2638                 while(sv) {
2639                         tempf= ((float)sv->vec.x)*facx;
2640                         sv->vec.x= (short)(tempf+0.5);
2641                         sv->vec.x+= AREAGRID-1;
2642                         sv->vec.x-=  (sv->vec.x % AREAGRID); 
2643                         
2644                         CLAMP(sv->vec.x, 0, prefsizx);
2645
2646                         tempf= ((float)sv->vec.y )*facy;
2647                         sv->vec.y= (short)(tempf+0.5);
2648                         sv->vec.y+= AREAGRID-1;
2649                         sv->vec.y-=  (sv->vec.y % AREAGRID); 
2650                         
2651                         CLAMP(sv->vec.y, 0, prefsizy);
2652
2653                         sv= sv->next;
2654                 }
2655                 
2656                 sc->sizex= prefsizx;
2657                 sc->sizey= prefsizy;
2658         }
2659
2660         /* test for collapsed areas. This could happen in some blender version... */
2661         sa= sc->areabase.first;
2662         while(sa) {
2663                 san= sa->next;
2664                 if(sa->v1==sa->v2 || sa->v3==sa->v4 || sa->v2==sa->v3) {
2665                         del_area(sa);
2666                         BLI_remlink(&sc->areabase, sa);
2667                         MEM_freeN(sa);
2668                 }
2669                 sa= san;
2670         }
2671
2672         /* make each window at least HEADERY high */
2673         sa= sc->areabase.first;
2674         while(sa) {
2675
2676                 if(sa->v1->vec.y+HEADERY > sa->v2->vec.y) {
2677                         /* lower edge */
2678                         se= screen_findedge(sc, sa->v4, sa->v1);
2679                         if(se && sa->v1!=sa->v2 ) {
2680                                 select_connected_scredge(sc, se);
2681                                 
2682                                 /* all selected vertices get the right offset */
2683                                 yval= sa->v2->vec.y-HEADERY;
2684                                 sv= sc->vertbase.first;
2685                                 while(sv) {
2686                                         /* if is a collapsed area */
2687                                         if(sv!=sa->v2 && sv!=sa->v3) {
2688                                                 if(sv->flag) sv->vec.y= yval;
2689                                         }
2690                                         sv= sv->next;
2691                                 }
2692                         }
2693                 }
2694
2695                 sa= sa->next;
2696         }
2697
2698 }
2699
2700 static void draw_front_xor_dirdist_line(char dir, int dist, int start, int end)
2701 {
2702         if (dir=='h') {
2703                 sdrawXORline(start, dist, end, dist);
2704                 sdrawXORline(start, dist+1, end, dist+1);
2705         } else {
2706                 sdrawXORline(dist, start, dist, end);
2707                 sdrawXORline(dist+1, start, dist+1, end);
2708         }
2709 }
2710
2711 static void moveareas(ScrEdge *edge)
2712 {
2713         ScrVert *v1;
2714         ScrArea *sa;
2715         short mvalo[2];
2716         short edge_start, edge_end, edge_position;
2717         short bigger, smaller, headery, areaminy;
2718         int delta, doit;
2719         char dir;
2720         
2721         if(edge->border) return;
2722
2723         dir= scredge_is_horizontal(edge)?'h':'v';
2724         
2725         select_connected_scredge(G.curscreen, edge);
2726
2727         edge_position= (dir=='h')?edge->v1->vec.y:edge->v1->vec.x;
2728         edge_start= 10000;
2729         edge_end= -10000;
2730         for (v1= G.curscreen->vertbase.first; v1; v1= v1->next) {
2731                 if (v1->flag) {
2732                         if (dir=='h') {
2733                                 edge_start= MIN2(edge_start, v1->vec.x);
2734                                 edge_end= MAX2(edge_end, v1->vec.x);
2735                         } else {
2736                                 edge_start= MIN2(edge_start, v1->vec.y);
2737                                 edge_end= MAX2(edge_end, v1->vec.y);
2738                         }
2739                 }
2740         }
2741
2742         /* now all verices with 'flag==1' are the ones that can be moved. */
2743         /* we check all areas and test for free space with MINSIZE */
2744         bigger= smaller= 10000;
2745         sa= G.curscreen->areabase.first;
2746         while(sa) {
2747                 if(dir=='h') {  /* if top or down edge selected, test height */
2748                         if(sa->headertype) {
2749                                 headery= HEADERY;
2750                                 areaminy= AREAMINY;
2751                         }
2752                         else {
2753                                 headery= 0;
2754                                 areaminy= AREAMINY; /*areaminy= EDGEWIDTH;*/
2755                         }
2756
2757                         if(sa->v1->flag && sa->v4->flag) {
2758                                 int y1;
2759                                 if(sa->v2->vec.y==G.curscreen->sizey-1) /* top edge */
2760                                         y1= sa->v2->vec.y - sa->v1->vec.y-headery-EDGEWIDTH;
2761                                 else 
2762                                         y1= sa->v2->vec.y - sa->v1->vec.y-areaminy;
2763                                 bigger= MIN2(bigger, y1);
2764                         }
2765                         else if(sa->v2->flag && sa->v3->flag) {
2766                                 int y1;
2767                                 if(sa->v1->vec.y==0)    /* bottom edge */
2768                                         y1= sa->v2->vec.y - sa->v1->vec.y-headery-EDGEWIDTH;
2769                                 else
2770                                         y1= sa->v2->vec.y - sa->v1->vec.y-areaminy;
2771                                 smaller= MIN2(smaller, y1);
2772                         }
2773                 }
2774                 else {  /* if left or right edge selected, test width */
2775                         if(sa->v1->flag && sa->v2->flag) {
2776                                 int x1= sa->v4->vec.x - sa->v1->vec.x-AREAMINX;
2777                                 bigger= MIN2(bigger, x1);
2778                         }
2779                         else if(sa->v3->flag && sa->v4->flag) {
2780                                 int x1= sa->v4->vec.x - sa->v1->vec.x-AREAMINX;
2781                                 smaller= MIN2(smaller, x1);
2782                         }
2783                 }
2784                 sa= sa->next;
2785         }
2786         
2787         mywinset(G.curscreen->mainwin);
2788
2789         glReadBuffer(GL_FRONT);
2790         glDrawBuffer(GL_FRONT);
2791
2792         doit= delta= 0;
2793         getmouseco_sc(mvalo);
2794         draw_front_xor_dirdist_line(dir, edge_position+delta, edge_start, edge_end);
2795
2796         while (!doit) {
2797                 short val;
2798                 unsigned short event= extern_qread(&val);
2799                 
2800                 if (event==MOUSEY) {
2801                         short mval[2];
2802                         
2803                         getmouseco_sc(mval);
2804                         
2805                         draw_front_xor_dirdist_line(dir, edge_position+delta, edge_start, edge_end);
2806
2807                         delta= (dir=='h')?(mval[1]-mvalo[1]):(mval[0]-mvalo[0]);
2808                         delta= CLAMPIS(delta, -smaller, bigger);
2809                         
2810                         draw_front_xor_dirdist_line(dir, edge_position+delta, edge_start, edge_end);
2811                         glFlush();
2812                 } 
2813                 else if (event==LEFTMOUSE) {
2814                         doit= 1;
2815                 } 
2816                 else if (val) {
2817                         if (ELEM(event, ESCKEY, RIGHTMOUSE))
2818                                 doit= -1;
2819                         else if (ELEM(event, SPACEKEY, RETKEY))
2820                                 doit= 1;
2821                 }
2822         }
2823         draw_front_xor_dirdist_line(dir, edge_position+delta, edge_start, edge_end);
2824         glFlush();
2825         glReadBuffer(GL_BACK);
2826         glDrawBuffer(GL_BACK);
2827
2828         if (doit==1) {
2829                 for (v1= G.curscreen->vertbase.first; v1; v1= v1->next) {
2830                         if (v1->flag) {
2831                                         /* that way a nice AREAGRID  */
2832                                 if((dir=='v') && v1->vec.x>0 && v1->vec.x<G.curscreen->sizex-1) {
2833                                         v1->vec.x+= delta;
2834                                         if(delta != bigger && delta != -smaller) v1->vec.x-= (v1->vec.x % AREAGRID);
2835                                 }
2836                                 if((dir=='h') && v1->vec.y>0 && v1->vec.y<G.curscreen->sizey-1) {
2837                                         v1->vec.y+= delta;
2838                                         
2839                                         /* with these lines commented out you can pull the top bar exact to the screen border. */
2840                                         /* if(delta != bigger && delta != -smaller) { */
2841                                                 v1->vec.y+= AREAGRID-1;
2842                                                 v1->vec.y-= (v1->vec.y % AREAGRID);
2843                                         
2844                                         /* } */
2845                                 }
2846                         }
2847                         v1->flag= 0;
2848                 }
2849                 
2850                 removedouble_scrverts();
2851                 removedouble_scredges();
2852                 testareas();
2853         }
2854         
2855         mainqenter(DRAWEDGES, 1);
2856         dodrawscreen= 1;                /* patch! event gets lost,,,? */
2857 }
2858
2859 static void scrollheader(ScrArea *area)
2860 {
2861         short mval[2], mvalo[2];
2862         
2863         if(area->headbutlen<area->winx) {
2864                 area->headbutofs= 0;
2865         }
2866         else if(area->headbutofs+area->winx > area->headbutlen) {
2867                 area->headbutofs= area->headbutlen-area->winx;
2868         }
2869
2870         getmouseco_sc(mvalo);
2871
2872         while(get_mbut() & M_MOUSE) {
2873                 getmouseco_sc(mval);
2874                 if(mval[0]!=mvalo[0]) {
2875                         area->headbutofs-= (mval[0]-mvalo[0]);
2876
2877                         if(area->headbutlen-area->winx < area->headbutofs) area->headbutofs= area->headbutlen-area->winx;
2878                         if(area->headbutofs<0) area->headbutofs= 0;
2879
2880                         scrarea_do_headchange(area);
2881                         scrarea_do_headdraw(area);
2882
2883                         screen_swapbuffers();
2884                         
2885                         mvalo[0]= mval[0];
2886                 } else {
2887                         BIF_wait_for_statechange();
2888                 }
2889         }
2890 }
2891
2892 int select_area(int spacetype)
2893 {
2894         /* call from edit routines, when there are more areas
2895          * of type 'spacetype', you can indicate an area manually
2896          */
2897         ScrArea *sa, *sact = NULL;
2898         int tot=0;
2899         unsigned short event = 0;
2900         short val, mval[2];
2901         
2902         sa= G.curscreen->areabase.first;
2903         while(sa) {
2904                 if(sa->spacetype==spacetype) {
2905                         sact= sa;
2906                         tot++;
2907                 }
2908                 sa= sa->next;
2909         } 
2910         
2911         if(tot==0) {
2912                 error("Can't do this! Open correct window");
2913                 return 0;
2914         }
2915         
2916         if(tot==1) {
2917                 if(curarea!=sact) areawinset(sact->win);
2918                 return 1;
2919         }
2920         else if(tot>1) {
2921                 set_cursor(CURSOR_HELP);
2922                 while(1) {
2923                         event= extern_qread(&val);
2924                         
2925                         if (val) {
2926                                 if(event==ESCKEY) break;
2927                                 if(event==LEFTMOUSE) break;
2928                                 if(event==SPACEKEY) break;
2929                         } else {
2930                                 BIF_wait_for_statechange();
2931                         }
2932                 }
2933                 screen_set_cursor(G.curscreen);
2934                 
2935                 /* recalculate winakt */
2936                 getmouseco_sc(mval);
2937                 
2938                 if(event==LEFTMOUSE) {
2939                         ScrArea *sa= screen_find_area_for_pt(G.curscreen, mval);
2940                         
2941                         if (sa &&sa->spacetype==spacetype) {
2942                                 G.curscreen->winakt= sa->win;
2943                                 areawinset(G.curscreen->winakt);
2944                         } else {
2945                                 error("Wrong window");
2946                                 return 0;
2947                         }
2948                 }
2949         }
2950         
2951         if(event==LEFTMOUSE) return 1;
2952         else return 0;
2953 }
2954
2955 /* ************  END JOIN/SPLIT/MOVE ************** */
2956 /* **************** DRAW SCREENEDGES ***************** */
2957
2958
2959 void draw_area_emboss(ScrArea *sa)
2960 {
2961         
2962         /* set transp line */
2963         glEnable( GL_BLEND );
2964         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2965
2966         /* right  */
2967         glColor4ub(0,0,0, 80);
2968         sdrawline(sa->winx-1, 0, sa->winx-1, sa->winy-1);
2969         
2970         /* bottom  */
2971         if(sa->headertype!=HEADERDOWN) {
2972                 glColor4ub(0,0,0, 128);
2973                 sdrawline(0, 0, sa->winx-1, 0);
2974         }
2975         
2976         /* top  */
2977         if(sa->headertype!=HEADERTOP) {
2978                 glColor4ub(255,255,255, 96);
2979                 sdrawline(0, sa->winy-1, sa->winx-1, sa->winy-1);
2980         }
2981         /* left  */
2982         glColor4ub(255,255,255, 80);
2983         sdrawline(0, 0, 0, sa->winy);
2984
2985         glDisable( GL_BLEND );
2986         
2987         
2988         /* for test */
2989         if(FALSE && sa->spacetype==SPACE_VIEW3D) {
2990         cpack(0xA0A0A0);
2991         uiSetRoundBox(31);
2992         uiRoundBoxEmboss(5.0, 5.0, 25.0, 100.0, 8.0, 0);
2993
2994         glEnable(GL_BLEND);
2995         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
2996         
2997         glRasterPos2f(8.0, 10.0);
2998         BIF_draw_icon(ICON_MATERIAL_HLT);
2999         glRasterPos2f(8.0, 30.0);
3000         BIF_draw_icon(ICON_IPO_HLT);
3001         glRasterPos2f(8.0, 50.0);
3002         BIF_draw_icon(ICON_HOME);
3003         glRasterPos2f(8.0, 70.0);
3004         BIF_draw_icon(ICON_BORDERMOVE);
3005         
3006         glBlendFunc(GL_ONE,  GL_ZERO); 
3007         glDisable(GL_BLEND);
3008         }
3009 }
3010
3011
3012 void drawscredge_area(ScrArea *sa)
3013 {
3014         short x1= sa->v1->vec.x;
3015         short y1= sa->v1->vec.y;
3016         short x2= sa->v3->vec.x;
3017         short y2= sa->v3->vec.y;
3018
3019         /* this to fill the (undrawn) edge area with back color first */
3020         glColor3f(SCR_BACK,SCR_BACK,SCR_BACK);
3021         sdrawline(x2, y1, x2, y2);
3022         sdrawline(x1, y1, x2, y1);
3023         
3024         cpack(0x0);
3025         
3026         /* right border area */
3027         if(sa->headertype==HEADERTOP) sdrawline(x2, y1, x2, y2-SCR_ROUND+1);
3028         else if(sa->headertype==HEADERDOWN) sdrawline(x2, y1+SCR_ROUND-1, x2, y2);
3029         else sdrawline(x2, y1, x2, y2);
3030         
3031         /* left border area */
3032         if(x1>0) { // otherwise it draws the emboss of window over
3033                 if(sa->headertype==HEADERTOP) sdrawline(x1, y1, x1, y2-SCR_ROUND+1);
3034                 else if(sa->headertype==HEADERDOWN) sdrawline(x1, y1+SCR_ROUND-1, x1, y2);
3035                 else sdrawline(x1, y1, x1, y2);
3036         }       
3037         /* top border area */
3038         if(sa->headertype==HEADERTOP) sdrawline(x1+SCR_ROUND, y2, x2-SCR_ROUND+1, y2);
3039         else sdrawline(x1, y2, x2, y2);
3040         
3041         /* bottom border area */
3042         if(sa->headertype==HEADERDOWN) sdrawline(x1+SCR_ROUND, y1, x2-SCR_ROUND+1, y1);
3043         else sdrawline(x1, y1, x2, y1);
3044         
3045 }
3046
3047 void drawscreen(void)
3048 {
3049         ScrArea *sa;
3050
3051         mywinset(G.curscreen->mainwin);
3052         myortho2(-0.375, (float)G.curscreen->sizex-0.375, -0.375, (float)G.curscreen->sizey-0.375);
3053
3054         sa= G.curscreen->areabase.first;
3055         while(sa) {
3056                 drawscredge_area(sa);
3057                 sa= sa->next;
3058         }
3059
3060         /* this double draw patch seems to be needed for certain sgi's (octane, indigo2) */
3061         glDrawBuffer(GL_FRONT);
3062
3063         sa= G.curscreen->areabase.first;
3064         while(sa) {
3065                 drawscredge_area(sa);
3066                 sa= sa->next;
3067         }
3068
3069         glDrawBuffer(GL_BACK);
3070 }
3071
3072 /* ********************************* */
3073
3074 bScreen *default_twosplit() 
3075 {
3076         bScreen *sc= addscreen("screen");
3077         ScrArea *sa;
3078
3079         splitarea( (ScrArea *)sc->areabase.first, 'h', 0.99);
3080         sa= sc->areabase.first;
3081         newspace(sa, SPACE_VIEW3D);
3082         newspace(sa->next, SPACE_INFO);
3083         
3084         return sc;
3085 }
3086
3087 void initscreen(void)
3088 {
3089         default_twosplit();
3090 }
3091
3092 /***/
3093
3094 void screen_draw_info_text(bScreen *sc, char *text) {
3095         Window *oldactwin= winlay_get_active_window();
3096         ScrArea *sa;
3097         
3098                 /*
3099                  * Because this is called from within rendering
3100                  * internals it is possible our window is not
3101                  * active.
3102                  */
3103         window_make_active(mainwin);
3104         
3105         for (sa= sc->areabase.first; sa; sa= sa->next) {
3106                 if (sa->spacetype==SPACE_INFO) {
3107                         int x= sa->headbutlen - 28;
3108                         int y= 6;
3109                         
3110                         areawinset(sa->headwin);
3111                         glDrawBuffer(GL_FRONT);
3112
3113                         cpack(0xA08060);
3114                         glRecti(x-11,  y-6,  x+55,  y+13);
3115                         
3116                         cpack(0x909090);
3117                         glRecti(x+55,  y-6,  x+1280,  y+14);
3118                         
3119                         cpack(0x0);
3120                         glRasterPos2i(x,  y);
3121                         BMF_DrawString(G.fonts, text);
3122                         
3123                         glFlush();
3124                         glDrawBuffer(GL_BACK);
3125                         
3126                         sa->head_swap= WIN_FRONT_OK;
3127                 }
3128         }
3129         
3130         if (oldactwin && oldactwin!=mainwin) window_make_active(oldactwin);
3131 }
3132
3133 static int curcursor;
3134
3135 int get_cursor(void) {
3136         return curcursor;
3137 }
3138
3139 void set_cursor(int curs) {
3140         if (!(R.flag & R_RENDERING) && G.background == 0) {
3141                 if (curs!=curcursor) {
3142                         curcursor= curs;
3143                         window_set_cursor(mainwin, curs);
3144                 }
3145         }
3146 }
3147
3148 void unlink_screen(bScreen *sc) {
3149         ScrArea *sa;
3150         
3151         for (sa= sc->areabase.first; sa; sa= sa->next)  
3152                 del_area(sa);
3153 }
3154
3155 void warp_pointer(int x, int y)
3156 {
3157         window_warp_pointer(mainwin, x, y);
3158 }
3159
3160 void set_timecursor(int nr)
3161 {
3162                 /* 10 8x8 digits */
3163         static char number_bitmaps[10][8]= {
3164                 {0,  56,  68,  68,  68,  68,  68,  56}, 
3165                 {0,  24,  16,  16,  16,  16,  16,  56}, 
3166                 {0,  60,  66,  32,  16,   8,   4, 126}, 
3167                 {0, 124,  32,  16,  56,  64,  66,  60}, 
3168                 {0,  32,  48,  40,  36, 126,  32,  32}, 
3169                 {0, 124,   4,  60,  64,  64,  68,  56}, 
3170                 {0,  56,   4,   4,  60,  68,  68,  56}, 
3171                 {0, 124,  64,  32,  16,   8,   8,   8}, 
3172                 {0,  60,  66,  66,  60,  66,  66,  60}, 
3173                 {0,  56,  68,  68, 120,  64,  68,  56} 
3174         };
3175         unsigned char mask[16][2];
3176         unsigned char bitmap[16][2];
3177         int i, idx;
3178
3179         memset(&bitmap, 0x00, sizeof(bitmap));
3180         memset(&mask, 0xFF, sizeof(mask));
3181         
3182                 /* print number bottom right justified */
3183         for (idx= 3; nr && idx>=0; idx--) {
3184                 char *digit= number_bitmaps[nr%10];
3185                 int x = idx%2;
3186                 int y = idx/2;
3187                 
3188                 for (i=0; i<8; i++)
3189                         bitmap[i + y*8][x]= digit[i];
3190                 nr/= 10;
3191         }
3192
3193         curcursor= CURSOR_NONE;
3194         window_set_custom_cursor(mainwin, mask, bitmap, 7, 7);
3195         BIF_renderwin_set_custom_cursor(mask, bitmap);
3196 }
3197