Fl_Button.H 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // "$Id: Fl_Button.H 10386 2014-10-19 20:17:17Z AlbrechtS $"
  3. //
  4. // Button header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2014 by Bill Spitzak and others.
  7. //
  8. // This library is free software. Distribution and use rights are outlined in
  9. // the file "COPYING" which should have been included with this file. If this
  10. // file is missing or damaged, see the license at:
  11. //
  12. // http://www.fltk.org/COPYING.php
  13. //
  14. // Please report all bugs and problems on the following page:
  15. //
  16. // http://www.fltk.org/str.php
  17. //
  18. /* \file
  19. Fl_Button widget . */
  20. #ifndef Fl_Button_H
  21. #define Fl_Button_H
  22. #ifndef Fl_Widget_H
  23. #include "Fl_Widget.H"
  24. #endif
  25. // values for type()
  26. #define FL_NORMAL_BUTTON 0 /**< value() will be set to 1 during the press of the button and
  27. reverts back to 0 when the button is released */
  28. #define FL_TOGGLE_BUTTON 1 ///< value() toggles between 0 and 1 at every click of the button
  29. #define FL_RADIO_BUTTON (FL_RESERVED_TYPE+2) /**< is set to 1 at button press, and all other
  30. buttons in the same group with <tt>type() == FL_RADIO_BUTTON</tt>
  31. are set to zero.*/
  32. #define FL_HIDDEN_BUTTON 3 ///< for Forms compatibility
  33. extern FL_EXPORT Fl_Shortcut fl_old_shortcut(const char*);
  34. class Fl_Widget_Tracker;
  35. /**
  36. \class Fl_Button
  37. \brief Buttons generate callbacks when they are clicked by the user.
  38. You control exactly when and how by changing the values for type() and
  39. when(). Buttons can also generate callbacks in response to \c FL_SHORTCUT
  40. events. The button can either have an explicit shortcut(int s) value or a
  41. letter shortcut can be indicated in the label() with an '\&' character
  42. before it. For the label shortcut it does not matter if \e Alt is held
  43. down, but if you have an input field in the same window, the user will have
  44. to hold down the \e Alt key so that the input field does not eat the event
  45. first as an \c FL_KEYBOARD event.
  46. \todo Refactor the doxygen comments for Fl_Button type() documentation.
  47. For an Fl_Button object, the type() call returns one of:
  48. \li \c FL_NORMAL_BUTTON (0): value() remains unchanged after button press.
  49. \li \c FL_TOGGLE_BUTTON: value() is inverted after button press.
  50. \li \c FL_RADIO_BUTTON: value() is set to 1 after button press, and all other
  51. buttons in the current group with <tt>type() == FL_RADIO_BUTTON</tt>
  52. are set to zero.
  53. \todo Refactor the doxygen comments for Fl_Button when() documentation.
  54. For an Fl_Button object, the following when() values are useful, the default
  55. being \c FL_WHEN_RELEASE:
  56. \li \c 0: The callback is not done, instead changed() is turned on.
  57. \li \c FL_WHEN_RELEASE: The callback is done after the user successfully
  58. clicks the button, or when a shortcut is typed.
  59. \li \c FL_WHEN_CHANGED: The callback is done each time the value() changes
  60. (when the user pushes and releases the button, and as the mouse is
  61. dragged around in and out of the button).
  62. */
  63. class FL_EXPORT Fl_Button : public Fl_Widget {
  64. int shortcut_;
  65. char value_;
  66. char oldval;
  67. uchar down_box_;
  68. uchar handle_return_;
  69. protected:
  70. static Fl_Widget_Tracker *key_release_tracker;
  71. static void key_release_timeout(void*);
  72. void simulate_key_action();
  73. virtual void draw();
  74. public:
  75. virtual int handle(int);
  76. Fl_Button(int X, int Y, int W, int H, const char *L = 0);
  77. int value(int v);
  78. /**
  79. Returns the current value of the button (0 or 1).
  80. */
  81. char value() const {return value_;}
  82. /**
  83. Same as \c value(1).
  84. \see value(int v)
  85. */
  86. int set() {return value(1);}
  87. /**
  88. Same as \c value(0).
  89. \see value(int v)
  90. */
  91. int clear() {return value(0);}
  92. void setonly(); // this should only be called on FL_RADIO_BUTTONs
  93. /**
  94. Returns the current shortcut key for the button.
  95. \retval int
  96. */
  97. int shortcut() const {return shortcut_;}
  98. /**
  99. Sets the shortcut key to \c s.
  100. Setting this overrides the use of '\&' in the label().
  101. The value is a bitwise OR of a key and a set of shift flags, for example:
  102. <tt>FL_ALT | 'a'</tt>, or
  103. <tt>FL_ALT | (FL_F + 10)</tt>, or just
  104. <tt>'a'</tt>.
  105. A value of 0 disables the shortcut.
  106. The key can be any value returned by Fl::event_key(), but will usually be
  107. an ASCII letter. Use a lower-case letter unless you require the shift key
  108. to be held down.
  109. The shift flags can be any set of values accepted by Fl::event_state().
  110. If the bit is on, that shift key must be pushed. Meta, Alt, Ctrl, and
  111. Shift must be off if they are not in the shift flags (zero for the other
  112. bits indicates a "don't care" setting).
  113. \param[in] s bitwise OR of key and shift flags
  114. */
  115. void shortcut(int s) {shortcut_ = s;}
  116. void handle_return(uchar bval) {handle_return_ = bval;}
  117. int handle_return() const {return handle_return_;}
  118. /**
  119. Returns the current down box type, which is drawn when value() is non-zero.
  120. \retval Fl_Boxtype
  121. */
  122. Fl_Boxtype down_box() const {return (Fl_Boxtype)down_box_;}
  123. /**
  124. Sets the down box type. The default value of 0 causes FLTK to figure out
  125. the correct matching down version of box().
  126. Some derived classes (e.g. Fl_Round_Button and Fl_Light_Button use
  127. down_box() for special purposes. See docs of these classes.
  128. \param[in] b down box type
  129. */
  130. void down_box(Fl_Boxtype b) {down_box_ = b;}
  131. /// (for backwards compatibility)
  132. void shortcut(const char *s) {shortcut(fl_old_shortcut(s));}
  133. /// (for backwards compatibility)
  134. Fl_Color down_color() const {return selection_color();}
  135. /// (for backwards compatibility)
  136. void down_color(unsigned c) {selection_color(c);}
  137. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Button, Fl_Widget)
  138. };
  139. #endif
  140. //
  141. // End of "$Id: Fl_Button.H 10386 2014-10-19 20:17:17Z AlbrechtS $".
  142. //