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