diff --git a/m4/win32.m4 b/m4/win32.m4 index 455fb6f5..6090afc5 100644 --- a/m4/win32.m4 +++ b/m4/win32.m4 @@ -18,6 +18,7 @@ if test "x$target_win32" = "xyes"; then AC_MSG_WARN([The windres utility could not be found]) fi AC_DEFINE([__WOE32__], 1, [Define to 1 if we are building on cygwin or mingw]) + AC_DEFINE([__MINGW32__], 1, [Define to 1 if we are building on cygwin or mingw]) AC_DEFINE([_WINDOWS], 1, [Define to 1 if we are building on cygwin or mingw]) fi diff --git a/src/Makefile.am b/src/Makefile.am index 37684861..f9810da9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -546,6 +546,8 @@ EXTRA_fldigi_SOURCES += \ fileselector/Fl_Native_File_Chooser_MAC.cxx \ fileselector/Fl_Native_File_Chooser_WIN32.cxx \ fileselector/flnfc_common.cxx \ + fileselector/fileselect_1_1.cxx \ + fileselector/fileselect_1_3.cxx \ feld/Feld7x7-14.cxx \ feld/Feld7x7n-14.cxx \ feld/FeldDx-14.cxx \ diff --git a/src/dialogs/fl_digi.cxx b/src/dialogs/fl_digi.cxx index 11fe6b2b..30635b36 100644 --- a/src/dialogs/fl_digi.cxx +++ b/src/dialogs/fl_digi.cxx @@ -41,7 +41,23 @@ #include #include #include -#include + +// this tests depends on a modified FL/filename.H in the Fltk-1.3.0 +// change +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) +// to +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WOE32__) + +#ifdef __MINGW32__ +# if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# undef dirent +# include +# else +# include +# endif +#else +# include +#endif #ifndef __WOE32__ #include @@ -3430,7 +3446,11 @@ int rightof(Fl_Widget* w) int leftof(Fl_Widget* w) { +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + unsigned int a = w->align(); +#else int a = w->align(); +#endif if (a == FL_ALIGN_CENTER || a & FL_ALIGN_INSIDE) return w->x(); @@ -3455,7 +3475,11 @@ int leftof(Fl_Widget* w) int above(Fl_Widget* w) { +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + unsigned int a = w->align(); +#else int a = w->align(); +#endif if (a == FL_ALIGN_CENTER || a & FL_ALIGN_INSIDE) return w->y(); @@ -3464,7 +3488,11 @@ int above(Fl_Widget* w) int below(Fl_Widget* w) { +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + unsigned int a = w->align(); +#else int a = w->align(); +#endif if (a == FL_ALIGN_CENTER || a & FL_ALIGN_INSIDE) return w->y() + w->h(); @@ -5782,6 +5810,7 @@ int Qidle_time = 0; static int que_timeout = 0; bool que_ok = true; +bool que_waiting = true; void post_queue_execute(void*) { @@ -5798,6 +5827,7 @@ void post_queue_execute(void*) void queue_execute_after_rx(void*) { + que_waiting = false; if (!que_timeout) { LOG_ERROR("%s", "timed out"); return; @@ -5813,6 +5843,12 @@ void queue_execute_after_rx(void*) queue_execute(); } +void do_que_execute(void *) +{ + que_waiting = false; + queue_execute(); +} + char szTestChar[] = "E|I|S|T|M|O|A|V"; int get_tx_char(void) { @@ -5911,10 +5947,12 @@ int get_tx_char(void) if (queue_must_rx()) { c = 3; que_timeout = 400; // 20 seconds - Fl::add_timeout(0.0, queue_execute_after_rx); + REQ(queue_execute_after_rx, (void *)0); + while(que_waiting) MilliSleep(1); } else { c = -1; - queue_execute(); + REQ(do_que_execute, (void*)0); + while(que_waiting) MilliSleep(1); } break; case '^': diff --git a/src/fileselector/Fl_Native_File_Chooser.cxx b/src/fileselector/Fl_Native_File_Chooser.cxx index 4967fcb2..05d336a2 100644 --- a/src/fileselector/Fl_Native_File_Chooser.cxx +++ b/src/fileselector/Fl_Native_File_Chooser.cxx @@ -19,10 +19,13 @@ // USA. // + #include +#if (FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3) || (FLARQ_FLTK_API_MAJOR == 1 && FLARQ_FLTK_API_MINOR < 3) + // Use Windows' chooser -#if defined(_WIN32) || defined(__CYGWIN__) +#if defined(__WIN32__) || defined(__CYGWIN__) #include "Fl_Native_File_Chooser_WIN32.cxx" #endif @@ -36,3 +39,4 @@ #include "Fl_Native_File_Chooser_FLTK.cxx" #endif +#endif diff --git a/src/fileselector/fileselect.cxx b/src/fileselector/fileselect.cxx index 8e893a84..4eec58b3 100644 --- a/src/fileselector/fileselect.cxx +++ b/src/fileselector/fileselect.cxx @@ -23,167 +23,12 @@ #include -#include -#include -#include +#if (FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3) || (FLARQ_FLTK_API_MAJOR == 1 && FLARQ_FLTK_API_MINOR < 3) -#include "fileselect.h" -#include "icons.h" -#include "debug.h" +# include "fileselect_1_1.cxx" -#include -#include - -#if FSEL_THREAD -# include -# include -# include "threads.h" -#endif - -using namespace std; - - -FSEL* FSEL::inst = 0; -static std::string filename; -#if FSEL_THREAD -static pthread_t fsel_thread; -sem_t fsel_sem; -#endif - -void FSEL::create(void) -{ - if (inst) - return; -#if FSEL_THREAD - if (sem_init(&fsel_sem, 0, 0) == -1) { - LOG_PERROR("sem_init"); - return; - } -#endif - inst = new FSEL; -} - -void FSEL::destroy(void) -{ -#if FSEL_THREAD - sem_destroy(&fsel_sem); -#endif - delete inst; - inst = 0; -} - - -FSEL::FSEL() - : chooser(new Fl_Native_File_Chooser) { } -FSEL::~FSEL() { delete chooser; } - - -#if FSEL_THREAD -void* FSEL::thread_func(void* arg) -{ - FSEL* fsel = reinterpret_cast(arg); - fsel->result = fsel->chooser->show(); - sem_post(&fsel_sem); - return NULL; -} -#endif - -const char* FSEL::get_file(void) -{ - // Calling directory() is apparently not enough on Linux -#if !defined(__WOE32__) && !defined(__APPLE__) - const char* preset = chooser->preset_file(); - if (preset && *preset != '/' && chooser->directory()) { - filename = chooser->directory(); - filename.append("/").append(preset); - chooser->preset_file(filename.c_str()); - } -#endif - -#if FSEL_THREAD - if (pthread_create(&fsel_thread, NULL, thread_func, this) != 0) { - fl_alert2("could not create file selector thread"); - return NULL; - } - for (;;) { - if (sem_trywait(&fsel_sem) == 0) - break; - Fl::wait(0.1); - } #else - result = chooser->show(); + +# include "fileselect_1_3.cxx" + #endif - - switch (result) { - case -1: - fl_alert2("%s", chooser->errmsg()); - // fall through - case 1: - return NULL; - default: - filename = chooser->filename(); - string::size_type i = filename.rfind('/'); - if (i != string::npos) - chooser->directory(filename.substr(0, i).c_str()); - return filename.c_str(); - } -} - -const char* FSEL::select(const char* title, const char* filter, const char* def, int* fsel) -{ - inst->chooser->title(title); - inst->chooser->filter(filter); - if (def) { - char *s = strdup(def), *dir = dirname(s); - if (strcmp(".", dir)) - inst->chooser->directory(dir); - free(s); - s = strdup(def); - inst->chooser->preset_file(basename(s)); - free(s); - } - inst->chooser->options(Fl_Native_File_Chooser::PREVIEW); - inst->chooser->type(Fl_Native_File_Chooser::BROWSE_FILE); - - const char* fn = inst->get_file(); - if (fsel) - *fsel = inst->chooser->filter_value(); - return fn; -} - -const char* FSEL::saveas(const char* title, const char* filter, const char* def, int* fsel) -{ - inst->chooser->title(title); - inst->chooser->filter(filter); - if (def) { - char *s = strdup(def), *dir = dirname(s); - if (strcmp(".", dir)) - inst->chooser->directory(dir); - free(s); - s = strdup(def); - inst->chooser->preset_file(basename(s)); - free(s); - } - inst->chooser->options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | - Fl_Native_File_Chooser::NEW_FOLDER | - Fl_Native_File_Chooser::PREVIEW); - inst->chooser->type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE); - - const char* fn = inst->get_file(); - if (fsel) - *fsel = inst->chooser->filter_value(); - return fn; -} - -const char* FSEL::dir_select(const char* title, const char* filter, const char* def) -{ - inst->chooser->title(title); - inst->chooser->filter(filter); - if (def) - inst->chooser->directory(def); - inst->chooser->options(Fl_Native_File_Chooser::NEW_FOLDER | - Fl_Native_File_Chooser::PREVIEW); - inst->chooser->type(Fl_Native_File_Chooser::BROWSE_DIRECTORY); - - return inst->get_file(); -} diff --git a/src/flarq-src/arqhelp.cxx b/src/flarq-src/arqhelp.cxx index eb482b7e..0ec4149b 100644 --- a/src/flarq-src/arqhelp.cxx +++ b/src/flarq-src/arqhelp.cxx @@ -47,6 +47,23 @@ #include #endif +// this tests depends on a modified FL/filename.H in the Fltk-1.3.0 +// change +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) +// to +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WOE32__) + +#ifdef __MINGW32__ +# if FLARQ_FLTK_API_MAJOR == 1 && FLARQ_FLTK_API_MINOR < 3 +# undef dirent +# include +# else +# include +# endif +#else +# include +#endif + #include "flarq.h" #include "arq.h" #include "arqdialogs.h" diff --git a/src/flarq-src/flarq.cxx b/src/flarq-src/flarq.cxx index f42945d2..1cde93cf 100644 --- a/src/flarq-src/flarq.cxx +++ b/src/flarq-src/flarq.cxx @@ -44,16 +44,25 @@ #include #include -#ifdef __MINGW32__ -# include "compat.h" -#endif +// this tests depends on a modified FL/filename.H in the Fltk-1.3.0 +// change +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) +// to +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WOE32__) + +#include #ifdef __MINGW32__ -# define dirent fl_dirent_no_thanks -#endif -#include -#ifdef __MINGW32__ -# undef dirent +# include "compat.h" +# if FLARQ_FLTK_API_MAJOR == 1 && FLARQ_FLTK_API_MINOR < 3 +# define dirent fl_dirent_no_thanks +# undef dirent +# include +# else +# include +# endif +#else +# include #endif #include "fileselect.h" diff --git a/src/include/Fl_Text_Buffer_mod_1_3.H b/src/include/Fl_Text_Buffer_mod_1_3.H index 98067ff6..b6f29417 100644 --- a/src/include/Fl_Text_Buffer_mod_1_3.H +++ b/src/include/Fl_Text_Buffer_mod_1_3.H @@ -1,9 +1,9 @@ // -// "$Id: Fl_Text_Buffer.H 7462 2010-04-06 23:00:56Z matt $" +// "$Id: Fl_Text_Buffer_mod.H 8148 2010-12-31 22:38:03Z matt $" // -// Header file for Fl_Text_Buffer class. +// Header file for Fl_Text_Buffer_mod class. // -// Copyright 2001-2009 by Bill Spitzak and others. +// Copyright 2001-2010 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under // the LGPL for the FLTK library granted by Mark Edel. // @@ -28,29 +28,39 @@ // /* \file - Fl_Text_Buffer, Fl_Text_Selection widget . */ + Fl_Text_Buffer_mod, Fl_Text_Selection_mod widget . */ + +#ifndef Fl_Text_Buffer_mod_H +#define Fl_Text_Buffer_mod_H + + +#undef ASSERT_UTF8 + +#ifdef ASSERT_UTF8 +# include +# define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0); +# define IS_UTF8_ALIGNED2(a, b) if (b>=0 && blength()) assert(fl_utf8len(a->byte_at(b))>0); +#else +# define IS_UTF8_ALIGNED(a) +# define IS_UTF8_ALIGNED2(a, b) +#endif -#ifndef FL_TEXT_BUFFER_H -#define FL_TEXT_BUFFER_H /* - Suggested UTF-8 terminology for this file: - - ?? "length" is the number of characters in a string - ?? "size" is the number of bytes - ?? "index" is the position in a string in number of characters - ?? "offset" is the position in a strin in bytes (and must be kept on a charater boundary) - (there seems to be no standard in Uncode documents, howevere "length" is commonly - referencing the number of bytes. Maybe "bytes" and "glyphs" would be the most - obvious way to describe sizes?) - "character size" is the size of a UTF-8 character in bytes - "character width" is the width of a Unicode character in pixels - - "column" was orginally defined as a character offset from the left margin. It was - identical to the byte offset. In UTF-8, we have neither a byte offset nor - truly fixed width fonts. Column could be a pixel value multiplied with + "character width" is the width of a Unicode character in pixels + "column" was orginally defined as a character offset from the left margin. + It was identical to the byte offset. In UTF-8, we have neither a byte offset + nor truly fixed width fonts (*). Column could be a pixel value multiplied with an average character width (which is a bearable approximation). + + * in Unicode, there are no fixed width fonts! Even if the ASCII characters may + happen to be all the same width in pixels, chinese charcaters surely are not. + There are plenty of exceptions, like ligatures, that make special handling of + "fixed" character widths a nightmare. I decided to remove all references to + fixed fonts and see "columns" as a multiple of the average width of a + character in the main font. + - Matthias */ @@ -62,12 +72,12 @@ /** - \class Fl_Text_Selection - \brief This is an internal class for Fl_Text_Buffer to manage text selections. + \class Fl_Text_Selection_mod + \brief This is an internal class for Fl_Text_Buffer_mod to manage text selections. This class works correctly with utf-8 strings assuming that the parameters for all calls are on character boundaries. */ -class FL_EXPORT Fl_Text_Selection { +class FL_EXPORT Fl_Text_Selection_mod { friend class Fl_Text_Buffer_mod; public: @@ -79,15 +89,6 @@ public: */ void set(int start, int end); - /** - \brief Set a regtangular selection range. - \param start byte offset to first selected character - \param end byte offset pointing after last selected character - \param rectStart first selected column - \param rectEnd last selected column +1 - */ - void set_rectangular(int start, int end, int rectStart, int rectEnd); - /** \brief Updates a selection afer text was modified. Updates an individual selection for changes in the corresponding text @@ -97,12 +98,6 @@ public: */ void update(int pos, int nDeleted, int nInserted); - /** - \brief Returns true if the selection is rectangular. - \return flag - */ - char rectangular() const { return mRectangular; } - /** \brief Return the byte offset to the first selected character. \return byte offset @@ -115,36 +110,24 @@ public: */ int end() const { return mEnd; } - /** - \brief Return the first column of the rectangular selection. - \return first column of rectangular selection - */ - int rect_start() const { return mRectStart; } - - /** - \brief Return the last column of the rectangular selection + 1. - \return last column of rectangular selection +1 - */ - int rect_end() const { return mRectEnd; } - /** \brief Returns true if any text is selected. \return a non-zero number if any text has been selected, or 0 if no text is selected. */ - char selected() const { return mSelected; } + bool selected() const { return mSelected; } /** \brief Modify the 'selected' flag. \param b new flag */ - void selected(char b) { mSelected = b; } + void selected(bool b) { mSelected = b; } /** Return true if position \p pos with indentation \p dispIndex is in - the Fl_Text_Selection. + the Fl_Text_Selection_mod. */ - int includes(int pos, int lineStartPos, int dispIndex) const; + int includes(int pos) const; /** \brief Return the positions of this selection. @@ -154,41 +137,33 @@ public: */ int position(int* start, int* end) const; - /** - \brief Return the positions of this rectangular selection. - \param start return byte offset to first selected character - \param end return byte offset pointing after last selected character - \param isRect return if the selection is rectangular - \param rectStart return first selected column - \param rectEnd return last selected column +1 - \return true if selected - */ - int position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd) const; - protected: - char mSelected; ///< this flag is set if any text is selected - char mRectangular; ///< this flag is set if the selection is rectangular int mStart; ///< byte offset to the first selected character int mEnd; ///< byte offset to the character after the last selected character - int mRectStart; ///< first selected column (see "column") - int mRectEnd; ///< last selected column +1 (see "column") + bool mSelected; ///< this flag is set if any text is selected }; + typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted, int nRestyled, const char* deletedText, void* cbArg); + typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg); /** - \brief This class manages unicode displayed in one or more Fl_Text_Display widgets. + \brief This class manages unicode displayed in one or more Fl_Text_Display_mod widgets. - The Fl_Text_Buffer class is used by the Fl_Text_Display - and Fl_Text_Editor to manage complex text data and is based upon the + All text in Fl_Text_Buffer_modmust be encoded in UTF-8. All indices used in the + function calls must be aligned to the start of a UTF-8 sequence. All indices + and pointers returned will be aligned. All functions that return a single + character will return that in an unsiged int in UCS-4 encoding. + + The Fl_Text_Buffer_mod class is used by the Fl_Text_Display_mod + and Fl_Text_Editor_mod to manage complex text data and is based upon the excellent NEdit text editor engine - see http://www.nedit.org/. - \todo unicode check */ class FL_EXPORT Fl_Text_Buffer_mod { public: @@ -218,13 +193,13 @@ public: \brief Get a copy of the entire contents of the text buffer. Memory is allocated to contain the returned string, which the caller must free. - \return newly allocated text buffer - must be free'd + \return newly allocated text buffer - must be free'd, text is utf8 */ char* text() const; /** - Replaces the entire contents of the text buffer - \todo unicode check + Replaces the entire contents of the text buffer. + \param text Text must be valid utf8. */ void text(const char* text); @@ -236,24 +211,41 @@ public: When you are done with the text, free it using the free() function. \param start byte offset to first character \param end byte offset after last character in range - \return newly allocated text buffer - must be free'd + \return newly allocated text buffer - must be free'd, text is utf8 */ char* text_range(int start, int end) const; /** Returns the character at the specified position pos in the buffer. Positions start at 0 - \param pos byte offset into buffer + \param pos byte offset into buffer, pos must be at acharacter boundary \return Unicode UCS-4 encoded character */ - unsigned int character(int pos) const; + unsigned int char_at(int pos) const; - /** - Returns the text from the given rectangle. When you are done - with the text, free it using the free() function. - \todo unicode check + /** + Returns the raw byte at the specified position pos in the buffer. + Positions start at 0 + \param pos byte offset into buffer + \return unencoded raw byte */ - char* text_in_rectangle(int start, int end, int rectStart, int rectEnd) const; + char byte_at(int pos) const; + + /** + Convert a byte offset in buffer into a memory address. + \param pos byte offset into buffer + \return byte offset converted to a memory address + */ + const char *address(int pos) const + { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; } + + /** + Convert a byte offset in buffer into a memory address. + \param pos byte offset into buffer + \return byte offset converted to a memory address + */ + char *address(int pos) + { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; } /** Inserts null-terminated string \p text at position \p pos. @@ -264,7 +256,7 @@ public: /** Appends the text string to the end of the buffer. - \todo unicode check + \param t utf-8 encoded and nul terminated text */ void append(const char* t) { insert(length(), t); } @@ -284,16 +276,17 @@ public: void replace(int start, int end, const char *text); /** - Copies text from one buffer to this one; fromBuf may - be the same as this. - \todo unicode check + Copies text from one buffer to this one. + \param fromBuf source text buffer may be the same as this + \param fromStart byte offset into buffer + \param fromEnd byte offset into buffer + \param toPos destination byte offset into buffer */ void copy(Fl_Text_Buffer_mod* fromBuf, int fromStart, int fromEnd, int toPos); /** Undo text modification according to the undo variables or insert text from the undo buffer - \todo unicode check */ int undo(int *cp=0); @@ -307,23 +300,22 @@ public: non-zero on error (strerror() contains reason). 1 indicates open for read failed (no data loaded). 2 indicates error occurred while reading data (data was partially loaded). - \todo unicode check + File can be UTF-8 or CP1252-encoded. + If the input file is not UTF-8-encoded, the Fl_Text_Buffer_mod widget will contain + UTF-8-transcoded data. By default, the message Fl_Text_Buffer_mod::file_encoding_warning_message + will warn the user about this. + \see input_file_was_transcoded and transcoding_warning_action. */ int insertfile(const char *file, int pos, int buflen = 128*1024); /** - Appends the named file to the end of the buffer. Returns 0 on - success, non-zero on error (strerror() contains reason). 1 indicates - open for read failed (no data loaded). 2 indicates error occurred - while reading data (data was partially loaded). - \todo unicode check + Appends the named file to the end of the buffer. See also insertfile(). */ int appendfile(const char *file, int buflen = 128*1024) { return insertfile(file, length(), buflen); } /** - Loads a text file into the buffer - \todo unicode check + Loads a text file into the buffer. See also insertfile(). */ int loadfile(const char *file, int buflen = 128*1024) { select(0, length()); remove_selection(); return appendfile(file, buflen); } @@ -333,81 +325,28 @@ public: on error (strerror() contains reason). 1 indicates open for write failed (no data saved). 2 indicates error occurred while writing data (data was partially saved). - \todo unicode check */ int outputfile(const char *file, int start, int end, int buflen = 128*1024); /** Saves a text file from the current buffer - \todo unicode check */ int savefile(const char *file, int buflen = 128*1024) { return outputfile(file, 0, length(), buflen); } /** - Insert \p s columnwise into buffer starting at displayed character - position \p column on the line beginning at \p startPos. Opens a rectangular - space the width and height of \p s, by moving all text to the right of - \p column right. If \p charsInserted and \p charsDeleted are not NULL, the - number of characters inserted and deleted in the operation (beginning - at \p startPos) are returned in these arguments. - \todo unicode check - */ - void insert_column(int column, int startPos, const char* text, - int* charsInserted, int* charsDeleted); - - /** - Replaces a rectangular area in the buffer, given by \p start, \p end, - \p rectStart, and \p rectEnd, with \p text. If \p text is vertically - longer than the rectangle, add extra lines to make room for it. - \todo unicode check - */ - void replace_rectangular(int start, int end, int rectStart, int rectEnd, - const char* text); - - /** - Overlay \p text between displayed character positions \p rectStart and - \p rectEnd on the line beginning at \p startPos. If \p charsInserted and - \p charsDeleted are not NULL, the number of characters inserted and deleted - in the operation (beginning at \p startPos) are returned in these arguments. - \todo unicode check - */ - void overlay_rectangular(int startPos, int rectStart, int rectEnd, - const char* text, int* charsInserted, - int* charsDeleted); - - /** - Removes a rectangular swath of characters between character positions start - and end and horizontal displayed-character offsets rectStart and rectEnd. - \todo unicode check - */ - void remove_rectangular(int start, int end, int rectStart, int rectEnd); - - /** - Clears text in the specified area. - It clears a rectangular "hole" out of the buffer between character positions - start and end and horizontal displayed-character offsets rectStart and - rectEnd. - \todo unicode check - */ - void clear_rectangular(int start, int end, int rectStart, int rectEnd); - - /** Gets the tab width. - \todo unicode check */ int tab_distance() const { return mTabDist; } /** Set the hardware tab distance (width) used by all displays for this buffer, and used in computing offsets for rectangular selection operations. - \todo unicode check */ void tab_distance(int tabDist); /** Selects a range of characters in the buffer. - \todo unicode check */ void select(int start, int end); @@ -418,51 +357,32 @@ public: /** Cancels any previous selection on the primary text selection object - \todo unicode check */ void unselect(); - /** - Achieves a rectangular selection on the primary text selection object - \todo unicode check - */ - void select_rectangular(int start, int end, int rectStart, int rectEnd); - /** Gets the selection position - \todo unicode check */ int selection_position(int* start, int* end); /** - Gets the selection position, and rectangular selection info - \todo unicode check - */ - int selection_position(int* start, int* end, int* isRect, int* rectStart, - int* rectEnd); - - /** Returns the currently selected text. When you are done with the text, free it using the free() function. - \todo unicode check */ char* selection_text(); /** Removes the text in the primary selection. - \todo unicode check */ void remove_selection(); /** Replaces the text in the primary selection. - \todo unicode check */ void replace_selection(const char* text); /** Selects a range of characters in the secondary selection. - \todo unicode check */ void secondary_select(int start, int end); @@ -474,53 +394,33 @@ public: /** Clears any selection in the secondary text selection object. - \todo unicode check */ void secondary_unselect(); /** - Achieves a rectangular selection on the secondary text selection object - \todo unicode check - */ - void secondary_select_rectangular(int start, int end, int rectStart, - int rectEnd); - - /** Returns the current selection in the secondary text selection object. - \todo unicode check */ int secondary_selection_position(int* start, int* end); /** - Returns the current selection in the secondary text selection object. - \todo unicode check - */ - int secondary_selection_position(int* start, int* end, int* isRect, - int* rectStart, int* rectEnd); - - /** Returns the text in the secondary selection. When you are done with the text, free it using the free() function. - \todo unicode check */ char* secondary_selection_text(); /** Removes the text from the buffer corresponding to the secondary text selection object. - \todo unicode check */ void remove_secondary_selection(); /** Replaces the text from the buffer corresponding to the secondary text selection object with the new string \p text. - \todo unicode check */ void replace_secondary_selection(const char* text); /** Highlights the specified text within the buffer. - \todo unicode check */ void highlight(int start, int end); @@ -532,33 +432,17 @@ public: /** Unhighlights text in the buffer. - \todo unicode check */ void unhighlight(); - /** - Highlights a rectangular selection in the buffer - \todo unicode check - */ - void highlight_rectangular(int start, int end, int rectStart, int rectEnd); - /** Highlights the specified text between \p start and \p end within the buffer. - \todo unicode check */ int highlight_position(int* start, int* end); /** - Highlights the specified rectangle of text within the buffer. - \todo unicode check - */ - int highlight_position(int* start, int* end, int* isRect, int* rectStart, - int* rectEnd); - - /** Returns the highlighted text. When you are done with the text, free it using the free() function. - \todo unicode check */ char* highlight_text(); @@ -571,13 +455,11 @@ public: int nRestyled, const char* deletedText, void* cbArg); \endcode - \todo unicode check */ void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg); /** Removes a modify callback. - \todo unicode check */ void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg); @@ -585,27 +467,23 @@ public: Calls all modify callbacks that have been registered using the add_modify_callback() method. - \todo unicode check */ void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); } /** Adds a callback routine to be called before text is deleted from the buffer. - \todo unicode check */ void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg); /** Removes a callback routine \p bufPreDeleteCB associated with argument \p cbArg to be called before text is deleted from the buffer. - \todo unicode check */ void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg); /** Calls the stored pre-delete callback procedure(s) for this buffer to update the changed area(s) on the screen and any other listeners. - \todo unicode check */ void call_predelete_callbacks() { call_predelete_callbacks(0, 0); } @@ -613,13 +491,15 @@ public: Returns the text from the entire line containing the specified character position. When you are done with the text, free it using the free() function. - \todo unicode check + \param pos byte index into buffer + \return copy of utf8 text, must be free'd */ char* line_text(int pos) const; /** Returns the position of the start of the line containing position \p pos. - \todo unicode check + \param pos byte index into buffer + \return byte offset to line start */ int line_start(int pos) const; @@ -627,67 +507,30 @@ public: Finds and returns the position of the end of the line containing position \p pos (which is either a pointer to the newline character ending the line, or a pointer to one character beyond the end of the buffer) - \todo unicode check + \param pos byte index into buffer + \return byte offset to line end */ int line_end(int pos) const; /** Returns the position corresponding to the start of the word - \todo unicode check + \param pos byte index into buffer + \return byte offset to word start */ int word_start(int pos) const; /** Returns the position corresponding to the end of the word. - \todo unicode check + \param pos byte index into buffer + \return byte offset to word end */ int word_end(int pos) const; - /** - Expands the given character to a displayable format. Tabs and - other control characters are given special treatment. - Get a character from the text buffer expanded into its screen - representation (which may be several characters for a tab or a - control code). Returns the number of characters written to \p outStr. - \p indent is the number of characters from the start of the line - for figuring tabs. Output string is guranteed to be shorter or - equal in length to FL_TEXT_MAX_EXP_CHAR_LEN - \todo unicode check - */ - int expand_character(int pos, int indent, char *outStr) const; - - /** - Expand a single character \p c from the text buffer into it's displayable - screen representation (which may be several characters for a tab or a - control code). Returns the number of characters added to \p outStr. - \p indent is the number of characters from the start of the line - for figuring tabs of length \p tabDist. Output string is guaranteed - to be shorter or equal in length to FL_TEXT_MAX_EXP_CHAR_LEN - Tabs and other control characters are given special treatment. - \param src address of utf-8 text - \param indent - \param[out] outStr write substitution here - \param tabDist - \return number of byte in substitution - */ - static int expand_character(const char *src, int indent, char* outStr, int tabDist); - - /** - Return the length in displayed characters of character \p c expanded - for display (as discussed above in expand_character() ). - \param src address of utf-8 text - \param indent - \param tabDist - \return number of byte in substitution - */ - static int character_width(const char *src, int indent, int tabDist); - /** Count the number of displayed characters between buffer position \p lineStartPos and \p targetPos. (displayed characters are the characters shown on the screen to represent characters in the buffer, where tabs and control characters are expanded) - \todo unicode check */ int count_displayed_characters(int lineStartPos, int targetPos) const; @@ -698,21 +541,18 @@ public: \param lineStartPos byte offset into buffer \param nChars number of bytes that are sent to the display \return byte offset in input after all output bytes are sent - \todo unicode check */ int skip_displayed_characters(int lineStartPos, int nChars); /** Counts the number of newlines between \p startPos and \p endPos in buffer. The character at position \p endPos is not counted. - \todo unicode check */ int count_lines(int startPos, int endPos) const; /** Finds the first character of the line \p nLines forward from \p startPos in the buffer and returns its position - \todo unicode check */ int skip_lines(int startPos, int nLines); @@ -720,7 +560,6 @@ public: Finds and returns the position of the first character of the line \p nLines backwards from \p startPos (not counting the character pointed to by \p startpos if that is a newline) in the buffer. \p nLines == 0 means find the beginning of the line - \todo unicode check */ int rewind_lines(int startPos, int nLines); @@ -732,9 +571,12 @@ public: BufSearchForward is that it's optimized for single characters. The overall performance of the text widget is dependent on its ability to count lines quickly, hence searching for a single character: newline) - \todo unicode check + \param startPos byte offset to start position + \param searchChar UCS-4 character that we want to find + \param foundPos byte offset where the character was found + \return 1 if found, 0 if not */ - int findchar_forward(int startPos, char searchChar, int* foundPos) const; + int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const; /** Search backwards in buffer \p buf for character \p searchChar, starting @@ -743,33 +585,22 @@ public: BufSearchBackward is that it's optimized for single characters. The overall performance of the text widget is dependent on its ability to count lines quickly, hence searching for a single character: newline) - \todo unicode check + \param startPos byte offset to start position + \param searchChar UCS-4 character that we want to find + \param foundPos byte offset where the character was found + \return 1 if found, 0 if not */ - int findchar_backward(int startPos, char searchChar, int* foundPos) const; - - /** - Finds the next occurrence of the specified characters. - Search forwards in buffer for characters in \p searchChars, starting - with the character \p startPos, and returning the result in \p foundPos - returns 1 if found, 0 if not. - \todo unicode check - */ - int findchars_forward(int startPos, const char* searchChars, int* foundPos) const; - - /** - Finds the previous occurrence of the specified characters. - Search backwards in buffer for characters in \p searchChars, starting - with the character BEFORE \p startPos, returning the result in \p foundPos - returns 1 if found, 0 if not. - \todo unicode check - */ - int findchars_backward(int startPos, const char* searchChars, int* foundPos) const; + int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const; /** Search forwards in buffer for string \p searchString, starting with the character \p startPos, and returning the result in \p foundPos returns 1 if found, 0 if not. - \todo unicode check + \param startPos byte offset to start position + \param searchString utf8 string that we want to find + \param foundPos byte offset where the string was found + \param matchCase if set, match character case + \return 1 if found, 0 if not */ int search_forward(int startPos, const char* searchString, int* foundPos, int matchCase = 0) const; @@ -778,7 +609,11 @@ public: Search backwards in buffer for string searchCharssearchString, starting with the character BEFORE \p startPos, returning the result in \p foundPos returns 1 if found, 0 if not. - \todo unicode check + \param startPos byte offset to start position + \param searchString utf8 string that we want to find + \param foundPos byte offset where the string was found + \param matchCase if set, match character case + \return 1 if found, 0 if not */ int search_backward(int startPos, const char* searchString, int* foundPos, int matchCase = 0) const; @@ -786,29 +621,68 @@ public: /** Returns the primary selection. */ - const Fl_Text_Selection* primary_selection() const { return &mPrimary; } + const Fl_Text_Selection_mod* primary_selection() const { return &mPrimary; } /** Returns the primary selection. */ - Fl_Text_Selection* primary_selection() { return &mPrimary; } + Fl_Text_Selection_mod* primary_selection() { return &mPrimary; } /** Returns the secondary selection. */ - const Fl_Text_Selection* secondary_selection() const { return &mSecondary; } + const Fl_Text_Selection_mod* secondary_selection() const { return &mSecondary; } /** Returns the current highlight selection. */ - const Fl_Text_Selection* highlight_selection() const { return &mHighlight; } + const Fl_Text_Selection_mod* highlight_selection() const { return &mHighlight; } + + /** + Returns the index of the previous character. + \param ix index to the current char + */ + int prev_char(int ix) const; + int prev_char_clipped(int ix) const; + + /** + Returns the index of the next character. + \param ix index to the current char + */ + int next_char(int ix) const; + int next_char_clipped(int ix) const; + + /** + Align an index into the buffer to the current or previous utf8 boundary. + */ + int utf8_align(int) const; + + /** + \brief true iff the loaded file has been transcoded to UTF-8 + */ + int input_file_was_transcoded; + + /** This message may be displayed using the fl_alert() function when a file + which was not UTF-8 encoded is input. + */ + static const char* file_encoding_warning_message; + + /** + \brief Pointer to a function called after reading a non UTF-8 encoded file. + + This function is called after reading a file if the file content + was transcoded to UTF-8. Its default implementation calls fl_alert() + with the text of \ref file_encoding_warning_message. No warning message is + displayed if this pointer is set to NULL. Use \ref input_file_was_transcoded + to be informed if file input required transcoding to UTF-8. + */ + void (*transcoding_warning_action)(Fl_Text_Buffer_mod*); protected: /** Calls the stored modify callback procedure(s) for this buffer to update the changed area(s) on the screen and any other listeners. - \todo unicode check */ void call_modify_callbacks(int pos, int nDeleted, int nInserted, int nRestyled, const char* deletedText) const; @@ -816,7 +690,6 @@ protected: /** Calls the stored pre-delete callback procedure(s) for this buffer to update the changed area(s) on the screen and any other listeners. - \todo unicode check */ void call_predelete_callbacks(int pos, int nDeleted) const; @@ -826,7 +699,7 @@ protected: expensive and the length will be required by any caller who will continue on to call redisplay). \p pos must be contiguous with the existing text in the buffer (i.e. not past the end). - \todo unicode check + \return the number of bytes inserted */ int insert_(int pos, const char* text); @@ -834,121 +707,47 @@ protected: Internal (non-redisplaying) version of BufRemove. Removes the contents of the buffer between start and end (and moves the gap to the site of the delete). - \todo unicode check */ void remove_(int start, int end); - /** - Deletes a rectangle of text without calling the modify callbacks. Returns - the number of characters replacing those between \p start and \p end. Note that - in some pathological cases, deleting can actually increase the size of - the buffer because of tab expansions. \p endPos returns the buffer position - of the point in the last line where the text was removed (as a hint for - routines which need to position the cursor after a delete operation) - \todo unicode check - */ - void remove_rectangular_(int start, int end, int rectStart, int rectEnd, - int* replaceLen, int* endPos); - - /** - Inserts a column of text without calling the modify callbacks. Note that - in some pathological cases, inserting can actually decrease the size of - the buffer because of spaces being coalesced into tabs. \p nDeleted and - \p nInserted return the number of characters deleted and inserted beginning - at the start of the line containing \p startPos. \p endPos returns buffer - position of the lower left edge of the inserted column (as a hint for - routines which need to set a cursor position). - \todo unicode check - */ - void insert_column_(int column, int startPos, const char* insText, - int* nDeleted, int* nInserted, int* endPos); - - /** - Overlay a rectangular area of text without calling the modify callbacks. - \p nDeleted and \p nInserted return the number of characters deleted and - inserted beginning at the start of the line containing \p startPos. - \p endPos returns buffer position of the lower left edge of the inserted - column (as a hint for routines which need to set a cursor position). - \todo unicode check - */ - void overlay_rectangular_(int startPos, int rectStart, int rectEnd, - const char* insText, int* nDeleted, - int* nInserted, int* endPos); - /** Calls the stored redisplay procedure(s) for this buffer to update the screen for a change in a selection. - \todo unicode check */ - void redisplay_selection(Fl_Text_Selection* oldSelection, - Fl_Text_Selection* newSelection) const; + void redisplay_selection(Fl_Text_Selection_mod* oldSelection, + Fl_Text_Selection_mod* newSelection) const; /** - \todo unicode check + Move the gap to start at a new position. */ void move_gap(int pos); /** Reallocates the text storage in the buffer to have a gap starting at \p newGapStart and a gap size of \p newGapLen, preserving the buffer's current contents. - \todo unicode check */ void reallocate_with_gap(int newGapStart, int newGapLen); - char* selection_text_(Fl_Text_Selection* sel) const; + char* selection_text_(Fl_Text_Selection_mod* sel) const; /** Removes the text from the buffer corresponding to \p sel. - \todo unicode check */ - void remove_selection_(Fl_Text_Selection* sel); + void remove_selection_(Fl_Text_Selection_mod* sel); /** Replaces the \p text in selection \p sel. - \todo unicode check */ - void replace_selection_(Fl_Text_Selection* sel, const char* text); - - /** - Finds the first and last character position in a line within a rectangular - selection (for copying). Includes tabs which cross rectStart, but not - control characters which do so. Leaves off tabs which cross rectEnd. - - Technically, the calling routine should convert tab characters which - cross the right boundary of the selection to spaces which line up with - the edge of the selection. Unfortunately, the additional memory - management required in the parent routine to allow for the changes - in string size is not worth all the extra work just for a couple of - shifted characters, so if a tab protrudes, just lop it off and hope - that there are other characters in the selection to establish the right - margin for subsequent columnar pastes of this data. - \todo unicode check - */ - void rectangular_selection_boundaries(int lineStartPos, int rectStart, - int rectEnd, int* selStart, - int* selEnd) const; + void replace_selection_(Fl_Text_Selection_mod* sel, const char* text); /** Updates all of the selections in the buffer for changes in the buffer's text - \todo unicode check */ void update_selections(int pos, int nDeleted, int nInserted); - /** - Convert a byte offset in buffer into a memory address. - */ - const char *address(int pos) const - { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; } - - /** - Convert a byte offset in buffer into a memory address. - */ - char *address(int pos) - { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; } - - Fl_Text_Selection mPrimary; /**< highlighted areas */ - Fl_Text_Selection mSecondary; /**< highlighted areas */ - Fl_Text_Selection mHighlight; /**< highlighted areas */ + Fl_Text_Selection_mod mPrimary; /**< highlighted areas */ + Fl_Text_Selection_mod mSecondary; /**< highlighted areas */ + Fl_Text_Selection_mod mHighlight; /**< highlighted areas */ int mLength; /**< length of the text in the buffer (the length of the buffer itself must be calculated: gapEnd - gapStart + length) */ @@ -958,15 +757,13 @@ protected: // The hardware tab distance used by all displays for this buffer, // and used in computing offsets for rectangular selection operations. int mTabDist; /**< equiv. number of characters in a tab */ - int mUseTabs; /**< True if buffer routines are allowed to use - tabs for padding in rectangular operations */ int mNModifyProcs; /**< number of modify-redisplay procs attached */ - Fl_Text_Modify_Cb* /**< procedures to call when buffer is */ - mModifyProcs; /**< modified to redisplay contents */ + Fl_Text_Modify_Cb *mModifyProcs;/**< procedures to call when buffer is + modified to redisplay contents */ void** mCbArgs; /**< caller arguments for modifyProcs above */ int mNPredeleteProcs; /**< number of pre-delete procs attached */ - Fl_Text_Predelete_Cb* /**< procedure to call before text is deleted */ - mPredeleteProcs; /**< from the buffer; at most one is supported. */ + Fl_Text_Predelete_Cb *mPredeleteProcs; /**< procedure to call before text is deleted + from the buffer; at most one is supported. */ void **mPredeleteCbArgs; /**< caller argument for pre-delete proc above */ int mCursorPosHint; /**< hint for reasonable cursor position after a buffer modification operation */ @@ -980,5 +777,5 @@ protected: #endif // -// End of "$Id: Fl_Text_Buffer.H 7462 2010-04-06 23:00:56Z matt $". +// End of "$Id: Fl_Text_Buffer_mod.H 8148 2010-12-31 22:38:03Z matt $". // diff --git a/src/include/Fl_Text_Display_mod_1_3.H b/src/include/Fl_Text_Display_mod_1_3.H index fc2368c4..920455f6 100644 --- a/src/include/Fl_Text_Display_mod_1_3.H +++ b/src/include/Fl_Text_Display_mod_1_3.H @@ -1,9 +1,9 @@ // -// "$Id: Fl_Text_Display.H 6902 2009-09-27 11:06:56Z matt $" +// "$Id: Fl_Text_Display_mod.H 8306 2011-01-24 17:04:22Z matt $" // -// Header file for Fl_Text_Display class. +// Header file for Fl_Text_Display_mod class. // -// Copyright 2001-2009 by Bill Spitzak and others. +// Copyright 2001-2010 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under // the LGPL for the FLTK library granted by Mark Edel. // @@ -28,10 +28,12 @@ // /* \file - Fl_Text_Display widget . */ + Fl_Text_Display_mod widget . */ -#ifndef FL_TEXT_DISPLAY_H -#define FL_TEXT_DISPLAY_H +#ifndef Fl_Text_Display_mod_H +#define Fl_Text_Display_mod_H + +#include #include #include @@ -40,311 +42,451 @@ #include "Fl_Text_Buffer_mod.H" /** - This is the FLTK text display widget. It allows the user to - view multiple lines of text and supports highlighting and - scrolling. The buffer that is displayed in the widget is managed - by the Fl_Text_Buffer - class. -*/ + \brief Rich text display widget. + + This is the FLTK text display widget. It allows the user to view multiple lines + of text and supports highlighting and scrolling. The buffer that is displayed + in the widget is managed by the Fl_Text_Buffer_mod class. A single Text Buffer + can be displayed by multiple Text Displays. + */ class FL_EXPORT Fl_Text_Display_mod: public Fl_Group { - public: - /** text display cursor shapes enumeration */ - enum { - NORMAL_CURSOR, CARET_CURSOR, DIM_CURSOR, - BLOCK_CURSOR, HEAVY_CURSOR - }; - enum { - CURSOR_POS, CHARACTER_POS - }; - - /** drag types- they match Fl::event_clicks() so that single clicking to - start a collection selects by character, double clicking selects by - word and triple clicking selects by line. - */ - enum { - DRAG_CHAR = 0, DRAG_WORD = 1, DRAG_LINE = 2 - }; - friend void fl_text_drag_me(int pos, Fl_Text_Display_mod* d); - - typedef void (*Unfinished_Style_Cb)(int, void *); - - /** style attributes - currently not implemented! */ - enum { - ATTR_NONE = 0, - ATTR_UNDERLINE = 1, - ATTR_HIDDEN = 2 - }; - /** This structure associates the color,font,size of a string to draw - with an attribute mask matching attr */ - struct Style_Table_Entry { - Fl_Color color; - Fl_Font font; - int size; - unsigned attr; - }; - - Fl_Text_Display_mod(int X, int Y, int W, int H, const char *l = 0); - ~Fl_Text_Display_mod(); - - virtual int handle(int e); - void buffer(Fl_Text_Buffer_mod* buf); +public: + + /** + text display cursor shapes enumeration + */ + enum { + NORMAL_CURSOR, /**< I-beam */ + CARET_CURSOR, /**< caret under the text */ + DIM_CURSOR, /**< dim I-beam */ + BLOCK_CURSOR, /**< unfille box under the current character */ + HEAVY_CURSOR /**< thick I-beam */ + }; + /** - Sets or gets the current text buffer associated with the text widget. - Multiple text widgets can be associated with the same text buffer. - */ - void buffer(Fl_Text_Buffer_mod& buf) { buffer(&buf); } - /** - Gets the current text buffer associated with the text widget. - Multiple text widgets can be associated with the same text buffer. - */ - Fl_Text_Buffer_mod* buffer() const { return mBuffer; } - void redisplay_range(int start, int end); - void scroll(int topLineNum, int horizOffset); - void insert(const char* text); - void overstrike(const char* text); - void insert_position(int newPos); - /** Gets the position of the text insertion cursor for text display */ - int insert_position() const { return mCursorPos; } - int in_selection(int x, int y) const; - void show_insert_position(); - int move_right(); - int move_left(); - int move_up(); - int move_down(); - int count_lines(int start, int end, bool start_pos_is_line_start) const; - int line_start(int pos) const; - int line_end(int pos, bool start_pos_is_line_start) const; - int skip_lines(int startPos, int nLines, bool startPosIsLineStart); - int rewind_lines(int startPos, int nLines); - void next_word(void); - void previous_word(void); - void show_cursor(int b = 1); - /** Hides the text cursor */ - void hide_cursor() { show_cursor(0); } - void cursor_style(int style); - /** Sets or gets the text cursor color. */ - Fl_Color cursor_color() const {return mCursor_color;} - /** Sets or gets the text cursor color. */ - void cursor_color(Fl_Color n) {mCursor_color = n;} - /** Sets or gets the width/height of the scrollbars. */ - int scrollbar_width() const { return scrollbar_width_; } - /** Sets or gets the width/height of the scrollbars. */ - void scrollbar_width(int W) { scrollbar_width_ = W; } - /** Gets the scrollbar alignment type */ - Fl_Align scrollbar_align() const { return scrollbar_align_; } - /** Sets the scrollbar alignment type */ - void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; } - /** Moves the insert position to the beginning of the current word. */ - int word_start(int pos) const { return buffer()->word_start(pos); } - /** Moves the insert position to the end of the current word. */ - int word_end(int pos) const { return buffer()->word_end(pos); } + the character position is the left edge of a character, whereas + the cursor is thought to be between the centers of two consecutive + characters. + */ + enum { + CURSOR_POS, + CHARACTER_POS + }; + + /** + drag types - they match Fl::event_clicks() so that single clicking to + start a collection selects by character, double clicking selects by + word and triple clicking selects by line. + */ + enum { + DRAG_NONE = -2, + DRAG_START_DND = -1, + DRAG_CHAR = 0, + DRAG_WORD = 1, + DRAG_LINE = 2 + }; + + /** + wrap types - used in wrap_mode() + */ + enum { + WRAP_NONE, /**< don't wrap text at all */ + WRAP_AT_COLUMN, /**< wrap text at the given text column */ + WRAP_AT_PIXEL, /**< wrap text at a pixel position */ + WRAP_AT_BOUNDS /**< wrap text so that it fits into the widget width */ + }; + + friend void fl_text_drag_me(int pos, Fl_Text_Display_mod* d); + + typedef void (*Unfinished_Style_Cb)(int, void *); + + /** + This structure associates the color, font, andsize of a string to draw + with an attribute mask matching attr + */ + struct Style_Table_Entry { + Fl_Color color; + Fl_Font font; + Fl_Fontsize size; + unsigned attr; + }; + + Fl_Text_Display_mod(int X, int Y, int W, int H, const char *l = 0); + ~Fl_Text_Display_mod(); + + virtual int handle(int e); + + void buffer(Fl_Text_Buffer_mod* buf); + + /** + Sets the current text buffer associated with the text widget. + Multiple text widgets can be associated with the same text buffer. + \param buf new text buffer + */ + void buffer(Fl_Text_Buffer_mod& buf) { buffer(&buf); } + + /** + Gets the current text buffer associated with the text widget. + Multiple text widgets can be associated with the same text buffer. + \return current text buffer + */ + Fl_Text_Buffer_mod* buffer() const { return mBuffer; } + + void redisplay_range(int start, int end); + void scroll(int topLineNum, int horizOffset); + void insert(const char* text); + void overstrike(const char* text); + void insert_position(int newPos); + + /** + Gets the position of the text insertion cursor for text display. + \return insert position index into text buffer + */ + int insert_position() const { return mCursorPos; } + int position_to_xy(int pos, int* x, int* y) const; - - void highlight_data(Fl_Text_Buffer_mod *styleBuffer, - const Style_Table_Entry *styleTable, - int nStyles, char unfinishedStyle, - Unfinished_Style_Cb unfinishedHighlightCB, - void *cbArg); + int in_selection(int x, int y) const; + void show_insert_position(); + + int move_right(); + int move_left(); + int move_up(); + int move_down(); + int count_lines(int start, int end, bool start_pos_is_line_start) const; + int line_start(int pos) const; + int line_end(int startPos, bool startPosIsLineStart) const; + int skip_lines(int startPos, int nLines, bool startPosIsLineStart); + int rewind_lines(int startPos, int nLines); + void next_word(void); + void previous_word(void); + + void show_cursor(int b = 1); + + /** + Hides the text cursor. + */ + void hide_cursor() { show_cursor(0); } + + void cursor_style(int style); + + /** + Gets the text cursor color. + \return cursor color + */ + Fl_Color cursor_color() const {return mCursor_color;} + + /** + Sets the text cursor color. + \param n new cursor color + */ + void cursor_color(Fl_Color n) {mCursor_color = n;} + + /** + Gets the width/height of the scrollbars. + /return width of scrollbars + */ + int scrollbar_width() const { return scrollbar_width_; } + + /** + Sets the width/height of the scrollbars. + \param W width of scrollbars + */ + void scrollbar_width(int W) { scrollbar_width_ = W; } + + /** + Gets the scrollbar alignment type. + \return scrollbar alignment + */ + Fl_Align scrollbar_align() const { return scrollbar_align_; } + + /** + Sets the scrollbar alignment type. + \param a new scrollbar alignment + */ + void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; } + + /** + Moves the insert position to the beginning of the current word. + \param pos start calculation at this index + \return beginning of the words + */ + int word_start(int pos) const { return buffer()->word_start(pos); } + + /** + Moves the insert position to the end of the current word. + \param pos start calculation at this index + \return index of first character after the end of the word + */ + int word_end(int pos) const { return buffer()->word_end(pos); } + + + void highlight_data(Fl_Text_Buffer_mod *styleBuffer, + const Style_Table_Entry *styleTable, + int nStyles, char unfinishedStyle, + Unfinished_Style_Cb unfinishedHighlightCB, + void *cbArg); + + int position_style(int lineStartPos, int lineLen, int lineIndex) const; + + /** + \todo FIXME : get set methods pointing on shortcut_ + have no effects as shortcut_ is unused in this class and derived! + \return the current shortcut key + */ + int shortcut() const {return shortcut_;} + + /** + \todo FIXME : get set methods pointing on shortcut_ + have no effects as shortcut_ is unused in this class and derived! + \param s the new shortcut key + */ + void shortcut(int s) {shortcut_ = s;} + + /** + Gets the default font used when drawing text in the widget. + \return current text font face unless overridden by a style + */ + Fl_Font textfont() const {return textfont_;} + + /** + Sets the default font used when drawing text in the widget. + \param s default text font face + */ + void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;} + + /** + Gets the default size of text in the widget. + \return current text height unless overridden by a style + */ + Fl_Fontsize textsize() const {return textsize_;} + + /** + Sets the default size of text in the widget. + \param s new text size + */ + void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;} + + /** + Gets the default color of text in the widget. + \return text color unless overridden by a style + */ + Fl_Color textcolor() const {return textcolor_;} + + /** + Sets the default color of text in the widget. + \param n new text color + */ + void textcolor(Fl_Color n) {textcolor_ = n;} + + int wrapped_column(int row, int column) const; + int wrapped_row(int row) const; + void wrap_mode(int wrap, int wrap_margin); + + virtual void resize(int X, int Y, int W, int H); - int position_style(int lineStartPos, int lineLen, int lineIndex, - int dispIndex) const; - /** \todo FIXME : get set methods pointing on shortcut_ - have no effects as shortcut_ is unused in this class and derived! */ - int shortcut() const {return shortcut_;} - /** \todo FIXME : get set methods pointing on shortcut_ - have no effects as shortcut_ is unused in this class and derived! */ - void shortcut(int s) {shortcut_ = s;} + /** + Convert an x pixel position into a column number. + \param x number of pixels from the left margin + \return an approximate column number based on the main font + */ + double x_to_col(double x) const; + + /** + Convert a column number into an x pixel position. + \param col an approximate column number based on the main font + \return number of pixels from the left margin to the left of an + average sized character + */ + double col_to_x(double col) const; + +protected: + // Most (all?) of this stuff should only be called from resize() or + // draw(). + // Anything with "vline" indicates thats it deals with currently + // visible lines. + + virtual void draw(); + void draw_text(int X, int Y, int W, int H); + void draw_range(int start, int end); + void draw_cursor(int, int); + + void draw_string(int style, int x, int y, int toX, const char *string, + int nChars) const; + + void draw_vline(int visLineNum, int leftClip, int rightClip, + int leftCharIndex, int rightCharIndex); + + int find_x(const char *s, int len, int style, int x) const; + + enum { + DRAW_LINE, + FIND_INDEX, + FIND_INDEX_FROM_ZERO, + GET_WIDTH + }; + + int handle_vline(int mode, + int lineStart, int lineLen, int leftChar, int rightChar, + int topClip, int bottomClip, + int leftClip, int rightClip) const; + + void draw_line_numbers(bool clearAll); + + void clear_rect(int style, int x, int y, int width, int height) const; + void display_insert(); + + void offset_line_starts(int newTopLineNum); + + void calc_line_starts(int startLine, int endLine); + + void update_line_starts(int pos, int charsInserted, int charsDeleted, + int linesInserted, int linesDeleted, int *scrolled); + + void calc_last_char(); + + int position_to_line( int pos, int* lineNum ) const; + double string_width(const char* string, int length, int style) const; + + static void scroll_timer_cb(void*); + + static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg); + static void buffer_modified_cb(int pos, int nInserted, int nDeleted, + int nRestyled, const char* deletedText, + void* cbArg); + + static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display_mod* d); + static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display_mod* d); + void update_v_scrollbar(); + void update_h_scrollbar(); + int measure_vline(int visLineNum) const; + int longest_vline() const; + int empty_vlines() const; + int vline_length(int visLineNum) const; + int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const; + + void xy_to_rowcol(int x, int y, int* row, int* column, + int PosType = CHARACTER_POS) const; + void maintain_absolute_top_line_number(int state); + int get_absolute_top_line_number() const; + void absolute_top_line_number(int oldFirstChar); + int maintaining_absolute_top_line_number() const; + void reset_absolute_top_line_number(); + int position_to_linecol(int pos, int* lineNum, int* column) const; + int scroll_(int topLineNum, int horizOffset); + + void extend_range_for_styles(int* start, int* end); + + void find_wrap_range(const char *deletedText, int pos, int nInserted, + int nDeleted, int *modRangeStart, int *modRangeEnd, + int *linesInserted, int *linesDeleted); + void measure_deleted_lines(int pos, int nDeleted); + void wrapped_line_counter(Fl_Text_Buffer_mod *buf, int startPos, int maxPos, + int maxLines, bool startPosIsLineStart, + int styleBufOffset, int *retPos, int *retLines, + int *retLineStart, int *retLineEnd, + bool countLastLineMissingNewLine = true) const; + void find_line_end(int pos, bool start_pos_is_text_start, int *lineEnd, + int *nextLineStart) const; + double measure_proportional_character(const char *s, int colNum, int pos) const; + int wrap_uses_character(int lineEndPos) const; + + static const int DEFAULT_TOP_MARGIN; + static const int DEFAULT_BOTTOM_MARGIN; + static const int DEFAULT_LEFT_MARGIN; + static const int DEFAULT_RIGHT_MARGIN; + int TOP_MARGIN, BOTTOM_MARGIN, LEFT_MARGIN, RIGHT_MARGIN; - /** Gets the default font used when drawing text in the widget. */ - Fl_Font textfont() const {return textfont_;} - /** Sets the default font used when drawing text in the widget. */ - void textfont(Fl_Font s) {textfont_ = s;} - /** Gets the default size of text in the widget. */ - Fl_Fontsize textsize() const {return textsize_;} - /** Sets the default size of text in the widget. */ - void textsize(Fl_Fontsize s) {textsize_ = s;} - /** Gets the default color of text in the widget. */ - Fl_Color textcolor() const {return textcolor_;} - /** Sets the default color of text in the widget. */ - void textcolor(Fl_Color n) {textcolor_ = n;} - - int wrapped_column(int row, int column) const; - int wrapped_row(int row) const; - void wrap_mode(int wrap, int wrap_margin); - - virtual void resize(int X, int Y, int W, int H); - - protected: - // Most (all?) of this stuff should only be called from resize() or - // draw(). - // Anything with "vline" indicates thats it deals with currently - // visible lines. - - virtual void draw(); - void draw_text(int X, int Y, int W, int H); - void draw_range(int start, int end); - void draw_cursor(int, int); - - void draw_string(int style, int x, int y, int toX, const char *string, - int nChars); - - void draw_vline(int visLineNum, int leftClip, int rightClip, - int leftCharIndex, int rightCharIndex); - - void draw_line_numbers(bool clearAll); - - void clear_rect(int style, int x, int y, int width, int height); - void display_insert(); - - void offset_line_starts(int newTopLineNum); - - void calc_line_starts(int startLine, int endLine); - - void update_line_starts(int pos, int charsInserted, int charsDeleted, - int linesInserted, int linesDeleted, int *scrolled); - - void calc_last_char(); - - int position_to_line( size_t pos, int* lineNum ) const; - int string_width(const char* string, int length, int style) const; - - static void scroll_timer_cb(void*); - - static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg); - static void buffer_modified_cb(int pos, int nInserted, int nDeleted, - int nRestyled, const char* deletedText, - void* cbArg); - - static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display_mod* d); - static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display_mod* d); - void update_v_scrollbar(); - void update_h_scrollbar(); - int measure_vline(int visLineNum) const; - int longest_vline() const; - int empty_vlines() const; - int vline_length(int visLineNum) const; - int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const; - - void xy_to_rowcol(int x, int y, int* row, int* column, - int PosType = CHARACTER_POS) const; - - int position_to_xy(int pos, int* x, int* y) const; - void maintain_absolute_top_line_number(int state); - int get_absolute_top_line_number() const; - void absolute_top_line_number(int oldFirstChar); - int maintaining_absolute_top_line_number() const; - void reset_absolute_top_line_number(); - int position_to_linecol(int pos, int* lineNum, int* column) const; - void scroll_(int topLineNum, int horizOffset); - - void extend_range_for_styles(int* start, int* end); - - void find_wrap_range(const char *deletedText, int pos, int nInserted, - int nDeleted, int *modRangeStart, int *modRangeEnd, - int *linesInserted, int *linesDeleted); - void measure_deleted_lines(int pos, int nDeleted); - void wrapped_line_counter(Fl_Text_Buffer_mod *buf, int startPos, int maxPos, - int maxLines, bool startPosIsLineStart, - int styleBufOffset, int *retPos, int *retLines, - int *retLineStart, int *retLineEnd, - bool countLastLineMissingNewLine = true) const; - void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd, - int *nextLineStart) const; - int measure_proportional_character(char c, int colNum, int pos) const; - int wrap_uses_character(int lineEndPos) const; - int range_touches_selection(const Fl_Text_Selection *sel, int rangeStart, - int rangeEnd) const; -#ifndef FL_DOXYGEN - static const int DEFAULT_TOP_MARGIN; - static const int DEFAULT_BOTTOM_MARGIN; - static const int DEFAULT_LEFT_MARGIN; - static const int DEFAULT_RIGHT_MARGIN; - int TOP_MARGIN, BOTTOM_MARGIN, LEFT_MARGIN, RIGHT_MARGIN; - - int damage_range1_start, damage_range1_end; - int damage_range2_start, damage_range2_end; - int mCursorPos; - int mCursorOn; - int mCursorOldY; /* Y pos. of cursor for blanking */ - int mCursorToHint; /* Tells the buffer modified callback - where to move the cursor, to reduce - the number of redraw calls */ - int mCursorStyle; /* One of enum cursorStyles above */ - int mCursorPreferredCol; /* Column for vert. cursor movement */ - int mNVisibleLines; /* # of visible (displayed) lines */ - int mNBufferLines; /* # of newlines in the buffer */ - Fl_Text_Buffer_mod* mBuffer; /* Contains text to be displayed */ - Fl_Text_Buffer_mod* mStyleBuffer; /* Optional parallel buffer containing - color and font information */ - int mFirstChar, mLastChar; /* Buffer positions of first and last - displayed character (lastChar points - either to a newline or one character - beyond the end of the buffer) */ - int mContinuousWrap; /* Wrap long lines when displaying */ - int mWrapMargin; /* Margin in # of char positions for - wrapping in continuousWrap mode */ - int* mLineStarts; - int mTopLineNum; /* Line number of top displayed line - of file (first line of file is 1) */ - int mAbsTopLineNum; /* In continuous wrap mode, the line - number of the top line if the text - were not wrapped (note that this is - only maintained as needed). */ - int mNeedAbsTopLineNum; /* Externally settable flag to continue - maintaining absTopLineNum even if - it isn't needed for line # display */ - int mHorizOffset; /* Horizontal scroll pos. in pixels */ - int mTopLineNumHint; /* Line number of top displayed line - of file (first line of file is 1) */ - int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */ - int mNStyles; /* Number of entries in styleTable */ - const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for - coloring/syntax-highlighting */ - char mUnfinishedStyle; /* Style buffer entry which triggers - on-the-fly reparsing of region */ - Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */ - /* regions */ - void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */ - - int mMaxsize; - - int mFixedFontWidth; /* Font width if all current fonts are - fixed and match in width, else -1 */ - int mSuppressResync; /* Suppress resynchronization of line - starts during buffer updates */ - int mNLinesDeleted; /* Number of lines deleted during - buffer modification (only used - when resynchronization is suppressed) */ - int mModifyingTabDistance; /* Whether tab distance is being - modified */ - - Fl_Color mCursor_color; - - Fl_Scrollbar* mHScrollBar; - Fl_Scrollbar* mVScrollBar; - int scrollbar_width_; - Fl_Align scrollbar_align_; - int dragPos, dragType, dragging; - int display_insert_position_hint; - struct { int x, y, w, h; } text_area; - - int shortcut_; - - Fl_Font textfont_; - Fl_Fontsize textsize_; - Fl_Color textcolor_; - - // The following are not presently used from the original NEdit code, - // but are being put here so that future versions of Fl_Text_Display - // can implement line numbers without breaking binary compatibility. - int mLineNumLeft, mLineNumWidth; - /* Line number margin and width */ -#endif + int damage_range1_start, damage_range1_end; + int damage_range2_start, damage_range2_end; + int mCursorPos; + int mCursorOn; + int mCursorOldY; /* Y pos. of cursor for blanking */ + int mCursorToHint; /* Tells the buffer modified callback + where to move the cursor, to reduce + the number of redraw calls */ + int mCursorStyle; /* One of enum cursorStyles above */ + int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */ + int mNVisibleLines; /* # of visible (displayed) lines */ + int mNBufferLines; /* # of newlines in the buffer */ + Fl_Text_Buffer_mod* mBuffer; /* Contains text to be displayed */ + Fl_Text_Buffer_mod* mStyleBuffer; /* Optional parallel buffer containing + color and font information */ + int mFirstChar, mLastChar; /* Buffer positions of first and last + displayed character (lastChar points + either to a newline or one character + beyond the end of the buffer) */ + int mContinuousWrap; /* Wrap long lines when displaying */ + int mFastDisplay; /* force continuous wrap and insertion of eol characters + used in FTextRX */ + std::string s_text; /* current contents of line text buffer */ + std::string s_style; /* current contents of line stype buffer */ + int mWrapMarginPix; /* Margin in # of pixels for + wrapping in continuousWrap mode */ + int* mLineStarts; + int mTopLineNum; /* Line number of top displayed line + of file (first line of file is 1) */ + int mAbsTopLineNum; /* In continuous wrap mode, the line + number of the top line if the text + were not wrapped (note that this is + only maintained as needed). */ + int mNeedAbsTopLineNum; /* Externally settable flag to continue + maintaining absTopLineNum even if + it isn't needed for line # display */ + int mHorizOffset; /* Horizontal scroll pos. in pixels */ + int mTopLineNumHint; /* Line number of top displayed line + of file (first line of file is 1) */ + int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */ + int mNStyles; /* Number of entries in styleTable */ + const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for + coloring/syntax-highlighting */ + char mUnfinishedStyle; /* Style buffer entry which triggers + on-the-fly reparsing of region */ + Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */ + /* regions */ + void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */ + + int mMaxsize; + + int mSuppressResync; /* Suppress resynchronization of line + starts during buffer updates */ + int mNLinesDeleted; /* Number of lines deleted during + buffer modification (only used + when resynchronization is suppressed) */ + int mModifyingTabDistance; /* Whether tab distance is being + modified */ + + mutable double mColumnScale; /* Width in pixels of an average character. This + value is calculated as needed (lazy eval); it + needs to be mutable so that it can be calculated + within a method marked as "const" */ + + Fl_Color mCursor_color; + + Fl_Scrollbar* mHScrollBar; + Fl_Scrollbar* mVScrollBar; + int scrollbar_width_; + Fl_Align scrollbar_align_; + int dragPos, dragType, dragging; + int display_insert_position_hint; + struct { int x, y, w, h; } text_area; + + int shortcut_; + + Fl_Font textfont_; + Fl_Fontsize textsize_; + Fl_Color textcolor_; + + // The following are not presently used from the original NEdit code, + // but are being put here so that future versions of Fl_Text_Display_mod + // can implement line numbers without breaking binary compatibility. + + /* Line number margin and width */ + int mLineNumLeft, mLineNumWidth; }; #endif // -// End of "$Id: Fl_Text_Display.H 6902 2009-09-27 11:06:56Z matt $". +// End of "$Id: Fl_Text_Display_mod.H 8306 2011-01-24 17:04:22Z matt $". // diff --git a/src/include/Fl_Text_Editor_mod_1_3.H b/src/include/Fl_Text_Editor_mod_1_3.H index 1c2d0c8a..0af645c8 100644 --- a/src/include/Fl_Text_Editor_mod_1_3.H +++ b/src/include/Fl_Text_Editor_mod_1_3.H @@ -1,9 +1,9 @@ // -// "$Id: Fl_Text_Editor.H 6893 2009-09-20 19:24:24Z greg.ercolano $" +// "$Id: Fl_Text_Editor_mod.H 7903 2010-11-28 21:06:39Z matt $" // -// Header file for Fl_Text_Editor class. +// Header file for Fl_Text_Editor_mod class. // -// Copyright 2001-2009 by Bill Spitzak and others. +// Copyright 2001-2010 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under // the LGPL for the FLTK library granted by Mark Edel. // @@ -28,22 +28,22 @@ // /* \file - Fl_Text_Editor widget . */ + Fl_Text_Editor_mod widget . */ -#ifndef FL_TEXT_EDITOR_H -#define FL_TEXT_EDITOR_H +#ifndef Fl_Text_Editor_mod_H +#define Fl_Text_Editor_mod_H #include "Fl_Text_Display_mod.H" // key will match in any state -#define FL_TEXT_EDITOR_ANY_STATE (-1L) +#define Fl_Text_Editor_mod_ANY_STATE (-1L) /** This is the FLTK text editor widget. It allows the user to edit multiple lines of text and supports highlighting and scrolling. The buffer that is displayed in the widget is managed - by the Fl_Text_Buffer + by the Fl_Text_Buffer_mod class. */ class FL_EXPORT Fl_Text_Editor_mod : public Fl_Text_Display_mod { @@ -136,6 +136,6 @@ class FL_EXPORT Fl_Text_Editor_mod : public Fl_Text_Display_mod { #endif // -// End of "$Id: Fl_Text_Editor.H 6893 2009-09-20 19:24:24Z greg.ercolano $". +// End of "$Id: Fl_Text_Editor_mod.H 7903 2010-11-28 21:06:39Z matt $". // diff --git a/src/include/compat.h b/src/include/compat.h index 6b82d55b..43dfeee5 100644 --- a/src/include/compat.h +++ b/src/include/compat.h @@ -25,7 +25,24 @@ #include #include #include -#include + +// this tests depends on a modified FL/filename.H in the Fltk-1.3.0 +// change +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) +// to +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WOE32__) + +#ifdef __MINGW32__ +# if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# undef dirent +# include +# else +# include +# endif +#else +# include +#endif + #include #include #include diff --git a/src/include/fileselect.h b/src/include/fileselect.h index 79992a05..52fa20cb 100644 --- a/src/include/fileselect.h +++ b/src/include/fileselect.h @@ -1,34 +1,18 @@ -// ---------------------------------------------------------------------------- -// -// fileselect.h -// -// Copyright (C) 2008-2009 -// Stelios Bounanos, M0GLD -// -// This file is part of fldigi. -// -// Fldigi is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Fldigi is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with fldigi. If not, see . -// ---------------------------------------------------------------------------- - #ifndef FILESELECT_H #define FILESELECT_H -#ifdef __WOE32__ -# define FSEL_THREAD 1 -#endif +#include +#if (FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3) || (FLARQ_FLTK_API_MAJOR == 1 && FLARQ_FLTK_API_MINOR < 3) +//#ifdef __WIN32__ +//# define FSEL_THREAD 1 +//#endif +#define FSEL_THREAD 0 class Fl_Native_File_Chooser; +#else +#include +#define FSEL_THREAD 0 +#endif class FSEL { diff --git a/src/main.cxx b/src/main.cxx index 77918bf2..ee9a552c 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -47,9 +47,7 @@ #endif #include -#ifndef __MINGW32__ -# include -#endif + #include #include #include @@ -63,10 +61,17 @@ # define dirent fl_dirent_no_thanks #endif #include -#ifdef __MINGW32__ -# undef dirent + +#ifdef __WOE32__ +# if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# undef dirent +# include +# endif +#else +# include #endif + #include "gettext.h" #include "main.h" #include "waterfall.h" diff --git a/src/misc/configuration.cxx b/src/misc/configuration.cxx index 30eb8289..8c9a082a 100644 --- a/src/misc/configuration.cxx +++ b/src/misc/configuration.cxx @@ -68,6 +68,23 @@ # include #endif +// this tests depends on a modified FL/filename.H in the Fltk-1.3.0 +// change +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) +// to +//# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WOE32__) + +#ifdef __MINGW32__ +# if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# undef dirent +# include +# else +# include +# endif +#else +# include +#endif + using namespace std; diff --git a/src/misc/macros.cxx b/src/misc/macros.cxx index 7134a6ea..5c86bbc7 100644 --- a/src/misc/macros.cxx +++ b/src/misc/macros.cxx @@ -70,12 +70,12 @@ struct CMDS { std::string cmd; void (*fp)(std::string); }; static queue cmds; // following used for debugging and development -//void pushcmd(CMDS cmd) -//{ -// LOG_INFO("%s, # = %d", cmd.cmd.c_str(), (int)cmds.size()); -// cmds.push(cmd); -//} -#define pushcmd(a) cmds.push((a)) +void pushcmd(CMDS cmd) +{ + LOG_INFO("%s, # = %d", cmd.cmd.c_str(), (int)cmds.size()); + cmds.push(cmd); +} +//#define pushcmd(a) cmds.push((a)) // these variables are referenced outside of this file MACROTEXT macros; @@ -670,7 +670,7 @@ static void pZD(std::string &s, size_t &i, size_t endbracket) s.replace( i, 4, szDt); } -static void pID(std::string &s, size_t &i, size_t endbracket) +static void p_ID(std::string &s, size_t &i, size_t endbracket) { if (within_exec) { s.replace(i, endbracket - i + 1, ""); @@ -1833,7 +1833,8 @@ bool queue_must_rx() static std::string rxcmds = "", pZT}, {"", pLD}, {"", pZD}, -{"", pID}, +{"", p_ID}, {"", pTEXT}, {"", pCWID}, {"", pRX}, @@ -2016,7 +2017,7 @@ void MACROTEXT::openMacroFile() { std::string deffilename = MacrosDir; deffilename.append(progStatus.LastMacroFile); - const char *p = FSEL::select(_("Open macro file"), _("Fldigi macro definition file\t*.mdf"), deffilename.c_str()); + const char *p = FSEL::select(_("Open macro file"), _("Fldigi macro definition file\t*.{mdf}"), deffilename.c_str()); if (p) { loadMacros(p); progStatus.LastMacroFile = fl_filename_name(p); @@ -2028,10 +2029,15 @@ void MACROTEXT::saveMacroFile() { std::string deffilename = MacrosDir; deffilename.append(progStatus.LastMacroFile); - const char *p = FSEL::saveas(_("Save macro file"), _("Fldigi macro definition file\t*.mdf"), deffilename.c_str()); + const char *p = FSEL::saveas( + _("Save macro file"), + _("Fldigi macro definition file\t*.{mdf}"), + deffilename.c_str()); if (p) { - saveMacros(p); - progStatus.LastMacroFile = fl_filename_name(p); + string sp = p; + if (sp.rfind(".mdf") == string::npos) sp.append(".mdf"); + saveMacros(sp.c_str()); + progStatus.LastMacroFile = fl_filename_name(sp.c_str()); } } diff --git a/src/widgets/FTextRXTX.cxx b/src/widgets/FTextRXTX.cxx index 67369ace..13135168 100644 --- a/src/widgets/FTextRXTX.cxx +++ b/src/widgets/FTextRXTX.cxx @@ -143,7 +143,9 @@ FTextRX::FTextRX(int x, int y, int w, int h, const char *l) init_context_menu(); menu[RX_MENU_QUICK_ENTRY].clear(); menu[RX_MENU_SCROLL_HINTS].clear(); - +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + menu[RX_MENU_WRAP].hide(); +#endif // Replace the scrollbar widget MVScrollbar* mvsb = new MVScrollbar(mVScrollBar->x(), mVScrollBar->y(), mVScrollBar->w(), mVScrollBar->h(), NULL); @@ -152,6 +154,9 @@ FTextRX::FTextRX(int x, int y, int w, int h, const char *l) remove(mVScrollBar); delete mVScrollBar; Fl_Group::add(mVScrollBar = mvsb); +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + mFastDisplay = 1; +#endif } FTextRX::~FTextRX() @@ -209,7 +214,11 @@ int FTextRX::handle(int event) } case FL_MOVE: { int p = xy_to_position(Fl::event_x(), Fl::event_y(), Fl_Text_Display_mod::CURSOR_POS); +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + if (sbuf->char_at(p) >= CLICK_START + FTEXT_DEF) { +#else if (sbuf->character(p) >= CLICK_START + FTEXT_DEF) { +#endif if (cursor != FL_CURSOR_HAND) window()->cursor(cursor = FL_CURSOR_HAND); return 1; @@ -248,6 +257,99 @@ out: /// @param c The character /// @param attr The attribute (@see enum text_attr_e); RECV if omitted. /// + +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +void FTextRX::add(unsigned char c, int attr) +{ + if (c == '\r') + return; + + char s[] = { '\0', '\0', FTEXT_DEF + attr, '\0' }; + const char *cp = &s[0]; + + // The user may have moved the cursor by selecting text or + // scrolling. Place it at the end of the buffer. + if (mCursorPos != tbuf->length()) + insert_position(tbuf->length()); + + switch (c) { + case '\b': + // we don't call kf_backspace because it kills selected text + tbuf->remove(tbuf->length() - 1, tbuf->length()); + sbuf->remove(sbuf->length() - 1, sbuf->length()); + if (s_text.length()) { + s_text.erase(s_text.end() - 1); + s_style.erase(s_style.end() - 1); + } + break; + case '\n': + // maintain the scrollback limit, if we have one + if (max_lines > 0 && tbuf->count_lines(0, tbuf->length()) >= max_lines) { + int le = tbuf->line_end(0) + 1; // plus 1 for the newline + tbuf->remove(0, le); + sbuf->remove(0, le); + } + s_text.clear(); + s_style.clear(); + insert("\n"); + sbuf->append(s + 2); + break; + default: + if ((c < ' ' || c == 127) && attr != CTRL) // look it up + cp = ascii[(unsigned char)c]; + else // insert verbatim + s[0] = c; + + for (int i = 0; cp[i]; ++i) { + s_text += cp[i]; + s_style += s[2]; + } + + fl_font( textfont(), textsize() ); + int lwidth = (int)fl_width( s_text.c_str(), s_text.length()); + bool wrapped = false; + if ( lwidth >= (text_area.w - mVScrollBar->w() - LEFT_MARGIN - RIGHT_MARGIN)) { + if (c != ' ') { + size_t p = s_text.rfind(' '); + if (p != string::npos) { + s_text.erase(0, p+1); + s_style.erase(0, p+1); + if (s_text.length() < 10) { // wrap and delete trailing space + tbuf->remove(tbuf->length() - s_text.length(), tbuf->length()); + sbuf->remove(sbuf->length() - s_style.length(), sbuf->length()); + insert("\n"); // always insert new line + sbuf->append(s + 2); + insert(s_text.c_str()); + sbuf->append(s_style.c_str()); + wrapped = true; + } + } + } + if (!wrapped) { // add a new line if not wrapped + insert("\n"); + sbuf->append(s + 2); + if (c != ' ') { // add character if not a space (no leading spaces) + for (int i = 0; cp[i]; ++i) + sbuf->append(s + 2); + insert(cp); + } + } + s_text.clear(); + s_style.clear(); + } else { + for (int i = 0; cp[i]; ++i) + sbuf->append(s + 2); + insert(cp); + } + break; + } + +// test for bottom of text visibility + if (// !mFastDisplay && + (mVScrollBar->value() >= mNBufferLines - mNVisibleLines + mVScrollBar->linesize() - 1)) + show_insert_position(); +} +#else void FTextRX::add(unsigned char c, int attr) { if (c == '\r') @@ -288,7 +390,11 @@ void FTextRX::add(unsigned char c, int attr) insert(cp); break; } +// test for bottom of text visibility + if (mVScrollBar->value() >= mNBufferLines - mNVisibleLines + mVScrollBar->linesize() - 1) + show_insert_position(); } +#endif void FTextRX::set_quick_entry(bool b) { @@ -317,6 +423,10 @@ void FTextRX::mark(FTextBase::TEXT_ATTR attr) void FTextRX::clear(void) { FTextBase::clear(); +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + s_text.clear(); + s_style.clear(); +#endif static_cast(mVScrollBar)->clear(); } @@ -328,21 +438,38 @@ void FTextRX::setFont(Fl_Font f, int attr) void FTextRX::handle_clickable(int x, int y) { +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + int pos; + unsigned int style; +#else int pos, style; +#endif pos = xy_to_position(x + this->x(), y + this->y(), CURSOR_POS); // return unless clickable style +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + if ((style = sbuf->char_at(pos)) < CLICK_START + FTEXT_DEF) +#else if ((style = sbuf->character(pos)) < CLICK_START + FTEXT_DEF) +#endif return; int start, end; for (start = pos-1; start >= 0; start--) +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + if (sbuf->char_at(start) != style) +#else if (sbuf->character(start) != style) +#endif break; start++; int len = sbuf->length(); for (end = pos+1; end < len; end++) +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + if (sbuf->char_at(end) != style) +#else if (sbuf->character(end) != style) +#endif break; switch (style - FTEXT_DEF) { @@ -786,7 +913,11 @@ int FTextTX::nextChar(void) else if (insert_position() <= txpos) // empty buffer or cursor inside transmitted text c = -1; else { +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + if ((c = static_cast(tbuf->char_at(txpos)))) { +#else if ((c = static_cast(tbuf->character(txpos)))) { +#endif ++txpos; REQ(FTextTX::changed_cb, txpos, 0, 0, -1, static_cast(0), this); diff --git a/src/widgets/FTextView.cxx b/src/widgets/FTextView.cxx index 61750f22..42d1fe66 100644 --- a/src/widgets/FTextView.cxx +++ b/src/widgets/FTextView.cxx @@ -49,6 +49,8 @@ #include "FTextView.h" +#include "debug.h" + using namespace std; @@ -192,6 +194,17 @@ void FTextBase::resize(int X, int Y, int W, int H) if (need_wrap_reset) reset_wrap_col(); +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + TOP_MARGIN = DEFAULT_TOP_MARGIN; + int r = H - Fl::box_dh(box()) - TOP_MARGIN - BOTTOM_MARGIN; + if (mHScrollBar->visible()) + r -= scrollbar_width(); + int msize = mMaxsize ? mMaxsize : textsize(); + if (!msize) msize = 1; +//printf("H %d, textsize %d, lines %d, extra %d\n", r, msize, r / msize, r % msize); + if (r %= msize) + TOP_MARGIN += r; +#else if (need_margin_reset && textsize() > 0) { TOP_MARGIN = DEFAULT_TOP_MARGIN; int r = H - Fl::box_dh(box()) - TOP_MARGIN - BOTTOM_MARGIN; @@ -200,13 +213,13 @@ void FTextBase::resize(int X, int Y, int W, int H) if (r %= textsize()) TOP_MARGIN += r; } - - if (scroll_hint) { - mTopLineNumHint = mNBufferLines; - mHorizOffsetHint = 0; +#endif + if (scroll_hint) { + mTopLineNumHint = mNBufferLines; + mHorizOffsetHint = 0; // display_insert_position_hint = 1; - scroll_hint = false; - } + scroll_hint = false; + } bool hscroll_visible = mHScrollBar->visible(); Fl_Text_Editor_mod::resize(X, Y, W, H); @@ -285,6 +298,8 @@ void FTextBase::set_style(int attr, Fl_Font f, int s, Fl_Color c, int set) /// @return 0 on success, -1 on error int FTextBase::readFile(const char* fn) { + set_word_wrap(restore_wrap); + if ( !(fn || (fn = FSEL::select(_("Insert text"), "Text\t*.txt"))) ) return -1; @@ -393,6 +408,10 @@ char* FTextBase::get_word(int x, int y, const char* nwchars, bool ontext) return tbuf->selection_text(); } +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 + start = tbuf->word_start(p); + end = tbuf->word_end(p); +#else string nonword = nwchars; nonword.append(" \t\n"); if (!tbuf->findchars_backward(p, nonword.c_str(), &start)) @@ -401,6 +420,7 @@ char* FTextBase::get_word(int x, int y, const char* nwchars, bool ontext) start++; if (!tbuf->findchars_forward(p, nonword.c_str(), &end)) end = tbuf->length(); +#endif if (ontext && (p < start || p >= end)) return 0; @@ -775,35 +795,32 @@ int FTextEdit::handle_dnd_drag(int pos) /// @return 1 or FTextBase::handle(FL_PASTE) int FTextEdit::handle_dnd_drop(void) { - restore_wrap = wrap; - set_word_wrap(false); - +// paste verbatim if the shift key was held down during dnd if (Fl::event_shift()) return FTextBase::handle(FL_PASTE); -#if !defined(__APPLE__) && !defined(__WOE32__) - if (strncmp(Fl::event_text(), "file://", 7)) - return FTextBase::handle(FL_PASTE); -#endif + string text; + string::size_type p, len; + + text = Fl::event_text(); - string text = Fl::event_text(); -#if defined(__APPLE__) || defined(__WOE32__) const char sep[] = "\n"; +#if defined(__APPLE__) || defined(__WOE32__) text += sep; -#else - const char sep[] = "\r\n"; #endif - string::size_type p, len = text.length(); + len = text.length(); while ((p = text.find(sep)) != string::npos) { text[p] = '\0'; #if !defined(__APPLE__) && !defined(__WOE32__) if (text.find("file://") == 0) { text.erase(0, 7); p -= 7; + len -= 7; } #endif - // paste everything verbatim if we couldn't read the first file +// paste everything verbatim if we cannot read the first file +LOG_INFO("DnD file %s", text.c_str()); if (readFile(text.c_str()) == -1 && len == text.length()) return FTextBase::handle(FL_PASTE); text.erase(0, p + sizeof(sep) - 1); diff --git a/src/widgets/Fl_Text_Buffer_mod_1_3.cxx b/src/widgets/Fl_Text_Buffer_mod_1_3.cxx index bffbabac..fd88fed5 100644 --- a/src/widgets/Fl_Text_Buffer_mod_1_3.cxx +++ b/src/widgets/Fl_Text_Buffer_mod_1_3.cxx @@ -1,7 +1,7 @@ // -// "$Id: Fl_Text_Buffer.cxx 7462 2010-04-06 23:00:56Z matt $" +// "$Id: Fl_Text_Buffer_mod.cxx 8040 2010-12-15 17:38:39Z manolo $" // -// Copyright 2001-2009 by Bill Spitzak and others. +// Copyright 2001-2010 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under // the LGPL for the FLTK library granted by Mark Edel. // @@ -31,92 +31,37 @@ #include "flstring.h" #include #include +#include #include "Fl_Text_Buffer_mod.H" + /* This file is based on a port of NEdit to FLTK many years ago. NEdit at that point was already stretched beyond the task it was designed for which explains - why the source code is sometimes pretty convoluted. It still is a nice widget - for FLTK. + why the source code is sometimes pretty convoluted. It still is a very useful + widget for FLTK, and we are thankful that the nedit team allowed us to + integrate their code. With the introduction of Unicode and UTF-8, Fl_Text_... has to go into a whole - new generation of code. Originally designed for monspaced fonts only, many - features make les sense in the multibyte and multiwdth world of UTF-8. - - Columns are a good example. There is simply no such thing. - - Rectangular selections pose a real problem. - - Using multiple spaces to emulate tab stops will no longer work. - - And constantly recalculating character widths is just much too expensive. - - But nevertheless, we will get ths widget rolling again ;-) - + new generation of code. Originally designed for monospaced fonts only, many + features make less sense in the multibyte and multiwidth world of UTF-8. + + Columns are a good example. There is simply no such thing. The new Fl_Text_... + widget converts columns to pixels by multiplying them with the average + character width for a given font. + + Rectangular selections were rarely used (if at all) and make little sense when + using variable width fonts. They have been removed. + + Using multiple spaces to emulate tab stops has been replaced by pixel counting + routines. They are slower, but give the expected result for proportional fonts. + + And constantly recalculating character widths is just much too expensive. Lines + of text are now subdivided into blocks of text which are measured at once + instead of individual characters. */ -/* - \todo unicode check - */ -static void insertColInLine(const char *line, char *insLine, int column, - int insWidth, int tabDist, int useTabs, - char *outStr, int *outLen, - int *endOffset); - -/* - \todo unicode check - */ -static void deleteRectFromLine(const char *line, int rectStart, - int rectEnd, int tabDist, int useTabs, - char *outStr, - int *outLen, int *endOffset); - -/* - \todo unicode check - */ -static void overlayRectInLine(const char *line, char *insLine, - int rectStart, int rectEnd, int tabDist, - int useTabs, char *outStr, - int *outLen, int *endOffset); - -/* - \todo unicode check - */ -static void addPadding(char *string, int startIndent, int toIndent, - int tabDist, int useTabs, int *charsAdded); - -/* - \todo unicode check - */ -static char *copyLine(const char *text, int *lineLen); - -/* - unicode tested - */ -static int countLines(const char *string); - -/* - \todo unicode check - */ -static int textWidth(const char *text, int tabDist); - -/* - \todo unicode check - */ -static char *realignTabs(const char *text, int origIndent, int newIndent, - int tabDist, int useTabs, int *newLength); - -/* - \todo unicode check - */ -static char *expandTabs(const char *text, int startIndent, int tabDist, int *newLen); - -/* - \todo unicode check - */ -static char *unexpandTabs(char *text, int startIndent, int tabDist, int *newLen); - #ifndef min static int max(int i1, int i2) @@ -131,12 +76,6 @@ static int min(int i1, int i2) #endif -static const char *ControlCodeTable[32] = { - "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", - "bs", "ht", "nl", "vt", "np", "cr", "so", "si", - "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", - "can", "em", "sub", "esc", "fs", "gs", "rs", "us" -}; static char *undobuffer; static int undobufferlength; @@ -146,6 +85,9 @@ static int undocut; // number of characters deleted there static int undoinsert; // number of characters inserted static int undoyankcut; // length of valid contents of buffer, even if undocut=0 +/* + Resize the undo buffer to match at least the requested size. + */ static void undobuffersize(int n) { if (n > undobufferlength) { @@ -161,8 +103,14 @@ static void undobuffersize(int n) } } +static void def_transcoding_warning_action(Fl_Text_Buffer_mod *text) +{ + fl_alert("%s", text->file_encoding_warning_message); +} -// unicode ok +/* + Initialize all variables. + */ Fl_Text_Buffer_mod::Fl_Text_Buffer_mod(int requestedSize, int preferredGapSize) { mLength = 0; @@ -171,16 +119,12 @@ Fl_Text_Buffer_mod::Fl_Text_Buffer_mod(int requestedSize, int preferredGapSize) mGapStart = 0; mGapEnd = mPreferredGapSize; mTabDist = 8; - mUseTabs = 1; mPrimary.mSelected = 0; - mPrimary.mRectangular = 0; mPrimary.mStart = mPrimary.mEnd = 0; mSecondary.mSelected = 0; mSecondary.mStart = mSecondary.mEnd = 0; - mSecondary.mRectangular = 0; mHighlight.mSelected = 0; mHighlight.mStart = mHighlight.mEnd = 0; - mHighlight.mRectangular = 0; mModifyProcs = NULL; mCbArgs = NULL; mNModifyProcs = 0; @@ -189,17 +133,14 @@ Fl_Text_Buffer_mod::Fl_Text_Buffer_mod(int requestedSize, int preferredGapSize) mPredeleteCbArgs = NULL; mCursorPosHint = 0; mCanUndo = 1; -#ifdef PURIFY - { - int i; - for (i = mGapStart; i < mGapEnd; i++) - mBuf[i] = '.'; - } -#endif + input_file_was_transcoded = 0; + transcoding_warning_action = def_transcoding_warning_action; } -// unicode ok +/* + Free all resources. + */ Fl_Text_Buffer_mod::~Fl_Text_Buffer_mod() { free(mBuf); @@ -214,21 +155,26 @@ Fl_Text_Buffer_mod::~Fl_Text_Buffer_mod() } -// This function copies verbose whatever is in front and after the gap into a -// single buffer. -// - unicode ok +/* + This function copies verbose whatever is in front and after the gap into a + single buffer. + */ char *Fl_Text_Buffer_mod::text() const { char *t = (char *) malloc(mLength + 1); memcpy(t, mBuf, mGapStart); - memcpy(&t[mGapStart], &mBuf[mGapEnd], mLength - mGapStart); + memcpy(t+mGapStart, mBuf+mGapEnd, mLength - mGapStart); t[mLength] = '\0'; return t; } -// unicode ok, functions called have not been verified yet +/* + Set the text buffer to a new string. + */ void Fl_Text_Buffer_mod::text(const char *t) { + IS_UTF8_ALIGNED(t) + call_predelete_callbacks(0, length()); /* Save information for redisplay, and get rid of the old buffer */ @@ -243,13 +189,6 @@ void Fl_Text_Buffer_mod::text(const char *t) mGapStart = insertedLength; mGapEnd = mGapStart + mPreferredGapSize; memcpy(mBuf, t, insertedLength); -#ifdef PURIFY - { - int i; - for (i = mGapStart; i < mGapEnd; i++) - mBuf[i] = '.'; - } -#endif /* Zero all of the existing selections */ update_selections(0, deletedLength, 0); @@ -260,9 +199,13 @@ void Fl_Text_Buffer_mod::text(const char *t) } -// Creates a new buffer and copies verbose from around the gap. -// - unicode ok +/* + Creates a range of text to a new buffer and copies verbose from around the gap. + */ char *Fl_Text_Buffer_mod::text_range(int start, int end) const { + IS_UTF8_ALIGNED2(this, (start)) + IS_UTF8_ALIGNED2(this, (end)) + char *s = NULL; /* Make sure start and end are ok, and allocate memory for returned string. @@ -285,32 +228,58 @@ char *Fl_Text_Buffer_mod::text_range(int start, int end) const { /* Copy the text from the buffer to the returned string */ if (end <= mGapStart) { - memcpy(s, &mBuf[start], copiedLength); + memcpy(s, mBuf + start, copiedLength); } else if (start >= mGapStart) { - memcpy(s, &mBuf[start + (mGapEnd - mGapStart)], copiedLength); + memcpy(s, mBuf + start + (mGapEnd - mGapStart), copiedLength); } else { int part1Length = mGapStart - start; - memcpy(s, &mBuf[start], part1Length); - memcpy(&s[part1Length], &mBuf[mGapEnd], copiedLength - part1Length); + memcpy(s, mBuf + start, part1Length); + memcpy(s + part1Length, mBuf + mGapEnd, copiedLength - part1Length); } s[copiedLength] = '\0'; return s; } - -// TODO: we will need the same signature function to get bytes (style buffer) -// unicode ok -unsigned int Fl_Text_Buffer_mod::character(int pos) const { +/* + Return a UCS-4 character at the given index. + Pos must be at a character boundary. + */ +unsigned int Fl_Text_Buffer_mod::char_at(int pos) const { if (pos < 0 || pos >= mLength) return '\0'; + + IS_UTF8_ALIGNED2(this, (pos)) + const char *src = address(pos); return fl_utf8decode(src, 0, 0); } -// unicode ok, dependents not tested +/* + Return the raw byte at the given index. + This function ignores all unicode encoding. + */ +char Fl_Text_Buffer_mod::byte_at(int pos) const { + if (pos < 0 || pos >= mLength) + return '\0'; + const char *src = address(pos); + return *src; +} + + +/* + Insert some text at the given index. + Pos must be at a character boundary. +*/ void Fl_Text_Buffer_mod::insert(int pos, const char *text) { + IS_UTF8_ALIGNED2(this, (pos)) + IS_UTF8_ALIGNED(text) + + /* check if there is actually any text */ + if (!text || !*text) + return; + /* if pos is not contiguous to existing text, make it */ if (pos > mLength) pos = mLength; @@ -323,11 +292,15 @@ void Fl_Text_Buffer_mod::insert(int pos, const char *text) /* insert and redisplay */ int nInserted = insert_(pos, text); mCursorPosHint = pos + nInserted; + IS_UTF8_ALIGNED2(this, (mCursorPosHint)) call_modify_callbacks(pos, 0, nInserted, 0, NULL); } -// unicode ok, dependents not tested +/* + Replace a range of text with new text. + Start and end must be at a character boundary. +*/ void Fl_Text_Buffer_mod::replace(int start, int end, const char *text) { // Range check... @@ -335,10 +308,12 @@ void Fl_Text_Buffer_mod::replace(int start, int end, const char *text) return; if (start < 0) start = 0; - if (end < 0) - end = 0; if (end > mLength) end = mLength; + + IS_UTF8_ALIGNED2(this, (start)) + IS_UTF8_ALIGNED2(this, (end)) + IS_UTF8_ALIGNED(text) call_predelete_callbacks(start, end - start); const char *deletedText = text_range(start, end); @@ -350,7 +325,10 @@ void Fl_Text_Buffer_mod::replace(int start, int end, const char *text) } -// unicode ok, dependents not tested +/* + Remove a range of text. + Start and End must be at a character boundary. +*/ void Fl_Text_Buffer_mod::remove(int start, int end) { /* Make sure the arguments make sense */ @@ -367,6 +345,9 @@ void Fl_Text_Buffer_mod::remove(int start, int end) end = mLength; if (end < 0) end = 0; + + IS_UTF8_ALIGNED2(this, (start)) + IS_UTF8_ALIGNED2(this, (end)) if (start == end) return; @@ -380,9 +361,18 @@ void Fl_Text_Buffer_mod::remove(int start, int end) free((void *) deletedText); } + +/* + Copy a range of text from another text buffer. + fromStart, fromEnd, and toPos must be at a character boundary. + */ void Fl_Text_Buffer_mod::copy(Fl_Text_Buffer_mod * fromBuf, int fromStart, int fromEnd, int toPos) { + IS_UTF8_ALIGNED2(fromBuf, fromStart) + IS_UTF8_ALIGNED2(fromBuf, fromEnd) + IS_UTF8_ALIGNED2(this, (toPos)) + int copiedLength = fromEnd - fromStart; /* Prepare the buffer to receive the new text. If the new text fits in @@ -400,8 +390,7 @@ void Fl_Text_Buffer_mod::copy(Fl_Text_Buffer_mod * fromBuf, int fromStart, memcpy(&mBuf[toPos], &fromBuf->mBuf[fromStart], copiedLength); } else if (fromStart >= fromBuf->mGapStart) { memcpy(&mBuf[toPos], - &fromBuf->mBuf[fromStart + - (fromBuf->mGapEnd - fromBuf->mGapStart)], + &fromBuf->mBuf[fromStart + (fromBuf->mGapEnd - fromBuf->mGapStart)], copiedLength); } else { int part1Length = fromBuf->mGapStart - fromStart; @@ -414,9 +403,15 @@ void Fl_Text_Buffer_mod::copy(Fl_Text_Buffer_mod * fromBuf, int fromStart, update_selections(toPos, 0, copiedLength); } + +/* + Take the previous changes and undo them. Return the previous + cursor position in cursorPos. Returns 1 if the undo was applied. + CursorPos will be at a character boundary. + */ int Fl_Text_Buffer_mod::undo(int *cursorPos) { - if (undowidget != this || !undocut && !undoinsert && !mCanUndo) + if (undowidget != this || (!undocut && !undoinsert && !mCanUndo)) return 0; int ilen = undocut; @@ -452,181 +447,25 @@ int Fl_Text_Buffer_mod::undo(int *cursorPos) } -// unicode ok +/* + Set a flag if undo function will work. + */ void Fl_Text_Buffer_mod::canUndo(char flag) { mCanUndo = flag; + // disabling undo also clears the last undo operation! + if (!mCanUndo && undowidget==this) + undowidget = 0; } -void Fl_Text_Buffer_mod::insert_column(int column, int startPos, - const char *text, int *charsInserted, - int *charsDeleted) -{ - int nLines = countLines(text); - int lineStartPos = line_start(startPos); - int nDeleted = line_end(skip_lines(startPos, nLines)) - lineStartPos; - call_predelete_callbacks(lineStartPos, nDeleted); - const char *deletedText = - text_range(lineStartPos, lineStartPos + nDeleted); - int insertDeleted, nInserted; - insert_column_(column, lineStartPos, text, &insertDeleted, &nInserted, - &mCursorPosHint); - if (nDeleted != insertDeleted) - Fl::error - ("Fl_Text_Buffer_mod::insert_column(): internal consistency check ins1 failed"); - call_modify_callbacks(lineStartPos, nDeleted, nInserted, 0, deletedText); - free((void *) deletedText); - if (charsInserted != NULL) - *charsInserted = nInserted; - if (charsDeleted != NULL) - *charsDeleted = nDeleted; -} - -void Fl_Text_Buffer_mod::overlay_rectangular(int startPos, int rectStart, - int rectEnd, const char *text, - int *charsInserted, - int *charsDeleted) -{ - - int nLines = countLines(text); - int lineStartPos = line_start(startPos); - int nDeleted = line_end(skip_lines(startPos, nLines)) - lineStartPos; - call_predelete_callbacks(lineStartPos, nDeleted); - const char *deletedText = - text_range(lineStartPos, lineStartPos + nDeleted); - int insertDeleted, nInserted; - overlay_rectangular_(lineStartPos, rectStart, rectEnd, text, - &insertDeleted, &nInserted, &mCursorPosHint); - if (nDeleted != insertDeleted) - Fl::error - ("Fl_Text_Buffer_mod::overlay_rectangle(): internal consistency check ovly1 failed"); - call_modify_callbacks(lineStartPos, nDeleted, nInserted, 0, deletedText); - free((void *) deletedText); - if (charsInserted != NULL) - *charsInserted = nInserted; - if (charsDeleted != NULL) - *charsDeleted = nDeleted; -} - -void Fl_Text_Buffer_mod::replace_rectangular(int start, int end, int rectStart, - int rectEnd, const char *text) -{ - char *insText = (char *) ""; - int linesPadded = 0; - - /* Make sure start and end refer to complete lines, since the - columnar delete and insert operations will replace whole lines */ - start = line_start(start); - end = line_end(end); - - call_predelete_callbacks(start, end - start); - - /* If more lines will be deleted than inserted, pad the inserted text - with newlines to make it as long as the number of deleted lines. This - will indent all of the text to the right of the rectangle to the same - column. If more lines will be inserted than deleted, insert extra - lines in the buffer at the end of the rectangle to make room for the - additional lines in "text" */ - int nInsertedLines = countLines(text); - int nDeletedLines = count_lines(start, end); - if (nInsertedLines < nDeletedLines) { - int insLen = strlen(text); - insText = (char *) malloc(insLen + nDeletedLines - nInsertedLines + 1); - strcpy(insText, text); - char *insPtr = insText + insLen; - for (int i = 0; i < nDeletedLines - nInsertedLines; i++) - *insPtr++ = '\n'; - *insPtr = '\0'; - } else if (nDeletedLines < nInsertedLines) { - linesPadded = nInsertedLines - nDeletedLines; - for (int i = 0; i < linesPadded; i++) - insert_(end, "\n"); - } - - /* else nDeletedLines == nInsertedLines; */ - /* Save a copy of the text which will be modified for the modify CBs */ - const char *deletedText = text_range(start, end); - - /* Delete then insert */ - int insertDeleted, insertInserted, deleteInserted, hint; - remove_rectangular_(start, end, rectStart, rectEnd, &deleteInserted, - &hint); - insert_column_(rectStart, start, insText, &insertDeleted, - &insertInserted, &mCursorPosHint); - - /* Figure out how many chars were inserted and call modify callbacks */ - if (insertDeleted != deleteInserted + linesPadded) - Fl::error - ("Fl_Text_Buffer_mod::replace_rectangular(): internal consistency check repl1 failed"); - call_modify_callbacks(start, end - start, insertInserted, 0, - deletedText); - free((void *) deletedText); - if (nInsertedLines < nDeletedLines) - free((void *) insText); -} - -void Fl_Text_Buffer_mod::remove_rectangular(int start, int end, int rectStart, - int rectEnd) -{ - - start = line_start(start); - end = line_end(end); - call_predelete_callbacks(start, end - start); - const char *deletedText = text_range(start, end); - int nInserted; - remove_rectangular_(start, end, rectStart, rectEnd, &nInserted, - &mCursorPosHint); - call_modify_callbacks(start, end - start, nInserted, 0, deletedText); - free((void *) deletedText); -} - -void Fl_Text_Buffer_mod::clear_rectangular(int start, int end, int rectStart, - int rectEnd) -{ - int nLines = count_lines(start, end); - char *newlineString = (char *) malloc(nLines + 1); - int i; - for (i = 0; i < nLines; i++) - newlineString[i] = '\n'; - newlineString[i] = '\0'; - overlay_rectangular(start, rectStart, rectEnd, newlineString, - NULL, NULL); - free((void *) newlineString); -} - -char *Fl_Text_Buffer_mod::text_in_rectangle(int start, int end, - int rectStart, - int rectEnd) const { - start = line_start(start); - end = line_end(end); - char *textOut = (char *) malloc((end - start) + 1); - int lineStart = start; - char *outPtr = textOut; - int selLeft, selRight; - while (lineStart <= end) - { - rectangular_selection_boundaries(lineStart, rectStart, rectEnd, - &selLeft, &selRight); - const char *textIn = text_range(selLeft, selRight); - int len = selRight - selLeft; - memcpy(outPtr, textIn, len); - free((void *) textIn); - outPtr += len; - lineStart = line_end(selRight) + 1; - *outPtr++ = '\n'; - } if (outPtr != textOut) - outPtr--; /* don't leave trailing newline */ - *outPtr = '\0'; - - /* If necessary, realign the tabs in the selection as if the text were - positioned at the left margin */ - int len; - char *retabbedStr = realignTabs(textOut, rectStart, 0, mTabDist, - mUseTabs, &len); - free((void *) textOut); - return retabbedStr; -} +/* + Change the tab width. This will cause a couple of callbacks and a complete + redisplay. + Matt: I am not entirely sure why we need to trigger callbacks because + tabs are only a graphical hint, not changing any text at all, but I leave + this in here for back compatibility. + */ void Fl_Text_Buffer_mod::tab_distance(int tabDist) { /* First call the pre-delete callbacks with the previous tab setting @@ -643,154 +482,178 @@ void Fl_Text_Buffer_mod::tab_distance(int tabDist) free((void *) deletedText); } + +/* + Select a range of text. + Start and End must be at a character boundary. + */ void Fl_Text_Buffer_mod::select(int start, int end) { - Fl_Text_Selection oldSelection = mPrimary; + IS_UTF8_ALIGNED2(this, (start)) + IS_UTF8_ALIGNED2(this, (end)) + + Fl_Text_Selection_mod oldSelection = mPrimary; mPrimary.set(start, end); redisplay_selection(&oldSelection, &mPrimary); } + +/* + Clear the primary selection. + */ void Fl_Text_Buffer_mod::unselect() { - Fl_Text_Selection oldSelection = mPrimary; + Fl_Text_Selection_mod oldSelection = mPrimary; mPrimary.mSelected = 0; redisplay_selection(&oldSelection, &mPrimary); } -void Fl_Text_Buffer_mod::select_rectangular(int start, int end, int rectStart, - int rectEnd) -{ - Fl_Text_Selection oldSelection = mPrimary; - mPrimary.set_rectangular(start, end, rectStart, rectEnd); - redisplay_selection(&oldSelection, &mPrimary); -} - +/* + Return the primary selection range. + */ int Fl_Text_Buffer_mod::selection_position(int *start, int *end) { return mPrimary.position(start, end); } -int Fl_Text_Buffer_mod::selection_position(int *start, int *end, - int *isRect, int *rectStart, - int *rectEnd) -{ - return mPrimary.position(start, end, isRect, rectStart, rectEnd); -} +/* + Return a copy of the selected text. + */ char *Fl_Text_Buffer_mod::selection_text() { return selection_text_(&mPrimary); } + +/* + Remove the selected text. + */ void Fl_Text_Buffer_mod::remove_selection() { remove_selection_(&mPrimary); } + +/* + Replace the selected text. + */ void Fl_Text_Buffer_mod::replace_selection(const char *text) { replace_selection_(&mPrimary, text); } + +/* + Select text. + Start and End must be at a character boundary. + */ void Fl_Text_Buffer_mod::secondary_select(int start, int end) { - Fl_Text_Selection oldSelection = mSecondary; + Fl_Text_Selection_mod oldSelection = mSecondary; mSecondary.set(start, end); redisplay_selection(&oldSelection, &mSecondary); } + +/* + Deselect text. + */ void Fl_Text_Buffer_mod::secondary_unselect() { - Fl_Text_Selection oldSelection = mSecondary; + Fl_Text_Selection_mod oldSelection = mSecondary; mSecondary.mSelected = 0; redisplay_selection(&oldSelection, &mSecondary); } -void Fl_Text_Buffer_mod::secondary_select_rectangular(int start, int end, - int rectStart, - int rectEnd) -{ - Fl_Text_Selection oldSelection = mSecondary; - mSecondary.set_rectangular(start, end, rectStart, rectEnd); - redisplay_selection(&oldSelection, &mSecondary); -} - +/* + Return the selected range. + */ int Fl_Text_Buffer_mod::secondary_selection_position(int *start, int *end) { return mSecondary.position(start, end); } -int Fl_Text_Buffer_mod::secondary_selection_position(int *start, int *end, - int *isRect, - int *rectStart, - int *rectEnd) -{ - return mSecondary.position(start, end, isRect, rectStart, rectEnd); -} +/* + Return a copy of the text in this selection. + */ char *Fl_Text_Buffer_mod::secondary_selection_text() { return selection_text_(&mSecondary); } + +/* + Remove the selected text. + */ void Fl_Text_Buffer_mod::remove_secondary_selection() { remove_selection_(&mSecondary); } + +/* + Replace selected text. + */ void Fl_Text_Buffer_mod::replace_secondary_selection(const char *text) { replace_selection_(&mSecondary, text); } + +/* + Highlight a range of text. + Start and End must be at a character boundary. + */ void Fl_Text_Buffer_mod::highlight(int start, int end) { - Fl_Text_Selection oldSelection = mHighlight; + Fl_Text_Selection_mod oldSelection = mHighlight; mHighlight.set(start, end); redisplay_selection(&oldSelection, &mHighlight); } + +/* + Remove text highlighting. + */ void Fl_Text_Buffer_mod::unhighlight() { - Fl_Text_Selection oldSelection = mHighlight; + Fl_Text_Selection_mod oldSelection = mHighlight; mHighlight.mSelected = 0; redisplay_selection(&oldSelection, &mHighlight); } -void Fl_Text_Buffer_mod::highlight_rectangular(int start, int end, - int rectStart, int rectEnd) -{ - Fl_Text_Selection oldSelection = mHighlight; - mHighlight.set_rectangular(start, end, rectStart, rectEnd); - redisplay_selection(&oldSelection, &mHighlight); -} - +/* + Return position of highlight. + */ int Fl_Text_Buffer_mod::highlight_position(int *start, int *end) { return mHighlight.position(start, end); } -int Fl_Text_Buffer_mod::highlight_position(int *start, int *end, - int *isRect, int *rectStart, - int *rectEnd) -{ - return mHighlight.position(start, end, isRect, rectStart, rectEnd); -} +/* + Return a copy of highlighted text. + */ char *Fl_Text_Buffer_mod::highlight_text() { return selection_text_(&mHighlight); } + +/* + Add a callback that is called whenever text is modified. + */ void Fl_Text_Buffer_mod::add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void *cbArg) { @@ -812,8 +675,12 @@ void Fl_Text_Buffer_mod::add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, mCbArgs = newCBArgs; } -void Fl_Text_Buffer_mod:: -remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void *cbArg) + +/* + Remove a callback. + */ +void Fl_Text_Buffer_mod::remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, + void *cbArg) { int i, toRemove = -1; @@ -859,8 +726,12 @@ remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void *cbArg) mCbArgs = newCBArgs; } -void Fl_Text_Buffer_mod:: -add_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg) + +/* + Add a callback that is called before deleting text. + */ +void Fl_Text_Buffer_mod::add_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, + void *cbArg) { Fl_Text_Predelete_Cb *newPreDeleteProcs = new Fl_Text_Predelete_Cb[mNPredeleteProcs + 1]; @@ -880,8 +751,11 @@ add_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg) mPredeleteCbArgs = newCBArgs; } -void Fl_Text_Buffer_mod:: -remove_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg) + +/* + Remove a callback. + */ +void Fl_Text_Buffer_mod::remove_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg) { int i, toRemove = -1; /* find the matching callback to remove */ @@ -928,148 +802,116 @@ remove_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg) mPredeleteCbArgs = newCBArgs; } + +/* + Return a copy of the line that contains a given index. + Pos must be at a character boundary. + */ char *Fl_Text_Buffer_mod::line_text(int pos) const { return text_range(line_start(pos), line_end(pos)); } -int Fl_Text_Buffer_mod::line_start(int pos) const { + +/* + Find the beginning of the line. + */ +int Fl_Text_Buffer_mod::line_start(int pos) const +{ if (!findchar_backward(pos, '\n', &pos)) return 0; return pos + 1; } + +/* + Find the end of the line. + */ int Fl_Text_Buffer_mod::line_end(int pos) const { if (!findchar_forward(pos, '\n', &pos)) pos = mLength; return pos; } + +/* + Find the beginning of a word. + NOT UNICODE SAFE. + */ int Fl_Text_Buffer_mod::word_start(int pos) const { // FIXME: character is ucs-4 - while (pos && (isalnum(character(pos)) || character(pos) == '_')) { - pos--; + while (pos>0 && (isalnum(char_at(pos)) || char_at(pos) == '_')) { + pos = prev_char(pos); } // FIXME: character is ucs-4 - if (!(isalnum(character(pos)) || character(pos) == '_')) - pos++; + if (!(isalnum(char_at(pos)) || char_at(pos) == '_')) + pos = next_char(pos); return pos; } + +/* + Find the end of a word. + NOT UNICODE SAFE. + */ int Fl_Text_Buffer_mod::word_end(int pos) const { // FIXME: character is ucs-4 - while (pos < length() && (isalnum(character(pos)) || character(pos) == '_')) + while (pos < length() && (isalnum(char_at(pos)) || char_at(pos) == '_')) { - pos++; - } return pos; -} - - -// Expand from the byte representation into some readable text. -// Under unicode, this is not really needed because all characters should -// be prinatble one way or the other. But since we use this to (badly) simulate -// tabs, we can leave this here anyway. -// - unicode ok -int Fl_Text_Buffer_mod::expand_character(int pos, int indent, char *outStr) const { - const char *src = address(pos); - return expand_character(src, indent, outStr, mTabDist); -} - - -// static function and counterpart to "character_width" -// - unicode ok -int Fl_Text_Buffer_mod::expand_character(const char *src, int indent, char *outStr, int tabDist) -{ - char c = *src; - /* Convert tabs to spaces */ - if (c == '\t') { - int nSpaces = tabDist - (indent % tabDist); - for (int i = 0; i < nSpaces; i++) - outStr[i] = ' '; - return nSpaces; + pos = next_char(pos); } - - /* Convert control codes to readable character sequences */ - if (((unsigned char) c) <= 31) { - sprintf(outStr, "<%s>", ControlCodeTable[(unsigned char) c]); - return strlen(outStr); - } else if (c == 127) { - sprintf(outStr, ""); - return 5; - } else if ((c & 0x80) && !(c & 0x40)) { -#ifdef DEBUG - fprintf(stderr, "%s:%d - Error in UTF-8 encoding!\n", __FILE__, __LINE__); -#endif - *outStr = c; - return 1; - } else if (c & 0x80) { - int i, n = fl_utf8len(c); - for (i=0; i= nLines) + if (lineCount >= nLines) { + IS_UTF8_ALIGNED2(this, (pos)) return pos; + } } } + IS_UTF8_ALIGNED2(this, (pos)) return pos; } + +/* + Skip to the first character, n lines back. + StartPos must be at a character boundary. + This function is optimized for speed by not using UTF-8 calls. + */ int Fl_Text_Buffer_mod::rewind_lines(int startPos, int nLines) { + IS_UTF8_ALIGNED2(this, (startPos)) + int pos = startPos - 1; if (pos <= 0) return 0; @@ -1124,136 +988,144 @@ int Fl_Text_Buffer_mod::rewind_lines(int startPos, int nLines) int lineCount = -1; while (pos >= mGapStart) { if (mBuf[pos + gapLen] == '\n') { - if (++lineCount >= nLines) + if (++lineCount >= nLines) { + IS_UTF8_ALIGNED2(this, (pos+1)) return pos + 1; + } } pos--; } while (pos >= 0) { if (mBuf[pos] == '\n') { - if (++lineCount >= nLines) + if (++lineCount >= nLines) { + IS_UTF8_ALIGNED2(this, (pos+1)) return pos + 1; + } } pos--; } return 0; } + +/* + Find a matching string in the buffer. + */ int Fl_Text_Buffer_mod::search_forward(int startPos, const char *searchString, - int *foundPos, - int matchCase) const + int *foundPos, int matchCase) const { + IS_UTF8_ALIGNED2(this, (startPos)) + IS_UTF8_ALIGNED(searchString) + if (!searchString) return 0; int bp; const char *sp; - while (startPos < length()) { - bp = startPos; - sp = searchString; - do { - if (!*sp) { - *foundPos = startPos; - return 1; + if (matchCase) { + while (startPos < length()) { + bp = startPos; + sp = searchString; + for (;;) { + char c = *sp; + // we reached the end of the "needle", so we found the string! + if (!c) { + *foundPos = startPos; + return 1; + } + int l = fl_utf8len1(c); + if (memcmp(sp, address(bp), l)) + break; + sp += l; bp += l; } - // FIXME: character is ucs-4 - } while ((matchCase ? character(bp++) == *sp++ : - toupper(character(bp++)) == toupper(*sp++)) - && bp < length()); - startPos++; - } + startPos = next_char(startPos); + } + } else { + while (startPos < length()) { + bp = startPos; + sp = searchString; + for (;;) { + // we reached the end of the "needle", so we found the string! + if (!*sp) { + *foundPos = startPos; + return 1; + } + int l; + unsigned int b = char_at(bp); + unsigned int s = fl_utf8decode(sp, 0, &l); + if (fl_tolower(b)!=fl_tolower(s)) + break; + sp += l; + bp = next_char(bp); + } + startPos = next_char(startPos); + } + } return 0; } int Fl_Text_Buffer_mod::search_backward(int startPos, const char *searchString, - int *foundPos, - int matchCase) const { + int *foundPos, int matchCase) const +{ + IS_UTF8_ALIGNED2(this, (startPos)) + IS_UTF8_ALIGNED(searchString) + if (!searchString) return 0; int bp; const char *sp; - while (startPos > 0) - { - bp = startPos - 1; - sp = searchString + strlen(searchString) - 1; - do { - if (sp < searchString) { - *foundPos = bp + 1; - return 1; + if (matchCase) { + while (startPos >= 0) { + bp = startPos; + sp = searchString; + for (;;) { + char c = *sp; + // we reached the end of the "needle", so we found the string! + if (!c) { + *foundPos = startPos; + return 1; + } + int l = fl_utf8len1(c); + if (memcmp(sp, address(bp), l)) + break; + sp += l; bp += l; } - // FIXME: character is ucs-4 - } while ((matchCase ? character(bp--) == *sp-- : - toupper(character(bp--)) == toupper(*sp--)) - && bp >= 0); - startPos--; - } + startPos = prev_char(startPos); + } + } else { + while (startPos >= 0) { + bp = startPos; + sp = searchString; + for (;;) { + // we reached the end of the "needle", so we found the string! + if (!*sp) { + *foundPos = startPos; + return 1; + } + int l; + unsigned int b = char_at(bp); + unsigned int s = fl_utf8decode(sp, 0, &l); + if (fl_tolower(b)!=fl_tolower(s)) + break; + sp += l; + bp = next_char(bp); + } + startPos = prev_char(startPos); + } + } return 0; } -int Fl_Text_Buffer_mod::findchars_forward(int startPos, - const char *searchChars, - int *foundPos) const { - int gapLen = mGapEnd - mGapStart; - const char *c; - - int pos = startPos; - while (pos < mGapStart) - { - for (c = searchChars; *c != '\0'; c++) { - if (mBuf[pos] == *c) { - *foundPos = pos; - return 1; - } - } pos++; - } - while (pos < mLength) { - for (c = searchChars; *c != '\0'; c++) { - if (mBuf[pos + gapLen] == *c) { - *foundPos = pos; - return 1; - } - } - pos++; - } - *foundPos = mLength; - return 0; -} -int Fl_Text_Buffer_mod::findchars_backward(int startPos, - const char *searchChars, - int *foundPos) const { - int gapLen = mGapEnd - mGapStart; - const char *c; - - if (startPos == 0) - { - *foundPos = 0; - return 0; - } - int pos = startPos == 0 ? 0 : startPos - 1; - while (pos >= mGapStart) { - for (c = searchChars; *c != '\0'; c++) { - if (mBuf[pos + gapLen] == *c) { - *foundPos = pos; - return 1; - } - } - pos--; - } - while (pos >= 0) { - for (c = searchChars; *c != '\0'; c++) { - if (mBuf[pos] == *c) { - *foundPos = pos; - return 1; - } - } - pos--; - } - *foundPos = 0; - return 0; -} +/* + Insert a string into the buffer. + Pos must be at a character boundary. Text must be a correct UTF-8 string. + */ int Fl_Text_Buffer_mod::insert_(int pos, const char *text) { + if (!text || !*text) + return 0; + int insertedLength = strlen(text); /* Prepare the buffer to receive the new text. If the new text fits in @@ -1287,6 +1159,11 @@ int Fl_Text_Buffer_mod::insert_(int pos, const char *text) return insertedLength; } + +/* + Remove a string from the buffer. + Unicode safe. Start and end must be at a character boundary. + */ void Fl_Text_Buffer_mod::remove_(int start, int end) { /* if the gap is not contiguous to the area to remove, move it there */ @@ -1334,485 +1211,92 @@ void Fl_Text_Buffer_mod::remove_(int start, int end) update_selections(start, end - start, 0); } -void Fl_Text_Buffer_mod::insert_column_(int column, int startPos, - const char *insText, int *nDeleted, - int *nInserted, int *endPos) + +/* + simple setter. + Unicode safe. Start and end must be at a character boundary. + */ +void Fl_Text_Selection_mod::set(int startpos, int endpos) { - if (column < 0) - column = 0; - - /* Allocate a buffer for the replacement string large enough to hold - possibly expanded tabs in both the inserted text and the replaced - area, as well as per line: 1) an additional 2*FL_TEXT_MAX_EXP_CHAR_LEN - characters for padding where tabs and control characters cross the - column of the selection, 2) up to "column" additional spaces per - line for padding out to the position of "column", 3) padding up - to the width of the inserted text if that must be padded to align - the text beyond the inserted column. (Space for additional - newlines if the inserted text extends beyond the end of the buffer - is counted with the length of insText) */ - int start = line_start(startPos); - int nLines = countLines(insText) + 1; - int insWidth = textWidth(insText, mTabDist); // this function probably returns a useless value - int end = line_end(skip_lines(start, nLines - 1)); - int expReplLen, expInsLen, len, endOffset; - const char *replText = text_range(start, end); - char *expText = expandTabs(replText, 0, mTabDist, &expReplLen); - free((void *) replText); - free((void *) expText); - expText = expandTabs(insText, 0, mTabDist, &expInsLen); - free((void *) expText); - char *outStr = (char *) malloc(expReplLen + expInsLen + - nLines * (column + insWidth + - FL_TEXT_MAX_EXP_CHAR_LEN) + 1); - - /* Loop over all lines in the buffer between start and end removing the - text between rectStart and rectEnd and padding appropriately. Trim - trailing space from line (whitespace at the ends of lines otherwise - tends to multiply, since additional padding is added to maintain it */ - char *outPtr = outStr, *insLine; - const char *insPtr = insText, *line; - for (int lineStart = start, lineEnd;;) { - lineEnd = line_end(lineStart); - line = text_range(lineStart, lineEnd); - insLine = copyLine(insPtr, &len); - insPtr += len; - insertColInLine(line, insLine, column, insWidth, mTabDist, - mUseTabs, outPtr, &len, &endOffset); - free((void *) line); - free((void *) insLine); - for (const char *c = outPtr + len - 1; c > outPtr && isspace(*c); c--) - len--; - outPtr += len; - *outPtr++ = '\n'; - lineStart = lineEnd < mLength ? lineEnd + 1 : mLength; - if (*insPtr == '\0') - break; - insPtr++; - } - if (outPtr != outStr) - outPtr--; /* trim back off extra newline */ - *outPtr = '\0'; - - /* replace the text between start and end with the new stuff */ - remove_(start, end); - insert_(start, outStr); - *nInserted = outPtr - outStr; - *nDeleted = end - start; - *endPos = start + (outPtr - outStr) - len + endOffset; - free((void *) outStr); + mSelected = startpos != endpos; + mStart = min(startpos, endpos); + mEnd = max(startpos, endpos); } -void Fl_Text_Buffer_mod::remove_rectangular_(int start, int end, int rectStart, - int rectEnd, int *replaceLen, - int *endPos) -{ - /* allocate a buffer for the replacement string large enough to hold - possibly expanded tabs as well as an additional FL_TEXT_MAX_EXP_CHAR_LEN * 2 - characters per line for padding where tabs and control characters cross - the edges of the selection */ - start = line_start(start); - end = line_end(end); - int nLines = count_lines(start, end) + 1; - const char *s = text_range(start, end); - int len; - char *expText = expandTabs(s, 0, mTabDist, &len); - free((void *) s); - free((void *) expText); - char *outStr = - (char *) malloc(len + nLines * FL_TEXT_MAX_EXP_CHAR_LEN * 2 + 1); - - /* loop over all lines in the buffer between start and end removing - the text between rectStart and rectEnd and padding appropriately */ - int endOffset = 0; - char *outPtr = outStr; - const char *line; - for (int lineStart = start, lineEnd; - lineStart <= mLength && lineStart <= end;) { - lineEnd = line_end(lineStart); - line = text_range(lineStart, lineEnd); - deleteRectFromLine(line, rectStart, rectEnd, mTabDist, - mUseTabs, outPtr, &len, &endOffset); - free((void *) line); - outPtr += len; - *outPtr++ = '\n'; - lineStart = lineEnd + 1; - } - if (outPtr != outStr) - outPtr--; /* trim back off extra newline */ - *outPtr = '\0'; - - /* replace the text between start and end with the newly created string */ - remove_(start, end); - insert_(start, outStr); - *replaceLen = outPtr - outStr; - *endPos = start + (outPtr - outStr) - len + endOffset; - free((void *) outStr); -} - -void Fl_Text_Buffer_mod::overlay_rectangular_(int startPos, int rectStart, - int rectEnd, const char *insText, - int *nDeleted, int *nInserted, - int *endPos) -{ - - /* Allocate a buffer for the replacement string large enough to hold - possibly expanded tabs in the inserted text, as well as per line: 1) - an additional 2*FL_TEXT_MAX_EXP_CHAR_LEN characters for padding where tabs - and control characters cross the column of the selection, 2) up to - "column" additional spaces per line for padding out to the position - of "column", 3) padding up to the width of the inserted text if that - must be padded to align the text beyond the inserted column. (Space - for additional newlines if the inserted text extends beyond the end - of the buffer is counted with the length of insText) */ - int start = line_start(startPos); - int nLines = countLines(insText) + 1; - int end = line_end(skip_lines(start, nLines - 1)), expInsLen; - char *expText = expandTabs(insText, 0, mTabDist, &expInsLen); - free((void *) expText); - char *outStr = (char *) malloc(end - start + expInsLen + - nLines * (rectEnd + - FL_TEXT_MAX_EXP_CHAR_LEN) + 1); - - /* Loop over all lines in the buffer between start and end overlaying the - text between rectStart and rectEnd and padding appropriately. Trim - trailing space from line (whitespace at the ends of lines otherwise - tends to multiply, since additional padding is added to maintain it */ - int len, endOffset; - char *outPtr = outStr, *insLine; - const char *insPtr = insText, *line; - for (int lineStart = start, lineEnd;;) { - lineEnd = line_end(lineStart); - line = text_range(lineStart, lineEnd); - insLine = copyLine(insPtr, &len); - insPtr += len; - overlayRectInLine(line, insLine, rectStart, rectEnd, mTabDist, - mUseTabs, outPtr, &len, &endOffset); - free((void *) line); - free((void *) insLine); - for (const char *c = outPtr + len - 1; c > outPtr && isspace(*c); c--) - len--; - outPtr += len; - *outPtr++ = '\n'; - lineStart = lineEnd < mLength ? lineEnd + 1 : mLength; - if (*insPtr == '\0') - break; - insPtr++; - } - if (outPtr != outStr) - outPtr--; /* trim back off extra newline */ - *outPtr = '\0'; - - /* replace the text between start and end with the new stuff */ - remove_(start, end); - insert_(start, outStr); - *nInserted = outPtr - outStr; - *nDeleted = end - start; - *endPos = start + (outPtr - outStr) - len + endOffset; - free((void *) outStr); -} /* - Inserts characters from single-line string \p insLine in single-line string - \p line at \p column, leaving \p insWidth space before continuing line. - \p outLen returns the number of characters written to \p outStr, \p endOffset - returns the number of characters from the beginning of the string to - the right edge of the inserted text (as a hint for routines which need - to position the cursor). + simple getter. + Unicode safe. Start and end will be at a character boundary. */ -static void insertColInLine(const char *line, char *insLine, int column, - int insWidth, int tabDist, int useTabs, - char *outStr, int *outLen, - int *endOffset) -{ - /* copy the line up to "column" */ - char *outPtr = outStr; - int indent = 0, len; - const char *linePtr; +int Fl_Text_Selection_mod::position(int *startpos, int *endpos) const { + if (!mSelected) + return 0; + *startpos = mStart; + *endpos = mEnd; - for (linePtr = line; *linePtr != '\0'; linePtr++) { - len = - Fl_Text_Buffer_mod::character_width(linePtr, indent, tabDist); - if (indent + len > column) - break; - indent += len; - *outPtr++ = *linePtr; - } - - /* If "column" falls in the middle of a character, and the character is a - tab, leave it off and leave the indent short and it will get padded - later. If it's a control character, insert it and adjust indent - accordingly. */ - int postColIndent; - if (indent < column && *linePtr != '\0') { - postColIndent = indent + len; - if (*linePtr == '\t') - linePtr++; - else { - *outPtr++ = *linePtr++; - indent += len; - } - } else - postColIndent = indent; - - /* If there's no text after the column and no text to insert, that's all */ - if (*insLine == '\0' && *linePtr == '\0') { - *outLen = *endOffset = outPtr - outStr; - return; - } - - /* pad out to column if text is too short */ - if (indent < column) { - addPadding(outPtr, indent, column, tabDist, useTabs, &len); - outPtr += len; - indent = column; - } - - /* Copy the text from "insLine" (if any), recalculating the tabs as if - the inserted string began at column 0 to its new column destination */ - if (*insLine != '\0') { - char *retabbedStr = realignTabs(insLine, 0, indent, tabDist, useTabs, - &len); - for (const char *c = retabbedStr; *c != '\0'; c++) { - *outPtr++ = *c; - len = - Fl_Text_Buffer_mod::character_width(c, indent, tabDist); - indent += len; - } - free((void *) retabbedStr); - } - - /* If the original line did not extend past "column", that's all */ - if (*linePtr == '\0') { - *outLen = *endOffset = outPtr - outStr; - return; - } - - /* Pad out to column + width of inserted text + (additional original - offset due to non-breaking character at column) */ - int toIndent = column + insWidth + postColIndent - column; - addPadding(outPtr, indent, toIndent, tabDist, useTabs, &len); - outPtr += len; - indent = toIndent; - - /* realign tabs for text beyond "column" and write it out */ - char *retabbedStr = realignTabs(linePtr, postColIndent, indent, tabDist, - useTabs, &len); - strcpy(outPtr, retabbedStr); - free((void *) retabbedStr); - *endOffset = outPtr - outStr; - *outLen = (outPtr - outStr) + len; -} + return 1; +} -/** - Removes characters in single-line string \p line between displayed positions - \p rectStart and \p rectEnd, and write the result to \p outStr, which is - assumed to be large enough to hold the returned string. Note that in - certain cases, it is possible for the string to get longer due to - expansion of tabs. \p endOffset returns the number of characters from - the beginning of the string to the point where the characters were - deleted (as a hint for routines which need to position the cursor). - */ -static void deleteRectFromLine(const char *line, int rectStart, - int rectEnd, int tabDist, int useTabs, - char *outStr, - int *outLen, int *endOffset) -{ - - /* copy the line up to rectStart */ - char *outPtr = outStr; - int indent = 0, len; - const char *c; - for (c = line; *c != '\0'; c++) { - if (indent > rectStart) - break; - len = - Fl_Text_Buffer_mod::character_width(c, indent, tabDist); - if (indent + len > rectStart && (indent == rectStart || *c == '\t')) - break; - indent += len; - *outPtr++ = *c; - } - int preRectIndent = indent; - - /* skip the characters between rectStart and rectEnd */ - for (; *c != '\0' && indent < rectEnd; c++) - indent += - Fl_Text_Buffer_mod::character_width(c, indent, tabDist); - int postRectIndent = indent; - - /* If the line ended before rectEnd, there's nothing more to do */ - if (*c == '\0') { - *outPtr = '\0'; - *outLen = *endOffset = outPtr - outStr; - return; - } - - /* fill in any space left by removed tabs or control characters - which straddled the boundaries */ - indent = max(rectStart + postRectIndent - rectEnd, preRectIndent); - addPadding(outPtr, preRectIndent, indent, tabDist, useTabs, &len); - outPtr += len; - - /* Copy the rest of the line. If the indentation has changed, preserve - the position of non-whitespace characters by converting tabs to - spaces, then back to tabs with the correct offset */ - char *retabbedStr = - realignTabs(c, postRectIndent, indent, tabDist, useTabs, &len); - strcpy(outPtr, retabbedStr); - free((void *) retabbedStr); - *endOffset = outPtr - outStr; - *outLen = (outPtr - outStr) + len; -} -/** - Overlay characters from single-line string \p insLine on single-line string - \p line between displayed character offsets \p rectStart and \p rectEnd. - \p outLen returns the number of characters written to \p outStr, \p endOffset - returns the number of characters from the beginning of the string to - the right edge of the inserted text (as a hint for routines which need - to position the cursor). +/* + Return if a position is inside the selected area. + Unicode safe. Pos must be at a character boundary. */ -static void overlayRectInLine(const char *line, char *insLine, - int rectStart, int rectEnd, int tabDist, - int useTabs, char *outStr, - int *outLen, int *endOffset) -{ - /* copy the line up to "rectStart" */ - char *outPtr = outStr; - int inIndent = 0, outIndent = 0, len; - const char *linePtr = line; - - for (; *linePtr != '\0'; linePtr++) { - len = - Fl_Text_Buffer_mod::character_width(linePtr, inIndent, tabDist); - if (inIndent + len > rectStart) - break; - inIndent += len; - outIndent += len; - *outPtr++ = *linePtr; - } - - /* If "rectStart" falls in the middle of a character, and the character - is a tab, leave it off and leave the outIndent short and it will get - padded later. If it's a control character, insert it and adjust - outIndent accordingly. */ - if (inIndent < rectStart && *linePtr != '\0') { - if (*linePtr == '\t') { - linePtr++; - inIndent += len; - } else { - *outPtr++ = *linePtr++; - outIndent += len; - inIndent += len; - } - } - - /* skip the characters between rectStart and rectEnd */ - int postRectIndent = rectEnd; - for (; *linePtr != '\0'; linePtr++) { - inIndent += - Fl_Text_Buffer_mod::character_width(linePtr, inIndent, tabDist); - if (inIndent >= rectEnd) { - linePtr++; - postRectIndent = inIndent; - break; - } - } - - /* If there's no text after rectStart and no text to insert, that's all */ - if (*insLine == '\0' && *linePtr == '\0') { - *outLen = *endOffset = outPtr - outStr; - return; - } - - /* pad out to rectStart if text is too short */ - if (outIndent < rectStart) { - addPadding(outPtr, outIndent, rectStart, tabDist, useTabs, &len); - outPtr += len; - } - outIndent = rectStart; - - /* Copy the text from "insLine" (if any), recalculating the tabs as if - the inserted string began at column 0 to its new column destination */ - if (*insLine != '\0') { - char *retabbedStr = - realignTabs(insLine, 0, rectStart, tabDist, useTabs, &len); - for (const char *c = retabbedStr; *c != '\0'; c++) { - *outPtr++ = *c; - len = - Fl_Text_Buffer_mod::character_width(c, outIndent, tabDist); - outIndent += len; - } - free((void *) retabbedStr); - } - - /* If the original line did not extend past "rectStart", that's all */ - if (*linePtr == '\0') { - *outLen = *endOffset = outPtr - outStr; - return; - } - - /* Pad out to rectEnd + (additional original offset - due to non-breaking character at right boundary) */ - addPadding(outPtr, outIndent, postRectIndent, tabDist, useTabs, &len); - outPtr += len; - outIndent = postRectIndent; - - /* copy the text beyond "rectEnd" */ - strcpy(outPtr, linePtr); - *endOffset = outPtr - outStr; - *outLen = (outPtr - outStr) + strlen(linePtr); +int Fl_Text_Selection_mod::includes(int pos) const { + return (selected() && pos >= start() && pos < end() ); } -char *Fl_Text_Buffer_mod::selection_text_(Fl_Text_Selection * sel) const { - int start, end, isRect, rectStart, rectEnd; +/* + Return a duplicate of the selected text, or an empty string. + Unicode safe. + */ +char *Fl_Text_Buffer_mod::selection_text_(Fl_Text_Selection_mod * sel) const { + int start, end; /* If there's no selection, return an allocated empty string */ - if (!sel->position(&start, &end, &isRect, &rectStart, &rectEnd)) + if (!sel->position(&start, &end)) { char *s = (char *) malloc(1); *s = '\0'; return s; } - /* If the selection is not rectangular, return the selected range */ - if (isRect) - return text_in_rectangle(start, end, rectStart, rectEnd); - else + /* Return the selected range */ return text_range(start, end); } -void Fl_Text_Buffer_mod::remove_selection_(Fl_Text_Selection * sel) + +/* + Remove the selected text. + Unicode safe. + */ +void Fl_Text_Buffer_mod::remove_selection_(Fl_Text_Selection_mod * sel) { - int start, end, isRect, rectStart, rectEnd; + int start, end; - if (!sel->position(&start, &end, &isRect, &rectStart, &rectEnd)) + if (!sel->position(&start, &end)) return; - if (isRect) - remove_rectangular(start, end, rectStart, rectEnd); - else { - remove(start, end); - //undoyankcut = undocut; - } + remove(start, end); + //undoyankcut = undocut; } -void Fl_Text_Buffer_mod::replace_selection_(Fl_Text_Selection * sel, +/* + Replace selection with text. + Unicode safe. + */ +void Fl_Text_Buffer_mod::replace_selection_(Fl_Text_Selection_mod * sel, const char *text) { - Fl_Text_Selection oldSelection = *sel; + Fl_Text_Selection_mod oldSelection = *sel; /* If there's no selection, return */ - int start, end, isRect, rectStart, rectEnd; - if (!sel->position(&start, &end, &isRect, &rectStart, &rectEnd)) + int start, end; + if (!sel->position(&start, &end)) return; /* Do the appropriate type of replace */ - if (isRect) - replace_rectangular(start, end, rectStart, rectEnd, text); - else replace(start, end, text); /* Unselect (happens automatically in BufReplace, but BufReplaceRect @@ -1821,45 +1305,38 @@ void Fl_Text_Buffer_mod::replace_selection_(Fl_Text_Selection * sel, redisplay_selection(&oldSelection, sel); } -static void addPadding(char *string, int startIndent, int toIndent, - int tabDist, int useTabs, int *charsAdded) -{ - int indent = startIndent, len; - char *outPtr = string; - if (useTabs) { - while (indent < toIndent) { - static char t = '\t'; - len = Fl_Text_Buffer_mod::character_width(&t, indent, tabDist); - if (len > 1 && indent + len <= toIndent) { - *outPtr++ = '\t'; - indent += len; - } else { - *outPtr++ = ' '; - indent++; - } - } - } else { - while (indent < toIndent) { - *outPtr++ = ' '; - indent++; - } - } - *charsAdded = outPtr - string; -} - +/* + Call all callbacks. + Unicode safe. + */ void Fl_Text_Buffer_mod::call_modify_callbacks(int pos, int nDeleted, int nInserted, int nRestyled, const char *deletedText) const { + IS_UTF8_ALIGNED2(this, pos) for (int i = 0; i < mNModifyProcs; i++) (*mModifyProcs[i]) (pos, nInserted, nDeleted, nRestyled, deletedText, mCbArgs[i]); -} void Fl_Text_Buffer_mod::call_predelete_callbacks(int pos, int nDeleted) const { +} + + +/* + Call all callbacks. + Unicode safe. + */ +void Fl_Text_Buffer_mod::call_predelete_callbacks(int pos, int nDeleted) const { for (int i = 0; i < mNPredeleteProcs; i++) (*mPredeleteProcs[i]) (pos, nDeleted, mPredeleteCbArgs[i]); -} void Fl_Text_Buffer_mod::redisplay_selection(Fl_Text_Selection * +} + + +/* + Redisplay a new selected area. + Unicode safe. + */ +void Fl_Text_Buffer_mod::redisplay_selection(Fl_Text_Selection_mod * oldSelection, - Fl_Text_Selection * + Fl_Text_Selection_mod * newSelection) const { int oldStart, oldEnd, newStart, newEnd, ch1Start, ch1End, ch2Start, @@ -1872,10 +1349,6 @@ void Fl_Text_Buffer_mod::call_modify_callbacks(int pos, int nDeleted, newStart = newSelection->mStart; oldEnd = oldSelection->mEnd; newEnd = newSelection->mEnd; - if (oldSelection->mRectangular) - oldEnd++; - if (newSelection->mRectangular) - newEnd++; /* If the old or new selection is unselected, just redisplay the single area that is (was) selected and return */ @@ -1891,20 +1364,6 @@ void Fl_Text_Buffer_mod::call_modify_callbacks(int pos, int nDeleted, return; } - /* If the selection changed from normal to rectangular or visa versa, or - if a rectangular selection changed boundaries, redisplay everything */ - if ((oldSelection->mRectangular && !newSelection->mRectangular) || - (!oldSelection->mRectangular && newSelection->mRectangular) || - (oldSelection->mRectangular && ((oldSelection->mRectStart != - newSelection->mRectStart) - || (oldSelection->mRectEnd != - newSelection->mRectEnd)))) { - call_modify_callbacks(min(oldStart, newStart), 0, 0, - max(oldEnd, newEnd) - min(oldStart, - newStart), NULL); - return; - } - /* If the selections are non-contiguous, do two separate updates and return */ if (oldEnd < newStart || newEnd < oldStart) { @@ -1926,6 +1385,11 @@ void Fl_Text_Buffer_mod::call_modify_callbacks(int pos, int nDeleted, call_modify_callbacks(ch2Start, 0, 0, ch2End - ch2Start, NULL); } + +/* + Move the gap around without changing buffer content. + Unicode safe. Pos must be at a character boundary. + */ void Fl_Text_Buffer_mod::move_gap(int pos) { int gapLen = mGapEnd - mGapStart; @@ -1938,6 +1402,11 @@ void Fl_Text_Buffer_mod::move_gap(int pos) mGapStart += pos - mGapStart; } + +/* + Create a larger gap. + Unicode safe. Start must be at a character boundary. + */ void Fl_Text_Buffer_mod::reallocate_with_gap(int newGapStart, int newGapLen) { char *newBuf = (char *) malloc(mLength + newGapLen); @@ -1960,15 +1429,13 @@ void Fl_Text_Buffer_mod::reallocate_with_gap(int newGapStart, int newGapLen) mBuf = newBuf; mGapStart = newGapStart; mGapEnd = newGapEnd; -#ifdef PURIFY - { - int i; - for (i = mGapStart; i < mGapEnd; i++) - mBuf[i] = '.'; } -#endif -} + +/* + Update selection range if characters were inserted. + Unicode safe. Pos must be at a character boundary. + */ void Fl_Text_Buffer_mod::update_selections(int pos, int nDeleted, int nInserted) { @@ -1978,303 +1445,262 @@ void Fl_Text_Buffer_mod::update_selections(int pos, int nDeleted, } -int Fl_Text_Buffer_mod::findchar_forward(int startPos, char searchChar, - int *foundPos) const { - if (startPos < 0 || startPos >= mLength) - { +// unicode safe, assuming the arguments are on character boundaries +void Fl_Text_Selection_mod::update(int pos, int nDeleted, int nInserted) +{ + if (!mSelected || pos > mEnd) + return; + if (pos + nDeleted <= mStart) { + mStart += nInserted - nDeleted; + mEnd += nInserted - nDeleted; + } else if (pos <= mStart && pos + nDeleted >= mEnd) { + mStart = pos; + mEnd = pos; + mSelected = 0; + } else if (pos <= mStart && pos + nDeleted < mEnd) { + mStart = pos; + mEnd = nInserted + mEnd - nDeleted; + } else if (pos < mEnd) { + mEnd += nInserted - nDeleted; + if (mEnd <= mStart) + mSelected = 0; + } +} + + +/* + Find a UCS-4 character. + StartPos must be at a character boundary, searchChar is UCS-4 encoded. + */ +int Fl_Text_Buffer_mod::findchar_forward(int startPos, unsigned searchChar, + int *foundPos) const +{ + if (startPos >= mLength) { *foundPos = mLength; return 0; } - int pos = startPos; - while (pos < mGapStart) { - if (mBuf[pos] == searchChar) { - *foundPos = pos; + if (startPos<0) + startPos = 0; + + for ( ; startPos mLength) - { +/* + Find a UCS-4 character. + StartPos must be at a character boundary, searchChar is UCS-4 encoded. + */ +int Fl_Text_Buffer_mod::findchar_backward(int startPos, unsigned int searchChar, + int *foundPos) const { + if (startPos <= 0) { *foundPos = 0; return 0; } - int pos = startPos - 1; - for (int gapLen = mGapEnd - mGapStart; pos >= mGapStart; pos--) { - if (mBuf[pos + gapLen] == searchChar) { - *foundPos = pos; + if (startPos > mLength) + startPos = mLength; + + for (startPos = prev_char(startPos); startPos>=0; startPos = prev_char(startPos)) { + if (searchChar == char_at(startPos)) { + *foundPos = startPos; return 1; } } - for (; pos >= 0; pos--) { - if (mBuf[pos] == searchChar) { - *foundPos = pos; - return 1; - } - } *foundPos = 0; return 0; } -/* - Copies from \p text to end up to but not including newline (or end of \p text) - and return the copy as the function value, and the length of the line in - \p lineLen - */ -static char *copyLine(const char *text, int *lineLen) +//#define EXAMPLE_ENCODING // shows how to process any encoding for which a decoding function exists +#ifdef EXAMPLE_ENCODING + +// returns the UCS equivalent of *p in CP1252 and advances p by 1 +unsigned cp1252toucs(char* &p) { - int len = 0; - - for (const char *c = text; *c != '\0' && *c != '\n'; c++) - len++; - char *outStr = (char *) malloc(len + 1); - strlcpy(outStr, text, len + 1); - *lineLen = len; - return outStr; + // Codes 0x80..0x9f from the Microsoft CP1252 character set, translated + // to Unicode + static unsigned cp1252[32] = { + 0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021, + 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x017d, 0x008f, + 0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, + 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x017e, 0x0178 + }; + unsigned char uc = *(unsigned char*)p; + p++; + return (uc < 0x80 || uc >= 0xa0 ? uc : cp1252[uc - 0x80]); } -/* - Counts the number of newlines in a null-terminated text string. - Unicode tested. - */ -static int countLines(const char *string) +// returns the UCS equivalent of *p in UTF-16 and advances p by 2 (or more for surrogates) +unsigned utf16toucs(char* &p) { - int lineCount = 0; - - for (const char *c = string; *c != '\0'; c++) - if (*c == '\n') - lineCount++; - return lineCount; + union { +#if WORDS_BIGENDIAN + struct { unsigned char a, b;} chars; +#else + struct { unsigned char b, a;} chars; +#endif + U16 short_val; + } u; + u.chars.a = *(unsigned char*)p++; + u.chars.b = *(unsigned char*)p++; + return u.short_val; } -/* - Measures the width in displayed characters of string \p text - */ -static int textWidth(const char *text, int tabDist) +// filter that produces, from an input stream fed by reading from fp, +// a UTF-8-encoded output stream written in buffer. +// Input can be any (e.g., 8-bit, UTF-16) encoding. +// Output is true UTF-8. +// p_trf points to a function that transforms encoded byte(s) into one UCS +// and that increases the pointer by the adequate quantity +static int general_input_filter(char *buffer, int buflen, + char *line, int sline, char* &endline, + unsigned (*p_trf)(char* &), + FILE *fp) { - int width = 0, maxWidth = 0; - - // HUH? Why is "c" incremented? Shouldn't "text" be incrmented? - // FIXME: increment is wrong! - for (const char *c = text; *c != '\0'; c++) { - if (*c == '\n') { - if (width > maxWidth) - maxWidth = width; - width = 0; - } else - width += Fl_Text_Buffer_mod::character_width(c, width, tabDist); + char *p, *q, multibyte[5]; + int lq, r, offset; + p = endline = line; + q = buffer; + while (q < buffer + buflen) { + if (p >= endline) { + r = fread(line, 1, sline, fp); + endline = line + r; + if (r == 0) return q - buffer; + p = line; + } + if (q + 4 /*max width of utf-8 char*/ > buffer + buflen) { + memmove(line, p, endline - p); + endline -= (p - line); + return q - buffer; + } + lq = fl_utf8encode( p_trf(p), multibyte ); + memcpy(q, multibyte, lq); + q += lq; } - if (width > maxWidth) - return width; - return maxWidth; + memmove(line, p, endline - p); + endline -= (p - line); + return q - buffer; } +#endif // EXAMPLE_ENCODING -void Fl_Text_Buffer_mod::rectangular_selection_boundaries(int lineStartPos, - int rectStart, - int rectEnd, - int *selStart, - int *selEnd) const +/* + filter that produces, from an input stream fed by reading from fp, + a UTF-8-encoded output stream written in buffer. + Input can be UTF-8. If it is not, it is decoded with CP1252. + Output is UTF-8. + *input_was_changed is set to true if the input was not strict UTF-8 so output + differs from input. + */ +static int utf8_input_filter(char *buffer, int buflen, char *line, int sline, char* &endline, + FILE *fp, int *input_was_changed) { - int pos, width, indent = 0; - char c; - - /* find the start of the selection */ - for (pos = lineStartPos; pos < mLength; pos++) - { - // FIXME: character is ucs-4 - c = character(pos); - if (c == '\n') - break; - width = - Fl_Text_Buffer_mod::character_width(&c, indent, mTabDist); // FIXME: c is not unicode - if (indent + width > rectStart) { - if (indent != rectStart && c != '\t') { - pos++; - indent += width; + char *p, *q, multibyte[5]; + int l, lp, lq, r; + unsigned u; + p = endline = line; + q = buffer; + while (q < buffer + buflen) { + if (p >= endline) { + r = fread(line, 1, sline, fp); + endline = line + r; + if (r == 0) return q - buffer; + p = line; + } + l = fl_utf8len1(*p); + if (p + l > endline) { + memmove(line, p, endline - p); + endline -= (p - line); + r = fread(endline, 1, sline - (endline - line), fp); + endline += r; + p = line; + if (endline - line < l) break; + } + while ( l > 0) { + u = fl_utf8decode(p, p+l, &lp); + lq = fl_utf8encode(u, multibyte); + if (lp != l || lq != l) *input_was_changed = true; + if (q + lq > buffer + buflen) { + memmove(line, p, endline - p); + endline -= (p - line); + return q - buffer; } - break; - } - indent += width; - } - *selStart = pos; - - /* find the end */ - for (; pos < mLength; pos++) { - // FIXME: character is ucs-4 - c = character(pos); - if (c == '\n') - break; - width = - Fl_Text_Buffer_mod::character_width(&c, indent, mTabDist); // FIXME: c is not unicode - indent += width; - if (indent > rectEnd) { - if (indent - width != rectEnd && c != '\t') - pos++; - break; + memcpy(q, multibyte, lq); + q += lq; + p += lp; + l -= lp; } } - *selEnd = pos; + memmove(line, p, endline - p); + endline -= (p - line); + return q - buffer; } +const char *Fl_Text_Buffer_mod::file_encoding_warning_message = +"Displayed text contains the UTF-8 transcoding\n" +"of the input file which was not UTF-8 encoded.\n" +"Some changes may have occurred."; + /* - Adjust the space and tab characters from string \p text so that non-white - characters remain stationary when the text is shifted from starting at - \p origIndent to starting at \p newIndent. Returns an allocated string - which must be freed by the caller with XtFree. + Insert text from a file. + Input file can be of various encodings according to what input fiter is used. + utf8_input_filter accepts UTF-8 or CP1252 as input encoding. + Output is always UTF-8. */ -static char *realignTabs(const char *text, int origIndent, int newIndent, - int tabDist, int useTabs, int *newLength) -{ - /* If the tabs settings are the same, retain original tabs */ - int len; - char *outStr; - if (origIndent % tabDist == newIndent % tabDist) { - len = strlen(text); - outStr = (char *) malloc(len + 1); - strcpy(outStr, text); - *newLength = len; - return outStr; - } - - /* If the tab settings are not the same, brutally convert tabs to - spaces, then back to tabs in the new position */ - char *expStr = expandTabs(text, origIndent, tabDist, &len); - if (!useTabs) { - *newLength = len; - return expStr; - } - outStr = - unexpandTabs(expStr, newIndent, tabDist, newLength); - free((void *) expStr); - return outStr; -} - -/* - Expand tabs to spaces for a block of text. The additional parameter - \p startIndent if nonzero, indicates that the text is a rectangular selection - beginning at column \p startIndent - */ -static char *expandTabs(const char *text, int startIndent, int tabDist, int *newLen) -{ - /* rehearse the expansion to figure out length for output string */ - int indent = startIndent, len, outLen = 0; - const char *c; - for (c = text; *c != '\0'; c++) { - if (*c == '\t') { - len = - Fl_Text_Buffer_mod::character_width(c, indent, tabDist); - outLen += len; - indent += len; - } else if (*c == '\n') { - indent = startIndent; - outLen++; - } else { - indent += - Fl_Text_Buffer_mod::character_width(c, indent, tabDist); - outLen++; - } - } - - /* do the expansion */ - char *outStr = (char *) malloc(outLen + 1); - char *outPtr = outStr; - indent = startIndent; - for (c = text; *c != '\0'; c++) { - if (*c == '\t') { - len = - Fl_Text_Buffer_mod::expand_character(c, indent, outPtr, tabDist); - outPtr += len; - indent += len; - } else if (*c == '\n') { - indent = startIndent; - *outPtr++ = *c; - } else { - indent += - Fl_Text_Buffer_mod::character_width(c, indent, tabDist); - *outPtr++ = *c; - } - } - outStr[outLen] = '\0'; - *newLen = outLen; - return outStr; -} - -/* - Convert sequences of spaces into tabs. The threshold for conversion is - when 3 or more spaces can be converted into a single tab, this avoids - converting double spaces after a period withing a block of text. - */ -static char *unexpandTabs(char *text, int startIndent, int tabDist, int *newLen) -{ - char expandedChar[FL_TEXT_MAX_EXP_CHAR_LEN]; - char *outStr = (char *) malloc(strlen(text) + 1); - char *outPtr = outStr; - int indent = startIndent, len; - - for (const char *c = text; *c != '\0';) { - if (*c == ' ') { - static char tab = '\t'; - len = - Fl_Text_Buffer_mod::expand_character(&tab, indent, expandedChar, tabDist); - if (len >= 3 && !strncmp(c, expandedChar, len)) { - c += len; - *outPtr++ = '\t'; - indent += len; - } else { - *outPtr++ = *c++; - indent++; - } - } else if (*c == '\n') { - indent = startIndent; - *outPtr++ = *c++; - } else { - *outPtr++ = *c++; - indent++; - } - } - *outPtr = '\0'; - *newLen = outPtr - outStr; - return outStr; -} - -int Fl_Text_Buffer_mod::insertfile(const char *file, int pos, int buflen) + int Fl_Text_Buffer_mod::insertfile(const char *file, int pos, int buflen) { FILE *fp; if (!(fp = fl_fopen(file, "r"))) return 1; - char *buffer = new char[buflen]; - for (int r; (r = fread(buffer, 1, buflen - 1, fp)) > 0; pos += r) { - buffer[r] = (char) 0; + char *buffer = new char[buflen + 1]; + char *endline, line[100]; + int l; + input_file_was_transcoded = false; + endline = line; + while (true) { +#ifdef EXAMPLE_ENCODING + // example of 16-bit encoding: UTF-16 + l = general_input_filter(buffer, buflen, + line, sizeof(line), endline, + utf16toucs, // use cp1252toucs to read CP1252-encoded files + fp); + input_file_was_transcoded = true; +#else + l = utf8_input_filter(buffer, buflen, line, sizeof(line), endline, + fp, &input_file_was_transcoded); +#endif + if (l == 0) break; + buffer[l] = 0; insert(pos, buffer); - } - + pos += l; + } int e = ferror(fp) ? 2 : 0; fclose(fp); delete[]buffer; + if ( (!e) && input_file_was_transcoded && transcoding_warning_action) { + transcoding_warning_action(this); + } return e; } -int Fl_Text_Buffer_mod::outputfile(const char *file, int start, int end, - int buflen) -{ + +/* + Write text to file. + Unicode safe. + */ +int Fl_Text_Buffer_mod::outputfile(const char *file, + int start, int end, + int buflen) { FILE *fp; - if (!(fp = fl_fopen(file, "wb"))) + if (!(fp = fl_fopen(file, "w"))) return 1; for (int n; (n = min(end - start, buflen)); start += n) { const char *p = text_range(start, start + n); @@ -2290,6 +1716,79 @@ int Fl_Text_Buffer_mod::outputfile(const char *file, int start, int end, } +/* + Return the previous character position. + Unicode safe. + */ +int Fl_Text_Buffer_mod::prev_char_clipped(int pos) const +{ + if (pos<=0) + return 0; + + IS_UTF8_ALIGNED2(this, (pos)) + + char c; + do { + pos--; + if (pos==0) + return 0; + c = byte_at(pos); + } while ( (c&0xc0) == 0x80); + + IS_UTF8_ALIGNED2(this, (pos)) + return pos; +} + + +/* + Return the previous character position. + Returns -1 if the beginning of the buffer is reached. + */ +int Fl_Text_Buffer_mod::prev_char(int pos) const +{ + if (pos==0) return -1; + return prev_char_clipped(pos); +} + + +/* + Return the next character position. + Returns length() if the end of the buffer is reached. + */ +int Fl_Text_Buffer_mod::next_char(int pos) const +{ + IS_UTF8_ALIGNED2(this, (pos)) + int n = fl_utf8len1(byte_at(pos)); + pos += n; + if (pos>=mLength) + return mLength; + IS_UTF8_ALIGNED2(this, (pos)) + return pos; +} + + +/* + Return the next character position. + If the end of the buffer is reached, it returns the current position. + */ +int Fl_Text_Buffer_mod::next_char_clipped(int pos) const +{ + return next_char(pos); +} + +/* + Align an index to the current UTF-8 boundary. + */ +int Fl_Text_Buffer_mod::utf8_align(int pos) const +{ + char c = byte_at(pos); + while ( (c&0xc0) == 0x80) { + pos--; + c = byte_at(pos); + } + return pos; +} + // -// End of "$Id: Fl_Text_Buffer.cxx 7462 2010-04-06 23:00:56Z matt $". +// End of "$Id: Fl_Text_Buffer_mod.cxx 8040 2010-12-15 17:38:39Z manolo $". // diff --git a/src/widgets/Fl_Text_Display_mod_1_3.cxx b/src/widgets/Fl_Text_Display_mod_1_3.cxx index 4abd904f..e6f0c444 100644 --- a/src/widgets/Fl_Text_Display_mod_1_3.cxx +++ b/src/widgets/Fl_Text_Display_mod_1_3.cxx @@ -1,7 +1,7 @@ // -// "$Id: Fl_Text_Display.cxx 7462 2010-04-06 23:00:56Z matt $" +// "$Id: Fl_Text_Display_mod.cxx 8808 2011-06-16 13:31:25Z manolo $" // -// Copyright 2001-2009 by Bill Spitzak and others. +// Copyright 2001-2010 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under // the LGPL for the FLTK library granted by Mark Edel. // @@ -25,6 +25,9 @@ // http://www.fltk.org/str.php // +// TODO: rendering of the "optional hyphen" +// TODO: make line numbering work again + #include #include #include @@ -32,9 +35,10 @@ #include #include #include +#include +#include #include "Fl_Text_Buffer_mod.H" #include "Fl_Text_Display_mod.H" -#include #undef min #undef max @@ -45,11 +49,15 @@ const int Fl_Text_Display_mod::DEFAULT_TOP_MARGIN = 1; const int Fl_Text_Display_mod::DEFAULT_BOTTOM_MARGIN = 1; const int Fl_Text_Display_mod::DEFAULT_LEFT_MARGIN = 3; const int Fl_Text_Display_mod::DEFAULT_RIGHT_MARGIN = 3; +//#define TOP_MARGIN 1 +//#define BOTTOM_MARGIN 1 +//#define LEFT_MARGIN 3 +//#define RIGHT_MARGIN 3 #define NO_HINT -1 /* Masks for text drawing methods. These are or'd together to form an - integer which describes what drawing calls to use to draw a string */ + integer which describes what drawing calls to use to draw a string */ #define FILL_MASK 0x0100 #define SECONDARY_MASK 0x0200 #define PRIMARY_MASK 0x0400 @@ -59,8 +67,8 @@ const int Fl_Text_Display_mod::DEFAULT_RIGHT_MARGIN = 3; #define STYLE_LOOKUP_MASK 0xff /* Maximum displayable line length (how many characters will fit across the - widest window). This amount of memory is temporarily allocated from the - stack in the draw_vline() method for drawing strings */ + widest window). This amount of memory is temporarily allocated from the + stack in the draw_vline() method for drawing strings */ #define MAX_DISP_LINE_LEN 1000 static int max( int i1, int i2 ); @@ -68,7 +76,7 @@ static int min( int i1, int i2 ); static int countlines( const char *string ); /* The variables below are used in a timer event to allow smooth - scrolling of the text area when the pointer has left the area. */ + scrolling of the text area when the pointer has left the area. */ static int scroll_direction = 0; static int scroll_amount = 0; static int scroll_y = 0; @@ -77,11 +85,18 @@ static int scroll_x = 0; // CET - FIXME #define TMPFONTWIDTH 6 -/** Creates a new text display widget.*/ -Fl_Text_Display_mod::Fl_Text_Display_mod(int X, int Y, int W, int H, const char* l) - : Fl_Group(X, Y, W, H, l) { - int i; + +/** + \brief Creates a new text display widget. + + \param X, Y, W, H position and size of widget + \param l label text, defaults to none + */ +Fl_Text_Display_mod::Fl_Text_Display_mod(int X, int Y, int W, int H, const char* l) +: Fl_Group(X, Y, W, H, l) { + int i; + TOP_MARGIN = DEFAULT_TOP_MARGIN; BOTTOM_MARGIN = DEFAULT_BOTTOM_MARGIN; LEFT_MARGIN = DEFAULT_LEFT_MARGIN; @@ -90,40 +105,40 @@ Fl_Text_Display_mod::Fl_Text_Display_mod(int X, int Y, int W, int H, const char mMaxsize = 0; damage_range1_start = damage_range1_end = -1; damage_range2_start = damage_range2_end = -1; - dragPos = dragType = dragging = 0; + dragPos = dragging = 0; + dragType = DRAG_CHAR; display_insert_position_hint = 0; shortcut_ = 0; - + color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR); -// box(FL_DOWN_FRAME); - box(FL_DOWN_BOX); // DO NOT USE FRAME OR TILED GROUP WILL NOT CORRECTLY DRAW THE BORDERS !! + box(FL_DOWN_FRAME); textsize(FL_NORMAL_SIZE); textcolor(FL_FOREGROUND_COLOR); textfont(FL_HELVETICA); set_flag(SHORTCUT_LABEL); - + text_area.x = 0; text_area.y = 0; text_area.w = 0; text_area.h = 0; - + mVScrollBar = new Fl_Scrollbar(0,0,1,1); mVScrollBar->callback((Fl_Callback*)v_scrollbar_cb, this); mHScrollBar = new Fl_Scrollbar(0,0,1,1); mHScrollBar->callback((Fl_Callback*)h_scrollbar_cb, this); mHScrollBar->type(FL_HORIZONTAL); - + end(); - + scrollbar_width(Fl::scrollbar_size()); scrollbar_align(FL_ALIGN_BOTTOM_RIGHT); - + mCursorOn = 0; mCursorPos = 0; mCursorOldY = -100; mCursorToHint = NO_HINT; mCursorStyle = NORMAL_CURSOR; - mCursorPreferredCol = -1; + mCursorPreferredXPos = -1; mBuffer = 0; mFirstChar = 0; mLastChar = 0; @@ -132,10 +147,9 @@ Fl_Text_Display_mod::Fl_Text_Display_mod(int X, int Y, int W, int H, const char mAbsTopLineNum = 1; mNeedAbsTopLineNum = 0; mHorizOffset = mHorizOffsetHint = 0; - + mCursor_color = FL_FOREGROUND_COLOR; - - mFixedFontWidth = -1; + mStyleBuffer = 0; mStyleTable = 0; mNStyles = 0; @@ -147,92 +161,128 @@ Fl_Text_Display_mod::Fl_Text_Display_mod(int X, int Y, int W, int H, const char mSuppressResync = 0; mNLinesDeleted = 0; mModifyingTabDistance = 0; - + mUnfinishedStyle = 0; mUnfinishedHighlightCB = 0; mHighlightCBArg = 0; - + mLineNumLeft = mLineNumWidth = 0; mContinuousWrap = 0; - mWrapMargin = 0; + mFastDisplay = 0; + s_text.clear(); + s_style.clear(); + mWrapMarginPix = 0; mSuppressResync = mNLinesDeleted = mModifyingTabDistance = 0; } -/** Free a text display and release its associated memory. Note, the text - BUFFER that the text display displays is a separate entity and is not - freed, nor are the style buffer or style table. -*/ + + +/** + Free a text display and release its associated memory. + + Note, the text BUFFER that the text display displays is a separate + entity and is not freed, nor are the style buffer or style table. + */ Fl_Text_Display_mod::~Fl_Text_Display_mod() { if (scroll_direction) { Fl::remove_timeout(scroll_timer_cb, this); scroll_direction = 0; } - // sb - // if (mBuffer) { - // mBuffer->remove_modify_callback(buffer_modified_cb, this); - // mBuffer->remove_predelete_callback(buffer_predelete_cb, this); - // } +//df +// if (mBuffer) { +// mBuffer->remove_modify_callback(buffer_modified_cb, this); +// mBuffer->remove_predelete_callback(buffer_predelete_cb, this); +// } if (mLineStarts) delete[] mLineStarts; } + + /** - Attach a text buffer to display, replacing the current buffer (if any) -*/ + Attach a text buffer to display, replacing the current buffer (if any) + \param buf attach this text buffer + */ void Fl_Text_Display_mod::buffer( Fl_Text_Buffer_mod *buf ) { /* If the text display is already displaying a buffer, clear it off - of the display and remove our callback from it */ + of the display and remove our callback from it */ if ( buf == mBuffer) return; if ( mBuffer != 0 ) { - buffer_modified_cb( 0, 0, mBuffer->length(), 0, 0, this ); - mNBufferLines = 0; + // we must provide a copy of the buffer that we are deleting! + char *deletedText = mBuffer->text(); + buffer_modified_cb( 0, 0, mBuffer->length(), 0, deletedText, this ); + free(deletedText); + mNBufferLines = 0; mBuffer->remove_modify_callback( buffer_modified_cb, this ); mBuffer->remove_predelete_callback( buffer_predelete_cb, this ); } - + /* Add the buffer to the display, and attach a callback to the buffer for - receiving modification information when the buffer contents change */ + receiving modification information when the buffer contents change */ mBuffer = buf; if (mBuffer) { mBuffer->add_modify_callback( buffer_modified_cb, this ); mBuffer->add_predelete_callback( buffer_predelete_cb, this ); - + /* Update the display */ buffer_modified_cb( 0, buf->length(), 0, 0, 0, this ); } - + /* Resize the widget to update the screen... */ resize(x(), y(), w(), h()); } -/** - Attach (or remove) highlight information in text display and redisplay. - Highlighting information consists of a style buffer which parallels the - normal text buffer, but codes font and color information for the display; - a style table which translates style buffer codes (indexed by buffer - character - 'A') into fonts and colors; and a callback mechanism for - as-needed highlighting, triggered by a style buffer entry of - "unfinishedStyle". Style buffer can trigger additional redisplay during - a normal buffer modification if the buffer contains a primary Fl_Text_Selection - (see extendRangeForStyleMods for more information on this protocol). - Style buffers, tables and their associated memory are managed by the caller. -*/ + +/** + \brief Attach (or remove) highlight information in text display and redisplay. + + Highlighting information consists of a style buffer which parallels the + normal text buffer, but codes font and color information for the display; + a style table which translates style buffer codes (indexed by buffer + character - 'A') into fonts and colors; and a callback mechanism for + as-needed highlighting, triggered by a style buffer entry of + "unfinishedStyle". Style buffer can trigger additional redisplay during + a normal buffer modification if the buffer contains a primary Fl_Text_Selection + (see extendRangeForStyleMods for more information on this protocol). + + Style buffers, tables and their associated memory are managed by the caller. + + Styles are ranged from 65 ('A') to 126. + + \param styleBuffer this buffer works in parallel to the text buffer. For every + character in the text buffer, the stye buffer has a byte at the same offset + that contains an index into an array of possible styles. + \param styleTable a list of styles indexed by the style buffer + \param nStyles number of styles in the style table + \param unfinishedStyle if this style is found, the callback below is called + \param unfinishedHighlightCB if a character with an unfinished style is found, + this callback will be called + \param cbArg and optional argument for the callback above, usually a pointer + to the Text Display. + */ void Fl_Text_Display_mod::highlight_data(Fl_Text_Buffer_mod *styleBuffer, - const Style_Table_Entry *styleTable, - int nStyles, char unfinishedStyle, - Unfinished_Style_Cb unfinishedHighlightCB, - void *cbArg ) { + const Style_Table_Entry *styleTable, + int nStyles, char unfinishedStyle, + Unfinished_Style_Cb unfinishedHighlightCB, + void *cbArg ) { mStyleBuffer = styleBuffer; mStyleTable = styleTable; mNStyles = nStyles; mUnfinishedStyle = unfinishedStyle; mUnfinishedHighlightCB = unfinishedHighlightCB; mHighlightCBArg = cbArg; - + mColumnScale = 0; + mStyleBuffer->canUndo(0); damage(FL_DAMAGE_EXPOSE); } + + +/** + \brief Find the longest line of all visible lines. + \return the width of the longest visible line in pixels + */ int Fl_Text_Display_mod::longest_vline() const { int longest = 0; for (int i = 0; i < mNVisibleLines; i++) @@ -240,17 +290,21 @@ int Fl_Text_Display_mod::longest_vline() const { return longest; } + + /** - Change the size of the displayed text area -*/ + \brief Change the size of the displayed text area. + Calling this function will trigger a recalculation of all lines visible and + of all scrollbar sizes. + \param X, Y, W, H new position and size of this widget + */ void Fl_Text_Display_mod::resize(int X, int Y, int W, int H) { #ifdef DEBUG printf("Fl_Text_Display_mod::resize(X=%d, Y=%d, W=%d, H=%d)\n", X, Y, W, H); #endif // DEBUG const int oldWidth = w(); #ifdef DEBUG - printf(" oldWidth=%d, mContinuousWrap=%d, mWrapMargin=%d\n", oldWidth, - mContinuousWrap, mWrapMargin); + printf(" oldWidth=%d, mContinuousWrap=%d, mWrapMargin=%d\n", oldWidth, mContinuousWrap, mWrapMargin); #endif // DEBUG Fl_Widget::resize(X,Y,W,H); if (!buffer()) return; @@ -258,44 +312,43 @@ void Fl_Text_Display_mod::resize(int X, int Y, int W, int H) { Y += Fl::box_dy(box()); W -= Fl::box_dw(box()); H -= Fl::box_dh(box()); - + text_area.x = X+LEFT_MARGIN; text_area.y = Y+TOP_MARGIN; text_area.w = W-LEFT_MARGIN-RIGHT_MARGIN; text_area.h = H-TOP_MARGIN-BOTTOM_MARGIN; - int i = 0; - + int i; + /* Find the new maximum font height for this text display */ for (i = 0, mMaxsize = fl_height(textfont(), textsize()); i < mNStyles; i++) mMaxsize = max(mMaxsize, fl_height(mStyleTable[i].font, mStyleTable[i].size)); - + // did we have scrollbars initially? unsigned int hscrollbarvisible = mHScrollBar->visible(); unsigned int vscrollbarvisible = mVScrollBar->visible(); - + // try without scrollbars first mVScrollBar->clear_visible(); mHScrollBar->clear_visible(); - + for (int again = 1; again;) { - again = 0; + again = 0; /* In continuous wrap mode, a change in width affects the total number of - lines in the buffer, and can leave the top line number incorrect, and - the top character no longer pointing at a valid line start */ - if (mContinuousWrap && !mWrapMargin && W!=oldWidth) { + lines in the buffer, and can leave the top line number incorrect, and + the top character no longer pointing at a valid line start */ + if (!mFastDisplay && mContinuousWrap && !mWrapMarginPix && W!=oldWidth) { int oldFirstChar = mFirstChar; mNBufferLines = count_lines(0, buffer()->length(), true); mFirstChar = line_start(mFirstChar); mTopLineNum = count_lines(0, mFirstChar, true)+1; - absolute_top_line_number(oldFirstChar); - + absolute_top_line_number(oldFirstChar); #ifdef DEBUG printf(" mNBufferLines=%d\n", mNBufferLines); #endif // DEBUG } - + /* reallocate and update the line starts array, which may have changed - size and / or contents. */ + size and / or contents. */ int nvlines = (text_area.h + mMaxsize - 1) / mMaxsize; if (nvlines < 1) nvlines = 1; if (mNVisibleLines != nvlines) { @@ -303,18 +356,17 @@ void Fl_Text_Display_mod::resize(int X, int Y, int W, int H) { if (mLineStarts) delete[] mLineStarts; mLineStarts = new int [mNVisibleLines]; } - + calc_line_starts(0, mNVisibleLines); calc_last_char(); - + // figure the scrollbars if (scrollbar_width()) { - /* Decide if the vertical scroll bar needs to be visible */ - if (scrollbar_align() & (FL_ALIGN_LEFT|FL_ALIGN_RIGHT) && - mNBufferLines >= mNVisibleLines - 1) + /* Decide if the vertical scrollbar needs to be visible */ +// always display vertical scroll bar +// if (scrollbar_align() & (FL_ALIGN_LEFT|FL_ALIGN_RIGHT) && +// mNBufferLines >= mNVisibleLines - 1) { - if (!mVScrollBar->visible()) - again = 1; mVScrollBar->set_visible(); if (scrollbar_align() & FL_ALIGN_LEFT) { text_area.x = X+scrollbar_width()+LEFT_MARGIN; @@ -328,29 +380,29 @@ void Fl_Text_Display_mod::resize(int X, int Y, int W, int H) { scrollbar_width(), text_area.h+TOP_MARGIN+BOTTOM_MARGIN); } } - + /* - Decide if the horizontal scroll bar needs to be visible. If there - is a vertical scrollbar, a horizontal is always created too. This - is because the alternatives are unatractive: - * Dynamically creating a horizontal scrollbar based on the currently - visible lines is what the original nedit does, but it always wastes - space for the scrollbar even when it's not used. Since the FLTK - widget dynamically allocates the space for the scrollbar and - rearranges the widget to make room for it, this would create a very - visually displeasing "bounce" effect when the vertical scrollbar is - dragged. Trust me, I tried it and it looks really bad. - * The other alternative would be to keep track of what the longest - line in the entire buffer is and base the scrollbar on that. I - didn't do this because I didn't see any easy way to do that using - the nedit code and this could involve a lengthy calculation for - large buffers. If an efficient and non-costly way of doing this - can be found, this might be a way to go. - */ + Decide if the horizontal scrollbar needs to be visible. If there + is a vertical scrollbar, a horizontal is always created too. This + is because the alternatives are unattractive: + * Dynamically creating a horizontal scrollbar based on the currently + visible lines is what the original nedit does, but it always wastes + space for the scrollbar even when it's not used. Since the FLTK + widget dynamically allocates the space for the scrollbar and + rearranges the widget to make room for it, this would create a very + visually displeasing "bounce" effect when the vertical scrollbar is + dragged. Trust me, I tried it and it looks really bad. + * The other alternative would be to keep track of what the longest + line in the entire buffer is and base the scrollbar on that. I + didn't do this because I didn't see any easy way to do that using + the nedit code and this could involve a lengthy calculation for + large buffers. If an efficient and non-costly way of doing this + can be found, this might be a way to go. + */ /* WAS: Suggestion: Try turning the horizontal scrollbar on when - you first see a line that is too wide in the window, but then - don't turn it off (ie mix both of your solutions). */ - if (!mContinuousWrap && scrollbar_align() & (FL_ALIGN_TOP|FL_ALIGN_BOTTOM) && + you first see a line that is too wide in the window, but then + don't turn it off (ie mix both of your solutions). */ + if (!mFastDisplay && !mContinuousWrap && scrollbar_align() & (FL_ALIGN_TOP|FL_ALIGN_BOTTOM) && (mVScrollBar->visible() || longest_vline() > text_area.w)) { if (!mHScrollBar->visible()) { @@ -371,87 +423,86 @@ void Fl_Text_Display_mod::resize(int X, int Y, int W, int H) { } } } - + // user request to change viewport if (mTopLineNumHint != mTopLineNum || mHorizOffsetHint != mHorizOffset) scroll_(mTopLineNumHint, mHorizOffsetHint); - + // everything will fit in the viewport - if (mNBufferLines < mNVisibleLines || mBuffer == NULL || mBuffer->length() == 0) + if (mNBufferLines < mNVisibleLines || mBuffer == NULL || mBuffer->length() == 0) { scroll_(1, mHorizOffset); /* if empty lines become visible, there may be an opportunity to - display more text by scrolling down */ - else while (mLineStarts[mNVisibleLines-2] == -1) - scroll_(mTopLineNum-1, mHorizOffset); - + display more text by scrolling down */ + } else { + while ( mNVisibleLines>=2 + && (mLineStarts[mNVisibleLines-2]==-1) + && scroll_(mTopLineNum-1, mHorizOffset)) + { } + } + // user request to display insert position if (display_insert_position_hint) display_insert(); - + // in case horizontal offset is now greater than longest line int maxhoffset = max(0, longest_vline()-text_area.w); if (mHorizOffset > maxhoffset) scroll_(mTopLineNumHint, maxhoffset); - + mTopLineNumHint = mTopLineNum; mHorizOffsetHint = mHorizOffset; display_insert_position_hint = 0; - - if (mContinuousWrap || + + if ((!mFastDisplay && mContinuousWrap) || hscrollbarvisible != mHScrollBar->visible() || vscrollbarvisible != mVScrollBar->visible()) redraw(); - + update_v_scrollbar(); update_h_scrollbar(); } + + /** - Refresh a rectangle of the text display. left and top are in coordinates of - the text drawing window -*/ + \brief Refresh a rectangle of the text display. + \param left, top are in coordinates of the text drawing window. + \param width, height size in pixels + */ void Fl_Text_Display_mod::draw_text( int left, int top, int width, int height ) { int fontHeight, firstLine, lastLine, line; - + /* find the line number range of the display */ fontHeight = mMaxsize ? mMaxsize : textsize_; firstLine = ( top - text_area.y - fontHeight + 1 ) / fontHeight; lastLine = ( top + height - text_area.y ) / fontHeight + 1; - + fl_push_clip( left, top, width, height ); - + /* draw the lines */ for ( line = firstLine; line <= lastLine; line++ ) draw_vline( line, left, left + width, 0, INT_MAX ); - - /* draw the line numbers if exposed area includes them */ - if (mLineNumWidth != 0 && left <= mLineNumLeft + mLineNumWidth) - draw_line_numbers(false); - + + /* draw the line numbers if exposed area includes them */ + if (mLineNumWidth != 0 && left <= mLineNumLeft + mLineNumWidth) + draw_line_numbers(false); + fl_pop_clip(); } -/** Marks text from start to end as needing a redraw.*/ + + +/** + \brief Marks text from start to end as needing a redraw. + This function will trigger a damage event and later a redraw of parts of + the widget. + \param startpos index of first character needing redraw + \param endpos index after last character needing redraw + */ void Fl_Text_Display_mod::redisplay_range(int startpos, int endpos) { - int ok = 0; - while (!ok && startpos > 0) { - // FIXME: character is ucs-4 - char c = buffer()->character( startpos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - startpos--; - } - } - while (!ok && endpos < buffer()->length()) { - // FIXME: character is ucs-4 - char c = buffer()->character( endpos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - endpos++; - } - } + IS_UTF8_ALIGNED2(buffer(), startpos) + IS_UTF8_ALIGNED2(buffer(), endpos) + if (damage_range1_start == -1 && damage_range1_end == -1) { damage_range1_start = startpos; damage_range1_end = endpos; @@ -468,26 +519,38 @@ void Fl_Text_Display_mod::redisplay_range(int startpos, int endpos) { } damage(FL_DAMAGE_SCROLL); } + + + /** - Refresh all of the text between buffer positions "start" and "end" - not including the character at the position "end". - If end points beyond the end of the buffer, refresh the whole display - after pos, including blank lines which are not technically part of - any range of characters. -*/ + \brief Draw a range of text. + + Refresh all of the text between buffer positions \p startpos and + \p endpos not including the character at the position \p endpos. + + If \p endpos points beyond the end of the buffer, refresh the whole display + after \p startpos, including blank lines which are not technically part of + any range of characters. + + \param startpos index of first character to draw + \param endpos index after last character to draw + */ void Fl_Text_Display_mod::draw_range(int startpos, int endpos) { + startpos = buffer()->utf8_align(startpos); + endpos = buffer()->utf8_align(endpos); + int i, startLine, lastLine, startIndex, endIndex; - + /* If the range is outside of the displayed text, just return */ - if ( endpos < mFirstChar || ( startpos > mLastChar && - !empty_vlines() ) ) return; - + if ( endpos < mFirstChar || ( startpos > mLastChar && !empty_vlines() ) ) + return; + /* Clean up the starting and ending values */ if ( startpos < 0 ) startpos = 0; if ( startpos > mBuffer->length() ) startpos = mBuffer->length(); if ( endpos < 0 ) endpos = 0; if ( endpos > mBuffer->length() ) endpos = mBuffer->length(); - + /* Get the starting and ending lines */ if ( startpos < mFirstChar ) startpos = mFirstChar; @@ -501,19 +564,18 @@ void Fl_Text_Display_mod::draw_range(int startpos, int endpos) { lastLine = mNVisibleLines - 1; } } - + /* Get the starting and ending positions within the lines */ - startIndex = mLineStarts[ startLine ] == -1 ? 0 : - startpos - mLineStarts[ startLine ]; + startIndex = mLineStarts[ startLine ] == -1 ? 0 : startpos - mLineStarts[ startLine ]; if ( endpos >= mLastChar ) endIndex = INT_MAX; else if ( mLineStarts[ lastLine ] == -1 ) endIndex = 0; else endIndex = endpos - mLineStarts[ lastLine ]; - + /* If the starting and ending lines are the same, redisplay the single - line between "start" and "end" */ + line between "start" and "end" */ if ( startLine == lastLine ) { draw_vline( startLine, 0, INT_MAX, startIndex, endIndex ); return; @@ -530,74 +592,123 @@ void Fl_Text_Display_mod::draw_range(int startpos, int endpos) { draw_vline( lastLine, 0, INT_MAX, 0, endIndex ); } -/** Sets the position of the text insertion cursor for text display */ + + +/** + \brief Sets the position of the text insertion cursor for text display. + Move the insertion cursor in front of the character at \p newPos. + This function may trigger a redraw. + \param newPos new caret position + */ void Fl_Text_Display_mod::insert_position( int newPos ) { + IS_UTF8_ALIGNED2(buffer(), newPos) + /* make sure new position is ok, do nothing if it hasn't changed */ - if ( newPos == mCursorPos ) - return; + if ( newPos == mCursorPos ) return; if ( newPos < 0 ) newPos = 0; if ( newPos > mBuffer->length() ) newPos = mBuffer->length(); - + /* cursor movement cancels vertical cursor motion column */ - mCursorPreferredCol = -1; - - /* erase the cursor at it's previous position */ - redisplay_range(mCursorPos - 1, mCursorPos + 1); // FIXME utf8 - + mCursorPreferredXPos = -1; + + /* erase the cursor at its previous position */ + redisplay_range(buffer()->prev_char_clipped(mCursorPos), buffer()->next_char(mCursorPos)); + mCursorPos = newPos; - + /* draw cursor at its new position */ - redisplay_range(mCursorPos - 1, mCursorPos + 1); // FIXME utf8 + redisplay_range(buffer()->prev_char_clipped(mCursorPos), buffer()->next_char(mCursorPos)); } -/** Shows the text cursor */ + + + +/** + \brief Shows the text cursor. + This function may trigger a redraw. + \param b show(1) or hide(0) the text cursor (caret). + */ void Fl_Text_Display_mod::show_cursor(int b) { mCursorOn = b; - redisplay_range(mCursorPos - 1, mCursorPos + 1); // FIXME utf8 + redisplay_range(buffer()->prev_char_clipped(mCursorPos), buffer()->next_char(mCursorPos)); } + + /** - Sets the text cursor style to one of the following: - -
    - -
  • Fl_Text_Display_mod::NORMAL_CURSOR - Shows an I beam. - -
  • Fl_Text_Display_mod::CARET_CURSOR - Shows a caret under the text. - -
  • Fl_Text_Display_mod::DIM_CURSOR - Shows a dimmed I beam. - -
  • Fl_Text_Display_mod::BLOCK_CURSOR - Shows an unfilled box around the current - character. - -
  • Fl_Text_Display_mod::HEAVY_CURSOR - Shows a thick I beam. - -
-*/ + \brief Sets the text cursor style. + Sets the text cursor style to one of the following: + + \li Fl_Text_Display_mod::NORMAL_CURSOR - Shows an I beam. + \li Fl_Text_Display_mod::CARET_CURSOR - Shows a caret under the text. + \li Fl_Text_Display_mod::DIM_CURSOR - Shows a dimmed I beam. + \li Fl_Text_Display_mod::BLOCK_CURSOR - Shows an unfilled box around the current + character. + \li Fl_Text_Display_mod::HEAVY_CURSOR - Shows a thick I beam. + + This call also switches the cursor on and may trigger a redraw. + + \param style new cursor style + */ void Fl_Text_Display_mod::cursor_style(int style) { mCursorStyle = style; if (mCursorOn) show_cursor(); } -/** - If mode is not zero, this call enables automatic word wrapping at column pos. - Word-wrapping does not change the text buffer itself, only the way that the text is displayed. -*/ -void Fl_Text_Display_mod::wrap_mode(int wrap, int wrapMargin) { - mWrapMargin = wrapMargin; - mContinuousWrap = wrap; + +/** + \brief Set the new text wrap mode. + + If \p wrap mode is not zero, this call enables automatic word wrapping at column + \p wrapMargin. Word-wrapping does not change the text buffer itself, only the way + the text is displayed. Different Text Displays can have different wrap modes, + even if they share the same Text Buffer. + + \param wrap new wrap mode is WRAP_NONE (don't wrap text at all), WRAP_AT_COLUMN + (wrap text at the given text column), WRAP_AT_PIXEL (wrap text at a pixel + position), or WRAP_AT_BOUNDS (wrap text so that it fits into the + widget width) + \param wrapMargin in WRAP_AT_COLUMN mode, text will wrap at the n'th character. + For variable width fonts, an average character width is calculated. The + column width is calculated using the current textfont or the first style + when this function is called. If the font size changes, this function + must be called again. In WRAP_AT_PIXEL mode, this is the pixel position. + \todo we need new wrap modes to wrap at the window edge and based on pixel width + or average character width. + */ +void Fl_Text_Display_mod::wrap_mode(int wrap, int wrapMargin) { + switch (wrap) { + case WRAP_NONE: + mWrapMarginPix = 0; + mContinuousWrap = 0; + break; + case WRAP_AT_COLUMN: + default: + mWrapMarginPix = int(col_to_x(wrapMargin)); + mContinuousWrap = 1; + break; + case WRAP_AT_PIXEL: + mWrapMarginPix = wrapMargin; + mContinuousWrap = 1; + break; + case WRAP_AT_BOUNDS: + mWrapMarginPix = 0; + mContinuousWrap = 1; + break; + } + if (buffer()) { /* wrapping can change the total number of lines, re-count */ mNBufferLines = count_lines(0, buffer()->length(), true); - + /* changing wrap margins or changing from wrapped mode to non-wrapped - can leave the character at the top no longer at a line start, and/or - change the line number */ + can leave the character at the top no longer at a line start, and/or + change the line number */ mFirstChar = line_start(mFirstChar); mTopLineNum = count_lines(0, mFirstChar, true) + 1; - + reset_absolute_top_line_number(); - + /* update the line starts array */ calc_line_starts(0, mNVisibleLines); calc_last_char(); @@ -608,52 +719,70 @@ void Fl_Text_Display_mod::wrap_mode(int wrap, int wrapMargin) { mTopLineNum = 1; mAbsTopLineNum = 0; } - + resize(x(), y(), w(), h()); } -/** - Inserts "text" at the current cursor location. This has the same - effect as inserting the text into the buffer using BufInsert and - then moving the insert position after the newly inserted text, except - that it's optimized to do less redrawing. -*/ -void Fl_Text_Display_mod::insert(const char* text) { - int pos = mCursorPos; + +/** + \brief Inserts "text" at the current cursor location. + + This has the same effect as inserting the text into the buffer using BufInsert + and then moving the insert position after the newly inserted text, except + that it's optimized to do less redrawing. + + \param text new text in UTF-8 encoding. + */ +void Fl_Text_Display_mod::insert(const char* text) { + IS_UTF8_ALIGNED2(buffer(), mCursorPos) + IS_UTF8_ALIGNED(text) + + int pos = mCursorPos; + mCursorToHint = pos + strlen( text ); mBuffer->insert( pos, text ); mCursorToHint = NO_HINT; } -/** Replaces text at the current insert position.*/ + + +/** + \brief Replaces text at the current insert position. + \param text new text in UTF-8 encoding + + \todo Unicode? Find out exactly what we do here and simplify. + */ void Fl_Text_Display_mod::overstrike(const char* text) { + IS_UTF8_ALIGNED2(buffer(), mCursorPos) + IS_UTF8_ALIGNED(text) + int startPos = mCursorPos; Fl_Text_Buffer_mod *buf = mBuffer; int lineStart = buf->line_start( startPos ); int textLen = strlen( text ); int i, p, endPos, indent, startIndent, endIndent; const char *c; - char ch, *paddedText = NULL; - + unsigned int ch; + char *paddedText = NULL; + /* determine how many displayed character positions are covered */ startIndent = mBuffer->count_displayed_characters( lineStart, startPos ); indent = startIndent; - for ( c = text; *c != '\0'; c++ ) - indent += Fl_Text_Buffer_mod::character_width( c, indent, buf->tab_distance() ); + for ( c = text; *c != '\0'; c += fl_utf8len1(*c) ) + indent++; endIndent = indent; - + /* find which characters to remove, and if necessary generate additional - padding to make up for removed control characters at the end */ + padding to make up for removed control characters at the end */ indent = startIndent; - for ( p = startPos; ; p++ ) { + for ( p = startPos; ; p=buffer()->next_char(p) ) { if ( p == buf->length() ) break; - // FIXME: character is ucs-4 - ch = buf->character( p ); + ch = buf->char_at( p ); if ( ch == '\n' ) break; - indent += Fl_Text_Buffer_mod::character_width( &ch, indent, buf->tab_distance() ); // FIXME: not unicode + indent++; if ( indent == endIndent ) { p++; break; @@ -670,7 +799,7 @@ void Fl_Text_Display_mod::overstrike(const char* text) { } } endPos = p; - + mCursorToHint = startPos + textLen; buf->replace( startPos, endPos, paddedText == NULL ? text : paddedText ); mCursorToHint = NO_HINT; @@ -678,205 +807,205 @@ void Fl_Text_Display_mod::overstrike(const char* text) { delete [] paddedText; } + + /** - Translate a buffer text position to the XY location where the top left - of the cursor would be positioned to point to that character. Returns - 0 if the position is not displayed because it is VERTICALLY out - of view. If the position is horizontally out of view, returns the - X coordinate where the position would be if it were visible. -*/ + \brief Convert a character index into a pixel position. + + Translate a buffer text position to the XY location where the top left of the + cursor would be positioned to point to that character. Returns 0 if the + position is not displayed because it is \e \b vertically out of view. + If the position is horizontally out of view, returns the X coordinate where + the position would be if it were visible. + \param pos character index + \param[out] X, Y pixel position of character on screen + \return 0 if character vertically out of view, X position otherwise + */ int Fl_Text_Display_mod::position_to_xy( int pos, int* X, int* Y ) const { - int charIndex, lineStartPos, fontHeight, lineLen; - int visLineNum, charLen, outIndex, xStep, charStyle; - char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - const char *lineStr; - -// printf("position_to_xy(pos=%d, X=%p, Y=%p)\n", pos, X, Y); + IS_UTF8_ALIGNED2(buffer(), pos) + int lineStartPos, fontHeight, lineLen; + int visLineNum; + /* If position is not displayed, return false */ if (pos < mFirstChar || (pos > mLastChar && !empty_vlines())) { -// printf(" returning 0\n" -// " mFirstChar=%d, mLastChar=%d, empty_vlines()=0\n", -// mFirstChar, mLastChar); return 0; } - + /* Calculate Y coordinate */ if (!position_to_line(pos, &visLineNum)) { -// puts(" returning 0\n" -// " position_to_line()=0"); return 0; } - if (visLineNum < 0 || visLineNum > mNBufferLines) { -// printf(" returning 0\n" -// " visLineNum=%d, mNBufferLines=%d\n", -// visLineNum, mNBufferLines); return 0; } - + fontHeight = mMaxsize; *Y = text_area.y + visLineNum * fontHeight; - - /* Get the text, length, and buffer position of the line. If the position - is beyond the end of the buffer and should be at the first position on - the first empty line, don't try to get or scan the text */ + + /* Get the text, length, and buffer position of the line. If the position + is beyond the end of the buffer and should be at the first position on + the first empty line, don't try to get or scan the text */ lineStartPos = mLineStarts[visLineNum]; if ( lineStartPos == -1 ) { *X = text_area.x - mHorizOffset; return 1; } lineLen = vline_length( visLineNum ); - lineStr = mBuffer->text_range( lineStartPos, lineStartPos + lineLen ); - - /* Step through character positions from the beginning of the line - to "pos" to calculate the X coordinate */ - xStep = text_area.x - mHorizOffset; - outIndex = 0; - for (charIndex = 0; - charIndex < lineLen && charIndex < pos - lineStartPos; - charIndex += fl_utf8len(lineStr[charIndex]) ) - { - charLen = Fl_Text_Buffer_mod::expand_character( lineStr+charIndex, outIndex, expandedChar, - mBuffer->tab_distance()); - charStyle = position_style( lineStartPos, lineLen, charIndex, - outIndex ); - xStep += string_width( expandedChar, charLen, charStyle ); - outIndex += charLen; - } - *X = xStep; - free((char *)lineStr); + *X = text_area.x + handle_vline(GET_WIDTH, lineStartPos, pos-lineStartPos, 0, 0, 0, 0, 0, 0) - mHorizOffset; return 1; } -/** - Find the line number of position "pos". Note: this only works for - displayed lines. If the line is not displayed, the function returns - 0 (without the mLineStarts array it could turn in to very long - calculation involving scanning large amounts of text in the buffer). - If continuous wrap mode is on, returns the absolute line number (as opposed - to the wrapped line number which is used for scrolling). -*/ -int Fl_Text_Display_mod::position_to_linecol( int pos, int* lineNum, int* column ) const { - int retVal; - - /* In continuous wrap mode, the absolute (non-wrapped) line count is - maintained separately, as needed. Only return it if we're actually - keeping track of it and pos is in the displayed text */ - if (mContinuousWrap) { - if (!maintaining_absolute_top_line_number() || - pos < mFirstChar || pos > mLastChar) - return 0; - *lineNum = mAbsTopLineNum + buffer()->count_lines(mFirstChar, pos); - *column - = buffer()->count_displayed_characters(buffer()->line_start(pos), pos); - return 1; - } + +/** + \brief Find the line and column number of position \p pos. + + This only works for displayed lines. If the line is not displayed, the + function returns 0 (without the mLineStarts array it could turn in to very long + calculation involving scanning large amounts of text in the buffer). + If continuous wrap mode is on, returns the absolute line number (as opposed + to the wrapped line number which is used for scrolling). + + \param pos character index + \param[out] lineNum absolute (unwrapped) line number + \param[out] column character offset to the beginning of the line + \return 0 if \p pos is off screen, line number otherwise + \todo a column number makes little sense in the UTF-8/variable font width + environment. We will have to further define what exactly we want to return. + Please check the functions that call this particular function. + */ +int Fl_Text_Display_mod::position_to_linecol( int pos, int* lineNum, int* column ) const { + IS_UTF8_ALIGNED2(buffer(), pos) + + int retVal; + + /* In continuous wrap mode, the absolute (non-wrapped) line count is + maintained separately, as needed. Only return it if we're actually + keeping track of it and pos is in the displayed text */ + if (!mFastDisplay && mContinuousWrap) { + if (!maintaining_absolute_top_line_number() || pos < mFirstChar || pos > mLastChar) + return 0; + *lineNum = mAbsTopLineNum + buffer()->count_lines(mFirstChar, pos); + *column = buffer()->count_displayed_characters(buffer()->line_start(pos), pos); + return 1; + } + retVal = position_to_line( pos, lineNum ); if ( retVal ) { - *column = mBuffer->count_displayed_characters( - mLineStarts[ *lineNum ], pos ); + *column = mBuffer->count_displayed_characters( mLineStarts[ *lineNum ], pos ); *lineNum += mTopLineNum; } return retVal; } + + /** - Return 1 if position (X, Y) is inside of the primary Fl_Text_Selection -*/ + \brief Check if a pixel position is within the primary selection. + \param X, Y pixel position to test + \return 1 if position (X, Y) is inside of the primary Fl_Text_Selection + */ int Fl_Text_Display_mod::in_selection( int X, int Y ) const { - int row, column, pos = xy_to_position( X, Y, CHARACTER_POS ); + int pos = xy_to_position( X, Y, CHARACTER_POS ); + IS_UTF8_ALIGNED2(buffer(), pos) Fl_Text_Buffer_mod *buf = mBuffer; - int ok = 0; - while (!ok) { - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - pos++; - } - } - - xy_to_rowcol( X, Y, &row, &column, CHARACTER_POS ); - if (range_touches_selection(buf->primary_selection(), mFirstChar, mLastChar)) - column = wrapped_column(row, column); - return buf->primary_selection()->includes(pos, buf->line_start( pos ), column); + return buf->primary_selection()->includes(pos); } + + /** - Correct a column number based on an unconstrained position (as returned by - TextDXYToUnconstrainedPosition) to be relative to the last actual newline - in the buffer before the row and column position given, rather than the - last line start created by line wrapping. This is an adapter - for rectangular selections and code written before continuous wrap mode, - which thinks that the unconstrained column is the number of characters - from the last newline. Obviously this is time consuming, because it - invloves character re-counting. -*/ + \brief Nobody knows what this function does. + + Correct a column number based on an unconstrained position (as returned by + TextDXYToUnconstrainedPosition) to be relative to the last actual newline + in the buffer before the row and column position given, rather than the + last line start created by line wrapping. This is an adapter + for rectangular selections and code written before continuous wrap mode, + which thinks that the unconstrained column is the number of characters + from the last newline. Obviously this is time consuming, because it + invloves character re-counting. + + \param row + \param column + \return something unknown + \todo What does this do and how is it useful? Column numbers mean little in + this context. Which functions depend on this one? + + \todo Unicode? + */ int Fl_Text_Display_mod::wrapped_column(int row, int column) const { - int lineStart, dispLineStart; - - if (!mContinuousWrap || row < 0 || row > mNVisibleLines) - return column; - dispLineStart = mLineStarts[row]; - if (dispLineStart == -1) - return column; - lineStart = buffer()->line_start(dispLineStart); - return column - + buffer()->count_displayed_characters(lineStart, dispLineStart); + int lineStart, dispLineStart; + + if (mFastDisplay || !mContinuousWrap || row < 0 || row > mNVisibleLines) + return column; + dispLineStart = mLineStarts[row]; + if (dispLineStart == -1) + return column; + lineStart = buffer()->line_start(dispLineStart); + return column + buffer()->count_displayed_characters(lineStart, dispLineStart); } -/** - Correct a row number from an unconstrained position (as returned by - TextDXYToUnconstrainedPosition) to a straight number of newlines from the - top line of the display. Because rectangular selections are based on - newlines, rather than display wrapping, and anywhere a rectangular selection - needs a row, it needs it in terms of un-wrapped lines. -*/ -int Fl_Text_Display_mod::wrapped_row(int row) const{ - if (!mContinuousWrap || row < 0 || row > mNVisibleLines) - return row; - return buffer()->count_lines(mFirstChar, mLineStarts[row]); -} + /** - Scroll the display to bring insertion cursor into view. -** - Note: it would be nice to be able to do this without counting lines twice - (scroll_() counts them too) and/or to count from the most efficient - starting point, but the efficiency of this routine is not as important to - the overall performance of the text display. -*/ + \brief Nobody knows what this function does. + + Correct a row number from an unconstrained position (as returned by + TextDXYToUnconstrainedPosition) to a straight number of newlines from the + top line of the display. Because rectangular selections are based on + newlines, rather than display wrapping, and anywhere a rectangular selection + needs a row, it needs it in terms of un-wrapped lines. + + \param row + \return something unknown + \todo What does this do and how is it useful? Column numbers mean little in + this context. Which functions depend on this one? + */ +int Fl_Text_Display_mod::wrapped_row(int row) const { + if (mFastDisplay || !mContinuousWrap || row < 0 || row > mNVisibleLines) + return row; + return buffer()->count_lines(mFirstChar, mLineStarts[row]); +} + + + +/** + \brief Scroll the display to bring insertion cursor into view. + + Note: it would be nice to be able to do this without counting lines twice + (scroll_() counts them too) and/or to count from the most efficient + starting point, but the efficiency of this routine is not as important to + the overall performance of the text display. + + \todo Unicode? + */ void Fl_Text_Display_mod::display_insert() { int hOffset, topLine, X, Y; hOffset = mHorizOffset; topLine = mTopLineNum; - -// FIXME: I don't understand this well enough to know if it is correct -// it is different than nedit 5.3 + if (insert_position() < mFirstChar) { topLine -= count_lines(insert_position(), mFirstChar, false); - } else if (mLineStarts[mNVisibleLines-2] != -1) { + } else if (mNVisibleLines>=2 && mLineStarts[mNVisibleLines-2] != -1) { int lastChar = line_end(mLineStarts[mNVisibleLines-2],true); if (insert_position() >= lastChar) - topLine - += count_lines(lastChar - (wrap_uses_character(mLastChar) ? 0 : 1), - insert_position(), false); + topLine += count_lines(lastChar - (wrap_uses_character(mLastChar) ? 0 : 1), + insert_position(), false); } - + /* Find the new setting for horizontal offset (this is a bit ungraceful). - If the line is visible, just use PositionToXY to get the position - to scroll to, otherwise, do the vertical scrolling first, then the - horizontal */ + If the line is visible, just use PositionToXY to get the position + to scroll to, otherwise, do the vertical scrolling first, then the + horizontal */ if (!position_to_xy( mCursorPos, &X, &Y )) { scroll_(topLine, hOffset); if (!position_to_xy( mCursorPos, &X, &Y )) { - #ifdef DEBUG +#ifdef DEBUG printf ("*** display_insert/position_to_xy # GIVE UP !\n"); fflush(stdout); - #endif // DEBUG +#endif // DEBUG return; /* Give up, it's not worth it (but why does it fail?) */ } } @@ -884,56 +1013,67 @@ void Fl_Text_Display_mod::display_insert() { hOffset += X-(text_area.x + text_area.w); else if (X < text_area.x) hOffset += X-text_area.x; - + /* Do the scroll */ if (topLine != mTopLineNum || hOffset != mHorizOffset) scroll_(topLine, hOffset); } -/** Scrolls the text buffer to show the current insert position.*/ + +/** + \brief Scrolls the text buffer to show the current insert position. + This function triggers a complete recalculation, ending in a call to + Fl_Text_Display_mod::display_insert() + */ void Fl_Text_Display_mod::show_insert_position() { display_insert_position_hint = 1; resize(x(), y(), w(), h()); } + /* - Cursor movement functions -*/ -/** Moves the current insert position right one character.*/ + Cursor movement functions + */ + +/** + \brief Moves the current insert position right one character. + \return 1 if the cursor moved, 0 if the end of the text was reached + */ int Fl_Text_Display_mod::move_right() { - int ok = 0; - while (!ok) { if ( mCursorPos >= mBuffer->length() ) return 0; - insert_position( mCursorPos + 1 ); - int pos = insert_position(); - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) ok = 1; - } + int p = insert_position(); + int q = buffer()->next_char(p); + insert_position(q); return 1; } -/** Moves the current insert position left one character.*/ + + + +/** + \brief Moves the current insert position left one character. + \return 1 if the cursor moved, 0 if the beginning of the text was reached + */ int Fl_Text_Display_mod::move_left() { - int ok = 0; - while (!ok) { if ( mCursorPos <= 0 ) return 0; - insert_position( mCursorPos - 1 ); - int pos = insert_position(); - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) ok = 1; - } + int p = insert_position(); + int q = buffer()->prev_char_clipped(p); + insert_position(q); return 1; } -/** Moves the current insert position up one line.*/ -int Fl_Text_Display_mod::move_up() { - int lineStartPos, column, prevLineStartPos, newPos, visLineNum; + +/** + \brief Moves the current insert position up one line. + \return 1 if the cursor moved, 0 if the beginning of the text was reached + */ +int Fl_Text_Display_mod::move_up() { + int lineStartPos, xPos, prevLineStartPos, newPos, visLineNum; + /* Find the position of the start of the line. Use the line starts array - if possible */ + if possible */ if ( position_to_line( mCursorPos, &visLineNum ) ) lineStartPos = mLineStarts[ visLineNum ]; else { @@ -942,319 +1082,403 @@ int Fl_Text_Display_mod::move_up() { } if ( lineStartPos == 0 ) return 0; - + /* Decide what column to move to, if there's a preferred column use that */ - column = mCursorPreferredCol >= 0 ? mCursorPreferredCol : - mBuffer->count_displayed_characters( lineStartPos, mCursorPos ); - + if (mCursorPreferredXPos >= 0) + xPos = mCursorPreferredXPos; + else + xPos = handle_vline(GET_WIDTH, lineStartPos, mCursorPos-lineStartPos, + 0, 0, 0, 0, 0, INT_MAX); + /* count forward from the start of the previous line to reach the column */ if ( visLineNum != -1 && visLineNum != 0 ) prevLineStartPos = mLineStarts[ visLineNum - 1 ]; else prevLineStartPos = rewind_lines( lineStartPos, 1 ); - newPos = mBuffer->skip_displayed_characters( prevLineStartPos, column ); - if (mContinuousWrap) - newPos = min(newPos, line_end(prevLineStartPos, true)); - + + int lineEnd = line_end(prevLineStartPos, true); + newPos = handle_vline(FIND_INDEX_FROM_ZERO, prevLineStartPos, lineEnd-prevLineStartPos, + 0, 0, 0, 0, 0, xPos); + /* move the cursor */ insert_position( newPos ); - - int ok = 0; - while (!ok) { - int pos = insert_position(); - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - insert_position( mCursorPos + 1 ); - } - } - + /* if a preferred column wasn't aleady established, establish it */ - mCursorPreferredCol = column; + mCursorPreferredXPos = xPos; return 1; } -/** Moves the current insert position down one line.*/ -int Fl_Text_Display_mod::move_down() { - int lineStartPos, column, nextLineStartPos, newPos, visLineNum; + +/** + \brief Moves the current insert position down one line. + \return 1 if the cursor moved, 0 if the beginning of the text was reached + */ +int Fl_Text_Display_mod::move_down() { + int lineStartPos, xPos, newPos, visLineNum; + if ( mCursorPos == mBuffer->length() ) return 0; + if ( position_to_line( mCursorPos, &visLineNum ) ) lineStartPos = mLineStarts[ visLineNum ]; else { lineStartPos = line_start( mCursorPos ); visLineNum = -1; } - column = mCursorPreferredCol >= 0 ? mCursorPreferredCol : - mBuffer->count_displayed_characters( lineStartPos, mCursorPos ); - nextLineStartPos = skip_lines( lineStartPos, 1, true ); - newPos = mBuffer->skip_displayed_characters( nextLineStartPos, column ); - if (mContinuousWrap) - newPos = min(newPos, line_end(nextLineStartPos, true)); - - insert_position( newPos ); - int ok = 0; - while (!ok) { - int pos = insert_position(); - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - insert_position( mCursorPos + 1 ); - } + if (mCursorPreferredXPos >= 0) { + xPos = mCursorPreferredXPos; + } else { + xPos = handle_vline(GET_WIDTH, lineStartPos, mCursorPos-lineStartPos, + 0, 0, 0, 0, 0, INT_MAX); } - mCursorPreferredCol = column; + + int nextLineStartPos = skip_lines( lineStartPos, 1, true ); + int lineEnd = line_end(nextLineStartPos, true); + newPos = handle_vline(FIND_INDEX_FROM_ZERO, nextLineStartPos, lineEnd-nextLineStartPos, + 0, 0, 0, 0, 0, xPos); + + insert_position( newPos ); + mCursorPreferredXPos = xPos; return 1; } + + /** - Same as BufCountLines, but takes in to account wrapping if wrapping is - turned on. If the caller knows that startPos is at a line start, it - can pass "startPosIsLineStart" as True to make the call more efficient - by avoiding the additional step of scanning back to the last newline. -*/ + \brief Count the number of lines between two positions. + + Same as BufCountLines, but takes into account wrapping if wrapping is + turned on. If the caller knows that \p startPos is at a line start, it + can pass \p startPosIsLineStart as True to make the call more efficient + by avoiding the additional step of scanning back to the last newline. + + \param startPos index to first character + \param endPos index after last character + \param startPosIsLineStart avoid scanning back to the line start + \return number of lines + */ int Fl_Text_Display_mod::count_lines(int startPos, int endPos, - bool startPosIsLineStart) const { - int retLines, retPos, retLineStart, retLineEnd; - + bool startPosIsLineStart) const { + IS_UTF8_ALIGNED2(buffer(), startPos) + IS_UTF8_ALIGNED2(buffer(), endPos) + + int retLines, retPos, retLineStart, retLineEnd; + #ifdef DEBUG - printf("Fl_Text_Display_mod::count_lines(startPos=%d, endPos=%d, startPosIsLineStart=%d\n", - startPos, endPos, startPosIsLineStart); + printf("Fl_Text_Display_mod::count_lines(startPos=%d, endPos=%d, startPosIsLineStart=%d\n", + startPos, endPos, startPosIsLineStart); #endif // DEBUG - - /* If we're not wrapping use simple (and more efficient) BufCountLines */ - if (!mContinuousWrap) - return buffer()->count_lines(startPos, endPos); - - wrapped_line_counter(buffer(), startPos, endPos, INT_MAX, - startPosIsLineStart, 0, &retPos, &retLines, &retLineStart, - &retLineEnd); - + + /* If we're not wrapping use simple (and more efficient) BufCountLines */ + if (mFastDisplay || !mContinuousWrap) + return buffer()->count_lines(startPos, endPos); + + wrapped_line_counter(buffer(), startPos, endPos, INT_MAX, + startPosIsLineStart, 0, &retPos, &retLines, &retLineStart, + &retLineEnd); + #ifdef DEBUG - printf(" # after WLC: retPos=%d, retLines=%d, retLineStart=%d, retLineEnd=%d\n", - retPos, retLines, retLineStart, retLineEnd); + printf(" # after WLC: retPos=%d, retLines=%d, retLineStart=%d, retLineEnd=%d\n", + retPos, retLines, retLineStart, retLineEnd); #endif // DEBUG - - return retLines; + + return retLines; } + + /** - Same as BufCountForwardNLines, but takes in to account line breaks when - wrapping is turned on. If the caller knows that startPos is at a line start, - it can pass "startPosIsLineStart" as True to make the call more efficient - by avoiding the additional step of scanning back to the last newline. -*/ + \brief Skip a number of lines forward. + + Same as BufCountForwardNLines, but takes into account line breaks when + wrapping is turned on. If the caller knows that startPos is at a line start, + it can pass "startPosIsLineStart" as True to make the call more efficient + by avoiding the additional step of scanning back to the last newline. + + \param startPos index to starting character + \param nLines number of lines to skip ahead + \param startPosIsLineStart avoid scanning back to the line start + \return new position as index + */ int Fl_Text_Display_mod::skip_lines(int startPos, int nLines, - bool startPosIsLineStart) { - int retLines, retPos, retLineStart, retLineEnd; - - /* if we're not wrapping use more efficient BufCountForwardNLines */ - if (!mContinuousWrap) - return buffer()->skip_lines(startPos, nLines); - - /* wrappedLineCounter can't handle the 0 lines case */ - if (nLines == 0) - return startPos; - - /* use the common line counting routine to count forward */ - wrapped_line_counter(buffer(), startPos, buffer()->length(), - nLines, startPosIsLineStart, 0, &retPos, &retLines, &retLineStart, - &retLineEnd); - return retPos; + bool startPosIsLineStart) { + IS_UTF8_ALIGNED2(buffer(), startPos) + + int retLines, retPos, retLineStart, retLineEnd; + + /* if we're not wrapping use more efficient BufCountForwardNLines */ + if (mFastDisplay || !mContinuousWrap) + return buffer()->skip_lines(startPos, nLines); + + /* wrappedLineCounter can't handle the 0 lines case */ + if (nLines == 0) + return startPos; + + /* use the common line counting routine to count forward */ + wrapped_line_counter(buffer(), startPos, buffer()->length(), + nLines, startPosIsLineStart, 0, + &retPos, &retLines, &retLineStart, &retLineEnd); + IS_UTF8_ALIGNED2(buffer(), retPos) + return retPos; } -/** - Same as BufEndOfLine, but takes in to account line breaks when wrapping - is turned on. If the caller knows that startPos is at a line start, it - can pass "startPosIsLineStart" as True to make the call more efficient - by avoiding the additional step of scanning back to the last newline. -** - Note that the definition of the end of a line is less clear when continuous - wrap is on. With continuous wrap off, it's just a pointer to the newline - that ends the line. When it's on, it's the character beyond the last - DISPLAYABLE character on the line, where a whitespace character which has - been "converted" to a newline for wrapping is not considered displayable. - Also note that, a line can be wrapped at a non-whitespace character if the - line had no whitespace. In this case, this routine returns a pointer to - the start of the next line. This is also consistent with the model used by - visLineLength. -*/ -int Fl_Text_Display_mod::line_end(int pos, bool startPosIsLineStart) const { - int retLines, retPos, retLineStart, retLineEnd; - - /* If we're not wrapping use more efficien BufEndOfLine */ - if (!mContinuousWrap) - return buffer()->line_end(pos); - - if (pos == buffer()->length()) - return pos; - wrapped_line_counter(buffer(), pos, buffer()->length(), 1, - startPosIsLineStart, 0, &retPos, &retLines, &retLineStart, - &retLineEnd); - return retLineEnd; -} + /** - Same as BufStartOfLine, but returns the character after last wrap point - rather than the last newline. -*/ + \brief Returns the end of a line. + + Same as BufEndOfLine, but takes into account line breaks when wrapping + is turned on. If the caller knows that \p startPos is at a line start, it + can pass "startPosIsLineStart" as True to make the call more efficient + by avoiding the additional step of scanning back to the last newline. + + Note that the definition of the end of a line is less clear when continuous + wrap is on. With continuous wrap off, it's just a pointer to the newline + that ends the line. When it's on, it's the character beyond the last + \b displayable character on the line, where a whitespace character which has + been "converted" to a newline for wrapping is not considered displayable. + Also note that a line can be wrapped at a non-whitespace character if the + line had no whitespace. In this case, this routine returns a pointer to + the start of the next line. This is also consistent with the model used by + visLineLength. + + \param startPos index to starting character + \param startPosIsLineStart avoid scanning back to the line start + \return new position as index + */ +int Fl_Text_Display_mod::line_end(int startPos, bool startPosIsLineStart) const { + IS_UTF8_ALIGNED2(buffer(), startPos) + + int retLines, retPos, retLineStart, retLineEnd; + + /* If we're not wrapping use more efficient BufEndOfLine */ + if (mFastDisplay || !mContinuousWrap) + return buffer()->line_end(startPos); + + if (startPos == buffer()->length()) + return startPos; + + wrapped_line_counter(buffer(), startPos, buffer()->length(), 1, + startPosIsLineStart, 0, &retPos, &retLines, &retLineStart, + &retLineEnd); + + IS_UTF8_ALIGNED2(buffer(), retLineEnd) + return retLineEnd; +} + + + +/** + \brief Return the beginning of a line. + + Same as BufStartOfLine, but returns the character after last wrap point + rather than the last newline. + + \param pos index to starting character + \return new position as index + */ int Fl_Text_Display_mod::line_start(int pos) const { - int retLines, retPos, retLineStart, retLineEnd; - - /* If we're not wrapping, use the more efficient BufStartOfLine */ - if (!mContinuousWrap) - return buffer()->line_start(pos); + IS_UTF8_ALIGNED2(buffer(), pos) - wrapped_line_counter(buffer(), buffer()->line_start(pos), pos, INT_MAX, true, 0, - &retPos, &retLines, &retLineStart, &retLineEnd); - return retLineStart; + int retLines, retPos, retLineStart, retLineEnd; + + /* If we're not wrapping, use the more efficient BufStartOfLine */ + if (mFastDisplay || !mContinuousWrap) + return buffer()->line_start(pos); + + wrapped_line_counter(buffer(), buffer()->line_start(pos), pos, INT_MAX, true, 0, + &retPos, &retLines, &retLineStart, &retLineEnd); + + IS_UTF8_ALIGNED2(buffer(), retLineStart) + return retLineStart; } + + /** - Same as BufCountBackwardNLines, but takes in to account line breaks when - wrapping is turned on. -*/ -int Fl_Text_Display_mod::rewind_lines(int startPos, int nLines) { - Fl_Text_Buffer_mod *buf = buffer(); - int pos, lineStart, retLines, retPos, retLineStart, retLineEnd; - - /* If we're not wrapping, use the more efficient BufCountBackwardNLines */ - if (!mContinuousWrap) - return buf->rewind_lines(startPos, nLines); + \brief Skip a number of lines back. - pos = startPos; - for (;;) { - lineStart = buf->line_start(pos); - wrapped_line_counter(buf, lineStart, pos, INT_MAX, - true, 0, &retPos, &retLines, &retLineStart, &retLineEnd, false); - if (retLines > nLines) - return skip_lines(lineStart, retLines-nLines, - true); - nLines -= retLines; - pos = lineStart - 1; - if (pos < 0) - return 0; - nLines -= 1; - } + Same as BufCountBackwardNLines, but takes into account line breaks when + wrapping is turned on. + + \param startPos index to starting character + \param nLines number of lines to skip back + \return new position as index + */ +int Fl_Text_Display_mod::rewind_lines(int startPos, int nLines) { + IS_UTF8_ALIGNED2(buffer(), startPos) + + Fl_Text_Buffer_mod *buf = buffer(); + int pos, lineStart, retLines, retPos, retLineStart, retLineEnd; + + /* If we're not wrapping, use the more efficient BufCountBackwardNLines */ + if (mFastDisplay || !mContinuousWrap) + return buf->rewind_lines(startPos, nLines); + + pos = startPos; + for (;;) { + lineStart = buf->line_start(pos); + wrapped_line_counter(buf, lineStart, pos, INT_MAX, true, 0, + &retPos, &retLines, &retLineStart, &retLineEnd, false); + if (retLines > nLines) + return skip_lines(lineStart, retLines-nLines, true); + nLines -= retLines; + pos = lineStart - 1; + if (pos < 0) + return 0; + nLines -= 1; + } } -static inline int fl_isseparator(int c) { + + +static inline int fl_isseparator(unsigned int c) { + // FIXME: this does not take UCS-4 encoding into account return c != '$' && c != '_' && (isspace(c) || ispunct(c)); } -/** Moves the current insert position right one word.*/ + + +/** + \brief Moves the current insert position right one word. + */ void Fl_Text_Display_mod::next_word() { int pos = insert_position(); - // FIXME: character is ucs-4 - while (pos < buffer()->length() && !fl_isseparator(buffer()->character(pos))) { - pos++; - } - // FIXME: character is ucs-4 - while (pos < buffer()->length() && fl_isseparator(buffer()->character(pos))) { - pos++; + + while (pos < buffer()->length() && !fl_isseparator(buffer()->char_at(pos))) { + pos = buffer()->next_char(pos); } + while (pos < buffer()->length() && fl_isseparator(buffer()->char_at(pos))) { + pos = buffer()->next_char(pos); + } + insert_position( pos ); } -/** Moves the current insert position left one word.*/ + + +/** + \brief Moves the current insert position left one word. + */ void Fl_Text_Display_mod::previous_word() { int pos = insert_position(); if (pos==0) return; - pos--; - // FIXME: character is ucs-4 - while (pos && fl_isseparator(buffer()->character(pos))) { - pos--; - } - // FIXME: character is ucs-4 - while (pos && !fl_isseparator(buffer()->character(pos))) { - pos--; - } - // FIXME: character is ucs-4 - if (fl_isseparator(buffer()->character(pos))) pos++; + pos = buffer()->prev_char(pos); + while (pos && fl_isseparator(buffer()->char_at(pos))) { + pos = buffer()->prev_char(pos); + } + + while (pos && !fl_isseparator(buffer()->char_at(pos))) { + pos = buffer()->prev_char(pos); + } + + if (fl_isseparator(buffer()->char_at(pos))) { + pos = buffer()->next_char(pos); + } + insert_position( pos ); } -/** - Callback attached to the text buffer to receive delete information before - the modifications are actually made. -*/ -void Fl_Text_Display_mod::buffer_predelete_cb(int pos, int nDeleted, void *cbArg) { - Fl_Text_Display_mod *textD = (Fl_Text_Display_mod *)cbArg; - if (textD->mContinuousWrap && - (textD->mFixedFontWidth == -1 || textD->mModifyingTabDistance)) - /* Note: we must perform this measurement, even if there is not a - single character deleted; the number of "deleted" lines is the - number of visual lines spanned by the real line in which the - modification takes place. - Also, a modification of the tab distance requires the same - kind of calculations in advance, even if the font width is "fixed", - because when the width of the tab characters changes, the layout - of the text may be completely different. */ - textD->measure_deleted_lines(pos, nDeleted); - else - textD->mSuppressResync = 0; /* Probably not needed, but just in case */ -} + /** - Callback attached to the text buffer to receive modification information -*/ + \brief This is called before any characters are deleted. + + Callback attached to the text buffer to receive delete information before + the modifications are actually made. + + \param pos starting index of deletion + \param nDeleted number of bytes we will delete (must be UTF-8 aligned!) + \param cbArg "this" pointer for static callback function + */ +void Fl_Text_Display_mod::buffer_predelete_cb(int pos, int nDeleted, void *cbArg) { + Fl_Text_Display_mod *textD = (Fl_Text_Display_mod *)cbArg; + if (textD->mContinuousWrap && !textD->mFastDisplay) { + /* Note: we must perform this measurement, even if there is not a + single character deleted; the number of "deleted" lines is the + number of visual lines spanned by the real line in which the + modification takes place. + Also, a modification of the tab distance requires the same + kind of calculations in advance, even if the font width is "fixed", + because when the width of the tab characters changes, the layout + of the text may be completely different. */ + IS_UTF8_ALIGNED2(textD->buffer(), pos) + textD->measure_deleted_lines(pos, nDeleted); + } else { + textD->mSuppressResync = 0; /* Probably not needed, but just in case */ + } +} + + + +/** + \brief This is called whenever the buffer is modified. + + Callback attached to the text buffer to receive modification information + + \param pos starting index of modification + \param nInserted number of bytes we inserted (must be UTF-8 aligned!) + \param nDeleted number of bytes deleted (must be UTF-8 aligned!) + \param nRestyled ?? + \param deletedText this is what was removed, must not be NULL if nDeleted is set + \param cbArg "this" pointer for static callback function + */ void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDeleted, - int nRestyled, const char *deletedText, void *cbArg ) { + int nRestyled, const char *deletedText, void *cbArg ) { int linesInserted, linesDeleted, startDispPos, endDispPos; Fl_Text_Display_mod *textD = ( Fl_Text_Display_mod * ) cbArg; Fl_Text_Buffer_mod *buf = textD->mBuffer; int oldFirstChar = textD->mFirstChar; int scrolled, origCursorPos = textD->mCursorPos; - int wrapModStart, wrapModEnd; + int wrapModStart = 0, wrapModEnd = 0; + IS_UTF8_ALIGNED2(buf, pos) + IS_UTF8_ALIGNED2(buf, oldFirstChar) + /* buffer modification cancels vertical cursor motion column */ if ( nInserted != 0 || nDeleted != 0 ) - textD->mCursorPreferredCol = -1; - - /* Count the number of lines inserted and deleted, and in the case - of continuous wrap mode, how much has changed */ - if (textD->mContinuousWrap) { - textD->find_wrap_range(deletedText, pos, nInserted, nDeleted, - &wrapModStart, &wrapModEnd, &linesInserted, &linesDeleted); - } else { - linesInserted = nInserted == 0 ? 0 : - buf->count_lines( pos, pos + nInserted ); - linesDeleted = nDeleted == 0 ? 0 : countlines( deletedText ); - } - + textD->mCursorPreferredXPos = -1; + + /* Count the number of lines inserted and deleted, and in the case + of continuous wrap mode, how much has changed */ + if (textD->mContinuousWrap && !textD->mFastDisplay) { + textD->find_wrap_range(deletedText, pos, nInserted, nDeleted, + &wrapModStart, &wrapModEnd, &linesInserted, &linesDeleted); + } else { + linesInserted = nInserted == 0 ? 0 : buf->count_lines( pos, pos + nInserted ); + linesDeleted = nDeleted == 0 ? 0 : countlines( deletedText ); + } + /* Update the line starts and mTopLineNum */ if ( nInserted != 0 || nDeleted != 0 ) { - if (textD->mContinuousWrap) { - textD->update_line_starts( wrapModStart, wrapModEnd-wrapModStart, - nDeleted + pos-wrapModStart + (wrapModEnd-(pos+nInserted)), - linesInserted, linesDeleted, &scrolled ); - } else { - textD->update_line_starts( pos, nInserted, nDeleted, linesInserted, - linesDeleted, &scrolled ); - } + if (textD->mContinuousWrap && !textD->mFastDisplay) { + textD->update_line_starts( wrapModStart, wrapModEnd-wrapModStart, + nDeleted + pos-wrapModStart + (wrapModEnd-(pos+nInserted)), + linesInserted, linesDeleted, &scrolled ); + } else { + textD->update_line_starts( pos, nInserted, nDeleted, linesInserted, + linesDeleted, &scrolled ); + } } else scrolled = 0; - - /* If we're counting non-wrapped lines as well, maintain the absolute - (non-wrapped) line number of the text displayed */ - if (textD->maintaining_absolute_top_line_number() && - (nInserted != 0 || nDeleted != 0)) { - if (pos + nDeleted < oldFirstChar) - textD->mAbsTopLineNum += buf->count_lines(pos, pos + nInserted) - - countlines(deletedText); - else if (pos < oldFirstChar) - textD->reset_absolute_top_line_number(); - } - + + /* If we're counting non-wrapped lines as well, maintain the absolute + (non-wrapped) line number of the text displayed */ + if (textD->maintaining_absolute_top_line_number() && + (nInserted != 0 || nDeleted != 0)) { + if (deletedText && (pos + nDeleted < oldFirstChar)) + textD->mAbsTopLineNum += buf->count_lines(pos, pos + nInserted) - + countlines(deletedText); + else if (pos < oldFirstChar) + textD->reset_absolute_top_line_number(); + } + /* Update the line count for the whole buffer */ textD->mNBufferLines += linesInserted - linesDeleted; - + /* Update the cursor position */ if ( textD->mCursorToHint != NO_HINT ) { textD->mCursorPos = textD->mCursorToHint; @@ -1265,13 +1489,13 @@ void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDelet else textD->mCursorPos += nInserted - nDeleted; } - + // refigure scrollbars & stuff textD->resize(textD->x(), textD->y(), textD->w(), textD->h()); - + // don't need to do anything else if not visible? if (!textD->visible_r()) return; - + /* If the changes caused scrolling, re-paint everything and we're done. */ if ( scrolled ) { textD->damage(FL_DAMAGE_EXPOSE); @@ -1279,135 +1503,176 @@ void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDelet textD->mStyleBuffer->primary_selection()->selected(0); return; } - + /* If the changes didn't cause scrolling, decide the range of characters - that need to be re-painted. Also if the cursor position moved, be - sure that the redisplay range covers the old cursor position so the - old cursor gets erased, and erase the bits of the cursor which extend - beyond the left and right edges of the text. */ - startDispPos = textD->mContinuousWrap ? wrapModStart : pos; + that need to be re-painted. Also if the cursor position moved, be + sure that the redisplay range covers the old cursor position so the + old cursor gets erased, and erase the bits of the cursor which extend + beyond the left and right edges of the text. */ + startDispPos = (textD->mContinuousWrap && !textD->mFastDisplay) ? wrapModStart : pos; + IS_UTF8_ALIGNED2(buf, startDispPos) + if ( origCursorPos == startDispPos && textD->mCursorPos != startDispPos ) - startDispPos = min( startDispPos, origCursorPos - 1 ); + startDispPos = min( startDispPos, buf->prev_char_clipped(origCursorPos) ); + IS_UTF8_ALIGNED2(buf, startDispPos) + if ( linesInserted == linesDeleted ) { if ( nInserted == 0 && nDeleted == 0 ) endDispPos = pos + nRestyled; else { endDispPos = textD->mContinuousWrap ? wrapModEnd : buf->line_end( pos + nInserted ) + 1; + if (textD->mContinuousWrap && !textD->mFastDisplay) + endDispPos = wrapModEnd; + else + endDispPos = buf->next_char(buf->line_end( pos + nInserted )); + // CET - FIXME if ( origCursorPos >= startDispPos && // ( origCursorPos <= endDispPos || endDispPos == buf->length() ) ) } - - if (linesInserted > 1) textD->draw_line_numbers(false); + + if (linesInserted > 1) textD->draw_line_numbers(false); } else { - endDispPos = textD->mLastChar + 1; + endDispPos = buf->next_char(textD->mLastChar); // CET - FIXME if ( origCursorPos >= pos ) - /* If more than one line is inserted/deleted, a line break may have - been inserted or removed in between, and the line numbers may - have changed. If only one line is altered, line numbers cannot - be affected (the insertion or removal of a line break always - results in at least two lines being redrawn). */ - textD->draw_line_numbers(false); + /* If more than one line is inserted/deleted, a line break may have + been inserted or removed in between, and the line numbers may + have changed. If only one line is altered, line numbers cannot + be affected (the insertion or removal of a line break always + results in at least two lines being redrawn). */ + textD->draw_line_numbers(false); } - + IS_UTF8_ALIGNED2(buf, startDispPos) + IS_UTF8_ALIGNED2(buf, endDispPos) + /* If there is a style buffer, check if the modification caused additional - changes that need to be redisplayed. (Redisplaying separately would - cause double-redraw on almost every modification involving styled - text). Extend the redraw range to incorporate style changes */ + changes that need to be redisplayed. (Redisplaying separately would + cause double-redraw on almost every modification involving styled + text). Extend the redraw range to incorporate style changes */ if ( textD->mStyleBuffer ) textD->extend_range_for_styles( &startDispPos, &endDispPos ); - + IS_UTF8_ALIGNED2(buf, startDispPos) + IS_UTF8_ALIGNED2(buf, endDispPos) + /* Redisplay computed range */ - textD->redisplay_range( startDispPos, endDispPos ); // FIXME utf8 + textD->redisplay_range( startDispPos, endDispPos ); } + + /** - In continuous wrap mode, internal line numbers are calculated after - wrapping. A separate non-wrapped line count is maintained when line - numbering is turned on. There is some performance cost to maintaining this - line count, so normally absolute line numbers are not tracked if line - numbering is off. This routine allows callers to specify that they still - want this line count maintained (for use via TextDPosToLineAndCol). - More specifically, this allows the line number reported in the statistics - line to be calibrated in absolute lines, rather than post-wrapped lines. -*/ + \brief Line numbering stuff, currently unused. + + In continuous wrap mode, internal line numbers are calculated after + wrapping. A separate non-wrapped line count is maintained when line + numbering is turned on. There is some performance cost to maintaining this + line count, so normally absolute line numbers are not tracked if line + numbering is off. This routine allows callers to specify that they still + want this line count maintained (for use via TextDPosToLineAndCol). + More specifically, this allows the line number reported in the statistics + line to be calibrated in absolute lines, rather than post-wrapped lines. + */ void Fl_Text_Display_mod::maintain_absolute_top_line_number(int state) { - mNeedAbsTopLineNum = state; - reset_absolute_top_line_number(); + mNeedAbsTopLineNum = state; + reset_absolute_top_line_number(); } + + /** - Returns the absolute (non-wrapped) line number of the first line displayed. - Returns 0 if the absolute top line number is not being maintained. -*/ + \brief Line numbering stuff, currently unused. + + Returns the absolute (non-wrapped) line number of the first line displayed. + Returns 0 if the absolute top line number is not being maintained. + */ int Fl_Text_Display_mod::get_absolute_top_line_number() const { - if (!mContinuousWrap) - return mTopLineNum; - if (maintaining_absolute_top_line_number()) - return mAbsTopLineNum; - return 0; + if (mFastDisplay || !mContinuousWrap) + return mTopLineNum; + if (maintaining_absolute_top_line_number()) + return mAbsTopLineNum; + return 0; } + + /** - Re-calculate absolute top line number for a change in scroll position. -*/ + \brief Line numbering stuff, currently unused. + + Re-calculate absolute top line number for a change in scroll position. + */ void Fl_Text_Display_mod::absolute_top_line_number(int oldFirstChar) { - if (maintaining_absolute_top_line_number()) { - if (mFirstChar < oldFirstChar) - mAbsTopLineNum -= buffer()->count_lines(mFirstChar, oldFirstChar); - else - mAbsTopLineNum += buffer()->count_lines(oldFirstChar, mFirstChar); - } + if (maintaining_absolute_top_line_number()) { + if (mFirstChar < oldFirstChar) + mAbsTopLineNum -= buffer()->count_lines(mFirstChar, oldFirstChar); + else + mAbsTopLineNum += buffer()->count_lines(oldFirstChar, mFirstChar); + } } + + /** - Return true if a separate absolute top line number is being maintained - (for displaying line numbers or showing in the statistics line). -*/ + \brief Line numbering stuff, currently unused. + + Return true if a separate absolute top line number is being maintained + (for displaying line numbers or showing in the statistics line). + */ int Fl_Text_Display_mod::maintaining_absolute_top_line_number() const { - return mContinuousWrap && - (mLineNumWidth != 0 || mNeedAbsTopLineNum); + return (!mFastDisplay && mContinuousWrap && (mLineNumWidth != 0 || mNeedAbsTopLineNum)); } + + /** - Count lines from the beginning of the buffer to reestablish the - absolute (non-wrapped) top line number. If mode is not continuous wrap, - or the number is not being maintained, does nothing. -*/ + \brief Line numbering stuff, probably unused. + + Count lines from the beginning of the buffer to reestablish the + absolute (non-wrapped) top line number. If mode is not continuous wrap, + or the number is not being maintained, does nothing. + */ void Fl_Text_Display_mod::reset_absolute_top_line_number() { - mAbsTopLineNum = 1; - absolute_top_line_number(0); + mAbsTopLineNum = 1; + absolute_top_line_number(0); } -/** - Find the line number of position "pos" relative to the first line of - displayed text. Returns 0 if the line is not displayed. -*/ -int Fl_Text_Display_mod::position_to_line( size_t pos, int *lineNum ) const { - int i; + +/** + \brief Convert a position index into a line number offset. + + Find the line number of position \p pos relative to the first line of + displayed text. Returns 0 if the line is not displayed. + + \param pos ?? + \param[out] lineNum ?? + \return ?? + \todo What does this do? + */ +int Fl_Text_Display_mod::position_to_line( int pos, int *lineNum ) const { + IS_UTF8_ALIGNED2(buffer(), pos) + + int i; + *lineNum = 0; - if ( pos < (unsigned int)mFirstChar ) return 0; - if ( pos > (unsigned int)mLastChar ) { + if ( pos < mFirstChar ) return 0; + if ( pos > mLastChar ) { if ( empty_vlines() ) { if ( mLastChar < mBuffer->length() ) { if ( !position_to_line( mLastChar, lineNum ) ) { -#ifdef DEBUG Fl::error("Fl_Text_Display_mod::position_to_line(): Consistency check ptvl failed"); -#endif return 0; } return ++( *lineNum ) <= mNVisibleLines - 1; } else { - position_to_line( mLastChar - 1, lineNum ); + position_to_line( buffer()->prev_char_clipped(mLastChar), lineNum ); return 1; } } return 0; } - + for ( i = mNVisibleLines - 1; i >= 0; i-- ) { - if ( mLineStarts[ i ] != -1 && pos >= (unsigned int)mLineStarts[ i ] ) { + if ( mLineStarts[ i ] != -1 && pos >= mLineStarts[ i ] ) { *lineNum = i; return 1; } @@ -1415,278 +1680,301 @@ int Fl_Text_Display_mod::position_to_line( size_t pos, int *lineNum ) const { return 0; /* probably never be reached */ } + /** - Draw the text on a single line represented by "visLineNum" (the - number of lines down from the top of the display), limited by - "leftClip" and "rightClip" window coordinates and "leftCharIndex" and - "rightCharIndex" character positions (not including the character at - position "rightCharIndex"). -*/ + Universal pixel machine. + + We use a single function that handles all line layout, measuring, and drawing + \li draw a text range + \li return the width of a text range in pixels + \li return the index of a character that is at a pixel position + + \param[in] mode DRAW_LINE, GET_WIDTH, FIND_INDEX + \param[in] lineStartPos index of first character + \param[in] lineLen size of string in bytes + \param[in] leftChar, rightChar + \param[in] Y drawing position + \param[in] bottomClip, leftClip, rightClip stop work when we reach the clipped + area. rightClip is the X position that we search in FIND_INDEX. + \retval DRAW_LINE index of last drawn character + \retval GET_WIDTH width in pixels of text segment if we would draw it + \retval FIND_INDEX index of character at given x position in window coordinates + \retval FIND_INDEX_FROM_ZERO index of character at given x position without scrolling and widget offsets + \todo we need to handle hidden hyphens and tabs here! + \todo we handle all styles and selections + \todo we must provide code to get pixel positions of the middle of a character as well + */ +int Fl_Text_Display_mod::handle_vline( + int mode, + int lineStartPos, int lineLen, int leftChar, int rightChar, + int Y, int bottomClip, + int leftClip, int rightClip) const +{ + IS_UTF8_ALIGNED2(buffer(), lineStartPos) + + // FIXME: we need to allow two modes for FIND_INDEX: one on the edge of the + // FIXME: character for selection, and one on the character center for cursors. + int i, X, startX, startIndex, style, charStyle; + char *lineStr; + + if ( lineStartPos == -1 ) { + lineStr = NULL; + } else { + lineStr = mBuffer->text_range( lineStartPos, lineStartPos + lineLen ); + } + + if (mode==GET_WIDTH) { + X = 0; + } else if (mode==FIND_INDEX_FROM_ZERO) { + X = 0; + mode = FIND_INDEX; + } else { + X = text_area.x - mHorizOffset; + } + + startX = X; + startIndex = 0; + if (!lineStr) { + // just clear the background + if (mode==DRAW_LINE) { + style = position_style(lineStartPos, lineLen, -1); + draw_string( style|BG_ONLY_MASK, text_area.x, Y, text_area.x+text_area.w, lineStr, lineLen ); + } + if (mode==FIND_INDEX) { + IS_UTF8_ALIGNED2(buffer(), lineStartPos) + return lineStartPos; + } + return 0; + } + + char currChar = 0, prevChar = 0; + // draw the line + style = position_style(lineStartPos, lineLen, 0); + for (i=0; itab_distance()); + int xAbs = (mode==GET_WIDTH) ? startX : startX+mHorizOffset-text_area.x; + w = (((xAbs/tab)+1)*tab) - xAbs; + if (mode==DRAW_LINE) + draw_string( style|BG_ONLY_MASK, startX, Y, startX+w, 0, 0 ); + if (mode==FIND_INDEX && startX+w>rightClip) { + // find x pos inside block + free(lineStr); + return lineStartPos + startIndex; + } + } else { + // draw a text segment + w = int( string_width( lineStr+startIndex, i-startIndex, style ) ); + if (mode==DRAW_LINE) + draw_string( style, startX, Y, startX+w, lineStr+startIndex, i-startIndex ); + if (mode==FIND_INDEX && startX+w>rightClip) { + // find x pos inside block + int di = find_x(lineStr+startIndex, i-startIndex, style, rightClip-startX); + free(lineStr); + IS_UTF8_ALIGNED2(buffer(), (lineStartPos+startIndex+di)) + return lineStartPos + startIndex + di; + } + } + style = charStyle; + startX += w; + startIndex = i; + } + i += len; + prevChar = currChar; + } + int w = 0; + if (currChar=='\t') { + // draw a single Tab space + int tab = (int)col_to_x(mBuffer->tab_distance()); + int xAbs = (mode==GET_WIDTH) ? startX : startX+mHorizOffset-text_area.x; + w = (((xAbs/tab)+1)*tab) - xAbs; + if (mode==DRAW_LINE) + draw_string( style|BG_ONLY_MASK, startX, Y, startX+w, 0, 0 ); + if (mode==FIND_INDEX) { + // find x pos inside block + free(lineStr); + return lineStartPos + startIndex + ( rightClip-startX>w ? 1 : 0 ); + } + } else { + w = int( string_width( lineStr+startIndex, i-startIndex, style ) ); + if (mode==DRAW_LINE) + draw_string( style, startX, Y, startX+w, lineStr+startIndex, i-startIndex ); + if (mode==FIND_INDEX) { + // find x pos inside block + int di = find_x(lineStr+startIndex, i-startIndex, style, rightClip-startX); + free(lineStr); + IS_UTF8_ALIGNED2(buffer(), (lineStartPos+startIndex+di)) + return lineStartPos + startIndex + di; + } + } + if (mode==GET_WIDTH) { + free(lineStr); + return startX+w; + } + + // clear the rest of the line + startX += w; + style = position_style(lineStartPos, lineLen, i); + if (mode==DRAW_LINE) + draw_string( style|BG_ONLY_MASK, startX, Y, text_area.x+text_area.w, lineStr, lineLen ); + + free(lineStr); + IS_UTF8_ALIGNED2(buffer(), (lineStartPos+lineLen)) + return lineStartPos + lineLen; +} + + +/** + \brief Find the index of the character that lies at the given x position. + \param s UTF-8 text string + \param len length of string + \param style index into style lookup table + \param x position in pixels + \return index into buffer + */ +int Fl_Text_Display_mod::find_x(const char *s, int len, int style, int x) const { + IS_UTF8_ALIGNED(s) + + // TODO: use binary search which may be quicker. + int i = 0; + while (ix) + return i; + i += cl; + } + return len; +} + + + +/** + \brief Draw a single line of text. + + Draw the text on a single line represented by \p visLineNum (the + number of lines down from the top of the display), limited by + \p leftClip and \p rightClip window coordinates and \p leftCharIndex and + \p rightCharIndex character positions (not including the character at + position \p rightCharIndex). + + \param visLineNum index of line in the visible line number lookup + \param leftClip, rightClip pixel position of clipped area + \param leftCharIndex, rightCharIndex index into line of segment that we want to draw + */ void Fl_Text_Display_mod::draw_vline(int visLineNum, int leftClip, int rightClip, int leftCharIndex, int rightCharIndex) { - Fl_Text_Buffer_mod * buf = mBuffer; - int i, X, Y, startX, charIndex, lineStartPos, lineLen, fontHeight; - int stdCharWidth, charWidth, startIndex, charStyle, style; - int charLen, outStartIndex, outIndex; - int dispIndexOffset; - char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ], outStr[ MAX_DISP_LINE_LEN ]; - char *outPtr; - const char *lineStr; - -// printf("draw_vline(visLineNum=%d, leftClip=%d, rightClip=%d, leftCharIndex=%d, rightCharIndex=%d)\n", -// visLineNum, leftClip, rightClip, leftCharIndex, rightCharIndex); -// printf("nNVisibleLines=%d\n", mNVisibleLines); - + int Y, lineStartPos, lineLen, fontHeight; + + // printf("draw_vline(visLineNum=%d, leftClip=%d, rightClip=%d, leftCharIndex=%d, rightCharIndex=%d)\n", + // visLineNum, leftClip, rightClip, leftCharIndex, rightCharIndex); + // printf("nNVisibleLines=%d\n", mNVisibleLines); + /* If line is not displayed, skip it */ if ( visLineNum < 0 || visLineNum >= mNVisibleLines ) return; - + /* Calculate Y coordinate of the string to draw */ fontHeight = mMaxsize; Y = text_area.y + visLineNum * fontHeight; - + /* Get the text, length, and buffer position of the line to display */ lineStartPos = mLineStarts[ visLineNum ]; -// printf("lineStartPos=%d\n", lineStartPos); if ( lineStartPos == -1 ) { lineLen = 0; - lineStr = NULL; } else { lineLen = vline_length( visLineNum ); - lineStr = buf->text_range( lineStartPos, lineStartPos + lineLen ); } - - /* Space beyond the end of the line is still counted in units of characters - of a standardized character width (this is done mostly because style - changes based on character position can still occur in this region due - to rectangular Fl_Text_Selections). stdCharWidth must be non-zero to - prevent a potential infinite loop if X does not advance */ - stdCharWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width; - if ( stdCharWidth <= 0 ) { - Fl::error("Fl_Text_Display_mod::draw_vline(): bad font measurement"); - if (lineStr) free((void *)lineStr); - return; - } - + /* Shrink the clipping range to the active display area */ leftClip = max( text_area.x, leftClip ); rightClip = min( rightClip, text_area.x + text_area.w ); - - /* Rectangular Fl_Text_Selections are based on "real" line starts (after - a newline or start of buffer). Calculate the difference between the - last newline position and the line start we're using. Since scanning - back to find a newline is expensive, only do so if there's actually a - rectangular Fl_Text_Selection which needs it */ - if (mContinuousWrap && (range_touches_selection(buf->primary_selection(), - lineStartPos, lineStartPos + lineLen) || range_touches_selection( - buf->secondary_selection(), lineStartPos, lineStartPos + lineLen) || - range_touches_selection(buf->highlight_selection(), lineStartPos, - lineStartPos + lineLen))) { - dispIndexOffset = buf->count_displayed_characters( - buf->line_start(lineStartPos), lineStartPos); - } else - dispIndexOffset = 0; - - /* Step through character positions from the beginning of the line (even if - that's off the left edge of the displayed area) to find the first - character position that's not clipped, and the X coordinate for drawing - that character */ - X = text_area.x - mHorizOffset; - outIndex = 0; - for ( charIndex = 0; ; charIndex += lineStr ? fl_utf8len(lineStr[charIndex]) : 1 ) { - charLen = charIndex >= lineLen ? 1 : - Fl_Text_Buffer_mod::expand_character( lineStr+charIndex, outIndex, - expandedChar, buf->tab_distance()); - style = position_style( lineStartPos, lineLen, charIndex, - outIndex + dispIndexOffset ); - charWidth = charIndex >= lineLen ? stdCharWidth : - string_width( expandedChar, charLen, style ); - if ( X + charWidth >= leftClip && charIndex >= leftCharIndex ) { - startIndex = charIndex; - outStartIndex = outIndex; - startX = X; - break; - } - X += charWidth; - outIndex += charLen; - } - - /* Scan character positions from the beginning of the clipping range, and - draw parts whenever the style changes (also note if the cursor is on - this line, and where it should be drawn to take advantage of the x - position which we've gone to so much trouble to calculate) */ - /* since characters between style may overlap, we draw the full - background first */ - int sX = startX; - outPtr = outStr; - outIndex = outStartIndex; - X = startX; - for (charIndex = startIndex; - charIndex < rightCharIndex; - charIndex += lineStr ? fl_utf8len(lineStr[charIndex]) : 1 ) - { - charLen = charIndex >= lineLen ? 1 : - Fl_Text_Buffer_mod::expand_character( lineStr+charIndex, outIndex, expandedChar, - buf->tab_distance()); - charStyle = position_style( lineStartPos, lineLen, charIndex, - outIndex + dispIndexOffset ); - for ( i = 0; i < charLen; i++ ) { // FIXME: this rips apart the utf-8 sequneces - if ( i != 0 && charIndex < lineLen && lineStr[ charIndex ] == '\t' ) - charStyle = position_style( lineStartPos, lineLen, - charIndex, outIndex + dispIndexOffset ); - if ( charStyle != style ) { - draw_string( style|BG_ONLY_MASK, sX, Y, X, outStr, outPtr - outStr ); - outPtr = outStr; - sX = X; - style = charStyle; - } - if ( charIndex < lineLen ) { - *outPtr = expandedChar[ i ]; - int l = 1; - if (*outPtr & 0x80) { - l = fl_utf8len(*outPtr); - if (l<=0) l = 1; - } - charWidth = string_width( &expandedChar[ i ], l, charStyle ); - } else - charWidth = stdCharWidth; - outPtr++; - X += charWidth; - outIndex++; - } - if ( outPtr - outStr + FL_TEXT_MAX_EXP_CHAR_LEN >= MAX_DISP_LINE_LEN || X >= rightClip ) - break; - } - draw_string( style|BG_ONLY_MASK, sX, Y, X, outStr, outPtr - outStr ); - - /* now draw the text over the previously erased background */ - outPtr = outStr; - outIndex = outStartIndex; - X = startX; - for (charIndex = startIndex; - charIndex < rightCharIndex; - charIndex += lineStr ? fl_utf8len(lineStr[charIndex]) : 0) - { - charLen = charIndex >= lineLen ? 1 : - Fl_Text_Buffer_mod::expand_character( lineStr+charIndex, outIndex, expandedChar, - buf->tab_distance()); - charStyle = position_style( lineStartPos, lineLen, charIndex, - outIndex + dispIndexOffset ); - for ( i = 0; i < charLen; i++ ) { // FIXME: this rips apart the utf-8 sequneces - if ( i != 0 && charIndex < lineLen && lineStr[ charIndex ] == '\t' ) - charStyle = position_style( lineStartPos, lineLen, - charIndex, outIndex + dispIndexOffset ); - if ( charStyle != style ) { - draw_string( style|TEXT_ONLY_MASK, startX, Y, X, outStr, outPtr - outStr ); - outPtr = outStr; - startX = X; - style = charStyle; - } - if ( charIndex < lineLen ) { - *outPtr = expandedChar[ i ]; - int l = 1; - if (*outPtr & 0x80) { - l = fl_utf8len(*outPtr); - if (l<=0) l = 1; - } - charWidth = string_width( &expandedChar[ i ], l, charStyle ); - } else - charWidth = stdCharWidth; - outPtr++; - X += charWidth; - outIndex++; - } - if ( outPtr - outStr + FL_TEXT_MAX_EXP_CHAR_LEN >= MAX_DISP_LINE_LEN || X >= rightClip ) - break; - } - - /* Draw the remaining style segment */ - draw_string( style|TEXT_ONLY_MASK, startX, Y, X, outStr, outPtr - outStr ); - - /* Draw the cursor if part of it appeared on the redisplayed part of - this line. Also check for the cases which are not caught as the - line is scanned above: when the cursor appears at the very end - of the redisplayed section. */ - /* CET - FIXME - if ( mCursorOn ) - { - if ( hasCursor ) - draw_cursor( cursorX, Y ); - else if ( charIndex < lineLen && ( lineStartPos + charIndex + 1 == cursorPos ) - && X == rightClip ) - { - if ( cursorPos >= buf->length() ) - draw_cursor( X - 1, Y ); - else - { - draw_cursor( X - 1, Y ); - } - } - } - */ - if ( lineStr != NULL ) - free((void *)lineStr); + + handle_vline(DRAW_LINE, + lineStartPos, lineLen, leftCharIndex, rightCharIndex, + Y, Y+fontHeight, leftClip, rightClip); + return; } -/** - Draw a string or blank area according to parameter "style", using the - appropriate colors and drawing method for that style, with top left - corner at X, y. If style says to draw text, use "string" as source of - characters, and draw "nChars", if style is FILL, erase - rectangle where text would have drawn from X to toX and from Y to - the maximum Y extent of the current font(s). -*/ -void Fl_Text_Display_mod::draw_string( int style, int X, int Y, int toX, - const char *string, int nChars ) { - const Style_Table_Entry * styleRec; + +/** + \brief Draw a text segment in a single style. + + Draw a string or blank area according to parameter \p style, using the + appropriate colors and drawing method for that style, with top left + corner at \p X, \p Y. If style says to draw text, use \p string as + source of characters, and draw \p nChars, if style is FILL, erase + rectangle where text would have drawn from \p X to \p toX and from + \p Y to the maximum y extent of the current font(s). + + \param style index into style lookup table + \param X, Y drawing origin + \param toX rightmost position if this is a fill operation + \param string text if this is a drawing operation + \param nChars number of characters to draw + */ +void Fl_Text_Display_mod::draw_string(int style, + int X, int Y, int toX, + const char *string, int nChars) const { + IS_UTF8_ALIGNED(string) + + const Style_Table_Entry * styleRec; + /* Draw blank area rather than text, if that was the request */ if ( style & FILL_MASK ) { if (style & TEXT_ONLY_MASK) return; clear_rect( style, X, Y, toX - X, mMaxsize ); return; } - /* Set font, color, and gc depending on style. For normal text, GCs - for normal drawing, or drawing within a Fl_Text_Selection or highlight are - pre-allocated and pre-configured. For syntax highlighting, GCs are - configured here, on the fly. */ - + for normal drawing, or drawing within a Fl_Text_Selection or highlight are + pre-allocated and pre-configured. For syntax highlighting, GCs are + configured here, on the fly. */ + Fl_Font font = textfont(); int fsize = textsize(); Fl_Color foreground; Fl_Color background; - + if ( style & STYLE_LOOKUP_MASK ) { int si = (style & STYLE_LOOKUP_MASK) - 'A'; if (si < 0) si = 0; else if (si >= mNStyles) si = mNStyles - 1; - + styleRec = mStyleTable + si; font = styleRec->font; fsize = styleRec->size; - + if (style & PRIMARY_MASK) { - if (Fl::focus() == this) background = selection_color(); + if (Fl::focus() == (Fl_Widget*)this) background = selection_color(); else background = fl_color_average(color(), selection_color(), 0.4f); } else if (style & HIGHLIGHT_MASK) { - if (Fl::focus() == this) background = fl_color_average(color(), selection_color(), 0.5f); + if (Fl::focus() == (Fl_Widget*)this) background = fl_color_average(color(), selection_color(), 0.5f); else background = fl_color_average(color(), selection_color(), 0.6f); } else background = color(); foreground = fl_contrast(styleRec->color, background); } else if (style & PRIMARY_MASK) { - if (Fl::focus() == this) background = selection_color(); + if (Fl::focus() == (Fl_Widget*)this) background = selection_color(); else background = fl_color_average(color(), selection_color(), 0.4f); foreground = fl_contrast(textcolor(), background); } else if (style & HIGHLIGHT_MASK) { - if (Fl::focus() == this) background = fl_color_average(color(), selection_color(), 0.5f); + if (Fl::focus() == (Fl_Widget*)this) background = fl_color_average(color(), selection_color(), 0.5f); else background = fl_color_average(color(), selection_color(), 0.6f); foreground = fl_contrast(textcolor(), background); } else { foreground = textcolor(); background = color(); } - + if (!(style & TEXT_ONLY_MASK)) { fl_color( background ); fl_rectf( X, Y, toX - X, mMaxsize ); @@ -1694,44 +1982,62 @@ void Fl_Text_Display_mod::draw_string( int style, int X, int Y, int toX, if (!(style & BG_ONLY_MASK)) { fl_color( foreground ); fl_font( font, fsize ); +#if !(defined(__APPLE__) || defined(WIN32)) && USE_XFT + // makes sure antialiased ÄÖÜ do not leak on line above + fl_push_clip(X, Y, toX - X, mMaxsize); +#endif fl_draw( string, nChars, X, Y + mMaxsize - fl_descent()); +#if !(defined(__APPLE__) || defined(WIN32)) && USE_XFT + fl_pop_clip(); +#endif } - + // CET - FIXME /* If any space around the character remains unfilled (due to use of - different sized fonts for highlighting), fill in above or below - to erase previously drawn characters */ + different sized fonts for highlighting), fill in above or below + to erase previously drawn characters */ /* - if (fs->ascent < mAscent) - clear_rect( style, X, Y, toX - X, mAscent - fs->ascent); - if (fs->descent < mDescent) - clear_rect( style, X, Y + mAscent + fs->descent, toX - x, - mDescent - fs->descent); - */ + if (fs->ascent < mAscent) + clear_rect( style, X, Y, toX - X, mAscent - fs->ascent); + if (fs->descent < mDescent) + clear_rect( style, X, Y + mAscent + fs->descent, toX - x, + mDescent - fs->descent); + */ /* Underline if style is secondary Fl_Text_Selection */ - + /* - if (style & SECONDARY_MASK) - XDrawLine(XtDisplay(mW), XtWindow(mW), gc, x, - y + mAscent, toX - 1, Y + fs->ascent); - */ + if (style & SECONDARY_MASK) + XDrawLine(XtDisplay(mW), XtWindow(mW), gc, x, + y + mAscent, toX - 1, Y + fs->ascent); + */ } + /** - Clear a rectangle with the appropriate background color for "style" -*/ -void Fl_Text_Display_mod::clear_rect( int style, int X, int Y, - int width, int height ) { + \brief Clear a rectangle with the appropriate background color for \p style. + + \param style index into style table + \param X, Y, width, height size and position of background area + */ +void Fl_Text_Display_mod::clear_rect(int style, + int X, int Y, + int width, int height) const { /* A width of zero means "clear to end of window" to XClearArea */ if ( width == 0 ) return; - - if ( Fl::focus() != this ) { - if (style & (HIGHLIGHT_MASK | PRIMARY_MASK)) { + + if (style & PRIMARY_MASK) { + if (Fl::focus()==(Fl_Widget*)this) { + fl_color(selection_color()); + } else { + fl_color(fl_color_average(color(), selection_color(), 0.4f)); + } + } else if (style & HIGHLIGHT_MASK) { + if (Fl::focus()==(Fl_Widget*)this) { fl_color(fl_color_average(color(), selection_color(), 0.5f)); } else { - fl_color( color() ); + fl_color(fl_color_average(color(), selection_color(), 0.6f)); } } else { fl_color( color() ); @@ -1740,10 +2046,14 @@ void Fl_Text_Display_mod::clear_rect( int style, int X, int Y, } + /** - Draw a cursor with top center at X, y. -*/ + \brief Draw a cursor with top center at \p X, \p Y. + + \param X, Y cursor position in pixels + */ void Fl_Text_Display_mod::draw_cursor( int X, int Y ) { + typedef struct { int x1, y1, x2, y2; } @@ -1756,17 +2066,17 @@ void Fl_Text_Display_mod::draw_cursor( int X, int Y ) { int nSegs = 0; int fontHeight = mMaxsize; int bot = Y + fontHeight - 1; - + if ( X < text_area.x - 1 || X > text_area.x + text_area.w ) return; - + /* For cursors other than the block, make them around 2/3 of a character - width, rounded to an even number of pixels so that X will draw an - odd number centered on the stem at x. */ + width, rounded to an even number of pixels so that X will draw an + odd number centered on the stem at x. */ cursorWidth = 4; //(fontWidth/3) * 2; left = X - cursorWidth / 2; right = left + cursorWidth; - + /* Create segments and draw cursor */ if ( mCursorStyle == CARET_CURSOR ) { midY = bot - fontHeight / 5; @@ -1802,72 +2112,89 @@ void Fl_Text_Display_mod::draw_cursor( int X, int Y ) { nSegs = 4; } fl_color( mCursor_color ); - + for ( int k = 0; k < nSegs; k++ ) { fl_line( segs[ k ].x1, segs[ k ].y1, segs[ k ].x2, segs[ k ].y2 ); } } + + /** - Determine the drawing method to use to draw a specific character from "buf". - "lineStartPos" gives the character index where the line begins, "lineIndex", - the number of characters past the beginning of the line, and "dispIndex", - the number of displayed characters past the beginning of the line. Passing - lineStartPos of -1 returns the drawing style for "no text". -** - Why not just: position_style(pos)? Because style applies to blank areas - of the window beyond the text boundaries, and because this routine must also - decide whether a position is inside of a rectangular Fl_Text_Selection, and do - so efficiently, without re-counting character positions from the start of the - line. -** - Note that style is a somewhat incorrect name, drawing method would - be more appropriate. -*/ -int Fl_Text_Display_mod::position_style( int lineStartPos, - int lineLen, int lineIndex, int dispIndex ) const { + \brief Find the correct style for a character. + + Determine the drawing method to use to draw a specific character from "buf". + \p lineStartPos gives the character index where the line begins, \p lineIndex, + the number of characters past the beginning of the line, and \p lineIndex + the number of displayed characters past the beginning of the line. Passing + \p lineStartPos of -1 returns the drawing style for "no text". + + Why not just: position_style(pos)? Because style applies to blank areas + of the window beyond the text boundaries, and because this routine must also + decide whether a position is inside of a rectangular Fl_Text_Selection, and do + so efficiently, without re-counting character positions from the start of the + line. + + Note that style is a somewhat incorrect name, drawing method would + be more appropriate. + + \param lineStartPos beginning of this line + \param lineLen number of bytes in line + \param lineIndex position of character within line + \return style for the given character + */ +int Fl_Text_Display_mod::position_style( int lineStartPos, int lineLen, int lineIndex) const +{ + IS_UTF8_ALIGNED2(buffer(), lineStartPos) + Fl_Text_Buffer_mod * buf = mBuffer; Fl_Text_Buffer_mod *styleBuf = mStyleBuffer; int pos, style = 0; - + if ( lineStartPos == -1 || buf == NULL ) return FILL_MASK; - + pos = lineStartPos + min( lineIndex, lineLen ); - + if ( lineIndex >= lineLen ) style = FILL_MASK; else if ( styleBuf != NULL ) { - // FIXME: character is ucs-4 - style = ( unsigned char ) styleBuf->character( pos ); + style = ( unsigned char ) styleBuf->byte_at( pos ); if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { - /* encountered "unfinished" style, trigger parsing */ - (mUnfinishedHighlightCB)( pos, mHighlightCBArg); - // FIXME: character is ucs-4 - style = (unsigned char) styleBuf->character( pos); + /* encountered "unfinished" style, trigger parsing */ + (mUnfinishedHighlightCB)( pos, mHighlightCBArg); + style = (unsigned char) styleBuf->byte_at( pos); } } - if (buf->primary_selection()->includes(pos, lineStartPos, dispIndex)) + if (buf->primary_selection()->includes(pos)) style |= PRIMARY_MASK; - if (buf->highlight_selection()->includes(pos, lineStartPos, dispIndex)) + if (buf->highlight_selection()->includes(pos)) style |= HIGHLIGHT_MASK; - if (buf->secondary_selection()->includes(pos, lineStartPos, dispIndex)) + if (buf->secondary_selection()->includes(pos)) style |= SECONDARY_MASK; return style; } -/** - Find the width of a string in the font of a particular style -*/ -int Fl_Text_Display_mod::string_width( const char *string, int length, int style ) const { - Fl_Font font; - int fsize; - if ( style & STYLE_LOOKUP_MASK ) { +/** + \brief Find the width of a string in the font of a particular style. + + \param string the text + \param length number of bytes in string + \param style index into style table + \return width of text segment in pixels + */ +double Fl_Text_Display_mod::string_width( const char *string, int length, int style ) const { + IS_UTF8_ALIGNED(string) + + Fl_Font font; + Fl_Fontsize fsize; + + if ( mNStyles && (style & STYLE_LOOKUP_MASK) ) { int si = (style & STYLE_LOOKUP_MASK) - 'A'; if (si < 0) si = 0; else if (si >= mNStyles) si = mNStyles - 1; - + font = mStyleTable[si].font; fsize = mStyleTable[si].size; } else { @@ -1875,23 +2202,28 @@ int Fl_Text_Display_mod::string_width( const char *string, int length, int style fsize = textsize(); } fl_font( font, fsize ); - - return ( int ) ( fl_width( string, length ) ); + return fl_width( string, length ); } -/** - Translate window coordinates to the nearest (insert cursor or character - cell) text position. The parameter posType specifies how to interpret the - position: CURSOR_POS means translate the coordinates to the nearest cursor - position, and CHARACTER_POS means return the position of the character - closest to (X, Y). -*/ -int Fl_Text_Display_mod::xy_to_position( int X, int Y, int posType ) const { - int charIndex, lineStart, lineLen, fontHeight; - int charWidth, charLen, charStyle, visLineNum, xStep, outIndex; - char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - const char *lineStr; + +/** + \brief Translate a pixel position into a character index. + + Translate window coordinates to the nearest (insert cursor or character + cell) text position. The parameter \p posType specifies how to interpret the + position: CURSOR_POS means translate the coordinates to the nearest cursor + position, and CHARACTER_POS means return the position of the character + closest to (\p X, \p Y). + + \param X, Y pixel position + \param posType CURSOR_POS or CHARACTER_POS + \return index into text buffer + */ +int Fl_Text_Display_mod::xy_to_position( int X, int Y, int posType ) const { + int lineStart, lineLen, fontHeight; + int visLineNum; + /* Find the visible line number corresponding to the Y coordinate */ fontHeight = mMaxsize; visLineNum = ( Y - text_area.y ) / fontHeight; @@ -1899,73 +2231,67 @@ int Fl_Text_Display_mod::xy_to_position( int X, int Y, int posType ) const { return mFirstChar; if ( visLineNum >= mNVisibleLines ) visLineNum = mNVisibleLines - 1; - + /* Find the position at the start of the line */ lineStart = mLineStarts[ visLineNum ]; - + /* If the line start was empty, return the last position in the buffer */ if ( lineStart == -1 ) return mBuffer->length(); - + /* Get the line text and its length */ lineLen = vline_length( visLineNum ); - lineStr = mBuffer->text_range( lineStart, lineStart + lineLen ); - - /* Step through character positions from the beginning of the line - to find the character position corresponding to the X coordinate */ - xStep = text_area.x - mHorizOffset; - outIndex = 0; - for (charIndex = 0; - charIndex < lineLen; - charIndex += fl_utf8len(lineStr[charIndex]) ) - { - charLen = Fl_Text_Buffer_mod::expand_character( lineStr+charIndex, outIndex, expandedChar, - mBuffer->tab_distance()); - charStyle = position_style( lineStart, lineLen, charIndex, outIndex ); - charWidth = string_width( expandedChar, charLen, charStyle ); - if ( X < xStep + ( posType == CURSOR_POS ? charWidth / 2 : charWidth ) ) { - free((char *)lineStr); - return lineStart + charIndex; - } - xStep += charWidth; - outIndex += charLen; - } - - /* If the X position was beyond the end of the line, return the position - of the newline at the end of the line */ - free((char *)lineStr); - return lineStart + lineLen; + + return handle_vline(FIND_INDEX, + lineStart, lineLen, 0, 0, + 0, 0, + text_area.x, X); } + + /** - Translate window coordinates to the nearest row and column number for - positioning the cursor. This, of course, makes no sense when the font is - proportional, since there are no absolute columns. The parameter posType - specifies how to interpret the position: CURSOR_POS means translate the - coordinates to the nearest position between characters, and CHARACTER_POS - means translate the position to the nearest character cell. -*/ + \brief Translate pixel coordinates into row and column. + + Translate window coordinates to the nearest row and column number for + positioning the cursor. This, of course, makes no sense when the font is + proportional, since there are no absolute columns. The parameter posType + specifies how to interpret the position: CURSOR_POS means translate the + coordinates to the nearest position between characters, and CHARACTER_POS + means translate the position to the nearest character cell. + + \param X, Y pixel coordinates + \param[out] row, column neares row and column + \param posType CURSOR_POS or CHARACTER_POS + */ void Fl_Text_Display_mod::xy_to_rowcol( int X, int Y, int *row, - int *column, int posType ) const { + int *column, int posType ) const { int fontHeight = mMaxsize; int fontWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width; - + /* Find the visible line number corresponding to the Y coordinate */ *row = ( Y - text_area.y ) / fontHeight; - if ( *row < 0 ) * row = 0; - if ( *row >= mNVisibleLines ) * row = mNVisibleLines - 1; + if ( *row < 0 ) *row = 0; + if ( *row >= mNVisibleLines ) *row = mNVisibleLines - 1; + *column = ( ( X - text_area.x ) + mHorizOffset + - ( posType == CURSOR_POS ? fontWidth / 2 : 0 ) ) / fontWidth; + ( posType == CURSOR_POS ? fontWidth / 2 : 0 ) ) / fontWidth; if ( *column < 0 ) * column = 0; } + + /** - Offset the line starts array, mTopLineNum, mFirstChar and lastChar, for a new - vertical scroll position given by newTopLineNum. If any currently displayed - lines will still be visible, salvage the line starts values, otherwise, - count lines from the nearest known line start (start or end of buffer, or - the closest value in the mLineStarts array) -*/ + \brief Offset line start counters for a new vertical scroll position. + + Offset the line starts array, mTopLineNum, mFirstChar and lastChar, for a new + vertical scroll position given by newTopLineNum. If any currently displayed + lines will still be visible, salvage the line starts values, otherwise, + count lines from the nearest known line start (start or end of buffer, or + the closest value in the mLineStarts array) + + \param newTopLineNum index into buffer + */ void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { int oldTopLineNum = mTopLineNum; int oldFirstChar = mFirstChar; @@ -1974,14 +2300,14 @@ void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { int *lineStarts = mLineStarts; int i, lastLineNum; Fl_Text_Buffer_mod *buf = mBuffer; - + /* If there was no offset, nothing needs to be changed */ if ( lineDelta == 0 ) return; - + /* Find the new value for mFirstChar by counting lines from the nearest - known line start (start or end of buffer, or the closest value in the - lineStarts array) */ + known line start (start or end of buffer, or the closest value in the + lineStarts array) */ lastLineNum = oldTopLineNum + nVisLines - 1; if ( newTopLineNum < oldTopLineNum && newTopLineNum < -lineDelta ) { mFirstChar = skip_lines( 0, newTopLineNum - 1, true ); @@ -1991,11 +2317,11 @@ void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { mFirstChar = lineStarts[ newTopLineNum - oldTopLineNum ]; } else if ( newTopLineNum - lastLineNum < mNBufferLines - newTopLineNum ) { mFirstChar = skip_lines( lineStarts[ nVisLines - 1 ], - newTopLineNum - lastLineNum, true ); + newTopLineNum - lastLineNum, true ); } else { mFirstChar = rewind_lines( buf->length(), mNBufferLines - newTopLineNum + 1 ); } - + /* Fill in the line starts array */ if ( lineDelta < 0 && -lineDelta < nVisLines ) { for ( i = nVisLines - 1; i >= -lineDelta; i-- ) @@ -2007,32 +2333,46 @@ void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { calc_line_starts( nVisLines - lineDelta, nVisLines - 1 ); } else calc_line_starts( 0, nVisLines ); - + /* Set lastChar and mTopLineNum */ calc_last_char(); mTopLineNum = newTopLineNum; - - /* If we're numbering lines or being asked to maintain an absolute line - number, re-calculate the absolute line number */ - absolute_top_line_number(oldFirstChar); + + /* If we're numbering lines or being asked to maintain an absolute line + number, re-calculate the absolute line number */ + absolute_top_line_number(oldFirstChar); } + + /** - Update the line starts array, mTopLineNum, mFirstChar and lastChar for text - display "textD" after a modification to the text buffer, given by the - position where the change began "pos", and the nmubers of characters - and lines inserted and deleted. -*/ -void Fl_Text_Display_mod::update_line_starts( int pos, int charsInserted, - int charsDeleted, int linesInserted, int linesDeleted, int *scrolled ) { - int * lineStarts = mLineStarts; + \brief Update line start arrays and variables. + + Update the line starts array, mTopLineNum, mFirstChar and lastChar for this + text display after a modification to the text buffer, given by the + position \p pos where the change began, and the numbers of characters + and lines inserted and deleted. + + \param pos index into buffer of recent changes + \param charsInserted number of bytes(!) inserted + \param charsDeleted number of bytes(!) deleted + \param linesInserted number of lines + \param linesDeleted number of lines + \param[out] scrolled set to 1 if the text display needs to be scrolled + */ +void Fl_Text_Display_mod::update_line_starts(int pos, int charsInserted, + int charsDeleted, int linesInserted, + int linesDeleted, int *scrolled ) { + IS_UTF8_ALIGNED2(buffer(), pos) + + int *lineStarts = mLineStarts; int i, lineOfPos, lineOfEnd, nVisLines = mNVisibleLines; int charDelta = charsInserted - charsDeleted; int lineDelta = linesInserted - linesDeleted; - + /* If all of the changes were before the displayed text, the display - doesn't change, just update the top line num and offset the line - start entries and first and last characters */ + doesn't change, just update the top line num and offset the line + start entries and first and last characters */ if ( pos + charsDeleted < mFirstChar ) { mTopLineNum += lineDelta; for ( i = 0; i < nVisLines && lineStarts[i] != -1; i++ ) @@ -2042,16 +2382,15 @@ void Fl_Text_Display_mod::update_line_starts( int pos, int charsInserted, *scrolled = 0; return; } - + /* The change began before the beginning of the displayed text, but - part or all of the displayed text was deleted */ + part or all of the displayed text was deleted */ if ( pos < mFirstChar ) { /* If some text remains in the window, anchor on that */ if ( position_to_line( pos + charsDeleted, &lineOfEnd ) && - ++lineOfEnd < nVisLines && lineStarts[ lineOfEnd ] != -1 ) { + ++lineOfEnd < nVisLines && lineStarts[ lineOfEnd ] != -1 ) { mTopLineNum = max( 1, mTopLineNum + lineDelta ); - mFirstChar = rewind_lines( - lineStarts[ lineOfEnd ] + charDelta, lineOfEnd ); + mFirstChar = rewind_lines(lineStarts[ lineOfEnd ] + charDelta, lineOfEnd ); /* Otherwise anchor on original line number and recount everything */ } else { if ( mTopLineNum > mNBufferLines + lineDelta ) { @@ -2066,12 +2405,12 @@ void Fl_Text_Display_mod::update_line_starts( int pos, int charsInserted, *scrolled = 1; return; } - + /* If the change was in the middle of the displayed text (it usually is), - salvage as much of the line starts array as possible by moving and - offsetting the entries after the changed area, and re-counting the - added lines or the lines beyond the salvaged part of the line starts - array */ + salvage as much of the line starts array as possible by moving and + offsetting the entries after the changed area, and re-counting the + added lines or the lines beyond the salvaged part of the line starts + array */ if ( pos <= mLastChar ) { /* find line on which the change began */ position_to_line( pos, &lineOfPos ); @@ -2082,11 +2421,11 @@ void Fl_Text_Display_mod::update_line_starts( int pos, int charsInserted, } else if ( lineDelta > 0 ) { for ( i = nVisLines - 1; i >= lineOfPos + lineDelta + 1; i-- ) lineStarts[ i ] = lineStarts[ i - lineDelta ] + - ( lineStarts[ i - lineDelta ] == -1 ? 0 : charDelta ); + ( lineStarts[ i - lineDelta ] == -1 ? 0 : charDelta ); } else /* (lineDelta < 0) */ { for ( i = max( 0, lineOfPos + 1 ); i < nVisLines + lineDelta; i++ ) lineStarts[ i ] = lineStarts[ i - lineDelta ] + - ( lineStarts[ i - lineDelta ] == -1 ? 0 : charDelta ); + ( lineStarts[ i - lineDelta ] == -1 ? 0 : charDelta ); } /* fill in the missing line starts */ if ( linesInserted >= 0 ) @@ -2098,9 +2437,9 @@ void Fl_Text_Display_mod::update_line_starts( int pos, int charsInserted, *scrolled = 0; return; } - + /* Change was past the end of the displayed text, but displayable by virtue - of being an insert at the end of the buffer into visible blank lines */ + of being an insert at the end of the buffer into visible blank lines */ if ( empty_vlines() ) { position_to_line( pos, &lineOfPos ); calc_line_starts( lineOfPos, lineOfPos + linesInserted ); @@ -2108,24 +2447,30 @@ void Fl_Text_Display_mod::update_line_starts( int pos, int charsInserted, *scrolled = 0; return; } - + /* Change was beyond the end of the buffer and not visible, do nothing */ *scrolled = 0; } + + /** - Scan through the text in the "textD"'s buffer and recalculate the line - starts array values beginning at index "startLine" and continuing through - (including) "endLine". It assumes that the line starts entry preceding - "startLine" (or mFirstChar if startLine is 0) is good, and re-counts - newlines to fill in the requested entries. Out of range values for - "startLine" and "endLine" are acceptable. -*/ + \brief Update the line start arrays. + + Scan through the text in the "textD"'s buffer and recalculate the line + starts array values beginning at index "startLine" and continuing through + (including) "endLine". It assumes that the line starts entry preceding + "startLine" (or mFirstChar if startLine is 0) is good, and re-counts + newlines to fill in the requested entries. Out of range values for + "startLine" and "endLine" are acceptable. + + \param startLine, endLine range of lines to scan as line numbers + */ void Fl_Text_Display_mod::calc_line_starts( int startLine, int endLine ) { int startPos, bufLen = mBuffer->length(); int line, lineEnd, nextLineStart, nVis = mNVisibleLines; int *lineStarts = mLineStarts; - + /* Clean up (possibly) messy input parameters */ if ( endLine < 0 ) endLine = 0; if ( endLine >= nVis ) endLine = nVis - 1; @@ -2133,34 +2478,34 @@ void Fl_Text_Display_mod::calc_line_starts( int startLine, int endLine ) { if ( startLine >= nVis ) startLine = nVis - 1; if ( startLine > endLine ) return; - + /* Find the last known good line number -> position mapping */ if ( startLine == 0 ) { lineStarts[ 0 ] = mFirstChar; startLine = 1; } startPos = lineStarts[ startLine - 1 ]; - + /* If the starting position is already past the end of the text, - fill in -1's (means no text on line) and return */ + fill in -1's (means no text on line) and return */ if ( startPos == -1 ) { for ( line = startLine; line <= endLine; line++ ) lineStarts[ line ] = -1; return; } - + /* Loop searching for ends of lines and storing the positions of the - start of the next line in lineStarts */ + start of the next line in lineStarts */ for ( line = startLine; line <= endLine; line++ ) { find_line_end(startPos, true, &lineEnd, &nextLineStart); startPos = nextLineStart; if ( startPos >= bufLen ) { /* If the buffer ends with a newline or line break, put - buf->length() in the next line start position (instead of - a -1 which is the normal marker for an empty line) to - indicate that the cursor may safely be displayed there */ + buf->length() in the next line start position (instead of + a -1 which is the normal marker for an empty line) to + indicate that the cursor may safely be displayed there */ if ( line == 0 || ( lineStarts[ line - 1 ] != bufLen && - lineEnd != nextLineStart ) ) { + lineEnd != nextLineStart ) ) { lineStarts[ line ] = bufLen; line++; } @@ -2168,153 +2513,190 @@ void Fl_Text_Display_mod::calc_line_starts( int startLine, int endLine ) { } lineStarts[ line ] = startPos; } - + /* Set any entries beyond the end of the text to -1 */ for ( ; line <= endLine; line++ ) lineStarts[ line ] = -1; } + + /** - Given a Fl_Text_Display with a complete, up-to-date lineStarts array, update - the lastChar entry to point to the last buffer position displayed. -*/ + \brief Update last display character index. + + Given a Fl_Text_Display_mod with a complete, up-to-date lineStarts array, update + the lastChar entry to point to the last buffer position displayed. + */ void Fl_Text_Display_mod::calc_last_char() { int i; for (i = mNVisibleLines - 1; i >= 0 && mLineStarts[i] == -1; i--) ; mLastChar = i < 0 ? 0 : line_end(mLineStarts[i], true); } -/** Scrolls the current buffer to start at the specified line and column.*/ + + +/** + \brief Scrolls the current buffer to start at the specified line and column. + \param topLineNum top line number + \param horizOffset column number + \todo Column numbers make little sense here. + */ void Fl_Text_Display_mod::scroll(int topLineNum, int horizOffset) { mTopLineNumHint = topLineNum; mHorizOffsetHint = horizOffset; resize(x(), y(), w(), h()); } -void Fl_Text_Display_mod::scroll_(int topLineNum, int horizOffset) { + + +/** + \brief Scrolls the current buffer to start at the specified line and column. + \param topLineNum top line number + \param horizOffset in pixels + \return 0 if nothing changed, 1 if we scrolled + */ +int Fl_Text_Display_mod::scroll_(int topLineNum, int horizOffset) { /* Limit the requested scroll position to allowable values */ if (topLineNum > mNBufferLines + 3 - mNVisibleLines) topLineNum = mNBufferLines + 3 - mNVisibleLines; if (topLineNum < 1) topLineNum = 1; - + if (horizOffset > longest_vline() - text_area.w) horizOffset = longest_vline() - text_area.w; if (horizOffset < 0) horizOffset = 0; - + /* Do nothing if scroll position hasn't actually changed or there's no - window to draw in yet */ + window to draw in yet */ if (mHorizOffset == horizOffset && mTopLineNum == topLineNum) - return; - + return 0; + /* If the vertical scroll position has changed, update the line - starts array and related counters in the text display */ + starts array and related counters in the text display */ offset_line_starts(topLineNum); - + /* Just setting mHorizOffset is enough information for redisplay */ mHorizOffset = horizOffset; - + // redraw all text damage(FL_DAMAGE_EXPOSE); + return 1; } + + /** - Update the minimum, maximum, slider size, page increment, and value - for vertical scroll bar. -*/ + \brief Update vertical scrollbar. + + Update the minimum, maximum, slider size, page increment, and value + for vertical scrollbar. + */ void Fl_Text_Display_mod::update_v_scrollbar() { - /* The Vert. scroll bar value and slider size directly represent the top - line number, and the number of visible lines respectively. The scroll - bar maximum value is chosen to generally represent the size of the whole - buffer, with minor adjustments to keep the scroll bar widget happy */ + /* The vertical scrollbar value and slider size directly represent the top + line number, and the number of visible lines respectively. The scroll + bar maximum value is chosen to generally represent the size of the whole + buffer, with minor adjustments to keep the scrollbar widget happy */ #ifdef DEBUG printf("Fl_Text_Display_mod::update_v_scrollbar():\n" " mTopLineNum=%d, mNVisibleLines=%d, mNBufferLines=%d\n", mTopLineNum, mNVisibleLines, mNBufferLines); #endif // DEBUG - - mVScrollBar->value(mTopLineNum, mNVisibleLines, 1, mNBufferLines+2); + + mVScrollBar->value(mTopLineNum, mNVisibleLines, 1, mNBufferLines + 2); mVScrollBar->linesize(3); } + /** - Update the minimum, maximum, slider size, page increment, and value - for the horizontal scroll bar. -*/ + \brief Update vertical scrollbar. + + Update the minimum, maximum, slider size, page increment, and value + for the horizontal scrollbar. + */ void Fl_Text_Display_mod::update_h_scrollbar() { int sliderMax = max(longest_vline(), text_area.w + mHorizOffset); mHScrollBar->value( mHorizOffset, text_area.w, 0, sliderMax ); } + + /** - Callbacks for drag or valueChanged on scroll bars -*/ + \brief Callbacks for drag or valueChanged on scrollbars. + */ void Fl_Text_Display_mod::v_scrollbar_cb(Fl_Scrollbar* b, Fl_Text_Display_mod* textD) { if (b->value() == textD->mTopLineNum) return; textD->scroll(b->value(), textD->mHorizOffset); } + + +/** + \brief Callbacks for drag or valueChanged on scrollbars. + */ void Fl_Text_Display_mod::h_scrollbar_cb(Fl_Scrollbar* b, Fl_Text_Display_mod* textD) { if (b->value() == textD->mHorizOffset) return; textD->scroll(textD->mTopLineNum, b->value()); } + + /** - Refresh the line number area. If clearAll is False, writes only over - the character cell areas. Setting clearAll to True will clear out any - stray marks outside of the character cell area, which might have been - left from before a resize or font change. -*/ + \brief Refresh the line number area. + + If clearAll is False, writes only over the character cell areas. Setting + clearAll to True will clear out any stray marks outside of the character cell + area, which might have been left from before a resize or font change. + + This function is not used. + */ void Fl_Text_Display_mod::draw_line_numbers(bool /*clearAll*/) { #if 0 - // FIXME: don't want this yet, so will leave for another time - - int y, line, visLine, nCols, lineStart; - char lineNumString[12]; - int lineHeight = mMaxsize ? mMaxsize : textsize_; - int charWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width; - - /* Don't draw if mLineNumWidth == 0 (line numbers are hidden), or widget is - not yet realized */ - if (mLineNumWidth == 0 || visible_r()) - return; - - /* GC is allocated on demand, since not everyone will use line numbering */ - if (textD->lineNumGC == NULL) { - XGCValues values; - values.foreground = textD->lineNumFGPixel; - values.background = textD->bgPixel; - values.font = textD->fontStruct->fid; - textD->lineNumGC = XtGetGC(textD->w, - GCFont| GCForeground | GCBackground, &values); - } - - /* Erase the previous contents of the line number area, if requested */ - if (clearAll) - XClearArea(XtDisplay(textD->w), XtWindow(textD->w), textD->lineNumLeft, - textD->top, textD->lineNumWidth, textD->height, False); - - /* Draw the line numbers, aligned to the text */ - nCols = min(11, textD->lineNumWidth / charWidth); - y = textD->top; - line = getAbsTopLineNum(textD); - for (visLine=0; visLine < textD->nVisibleLines; visLine++) { - lineStart = textD->lineStarts[visLine]; - if (lineStart != -1 && (lineStart==0 || - BufGetCharacter(textD->buffer, lineStart-1)=='\n')) { - sprintf(lineNumString, "%*d", nCols, line); - XDrawImageString(XtDisplay(textD->w), XtWindow(textD->w), - textD->lineNumGC, textD->lineNumLeft, y + textD->ascent, - lineNumString, strlen(lineNumString)); - line++; - } else { - XClearArea(XtDisplay(textD->w), XtWindow(textD->w), - textD->lineNumLeft, y, textD->lineNumWidth, - textD->ascent + textD->descent, False); - if (visLine == 0) - line++; - } - y += lineHeight; + int y, line, visLine, nCols, lineStart; + char lineNumString[12]; + int lineHeight = mMaxsize ? mMaxsize : textsize_; + int charWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width; + + /* Don't draw if mLineNumWidth == 0 (line numbers are hidden), or widget is + not yet realized */ + if (mLineNumWidth == 0 || visible_r()) + return; + + /* GC is allocated on demand, since not everyone will use line numbering */ + if (textD->lineNumGC == NULL) { + XGCValues values; + values.foreground = textD->lineNumFGPixel; + values.background = textD->bgPixel; + values.font = textD->fontStruct->fid; + textD->lineNumGC = XtGetGC(textD->w, + GCFont| GCForeground | GCBackground, &values); + } + + /* Erase the previous contents of the line number area, if requested */ + if (clearAll) + XClearArea(XtDisplay(textD->w), XtWindow(textD->w), textD->lineNumLeft, + textD->top, textD->lineNumWidth, textD->height, False); + + /* Draw the line numbers, aligned to the text */ + nCols = min(11, textD->lineNumWidth / charWidth); + y = textD->top; + line = getAbsTopLineNum(textD); + for (visLine=0; visLine < textD->nVisibleLines; visLine++) { + lineStart = textD->lineStarts[visLine]; + if (lineStart != -1 && (lineStart==0 || + BufGetCharacter(textD->buffer, lineStart-1)=='\n')) { + sprintf(lineNumString, "%*d", nCols, line); + XDrawImageString(XtDisplay(textD->w), XtWindow(textD->w), + textD->lineNumGC, textD->lineNumLeft, y + textD->ascent, + lineNumString, strlen(lineNumString)); + line++; + } else { + XClearArea(XtDisplay(textD->w), XtWindow(textD->w), + textD->lineNumLeft, y, textD->lineNumWidth, + textD->ascent + textD->descent, False); + if (visLine == 0) + line++; } + y += lineHeight; + } #endif } @@ -2326,652 +2708,713 @@ static int min( int i1, int i2 ) { return i1 <= i2 ? i1 : i2; } + + /** - Count the number of newlines in a null-terminated text string; -*/ + Count the number of newlines in a null-terminated text string; + */ static int countlines( const char *string ) { + IS_UTF8_ALIGNED(string) + const char * c; int lineCount = 0; - + if (!string) return 0; - + for ( c = string; *c != '\0'; c++ ) if ( *c == '\n' ) lineCount++; return lineCount; } + + + /** - Return the width in pixels of the displayed line pointed to by "visLineNum" -*/ + \brief Returns the width in pixels of the displayed line pointed to by "visLineNum". + \param visLineNum index into visible lines array + \return width of line in pixels + */ int Fl_Text_Display_mod::measure_vline( int visLineNum ) const { - int i, width = 0, len, style, lineLen = vline_length( visLineNum ); - int charCount = 0, lineStartPos = mLineStarts[ visLineNum ]; - char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - + int lineLen = vline_length( visLineNum ); + int lineStartPos = mLineStarts[ visLineNum ]; if (lineStartPos < 0 || lineLen == 0) return 0; - if ( mStyleBuffer == NULL ) { - for ( i = 0; i < lineLen; i++ ) { - len = mBuffer->expand_character( lineStartPos + i, - charCount, expandedChar ); - - fl_font( textfont(), textsize() ); - - width += ( int ) fl_width( expandedChar, len ); - - charCount += len; - } - } else { - for ( i = 0; i < lineLen; i++ ) { - len = mBuffer->expand_character( lineStartPos + i, - charCount, expandedChar ); - // FIXME: character is ucs-4 - style = ( unsigned char ) mStyleBuffer->character( - lineStartPos + i ) - 'A'; - - if (style < 0) style = 0; - else if (style >= mNStyles) style = mNStyles - 1; - - fl_font( mStyleTable[ style ].font, mStyleTable[ style ].size ); - - width += ( int ) fl_width( expandedChar, len ); - - charCount += len; - } - } - return width; + return handle_vline(GET_WIDTH, lineStartPos, lineLen, 0, 0, 0, 0, 0, 0); } + + /** - Return true if there are lines visible with no corresponding buffer text -*/ + \brief Return true if there are lines visible with no corresponding buffer text. + \return 1 if there are empty lines + */ int Fl_Text_Display_mod::empty_vlines() const { - return mNVisibleLines > 0 && - mLineStarts[ mNVisibleLines - 1 ] == -1; + return (mNVisibleLines > 0) && (mLineStarts[ mNVisibleLines - 1 ] == -1); } + + /** - Return the length of a line (number of displayable characters) by examining - entries in the line starts array rather than by scanning for newlines -*/ + \brief Count number of bytes in a visible line. + + Return the length of a line (number of bytes) by examining + entries in the line starts array rather than by scanning for newlines. + + \param visLineNum index of line in visible line array + \return number of bytes in this line + */ int Fl_Text_Display_mod::vline_length( int visLineNum ) const { int nextLineStart, lineStartPos; - + if (visLineNum < 0 || visLineNum >= mNVisibleLines) return (0); - + lineStartPos = mLineStarts[ visLineNum ]; - + if ( lineStartPos == -1 ) return 0; + if ( visLineNum + 1 >= mNVisibleLines ) return mLastChar - lineStartPos; + nextLineStart = mLineStarts[ visLineNum + 1 ]; if ( nextLineStart == -1 ) return mLastChar - lineStartPos; - if (wrap_uses_character(nextLineStart-1)) - return nextLineStart-1 - lineStartPos; + + int nextLineStartMinus1 = buffer()->prev_char(nextLineStart); + if (wrap_uses_character(nextLineStartMinus1)) + return nextLineStartMinus1 - lineStartPos; + return nextLineStart - lineStartPos; } + + /** - When continuous wrap is on, and the user inserts or deletes characters, - wrapping can happen before and beyond the changed position. This routine - finds the extent of the changes, and counts the deleted and inserted lines - over that range. It also attempts to minimize the size of the range to - what has to be counted and re-displayed, so the results can be useful - both for delimiting where the line starts need to be recalculated, and - for deciding what part of the text to redisplay. -*/ + \brief Wrapping calculations. + + When continuous wrap is on, and the user inserts or deletes characters, + wrapping can happen before and beyond the changed position. This routine + finds the extent of the changes, and counts the deleted and inserted lines + over that range. It also attempts to minimize the size of the range to + what has to be counted and re-displayed, so the results can be useful + both for delimiting where the line starts need to be recalculated, and + for deciding what part of the text to redisplay. + + \param deletedText + \param pos + \param nInserted + \param nDeleted + \param modRangeStart + \param modRangeEnd + \param linesInserted + \param linesDeleted + */ void Fl_Text_Display_mod::find_wrap_range(const char *deletedText, int pos, - int nInserted, int nDeleted, int *modRangeStart, int *modRangeEnd, - int *linesInserted, int *linesDeleted) { - int length, retPos, retLines, retLineStart, retLineEnd; - Fl_Text_Buffer_mod *deletedTextBuf, *buf = buffer(); - int nVisLines = mNVisibleLines; - int *lineStarts = mLineStarts; - int countFrom, countTo, lineStart, adjLineStart, i; - int visLineNum = 0, nLines = 0; - - /* - ** Determine where to begin searching: either the previous newline, or - ** if possible, limit to the start of the (original) previous displayed - ** line, using information from the existing line starts array - */ - if (pos >= mFirstChar && pos <= mLastChar) { - for (i=nVisLines-1; i>0; i--) - if (lineStarts[i] != -1 && pos >= lineStarts[i]) - break; - if (i > 0) { - countFrom = lineStarts[i-1]; - visLineNum = i-1; - } else - countFrom = buf->line_start(pos); - } else - countFrom = buf->line_start(pos); + int nInserted, int nDeleted, + int *modRangeStart, int *modRangeEnd, + int *linesInserted, int *linesDeleted) { + IS_UTF8_ALIGNED(deletedText) + IS_UTF8_ALIGNED2(buffer(), pos) - - /* - ** Move forward through the (new) text one line at a time, counting - ** displayed lines, and looking for either a real newline, or for the - ** line starts to re-sync with the original line starts array - */ - lineStart = countFrom; - *modRangeStart = countFrom; - for (;;) { - - /* advance to the next line. If the line ended in a real newline - or the end of the buffer, that's far enough */ - wrapped_line_counter(buf, lineStart, buf->length(), 1, true, 0, - &retPos, &retLines, &retLineStart, &retLineEnd); - if (retPos >= buf->length()) { - countTo = buf->length(); - *modRangeEnd = countTo; - if (retPos != retLineEnd) - nLines++; - break; - } else - lineStart = retPos; - nLines++; - if (lineStart > pos + nInserted && - // FIXME: character is ucs-4 - buf->character(lineStart-1) == '\n') { - countTo = lineStart; - *modRangeEnd = lineStart; - break; - } - - /* Don't try to resync in continuous wrap mode with non-fixed font - sizes; it would result in a chicken-and-egg dependency between - the calculations for the inserted and the deleted lines. - If we're in that mode, the number of deleted lines is calculated in - advance, without resynchronization, so we shouldn't resynchronize - for the inserted lines either. */ - if (mSuppressResync) - continue; - - /* check for synchronization with the original line starts array - before pos, if so, the modified range can begin later */ - if (lineStart <= pos) { - while (visLineNum pos + nInserted) { - adjLineStart = lineStart - nInserted + nDeleted; - while (visLineNum= mFirstChar && pos <= mLastChar) { + for (i=nVisLines-1; i>0; i--) { + if (lineStarts[i] != -1 && pos >= lineStarts[i]) { + break; + } } - *linesInserted = nLines; - - - /* Count deleted lines between countFrom and countTo as the text existed - before the modification (that is, as if the text between pos and - pos+nInserted were replaced by "deletedText"). This extra context is - necessary because wrapping can occur outside of the modified region - as a result of adding or deleting text in the region. This is done by - creating a textBuffer containing the deleted text and the necessary - additional context, and calling the wrappedLineCounter on it. - - NOTE: This must not be done in continuous wrap mode when the font - width is not fixed. In that case, the calculation would try - to access style information that is no longer available (deleted - text), or out of date (updated highlighting), possibly leading - to completely wrong calculations and/or even crashes eventually. - (This is not theoretical; it really happened.) - - In that case, the calculation of the number of deleted lines - has happened before the buffer was modified (only in that case, - because resynchronization of the line starts is impossible - in that case, which makes the whole calculation less efficient). - */ - if (mSuppressResync) { - *linesDeleted = mNLinesDeleted; - mSuppressResync = 0; - return; + if (i > 0) { + countFrom = lineStarts[i-1]; + visLineNum = i-1; + } else { + countFrom = buf->line_start(pos); } - - length = (pos-countFrom) + nDeleted +(countTo-(pos+nInserted)); - deletedTextBuf = new Fl_Text_Buffer_mod(length); - deletedTextBuf->copy(buffer(), countFrom, pos, 0); - if (nDeleted != 0) - deletedTextBuf->insert(pos-countFrom, deletedText); - deletedTextBuf->copy(buffer(), - pos+nInserted, countTo, pos-countFrom+nDeleted); - /* Note that we need to take into account an offset for the style buffer: - the deletedTextBuf can be out of sync with the style buffer. */ - wrapped_line_counter(deletedTextBuf, 0, length, INT_MAX, true, - countFrom, &retPos, &retLines, &retLineStart, &retLineEnd, false); - delete deletedTextBuf; - *linesDeleted = retLines; - mSuppressResync = 0; -} - -/** - This is a stripped-down version of the findWrapRange() function above, - intended to be used to calculate the number of "deleted" lines during - a buffer modification. It is called _before_ the modification takes place. + } else { + countFrom = buf->line_start(pos); + } + + IS_UTF8_ALIGNED2(buf, countFrom) + + /* + ** Move forward through the (new) text one line at a time, counting + ** displayed lines, and looking for either a real newline, or for the + ** line starts to re-sync with the original line starts array + */ + lineStart = countFrom; + *modRangeStart = countFrom; + for (;;) { + + /* advance to the next line. If the line ended in a real newline + or the end of the buffer, that's far enough */ + wrapped_line_counter(buf, lineStart, buf->length(), 1, true, 0, + &retPos, &retLines, &retLineStart, &retLineEnd); + if (retPos >= buf->length()) { + countTo = buf->length(); + *modRangeEnd = countTo; + if (retPos != retLineEnd) + nLines++; + break; + } else { + lineStart = retPos; + } + nLines++; + if (lineStart > pos + nInserted && buf->char_at(buf->prev_char(lineStart)) == '\n') { + countTo = lineStart; + *modRangeEnd = lineStart; + break; + } + + /* Don't try to resync in continuous wrap mode with non-fixed font + sizes; it would result in a chicken-and-egg dependency between + the calculations for the inserted and the deleted lines. + If we're in that mode, the number of deleted lines is calculated in + advance, without resynchronization, so we shouldn't resynchronize + for the inserted lines either. */ + if (mSuppressResync) + continue; + + /* check for synchronization with the original line starts array + before pos, if so, the modified range can begin later */ + if (lineStart <= pos) { + while (visLineNumprev_char(lineStarts[visLineNum+1])); + else + *modRangeStart = countFrom; + } else + *modRangeStart = min(*modRangeStart, buf->prev_char(lineStart)); + } + + /* check for synchronization with the original line starts array + after pos, if so, the modified range can end early */ + else if (lineStart > pos + nInserted) { + adjLineStart = lineStart - nInserted + nDeleted; + while (visLineNum= mFirstChar && pos <= mLastChar) { - for (i=nVisLines-1; i>0; i--) - if (lineStarts[i] != -1 && pos >= lineStarts[i]) - break; - if (i > 0) { - countFrom = lineStarts[i-1]; - visLineNum = i-1; - } else - countFrom = buf->line_start(pos); - } else - countFrom = buf->line_start(pos); - - /* - ** Move forward through the (new) text one line at a time, counting - ** displayed lines, and looking for either a real newline, or for the - ** line starts to re-sync with the original line starts array - */ - lineStart = countFrom; - for (;;) { - /* advance to the next line. If the line ended in a real newline - or the end of the buffer, that's far enough */ - wrapped_line_counter(buf, lineStart, buf->length(), 1, true, 0, - &retPos, &retLines, &retLineStart, &retLineEnd); - if (retPos >= buf->length()) { - if (retPos != retLineEnd) - nLines++; - break; - } else - lineStart = retPos; - nLines++; - if (lineStart > pos + nDeleted && - // FIXME: character is ucs-4 - buf->character(lineStart-1) == '\n') { - break; - } - - /* Unlike in the findWrapRange() function above, we don't try to - resync with the line starts, because we don't know the length - of the inserted text yet, nor the updated style information. - - Because of that, we also shouldn't resync with the line starts - after the modification either, because we must perform the - calculations for the deleted and inserted lines in the same way. - - This can result in some unnecessary recalculation and redrawing - overhead, and therefore we should only use this two-phase mode - of calculation when it's really needed (continuous wrap + variable - font width). */ - } - mNLinesDeleted = nLines; - mSuppressResync = 1; -} - -/** - Count forward from startPos to either maxPos or maxLines (whichever is - reached first), and return all relevant positions and line count. - The provided textBuffer may differ from the actual text buffer of the - widget. In that case it must be a (partial) copy of the actual text buffer - and the styleBufOffset argument must indicate the starting position of the - copy, to take into account the correct style information. -** - Returned values: -** - retPos: Position where counting ended. When counting lines, the - position returned is the start of the line "maxLines" - lines beyond "startPos". - retLines: Number of line breaks counted - retLineStart: Start of the line where counting ended - retLineEnd: End position of the last line traversed -*/ -void Fl_Text_Display_mod::wrapped_line_counter(Fl_Text_Buffer_mod *buf, int startPos, - int maxPos, int maxLines, bool startPosIsLineStart, int styleBufOffset, - int *retPos, int *retLines, int *retLineStart, int *retLineEnd, - bool countLastLineMissingNewLine) const { - int lineStart, newLineStart = 0, b, p, colNum, wrapMargin; - int maxWidth, i, foundBreak, width; - bool countPixels; - int nLines = 0, tabDist = buffer()->tab_distance(); - unsigned char c; - - /* If the font is fixed, or there's a wrap margin set, it's more efficient - to measure in columns, than to count pixels. Determine if we can count - in columns (countPixels == False) or must count pixels (countPixels == - True), and set the wrap target for either pixels or columns */ - if (mFixedFontWidth != -1 || mWrapMargin != 0) { - countPixels = false; - wrapMargin = mWrapMargin ? mWrapMargin : text_area.w / (mFixedFontWidth + 1); - maxWidth = INT_MAX; - } else { - countPixels = true; - wrapMargin = INT_MAX; - maxWidth = text_area.w; - } - - /* Find the start of the line if the start pos is not marked as a - line start. */ - if (startPosIsLineStart) - lineStart = startPos; - else - lineStart = line_start(startPos); - - /* - ** Loop until position exceeds maxPos or line count exceeds maxLines. - ** (actually, contines beyond maxPos to end of line containing maxPos, - ** in case later characters cause a word wrap back before maxPos) - */ - colNum = 0; - width = 0; - for (p=lineStart; plength(); p++) { - // FIXME: character is ucs-4 - c = (unsigned char)buf->character(p); - - /* If the character was a newline, count the line and start over, - otherwise, add it to the width and column counts */ - if (c == '\n') { - if (p >= maxPos) { - *retPos = maxPos; - *retLines = nLines; - *retLineStart = lineStart; - *retLineEnd = maxPos; - return; - } - nLines++; - if (nLines >= maxLines) { - *retPos = p + 1; - *retLines = nLines; - *retLineStart = p + 1; - *retLineEnd = p; - return; - } - lineStart = p + 1; - colNum = 0; - width = 0; - } else { - colNum += Fl_Text_Buffer_mod::character_width((char*)&c, colNum, tabDist); // FIXME: unicode - if (countPixels) - width += measure_proportional_character(c, colNum, p+styleBufOffset); - } - - /* If character exceeded wrap margin, find the break point - and wrap there */ - if (colNum > wrapMargin || width > maxWidth) { - foundBreak = false; - for (b=p; b>=lineStart; b--) { - // FIXME: character is ucs-4 - c = (unsigned char)buf->character(b); - if (c == '\t' || c == ' ') { - newLineStart = b + 1; - if (countPixels) { - colNum = 0; - width = 0; - for (i=b+1; icharacter(i), colNum, - i+styleBufOffset); - colNum++; - } - } else - colNum = buf->count_displayed_characters(b+1, p+1); - foundBreak = true; - break; - } - } - if (!foundBreak) { /* no whitespace, just break at margin */ - newLineStart = max(p, lineStart+1); - colNum = Fl_Text_Buffer_mod::character_width((char*)&c, colNum, tabDist); // FIXME: unicode - if (countPixels) - width = measure_proportional_character(c, colNum, p+styleBufOffset); - } - if (p >= maxPos) { - *retPos = maxPos; - *retLines = maxPos < newLineStart ? nLines : nLines + 1; - *retLineStart = maxPos < newLineStart ? lineStart : - newLineStart; - *retLineEnd = maxPos; - return; - } - nLines++; - if (nLines >= maxLines) { - *retPos = foundBreak ? b + 1 : max(p, lineStart+1); - *retLines = nLines; - *retLineStart = lineStart; - *retLineEnd = foundBreak ? b : p; - return; - } - lineStart = newLineStart; - } - } - - /* reached end of buffer before reaching pos or line target */ - *retPos = buf->length(); - *retLines = nLines; - if (countLastLineMissingNewLine && colNum > 0) - ++(*retLines); - *retLineStart = lineStart; - *retLineEnd = buf->length(); -} - -/** - Measure the width in pixels of a character "c" at a particular column - "colNum" and buffer position "pos". This is for measuring characters in - proportional or mixed-width highlighting fonts. -** - A note about proportional and mixed-width fonts: the mixed width and - proportional font code in nedit does not get much use in general editing, - because nedit doesn't allow per-language-mode fonts, and editing programs - in a proportional font is usually a bad idea, so very few users would - choose a proportional font as a default. There are still probably mixed- - width syntax highlighting cases where things don't redraw properly for - insertion/deletion, though static display and wrapping and resizing - should now be solid because they are now used for online help display. -*/ -int Fl_Text_Display_mod::measure_proportional_character(char c, int colNum, int pos) const { - int charLen, style; - char expChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - Fl_Text_Buffer_mod *styleBuf = mStyleBuffer; - - charLen = Fl_Text_Buffer_mod::expand_character(&c, colNum, expChar, buffer()->tab_distance()); // FIXME: unicode - if (styleBuf == 0) { - style = 0; - } else { - // FIXME: character is ucs-4 - style = (unsigned char)styleBuf->character(pos); - if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { - /* encountered "unfinished" style, trigger parsing */ - (mUnfinishedHighlightCB)(pos, mHighlightCBArg); - // FIXME: character is ucs-4 - style = (unsigned char)styleBuf->character(pos); - } - } - return string_width(expChar, charLen, style); -} - -/** - Finds both the end of the current line and the start of the next line. Why? - In continuous wrap mode, if you need to know both, figuring out one from the - other can be expensive or error prone. The problem comes when there's a - trailing space or tab just before the end of the buffer. To translate an - end of line value to or from the next lines start value, you need to know - whether the trailing space or tab is being used as a line break or just a - normal character, and to find that out would otherwise require counting all - the way back to the beginning of the line. -*/ -void Fl_Text_Display_mod::find_line_end(int startPos, bool startPosIsLineStart, - int *lineEnd, int *nextLineStart) const { - int retLines, retLineStart; - - /* if we're not wrapping use more efficient BufEndOfLine */ - if (!mContinuousWrap) { - *lineEnd = buffer()->line_end(startPos); - *nextLineStart = min(buffer()->length(), *lineEnd + 1); - return; - } - - /* use the wrapped line counter routine to count forward one line */ - wrapped_line_counter(buffer(), startPos, buffer()->length(), - 1, startPosIsLineStart, 0, nextLineStart, &retLines, - &retLineStart, lineEnd); + NOTE: This must not be done in continuous wrap mode when the font + width is not fixed. In that case, the calculation would try + to access style information that is no longer available (deleted + text), or out of date (updated highlighting), possibly leading + to completely wrong calculations and/or even crashes eventually. + (This is not theoretical; it really happened.) + + In that case, the calculation of the number of deleted lines + has happened before the buffer was modified (only in that case, + because resynchronization of the line starts is impossible + in that case, which makes the whole calculation less efficient). + */ + if (mSuppressResync) { + *linesDeleted = mNLinesDeleted; + mSuppressResync = 0; return; + } + + length = (pos-countFrom) + nDeleted +(countTo-(pos+nInserted)); + deletedTextBuf = new Fl_Text_Buffer_mod(length); + deletedTextBuf->copy(buffer(), countFrom, pos, 0); + if (nDeleted != 0) + deletedTextBuf->insert(pos-countFrom, deletedText); + deletedTextBuf->copy(buffer(), pos+nInserted, countTo, pos-countFrom+nDeleted); + /* Note that we need to take into account an offset for the style buffer: + the deletedTextBuf can be out of sync with the style buffer. */ + wrapped_line_counter(deletedTextBuf, 0, length, INT_MAX, true, countFrom, + &retPos, &retLines, &retLineStart, &retLineEnd, false); + delete deletedTextBuf; + *linesDeleted = retLines; + mSuppressResync = 0; } + + /** - Line breaks in continuous wrap mode usually happen at newlines or - whitespace. This line-terminating character is not included in line - width measurements and has a special status as a non-visible character. - However, lines with no whitespace are wrapped without the benefit of a - line terminating character, and this distinction causes endless trouble - with all of the text display code which was originally written without - continuous wrap mode and always expects to wrap at a newline character. -** - Given the position of the end of the line, as returned by TextDEndOfLine - or BufEndOfLine, this returns true if there is a line terminating - character, and false if there's not. On the last character in the - buffer, this function can't tell for certain whether a trailing space was - used as a wrap point, and just guesses that it wasn't. So if an exact - accounting is necessary, don't use this function. -*/ + \brief Wrapping calculations. + + This is a stripped-down version of the findWrapRange() function above, + intended to be used to calculate the number of "deleted" lines during + a buffer modification. It is called _before_ the modification takes place. + + This function should only be called in continuous wrap mode with a + non-fixed font width. In that case, it is impossible to calculate + the number of deleted lines, because the necessary style information + is no longer available _after_ the modification. In other cases, we + can still perform the calculation afterwards (possibly even more + efficiently). + + \param pos + \param nDeleted + */ +void Fl_Text_Display_mod::measure_deleted_lines(int pos, int nDeleted) { + IS_UTF8_ALIGNED2(buffer(), pos) + + int retPos, retLines, retLineStart, retLineEnd; + Fl_Text_Buffer_mod *buf = buffer(); + int nVisLines = mNVisibleLines; + int *lineStarts = mLineStarts; + int countFrom, lineStart; + int visLineNum = 0, nLines = 0, i; + /* + ** Determine where to begin searching: either the previous newline, or + ** if possible, limit to the start of the (original) previous displayed + ** line, using information from the existing line starts array + */ + if (pos >= mFirstChar && pos <= mLastChar) { + for (i=nVisLines-1; i>0; i--) + if (lineStarts[i] != -1 && pos >= lineStarts[i]) + break; + if (i > 0) { + countFrom = lineStarts[i-1]; + visLineNum = i-1; + } else + countFrom = buf->line_start(pos); + } else + countFrom = buf->line_start(pos); + + /* + ** Move forward through the (new) text one line at a time, counting + ** displayed lines, and looking for either a real newline, or for the + ** line starts to re-sync with the original line starts array + */ + lineStart = countFrom; + for (;;) { + /* advance to the next line. If the line ended in a real newline + or the end of the buffer, that's far enough */ + wrapped_line_counter(buf, lineStart, buf->length(), 1, true, 0, + &retPos, &retLines, &retLineStart, &retLineEnd); + if (retPos >= buf->length()) { + if (retPos != retLineEnd) + nLines++; + break; + } else + lineStart = retPos; + nLines++; + if (lineStart > pos + nDeleted && buf->char_at(lineStart-1) == '\n') { + break; + } + + /* Unlike in the findWrapRange() function above, we don't try to + resync with the line starts, because we don't know the length + of the inserted text yet, nor the updated style information. + + Because of that, we also shouldn't resync with the line starts + after the modification either, because we must perform the + calculations for the deleted and inserted lines in the same way. + + This can result in some unnecessary recalculation and redrawing + overhead, and therefore we should only use this two-phase mode + of calculation when it's really needed (continuous wrap + variable + font width). */ + } + mNLinesDeleted = nLines; + mSuppressResync = 1; +} + + + +/** + \brief Wrapping calculations. + + Count forward from startPos to either maxPos or maxLines (whichever is + reached first), and return all relevant positions and line count. + The provided textBuffer may differ from the actual text buffer of the + widget. In that case it must be a (partial) copy of the actual text buffer + and the styleBufOffset argument must indicate the starting position of the + copy, to take into account the correct style information. + + \param buf + \param startPos + \param maxPos + \param maxLines + \param startPosIsLineStart + \param styleBufOffset + + \param[out] retPos Position where counting ended. When counting lines, the + position returned is the start of the line "maxLines" lines + beyond "startPos". + \param[out] retLines Number of line breaks counted + \param[out] retLineStart Start of the line where counting ended + \param[out] retLineEnd End position of the last line traversed + \param[out] countLastLineMissingNewLine + */ +void Fl_Text_Display_mod::wrapped_line_counter(Fl_Text_Buffer_mod *buf, int startPos, + int maxPos, int maxLines, bool startPosIsLineStart, int styleBufOffset, + int *retPos, int *retLines, int *retLineStart, int *retLineEnd, + bool countLastLineMissingNewLine) const { + IS_UTF8_ALIGNED2(buf, startPos) + IS_UTF8_ALIGNED2(buf, maxPos) + + int lineStart, newLineStart = 0, b, p, colNum, wrapMarginPix; + int i, foundBreak; + double width; + int nLines = 0; + unsigned int c; + + /* Set the wrap margin to the wrap column or the view width */ + if (mWrapMarginPix != 0) { + wrapMarginPix = mWrapMarginPix; + } else { + wrapMarginPix = text_area.w; + } + + /* Find the start of the line if the start pos is not marked as a + line start. */ + if (startPosIsLineStart) + lineStart = startPos; + else + lineStart = line_start(startPos); + + /* + ** Loop until position exceeds maxPos or line count exceeds maxLines. + ** (actually, continues beyond maxPos to end of line containing maxPos, + ** in case later characters cause a word wrap back before maxPos) + */ + colNum = 0; + width = 0; + for (p=lineStart; plength(); p=buf->next_char(p)) { + c = buf->char_at(p); // UCS-4 + + /* If the character was a newline, count the line and start over, + otherwise, add it to the width and column counts */ + if (c == '\n') { + if (p >= maxPos) { + *retPos = maxPos; + *retLines = nLines; + *retLineStart = lineStart; + *retLineEnd = maxPos; + return; + } + nLines++; + int p1 = buf->next_char(p); + if (nLines >= maxLines) { + *retPos = p1; + *retLines = nLines; + *retLineStart = p1; + *retLineEnd = p; + return; + } + lineStart = p1; + colNum = 0; + width = 0; + } else { + const char *s = buf->address(p); + colNum++; + // FIXME: it is not a good idea to simply add character widths because on + // some platforms, the width is a floating point value and depends on the + // previous character as well. + width += measure_proportional_character(s, (int)width, p+styleBufOffset); + } + + /* If character exceeded wrap margin, find the break point and wrap there */ + if (width > wrapMarginPix) { + foundBreak = false; + for (b=p; b>=lineStart; b=buf->prev_char(b)) { + c = buf->char_at(b); + if (c == '\t' || c == ' ') { + newLineStart = buf->next_char(b); + colNum = 0; + width = 0; + int iMax = buf->next_char(p); + for (i=buf->next_char(b); inext_char(i)) { + width += measure_proportional_character(buf->address(i), (int)width, + i+styleBufOffset); + colNum++; + } + foundBreak = true; + break; + } + } + if (!foundBreak) { /* no whitespace, just break at margin */ + newLineStart = max(p, buf->next_char(lineStart)); + const char *s = buf->address(b); + colNum++; + width = measure_proportional_character(s, 0, p+styleBufOffset); + } + if (p >= maxPos) { + *retPos = maxPos; + *retLines = maxPos < newLineStart ? nLines : nLines + 1; + *retLineStart = maxPos < newLineStart ? lineStart : newLineStart; + *retLineEnd = maxPos; + return; + } + nLines++; + if (nLines >= maxLines) { + *retPos = foundBreak ? buf->next_char(b) : max(p, buf->next_char(lineStart)); + *retLines = nLines; + *retLineStart = lineStart; + *retLineEnd = foundBreak ? b : p; + return; + } + lineStart = newLineStart; + } + } + + /* reached end of buffer before reaching pos or line target */ + *retPos = buf->length(); + *retLines = nLines; + if (countLastLineMissingNewLine && colNum > 0) + *retLines = buf->next_char(*retLines); + *retLineStart = lineStart; + *retLineEnd = buf->length(); +} + + + +/** + \brief Wrapping calculations. + + Measure the width in pixels of the first character of string "s" at a + particular column "colNum" and buffer position "pos". This is for measuring + characters in proportional or mixed-width highlighting fonts. + + A note about proportional and mixed-width fonts: the mixed width and + proportional font code in nedit does not get much use in general editing, + because nedit doesn't allow per-language-mode fonts, and editing programs + in a proportional font is usually a bad idea, so very few users would + choose a proportional font as a default. There are still probably mixed- + width syntax highlighting cases where things don't redraw properly for + insertion/deletion, though static display and wrapping and resizing + should now be solid because they are now used for online help display. + + \param s text string + \param xPix x pixel position needed for calculating tab widths + \param pos offset within string + \return width of character in pixels + */ +double Fl_Text_Display_mod::measure_proportional_character(const char *s, int xPix, int pos) const { + IS_UTF8_ALIGNED(s) + + if (*s=='\t') { + int tab = (int)col_to_x(mBuffer->tab_distance()); + return (((xPix/tab)+1)*tab) - xPix; + } + + int charLen = fl_utf8len1(*s), style = 0; + if (mStyleBuffer) { + style = mStyleBuffer->byte_at(pos); + } + return string_width(s, charLen, style); +} + + + +/** + \brief Finds both the end of the current line and the start of the next line. + + Why? + In continuous wrap mode, if you need to know both, figuring out one from the + other can be expensive or error prone. The problem comes when there's a + trailing space or tab just before the end of the buffer. To translate an + end of line value to or from the next lines start value, you need to know + whether the trailing space or tab is being used as a line break or just a + normal character, and to find that out would otherwise require counting all + the way back to the beginning of the line. + + \param startPos + \param startPosIsLineStart + \param[out] lineEnd + \param[out] nextLineStart + */ +void Fl_Text_Display_mod::find_line_end(int startPos, bool startPosIsLineStart, + int *lineEnd, int *nextLineStart) const { + IS_UTF8_ALIGNED2(buffer(), startPos) + + int retLines, retLineStart; + + /* if we're not wrapping use more efficient BufEndOfLine */ + if (mFastDisplay || !mContinuousWrap) { + int le = buffer()->line_end(startPos); + int ls = buffer()->next_char(le); + *lineEnd = le; + *nextLineStart = min(buffer()->length(), ls); + return; + } + + /* use the wrapped line counter routine to count forward one line */ + wrapped_line_counter(buffer(), startPos, buffer()->length(), + 1, startPosIsLineStart, 0, nextLineStart, &retLines, + &retLineStart, lineEnd); +} + + + +/** + \brief Check if the line break is caused by a \\n or by line wrapping. + + Line breaks in continuous wrap mode usually happen at newlines or + whitespace. This line-terminating character is not included in line + width measurements and has a special status as a non-visible character. + However, lines with no whitespace are wrapped without the benefit of a + line terminating character, and this distinction causes endless trouble + with all of the text display code which was originally written without + continuous wrap mode and always expects to wrap at a newline character. + + Given the position of the end of the line, as returned by TextDEndOfLine + or BufEndOfLine, this returns true if there is a line terminating + character, and false if there's not. On the last character in the + buffer, this function can't tell for certain whether a trailing space was + used as a wrap point, and just guesses that it wasn't. So if an exact + accounting is necessary, don't use this function. + + \param lineEndPos index of character where the line wraps + \return 1 if a \\n character causes the line wrap + */ int Fl_Text_Display_mod::wrap_uses_character(int lineEndPos) const { - char c; - - if (!mContinuousWrap || lineEndPos == buffer()->length()) - return 1; - - // FIXME: character is ucs-4 - c = buffer()->character(lineEndPos); - return c == '\n' || ((c == '\t' || c == ' ') && - lineEndPos + 1 != buffer()->length()); + IS_UTF8_ALIGNED2(buffer(), lineEndPos) + + unsigned int c; + + if (mFastDisplay || !mContinuousWrap || lineEndPos == buffer()->length()) + return 1; + + c = buffer()->char_at(lineEndPos); + return c == '\n' || ((c == '\t' || c == ' ') && + lineEndPos + 1 < buffer()->length()); } /** - Return true if the selection "sel" is rectangular, and touches a - buffer position withing "rangeStart" to "rangeEnd" -*/ -int Fl_Text_Display_mod::range_touches_selection(const Fl_Text_Selection *sel, - int rangeStart, int rangeEnd) const { - return sel->selected() && sel->rectangular() && sel->end() >= rangeStart && - sel->start() <= rangeEnd; -} - -/** - Extend the range of a redraw request (from *start to *end) with additional - redraw requests resulting from changes to the attached style buffer (which - contains auxiliary information for coloring or styling text). -*/ + \brief I don't know what this does! + + Extend the range of a redraw request (from *start to *end) with additional + redraw requests resulting from changes to the attached style buffer (which + contains auxiliary information for coloring or styling text). + + \param startpos ?? + \param endpos ?? + + \todo Unicode? + */ void Fl_Text_Display_mod::extend_range_for_styles( int *startpos, int *endpos ) { - Fl_Text_Selection * sel = mStyleBuffer->primary_selection(); + IS_UTF8_ALIGNED2(buffer(), (*startpos)) + IS_UTF8_ALIGNED2(buffer(), (*endpos)) + + Fl_Text_Selection_mod * sel = mStyleBuffer->primary_selection(); int extended = 0; - + /* The peculiar protocol used here is that modifications to the style - buffer are marked by selecting them with the buffer's primary Fl_Text_Selection. - The style buffer is usually modified in response to a modify callback on - the text buffer BEFORE Fl_Text_Display.c's modify callback, so that it can keep - the style buffer in step with the text buffer. The style-update - callback can't just call for a redraw, because Fl_Text_Display hasn't processed - the original text changes yet. Anyhow, to minimize redrawing and to - avoid the complexity of scheduling redraws later, this simple protocol - tells the text display's buffer modify callback to extend it's redraw - range to show the text color/and font changes as well. */ + buffer are marked by selecting them with the buffer's primary Fl_Text_Selection. + The style buffer is usually modified in response to a modify callback on + the text buffer BEFORE Fl_Text_Display_mod.c's modify callback, so that it can keep + the style buffer in step with the text buffer. The style-update + callback can't just call for a redraw, because Fl_Text_Display_mod hasn't processed + the original text changes yet. Anyhow, to minimize redrawing and to + avoid the complexity of scheduling redraws later, this simple protocol + tells the text display's buffer modify callback to extend its redraw + range to show the text color/and font changes as well. */ if ( sel->selected() ) { if ( sel->start() < *startpos ) { *startpos = sel->start(); + // somewhere while deleting, alignment is lost. We do this just to be sure. + *startpos = buffer()->utf8_align(*startpos); + IS_UTF8_ALIGNED2(buffer(), (*startpos)) extended = 1; } if ( sel->end() > *endpos ) { *endpos = sel->end(); + *endpos = buffer()->utf8_align(*endpos); + IS_UTF8_ALIGNED2(buffer(), (*endpos)) extended = 1; } } - + /* If the Fl_Text_Selection was extended due to a style change, and some of the - fonts don't match in spacing, extend redraw area to end of line to - redraw characters exposed by possible font size changes */ - if ( mFixedFontWidth == -1 && extended ) + fonts don't match in spacing, extend redraw area to end of line to + redraw characters exposed by possible font size changes */ + if ( extended ) *endpos = mBuffer->line_end( *endpos ) + 1; + + IS_UTF8_ALIGNED2(buffer(), (*endpos)) } -// The draw() method. It tries to minimize what is draw as much as possible. + + +/** + \brief Draw the widget. + + This function tries to limit drawing to smaller areas if possible. + */ void Fl_Text_Display_mod::draw(void) { // don't even try if there is no associated text buffer! if (!buffer()) { draw_box(); return; } - + fl_push_clip(x(),y(),w(),h()); // prevent drawing outside widget area - + // draw the non-text, non-scrollbar areas. if (damage() & FL_DAMAGE_ALL) { -// printf("drawing all (box = %d)\n", box()); + // printf("drawing all (box = %d)\n", box()); + if (Fl_Surface_Device::surface()->class_name() == Fl_Printer::class_id) { + // if to printer, draw the background + fl_rectf(text_area.x, text_area.y, text_area.w, text_area.h, color() ); + } // draw the box() int W = w(), H = h(); draw_box(box(), x(), y(), W, H, color()); - + if (mHScrollBar->visible()) W -= scrollbar_width(); if (mVScrollBar->visible()) H -= scrollbar_width(); - + // left margin fl_rectf(text_area.x-LEFT_MARGIN, text_area.y-TOP_MARGIN, LEFT_MARGIN, text_area.h+TOP_MARGIN+BOTTOM_MARGIN, color()); - + // right margin fl_rectf(text_area.x+text_area.w, text_area.y-TOP_MARGIN, RIGHT_MARGIN, text_area.h+TOP_MARGIN+BOTTOM_MARGIN, color()); - + // top margin fl_rectf(text_area.x, text_area.y-TOP_MARGIN, text_area.w, TOP_MARGIN, color()); - + // bottom margin fl_rectf(text_area.x, text_area.y+text_area.h, text_area.w, BOTTOM_MARGIN, color()); - + // draw that little box in the corner of the scrollbars if (mVScrollBar->visible() && mHScrollBar->visible()) fl_rectf(mVScrollBar->x(), mHScrollBar->y(), mVScrollBar->w(), mHScrollBar->h(), FL_GRAY); - + // blank the previous cursor protrusions } else if (damage() & (FL_DAMAGE_SCROLL | FL_DAMAGE_EXPOSE)) { -// printf("blanking previous cursor extrusions at Y: %d\n", mCursorOldY); + // printf("blanking previous cursor extrusions at Y: %d\n", mCursorOldY); // CET - FIXME - save old cursor position instead and just draw side needed? fl_push_clip(text_area.x-LEFT_MARGIN, text_area.y, @@ -2983,7 +3426,7 @@ void Fl_Text_Display_mod::draw(void) { RIGHT_MARGIN, mMaxsize, color()); fl_pop_clip(); } - + // draw the scrollbars if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_CHILD)) { mVScrollBar->damage(FL_DAMAGE_ALL); @@ -2991,7 +3434,7 @@ void Fl_Text_Display_mod::draw(void) { } update_child(*mVScrollBar); update_child(*mHScrollBar); - + // draw all of the text if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_EXPOSE)) { //printf("drawing all text\n"); @@ -3020,19 +3463,19 @@ void Fl_Text_Display_mod::draw(void) { damage_range2_start = damage_range2_end = -1; fl_pop_clip(); } - + // draw the text cursor if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_SCROLL | FL_DAMAGE_EXPOSE) && !buffer()->primary_selection()->selected() && - mCursorOn && Fl::focus() == this ) { + mCursorOn && Fl::focus() == (Fl_Widget*)this ) { fl_push_clip(text_area.x-LEFT_MARGIN, text_area.y, text_area.w+LEFT_MARGIN+RIGHT_MARGIN, text_area.h); - + int X, Y; if (position_to_xy(mCursorPos, &X, &Y)) draw_cursor(X, Y); -// else puts("position_to_xy() failed - unable to draw cursor!"); + // else puts("position_to_xy() failed - unable to draw cursor!"); //printf("drew cursor at pos: %d (%d,%d)\n", mCursorPos, X, Y); mCursorOldY = Y; fl_pop_clip(); @@ -3040,9 +3483,11 @@ void Fl_Text_Display_mod::draw(void) { fl_pop_clip(); } -// this processes drag events due to mouse for Fl_Text_Display and + + +// this processes drag events due to mouse for Fl_Text_Display_mod and // also drags due to cursor movement with shift held down for -// Fl_Text_Editor +// Fl_Text_Editor_mod void fl_text_drag_me(int pos, Fl_Text_Display_mod* d) { if (d->dragType == Fl_Text_Display_mod::DRAG_CHAR) { if (pos >= d->dragPos) { @@ -3072,37 +3517,47 @@ void fl_text_drag_me(int pos, Fl_Text_Display_mod* d) { } } -// This timer event scrolls the text view proportionally to -// how far the mouse pointer has left the text area. This -// allows for smooth scrolling without "wiggeling" the mouse. + + +/** + \brief Timer callback for scroll events. + + This timer event scrolls the text view proportionally to + how far the mouse pointer has left the text area. This + allows for smooth scrolling without "wiggeling" the mouse. + */ void Fl_Text_Display_mod::scroll_timer_cb(void *user_data) { Fl_Text_Display_mod *w = (Fl_Text_Display_mod*)user_data; int pos; switch (scroll_direction) { - case 1: // mouse is to the right, scroll left - w->scroll(w->mTopLineNum, w->mHorizOffset + scroll_amount); - pos = w->xy_to_position(w->text_area.x + w->text_area.w, scroll_y, CURSOR_POS); - break; - case 2: // mouse is to the left, scroll right - w->scroll(w->mTopLineNum, w->mHorizOffset + scroll_amount); - pos = w->xy_to_position(w->text_area.x, scroll_y, CURSOR_POS); - break; - case 3: // mouse is above, scroll down - w->scroll(w->mTopLineNum + scroll_amount, w->mHorizOffset); - pos = w->xy_to_position(scroll_x, w->text_area.y, CURSOR_POS); - break; - case 4: // mouse is below, scroll up - w->scroll(w->mTopLineNum + scroll_amount, w->mHorizOffset); - pos = w->xy_to_position(scroll_x, w->text_area.y + w->text_area.h, CURSOR_POS); - break; - default: - return; + case 1: // mouse is to the right, scroll left + w->scroll(w->mTopLineNum, w->mHorizOffset + scroll_amount); + pos = w->xy_to_position(w->text_area.x + w->text_area.w, scroll_y, CURSOR_POS); + break; + case 2: // mouse is to the left, scroll right + w->scroll(w->mTopLineNum, w->mHorizOffset + scroll_amount); + pos = w->xy_to_position(w->text_area.x, scroll_y, CURSOR_POS); + break; + case 3: // mouse is above, scroll down + w->scroll(w->mTopLineNum + scroll_amount, w->mHorizOffset); + pos = w->xy_to_position(scroll_x, w->text_area.y, CURSOR_POS); + break; + case 4: // mouse is below, scroll up + w->scroll(w->mTopLineNum + scroll_amount, w->mHorizOffset); + pos = w->xy_to_position(scroll_x, w->text_area.y + w->text_area.h, CURSOR_POS); + break; + default: + return; } fl_text_drag_me(pos, w); Fl::repeat_timeout(.1, scroll_timer_cb, user_data); } + +/** + \brief Event handling. + */ int Fl_Text_Display_mod::handle(int event) { if (!buffer()) return 0; // This isn't very elegant! @@ -3112,7 +3567,7 @@ int Fl_Text_Display_mod::handle(int event) { event != FL_KEYBOARD && event != FL_KEYUP) { return Fl_Group::handle(event); } - + switch (event) { case FL_ENTER: case FL_MOVE: @@ -3124,137 +3579,144 @@ int Fl_Text_Display_mod::handle(int event) { } else { return 0; } - + case FL_LEAVE: case FL_HIDE: if (active_r() && window()) { window()->cursor(FL_CURSOR_DEFAULT); - + return 1; } else { return 0; } - + case FL_PUSH: { - if (active_r() && window()) { - if (Fl::event_inside(text_area.x, text_area.y, text_area.w, - text_area.h)) window()->cursor(FL_CURSOR_INSERT); - else window()->cursor(FL_CURSOR_DEFAULT); - } - - if (Fl::focus() != this) { - Fl::focus(this); - handle(FL_FOCUS); - } - if (Fl_Group::handle(event)) return 1; - if (Fl::event_state()&FL_SHIFT) return handle(FL_DRAG); - dragging = 1; - int pos = xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS); - int ok = 0; - while (!ok) { - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - pos++; - } - } - dragType = Fl::event_clicks(); - dragPos = pos; - if (dragType == DRAG_CHAR) - buffer()->unselect(); - else if (dragType == DRAG_WORD) - buffer()->select(word_start(pos), word_end(pos)); - else if (dragType == DRAG_LINE) - buffer()->select(buffer()->line_start(pos), buffer()->line_end(pos)+1); - - if (buffer()->primary_selection()->selected()) - insert_position(buffer()->primary_selection()->end()); - else - insert_position(pos); - show_insert_position(); + if (active_r() && window()) { + if (Fl::event_inside(text_area.x, text_area.y, text_area.w, + text_area.h)) window()->cursor(FL_CURSOR_INSERT); + else window()->cursor(FL_CURSOR_DEFAULT); + } + + if (Fl::focus() != this) { + Fl::focus(this); + handle(FL_FOCUS); + } + if (Fl_Group::handle(event)) return 1; + if (Fl::event_state()&FL_SHIFT) return handle(FL_DRAG); + dragging = 1; + int pos = xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS); + dragPos = pos; + if (buffer()->primary_selection()->includes(pos)) { + dragType = DRAG_START_DND; return 1; } - + dragType = Fl::event_clicks(); + if (dragType == DRAG_CHAR) { + buffer()->unselect(); +// Fl::copy("", 0, 0); /* removed for STR 2668 */ + } + else if (dragType == DRAG_WORD) { + buffer()->select(word_start(pos), word_end(pos)); + dragPos = word_start(pos); + } + + if (buffer()->primary_selection()->selected()) + insert_position(buffer()->primary_selection()->end()); + else + insert_position(pos); + show_insert_position(); + return 1; + } + case FL_DRAG: { - if (dragType < 0) return 1; - int X = Fl::event_x(), Y = Fl::event_y(), pos = 0; - // if we leave the text_area, we start a timer event - // that will take care of scrolling and selecting - if (Y < text_area.y) { - scroll_x = X; - scroll_amount = (Y - text_area.y) / 5 - 1; - if (!scroll_direction) { - Fl::add_timeout(.01, scroll_timer_cb, this); - } - scroll_direction = 3; - } else if (Y >= text_area.y+text_area.h) { - scroll_x = X; - scroll_amount = (Y - text_area.y - text_area.h) / 5 + 1; - if (!scroll_direction) { - Fl::add_timeout(.01, scroll_timer_cb, this); - } - scroll_direction = 4; - } else if (X < text_area.x) { - scroll_y = Y; - scroll_amount = (X - text_area.x) / 2 - 1; - if (!scroll_direction) { - Fl::add_timeout(.01, scroll_timer_cb, this); - } - scroll_direction = 2; - } else if (X >= text_area.x+text_area.w) { - scroll_y = Y; - scroll_amount = (X - text_area.x - text_area.w) / 2 + 1; - if (!scroll_direction) { - Fl::add_timeout(.01, scroll_timer_cb, this); - } - scroll_direction = 1; - } else { - if (scroll_direction) { - Fl::remove_timeout(scroll_timer_cb, this); - scroll_direction = 0; - } - pos = xy_to_position(X, Y, CURSOR_POS); - int ok = 0; - while (!ok) { - // FIXME: character is ucs-4 - char c = buffer()->character( pos ); - if (!((c & 0x80) && !(c & 0x40))) { - ok = 1; - } else { - pos++; - } - } + if (dragType==DRAG_NONE) + return 1; + if (dragType==DRAG_START_DND) { + if (!Fl::event_is_click() && Fl::dnd_text_ops()) { + const char* copy = buffer()->selection_text(); + Fl::dnd(); + free((void*)copy); } - fl_text_drag_me(pos, this); return 1; } - - case FL_RELEASE: { - dragging = 0; + int X = Fl::event_x(), Y = Fl::event_y(), pos = insert_position(); + // if we leave the text_area, we start a timer event + // that will take care of scrolling and selecting + if (Y < text_area.y) { + scroll_x = X; + scroll_amount = (Y - text_area.y) / 5 - 1; + if (!scroll_direction) { + Fl::add_timeout(.01, scroll_timer_cb, this); + } + scroll_direction = 3; + } else if (Y >= text_area.y+text_area.h) { + scroll_x = X; + scroll_amount = (Y - text_area.y - text_area.h) / 5 + 1; + if (!scroll_direction) { + Fl::add_timeout(.01, scroll_timer_cb, this); + } + scroll_direction = 4; + } else if (X < text_area.x) { + scroll_y = Y; + scroll_amount = (X - text_area.x) / 2 - 1; + if (!scroll_direction) { + Fl::add_timeout(.01, scroll_timer_cb, this); + } + scroll_direction = 2; + } else if (X >= text_area.x+text_area.w) { + scroll_y = Y; + scroll_amount = (X - text_area.x - text_area.w) / 2 + 1; + if (!scroll_direction) { + Fl::add_timeout(.01, scroll_timer_cb, this); + } + scroll_direction = 1; + } else { if (scroll_direction) { Fl::remove_timeout(scroll_timer_cb, this); scroll_direction = 0; } - - // convert from WORD or LINE selection to CHAR - if (insert_position() >= dragPos) - dragPos = buffer()->primary_selection()->start(); - else - dragPos = buffer()->primary_selection()->end(); - dragType = DRAG_CHAR; - - const char* copy = buffer()->selection_text(); - if (*copy) Fl::copy(copy, strlen(copy), 0); - free((void*)copy); - return 1; + pos = xy_to_position(X, Y, CURSOR_POS); + pos = buffer()->next_char(pos); } - + fl_text_drag_me(pos, this); + return 1; + } + + case FL_RELEASE: { + if (Fl::event_is_click() && (! Fl::event_clicks()) && + buffer()->primary_selection()->includes(dragPos) && !(Fl::event_state()&FL_SHIFT) ) { + buffer()->unselect(); // clicking in the selection: unselect and move cursor + insert_position(dragPos); + return 1; + } else if (Fl::event_clicks() == DRAG_LINE && Fl::event_button() == FL_LEFT_MOUSE) { + buffer()->select(buffer()->line_start(dragPos), buffer()->next_char(buffer()->line_end(dragPos))); + dragPos = line_start(dragPos); + dragType = DRAG_CHAR; + } else { + dragging = 0; + if (scroll_direction) { + Fl::remove_timeout(scroll_timer_cb, this); + scroll_direction = 0; + } + + // convert from WORD or LINE selection to CHAR + /*if (insert_position() >= dragPos) + dragPos = buffer()->primary_selection()->start(); + else + dragPos = buffer()->primary_selection()->end();*/ + dragType = DRAG_CHAR; + } + + const char* copy = buffer()->selection_text(); + if (*copy) Fl::copy(copy, strlen(copy), 0); + free((void*)copy); + return 1; + } + case FL_MOUSEWHEEL: if (Fl::event_dy()) return mVScrollBar->handle(event); else return mHScrollBar->handle(event); - + case FL_UNFOCUS: if (active_r() && window()) window()->cursor(FL_CURSOR_DEFAULT); case FL_FOCUS: @@ -3274,28 +3736,31 @@ int Fl_Text_Display_mod::handle(int event) { redisplay_range(start, end); } return 1; - + case FL_KEYBOARD: // Copy? if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='c') { - if (!buffer()->selected()) return 1; - const char *copy = buffer()->selection_text(); - if (*copy) Fl::copy(copy, strlen(copy), 1); - free((void*)copy); - return 1; + if (!buffer()->selected()) return 1; + const char *copy = buffer()->selection_text(); + if (*copy) Fl::copy(copy, strlen(copy), 1); + free((void*)copy); + return 1; } - + // Select all ? if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='a') { - buffer()->select(0,buffer()->length()); - return 1; + buffer()->select(0,buffer()->length()); + const char *copy = buffer()->selection_text(); + if (*copy) Fl::copy(copy, strlen(copy), 0); + free((void*)copy); + return 1; } - + if (mVScrollBar->handle(event)) return 1; if (mHScrollBar->handle(event)) return 1; - + break; - + case FL_SHORTCUT: if (!(shortcut() ? Fl::test_shortcut(shortcut()) : test_shortcut())) return 0; @@ -3306,11 +3771,38 @@ int Fl_Text_Display_mod::handle(int event) { break; } - + return 0; } +/* + Convert an x pixel position into a column number. + */ +double Fl_Text_Display_mod::x_to_col(double y) const +{ + if (!mColumnScale) { + mColumnScale = string_width("Mitg", 4, 'A') / 4.0; + } + return (y/mColumnScale)+0.5; +} + + +/** + Convert a column number into an x pixel position. + */ +double Fl_Text_Display_mod::col_to_x(double col) const +{ + if (!mColumnScale) { + // recalculate column scale value + x_to_col(0); + } + return col*mColumnScale; +} + + + + // -// End of "$Id: Fl_Text_Display.cxx 7462 2010-04-06 23:00:56Z matt $". +// End of "$Id: Fl_Text_Display_mod.cxx 8808 2011-06-16 13:31:25Z manolo $". // diff --git a/src/widgets/Fl_Text_Editor_mod_1_3.cxx b/src/widgets/Fl_Text_Editor_mod_1_3.cxx index fe21dc0c..15ccc80d 100644 --- a/src/widgets/Fl_Text_Editor_mod_1_3.cxx +++ b/src/widgets/Fl_Text_Editor_mod_1_3.cxx @@ -1,7 +1,7 @@ // -// "$Id: Fl_Text_Editor.cxx 7462 2010-04-06 23:00:56Z matt $" +// "$Id: Fl_Text_Editor_mod.cxx 8034 2010-12-15 12:21:55Z AlbrechtS $" // -// Copyright 2001-2009 by Bill Spitzak and others. +// Copyright 2001-2010 by Bill Spitzak and others. // Original code Copyright Mark Edel. Permission to distribute under // the LGPL for the FLTK library granted by Mark Edel. // @@ -31,8 +31,8 @@ #include #include #include -#include "Fl_Text_Editor_mod.H" #include +#include "Fl_Text_Editor_mod.H" /* Keyboard Control Matrix @@ -100,12 +100,12 @@ static struct { int state; Fl_Text_Editor_mod::Key_Func func; } default_key_bindings[] = { - { FL_Escape, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor_mod::kf_ignore }, - { FL_Enter, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor_mod::kf_enter }, - { FL_KP_Enter, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor_mod::kf_enter }, - { FL_BackSpace, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor_mod::kf_backspace }, - { FL_Insert, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor_mod::kf_insert }, - { FL_Delete, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor_mod::kf_delete }, + { FL_Escape, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_ignore }, + { FL_Enter, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_enter }, + { FL_KP_Enter, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_enter }, + { FL_BackSpace, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_backspace }, + { FL_Insert, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_insert }, + { FL_Delete, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_delete }, { FL_Home, 0, Fl_Text_Editor_mod::kf_move }, { FL_End, 0, Fl_Text_Editor_mod::kf_move }, { FL_Left, 0, Fl_Text_Editor_mod::kf_move }, @@ -184,7 +184,7 @@ Fl_Text_Editor_mod::Key_Func Fl_Text_Editor_mod::bound_key_function(int key, int Key_Binding* cur; for (cur = list; cur; cur = cur->next) if (cur->key == key) - if (cur->state == FL_TEXT_EDITOR_ANY_STATE || cur->state == state) + if (cur->state == Fl_Text_Editor_mod_ANY_STATE || cur->state == state) break; if (!cur) return 0; return cur->function; @@ -223,8 +223,6 @@ void Fl_Text_Editor_mod::add_key_binding(int key, int state, Key_Func function, //////////////////////////////////////////////////////////////// -#define NORMAL_INPUT_MOVE 0 - static void kill_selection(Fl_Text_Editor_mod* e) { if (e->buffer()->selected()) { e->insert_position(e->buffer()->primary_selection()->start()); @@ -234,6 +232,7 @@ static void kill_selection(Fl_Text_Editor_mod* e) { /** Inserts the text associated with the key */ int Fl_Text_Editor_mod::kf_default(int c, Fl_Text_Editor_mod* e) { + // FIXME: this function is a mess! Fix this! if (!c || (!isprint(c) && c != '\t')) return 0; char s[2] = "\0"; s[0] = (char)c; @@ -253,13 +252,9 @@ int Fl_Text_Editor_mod::kf_ignore(int, Fl_Text_Editor_mod*) { /** Does a backspace in the current buffer.*/ int Fl_Text_Editor_mod::kf_backspace(int, Fl_Text_Editor_mod* e) { if (!e->buffer()->selected() && e->move_left()) { - int l = 1; - // FIXME: character is ucs-4 - char c = e->buffer()->character(e->insert_position()); - if (c & 0x80 && c & 0x40) { - l = fl_utf8len(c); - } - e->buffer()->select(e->insert_position(), e->insert_position()+l); + int p1 = e->insert_position(); + int p2 = e->buffer()->next_char(p1); + e->buffer()->select(p1, p2); } kill_selection(e); e->show_insert_position(); @@ -286,6 +281,7 @@ int Fl_Text_Editor_mod::kf_move(int c, Fl_Text_Editor_mod* e) { if (!selected) e->dragPos = e->insert_position(); e->buffer()->unselect(); + Fl::copy("", 0, 0); switch (c) { case FL_Home: e->insert_position(e->buffer()->line_start(e->insert_position())); @@ -320,6 +316,11 @@ int Fl_Text_Editor_mod::kf_move(int c, Fl_Text_Editor_mod* e) { int Fl_Text_Editor_mod::kf_shift_move(int c, Fl_Text_Editor_mod* e) { kf_move(c, e); fl_text_drag_me(e->insert_position(), e); + char *copy = e->buffer()->selection_text(); + if (copy) { + Fl::copy(copy, strlen(copy), 0); + free(copy); + } return 1; } /** Moves the current text cursor in the direction indicated by control key */ @@ -328,6 +329,7 @@ int Fl_Text_Editor_mod::kf_ctrl_move(int c, Fl_Text_Editor_mod* e) { e->dragPos = e->insert_position(); if (c != FL_Up && c != FL_Down) { e->buffer()->unselect(); + Fl::copy("", 0, 0); e->show_insert_position(); } switch (c) { @@ -367,6 +369,7 @@ int Fl_Text_Editor_mod::kf_meta_move(int c, Fl_Text_Editor_mod* e) { e->dragPos = e->insert_position(); if (c != FL_Up && c != FL_Down) { e->buffer()->unselect(); + Fl::copy("", 0, 0); e->show_insert_position(); } switch (c) { @@ -449,13 +452,9 @@ int Fl_Text_Editor_mod::kf_insert(int, Fl_Text_Editor_mod* e) { /** Does a delete of selected text or the current character in the current buffer.*/ int Fl_Text_Editor_mod::kf_delete(int, Fl_Text_Editor_mod* e) { if (!e->buffer()->selected()) { - int l = 1; - // FIXME: character is ucs-4 - char c = e->buffer()->character(e->insert_position()); - if (c & 0x80 && c & 0x40) { - l = fl_utf8len(c); - } - e->buffer()->select(e->insert_position(), e->insert_position()+l); + int p1 = e->insert_position(); + int p2 = e->buffer()->next_char(p1); + e->buffer()->select(p1, p2); } kill_selection(e); @@ -497,11 +496,15 @@ int Fl_Text_Editor_mod::kf_paste(int, Fl_Text_Editor_mod* e) { /** Selects all text in the current buffer.*/ int Fl_Text_Editor_mod::kf_select_all(int, Fl_Text_Editor_mod* e) { e->buffer()->select(0, e->buffer()->length()); + const char *copy = e->buffer()->selection_text(); + if (*copy) Fl::copy(copy, strlen(copy), 0); + free((void*)copy); return 1; } /** Undo last edit in the current buffer. Also deselect previous selection. */ int Fl_Text_Editor_mod::kf_undo(int , Fl_Text_Editor_mod* e) { e->buffer()->unselect(); + Fl::copy("", 0, 0); int crsr; int ret = e->buffer()->undo(&crsr); e->insert_position(crsr); @@ -519,7 +522,11 @@ int Fl_Text_Editor_mod::handle_key() { // bytes to delete and a string to insert: int del = 0; if (Fl::compose(del)) { - if (del) buffer()->select(insert_position()-del, insert_position()); + if (del) { + int dp = insert_position(), di = del; + while (di--) dp = buffer()->prev_char_clipped(dp); + buffer()->select(dp, insert_position()); + } kill_selection(this); if (Fl::event_length()) { if (insert_mode()) insert(Fl::event_text()); @@ -549,6 +556,8 @@ void Fl_Text_Editor_mod::maybe_do_callback() { } int Fl_Text_Editor_mod::handle(int event) { + static int dndCursorPos; + if (!buffer()) return 0; switch (event) { @@ -593,7 +602,12 @@ int Fl_Text_Editor_mod::handle(int event) { if (Fl::event_button() == 2) { // don't let the text_display see this event if (Fl_Group::handle(event)) return 1; - dragType = -1; + dragType = DRAG_NONE; + if(buffer()->selected()) { + buffer()->unselect(); + } + int pos = xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS); + insert_position(pos); Fl::paste(*this, 0); Fl::focus(this); set_changed(); @@ -610,11 +624,29 @@ int Fl_Text_Editor_mod::handle(int event) { return 1; } break; + + // Handle drag'n'drop attempt by the user. This is a simplified + // implementation which allows dnd operations onto the scroll bars. + case FL_DND_ENTER: // save the current cursor position + if (Fl::visible_focus() && handle(FL_FOCUS)) + Fl::focus(this); + show_cursor(mCursorOn); + dndCursorPos = insert_position(); + /* fall through */ + case FL_DND_DRAG: // show a temporary insertion cursor + insert_position(xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS)); + return 1; + case FL_DND_LEAVE: // restore original cursor + insert_position(dndCursorPos); + return 1; + case FL_DND_RELEASE: // keep insertion cursor and wait for the FL_PASTE event + buffer()->unselect(); // FL_PASTE must not destroy current selection! + return 1; } return Fl_Text_Display_mod::handle(event); } // -// End of "$Id: Fl_Text_Editor.cxx 7462 2010-04-06 23:00:56Z matt $". +// End of "$Id: Fl_Text_Editor_mod.cxx 8034 2010-12-15 12:21:55Z AlbrechtS $". // diff --git a/src/widgets/Panel.cxx b/src/widgets/Panel.cxx index 90e9d2bf..3473aa5b 100644 --- a/src/widgets/Panel.cxx +++ b/src/widgets/Panel.cxx @@ -30,6 +30,12 @@ #include #include "Panel.h" +#include + +#define FLTK13 +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +#undef FLTK13 +#endif // Drag the edges that were initially at oldx,oldy to newx,newy: // pass -1 as oldx or oldy to disable drag in that direction: @@ -37,7 +43,11 @@ int Panel::orgx() { int oldx = w(); +#ifdef FLTK13 + int *p = sizes() + 8; +#else short* p = sizes()+8; +#endif for (int i=children(); i--; p += 4) if (p[1] < oldx) oldx = p[1]; return oldx; @@ -46,7 +56,11 @@ int Panel::orgx() int Panel::orgy() { int oldy = h(); +#ifdef FLTK13 + int *p = sizes() + 8; +#else short* p = sizes()+8; +#endif for (int i=children(); i--; p += 4) if (p[3] < oldy) oldy = p[3]; return oldy; @@ -55,7 +69,11 @@ int Panel::orgy() void Panel::position(int oix, int oiy, int newx, int newy) { //printf("oix %3d, oiy %3d, nux %3d, nuy %3d\n", oix, oiy, newx, newy); Fl_Widget* const* a = array(); +#ifdef FLTK13 + int *p = sizes(); +#else short* p = sizes(); +#endif //printf("p0 %3d, p1 %3d, p2 %3d, p3 %3d\n", p[0], p[1], p[2], p[3]); //printf("p4 %3d, p5 %3d, p6 %3d, p7 %3d\n", p[0], p[1], p[2], p[3]); p += 8; // skip group & resizable's saved size @@ -90,7 +108,11 @@ void Panel::position(int oix, int oiy, int newx, int newy) { // move the lower-right corner (sort of): void Panel::resize(int X,int Y,int W,int H) { // remember how much to move the child widgets: +#ifdef FLTK13 + int *p = sizes(); +#else short* p = sizes(); +#endif int OX = x(); int OY = y(); int OW = w(); @@ -191,8 +213,13 @@ int Panel::handle(int event) { int oldx = 0; int oldy = 0; Fl_Widget*const* a = array(); +#ifdef FLTK13 + int *q = sizes(); + int *p = q + 8; +#else short* q = sizes(); - short* p = q+8; + short* p = q + 8; +#endif for (int i=children(); i--; p += 4) { Fl_Widget* o = *a++; if (o == resizable()) continue; @@ -227,7 +254,9 @@ int Panel::handle(int event) { case FL_DRAG: // This is necessary if CONSOLIDATE_MOTION in Fl_x.cxx is turned off: - // if (damage()) return 1; // don't fall behind +#ifdef FLTK13 + if (damage()) return 1; // don't fall behind +#endif case FL_RELEASE: { if (!sdrag) return 0; // should not happen Fl_Widget* r = resizable(); if (!r) r = this;