Filter:   InfoImg
download Fl_Choice.cxx
Language: C++
Copyright: Copyright 1998-2004 by Bill Spitzak and others.
LOC: 140
Project Info
Fast Light Tool Kit(fltk)
Server: SourceForge
Type: cvs
...Forge\f\fltk\fltk\fltk\src\
   allfiles.xbm
   ARRAY.h
   browser_minus.xbm
   browser_plus.xbm
   cmap.cxx
   CycleButton.cxx
   d1.xbm
   d1_mask.xbm
   dlload_osx.cxx
   dump_compose.c
   ew.xbm
   ew_mask.xbm
   fastarrow.h
   filename_absolute.cxx
   filename_expand.cxx
   filename_ext.cxx
   filename_isdir.cxx
   filename_list.cxx
   filename_match.cxx
   filename_setext.cxx
   Fl.cxx
   Fl_abort.cxx
   Fl_add_idle.cxx
   Fl_Adjuster.cxx
   Fl_Align_Group.cxx
   fl_arc.cxx
   fl_arci.cxx
   Fl_arg.cxx
   fl_ask.cxx
   Fl_Bar.cxx
   Fl_Bitmap.cxx
   fl_bmp.cxx
   Fl_Box.cxx
   Fl_Boxtype.cxx
   Fl_Browser.cxx
   Fl_Browser_load.cxx
   Fl_Button.cxx
   fl_call_main.c
   Fl_Check_Button.cxx
   Fl_Choice.cxx
   fl_clip.cxx
   Fl_Clock.cxx
   fl_cmap.h
   fl_color.cxx
   Fl_Color_Chooser.cxx
   fl_color_mac.cxx
   fl_color_win32.cxx
   fl_color_x.cxx
   Fl_compose.cxx
   fl_cursor.cxx
   fl_curve.cxx
   Fl_Dial.cxx
   fl_diamond_box.cxx
   Fl_display.cxx
   fl_dnd.cxx
   fl_dnd_mac.cxx
   fl_dnd_win32.cxx
   fl_dnd_x.cxx
   fl_draw.cxx
   fl_draw_image.cxx
   fl_draw_image_mac.cxx
   fl_draw_image_win32.cxx
   fl_draw_image_x.cxx
   fl_draw_pixmap.cxx
   fl_engraved_label.cxx
   fl_file_chooser.cxx
   Fl_FileBrowser.cxx
   Fl_FileChooser.cxx
   Fl_FileChooser.fl
   Fl_FileChooser2.cxx
   Fl_FileIcon.cxx
   Fl_FileIcon2.cxx
   Fl_FileInput.cxx
   Fl_Float_Input.cxx
   fl_font.cxx
   fl_font_mac.cxx
   fl_font_win32.cxx
   fl_font_x.cxx
   fl_font_xft.cxx
   Fl_get_key.cxx
   Fl_get_key_mac.cxx
   Fl_get_key_win32.cxx
   fl_gif.cxx
   fl_glyph.cxx
   Fl_Group.cxx
   Fl_Help_Dialog.cxx
   Fl_Help_View.cxx
   Fl_Highlight_Button.cxx
   Fl_Image.cxx
   Fl_Input.cxx
   Fl_Input_Browser.cxx
   Fl_Item.cxx
   Fl_key_name.cxx
   fl_labeltype.cxx
   Fl_Light_Button.cxx
   fl_list_fonts.cxx
   fl_list_fonts_mac.cxx
   fl_list_fonts_win32.cxx
   fl_list_fonts_x.cxx
   fl_load_plugin.cxx
   Fl_lock.cxx
   Fl_mac.cxx
   Fl_Menu.cxx
   Fl_Menu_.cxx
   Fl_Menu_add.cxx
   Fl_Menu_Bar.cxx
   Fl_Menu_Button.cxx
   Fl_Menu_global.cxx
   Fl_Menu_Item.cxx
   Fl_Menu_Window.cxx
   Fl_Multi_Image.cxx
   Fl_Numeric_Input.cxx
   Fl_Output.cxx
   fl_oval_box.cxx
   fl_overlay.cxx
   fl_overlay_visual.cxx
   Fl_own_colormap.cxx
   Fl_Pack.cxx
   Fl_Pixmap.cxx
   fl_plastic_box.cxx
   Fl_ProgressBar.cxx
   Fl_Radio_Button.cxx
   fl_rect.cxx
   Fl_Repeat_Button.cxx
   Fl_Return_Button.cxx
   fl_rgb.cxx
   Fl_RGB_Image.cxx
   Fl_Roller.cxx
   fl_round_box.cxx
   fl_rounded_box.cxx
   Fl_Scroll.cxx
   fl_scroll_area.cxx
   Fl_Scrollbar.cxx
   fl_set_fonts_mac.cxx
   fl_shadow_box.cxx
   Fl_Shaped_Window.cxx
   Fl_Shared_Image.cxx
   fl_show_colormap.cxx
   Fl_Single_Window.cxx
   Fl_Slider.cxx
   Fl_String_List.cxx
   Fl_Style.cxx
   Fl_Style_Set.cxx
   fl_symbols.cxx
   Fl_Tabs.cxx
   Fl_Text_Buffer.cxx
   Fl_Text_Display.cxx
   Fl_Text_Editor.cxx
   Fl_Tile.cxx
   Fl_Tiled_Image.cxx
   Fl_Tooltip.cxx
   Fl_Valuator.cxx
   Fl_Value_Input.cxx
   Fl_Value_Slider.cxx
   fl_vertex.cxx
   Fl_visual.cxx
   Fl_Widget.cxx
   Fl_win32.cxx
   Fl_Window.cxx
   Fl_Window_fullscreen.cxx
   Fl_Window_hotspot.cxx
   Fl_Window_iconize.cxx
   Fl_x.cxx
   Fl_XColor.h
   fl_xpm.cxx
   fltk_theme.cxx
   fltk_theme_win32.cxx
   fltk_theme_x.cxx
   IFont.h
   mediumarrow.h
   new.xbm
   ns.xbm
   ns_mask.xbm
   pktdef.h
   scandir.c
   scandir_win32.c
   shortcuts.cxx
   slowarrow.h
   string.c
   up.xbm
   utf.c
   ValueOutput.cxx
   vsnprintf.c
   wintab.h
   wintabx.h
   XColorMap.h
   xutf8.cxx

// "$Id: Fl_Choice.cxx,v 1.81 2005/01/24 08:34:28 spitzak Exp $"
//
// Copyright 1998-2004 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".

/*! \class fltk::Choice

Subclass of fltk::Menu that provides a button that pops up the menu, and
also displays the text of the most-recently selected menu item.

\image html choice.gif

The appearance is designed to look like an "uneditable ComboBox" in
Windows, but it is somewhat different in that it does not contain a
text editor, also the menu pops up with the current item under the
cursor, which is immensely easier to use once you get used to it. This
is the same UI as the Macintosh and Motif, which called this an
OptionButton.

The user can change the value by popping up the menu by clicking
anywhere in the widget and moving the cursor to a different item, or
by typing up and down arrow keys to cycle amoung the items.  Typing
the fltk::Widget::shortcut() of any of the
items will also change the value to that item.

If you set a shortcut() on this widget itself or put &x in the label,
that shortcut will pop up the menu. The user can then use arrow keys
or the mouse to change the selected item.

When the user changes the value() the callback is done.

If you wish to display text that is different than any of the menu
items, you may instead want an fltk::PopupMenu. It works identically
but instead displays an empty box with the label() inside it, you
can then change the label() as needed.

If you want a "real" ComboBox where the user edits the text, this is
a planned addition to the fltk::Input widget. All text input will have
menus of possible replacements and completions. Not yet implemented,
unfortunately.

*/

#include <fltk/Choice.h>
#include <fltk/events.h>
#include <fltk/damage.h>
#include <fltk/Box.h>
#include <fltk/Item.h>
#include <fltk/draw.h>
using namespace fltk;

// The dimensions for the glyph in this and the PopupMenu are exactly
// the same, so that glyphs may be shared between them.

extern bool fl_hide_underscore;

/*! You can change the icon drawn on the right edge by setting glyph()
  to your own function that draws whatever you want.
*/
void Choice::draw() {
  if (damage() & DAMAGE_ALL) draw_frame();
  Rectangle r(w(),h()); box()->inset(r);
  int w1 = r.h()*4/5;
  r.move_r(-w1);
  // draw the little mark at the right:
  if (damage() & (DAMAGE_ALL|DAMAGE_HIGHLIGHT)) {
    Flags flags = current_flags_highlight();
    Rectangle gr(r.r(), r.y(), w1, r.h());
    draw_glyph(GLYPH_DOWN_BUTTON, gr, flags & ~FOCUSED);
  }
  if (damage() & (DAMAGE_ALL|DAMAGE_VALUE)) {
    setcolor(color());
    fillrect(r);
    if (focused()) {
      setcolor(selection_color());
      Rectangle fr(r); fr.inset(2); fillrect(fr);
    }
    Widget* o = get_item();
    //if (!o && children()) o = child(0);
    if (o) {
      Item::set_style(this);
      Flags saved = o->flags();
      if (focused()) o->set_flag(SELECTED);
      else o->clear_flag(SELECTED);
      if (!active_r()) o->set_flag(INACTIVE);
      r.move_x(2);
      push_clip(r);
      push_matrix();
      translate(r.x(), r.y()+((r.h()-o->height())>>1));
      int save_w = o->w(); o->w(r.r());
      fl_hide_underscore = true;
      o->draw();
      fl_hide_underscore = false;
      Item::clear_style();
      o->w(save_w);
      o->flags(saved);
      pop_matrix();
      pop_clip();
    }
  }
}

static bool try_item(Choice* choice, int i) {
  Widget* w = choice->child(i);
  if (!w->takesevents()) return false;
  choice->value(i);
  choice->execute(w);
  return true;
}

int Choice::handle(int e) {
  int children = this->children(0,0);
  if (!children) return 0;
  switch (e) {

  case FOCUS:
  case UNFOCUS:
    redraw(DAMAGE_VALUE);
    return 1;

  case ENTER:
  case LEAVE:
    redraw_highlight();
  case MOVE:
    return 1;

  case PUSH:
    // Normally a mouse click pops up the menu. If you uncomment this line
    // (or make a subclass that does this), a mouse click will re-pick the
    // current item (it will popup the menu and immediately dismiss it).
    // Depending on the size and usage of the menu this may be more
    // user-friendly.
//  event_is_click(0);
    if (click_to_focus()) take_focus();
  EXECUTE:
    if (popup(0, 0, w(), h(), 0)) redraw(DAMAGE_VALUE);
    return 1;

  case SHORTCUT:
    if (test_shortcut()) goto EXECUTE;
    if (handle_shortcut()) {redraw(DAMAGE_VALUE); return 1;}
    return 0;

  case KEY:
    switch (event_key()) {

    case ReturnKey:
    case SpaceKey:
      goto EXECUTE;

    case UpKey: {
      int i = value(); if (i < 0) i = children;
      while (i > 0) {--i; if (try_item(this, i)) return 1;}
      return 1;}
    case DownKey: {
      int i = value();
      while (++i < children) if (try_item(this,i)) return 1;
      return 1;}
    }
    return 0;

  default:
    return 0;
  }
}

#define MOTIF_STYLE 0

#if MOTIF_STYLE
// Glyph erases the area and draws a long, narrow box:
static void glyph(const Widget* widget, int,
		  int x,int y,int w,int h, Flags)
{
  color(widget->buttoncolor());
  fillrect(x,y,w,h);
  // draw a long narrow box:
  Widget::default_style->glyph(widget, 0, x, y+(h-h/3)/2, w-2, h/3, 0);
}
#endif

#define MAC_STYLE 0
#if MAC_STYLE
// Attempt to draw an up/down arrow like the Mac uses, since the
// popup menu is more like how the Mac works:
static void glyph(const Widget* widget, int,
		  int x,int y,int w,int h, Flags flags)
{
  Widget::default_style->glyph(widget, 0, x, y, w, h, flags);
  x += 2;
  w -= 4;
  y = y+h/2;
  h = (w+1)/2;
  Widget::default_style->glyph(widget, GLYPH_UP, x, y-h-1, w, h, flags);
  Widget::default_style->glyph(widget, GLYPH_DOWN, x, y+1, w, h, flags);
}
#endif

static void revert(Style* s) {
#if MOTIF_STYLE
  s->color = GRAY75;
  s->box = s->buttonbox = Widget::default_style->buttonbox;
  s->glyph = ::glyph;
#endif
#if MAC_STYLE
  s->glyph = ::glyph;
#endif
}
static NamedStyle style("Choice", revert, &Choice::default_style);
NamedStyle* Choice::default_style = &::style;

/*! The constructor makes the menu empty. See Menu and StringList
  for information on how to set the menu to a list of items.
*/
Choice::Choice(int x,int y,int w,int h, const char *l) : Menu(x,y,w,h,l) {
  value(0);
  style(default_style);
  clear_flag(ALIGN_MASK);
  set_flag(ALIGN_LEFT);
  set_click_to_focus();
  when(WHEN_RELEASE);
}

//
// End of "$Id: Fl_Choice.cxx,v 1.81 2005/01/24 08:34:28 spitzak Exp $".
//