Include wx/combobox.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / os2 / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_COMBOBOX
16
17 #include "wx/combobox.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/settings.h"
21 #endif
22
23 #include "wx/clipbrd.h"
24 #include "wx/os2/private.h"
25
26 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
27
28 MRESULT EXPENTRY wxComboEditWndProc( HWND hWnd
29 ,UINT uMessage
30 ,MPARAM wParam
31 ,MPARAM lParam
32 );
33 //
34 // The pointer to standard wnd proc
35 //
36 static WXFARPROC gfnWndprocEdit = (WXFARPROC)NULL;
37
38 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
39
40 bool wxComboBox::OS2Command( WXUINT uParam, WXWORD WXUNUSED(wId) )
41 {
42 long lSel = -1L;
43 wxString sValue;
44
45 switch (uParam)
46 {
47 case CBN_LBSELECT:
48 if (GetSelection() > -1)
49 {
50 wxCommandEvent vEvent( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
51
52 vEvent.SetInt(GetSelection());
53 vEvent.SetEventObject(this);
54 vEvent.SetString(GetStringSelection());
55
56 ProcessCommand(vEvent);
57 }
58 break;
59
60 case CBN_EFCHANGE:
61 {
62 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
63
64 if (lSel == -1L)
65 sValue = GetValue();
66 else
67 SetValue(sValue);
68 vEvent.SetString(GetValue());
69 vEvent.SetEventObject(this);
70 ProcessCommand(vEvent);
71 }
72 break;
73 }
74 //
75 // There is no return value for the CBN_ notifications, so always return
76 // false from here to pass the message to DefWindowProc()
77 //
78 return false;
79 } // end of wxComboBox::OS2Command
80
81 bool wxComboBox::Create(
82 wxWindow* pParent
83 , wxWindowID vId
84 , const wxString& rsValue
85 , const wxPoint& rPos
86 , const wxSize& rSize
87 , const wxArrayString& asChoices
88 , long lStyle
89 , const wxValidator& rValidator
90 , const wxString& rsName
91 )
92 {
93 wxCArrayString chs(asChoices);
94
95 return Create(pParent, vId, rsValue, rPos, rSize, chs.GetCount(),
96 chs.GetStrings(), lStyle, rValidator, rsName);
97 }
98
99 bool wxComboBox::Create(
100 wxWindow* pParent
101 , wxWindowID vId
102 , const wxString& rsValue
103 , const wxPoint& rPos
104 , const wxSize& rSize
105 , int n
106 , const wxString asChoices[]
107 , long lStyle
108 , const wxValidator& rValidator
109 , const wxString& rsName
110 )
111 {
112 m_isShown = false;
113
114 if (!CreateControl( pParent
115 ,vId
116 ,rPos
117 ,rSize
118 ,lStyle
119 ,rValidator
120 ,rsName
121 ))
122 return false;
123
124 //
125 // Get the right style
126 //
127 long lSstyle = 0L;
128
129 lSstyle = WS_TABSTOP |
130 WS_VISIBLE;
131
132 if (lStyle & wxCLIP_SIBLINGS )
133 lSstyle |= WS_CLIPSIBLINGS;
134 if (lStyle & wxCB_READONLY)
135 lSstyle |= CBS_DROPDOWNLIST;
136 else if (lStyle & wxCB_SIMPLE)
137 lSstyle |= CBS_SIMPLE; // A list (shown always) and edit control
138 else
139 lSstyle |= CBS_DROPDOWN;
140
141
142 if (!OS2CreateControl( _T("COMBOBOX")
143 ,lSstyle
144 ))
145 return false;
146
147 //
148 // A choice/combobox normally has a white background (or other, depending
149 // on global settings) rather than inheriting the parent's background colour.
150 //
151 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
152
153 for (int i = 0; i < n; i++)
154 {
155 Append(asChoices[i]);
156 }
157
158 SetSize( rPos.x
159 ,rPos.y
160 ,rSize.x
161 ,rSize.y
162 );
163 if (!rsValue.empty())
164 {
165 SetValue(rsValue);
166 }
167 gfnWndprocEdit = (WXFARPROC)::WinSubclassWindow( (HWND)GetHwnd()
168 ,(PFNWP)wxComboEditWndProc
169 );
170 ::WinSetWindowULong(GetHwnd(), QWL_USER, (ULONG)this);
171 Show(true);
172 return true;
173 } // end of wxComboBox::Create
174
175 void wxComboBox::SetValue(
176 const wxString& rsValue
177 )
178 {
179 if ( HasFlag(wxCB_READONLY) )
180 SetStringSelection(rsValue);
181 else
182 ::WinSetWindowText(GetHwnd(), (PSZ)rsValue.c_str());
183 } // end of wxComboBox::SetValue
184
185 //
186 // Clipboard operations
187 //
188 void wxComboBox::Copy()
189 {
190 HWND hWnd = GetHwnd();
191
192 ::WinSendMsg(hWnd, EM_COPY, (MPARAM)0, (MPARAM)0);
193 } // end of wxComboBox::Copy
194
195 void wxComboBox::Cut()
196 {
197 HWND hWnd = GetHwnd();
198
199 ::WinSendMsg(hWnd, EM_CUT, (MPARAM)0, (MPARAM)0);
200 } // end of wxComboBox::Cut
201
202 void wxComboBox::Paste()
203 {
204 HWND hWnd = GetHwnd();
205
206 ::WinSendMsg(hWnd, EM_PASTE, (MPARAM)0, (MPARAM)0);
207 } // end of wxComboBox::Paste
208
209 void wxComboBox::SetEditable(
210 bool bEditable
211 )
212 {
213 HWND hWnd = GetHwnd();
214
215 ::WinSendMsg(hWnd, EM_SETREADONLY, (MPARAM)!bEditable, (MPARAM)0L);
216 } // end of wxComboBox::SetEditable
217
218 void wxComboBox::SetInsertionPoint(
219 long lPos
220 )
221 {
222 HWND hWnd = GetHwnd();
223
224 ::WinSendMsg(hWnd, EM_SETFIRSTCHAR, MPFROMLONG(lPos), (MPARAM)0);
225 } // end of wxComboBox::SetInsertionPoint
226
227 void wxComboBox::SetInsertionPointEnd()
228 {
229 wxTextPos lPos = GetLastPosition();
230
231 SetInsertionPoint(lPos);
232 } // end of wxComboBox::SetInsertionPointEnd
233
234 long wxComboBox::GetInsertionPoint() const
235 {
236 long lPos = LONGFROMMR(::WinSendMsg( GetHwnd()
237 ,LM_QUERYSELECTION
238 ,(MPARAM)0
239 ,(MPARAM)0
240 ));
241 if (lPos == LIT_NONE)
242 return wxNOT_FOUND;
243 return lPos;
244 } // end of wxComboBox::GetInsertionPoint
245
246 wxTextPos wxComboBox::GetLastPosition() const
247 {
248 long lLineLength = 0L;
249 WNDPARAMS vParams;
250
251 //
252 // Get number of characters in the last (only) line. We'll add this to the character
253 // index for the last line, 1st position.
254 //
255
256
257 vParams.fsStatus = WPM_CCHTEXT;
258 if (::WinSendMsg( GetHwnd()
259 ,WM_QUERYWINDOWPARAMS
260 ,&vParams
261 ,0
262 ))
263 {
264 lLineLength = (long)vParams.cchText;
265 }
266 else
267 lLineLength = 0L;
268 return lLineLength;
269 } // end of wxComboBox::GetLastPosition
270
271 void wxComboBox::Replace( long lFrom,
272 long lTo,
273 const wxString& rsValue )
274 {
275 #if wxUSE_CLIPBOARD
276 HWND hWnd = GetHwnd();
277
278 //
279 // Set selection and remove it
280 //
281 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
282 ::WinSendMsg(hWnd, EM_CUT, (MPARAM)0, (MPARAM)0);
283
284 //
285 // Now replace with 'value', by pasting.
286 //
287 wxSetClipboardData( wxDF_TEXT
288 ,(wxObject *)rsValue.c_str()
289 ,0
290 ,0
291 );
292
293 //
294 // Paste into edit control
295 //
296 ::WinSendMsg(hWnd, EM_PASTE, (MPARAM)0, (MPARAM)0L);
297 #else
298 wxUnusedVar(lFrom);
299 wxUnusedVar(lTo);
300 wxUnusedVar(rsValue);
301 #endif
302 } // end of wxComboBox::Replace
303
304 void wxComboBox::Remove( long lFrom, long lTo)
305 {
306 #if wxUSE_CLIPBOARD
307 HWND hWnd = GetHwnd();
308
309 ::WinSendMsg(hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), 0);
310 ::WinSendMsg(hWnd, EM_CUT, (MPARAM)0, (MPARAM)0);
311 #else
312 wxUnusedVar(lFrom);
313 wxUnusedVar(lTo);
314 #endif
315 } // end of wxComboBox::Remove
316
317 void wxComboBox::SetSelection( long lFrom, long lTo )
318 {
319 HWND hWnd = GetHwnd();
320 long lFromChar = 0;
321 long lToChar = 0;
322
323 //
324 // If from and to are both -1, it means
325 // (in wxWidgets) that all text should be selected.
326 // This translates into Windows convention
327 //
328 if ((lFrom == -1L) && (lTo == -1L))
329 {
330 lFromChar = 0;
331 lToChar = -1;
332 }
333
334 ::WinSendMsg( hWnd
335 ,EM_SETSEL
336 ,MPFROM2SHORT((USHORT)lFromChar, (USHORT)lToChar)
337 ,(MPARAM)0
338 );
339 } // end of wxComboBox::SetSelection
340
341 void wxComboBox::DoSetSize(
342 int nX
343 , int nY
344 , int nWidth
345 , int nHeight
346 , int nSizeFlags
347 )
348 {
349 wxControl::DoSetSize( nX
350 ,nY
351 ,nWidth
352 ,nHeight
353 ,nSizeFlags
354 );
355 } // end of wxComboBox::DoSetSize
356
357 bool wxComboBox::ProcessEditMsg(
358 WXUINT uMsg
359 , WXWPARAM wParam
360 , WXLPARAM lParam)
361 {
362 SHORT vFlag;
363 switch (uMsg)
364 {
365 case WM_CHAR:
366 vFlag = SHORT1FROMMP(wParam);
367 switch(vFlag)
368 {
369 case KC_CHAR:
370 return (HandleChar( wParam
371 ,lParam
372 ,true /* isASCII */
373 ));
374
375 case KC_PREVDOWN:
376 return (HandleKeyDown( wParam
377 ,lParam
378 ));
379
380 case KC_KEYUP:
381 return (HandleKeyUp( wParam
382 ,lParam
383 ));
384 }
385 break;
386
387 case WM_SETFOCUS:
388 if (SHORT1FROMMP((MPARAM)lParam) == TRUE)
389 return(HandleSetFocus((WXHWND)(HWND)wParam));
390 else
391 return(HandleKillFocus((WXHWND)(HWND)wParam));
392 }
393 return false;
394 } // end of WinGuiBase_CComboBox::ProcessEditMsg
395
396 MRESULT EXPENTRY wxComboEditWndProc(
397 HWND hWnd
398 , UINT uMessage
399 , MPARAM wParam
400 , MPARAM lParam
401 )
402 {
403 switch (uMessage)
404 {
405 //
406 // Forward some messages to the combobox
407 //
408 case WM_SETFOCUS:
409 case WM_CHAR:
410 {
411 wxComboBox* pCombo = (wxComboBox *)::WinQueryWindowULong( hWnd
412 ,QWL_USER
413 );
414
415 if (pCombo->ProcessEditMsg( uMessage
416 ,wParam
417 ,lParam
418 ))
419 return ((MRESULT)0);
420 }
421 break;
422
423 //
424 // TODO: Deal with tooltips here
425 //
426 }
427 return (gfnWndprocEdit(hWnd, (ULONG)uMessage, (MPARAM)wParam, (MPARAM)lParam));
428 } // end of wxComboEditWndProc
429
430 #endif
431 // wxUSE_COMBOBOX