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