button.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: button.h
  3. // Purpose: interface of wxButton
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxButton
  9. A button is a control that contains a text string, and is one of the most
  10. common elements of a GUI.
  11. It may be placed on a @ref wxDialog "dialog box" or on a @ref wxPanel panel,
  12. or indeed on almost any other window.
  13. By default, i.e. if none of the alignment styles are specified, the label
  14. is centered both horizontally and vertically. If the button has both a
  15. label and a bitmap, the alignment styles above specify the location of the
  16. rectangle combining both the label and the bitmap and the bitmap position
  17. set with wxButton::SetBitmapPosition() defines the relative position of the
  18. bitmap with respect to the label (however currently non-default alignment
  19. combinations are not implemented on all platforms).
  20. Since version 2.9.1 wxButton supports showing both text and an image
  21. (currently only when using wxMSW, wxGTK or wxOSX/Cocoa ports), see
  22. SetBitmap() and SetBitmapLabel(), SetBitmapDisabled() &c methods. In the
  23. previous wxWidgets versions this functionality was only available in (the
  24. now trivial) wxBitmapButton class which was only capable of showing an
  25. image without text.
  26. A button may have either a single image for all states or different images
  27. for the following states (different images are not currently supported
  28. under OS X where the normal image is used for all states):
  29. @li @b normal: the default state
  30. @li @b disabled: bitmap shown when the button is disabled.
  31. @li @b pressed: bitmap shown when the button is pushed (e.g. while the user
  32. keeps the mouse button pressed on it)
  33. @li @b focus: bitmap shown when the button has keyboard focus (but is not
  34. pressed as in this case the button is in the pressed state)
  35. @li @b current: bitmap shown when the mouse is over the button (but it is
  36. not pressed although it may have focus). Notice that if current bitmap
  37. is not specified but the current platform UI uses hover images for the
  38. buttons (such as Windows XP or GTK+), then the focus bitmap is used for
  39. hover state as well. This makes it possible to set focus bitmap only to
  40. get reasonably good behaviour on all platforms.
  41. All of the bitmaps must be of the same size and the normal bitmap must be
  42. set first (to a valid bitmap), before setting any other ones. Also, if the
  43. size of the bitmaps is changed later, you need to change the size of the
  44. normal bitmap before setting any other bitmaps with the new size (and you
  45. do need to reset all of them as their original values can be lost when the
  46. normal bitmap size changes).
  47. The position of the image inside the button be configured using
  48. SetBitmapPosition(). By default the image is on the left of the text.
  49. Please also notice that GTK+ uses a global setting called @c gtk-button-images
  50. to determine if the images should be shown in the buttons
  51. at all. If it is off (which is the case in e.g. Gnome 2.28 by default), no
  52. images will be shown, consistently with the native behaviour.
  53. @beginStyleTable
  54. @style{wxBU_LEFT}
  55. Left-justifies the label. Windows and GTK+ only.
  56. @style{wxBU_TOP}
  57. Aligns the label to the top of the button. Windows and GTK+ only.
  58. @style{wxBU_RIGHT}
  59. Right-justifies the bitmap label. Windows and GTK+ only.
  60. @style{wxBU_BOTTOM}
  61. Aligns the label to the bottom of the button. Windows and GTK+ only.
  62. @style{wxBU_EXACTFIT}
  63. By default, all buttons are made of at least the standard button size,
  64. even if their contents is small enough to fit into a smaller size. This
  65. is done for consistency as most platforms use buttons of the same size
  66. in the native dialogs, but can be overridden by specifying this flag.
  67. If it is given, the button will be made just big enough for its
  68. contents. Notice that under MSW the button will still have at least the
  69. standard height, even with this style, if it has a non-empty label.
  70. @style{wxBU_NOTEXT}
  71. Disables the display of the text label in the button even if it has one
  72. or its id is one of the standard stock ids with an associated label:
  73. without using this style a button which is only supposed to show a
  74. bitmap but uses a standard id would display a label too.
  75. @style{wxBORDER_NONE}
  76. Creates a button without border. This is currently implemented in MSW,
  77. GTK2 and OSX/Cocoa and OSX/Carbon ports but in the latter only applies
  78. to buttons with bitmaps and using bitmap of one of the standard sizes
  79. only, namely 128*128, 48*48, 24*24 or 16*16. In all the other cases
  80. wxBORDER_NONE is ignored under OSX/Carbon (these restrictions don't
  81. exist in OSX/Cocoa however).
  82. @endStyleTable
  83. @beginEventEmissionTable{wxCommandEvent}
  84. @event{EVT_BUTTON(id, func)}
  85. Process a @c wxEVT_BUTTON event, when the button is clicked.
  86. @endEventTable
  87. @library{wxcore}
  88. @category{ctrl}
  89. @appearance{button}
  90. @see wxBitmapButton
  91. */
  92. class wxButton : public wxAnyButton
  93. {
  94. public:
  95. /**
  96. Default ctor.
  97. */
  98. wxButton();
  99. /**
  100. Constructor, creating and showing a button.
  101. The preferred way to create standard buttons is to use default value of
  102. @a label. If no label is supplied and @a id is one of standard IDs from
  103. @ref page_stockitems "this list", a standard label will be used. In
  104. other words, if you use a predefined @c wxID_XXX constant, just omit
  105. the label completely rather than specifying it. In particular, help
  106. buttons (the ones with @a id of @c wxID_HELP) under Mac OS X can't
  107. display any label at all and while wxButton will detect if the standard
  108. "Help" label is used and ignore it, using any other label will prevent
  109. the button from correctly appearing as a help button and so should be
  110. avoided.
  111. In addition to that, the button will be decorated with stock icons under GTK+ 2.
  112. @param parent
  113. Parent window. Must not be @NULL.
  114. @param id
  115. Button identifier. A value of @c wxID_ANY indicates a default value.
  116. @param label
  117. Text to be displayed on the button.
  118. @param pos
  119. Button position.
  120. @param size
  121. Button size. If the default size is specified then the button is sized
  122. appropriately for the text.
  123. @param style
  124. Window style. See wxButton class description.
  125. @param validator
  126. Window validator.
  127. @param name
  128. Window name.
  129. @see Create(), wxValidator
  130. */
  131. wxButton(wxWindow* parent, wxWindowID id,
  132. const wxString& label = wxEmptyString,
  133. const wxPoint& pos = wxDefaultPosition,
  134. const wxSize& size = wxDefaultSize,
  135. long style = 0,
  136. const wxValidator& validator = wxDefaultValidator,
  137. const wxString& name = wxButtonNameStr);
  138. /**
  139. Button creation function for two-step creation.
  140. For more details, see wxButton().
  141. */
  142. bool Create(wxWindow* parent, wxWindowID id,
  143. const wxString& label = wxEmptyString,
  144. const wxPoint& pos = wxDefaultPosition,
  145. const wxSize& size = wxDefaultSize,
  146. long style = 0,
  147. const wxValidator& validator = wxDefaultValidator,
  148. const wxString& name = wxButtonNameStr);
  149. /**
  150. Returns @true if an authentication needed symbol is displayed on the
  151. button.
  152. @remarks This method always returns @false if the platform is not
  153. Windows Vista or newer.
  154. @see SetAuthNeeded()
  155. @since 2.9.1
  156. */
  157. bool GetAuthNeeded() const;
  158. /**
  159. Returns the default size for the buttons. It is advised to make all the dialog
  160. buttons of the same size and this function allows to retrieve the (platform and
  161. current font dependent size) which should be the best suited for this.
  162. */
  163. static wxSize GetDefaultSize();
  164. /**
  165. Returns the string label for the button.
  166. @see SetLabel()
  167. */
  168. wxString GetLabel() const;
  169. /**
  170. Sets whether an authentication needed symbol should be displayed on the
  171. button.
  172. @remarks This method doesn't do anything if the platform is not Windows
  173. Vista or newer.
  174. @see GetAuthNeeded()
  175. @since 2.9.1
  176. */
  177. void SetAuthNeeded(bool needed = true);
  178. /**
  179. This sets the button to be the default item in its top-level window
  180. (e.g. the panel or the dialog box containing it).
  181. As normal, pressing return causes the default button to be depressed when
  182. the return key is pressed.
  183. See also wxWindow::SetFocus() which sets the keyboard focus for windows
  184. and text panel items, and wxTopLevelWindow::SetDefaultItem().
  185. @remarks Under Windows, only dialog box buttons respond to this function.
  186. @return the old default item (possibly @NULL)
  187. */
  188. virtual wxWindow* SetDefault();
  189. /**
  190. Sets the string label for the button.
  191. @param label
  192. The label to set.
  193. */
  194. void SetLabel(const wxString& label);
  195. };