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