Back to index

texmacs  1.0.7.15
inputs_list_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : inputs_list_widget.cpp
00004 * DESCRIPTION: A list of textual inputs with ok and cancel buttons
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 "analyze.hpp"
00013 #include "dictionary.hpp"
00014 #include "window.hpp"
00015 #include "Widkit/basic_widget.hpp"
00016 #include "Widkit/attribute_widget.hpp"
00017 #include "Widkit/layout.hpp"
00018 
00019 /******************************************************************************
00020 * Inputs list widgets
00021 ******************************************************************************/
00022 
00023 class inputs_list_widget_rep: public attribute_widget_rep {
00024   command       cmd;
00025   array<string> prompts;
00026   bool          ok;
00027 
00028 public:
00029   inputs_list_widget_rep (command cmd, array<string> prompts);
00030   operator tree ();
00031 
00032   void handle_get_size (get_size_event ev);
00033   void handle_set_string (set_string_event ev);
00034   void handle_get_string (get_string_event ev);
00035   void handle_destroy (destroy_event ev);
00036 };
00037 
00038 /******************************************************************************
00039 * Inputs list commands
00040 ******************************************************************************/
00041 
00042 class inputs_list_command_rep: public command_rep {
00043   wk_widget_rep* ilw;
00044   int i;  // -1 corresponds to cancel
00045   int n;
00046 public:
00047   inputs_list_command_rep (wk_widget w, int i2, int n2):
00048     ilw (w.rep), i (i2), n (n2) {}
00049   void apply ();
00050   tm_ostream& print (tm_ostream& out) {
00051     return out << "Input list command (" << i << ")"; }
00052 };
00053 
00054 void
00055 inputs_list_command_rep::apply () {
00056   wk_widget il_wid (ilw);
00057   if (i < 0)
00058     il_wid << set_string ("return", "#f");
00059   else if (i == n-1)
00060     il_wid << set_string ("return", "#t");
00061   else {
00062     string answer;
00063     il_wid[0]["inputs"][2 * i]["input"] << get_string ("input", answer);
00064     if (answer == "#f") il_wid << set_string ("return", "#f");
00065     else il_wid[0]->win->set_keyboard_focus
00066           (abstract (il_wid[0]["inputs"][2 * i + 2]["input"]));
00067   }
00068 }
00069 
00070 command
00071 inputs_list_command (wk_widget ilw, int i, int n) {
00072   return tm_new<inputs_list_command_rep> (ilw, i, n);
00073 }
00074 
00075 /******************************************************************************
00076 * Implementation of inputs list widgets
00077 ******************************************************************************/
00078 
00079 inputs_list_widget_rep::inputs_list_widget_rep (
00080   command cmd2, array<string> prompts2):
00081     attribute_widget_rep (1), cmd (cmd2), prompts (prompts2), ok (true)
00082 {
00083   ref_count++;
00084 
00085   int i, n= N (prompts);
00086   array<wk_widget> prompts_w (n);
00087   array<wk_widget> inputs_w (n);
00088   for (i=0; i<n; i++) {
00089     prompts_w[i]= text_wk_widget (prompts[i]);
00090     inputs_w [i]= input_text_wk_widget (inputs_list_command (this, i, n));
00091   }
00092 
00093   array<wk_widget> buttons_w (5);
00094   buttons_w[0]= glue_wk_widget (true, false);
00095   buttons_w[1]= command_button (text_wk_widget (translate ("Cancel")),
00096                             inputs_list_command (this, -1, n),
00097                             WIDGET_STYLE_BUTTON);
00098   buttons_w[2]= glue_wk_widget (false, false, 5*PIXEL);
00099   buttons_w[3]= command_button (text_wk_widget (translate ("Ok")),
00100                             inputs_list_command (this, n-1, n),
00101                             WIDGET_STYLE_BUTTON);
00102 #ifdef OS_MACOS
00103   buttons_w[4]= glue_wk_widget (false, false, 19*PIXEL);
00104 #else
00105   buttons_w[4]= glue_wk_widget (false, false, 5*PIXEL);
00106 #endif
00107 
00108   array<wk_widget> main_w (5);
00109   array<string> main_n (5);
00110   main_w[0]= glue_wk_widget (false, false, 0, 5*PIXEL);
00111   main_w[1]= aligned_widget (prompts_w, inputs_w,
00112                           5*PIXEL, 3*PIXEL, 2*PIXEL, 5*PIXEL);
00113   main_n[1]= "inputs";
00114   main_w[2]= glue_wk_widget (false, false, 0, 5*PIXEL);
00115   main_w[3]= horizontal_list (buttons_w);
00116   main_n[3]= "buttons";
00117   main_w[4]= glue_wk_widget (false, false, 0, 5*PIXEL);
00118 
00119   a[0]= vertical_list (main_w, main_n);
00120 
00121   ref_count--;
00122 }
00123 
00124 inputs_list_widget_rep::operator tree () {
00125   return tree (TUPLE, "inputs_list_widget", (tree) a[0]);
00126 }
00127 
00128 void
00129 inputs_list_widget_rep::handle_get_size (get_size_event ev) {
00130   SI dummy;
00131   a[0] << get_size (ev->w, ev->h, ev->mode);
00132   if (ev->mode < 1) ev->w= max (ev->w, 300*PIXEL);
00133   else gui_maximal_extents (ev->w, dummy);
00134 }
00135 
00136 void
00137 inputs_list_widget_rep::handle_set_string (set_string_event ev) {
00138   if (ev->which == "return") {
00139     ok= (ev->s == "#t");
00140     cmd ();
00141   }
00142   else attribute_widget_rep::handle_set_string (ev);
00143 }
00144 
00145 void
00146 inputs_list_widget_rep::handle_get_string (get_string_event ev) {
00147   string s= ev->which;
00148   if (s == "input") s= "input-0";
00149   if (starts (s, "input-")) {
00150     wk_widget ch=
00151       wk_widget(this)[0]["inputs"][2 * as_int (s (6, N(s)))]["input"];
00152     ch << get_string ("input", ev->s);
00153     if (!ok) ev->s= "#f";
00154   }
00155   else attribute_widget_rep::handle_get_string (ev);
00156 }
00157 
00158 void
00159 inputs_list_widget_rep::handle_destroy (destroy_event ev) {
00160   (void) ev;
00161   this << set_string ("return", "#f");
00162 }
00163 
00164 /******************************************************************************
00165 * exported routines
00166 ******************************************************************************/
00167 
00168 wk_widget
00169 inputs_list_wk_widget (command cmd, array<string> fields) {
00170   return tm_new<inputs_list_widget_rep> (cmd, fields);
00171 }