pickerbase.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/pickerbase.h
  3. // Purpose: wxPickerBase definition
  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_PICKERBASE_H_BASE_
  11. #define _WX_PICKERBASE_H_BASE_
  12. #include "wx/control.h"
  13. #include "wx/sizer.h"
  14. #include "wx/containr.h"
  15. class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
  16. class WXDLLIMPEXP_FWD_CORE wxToolTip;
  17. extern WXDLLIMPEXP_DATA_CORE(const char) wxButtonNameStr[];
  18. // ----------------------------------------------------------------------------
  19. // wxPickerBase is the base class for the picker controls which support
  20. // a wxPB_USE_TEXTCTRL style; i.e. for those pickers which can use an auxiliary
  21. // text control next to the 'real' picker.
  22. //
  23. // The wxTextPickerHelper class manages enabled/disabled state of the text control,
  24. // its sizing and positioning.
  25. // ----------------------------------------------------------------------------
  26. #define wxPB_USE_TEXTCTRL 0x0002
  27. #define wxPB_SMALL 0x8000
  28. class WXDLLIMPEXP_CORE wxPickerBase : public wxNavigationEnabled<wxControl>
  29. {
  30. public:
  31. // ctor: text is the associated text control
  32. wxPickerBase() : m_text(NULL), m_picker(NULL), m_sizer(NULL)
  33. { }
  34. virtual ~wxPickerBase() {}
  35. // if present, intercepts wxPB_USE_TEXTCTRL style and creates the text control
  36. // The 3rd argument is the initial wxString to display in the text control
  37. bool CreateBase(wxWindow *parent,
  38. wxWindowID id,
  39. const wxString& text = wxEmptyString,
  40. const wxPoint& pos = wxDefaultPosition,
  41. const wxSize& size = wxDefaultSize,
  42. long style = 0,
  43. const wxValidator& validator = wxDefaultValidator,
  44. const wxString& name = wxButtonNameStr);
  45. public: // public API
  46. // margin between the text control and the picker
  47. void SetInternalMargin(int newmargin)
  48. { GetTextCtrlItem()->SetBorder(newmargin); m_sizer->Layout(); }
  49. int GetInternalMargin() const
  50. { return GetTextCtrlItem()->GetBorder(); }
  51. // proportion of the text control
  52. void SetTextCtrlProportion(int prop)
  53. { GetTextCtrlItem()->SetProportion(prop); m_sizer->Layout(); }
  54. int GetTextCtrlProportion() const
  55. { return GetTextCtrlItem()->GetProportion(); }
  56. // proportion of the picker control
  57. void SetPickerCtrlProportion(int prop)
  58. { GetPickerCtrlItem()->SetProportion(prop); m_sizer->Layout(); }
  59. int GetPickerCtrlProportion() const
  60. { return GetPickerCtrlItem()->GetProportion(); }
  61. bool IsTextCtrlGrowable() const
  62. { return (GetTextCtrlItem()->GetFlag() & wxGROW) != 0; }
  63. void SetTextCtrlGrowable(bool grow = true)
  64. {
  65. int f = GetDefaultTextCtrlFlag();
  66. if ( grow )
  67. f |= wxGROW;
  68. else
  69. f &= ~wxGROW;
  70. GetTextCtrlItem()->SetFlag(f);
  71. }
  72. bool IsPickerCtrlGrowable() const
  73. { return (GetPickerCtrlItem()->GetFlag() & wxGROW) != 0; }
  74. void SetPickerCtrlGrowable(bool grow = true)
  75. {
  76. int f = GetDefaultPickerCtrlFlag();
  77. if ( grow )
  78. f |= wxGROW;
  79. else
  80. f &= ~wxGROW;
  81. GetPickerCtrlItem()->SetFlag(f);
  82. }
  83. bool HasTextCtrl() const
  84. { return m_text != NULL; }
  85. wxTextCtrl *GetTextCtrl()
  86. { return m_text; }
  87. wxControl *GetPickerCtrl()
  88. { return m_picker; }
  89. void SetTextCtrl(wxTextCtrl* text)
  90. { m_text = text; }
  91. void SetPickerCtrl(wxControl* picker)
  92. { m_picker = picker; }
  93. // methods that derived class must/may override
  94. virtual void UpdatePickerFromTextCtrl() = 0;
  95. virtual void UpdateTextCtrlFromPicker() = 0;
  96. protected:
  97. // overridden base class methods
  98. #if wxUSE_TOOLTIPS
  99. virtual void DoSetToolTip(wxToolTip *tip);
  100. #endif // wxUSE_TOOLTIPS
  101. // event handlers
  102. void OnTextCtrlDelete(wxWindowDestroyEvent &);
  103. void OnTextCtrlUpdate(wxCommandEvent &);
  104. void OnTextCtrlKillFocus(wxFocusEvent &);
  105. // returns the set of styles for the attached wxTextCtrl
  106. // from given wxPickerBase's styles
  107. virtual long GetTextCtrlStyle(long style) const
  108. { return (style & wxWINDOW_STYLE_MASK); }
  109. // returns the set of styles for the m_picker
  110. virtual long GetPickerStyle(long style) const
  111. { return (style & wxWINDOW_STYLE_MASK); }
  112. wxSizerItem *GetPickerCtrlItem() const
  113. {
  114. if (this->HasTextCtrl())
  115. return m_sizer->GetItem((size_t)1);
  116. return m_sizer->GetItem((size_t)0);
  117. }
  118. wxSizerItem *GetTextCtrlItem() const
  119. {
  120. wxASSERT(this->HasTextCtrl());
  121. return m_sizer->GetItem((size_t)0);
  122. }
  123. int GetDefaultPickerCtrlFlag() const
  124. {
  125. // on macintosh, without additional borders
  126. // there's not enough space for focus rect
  127. return wxALIGN_CENTER_VERTICAL|wxGROW
  128. #ifdef __WXMAC__
  129. | wxTOP | wxRIGHT | wxBOTTOM
  130. #endif
  131. ;
  132. }
  133. int GetDefaultTextCtrlFlag() const
  134. {
  135. // on macintosh, without wxALL there's not enough space for focus rect
  136. return wxALIGN_CENTER_VERTICAL
  137. #ifdef __WXMAC__
  138. | wxALL
  139. #else
  140. | wxRIGHT
  141. #endif
  142. ;
  143. }
  144. void PostCreation();
  145. protected:
  146. wxTextCtrl *m_text; // can be NULL
  147. wxControl *m_picker;
  148. wxBoxSizer *m_sizer;
  149. private:
  150. DECLARE_ABSTRACT_CLASS(wxPickerBase)
  151. };
  152. #endif
  153. // _WX_PICKERBASE_H_BASE_