anybutton.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/anybutton.h
  3. // Purpose: wxAnyButtonBase class
  4. // Author: Vadim Zetlin
  5. // Created: 2000-08-15 (extracted from button.h)
  6. // Copyright: (c) Vadim Zetlin
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_ANYBUTTON_H_BASE_
  10. #define _WX_ANYBUTTON_H_BASE_
  11. #include "wx/defs.h"
  12. #ifdef wxHAS_ANY_BUTTON
  13. // ----------------------------------------------------------------------------
  14. // wxAnyButton specific flags
  15. // ----------------------------------------------------------------------------
  16. // These flags affect label alignment
  17. #define wxBU_LEFT 0x0040
  18. #define wxBU_TOP 0x0080
  19. #define wxBU_RIGHT 0x0100
  20. #define wxBU_BOTTOM 0x0200
  21. #define wxBU_ALIGN_MASK ( wxBU_LEFT | wxBU_TOP | wxBU_RIGHT | wxBU_BOTTOM )
  22. // These two flags are obsolete
  23. #define wxBU_NOAUTODRAW 0x0000
  24. #define wxBU_AUTODRAW 0x0004
  25. // by default, the buttons will be created with some (system dependent)
  26. // minimal size to make them look nicer, giving this style will make them as
  27. // small as possible
  28. #define wxBU_EXACTFIT 0x0001
  29. // this flag can be used to disable using the text label in the button: it is
  30. // mostly useful when creating buttons showing bitmap and having stock id as
  31. // without it both the standard label corresponding to the stock id and the
  32. // bitmap would be shown
  33. #define wxBU_NOTEXT 0x0002
  34. #include "wx/bitmap.h"
  35. #include "wx/control.h"
  36. // ----------------------------------------------------------------------------
  37. // wxAnyButton: common button functionality
  38. // ----------------------------------------------------------------------------
  39. class WXDLLIMPEXP_CORE wxAnyButtonBase : public wxControl
  40. {
  41. public:
  42. wxAnyButtonBase() { }
  43. // show the image in the button in addition to the label: this method is
  44. // supported on all (major) platforms
  45. void SetBitmap(const wxBitmap& bitmap, wxDirection dir = wxLEFT)
  46. {
  47. SetBitmapLabel(bitmap);
  48. SetBitmapPosition(dir);
  49. }
  50. wxBitmap GetBitmap() const { return DoGetBitmap(State_Normal); }
  51. // Methods for setting individual images for different states: normal,
  52. // selected (meaning pushed or pressed), focused (meaning normal state for
  53. // a focused button), disabled or hover (a.k.a. hot or current).
  54. //
  55. // Remember that SetBitmap() itself must be called before any other
  56. // SetBitmapXXX() methods (except for SetBitmapLabel() which is a synonym
  57. // for it anyhow) and that all bitmaps passed to these functions should be
  58. // of the same size.
  59. void SetBitmapLabel(const wxBitmap& bitmap)
  60. { DoSetBitmap(bitmap, State_Normal); }
  61. void SetBitmapPressed(const wxBitmap& bitmap)
  62. { DoSetBitmap(bitmap, State_Pressed); }
  63. void SetBitmapDisabled(const wxBitmap& bitmap)
  64. { DoSetBitmap(bitmap, State_Disabled); }
  65. void SetBitmapCurrent(const wxBitmap& bitmap)
  66. { DoSetBitmap(bitmap, State_Current); }
  67. void SetBitmapFocus(const wxBitmap& bitmap)
  68. { DoSetBitmap(bitmap, State_Focused); }
  69. wxBitmap GetBitmapLabel() const { return DoGetBitmap(State_Normal); }
  70. wxBitmap GetBitmapPressed() const { return DoGetBitmap(State_Pressed); }
  71. wxBitmap GetBitmapDisabled() const { return DoGetBitmap(State_Disabled); }
  72. wxBitmap GetBitmapCurrent() const { return DoGetBitmap(State_Current); }
  73. wxBitmap GetBitmapFocus() const { return DoGetBitmap(State_Focused); }
  74. // set the margins around the image
  75. void SetBitmapMargins(wxCoord x, wxCoord y) { DoSetBitmapMargins(x, y); }
  76. void SetBitmapMargins(const wxSize& sz) { DoSetBitmapMargins(sz.x, sz.y); }
  77. wxSize GetBitmapMargins() { return DoGetBitmapMargins(); }
  78. // set the image position relative to the text, i.e. wxLEFT means that the
  79. // image is to the left of the text (this is the default)
  80. void SetBitmapPosition(wxDirection dir);
  81. // Buttons on MSW can look bad if they are not native colours, because
  82. // then they become owner-drawn and not theme-drawn. Disable it here
  83. // in wxAnyButtonBase to make it consistent.
  84. virtual bool ShouldInheritColours() const { return false; }
  85. // wxUniv-compatible and deprecated equivalents to SetBitmapXXX()
  86. #if WXWIN_COMPATIBILITY_2_8
  87. void SetImageLabel(const wxBitmap& bitmap) { SetBitmap(bitmap); }
  88. void SetImageMargins(wxCoord x, wxCoord y) { SetBitmapMargins(x, y); }
  89. #endif // WXWIN_COMPATIBILITY_2_8
  90. // backwards compatible names for pressed/current bitmaps: they're not
  91. // deprecated as there is nothing really wrong with using them and no real
  92. // advantage to using the new names but the new names are still preferred
  93. wxBitmap GetBitmapSelected() const { return GetBitmapPressed(); }
  94. wxBitmap GetBitmapHover() const { return GetBitmapCurrent(); }
  95. void SetBitmapSelected(const wxBitmap& bitmap) { SetBitmapPressed(bitmap); }
  96. void SetBitmapHover(const wxBitmap& bitmap) { SetBitmapCurrent(bitmap); }
  97. // this enum is not part of wx public API, it is public because it is used
  98. // in non wxAnyButton-derived classes internally
  99. //
  100. // also notice that MSW code relies on the values of the enum elements, do
  101. // not change them without revising src/msw/button.cpp
  102. enum State
  103. {
  104. State_Normal,
  105. State_Current, // a.k.a. hot or "hovering"
  106. State_Pressed, // a.k.a. "selected" in public API for some reason
  107. State_Disabled,
  108. State_Focused,
  109. State_Max
  110. };
  111. // return true if this button shouldn't show the text label, either because
  112. // it doesn't have it or because it was explicitly disabled with wxBU_NOTEXT
  113. bool DontShowLabel() const
  114. {
  115. return HasFlag(wxBU_NOTEXT) || GetLabel().empty();
  116. }
  117. // return true if we do show the label
  118. bool ShowsLabel() const
  119. {
  120. return !DontShowLabel();
  121. }
  122. protected:
  123. // choose the default border for this window
  124. virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
  125. virtual wxBitmap DoGetBitmap(State WXUNUSED(which)) const
  126. { return wxBitmap(); }
  127. virtual void DoSetBitmap(const wxBitmap& WXUNUSED(bitmap),
  128. State WXUNUSED(which))
  129. { }
  130. virtual wxSize DoGetBitmapMargins() const
  131. { return wxSize(0, 0); }
  132. virtual void DoSetBitmapMargins(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
  133. { }
  134. virtual void DoSetBitmapPosition(wxDirection WXUNUSED(dir))
  135. { }
  136. virtual bool DoGetAuthNeeded() const { return false; }
  137. virtual void DoSetAuthNeeded(bool WXUNUSED(show)) { }
  138. wxDECLARE_NO_COPY_CLASS(wxAnyButtonBase);
  139. };
  140. #if defined(__WXUNIVERSAL__)
  141. #include "wx/univ/anybutton.h"
  142. #elif defined(__WXMSW__)
  143. #include "wx/msw/anybutton.h"
  144. //#elif defined(__WXMOTIF__)
  145. // #include "wx/motif/anybutton.h"
  146. #elif defined(__WXGTK20__)
  147. #include "wx/gtk/anybutton.h"
  148. //#elif defined(__WXGTK__)
  149. // #include "wx/gtk1/anybutton.h"
  150. #elif defined(__WXMAC__)
  151. #include "wx/osx/anybutton.h"
  152. //#elif defined(__WXCOCOA__)
  153. // #include "wx/cocoa/anybutton.h"
  154. //#elif defined(__WXPM__)
  155. // #include "wx/os2/anybutton.h"
  156. #else
  157. typedef wxAnyButtonBase wxAnyButton;
  158. #endif
  159. #endif // wxHAS_ANY_BUTTON
  160. #endif // _WX_ANYBUTTON_H_BASE_