refactor wxMSW code to extract parts common to wxTextCtrl and wxComboBox into wxTextEntry
[wxWidgets.git] / src / common / textcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 13.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/event.h"
25 #endif // WX_PRECOMP
26
27 #if wxUSE_TEXTCTRL
28
29 #include "wx/textctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/ffile.h"
37
38 // ----------------------------------------------------------------------------
39 // macros
40 // ----------------------------------------------------------------------------
41
42 // we don't have any objects of type wxTextCtrlBase in the program, only
43 // wxTextCtrl, so this cast is safe
44 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
51
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN)
56
57 IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase, wxControl)
58
59 // ----------------------------------------------------------------------------
60 // style functions - not implemented here
61 // ----------------------------------------------------------------------------
62
63 wxTextAttr::wxTextAttr(const wxColour& colText,
64 const wxColour& colBack,
65 const wxFont& font,
66 wxTextAttrAlignment alignment)
67 : m_colText(colText), m_colBack(colBack), m_font(font), m_textAlignment(alignment)
68 {
69 m_flags = 0;
70 m_leftIndent = 0;
71 m_leftSubIndent = 0;
72 m_rightIndent = 0;
73 if (m_colText.Ok()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
74 if (m_colBack.Ok()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
75 if (m_font.Ok()) m_flags |= wxTEXT_ATTR_FONT;
76 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
77 m_flags |= wxTEXT_ATTR_ALIGNMENT;
78 }
79
80 void wxTextAttr::Init()
81 {
82 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
83 m_flags = 0;
84 m_leftIndent = 0;
85 m_leftSubIndent = 0;
86 m_rightIndent = 0;
87 }
88
89 /* static */
90 wxTextAttr wxTextAttr::Combine(const wxTextAttr& attr,
91 const wxTextAttr& attrDef,
92 const wxTextCtrlBase *text)
93 {
94 wxFont font = attr.GetFont();
95 if ( !font.Ok() )
96 {
97 font = attrDef.GetFont();
98
99 if ( text && !font.Ok() )
100 font = text->GetFont();
101 }
102
103 wxColour colFg = attr.GetTextColour();
104 if ( !colFg.Ok() )
105 {
106 colFg = attrDef.GetTextColour();
107
108 if ( text && !colFg.Ok() )
109 colFg = text->GetForegroundColour();
110 }
111
112 wxColour colBg = attr.GetBackgroundColour();
113 if ( !colBg.Ok() )
114 {
115 colBg = attrDef.GetBackgroundColour();
116
117 if ( text && !colBg.Ok() )
118 colBg = text->GetBackgroundColour();
119 }
120
121 wxTextAttr newAttr(colFg, colBg, font);
122
123 if (attr.HasAlignment())
124 newAttr.SetAlignment(attr.GetAlignment());
125 else if (attrDef.HasAlignment())
126 newAttr.SetAlignment(attrDef.GetAlignment());
127
128 if (attr.HasTabs())
129 newAttr.SetTabs(attr.GetTabs());
130 else if (attrDef.HasTabs())
131 newAttr.SetTabs(attrDef.GetTabs());
132
133 if (attr.HasLeftIndent())
134 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
135 else if (attrDef.HasLeftIndent())
136 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
137
138 if (attr.HasRightIndent())
139 newAttr.SetRightIndent(attr.GetRightIndent());
140 else if (attrDef.HasRightIndent())
141 newAttr.SetRightIndent(attrDef.GetRightIndent());
142
143 return newAttr;
144 }
145
146 void wxTextAttr::operator= (const wxTextAttr& attr)
147 {
148 m_font = attr.m_font;
149 m_colText = attr.m_colText;
150 m_colBack = attr.m_colBack;
151 m_textAlignment = attr.m_textAlignment;
152 m_leftIndent = attr.m_leftIndent;
153 m_leftSubIndent = attr.m_leftSubIndent;
154 m_rightIndent = attr.m_rightIndent;
155 m_tabs = attr.m_tabs;
156 m_flags = attr.m_flags;
157 }
158
159
160 // apply styling to text range
161 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
162 const wxTextAttr& WXUNUSED(style))
163 {
164 // to be implemented in derived classes
165 return false;
166 }
167
168 // get the styling at the given position
169 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
170 {
171 // to be implemented in derived classes
172 return false;
173 }
174
175 // change default text attributes
176 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
177 {
178 // keep the old attributes if the new style doesn't specify them unless the
179 // new style is empty - then reset m_defaultStyle (as there is no other way
180 // to do it)
181 if ( style.IsDefault() )
182 m_defaultStyle = style;
183 else
184 m_defaultStyle = wxTextAttr::Combine(style, m_defaultStyle, this);
185
186 return true;
187 }
188
189 // ----------------------------------------------------------------------------
190 // file IO functions
191 // ----------------------------------------------------------------------------
192
193 bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
194 {
195 #if wxUSE_FFILE
196 wxFFile file(filename);
197 if ( file.IsOpened() )
198 {
199 wxString text;
200 if ( file.ReadAll(&text) )
201 {
202 SetValue(text);
203
204 DiscardEdits();
205
206 m_filename = filename;
207
208 return true;
209 }
210 }
211
212 wxLogError(_("File couldn't be loaded."));
213 #endif // wxUSE_FFILE
214
215 return false;
216 }
217
218 bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
219 {
220 wxString filenameToUse = filename.empty() ? m_filename : filename;
221 if ( filenameToUse.empty() )
222 {
223 // what kind of message to give? is it an error or a program bug?
224 wxLogDebug(wxT("Can't save textctrl to file without filename."));
225
226 return false;
227 }
228
229 return DoSaveFile(filenameToUse, fileType);
230 }
231
232 bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
233 {
234 #if wxUSE_FFILE
235 wxFFile file(filename, _T("w"));
236 if ( file.IsOpened() && file.Write(GetValue()) )
237 {
238 // if it worked, save for future calls
239 m_filename = filename;
240
241 // it's not modified any longer
242 DiscardEdits();
243
244 return true;
245 }
246 #endif // wxUSE_FFILE
247
248 wxLogError(_("The text couldn't be saved."));
249
250 return false;
251 }
252
253 // ----------------------------------------------------------------------------
254 // stream-like insertion operator
255 // ----------------------------------------------------------------------------
256
257 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
258 {
259 AppendText(s);
260 return *TEXTCTRL(this);
261 }
262
263 wxTextCtrl& wxTextCtrlBase::operator<<(float f)
264 {
265 wxString str;
266 str.Printf(wxT("%.2f"), f);
267 AppendText(str);
268 return *TEXTCTRL(this);
269 }
270
271 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
272 {
273 wxString str;
274 str.Printf(wxT("%.2f"), d);
275 AppendText(str);
276 return *TEXTCTRL(this);
277 }
278
279 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
280 {
281 wxString str;
282 str.Printf(wxT("%d"), i);
283 AppendText(str);
284 return *TEXTCTRL(this);
285 }
286
287 wxTextCtrl& wxTextCtrlBase::operator<<(long i)
288 {
289 wxString str;
290 str.Printf(wxT("%ld"), i);
291 AppendText(str);
292 return *TEXTCTRL(this);
293 }
294
295 wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
296 {
297 return operator<<(wxString(c));
298 }
299
300 // ----------------------------------------------------------------------------
301 // streambuf methods implementation
302 // ----------------------------------------------------------------------------
303
304 #if wxHAS_TEXT_WINDOW_STREAM
305
306 int wxTextCtrlBase::overflow(int c)
307 {
308 AppendText((wxChar)c);
309
310 // return something different from EOF
311 return 0;
312 }
313
314 #endif // wxHAS_TEXT_WINDOW_STREAM
315
316 // ----------------------------------------------------------------------------
317 // emulating key presses
318 // ----------------------------------------------------------------------------
319
320 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
321 {
322 // we have a native implementation for Win32 and so don't need this one
323 #ifndef __WIN32__
324 wxChar ch = 0;
325 int keycode = event.GetKeyCode();
326 switch ( keycode )
327 {
328 case WXK_NUMPAD0:
329 case WXK_NUMPAD1:
330 case WXK_NUMPAD2:
331 case WXK_NUMPAD3:
332 case WXK_NUMPAD4:
333 case WXK_NUMPAD5:
334 case WXK_NUMPAD6:
335 case WXK_NUMPAD7:
336 case WXK_NUMPAD8:
337 case WXK_NUMPAD9:
338 ch = (wxChar)(_T('0') + keycode - WXK_NUMPAD0);
339 break;
340
341 case WXK_MULTIPLY:
342 case WXK_NUMPAD_MULTIPLY:
343 ch = _T('*');
344 break;
345
346 case WXK_ADD:
347 case WXK_NUMPAD_ADD:
348 ch = _T('+');
349 break;
350
351 case WXK_SUBTRACT:
352 case WXK_NUMPAD_SUBTRACT:
353 ch = _T('-');
354 break;
355
356 case WXK_DECIMAL:
357 case WXK_NUMPAD_DECIMAL:
358 ch = _T('.');
359 break;
360
361 case WXK_DIVIDE:
362 case WXK_NUMPAD_DIVIDE:
363 ch = _T('/');
364 break;
365
366 case WXK_DELETE:
367 case WXK_NUMPAD_DELETE:
368 // delete the character at cursor
369 {
370 const long pos = GetInsertionPoint();
371 if ( pos < GetLastPosition() )
372 Remove(pos, pos + 1);
373 }
374 break;
375
376 case WXK_BACK:
377 // delete the character before the cursor
378 {
379 const long pos = GetInsertionPoint();
380 if ( pos > 0 )
381 Remove(pos - 1, pos);
382 }
383 break;
384
385 default:
386 #if wxUSE_UNICODE
387 if ( event.GetUnicodeKey() )
388 {
389 ch = event.GetUnicodeKey();
390 }
391 else
392 #endif
393 if ( keycode < 256 && keycode >= 0 && wxIsprint(keycode) )
394 {
395 // FIXME this is not going to work for non letters...
396 if ( !event.ShiftDown() )
397 {
398 keycode = wxTolower(keycode);
399 }
400
401 ch = (wxChar)keycode;
402 }
403 else
404 {
405 ch = _T('\0');
406 }
407 }
408
409 if ( ch )
410 {
411 WriteText(ch);
412
413 return true;
414 }
415 #else // __WIN32__
416 wxUnusedVar(event);
417 #endif // !__WIN32__/__WIN32__
418
419 return false;
420 }
421
422 // do the window-specific processing after processing the update event
423 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
424 {
425 // call inherited, but skip the wxControl's version, and call directly the
426 // wxWindow's one instead, because the only reason why we are overriding this
427 // function is that we want to use SetValue() instead of wxControl::SetLabel()
428 wxWindowBase::DoUpdateWindowUI(event);
429
430 // update text
431 if ( event.GetSetText() )
432 {
433 if ( event.GetText() != GetValue() )
434 SetValue(event.GetText());
435 }
436 }
437
438 // ----------------------------------------------------------------------------
439 // hit testing
440 // ----------------------------------------------------------------------------
441
442 wxTextCtrlHitTestResult
443 wxTextAreaBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
444 {
445 // implement in terms of the other overload as the native ports typically
446 // can get the position and not (x, y) pair directly (although wxUniv
447 // directly gets x and y -- and so overrides this method as well)
448 long pos;
449 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
450
451 if ( rc != wxTE_HT_UNKNOWN )
452 {
453 PositionToXY(pos, x, y);
454 }
455
456 return rc;
457 }
458
459 wxTextCtrlHitTestResult
460 wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
461 {
462 // not implemented
463 return wxTE_HT_UNKNOWN;
464 }
465
466 // ----------------------------------------------------------------------------
467 // events
468 // ----------------------------------------------------------------------------
469
470 /* static */
471 bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win)
472 {
473 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, win->GetId());
474
475 // do not do this as it could be very inefficient if the text control
476 // contains a lot of text and we're not using ref-counted wxString
477 // implementation -- instead, event.GetString() will query the control for
478 // its current text if needed
479 //event.SetString(win->GetValue());
480
481 event.SetEventObject(win);
482 return win->GetEventHandler()->ProcessEvent(event);
483 }
484
485 #else // !wxUSE_TEXTCTRL
486
487 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
488 // controls (wxComboBox and wxSpinCtrl)
489
490 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
491
492 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL