• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

Fl_Input_Choice.H

00001 //
00002 // "$Id$"
00003 //
00004 // An input/chooser widget.
00005 //            ______________  ____
00006 //           |              || __ |
00007 //           | input area   || \/ |
00008 //           |______________||____|
00009 //
00010 // Copyright 1998-2010 by Bill Spitzak and others.
00011 // Copyright 2004 by Greg Ercolano.
00012 //
00013 // This library is free software. Distribution and use rights are outlined in
00014 // the file "COPYING" which should have been included with this file.  If this
00015 // file is missing or damaged, see the license at:
00016 //
00017 //     http://www.fltk.org/COPYING.php
00018 //
00019 // Please report all bugs and problems on the following page:
00020 //
00021 //     http://www.fltk.org/str.php
00022 //
00023 
00024 /* \file
00025    Fl_Input_Choice widget . */
00026 
00027 #ifndef Fl_Input_Choice_H
00028 #define Fl_Input_Choice_H
00029 
00030 #include <FL/Fl.H>
00031 #include <FL/Fl_Group.H>
00032 #include <FL/Fl_Input.H>
00033 #include <FL/Fl_Menu_Button.H>
00034 #include <FL/fl_draw.H>
00035 #include <string.h>
00036 
00095 class FL_EXPORT Fl_Input_Choice : public Fl_Group {
00096   // Private class to handle slightly 'special' behavior of menu button
00097   class InputMenuButton : public Fl_Menu_Button {
00098     void draw() {
00099       draw_box(FL_UP_BOX, color());
00100       fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
00101       int xc = x()+w()/2, yc=y()+h()/2;
00102       fl_polygon(xc-5,yc-3,xc+5,yc-3,xc,yc+3);
00103       if (Fl::focus() == this) draw_focus();
00104     }
00105   public:
00106     InputMenuButton(int X,int Y,int W,int H,const char*L=0) : 
00107         Fl_Menu_Button(X, Y, W, H, L) { box(FL_UP_BOX); }
00108   };
00109 
00110   Fl_Input *inp_;
00111   InputMenuButton *menu_;
00112 
00113   // note: this is used by the Fl_Input_Choice ctor defined in Fl_Group.
00114   static void menu_cb(Fl_Widget*, void *data) { 
00115     Fl_Input_Choice *o=(Fl_Input_Choice *)data;
00116     Fl_Widget_Tracker wp(o);
00117     const Fl_Menu_Item *item = o->menubutton()->mvalue();
00118     if (item && item->flags & (FL_SUBMENU|FL_SUBMENU_POINTER)) return;  // ignore submenus
00119     if (!strcmp(o->inp_->value(), o->menu_->text()))
00120     {
00121       o->Fl_Widget::clear_changed();
00122       if (o->when() & FL_WHEN_NOT_CHANGED)
00123         o->do_callback();
00124     }
00125     else
00126     {
00127       o->inp_->value(o->menu_->text());
00128       o->inp_->set_changed();
00129       o->Fl_Widget::set_changed();
00130       if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
00131         o->do_callback();
00132     }
00133     
00134     if (wp.deleted()) return;
00135 
00136     if (o->callback() != default_callback)
00137     {
00138       o->Fl_Widget::clear_changed();
00139       o->inp_->clear_changed();
00140     }
00141   }
00142 
00143   // note: this is used by the Fl_Input_Choice ctor defined in Fl_Group.
00144   static void inp_cb(Fl_Widget*, void *data) { 
00145     Fl_Input_Choice *o=(Fl_Input_Choice *)data;
00146     Fl_Widget_Tracker wp(o);
00147     if (o->inp_->changed()) {
00148       o->Fl_Widget::set_changed();
00149       if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
00150         o->do_callback();
00151     } else {
00152       o->Fl_Widget::clear_changed();
00153       if (o->when() & FL_WHEN_NOT_CHANGED)
00154         o->do_callback();
00155     }
00156     
00157     if (wp.deleted()) return;
00158 
00159     if (o->callback() != default_callback)
00160       o->Fl_Widget::clear_changed();
00161   }
00162 
00163   // Custom resize behavior -- input stretches, menu button doesn't
00164   inline int inp_x() { return(x() + Fl::box_dx(box())); }
00165   inline int inp_y() { return(y() + Fl::box_dy(box())); }
00166   inline int inp_w() { return(w() - Fl::box_dw(box()) - 20); }
00167   inline int inp_h() { return(h() - Fl::box_dh(box())); }
00168 
00169   inline int menu_x() { return(x() + w() - 20 - Fl::box_dx(box())); }
00170   inline int menu_y() { return(y() + Fl::box_dy(box())); }
00171   inline int menu_w() { return(20); }
00172   inline int menu_h() { return(h() - Fl::box_dh(box())); }
00173 
00174 public:
00180   Fl_Input_Choice(int X,int Y,int W,int H,const char*L=0);
00181   
00193   void add(const char *s) { menu_->add(s);  }
00195   int changed() const { return inp_->changed() | Fl_Widget::changed(); }
00197   void clear_changed() { 
00198     inp_->clear_changed();
00199     Fl_Widget::clear_changed();
00200   }
00203   void set_changed() { 
00204     inp_->set_changed();
00205     // no need to call Fl_Widget::set_changed()
00206   }
00208   void clear() { menu_->clear(); }
00210   Fl_Boxtype down_box() const { return (menu_->down_box()); }
00212   void down_box(Fl_Boxtype b) { menu_->down_box(b); }
00214   const Fl_Menu_Item *menu() { return (menu_->menu()); }
00216   void menu(const Fl_Menu_Item *m) { menu_->menu(m); }
00217   void resize(int X, int Y, int W, int H) {
00218     Fl_Group::resize(X,Y,W,H);
00219     inp_->resize(inp_x(), inp_y(), inp_w(), inp_h());
00220     menu_->resize(menu_x(), menu_y(), menu_w(), menu_h());
00221   }
00223   Fl_Color textcolor() const { return (inp_->textcolor());}
00225   void textcolor(Fl_Color c) { inp_->textcolor(c);}
00227   Fl_Font textfont() const { return (inp_->textfont());}
00229   void textfont(Fl_Font f) { inp_->textfont(f);}
00231   Fl_Fontsize textsize() const { return (inp_->textsize()); }
00233   void textsize(Fl_Fontsize s) { inp_->textsize(s); }
00235   const char* value() const { return (inp_->value()); }
00238   void value(const char *val) { inp_->value(val); }
00241   void value(int val) {
00242     menu_->value(val);
00243     inp_->value(menu_->text(val));
00244   }
00257   Fl_Menu_Button *menubutton() { return menu_; }
00261   Fl_Input *input() { return inp_; }
00262 };
00263 
00264 #endif // !Fl_Input_Choice_H
00265 
00266 //
00267 // End of "$Id$".
00268 //
  • © 1998-2016 by Bill Spitzak and others.     FLTK

  • © 1998-2016 by Bill Spitzak and others.     FLTK

    Permission is granted to reproduce this manual or any portion for any purpose, provided this copyright and permission notice are preserved.