| 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 | #include "wx/tooltip.h" |
| 34 | |
| 35 | #ifndef WX_PRECOMP |
| 36 | #include "wx/textctrl.h" |
| 37 | #endif |
| 38 | |
| 39 | |
| 40 | // ============================================================================ |
| 41 | // implementation |
| 42 | // ============================================================================ |
| 43 | |
| 44 | IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl) |
| 45 | |
| 46 | BEGIN_EVENT_TABLE(wxPickerBase, wxControl) |
| 47 | EVT_SIZE(wxPickerBase::OnSize) |
| 48 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxPickerBase) |
| 49 | END_EVENT_TABLE() |
| 50 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxPickerBase, wxControl) |
| 51 | |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // wxPickerBase |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | bool wxPickerBase::CreateBase(wxWindow *parent, |
| 58 | wxWindowID id, |
| 59 | const wxString &text, |
| 60 | const wxPoint& pos, |
| 61 | const wxSize& size, |
| 62 | long style, |
| 63 | const wxValidator& validator, |
| 64 | const wxString& name) |
| 65 | { |
| 66 | // remove any border style from our style as wxPickerBase's window must be |
| 67 | // invisible (user styles must be set on the textctrl or the platform-dependent picker) |
| 68 | style &= ~wxBORDER_MASK; |
| 69 | if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL, |
| 70 | validator, name)) |
| 71 | return false; |
| 72 | |
| 73 | m_sizer = new wxBoxSizer(wxHORIZONTAL); |
| 74 | |
| 75 | if (HasFlag(wxPB_USE_TEXTCTRL)) |
| 76 | { |
| 77 | // NOTE: the style of this class (wxPickerBase) and the style of the |
| 78 | // attached text control are different: GetTextCtrlStyle() extracts |
| 79 | // the styles related to the textctrl from the styles passed here |
| 80 | m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
| 81 | wxDefaultPosition, wxDefaultSize, |
| 82 | GetTextCtrlStyle(style)); |
| 83 | if (!m_text) |
| 84 | { |
| 85 | wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") ); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // set the maximum lenght allowed for this textctrl. |
| 90 | // This is very important since any change to it will trigger an update in |
| 91 | // the m_picker; for very long strings, this real-time synchronization could |
| 92 | // become a CPU-blocker and thus should be avoided. |
| 93 | // 32 characters will be more than enough for all common uses. |
| 94 | m_text->SetMaxLength(32); |
| 95 | |
| 96 | // set the initial contents of the textctrl |
| 97 | m_text->SetValue(text); |
| 98 | |
| 99 | m_text->Connect(m_text->GetId(), wxEVT_COMMAND_TEXT_UPDATED, |
| 100 | wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate), |
| 101 | NULL, this); |
| 102 | m_text->Connect(m_text->GetId(), wxEVT_KILL_FOCUS, |
| 103 | wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus), |
| 104 | NULL, this); |
| 105 | |
| 106 | m_text->Connect(m_text->GetId(), wxEVT_DESTROY, |
| 107 | wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete), |
| 108 | NULL, this); |
| 109 | |
| 110 | // the text control's proportion values defaults to 2 |
| 111 | m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5); |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | void wxPickerBase::PostCreation() |
| 118 | { |
| 119 | // the picker's proportion value defaults to 1 when there's no text control |
| 120 | // associated with it - in that case it defaults to 0 |
| 121 | m_sizer->Add(m_picker, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5); |
| 122 | |
| 123 | SetSizer(m_sizer); |
| 124 | SetMinSize( m_sizer->GetMinSize() ); |
| 125 | } |
| 126 | |
| 127 | #if wxUSE_TOOLTIPS |
| 128 | |
| 129 | void wxPickerBase::DoSetToolTip(wxToolTip *tip) |
| 130 | { |
| 131 | // don't set the tooltip on us but rather on our two child windows |
| 132 | // as otherwise it would appear only when the cursor is placed on the |
| 133 | // small area around the child windows which belong to wxPickerBase |
| 134 | m_picker->SetToolTip(tip); |
| 135 | |
| 136 | // do a copy as wxWindow will own the pointer we pass |
| 137 | if ( m_text ) |
| 138 | m_text->SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL); |
| 139 | } |
| 140 | |
| 141 | #endif // wxUSE_TOOLTIPS |
| 142 | |
| 143 | // ---------------------------------------------------------------------------- |
| 144 | // wxPickerBase - event handlers |
| 145 | // ---------------------------------------------------------------------------- |
| 146 | |
| 147 | void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent& event) |
| 148 | { |
| 149 | event.Skip(); |
| 150 | |
| 151 | // don't leave the textctrl empty |
| 152 | if (m_text->GetValue().empty()) |
| 153 | UpdateTextCtrlFromPicker(); |
| 154 | } |
| 155 | |
| 156 | void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &) |
| 157 | { |
| 158 | // the textctrl has been deleted; our pointer is invalid! |
| 159 | m_text = NULL; |
| 160 | } |
| 161 | |
| 162 | void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &) |
| 163 | { |
| 164 | // for each text-change, update the picker |
| 165 | UpdatePickerFromTextCtrl(); |
| 166 | } |
| 167 | |
| 168 | void wxPickerBase::OnSize(wxSizeEvent &event) |
| 169 | { |
| 170 | if (GetAutoLayout()) |
| 171 | Layout(); |
| 172 | event.Skip(); |
| 173 | } |
| 174 | |
| 175 | #endif // Any picker in use |