added wxTextEntry::AutoCompleteFileNames() and implemented it for wxMSW
[wxWidgets.git] / src / msw / textentry.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/textentry.cpp
3 // Purpose: wxTextEntry implementation for wxMSW
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-26
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/string.h"
28 #endif // WX_PRECOMP
29
30 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
31
32 #include "wx/textentry.h"
33 #include "wx/dynlib.h"
34
35 #include "wx/msw/private.h"
36
37 #ifndef SHACF_FILESYS_ONLY
38 #define SHACF_FILESYS_ONLY 0x00000010
39 #endif
40
41 #define GetEditHwnd() ((HWND)(GetEditHWND()))
42
43 // ============================================================================
44 // wxTextEntry implementation
45 // ============================================================================
46
47 void wxTextEntry::WriteText(const wxString& text)
48 {
49 ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str());
50 }
51
52 wxString wxTextEntry::GetValue() const
53 {
54 return wxGetWindowText(GetEditHWND());
55 }
56
57 void wxTextEntry::Remove(long from, long to)
58 {
59 DoSetSelection(from, to, SetSel_NoScroll);
60 WriteText(wxString());
61 }
62
63 void wxTextEntry::Copy()
64 {
65 ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0);
66 }
67
68 void wxTextEntry::Cut()
69 {
70 ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0);
71 }
72
73 void wxTextEntry::Paste()
74 {
75 ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0);
76 }
77
78 void wxTextEntry::Undo()
79 {
80 ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0);
81 }
82
83 void wxTextEntry::Redo()
84 {
85 // same as Undo, since Undo undoes the undo
86 Undo();
87 return;
88 }
89
90 bool wxTextEntry::CanUndo() const
91 {
92 return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0;
93 }
94
95 bool wxTextEntry::CanRedo() const
96 {
97 // see comment in Redo()
98 return CanUndo();
99 }
100
101 void wxTextEntry::SetInsertionPoint(long pos)
102 {
103 // be careful to call DoSetSelection() which is overridden in wxTextCtrl
104 // and not just SetSelection() here
105 DoSetSelection(pos, pos);
106 }
107
108 long wxTextEntry::GetInsertionPoint() const
109 {
110 long from;
111 GetSelection(&from, NULL);
112 return from;
113 }
114
115 long wxTextEntry::GetLastPosition() const
116 {
117 return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0);
118 }
119
120 void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags))
121 {
122 // if from and to are both -1, it means (in wxWidgets) that all text should
123 // be selected, translate this into Windows convention
124 if ( (from == -1) && (to == -1) )
125 {
126 from = 0;
127 }
128
129 ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to);
130 }
131
132 void wxTextEntry::GetSelection(long *from, long *to) const
133 {
134 DWORD dwStart, dwEnd;
135 ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
136
137 if ( from )
138 *from = dwStart;
139 if ( to )
140 *to = dwEnd;
141 }
142
143 bool wxTextEntry::AutoCompleteFileNames()
144 {
145 typedef HRESULT (WINAPI *SHAutoComplete_t)(HWND, DWORD);
146 static SHAutoComplete_t s_pfnSHAutoComplete = (SHAutoComplete_t)-1;
147 static wxDynamicLibrary s_dllShlwapi;
148 if ( s_pfnSHAutoComplete == (SHAutoComplete_t)-1 )
149 {
150 wxLogNull noLog;
151
152 if ( !s_dllShlwapi.Load(_T("shlwapi.dll"), wxDL_VERBATIM) )
153 {
154 s_pfnSHAutoComplete = NULL;
155 }
156 else
157 {
158 wxDL_INIT_FUNC(s_pfn, SHAutoComplete, s_dllShlwapi);
159 }
160 }
161
162 if ( !s_pfnSHAutoComplete )
163 return false;
164
165 HRESULT hr = (*s_pfnSHAutoComplete)(GetEditHwnd(), SHACF_FILESYS_ONLY);
166 if ( FAILED(hr) )
167 {
168 wxLogApiError(_T("SHAutoComplete()"), hr);
169
170 return false;
171 }
172
173 return true;
174 }
175
176 bool wxTextEntry::IsEditable() const
177 {
178 return !(::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY);
179 }
180
181 void wxTextEntry::SetEditable(bool editable)
182 {
183 ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0);
184 }
185
186 void wxTextEntry::SetMaxLength(unsigned long len)
187 {
188 if ( len >= 0xffff )
189 {
190 // this will set it to a platform-dependent maximum (much more
191 // than 64Kb under NT)
192 len = 0;
193 }
194
195 ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0);
196 }
197
198 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX