1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
34 extern struct display
*display
;
35 extern struct win
*fore
, *windows
;
36 extern struct layer
*flayer
;
37 extern int captionalways
;
38 extern struct LayFuncs BlankLf
;
39 extern int focusminwidth
, focusminheight
;
45 cv
->c_blank
.l_cvlist
= cv
;
46 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
47 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
48 cv
->c_blank
.l_x
= cv
->c_blank
.l_y
= 0;
49 cv
->c_blank
.l_layfn
= &BlankLf
;
50 cv
->c_blank
.l_data
= 0;
51 cv
->c_blank
.l_next
= 0;
52 cv
->c_blank
.l_bottom
= &cv
->c_blank
;
53 cv
->c_blank
.l_blocking
= 0;
54 cv
->c_layer
= &cv
->c_blank
;
66 cv
->c_slprev
= pcv
->c_slprev
;
68 cv
->c_slprev
->c_slnext
= cv
;
69 cv
->c_slback
= pcv
->c_slback
;
70 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== pcv
)
71 cv
->c_slback
->c_slperp
= cv
;
72 cv
->c_slorient
= pcv
->c_slorient
;
73 cv
->c_slweight
= pcv
->c_slweight
;
77 cv
->c_slorient
= pcv
->c_slorient
;
78 cv
->c_slback
= pcv
->c_slback
;
79 cv
->c_slweight
= pcv
->c_slweight
;
81 cv
->c_slnext
= pcv
->c_slnext
;
83 cv
->c_slnext
->c_slprev
= cv
;
84 LayerCleanupMemory(&pcv
->c_blank
);
92 struct viewport
*vp
, *nvp
;
97 cv
->c_slprev
->c_slnext
= cv
->c_slnext
;
99 cv
->c_slnext
->c_slprev
= cv
->c_slprev
;
100 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
101 cv
->c_slback
->c_slperp
= cv
->c_slnext
? cv
->c_slnext
: cv
->c_slprev
;
105 FreeCanvas(cv
->c_slperp
);
106 LayerCleanupMemory(&cv
->c_blank
);
115 /* remove from canvas chain as SetCanvasWindow might call
116 * some layer function */
117 for (cvp
= &D_cvlist
; *cvp
; cvp
= &(*cvp
)->c_next
)
124 p
= cv
->c_layer
? Layer2Window(cv
->c_layer
) : 0;
125 SetCanvasWindow(cv
, 0);
127 WindowChanged(p
, 'u');
128 if (flayer
== cv
->c_layer
)
130 for (vp
= cv
->c_vplist
; vp
; vp
= nvp
)
137 evdeq(&cv
->c_captev
);
138 LayerCleanupMemory(&cv
->c_blank
);
147 for (; cv
; cv
= cv
->c_slnext
)
153 for (cvp
= cv
->c_slperp
; cvp
; cvp
= cvp
->c_slnext
)
156 n
= CountCanvas(cvp
->c_slperp
);
174 for (cvp
= cv
->c_slperp
; cvp
; cvp
= cvp
->c_slnext
)
177 n
= CountCanvas(cvp
->c_slperp
);
188 struct canvas
*cv
, *mcv
= 0;
191 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
193 /* ye + 1 because of caption line */
194 if (x
>= cv
->c_xs
&& x
<= cv
->c_xe
&& y
>= cv
->c_ys
&& y
<= cv
->c_ye
+ 1)
199 if (x
>= D_forecv
->c_xs
&& x
<= D_forecv
->c_xe
)
201 if (x
< cv
->c_xs
|| x
> cv
->c_xe
)
203 if (y
< D_forecv
->c_ys
&& y
< cv
->c_ys
)
205 if (y
> D_forecv
->c_ye
+ 1 && y
> cv
->c_ye
+ 1)
209 if (y
> cv
->c_ye
+ 1)
210 m
= y
- (cv
->c_ye
+ 1);
212 if (y
>= D_forecv
->c_ys
&& y
<= D_forecv
->c_ye
+ 1)
214 if (y
< cv
->c_ys
|| y
> cv
->c_ye
+ 1)
216 if (x
< D_forecv
->c_xs
&& x
< cv
->c_xs
)
218 if (x
> D_forecv
->c_xe
&& x
> cv
->c_xe
)
225 if (m
&& (!mm
|| m
< mm
))
231 return mcv
? mcv
: D_forecv
;
235 SetCanvasWindow(cv
, wi
)
239 struct win
*p
= 0, **pp
;
241 struct canvas
*cvp
, **cvpp
;
244 display
= cv
->c_display
;
248 /* remove old layer */
249 for (cvpp
= &l
->l_cvlist
; (cvp
= *cvpp
); cvpp
= &cvp
->c_lnext
)
253 *cvpp
= cvp
->c_lnext
;
259 if (p
&& cv
== D_forecv
)
262 ReleaseAutoWritelock(display
, p
);
266 SetTimeout(&p
->w_silenceev
, p
->w_silencewait
* 1000);
267 evenq(&p
->w_silenceev
);
272 if (l
->l_cvlist
== 0 && (p
== 0 || l
!= p
->w_savelayer
))
276 /* find right layer to display on canvas */
277 if (wi
&& wi
->w_type
!= W_TYPE_GROUP
)
280 if (wi
->w_savelayer
&& (wi
->w_blocked
|| wi
->w_savelayer
->l_cvlist
== 0))
287 l
->l_data
= (char *)wi
;
292 /* add our canvas to the layer's canvaslist */
293 ASSERT(l
->l_cvlist
!= cv
);
294 cv
->c_lnext
= l
->l_cvlist
;
297 cv
->c_xoff
= cv
->c_xs
;
298 cv
->c_yoff
= cv
->c_ys
;
299 RethinkViewportOffsets(cv
);
304 if (wi
&& wi
->w_type
== W_TYPE_GROUP
)
306 /* auto-start windowlist on groups */
307 struct display
*d
= display
;
308 struct layer
*oldflayer
= flayer
;
310 display_wlist(0, 0, wi
);
315 if (wi
&& D_other
== wi
)
316 D_other
= wi
->w_next
; /* Might be 0, but that's OK. */
320 fore
= D_fore
; /* XXX ? */
324 ObtainAutoWritelock(display
, wi
);
327 * Place the window at the head of the most-recently-used list
331 for (pp
= &windows
; (p
= *pp
); pp
= &p
->w_next
)
345 cv_winid_fn(ev
, data
)
350 struct canvas
*cv
= (struct canvas
*)data
;
352 display
= cv
->c_display
;
353 if (D_status
== STATUS_ON_WIN
)
361 if (cv
->c_ye
+ 1 < D_height
)
362 RefreshLine(cv
->c_ye
+ 1, 0, D_width
- 1, 0);
363 if (ox
!= -1 && oy
!= -1)
373 if ((cv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
376 cv
->c_xe
= D_width
- 1;
378 cv
->c_ye
= D_height
- 1 - (D_has_hstatus
== HSTATUS_LASTLINE
) - captionalways
;
379 debug2("MakeDefaultCanvas 0,0 %d,%d\n", cv
->c_xe
, cv
->c_ye
);
383 cv
->c_display
= display
;
389 cv
->c_slback
= &D_canvas
;
390 D_canvas
.c_slperp
= cv
;
391 D_canvas
.c_xs
= cv
->c_xs
;
392 D_canvas
.c_xe
= cv
->c_xe
;
393 D_canvas
.c_ys
= cv
->c_ys
;
394 D_canvas
.c_ye
= cv
->c_ye
;
395 cv
->c_slorient
= SLICE_UNKN
;
396 cv
->c_captev
.type
= EV_TIMEOUT
;
397 cv
->c_captev
.data
= (char *)cv
;
398 cv
->c_captev
.handler
= cv_winid_fn
;
404 RethinkDisplayViewports();
405 D_forecv
= cv
; /* default input focus */
409 static struct canvas
**
410 CreateCanvasChainRec(cv
, cvp
)
414 for (; cv
; cv
= cv
->c_slnext
)
417 cvp
= CreateCanvasChainRec(cv
->c_slperp
, cvp
);
428 RecreateCanvasChain()
431 cvp
= CreateCanvasChainRec(D_canvas
.c_slperp
, &D_cvlist
);
436 EqualizeCanvas(cv
, gflag
)
441 for (; cv
; cv
= cv
->c_slnext
)
443 if (cv
->c_slperp
&& gflag
)
445 cv
->c_slweight
= CountCanvasPerp(cv
);
446 for (cv2
= cv
->c_slperp
; cv2
; cv2
= cv2
->c_slnext
)
448 EqualizeCanvas(cv2
->c_slperp
, gflag
);
459 struct canvas
*cv2
, *cvn
, *fcv
;
460 int nh
, i
, maxi
, hh
, m
, w
, wsum
;
470 debug2("ResizeCanvas: %d,%d", xs
, ys
);
471 debug2(" %d,%d\n", xe
, ye
);
474 if (cv
->c_slorient
== SLICE_UNKN
)
476 ASSERT(!cv
->c_slnext
&& !cv
->c_slperp
);
481 cv
->c_xoff
= cv
->c_xs
;
482 cv
->c_yoff
= cv
->c_ys
;
483 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
484 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
489 if (focusminwidth
|| focusminheight
)
491 debug("searching for focus canvas\n");
493 while (cv2
->c_slback
)
495 if (cv2
->c_slback
== cv
->c_slback
)
498 focusmin
= cv
->c_slorient
== SLICE_VERT
? focusminheight
: focusminwidth
;
501 else if (focusmin
< 0)
502 focusmin
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
503 debug1("found, focusmin=%d\n", focusmin
);
510 m
= CountCanvas(cv
) * 2;
511 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
517 debug1("corrected to %d\n", focusmin
);
520 /* pass 1: calculate weight sum */
521 for (cv2
= cv
, wsum
= 0; cv2
; cv2
= cv2
->c_slnext
)
523 debug1(" weight %d\n", cv2
->c_slweight
);
524 wsum
+= cv2
->c_slweight
;
526 debug1("wsum = %d\n", wsum
);
531 /* pass 2: calculate need/excess space */
532 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
533 for (cv2
= cv
, need
= got
= 0; cv2
; cv2
= cv2
->c_slnext
)
535 m
= cv2
->c_slperp
? CountCanvasPerp(cv2
) * 2 - 1 : 1;
538 hh
= cv2
->c_slweight
? nh
* cv2
->c_slweight
/ w
: 0;
539 w
-= cv2
->c_slweight
;
541 debug2(" should %d min %d\n", hh
, m
);
547 debug2("need: %d, got %d\n", need
, got
);
551 /* pass 3: distribute space */
552 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
553 i
= cv
->c_slorient
== SLICE_VERT
? ys
: xs
;
554 maxi
= cv
->c_slorient
== SLICE_VERT
? ye
: xe
;
561 if (cv
->c_slprev
&& !cv
->c_slback
->c_slback
&& !cv
->c_slprev
->c_slperp
&& !cv
->c_slprev
->c_slprev
)
563 cv
->c_slprev
->c_slorient
= SLICE_UNKN
;
566 cv
->c_slback
->c_ye
++;
567 cv
->c_slprev
->c_ye
++;
570 SetCanvasWindow(cv
, 0);
574 m
= cv
->c_slperp
? CountCanvasPerp(cv
) * 2 - 1 : 1;
577 hh
= cv
->c_slweight
? nh
* cv
->c_slweight
/ w
: 0;
580 debug2(" should %d min %d\n", hh
, m
);
584 debug1(" -> %d\n", hh
);
588 int hx
= need
* (hh
- m
- 1) / got
;
589 debug3(" -> %d - %d = %d\n", hh
, hx
, hh
- hx
);
593 debug2(" now need=%d got=%d\n", need
, got
);
596 /* hh is window size plus pation line */
597 if (i
+ hh
> maxi
+ 2)
600 debug1(" not enough space, reducing to %d\n", hh
);
602 if (i
+ hh
== maxi
+ 1)
605 debug(" incrementing as no other canvas will fit\n");
607 if (cv
->c_slorient
== SLICE_VERT
)
612 cv
->c_ye
= i
+ hh
- 2;
619 cv
->c_xe
= i
+ hh
- 2;
625 cv
->c_xoff
= cv
->c_xs
;
626 cv
->c_yoff
= cv
->c_ys
;
627 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
628 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
632 if (!cv
->c_slperp
->c_slnext
)
634 debug("deleting perp node\n");
635 FreePerp(cv
->c_slperp
);
643 static struct canvas
*
648 debug("Creating new perp node\n");
650 if ((pcv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
653 pcv
->c_display
= cv
->c_display
;
654 pcv
->c_slnext
= cv
->c_slnext
;
655 pcv
->c_slprev
= cv
->c_slprev
;
657 pcv
->c_slback
= cv
->c_slback
;
658 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
659 cv
->c_slback
->c_slperp
= pcv
;
660 pcv
->c_slorient
= cv
->c_slorient
;
663 pcv
->c_xs
= cv
->c_xs
;
664 pcv
->c_xe
= cv
->c_xe
;
665 pcv
->c_ys
= cv
->c_ys
;
666 pcv
->c_ye
= cv
->c_ye
;
668 pcv
->c_slnext
->c_slprev
= pcv
;
670 pcv
->c_slprev
->c_slnext
= pcv
;
671 pcv
->c_slweight
= cv
->c_slweight
;
672 CanvasInitBlank(pcv
);
678 cv
->c_slorient
= SLICE_UNKN
;
691 debug2("AddCanvas orient %d, forecv is %d\n", orient
, cv
->c_slorient
);
693 if (cv
->c_slorient
!= SLICE_UNKN
&& cv
->c_slorient
!= orient
)
698 xs
= cv
->c_slback
->c_xs
;
699 xe
= cv
->c_slback
->c_xe
;
700 ys
= cv
->c_slback
->c_ys
;
701 ye
= cv
->c_slback
->c_ye
;
702 if (!captionalways
&& cv
== D_canvas
.c_slperp
&& !cv
->c_slnext
)
703 ye
--; /* need space for caption */
704 debug2("Adding Canvas to slice %d,%d ", xs
, ys
);
705 debug2("%d,%d\n", xe
, ye
);
707 num
= CountCanvas(cv
->c_slback
->c_slperp
) + 1;
708 debug1("Num = %d\n", num
);
709 if (orient
== SLICE_VERT
)
716 return -1; /* can't fit in */
718 if ((cv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
721 D_forecv
->c_slback
->c_ye
= ye
; /* in case we modified it above */
722 D_forecv
->c_slorient
= orient
; /* in case it was UNKN */
723 cv
->c_slnext
= D_forecv
->c_slnext
;
724 cv
->c_slprev
= D_forecv
;
725 D_forecv
->c_slnext
= cv
;
727 cv
->c_slnext
->c_slprev
= cv
;
728 cv
->c_slorient
= orient
;
729 cv
->c_slback
= D_forecv
->c_slback
;
737 cv
->c_display
= display
;
739 cv
->c_captev
.type
= EV_TIMEOUT
;
740 cv
->c_captev
.data
= (char *)cv
;
741 cv
->c_captev
.handler
= cv_winid_fn
;
749 EqualizeCanvas(cv
->c_slperp
, 0);
751 RecreateCanvasChain();
752 RethinkDisplayViewports();
753 ResizeLayersToCanvases();
763 debug("RemCanvas\n");
765 if (cv
->c_slorient
== SLICE_UNKN
)
771 if (!cv
->c_slnext
->c_slnext
&& cv
->c_slback
->c_slback
)
773 /* two canvases in slice, kill perp node */
775 debug("deleting perp node\n");
776 FreePerp(cv
->c_slprev
? cv
->c_slprev
: cv
->c_slnext
);
777 FreePerp(cv
->c_slback
);
779 xs
= cv
->c_slback
->c_xs
;
780 xe
= cv
->c_slback
->c_xe
;
781 ys
= cv
->c_slback
->c_ys
;
782 ye
= cv
->c_slback
->c_ye
;
785 D_forecv
= cv
->c_slprev
;
787 D_forecv
= cv
->c_slnext
;
791 while (D_forecv
->c_slperp
)
792 D_forecv
= D_forecv
->c_slperp
;
794 /* if only one canvas left, set orient back to unknown */
795 if (!cv
->c_slnext
&& !cv
->c_slprev
&& !cv
->c_slback
->c_slback
&& !cv
->c_slperp
)
797 cv
->c_slorient
= SLICE_UNKN
;
799 cv
->c_slback
->c_ye
= ++ye
; /* caption line no longer needed */
802 EqualizeCanvas(cv
->c_slperp
, 0);
805 D_fore
= Layer2Window(D_forecv
->c_layer
);
806 flayer
= D_forecv
->c_layer
;
808 RecreateCanvasChain();
809 RethinkDisplayViewports();
810 ResizeLayersToCanvases();
816 struct canvas
*cv
= D_forecv
, *ocv
= 0;
821 cv
->c_slprev
->c_slnext
= cv
->c_slnext
;
826 cv
->c_slnext
->c_slprev
= cv
->c_slprev
;
830 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
831 cv
->c_slback
->c_slperp
= ocv
;
832 cv
->c_slorient
= SLICE_UNKN
;
833 while (D_canvas
.c_slperp
)
834 FreeCanvas(D_canvas
.c_slperp
);
836 D_canvas
.c_slperp
= cv
;
837 cv
->c_slback
= &D_canvas
;
840 ASSERT(!cv
->c_slperp
);
842 D_canvas
.c_ye
++; /* caption line no longer needed */
843 ResizeCanvas(&D_canvas
);
844 RecreateCanvasChain();
845 RethinkDisplayViewports();
846 ResizeLayersToCanvases();
850 DupLayoutCv(cvf
, cvt
, save
)
851 struct canvas
*cvf
, *cvt
;
856 cvt
->c_slorient
= cvf
->c_slorient
;
857 cvt
->c_slweight
= cvf
->c_slweight
;
862 cvt
->c_display
= display
;
865 cvt
->c_captev
.type
= EV_TIMEOUT
;
866 cvt
->c_captev
.data
= (char *)cvt
;
867 cvt
->c_captev
.handler
= cv_winid_fn
;
868 cvt
->c_blank
.l_cvlist
= 0;
869 cvt
->c_blank
.l_layfn
= &BlankLf
;
870 cvt
->c_blank
.l_bottom
= &cvt
->c_blank
;
872 cvt
->c_layer
= cvf
->c_layer
;
876 struct win
*p
= cvf
->c_layer
? Layer2Window(cvf
->c_layer
) : 0;
877 cvt
->c_layer
= p
? &p
->w_layer
: 0;
881 cvt
->c_slperp
= (struct canvas
*)calloc(1, sizeof(struct canvas
));
882 cvt
->c_slperp
->c_slback
= cvt
;
883 CanvasInitBlank(cvt
->c_slperp
);
884 DupLayoutCv(cvf
->c_slperp
, cvt
->c_slperp
, save
);
888 cvt
->c_slnext
= (struct canvas
*)calloc(1, sizeof(struct canvas
));
889 cvt
->c_slnext
->c_slprev
= cvt
;
890 cvt
->c_slnext
->c_slback
= cvt
->c_slback
;
891 CanvasInitBlank(cvt
->c_slnext
);
903 for (; cv
; cv
= cv
->c_slnext
)
907 PutWindowCv(cv
->c_slperp
);
910 p
= cv
->c_layer
? (struct win
*)cv
->c_layer
->l_data
: 0;
912 SetCanvasWindow(cv
, p
);