download CycleButton.cxx
Language: C++
Copyright: Copyright 1998-2004 by Bill Spitzak and others.
LOC: 134
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: CycleButton.cxx,v 1.7 2005/01/24 08:07:10 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".
//

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

/*! \class fltk::CycleButton

  This widget lets the user select one of a set of choices by clicking
  on it. Each click cycles to the next choice. Holding down any shift
  key or using the middle or right mouse button cycles backwards.

  Notice that the number of items can be 2. In this case this widget
  serves the common purpose of a "toggle" button that shows the
  current on/off state by changing it's label.

  This is a subclass of Menu. The possible states are defined by using
  Menu::add() or other methods that define the menu items. You can
  also put a different callback on each item. Or you can replace this
  widget's callback with your own and use value() to get the index of
  the current setting.  Items that are not visible() or are not
  active() are skipped by the cycling.

  If you set buttonbox() to NO_BOX then you must define your items to
  draw identical-sized and fully opaque images, so that drawing one
  completely obscures any other one. This was done to avoid blinking
  when drawing "artistic" user interfaces where all the entire button
  is an image.

*/

extern bool fl_hide_underscore;

static CycleButton* held_down;

void CycleButton::draw() {

  // this code is copied from Button, but simplified as a lot of
  // back-compatability and the glyphs are eliminated:

  Flags flags = current_flags_highlight();
  if (this == held_down) flags |= VALUE|PUSHED;

  Style style = *(this->style());
  if (!style.color_) style.color_ = buttoncolor();
  if (!style.box_) style.box_ = buttonbox();
  if (!style.textcolor_) style.textcolor_ = labelcolor();

  Box* box = style.box();
  Rectangle r(w(),h());

  if (!box->fills_rectangle()) {
    Color bg = highlight_color();
    if ((flags&HIGHLIGHT) && bg) {
      // highlight fills the rectangle, this is for NO_BOX
      setcolor(bg);
      fillrect(r);
    } else if (damage()&DAMAGE_EXPOSE ||
	       bg && (damage()&DAMAGE_HIGHLIGHT)) {
      draw_background();
    }
  }
  box->draw(r, &style, flags);
  box->inset(r);

  // This portion of the code is copied from Choice:
  Widget* o = get_item();
  if (o) {
    Item::set_style(&style);
    Flags saved = o->flags();
    o->set_flag(flags&(INACTIVE|VALUE|HIGHLIGHT));
    push_clip(r);
    push_matrix();
    translate(r.x(),r.y());
    int save_w = o->w(); o->w(r.w());
    int save_h = o->h(); o->h(r.h());
    fl_hide_underscore = true;
    o->draw();
    fl_hide_underscore = false;
    Item::clear_style();
    o->w(save_w);
    o->h(save_h);
    o->flags(saved);
    pop_matrix();
    pop_clip();
  }

  focusbox()->draw(r, &style, flags);
}

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

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

  int i,j;

  switch (e) {

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

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

  case PUSH:
  case DRAG:
    if (event_inside(Rectangle(w(),h()))) {
      if (held_down != this) {held_down = this; redraw(DAMAGE_VALUE);}
    } else {
      if (held_down) {held_down = 0; redraw(DAMAGE_VALUE);}
    }
    return 1;
  case RELEASE:
    if (held_down != this) return 0;
    held_down = 0;
    redraw(DAMAGE_VALUE);
    if (event_button()>1 || event_state(CTRL|SHIFT|ALT|META)) goto UP;
    else goto DOWN;

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

  case KEY:
    switch (event_key()) {
    case UpKey:
      goto UP;
    case DownKey:
    case ReturnKey:
    case SpaceKey:
      goto DOWN;
    }
    return 0;

  UP:
    i = value(); if (i < 0) i = 0;
    j = i;
    for (;;) {
      if (--j < 0) j = children-1;
      if (j==i) break;
      if (try_item(this, j)) return 1;
    }
    return 0;

  DOWN:
    i = value(); if (i < 0) i = 0;
    j = i;
    for (;;) {
      if (++j >= children) j = 0;
      if (j == i) break;
      if (try_item(this, j)) return 1;
    }
    return 0;

  default:
    return 0;
  }
}

static NamedStyle style("CycleButton", 0, &CycleButton::default_style);
NamedStyle* CycleButton::default_style = &::style;

CycleButton::CycleButton(int x,int y,int w,int h, const char *l)
  : Menu(x,y,w,h,l)
{
  value(0);
  style(::style);
  clear_flag(ALIGN_MASK);
  set_flag(ALIGN_LEFT);
  //set_click_to_focus();
}

//
// End of "$Id: CycleButton.cxx,v 1.7 2005/01/24 08:07:10 spitzak Exp $".
//

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us