]> git.saurik.com Git - wxWidgets.git/blob - src/generic/combog.cpp
fixed crash in <sub/sup> handling (bug #1545152)
[wxWidgets.git] / src / generic / combog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/combog.cpp
3 // Purpose: Generic wxComboCtrl
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
26 #if wxUSE_COMBOCTRL
27
28 #include "wx/combo.h"
29
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
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
92 BEGIN_EVENT_TABLE(wxGenericComboControl, wxComboCtrlBase)
93 EVT_PAINT(wxGenericComboControl::OnPaintEvent)
94 EVT_MOUSE_EVENTS(wxGenericComboControl::OnMouseEvent)
95 END_EVENT_TABLE()
96
97
98 IMPLEMENT_DYNAMIC_CLASS(wxGenericComboControl, wxComboCtrlBase)
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
150 if ( !wxComboCtrlBase::Create(parent,
151 id,
152 value,
153 pos,
154 size,
155 style | wxFULL_REPAINT_ON_RESIZE,
156 wxDefaultValidator,
157 name) )
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();
165
166 // Set background
167 SetBackgroundStyle( wxBG_STYLE_CUSTOM ); // for double-buffering
168
169 // SetBestSize should be called last
170 SetBestSize(size);
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
262 if ( (!m_text || m_widthCustomPaint) )
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);
274 if ( m_popupInterface )
275 m_popupInterface->PaintComboControl(dc,rect);
276 else
277 wxComboPopup::DefaultPaintComboControl(this,dc,rect);
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
291 const bool ctrlIsButton = wxPlatformIs(wxOS_WINDOWS);
292
293 if ( ctrlIsButton &&
294 (m_windowStyle & (wxCC_SPECIAL_DCLICK|wxCB_READONLY)) == wxCB_READONLY )
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 bool wxGenericComboControl::IsKeyPopupToggle(const wxKeyEvent& event) const
325 {
326 int keycode = event.GetKeyCode();
327 bool isPopupShown = IsPopupShown();
328
329 // This code is AFAIK appropriate for wxGTK.
330
331 if ( isPopupShown )
332 {
333 if ( keycode == WXK_ESCAPE ||
334 ( keycode == WXK_UP && event.AltDown() ) )
335 return true;
336 }
337 else
338 {
339 if ( keycode == WXK_DOWN && event.AltDown() )
340 return true;
341 }
342
343 return false;
344 }
345
346 #ifdef __WXUNIVERSAL__
347
348 bool wxGenericComboControl::PerformAction(const wxControlAction& action,
349 long numArg,
350 const wxString& strArg)
351 {
352 bool processed = false;
353 if ( action == wxACTION_COMBOBOX_POPUP )
354 {
355 if ( !m_isPopupShown )
356 {
357 ShowPopup();
358
359 processed = true;
360 }
361 }
362 else if ( action == wxACTION_COMBOBOX_DISMISS )
363 {
364 if ( m_isPopupShown )
365 {
366 HidePopup();
367
368 processed = true;
369 }
370 }
371
372 if ( !processed )
373 {
374 // pass along
375 return wxControl::PerformAction(action, numArg, strArg);
376 }
377
378 return true;
379 }
380
381 #endif // __WXUNIVERSAL__
382
383 // If native wxComboCtrl was not defined, then prepare a simple
384 // front-end so that wxRTTI works as expected.
385 #ifndef _WX_COMBOCONTROL_H_
386 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl, wxGenericComboControl)
387 #endif
388
389 #endif // !wxCOMBOCONTROL_FULLY_FEATURED
390
391 #endif // wxUSE_COMBOCTRL