1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "textctrlbase.h"
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/textctrl.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
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))
45 // ============================================================================
47 // ============================================================================
49 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent
, wxCommandEvent
)
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
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxTextCtrlBase::wxTextCtrlBase()
64 wxTextCtrlBase::~wxTextCtrlBase()
68 // ----------------------------------------------------------------------------
69 // style functions - not implemented here
70 // ----------------------------------------------------------------------------
72 wxTextAttr::wxTextAttr(const wxColour
& colText
,
73 const wxColour
& colBack
,
75 wxTextAttrAlignment alignment
)
76 : m_colText(colText
), m_colBack(colBack
), m_font(font
), m_textAlignment(alignment
)
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
;
89 void wxTextAttr::Init()
91 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
99 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
100 const wxTextAttr
& attrDef
,
101 const wxTextCtrlBase
*text
)
103 wxFont font
= attr
.GetFont();
106 font
= attrDef
.GetFont();
108 if ( text
&& !font
.Ok() )
109 font
= text
->GetFont();
112 wxColour colFg
= attr
.GetTextColour();
115 colFg
= attrDef
.GetTextColour();
117 if ( text
&& !colFg
.Ok() )
118 colFg
= text
->GetForegroundColour();
121 wxColour colBg
= attr
.GetBackgroundColour();
124 colBg
= attrDef
.GetBackgroundColour();
126 if ( text
&& !colBg
.Ok() )
127 colBg
= text
->GetBackgroundColour();
130 wxTextAttr
newAttr(colFg
, colBg
, font
);
132 if (attr
.HasAlignment())
133 newAttr
.SetAlignment(attr
.GetAlignment());
134 else if (attrDef
.HasAlignment())
135 newAttr
.SetAlignment(attrDef
.GetAlignment());
138 newAttr
.SetTabs(attr
.GetTabs());
139 else if (attrDef
.HasTabs())
140 newAttr
.SetTabs(attrDef
.GetTabs());
142 if (attr
.HasLeftIndent())
143 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
144 else if (attrDef
.HasLeftIndent())
145 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
147 if (attr
.HasRightIndent())
148 newAttr
.SetRightIndent(attr
.GetRightIndent());
149 else if (attrDef
.HasRightIndent())
150 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
155 void wxTextAttr::operator= (const wxTextAttr
& attr
)
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
;
169 // apply styling to text range
170 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
171 const wxTextAttr
& WXUNUSED(style
))
173 // to be implemented in derived TextCtrl classes
177 // get the styling at the given position
178 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
180 // to be implemented in derived TextCtrl classes
184 // change default text attributes
185 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
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
190 if ( style
.IsDefault() )
191 m_defaultStyle
= style
;
193 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
198 // get default text attributes
199 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
201 return m_defaultStyle
;
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
211 wxFFile
file(filename
);
212 if ( file
.IsOpened() )
215 if ( file
.ReadAll(&text
) )
221 m_filename
= filename
;
227 wxLogError(_("File couldn't be loaded."));
228 #endif // wxUSE_FFILE
233 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
235 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
236 if ( filenameToUse
.empty() )
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."));
245 wxFFile
file(filenameToUse
, _T("w"));
246 if ( file
.IsOpened() && file
.Write(GetValue()) )
248 // it's not modified any longer
251 // if it worked, save for future calls
252 m_filename
= filenameToUse
;
256 #endif // wxUSE_FFILE
258 wxLogError(_("The text couldn't be saved."));
263 // ----------------------------------------------------------------------------
264 // stream-like insertion operator
265 // ----------------------------------------------------------------------------
267 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
270 return *TEXTCTRL(this);
273 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
276 str
.Printf(wxT("%.2f"), f
);
278 return *TEXTCTRL(this);
281 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
284 str
.Printf(wxT("%.2f"), d
);
286 return *TEXTCTRL(this);
289 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
292 str
.Printf(wxT("%d"), i
);
294 return *TEXTCTRL(this);
297 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
300 str
.Printf(wxT("%ld"), i
);
302 return *TEXTCTRL(this);
305 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
307 return operator<<(wxString(c
));
310 // ----------------------------------------------------------------------------
311 // streambuf methods implementation
312 // ----------------------------------------------------------------------------
314 #ifndef NO_TEXT_WINDOW_STREAM
316 int wxTextCtrlBase::overflow(int c
)
318 AppendText((wxChar
)c
);
320 // return something different from EOF
324 #endif // NO_TEXT_WINDOW_STREAM
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 bool wxTextCtrlBase::CanCopy() const
332 // can copy if there's a selection
334 GetSelection(&from
, &to
);
338 bool wxTextCtrlBase::CanCut() const
340 // can cut if there's a selection and if we're not read only
341 return CanCopy() && IsEditable();
344 bool wxTextCtrlBase::CanPaste() const
346 // can paste if we are not read only
350 // ----------------------------------------------------------------------------
351 // emulating key presses
352 // ----------------------------------------------------------------------------
355 // the generic version is unused in wxMSW
356 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& WXUNUSED(event
))
361 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
364 int keycode
= event
.GetKeyCode();
377 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
381 case WXK_NUMPAD_MULTIPLY
:
391 case WXK_NUMPAD_SUBTRACT
:
396 case WXK_NUMPAD_DECIMAL
:
401 case WXK_NUMPAD_DIVIDE
:
406 case WXK_NUMPAD_DELETE
:
407 // delete the character at cursor
409 const long pos
= GetInsertionPoint(),
410 last
= GetLastPosition();
412 Remove(pos
, pos
+ 1);
417 // delete the character before the cursor
419 const long pos
= GetInsertionPoint();
421 Remove(pos
- 1, pos
);
426 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
428 // FIXME this is not going to work for non letters...
429 if ( !event
.ShiftDown() )
431 keycode
= wxTolower(keycode
);
434 ch
= (wxChar
)keycode
;
453 // ----------------------------------------------------------------------------
454 // selection and ranges
455 // ----------------------------------------------------------------------------
457 void wxTextCtrlBase::SelectAll()
459 SetSelection(0, GetLastPosition());
462 wxString
wxTextCtrlBase::GetStringSelection() const
465 GetSelection(&from
, &to
);
467 return GetRange(from
, to
);
470 wxString
wxTextCtrlBase::GetRange(long from
, long to
) const
475 sel
= GetValue().Mid(from
, to
- from
);
481 // do the window-specific processing after processing the update event
482 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
484 if ( event
.GetSetEnabled() )
485 Enable(event
.GetEnabled());
487 if ( event
.GetSetText() )
489 if ( event
.GetText() != GetValue() )
490 SetValue(event
.GetText());
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
498 wxTextCtrlHitTestResult
499 wxTextCtrlBase::HitTest(const wxPoint
& WXUNUSED(pt
),
500 wxTextCoord
* WXUNUSED(col
),
501 wxTextCoord
* WXUNUSED(row
)) const
504 return wxTE_HT_UNKNOWN
;
507 #else // !wxUSE_TEXTCTRL
509 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
510 // controls (wxComboBox and wxSpinCtrl)
511 #include "wx/event.h"
513 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
515 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL