1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/settings.h"
29 #include "wx/combobox.h"
30 #include "wx/clipbrd.h"
31 #include "wx/msw/private.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
35 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
40 if (GetSelection() > -1)
42 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId());
43 event
.SetInt(GetSelection());
44 event
.SetEventObject(this);
45 event
.SetString(GetStringSelection());
46 ProcessCommand(event
);
52 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
53 event
.SetString(GetValue());
54 event
.SetEventObject(this);
55 ProcessCommand(event
);
60 // there is no return value for the CBN_ notifications, so always return
61 // FALSE from here to pass the message to DefWindowProc()
65 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
66 const wxString
& value
,
69 int n
, const wxString choices
[],
71 const wxValidator
& validator
,
76 SetValidator(validator
);
77 #endif // wxUSE_VALIDATORS
78 if (parent
) parent
->AddChild(this);
79 // SetBackgroundColour(parent->GetBackgroundColour()) ;
81 // A choice/combobox normally has a white background (or other, depending
82 // on global settings) rather than inheriting the parent's background colour.
83 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
85 SetForegroundColour(parent
->GetForegroundColour()) ;
87 m_windowStyle
= style
;
90 m_windowId
= (int)NewControlId();
99 long msStyle
= WS_CHILD
| WS_TABSTOP
| WS_VISIBLE
|
100 CBS_AUTOHSCROLL
| CBS_NOINTEGRALHEIGHT
;
102 if (m_windowStyle
& wxCB_READONLY
)
103 msStyle
|= CBS_DROPDOWNLIST
;
104 else if (m_windowStyle
& wxCB_SIMPLE
)
105 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
107 msStyle
|= CBS_DROPDOWN
;
109 if (m_windowStyle
& wxCB_SORT
)
113 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
115 // Even with extended styles, need to combine with WS_BORDER
116 // for them to look right.
117 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
118 msStyle
|= WS_BORDER
;
120 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, wxT("COMBOBOX"), NULL
,
122 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
123 wxGetInstance(), NULL
);
125 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create combobox") );
131 Ctl3dSubclassCtl(wx_combo);
137 // Subclass again for purposes of dialog editing mode
140 SetFont(parent
->GetFont());
142 for (i
= 0; i
< n
; i
++)
149 SetSize(x
, y
, width
, height
);
150 if ( !value
.IsEmpty() )
158 void wxComboBox::SetValue(const wxString
& value
)
160 // If newlines are denoted by just 10, must stick 13 in front.
162 int len
= value
.Length();
164 for (i
= 0; i
< len
; i
++)
166 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
171 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
173 for (i
= 0; i
< len
; i
++)
175 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
184 SetWindowText(GetHwnd(), tmp
);
188 SetWindowText(GetHwnd(), value
);
191 // Clipboard operations
192 void wxComboBox::Copy()
194 HWND hWnd
= GetHwnd();
195 SendMessage(hWnd
, WM_COPY
, 0, 0L);
198 void wxComboBox::Cut()
200 HWND hWnd
= GetHwnd();
201 SendMessage(hWnd
, WM_CUT
, 0, 0L);
204 void wxComboBox::Paste()
206 HWND hWnd
= GetHwnd();
207 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
210 void wxComboBox::SetEditable(bool editable
)
212 // Can't implement in MSW?
213 // HWND hWnd = GetHwnd();
214 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
217 void wxComboBox::SetInsertionPoint(long pos
)
220 HWND hWnd = GetHwnd();
222 SendMessage(hWnd, EM_SETSEL, pos, pos);
223 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
225 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
228 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
232 void wxComboBox::SetInsertionPointEnd()
235 long pos = GetLastPosition();
236 SetInsertionPoint(pos);
240 long wxComboBox::GetInsertionPoint() const
243 DWORD Pos=(DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
249 long wxComboBox::GetLastPosition() const
252 HWND hWnd = GetHwnd();
254 // Will always return a number > 0 (according to docs)
255 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
257 // This gets the char index for the _beginning_ of the last line
258 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
260 // Get number of characters in the last line. We'll add this to the character
261 // index for the last line, 1st position.
262 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
264 return (long)(charIndex + lineLength);
269 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
272 HWND hWnd
= GetHwnd();
273 long fromChar
= from
;
276 // Set selection and remove it
278 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
280 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
282 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
284 // Now replace with 'value', by pasting.
285 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const wxChar
*)value
, 0, 0);
287 // Paste into edit control
288 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
292 void wxComboBox::Remove(long from
, long to
)
294 HWND hWnd
= GetHwnd();
295 long fromChar
= from
;
298 // Cut all selected text
300 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
302 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
304 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
307 void wxComboBox::SetSelection(long from
, long to
)
309 HWND hWnd
= GetHwnd();
310 long fromChar
= from
;
312 // if from and to are both -1, it means
313 // (in wxWindows) that all text should be selected.
314 // This translates into Windows convention
315 if ((from
== -1) && (to
== -1))
322 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
323 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
325 // WPARAM is 0: selection is scrolled into view
326 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
330 void wxComboBox::DoMoveWindow(int x
, int y
, int width
, int height
)
333 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
339 height
= n
* EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
341 wxControl::DoMoveWindow(x
, y
, width
, height
);
344 wxSize
wxComboBox::DoGetBestSize() const
346 // the choice calculates the horz size correctly, but not the vertical
347 // component: correct it
348 wxSize size
= wxChoice::DoGetBestSize();
351 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
352 size
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);