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"
29 #include "wx/combobox.h"
30 #include "wx/clipbrd.h"
31 #include "wx/msw/private.h"
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
37 bool wxComboBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
39 if (param
== CBN_SELCHANGE
)
41 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
42 event
.SetInt(GetSelection());
43 event
.SetEventObject(this);
44 event
.SetString(copystring(GetStringSelection()));
45 ProcessCommand(event
);
46 delete[] event
.GetString();
49 else if (param
== CBN_EDITCHANGE
)
51 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
52 event
.SetString(copystring(GetValue()));
53 event
.SetEventObject(this);
54 ProcessCommand(event
);
55 delete[] event
.GetString();
61 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
62 const wxString
& value
,
65 int n
, const wxString choices
[],
67 const wxValidator
& validator
,
71 SetValidator(validator
);
72 if (parent
) parent
->AddChild(this);
73 SetBackgroundColour(parent
->GetBackgroundColour()) ;
74 SetForegroundColour(parent
->GetForegroundColour()) ;
77 m_windowStyle
= style
;
80 m_windowId
= (int)NewControlId();
89 long msStyle
= WS_CHILD
| WS_HSCROLL
| WS_VSCROLL
|
90 WS_TABSTOP
| WS_VISIBLE
| CBS_NOINTEGRALHEIGHT
;
92 if (m_windowStyle
& wxCB_READONLY
)
93 msStyle
|= CBS_DROPDOWNLIST
;
94 else if (m_windowStyle
& wxCB_SIMPLE
)
95 msStyle
|= CBS_SIMPLE
; // A list (shown always) and edit control
97 msStyle
|= CBS_DROPDOWN
;
99 if (m_windowStyle
& wxCB_SORT
)
103 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
105 // Even with extended styles, need to combine with WS_BORDER
106 // for them to look right.
107 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
108 msStyle
|= WS_BORDER
;
110 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, "COMBOBOX", NULL
,
112 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
113 wxGetInstance(), NULL
);
115 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create combobox" );
121 Ctl3dSubclassCtl(wx_combo);
127 // Subclass again for purposes of dialog editing mode
130 SetFont(* parent
->GetFont());
132 for (i
= 0; i
< n
; i
++)
139 SetSize(x
, y
, width
, height
);
140 if ( !value
.IsEmpty() )
148 wxString
wxComboBox::GetValue() const
150 return wxGetWindowText(GetHWND());
153 void wxComboBox::SetValue(const wxString
& value
)
155 // If newlines are denoted by just 10, must stick 13 in front.
157 int len
= value
.Length();
159 for (i
= 0; i
< len
; i
++)
161 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
166 char *tmp
= new char[len
+ singletons
+ 1];
168 for (i
= 0; i
< len
; i
++)
170 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
179 SetWindowText((HWND
) GetHWND(), tmp
);
183 SetWindowText((HWND
) GetHWND(), (const char *)value
);
186 // Clipboard operations
187 void wxComboBox::Copy()
189 HWND hWnd
= (HWND
) GetHWND();
190 SendMessage(hWnd
, WM_COPY
, 0, 0L);
193 void wxComboBox::Cut()
195 HWND hWnd
= (HWND
) GetHWND();
196 SendMessage(hWnd
, WM_CUT
, 0, 0L);
199 void wxComboBox::Paste()
201 HWND hWnd
= (HWND
) GetHWND();
202 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
205 void wxComboBox::SetEditable(bool editable
)
207 // Can't implement in MSW?
208 // HWND hWnd = (HWND) GetHWND();
209 // SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
212 void wxComboBox::SetInsertionPoint(long pos
)
215 HWND hWnd = (HWND) GetHWND();
217 SendMessage(hWnd, EM_SETSEL, pos, pos);
218 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
220 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
223 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
227 void wxComboBox::SetInsertionPointEnd()
230 long pos = GetLastPosition();
231 SetInsertionPoint(pos);
235 long wxComboBox::GetInsertionPoint() const
238 DWORD Pos=(DWORD)SendMessage((HWND) GetHWND(), EM_GETSEL, 0, 0L);
244 long wxComboBox::GetLastPosition() const
247 HWND hWnd = (HWND) GetHWND();
249 // Will always return a number > 0 (according to docs)
250 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
252 // This gets the char index for the _beginning_ of the last line
253 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
255 // Get number of characters in the last line. We'll add this to the character
256 // index for the last line, 1st position.
257 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
259 return (long)(charIndex + lineLength);
264 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
267 HWND hWnd
= (HWND
) GetHWND();
268 long fromChar
= from
;
271 // Set selection and remove it
273 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
275 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
277 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
279 // Now replace with 'value', by pasting.
280 wxSetClipboardData(wxDF_TEXT
, (wxObject
*)(const char *)value
, 0, 0);
282 // Paste into edit control
283 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
287 void wxComboBox::Remove(long from
, long to
)
289 HWND hWnd
= (HWND
) GetHWND();
290 long fromChar
= from
;
293 // Cut all selected text
295 SendMessage(hWnd
, CB_SETEDITSEL
, fromChar
, toChar
);
297 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
299 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
302 void wxComboBox::SetSelection(long from
, long to
)
304 HWND hWnd
= (HWND
) GetHWND();
305 long fromChar
= from
;
307 // if from and to are both -1, it means
308 // (in wxWindows) that all text should be selected.
309 // This translates into Windows convention
310 if ((from
== -1) && (to
== -1))
317 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
318 // SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
320 // WPARAM is 0: selection is scrolled into view
321 SendMessage(hWnd
, CB_SETEDITSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));