Back to index

texmacs  1.0.7.15
enum_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : enum_widget.cpp
00004 * DESCRIPTION: Select a possibility among a given list of possibilities
00005 * COPYRIGHT  : (C) 2012  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 "window.hpp"
00013 #include "Widkit/wk_widget.hpp"
00014 #include "scheme.hpp"
00015 
00016 /******************************************************************************
00017 * Enumeration commands
00018 ******************************************************************************/
00019 
00020 class enum_command_rep: public command_rep {
00021   wk_widget_rep* in;
00022   string val;
00023   command cb;
00024 public:
00025   enum_command_rep (wk_widget w, string val2, command cb2):
00026     in (w.rep), val (val2), cb (cb2) {}
00027   void apply ();
00028   tm_ostream& print (tm_ostream& out) {
00029     return out << "Enum widget command"; }
00030 };
00031 
00032 void
00033 enum_command_rep::apply () {
00034   in << set_string ("input", val);
00035   cb (list_object (object (val)));
00036   windows_delayed_refresh (1);
00037 }
00038 
00039 command
00040 enum_command (wk_widget in, string val, command cb) {
00041   return tm_new<enum_command_rep> (in, val, cb);
00042 }
00043 
00044 /******************************************************************************
00045 * Enumeration widgets
00046 ******************************************************************************/
00047 
00048 wk_widget
00049 enum_wk_widget (command cb, array<string> vals, string val,
00050                 int style, string w) {
00051   int i, n= N(vals);
00052   array<string> def (1);
00053   def[0]= val;
00054   wk_widget in= input_text_wk_widget (cb, "string", def, style, w, true);
00055   if (n == 0) return in;
00056   bool editable= (val == "" || vals[n-1] == "");
00057   if (vals[n-1] == "") { n= n-1; vals= range (vals, 0, n); }
00058   array<wk_widget> entries (n);
00059   for (i=0; i<n; i++) {
00060     wk_widget txt= text_wk_widget (vals[i], style);
00061     command cmd= enum_command (in, vals[i], cb);
00062     entries[i]= command_button (txt, cmd, style);
00063   }
00064   wk_widget menu= vertical_menu (entries);
00065   if (!editable)
00066     return pulldown_button (in, menu, style);
00067   wk_widget v= text_wk_widget ("v", style);
00068   wk_widget v_but= command_button (v, command (), style|WIDGET_STYLE_BUTTON);
00069   wk_widget v_pop= pulldown_button (v_but, menu, style);
00070   array<wk_widget> ret (2);
00071   ret[0]= in;
00072   ret[1]= v_pop;
00073   return horizontal_list (ret);
00074 }