| 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) wxWindows team |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | #ifdef __GNUG__ |
| 16 | #pragma implementation "textctrlbase.h" |
| 17 | #endif |
| 18 | |
| 19 | // for compilers that support precompilation, includes "wx.h". |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #ifndef WX_PRECOMP |
| 27 | #include "wx/intl.h" |
| 28 | #include "wx/log.h" |
| 29 | #include "wx/textctrl.h" |
| 30 | #endif // WX_PRECOMP |
| 31 | |
| 32 | #include "wx/ffile.h" |
| 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | // macros |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | // we don't have any objects of type wxTextCtrlBase in the program, only |
| 39 | // wxTextCtrl, so this cast is safe |
| 40 | #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr)) |
| 41 | |
| 42 | // ============================================================================ |
| 43 | // implementation |
| 44 | // ============================================================================ |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // ctor |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | wxTextCtrlBase::wxTextCtrlBase() |
| 51 | { |
| 52 | #ifndef NO_TEXT_WINDOW_STREAM |
| 53 | if (allocate()) |
| 54 | setp(base(),ebuf()); |
| 55 | #endif // NO_TEXT_WINDOW_STREAM |
| 56 | } |
| 57 | |
| 58 | // ---------------------------------------------------------------------------- |
| 59 | // file IO functions |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | bool wxTextCtrlBase::LoadFile(const wxString& filename) |
| 63 | { |
| 64 | wxFFile file(filename); |
| 65 | if ( file.IsOpened() ) |
| 66 | { |
| 67 | wxString text; |
| 68 | if ( file.ReadAll(&text) ) |
| 69 | { |
| 70 | SetValue(text); |
| 71 | |
| 72 | DiscardEdits(); |
| 73 | |
| 74 | m_filename = filename; |
| 75 | |
| 76 | return TRUE; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | wxLogError(_("File couldn't be loaded.")); |
| 81 | |
| 82 | return FALSE; |
| 83 | } |
| 84 | |
| 85 | bool wxTextCtrlBase::SaveFile(const wxString& filename) |
| 86 | { |
| 87 | wxString filenameToUse = filename.IsEmpty() ? m_filename : filename; |
| 88 | if ( !filenameToUse ) |
| 89 | { |
| 90 | // what kind of message to give? is it an error or a program bug? |
| 91 | wxLogDebug(wxT("Can't save textctrl to file without filename.")); |
| 92 | |
| 93 | return FALSE; |
| 94 | } |
| 95 | |
| 96 | wxFFile file(filename, "w"); |
| 97 | if ( file.IsOpened() && file.Write(GetValue()) ) |
| 98 | { |
| 99 | // it's not modified any longer |
| 100 | DiscardEdits(); |
| 101 | |
| 102 | m_filename = filename; |
| 103 | |
| 104 | return TRUE; |
| 105 | } |
| 106 | |
| 107 | wxLogError(_("The text couldn't be saved.")); |
| 108 | |
| 109 | return FALSE; |
| 110 | } |
| 111 | |
| 112 | // ---------------------------------------------------------------------------- |
| 113 | // stream-like insertion operator |
| 114 | // ---------------------------------------------------------------------------- |
| 115 | |
| 116 | wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s) |
| 117 | { |
| 118 | AppendText(s); |
| 119 | return *TEXTCTRL(this); |
| 120 | } |
| 121 | |
| 122 | wxTextCtrl& wxTextCtrlBase::operator<<(float f) |
| 123 | { |
| 124 | wxString str; |
| 125 | str.Printf(wxT("%.2f"), f); |
| 126 | AppendText(str); |
| 127 | return *TEXTCTRL(this); |
| 128 | } |
| 129 | |
| 130 | wxTextCtrl& wxTextCtrlBase::operator<<(double d) |
| 131 | { |
| 132 | wxString str; |
| 133 | str.Printf(wxT("%.2f"), d); |
| 134 | AppendText(str); |
| 135 | return *TEXTCTRL(this); |
| 136 | } |
| 137 | |
| 138 | wxTextCtrl& wxTextCtrlBase::operator<<(int i) |
| 139 | { |
| 140 | wxString str; |
| 141 | str.Printf(wxT("%d"), i); |
| 142 | AppendText(str); |
| 143 | return *TEXTCTRL(this); |
| 144 | } |
| 145 | |
| 146 | wxTextCtrl& wxTextCtrlBase::operator<<(long i) |
| 147 | { |
| 148 | wxString str; |
| 149 | str.Printf(wxT("%ld"), i); |
| 150 | AppendText(str); |
| 151 | return *TEXTCTRL(this); |
| 152 | } |
| 153 | |
| 154 | wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c) |
| 155 | { |
| 156 | return operator<<(wxString(c)); |
| 157 | } |
| 158 | |
| 159 | // ---------------------------------------------------------------------------- |
| 160 | // streambuf methods implementation |
| 161 | // ---------------------------------------------------------------------------- |
| 162 | |
| 163 | #ifndef NO_TEXT_WINDOW_STREAM |
| 164 | |
| 165 | int wxTextCtrlBase::overflow( int WXUNUSED(c) ) |
| 166 | { |
| 167 | int len = pptr() - pbase(); |
| 168 | char *txt = new char[len+1]; |
| 169 | strncpy(txt, pbase(), len); |
| 170 | txt[len] = '\0'; |
| 171 | (*this) << txt; |
| 172 | setp(pbase(), epptr()); |
| 173 | delete[] txt; |
| 174 | return EOF; |
| 175 | } |
| 176 | |
| 177 | int wxTextCtrlBase::sync() |
| 178 | { |
| 179 | int len = pptr() - pbase(); |
| 180 | char *txt = new char[len+1]; |
| 181 | strncpy(txt, pbase(), len); |
| 182 | txt[len] = '\0'; |
| 183 | (*this) << txt; |
| 184 | setp(pbase(), epptr()); |
| 185 | delete[] txt; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int wxTextCtrlBase::underflow() |
| 190 | { |
| 191 | return EOF; |
| 192 | } |
| 193 | |
| 194 | #endif // NO_TEXT_WINDOW_STREAM |
| 195 | |