]>
Commit | Line | Data |
---|---|---|
a340b80d | 1 | ///////////////////////////////////////////////////////////////////////////// |
85fed18c | 2 | // Name: src/generic/combog.cpp |
a57d600f | 3 | // Purpose: Generic wxComboCtrl |
a340b80d VZ |
4 | // Author: Jaakko Salli |
5 | // Modified by: | |
6 | // Created: Apr-30-2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Jaakko Salli | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
a57d600f | 26 | #if wxUSE_COMBOCTRL |
a340b80d | 27 | |
85fed18c WS |
28 | #include "wx/combo.h" |
29 | ||
a340b80d VZ |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/log.h" | |
32 | #include "wx/combobox.h" | |
33 | #include "wx/dcclient.h" | |
34 | #include "wx/settings.h" | |
35 | #endif | |
36 | ||
37 | #include "wx/dcbuffer.h" | |
38 | ||
a340b80d VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // Some constant adjustments to make the generic more bearable | |
41 | ||
42 | #if defined(__WXUNIVERSAL__) | |
43 | ||
44 | #define TEXTCTRLXADJUST 0 // position adjustment for wxTextCtrl, with zero indent | |
45 | #define TEXTCTRLYADJUST 0 | |
46 | #define TEXTXADJUST 0 // how much is read-only text's x adjusted | |
47 | #define DEFAULT_DROPBUTTON_WIDTH 19 | |
48 | ||
49 | #elif defined(__WXMSW__) | |
50 | ||
51 | #define TEXTCTRLXADJUST 2 // position adjustment for wxTextCtrl, with zero indent | |
52 | #define TEXTCTRLYADJUST 3 | |
53 | #define TEXTXADJUST 0 // how much is read-only text's x adjusted | |
54 | #define DEFAULT_DROPBUTTON_WIDTH 17 | |
55 | ||
56 | #elif defined(__WXGTK__) | |
57 | ||
58 | #define TEXTCTRLXADJUST -1 // position adjustment for wxTextCtrl, with zero indent | |
59 | #define TEXTCTRLYADJUST 0 | |
60 | #define TEXTXADJUST 1 // how much is read-only text's x adjusted | |
61 | #define DEFAULT_DROPBUTTON_WIDTH 23 | |
62 | ||
63 | #elif defined(__WXMAC__) | |
64 | ||
65 | #define TEXTCTRLXADJUST 0 // position adjustment for wxTextCtrl, with zero indent | |
66 | #define TEXTCTRLYADJUST 0 | |
67 | #define TEXTXADJUST 0 // how much is read-only text's x adjusted | |
68 | #define DEFAULT_DROPBUTTON_WIDTH 19 | |
69 | ||
70 | #else | |
71 | ||
72 | #define TEXTCTRLXADJUST 0 // position adjustment for wxTextCtrl, with zero indent | |
73 | #define TEXTCTRLYADJUST 0 | |
74 | #define TEXTXADJUST 0 // how much is read-only text's x adjusted | |
75 | #define DEFAULT_DROPBUTTON_WIDTH 19 | |
76 | ||
77 | #endif | |
78 | ||
79 | ||
80 | // ============================================================================ | |
81 | // implementation | |
82 | // ============================================================================ | |
83 | ||
84 | // Only implement if no native or it wasn't fully featured | |
85 | #ifndef wxCOMBOCONTROL_FULLY_FEATURED | |
86 | ||
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // wxGenericComboControl | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
a57d600f | 92 | BEGIN_EVENT_TABLE(wxGenericComboControl, wxComboCtrlBase) |
a340b80d VZ |
93 | EVT_PAINT(wxGenericComboControl::OnPaintEvent) |
94 | EVT_MOUSE_EVENTS(wxGenericComboControl::OnMouseEvent) | |
95 | END_EVENT_TABLE() | |
96 | ||
97 | ||
a57d600f | 98 | IMPLEMENT_DYNAMIC_CLASS(wxGenericComboControl, wxComboCtrlBase) |
a340b80d VZ |
99 | |
100 | void wxGenericComboControl::Init() | |
101 | { | |
102 | } | |
103 | ||
104 | bool wxGenericComboControl::Create(wxWindow *parent, | |
105 | wxWindowID id, | |
106 | const wxString& value, | |
107 | const wxPoint& pos, | |
108 | const wxSize& size, | |
109 | long style, | |
110 | const wxValidator& validator, | |
111 | const wxString& name) | |
112 | { | |
113 | ||
114 | // Set border | |
115 | long border = style & wxBORDER_MASK; | |
116 | ||
117 | if ( !border ) | |
118 | { | |
119 | #if defined(__WXUNIVERSAL__) | |
120 | border = wxBORDER_SIMPLE; | |
121 | #elif defined(__WXMSW__) | |
122 | // For XP, have 1-width custom border, for older version use sunken | |
123 | if ( wxUxThemeEngine::GetIfActive() ) | |
124 | { | |
125 | border = wxBORDER_NONE; | |
126 | m_widthCustomBorder = 1; | |
127 | } | |
128 | else | |
129 | border = wxBORDER_SUNKEN; | |
130 | #elif defined(__WXGTK__) | |
131 | border = wxBORDER_NONE; | |
132 | //m_widthCustomBorder = 2; | |
133 | m_widthCustomBorder = 1; | |
134 | #else | |
135 | border = wxBORDER_SIMPLE; | |
136 | #endif | |
137 | ||
138 | style = (style & ~(wxBORDER_MASK)) | border; | |
139 | } | |
140 | ||
141 | #if defined(__WXGTK__) | |
142 | Customize( wxCC_BUTTON_OUTSIDE_BORDER | | |
143 | wxCC_NO_TEXT_AUTO_SELECT ); | |
144 | #endif | |
145 | ||
146 | if ( style & wxCC_STD_BUTTON ) | |
147 | m_iFlags |= wxCC_POPUP_ON_MOUSE_UP; | |
148 | ||
149 | // create main window | |
a57d600f | 150 | if ( !wxComboCtrlBase::Create(parent, |
93f7f8be WS |
151 | id, |
152 | value, | |
153 | pos, | |
154 | size, | |
155 | style | wxFULL_REPAINT_ON_RESIZE, | |
156 | wxDefaultValidator, | |
157 | name) ) | |
a340b80d VZ |
158 | return false; |
159 | ||
160 | // Create textctrl, if necessary | |
161 | CreateTextCtrl( wxNO_BORDER, validator ); | |
162 | ||
163 | // Add keyboard input handlers for main control and textctrl | |
164 | InstallInputHandlers( true ); | |
165 | ||
166 | // Set background | |
167 | SetBackgroundStyle( wxBG_STYLE_CUSTOM ); // for double-buffering | |
168 | ||
93f7f8be WS |
169 | // SetBestSize should be called last |
170 | SetBestSize(size); | |
a340b80d VZ |
171 | |
172 | return true; | |
173 | } | |
174 | ||
175 | wxGenericComboControl::~wxGenericComboControl() | |
176 | { | |
177 | } | |
178 | ||
179 | void wxGenericComboControl::OnResize() | |
180 | { | |
181 | ||
182 | // Recalculates button and textctrl areas | |
183 | CalculateAreas(DEFAULT_DROPBUTTON_WIDTH); | |
184 | ||
185 | #if 0 | |
186 | // Move separate button control, if any, to correct position | |
187 | if ( m_btn ) | |
188 | { | |
189 | wxSize sz = GetClientSize(); | |
190 | m_btn->SetSize( m_btnArea.x + m_btnSpacingX, | |
191 | (sz.y-m_btnSize.y)/2, | |
192 | m_btnSize.x, | |
193 | m_btnSize.y ); | |
194 | } | |
195 | #endif | |
196 | ||
197 | // Move textctrl, if any, accordingly | |
198 | PositionTextCtrl( TEXTCTRLXADJUST, TEXTCTRLYADJUST ); | |
199 | } | |
200 | ||
201 | void wxGenericComboControl::OnPaintEvent( wxPaintEvent& WXUNUSED(event) ) | |
202 | { | |
203 | wxSize sz = GetClientSize(); | |
204 | wxBufferedPaintDC dc(this,GetBufferBitmap(sz)); | |
205 | ||
206 | const wxRect& rectb = m_btnArea; | |
207 | wxRect rect = m_tcArea; | |
208 | ||
209 | // artificial simple border | |
210 | if ( m_widthCustomBorder ) | |
211 | { | |
212 | int customBorder = m_widthCustomBorder; | |
213 | ||
214 | // Set border colour | |
215 | wxPen pen1( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT), | |
216 | customBorder, | |
217 | wxSOLID ); | |
218 | dc.SetPen( pen1 ); | |
219 | ||
220 | // area around both controls | |
221 | wxRect rect2(0,0,sz.x,sz.y); | |
222 | if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE ) | |
223 | { | |
224 | rect2 = m_tcArea; | |
225 | if ( customBorder == 1 ) | |
226 | { | |
227 | rect2.Inflate(1); | |
228 | } | |
229 | else | |
230 | { | |
231 | #ifdef __WXGTK__ | |
232 | rect2.x -= 1; | |
233 | rect2.y -= 1; | |
234 | #else | |
235 | rect2.x -= customBorder; | |
236 | rect2.y -= customBorder; | |
237 | #endif | |
238 | rect2.width += 1 + customBorder; | |
239 | rect2.height += 1 + customBorder; | |
240 | } | |
241 | } | |
242 | ||
243 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
244 | dc.DrawRectangle(rect2); | |
245 | } | |
246 | ||
247 | wxColour winCol = GetBackgroundColour(); | |
248 | dc.SetBrush(winCol); | |
249 | dc.SetPen(winCol); | |
250 | ||
251 | //wxLogDebug(wxT("hei: %i tcy: %i tchei: %i"),GetClientSize().y,m_tcArea.y,m_tcArea.height); | |
252 | //wxLogDebug(wxT("btnx: %i tcx: %i tcwid: %i"),m_btnArea.x,m_tcArea.x,m_tcArea.width); | |
253 | ||
254 | // clear main background | |
255 | dc.DrawRectangle(rect); | |
256 | ||
257 | if ( !m_btn ) | |
258 | // Standard button rendering | |
259 | DrawButton(dc,rectb,true); | |
260 | ||
261 | // paint required portion on the control | |
6d0ce565 | 262 | if ( (!m_text || m_widthCustomPaint) ) |
a340b80d VZ |
263 | { |
264 | wxASSERT( m_widthCustomPaint >= 0 ); | |
265 | ||
266 | // this is intentionally here to allow drawed rectangle's | |
267 | // right edge to be hidden | |
268 | if ( m_text ) | |
269 | rect.width = m_widthCustomPaint; | |
270 | ||
271 | dc.SetFont( GetFont() ); | |
272 | ||
273 | dc.SetClippingRegion(rect); | |
6d0ce565 VZ |
274 | if ( m_popupInterface ) |
275 | m_popupInterface->PaintComboControl(dc,rect); | |
276 | else | |
277 | wxComboPopup::DefaultPaintComboControl(this,dc,rect); | |
a340b80d VZ |
278 | } |
279 | } | |
280 | ||
281 | void wxGenericComboControl::OnMouseEvent( wxMouseEvent& event ) | |
282 | { | |
283 | bool isOnButtonArea = m_btnArea.Inside(event.m_x,event.m_y); | |
284 | int handlerFlags = isOnButtonArea ? wxCC_MF_ON_BUTTON : 0; | |
285 | ||
286 | // Preprocessing fabricates double-clicks and prevents | |
287 | // (it may also do other common things in future) | |
288 | if ( PreprocessMouseEvent(event,handlerFlags) ) | |
289 | return; | |
290 | ||
61335117 | 291 | const bool ctrlIsButton = wxPlatformIs(wxMSW); |
6d0ce565 VZ |
292 | |
293 | if ( ctrlIsButton && | |
294 | (m_windowStyle & (wxCC_SPECIAL_DCLICK|wxCB_READONLY)) == wxCB_READONLY ) | |
a340b80d VZ |
295 | { |
296 | // if no textctrl and no special double-click, then the entire control acts | |
297 | // as a button | |
298 | handlerFlags |= wxCC_MF_ON_BUTTON; | |
299 | if ( HandleButtonMouseEvent(event,handlerFlags) ) | |
300 | return; | |
301 | } | |
302 | else | |
303 | { | |
304 | if ( isOnButtonArea || HasCapture() ) | |
305 | { | |
306 | if ( HandleButtonMouseEvent(event,handlerFlags) ) | |
307 | return; | |
308 | } | |
309 | else if ( m_btnState ) | |
310 | { | |
311 | // otherwise need to clear the hover status | |
312 | m_btnState = 0; | |
313 | RefreshRect(m_btnArea); | |
314 | } | |
315 | } | |
316 | ||
317 | // | |
318 | // This will handle left_down and left_dclick events outside button in a Windows/GTK-like manner. | |
319 | // See header file for further information on this method. | |
320 | HandleNormalMouseEvent(event); | |
321 | ||
322 | } | |
323 | ||
324 | #ifdef __WXUNIVERSAL__ | |
325 | ||
326 | bool wxGenericComboControl::PerformAction(const wxControlAction& action, | |
327 | long numArg, | |
328 | const wxString& strArg) | |
329 | { | |
330 | bool processed = false; | |
331 | if ( action == wxACTION_COMBOBOX_POPUP ) | |
332 | { | |
333 | if ( !m_isPopupShown ) | |
334 | { | |
335 | ShowPopup(); | |
336 | ||
337 | processed = true; | |
338 | } | |
339 | } | |
340 | else if ( action == wxACTION_COMBOBOX_DISMISS ) | |
341 | { | |
342 | if ( m_isPopupShown ) | |
343 | { | |
344 | HidePopup(); | |
345 | ||
346 | processed = true; | |
347 | } | |
348 | } | |
349 | ||
350 | if ( !processed ) | |
351 | { | |
352 | // pass along | |
353 | return wxControl::PerformAction(action, numArg, strArg); | |
354 | } | |
355 | ||
356 | return true; | |
357 | } | |
358 | ||
359 | #endif // __WXUNIVERSAL__ | |
360 | ||
a57d600f | 361 | // If native wxComboCtrl was not defined, then prepare a simple |
a340b80d VZ |
362 | // front-end so that wxRTTI works as expected. |
363 | #ifndef _WX_COMBOCONTROL_H_ | |
a57d600f | 364 | IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl, wxGenericComboControl) |
a340b80d VZ |
365 | #endif |
366 | ||
367 | #endif // !wxCOMBOCONTROL_FULLY_FEATURED | |
368 | ||
a57d600f | 369 | #endif // wxUSE_COMBOCTRL |