clrpicker.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/clrpicker.h
  3. // Purpose: wxColourPickerCtrl base header
  4. // Author: Francesco Montorsi (based on Vadim Zeitlin's code)
  5. // Modified by:
  6. // Created: 14/4/2006
  7. // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
  8. // Licence: wxWindows Licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CLRPICKER_H_BASE_
  11. #define _WX_CLRPICKER_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_COLOURPICKERCTRL
  14. #include "wx/pickerbase.h"
  15. class WXDLLIMPEXP_FWD_CORE wxColourPickerEvent;
  16. extern WXDLLIMPEXP_DATA_CORE(const char) wxColourPickerWidgetNameStr[];
  17. extern WXDLLIMPEXP_DATA_CORE(const char) wxColourPickerCtrlNameStr[];
  18. // show the colour in HTML form (#AABBCC) as colour button label
  19. #define wxCLRBTN_SHOW_LABEL 100
  20. // the default style
  21. #define wxCLRBTN_DEFAULT_STYLE (wxCLRBTN_SHOW_LABEL)
  22. // ----------------------------------------------------------------------------
  23. // wxColourPickerWidgetBase: a generic abstract interface which must be
  24. // implemented by controls used by wxColourPickerCtrl
  25. // ----------------------------------------------------------------------------
  26. class WXDLLIMPEXP_CORE wxColourPickerWidgetBase
  27. {
  28. public:
  29. wxColourPickerWidgetBase() { m_colour = *wxBLACK; }
  30. virtual ~wxColourPickerWidgetBase() {}
  31. wxColour GetColour() const
  32. { return m_colour; }
  33. virtual void SetColour(const wxColour &col)
  34. { m_colour = col; UpdateColour(); }
  35. virtual void SetColour(const wxString &col)
  36. { m_colour.Set(col); UpdateColour(); }
  37. protected:
  38. virtual void UpdateColour() = 0;
  39. // the current colour (may be invalid if none)
  40. wxColour m_colour;
  41. };
  42. // Styles which must be supported by all controls implementing wxColourPickerWidgetBase
  43. // NB: these styles must be defined to carefully-chosen values to
  44. // avoid conflicts with wxButton's styles
  45. // show the colour in HTML form (#AABBCC) as colour button label
  46. // (instead of no label at all)
  47. // NOTE: this style is supported just by wxColourButtonGeneric and
  48. // thus is not exposed in wxColourPickerCtrl
  49. #define wxCLRP_SHOW_LABEL 0x0008
  50. // map platform-dependent controls which implement the wxColourPickerWidgetBase
  51. // under the name "wxColourPickerWidget".
  52. // NOTE: wxColourPickerCtrl allocates a wxColourPickerWidget and relies on the
  53. // fact that all classes being mapped as wxColourPickerWidget have the
  54. // same prototype for their contructor (and also explains why we use
  55. // define instead of a typedef)
  56. // since GTK > 2.4, there is GtkColorButton
  57. #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
  58. #include "wx/gtk/clrpicker.h"
  59. #define wxColourPickerWidget wxColourButton
  60. #else
  61. #include "wx/generic/clrpickerg.h"
  62. #define wxColourPickerWidget wxGenericColourButton
  63. #endif
  64. // ----------------------------------------------------------------------------
  65. // wxColourPickerCtrl: platform-independent class which embeds a
  66. // platform-dependent wxColourPickerWidget and, if wxCLRP_USE_TEXTCTRL style is
  67. // used, a textctrl next to it.
  68. // ----------------------------------------------------------------------------
  69. #define wxCLRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
  70. #define wxCLRP_DEFAULT_STYLE 0
  71. class WXDLLIMPEXP_CORE wxColourPickerCtrl : public wxPickerBase
  72. {
  73. public:
  74. wxColourPickerCtrl() {}
  75. virtual ~wxColourPickerCtrl() {}
  76. wxColourPickerCtrl(wxWindow *parent, wxWindowID id,
  77. const wxColour& col = *wxBLACK, const wxPoint& pos = wxDefaultPosition,
  78. const wxSize& size = wxDefaultSize, long style = wxCLRP_DEFAULT_STYLE,
  79. const wxValidator& validator = wxDefaultValidator,
  80. const wxString& name = wxColourPickerCtrlNameStr)
  81. { Create(parent, id, col, pos, size, style, validator, name); }
  82. bool Create(wxWindow *parent, wxWindowID id,
  83. const wxColour& col = *wxBLACK,
  84. const wxPoint& pos = wxDefaultPosition,
  85. const wxSize& size = wxDefaultSize,
  86. long style = wxCLRP_DEFAULT_STYLE,
  87. const wxValidator& validator = wxDefaultValidator,
  88. const wxString& name = wxColourPickerCtrlNameStr);
  89. public: // public API
  90. // get the colour chosen
  91. wxColour GetColour() const
  92. { return ((wxColourPickerWidget *)m_picker)->GetColour(); }
  93. // set currently displayed color
  94. void SetColour(const wxColour& col);
  95. // set colour using RGB(r,g,b) syntax or considering given text as a colour name;
  96. // returns true if the given text was successfully recognized.
  97. bool SetColour(const wxString& text);
  98. public: // internal functions
  99. // update the button colour to match the text control contents
  100. void UpdatePickerFromTextCtrl();
  101. // update the text control to match the button's colour
  102. void UpdateTextCtrlFromPicker();
  103. // event handler for our picker
  104. void OnColourChange(wxColourPickerEvent &);
  105. protected:
  106. virtual long GetPickerStyle(long style) const
  107. { return (style & wxCLRP_SHOW_LABEL); }
  108. private:
  109. DECLARE_DYNAMIC_CLASS(wxColourPickerCtrl)
  110. };
  111. // ----------------------------------------------------------------------------
  112. // wxColourPickerEvent: used by wxColourPickerCtrl only
  113. // ----------------------------------------------------------------------------
  114. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COLOURPICKER_CHANGED, wxColourPickerEvent );
  115. class WXDLLIMPEXP_CORE wxColourPickerEvent : public wxCommandEvent
  116. {
  117. public:
  118. wxColourPickerEvent() {}
  119. wxColourPickerEvent(wxObject *generator, int id, const wxColour &col)
  120. : wxCommandEvent(wxEVT_COLOURPICKER_CHANGED, id),
  121. m_colour(col)
  122. {
  123. SetEventObject(generator);
  124. }
  125. wxColour GetColour() const { return m_colour; }
  126. void SetColour(const wxColour &c) { m_colour = c; }
  127. // default copy ctor, assignment operator and dtor are ok
  128. virtual wxEvent *Clone() const { return new wxColourPickerEvent(*this); }
  129. private:
  130. wxColour m_colour;
  131. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxColourPickerEvent)
  132. };
  133. // ----------------------------------------------------------------------------
  134. // event types and macros
  135. // ----------------------------------------------------------------------------
  136. typedef void (wxEvtHandler::*wxColourPickerEventFunction)(wxColourPickerEvent&);
  137. #define wxColourPickerEventHandler(func) \
  138. wxEVENT_HANDLER_CAST(wxColourPickerEventFunction, func)
  139. #define EVT_COLOURPICKER_CHANGED(id, fn) \
  140. wx__DECLARE_EVT1(wxEVT_COLOURPICKER_CHANGED, id, wxColourPickerEventHandler(fn))
  141. // old wxEVT_COMMAND_* constant
  142. #define wxEVT_COMMAND_COLOURPICKER_CHANGED wxEVT_COLOURPICKER_CHANGED
  143. #endif // wxUSE_COLOURPICKERCTRL
  144. #endif // _WX_CLRPICKER_H_BASE_