1999-07-14 22:55:57 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: common/textcmn.cpp
|
|
|
|
// Purpose: implementation of platform-independent functions of wxTextCtrl
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by:
|
|
|
|
// Created: 13.07.99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) wxWindows team
|
|
|
|
// Licence: wxWindows license
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
2001-06-26 20:59:19 +00:00
|
|
|
|
1999-08-12 12:18:49 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "textctrlbase.h"
|
|
|
|
#endif
|
2001-06-26 20:59:19 +00:00
|
|
|
|
1999-07-14 22:55:57 +00:00
|
|
|
// for compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2001-06-26 20:59:19 +00:00
|
|
|
#if wxUSE_TEXTCTRL
|
|
|
|
|
1999-07-14 22:55:57 +00:00
|
|
|
#ifndef WX_PRECOMP
|
1999-07-15 13:42:27 +00:00
|
|
|
#include "wx/intl.h"
|
|
|
|
#include "wx/log.h"
|
1999-07-14 22:55:57 +00:00
|
|
|
#include "wx/textctrl.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// macros
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// we don't have any objects of type wxTextCtrlBase in the program, only
|
|
|
|
// wxTextCtrl, so this cast is safe
|
|
|
|
#define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
2001-07-29 22:40:21 +00:00
|
|
|
IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
|
|
|
|
|
|
|
|
DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
|
|
|
|
DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER)
|
|
|
|
DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL)
|
|
|
|
|
1999-07-14 22:55:57 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ctor
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wxTextCtrlBase::wxTextCtrlBase()
|
|
|
|
{
|
2000-02-17 18:23:41 +00:00
|
|
|
#ifndef NO_TEXT_WINDOW_STREAM
|
2001-06-26 20:59:19 +00:00
|
|
|
#if wxUSE_IOSTREAMH
|
|
|
|
if (allocate())
|
|
|
|
setp(base(),ebuf());
|
|
|
|
#else
|
|
|
|
m_streambuf = new char[64];
|
|
|
|
setp(m_streambuf, m_streambuf + 64);
|
|
|
|
#endif //wxUSE_IOSTREAMH
|
2000-02-17 18:23:41 +00:00
|
|
|
#endif // NO_TEXT_WINDOW_STREAM
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTextCtrlBase::~wxTextCtrlBase()
|
|
|
|
{
|
|
|
|
#ifndef NO_TEXT_WINDOW_STREAM
|
|
|
|
#if !wxUSE_IOSTREAMH
|
2001-06-06 11:17:30 +00:00
|
|
|
delete[] m_streambuf;
|
2000-02-17 18:23:41 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
1999-07-14 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2001-05-27 03:28:28 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// style functions - not implemented here
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// apply styling to text range
|
|
|
|
bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
|
|
|
|
const wxTextAttr& WXUNUSED(style))
|
|
|
|
{
|
|
|
|
// to be implemented in derived TextCtrl classes
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// change default text attributes
|
|
|
|
bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr &style)
|
|
|
|
{
|
|
|
|
m_defaultStyle = style;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get default text attributes
|
|
|
|
const wxTextAttr& wxTextCtrlBase::GetDefaultStyle() const
|
|
|
|
{
|
|
|
|
return m_defaultStyle;
|
|
|
|
}
|
|
|
|
|
1999-07-14 22:55:57 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// file IO functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool wxTextCtrlBase::LoadFile(const wxString& filename)
|
|
|
|
{
|
2001-06-26 20:59:19 +00:00
|
|
|
#if wxUSE_FFILE
|
1999-07-14 22:55:57 +00:00
|
|
|
wxFFile file(filename);
|
|
|
|
if ( file.IsOpened() )
|
|
|
|
{
|
|
|
|
wxString text;
|
|
|
|
if ( file.ReadAll(&text) )
|
|
|
|
{
|
|
|
|
SetValue(text);
|
|
|
|
|
|
|
|
DiscardEdits();
|
|
|
|
|
|
|
|
m_filename = filename;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogError(_("File couldn't be loaded."));
|
2001-06-26 20:59:19 +00:00
|
|
|
#endif // wxUSE_FFILE
|
1999-07-14 22:55:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxTextCtrlBase::SaveFile(const wxString& filename)
|
|
|
|
{
|
|
|
|
wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
|
|
|
|
if ( !filenameToUse )
|
|
|
|
{
|
|
|
|
// what kind of message to give? is it an error or a program bug?
|
1999-10-08 14:35:56 +00:00
|
|
|
wxLogDebug(wxT("Can't save textctrl to file without filename."));
|
1999-07-14 22:55:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-06-26 20:59:19 +00:00
|
|
|
#if wxUSE_FFILE
|
1999-07-14 22:55:57 +00:00
|
|
|
wxFFile file(filename, "w");
|
|
|
|
if ( file.IsOpened() && file.Write(GetValue()) )
|
|
|
|
{
|
|
|
|
// it's not modified any longer
|
|
|
|
DiscardEdits();
|
|
|
|
|
|
|
|
m_filename = filename;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogError(_("The text couldn't be saved."));
|
2001-06-26 20:59:19 +00:00
|
|
|
#endif // wxUSE_FFILE
|
1999-07-14 22:55:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// stream-like insertion operator
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
|
|
|
|
{
|
|
|
|
AppendText(s);
|
|
|
|
return *TEXTCTRL(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTextCtrl& wxTextCtrlBase::operator<<(float f)
|
|
|
|
{
|
|
|
|
wxString str;
|
1999-10-08 14:35:56 +00:00
|
|
|
str.Printf(wxT("%.2f"), f);
|
1999-07-14 22:55:57 +00:00
|
|
|
AppendText(str);
|
|
|
|
return *TEXTCTRL(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTextCtrl& wxTextCtrlBase::operator<<(double d)
|
|
|
|
{
|
|
|
|
wxString str;
|
1999-10-08 14:35:56 +00:00
|
|
|
str.Printf(wxT("%.2f"), d);
|
1999-07-14 22:55:57 +00:00
|
|
|
AppendText(str);
|
|
|
|
return *TEXTCTRL(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTextCtrl& wxTextCtrlBase::operator<<(int i)
|
|
|
|
{
|
|
|
|
wxString str;
|
1999-10-08 14:35:56 +00:00
|
|
|
str.Printf(wxT("%d"), i);
|
1999-07-14 22:55:57 +00:00
|
|
|
AppendText(str);
|
|
|
|
return *TEXTCTRL(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTextCtrl& wxTextCtrlBase::operator<<(long i)
|
|
|
|
{
|
|
|
|
wxString str;
|
1999-10-08 14:35:56 +00:00
|
|
|
str.Printf(wxT("%ld"), i);
|
1999-07-14 22:55:57 +00:00
|
|
|
AppendText(str);
|
|
|
|
return *TEXTCTRL(this);
|
|
|
|
}
|
|
|
|
|
1999-07-22 17:51:54 +00:00
|
|
|
wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
|
1999-07-14 22:55:57 +00:00
|
|
|
{
|
|
|
|
return operator<<(wxString(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// streambuf methods implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifndef NO_TEXT_WINDOW_STREAM
|
|
|
|
|
|
|
|
int wxTextCtrlBase::overflow( int WXUNUSED(c) )
|
|
|
|
{
|
|
|
|
int len = pptr() - pbase();
|
|
|
|
char *txt = new char[len+1];
|
|
|
|
strncpy(txt, pbase(), len);
|
|
|
|
txt[len] = '\0';
|
|
|
|
(*this) << txt;
|
|
|
|
setp(pbase(), epptr());
|
|
|
|
delete[] txt;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wxTextCtrlBase::sync()
|
|
|
|
{
|
|
|
|
int len = pptr() - pbase();
|
|
|
|
char *txt = new char[len+1];
|
|
|
|
strncpy(txt, pbase(), len);
|
|
|
|
txt[len] = '\0';
|
|
|
|
(*this) << txt;
|
|
|
|
setp(pbase(), epptr());
|
|
|
|
delete[] txt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wxTextCtrlBase::underflow()
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // NO_TEXT_WINDOW_STREAM
|
|
|
|
|
2001-06-26 20:59:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// clipboard stuff
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool wxTextCtrlBase::CanCopy() const
|
|
|
|
{
|
|
|
|
// can copy if there's a selection
|
|
|
|
long from, to;
|
|
|
|
GetSelection(&from, &to);
|
|
|
|
return from != to;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxTextCtrlBase::CanCut() const
|
|
|
|
{
|
|
|
|
// can cut if there's a selection and if we're not read only
|
|
|
|
return CanCopy() && IsEditable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxTextCtrlBase::CanPaste() const
|
|
|
|
{
|
|
|
|
// can paste if we are not read only
|
|
|
|
return IsEditable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// misc
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void wxTextCtrlBase::SelectAll()
|
|
|
|
{
|
|
|
|
SetSelection(0, GetLastPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // wxUSE_TEXTCTRL
|
|
|
|
|