Back to index

texmacs  1.0.7.15
switch_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : switch_widget.cpp
00004 * DESCRIPTION: switch between several possibilities
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "Widkit/composite_widget.hpp"
00013 #include "Widkit/Event/attribute_event.hpp"
00014 
00015 /******************************************************************************
00016 * Switch widgets
00017 ******************************************************************************/
00018 
00019 class switch_widget_rep: public composite_widget_rep {
00020   int current;
00021   array<wk_widget> variant;
00022   array<string>    variant_name;
00023 public:
00024   switch_widget_rep (array<wk_widget> a, array<string> name, int i);
00025   operator tree ();
00026 
00027   void handle_attach_window (attach_window_event ev);
00028   void handle_get_widget    (get_widget_event ev);
00029   void handle_set_widget    (set_widget_event ev);
00030   void handle_get_integer   (get_integer_event ev);
00031   void handle_set_integer   (set_integer_event ev);
00032   bool handle               (event ev);
00033 };
00034 
00035 switch_widget_rep::switch_widget_rep (array<wk_widget> v,
00036   array<string> name, int init):
00037     composite_widget_rep (1),
00038     current (init),
00039     variant (v), variant_name (name)
00040 {
00041   a[0]= variant [current];
00042 }
00043 
00044 switch_widget_rep::operator tree () {
00045   int i;
00046   tree t (TUPLE, N(variant)+1);
00047   t[0]= "switch";
00048   for (i=0; i<N(variant); i++) t[i+1]= (tree) variant[i];
00049   return t;
00050 }
00051 
00052 void
00053 switch_widget_rep::handle_attach_window (attach_window_event ev) {
00054   int i, n= N(variant);
00055   composite_widget_rep::handle_attach_window (ev);
00056   for (i=0; i<n; i++)
00057     if (i != current)
00058       variant[i] << emit_attach_window (win);
00059 }
00060 
00061 void
00062 switch_widget_rep::handle_get_widget (get_widget_event ev) {
00063   int i, n= N(variant);
00064   for (i=0; i<n; i++)
00065     if (variant_name[i] == ev->which) {
00066       ev->w= variant[i];
00067       return;
00068     }
00069   for (i=0; i<n; i++)
00070     if (variant_name[i] == "default") {
00071       variant[i] << ev;
00072       return;
00073     }
00074   variant [current] << ev;
00075 }
00076 
00077 void
00078 switch_widget_rep::handle_set_widget (set_widget_event ev) {
00079   int i, n= N(variant);
00080   for (i=0; i<n; i++)
00081     if (variant_name[i] == ev->which) {
00082       variant[i]= ev->w;
00083       return;
00084     }
00085   for (i=0; i<n; i++)
00086     if (variant_name[i] == "default") {
00087       variant[i] << ev;
00088       return;
00089     }
00090   variant [current] << ev;
00091 }
00092 
00093 void
00094 switch_widget_rep::handle_get_integer (get_integer_event ev) {
00095   if (ev->which == "switch") ev->i= current;
00096   else variant [current] << ev;
00097 }
00098 
00099 void
00100 switch_widget_rep::handle_set_integer (set_integer_event ev) {
00101   if (ev->which == "switch") {
00102     current= ev->i;
00103     a[0]   = variant [current];
00104   }
00105   else variant [current] << ev;
00106 }
00107 
00108 bool
00109 switch_widget_rep::handle (event ev) {
00110   switch (ev->type) {
00111   case ATTACH_WINDOW_EVENT:
00112     handle_attach_window (ev);
00113     return true;
00114   case GET_WIDGET_EVENT:
00115     handle_get_widget (ev);
00116     return true;
00117   case SET_WIDGET_EVENT:
00118     handle_set_widget (ev);
00119     return true;
00120   case GET_INTEGER_EVENT:
00121     handle_get_integer (ev);
00122     return true;
00123   case SET_INTEGER_EVENT:
00124     handle_set_integer (ev);
00125     return true;
00126   default:
00127     return composite_widget_rep::handle (ev);
00128   }
00129   return false;
00130 }
00131 
00132 /******************************************************************************
00133 * Wrapped widgets
00134 ******************************************************************************/
00135 
00136 class wrapped_widget_rep: public composite_widget_rep {
00137   command quit;
00138 public:
00139   wrapped_widget_rep (wk_widget w, command quit);
00140   ~wrapped_widget_rep ();
00141   void handle_destroy (destroy_event ev);
00142   operator tree ();
00143 };
00144 
00145 wrapped_widget_rep::wrapped_widget_rep (wk_widget w, command q):
00146   composite_widget_rep (1), quit (q) { a[0]= w; }
00147 
00148 wrapped_widget_rep::~wrapped_widget_rep () {
00149   if (!is_nil (quit)) quit (); }
00150 
00151 void
00152 wrapped_widget_rep::handle_destroy (destroy_event ev) {
00153   if (!is_nil (quit)) quit ();
00154   quit= command (); }
00155 
00156 wrapped_widget_rep::operator tree () {
00157   return tree (TUPLE, "wrapped", (tree) a[0]); }
00158 
00159 /******************************************************************************
00160 * Interface
00161 ******************************************************************************/
00162 
00163 wk_widget
00164 switch_widget (array<wk_widget> a, array<string> name, int init) {
00165   return tm_new<switch_widget_rep> (a, name, init);
00166 }
00167 
00168 wk_widget
00169 optional_widget (wk_widget w, bool on) {
00170   array<wk_widget> a (2);
00171   a[0]= w;
00172   a[1]= glue_wk_widget (false, false);
00173   array<string> name (2);
00174   name[0]= "default";
00175   return switch_widget (a, name, on? 0: 1);
00176 }
00177 
00178 wk_widget
00179 wrapped_widget (wk_widget w, command cmd) {
00180   return tm_new<wrapped_widget_rep> (w, cmd);
00181 }