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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
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 // ----------------------------------------------------------------------------
73 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
74 const wxTextAttr
& attrDef
,
75 const wxTextCtrlBase
*text
)
77 wxFont font
= attr
.GetFont();
80 font
= attrDef
.GetFont();
82 if ( text
&& !font
.Ok() )
83 font
= text
->GetFont();
86 wxColour colFg
= attr
.GetTextColour();
89 colFg
= attrDef
.GetTextColour();
91 if ( text
&& !colFg
.Ok() )
92 colFg
= text
->GetForegroundColour();
95 wxColour colBg
= attr
.GetBackgroundColour();
98 colBg
= attrDef
.GetBackgroundColour();
100 if ( text
&& !colBg
.Ok() )
101 colBg
= text
->GetBackgroundColour();
104 return wxTextAttr(colFg
, colBg
, font
);
107 // apply styling to text range
108 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
109 const wxTextAttr
& WXUNUSED(style
))
111 // to be implemented in derived TextCtrl classes
115 // change default text attributes
116 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
118 // keep the old attributes if the new style doesn't specify them unless the
119 // new style is empty - then reset m_defaultStyle (as there is no other way
121 if ( style
.IsDefault() )
122 m_defaultStyle
= style
;
124 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
129 // get default text attributes
130 const wxTextAttr
& wxTextCtrlBase::GetDefaultStyle() const
132 return m_defaultStyle
;
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 bool wxTextCtrlBase::LoadFile(const wxString
& filename
)
142 wxFFile
file(filename
);
143 if ( file
.IsOpened() )
146 if ( file
.ReadAll(&text
) )
152 m_filename
= filename
;
158 wxLogError(_("File couldn't be loaded."));
159 #endif // wxUSE_FFILE
164 bool wxTextCtrlBase::SaveFile(const wxString
& filename
)
166 wxString filenameToUse
= filename
.IsEmpty() ? m_filename
: filename
;
167 if ( !filenameToUse
)
169 // what kind of message to give? is it an error or a program bug?
170 wxLogDebug(wxT("Can't save textctrl to file without filename."));
176 wxFFile
file(filename
, "w");
177 if ( file
.IsOpened() && file
.Write(GetValue()) )
179 // it's not modified any longer
182 m_filename
= filename
;
187 wxLogError(_("The text couldn't be saved."));
188 #endif // wxUSE_FFILE
193 // ----------------------------------------------------------------------------
194 // stream-like insertion operator
195 // ----------------------------------------------------------------------------
197 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
200 return *TEXTCTRL(this);
203 wxTextCtrl
& wxTextCtrlBase::operator<<(float f
)
206 str
.Printf(wxT("%.2f"), f
);
208 return *TEXTCTRL(this);
211 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
214 str
.Printf(wxT("%.2f"), d
);
216 return *TEXTCTRL(this);
219 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
222 str
.Printf(wxT("%d"), i
);
224 return *TEXTCTRL(this);
227 wxTextCtrl
& wxTextCtrlBase::operator<<(long i
)
230 str
.Printf(wxT("%ld"), i
);
232 return *TEXTCTRL(this);
235 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxChar c
)
237 return operator<<(wxString(c
));
240 // ----------------------------------------------------------------------------
241 // streambuf methods implementation
242 // ----------------------------------------------------------------------------
244 #ifndef NO_TEXT_WINDOW_STREAM
246 int wxTextCtrlBase::overflow(int c
)
248 AppendText((wxChar
)c
);
250 // return something different from EOF
254 #endif // NO_TEXT_WINDOW_STREAM
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 bool wxTextCtrlBase::CanCopy() const
262 // can copy if there's a selection
264 GetSelection(&from
, &to
);
268 bool wxTextCtrlBase::CanCut() const
270 // can cut if there's a selection and if we're not read only
271 return CanCopy() && IsEditable();
274 bool wxTextCtrlBase::CanPaste() const
276 // can paste if we are not read only
280 // ----------------------------------------------------------------------------
281 // emulating key presses
282 // ----------------------------------------------------------------------------
284 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
286 // the generic version is unused in wxMSW
289 int keycode
= event
.GetKeyCode();
302 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
306 case WXK_NUMPAD_MULTIPLY
:
316 case WXK_NUMPAD_SUBTRACT
:
321 case WXK_NUMPAD_DECIMAL
:
326 case WXK_NUMPAD_DIVIDE
:
331 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
333 // FIXME this is not going to work for non letters...
334 if ( !event
.ShiftDown() )
336 keycode
= tolower(keycode
);
339 ch
= (wxChar
)keycode
;
358 // ----------------------------------------------------------------------------
359 // selection and ranges
360 // ----------------------------------------------------------------------------
362 void wxTextCtrlBase::SelectAll()
364 SetSelection(0, GetLastPosition());
367 wxString
wxTextCtrlBase::GetStringSelection() const
370 GetSelection(&from
, &to
);
372 return GetRange(from
, to
);
375 wxString
wxTextCtrlBase::GetRange(long from
, long to
) const
380 sel
= GetValue().Mid(from
, to
- from
);
386 #else // !wxUSE_TEXTCTRL
388 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
389 // controls (wxComboBox and wxSpinCtrl)
390 #include "wx/event.h"
392 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
394 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL