Back to index

texmacs  1.0.7.15
array_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : array_widget.cpp
00004 * DESCRIPTION: composite array widgets
00005 *              If there is space left in a horizontal array,
00006 *              then only the item named 'stretch_me' is stretched;
00007 *              by default this is the last item of the array.
00008 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00009 *******************************************************************************
00010 * This software falls under the GNU general public license version 3 or later.
00011 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00012 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00013 ******************************************************************************/
00014 
00015 #include "Widkit/composite_widget.hpp"
00016 
00017 void abs_round (SI& l);
00018 
00019 /******************************************************************************
00020 * Horizontal arrays
00021 ******************************************************************************/
00022 
00023 class horizontal_array_rep: public composite_widget_rep {
00024   int stretch_me;
00025 
00026 public:
00027   horizontal_array_rep (array<wk_widget> a, int stretch_me);
00028   horizontal_array_rep (array<wk_widget> a, array<string> name,
00029                      int stretch_me);
00030   operator tree ();
00031 
00032   void handle_get_size (get_size_event ev);
00033   void handle_position (position_event ev);
00034   void handle_find_child (find_child_event ev);
00035 };
00036 
00037 horizontal_array_rep::horizontal_array_rep (array<wk_widget> a, int sm):
00038   composite_widget_rep (a), stretch_me (sm) {}
00039 
00040 horizontal_array_rep::horizontal_array_rep (array<wk_widget> a,
00041   array<string> name, int sm):
00042     composite_widget_rep (a, name), stretch_me (sm) {}
00043 
00044 horizontal_array_rep::operator tree () {
00045   int i;
00046   tree t (TUPLE, N(a)+1);
00047   t[0]= "horizontal array";
00048   for (i=0; i<N(a); i++) t[i+1]= (tree) a[i];
00049   return t;
00050 }
00051 
00052 void
00053 horizontal_array_rep::handle_get_size (get_size_event ev) {
00054   SI& w= ev->w;
00055   SI& h= ev->h;
00056 
00057   if (ev->mode==-1) {
00058     int i, hh=0;
00059     for (i=0; i<N(a); i++) {
00060       int www= w/N(a), hhh= h;
00061       a[i] << get_size (www, hhh, -1);
00062       hh= max (hh, hhh);
00063     }
00064     w= 0; h= hh;
00065   }
00066   else {
00067     int i, ww=0, hh=0;
00068     for (i=0; i<N(a); i++) {
00069       int www= w/N(a), hhh= h;
00070       a[i] << get_size (www, hhh,  1);
00071       ww= ww+ www;
00072       hh= max (hh, hhh);
00073     }
00074     if (ev->mode==0) { w= min (w, ww); h= min (h, hh); }
00075     else { w= ww; h= hh; }
00076   }
00077 }
00078 
00079 void
00080 horizontal_array_rep::handle_position (position_event ev) {
00081   (void) ev;
00082   if (N(a)==0) return;
00083 
00084   int i;
00085   int j= (stretch_me==-1) ? N(a)-1: stretch_me;
00086   SI  tot_w= 0;
00087   SI  stretch_w= 0;
00088   for (i=0; i<N(a); i++) {
00089     SI the_w= w-tot_w, the_h= h;
00090     if (i==j) {
00091       a[i] << get_size (the_w, the_h, -1);
00092       stretch_w= the_w;
00093     }
00094     else a[i] << get_size (the_w, the_h);
00095     abs_round (the_w);
00096     tot_w += the_w;
00097   }
00098 
00099   SI  cur_w= 0;
00100   SI  extra= max (w- tot_w, 0);
00101   for (i=0; i<N(a); i++) {
00102     SI the_w= w-cur_w, the_h= h;
00103     if (i==j) the_w= stretch_w+ extra;
00104     else a[i] << get_size (the_w, the_h);
00105     abs_round (the_w);
00106     a[i] << emit_position (cur_w, 0, the_w, h);
00107     cur_w += the_w;
00108   }
00109 }
00110 
00111 void
00112 horizontal_array_rep::handle_find_child (find_child_event ev) {
00113   int& i= ev->which;
00114   for (i=0; i<N(a); i++)
00115     if ((ev->x >= a[i]->x1()-ox) && (ev->x < a[i]->x2()-ox)) return;
00116   i= -1;
00117 }
00118 
00119 /******************************************************************************
00120 * Interface
00121 ******************************************************************************/
00122 
00123 wk_widget
00124 horizontal_array (array<wk_widget> a, int stretch_me) {
00125   return tm_new<horizontal_array_rep> (a, stretch_me);
00126 }
00127 
00128 wk_widget
00129 horizontal_array (array<wk_widget> a, array<string> name, int sm) {
00130   return tm_new<horizontal_array_rep> (a, name, sm);
00131 }