| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: textctrl.cpp |
| 3 | // Purpose: wxTextCtrl |
| 4 | // Author: AUTHOR |
| 5 | // Modified by: |
| 6 | // Created: ??/??/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) AUTHOR |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "textctrl.h" |
| 14 | #endif |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <fstream.h> |
| 19 | |
| 20 | #include "wx/textctrl.h" |
| 21 | #include "wx/settings.h" |
| 22 | #include "wx/filefn.h" |
| 23 | #include "wx/utils.h" |
| 24 | |
| 25 | #if defined(__BORLANDC__) && !defined(__WIN32__) |
| 26 | #include <alloc.h> |
| 27 | #else |
| 28 | #ifndef __GNUWIN32__ |
| 29 | #include <malloc.h> |
| 30 | #endif |
| 31 | #endif |
| 32 | |
| 33 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
| 34 | |
| 35 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
| 36 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) |
| 37 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) |
| 38 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) |
| 39 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) |
| 40 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) |
| 41 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) |
| 42 | |
| 43 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) |
| 44 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) |
| 45 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) |
| 46 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) |
| 47 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) |
| 48 | END_EVENT_TABLE() |
| 49 | |
| 50 | // Text item |
| 51 | wxTextCtrl::wxTextCtrl() |
| 52 | #ifndef NO_TEXT_WINDOW_STREAM |
| 53 | :streambuf() |
| 54 | #endif |
| 55 | { |
| 56 | m_fileName = ""; |
| 57 | } |
| 58 | |
| 59 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, |
| 60 | const wxString& value, |
| 61 | const wxPoint& pos, |
| 62 | const wxSize& size, long style, |
| 63 | const wxValidator& validator, |
| 64 | const wxString& name) |
| 65 | { |
| 66 | m_fileName = ""; |
| 67 | SetName(name); |
| 68 | SetValidator(validator); |
| 69 | if (parent) parent->AddChild(this); |
| 70 | |
| 71 | m_windowStyle = style; |
| 72 | |
| 73 | if ( id == -1 ) |
| 74 | m_windowId = (int)NewControlId(); |
| 75 | else |
| 76 | m_windowId = id; |
| 77 | |
| 78 | return TRUE; |
| 79 | } |
| 80 | |
| 81 | wxString wxTextCtrl::GetValue() const |
| 82 | { |
| 83 | // TODO |
| 84 | return wxString(""); |
| 85 | } |
| 86 | |
| 87 | void wxTextCtrl::SetValue(const wxString& value) |
| 88 | { |
| 89 | // TODO |
| 90 | } |
| 91 | |
| 92 | void wxTextCtrl::SetSize(int x, int y, int width, int height, int sizeFlags) |
| 93 | { |
| 94 | // TODO |
| 95 | } |
| 96 | |
| 97 | // Clipboard operations |
| 98 | void wxTextCtrl::Copy() |
| 99 | { |
| 100 | // TODO |
| 101 | } |
| 102 | |
| 103 | void wxTextCtrl::Cut() |
| 104 | { |
| 105 | // TODO |
| 106 | } |
| 107 | |
| 108 | void wxTextCtrl::Paste() |
| 109 | { |
| 110 | // TODO |
| 111 | } |
| 112 | |
| 113 | void wxTextCtrl::SetEditable(bool editable) |
| 114 | { |
| 115 | // TODO |
| 116 | } |
| 117 | |
| 118 | void wxTextCtrl::SetInsertionPoint(long pos) |
| 119 | { |
| 120 | // TODO |
| 121 | } |
| 122 | |
| 123 | void wxTextCtrl::SetInsertionPointEnd() |
| 124 | { |
| 125 | long pos = GetLastPosition(); |
| 126 | SetInsertionPoint(pos); |
| 127 | } |
| 128 | |
| 129 | long wxTextCtrl::GetInsertionPoint() const |
| 130 | { |
| 131 | // TODO |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | long wxTextCtrl::GetLastPosition() const |
| 136 | { |
| 137 | // TODO |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
| 142 | { |
| 143 | // TODO |
| 144 | } |
| 145 | |
| 146 | void wxTextCtrl::Remove(long from, long to) |
| 147 | { |
| 148 | // TODO |
| 149 | } |
| 150 | |
| 151 | void wxTextCtrl::SetSelection(long from, long to) |
| 152 | { |
| 153 | // TODO |
| 154 | } |
| 155 | |
| 156 | bool wxTextCtrl::LoadFile(const wxString& file) |
| 157 | { |
| 158 | if (!wxFileExists(file)) |
| 159 | return FALSE; |
| 160 | |
| 161 | m_fileName = file; |
| 162 | |
| 163 | Clear(); |
| 164 | |
| 165 | ifstream input((char*) (const char*) file, ios::nocreate | ios::in); |
| 166 | |
| 167 | if (!input.bad()) |
| 168 | { |
| 169 | struct stat stat_buf; |
| 170 | if (stat(file, &stat_buf) < 0) |
| 171 | return FALSE; |
| 172 | // This may need to be a bigger buffer than the file size suggests, |
| 173 | // if it's a UNIX file. Give it an extra 1000 just in case. |
| 174 | char *tmp_buffer = (char*)malloc((size_t)(stat_buf.st_size+1+1000)); |
| 175 | long no_lines = 0; |
| 176 | long pos = 0; |
| 177 | while (!input.eof() && input.peek() != EOF) |
| 178 | { |
| 179 | input.getline(wxBuffer, 500); |
| 180 | int len = strlen(wxBuffer); |
| 181 | wxBuffer[len] = 13; |
| 182 | wxBuffer[len+1] = 10; |
| 183 | wxBuffer[len+2] = 0; |
| 184 | strcpy(tmp_buffer+pos, wxBuffer); |
| 185 | pos += strlen(wxBuffer); |
| 186 | no_lines++; |
| 187 | } |
| 188 | |
| 189 | // TODO add line |
| 190 | |
| 191 | free(tmp_buffer); |
| 192 | |
| 193 | return TRUE; |
| 194 | } |
| 195 | return FALSE; |
| 196 | } |
| 197 | |
| 198 | // If file is null, try saved file name first |
| 199 | // Returns TRUE if succeeds. |
| 200 | bool wxTextCtrl::SaveFile(const wxString& file) |
| 201 | { |
| 202 | wxString theFile(file); |
| 203 | if (theFile == "") |
| 204 | theFile = m_fileName; |
| 205 | if (theFile == "") |
| 206 | return FALSE; |
| 207 | m_fileName = theFile; |
| 208 | |
| 209 | ofstream output((char*) (const char*) theFile); |
| 210 | if (output.bad()) |
| 211 | return FALSE; |
| 212 | |
| 213 | // TODO get and save text |
| 214 | |
| 215 | return FALSE; |
| 216 | } |
| 217 | |
| 218 | void wxTextCtrl::WriteText(const wxString& text) |
| 219 | { |
| 220 | // TODO write text to control |
| 221 | } |
| 222 | |
| 223 | void wxTextCtrl::AppendText(const wxString& text) |
| 224 | { |
| 225 | // TODO append text to control |
| 226 | } |
| 227 | |
| 228 | void wxTextCtrl::Clear() |
| 229 | { |
| 230 | // TODO |
| 231 | } |
| 232 | |
| 233 | bool wxTextCtrl::IsModified() const |
| 234 | { |
| 235 | // TODO |
| 236 | return FALSE; |
| 237 | } |
| 238 | |
| 239 | // Makes 'unmodified' |
| 240 | void wxTextCtrl::DiscardEdits() |
| 241 | { |
| 242 | // TODO |
| 243 | } |
| 244 | |
| 245 | int wxTextCtrl::GetNumberOfLines() const |
| 246 | { |
| 247 | // TODO |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | long wxTextCtrl::XYToPosition(long x, long y) const |
| 252 | { |
| 253 | // TODO |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
| 258 | { |
| 259 | // TODO |
| 260 | } |
| 261 | |
| 262 | void wxTextCtrl::ShowPosition(long pos) |
| 263 | { |
| 264 | // TODO |
| 265 | } |
| 266 | |
| 267 | int wxTextCtrl::GetLineLength(long lineNo) const |
| 268 | { |
| 269 | // TODO |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | wxString wxTextCtrl::GetLineText(long lineNo) const |
| 274 | { |
| 275 | // TODO |
| 276 | return wxString(""); |
| 277 | } |
| 278 | |
| 279 | bool wxTextCtrl::CanCopy() const |
| 280 | { |
| 281 | // Can copy if there's a selection |
| 282 | long from, to; |
| 283 | GetSelection(& from, & to); |
| 284 | return (from != to) ; |
| 285 | } |
| 286 | |
| 287 | bool wxTextCtrl::CanCut() const |
| 288 | { |
| 289 | // Can cut if there's a selection |
| 290 | long from, to; |
| 291 | GetSelection(& from, & to); |
| 292 | return (from != to) ; |
| 293 | } |
| 294 | |
| 295 | bool wxTextCtrl::CanPaste() const |
| 296 | { |
| 297 | return IsEditable() ; |
| 298 | } |
| 299 | |
| 300 | // Undo/redo |
| 301 | void wxTextCtrl::Undo() |
| 302 | { |
| 303 | // TODO |
| 304 | } |
| 305 | |
| 306 | void wxTextCtrl::Redo() |
| 307 | { |
| 308 | // TODO |
| 309 | } |
| 310 | |
| 311 | bool wxTextCtrl::CanUndo() const |
| 312 | { |
| 313 | // TODO |
| 314 | return FALSE; |
| 315 | } |
| 316 | |
| 317 | bool wxTextCtrl::CanRedo() const |
| 318 | { |
| 319 | // TODO |
| 320 | return FALSE; |
| 321 | } |
| 322 | |
| 323 | // If the return values from and to are the same, there is no |
| 324 | // selection. |
| 325 | void wxTextCtrl::GetSelection(long* from, long* to) const |
| 326 | { |
| 327 | // TODO |
| 328 | *from = 0; |
| 329 | *to = 0; |
| 330 | } |
| 331 | |
| 332 | bool wxTextCtrl::IsEditable() const |
| 333 | { |
| 334 | // TODO |
| 335 | return FALSE; |
| 336 | } |
| 337 | |
| 338 | void wxTextCtrl::Command(wxCommandEvent & event) |
| 339 | { |
| 340 | SetValue (event.GetString()); |
| 341 | ProcessCommand (event); |
| 342 | } |
| 343 | |
| 344 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) |
| 345 | { |
| 346 | // By default, load the first file into the text window. |
| 347 | if (event.GetNumberOfFiles() > 0) |
| 348 | { |
| 349 | LoadFile(event.GetFiles()[0]); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of |
| 354 | // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers |
| 355 | |
| 356 | //========================================================================= |
| 357 | // Called then the buffer is full (gcc 2.6.3) |
| 358 | // or when "endl" is output (Borland 4.5) |
| 359 | //========================================================================= |
| 360 | // Class declaration using multiple inheritance doesn't work properly for |
| 361 | // Borland. See note in wb_text.h. |
| 362 | #ifndef NO_TEXT_WINDOW_STREAM |
| 363 | int wxTextCtrl::overflow(int c) |
| 364 | { |
| 365 | // Make sure there is a holding area |
| 366 | if ( allocate()==EOF ) |
| 367 | { |
| 368 | wxError("Streambuf allocation failed","Internal error"); |
| 369 | return EOF; |
| 370 | } |
| 371 | |
| 372 | // Verify that there are no characters in get area |
| 373 | if ( gptr() && gptr() < egptr() ) |
| 374 | { |
| 375 | wxError("Who's trespassing my get area?","Internal error"); |
| 376 | return EOF; |
| 377 | } |
| 378 | |
| 379 | // Reset get area |
| 380 | setg(0,0,0); |
| 381 | |
| 382 | // Make sure there is a put area |
| 383 | if ( ! pptr() ) |
| 384 | { |
| 385 | /* This doesn't seem to be fatal so comment out error message */ |
| 386 | // wxError("Put area not opened","Internal error"); |
| 387 | setp( base(), base() ); |
| 388 | } |
| 389 | |
| 390 | // Determine how many characters have been inserted but no consumed |
| 391 | int plen = pptr() - pbase(); |
| 392 | |
| 393 | // Now Jerry relies on the fact that the buffer is at least 2 chars |
| 394 | // long, but the holding area "may be as small as 1" ??? |
| 395 | // And we need an additional \0, so let's keep this inefficient but |
| 396 | // safe copy. |
| 397 | |
| 398 | // If c!=EOF, it is a character that must also be comsumed |
| 399 | int xtra = c==EOF? 0 : 1; |
| 400 | |
| 401 | // Write temporary C-string to wxTextWindow |
| 402 | { |
| 403 | char *txt = new char[plen+xtra+1]; |
| 404 | memcpy(txt, pbase(), plen); |
| 405 | txt[plen] = (char)c; // append c |
| 406 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c |
| 407 | // If the put area already contained \0, output will be truncated there |
| 408 | AppendText(txt); |
| 409 | delete[] txt; |
| 410 | } |
| 411 | |
| 412 | // Reset put area |
| 413 | setp(pbase(), epptr()); |
| 414 | |
| 415 | #if defined(__WATCOMC__) |
| 416 | return __NOT_EOF; |
| 417 | #elif defined(zapeof) // HP-UX (all cfront based?) |
| 418 | return zapeof(c); |
| 419 | #else |
| 420 | return c!=EOF ? c : 0; // this should make everybody happy |
| 421 | #endif |
| 422 | } |
| 423 | |
| 424 | //========================================================================= |
| 425 | // called then "endl" is output (gcc) or then explicit sync is done (Borland) |
| 426 | //========================================================================= |
| 427 | int wxTextCtrl::sync() |
| 428 | { |
| 429 | // Verify that there are no characters in get area |
| 430 | if ( gptr() && gptr() < egptr() ) |
| 431 | { |
| 432 | wxError("Who's trespassing my get area?","Internal error"); |
| 433 | return EOF; |
| 434 | } |
| 435 | |
| 436 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); |
| 437 | |
| 438 | return 0; |
| 439 | /* OLD CODE |
| 440 | int len = pptr() - pbase(); |
| 441 | char *txt = new char[len+1]; |
| 442 | strncpy(txt, pbase(), len); |
| 443 | txt[len] = '\0'; |
| 444 | (*this) << txt; |
| 445 | setp(pbase(), epptr()); |
| 446 | delete[] txt; |
| 447 | return 0; |
| 448 | */ |
| 449 | } |
| 450 | |
| 451 | //========================================================================= |
| 452 | // Should not be called by a "ostream". Used by a "istream" |
| 453 | //========================================================================= |
| 454 | int wxTextCtrl::underflow() |
| 455 | { |
| 456 | return EOF; |
| 457 | } |
| 458 | #endif |
| 459 | |
| 460 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) |
| 461 | { |
| 462 | AppendText(s); |
| 463 | return *this; |
| 464 | } |
| 465 | |
| 466 | wxTextCtrl& wxTextCtrl::operator<<(float f) |
| 467 | { |
| 468 | wxString str; |
| 469 | str.Printf("%.2f", f); |
| 470 | AppendText(str); |
| 471 | return *this; |
| 472 | } |
| 473 | |
| 474 | wxTextCtrl& wxTextCtrl::operator<<(double d) |
| 475 | { |
| 476 | wxString str; |
| 477 | str.Printf("%.2f", d); |
| 478 | AppendText(str); |
| 479 | return *this; |
| 480 | } |
| 481 | |
| 482 | wxTextCtrl& wxTextCtrl::operator<<(int i) |
| 483 | { |
| 484 | wxString str; |
| 485 | str.Printf("%d", i); |
| 486 | AppendText(str); |
| 487 | return *this; |
| 488 | } |
| 489 | |
| 490 | wxTextCtrl& wxTextCtrl::operator<<(long i) |
| 491 | { |
| 492 | wxString str; |
| 493 | str.Printf("%ld", i); |
| 494 | AppendText(str); |
| 495 | return *this; |
| 496 | } |
| 497 | |
| 498 | wxTextCtrl& wxTextCtrl::operator<<(const char c) |
| 499 | { |
| 500 | char buf[2]; |
| 501 | |
| 502 | buf[0] = c; |
| 503 | buf[1] = 0; |
| 504 | AppendText(buf); |
| 505 | return *this; |
| 506 | } |
| 507 | |
| 508 | void wxTextCtrl::OnCut(wxCommandEvent& event) |
| 509 | { |
| 510 | Cut(); |
| 511 | } |
| 512 | |
| 513 | void wxTextCtrl::OnCopy(wxCommandEvent& event) |
| 514 | { |
| 515 | Copy(); |
| 516 | } |
| 517 | |
| 518 | void wxTextCtrl::OnPaste(wxCommandEvent& event) |
| 519 | { |
| 520 | Paste(); |
| 521 | } |
| 522 | |
| 523 | void wxTextCtrl::OnUndo(wxCommandEvent& event) |
| 524 | { |
| 525 | Undo(); |
| 526 | } |
| 527 | |
| 528 | void wxTextCtrl::OnRedo(wxCommandEvent& event) |
| 529 | { |
| 530 | Redo(); |
| 531 | } |
| 532 | |
| 533 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) |
| 534 | { |
| 535 | event.Enable( CanCut() ); |
| 536 | } |
| 537 | |
| 538 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) |
| 539 | { |
| 540 | event.Enable( CanCopy() ); |
| 541 | } |
| 542 | |
| 543 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) |
| 544 | { |
| 545 | event.Enable( CanPaste() ); |
| 546 | } |
| 547 | |
| 548 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) |
| 549 | { |
| 550 | event.Enable( CanUndo() ); |
| 551 | } |
| 552 | |
| 553 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) |
| 554 | { |
| 555 | event.Enable( CanRedo() ); |
| 556 | } |