1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) wxWindows 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
)
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
;
88 void wxTextAttr::Init()
90 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
97 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
98 const wxTextAttr
& attrDef
,
99 const wxTextCtrlBase
*text
)
101 wxFont font
= attr
.GetFont();
104 font
= attrDef
.GetFont();
106 if ( text
&& !font
.Ok() )
107 font
= text
->GetFont();
110 wxColour colFg
= attr
.GetTextColour();
113 colFg
= attrDef
.GetTextColour();
115 if ( text
&& !colFg
.Ok() )
116 colFg
= text
->GetForegroundColour();
119 wxColour colBg
= attr
.GetBackgroundColour();
122 colBg
= attrDef
.GetBackgroundColour();
124 if ( text
&& !colBg
.Ok() )
125 colBg
= text
->GetBackgroundColour();
128 wxTextAttr
newAttr(colFg
, colBg
, font
);
130 if (attr
.HasAlignment())
131 newAttr
.SetAlignment(attr
.GetAlignment());
132 else if (attrDef
.HasAlignment())
133 newAttr
.SetAlignment(attrDef
.GetAlignment());
136 newAttr
.SetTabs(attr
.GetTabs());
137 else if (attrDef
.HasTabs())
138 newAttr
.SetTabs(attrDef
.GetTabs());
140 if (attr
.HasLeftIndent())
141 newAttr
.SetLeftIndent(attr
.GetLeftIndent());
142 else if (attrDef
.HasLeftIndent())
143 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent());
145 if (attr
.HasRightIndent())
146 newAttr
.SetRightIndent(attr
.GetRightIndent());
147 else if (attrDef
.HasRightIndent())
148 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
153 void wxTextAttr::operator= (const wxTextAttr
& attr
)
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
;
166 // apply styling to text range
167 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
168 const wxTextAttr
& WXUNUSED(style
))
170 // to be implemented in derived TextCtrl classes
174 // get the styling at the given position
175 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
177 // to be implemented in derived TextCtrl classes
181 // change default text attributes
182 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
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
187 if ( style
.IsDefault() )
188 m_defaultStyle
= style
;
190 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
195 // get default text attributes
196 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
198 return m_defaultStyle
;
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
208 wxFFile
file(filename
);
209 if ( file
.IsOpened() )
212 if ( file
.ReadAll(&text
) )
218 m_filename
= filename
;
224 wxLogError(_("File couldn't be loaded."));
225 #endif // wxUSE_FFILE
230 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
232 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
233 if ( !filenameToUse
)
235 // what kind of message to give? is it an error or a program bug?
236 wxLogDebug(wxT("Can't save textctrl to file without filename."));
242 wxFFile
file(filename
, _T("w"));
243 if ( file
.IsOpened() && file
.Write(GetValue()) )
245 // it's not modified any longer
248 m_filename
= filename
;
253 wxLogError(_("The text couldn't be saved."));
254 #endif // wxUSE_FFILE
259 // ----------------------------------------------------------------------------
260 // stream-like insertion operator
261 // ----------------------------------------------------------------------------
263 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
266 return *TEXTCTRL(this);
269 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
272 str
.Printf(wxT("%.2f"), f
);
274 return *TEXTCTRL(this);
277 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
280 str
.Printf(wxT("%.2f"), d
);
282 return *TEXTCTRL(this);
285 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
288 str
.Printf(wxT("%d"), i
);
290 return *TEXTCTRL(this);
293 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
296 str
.Printf(wxT("%ld"), i
);
298 return *TEXTCTRL(this);
301 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
303 return operator<<(wxString(c
));
306 // ----------------------------------------------------------------------------
307 // streambuf methods implementation
308 // ----------------------------------------------------------------------------
310 #ifndef NO_TEXT_WINDOW_STREAM
312 int wxTextCtrlBase::overflow(int c
)
314 AppendText((wxChar
)c
);
316 // return something different from EOF
320 #endif // NO_TEXT_WINDOW_STREAM
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 bool wxTextCtrlBase::CanCopy() const
328 // can copy if there's a selection
330 GetSelection(&from
, &to
);
334 bool wxTextCtrlBase::CanCut() const
336 // can cut if there's a selection and if we're not read only
337 return CanCopy() && IsEditable();
340 bool wxTextCtrlBase::CanPaste() const
342 // can paste if we are not read only
346 // ----------------------------------------------------------------------------
347 // emulating key presses
348 // ----------------------------------------------------------------------------
351 // the generic version is unused in wxMSW
352 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& WXUNUSED(event
))
357 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
360 int keycode
= event
.GetKeyCode();
373 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
377 case WXK_NUMPAD_MULTIPLY
:
387 case WXK_NUMPAD_SUBTRACT
:
392 case WXK_NUMPAD_DECIMAL
:
397 case WXK_NUMPAD_DIVIDE
:
402 case WXK_NUMPAD_DELETE
:
403 // delete the character at cursor
405 const long pos
= GetInsertionPoint(),
406 last
= GetLastPosition();
408 Remove(pos
, pos
+ 1);
413 // delete the character before the cursor
415 const long pos
= GetInsertionPoint();
417 Remove(pos
- 1, pos
);
422 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
424 // FIXME this is not going to work for non letters...
425 if ( !event
.ShiftDown() )
427 keycode
= wxTolower(keycode
);
430 ch
= (wxChar
)keycode
;
449 // ----------------------------------------------------------------------------
450 // selection and ranges
451 // ----------------------------------------------------------------------------
453 void wxTextCtrlBase::SelectAll()
455 SetSelection(0, GetLastPosition());
458 wxString
wxTextCtrlBase::GetStringSelection() const
461 GetSelection(&from
, &to
);
463 return GetRange(from
, to
);
466 wxString
wxTextCtrlBase::GetRange(long from
, long to
) const
471 sel
= GetValue().Mid(from
, to
- from
);
477 // do the window-specific processing after processing the update event
478 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
480 if ( event
.GetSetEnabled() )
481 Enable(event
.GetEnabled());
483 if ( event
.GetSetText() )
485 if ( event
.GetText() != GetValue() )
486 SetValue(event
.GetText());
491 #else // !wxUSE_TEXTCTRL
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"
497 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
499 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL