| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/pickerbase.cpp |
| 3 | // Purpose: wxPickerBase class implementation |
| 4 | // Author: Francesco Montorsi |
| 5 | // Modified by: |
| 6 | // Created: 15/04/2006 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Francesco Montorsi |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_COLOURPICKERCTRL || \ |
| 28 | wxUSE_DIRPICKERCTRL || \ |
| 29 | wxUSE_FILEPICKERCTRL || \ |
| 30 | wxUSE_FONTPICKERCTRL |
| 31 | |
| 32 | #include "wx/pickerbase.h" |
| 33 | |
| 34 | #ifndef WX_PRECOMP |
| 35 | #include "wx/textctrl.h" |
| 36 | #endif |
| 37 | |
| 38 | // ============================================================================ |
| 39 | // implementation |
| 40 | // ============================================================================ |
| 41 | |
| 42 | IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl) |
| 43 | |
| 44 | BEGIN_EVENT_TABLE(wxPickerBase, wxControl) |
| 45 | EVT_SIZE(wxPickerBase::OnSize) |
| 46 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxPickerBase) |
| 47 | END_EVENT_TABLE() |
| 48 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxPickerBase, wxControl) |
| 49 | |
| 50 | |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | // wxPickerBase |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | |
| 55 | bool wxPickerBase::CreateBase(wxWindow *parent, |
| 56 | wxWindowID id, |
| 57 | const wxString &text, |
| 58 | const wxPoint& pos, |
| 59 | const wxSize& size, |
| 60 | long style, |
| 61 | const wxValidator& validator, |
| 62 | const wxString& name) |
| 63 | { |
| 64 | // remove any border style from our style as wxPickerBase's window must be |
| 65 | // invisible (user styles must be set on the textctrl or the platform-dependent picker) |
| 66 | style &= ~wxBORDER_MASK; |
| 67 | if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL, |
| 68 | validator, name)) |
| 69 | return false; |
| 70 | |
| 71 | m_sizer = new wxBoxSizer(wxHORIZONTAL); |
| 72 | |
| 73 | if (HasFlag(wxPB_USE_TEXTCTRL)) |
| 74 | { |
| 75 | // NOTE: the style of this class (wxPickerBase) and the style of the |
| 76 | // attached text control are different: GetTextCtrlStyle() extracts |
| 77 | // the styles related to the textctrl from the styles passed here |
| 78 | m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
| 79 | wxDefaultPosition, wxDefaultSize, |
| 80 | GetTextCtrlStyle(style)); |
| 81 | if (!m_text) |
| 82 | { |
| 83 | wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") ); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // set the maximum lenght allowed for this textctrl. |
| 88 | // This is very important since any change to it will trigger an update in |
| 89 | // the m_picker; for very long strings, this real-time synchronization could |
| 90 | // become a CPU-blocker and thus should be avoided. |
| 91 | // 32 characters will be more than enough for all common uses. |
| 92 | m_text->SetMaxLength(32); |
| 93 | |
| 94 | // set the initial contents of the textctrl |
| 95 | m_text->SetValue(text); |
| 96 | |
| 97 | m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED, |
| 98 | wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate), |
| 99 | NULL, this); |
| 100 | m_text->Connect(wxEVT_KILL_FOCUS, |
| 101 | wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus), |
| 102 | NULL, this); |
| 103 | |
| 104 | m_text->Connect(wxEVT_DESTROY, |
| 105 | wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete), |
| 106 | NULL, this); |
| 107 | |
| 108 | // the text control's proportion values defaults to 2 |
| 109 | m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5); |
| 110 | } |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | void wxPickerBase::PostCreation() |
| 116 | { |
| 117 | // the picker's proportion value defaults to 1 when there's no text control |
| 118 | // associated with it - in that case it defaults to 0 |
| 119 | m_sizer->Add(m_picker, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5); |
| 120 | |
| 121 | SetSizer(m_sizer); |
| 122 | SetMinSize( m_sizer->GetMinSize() ); |
| 123 | } |
| 124 | |
| 125 | void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent &) |
| 126 | { |
| 127 | wxASSERT(m_text); |
| 128 | |
| 129 | // don't leave the textctrl empty |
| 130 | if (m_text->GetValue().empty()) |
| 131 | UpdateTextCtrlFromPicker(); |
| 132 | } |
| 133 | |
| 134 | void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &) |
| 135 | { |
| 136 | // the textctrl has been deleted; our pointer is invalid! |
| 137 | m_text = NULL; |
| 138 | } |
| 139 | |
| 140 | void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &) |
| 141 | { |
| 142 | // for each text-change, update the picker |
| 143 | UpdatePickerFromTextCtrl(); |
| 144 | } |
| 145 | |
| 146 | void wxPickerBase::OnSize(wxSizeEvent &event) |
| 147 | { |
| 148 | if (GetAutoLayout()) |
| 149 | Layout(); |
| 150 | event.Skip(); |
| 151 | } |
| 152 | |
| 153 | #endif // Any picker in use |