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