diff --git a/ChangeLog b/ChangeLog index a6032dca..66439e1c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,66 @@ -2010-03-26 Stelios Bounanos +2010-05-02 David Freese + ff47cc4: XCVR Freq + 5186a74: FLARQ 4.3.1 + + +=Version 3.20.1= + + 55eec2a: MAP macro update + e17ccfb: LOCK macro + 917b5bd: Freq Browser Select + 4474bd4: MAP macro + 2da1e55: ARQ auto file + 54a0569: Freq Control Font + +2010-04-22 Stelios Bounanos + + 5b7e839: Disable sprintf re-declaration + c5f34c6: Fix compiler warnings + 1733f1f: Add preliminary support for FLTK-1.3 + +2010-04-16 David Freese + + ab092a6: psk reporter mode + 6c15ddd: Psk Viewer Min Width + 2bc6105: Revert "SC Time Out" + +2010-04-12 Stelios Bounanos + + 5da5ba6: Update NEWS + +2010-04-12 David Freese + + aaaec18: WARNINGS + 8df121a: SC Time Out + 45654ea: PSKR fec metrics + faf5e6c: Pause-Break + 975f4a9: RX lowercase option + +2010-04-12 Stelios Bounanos + + d551f2e: Disable Czech l10n + f1762d7: Fix more widget labels + 4f80565: Update QSO frequency + +2010-04-08 David Freese + + 3754c63: Tx Pane Min Height + 717c151: Modem Init + 0469895: Contest Menu + 8f76576: AFC-SQL check buttons + 5dcf5cb: ADIF Export + 1b5f56f: Olivia-Contestia bandwidth + 0d77c1b: SSB modem + 586e3d5: QPSK dcd postamble + 1aae11e: RX_ONLY Modem type + d29495f: QSY Lock disable + 02767b0: Contestia + c10ee73: Freq Analysis Init + +2010-04-04 Stelios Bounanos + + 391167c: Add time functions 80572f5: Improve monitoring of transmitted signal 2010-03-25 David Freese @@ -513,3 +574,161 @@ 4fff116: Add missing include d1c206b: Restore MT-63 options dfab290: Initialise pthreads/win32 in flarq + ecf1f10: Restore two channel capture + a8a734c: Fix millisleep function + +2009-07-23 David Freese + + cf4c671: MT63 transmit bug fix + + +=Version 3.12= + + +2009-07-13 Stelios Bounanos + + 3537f62: Add RSID notifications + 2a4cd68: Improve frequency display kb entry + +2009-07-11 David Freese + + 9c291a8: Enable XMLRPC QSY capability + +2009-07-06 Stelios Bounanos + + deea0f6: Add ITU zone to countries list + d54a2ca: Fix compilation on glibc 2.10 + +2009-07-02 David Freese + + 9de9eb5: Xmlrpc mods + 07a01b1: Logbook bug fix + +2009-07-01 Stelios Bounanos + + 42b638d: Fix socket fd leak + 58c2f8b: Fix compilation error and warnings on 64bit archs + c07ec21: Add generic notifier + 88bedac: Add dragging to table widget + 757a8f8: Simplify RE classes + cbf0d87: Extend macro editor + 53a2820: Improve dxcc, spot, pskrep performance + 9648c30: Remove handling for REs without backref support + 1b474a2: Fix config input field labels + d6d5974: Add Fl_Input2 mouse wheel handling + bf4cea4: Add automatic FText text_area resizing + e803fcd: Improve FText drag and drop + ec26eb6: Add band to RX text tooltip + 8283833: Add string join and split functions + d70ea69: Print build info in crash handler + f25b5d1: Fix azimuth_long_path + d11f6fe: Add frequency display text paste support + +2009-06-30 David Freese + + 3bad2d4: xmlrpc mods + +2009-06-12 Stelios Bounanos + + 419123c: Fix text echo functions + 2a83da3: Remove FText text colour adjustment + 7a87b0b: Always set the FText cursor colour + +2009-06-12 David Freese + + a8dfa58: Logbook bug fix + 9b7786f: RigCAT mods + +2009-06-01 Stelios Bounanos + + 9f7b002: Fix Contest Country Files callsign lookup + +2009-06-01 David Freese + + b539c1c: ADIF_2.2.3 compliance + 8fc582e: Cut number mod for CW + +2009-05-26 dave + + 7690a1c: FLARQ + +2009-05-26 Stelios Bounanos + + d4b1a03: Merge flarq + 91a1425: Move setup_nls function + 9543fa7: Move make_pixmap function + ada43f4: Add new FText classes + bf6a2c3: Add thread id logging + 8207c62: Prefer ALSA default devices + d04b780: Fix initial window sizes + 4f90ecf: Fix format strings + +2009-05-16 David Freese + + 6b6cad9: PSK search algorithm + +2009-05-12 Stelios Bounanos + + b95b8e1: Add win32 installer support + d626273: Fix VPATH builds + +2009-05-05 David Freese + + b4112de: Added , macro tags + 7ae826f: RTTY pseudo output, corrected stop bit inversion + +2009-05-02 Stelios Bounanos + + ae5b9ca: Add more PNG text comments + 974e1ec: Remove JPEG write support + 44eceae: Fix xmlrpc-c m4 macro + +2009-05-02 David Freese + + b030b4b: Fix OS dependent compile error + abc11f7: Updated memory mapped rig contol + +2009-04-26 Stelios Bounanos + + 31e1902: Add missing includes + 6b2260c: Macro changes + 8f37399: Fix possible trx_startup deadlock + edca452: Fix Makefile DEBUG flag + +2009-04-25 David Freese + + affae09: CW default WPM (numeric keypad *) bug fix + +2009-04-23 Stelios Bounanos + + f7309ef: Make the combo box popup modal + ed0ae5c: Fix OS X native file selector + 88b3fd7: Fix uHRouter and parport PTT + 9c63531: Fix format string + +2009-04-23 David Freese + + 8944198: CW replay bug + fc50717: CW Farnsworth timing addition + 63ccc2f: MinGW serial i/o, hamlib, rigcat mods + +2009-04-18 Stelios Bounanos + + 206f815: Fix build without hamlib + +2009-04-18 David Freese + + a9c21e5: Escape key bug fix + d2674d5: Macro parse error fix + +2009-04-17 Stelios Bounanos + + 9e060b8: Update version output + 77bc966: Use band functions in logbook + 2124791: Add functions to set the socket buffer size + 4b1292c: Improve logging and error notification + d73c8fd: Add OS X and MinGW stack tracing + e192d1d: Add preliminary support for MinGW + 01fd3d9: Change set_log macro + 9d56656: Clean up headers + b4bf47f: Distribute cr.sh test script diff --git a/NEWS b/NEWS index b8a0bf3d..d1a91f7b 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,8 @@ * Added new PSK-500 mode * Added "robust PSK" modes: PSK-125R, PSK-250R, PSK-500R * Added PSK63F mode, compatible with MultiPsk PSK63FEC + * Added Contestia support + * Added SSB "modem" to simplify logging of SSB QSOs * Added MT63 modem preamble tones to aid signal acquisition * Added selectable filter widths for Hellschreiber modes * Added AGC and S/N detector for FM-HELL modes @@ -28,6 +30,11 @@ * Added , , , macros to search for a signal up and down in frequency, go to the "sweet spot frequency", and set the current modem frequency to ''nnnn'' + * The macro syntax is now . The older str syntax is + still supported. Modem parameters may be specified when using the new + syntax; currently for RTTY (shift, baud, bits), Olivia (bw, tones) and + Contestia (bw, tones), e.g.: , , + . == User interface == @@ -35,13 +42,14 @@ * The QSO toolbar can now be set to Contest mode, Minimal mode, Full mode, and some combinations thereof * The docked digiscope can be hidden and shown without a restart - * Is is now possible to select the modes for which to transmit RSID, VideoID + * It is now possible to select the modes for which to transmit RSID, VideoID and CWID * The Op Modes menu can now be customised * Removed the floating rig control window * Removed various Alt- shortcuts * Added a simple "wizard" for first-time users - * Added a waterfall-only mode; enabled with --wf-only + * Added a waterfall-only mode; enabled with --wfall-only + * Removed option to use check buttons for AFC and SQL toggles == Other changes == diff --git a/configure.ac b/configure.ac index 5a034763..0e588f29 100644 --- a/configure.ac +++ b/configure.ac @@ -9,10 +9,10 @@ dnl major and minor must be integers; patch may dnl contain other characters or be empty m4_define(FLDIGI_MAJOR, [3]) m4_define(FLDIGI_MINOR, [20]) -m4_define(FLDIGI_PATCH, [.0b9]) +m4_define(FLDIGI_PATCH, [.1]) m4_define(FLARQ_MAJOR, [4]) m4_define(FLARQ_MINOR, [3]) -m4_define(FLARQ_PATCH, [.0b1]) +m4_define(FLARQ_PATCH, [.1]) AC_INIT([dl-fldigi], FLDIGI_MAJOR.FLDIGI_MINOR[]FLDIGI_PATCH, [fldigi-devel@lists.berlios.de]) diff --git a/m4/fltk.m4 b/m4/fltk.m4 index 3a5ec0b7..89526581 100644 --- a/m4/fltk.m4 +++ b/m4/fltk.m4 @@ -22,15 +22,20 @@ AC_DEFUN([AC_FLDIGI_FLTK], [ if test $? -ne 0; then AC_MSG_ERROR([$FLTK_CONFIG failed]) fi - if test "x$FLTK_API_VERSION" = "x1.1"; then + if test -n "$(expr $FLTK_API_VERSION : '1\.1[3]')"; then HAVE_FLTK_API_VERSION=yes fi + FLDIGI_FLTK_API_MAJOR=${FLTK_API_VERSION%%.*} + FLDIGI_FLTK_API_MINOR=${FLTK_API_VERSION#*.}; FLDIGI_FLTK_API_MINOR=${FLDIGI_FLTK_API_MINOR%%.*} if test "${HAVE_FLTK_API_VERSION}" = "no"; then AC_MSG_ERROR([ *** The version of FLTK found on your system provides API version $FLTK_API_VERSION. - *** To build $PACKAGE you need a FLTK version that provides API 1.1. + *** To build $PACKAGE you need a FLTK version that provides API 1.1, 1.2 or 1.3. ]) fi + if test $FLDIGI_FLTK_API_MINOR -gt 1; then + AC_MSG_WARN([$PACKAGE_TARNAME may not work with FLTK $FLTK_API_VERSION]) + fi FLTK_CFLAGS=`$FLTK_CONFIG --cxxflags` if test "x$ac_cv_static" != "xyes"; then FLTK_LIBS=`$FLTK_CONFIG --ldflags --use-images` @@ -43,6 +48,8 @@ AC_DEFUN([AC_FLDIGI_FLTK], [ AC_SUBST([FLTK_CFLAGS]) AC_SUBST([FLTK_LIBS]) AC_DEFINE_UNQUOTED([FLTK_BUILD_VERSION], ["`$FLTK_CONFIG --version`"], [FLTK version]) + AC_DEFINE_UNQUOTED([FLDIGI_FLTK_API_MAJOR], [$FLDIGI_FLTK_API_MAJOR], [FLTK API major version]) + AC_DEFINE_UNQUOTED([FLDIGI_FLTK_API_MINOR], [$FLDIGI_FLTK_API_MINOR], [FLTK API minor version]) AC_ARG_VAR([FLUID], [Fast Light User-Interface Designer]) AC_CHECK_PROG([FLUID], [fluid], [fluid]) diff --git a/po/LINGUAS b/po/LINGUAS index 0c5c8cf0..527e861b 100644 --- a/po/LINGUAS +++ b/po/LINGUAS @@ -1 +1 @@ -cs fr +fr diff --git a/src/Makefile.am b/src/Makefile.am index f36d24f6..8143773a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -244,6 +244,7 @@ dl_fldigi_SOURCES += \ cw_rtty/cw.cxx \ cw_rtty/morse.cxx \ cw_rtty/rtty.cxx \ + contestia/contestia.cxx \ dialogs/colorsfonts.cxx \ dialogs/confdialog.cxx \ dialogs/fl_digi.cxx \ @@ -271,6 +272,7 @@ dl_fldigi_SOURCES += \ include/FTextView.h \ include/FTextRXTX.h \ include/fileselect.h \ + include/Fl_Text_Buffer_mod.H \ include/Fl_Text_Display_mod.H \ include/Fl_Text_Editor_mod.H \ include/Fl_Tile_Check.h \ @@ -282,6 +284,7 @@ dl_fldigi_SOURCES += \ include/combo.h \ include/complex.h \ include/configuration.h \ + include/contestia.h \ include/cw.h \ include/debug.h \ include/digiscope.h \ @@ -370,6 +373,7 @@ dl_fldigi_SOURCES += \ include/soundconf.h \ include/spot.h \ include/ssdv_rx.h \ + include/ssb.h \ include/stacktrace.h \ include/status.h \ include/strutil.h \ @@ -471,6 +475,7 @@ dl_fldigi_SOURCES += \ spot/spot.cxx \ ssdv/ssdv_rx.cxx \ ssdv/rs8decode.c \ + ssb/ssb.cxx \ throb/throb.cxx \ trx/modem.cxx \ trx/trx.cxx \ @@ -480,6 +485,7 @@ dl_fldigi_SOURCES += \ waterfall/waterfall.cxx \ widgets/FTextView.cxx \ widgets/FTextRXTX.cxx \ + widgets/Fl_Text_Buffer_mod.cxx \ widgets/Fl_Text_Display_mod.cxx \ widgets/Fl_Text_Editor_mod.cxx \ widgets/Fl_Tile_Check.cxx \ @@ -517,6 +523,12 @@ EXTRA_dl_fldigi_SOURCES += \ feld/FeldStyl-14.cxx \ feld/FeldVert-14.cxx \ feld/FeldWide-14.cxx \ + include/Fl_Text_Buffer_mod_1_1.H \ + include/Fl_Text_Buffer_mod_1_3.H \ + include/Fl_Text_Display_mod_1_1.H \ + include/Fl_Text_Display_mod_1_3.H \ + include/Fl_Text_Editor_mod_1_1.H \ + include/Fl_Text_Editor_mod_1_3.H \ mfsk/mfsk-pic.cxx \ mt63/alias_1k.dat \ mt63/alias_2k.dat \ @@ -525,7 +537,14 @@ EXTRA_dl_fldigi_SOURCES += \ mt63/alias_k5.dat \ mt63/mt63intl.dat \ trx/tune.cxx \ - dialogs/guide.cxx + dialogs/guide.cxx \ + widgets/Fl_Text_Buffer_mod_1_1.cxx \ + widgets/Fl_Text_Buffer_mod_1_3.cxx \ + widgets/Fl_Text_Display_mod_1_1.cxx \ + widgets/Fl_Text_Display_mod_1_3.cxx \ + widgets/Fl_Text_Editor_mod_1_1.cxx \ + widgets/Fl_Text_Editor_mod_1_3.cxx + flarq_SOURCES += \ dialogs/font_browser.cxx \ @@ -575,6 +594,7 @@ flarq_SOURCES += \ misc/re.cxx \ misc/socket.cxx \ misc/util.cxx \ + widgets/Fl_Text_Buffer_mod.cxx \ widgets/Fl_Text_Display_mod.cxx \ widgets/Fl_Text_Editor_mod.cxx \ widgets/FTextView.cxx diff --git a/src/contestia/contestia.cxx b/src/contestia/contestia.cxx new file mode 100644 index 00000000..12ed45ec --- /dev/null +++ b/src/contestia/contestia.cxx @@ -0,0 +1,371 @@ +// ---------------------------------------------------------------------------- +// contestia.cxx -- CONTESTIA modem +// +// Copyright (C) 2006-2010 +// Dave Freese, W1HKJ +// +// This file is part of fldigi. Adapted from code contained in gmfsk source code +// distribution. +// Copyright (C) 2005 +// Tomi Manninen (oh2bns@sral.fi) +// +// 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 . +// ---------------------------------------------------------------------------- + +#include + +#include + +#include "contestia.h" +#include "modem.h" +#include "fl_digi.h" + +#include "misc.h" +#include "confdialog.h" +#include "status.h" +#include "debug.h" + +LOG_FILE_SOURCE(debug::LOG_MODEM); + +using namespace std; + +double contestia::nco(double freq) +{ + preamblephase += 2.0 * M_PI * freq / samplerate; + + if (preamblephase > M_PI) + preamblephase -= 2.0 * M_PI; + + return cos(preamblephase); +} + +void contestia::tx_init(SoundBase *sc) +{ + unsigned char c; + + scard = sc; + phaseacc = 0; + prevsymbol = complex (1.0, 0.0); + preamble = 32; + shreg = 0; + + preamblesent = 0; + postamblesent = 0; + txbasefreq = get_txfreq_woffset(); + + Rx->Flush(); + + while (Rx->GetChar(c) > 0) + put_rx_char(c); + + if (reverse) { + Tx->FirstCarrierMultiplier = (txbasefreq + (Tx->Bandwidth / 2)) / 500; + Tx->Reverse = 1; + } else { + Tx->FirstCarrierMultiplier = (txbasefreq - (Tx->Bandwidth / 2)) / 500; + Tx->Reverse = 0; + } + + videoText(); + + Tx->Preset(); + Tx->Start(); + escape = 0; +} + +void contestia::send_tones() +{ + if (tone_midfreq != txbasefreq || tone_bw != bandwidth) { + double freqa, freqb; + tone_bw = bandwidth; + tone_midfreq = txbasefreq; + if (reverse) { + freqa = tone_midfreq + (tone_bw / 2.0); + freqb = tone_midfreq - (tone_bw / 2.0); + } else { + freqa = tone_midfreq - (tone_bw / 2.0); + freqb = tone_midfreq + (tone_bw / 2.0); + } + preamblephase = 0; + for (int i = 0; i < SR4; i++) + tonebuff[2*SR4 + i] = tonebuff[i] = nco(freqa) * ampshape[i]; + + preamblephase = 0; + for (int i = 0; i < SR4; i++) + tonebuff[3*SR4 + i] = tonebuff[SR4 + i] = nco(freqb) * ampshape[i]; + } + for (int j = 0; j < TONE_DURATION; j += SCBLOCKSIZE) + ModulateXmtr(&tonebuff[j], SCBLOCKSIZE); + +} + +void contestia::rx_init() +{ + Rx->Reset(); + escape = 0; +} + +int contestia::unescape(int c) +{ + if (progdefaults.contestia8bit == 0) + return c; + + if (escape) { + escape = 0; + return c + 128; + } + + if (c == 127) { + escape = 1; + return -1; + } + + return c; +} + +int contestia::tx_process() +{ + int c, len;//, i; + unsigned char ch; + + if (tones != progdefaults.contestiatones || + bw != progdefaults.contestiabw || + smargin != progdefaults.contestiasmargin || + sinteg != progdefaults.contestiasinteg ) + restart(); + + if (preamblesent != 1) { + send_tones(); + preamblesent = 1; + // Olivia Transmitter class requires at least character + Tx->PutChar(0); + } + +// The encoder works with BitsPerSymbol length blocks. If the +// modem already has that many characters buffered, don't try +// to read any more. If stopflag is set, we will always read +// whatever there is. + if (stopflag || (Tx->GetReadReady() < Tx->BitsPerSymbol)) { + if (!stopflag && (c = get_tx_char()) == 0x03) + stopflag = true; + if (stopflag) + Tx->Stop(); + else { + /* Replace un-representable characters with a dot */ + if (c == -1) + c = 0; + if (c > (progdefaults.contestia8bit ? 255 : 127)) + c = '.'; + if (c > 127) { + c &= 127; + Tx->PutChar(127); + } + Tx->PutChar(c); + } + } + + if (Tx->GetChar(ch) > 0) + if ((c = unescape(ch)) != -1) + put_echo_char(progdefaults.rx_lowercase ? tolower(c) : toupper(c)); + + if ((len = Tx->Output(txfbuffer)) > 0) + ModulateXmtr(txfbuffer, len); + + if (stopflag && Tx->DoPostambleYet() == 1 && postamblesent != 1) { + postamblesent = 1; + send_tones(); + } + + if (!Tx->Running()) { + cwid(); + stopflag = false; + return -1; + } + + return 0; +} + + +int contestia::rx_process(const double *buf, int len) +{ + int c; + unsigned char ch = 0; + static double snr = 1e-3; + static char msg1[20]; + static char msg2[20]; + + if (tones != progdefaults.contestiatones || + bw != progdefaults.contestiabw || + smargin != progdefaults.contestiasmargin || + sinteg != progdefaults.contestiasinteg ) + restart(); + + if ((lastfreq != frequency || Rx->Reverse) && !reverse) { + Rx->FirstCarrierMultiplier = (frequency - (Rx->Bandwidth / 2)) / 500; + Rx->Reverse = 0; + lastfreq = frequency; + Rx->Preset(); + } + else if ((lastfreq != frequency || !Rx->Reverse) && reverse) { + Rx->FirstCarrierMultiplier = (frequency + (Rx->Bandwidth / 2)) / 500; + Rx->Reverse = 1; + lastfreq = frequency; + Rx->Preset(); + } + + Rx->SyncThreshold = progStatus.sqlonoff ? + clamp(progStatus.sldrSquelchValue / 5.0 + 3.0, 3.0, 90.0) : 3.0; + + Rx->Process(buf, len); + sp = 0; + for (int i = frequency - Rx->Bandwidth/2; i < frequency - 1 + Rx->Bandwidth/2; i++) + if (wf->Pwr(i) > sp) + sp = wf->Pwr(i); + np = wf->Pwr(frequency + Rx->Bandwidth/2 + 2*Rx->Bandwidth/Rx->Tones); + if (np == 0) np = sp + 1e-8; + sigpwr = decayavg( sigpwr, sp, 10); + noisepwr = decayavg( noisepwr, np, 50); + snr = CLAMP(sigpwr / noisepwr, 0.001, 100000); + + metric = clamp( 5.0 * (Rx->SignalToNoiseRatio() - 3.0), 0, 100); + display_metric(metric); + + bool gotchar = false; + while (Rx->GetChar(ch) > 0) { + if ((c = unescape(ch)) != -1 && c > 7) { + put_rx_char(progdefaults.rx_lowercase ? tolower(c) : c); + gotchar = true; + } + } + if (gotchar) { + snprintf(msg1, sizeof(msg1), "s/n: %4.1f dB", 10*log10(snr) - 20); + put_Status1(msg1, 5, STATUS_CLEAR); + snprintf(msg2, sizeof(msg2), "f/o %+4.1f Hz", Rx->FrequencyOffset()); + put_Status2(msg2, 5, STATUS_CLEAR); + } + + return 0; +} + +void contestia::restart() +{ + tones = progdefaults.contestiatones; + bw = progdefaults.contestiabw; + smargin = progdefaults.contestiasmargin; + sinteg = progdefaults.contestiasinteg; + + samplerate = 8000; + bandwidth = 125 * (1 << bw); + + Tx->Tones = 2 * (1 << tones); + Tx->Bandwidth = bandwidth; + Tx->SampleRate = samplerate; + Tx->OutputSampleRate = samplerate; + txbasefreq = get_txfreq_woffset(); + Tx->bContestia = true; + + if (reverse) { + Tx->FirstCarrierMultiplier = (txbasefreq + (Tx->Bandwidth / 2)) / 500; + Tx->Reverse = 1; + } else { + Tx->FirstCarrierMultiplier = (txbasefreq - (Tx->Bandwidth / 2)) / 500; + Tx->Reverse = 0; + } + + if (Tx->Preset() < 0) { + LOG_ERROR("contestia: transmitter preset failed!"); + return; + } + + txbufferlen = Tx->MaxOutputLen; + + if (txfbuffer) delete [] txfbuffer; + txfbuffer = new double[txbufferlen]; + + Rx->Tones = Tx->Tones; + Rx->Bandwidth = bandwidth; + Rx->SyncMargin = smargin; + Rx->SyncIntegLen = sinteg; + Rx->SyncThreshold = progStatus.sqlonoff ? + clamp(progStatus.sldrSquelchValue / 5.0 + 3.0, 0, 90.0) : 0.0; + + Rx->SampleRate = samplerate; + Rx->InputSampleRate = samplerate; + Rx->bContestia = true; + + if (reverse) { + Rx->FirstCarrierMultiplier = (frequency + (Rx->Bandwidth / 2)) / 500; + Rx->Reverse = 1; + } else { + Rx->FirstCarrierMultiplier = (frequency - (Rx->Bandwidth /2)) / 500; + Rx->Reverse = 0; + } + + if (Rx->Preset() < 0) { + LOG_ERROR("contestia: receiver preset failed!"); + return; + } + fragmentsize = 1024; + set_bandwidth(Tx->Bandwidth - Tx->Bandwidth / Tx->Tones); + + put_MODEstatus("%s %" PRIuSZ "/%" PRIuSZ "", get_mode_name(), Tx->Tones, Tx->Bandwidth); + metric = 0; + + sigpwr = 1e-10; noisepwr = 1e-8; + LOG_INFO("\nContestia Rx parameters:\n%s", Rx->PrintParameters()); +} + +void contestia::init() +{ + restart(); + modem::init(); + set_scope_mode(Digiscope::BLANK); +} + +contestia::contestia() +{ + cap |= CAP_REV; + + txfbuffer = 0; + samplerate = 8000; + + Tx = new MFSK_Transmitter< double >; + Rx = new MFSK_Receiver< double >; + + Tx->bContestia = true; + Rx->bContestia = true; + + mode = MODE_CONTESTIA; + + lastfreq = 0; + + for (int i = 0; i < SR4; i++) ampshape[i] = 1.0; + for (int i = 0; i < SR4 / 8; i++) + ampshape[i] = ampshape[SR4 - 1 - i] = 0.5 * (1.0 - cos(M_PI * i / (SR4/8))); + + for (int i = 0; i < TONE_DURATION; i++) tonebuff[i] = 0; + + tone_bw = -1; + tone_midfreq = -1; +} + +contestia::~contestia() +{ + if (Tx) delete Tx; + if (Rx) delete Rx; + if (txfbuffer) delete [] txfbuffer; +} + diff --git a/src/cw_rtty/cw.cxx b/src/cw_rtty/cw.cxx index 8e1f5ec1..8fe2173c 100644 --- a/src/cw_rtty/cw.cxx +++ b/src/cw_rtty/cw.cxx @@ -66,7 +66,20 @@ void cw::rx_init() void cw::init() { - modem::init(); + bool wfrev = wf->Reverse(); + bool wfsb = wf->USB(); + reverse = wfrev ^ !wfsb; + + if (progdefaults.StartAtSweetSpot) + set_freq(progdefaults.CWsweetspot); + else if (progStatus.carrier != 0) { + set_freq(progStatus.carrier); +#if !BENCHMARK_MODE + progStatus.carrier = 0; +#endif + } else + set_freq(wf->Carrier()); + trackingfilter->reset(); cw_adaptive_receive_threshold = (long int)trackingfilter->run(2 * cw_send_dot_length); put_cwRcvWPM(cw_send_speed); @@ -347,7 +360,7 @@ int cw::rx_process(const double *buf, int len) } if (handle_event(CW_QUERY_EVENT, &c) == CW_SUCCESS) { while (*c) - put_rx_char(*c++); + put_rx_char(progdefaults.rx_lowercase ? tolower(*c++) : *c++); update_syncscope(); // display_metric(metric); } @@ -782,7 +795,7 @@ void cw::send_ch(int ch) flen -= symbollen; } if (flen) send_symbol(0, flen); - put_echo_char(ch); + put_echo_char(progdefaults.rx_lowercase ? tolower(ch) : ch); return; } diff --git a/src/cw_rtty/rtty.cxx b/src/cw_rtty/rtty.cxx index a8a0230f..c5080c7d 100644 --- a/src/cw_rtty/rtty.cxx +++ b/src/cw_rtty/rtty.cxx @@ -116,7 +116,20 @@ void rtty::rx_init() void rtty::init() { - modem::init(); + bool wfrev = wf->Reverse(); + bool wfsb = wf->USB(); + reverse = wfrev ^ !wfsb; + + if (progdefaults.StartAtSweetSpot) + set_freq(progdefaults.RTTYsweetspot); + else if (progStatus.carrier != 0) { + set_freq(progStatus.carrier); +#if !BENCHMARK_MODE + progStatus.carrier = 0; +#endif + } else + set_freq(wf->Carrier()); + rx_init(); put_MODEstatus(mode); snprintf(msg1, sizeof(msg1), "%-4.1f / %-4.0f", rtty_baud, rtty_shift); @@ -215,7 +228,7 @@ void rtty::restart() rtty::rtty(trx_mode tty_mode) { - cap = CAP_AFC | CAP_REV; + cap |= CAP_AFC | CAP_REV; mode = tty_mode; @@ -389,11 +402,13 @@ bool rtty::rx(bool bit) if (bit) { if ((metric >= progStatus.sldrSquelchValue && progStatus.sqlonoff)|| !progStatus.sqlonoff) { c = decode_char(); - if ( c != 0 ) put_rx_char(c); /* lb = estimated bytes lost */ lb = (lost - bytelen / 2) / bytelen; put_rx_ssdv(c, lb); + + if ( c != 0 ) + put_rx_char(progdefaults.rx_lowercase ? tolower(c) : c); } flag = true; lost = 0; @@ -711,7 +726,7 @@ void rtty::send_char(int c) else c = figures[c]; if (c) - put_echo_char(c); + put_echo_char(progdefaults.rx_lowercase ? tolower(c) : c); } } diff --git a/src/dialogs/Viewer.cxx b/src/dialogs/Viewer.cxx index fb8213b6..a36a80be 100644 --- a/src/dialogs/Viewer.cxx +++ b/src/dialogs/Viewer.cxx @@ -293,6 +293,7 @@ Fl_Double_Window* createViewer(void) cols[0] = labelwidth[progdefaults.VIEWERlabeltype]; int viewerwidth = (progStatus.VIEWERnchars * cwidth) + cols[0] + sbarwidth + border; + int minwidth = (30 * cwidth) + cols[0] + sbarwidth + border; int viewerheight = 40 + cheight * progdefaults.VIEWERchannels; int pad = border/2; @@ -305,7 +306,7 @@ Fl_Double_Window* createViewer(void) // search field const char* label = _("Find: "); fl_font(FL_HELVETICA, FL_NORMAL_SIZE); - inpSeek = new Fl_Input2(2 * border + fl_width(label), border, 200, g->h(), label); + inpSeek = new Fl_Input2(static_cast(2 * border + fl_width(label)), border, 200, g->h(), label); inpSeek->callback((Fl_Callback*)cb_Seek); inpSeek->when(FL_WHEN_CHANGED); inpSeek->textfont(FL_COURIER); @@ -356,7 +357,7 @@ Fl_Double_Window* createViewer(void) w->end(); w->callback((Fl_Callback*)cb_btnCloseViewer); w->resizable(brwsViewer); - w->size_range(w->w(), w->h() - brwsViewer->h() + 20); + w->size_range(minwidth, w->h() - brwsViewer->h() + 20); w->xclass(PACKAGE_NAME); return w; @@ -384,12 +385,12 @@ void viewer_redraw() brwsViewer->column_widths(cols); } -void viewaddchr(int ch, int freq, char c) +void viewaddchr(int ch, int freq, char c, int md) { if (!dlgViewer) return; if (progStatus.spot_recv) - spot_recv(c, ch, freq); + spot_recv(c, ch, freq, md); if (rfc != wf->rfcarrier() || usb != wf->USB()) viewer_redraw(); diff --git a/src/dialogs/colorsfonts.cxx b/src/dialogs/colorsfonts.cxx index dd147601..d58f49ff 100644 --- a/src/dialogs/colorsfonts.cxx +++ b/src/dialogs/colorsfonts.cxx @@ -92,6 +92,23 @@ void cbViewerFontBrowser(Fl_Widget*, void*) { font_browser->hide(); } +void cbFreqControlFontBrowser(Fl_Widget*, void*) { + Fl_Font fnt = font_browser->fontNumber(); + progdefaults.FreqControlFontnbr = fnt; + progdefaults.changed = true; + + FDdisplay->labelfont(progdefaults.FreqControlFontnbr); + FDdisplay->redraw(); + qsoFreqDisp1->font(progdefaults.FreqControlFontnbr); + qsoFreqDisp2->font(progdefaults.FreqControlFontnbr); + qsoFreqDisp3->font(progdefaults.FreqControlFontnbr); + qsoFreqDisp1->redraw(); + qsoFreqDisp2->redraw(); + qsoFreqDisp3->redraw(); + + font_browser->hide(); +} + Fl_Double_Window *dlgColorFont=(Fl_Double_Window *)0; Fl_Button *btnClrFntClose=(Fl_Button *)0; @@ -104,6 +121,18 @@ Fl_Tabs *tabsColors=(Fl_Tabs *)0; Fl_Box *FDdisplay=(Fl_Box *)0; +Fl_Button *btn_freq_control_font=(Fl_Button *)0; + +static void cb_btn_freq_control_font(Fl_Button*, void*) { + font_browser->fontNumber(progdefaults.FreqControlFontnbr); + font_browser->fontSize(14); + font_browser->fontColor(FL_FOREGROUND_COLOR); + font_browser->fontFilter(Font_Browser::FIXED_WIDTH); + font_browser->fontFilter(Font_Browser::ALL_TYPES); + font_browser->callback(cbFreqControlFontBrowser); +font_browser->show(); +} + Fl_Button *btnBackgroundColor=(Fl_Button *)0; static void cb_btnBackgroundColor(Fl_Button*, void*) { @@ -723,21 +752,25 @@ Fl_Double_Window* make_colorsfonts() { } // Fl_Button* btnClrFntClose { tabsColors = new Fl_Tabs(0, 5, 375, 185); { Fl_Group* o = new Fl_Group(5, 30, 365, 150, _("Freq Disp")); - { Fl_Box* o = FDdisplay = new Fl_Box(100, 45, 45, 67, _("8")); + { Fl_Box* o = FDdisplay = new Fl_Box(65, 43, 235, 45, _("14070.150")); FDdisplay->box(FL_DOWN_BOX); FDdisplay->color((Fl_Color)55); FDdisplay->labelfont(4); - FDdisplay->labelsize(48); + FDdisplay->labelsize(40); o->color(fl_rgb_color(progdefaults.FDbackground.R,progdefaults.FDbackground.G,progdefaults.FDbackground.B)); o->labelcolor(fl_rgb_color(progdefaults.FDforeground.R,progdefaults.FDforeground.G,progdefaults.FDforeground.B)); + o->labelfont(progdefaults.FreqControlFontnbr); } // Fl_Box* FDdisplay - { btnBackgroundColor = new Fl_Button(165, 45, 100, 30, _("Background")); + { btn_freq_control_font = new Fl_Button(144, 96, 87, 24, _("Font")); + btn_freq_control_font->callback((Fl_Callback*)cb_btn_freq_control_font); + } // Fl_Button* btn_freq_control_font + { btnBackgroundColor = new Fl_Button(35, 131, 87, 24, _("Bg Color")); btnBackgroundColor->callback((Fl_Callback*)cb_btnBackgroundColor); } // Fl_Button* btnBackgroundColor - { btnForegroundColor = new Fl_Button(165, 85, 100, 30, _("Foreground")); + { btnForegroundColor = new Fl_Button(144, 131, 87, 24, _("Digit Color")); btnForegroundColor->callback((Fl_Callback*)cb_btnForegroundColor); } // Fl_Button* btnForegroundColor - { btnFD_SystemColor = new Fl_Button(165, 125, 100, 30, _("System")); + { btnFD_SystemColor = new Fl_Button(259, 131, 87, 24, _("Sys Colors")); btnFD_SystemColor->callback((Fl_Callback*)cb_btnFD_SystemColor); } // Fl_Button* btnFD_SystemColor o->end(); diff --git a/src/dialogs/colorsfonts.fl b/src/dialogs/colorsfonts.fl index f25944da..daf62870 100644 --- a/src/dialogs/colorsfonts.fl +++ b/src/dialogs/colorsfonts.fl @@ -114,11 +114,29 @@ Function {cbViewerFontBrowser(Fl_Widget*, void*)} {open return_type void font_browser->hide();} {} } +Function {cbFreqControlFontBrowser(Fl_Widget*, void*)} {open return_type void +} { + code {Fl_Font fnt = font_browser->fontNumber(); + progdefaults.FreqControlFontnbr = fnt; + progdefaults.changed = true; + + FDdisplay->labelfont(progdefaults.FreqControlFontnbr); + FDdisplay->redraw(); + qsoFreqDisp1->font(progdefaults.FreqControlFontnbr); + qsoFreqDisp2->font(progdefaults.FreqControlFontnbr); + qsoFreqDisp3->font(progdefaults.FreqControlFontnbr); + qsoFreqDisp1->redraw(); + qsoFreqDisp2->redraw(); + qsoFreqDisp3->redraw(); + + font_browser->hide();} {} +} + Function {make_colorsfonts()} {open } { code {font_browser = new Font_Browser;} {} Fl_Window dlgColorFont { - label {Colors and Fonts} open selected + label {Colors and Fonts} open xywh {496 125 375 220} type Double code0 {dlgColorFont->xclass(PACKAGE_TARNAME);} visible } { @@ -135,13 +153,25 @@ Function {make_colorsfonts()} {open xywh {5 30 365 150} } { Fl_Box FDdisplay { - label 8 - xywh {100 45 45 67} box DOWN_BOX color 55 labelfont 4 labelsize 48 + label {14070.150} selected + xywh {65 43 235 45} box DOWN_BOX color 55 labelfont 4 labelsize 40 code0 {o->color(fl_rgb_color(progdefaults.FDbackground.R,progdefaults.FDbackground.G,progdefaults.FDbackground.B));} code1 {o->labelcolor(fl_rgb_color(progdefaults.FDforeground.R,progdefaults.FDforeground.G,progdefaults.FDforeground.B));} + code2 {o->labelfont(progdefaults.FreqControlFontnbr);} + } + Fl_Button btn_freq_control_font { + label Font + callback {font_browser->fontNumber(progdefaults.FreqControlFontnbr); + font_browser->fontSize(14); + font_browser->fontColor(FL_FOREGROUND_COLOR); + font_browser->fontFilter(Font_Browser::FIXED_WIDTH); + font_browser->fontFilter(Font_Browser::ALL_TYPES); + font_browser->callback(cbFreqControlFontBrowser); +font_browser->show();} + xywh {144 96 87 24} } Fl_Button btnBackgroundColor { - label Background + label {Bg Color} callback {uchar r, g, b; r = progdefaults.FDbackground.R; g = progdefaults.FDbackground.G; @@ -169,10 +199,10 @@ Function {make_colorsfonts()} {open } progdefaults.changed = true;} - xywh {165 45 100 30} + xywh {35 131 87 24} } Fl_Button btnForegroundColor { - label Foreground + label {Digit Color} callback {uchar r, g, b; r = progdefaults.FDforeground.R; g = progdefaults.FDforeground.G; @@ -200,10 +230,10 @@ Function {make_colorsfonts()} {open } progdefaults.changed = true;} - xywh {165 85 100 30} + xywh {144 131 87 24} } Fl_Button btnFD_SystemColor { - label System + label {Sys Colors} callback {uchar r, g, b; Fl_Color clr = FL_BACKGROUND2_COLOR; @@ -237,7 +267,7 @@ Function {make_colorsfonts()} {open } progdefaults.changed = true;} - xywh {165 125 100 30} + xywh {259 131 87 24} } } Fl_Group {} { diff --git a/src/dialogs/confdialog.cxx b/src/dialogs/confdialog.cxx index 81b4bee3..cc41e132 100644 --- a/src/dialogs/confdialog.cxx +++ b/src/dialogs/confdialog.cxx @@ -466,12 +466,10 @@ progdefaults.changed = true; WF_UI(); } -Fl_Group *tabWfallRestart=(Fl_Group *)0; +Fl_Check_Button *btn_rx_lowercase=(Fl_Check_Button *)0; -Fl_Check_Button *btnCheckButtons=(Fl_Check_Button *)0; - -static void cb_btnCheckButtons(Fl_Check_Button* o, void*) { - progdefaults.useCheckButtons = o->value(); +static void cb_btn_rx_lowercase(Fl_Check_Button* o, void*) { + progdefaults.rx_lowercase = o->value(); progdefaults.changed = true; } @@ -1091,6 +1089,49 @@ static void cb_btnOlivia_8bit(Fl_Check_Button* o, void*) { progdefaults.changed = true; } +Fl_Group *tabContestia=(Fl_Group *)0; + +Fl_Choice *mnuContestia_Bandwidth=(Fl_Choice *)0; + +static void cb_mnuContestia_Bandwidth(Fl_Choice* o, void*) { + progdefaults.contestiabw = o->value(); +set_contestia_default_integ(); +resetOLIVIA(); +progdefaults.changed = true; +} + +Fl_Choice *mnuContestia_Tones=(Fl_Choice *)0; + +static void cb_mnuContestia_Tones(Fl_Choice* o, void*) { + progdefaults.contestiatones = o->value(); +set_contestia_default_integ(); +resetCONTESTIA(); +progdefaults.changed = true; +} + +Fl_Counter2 *cntContestia_smargin=(Fl_Counter2 *)0; + +static void cb_cntContestia_smargin(Fl_Counter2* o, void*) { + progdefaults.contestiasmargin = (int)(o->value()); +resetCONTESTIA(); +progdefaults.changed = true; +} + +Fl_Counter2 *cntContestia_sinteg=(Fl_Counter2 *)0; + +static void cb_cntContestia_sinteg(Fl_Counter2* o, void*) { + progdefaults.contestiasinteg = (int)(o->value()); +resetCONTESTIA(); +progdefaults.changed = true; +} + +Fl_Check_Button *btnContestia_8bit=(Fl_Check_Button *)0; + +static void cb_btnContestia_8bit(Fl_Check_Button* o, void*) { + progdefaults.contestia8bit = o->value(); +progdefaults.changed = true; +} + Fl_Group *tabPSK=(Fl_Group *)0; Fl_Tabs *tabsPSK=(Fl_Tabs *)0; @@ -2583,6 +2624,8 @@ static const char szParity[] = "none|even|odd|zero|one"; static const char szStopBits[] = "1|1.5|2"; static const char szOliviaTones[] = "2|4|8|16|32|64|128|256"; static const char szOliviaBandwidth[] = "125|250|500|1000|2000"; +static const char szContestiaTones[] = "2|4|8|16|32|64|128|256"; +static const char szContestiaBandwidth[] = "125|250|500|1000|2000"; static const char szBaudRates[] = "300|600|1200|2400|4800|9600|19200|38400|57600|115200|230400|460800"; { Fl_Double_Window* o = new Fl_Double_Window(500, 400, _("Fldigi configuration")); w = o; @@ -2590,9 +2633,9 @@ static const char szBaudRates[] = "300|600|1200|2400|4800|9600|19200|38400|57600 o->selection_color((Fl_Color)51); o->labelsize(18); o->align(FL_ALIGN_CLIP|FL_ALIGN_INSIDE); - { tabsConfigure = new Fl_Tabs(-3, 0, 503, 375); - tabsConfigure->color(FL_LIGHT1); - tabsConfigure->selection_color(FL_LIGHT1); + { tabsConfigure = new Fl_Tabs(-3, 0, 500, 372); + tabsConfigure->color((Fl_Color)FL_LIGHT1); + tabsConfigure->selection_color((Fl_Color)FL_LIGHT1); { tabOperator = new Fl_Group(0, 25, 500, 345, _("Operator")); tabOperator->tooltip(_("Operator information")); tabOperator->labelsize(12); @@ -2717,13 +2760,11 @@ static const char szBaudRates[] = "300|600|1200|2400|4800|9600|19200|38400|57600 } // Fl_Input* MyRadio tabOperator->end(); } // Fl_Group* tabOperator - { tabUI = new Fl_Group(-3, 25, 503, 345, _("UI")); - tabUI->labelsize(12); + { tabUI = new Fl_Group(0, 25, 500, 345, _("UI")); tabUI->hide(); { tabsUI = new Fl_Tabs(-3, 25, 503, 345); tabsUI->selection_color(FL_LIGHT1); { tabUserInterface = new Fl_Group(0, 50, 500, 320, _("General")); - tabUserInterface->hide(); { Fl_Group* o = new Fl_Group(5, 60, 490, 301); o->box(FL_ENGRAVED_FRAME); { Fl_Check_Button* o = btnShowTooltips = new Fl_Check_Button(15, 70, 120, 20, _("Show tooltips")); @@ -2986,7 +3027,8 @@ ab and newline are automatically included.")); } // Fl_Group* o tabContest->end(); } // Fl_Group* tabContest - { tabWF_UI = new Fl_Group(0, 50, 500, 320, _("Oper\' Controls")); + { tabWF_UI = new Fl_Group(0, 50, 500, 320, _("Operator Controls")); + tabWF_UI->hide(); { Fl_Box* o = new Fl_Box(31, 65, 446, 25, _("Enable check box to show each respective operator control")); o->box(FL_FLAT_BOX); o->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE); @@ -3065,22 +3107,15 @@ ab and newline are automatically included.")); } // Fl_Button* btn_wf_disable_all tabWF_UI->end(); } // Fl_Group* tabWF_UI - { tabWfallRestart = new Fl_Group(0, 50, 500, 320, _("Restart")); - tabWfallRestart->hide(); - { Fl_Group* o = new Fl_Group(5, 63, 490, 70, _("Changes take effect on next program startup")); - o->tooltip(_("Show me more or less waterfall")); - o->box(FL_ENGRAVED_FRAME); - o->align(FL_ALIGN_TOP|FL_ALIGN_INSIDE); - { Fl_Check_Button* o = btnCheckButtons = new Fl_Check_Button(52, 93, 275, 20, _("Use check buttons for AFC and SQL")); - btnCheckButtons->tooltip(_("Check buttons or default lighted switch")); - btnCheckButtons->down_box(FL_DOWN_BOX); - btnCheckButtons->callback((Fl_Callback*)cb_btnCheckButtons); - o->value(progdefaults.useCheckButtons); - } // Fl_Check_Button* btnCheckButtons - o->end(); - } // Fl_Group* o - tabWfallRestart->end(); - } // Fl_Group* tabWfallRestart + { Fl_Group* o = new Fl_Group(0, 50, 500, 320, _("Rx Text")); + o->hide(); + { Fl_Check_Button* o = btn_rx_lowercase = new Fl_Check_Button(25, 75, 389, 15, _("print CW / RTTY / THROB / CONTESTIA in lowercase")); + btn_rx_lowercase->down_box(FL_DOWN_BOX); + btn_rx_lowercase->callback((Fl_Callback*)cb_btn_rx_lowercase); + o->value(progdefaults.rx_lowercase); + } // Fl_Check_Button* btn_rx_lowercase + o->end(); + } // Fl_Group* o tabsUI->end(); } // Fl_Tabs* tabsUI tabUI->end(); @@ -3092,6 +3127,7 @@ ab and newline are automatically included.")); tabsWaterfall->color(FL_LIGHT1); tabsWaterfall->selection_color(FL_LIGHT1); { Fl_Group* o = new Fl_Group(0, 50, 500, 320, _("Display")); + o->hide(); { Fl_Group* o = new Fl_Group(5, 60, 490, 162, _("Colors and cursors")); o->box(FL_ENGRAVED_FRAME); o->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE); @@ -3107,6 +3143,7 @@ ab and newline are automatically included.")); WF_Palette->align(FL_ALIGN_TOP_LEFT); WF_Palette->when(FL_WHEN_RELEASE); o->label(progdefaults.PaletteName.c_str()); + o->labelsize(FL_NORMAL_SIZE); } // colorbox* WF_Palette { btnColor[0] = new Fl_Button(15, 128, 20, 24); btnColor[0]->tooltip(_("Change color")); @@ -3234,7 +3271,7 @@ ab and newline are automatically included.")); valTxMonitorLevel->align(FL_ALIGN_TOP); valTxMonitorLevel->when(FL_WHEN_CHANGED); o->value(progdefaults.TxMonitorLevel); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* valTxMonitorLevel o->end(); } // Fl_Group* o @@ -3352,7 +3389,6 @@ an merging")); o->end(); } // Fl_Group* o { Fl_Group* o = new Fl_Group(0, 50, 500, 320, _("Mouse")); - o->hide(); { Fl_Group* o = new Fl_Group(5, 62, 490, 170); o->box(FL_ENGRAVED_FRAME); { Fl_Check_Button* o = btnWaterfallHistoryDefault = new Fl_Check_Button(15, 76, 340, 20, _("Left or right click always replays audio history")); @@ -3401,10 +3437,9 @@ an merging")); tabWaterfall->end(); } // Fl_Group* tabWaterfall { tabModems = new Fl_Group(0, 25, 500, 345, _("Modems")); - tabModems->labelsize(12); tabModems->hide(); - { tabsModems = new Fl_Tabs(0, 25, 500, 345); - tabsModems->selection_color(FL_LIGHT1); + { tabsModems = new Fl_Tabs(0, 25, 501, 345); + tabsModems->selection_color((Fl_Color)FL_LIGHT1); tabsModems->align(FL_ALIGN_TOP_RIGHT); { tabCW = new Fl_Group(0, 50, 500, 320, _("CW")); tabCW->hide(); @@ -3434,7 +3469,7 @@ an merging")); sldrCWbandwidth->align(FL_ALIGN_RIGHT); sldrCWbandwidth->when(FL_WHEN_CHANGED); o->value(progdefaults.CWbandwidth); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrCWbandwidth { Fl_Check_Button* o = btnCWrcvTrack = new Fl_Check_Button(40, 150, 80, 20, _("Tracking")); btnCWrcvTrack->tooltip(_("Automatic Rx speed tracking")); @@ -3500,7 +3535,7 @@ an merging")); sldrCWxmtWPM->align(FL_ALIGN_RIGHT); sldrCWxmtWPM->when(FL_WHEN_CHANGED); o->value(progdefaults.CWspeed); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrCWxmtWPM { Fl_Counter2* o = cntCWdefWPM = new Fl_Counter2(40, 281, 64, 20, _("Default")); cntCWdefWPM->tooltip(_("The default CW speed")); @@ -3565,7 +3600,7 @@ an merging")); sldrCWfarnsworth->align(FL_ALIGN_RIGHT); sldrCWfarnsworth->when(FL_WHEN_CHANGED); o->value(progdefaults.CWfarnsworth); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrCWfarnsworth { Fl_Check_Button* o = btnCWusefarnsworth = new Fl_Check_Button(40, 312, 180, 15, _("Use Farnsworth timing")); btnCWusefarnsworth->down_box(FL_DOWN_BOX); @@ -3726,7 +3761,7 @@ an merging")); } // Fl_Tabs* tabsCW tabCW->end(); } // Fl_Group* tabCW - { tabDomEX = new Fl_Group(0, 50, 500, 320, _("DominoEX")); + { tabDomEX = new Fl_Group(0, 50, 500, 320, _("DomEX")); tabDomEX->hide(); { Fl_Group* o = new Fl_Group(5, 60, 490, 180); o->box(FL_ENGRAVED_FRAME); @@ -3792,7 +3827,7 @@ an merging")); valDomCWI->align(FL_ALIGN_TOP); valDomCWI->when(FL_WHEN_CHANGED); o->value(progdefaults.DomCWI); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* valDomCWI { Fl_Counter2* o = valDominoEX_PATHS = new Fl_Counter2(339, 194, 63, 20, _("Paths (hidden)")); valDominoEX_PATHS->type(1); @@ -3887,7 +3922,7 @@ an merging")); sldrHellBW->align(FL_ALIGN_TOP_LEFT); sldrHellBW->when(FL_WHEN_CHANGED); o->value(progdefaults.HELL_BW); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrHellBW { Fl_Check_Button* o = btnFeldHellIdle = new Fl_Check_Button(15, 177, 230, 20, _("Transmit periods (.) when idle")); btnFeldHellIdle->tooltip(_("Transmits a diddle dot when no keyboard activity")); @@ -4035,7 +4070,80 @@ an merging")); } // Fl_Group* o tabOlivia->end(); } // Fl_Group* tabOlivia - { tabPSK = new Fl_Group(0, 50, 500, 320, _("PSK")); + { tabContestia = new Fl_Group(0, 50, 500, 320, _("Contestia")); + tabContestia->hide(); + { Fl_Group* o = new Fl_Group(5, 60, 490, 200); + o->box(FL_ENGRAVED_FRAME); + { Fl_Choice* o = mnuContestia_Bandwidth = new Fl_Choice(60, 80, 85, 20, _("Bandwidth")); + mnuContestia_Bandwidth->tooltip(_("Select bandwidth")); + mnuContestia_Bandwidth->down_box(FL_BORDER_BOX); + mnuContestia_Bandwidth->callback((Fl_Callback*)cb_mnuContestia_Bandwidth); + mnuContestia_Bandwidth->align(FL_ALIGN_RIGHT); + o->add(szContestiaBandwidth); + o->value(2); + } // Fl_Choice* mnuContestia_Bandwidth + { Fl_Choice* o = mnuContestia_Tones = new Fl_Choice(321, 80, 70, 20, _("Tones")); + mnuContestia_Tones->tooltip(_("Select number of tones")); + mnuContestia_Tones->down_box(FL_BORDER_BOX); + mnuContestia_Tones->callback((Fl_Callback*)cb_mnuContestia_Tones); + mnuContestia_Tones->align(FL_ALIGN_RIGHT); + o->add(szContestiaTones); + o->value(2); + } // Fl_Choice* mnuContestia_Tones + { Fl_Group* o = new Fl_Group(60, 119, 379, 100, _("Receive synchronizer")); + o->box(FL_ENGRAVED_FRAME); + o->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE); + { Fl_Counter2* o = cntContestia_smargin = new Fl_Counter2(80, 150, 70, 20, _("Tune margin (tone frequency spacing)")); + cntContestia_smargin->tooltip(_("Change ONLY to experiment")); + cntContestia_smargin->type(1); + cntContestia_smargin->box(FL_UP_BOX); + cntContestia_smargin->color((Fl_Color)FL_BACKGROUND_COLOR); + cntContestia_smargin->selection_color((Fl_Color)FL_INACTIVE_COLOR); + cntContestia_smargin->labeltype(FL_NORMAL_LABEL); + cntContestia_smargin->labelfont(0); + cntContestia_smargin->labelsize(14); + cntContestia_smargin->labelcolor((Fl_Color)FL_FOREGROUND_COLOR); + cntContestia_smargin->minimum(2); + cntContestia_smargin->maximum(128); + cntContestia_smargin->step(1); + cntContestia_smargin->value(8); + cntContestia_smargin->callback((Fl_Callback*)cb_cntContestia_smargin); + cntContestia_smargin->align(FL_ALIGN_RIGHT); + cntContestia_smargin->when(FL_WHEN_CHANGED); + o->labelsize(FL_NORMAL_SIZE); + } // Fl_Counter2* cntContestia_smargin + { Fl_Counter2* o = cntContestia_sinteg = new Fl_Counter2(80, 180, 70, 20, _("Integration period (FEC blocks)")); + cntContestia_sinteg->tooltip(_("Change ONLY to experiment")); + cntContestia_sinteg->type(1); + cntContestia_sinteg->box(FL_UP_BOX); + cntContestia_sinteg->color((Fl_Color)FL_BACKGROUND_COLOR); + cntContestia_sinteg->selection_color((Fl_Color)FL_INACTIVE_COLOR); + cntContestia_sinteg->labeltype(FL_NORMAL_LABEL); + cntContestia_sinteg->labelfont(0); + cntContestia_sinteg->labelsize(14); + cntContestia_sinteg->labelcolor((Fl_Color)FL_FOREGROUND_COLOR); + cntContestia_sinteg->minimum(2); + cntContestia_sinteg->maximum(128); + cntContestia_sinteg->step(1); + cntContestia_sinteg->value(4); + cntContestia_sinteg->callback((Fl_Callback*)cb_cntContestia_sinteg); + cntContestia_sinteg->align(FL_ALIGN_RIGHT); + cntContestia_sinteg->when(FL_WHEN_CHANGED); + o->labelsize(FL_NORMAL_SIZE); + } // Fl_Counter2* cntContestia_sinteg + o->end(); + } // Fl_Group* o + { btnContestia_8bit = new Fl_Check_Button(60, 229, 200, 20, _("8-bit extended characters")); + btnContestia_8bit->tooltip(_("Enable this for Latin-1 accented characters")); + btnContestia_8bit->down_box(FL_DOWN_BOX); + btnContestia_8bit->callback((Fl_Callback*)cb_btnContestia_8bit); + btnContestia_8bit->hide(); + } // Fl_Check_Button* btnContestia_8bit + o->end(); + } // Fl_Group* o + tabContestia->end(); + } // Fl_Group* tabContestia + { tabPSK = new Fl_Group(0, 50, 517, 320, _("PSK")); tabPSK->hide(); { tabsPSK = new Fl_Tabs(0, 50, 500, 320); tabsPSK->selection_color(FL_LIGHT1); @@ -4262,7 +4370,7 @@ an merging")); btnCRCRLF->when(FL_WHEN_RELEASE_ALWAYS); o->value(progdefaults.rtty_crcrlf); } // Fl_Check_Button* btnCRCRLF - { cntrAUTOCRLF = new Fl_Counter2(335, 118, 65, 20, _("characters")); + { Fl_Counter2* o = cntrAUTOCRLF = new Fl_Counter2(335, 118, 65, 20, _("characters")); cntrAUTOCRLF->tooltip(_("Set page width")); cntrAUTOCRLF->type(1); cntrAUTOCRLF->box(FL_UP_BOX); @@ -4279,6 +4387,7 @@ an merging")); cntrAUTOCRLF->callback((Fl_Callback*)cb_cntrAUTOCRLF); cntrAUTOCRLF->align(FL_ALIGN_RIGHT); cntrAUTOCRLF->when(FL_WHEN_CHANGED); + o->labelsize(FL_NORMAL_SIZE); } // Fl_Counter2* cntrAUTOCRLF { Fl_Box* o = new Fl_Box(265, 118, 60, 20, _("after:")); o->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); @@ -4347,7 +4456,7 @@ an merging")); sldrRTTYbandwidth->align(FL_ALIGN_TOP_LEFT); sldrRTTYbandwidth->when(FL_WHEN_CHANGED); o->value(progdefaults.RTTY_BW); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrRTTYbandwidth { Fl_Counter2* o = selCustomShift = new Fl_Counter2(15, 100, 100, 20, _("Custom shift")); selCustomShift->tooltip(_("Input carrier shift")); @@ -4432,7 +4541,7 @@ an merging")); valThorCWI->align(FL_ALIGN_TOP); valThorCWI->when(FL_WHEN_CHANGED); o->value(progdefaults.ThorCWI); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* valThorCWI { Fl_Counter2* o = valTHOR_PATHS = new Fl_Counter2(353, 193, 75, 21, _("Paths (hidden)")); valTHOR_PATHS->type(1); @@ -5167,7 +5276,7 @@ ll with your audio device.")); valPCMvolume->callback((Fl_Callback*)cb_valPCMvolume); valPCMvolume->align(FL_ALIGN_RIGHT); valPCMvolume->when(FL_WHEN_CHANGED); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* valPCMvolume o->end(); } // Fl_Group* o @@ -5236,7 +5345,7 @@ ll with your audio device.")); sldrVideowidth->when(FL_WHEN_CHANGED); o->value(progdefaults.videowidth); if (progdefaults.ID_SMALL) o->deactivate(); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrVideowidth { bVideoIDModes = new Fl_Button(365, 67, 120, 20, _("Video ID modes")); bVideoIDModes->callback((Fl_Callback*)cb_bVideoIDModes); @@ -5271,7 +5380,7 @@ ll with your audio device.")); sldrCWIDwpm->align(FL_ALIGN_TOP); sldrCWIDwpm->when(FL_WHEN_CHANGED); o->value(progdefaults.CWIDwpm); - o->labelsize(FL_NORMAL_SIZE); + o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE); } // Fl_Value_Slider2* sldrCWIDwpm { bCWIDModes = new Fl_Button(365, 185, 120, 20, _("CW ID modes")); bCWIDModes->callback((Fl_Callback*)cb_bCWIDModes); diff --git a/src/dialogs/confdialog.fl b/src/dialogs/confdialog.fl index 6707c815..23d74948 100644 --- a/src/dialogs/confdialog.fl +++ b/src/dialogs/confdialog.fl @@ -89,6 +89,8 @@ static const char szParity[] = "none|even|odd|zero|one"; static const char szStopBits[] = "1|1.5|2"; static const char szOliviaTones[] = "2|4|8|16|32|64|128|256"; static const char szOliviaBandwidth[] = "125|250|500|1000|2000"; +static const char szContestiaTones[] = "2|4|8|16|32|64|128|256"; +static const char szContestiaBandwidth[] = "125|250|500|1000|2000"; static const char szBaudRates[] = "300|600|1200|2400|4800|9600|19200|38400|57600|115200|230400|460800";} {} code {} {} Fl_Window {} { @@ -96,12 +98,12 @@ static const char szBaudRates[] = "300|600|1200|2400|4800|9600|19200|38400|57600 xywh {832 622 500 400} type Double color 45 selection_color 51 labelsize 18 align 80 visible } { Fl_Tabs tabsConfigure {open - xywh {-3 0 503 375} color 50 selection_color 50 + xywh {-3 0 500 372} color 50 selection_color 50 } { Fl_Group tabOperator { label Operator - callback {progdefaults.changed = true;} open - tooltip {Operator information} xywh {0 25 500 345} labelsize 12 when 1 hide + callback {progdefaults.changed = true;} selected + tooltip {Operator information} xywh {0 25 500 345} when 1 } { Fl_Group {} { label Station open @@ -202,14 +204,14 @@ progdefaults.changed = true;} } Fl_Group tabUI { label UI open - xywh {-3 25 503 345} labelsize 12 hide + xywh {0 25 500 345} hide } { Fl_Tabs tabsUI {open xywh {-3 25 503 345} selection_color 50 } { Fl_Group tabUserInterface { - label General open - xywh {0 50 500 320} hide + label General + xywh {0 50 500 320} } { Fl_Group {} {open xywh {5 60 490 301} box ENGRAVED_FRAME @@ -324,7 +326,7 @@ progdefaults.changed = true;} } } Fl_Group tabContest { - label Contest open + label Contest xywh {0 50 500 320} hide } { Fl_Group {} { @@ -464,8 +466,8 @@ progdefaults.changed = true;} } } Fl_Group tabWF_UI { - label {Oper' Controls} open - xywh {0 50 500 320} + label {Operator Controls} + xywh {0 50 500 320} hide } { Fl_Box {} { label {Enable check box to show each respective operator control} @@ -598,35 +600,30 @@ WF_UI();} xywh {301 321 88 20} } } - Fl_Group tabWfallRestart { - label Restart open + Fl_Group {} { + label {Rx Text} open xywh {0 50 500 320} hide } { - Fl_Group {} { - label {Changes take effect on next program startup} open - tooltip {Show me more or less waterfall} xywh {5 63 490 70} box ENGRAVED_FRAME align 17 - } { - Fl_Check_Button btnCheckButtons { - label {Use check buttons for AFC and SQL} - callback {progdefaults.useCheckButtons = o->value(); + Fl_Check_Button btn_rx_lowercase { + label {print CW / RTTY / THROB / CONTESTIA in lowercase} + callback {progdefaults.rx_lowercase = o->value(); progdefaults.changed = true;} - tooltip {Check buttons or default lighted switch} xywh {52 93 275 20} down_box DOWN_BOX - code0 {o->value(progdefaults.useCheckButtons);} - } + xywh {25 75 389 15} down_box DOWN_BOX + code0 {o->value(progdefaults.rx_lowercase);} } } } } Fl_Group tabWaterfall { - label Waterfall open - xywh {0 25 500 347} labelsize 12 hide + label Waterfall + xywh {0 25 500 347} hide } { Fl_Tabs tabsWaterfall {open xywh {0 25 500 347} color 50 selection_color 50 } { Fl_Group {} { label Display open - xywh {0 50 500 320} + xywh {0 50 500 320} hide } { Fl_Group {} { label {Colors and cursors} open @@ -637,6 +634,7 @@ progdefaults.changed = true;} xywh {15 98 260 24} box DOWN_BOX color 0 labelsize 12 align 5 code0 {\#include "colorbox.h"} code1 {o->label(progdefaults.PaletteName.c_str());} + code2 {o->labelsize(FL_NORMAL_SIZE);} class colorbox } Fl_Button {btnColor[0]} { @@ -803,7 +801,7 @@ progdefaults.changed = true;} progdefaults.changed = true;} tooltip {Set level for good viewing} xywh {270 315 200 20} type Horizontal align 1 step 0.05 value 0.5 textsize 14 code0 {o->value(progdefaults.TxMonitorLevel);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } } @@ -880,7 +878,7 @@ progdefaults.changed = true;} } Fl_Group {} { label Mouse open - xywh {0 50 500 320} hide + xywh {0 50 500 320} } { Fl_Group {} {open xywh {5 62 490 170} box ENGRAVED_FRAME @@ -930,11 +928,11 @@ behaves inside the waterfall} xywh {15 196 150 22} down_box BORDER_BOX align 8 } } Fl_Group tabModems { - label Modems open - xywh {0 25 500 345} labelsize 12 hide + label Modems + xywh {0 25 500 345} hide } { Fl_Tabs tabsModems {open - xywh {0 25 500 345} selection_color 50 align 9 + xywh {0 25 501 345} selection_color 50 align 9 } { Fl_Group tabCW { label CW open @@ -957,7 +955,7 @@ behaves inside the waterfall} xywh {15 196 150 22} down_box BORDER_BOX align 8 progdefaults.changed = true;} tooltip {CW dsp filter bandwidth} xywh {40 115 290 20} type Horizontal align 8 minimum 10 maximum 500 step 10 value 150 textsize 14 code0 {o->value(progdefaults.CWbandwidth);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Check_Button btnCWrcvTrack { @@ -1003,7 +1001,7 @@ progdefaults.changed = true; sync_cw_parameters();} tooltip {My transmit CW WPM} xywh {20 240 400 20} type Horizontal align 8 minimum 5 maximum 100 step 1 value 20 textsize 14 code0 {o->value(progdefaults.CWspeed);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Counter cntCWdefWPM { @@ -1047,7 +1045,7 @@ cntCW_WPM->maximum(progdefaults.CWupperlimit);} progdefaults.changed = true;} tooltip {My transmit CW WPM} xywh {20 335 400 20} type Horizontal align 8 minimum 5 maximum 100 step 1 value 20 textsize 14 code0 {o->value(progdefaults.CWfarnsworth);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Check_Button btnCWusefarnsworth { @@ -1163,7 +1161,7 @@ progdefaults.changed = true;} } } Fl_Group tabDomEX { - label DominoEX open + label DomEX open xywh {0 50 500 320} hide } { Fl_Group {} {open @@ -1208,7 +1206,7 @@ progdefaults.changed = true;} progdefaults.changed = true;} tooltip {CWI detection and suppression} xywh {15 207 260 20} type Horizontal align 1 textsize 14 code0 {o->value(progdefaults.DomCWI);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Counter valDominoEX_PATHS { @@ -1276,7 +1274,7 @@ progdefaults.changed = true;} open callback {progdefaults.HELL_BW = sldrHellBW->value();} tooltip {Adjust the DSP bandwidth} xywh {206 147 246 20} type Horizontal align 5 minimum 10 maximum 2400 step 5 value 400 textsize 14 code0 {o->value(progdefaults.HELL_BW);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Check_Button btnFeldHellIdle { @@ -1422,6 +1420,64 @@ progdefaults.changed = true;} } } } + Fl_Group tabContestia { + label Contestia open + xywh {0 50 500 320} hide + } { + Fl_Group {} {open + xywh {5 60 490 200} box ENGRAVED_FRAME + } { + Fl_Choice mnuContestia_Bandwidth { + label Bandwidth + callback {progdefaults.contestiabw = o->value(); +set_contestia_default_integ(); +resetOLIVIA(); +progdefaults.changed = true;} open + tooltip {Select bandwidth} xywh {60 80 85 20} down_box BORDER_BOX align 8 + code0 {o->add(szContestiaBandwidth);} + code1 {o->value(2);} + } {} + Fl_Choice mnuContestia_Tones { + label Tones + callback {progdefaults.contestiatones = o->value(); +set_contestia_default_integ(); +resetCONTESTIA(); +progdefaults.changed = true;} open + tooltip {Select number of tones} xywh {321 80 70 20} down_box BORDER_BOX align 8 + code0 {o->add(szContestiaTones);} + code1 {o->value(2);} + } {} + Fl_Group {} { + label {Receive synchronizer} open + xywh {60 119 379 100} box ENGRAVED_FRAME align 21 + } { + Fl_Counter cntContestia_smargin { + label {Tune margin (tone frequency spacing)} + callback {progdefaults.contestiasmargin = (int)(o->value()); +resetCONTESTIA(); +progdefaults.changed = true;} + tooltip {Change ONLY to experiment} xywh {80 150 70 20} type Simple align 8 minimum 2 maximum 128 step 1 value 8 + code0 {o->labelsize(FL_NORMAL_SIZE);} + class Fl_Counter2 + } + Fl_Counter cntContestia_sinteg { + label {Integration period (FEC blocks)} + callback {progdefaults.contestiasinteg = (int)(o->value()); +resetCONTESTIA(); +progdefaults.changed = true;} + tooltip {Change ONLY to experiment} xywh {80 180 70 20} type Simple align 8 minimum 2 maximum 128 step 1 value 4 + code0 {o->labelsize(FL_NORMAL_SIZE);} + class Fl_Counter2 + } + } + Fl_Check_Button btnContestia_8bit { + label {8-bit extended characters} + callback {progdefaults.contestia8bit = o->value(); +progdefaults.changed = true;} + tooltip {Enable this for Latin-1 accented characters} xywh {60 229 200 20} down_box DOWN_BOX hide + } + } + } Fl_Group tabPSK { label PSK open xywh {0 50 500 320} hide @@ -1630,6 +1686,7 @@ progdefaults.changed = true;} callback {progdefaults.rtty_autocount = (int)o->value(); progdefaults.changed = true;} tooltip {Set page width} xywh {335 118 65 20} type Simple align 8 minimum 68 maximum 80 step 1 value 72 + code0 {o->labelsize(FL_NORMAL_SIZE);} class Fl_Counter2 } Fl_Box {} { @@ -1695,7 +1752,7 @@ progdefaults.changed = true;} callback {progdefaults.RTTY_BW = o->value();} tooltip {Adjust the DSP bandwidth} xywh {100 325 300 20} type Horizontal align 5 minimum 5 maximum 200 step 1 value 25 textsize 14 code0 {o->value(progdefaults.RTTY_BW);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Counter selCustomShift { @@ -1749,7 +1806,7 @@ progdefaults.changed = true;} progdefaults.changed = true;} tooltip {CWI detection and suppression} xywh {15 194 260 20} type Horizontal align 1 textsize 14 code0 {o->value(progdefaults.ThorCWI);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Counter valTHOR_PATHS { @@ -1767,8 +1824,8 @@ progdefaults.changed = true;} } } Fl_Group tabRig { - label Rig open - tooltip {Transceiver control} xywh {0 25 500 345} labelsize 12 hide + label Rig + tooltip {Transceiver control} xywh {0 25 500 345} hide } { Fl_Tabs tabsRig {open xywh {0 25 500 345} selection_color 50 @@ -2346,8 +2403,8 @@ progdefaults.changed = true;} } } Fl_Group tabSoundCard { - label Audio open - tooltip {Audio devices} xywh {0 25 500 345} labelsize 12 hide + label Audio + tooltip {Audio devices} xywh {0 25 500 345} hide } { Fl_Tabs tabsSoundCard {open xywh {0 25 500 345} selection_color 50 @@ -2588,7 +2645,7 @@ progdefaults.changed = true;} progdefaults.changed = true;} tooltip {Set the sound card PCM level} xywh {15 167 340 20} type Horizontal selection_color 15 align 8 value 0.8 textsize 14 code0 {extern void setPCMvolume(double);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } } @@ -2596,8 +2653,8 @@ progdefaults.changed = true;} } } Fl_Group tabID { - label ID open - xywh {0 25 500 345} labelsize 12 hide + label ID + xywh {0 25 500 345} hide } { Fl_Group {} { label {Video Preamble ID} open @@ -2645,7 +2702,7 @@ progdefaults.changed = true;} tooltip {Set the number of characters per row} xywh {365 127 120 20} type Horizontal align 4 minimum 1 maximum 4 step 1 value 1 textsize 14 code0 {o->value(progdefaults.videowidth);} code1 {if (progdefaults.ID_SMALL) o->deactivate();} - code2 {o->labelsize(FL_NORMAL_SIZE);} + code2 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Button bVideoIDModes { @@ -2674,7 +2731,7 @@ progdefaults.changed = true;} progdefaults.changed = true;} tooltip {Send at this WPM} xywh {165 185 180 20} type Horizontal align 1 minimum 15 maximum 40 step 1 value 18 textsize 14 code0 {o->value(progdefaults.CWIDwpm);} - code1 {o->labelsize(FL_NORMAL_SIZE);} + code1 {o->labelsize(FL_NORMAL_SIZE); o->textsize(FL_NORMAL_SIZE);} class Fl_Value_Slider2 } Fl_Button bCWIDModes { @@ -2761,8 +2818,8 @@ progdefaults.changed = true;} } } Fl_Group tabMisc { - label Misc open - xywh {0 25 500 345} labelsize 12 hide + label Misc + xywh {0 25 500 345} hide } { Fl_Tabs tabsMisc {open xywh {0 25 500 345} selection_color 50 @@ -3058,8 +3115,8 @@ progdefaults.changed = true;} } } Fl_Group tabQRZ { - label {Callsign DB} open - tooltip {Callsign database} xywh {0 25 500 345} labelsize 12 hide + label {Callsign DB} + tooltip {Callsign database} xywh {0 25 500 345} hide } { Fl_Group {} { label CDROM open diff --git a/src/dialogs/fl_digi.cxx b/src/dialogs/fl_digi.cxx index 6eaefc95..2d8c9c2a 100644 --- a/src/dialogs/fl_digi.cxx +++ b/src/dialogs/fl_digi.cxx @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -77,12 +78,14 @@ #include "mt63.h" #include "rtty.h" #include "olivia.h" +#include "contestia.h" #include "thor.h" #include "dominoex.h" #include "feld.h" #include "throb.h" #include "wwv.h" #include "analysis.h" +#include "ssb.h" #include "ascii.h" #include "globals.h" @@ -157,8 +160,6 @@ int leftof(Fl_Widget* w); int above(Fl_Widget* w); int below(Fl_Widget* w); -//bool useCheckButtons; - Fl_Group *mnuFrame; Fl_Menu_Bar *mnu; @@ -387,6 +388,16 @@ void cb_oliviaD(Fl_Widget *w, void *arg); void cb_oliviaE(Fl_Widget *w, void *arg); void cb_oliviaCustom(Fl_Widget *w, void *arg); +void cb_contestiaA(Fl_Widget *w, void *arg); +void cb_contestiaB(Fl_Widget *w, void *arg); +void cb_contestiaC(Fl_Widget *w, void *arg); +void cb_contestiaD(Fl_Widget *w, void *arg); +void cb_contestiaE(Fl_Widget *w, void *arg); +void cb_contestiaF(Fl_Widget *w, void *arg); +void cb_contestiaG(Fl_Widget *w, void *arg); +void cb_contestiaH(Fl_Widget *w, void *arg); +void cb_contestiaCustom(Fl_Widget *w, void *arg); + void cb_rtty45(Fl_Widget *w, void *arg); void cb_rtty50(Fl_Widget *w, void *arg); void cb_rttyHAB50(Fl_Widget *w, void *arg); @@ -492,6 +503,19 @@ Fl_Menu_Item quick_change_olivia[] = { { 0 } }; +Fl_Menu_Item quick_change_contestia[] = { + { "4/250", 0, cb_contestiaA, (void *)MODE_CONTESTIA }, + { "8/250", 0, cb_contestiaB, (void *)MODE_CONTESTIA }, + { "4/500", 0, cb_contestiaC, (void *)MODE_CONTESTIA }, + { "8/500", 0, cb_contestiaD, (void *)MODE_CONTESTIA }, + { "16/500", 0, cb_contestiaE, (void *)MODE_CONTESTIA }, + { "8/1000", 0, cb_contestiaF, (void *)MODE_CONTESTIA }, + { "16/1000", 0, cb_contestiaG, (void *)MODE_CONTESTIA }, + { "32/1000", 0, cb_contestiaH, (void *)MODE_CONTESTIA }, + { _("Custom..."), 0, cb_contestiaCustom, (void *)MODE_CONTESTIA }, + { 0 } +}; + Fl_Menu_Item quick_change_rtty[] = { { "RTTY-45", 0, cb_rtty45, (void *)MODE_RTTY }, { "RTTY-50", 0, cb_rtty50, (void *)MODE_RTTY }, @@ -506,6 +530,7 @@ inline int minmax(int val, int min, int max) return val > min ? val : min; } +// Olivia void set_olivia_default_integ() { int tones = progdefaults.oliviatones; @@ -578,6 +603,105 @@ void cb_oliviaCustom(Fl_Widget *w, void *arg) cb_init_mode(w, arg); } +// Contestia +void set_contestia_default_integ() +{ + int tones = progdefaults.contestiatones; + int bw = progdefaults.contestiabw; + + if (tones < 1) tones = 1; + int depth = minmax( (8 * (1 << bw)) / (1 << tones), 4, 4 * (1 << bw)); + + progdefaults.contestiasinteg = depth; + cntContestia_sinteg->value(depth); +} + +void set_contestia_tab_widgets() +{ + mnuContestia_Bandwidth->value(progdefaults.contestiabw); + mnuContestia_Tones->value(progdefaults.contestiatones); + set_contestia_default_integ(); +} + +void cb_contestiaA(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 1; + progdefaults.contestiabw = 1; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaB(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 1; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaC(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 1; + progdefaults.contestiabw = 2; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaD(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 2; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaE(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 3; + progdefaults.contestiabw = 2; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaF(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 3; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaG(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 3; + progdefaults.contestiabw = 3; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaH(Fl_Widget *w, void *arg) +{ + progdefaults.contestiatones = 4; + progdefaults.contestiabw = 3; + set_contestia_tab_widgets(); + cb_init_mode(w, arg); +} + +void cb_contestiaCustom(Fl_Widget *w, void *arg) +{ + modem_config_tab = tabContestia; + tabsConfigure->value(tabModems); + tabsModems->value(modem_config_tab); +#if USE_HAMLIB + hamlib_restore_defaults(); +#endif + rigCAT_restore_defaults();; + dlgConfig->show(); + cb_init_mode(w, arg); +} + +// + void set_rtty_tab_widgets() { progdefaults.rtty_parity = 0; @@ -857,6 +981,13 @@ void init_modem(trx_mode mode, int freq) quick_change = quick_change_olivia; break; + case MODE_CONTESTIA: + startup_modem(*mode_info[mode].modem ? *mode_info[mode].modem : + *mode_info[mode].modem = new contestia, freq); + modem_config_tab = tabContestia; + quick_change = quick_change_contestia; + break; + case MODE_RTTY: startup_modem(*mode_info[mode].modem ? *mode_info[mode].modem : *mode_info[mode].modem = new rtty(mode), freq); @@ -881,6 +1012,11 @@ void init_modem(trx_mode mode, int freq) *mode_info[mode].modem = new anal, freq); break; + case MODE_SSB: + startup_modem(*mode_info[mode].modem ? *mode_info[mode].modem : + *mode_info[mode].modem = new ssb, freq); + break; + default: LOG_ERROR("Unknown mode: %" PRIdPTR, mode); return init_modem(MODE_PSK31, freq); @@ -1519,7 +1655,7 @@ void cb_ShowNBEMS(Fl_Widget*, void*) void cbTune(Fl_Widget *w, void *) { Fl_Button *b = (Fl_Button *)w; - if (active_modem == wwv_modem || active_modem == anal_modem) { + if (!(active_modem->get_cap() & modem::CAP_TX)) { b->value(0); return; } @@ -1869,6 +2005,7 @@ void qsoClear_cb(Fl_Widget *b, void *) if (clearlog) { clearQSO(); } + clear_Lookup(); restoreFocus(); } @@ -2121,6 +2258,7 @@ bool clean_exit(void) { #define OPMODES_FEWER _("Show fewer modes") #define OPMODES_ALL _("Show all modes") #define OLIVIA_MLABEL "Olivia" +#define CONTESTIA_MLABEL "Contestia" #define RTTY_MLABEL "RTTY" #define VIEW_MLABEL _("&View") #define MFSK_IMAGE_MLABEL _("&MFSK image") @@ -2349,6 +2487,18 @@ Fl_Menu_Item menu_[] = { { mode_info[MODE_CW].name, 0, cb_init_mode, (void *)MODE_CW, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ CONTESTIA_MLABEL, 0, 0, 0, FL_SUBMENU, FL_NORMAL_LABEL, 0, 14, 0}, +{ "4/250", 0, cb_contestiaA, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "8/250", 0, cb_contestiaB, (void *)MODE_CONTESTIA, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, +{ "4/500", 0, cb_contestiaC, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "8/500", 0, cb_contestiaD, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "16/500", 0, cb_contestiaE, (void *)MODE_CONTESTIA, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, +{ "8/1000", 0, cb_contestiaF, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "16/1000", 0, cb_contestiaG, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "32/1000", 0, cb_contestiaH, (void *)MODE_CONTESTIA, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, +{ _("Custom..."), 0, cb_contestiaCustom, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{0,0,0,0,0,0,0,0,0}, + {"DominoEX", 0, 0, 0, FL_SUBMENU, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX4].name, 0, cb_init_mode, (void *)MODE_DOMINOEX4, 0, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX5].name, 0, cb_init_mode, (void *)MODE_DOMINOEX5, 0, FL_NORMAL_LABEL, 0, 14, 0}, @@ -2442,7 +2592,7 @@ Fl_Menu_Item menu_[] = { { mode_info[MODE_THROBX4].name, 0, cb_init_mode, (void *)MODE_THROBX4, 0, FL_NORMAL_LABEL, 0, 14, 0}, {0,0,0,0,0,0,0,0,0}, -{"NBEMS modes", 0, 0, 0, FL_SUBMENU, FL_NORMAL_LABEL, 0, 14, 0}, +{"NBEMS modes", 0, 0, 0, FL_SUBMENU | FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX11].name, 0, cb_init_mode, (void *)MODE_DOMINOEX11, 0, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX22].name, 0, cb_init_mode, (void *)MODE_DOMINOEX22, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_MFSK16].name, 0, cb_init_mode, (void *)MODE_MFSK16, 0, FL_NORMAL_LABEL, 0, 14, 0}, @@ -2451,6 +2601,8 @@ Fl_Menu_Item menu_[] = { { mode_info[MODE_PSK250].name, 0, cb_init_mode, (void *)MODE_PSK250, 0, FL_NORMAL_LABEL, 0, 14, 0}, {0,0,0,0,0,0,0,0,0}, +{ mode_info[MODE_SSB].name, 0, cb_init_mode, (void *)MODE_SSB, 0, FL_NORMAL_LABEL, 0, 14, 0}, + { mode_info[MODE_WWV].name, 0, cb_init_mode, (void *)MODE_WWV, 0, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_ANALYSIS].name, 0, cb_init_mode, (void *)MODE_ANALYSIS, 0, FL_NORMAL_LABEL, 0, 14, 0}, @@ -2588,6 +2740,10 @@ static void cb_opmode_show(Fl_Widget* w, void*) getMenuItem("Olivia")->show(); else getMenuItem("Olivia")->hide(); + if (progdefaults.visible_modes.test(MODE_CONTESTIA)) + getMenuItem("Contestia")->show(); + else + getMenuItem("Contestia")->hide(); if (progdefaults.visible_modes.test(MODE_RTTY)) getMenuItem("RTTY")->show(); else @@ -3043,7 +3199,7 @@ void create_fl_digi_main_primary() { qsoFreqDisp1->color(FL_BACKGROUND_COLOR); qsoFreqDisp1->selection_color(FL_BACKGROUND_COLOR); qsoFreqDisp1->labeltype(FL_NORMAL_LABEL); - qsoFreqDisp1->labelfont(0); + qsoFreqDisp1->font(progdefaults.FreqControlFontnbr); qsoFreqDisp1->labelsize(12); qsoFreqDisp1->labelcolor(FL_FOREGROUND_COLOR); qsoFreqDisp1->align(FL_ALIGN_CENTER); @@ -3056,7 +3212,7 @@ void create_fl_digi_main_primary() { fl_rgb_color( progdefaults.FDbackground.R, progdefaults.FDbackground.G, progdefaults.FDbackground.B)); - qsoFreqDisp1->value(145580000); + qsoFreqDisp1->value(0); Y = Hmenu + 2 * (Hentry + pad); @@ -3349,6 +3505,7 @@ void create_fl_digi_main_primary() { qsoFreqDisp2->align(FL_ALIGN_CENTER); qsoFreqDisp2->when(FL_WHEN_RELEASE); qsoFreqDisp2->callback(qso_movFreq); + qsoFreqDisp2->font(progdefaults.FreqControlFontnbr); qsoFreqDisp2->SetONOFFCOLOR( fl_rgb_color( progdefaults.FDforeground.R, progdefaults.FDforeground.G, @@ -3356,7 +3513,7 @@ void create_fl_digi_main_primary() { fl_rgb_color( progdefaults.FDbackground.R, progdefaults.FDbackground.G, progdefaults.FDbackground.B)); - qsoFreqDisp2->value(145580000); + qsoFreqDisp2->value(0); qso_opPICK2 = new Fl_Button( rightof(qsoFreqDisp2), y, @@ -3389,7 +3546,7 @@ void create_fl_digi_main_primary() { const char *label2 = _("On"); btnTimeOn2 = new Fl_Button( pad + rightof(qsoSave2), y, - fl_width(label2), h, label2); + static_cast(fl_width(label2)), h, label2); btnTimeOn2->tooltip(_("Press to update")); btnTimeOn2->box(FL_NO_BOX); btnTimeOn2->callback(cb_btnTimeOn); @@ -3401,7 +3558,7 @@ void create_fl_digi_main_primary() { const char *label3 = _("Off"); Fl_Box *bx3 = new Fl_Box(pad + rightof(inpTimeOn2), y, - fl_width(label3), h, label3); + static_cast(fl_width(label3)), h, label3); inpTimeOff2 = new Fl_Input2( pad + bx3->x() + bx3->w(), y, w_inpTime2, h, ""); @@ -3410,7 +3567,7 @@ void create_fl_digi_main_primary() { const char *label4 = _("Call"); Fl_Box *bx4 = new Fl_Box(pad + rightof(inpTimeOff2), y, - fl_width(label4), h, label4); + static_cast(fl_width(label4)), h, label4); inpCall2 = new Fl_Input2( pad + bx4->x() + bx4->w(), y, w_inpCall2, h, ""); @@ -3418,7 +3575,7 @@ void create_fl_digi_main_primary() { const char *label6 = _("In"); Fl_Box *bx6 = new Fl_Box(pad + rightof(inpCall2), y, - fl_width(label6), h, label6); + static_cast(fl_width(label6)), h, label6); inpRstIn2 = new Fl_Input2( pad + bx6->x() + bx6->w(), y, w_inpRstIn2, h, ""); @@ -3426,7 +3583,7 @@ void create_fl_digi_main_primary() { const char *label7 = _("Out"); Fl_Box *bx7 = new Fl_Box(pad + rightof(inpRstIn2), y, - fl_width(label7), h, label7); + static_cast(fl_width(label7)), h, label7); inpRstOut2 = new Fl_Input2( pad + bx7->x() + bx7->w(), y, w_inpRstOut2, h, ""); @@ -3434,7 +3591,7 @@ void create_fl_digi_main_primary() { const char *label5 = _("Nm");//_("Name"); Fl_Box *bx5 = new Fl_Box(pad + rightof(inpRstOut2), y, - fl_width(label5), h, label5); + static_cast(fl_width(label5)), h, label5); int xn = pad + bx5->x() + bx5->w(); inpName2 = new Fl_Input2( xn, y, @@ -3460,6 +3617,7 @@ void create_fl_digi_main_primary() { qsoFreqDisp3->align(FL_ALIGN_CENTER); qsoFreqDisp3->when(FL_WHEN_RELEASE); qsoFreqDisp3->callback(qso_movFreq); + qsoFreqDisp3->font(progdefaults.FreqControlFontnbr); qsoFreqDisp3->SetONOFFCOLOR( fl_rgb_color( progdefaults.FDforeground.R, progdefaults.FDforeground.G, @@ -3467,7 +3625,7 @@ void create_fl_digi_main_primary() { fl_rgb_color( progdefaults.FDbackground.R, progdefaults.FDbackground.G, progdefaults.FDbackground.B)); - qsoFreqDisp3->value(145580000); + qsoFreqDisp3->value(0); qso_opPICK3 = new Fl_Button( rightof(qsoFreqDisp3), y, @@ -3499,12 +3657,12 @@ void create_fl_digi_main_primary() { const char *label7a = _("Ex"); const char *xData = "00000"; const char *xCall = "WW8WWW/WWWW"; - int wData = fl_width(xData); - int wCall = fl_width(xCall); + int wData = static_cast(fl_width(xData)); + int wCall = static_cast(fl_width(xCall)); Fl_Box *bx4a = new Fl_Box( pad + rightof(qsoSave3), y, - fl_width(label4a), h, label4a); + static_cast(fl_width(label4a)), h, label4a); inpCall3 = new Fl_Input2( pad + bx4a->x() + bx4a->w(), y, wCall, h, ""); @@ -3513,22 +3671,22 @@ void create_fl_digi_main_primary() { Fl_Box *bx7a = new Fl_Box( rightof(inpCall3), y, - fl_width(label7a), h, label7a); + static_cast(fl_width(label7a)), h, label7a); bx7a->align(FL_ALIGN_INSIDE); inpXchgIn2 = new Fl_Input2( rightof(bx7a), y, - progStatus.mainW + static_cast(progStatus.mainW - rightof(bx7a) - pad - fl_width(label6a) - wData - pad - fl_width(label5a) - wData - pad - fl_width(label2a) - wData - pad - - fl_width(label3a) - wData - pad, + - fl_width(label3a) - wData - pad), h, ""); inpXchgIn2->tooltip(_("Contest exchange in")); Fl_Box *bx6a = new Fl_Box( rightof(inpXchgIn2), y, - fl_width(label6a), h, label6a); + static_cast(fl_width(label6a)), h, label6a); bx6a->align(FL_ALIGN_INSIDE); inpSerNo2 = new Fl_Input2( rightof(bx6a) + pad, y, @@ -3537,7 +3695,7 @@ void create_fl_digi_main_primary() { Fl_Box *bx5a = new Fl_Box( rightof(inpSerNo2), y, - fl_width(label5a), h, label5a); + static_cast(fl_width(label5a)), h, label5a); bx5a->align(FL_ALIGN_INSIDE); outSerNo2 = new Fl_Input2( rightof(bx5a) + pad, y, @@ -3547,7 +3705,7 @@ void create_fl_digi_main_primary() { btnTimeOn3 = new Fl_Button( rightof(outSerNo2), y, - fl_width(label2a), h, label2a); + static_cast(fl_width(label2a)), h, label2a); btnTimeOn3->tooltip(_("Press to update")); btnTimeOn3->box(FL_NO_BOX); btnTimeOn3->callback(cb_btnTimeOn); @@ -3558,7 +3716,7 @@ void create_fl_digi_main_primary() { inpTimeOn3->type(FL_INT_INPUT); Fl_Box *bx3a = new Fl_Box(pad + rightof(inpTimeOn3), y, - fl_width(label3a), h, label3a); + static_cast(fl_width(label3a)), h, label3a); inpTimeOff3 = new Fl_Input2( bx3a->x() + bx3a->w() + pad, y, wData, h, ""); @@ -3648,7 +3806,7 @@ void create_fl_digi_main_primary() { TransmitText->setFontColor(progdefaults.SKIPcolor, FTextBase::SKIP); TransmitText->setFontColor(progdefaults.ALTRcolor, FTextBase::ALTR); - Fl_Box *minbox = new Fl_Box(sw,Y + 66, progStatus.mainW-sw, Htext - 66 - 32); + Fl_Box *minbox = new Fl_Box(sw,Y + 66, progStatus.mainW-sw, Htext - 66 - 66); minbox->hide(); TiledGroup->resizable(minbox); @@ -3779,27 +3937,17 @@ void create_fl_digi_main_primary() { #ifdef __APPLE__ sql_width -= 15; // leave room for resize handle #endif - if (progdefaults.useCheckButtons) { - btnAFC = new Fl_Check_Button( - progStatus.mainW - bwSqlOnOff - bwAfcOnOff, - Hmenu+Hrcvtxt+Hxmttxt+Hwfall, - bwAfcOnOff, Hstatus, "AFC"); - btnSQL = new Fl_Check_Button( - progStatus.mainW - bwSqlOnOff, - Hmenu+Hrcvtxt+Hxmttxt+Hwfall, - sql_width, Hstatus, "SQL"); - } else { - btnAFC = new Fl_Light_Button( - progStatus.mainW - bwSqlOnOff - bwAfcOnOff, - Hmenu+Hrcvtxt+Hxmttxt+Hwfall, - bwAfcOnOff, Hstatus, "AFC"); - btnAFC->selection_color(progdefaults.AfcColor); - btnSQL = new Fl_Light_Button( - progStatus.mainW - bwSqlOnOff, - Hmenu+Hrcvtxt+Hxmttxt+Hwfall, - sql_width, Hstatus, "SQL"); - btnSQL->selection_color(progdefaults.Sql1Color); - } + btnAFC = new Fl_Light_Button( + progStatus.mainW - bwSqlOnOff - bwAfcOnOff, + Hmenu+Hrcvtxt+Hxmttxt+Hwfall, + bwAfcOnOff, Hstatus, "AFC"); + btnAFC->selection_color(progdefaults.AfcColor); + btnSQL = new Fl_Light_Button( + progStatus.mainW - bwSqlOnOff, + Hmenu+Hrcvtxt+Hxmttxt+Hwfall, + sql_width, Hstatus, "SQL"); + btnSQL->selection_color(progdefaults.Sql1Color); + btnAFC->callback(cbAFC, 0); btnAFC->value(1); btnAFC->tooltip(_("Automatic Frequency Control")); @@ -3925,6 +4073,18 @@ Fl_Menu_Item alt_menu_[] = { { mode_info[MODE_CW].name, 0, cb_init_mode, (void *)MODE_CW, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{"Contestia", 0, 0, 0, FL_SUBMENU, FL_NORMAL_LABEL, 0, 14, 0}, +{ "4/250", 0, cb_contestiaA, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "8/250", 0, cb_contestiaB, (void *)MODE_CONTESTIA, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, +{ "4/500", 0, cb_contestiaC, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "8/500", 0, cb_contestiaD, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "16/500", 0, cb_contestiaE, (void *)MODE_CONTESTIA, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, +{ "8/1000", 0, cb_contestiaF, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "16/1000", 0, cb_contestiaG, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{ "32/1000", 0, cb_contestiaH, (void *)MODE_CONTESTIA, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, +{ _("Custom..."), 0, cb_contestiaCustom, (void *)MODE_CONTESTIA, 0, FL_NORMAL_LABEL, 0, 14, 0}, +{0,0,0,0,0,0,0,0,0}, + {"DominoEX", 0, 0, 0, FL_SUBMENU, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX4].name, 0, cb_init_mode, (void *)MODE_DOMINOEX4, 0, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX5].name, 0, cb_init_mode, (void *)MODE_DOMINOEX5, 0, FL_NORMAL_LABEL, 0, 14, 0}, @@ -4009,7 +4169,7 @@ Fl_Menu_Item alt_menu_[] = { { mode_info[MODE_THROBX4].name, 0, cb_init_mode, (void *)MODE_THROBX4, 0, FL_NORMAL_LABEL, 0, 14, 0}, {0,0,0,0,0,0,0,0,0}, -{"NBEMS modes", 0, 0, 0, FL_SUBMENU, FL_NORMAL_LABEL, 0, 14, 0}, +{"NBEMS modes", 0, 0, 0, FL_SUBMENU | FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX11].name, 0, cb_init_mode, (void *)MODE_DOMINOEX11, 0, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_DOMINOEX22].name, 0, cb_init_mode, (void *)MODE_DOMINOEX22, FL_MENU_DIVIDER, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_MFSK16].name, 0, cb_init_mode, (void *)MODE_MFSK16, 0, FL_NORMAL_LABEL, 0, 14, 0}, @@ -4018,6 +4178,8 @@ Fl_Menu_Item alt_menu_[] = { { mode_info[MODE_PSK250].name, 0, cb_init_mode, (void *)MODE_PSK250, 0, FL_NORMAL_LABEL, 0, 14, 0}, {0,0,0,0,0,0,0,0,0}, +{ mode_info[MODE_SSB].name, 0, cb_init_mode, (void *)MODE_SSB, 0, FL_NORMAL_LABEL, 0, 14, 0}, + { mode_info[MODE_WWV].name, 0, cb_init_mode, (void *)MODE_WWV, 0, FL_NORMAL_LABEL, 0, 14, 0}, { mode_info[MODE_ANALYSIS].name, 0, cb_init_mode, (void *)MODE_ANALYSIS, 0, FL_NORMAL_LABEL, 0, 14, 0}, @@ -4384,27 +4546,16 @@ void create_fl_digi_main_WF_only() { #ifdef __APPLE__ sql_width -= 15; // leave room for resize handle #endif - if (progdefaults.useCheckButtons) { - btnAFC = new Fl_Check_Button( - progStatus.mainW - bwSqlOnOff - bwAfcOnOff, - Y, - bwAfcOnOff, Hstatus, "AFC"); - btnSQL = new Fl_Check_Button( - progStatus.mainW - bwSqlOnOff, - Y, - sql_width, Hstatus, "SQL"); - } else { - btnAFC = new Fl_Light_Button( - progStatus.mainW - bwSqlOnOff - bwAfcOnOff, - Y, - bwAfcOnOff, Hstatus, "AFC"); - btnAFC->selection_color(progdefaults.AfcColor); - btnSQL = new Fl_Light_Button( - progStatus.mainW - bwSqlOnOff, - Y, - sql_width, Hstatus, "SQL"); - btnSQL->selection_color(progdefaults.Sql1Color); - } + btnAFC = new Fl_Light_Button( + progStatus.mainW - bwSqlOnOff - bwAfcOnOff, + Y, + bwAfcOnOff, Hstatus, "AFC"); + btnAFC->selection_color(progdefaults.AfcColor); + btnSQL = new Fl_Light_Button( + progStatus.mainW - bwSqlOnOff, + Y, + sql_width, Hstatus, "SQL"); + btnSQL->selection_color(progdefaults.Sql1Color); btnAFC->callback(cbAFC, 0); btnAFC->value(1); btnAFC->tooltip(_("Automatic Frequency Control")); @@ -5282,6 +5433,11 @@ void resetOLIVIA() { trx_start_modem(active_modem); } +void resetCONTESTIA() { + if (active_modem->get_mode() == MODE_CONTESTIA) + trx_start_modem(active_modem); +} + void resetTHOR() { trx_mode md = active_modem->get_mode(); if (md == MODE_THOR4 || md == MODE_THOR5 || md == MODE_THOR8 || @@ -5423,6 +5579,8 @@ void setReverse(int rev) { void start_tx() { + if (!(active_modem->get_cap() & modem::CAP_TX)) + return; trx_transmit(); REQ(&waterfall::set_XmtRcvBtn, wf, true); } @@ -5566,6 +5724,7 @@ void spot_selection_color() btnAutoSpot->redraw(); } +// Olivia void set_olivia_bw(int bw) { int i; @@ -5596,6 +5755,38 @@ void set_olivia_tones(int tones) progdefaults.changed = changed; } +//Contestia +void set_contestia_bw(int bw) +{ + int i; + if (bw == 125) + i = 0; + else if (bw == 250) + i = 1; + else if (bw == 500) + i = 2; + else if (bw == 1000) + i = 3; + else + i = 4; + bool changed = progdefaults.changed; + mnuContestia_Bandwidth->value(i); + mnuContestia_Bandwidth->do_callback(); + progdefaults.changed = changed; +} + +void set_contestia_tones(int tones) +{ + unsigned i = 0; + while (tones >>= 1) + i++; + bool changed = progdefaults.changed; + mnuContestia_Tones->value(i - 1); + mnuContestia_Tones->do_callback(); + progdefaults.changed = changed; +} + + void set_rtty_shift(int shift) { if (shift < selCustomShift->minimum() || shift > selCustomShift->maximum()) diff --git a/src/dialogs/font_browser.cxx b/src/dialogs/font_browser.cxx index 201dca5b..f5140543 100644 --- a/src/dialogs/font_browser.cxx +++ b/src/dialogs/font_browser.cxx @@ -71,7 +71,7 @@ void Font_Browser::fb_callback(Fl_Widget* w, void* arg) else { if (w == fb->lst_Size) fb->txt_Size->value(strtol(fb->lst_Size->text(fb->lst_Size->value()), NULL, 10)); - fb->fontsize = fb->txt_Size->value(); + fb->fontsize = static_cast(fb->txt_Size->value()); } fb->box_Example->SetFont(fb->fontnbr, fb->fontsize, fb->fontcolor); } diff --git a/src/dialogs/guide.cxx b/src/dialogs/guide.cxx index 48f5450b..e82d71fe 100644 --- a/src/dialogs/guide.cxx +++ b/src/dialogs/guide.cxx @@ -1676,7 +1676,7 @@ numeric keypad's 0-9 the sequence will be discarded.

\n\
\n\
\n\ Version 3.11
\n\ -Last updated 2009-10-17 07:11:38 CDT\n\ +Last updated 2010-05-02 05:49:46 CDT\n\
\n\
\n\ \n\ diff --git a/src/dialogs/notifydialog.cxx b/src/dialogs/notifydialog.cxx index a535f0ce..8b4096fe 100644 --- a/src/dialogs/notifydialog.cxx +++ b/src/dialogs/notifydialog.cxx @@ -170,7 +170,7 @@ Fl_Double_Window* make_notify_window() { { mnuNotifyDupWhich = new Fl_Choice(33, 367, 120, 20, _("in:")); mnuNotifyDupWhich->down_box(FL_BORDER_BOX); } // Fl_Choice* mnuNotifyDupWhich - { cntNotifyDupTime = new Fl_Spinner2(77, 397, 60, 20, _("Time (s):")); + { Fl_Spinner2* o = cntNotifyDupTime = new Fl_Spinner2(77, 397, 60, 20, _("Time (s):")); cntNotifyDupTime->box(FL_NO_BOX); cntNotifyDupTime->color((Fl_Color)FL_BACKGROUND_COLOR); cntNotifyDupTime->selection_color((Fl_Color)FL_BACKGROUND_COLOR); @@ -183,6 +183,7 @@ Fl_Double_Window* make_notify_window() { cntNotifyDupTime->value(600); cntNotifyDupTime->align(FL_ALIGN_LEFT); cntNotifyDupTime->when(FL_WHEN_RELEASE); + o->labelsize(FL_NORMAL_SIZE); } // Fl_Spinner2* cntNotifyDupTime { chkNotifyDupBand = new Fl_Check_Button(12, 427, 65, 20, _("Band")); chkNotifyDupBand->down_box(FL_DOWN_BOX); @@ -195,7 +196,7 @@ Fl_Double_Window* make_notify_window() { { grpNotifyAction = new Fl_Group(222, 2, 276, 394, _("Action")); grpNotifyAction->box(FL_ENGRAVED_FRAME); grpNotifyAction->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE); - { cntNotifyActionLimit = new Fl_Spinner2(343, 31, 52, 20, _("Trigger limit (s):")); + { Fl_Spinner2* o = cntNotifyActionLimit = new Fl_Spinner2(343, 31, 52, 20, _("Trigger limit (s):")); cntNotifyActionLimit->tooltip(_("Minimum time between events")); cntNotifyActionLimit->box(FL_NO_BOX); cntNotifyActionLimit->color((Fl_Color)FL_BACKGROUND_COLOR); @@ -208,6 +209,7 @@ Fl_Double_Window* make_notify_window() { cntNotifyActionLimit->maximum(3600); cntNotifyActionLimit->align(FL_ALIGN_LEFT); cntNotifyActionLimit->when(FL_WHEN_RELEASE); + o->labelsize(FL_NORMAL_SIZE); } // Fl_Spinner2* cntNotifyActionLimit { Fl_Input2* o = inpNotifyActionDialog = new Fl_Input2(232, 78, 218, 60, _("Show alert window:")); inpNotifyActionDialog->type(4); @@ -225,7 +227,7 @@ Fl_Double_Window* make_notify_window() { { btnNotifyActionDialogDefault = new Fl_Button(460, 96, 28, 23); btnNotifyActionDialogDefault->tooltip(_("Insert default text")); } // Fl_Button* btnNotifyActionDialogDefault - { cntNotifyActionDialogTimeout = new Fl_Spinner2(383, 148, 52, 20, _("Hide window after (s):")); + { Fl_Spinner2* o = cntNotifyActionDialogTimeout = new Fl_Spinner2(383, 148, 52, 20, _("Hide window after (s):")); cntNotifyActionDialogTimeout->box(FL_NO_BOX); cntNotifyActionDialogTimeout->color((Fl_Color)FL_BACKGROUND_COLOR); cntNotifyActionDialogTimeout->selection_color((Fl_Color)FL_BACKGROUND_COLOR); @@ -238,6 +240,7 @@ Fl_Double_Window* make_notify_window() { cntNotifyActionDialogTimeout->value(5); cntNotifyActionDialogTimeout->align(FL_ALIGN_LEFT); cntNotifyActionDialogTimeout->when(FL_WHEN_RELEASE); + o->labelsize(FL_NORMAL_SIZE); } // Fl_Spinner2* cntNotifyActionDialogTimeout { Fl_Input2* o = inpNotifyActionRXMarker = new Fl_Input2(232, 195, 218, 60, _("Append to RX text:")); inpNotifyActionRXMarker->type(4); diff --git a/src/dialogs/notifydialog.fl b/src/dialogs/notifydialog.fl index 484f91bc..849ba83a 100644 --- a/src/dialogs/notifydialog.fl +++ b/src/dialogs/notifydialog.fl @@ -103,6 +103,7 @@ chkNotifyFilterCall->value(0);} Fl_Spinner cntNotifyDupTime { label {Time (s):} xywh {77 397 60 20} minimum 0 maximum 97200 value 600 + code0 {o->labelsize(FL_NORMAL_SIZE);} class Fl_Spinner2 } Fl_Check_Button chkNotifyDupBand { @@ -121,6 +122,7 @@ chkNotifyFilterCall->value(0);} Fl_Spinner cntNotifyActionLimit { label {Trigger limit (s):} tooltip {Minimum time between events} xywh {343 31 52 20} minimum 0 maximum 3600 value 1 + code0 {o->labelsize(FL_NORMAL_SIZE);} class Fl_Spinner2 } Fl_Input inpNotifyActionDialog { @@ -135,6 +137,7 @@ chkNotifyFilterCall->value(0);} Fl_Spinner cntNotifyActionDialogTimeout { label {Hide window after (s):} xywh {383 148 52 20} minimum 0 maximum 3600 value 5 + code0 {o->labelsize(FL_NORMAL_SIZE);} class Fl_Spinner2 } Fl_Input inpNotifyActionRXMarker { diff --git a/src/dominoex/dominoex.cxx b/src/dominoex/dominoex.cxx index 39b9bee1..232a140d 100644 --- a/src/dominoex/dominoex.cxx +++ b/src/dominoex/dominoex.cxx @@ -204,7 +204,7 @@ dominoex::~dominoex() dominoex::dominoex(trx_mode md) { - cap = CAP_REV; + cap |= CAP_REV; mode = md; diff --git a/src/feld/feld.cxx b/src/feld/feld.cxx index cd42fcac..7eb2338e 100644 --- a/src/feld/feld.cxx +++ b/src/feld/feld.cxx @@ -103,7 +103,7 @@ feld::feld(trx_mode m) mode = m; samplerate = FeldSampleRate; - cap = CAP_BW; + cap |= CAP_BW; switch (mode) { // Amplitude modulation modes @@ -153,7 +153,7 @@ feld::feld(trx_mode m) hell_bandwidth = 122.5; phi2freq = samplerate / M_PI / (hell_bandwidth / 2.0); filter_bandwidth = progdefaults.HELL_BW_FSK; - cap = CAP_REV; + cap |= CAP_REV; break; case MODE_FSKH105: feldcolumnrate = 17.5; @@ -164,7 +164,7 @@ feld::feld(trx_mode m) hell_bandwidth = 55; phi2freq = samplerate / M_PI / (hell_bandwidth / 2.0); filter_bandwidth = progdefaults.HELL_BW_FSK105; - cap = CAP_REV; + cap |= CAP_REV; break; case MODE_HELL80: feldcolumnrate = 35; @@ -175,7 +175,7 @@ feld::feld(trx_mode m) hell_bandwidth = 300; phi2freq = samplerate / M_PI / (hell_bandwidth / 2.0); filter_bandwidth = progdefaults.HELL_BW_HELL80; - cap = CAP_REV; + cap |= CAP_REV; break; default : feldcolumnrate = 17.5; @@ -194,7 +194,7 @@ feld::feld(trx_mode m) bpfilt = new fftfilt(0, lp, 1024); bbfilt = new Cmovavg(8); - average = new Cmovavg( 500 / downsampleinc); + average = new Cmovavg( static_cast(500 / downsampleinc)); minmaxfilt = new Cmovavg(120); diff --git a/src/globals/globals.cxx b/src/globals/globals.cxx index a952ee93..788ee27e 100644 --- a/src/globals/globals.cxx +++ b/src/globals/globals.cxx @@ -42,6 +42,8 @@ using namespace std; const struct mode_info_t mode_info[NUM_MODES] = { { MODE_CW, &cw_modem, "CW", "CW", "CW", "CW" }, + { MODE_CONTESTIA, &contestia_modem, "CTSTIA", "Contestia", "", "CONTESTIA" }, + { MODE_DOMINOEX4, &dominoex4_modem, "DomEX4", "DominoEX 4", "DOMINOEX4", "DOMINO" }, { MODE_DOMINOEX5, &dominoex5_modem, "DomEX5", "DominoEX 5", "DOMINOEX5", "DOMINO" }, { MODE_DOMINOEX8, &dominoex8_modem, "DomEX8", "DominoEX 8", "DOMINOEX8", "DOMINO" }, @@ -106,8 +108,8 @@ const struct mode_info_t mode_info[NUM_MODES] = { { MODE_THROBX2, &throbx2_modem, "THRBX2", "ThrobX 2", "", "THRBX" }, { MODE_THROBX4, &throbx4_modem, "THRBX4", "ThrobX 4", "", "THRBX" }, + { MODE_SSB, &ssb_modem, "SSB", "SSB", "", "SSB" }, { MODE_WWV, &wwv_modem, "WWV", "WWV", "", "" }, - { MODE_ANALYSIS, &anal_modem, "ANALYSIS", "Freq Analysis", "", "" } }; diff --git a/src/include/FTextRXTX.h b/src/include/FTextRXTX.h index 7382ab4b..96d2a49b 100644 --- a/src/include/FTextRXTX.h +++ b/src/include/FTextRXTX.h @@ -56,7 +56,7 @@ protected: enum { RX_MENU_QRZ_THIS, RX_MENU_CALL, RX_MENU_NAME, RX_MENU_QTH, RX_MENU_STATE, RX_MENU_PROVINCE,RX_MENU_COUNTRY, RX_MENU_LOC, - RX_MENU_RST_IN, RX_MENU_SERIAL, RX_MENU_XCHG, + RX_MENU_RST_IN, RX_MENU_XCHG, RX_MENU_SERIAL, RX_MENU_DIV, RX_MENU_COPY, RX_MENU_CLEAR, RX_MENU_SELECT_ALL, RX_MENU_SAVE, RX_MENU_WRAP, RX_MENU_QUICK_ENTRY, RX_MENU_SCROLL_HINTS }; diff --git a/src/include/FTextView.h b/src/include/FTextView.h index 152801b3..0d30c215 100644 --- a/src/include/FTextView.h +++ b/src/include/FTextView.h @@ -92,8 +92,8 @@ private: protected: enum { FTEXT_DEF = 'A' }; enum set_style_op_e { SET_FONT = 1 << 0, SET_SIZE = 1 << 1, SET_COLOR = 1 << 2 }; - Fl_Text_Buffer *tbuf; ///< text buffer - Fl_Text_Buffer *sbuf; ///< style buffer + Fl_Text_Buffer_mod *tbuf; ///< text buffer + Fl_Text_Buffer_mod *sbuf; ///< style buffer Fl_Text_Display_mod::Style_Table_Entry styles[NATTR]; Fl_Menu_Item *context_menu; int popx, popy; diff --git a/src/include/Fl_Text_Buffer_mod.H b/src/include/Fl_Text_Buffer_mod.H new file mode 100644 index 00000000..58d03bd7 --- /dev/null +++ b/src/include/Fl_Text_Buffer_mod.H @@ -0,0 +1,7 @@ +#include + +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# include "Fl_Text_Buffer_mod_1_1.H" +#elif FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +# include "Fl_Text_Buffer_mod_1_3.H" +#endif diff --git a/src/include/Fl_Text_Buffer_mod_1_1.H b/src/include/Fl_Text_Buffer_mod_1_1.H new file mode 100644 index 00000000..61b900d9 --- /dev/null +++ b/src/include/Fl_Text_Buffer_mod_1_1.H @@ -0,0 +1,263 @@ +// +// "$Id: Fl_Text_Buffer.H 6010 2008-01-04 20:31:52Z matt $" +// +// Header file for Fl_Text_Buffer class. +// +// Copyright 2001-2005 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#ifndef FL_TEXT_BUFFER_H +#define FL_TEXT_BUFFER_H + +/* Maximum length in characters of a tab or control character expansion + of a single buffer character */ +#define FL_TEXT_MAX_EXP_CHAR_LEN 20 + +#include + +class FL_EXPORT Fl_Text_Selection { + friend class Fl_Text_Buffer_mod; + + public: + void set(int start, int end); + void set_rectangular(int start, int end, int rectStart, int rectEnd); + void update(int pos, int nDeleted, int nInserted); + char rectangular() { return mRectangular; } + int start() { return mStart; } + int end() { return mEnd; } + int rect_start() { return mRectStart; } + int rect_end() { return mRectEnd; } + char selected() { return mSelected; } + void selected(char b) { mSelected = b; } + int includes(int pos, int lineStartPos, int dispIndex); + int position(int* start, int* end); + int position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd); + + + protected: + char mSelected; + char mRectangular; + int mStart; + int mEnd; + int mRectStart; + int mRectEnd; +}; + +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); + +class FL_EXPORT Fl_Text_Buffer_mod { + public: + Fl_Text_Buffer_mod(int requestedSize = 0); + ~Fl_Text_Buffer_mod(); + + int length() { return mLength; } + char* text(); + void text(const char* text); + char* text_range(int start, int end); + char character(int pos); + char* text_in_rectangle(int start, int end, int rectStart, int rectEnd); + void insert(int pos, const char* text); + void append(const char* t) { insert(length(), t); } + void remove(int start, int end); + void replace(int start, int end, const char *text); + void copy(Fl_Text_Buffer_mod* fromBuf, int fromStart, int fromEnd, int toPos); + int undo(int *cp=0); + void canUndo(char flag=1); + int insertfile(const char *file, int pos, int buflen = 128*1024); + int appendfile(const char *file, int buflen = 128*1024) + { return insertfile(file, length(), buflen); } + int loadfile(const char *file, int buflen = 128*1024) + { select(0, length()); remove_selection(); return appendfile(file, buflen); } + int outputfile(const char *file, int start, int end, int buflen = 128*1024); + int savefile(const char *file, int buflen = 128*1024) + { return outputfile(file, 0, length(), buflen); } + + void insert_column(int column, int startPos, const char* text, + int* charsInserted, int* charsDeleted); + + void replace_rectangular(int start, int end, int rectStart, int rectEnd, + const char* text); + + void overlay_rectangular(int startPos, int rectStart, int rectEnd, + const char* text, int* charsInserted, + int* charsDeleted); + + void remove_rectangular(int start, int end, int rectStart, int rectEnd); + void clear_rectangular(int start, int end, int rectStart, int rectEnd); + int tab_distance() { return mTabDist; } + void tab_distance(int tabDist); + void select(int start, int end); + int selected() { return mPrimary.selected(); } + void unselect(); + void select_rectangular(int start, int end, int rectStart, int rectEnd); + int selection_position(int* start, int* end); + + int selection_position(int* start, int* end, int* isRect, int* rectStart, + int* rectEnd); + + char* selection_text(); + void remove_selection(); + void replace_selection(const char* text); + void secondary_select(int start, int end); + int secondary_selected() { return mSecondary.selected(); } + void secondary_unselect(); + + void secondary_select_rectangular(int start, int end, int rectStart, + int rectEnd); + + int secondary_selection_position(int* start, int* end); + int secondary_selection_position(int* start, int* end, int* isRect, + int* rectStart, int* rectEnd); + + char* secondary_selection_text(); + void remove_secondary_selection(); + void replace_secondary_selection(const char* text); + void highlight(int start, int end); + int highlight() { return mHighlight.selected(); } + void unhighlight(); + void highlight_rectangular(int start, int end, int rectStart, int rectEnd); + + int highlight_position(int* start, int* end); + int highlight_position(int* start, int* end, int* isRect, int* rectStart, + int* rectEnd); + + char* highlight_text(); + void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg); + void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg); + + void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); } + + void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg); + void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg); + + void call_predelete_callbacks() { call_predelete_callbacks(0, 0); } + + char* line_text(int pos); + int line_start(int pos); + int line_end(int pos); + int word_start(int pos); + int word_end(int pos); + int expand_character(int pos, int indent, char *outStr); + + static int expand_character(char c, int indent, char* outStr, int tabDist, + char nullSubsChar); + + static int character_width(char c, int indent, int tabDist, char nullSubsChar); + int count_displayed_characters(int lineStartPos, int targetPos); + int skip_displayed_characters(int lineStartPos, int nChars); + int count_lines(int startPos, int endPos); + int skip_lines(int startPos, int nLines); + int rewind_lines(int startPos, int nLines); + int findchar_forward(int startPos, char searchChar, int* foundPos); + int findchar_backward(int startPos, char searchChar, int* foundPos); + int findchars_forward(int startPos, const char* searchChars, int* foundPos); + int findchars_backward(int startPos, const char* searchChars, int* foundPos); + + int search_forward(int startPos, const char* searchString, int* foundPos, + int matchCase = 0); + + int search_backward(int startPos, const char* searchString, int* foundPos, + int matchCase = 0); + + int substitute_null_characters(char* string, int length); + void unsubstitute_null_characters(char* string); + char null_substitution_character() { return mNullSubsChar; } + Fl_Text_Selection* primary_selection() { return &mPrimary; } + Fl_Text_Selection* secondary_selection() { return &mSecondary; } + Fl_Text_Selection* highlight_selection() { return &mHighlight; } + + protected: + void call_modify_callbacks(int pos, int nDeleted, int nInserted, + int nRestyled, const char* deletedText); + void call_predelete_callbacks(int pos, int nDeleted); + + int insert_(int pos, const char* text); + void remove_(int start, int end); + + void remove_rectangular_(int start, int end, int rectStart, int rectEnd, + int* replaceLen, int* endPos); + + void insert_column_(int column, int startPos, const char* insText, + int* nDeleted, int* nInserted, int* endPos); + + void overlay_rectangular_(int startPos, int rectStart, int rectEnd, + const char* insText, int* nDeleted, + int* nInserted, int* endPos); + + void redisplay_selection(Fl_Text_Selection* oldSelection, + Fl_Text_Selection* newSelection); + + void move_gap(int pos); + void reallocate_with_gap(int newGapStart, int newGapLen); + char* selection_text_(Fl_Text_Selection* sel); + void remove_selection_(Fl_Text_Selection* sel); + void replace_selection_(Fl_Text_Selection* sel, const char* text); + + void rectangular_selection_boundaries(int lineStartPos, int rectStart, + int rectEnd, int* selStart, + int* selEnd); + + void update_selections(int pos, int nDeleted, int nInserted); + + Fl_Text_Selection mPrimary; /* highlighted areas */ + Fl_Text_Selection mSecondary; + Fl_Text_Selection mHighlight; + int mLength; /* length of the text in the buffer (the length + of the buffer itself must be calculated: + gapEnd - gapStart + length) */ + char* mBuf; /* allocated memory where the text is stored */ + int mGapStart; /* points to the first character of the gap */ + int mGapEnd; /* points to the first char after the gap */ + // 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 */ + mNodifyProcs; /* 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. */ + void **mPredeleteCbArgs; /* caller argument for pre-delete proc above */ + int mCursorPosHint; /* hint for reasonable cursor position after + a buffer modification operation */ + char mNullSubsChar; /* NEdit is based on C null-terminated strings, + so ascii-nul characters must be substituted + with something else. This is the else, but + of course, things get quite messy when you + use it */ + char mCanUndo; /* if this buffer is used for attributes, it must + not do any undo calls */ +}; + +#endif + +// +// End of "$Id: Fl_Text_Buffer.H 6010 2008-01-04 20:31:52Z matt $". +// diff --git a/src/include/Fl_Text_Buffer_mod_1_3.H b/src/include/Fl_Text_Buffer_mod_1_3.H new file mode 100644 index 00000000..98067ff6 --- /dev/null +++ b/src/include/Fl_Text_Buffer_mod_1_3.H @@ -0,0 +1,984 @@ +// +// "$Id: Fl_Text_Buffer.H 7462 2010-04-06 23:00:56Z matt $" +// +// Header file for Fl_Text_Buffer class. +// +// Copyright 2001-2009 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +/* \file + Fl_Text_Buffer, Fl_Text_Selection widget . */ + +#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 + an average character width (which is a bearable approximation). + */ + + +/* Maximum length in characters of a tab or control character expansion + of a single buffer character */ +#define FL_TEXT_MAX_EXP_CHAR_LEN 20 + +#include + + +/** + \class Fl_Text_Selection + \brief This is an internal class for Fl_Text_Buffer 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 { + friend class Fl_Text_Buffer_mod; + +public: + + /** + \brief Set the selection range. + \param start byte offset to first selected character + \param end byte offset pointing after last selected character + */ + 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 + \param pos byte offset into text buffer at which the change occured + \param nDeleted number of bytes deleted from the buffer + \param nInserted number of bytes inserted into the buffer + */ + 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 + */ + int start() const { return mStart; } + + /** + \brief Return the byte ofsset to the character after the last selected character. + \return byte offset + */ + 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; } + + /** + \brief Modify the 'selected' flag. + \param b new flag + */ + void selected(char b) { mSelected = b; } + + /** + Return true if position \p pos with indentation \p dispIndex is in + the Fl_Text_Selection. + */ + int includes(int pos, int lineStartPos, int dispIndex) const; + + /** + \brief Return the positions of this selection. + \param start retrun byte offset to first selected character + \param end retrun byte offset pointing after last selected character + \return true if selected + */ + 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") +}; + +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. + + 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 + excellent NEdit text editor engine - see http://www.nedit.org/. + \todo unicode check + */ +class FL_EXPORT Fl_Text_Buffer_mod { +public: + + /** + Create an empty text buffer of a pre-determined size. + \param requestedSize use this to avoid unnecessary re-allocation + if you know exactly how much the buffer will need to hold + \param preferredGapSize Initial size for the buffer gap (empty space + in the buffer where text might be inserted + if the user is typing sequential chars) + */ + Fl_Text_Buffer_mod(int requestedSize = 0, int preferredGapSize = 1024); + + /** + Frees a text buffer + */ + ~Fl_Text_Buffer_mod(); + + /** + \brief Returns the number of bytes in the buffer. + \return size of text in bytes + */ + int length() const { return mLength; } + + /** + \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 + */ + char* text() const; + + /** + Replaces the entire contents of the text buffer + \todo unicode check + */ + void text(const char* text); + + /** + \brief Get a copy of a part of the text buffer. + Return a copy of the text between \p start and \p end character positions + from text buffer \p buf. Positions start at 0, and the range does not + include the character pointed to by \p end. + 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 + */ + 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 + \return Unicode UCS-4 encoded character + */ + unsigned int character(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 + */ + char* text_in_rectangle(int start, int end, int rectStart, int rectEnd) const; + + /** + Inserts null-terminated string \p text at position \p pos. + \param pos insertion position as byte offset (must be utf-8 character aligned) + \param text utf-8 encoded and nul terminated text + */ + void insert(int pos, const char* text); + + /** + Appends the text string to the end of the buffer. + \todo unicode check + */ + void append(const char* t) { insert(length(), t); } + + /** + Deletes a range of characters in the buffer. + \param start byte offset to first character to be removed + \param end byte offset to charcatre after last character to be removed + */ + void remove(int start, int end); + + /** + Deletes the characters between \p start and \p end, and inserts the null-terminated string \p text in their place in the buffer. + \param start byte offset to first character to be removed and new insert position + \param end byte offset to charcatre after last character to be removed + \param text utf-8 encoded and nul terminated text + */ + 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 + */ + 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); + + /** + Lets the undo system know if we can undo changes + */ + void canUndo(char flag=1); + + /** + Inserts a file at the specified position. 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 + */ + 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 + */ + int appendfile(const char *file, int buflen = 128*1024) + { return insertfile(file, length(), buflen); } + + /** + Loads a text file into the buffer + \todo unicode check + */ + int loadfile(const char *file, int buflen = 128*1024) + { select(0, length()); remove_selection(); return appendfile(file, buflen); } + + /** + Writes the specified portions of the file to a file. Returns 0 on success, non-zero + 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); + + /** + Returns a non 0 value if text has been selected, 0 otherwise + */ + int selected() const { return mPrimary.selected(); } + + /** + 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); + + /** + Returns a non 0 value if text has been selected in the secondary + text selection, 0 otherwise + */ + int secondary_selected() { return mSecondary.selected(); } + + /** + 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); + + /** + Returns the highlighted text. When you are done with the + text, free it using the free() function. + */ + int highlight() { return mHighlight.selected(); } + + /** + 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(); + + /** + Adds a callback function that is called whenever the text buffer is + modified. The callback function is declared as follows: + + \code + typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted, + 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); + + /** + 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); } + + /** + 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 + */ + char* line_text(int pos) const; + + /** + Returns the position of the start of the line containing position \p pos. + \todo unicode check + */ + int line_start(int pos) const; + + /** + 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 + */ + int line_end(int pos) const; + + /** + Returns the position corresponding to the start of the word + \todo unicode check + */ + int word_start(int pos) const; + + /** + Returns the position corresponding to the end of the word. + \todo unicode check + */ + 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; + + /** + Count forward from buffer position \p startPos in displayed characters + (displayed characters are the characters shown on the screen to represent + characters in the buffer, where tabs and control characters are expanded) + \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); + + /** + 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); + + /** + Finds the next occurrence of the specified character. + Search forwards in buffer for character \p searchChar, starting + with the character \p startPos, and returning the result in \p foundPos + returns 1 if found, 0 if not. (The difference between this and + 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 + */ + int findchar_forward(int startPos, char searchChar, int* foundPos) const; + + /** + Search backwards in buffer \p buf for character \p searchChar, starting + with the character BEFORE \p startPos, returning the result in \p foundPos + returns 1 if found, 0 if not. (The difference between this and + 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 + */ + 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; + + /** + 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 + */ + int search_forward(int startPos, const char* searchString, int* foundPos, + int matchCase = 0) const; + + /** + 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 + */ + int search_backward(int startPos, const char* searchString, int* foundPos, + int matchCase = 0) const; + + /** + Returns the primary selection. + */ + const Fl_Text_Selection* primary_selection() const { return &mPrimary; } + + /** + Returns the primary selection. + */ + Fl_Text_Selection* primary_selection() { return &mPrimary; } + + /** + Returns the secondary selection. + */ + const Fl_Text_Selection* secondary_selection() const { return &mSecondary; } + + /** + Returns the current highlight selection. + */ + const Fl_Text_Selection* highlight_selection() const { return &mHighlight; } + +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; + + /** + 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; + + /** + Internal (non-redisplaying) version of BufInsert. Returns the length of + text inserted (this is just strlen(\p text), however this calculation can be + 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 + */ + int insert_(int pos, const char* text); + + /** + 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; + + /** + \todo unicode check + */ + 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; + + /** + Removes the text from the buffer corresponding to \p sel. + \todo unicode check + */ + void remove_selection_(Fl_Text_Selection* 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; + + /** + 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 */ + int mLength; /**< length of the text in the buffer (the length + of the buffer itself must be calculated: + gapEnd - gapStart + length) */ + char* mBuf; /**< allocated memory where the text is stored */ + int mGapStart; /**< points to the first character of the gap */ + int mGapEnd; /**< points to the first char after the gap */ + // 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 */ + 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. */ + void **mPredeleteCbArgs; /**< caller argument for pre-delete proc above */ + int mCursorPosHint; /**< hint for reasonable cursor position after + a buffer modification operation */ + char mCanUndo; /**< if this buffer is used for attributes, it must + not do any undo calls */ + int mPreferredGapSize; /**< the default allocation for the text gap is 1024 + bytes and should only be increased if frequent + and large changes in buffer size are expected */ +}; + +#endif + +// +// End of "$Id: Fl_Text_Buffer.H 7462 2010-04-06 23:00:56Z matt $". +// diff --git a/src/include/Fl_Text_Display_mod.H b/src/include/Fl_Text_Display_mod.H index 1f3fa8cb..4e9f7b33 100644 --- a/src/include/Fl_Text_Display_mod.H +++ b/src/include/Fl_Text_Display_mod.H @@ -1,304 +1,7 @@ -// -// "$Id: Fl_Text_Display.H 4502 2005-08-10 23:11:51Z matt $" -// -// Header file for Fl_Text_Display class. -// -// Copyright 2001-2005 by Bill Spitzak and others. -// Original code Copyright Mark Edel. Permission to distribute under -// the LGPL for the FLTK library granted by Mark Edel. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library 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 -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -// USA. -// -// Please report all bugs and problems on the following page: -// -// http://www.fltk.org/str.php -// - -#ifndef FL_TEXT_DISPLAY_MOD_H -#define FL_TEXT_DISPLAY_MOD_H - -#include -#include -#include -#include -#include - -class Fl_Text_Display_mod: public Fl_Group { - public: - 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 - }; - - 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* buf); - void buffer(Fl_Text_Buffer& buf) { buffer(&buf); } - Fl_Text_Buffer* buffer() { 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); - int insert_position() { return mCursorPos; } - int in_selection(int x, int y); - 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); - int line_start(int pos); - int line_end(int pos, bool start_pos_is_line_start); - 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); - void hide_cursor() { show_cursor(0); } - void cursor_style(int style); - Fl_Color cursor_color() const {return mCursor_color;} - void cursor_color(Fl_Color n) {mCursor_color = n;} - int scrollbar_width() { return scrollbar_width_; } - Fl_Align scrollbar_align() { return scrollbar_align_; } - void scrollbar_width(int W) { scrollbar_width_ = W; } - void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; } - int word_start(int pos) { return buffer()->word_start(pos); } - int word_end(int pos) { return buffer()->word_end(pos); } - - - void highlight_data(Fl_Text_Buffer *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, - int dispIndex); - - Fl_Font textfont() const {return textfont_;} - void textfont(Fl_Font f) {textfont_ = f;} - uchar textsize() const {return textsize_;} - void textsize(uchar s) {textsize_ = s;} - Fl_Color textcolor() const {return (Fl_Color)textcolor_;} - void textcolor(unsigned n) {textcolor_ = n;} - - int wrapped_column(int row, int column); - int wrapped_row(int row); - 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( int pos, int* lineNum ); - int string_width(const char* string, int length, int style); - - 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); - int longest_vline(); - int empty_vlines(); - int vline_length(int visLineNum); - int xy_to_position(int x, int y, int PosType = CHARACTER_POS); - - void xy_to_rowcol(int x, int y, int* row, int* column, - int PosType = CHARACTER_POS); - - int position_to_xy(int pos, int* x, int* y); - void maintain_absolute_top_line_number(int state); - int get_absolute_top_line_number(); - void absolute_top_line_number(int oldFirstChar); - int maintaining_absolute_top_line_number(); - void reset_absolute_top_line_number(); - int position_to_linecol(int pos, int* lineNum, int* column); - 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 *buf, int startPos, int maxPos, - int maxLines, bool startPosIsLineStart, - int styleBufOffset, int *retPos, int *retLines, - int *retLineStart, int *retLineEnd, - bool countLastLineMissingNewLine = true); - void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd, - int *nextLineStart); - int measure_proportional_character(char c, int colNum, int pos); - int wrap_uses_character(int lineEndPos); - int range_touches_selection(Fl_Text_Selection *sel, int rangeStart, - int rangeEnd); - - 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* mBuffer; /* Contains text to be displayed */ - Fl_Text_Buffer* 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; - - Fl_Font textfont_; - uchar textsize_; - unsigned 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 */ -}; +#include +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# include "Fl_Text_Display_mod_1_1.H" +#elif FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +# include "Fl_Text_Display_mod_1_3.H" #endif - -// -// End of "$Id: Fl_Text_Display.H 4502 2005-08-10 23:11:51Z matt $". -// diff --git a/src/include/Fl_Text_Display_mod_1_1.H b/src/include/Fl_Text_Display_mod_1_1.H new file mode 100644 index 00000000..8e151657 --- /dev/null +++ b/src/include/Fl_Text_Display_mod_1_1.H @@ -0,0 +1,304 @@ +// +// "$Id: Fl_Text_Display.H 4502 2005-08-10 23:11:51Z matt $" +// +// Header file for Fl_Text_Display class. +// +// Copyright 2001-2005 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#ifndef FL_TEXT_DISPLAY_MOD_H +#define FL_TEXT_DISPLAY_MOD_H + +#include +#include +#include +#include +#include "Fl_Text_Buffer_mod.H" + +class Fl_Text_Display_mod: public Fl_Group { + public: + 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 + }; + + 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); + void buffer(Fl_Text_Buffer_mod& buf) { buffer(&buf); } + Fl_Text_Buffer_mod* buffer() { 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); + int insert_position() { return mCursorPos; } + int in_selection(int x, int y); + 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); + int line_start(int pos); + int line_end(int pos, bool start_pos_is_line_start); + 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); + void hide_cursor() { show_cursor(0); } + void cursor_style(int style); + Fl_Color cursor_color() const {return mCursor_color;} + void cursor_color(Fl_Color n) {mCursor_color = n;} + int scrollbar_width() { return scrollbar_width_; } + Fl_Align scrollbar_align() { return scrollbar_align_; } + void scrollbar_width(int W) { scrollbar_width_ = W; } + void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; } + int word_start(int pos) { return buffer()->word_start(pos); } + int word_end(int pos) { 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, + int dispIndex); + + Fl_Font textfont() const {return textfont_;} + void textfont(Fl_Font f) {textfont_ = f;} + uchar textsize() const {return textsize_;} + void textsize(uchar s) {textsize_ = s;} + Fl_Color textcolor() const {return (Fl_Color)textcolor_;} + void textcolor(unsigned n) {textcolor_ = n;} + + int wrapped_column(int row, int column); + int wrapped_row(int row); + 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( int pos, int* lineNum ); + int string_width(const char* string, int length, int style); + + 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); + int longest_vline(); + int empty_vlines(); + int vline_length(int visLineNum); + int xy_to_position(int x, int y, int PosType = CHARACTER_POS); + + void xy_to_rowcol(int x, int y, int* row, int* column, + int PosType = CHARACTER_POS); + + int position_to_xy(int pos, int* x, int* y); + void maintain_absolute_top_line_number(int state); + int get_absolute_top_line_number(); + void absolute_top_line_number(int oldFirstChar); + int maintaining_absolute_top_line_number(); + void reset_absolute_top_line_number(); + int position_to_linecol(int pos, int* lineNum, int* column); + 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); + void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd, + int *nextLineStart); + int measure_proportional_character(char c, int colNum, int pos); + int wrap_uses_character(int lineEndPos); + int range_touches_selection(Fl_Text_Selection *sel, int rangeStart, + int rangeEnd); + + 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; + + Fl_Font textfont_; + uchar textsize_; + unsigned 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 + +// +// End of "$Id: Fl_Text_Display.H 4502 2005-08-10 23:11:51Z matt $". +// diff --git a/src/include/Fl_Text_Display_mod_1_3.H b/src/include/Fl_Text_Display_mod_1_3.H new file mode 100644 index 00000000..aab22a25 --- /dev/null +++ b/src/include/Fl_Text_Display_mod_1_3.H @@ -0,0 +1,350 @@ +// +// "$Id: Fl_Text_Display.H 6902 2009-09-27 11:06:56Z matt $" +// +// Header file for Fl_Text_Display class. +// +// Copyright 2001-2009 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +/* \file + Fl_Text_Display widget . */ + +#ifndef FL_TEXT_DISPLAY_H +#define FL_TEXT_DISPLAY_H + +#include +#include +#include +#include +#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. +*/ +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); + /** + 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); } + + + 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, + 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;} + + /** 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( int 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 +}; + +#endif + +// +// End of "$Id: Fl_Text_Display.H 6902 2009-09-27 11:06:56Z matt $". +// diff --git a/src/include/Fl_Text_Editor_mod.H b/src/include/Fl_Text_Editor_mod.H index 1f038734..df5c2a5a 100644 --- a/src/include/Fl_Text_Editor_mod.H +++ b/src/include/Fl_Text_Editor_mod.H @@ -1,110 +1,7 @@ -// -// "$Id: Fl_Text_Editor.H 4288 2005-04-16 00:13:17Z mike $" -// -// Header file for Fl_Text_Editor class. -// -// Copyright 2001-2005 by Bill Spitzak and others. -// Original code Copyright Mark Edel. Permission to distribute under -// the LGPL for the FLTK library granted by Mark Edel. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library 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 -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -// USA. -// -// Please report all bugs and problems on the following page: -// -// http://www.fltk.org/str.php -// - - -#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) - -class Fl_Text_Editor_mod : public Fl_Text_Display_mod { - public: - typedef int (*Key_Func)(int key, Fl_Text_Editor_mod* editor); - - struct Key_Binding { - int key; - int state; - Key_Func function; - Key_Binding* next; - }; - - Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l = 0); - ~Fl_Text_Editor_mod() { remove_all_key_bindings(); } - virtual int handle(int e); - void insert_mode(int b) { insert_mode_ = b; } - int insert_mode() { return insert_mode_; } - - void add_key_binding(int key, int state, Key_Func f, Key_Binding** list); - void add_key_binding(int key, int state, Key_Func f) - { add_key_binding(key, state, f, &key_bindings); } - void remove_key_binding(int key, int state, Key_Binding** list); - void remove_key_binding(int key, int state) - { remove_key_binding(key, state, &key_bindings); } - void remove_all_key_bindings(Key_Binding** list); - void remove_all_key_bindings() { remove_all_key_bindings(&key_bindings); } - void add_default_key_bindings(Key_Binding** list); - Key_Func bound_key_function(int key, int state, Key_Binding* list); - Key_Func bound_key_function(int key, int state) - { return bound_key_function(key, state, key_bindings); } - void default_key_function(Key_Func f) { default_key_function_ = f; } - - // functions for the built in default bindings - static int kf_default(int c, Fl_Text_Editor_mod* e); - static int kf_ignore(int c, Fl_Text_Editor_mod* e); - static int kf_backspace(int c, Fl_Text_Editor_mod* e); - static int kf_enter(int c, Fl_Text_Editor_mod* e); - static int kf_move(int c, Fl_Text_Editor_mod* e); - static int kf_shift_move(int c, Fl_Text_Editor_mod* e); - static int kf_ctrl_move(int c, Fl_Text_Editor_mod* e); - static int kf_c_s_move(int c, Fl_Text_Editor_mod* e); - static int kf_home(int, Fl_Text_Editor_mod* e); - static int kf_end(int c, Fl_Text_Editor_mod* e); - static int kf_left(int c, Fl_Text_Editor_mod* e); - static int kf_up(int c, Fl_Text_Editor_mod* e); - static int kf_right(int c, Fl_Text_Editor_mod* e); - static int kf_down(int c, Fl_Text_Editor_mod* e); - static int kf_page_up(int c, Fl_Text_Editor_mod* e); - static int kf_page_down(int c, Fl_Text_Editor_mod* e); - static int kf_insert(int c, Fl_Text_Editor_mod* e); - static int kf_delete(int c, Fl_Text_Editor_mod* e); - static int kf_copy(int c, Fl_Text_Editor_mod* e); - static int kf_cut(int c, Fl_Text_Editor_mod* e); - static int kf_paste(int c, Fl_Text_Editor_mod* e); - static int kf_select_all(int c, Fl_Text_Editor_mod* e); - static int kf_undo(int c, Fl_Text_Editor_mod* e); - - protected: - int handle_key(); - void maybe_do_callback(); - - int insert_mode_; - Key_Binding* key_bindings; - static Key_Binding* global_key_bindings; - Key_Func default_key_function_; -}; +#include +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# include "Fl_Text_Editor_mod_1_1.H" +#elif FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +# include "Fl_Text_Editor_mod_1_3.H" #endif - -// -// End of "$Id: Fl_Text_Editor.H 4288 2005-04-16 00:13:17Z mike $". -// - diff --git a/src/include/Fl_Text_Editor_mod_1_1.H b/src/include/Fl_Text_Editor_mod_1_1.H new file mode 100644 index 00000000..1f038734 --- /dev/null +++ b/src/include/Fl_Text_Editor_mod_1_1.H @@ -0,0 +1,110 @@ +// +// "$Id: Fl_Text_Editor.H 4288 2005-04-16 00:13:17Z mike $" +// +// Header file for Fl_Text_Editor class. +// +// Copyright 2001-2005 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + + +#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) + +class Fl_Text_Editor_mod : public Fl_Text_Display_mod { + public: + typedef int (*Key_Func)(int key, Fl_Text_Editor_mod* editor); + + struct Key_Binding { + int key; + int state; + Key_Func function; + Key_Binding* next; + }; + + Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l = 0); + ~Fl_Text_Editor_mod() { remove_all_key_bindings(); } + virtual int handle(int e); + void insert_mode(int b) { insert_mode_ = b; } + int insert_mode() { return insert_mode_; } + + void add_key_binding(int key, int state, Key_Func f, Key_Binding** list); + void add_key_binding(int key, int state, Key_Func f) + { add_key_binding(key, state, f, &key_bindings); } + void remove_key_binding(int key, int state, Key_Binding** list); + void remove_key_binding(int key, int state) + { remove_key_binding(key, state, &key_bindings); } + void remove_all_key_bindings(Key_Binding** list); + void remove_all_key_bindings() { remove_all_key_bindings(&key_bindings); } + void add_default_key_bindings(Key_Binding** list); + Key_Func bound_key_function(int key, int state, Key_Binding* list); + Key_Func bound_key_function(int key, int state) + { return bound_key_function(key, state, key_bindings); } + void default_key_function(Key_Func f) { default_key_function_ = f; } + + // functions for the built in default bindings + static int kf_default(int c, Fl_Text_Editor_mod* e); + static int kf_ignore(int c, Fl_Text_Editor_mod* e); + static int kf_backspace(int c, Fl_Text_Editor_mod* e); + static int kf_enter(int c, Fl_Text_Editor_mod* e); + static int kf_move(int c, Fl_Text_Editor_mod* e); + static int kf_shift_move(int c, Fl_Text_Editor_mod* e); + static int kf_ctrl_move(int c, Fl_Text_Editor_mod* e); + static int kf_c_s_move(int c, Fl_Text_Editor_mod* e); + static int kf_home(int, Fl_Text_Editor_mod* e); + static int kf_end(int c, Fl_Text_Editor_mod* e); + static int kf_left(int c, Fl_Text_Editor_mod* e); + static int kf_up(int c, Fl_Text_Editor_mod* e); + static int kf_right(int c, Fl_Text_Editor_mod* e); + static int kf_down(int c, Fl_Text_Editor_mod* e); + static int kf_page_up(int c, Fl_Text_Editor_mod* e); + static int kf_page_down(int c, Fl_Text_Editor_mod* e); + static int kf_insert(int c, Fl_Text_Editor_mod* e); + static int kf_delete(int c, Fl_Text_Editor_mod* e); + static int kf_copy(int c, Fl_Text_Editor_mod* e); + static int kf_cut(int c, Fl_Text_Editor_mod* e); + static int kf_paste(int c, Fl_Text_Editor_mod* e); + static int kf_select_all(int c, Fl_Text_Editor_mod* e); + static int kf_undo(int c, Fl_Text_Editor_mod* e); + + protected: + int handle_key(); + void maybe_do_callback(); + + int insert_mode_; + Key_Binding* key_bindings; + static Key_Binding* global_key_bindings; + Key_Func default_key_function_; +}; + +#endif + +// +// End of "$Id: Fl_Text_Editor.H 4288 2005-04-16 00:13:17Z mike $". +// + diff --git a/src/include/Fl_Text_Editor_mod_1_3.H b/src/include/Fl_Text_Editor_mod_1_3.H new file mode 100644 index 00000000..1c2d0c8a --- /dev/null +++ b/src/include/Fl_Text_Editor_mod_1_3.H @@ -0,0 +1,141 @@ +// +// "$Id: Fl_Text_Editor.H 6893 2009-09-20 19:24:24Z greg.ercolano $" +// +// Header file for Fl_Text_Editor class. +// +// Copyright 2001-2009 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +/* \file + Fl_Text_Editor widget . */ + + +#ifndef FL_TEXT_EDITOR_H +#define FL_TEXT_EDITOR_H + +#include "Fl_Text_Display_mod.H" + +// key will match in any state +#define FL_TEXT_EDITOR_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 + class. +*/ +class FL_EXPORT Fl_Text_Editor_mod : public Fl_Text_Display_mod { + public: + /** Key function binding callback type */ + typedef int (*Key_Func)(int key, Fl_Text_Editor_mod* editor); + + /** Simple linked list associating a key/state to a function */ + struct Key_Binding { + int key; ///< the key pressed + int state; ///< the state of key modifiers + Key_Func function; ///< associated function + Key_Binding* next; ///< next key binding in the list + }; + + Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l = 0); + ~Fl_Text_Editor_mod() { remove_all_key_bindings(); } + virtual int handle(int e); + /** + Sets the current insert mode; if non-zero, new text + is inserted before the current cursor position. Otherwise, new + text replaces text at the current cursor position. + */ + void insert_mode(int b) { insert_mode_ = b; } + /** + Gets the current insert mode; if non-zero, new text + is inserted before the current cursor position. Otherwise, new + text replaces text at the current cursor position. + */ + int insert_mode() { return insert_mode_; } + + void add_key_binding(int key, int state, Key_Func f, Key_Binding** list); + /** Adds a key of state "state" with the function "function" */ + void add_key_binding(int key, int state, Key_Func f) + { add_key_binding(key, state, f, &key_bindings); } + void remove_key_binding(int key, int state, Key_Binding** list); + /** Removes the key binding associated with the key "key" of state "state". */ + void remove_key_binding(int key, int state) + { remove_key_binding(key, state, &key_bindings); } + void remove_all_key_bindings(Key_Binding** list); + /** Removes all of the key bindings associated with the text editor or list. */ + void remove_all_key_bindings() { remove_all_key_bindings(&key_bindings); } + void add_default_key_bindings(Key_Binding** list); + Key_Func bound_key_function(int key, int state, Key_Binding* list); + /** Returns the function associated with a key binding. */ + Key_Func bound_key_function(int key, int state) + { return bound_key_function(key, state, key_bindings); } + /** Sets the default key function for unassigned keys. */ + void default_key_function(Key_Func f) { default_key_function_ = f; } + + // functions for the built in default bindings + static int kf_default(int c, Fl_Text_Editor_mod* e); + static int kf_ignore(int c, Fl_Text_Editor_mod* e); + static int kf_backspace(int c, Fl_Text_Editor_mod* e); + static int kf_enter(int c, Fl_Text_Editor_mod* e); + static int kf_move(int c, Fl_Text_Editor_mod* e); + static int kf_shift_move(int c, Fl_Text_Editor_mod* e); + static int kf_ctrl_move(int c, Fl_Text_Editor_mod* e); + static int kf_c_s_move(int c, Fl_Text_Editor_mod* e); + static int kf_meta_move(int c, Fl_Text_Editor_mod* e); + static int kf_m_s_move(int c, Fl_Text_Editor_mod* e); + static int kf_home(int, Fl_Text_Editor_mod* e); + static int kf_end(int c, Fl_Text_Editor_mod* e); + static int kf_left(int c, Fl_Text_Editor_mod* e); + static int kf_up(int c, Fl_Text_Editor_mod* e); + static int kf_right(int c, Fl_Text_Editor_mod* e); + static int kf_down(int c, Fl_Text_Editor_mod* e); + static int kf_page_up(int c, Fl_Text_Editor_mod* e); + static int kf_page_down(int c, Fl_Text_Editor_mod* e); + static int kf_insert(int c, Fl_Text_Editor_mod* e); + static int kf_delete(int c, Fl_Text_Editor_mod* e); + static int kf_copy(int c, Fl_Text_Editor_mod* e); + static int kf_cut(int c, Fl_Text_Editor_mod* e); + static int kf_paste(int c, Fl_Text_Editor_mod* e); + static int kf_select_all(int c, Fl_Text_Editor_mod* e); + static int kf_undo(int c, Fl_Text_Editor_mod* e); + + protected: + int handle_key(); + void maybe_do_callback(); + +#ifndef FL_DOXYGEN + int insert_mode_; + Key_Binding* key_bindings; + static Key_Binding* global_key_bindings; + Key_Func default_key_function_; +#endif +}; + +#endif + +// +// End of "$Id: Fl_Text_Editor.H 6893 2009-09-20 19:24:24Z greg.ercolano $". +// + diff --git a/src/include/FreqControl.h b/src/include/FreqControl.h index fd6d19d5..a582372c 100644 --- a/src/include/FreqControl.h +++ b/src/include/FreqControl.h @@ -55,6 +55,7 @@ public: void updatevalue(); void value(long lv); long value(){return val;}; + void font(Fl_Font n); void SetONCOLOR (uchar r, uchar g, uchar b); void SetOFFCOLOR (uchar r, uchar g, uchar b); void GetONCOLOR (uchar &r, uchar &g, uchar &b) { @@ -73,6 +74,7 @@ private: static const char *Label[]; int mult[MAX_DIGITS]; Fl_Box *decbx; + Fl_Font font_number; Fl_Color OFFCOLOR; Fl_Color ONCOLOR; Fl_Color SELCOLOR; diff --git a/src/include/Viewer.h b/src/include/Viewer.h index 08015576..6847fa9f 100644 --- a/src/include/Viewer.h +++ b/src/include/Viewer.h @@ -20,7 +20,7 @@ extern Fl_Double_Window *dlgViewer; extern Fl_Double_Window* createViewer(); extern void openViewer(); -extern void viewaddchr(int ch, int freq, char c); +extern void viewaddchr(int ch, int freq, char c, int md); extern void initViewer(); extern void viewclearchannel(int ch); extern void viewer_paste_freq(int freq); diff --git a/src/include/analysis.h b/src/include/analysis.h index 72f866b6..19e840f0 100644 --- a/src/include/analysis.h +++ b/src/include/analysis.h @@ -24,7 +24,7 @@ #define _anal_H #include -#include +#include #include "complex.h" #include "filters.h" @@ -47,29 +47,33 @@ private: fftfilt *bpfilt; Cmovavg *ffilt; Cmovavg *favg; - + mbuffer pipe; int pipeptr; double prevsymbol; complex prevsmpl; int symbollen; - + + int restart_count; + double fout_1; double fout_2; + long wf_freq; + + struct timespec start_time; double sum; - + void clear_syncscope(); inline complex mixer(complex in); int rx(bool bit); double nco(double freq); void writeFile(); - - std::ofstream analysisFile; + std::string analysisFilename; - + public: anal(); ~anal(); diff --git a/src/include/colorsfonts.h b/src/include/colorsfonts.h index 3b88fff3..93102066 100644 --- a/src/include/colorsfonts.h +++ b/src/include/colorsfonts.h @@ -7,6 +7,7 @@ void selectColorsFonts(); void cbWaterfallFontBrowser(Fl_Widget*, void*); void cbViewerFontBrowser(Fl_Widget*, void*); +void cbFreqControlFontBrowser(Fl_Widget*, void*); #include extern Fl_Double_Window *dlgColorFont; #include @@ -16,6 +17,7 @@ extern Fl_Tabs *tabsColors; #include #include extern Fl_Box *FDdisplay; +extern Fl_Button *btn_freq_control_font; extern Fl_Button *btnBackgroundColor; extern Fl_Button *btnForegroundColor; extern Fl_Button *btnFD_SystemColor; diff --git a/src/include/confdialog.h b/src/include/confdialog.h index 0ba37827..7c50340d 100644 --- a/src/include/confdialog.h +++ b/src/include/confdialog.h @@ -82,8 +82,7 @@ extern Fl_Check_Button *btnWF_UIqsy; extern Fl_Check_Button *btnWF_UIxmtlock; extern Fl_Button *btn_wf_enable_all; extern Fl_Button *btn_wf_disable_all; -extern Fl_Group *tabWfallRestart; -extern Fl_Check_Button *btnCheckButtons; +extern Fl_Check_Button *btn_rx_lowercase; extern Fl_Group *tabWaterfall; extern Fl_Tabs *tabsWaterfall; #include "colorbox.h" @@ -171,6 +170,12 @@ extern Fl_Choice *mnuOlivia_Tones; extern Fl_Counter2 *cntOlivia_smargin; extern Fl_Counter2 *cntOlivia_sinteg; extern Fl_Check_Button *btnOlivia_8bit; +extern Fl_Group *tabContestia; +extern Fl_Choice *mnuContestia_Bandwidth; +extern Fl_Choice *mnuContestia_Tones; +extern Fl_Counter2 *cntContestia_smargin; +extern Fl_Counter2 *cntContestia_sinteg; +extern Fl_Check_Button *btnContestia_8bit; extern Fl_Group *tabPSK; extern Fl_Tabs *tabsPSK; extern Fl_Counter2 *cntSearchRange; diff --git a/src/include/configuration.h b/src/include/configuration.h index 88e34110..0fa3f800 100644 --- a/src/include/configuration.h +++ b/src/include/configuration.h @@ -181,6 +181,9 @@ " 2: signal search; 3: change squelch level; 4: change modem carrier;\n" \ " 5: change modem; 6: scroll visible area. The default is 4.", \ waterfall::WF_CARRIER) \ + ELEM_(bool, rx_lowercase, "RX_LOWERCASE", \ + "Print Rx in lowercase for CW, RTTY, CONTESTIA and THROB", \ + false) \ /* PSK, filter can be 0, 1, 2, 3 or 4 */ \ ELEM_(int, PSK_filter, "PSKFILTER", \ "Not configurable; must always be 0", \ @@ -399,6 +402,24 @@ ELEM_(bool, olivia8bit, "OLIVIA8BIT", \ "8-bit extended characters", \ false) \ + /* CONTESTIA */ \ + ELEM_(int, contestiatones, "CONTESTIATONES", \ + "Number of tones. Values are as follows:\n" \ + " 0: 2; 1: 4; 2: 8; 3: 16; 4: 32; 5: 64; 6: 128; 7: 256", \ + 2) /* 8 */ \ + ELEM_(int, contestiabw, "CONTESTIABW", \ + "Bandwidth (Hz). Values are as follows:\n" \ + " 0: 125; 1: 250; 2: 500; 3: 1000; 4: 2000.", \ + 2) /* 500 */ \ + ELEM_(int, contestiasmargin, "CONTESTIASMARGIN", \ + "Tune margin (tone frequency spacing)", \ + 8) \ + ELEM_(int, contestiasinteg, "CONTESTIASINTEG", \ + "Integration period (FEC blocks)", \ + 4) \ + ELEM_(bool, contestia8bit, "CONTESTIA8BIT", \ + "8-bit extended characters", \ + false) \ /* THOR */ \ ELEM_(double, THOR_BW, "THORBW", \ "Filter bandwidth factor (bandwidth relative to signal width)", \ @@ -525,9 +546,6 @@ ELEM_(bool, tooltips, "TOOLTIPS", \ "Show tooltips", \ true) \ - ELEM_(bool, useCheckButtons, "USECHECKBUTTONS", \ - "Use check buttons for AFC and SQL", \ - false) \ ELEM_(bool, NagMe, "NAGME", \ "Prompt to save log", \ false) \ @@ -983,6 +1001,12 @@ ELEM_(int, WaterfallFontsize, "WATERFALLFONTSIZE", \ "Waterfall font size", \ 12) \ + ELEM_(std::string, FreqControlFontName, "FREQCONTROLFONTNAME", \ + "Frequency Control font name", \ + "") \ + ELEM_(Fl_Font, FreqControlFontnbr, "FREQCONTROLFONTNBR", \ + "Frequency Control font number", \ + FL_COURIER) \ ELEM_(std::string, ui_scheme, "UISCHEME", \ "FLTK UI scheme (none or base, gtk+, plastic)", \ "gtk+") \ diff --git a/src/include/contestia.h b/src/include/contestia.h new file mode 100644 index 00000000..b1e82040 --- /dev/null +++ b/src/include/contestia.h @@ -0,0 +1,86 @@ +// ---------------------------------------------------------------------------- +// contestia.h +// +// Copyright (C) 2006-2010 +// Dave Freese, W1HKJ +// +// 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 _CONTESTIA_H +#define _CONTESTIA_H + +#include "modem.h" +#include "jalocha/pj_mfsk.h" +#include "sound.h" + +#define TONE_DURATION (SCBLOCKSIZE * 16) +#define SR4 ((TONE_DURATION) / 4) + +class contestia : public modem { +private: + + MFSK_Transmitter < double >*Tx; + MFSK_Receiver < double >*Rx; + + double *txfbuffer; + int txbufferlen; + + double phaseacc; + complex prevsymbol; + int preamble; + unsigned int shreg; + + double np; + double sp; + double sigpwr; + double noisepwr; + + int escape; + int smargin; + int sinteg; + int tones; + int bw; + double tone_bw; + + int preamblesent; + int postamblesent; + double preamblephase; + + double txbasefreq; + double tone_midfreq; + double lastfreq; + + double ampshape[SR4]; + double tonebuff[TONE_DURATION]; + + double nco(double freq); + void send_tones(); + +public: + contestia(); + ~contestia(); + void init(); + void rx_init(); + void tx_init(SoundBase *sc); + void restart(); + int rx_process(const double *buf, int len); + int tx_process(); + int unescape(int c); +}; + + +#endif diff --git a/src/include/fl_digi.h b/src/include/fl_digi.h index 1aed1272..70488264 100644 --- a/src/include/fl_digi.h +++ b/src/include/fl_digi.h @@ -111,8 +111,8 @@ extern Fl_Button *qso_btnClearList; extern Fl_Value_Slider2 *valRcvMixer; extern Fl_Value_Slider2 *valXmtMixer; -extern Fl_Button *btnAFC; -extern Fl_Button *btnSQL; +extern Fl_Button *btnAFC; +extern Fl_Button *btnSQL; extern Fl_Light_Button *btnRSID; extern Fl_Light_Button *btnTxRSID; extern Fl_Light_Button *btnTune; @@ -132,7 +132,6 @@ extern Fl_Input2 *habCustom; extern Fl_Input2 *habChecksum; // extern bool withnoise; -extern bool useCheckButtons; extern int altMacros; extern waterfall *wf; @@ -208,6 +207,7 @@ extern int get_secondary_char(); extern void put_echo_char(unsigned int data); extern void resetRTTY(); extern void resetOLIVIA(); +extern void resetCONTESTIA(); extern void resetTHOR(); extern void resetDOMEX(); extern void resetSoundCard(); @@ -235,6 +235,9 @@ extern void set_rtty_tab_widgets(); extern void set_olivia_tab_widgets(); extern void set_olivia_default_integ(); +extern void set_contestia_tab_widgets(); +extern void set_contestia_default_integ(); + extern void startMacroTimer(); extern void stopMacroTimer(); extern void cb_ResetSerNbr(); @@ -254,6 +257,10 @@ void note_qrg(bool no_dup = true, const char* prefix = " ", const char* suffix = void set_olivia_bw(int bw); void set_olivia_tones(int tones); + +void set_contestia_bw(int bw); +void set_contestia_tones(int tones); + void set_rtty_shift(int shift); void set_rtty_baud(float baud); void set_rtty_bits(int bits); diff --git a/src/include/globals.h b/src/include/globals.h index 0470b263..bd0f808e 100644 --- a/src/include/globals.h +++ b/src/include/globals.h @@ -51,6 +51,8 @@ enum { MODE_CW, + MODE_CONTESTIA, + MODE_DOMINOEX4, MODE_DOMINOEX5, MODE_DOMINOEX8, @@ -128,6 +130,7 @@ enum { MODE_THROB_FIRST = MODE_THROB1, MODE_THROB_LAST = MODE_THROBX4, + MODE_SSB, MODE_WWV, MODE_ANALYSIS, diff --git a/src/include/jalocha/pj_cmpx.h b/src/include/jalocha/pj_cmpx.h old mode 100755 new mode 100644 diff --git a/src/include/jalocha/pj_fft.h b/src/include/jalocha/pj_fft.h old mode 100755 new mode 100644 diff --git a/src/include/jalocha/pj_fht.h b/src/include/jalocha/pj_fht.h old mode 100755 new mode 100644 diff --git a/src/include/jalocha/pj_fifo.h b/src/include/jalocha/pj_fifo.h old mode 100755 new mode 100644 diff --git a/src/include/jalocha/pj_gray.h b/src/include/jalocha/pj_gray.h old mode 100755 new mode 100644 diff --git a/src/include/jalocha/pj_lowpass3.h b/src/include/jalocha/pj_lowpass3.h old mode 100755 new mode 100644 diff --git a/src/include/jalocha/pj_mfsk.h b/src/include/jalocha/pj_mfsk.h old mode 100755 new mode 100644 index e41997ba..9a2f5ba3 --- a/src/include/jalocha/pj_mfsk.h +++ b/src/include/jalocha/pj_mfsk.h @@ -20,406 +20,27 @@ // ===================================================================== static inline size_t Exp2(uint32_t X) { - return (uint32_t)1 << X; + return (uint32_t)1 << X; } static inline size_t Log2(uint32_t X) { - uint32_t Y; - for ( Y = 0; X > 1; X >>= 1) - Y += 1; - return Y; + uint32_t Y; + for ( Y = 0; X > 1; X >>= 1) + Y += 1; + return Y; } // ===================================================================== -template -class MFSK_Delay -{ -public: - size_t Len; - Type *Tap; - size_t TapPtr; - - MFSK_Delay() { Tap = 0; Len = 0; } - ~MFSK_Delay() { free(Tap); } - - int Preset(size_t NewLen) { - Len = NewLen; - if (ReallocArray(&Tap, Len) < 0) { - Len = 0; - return -1; - } - size_t Idx; - for (Idx = 0; Idx < Len; Idx++) - Tap[Idx] = 0; - TapPtr = 0; - return 0; - } - - void Process(Type Input) { - if (Len == 0) - return; - Tap[TapPtr] = Input; - TapPtr += 1; - if (TapPtr >= Len) TapPtr -= Len; - } - - void Process(Type Input, Type &Output) { - if (Len == 0) { - Output = Input; - return; - } - Output = Tap[TapPtr]; - Tap[TapPtr] = Input; - TapPtr += 1; - if (TapPtr >= Len) TapPtr -= Len; - } - - void Process(Type * Buffer, size_t BufferLen) { - size_t Idx; - Type Output; - for (Idx = 0; Idx < BufferLen; Idx++) { - Process(Buffer[Idx], Output); - Buffer[Idx] = Output; - } - } -}; - -// ===================================================================== - -template -class MFSK_Symbol -{ -public: - size_t BitsPerSymbol; - size_t Carriers; - - Type *Energy; - Type *Correction; - Type *Corrected; - - size_t Code; - Type PeakEnergy; - Type Background; - Type ScanThreshold; - size_t ScanIndex; - - MFSK_Symbol() { - Init(); - Default(); - } - - ~MFSK_Symbol() { Free(); } - - void Init(void) { - Energy = 0; - Correction = 0; - Corrected = 0; - } - - void Free(void) { - free(Energy); - Energy = 0; - free(Correction); - Correction = 0; - free(Corrected); - Corrected = 0; - } - - void Default(void) { - BitsPerSymbol = 5; - } - - int Preset(void) { - Carriers = Exp2(BitsPerSymbol); - if (ReallocArray(&Energy, Carriers) < 0) goto Error; - if (ReallocArray(&Correction, Carriers) < 0) goto Error; - if (ReallocArray(&Corrected, Carriers) < 0) goto Error; - return 0; - Error: - Free(); - return -1; - } - - void ClearCorrection(void) { - size_t Idx; - for (Idx = 0; Idx < Carriers; Idx++) - Correction[Idx] = 0; - } - - void AddCorrection(void) { - size_t Idx; - for (Idx = 0; Idx < Carriers; Idx++) - Corrected[Idx] = Energy[Idx] + Correction[Idx]; - } - - void AddCorrection(Type Weight) { - size_t Idx; - for (Idx = 0; Idx < Carriers; Idx++) - Corrected[Idx] = Energy[Idx] + Weight * Correction[Idx]; - } - - void MakeRandom(Type SignalToNoise = 1.0) { - Type Signal = 1.0; - Type Noise = 1.0 / SignalToNoise; - Type NoiseEnergy = Noise * Noise; - NoiseEnergy /= Carriers; - - size_t Idx; - double UniformNoise = 0; - for (Idx = 0; Idx < Carriers; Idx++) { - UniformNoise = ((double)rand()+1.0)/((double)RAND_MAX+1.0); - Energy[Idx] = NoiseEnergy * (-log(UniformNoise)); - } - - double Phase = 2 * M_PI * ((double)rand()+1.0)/((double)RAND_MAX+1.0); - Type NoiseAmpl = sqrt(Energy[Code]); - Signal += cos(Phase) * NoiseAmpl; - NoiseAmpl *= sin(Phase); - Energy[Code] = Signal*Signal + NoiseAmpl * NoiseAmpl; - ClearCorrection(); - AddCorrection(); - } - - void Decode(void) { - size_t Idx; - PeakEnergy = 0; - Code = 0; - Background = 0; - for (Idx=0; Idx PeakEnergy) { - PeakEnergy = Energy; - Code = Idx; - } - } - Background -= PeakEnergy; - Background /= (Carriers-1); - } - - void Scan_Init(Type Threshold = 4.0) { - ScanThreshold = Threshold * Background; - } - - int Scan_First(void) { - for (ScanIndex = 0; ScanIndex < Carriers; ScanIndex++) - if (Corrected[ScanIndex] > ScanThreshold) - return 0; - return -1; - } // 0 => OK, -1 => no data above threshold - - int Scan_Next(void) { - for ( ScanIndex++; ScanIndex < Carriers; ScanIndex++) - if (Corrected[ScanIndex]>ScanThreshold) - return 0; - if (Scan_First() < 0) - return -1; - return 1; - } // 0 => OK, 1 => wrapped around, -1 => no data above threshold - - void Print(void) { - size_t Idx; - printf("MFSK_Symbol: %d bits/%d carriers, Peak = %3.1f/%3.1f @ 0x%02X\n", - BitsPerSymbol,Carriers, - PeakEnergy,Background, Code); - printf("Energy:"); - for (Idx=0; Idx -class MFSK_LineCorrector -{ -public: - - size_t BitsPerSymbol; - size_t SymbolsPerLine; - size_t CodeMask; - - MFSK_Symbol **Symbol; - - MFSK_LineCorrector() { - Init(); - Default(); - } - - ~MFSK_LineCorrector() { - Free(); - } - - void Init(void) { - Symbol = 0; - } - - void Free(void) { - free(Symbol); - Symbol = 0; - } - - void Default(void) { - BitsPerSymbol = 5; - SymbolsPerLine = 4; - } - - int Preset(void) { - CodeMask = Exp2(BitsPerSymbol) - 1; - if (ReallocArray(&Symbol, SymbolsPerLine) < 0) goto Error; - return 0; - Error: - Free(); - return -1; - } - - void CorrectSymbol(size_t SymbolIdx) { - size_t Idx; - size_t Check = 0; - Type Corr = 0; - for (Idx = 0; Idx < SymbolsPerLine; Idx++) { - if (Idx == SymbolIdx) continue; - MFSK_Symbol *SymbolPtr = Symbol[Idx]; - Check -= SymbolPtr->Code; - Corr += SymbolPtr->PeakEnergy; // -SymbolPtr->Background; - } - Check &= CodeMask; - Symbol[SymbolIdx]->Correction[Check] += Corr; - } - - void CorrectSymbol_Slow(size_t SymbolIdx) { - size_t Idx; - for (Idx = 0; IdxScan_First()<0) break; - } - if (Idx < SymbolsPerLine) return; - for ( ; ; ) { - size_t Check = 0; - Type Corr = 0; - for (Idx = 0; Idx < SymbolsPerLine; Idx++) { - if (Idx == SymbolIdx) continue; - MFSK_Symbol *SymbolPtr = Symbol[Idx]; - size_t Code = SymbolPtr->ScanIndex; - Check -= Code; - Corr += SymbolPtr->Corrected[Code] - SymbolPtr->ScanThreshold; - } - Check &= CodeMask; - Symbol[SymbolIdx]->Correction[Check] += Corr; - for (Idx = 0; Idx < SymbolsPerLine; Idx++) { - if (Idx == SymbolIdx) continue; - if (Symbol[Idx]->Scan_Next() == 0) break; - } - if (Idx >= SymbolsPerLine) break; - } - } - - void Correct(size_t Iter = 8, Type Weight = 1.0) { - Weight /= (2 * SymbolsPerLine); - for ( ; Iter; Iter--) { - size_t Idx; - for (Idx = 0; Idx < SymbolsPerLine; Idx++) { - Symbol[Idx]->ClearCorrection(); - Symbol[Idx]->Scan_Init(); - } - for (Idx = 0; Idx < SymbolsPerLine; Idx++) - CorrectSymbol(Idx); - for (Idx = 0; Idx < SymbolsPerLine; Idx++) { - Symbol[Idx]->AddCorrection(Weight); - Symbol[Idx]->Decode(); - } - } - } - -} ; - -template -class MFSK_FEC -{ -public: - size_t BitsPerSymbol; - size_t SymbolsPerLine; - size_t Dimensions; - size_t SymbolsPerBlock; - size_t CodeMask; - MFSK_Symbol *Symbol; - - MFSK_FEC() { - Init(); - Default(); - } - - ~MFSK_FEC() { - Free(); - } - - void Init(void) { - Symbol = 0; - } - - void Free(void) { - delete [] Symbol; - Symbol = 0; - } - - void Default(void) { - BitsPerSymbol = 5; - SymbolsPerLine = 4; - Dimensions = 3; - } - - int Preset(void) { - Free(); - SymbolsPerBlock = 1; - size_t Dim; - for (Dim = 0; Dim < Dimensions; Dim++) - SymbolsPerBlock *= SymbolsPerLine; - CodeMask = Exp2(BitsPerSymbol) - 1; - Symbol = new MFSK_Symbol[SymbolsPerBlock]; - if (Symbol == 0) return -1; - size_t Idx; - for (Idx = 0; Idx < SymbolsPerBlock; Idx++) { - Symbol[Idx].BitsPerSymbol = BitsPerSymbol; - if (Symbol[Idx].Preset() < 0) return -1; - } - return 0; - } - - void CalculateCheck(size_t Idx, size_t Step) { - size_t Check=0; - size_t LineIdx; - for ( LineIdx = 0; LineIdx < (SymbolsPerLine - 1); LineIdx++,Idx += Step) - Check -= Symbol[Idx].Code; - Check &= CodeMask; - Symbol[Idx].Code = Check; - } - - int Correct(void) { - return 0; - } - -}; - -// ===================================================================== - // the symbol shape described in frequency domain static const double MFSK_SymbolFreqShape[] = { 1.0, 1.0 } ; // use raised cosine shape - experimental // from gMFSK -// { +1.0000000000, -// +1.1913785723, -// -0.0793018558, -// -0.2171442026, -// -0.0014526076 +// { +1.0000000000, +// +1.1913785723, +// -0.0793018558, +// -0.2171442026, +// -0.0014526076 //}; // from DM780 //{ @@ -430,7 +51,7 @@ static const double MFSK_SymbolFreqShape[] = //}; static const size_t MFSK_SymbolFreqShapeLen = - sizeof(MFSK_SymbolFreqShape) / sizeof(double); + sizeof(MFSK_SymbolFreqShape) / sizeof(double); // ===================================================================== @@ -439,182 +60,180 @@ class MFSK_Modulator { public: // parameters to be set before calling Preset() - size_t SymbolLen; // length of the symbol, must be a power of 2 - size_t FirstCarrier; // first carrier in terms of FFT freq. bins - size_t BitsPerSymbol; // bits per symbol => number of carriers/tones - int UseGrayCode; + size_t SymbolLen; // length of the symbol, must be a power of 2 + size_t FirstCarrier; // first carrier in terms of FFT freq. bins + size_t BitsPerSymbol; // bits per symbol => number of carriers/tones + int UseGrayCode; - static const size_t CarrierSepar = 2; + static const size_t CarrierSepar = 2; // parameters that are calculated by Preset() - size_t Carriers; // number of tones/carriers - size_t SymbolSepar; // time distance between symbols - int SymbolPhase; // the phase of the tone being transmitted - int Reverse; // send carriers in reverse order? + size_t Carriers; // number of tones/carriers + size_t SymbolSepar; // time distance between symbols + int SymbolPhase; // the phase of the tone being transmitted + int Reverse; // send carriers in reverse order? private: - Type *CosineTable; // Cosine table for fast cos/sin calculation - Type *SymbolShape; // the shape of the symbol - Type *OutTap; // output tap (buffer) - size_t TapPtr; - size_t WrapMask; + Type *CosineTable; // Cosine table for fast cos/sin calculation + Type *SymbolShape; // the shape of the symbol + Type *OutTap; // output tap (buffer) + size_t TapPtr; + size_t WrapMask; public: - MFSK_Modulator() { - Init(); - Default(); - } + MFSK_Modulator() { + Init(); + Default(); + } - ~MFSK_Modulator() { - Free(); - } + ~MFSK_Modulator() { + Free(); + } - void Init(void) { - CosineTable = 0; - SymbolShape = 0; - OutTap = 0; } + void Init(void) { + CosineTable = 0; + SymbolShape = 0; + OutTap = 0; + } - void Free(void) { - free(CosineTable); - CosineTable = 0; - free(SymbolShape); - SymbolShape = 0; - free(OutTap); - OutTap = 0; - } + void Free(void) { + free(CosineTable); + CosineTable = 0; + free(SymbolShape); + SymbolShape = 0; + free(OutTap); + OutTap = 0; + } - void Default(void) { - SymbolLen = 512; - FirstCarrier = 32; - BitsPerSymbol = 5; - Reverse = 0; - UseGrayCode = 1; - } + void Default(void) { + SymbolLen = 512; + FirstCarrier = 32; + BitsPerSymbol = 5; + Reverse = 0; + UseGrayCode = 1; + } - int Preset(void) { - size_t Idx; + int Preset(void) { + size_t Idx; - Carriers = Exp2(BitsPerSymbol); - SymbolSepar = SymbolLen / 2; + Carriers = Exp2(BitsPerSymbol); + SymbolSepar = SymbolLen / 2; - if (ReallocArray(&CosineTable, SymbolLen) < 0) goto Error; - for (Idx = 0; Idx < SymbolLen; Idx++) - CosineTable[Idx] = cos((2*M_PI*Idx) / SymbolLen); + if (ReallocArray(&CosineTable, SymbolLen) < 0) goto Error; + for (Idx = 0; Idx < SymbolLen; Idx++) + CosineTable[Idx] = cos((2*M_PI*Idx) / SymbolLen); - if (ReallocArray(&SymbolShape, SymbolLen) < 0) goto Error; + if (ReallocArray(&SymbolShape, SymbolLen) < 0) goto Error; - { size_t Time; - double Ampl = MFSK_SymbolFreqShape[0]; - for (Time = 0; Time < SymbolLen; Time++) - SymbolShape[Time] = Ampl; - } - size_t Freq; - for (Freq = 1; Freq < MFSK_SymbolFreqShapeLen; Freq++) { - size_t Time; - double Ampl = MFSK_SymbolFreqShape[Freq]; - if (Freq & 1) Ampl = (-Ampl); - size_t Phase = 0; - for (Time = 0; Time < SymbolLen; Time++) { - SymbolShape[Time] += Ampl*CosineTable[Phase]; - Phase += Freq; - if (Phase >= SymbolLen) Phase-=SymbolLen; - } - } - { size_t Time; - double Scale = 1.0/4; - for (Time = 0; Time < SymbolLen; Time++) - SymbolShape[Time] *= Scale; - } + { size_t Time; + double Ampl = MFSK_SymbolFreqShape[0]; + for (Time = 0; Time < SymbolLen; Time++) + SymbolShape[Time] = Ampl; + } + size_t Freq; + for (Freq = 1; Freq < MFSK_SymbolFreqShapeLen; Freq++) { + size_t Time; + double Ampl = MFSK_SymbolFreqShape[Freq]; + if (Freq & 1) Ampl = (-Ampl); + size_t Phase = 0; + for (Time = 0; Time < SymbolLen; Time++) { + SymbolShape[Time] += Ampl*CosineTable[Phase]; + Phase += Freq; + if (Phase >= SymbolLen) Phase-=SymbolLen; + } + } + { size_t Time; + double Scale = 1.0/4; + for (Time = 0; Time < SymbolLen; Time++) + SymbolShape[Time] *= Scale; + } - if (ReallocArray(&OutTap, SymbolLen) < 0) goto Error; - for (Idx = 0; Idx < SymbolLen; Idx++) - OutTap[Idx] = 0; - TapPtr = 0; + if (ReallocArray(&OutTap, SymbolLen) < 0) goto Error; + for (Idx = 0; Idx < SymbolLen; Idx++) + OutTap[Idx] = 0; + TapPtr = 0; - WrapMask = SymbolLen-1; - SymbolPhase = 0; + WrapMask = SymbolLen-1; + SymbolPhase = 0; - return 0; + return 0; - Error: - Free(); - return -1; - } + Error: + Free(); + return -1; + } - void Send(uint8_t Symbol) - { - if (UseGrayCode) Symbol=GrayCode(Symbol); - int SymbolFreq; + void Send(uint8_t Symbol) { + if (UseGrayCode) Symbol=GrayCode(Symbol); + int SymbolFreq; - if (Reverse == 1) { int RevFirstCar=FirstCarrier-2; SymbolFreq=RevFirstCar-CarrierSepar*Symbol; } - else { SymbolFreq=FirstCarrier+CarrierSepar*Symbol; } + if (Reverse == 1) { int RevFirstCar=FirstCarrier-2; SymbolFreq=RevFirstCar-CarrierSepar*Symbol; } + else { SymbolFreq=FirstCarrier+CarrierSepar*Symbol; } - int TimeShift=SymbolSepar/2-SymbolLen/2; - SymbolPhase+=SymbolFreq*TimeShift; - SymbolPhase&=WrapMask; + int TimeShift=SymbolSepar/2-SymbolLen/2; + SymbolPhase+=SymbolFreq*TimeShift; + SymbolPhase&=WrapMask; - AddSymbol(SymbolFreq,SymbolPhase); + AddSymbol(SymbolFreq,SymbolPhase); - TimeShift=SymbolSepar/2+SymbolLen/2; - SymbolPhase+=SymbolFreq*TimeShift; - SymbolPhase&=WrapMask; + TimeShift=SymbolSepar/2+SymbolLen/2; + SymbolPhase+=SymbolFreq*TimeShift; + SymbolPhase&=WrapMask; - int PhaseDiffer=SymbolLen/4; - if (rand()&1) PhaseDiffer=(-PhaseDiffer); - SymbolPhase+=PhaseDiffer; - SymbolPhase&=WrapMask; - - } - + int PhaseDiffer=SymbolLen/4; + if (rand()&1) PhaseDiffer=(-PhaseDiffer); + SymbolPhase+=PhaseDiffer; + SymbolPhase&=WrapMask; + } + // get output as 16-bit signed data - int Output(int16_t *Buffer) { - const Type Scale = 32768.0; - const int32_t Limit = 0x7FFF; - size_t Idx; + int Output(int16_t *Buffer) { + const Type Scale = 32768.0; + const int32_t Limit = 0x7FFF; + size_t Idx; - for (Idx = 0; Idx < SymbolSepar; Idx++) { - Type Ampl = OutTap[TapPtr]; - Ampl *= Scale; - int32_t Out = (int32_t)floor(Ampl + 0.5); - if (Out > Limit) Out = Limit; - else if (Out < (-Limit)) Out = (-Limit); - Buffer[Idx] = (int16_t)Out; - OutTap[TapPtr] = 0; - TapPtr += 1; TapPtr &= WrapMask; - } - return SymbolSepar; - } + for (Idx = 0; Idx < SymbolSepar; Idx++) { + Type Ampl = OutTap[TapPtr]; + Ampl *= Scale; + int32_t Out = (int32_t)floor(Ampl + 0.5); + if (Out > Limit) Out = Limit; + else if (Out < (-Limit)) Out = (-Limit); + Buffer[Idx] = (int16_t)Out; + OutTap[TapPtr] = 0; + TapPtr += 1; TapPtr &= WrapMask; + } + return SymbolSepar; + } - template - int Output(OutType *Buffer) { - size_t Idx; - for (Idx = 0; Idx < SymbolSepar; Idx++) { - Buffer[Idx] = OutTap[TapPtr]; - OutTap[TapPtr] = 0; - TapPtr += 1; - TapPtr &= WrapMask; - } - return SymbolSepar; - } + template + int Output(OutType *Buffer) { + size_t Idx; + for (Idx = 0; Idx < SymbolSepar; Idx++) { + Buffer[Idx] = OutTap[TapPtr]; + OutTap[TapPtr] = 0; + TapPtr += 1; + TapPtr &= WrapMask; + } + return SymbolSepar; + } private: - void AddSymbol(int Freq, int Phase) { - size_t Time; - for (Time = 0; Time < SymbolLen; Time++) { + void AddSymbol(int Freq, int Phase) { + size_t Time; + for (Time = 0; Time < SymbolLen; Time++) { // experimental use with {1.0, 1.0}; - Type Shape=1.0-CosineTable[Time]; - OutTap[TapPtr] += CosineTable[Phase] * Shape; - -// OutTap[TapPtr] += CosineTable[Phase] * SymbolShape[Time]; + Type Shape=1.0-CosineTable[Time]; + OutTap[TapPtr] += CosineTable[Phase] * Shape; +// OutTap[TapPtr] += CosineTable[Phase] * SymbolShape[Time]; - Phase += Freq; - Phase &= WrapMask; - TapPtr += 1; - TapPtr &= WrapMask; - } - } + Phase += Freq; + Phase &= WrapMask; + TapPtr += 1; + TapPtr &= WrapMask; + } + } } ; @@ -624,47 +243,47 @@ template class BoxFilter { public: - size_t Len; - TapType *Tap; - size_t Ptr; - OutType Output; + size_t Len; + TapType *Tap; + size_t Ptr; + OutType Output; - BoxFilter() { - Tap = 0; - } + BoxFilter() { + Tap = 0; + } - ~BoxFilter() { - free(Tap); - } + ~BoxFilter() { + free(Tap); + } - void Free(void) { - free(Tap); - Tap = 0; - } + void Free(void) { + free(Tap); + Tap = 0; + } - int Preset(void) { - if (ReallocArray(&Tap, Len) < 0) - return -1; - Clear(); - return 0; - } + int Preset(void) { + if (ReallocArray(&Tap, Len) < 0) + return -1; + Clear(); + return 0; + } - void Clear(void) { - size_t Idx; - for (Idx = 0; Idx < Len; Idx++) - Tap[Idx] = 0; - Ptr = 0; - Output = 0; - } + void Clear(void) { + size_t Idx; + for (Idx = 0; Idx < Len; Idx++) + Tap[Idx] = 0; + Ptr = 0; + Output = 0; + } - template - void Process(InpType Input) { - Output -= Tap[Ptr]; - Output += Input; - Tap[Ptr] = Input; - Ptr += 1; - if (Ptr >= Len) Ptr -= Len; - } + template + void Process(InpType Input) { + Output -= Tap[Ptr]; + Output += Input; + Tap[Ptr] = Input; + Ptr += 1; + if (Ptr >= Len) Ptr -= Len; + } }; @@ -674,307 +293,307 @@ template class MFSK_InputProcessor { public: - size_t WindowLen; - size_t WrapMask; - Type *InpTap; - size_t InpTapPtr; - Type *OutTap; - size_t OutTapPtr; - Type *WindowShape; - size_t SliceSepar; + size_t WindowLen; + size_t WrapMask; + Type *InpTap; + size_t InpTapPtr; + Type *OutTap; + size_t OutTapPtr; + Type *WindowShape; + size_t SliceSepar; - r2FFT< Cmpx > FFT; // FFT engine - Cmpx *FFT_Buff; // FFT buffer + r2FFT< Cmpx > FFT; // FFT engine + Cmpx *FFT_Buff; // FFT buffer - size_t SpectraLen; - Cmpx *Spectra[2]; + size_t SpectraLen; + Cmpx *Spectra[2]; - Type *Output; - Type *Energy; + Type *Output; + Type *Energy; - BoxFilter Filter; + BoxFilter Filter; public: - MFSK_InputProcessor() { - Init(); - Default(); - } + MFSK_InputProcessor() { + Init(); + Default(); + } - ~MFSK_InputProcessor() { - Free(); - } - - void Init(void) { - InpTap = 0; - OutTap = 0; - WindowShape = 0; - FFT_Buff = 0; - Spectra[0] = 0; - Spectra[1] = 0; - Output = 0; - Energy = 0; - } + ~MFSK_InputProcessor() { + Free(); + } + + void Init(void) { + InpTap = 0; + OutTap = 0; + WindowShape = 0; + FFT_Buff = 0; + Spectra[0] = 0; + Spectra[1] = 0; + Output = 0; + Energy = 0; + } - void Free(void) { - free(InpTap); InpTap = 0; - free(OutTap); OutTap = 0; - free(WindowShape); WindowShape = 0; - free(FFT_Buff); FFT_Buff = 0; - free(Spectra[0]); Spectra[0] = 0; - free(Spectra[1]); Spectra[1] = 0; - free(Output); Output = 0; - free(Energy); Energy = 0; - FFT.Free(); - Filter.Free(); - } + void Free(void) { + free(InpTap); InpTap = 0; + free(OutTap); OutTap = 0; + free(WindowShape); WindowShape = 0; + free(FFT_Buff); FFT_Buff = 0; + free(Spectra[0]); Spectra[0] = 0; + free(Spectra[1]); Spectra[1] = 0; + free(Output); Output = 0; + free(Energy); Energy = 0; + FFT.Free(); + Filter.Free(); + } - void Default(void) { - WindowLen = 8192; - } - - int Preset(void) { - size_t Idx; - WrapMask = WindowLen - 1; - Type ShapeScale = 2.0 / WindowLen; + void Default(void) { + WindowLen = 8192; + } + + int Preset(void) { + size_t Idx; + WrapMask = WindowLen - 1; + Type ShapeScale = 2.0 / WindowLen; - if (ReallocArray(&InpTap, WindowLen) < 0) goto Error; - ClearArray(InpTap, WindowLen); - InpTapPtr = 0; - if (ReallocArray(&OutTap, WindowLen) < 0) goto Error; - ClearArray(OutTap, WindowLen); - OutTapPtr = 0; + if (ReallocArray(&InpTap, WindowLen) < 0) goto Error; + ClearArray(InpTap, WindowLen); + InpTapPtr = 0; + if (ReallocArray(&OutTap, WindowLen) < 0) goto Error; + ClearArray(OutTap, WindowLen); + OutTapPtr = 0; - if (FFT.Preset(WindowLen) < 0) goto Error; - if (ReallocArray(&FFT_Buff, WindowLen) < 0) goto Error; - SliceSepar = WindowLen / 2; + if (FFT.Preset(WindowLen) < 0) goto Error; + if (ReallocArray(&FFT_Buff, WindowLen) < 0) goto Error; + SliceSepar = WindowLen / 2; - if (ReallocArray(&WindowShape, WindowLen)< 0) goto Error; - for (Idx = 0; Idx < WindowLen; Idx++) - WindowShape[Idx] = ShapeScale * sqrt(1.0 - FFT.Twiddle[Idx].Re); + if (ReallocArray(&WindowShape, WindowLen)< 0) goto Error; + for (Idx = 0; Idx < WindowLen; Idx++) + WindowShape[Idx] = ShapeScale * sqrt(1.0 - FFT.Twiddle[Idx].Re); - SpectraLen = WindowLen / 2; - if (ReallocArray(&Spectra[0], SpectraLen) < 0) goto Error; - if (ReallocArray(&Spectra[1], SpectraLen) < 0) goto Error; + SpectraLen = WindowLen / 2; + if (ReallocArray(&Spectra[0], SpectraLen) < 0) goto Error; + if (ReallocArray(&Spectra[1], SpectraLen) < 0) goto Error; - if (ReallocArray(&Output, WindowLen) < 0) goto Error; - ClearArray(Output, WindowLen); + if (ReallocArray(&Output, WindowLen) < 0) goto Error; + ClearArray(Output, WindowLen); - if (ReallocArray(&Energy, SpectraLen) < 0) goto Error; + if (ReallocArray(&Energy, SpectraLen) < 0) goto Error; - Filter.Len = WindowLen / 16; - if (Filter.Preset() < 0) goto Error; + Filter.Len = WindowLen / 16; + if (Filter.Preset() < 0) goto Error; - return 0; - - Error: - Free(); - return -1; - } + return 0; + + Error: + Free(); + return -1; + } - void Reset(void) { - ClearArray(InpTap, WindowLen); - InpTapPtr = 0; - ClearArray(OutTap, WindowLen); - OutTapPtr = 0; - } + void Reset(void) { + ClearArray(InpTap, WindowLen); + InpTapPtr = 0; + ClearArray(OutTap, WindowLen); + OutTapPtr = 0; + } - void LimitSpectraPeaks( Cmpx *Spectra, - size_t Len = 64, - Type Threshold = 4.0) { - Filter.Len = Len; - Filter.Preset(); + void LimitSpectraPeaks( Cmpx *Spectra, + size_t Len = 64, + Type Threshold = 4.0) { + Filter.Len = Len; + Filter.Preset(); - size_t MaxFreq = 3 * (SpectraLen / 4); - size_t Freq, Idx; + size_t MaxFreq = 3 * (SpectraLen / 4); + size_t Freq, Idx; - for (Freq = 0; Freq < Len; Freq++) - Filter.Process(Energy[Freq]); + for (Freq = 0; Freq < Len; Freq++) + Filter.Process(Energy[Freq]); - for (Idx = Len / 2; Freq < MaxFreq; Freq++,Idx++) { - Filter.Process(Energy[Freq]); - Type Signal = Energy[Idx]; - Type Limit = (Filter.Output/Len) * Threshold; - if (Signal > Limit) { - Spectra[Idx] *= sqrt(Limit / Signal); - Energy[Idx] = Limit; - } - } - } + for (Idx = Len / 2; Freq < MaxFreq; Freq++,Idx++) { + Filter.Process(Energy[Freq]); + Type Signal = Energy[Idx]; + Type Limit = (Filter.Output/Len) * Threshold; + if (Signal > Limit) { + Spectra[Idx] *= sqrt(Limit / Signal); + Energy[Idx] = Limit; + } + } + } - void LimitOutputPeaks(Type Threshold = 2.5) { - size_t Idx; - Type RMS = 0; - for (Idx = 0; Idx < WindowLen; Idx++) { - Type Signal = Output[Idx]; - RMS += Signal * Signal; - } - RMS = sqrt(RMS / WindowLen); - Type Limit = RMS * Threshold; + void LimitOutputPeaks(Type Threshold = 2.5) { + size_t Idx; + Type RMS = 0; + for (Idx = 0; Idx < WindowLen; Idx++) { + Type Signal = Output[Idx]; + RMS += Signal * Signal; + } + RMS = sqrt(RMS / WindowLen); + Type Limit = RMS * Threshold; - for (Idx = 0; Idx < WindowLen; Idx++) { - Type Signal = Output[Idx]; - if (Signal > Limit) - Output[Idx] = Limit; - else if (Signal < (-Limit)) - Output[Idx] = (-Limit); - } - } + for (Idx = 0; Idx < WindowLen; Idx++) { + Type Signal = Output[Idx]; + if (Signal > Limit) + Output[Idx] = Limit; + else if (Signal < (-Limit)) + Output[Idx] = (-Limit); + } + } - void AverageEnergy(size_t Len = 32) { - Filter.Len = Len; - Filter.Preset(); + void AverageEnergy(size_t Len = 32) { + Filter.Len = Len; + Filter.Preset(); - size_t MaxFreq = 3 * (SpectraLen / 4); - Type Scale = 1.0 / Len; - size_t Len2 = Len / 2; - size_t Idx, Freq; + size_t MaxFreq = 3 * (SpectraLen / 4); + Type Scale = 1.0 / Len; + size_t Len2 = Len / 2; + size_t Idx, Freq; - for (Freq = 0; Freq < Len; Freq++) - Filter.Process(Energy[Freq]); + for (Freq = 0; Freq < Len; Freq++) + Filter.Process(Energy[Freq]); - for (Idx = 0; Idx < Len2; Idx++) - Energy[Idx] = Filter.Output * Scale; + for (Idx = 0; Idx < Len2; Idx++) + Energy[Idx] = Filter.Output * Scale; - for ( ; Freq < MaxFreq; Freq++,Idx++) { - Filter.Process(Energy[Freq]); - Energy[Idx] = Filter.Output * Scale; - } + for ( ; Freq < MaxFreq; Freq++,Idx++) { + Filter.Process(Energy[Freq]); + Energy[Idx] = Filter.Output * Scale; + } - for ( ; Idx < SpectraLen; Idx++) - Energy[Idx] = Filter.Output*Scale; - } + for ( ; Idx < SpectraLen; Idx++) + Energy[Idx] = Filter.Output*Scale; + } // here we process the spectral data - void ProcessSpectra(Cmpx *Spectra) { - size_t Freq; - for (Freq = 0; Freq < SpectraLen; Freq++) - Energy[Freq] = Spectra[Freq].Energy(); + void ProcessSpectra(Cmpx *Spectra) { + size_t Freq; + for (Freq = 0; Freq < SpectraLen; Freq++) + Energy[Freq] = Spectra[Freq].Energy(); - LimitSpectraPeaks(Spectra, WindowLen / 64, 4.0); - LimitSpectraPeaks(Spectra, WindowLen / 64, 4.0); - LimitSpectraPeaks(Spectra, WindowLen / 64, 4.0); + LimitSpectraPeaks(Spectra, WindowLen / 64, 4.0); + LimitSpectraPeaks(Spectra, WindowLen / 64, 4.0); + LimitSpectraPeaks(Spectra, WindowLen / 64, 4.0); - AverageEnergy(WindowLen / 96); - AverageEnergy(WindowLen / 64); + AverageEnergy(WindowLen / 96); + AverageEnergy(WindowLen / 64); - for (Freq = 0; Freq < SpectraLen; Freq++) { - Type Corr = Energy[Freq]; - if (Corr <= 0) continue; - Corr = 1.0 / sqrt(Corr); - Spectra[Freq] *= Corr; - } - } + for (Freq = 0; Freq < SpectraLen; Freq++) { + Type Corr = Energy[Freq]; + if (Corr <= 0) continue; + Corr = 1.0 / sqrt(Corr); + Spectra[Freq] *= Corr; + } + } - template - void ProcessInpTap(InpType *Input) { - size_t InpIdx; - for (InpIdx = 0; InpIdx < SliceSepar; InpIdx++) { - InpTap[InpTapPtr] = Input[InpIdx]; - InpTapPtr += 1; - InpTapPtr &= WrapMask; - } - } + template + void ProcessInpTap(InpType *Input) { + size_t InpIdx; + for (InpIdx = 0; InpIdx < SliceSepar; InpIdx++) { + InpTap[InpTapPtr] = Input[InpIdx]; + InpTapPtr += 1; + InpTapPtr &= WrapMask; + } + } - void ProcessInpTap() { - size_t InpIdx; - for (InpIdx = 0; InpIdx < SliceSepar; InpIdx++) { - InpTap[InpTapPtr] = 0; - InpTapPtr += 1; - InpTapPtr &= WrapMask; - } - } + void ProcessInpTap() { + size_t InpIdx; + for (InpIdx = 0; InpIdx < SliceSepar; InpIdx++) { + InpTap[InpTapPtr] = 0; + InpTapPtr += 1; + InpTapPtr &= WrapMask; + } + } - void ProcessInpWindow_Re(void) { - size_t Time; - for (Time = 0; Time < WindowLen; Time++) { - FFT_Buff[Time].Re = InpTap[InpTapPtr] * WindowShape[Time]; - InpTapPtr += 1; - InpTapPtr &= WrapMask; - } - } + void ProcessInpWindow_Re(void) { + size_t Time; + for (Time = 0; Time < WindowLen; Time++) { + FFT_Buff[Time].Re = InpTap[InpTapPtr] * WindowShape[Time]; + InpTapPtr += 1; + InpTapPtr &= WrapMask; + } + } - void ProcessInpWindow_Im(void) { - size_t Time; - for (Time = 0; Time < WindowLen; Time++) { - FFT_Buff[Time].Im = InpTap[InpTapPtr] * WindowShape[Time]; - InpTapPtr += 1; - InpTapPtr &= WrapMask; - } - } + void ProcessInpWindow_Im(void) { + size_t Time; + for (Time = 0; Time < WindowLen; Time++) { + FFT_Buff[Time].Im = InpTap[InpTapPtr] * WindowShape[Time]; + InpTapPtr += 1; + InpTapPtr &= WrapMask; + } + } - void ProcessOutWindow_Re(void) - { size_t Time; - for (Time=0; Time - int Process(InpType *Input) { + template + int Process(InpType *Input) { - if (Input) ProcessInpTap(Input); - else ProcessInpTap(); - ProcessInpWindow_Re(); + if (Input) ProcessInpTap(Input); + else ProcessInpTap(); + ProcessInpWindow_Re(); - if (Input) ProcessInpTap(Input+SliceSepar); - else ProcessInpTap(); - ProcessInpWindow_Im(); + if (Input) ProcessInpTap(Input+SliceSepar); + else ProcessInpTap(); + ProcessInpWindow_Im(); - FFT.Process(FFT_Buff); - FFT.SeparTwoReals(FFT_Buff, Spectra[0], Spectra[1]); + FFT.Process(FFT_Buff); + FFT.SeparTwoReals(FFT_Buff, Spectra[0], Spectra[1]); - ProcessSpectra(Spectra[0]); - ProcessSpectra(Spectra[1]); + ProcessSpectra(Spectra[0]); + ProcessSpectra(Spectra[1]); - FFT.JoinTwoReals(Spectra[0], Spectra[1], FFT_Buff); - FFT.Process(FFT_Buff); + FFT.JoinTwoReals(Spectra[0], Spectra[1], FFT_Buff); + FFT.Process(FFT_Buff); - ProcessOutWindow_Re(); - ProcessOutTap(Output); - ProcessOutWindow_Im(); - ProcessOutTap(Output+SliceSepar); + ProcessOutWindow_Re(); + ProcessOutTap(Output); + ProcessOutWindow_Im(); + ProcessOutTap(Output+SliceSepar); - LimitOutputPeaks(2.5); - LimitOutputPeaks(2.5); + LimitOutputPeaks(2.5); + LimitOutputPeaks(2.5); - return WindowLen; - } + return WindowLen; + } // get output as 16-bit signed data - int GetOutput(int16_t *Buffer) { - const Type Scale = 32768.0; - const int32_t Limit = 0x7FFF; - size_t Idx; + int GetOutput(int16_t *Buffer) { + const Type Scale = 32768.0; + const int32_t Limit = 0x7FFF; + size_t Idx; - for (Idx = 0; Idx < WindowLen; Idx++) { - Type Ampl = Output[Idx]; - Ampl *= Scale; - int32_t Out = (int32_t)floor(Ampl + 0.5); - if (Out > Limit) Out = Limit; - else if (Out < (-Limit)) Out = (-Limit); - Buffer[Idx] = (int16_t)Out; - } - return WindowLen; - } + for (Idx = 0; Idx < WindowLen; Idx++) { + Type Ampl = Output[Idx]; + Ampl *= Scale; + int32_t Out = (int32_t)floor(Ampl + 0.5); + if (Out > Limit) Out = Limit; + else if (Out < (-Limit)) Out = (-Limit); + Buffer[Idx] = (int16_t)Out; + } + return WindowLen; + } } ; @@ -987,97 +606,97 @@ template class CircularBuffer { public: - size_t Width; // input/output data width (row width) - size_t Len; // buffer length (column height) - size_t Size; // total size of the storage in the buffer - size_t Ptr; // current pointer (counts rows) - Type *Data; // allocated storage + size_t Width; // input/output data width (row width) + size_t Len; // buffer length (column height) + size_t Size; // total size of the storage in the buffer + size_t Ptr; // current pointer (counts rows) + Type *Data; // allocated storage - CircularBuffer() { Init(); } - ~CircularBuffer() { free(Data); } + CircularBuffer() { Init(); } + ~CircularBuffer() { free(Data); } - void Init(void) - { Data = 0; Size = 0; Width = 1; } + void Init(void) + { Data = 0; Size = 0; Width = 1; } - void Free(void) - { free(Data); Data = 0; Size = 0; } + void Free(void) + { free(Data); Data = 0; Size = 0; } // reset: set pointer to the beginning of the buffer - void Reset(void) { Ptr = 0; } + void Reset(void) { Ptr = 0; } // preset for given length and width - int Preset(void) { - Size = Width * Len; - if (ReallocArray(&Data, Size) < 0) return -1; - Reset(); - return 0; - } + int Preset(void) { + Size = Width * Len; + if (ReallocArray(&Data, Size) < 0) return -1; + Reset(); + return 0; + } // set all elements to given value - void Set(Type &Value) { - size_t Idx; - for (Idx = 0; Idx < Size; Idx++) - Data[Idx]=Value; - } + void Set(Type &Value) { + size_t Idx; + for (Idx = 0; Idx < Size; Idx++) + Data[Idx]=Value; + } // set all elements to zero - void Clear(void) { - Type Zero; - Zero = 0; - Set(Zero); - } + void Clear(void) { + Type Zero; + Zero = 0; + Set(Zero); + } // increment the pointer (with wrapping around) - void IncrPtr(size_t &Ptr, size_t Step=1) { - Ptr += Step; - if (Ptr >= Len) Ptr -= Len; - } + void IncrPtr(size_t &Ptr, size_t Step=1) { + Ptr += Step; + if (Ptr >= Len) Ptr -= Len; + } // decrement the pointer (with wrapping around) - void DecrPtr(size_t &Ptr, size_t Step=1) { - if (Ptr >= Step) Ptr -= Step; - else Ptr += (Len - Step); - } + void DecrPtr(size_t &Ptr, size_t Step=1) { + if (Ptr >= Step) Ptr -= Step; + else Ptr += (Len - Step); + } // synchronize current pointer with another circular buffer - template - void operator |= (CircularBuffer Buffer) { - Ptr = Buffer.Ptr; - } + template + void operator |= (CircularBuffer Buffer) { + Ptr = Buffer.Ptr; + } // advance (increment) current pointer - void operator += (size_t Step) { - IncrPtr(Ptr, Step); - } + void operator += (size_t Step) { + IncrPtr(Ptr, Step); + } // decrement current pointer - void operator -= (size_t Step) { - DecrPtr(Ptr, Step); - } + void operator -= (size_t Step) { + DecrPtr(Ptr, Step); + } // index operator to get the absolute data pointer - Type *operator [] (size_t Idx) { - return Data + (Idx * Width); - } + Type *operator [] (size_t Idx) { + return Data + (Idx * Width); + } // get storage pointer corresponding to an absolute pipe pointer - Type *AbsPtr(size_t Ptr) { - return Data + (Ptr * Width); - } + Type *AbsPtr(size_t Ptr) { + return Data + (Ptr * Width); + } // get storage pointer corresponding to current pipe pointer - Type *CurrPtr(void) { - return Data + (Ptr * Width); - } + Type *CurrPtr(void) { + return Data + (Ptr * Width); + } // get storage pointer corresponding to current pointer +/- offset - Type *OffsetPtr(int Offset) { - Offset += Ptr; - Offset *= Width; - if (Offset < 0) Offset += Size; - else if (Offset >= (int)Size) Offset -= Size; - return Data + Offset; - } + Type *OffsetPtr(int Offset) { + Offset += Ptr; + Offset *= Width; + if (Offset < 0) Offset += Size; + else if (Offset >= (int)Size) Offset -= Size; + return Data + Offset; + } }; // ===================================================================== @@ -1086,343 +705,343 @@ template class MFSK_Demodulator { public: - // parameters to be set before calling Preset() - size_t SymbolLen; // length of the symbol, must be a power of 2 - size_t FirstCarrier; // first carrier in terms of FFT freq. bins - size_t BitsPerSymbol; // bits per symbol => number of carriers/tones - int UseGrayCode; - size_t DecodeMargin; - int EqualizerDepth; // leave this at 0 (disable the equalizer) - int Reverse; + // parameters to be set before calling Preset() + size_t SymbolLen; // length of the symbol, must be a power of 2 + size_t FirstCarrier; // first carrier in terms of FFT freq. bins + size_t BitsPerSymbol; // bits per symbol => number of carriers/tones + int UseGrayCode; + size_t DecodeMargin; + int EqualizerDepth; // leave this at 0 (disable the equalizer) + int Reverse; - static const size_t CarrierSepar=2; - static const size_t SpectraPerSymbol = 2; // FFT slices per symbol + static const size_t CarrierSepar=2; + static const size_t SpectraPerSymbol = 2; // FFT slices per symbol - public: + public: - size_t Carriers; // number of tones/carriers - size_t SymbolSepar; // time distance between symbols + size_t Carriers; // number of tones/carriers + size_t SymbolSepar; // time distance between symbols - private: + private: - size_t DecodeWidth; + size_t DecodeWidth; - size_t SymbolSepar2; + size_t SymbolSepar2; - size_t WrapMask; + size_t WrapMask; - Type *InpTap; // input buffer - size_t InpTapPtr; + Type *InpTap; // input buffer + size_t InpTapPtr; - Type *SymbolShape; // the shape of the symbol and the FFT window + Type *SymbolShape; // the shape of the symbol and the FFT window - r2FFT< Cmpx > FFT; // FFT engine - Cmpx *FFT_Buff; // FFT buffer + r2FFT< Cmpx > FFT; // FFT engine + Cmpx *FFT_Buff; // FFT buffer - size_t SpectraLen; // number of spectra points per FFT - Cmpx *Spectra[SpectraPerSymbol]; // two buffers for FFT spectra - Type *Energy[SpectraPerSymbol]; + size_t SpectraLen; // number of spectra points per FFT + Cmpx *Spectra[SpectraPerSymbol]; // two buffers for FFT spectra + Type *Energy[SpectraPerSymbol]; - CircularBuffer EnergyBuffer; - LowPass3_Filter *AverageEnergy; - Type FilterWeight; + CircularBuffer EnergyBuffer; + LowPass3_Filter *AverageEnergy; + Type FilterWeight; - public: + public: - MFSK_Demodulator() - { Init(); - Default(); } + MFSK_Demodulator() + { Init(); + Default(); } - ~MFSK_Demodulator() - { Free(); } - - void Init(void) - { InpTap=0; - SymbolShape=0; - FFT_Buff=0; - Spectra[0]=0; - Spectra[1]=0; - Energy[0]=0; - Energy[1]=0; - AverageEnergy=0; } + ~MFSK_Demodulator() + { Free(); } + + void Init(void) + { InpTap=0; + SymbolShape=0; + FFT_Buff=0; + Spectra[0]=0; + Spectra[1]=0; + Energy[0]=0; + Energy[1]=0; + AverageEnergy=0; } - void Free(void) - { free(InpTap); InpTap=0; - free(SymbolShape); SymbolShape=0; - free(FFT_Buff); FFT_Buff=0; - free(Spectra[0]); Spectra[0]=0; - free(Spectra[1]); Spectra[1]=0; - free(Energy[0]); Energy[0]=0; - free(Energy[1]); Energy[1]=0; - free(AverageEnergy); AverageEnergy=0; - FFT.Free(); - EnergyBuffer.Free(); } + void Free(void) + { free(InpTap); InpTap=0; + free(SymbolShape); SymbolShape=0; + free(FFT_Buff); FFT_Buff=0; + free(Spectra[0]); Spectra[0]=0; + free(Spectra[1]); Spectra[1]=0; + free(Energy[0]); Energy[0]=0; + free(Energy[1]); Energy[1]=0; + free(AverageEnergy); AverageEnergy=0; + FFT.Free(); + EnergyBuffer.Free(); } - void Default(void) - { SymbolLen=512; - FirstCarrier=32; - BitsPerSymbol=5; - UseGrayCode=1; - DecodeMargin=32; - Reverse=0; - EqualizerDepth=0; } + void Default(void) + { SymbolLen=512; + FirstCarrier=32; + BitsPerSymbol=5; + UseGrayCode=1; + DecodeMargin=32; + Reverse=0; + EqualizerDepth=0; } - int Preset(void) - { + int Preset(void) + { - Carriers=Exp2(BitsPerSymbol); + Carriers=Exp2(BitsPerSymbol); - WrapMask=SymbolLen-1; + WrapMask=SymbolLen-1; - Type ShapeScale=1.0/SymbolLen; + Type ShapeScale=1.0/SymbolLen; - if (ReallocArray(&InpTap,SymbolLen)<0) goto Error; - ClearArray(InpTap,SymbolLen); - InpTapPtr=0; + if (ReallocArray(&InpTap,SymbolLen)<0) goto Error; + ClearArray(InpTap,SymbolLen); + InpTapPtr=0; - if (FFT.Preset(SymbolLen)<0) goto Error; - if (ReallocArray(&FFT_Buff,SymbolLen)<0) goto Error; - SymbolSepar=SymbolLen/2; - SymbolSepar2=SymbolSepar/2; + if (FFT.Preset(SymbolLen)<0) goto Error; + if (ReallocArray(&FFT_Buff,SymbolLen)<0) goto Error; + SymbolSepar=SymbolLen/2; + SymbolSepar2=SymbolSepar/2; - if (ReallocArray(&SymbolShape,SymbolLen)<0) goto Error; + if (ReallocArray(&SymbolShape,SymbolLen)<0) goto Error; - { size_t Time; - double Ampl=MFSK_SymbolFreqShape[0]; - for (Time=0; Time=SymbolLen) Phase-=SymbolLen; } - } - { size_t Time; - for (Time=0; Time=SymbolLen) Phase-=SymbolLen; } + } + { size_t Time; + for (Time=0; TimeFirstCarrier) DecodeMargin=FirstCarrier; - DecodeWidth=(Carriers*CarrierSepar-1)+2*DecodeMargin; + if (DecodeMargin>FirstCarrier) DecodeMargin=FirstCarrier; + DecodeWidth=(Carriers*CarrierSepar-1)+2*DecodeMargin; - if (ReallocArray(&Energy[0],DecodeWidth)<0) goto Error; - if (ReallocArray(&Energy[1],DecodeWidth)<0) goto Error; + if (ReallocArray(&Energy[0],DecodeWidth)<0) goto Error; + if (ReallocArray(&Energy[1],DecodeWidth)<0) goto Error; - if (EqualizerDepth) - { EnergyBuffer.Len=EqualizerDepth; - EnergyBuffer.Width=DecodeWidth; - if (EnergyBuffer.Preset()<0) goto Error; - EnergyBuffer.Clear(); - if (ReallocArray(&AverageEnergy,DecodeWidth)<0) goto Error; - size_t Idx; - for (Idx=0; Idx - void Process(InpType *Input) - { size_t InpIdx,Time; + template + void Process(InpType *Input) + { size_t InpIdx,Time; - for (InpIdx=0; InpIdx0) - { Energy[0][Idx]/=RefEnergy; - Energy[1][Idx]/=RefEnergy; } - else - { Energy[0][Idx]=0; - Energy[1][Idx]=0; } - } -*/ - EnergyBuffer+=2; - } - else - { size_t Idx; - if (Reverse==1) { - size_t Freq=FirstCarrier; - for (Idx=0; Idx0) + { Energy[0][Idx]/=RefEnergy; + Energy[1][Idx]/=RefEnergy; } + else + { Energy[0][Idx]=0; + Energy[1][Idx]=0; } + } +*/ + EnergyBuffer+=2; + } + else + { size_t Idx; + if (Reverse==1) { + size_t Freq=FirstCarrier; + for (Idx=0; IdxPeak) - { Peak=Energy; PeakIdx=Idx; } - Freq+=CarrierSepar; } + uint8_t HardDecode(size_t Slice=0, int FreqOffset=0) + { size_t Idx; + Type Peak=0; + uint8_t PeakIdx=0; + Type *EnergyPtr=Energy[Slice]+(DecodeMargin+FreqOffset); + size_t Freq=0; + for (Idx=0; IdxPeak) + { Peak=Energy; PeakIdx=Idx; } + Freq+=CarrierSepar; } - if (UseGrayCode) PeakIdx=BinaryCode(PeakIdx); - return PeakIdx; } + if (UseGrayCode) PeakIdx=BinaryCode(PeakIdx); + return PeakIdx; } - template - void SoftDecode(SymbType *Symbol, - size_t Slice=0, int FreqOffset=0) - { size_t Bit,Idx; - for (Bit=0; Bit + void SoftDecode(SymbType *Symbol, + size_t Slice=0, int FreqOffset=0) + { size_t Bit,Idx; + for (Bit=0; Bit0) - { for (Bit=0; Bit0) + { for (Bit=0; Bit - void SoftDecode_Test(SymbType *Symbol, - size_t Slice=0, int FreqOffset=0) - { size_t Bit,Idx,Freq; + template + void SoftDecode_Test(SymbType *Symbol, + size_t Slice=0, int FreqOffset=0) + { size_t Bit,Idx,Freq; - Type *EnergyPtr=Energy[Slice]+(DecodeMargin+FreqOffset); + Type *EnergyPtr=Energy[Slice]+(DecodeMargin+FreqOffset); -// printf("SoftDecode:"); +// printf("SoftDecode:"); - Type TotalEnergy=0; - Type PeakEnergy=0; - for (Freq=0,Idx=0; IdxPeakEnergy) PeakEnergy=Energy; } - Type AverageNoise=(TotalEnergy-PeakEnergy)/(Carriers-1)/2; + Type TotalEnergy=0; + Type PeakEnergy=0; + for (Freq=0,Idx=0; IdxPeakEnergy) PeakEnergy=Energy; } + Type AverageNoise=(TotalEnergy-PeakEnergy)/(Carriers-1)/2; /* - printf(" PeakEnergy/TotalEnergy=%4.3f",PeakEnergy/TotalEnergy); - printf(" AverageNoise/TotalEnergy=%4.3f\n",AverageNoise/TotalEnergy); - printf("Energy[%d]/Tot =",Carriers); - for (Freq=0,Idx=0; Idx template void PrintBinary(Type Number, size_t Bits) { Type Mask=1; Mask<<=(Bits-1); - for ( ; Bits; Bits--) - { printf("%c",Number&Mask ? '1':'0'); - Mask>>=1; } + for ( ; Bits; Bits--) + { printf("%c",Number&Mask ? '1':'0'); + Mask>>=1; } } // ===================================================================== @@ -1441,456 +1060,372 @@ template class MFSK_Encoder { public: - size_t BitsPerSymbol; - size_t BitsPerCharacter; + size_t BitsPerSymbol; + size_t BitsPerCharacter; - public: + public: - size_t Symbols; - size_t SymbolsPerBlock; + size_t Symbols; + size_t SymbolsPerBlock; - private: + bool bContestia; +// bool bRTTYM; - static const uint64_t ScramblingCode = 0xE257E6D0291574ECLL; + private: - int8_t *FHT_Buffer; + static const uint64_t ScramblingCodeOlivia = 0xE257E6D0291574ECLL; + static const uint64_t ScramblingCodeContestia = 0xEDB88320LL; +// static const uint64_t ScramblingCodeRTTYM = 0xEDB88320LL; + uint64_t ScramblingCode; - public: + int8_t *FHT_Buffer; - uint8_t *OutputBlock; + public: - public: + uint8_t *OutputBlock; - MFSK_Encoder() - { Init(); - Default(); } + public: - ~MFSK_Encoder() - { Free(); } + MFSK_Encoder() { + Default(); + Init(); + } - void Default(void) - { BitsPerSymbol=5; - BitsPerCharacter=7; } + ~MFSK_Encoder() + { Free(); } - void Init(void) - { FHT_Buffer=0; - OutputBlock=0; } + void Default(void) { + bContestia = false; + BitsPerSymbol=5; + BitsPerCharacter=7; + ScramblingCode = ScramblingCodeOlivia; + } - void Free(void) - { free(FHT_Buffer); FHT_Buffer=0; - free(OutputBlock); OutputBlock=0; } + void Init(void) + { FHT_Buffer=0; + OutputBlock=0; } - int Preset(void) - { Symbols = 1<= 'a' && Char <= 'z') + Char += 'A' - 'a'; + if (Char == ' ') + Char = 59; + else if (Char == '\r') + Char = 60; + else if (Char == '\n') + Char = 0; + else if (Char >= 33 && Char <= 90) + Char -= 32; + else if (Char == 8) + Char = 61; + else if (Char == 0) + Char = 0; + else + Char = '?' - 32; +// } else if (bRTTYM) { + } else { + Char &= Mask; + } + + for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) + FHT_Buffer[TimeBit] = 0; + if (Char=BitsPerSymbol) Bit-=BitsPerSymbol; - uint8_t Mask=1; Mask<<=Bit; - OutputBlock[TimeBit]|=Mask; } - Rotate+=1; if (Rotate>=BitsPerSymbol) Rotate-=BitsPerSymbol; } - } +// nShift = (bContestia || bRTTYM) ? 5 : 13; // Contestia/RTTYM or Olivia + nShift = (bContestia) ? 5 : 13; // Contestia/RTTYM or Olivia - } + for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit ++) + OutputBlock[TimeBit] = 0; - void PrintOutputBlock(void) - { size_t TimeBit; - for (TimeBit=0; TimeBit= BitsPerSymbol) Bit -= BitsPerSymbol; + uint8_t Mask = 1; + Mask <<= Bit; + OutputBlock[TimeBit] |= Mask; + } + Rotate += 1; + if (Rotate >= BitsPerSymbol) + Rotate -= BitsPerSymbol; + } + } + } + + void PrintOutputBlock(void) + { size_t TimeBit; + for (TimeBit=0; TimeBit>= Rotate; - Bit &= 1; - uint64_t CodeMask = 1; - CodeMask <<= CodeBit; - if (ScramblingCode&CodeMask) Bit ^= 1; - FHT_Buffer[TimeBit]= Bit ? -1 : 1; - CodeBit += 1; - CodeBit &= CodeWrap; - Rotate += 1; - if (Rotate >= BitsPerSymbol) Rotate -= BitsPerSymbol; - Ptr += 1; - Ptr &= InputWrap; - } - - FHT(FHT_Buffer,SymbolsPerBlock); - int32_t Peak = 0; - size_t PeakPos = 0; - int32_t SqrSum = 0; - for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { - int32_t Signal = FHT_Buffer[TimeBit]; - SqrSum += Signal * Signal; - if (abs(Signal) > abs(Peak)) { - Peak = Signal; - PeakPos = TimeBit; - } - } - - uint8_t Char = PeakPos; - if (Peak < 0) Char += SymbolsPerBlock; - SqrSum -= Peak * Peak; - - OutputBlock[FreqBit] = Char; - NoiseEnergy += (float)SqrSum / (SymbolsPerBlock - 1); - Signal += abs(Peak); - } - - void Process(void) { - size_t FreqBit; - Signal = 0; - NoiseEnergy = 0; - for (FreqBit=0; FreqBit 0; ) { - PackedBuffer <<= 8; - FreqBit--; - PackedBuffer |= OutputBlock[FreqBit]; - } - return BitsPerSymbol; - } - - size_t Output(uint64_t *PackedBuffer) { - return Output(*PackedBuffer); - } - - void PrintOutputBlock(FILE *File=stdout) - { size_t FreqBit; - fprintf(File,"'"); - for (FreqBit=0; FreqBit=' ')&&(Char<127) ? Char:' '); } - fprintf(File,"', S/N = %5.1f/%4.1f",Signal,sqrt(NoiseEnergy)); - if (NoiseEnergy>0) fprintf(File," = %5.1f",Signal/sqrt(NoiseEnergy)); - fprintf(File,"\n"); } - - void PrintInputBuffer(void) { - size_t TimeBit; - size_t Ptr = InputPtr; - for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { - printf("%2d: ",(int)TimeBit); - PrintBinary(InputBuffer[Ptr], BitsPerSymbol); - printf("\n"); - Ptr += 1; - Ptr &= InputWrap; - } - } - -}; - template class MFSK_SoftDecoder { public: - size_t BitsPerSymbol; // number of bits per symbol - size_t BitsPerCharacter; // number of bits per character - size_t Symbols; // number of symbols - size_t SymbolsPerBlock; // number of symbols per FEC block - float Signal, NoiseEnergy; - uint8_t *OutputBlock; + size_t BitsPerSymbol; // number of bits per symbol + size_t BitsPerCharacter; // number of bits per character + size_t Symbols; // number of symbols + size_t SymbolsPerBlock; // number of symbols per FEC block + float Signal, NoiseEnergy; + uint8_t *OutputBlock; + + bool bContestia; +// bool bRTTYM; private: + static const uint64_t ScramblingCodeOlivia = 0xE257E6D0291574ECLL; + static const uint64_t ScramblingCodeContestia = 0xEDB88320LL; +// static const uint64_t ScramblingCodeRTTYM = 0xEDB88320LL; + uint64_t ScramblingCode; - static const uint64_t ScramblingCode = 0xE257E6D0291574ECLL; - size_t InputBufferLen; - InpType *InputBuffer; - size_t InputPtr; + size_t InputBufferLen; + InpType *InputBuffer; + size_t InputPtr; - CalcType *FHT_Buffer; + CalcType *FHT_Buffer; public: - MFSK_SoftDecoder() { - Init(); - Default(); - } + MFSK_SoftDecoder() { + bContestia = false; + Init(); + Default(); + } - ~MFSK_SoftDecoder() { - Free(); - } + ~MFSK_SoftDecoder() { + Free(); + } - void Default(void) { - BitsPerSymbol = 5; - BitsPerCharacter = 7; - } + void Default(void) { + bContestia = false; + BitsPerSymbol = 5; + BitsPerCharacter = 7; + ScramblingCode = ScramblingCodeOlivia; + } - void Init(void) { - InputBuffer = 0; - FHT_Buffer = 0; - OutputBlock = 0; - } + void Init(void) { + InputBuffer = 0; + FHT_Buffer = 0; + OutputBlock = 0; + } - void Free(void) { - free(InputBuffer); - InputBuffer = 0; - free(FHT_Buffer); - FHT_Buffer = 0; - free(OutputBlock); - OutputBlock = 0; - } + void Free(void) { + free(InputBuffer); + InputBuffer = 0; + free(FHT_Buffer); + FHT_Buffer = 0; + free(OutputBlock); + OutputBlock = 0; + } - void Reset(void) { - size_t Idx; - for (Idx = 0; Idx < InputBufferLen; Idx++) - InputBuffer[Idx] = 0; - InputPtr = 0; - } + void Reset(void) { + size_t Idx; + for (Idx = 0; Idx < InputBufferLen; Idx++) + InputBuffer[Idx] = 0; + InputPtr = 0; + } - int Preset(void) { - Symbols = 1 << BitsPerSymbol; - SymbolsPerBlock = Exp2(BitsPerCharacter - 1); - InputBufferLen = SymbolsPerBlock * BitsPerSymbol; - if (ReallocArray(&InputBuffer, InputBufferLen) < 0) goto Error; - if (ReallocArray(&FHT_Buffer, SymbolsPerBlock) < 0) goto Error; - if (ReallocArray(&OutputBlock, BitsPerSymbol) < 0) goto Error; - Reset(); - return 0; - Error: - Free(); - return -1; - } + int Preset(void) { +// if (bRTTYM) { +// BitsPerCharacter = 5; +// ScramblingCode = ScramblingCodeRTTYM; +// } else + if (bContestia) { + BitsPerCharacter = 6; + ScramblingCode = ScramblingCodeContestia; + } else { + BitsPerCharacter = 7; + ScramblingCode = ScramblingCodeOlivia; + } - int Preset(MFSK_SoftDecoder &RefDecoder) { - BitsPerSymbol = RefDecoder.BitsPerSymbol; - BitsPerCharacter = RefDecoder.BitsPerCharacter; - return Preset(); - } + Symbols = 1 << BitsPerSymbol; + SymbolsPerBlock = Exp2(BitsPerCharacter - 1); + InputBufferLen = SymbolsPerBlock * BitsPerSymbol; + if (ReallocArray(&InputBuffer, InputBufferLen) < 0) goto Error; + if (ReallocArray(&FHT_Buffer, SymbolsPerBlock) < 0) goto Error; + if (ReallocArray(&OutputBlock, BitsPerSymbol) < 0) goto Error; + Reset(); + return 0; + Error: + Free(); + return -1; + } - void Input(InpType *Symbol) { - size_t FreqBit; - for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) { - InputBuffer[InputPtr] = Symbol[FreqBit]; - InputPtr += 1; - } - if (InputPtr >= InputBufferLen) InputPtr -= InputBufferLen; - } + int Preset(MFSK_SoftDecoder &RefDecoder) { + BitsPerSymbol = RefDecoder.BitsPerSymbol; +// BitsPerCharacter = RefDecoder.BitsPerCharacter; + return Preset(); + } - void DecodeCharacter(size_t FreqBit) { - size_t TimeBit; - size_t Ptr = InputPtr; - size_t Rotate = FreqBit; - size_t CodeWrap = (SymbolsPerBlock - 1); - size_t CodeBit = FreqBit * 13; - CodeBit &= CodeWrap; - for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { - InpType Bit = InputBuffer[Ptr + Rotate]; - uint64_t CodeMask = 1; - CodeMask <<= CodeBit; - if (ScramblingCode & CodeMask) Bit = (-Bit); - FHT_Buffer[TimeBit] = Bit; - CodeBit += 1; - CodeBit &= CodeWrap; - Rotate += 1; - if (Rotate >= BitsPerSymbol) Rotate -= BitsPerSymbol; - Ptr += BitsPerSymbol; - if (Ptr >= InputBufferLen) Ptr -= InputBufferLen; - } - FHT(FHT_Buffer, SymbolsPerBlock); - CalcType Peak = 0; - size_t PeakPos = 0; - CalcType SqrSum = 0; - for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { - CalcType Signal = FHT_Buffer[TimeBit]; - SqrSum += Signal * Signal; - if (fabs(Signal) > fabs(Peak)) { - Peak = Signal; - PeakPos = TimeBit; - } - } - uint8_t Char = PeakPos; - if (Peak < 0) Char += SymbolsPerBlock; - SqrSum -= Peak * Peak; - OutputBlock[FreqBit] = Char; - NoiseEnergy += (float)SqrSum / (SymbolsPerBlock - 1); - Signal += fabs(Peak); - } + void Input(InpType *Symbol) { + size_t FreqBit; + for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) { + InputBuffer[InputPtr] = Symbol[FreqBit]; + InputPtr += 1; + } + if (InputPtr >= InputBufferLen) InputPtr -= InputBufferLen; + } - void Process(void) { - size_t FreqBit; - Signal = 0; - NoiseEnergy = 0; - for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) - DecodeCharacter(FreqBit); - Signal /= BitsPerSymbol; - NoiseEnergy /= BitsPerSymbol; - } + void DecodeCharacter(size_t FreqBit) { + size_t TimeBit; + size_t Ptr = InputPtr; + size_t Rotate = FreqBit; + size_t CodeWrap = (SymbolsPerBlock - 1); + // Olivia (13 bit shift) or Contestia/RTTYM (5 bit shift) +// size_t nShift = (bContestia || bRTTYM) ? 5 : 13; + size_t nShift = (bContestia) ? 5 : 13; - size_t Output(uint8_t *Buffer) { - size_t FreqBit; - for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) - Buffer[FreqBit] = OutputBlock[FreqBit]; - return BitsPerSymbol; - } + size_t CodeBit = FreqBit * nShift; - size_t Output(uint64_t &PackedBuffer) { - size_t FreqBit; - PackedBuffer = 0; - for (FreqBit = BitsPerSymbol; FreqBit > 0; ) { - PackedBuffer <<= 8; - FreqBit--; - PackedBuffer |= OutputBlock[FreqBit]; - } - return BitsPerSymbol; - } + CodeBit &= CodeWrap; + for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { + InpType Bit = InputBuffer[Ptr + Rotate]; + uint64_t CodeMask = 1; + CodeMask <<= CodeBit; + if (ScramblingCode & CodeMask) Bit = (-Bit); + FHT_Buffer[TimeBit] = Bit; + CodeBit += 1; + CodeBit &= CodeWrap; + Rotate += 1; + if (Rotate >= BitsPerSymbol) Rotate -= BitsPerSymbol; + Ptr += BitsPerSymbol; + if (Ptr >= InputBufferLen) Ptr -= InputBufferLen; + } + FHT(FHT_Buffer, SymbolsPerBlock); + CalcType Peak = 0; + size_t PeakPos = 0; + CalcType SqrSum = 0; + for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { + CalcType Signal = FHT_Buffer[TimeBit]; + SqrSum += Signal * Signal; + if (fabs(Signal) > fabs(Peak)) { + Peak = Signal; + PeakPos = TimeBit; + } + } + uint8_t Char = PeakPos; + if (Peak < 0) Char += SymbolsPerBlock; + SqrSum -= Peak * Peak; - void PrintOutputBlock(FILE *File = stdout) { - size_t FreqBit; - fprintf(File, "'"); - for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) { - uint8_t Char = OutputBlock[FreqBit]; - fprintf(File, "%c", (Char >= ' ') && (Char < 127) ? Char:' '); - } - fprintf(File, "'"); - if (NoiseEnergy > 0) - fprintf(File, ", S/N = %5.1f", Signal / sqrt(NoiseEnergy)); - fprintf(File, "\n"); - } + if (bContestia && Char > 0) { + if (Char == 59) + Char = ' '; + else if (Char == 60) + Char = '\r'; + else if (Char == 61) + Char = 8; // backspace + else + Char += 32; + } + OutputBlock[FreqBit] = Char; - size_t Output(uint64_t *PackedBuffer) { - return Output(*PackedBuffer); - } + NoiseEnergy += (float)SqrSum / (SymbolsPerBlock - 1); + Signal += fabs(Peak); + } + + void Process(void) { + size_t FreqBit; + Signal = 0; + NoiseEnergy = 0; + for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) + DecodeCharacter(FreqBit); + Signal /= BitsPerSymbol; + NoiseEnergy /= BitsPerSymbol; + } + + size_t Output(uint8_t *Buffer) { + size_t FreqBit; + for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) + Buffer[FreqBit] = OutputBlock[FreqBit]; + return BitsPerSymbol; + } + + size_t Output(uint64_t &PackedBuffer) { + size_t FreqBit; + PackedBuffer = 0; + for (FreqBit = BitsPerSymbol; FreqBit > 0; ) { + PackedBuffer <<= 8; + FreqBit--; + PackedBuffer |= OutputBlock[FreqBit]; + } + return BitsPerSymbol; + } + + void PrintOutputBlock(FILE *File = stdout) { + size_t FreqBit; + fprintf(File, "'"); + for (FreqBit = 0; FreqBit < BitsPerSymbol; FreqBit++) { + uint8_t Char = OutputBlock[FreqBit]; + fprintf(File, "%c", (Char >= ' ') && (Char < 127) ? Char:' '); + } + fprintf(File, "'"); + if (NoiseEnergy > 0) + fprintf(File, ", S/N = %5.1f", Signal / sqrt(NoiseEnergy)); + fprintf(File, "\n"); + } + + size_t Output(uint64_t *PackedBuffer) { + return Output(*PackedBuffer); + } }; @@ -1902,179 +1437,179 @@ class RateConverter { public: // parameters to be set by the user - size_t TapLen; // filter tap length (in term of input samples) - size_t OverSampling; // internal oversampling factor - Type UpperFreq; // upper frequency of the (lowpass) filter (in terms of input sampling rate) - Type OutputRate; // the output rate (in terms of the input rate) + size_t TapLen; // filter tap length (in term of input samples) + size_t OverSampling; // internal oversampling factor + Type UpperFreq; // upper frequency of the (lowpass) filter (in terms of input sampling rate) + Type OutputRate; // the output rate (in terms of the input rate) private: - size_t FilterLen; // the total length of the filter (in term of oversampled rate) - Type *FilterShape; // the shape of the filter - Type *InputTap; // filter tap - size_t InputTapPtr; - size_t InputWrap; + size_t FilterLen; // the total length of the filter (in term of oversampled rate) + Type *FilterShape; // the shape of the filter + Type *InputTap; // filter tap + size_t InputTapPtr; + size_t InputWrap; - Type OutputTime; - Type OutputPeriod; - Type OutputBefore; - Type OutputAfter; - size_t OutputPtr; - + Type OutputTime; + Type OutputPeriod; + Type OutputBefore; + Type OutputAfter; + size_t OutputPtr; + public: - RateConverter() { - Init(); - Default(); - } - - ~RateConverter() { - Free(); - } + RateConverter() { + Init(); + Default(); + } + + ~RateConverter() { + Free(); + } - void Init(void) { - FilterShape = 0; - InputTap = 0; - } + void Init(void) { + FilterShape = 0; + InputTap = 0; + } - void Free(void) { - free(FilterShape); - FilterShape = 0; - free(InputTap); - InputTap = 0; - } + void Free(void) { + free(FilterShape); + FilterShape = 0; + free(InputTap); + InputTap = 0; + } - void Default(void) { - TapLen = 16; - OverSampling = 16; - UpperFreq = 3.0 / 8; - OutputRate = 1.0; - } + void Default(void) { + TapLen = 16; + OverSampling = 16; + UpperFreq = 3.0 / 8; + OutputRate = 1.0; + } - int Preset(void) { - size_t Idx; + int Preset(void) { + size_t Idx; - TapLen = Exp2(Log2(TapLen)); - FilterLen = TapLen * OverSampling; + TapLen = Exp2(Log2(TapLen)); + FilterLen = TapLen * OverSampling; - if ((ReallocArray(&FilterShape, FilterLen)) < 0) goto Error; - if ((ReallocArray(&InputTap, TapLen)) < 0) goto Error; + if ((ReallocArray(&FilterShape, FilterLen)) < 0) goto Error; + if ((ReallocArray(&InputTap, TapLen)) < 0) goto Error; - for (Idx = 0; Idx < FilterLen; Idx++) { - Type Phase = (M_PI * (2 * (int)Idx - (int)FilterLen)) / FilterLen; - // Hanning - // Type Window = 0.50 + 0.50 * cos(Phase); - // Blackman - // Type Window = 0.42 + 0.50 * cos(Phase) + 0.08 * cos(2 * Phase); - // Blackman-Harris - Type Window = 0.35875 + 0.48829 * cos(Phase) + - 0.14128 * cos(2 * Phase) + 0.01168 * cos(3 * Phase); - Type Filter = 1.0; - if (Phase != 0) { - Phase *= (UpperFreq * TapLen); - Filter = sin(Phase) / Phase; - } - // printf("%3d: %+9.6f %+9.6f %+9.6f\n", Idx, Window, Filter, Window*Filter); - FilterShape[Idx] = Window * Filter; - } - Reset(); - return 0; - Error: - Free(); - return -1; - } + for (Idx = 0; Idx < FilterLen; Idx++) { + Type Phase = (M_PI * (2 * (int)Idx - (int)FilterLen)) / FilterLen; + // Hanning + // Type Window = 0.50 + 0.50 * cos(Phase); + // Blackman + // Type Window = 0.42 + 0.50 * cos(Phase) + 0.08 * cos(2 * Phase); + // Blackman-Harris + Type Window = 0.35875 + 0.48829 * cos(Phase) + + 0.14128 * cos(2 * Phase) + 0.01168 * cos(3 * Phase); + Type Filter = 1.0; + if (Phase != 0) { + Phase *= (UpperFreq * TapLen); + Filter = sin(Phase) / Phase; + } + // printf("%3d: %+9.6f %+9.6f %+9.6f\n", Idx, Window, Filter, Window*Filter); + FilterShape[Idx] = Window * Filter; + } + Reset(); + return 0; + Error: + Free(); + return -1; + } - void Reset(void) { - size_t Idx; + void Reset(void) { + size_t Idx; - InputWrap = TapLen - 1; - for (Idx = 0; Idx < TapLen; Idx++) - InputTap[Idx] = 0; - InputTapPtr = 0; + InputWrap = TapLen - 1; + for (Idx = 0; Idx < TapLen; Idx++) + InputTap[Idx] = 0; + InputTapPtr = 0; - OutputTime = 0; - OutputPeriod = OverSampling / OutputRate; - OutputBefore = 0; - OutputAfter = 0; - OutputPtr = 0; - } + OutputTime = 0; + OutputPeriod = OverSampling / OutputRate; + OutputBefore = 0; + OutputAfter = 0; + OutputPtr = 0; + } private: - - Type Convolute(size_t Shift=0) { - Type Sum = 0; - Shift = (OverSampling - 1) - Shift; - size_t Idx = InputTapPtr; - for ( ; Shift < FilterLen; Shift += OverSampling) { - Sum += InputTap[Idx] * FilterShape[Shift]; - Idx += 1; - Idx &= InputWrap; - } - return Sum; - } + + Type Convolute(size_t Shift=0) { + Type Sum = 0; + Shift = (OverSampling - 1) - Shift; + size_t Idx = InputTapPtr; + for ( ; Shift < FilterLen; Shift += OverSampling) { + Sum += InputTap[Idx] * FilterShape[Shift]; + Idx += 1; + Idx &= InputWrap; + } + return Sum; + } - void NewInput(Type Input) - { // printf("I:\n"); - InputTap[InputTapPtr]=Input; - InputTapPtr+=1; InputTapPtr&=InputWrap; } + void NewInput(Type Input) + { // printf("I:\n"); + InputTap[InputTapPtr]=Input; + InputTapPtr+=1; InputTapPtr&=InputWrap; } public: - template - int Process(InpType *Input, size_t InputLen, OutType *Output) { - size_t OutputLen = 0; - // printf("E: %d %3.1f %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen); - for ( ; ; ) { - // printf("L: %d %3.1f %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen); - if (OutputPtr) { - size_t Idx = (size_t)floor(OutputTime) + 1; - if (Idx >= OverSampling) { - if (InputLen == 0) break; - NewInput(*Input); - Input++; - InputLen -= 1; - Idx -= OverSampling; - OutputTime -= (Type)OverSampling; - } - OutputAfter = Convolute(Idx); - Type Weight = Idx - OutputTime; - (*Output) = Weight * OutputBefore + (1.0 - Weight) * OutputAfter; - Output++; - OutputLen += 1; - // printf("O: %d %3.1f %d %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen, Idx); - OutputPtr = 0; - } else { - size_t Idx = (size_t)floor(OutputTime + OutputPeriod); - if (Idx >= OverSampling) { - if (InputLen == 0) break; - NewInput(*Input); - Input++; - InputLen -= 1; - Idx -= OverSampling; - OutputTime -= (Type)OverSampling; - } - OutputBefore = Convolute(Idx); - OutputTime += OutputPeriod; - OutputPtr = 1; - } - } - // printf("R: %d %3.1f %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen); - return OutputLen; - } + template + int Process(InpType *Input, size_t InputLen, OutType *Output) { + size_t OutputLen = 0; + // printf("E: %d %3.1f %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen); + for ( ; ; ) { + // printf("L: %d %3.1f %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen); + if (OutputPtr) { + size_t Idx = (size_t)floor(OutputTime) + 1; + if (Idx >= OverSampling) { + if (InputLen == 0) break; + NewInput(*Input); + Input++; + InputLen -= 1; + Idx -= OverSampling; + OutputTime -= (Type)OverSampling; + } + OutputAfter = Convolute(Idx); + Type Weight = Idx - OutputTime; + (*Output) = Weight * OutputBefore + (1.0 - Weight) * OutputAfter; + Output++; + OutputLen += 1; + // printf("O: %d %3.1f %d %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen, Idx); + OutputPtr = 0; + } else { + size_t Idx = (size_t)floor(OutputTime + OutputPeriod); + if (Idx >= OverSampling) { + if (InputLen == 0) break; + NewInput(*Input); + Input++; + InputLen -= 1; + Idx -= OverSampling; + OutputTime -= (Type)OverSampling; + } + OutputBefore = Convolute(Idx); + OutputTime += OutputPeriod; + OutputPtr = 1; + } + } + // printf("R: %d %3.1f %d %d\n",OutputPtr, OutputTime, InputLen, OutputLen); + return OutputLen; + } - template - int Process(InpType Input, size_t InputLen, Seq &Output) { - size_t OutPtr = Output.Len; - size_t MaxOutLen = (size_t)ceil(InputLen * OutputRate + 2); - if (Output.EnsureSpace(OutPtr + MaxOutLen) < 0) return -1; - int OutLen = Process(Input, InputLen, Output.Elem+OutPtr); - Output.Len += OutLen; - return OutLen; - } + template + int Process(InpType Input, size_t InputLen, Seq &Output) { + size_t OutPtr = Output.Len; + size_t MaxOutLen = (size_t)ceil(InputLen * OutputRate + 2); + if (Output.EnsureSpace(OutPtr + MaxOutLen) < 0) return -1; + int OutLen = Process(Input, InputLen, Output.Elem+OutPtr); + Output.Len += OutLen; + return OutLen; + } - template - int Process(InpType Input, OutType *Output) { - return Process(&Input, 1, Output); - } + template + int Process(InpType Input, OutType *Output) { + return Process(&Input, 1, Output); + } }; @@ -2085,228 +1620,231 @@ class MFSK_Transmitter { public: // primary parameters: set by the user - size_t Tones; // number of tones: 4, 8, 16, 32, 64, 128, 256 - size_t Bandwidth; // bandwidth: 125, 250, 500, 1000, 2000 - Type SampleRate; // audio sampling rate (internal processing) - Type OutputSampleRate; // true sampling rate of the soundcard - float FirstCarrierMultiplier; - int Reverse; + bool bContestia; + size_t Tones; // number of tones: 4, 8, 16, 32, 64, 128, 256 + size_t Bandwidth; // bandwidth: 125, 250, 500, 1000, 2000 + Type SampleRate; // audio sampling rate (internal processing) + Type OutputSampleRate; // true sampling rate of the soundcard + float FirstCarrierMultiplier; + int Reverse; // secondary parameters: calculated by Preset() - size_t BitsPerSymbol; // number of bits per symbol - size_t SymbolsPerBlock; // number of symbols per one FEC code block - size_t MaxOutputLen; // maximum length of the audio batch returned by Output() + size_t BitsPerSymbol; // number of bits per symbol + size_t SymbolsPerBlock; // number of symbols per one FEC code block + size_t MaxOutputLen; // maximum length of the audio batch returned by Output() private: - static const int State_Running = 0x0001; - static const int State_StopReq = 0x0010; - int State; + static const int State_Running = 0x0001; + static const int State_StopReq = 0x0010; + int State; - FIFO Input; // buffer(queue) for the characters to be encoded - uint8_t InputBlock[8]; // FEC code block buffer - FIFO Monitor; // buffer for monitoring the characters being sent + FIFO Input; // buffer(queue) for the characters to be encoded + uint8_t InputBlock[8]; // FEC code block buffer + FIFO Monitor; // buffer for monitoring the characters being sent - MFSK_Encoder Encoder; // FEC encoder - size_t SymbolPtr; + MFSK_Encoder Encoder; // FEC encoder + size_t SymbolPtr; - MFSK_Modulator Modulator; // MFSK modulator + MFSK_Modulator Modulator; // MFSK modulator - Type *ModulatorOutput; + Type *ModulatorOutput; - RateConverter Converter; // output rate converter + RateConverter Converter; // output rate converter - Type *ConverterOutput; + Type *ConverterOutput; public: - MFSK_Transmitter() { - Init(); - } + MFSK_Transmitter() { + bContestia = false; + Init(); + } - ~MFSK_Transmitter() { - Free(); - } + ~MFSK_Transmitter() { + Free(); + } - void Init(void) { - ModulatorOutput = 0; - ConverterOutput = 0; - } + void Init(void) { + ModulatorOutput = 0; + ConverterOutput = 0; + } - void Free(void) { - Input.Free(); - Monitor.Free(); - Encoder.Free(); - Modulator.Free(); - free(ModulatorOutput); - ModulatorOutput = 0; - Converter.Free(); - free(ConverterOutput); - ConverterOutput = 0; - } + void Free(void) { + Input.Free(); + Monitor.Free(); + Encoder.Free(); + Modulator.Free(); + free(ModulatorOutput); + ModulatorOutput = 0; + Converter.Free(); + free(ConverterOutput); + ConverterOutput = 0; + } // set default primary parameters - void Default(void) { - Tones = 32; - Bandwidth = 1000; - Reverse = 0; - SampleRate = 8000; - OutputSampleRate = 8000; - } + void Default(void) { + Tones = 32; + Bandwidth = 1000; + Reverse = 0; + SampleRate = 8000; + OutputSampleRate = 8000; + } // preset internal arrays according to primary paramaters - int Preset(void) { - // impose limits on the primary parameters - if (Tones <2 ) Tones = 2; - else if (Tones > 256) Tones = 256; - if (Bandwidth < 125) Bandwidth = 125; - else if (Bandwidth > 2000) Bandwidth = 2000; + int Preset(void) { + // impose limits on the primary parameters + if (Tones < 2 ) Tones = 2; + else if (Tones > 256) Tones = 256; + if (Bandwidth < 125) Bandwidth = 125; + else if (Bandwidth > 2000) Bandwidth = 2000; - // calculate the secondary parameters - BitsPerSymbol = Log2(Tones); - Tones = Exp2(BitsPerSymbol); + // calculate the secondary parameters + BitsPerSymbol = Log2(Tones); + Tones = Exp2(BitsPerSymbol); - // preset the input character buffer - Input.Len = 1024; - if (Input.Preset() <0 ) goto Error; - Monitor.Len = 256; - if (Monitor.Preset() < 0) goto Error; + // preset the input character buffer + Input.Len = 1024; + if (Input.Preset() <0 ) goto Error; + Monitor.Len = 256; + if (Monitor.Preset() < 0) goto Error; - // preset the encoder - Encoder.BitsPerSymbol = BitsPerSymbol; - if (Encoder.Preset() < 0) goto Error; - SymbolsPerBlock = Encoder.SymbolsPerBlock; + // preset the encoder + Encoder.bContestia = bContestia; + Encoder.BitsPerSymbol = BitsPerSymbol; + if (Encoder.Preset() < 0) goto Error; + SymbolsPerBlock = Encoder.SymbolsPerBlock; - // preset the modulator - Modulator.Default(); - Modulator.BitsPerSymbol = BitsPerSymbol; - Modulator.SymbolLen = - (size_t)1 << (BitsPerSymbol + 7 - Log2(Bandwidth / 125)); - Bandwidth = Exp2(Log2(Bandwidth / 125)) * 125; - Modulator.FirstCarrier = - (size_t) ((Modulator.SymbolLen / 16) * FirstCarrierMultiplier) + 1; - Modulator.Reverse = Reverse; - if (Modulator.Preset() < 0) goto Error; + // preset the modulator + Modulator.Default(); + Modulator.BitsPerSymbol = BitsPerSymbol; + Modulator.SymbolLen = + (size_t)1 << (BitsPerSymbol + 7 - Log2(Bandwidth / 125)); + Bandwidth = Exp2(Log2(Bandwidth / 125)) * 125; + Modulator.FirstCarrier = + (size_t) ((Modulator.SymbolLen / 16) * FirstCarrierMultiplier) + 1; + Modulator.Reverse = Reverse; + if (Modulator.Preset() < 0) goto Error; - if (ReallocArray(&ModulatorOutput, Modulator.SymbolSepar) < 0) - goto Error; + if (ReallocArray(&ModulatorOutput, Modulator.SymbolSepar) < 0) + goto Error; - // preset the rate converter - Converter.OutputRate = OutputSampleRate / SampleRate; - if (Converter.Preset() < 0) goto Error; + // preset the rate converter + Converter.OutputRate = OutputSampleRate / SampleRate; + if (Converter.Preset() < 0) goto Error; - MaxOutputLen = - (size_t)ceil(Modulator.SymbolSepar * OutputSampleRate / SampleRate + 2); - if (ReallocArray(&ConverterOutput, MaxOutputLen) < 0) goto Error; + MaxOutputLen = + (size_t)ceil(Modulator.SymbolSepar * OutputSampleRate / SampleRate + 2); + if (ReallocArray(&ConverterOutput, MaxOutputLen) < 0) goto Error; - // reset the state logic - SymbolPtr = 0; - State = 0; + // reset the state logic + SymbolPtr = 0; + State = 0; - return 0; + return 0; - Error: - Free(); - return -1; - } + Error: + Free(); + return -1; + } - void Reset(void) { - Input.Reset(); - Monitor.Reset(); - SymbolPtr = 0; - State = 0; - Converter.Reset(); - } + void Reset(void) { + Input.Reset(); + Monitor.Reset(); + SymbolPtr = 0; + State = 0; + Converter.Reset(); + } - Type BaudRate(void) { - return SampleRate / Modulator.SymbolSepar; - } + Type BaudRate(void) { + return SampleRate / Modulator.SymbolSepar; + } - - Type BlockPeriod(void) { - return (SymbolsPerBlock * Modulator.SymbolSepar) / SampleRate; - } + + Type BlockPeriod(void) { + return (SymbolsPerBlock * Modulator.SymbolSepar) / SampleRate; + } - Type CharactersPerSecond(void) { - return BitsPerSymbol * SampleRate / - (SymbolsPerBlock * Modulator.SymbolSepar); - } + Type CharactersPerSecond(void) { + return BitsPerSymbol * SampleRate / + (SymbolsPerBlock * Modulator.SymbolSepar); + } // start the transmission - void Start(void) { - State |= State_Running; - } + void Start(void) { + State |= State_Running; + } // request to stop (and complete) the transmission // but the transmitter will only stop after transmitting all the data - void Stop(void) { - State |= State_StopReq; - } + void Stop(void) { + State |= State_StopReq; + } // check if the transmission is still running (not complete) - int Running(void) { - return State & State_Running; - } + int Running(void) { + return State & State_Running; + } // put the character into the transmitter input queue - int PutChar(uint8_t Char) { - return Input.Write(Char); - } + int PutChar(uint8_t Char) { + return Input.Write(Char); + } // get one character from the monitor buffer - int GetChar(uint8_t &Char) { - return Monitor.Read(Char); - } + int GetChar(uint8_t &Char) { + return Monitor.Read(Char); + } - size_t GetReadReady(void) { - return Input.ReadReady(); - } + size_t GetReadReady(void) { + return Input.ReadReady(); + } - int DoPostambleYet(void) { - if (State == 0) - return 1; - else - return 0; - } + int DoPostambleYet(void) { + if (State == 0) + return 1; + else + return 0; + } - double GetSymbolPhase(void) { - return Modulator.SymbolPhase; - } + double GetSymbolPhase(void) { + return Modulator.SymbolPhase; + } // get out the transmitter output (audio) - int Output(double *Buffer) { - if (SymbolPtr == 0) { - if ((State&State_StopReq) && Input.Empty()) { - State=0; - } else if (State&State_Running) { - size_t Idx; - for (Idx = 0; Idx < BitsPerSymbol; Idx++) { - uint8_t Char; - if (Input.Read(Char) <= 0) break; - InputBlock[Idx] = Char; - Monitor.Write(Char); - } - for ( ; Idx < BitsPerSymbol; Idx++) - InputBlock[Idx] = 0; - Encoder.EncodeBlock(InputBlock); - } - } - if (State&State_Running) { - Modulator.Send(Encoder.OutputBlock[SymbolPtr]); - SymbolPtr += 1; - if (SymbolPtr >= SymbolsPerBlock) SymbolPtr = 0; - } - int ModLen = Modulator.Output(ModulatorOutput); - int ConvLen = Converter.Process(ModulatorOutput, ModLen, ConverterOutput); - if (ConvLen < 0) return ConvLen; - - double maxval = 0, tempnum; - for (int Idx = 0; Idx < ConvLen; Idx++) - if ((tempnum = fabs(ConverterOutput[Idx])) > maxval) - maxval = tempnum; - for (int Idx = 0; Idx < ConvLen; Idx++) - Buffer[Idx] = (double) ConverterOutput[Idx] / maxval; + int Output(double *Buffer) { + if (SymbolPtr == 0) { + if ((State&State_StopReq) && Input.Empty()) { + State=0; + } else if (State&State_Running) { + size_t Idx; + for (Idx = 0; Idx < BitsPerSymbol; Idx++) { + uint8_t Char; + if (Input.Read(Char) <= 0) break; + InputBlock[Idx] = Char; + Monitor.Write(Char); + } + for ( ; Idx < BitsPerSymbol; Idx++) + InputBlock[Idx] = 0; + Encoder.EncodeBlock(InputBlock); + } + } + if (State&State_Running) { + Modulator.Send(Encoder.OutputBlock[SymbolPtr]); + SymbolPtr += 1; + if (SymbolPtr >= SymbolsPerBlock) SymbolPtr = 0; + } + int ModLen = Modulator.Output(ModulatorOutput); + int ConvLen = Converter.Process(ModulatorOutput, ModLen, ConverterOutput); + if (ConvLen < 0) return ConvLen; + + double maxval = 0, tempnum; + for (int Idx = 0; Idx < ConvLen; Idx++) + if ((tempnum = fabs(ConverterOutput[Idx])) > maxval) + maxval = tempnum; + for (int Idx = 0; Idx < ConvLen; Idx++) + Buffer[Idx] = (double) ConverterOutput[Idx] / maxval; - return ConvLen; - } + return ConvLen; + } }; @@ -2318,73 +1856,73 @@ How to use the MFSK_Receiver class: 1. create an object like: - #include "mfsk.h" + #include "mfsk.h" - MFSK_Receiver Receiver; + MFSK_Receiver Receiver; 2. Set the parameters, for example: - Receiver.Tones = 32; // number of tones (symbols) - Receiver.Bandwidth = 1000; // bandwidth [Hz] - Receiver.SyncMargin = 8; // synchronizer tune margin [tone freq. spacing] - Receiver.SyncIntegLen = 4; // synchronizer integration period [FEC blocks] - Receiver.SyncThreshold = 3.2; // S/N threshold for printing - Receiver.SampleRate = 8000.0; // internal processor sampling rate [Hz] - Receiver.InputSampleRate = 8000.0; // soundcard sampling rate [Hz] + Receiver.Tones = 32; // number of tones (symbols) + Receiver.Bandwidth = 1000; // bandwidth [Hz] + Receiver.SyncMargin = 8; // synchronizer tune margin [tone freq. spacing] + Receiver.SyncIntegLen = 4; // synchronizer integration period [FEC blocks] + Receiver.SyncThreshold = 3.2; // S/N threshold for printing + Receiver.SampleRate = 8000.0; // internal processor sampling rate [Hz] + Receiver.InputSampleRate = 8000.0; // soundcard sampling rate [Hz] - You don't need to set all the parameters, as upon creation - of the Receiver object they are already given certain default - values. + You don't need to set all the parameters, as upon creation + of the Receiver object they are already given certain default + values. - If you changed parameters at one time and want later to go back - to the default values you can call: Receiver.Default(); + If you changed parameters at one time and want later to go back + to the default values you can call: Receiver.Default(); 3. Preset the Receiver internal arrays for the parameters you just set: - if (Receiver.Preset()<0) - printf("Not enough RAM or another problem\n"); + if (Receiver.Preset()<0) + printf("Not enough RAM or another problem\n"); - Each time you change the parameters you need to call Preset() - in order to resize the internal arrays. Preset() will as well - destroy all data being in the process of decoding, if you need - this data then call first Receiver.Flush() + Each time you change the parameters you need to call Preset() + in order to resize the internal arrays. Preset() will as well + destroy all data being in the process of decoding, if you need + this data then call first Receiver.Flush() 4. Read back the parameters you set in point 1., they could have been adjusted - by Preset() to their closest allowed values. + by Preset() to their closest allowed values. 5. Feed the audio into the Receiver: - Receiver.Process(AudioBuffer, BufferLength); + Receiver.Process(AudioBuffer, BufferLength); - AudioBuffer can be an array of int16_t (16-bit signed integers) - that you fill with the data from the soundcard. I suggest you feed - the receiver with batches of 512 or 1024 samples, but in can be any number - of samples at a time. + AudioBuffer can be an array of int16_t (16-bit signed integers) + that you fill with the data from the soundcard. I suggest you feed + the receiver with batches of 512 or 1024 samples, but in can be any number + of samples at a time. 6. Call GetChar(Char) to get decoded characters. Note, that - characters come in batches, and so, you need to call GetChar() - possibly several times. GetChar() returns 0 when the decoder FIFO - is empty or 1 when not empty. In the latter case the argument - contains the character read form the FIFO. The loop could be like: - - for ( ; ; ) - { uint8_t Char; - if (Receiver.GetChar(Char)==0) break; - printf("%c",Char); - } + characters come in batches, and so, you need to call GetChar() + possibly several times. GetChar() returns 0 when the decoder FIFO + is empty or 1 when not empty. In the latter case the argument + contains the character read form the FIFO. The loop could be like: + + for ( ; ; ) + { uint8_t Char; + if (Receiver.GetChar(Char)==0) break; + printf("%c",Char); + } - Keep in mind that you may see (random) control code characters here, - and thus you must be able to deal with them. I suggest to process - only carriage return (code=13) and Backspace (code=8). NUL (code=0) - is the idle character: it is being sent when there is no text to be sent. + Keep in mind that you may see (random) control code characters here, + and thus you must be able to deal with them. I suggest to process + only carriage return (code=13) and Backspace (code=8). NUL (code=0) + is the idle character: it is being sent when there is no text to be sent. 7. At any time you can read the signal-to-noise ratio of the - incoming signal by calling Receiver.SignalToNoiseRatio() or frequency offset - by calling Receiver.FrequencyOffset() + incoming signal by calling Receiver.SignalToNoiseRatio() or frequency offset + by calling Receiver.FrequencyOffset() 8. When the user decides to turn off the receiver and switch over - to transmitt you may still call Receiver.Flush()in order to flush - the data still being buffered in the decoder pipeplines. + to transmitt you may still call Receiver.Flush()in order to flush + the data still being buffered in the decoder pipeplines. */ @@ -2393,433 +1931,999 @@ class MFSK_Receiver { public: // primary parameters: set by the user - size_t Tones; // number of tones: 4, 8, 16, 32, 64, 128, 256 - size_t Bandwidth; // bandwidth: 125, 250, 500, 1000, 2000 - size_t SyncMargin; // synchronizer search margin, frequency-wide - size_t SyncIntegLen; // synchronizer integration period - Type SyncThreshold; // synchronizer S/N threshold - Type SampleRate; // audio sampling rate (internal processing) - Type InputSampleRate; // true sampling rate of the soundcard - float FirstCarrierMultiplier; - int Reverse; + + bool bContestia; + size_t Tones; // number of tones: 4, 8, 16, 32, 64, 128, 256 + size_t Bandwidth; // bandwidth: 125, 250, 500, 1000, 2000 + size_t SyncMargin; // synchronizer search margin, frequency-wide + size_t SyncIntegLen; // synchronizer integration period + Type SyncThreshold; // synchronizer S/N threshold + Type SampleRate; // audio sampling rate (internal processing) + Type InputSampleRate; // true sampling rate of the soundcard + float FirstCarrierMultiplier; + int Reverse; // secondary parameters: calculated by Preset() - size_t BitsPerSymbol; // number of bits per symbol - size_t SymbolsPerBlock; // number of symbols per one FEC code block + size_t BitsPerSymbol; // number of bits per symbol + size_t SymbolsPerBlock; // number of symbols per one FEC code block private: - RateConverter Converter; - Seq InputBuffer; - MFSK_InputProcessor InputProcessor; // equalizes the input spectrum - // and removes coherent interferences - MFSK_Demodulator Demodulator; // FFT demodulator + RateConverter Converter; + Seq InputBuffer; + MFSK_InputProcessor InputProcessor; // equalizes the input spectrum + // and removes coherent interferences + MFSK_Demodulator Demodulator; // FFT demodulator - const static size_t SlicesPerSymbol = - MFSK_Demodulator::SpectraPerSymbol; + const static size_t SlicesPerSymbol = + MFSK_Demodulator::SpectraPerSymbol; // number of possible frequency offsets - size_t FreqOffsets; + size_t FreqOffsets; // number of possible time-phases within the FEC block - size_t BlockPhases; + size_t BlockPhases; // reference decoder - MFSK_SoftDecoder RefDecoder; + MFSK_SoftDecoder RefDecoder; // array of decoders - MFSK_SoftDecoder *Decoder; + MFSK_SoftDecoder *Decoder; // current running time-phase - size_t BlockPhase; + size_t BlockPhase; // FEC noise integrators - CircularBuffer< LowPass3_Filter > SyncNoiseEnergy; + CircularBuffer< LowPass3_Filter > SyncNoiseEnergy; // FEC signal integrators - CircularBuffer< LowPass3_Filter > SyncSignal; + CircularBuffer< LowPass3_Filter > SyncSignal; // weight for the integrators - Type SyncFilterWeight; + Type SyncFilterWeight; // best signal - Type SyncBestSignal; + Type SyncBestSignal; // time-phase of the best signal - size_t SyncBestBlockPhase; + size_t SyncBestBlockPhase; // frequency offset of the best signal - size_t SyncBestFreqOffset; + size_t SyncBestFreqOffset; // S/N corresponding to the SyncBestSignal - Type SyncSNR; + Type SyncSNR; // pipeline for decoded FEC blocks - CircularBuffer *DecodePipe; + CircularBuffer *DecodePipe; // buffer for decoded characters - FIFO Output; + FIFO Output; public: - MFSK_Receiver() { - Init(); - Default(); - } - ~MFSK_Receiver() { - Free(); - } - void Init(void) { - Decoder = 0; - DecodePipe = 0; - } - void Free(void) { - if (Decoder) { - size_t Idx; - for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) - Decoder[Idx].Free(); - free(Decoder); Decoder=0; - } - if (DecodePipe) { - size_t Idx; - for (Idx = 0; Idx < BlockPhases; Idx++) - DecodePipe[Idx].Free(); - free(DecodePipe); - DecodePipe = 0; - } - Converter.Free(); - InputBuffer.Free(); - InputProcessor.Free(); - Demodulator.Free(); - RefDecoder.Free(); - SyncSignal.Free(); - SyncNoiseEnergy.Free(); - Output.Free(); - } + MFSK_Receiver() { + bContestia = false; + Init(); + Default(); + } + ~MFSK_Receiver() { + Free(); + } + void Init(void) { + Decoder = 0; + DecodePipe = 0; + } + void Free(void) { + if (Decoder) { + size_t Idx; + for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) + Decoder[Idx].Free(); + free(Decoder); Decoder=0; + } + if (DecodePipe) { + size_t Idx; + for (Idx = 0; Idx < BlockPhases; Idx++) + DecodePipe[Idx].Free(); + free(DecodePipe); + DecodePipe = 0; + } + Converter.Free(); + InputBuffer.Free(); + InputProcessor.Free(); + Demodulator.Free(); + RefDecoder.Free(); + SyncSignal.Free(); + SyncNoiseEnergy.Free(); + Output.Free(); + } // set defaults values for all parameters - void Default(void) { - Tones = 32; - Reverse = 0; - Bandwidth = 1000; - SyncMargin = 8; - SyncIntegLen = 4; - SyncThreshold = 3.0; - SampleRate = 8000; - InputSampleRate = 8000.0; - } + void Default(void) { + Tones = 32; + Reverse = 0; + Bandwidth = 1000; + SyncMargin = 8; + SyncIntegLen = 4; + SyncThreshold = 3.0; + SampleRate = 8000; + InputSampleRate = 8000.0; + } // resize internal arrays according the parameters - int Preset(void) { - size_t Idx; - if (Tones < 2) Tones = 2; - else if (Tones > 256) Tones = 256; - - if (Bandwidth < 125) Bandwidth = 125; - else if (Bandwidth > 2000) Bandwidth = 2000; + int Preset(void) { + size_t Idx; + if (Tones < 2) Tones = 2; + else if (Tones > 256) Tones = 256; + + if (Bandwidth < 125) Bandwidth = 125; + else if (Bandwidth > 2000) Bandwidth = 2000; - if (SyncMargin < 2) SyncMargin = 2; + if (SyncMargin < 2) SyncMargin = 2; - if (SyncIntegLen < 2) SyncIntegLen = 2; + if (SyncIntegLen < 2) SyncIntegLen = 2; - if (SyncThreshold < 3.0) SyncThreshold = 3.0; + if (SyncThreshold < 3.0) SyncThreshold = 3.0; - BitsPerSymbol = Log2(Tones); - Tones = Exp2(BitsPerSymbol); + BitsPerSymbol = Log2(Tones); + Tones = Exp2(BitsPerSymbol); - Converter.OutputRate = SampleRate/InputSampleRate; - if (Converter.Preset() < 0) goto Error; + Converter.OutputRate = SampleRate/InputSampleRate; + if (Converter.Preset() < 0) goto Error; - Demodulator.BitsPerSymbol = BitsPerSymbol; - Demodulator.SymbolLen = Exp2(BitsPerSymbol + 7 - Log2(Bandwidth/125)); - Bandwidth = Exp2(Log2(Bandwidth/125)) * 125; - Demodulator.FirstCarrier = - (size_t) ((Demodulator.SymbolLen/16) * FirstCarrierMultiplier) + 1; - Demodulator.Reverse = Reverse; - Demodulator.DecodeMargin = SyncMargin; - if (Demodulator.Preset() < 0) goto Error; - SyncMargin = Demodulator.DecodeMargin; + Demodulator.BitsPerSymbol = BitsPerSymbol; + Demodulator.SymbolLen = Exp2(BitsPerSymbol + 7 - Log2(Bandwidth/125)); + Bandwidth = Exp2(Log2(Bandwidth/125)) * 125; + Demodulator.FirstCarrier = + (size_t) ((Demodulator.SymbolLen/16) * FirstCarrierMultiplier) + 1; + Demodulator.Reverse = Reverse; + Demodulator.DecodeMargin = SyncMargin; + if (Demodulator.Preset() < 0) goto Error; + SyncMargin = Demodulator.DecodeMargin; - InputProcessor.WindowLen = 16 * Demodulator.SymbolLen; - if (InputProcessor.Preset() < 0) goto Error; + InputProcessor.WindowLen = 16 * Demodulator.SymbolLen; + if (InputProcessor.Preset() < 0) goto Error; - RefDecoder.Default(); - RefDecoder.BitsPerSymbol=BitsPerSymbol; - if (RefDecoder.Preset()<0) goto Error; - SymbolsPerBlock=RefDecoder.SymbolsPerBlock; + RefDecoder.Default(); + RefDecoder.BitsPerSymbol=BitsPerSymbol; + RefDecoder.bContestia = bContestia; + if (RefDecoder.Preset()<0) goto Error; + SymbolsPerBlock=RefDecoder.SymbolsPerBlock; - if (Decoder) { - for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) - Decoder[Idx].Free(); - } + if (Decoder) { + for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) + Decoder[Idx].Free(); + } - if (DecodePipe) { - for (Idx = 0; Idx < BlockPhases; Idx++) - DecodePipe[Idx].Free(); - } + if (DecodePipe) { + for (Idx = 0; Idx < BlockPhases; Idx++) + DecodePipe[Idx].Free(); + } - FreqOffsets = 2 * SyncMargin + 1; - BlockPhases = SlicesPerSymbol * SymbolsPerBlock; + FreqOffsets = 2 * SyncMargin + 1; + BlockPhases = SlicesPerSymbol * SymbolsPerBlock; - if (ReallocArray(&Decoder, SlicesPerSymbol * FreqOffsets) < 0) - goto Error; - for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) - Decoder[Idx].Init(); - for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) - if (Decoder[Idx].Preset(RefDecoder) < 0) goto Error; + if (ReallocArray(&Decoder, SlicesPerSymbol * FreqOffsets) < 0) + goto Error; - if (ReallocArray(&DecodePipe,BlockPhases) < 0) goto Error; - for (Idx = 0; Idx < BlockPhases; Idx++) - DecodePipe[Idx].Init(); - for (Idx = 0; Idx < BlockPhases; Idx++) { - DecodePipe[Idx].Width = FreqOffsets; - DecodePipe[Idx].Len = SyncIntegLen; - if (DecodePipe[Idx].Preset() < 0) goto Error; - DecodePipe[Idx].Clear(); - } + for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) { + Decoder[Idx].bContestia = bContestia; + Decoder[Idx].Init(); + } - SyncSignal.Width = FreqOffsets; - SyncSignal.Len = BlockPhases; - if (SyncSignal.Preset() < 0) goto Error; - SyncSignal.Clear(); + for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) + if (Decoder[Idx].Preset(RefDecoder) < 0) goto Error; - SyncNoiseEnergy.Width = FreqOffsets; - SyncNoiseEnergy.Len = BlockPhases; - if (SyncNoiseEnergy.Preset() < 0) goto Error; - SyncNoiseEnergy.Clear(); + if (ReallocArray(&DecodePipe,BlockPhases) < 0) goto Error; + for (Idx = 0; Idx < BlockPhases; Idx++) + DecodePipe[Idx].Init(); + for (Idx = 0; Idx < BlockPhases; Idx++) { + DecodePipe[Idx].Width = FreqOffsets; + DecodePipe[Idx].Len = SyncIntegLen; + if (DecodePipe[Idx].Preset() < 0) goto Error; + DecodePipe[Idx].Clear(); + } - SyncFilterWeight = 1.0 / SyncIntegLen; + SyncSignal.Width = FreqOffsets; + SyncSignal.Len = BlockPhases; + if (SyncSignal.Preset() < 0) goto Error; + SyncSignal.Clear(); - BlockPhase=0; + SyncNoiseEnergy.Width = FreqOffsets; + SyncNoiseEnergy.Len = BlockPhases; + if (SyncNoiseEnergy.Preset() < 0) goto Error; + SyncNoiseEnergy.Clear(); - SyncBestSignal = 0; - SyncBestBlockPhase = 0; - SyncBestFreqOffset = 0; - SyncSNR = 0; + SyncFilterWeight = 1.0 / SyncIntegLen; - if (InputBuffer.EnsureSpace(InputProcessor.WindowLen + 2048) < 0) - goto Error; + BlockPhase=0; - Output.Len = 1024; - if (Output.Preset() < 0) goto Error; + SyncBestSignal = 0; + SyncBestBlockPhase = 0; + SyncBestFreqOffset = 0; + SyncSNR = 0; - return 0; + if (InputBuffer.EnsureSpace(InputProcessor.WindowLen + 2048) < 0) + goto Error; - Error: - Free(); - return -1; - } + Output.Len = 1024; + if (Output.Preset() < 0) goto Error; - void Reset(void) { - size_t Idx; - Converter.Reset(); - InputBuffer.Clear(); - InputProcessor.Reset(); - for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) - Decoder[Idx].Reset(); + return 0; - for (Idx = 0; Idx < BlockPhases; Idx++) - DecodePipe[Idx].Clear(); + Error: + Free(); + return -1; + } - SyncSignal.Clear(); - SyncNoiseEnergy.Clear(); + void Reset(void) { + size_t Idx; + Converter.Reset(); + InputBuffer.Clear(); + InputProcessor.Reset(); + for (Idx = 0; Idx < (SlicesPerSymbol * FreqOffsets); Idx++) + Decoder[Idx].Reset(); - BlockPhase = 0; + for (Idx = 0; Idx < BlockPhases; Idx++) + DecodePipe[Idx].Clear(); - SyncBestSignal = 0; - SyncBestBlockPhase = 0; - SyncBestFreqOffset = 0; - SyncSNR = 0; + SyncSignal.Clear(); + SyncNoiseEnergy.Clear(); + + BlockPhase = 0; + + SyncBestSignal = 0; + SyncBestBlockPhase = 0; + SyncBestFreqOffset = 0; + SyncSNR = 0; - Output.Reset(); - } + Output.Reset(); + } - void PrintParameters(void) { - printf("MFSK_Receiver parameters:\n"); + char *PrintParameters(void) { + static char szParams[1000]; + Type FreqBin = SampleRate/Demodulator.SymbolLen; + snprintf(szParams, sizeof(szParams), "\ +%d tones, %4.2f Hz/tone, %4.1f Hz bandwidth\n\ +%d bits/symbol(tone), %4.2f baud\n\ +FEC block: %d char. => %d x %d bits, %3.1f sec\n\ +Audio band: %3.1f - %3.1f Hz, %3.1f Hz total\n\ +Tuning tolerance = +/- %3.1f Hz\n\ +Sync. S/N threshold = %3.1f\n", + Demodulator.Carriers, + FreqBin*Demodulator.CarrierSepar, + FreqBin*Demodulator.CarrierSepar*Demodulator.Carriers, + Demodulator.BitsPerSymbol, + SampleRate/Demodulator.SymbolSepar, + RefDecoder.BitsPerSymbol, + RefDecoder.BitsPerSymbol, RefDecoder.SymbolsPerBlock, + (SymbolsPerBlock*Demodulator.SymbolSepar)/SampleRate, + FreqBin * Demodulator.FirstCarrier, + FreqBin * (Demodulator.FirstCarrier + Demodulator.CarrierSepar * Demodulator.Carriers), + FreqBin * Demodulator.CarrierSepar * Demodulator.Carriers, + FreqBin * SyncMargin, SyncThreshold ); + return szParams; + } - Type FreqBin = SampleRate/Demodulator.SymbolLen; - printf( - "%d bits/symbol(tone), %d tones, %4.2f Hz/tone, %4.1f Hz bandwidth\n", - Demodulator.BitsPerSymbol, - Demodulator.Carriers, - FreqBin*Demodulator.CarrierSepar, - FreqBin*Demodulator.CarrierSepar*Demodulator.Carriers); + Type BaudRate(void) { + return SampleRate / Demodulator.SymbolSepar; + } - printf( - "%4.2f baud, FEC block: %d char. => %dx%d bits, %3.1f sec\n", - SampleRate/Demodulator.SymbolSepar, - RefDecoder.BitsPerSymbol, - RefDecoder.BitsPerSymbol, RefDecoder.SymbolsPerBlock, - (SymbolsPerBlock*Demodulator.SymbolSepar)/SampleRate); + Type TuneMargin(void) { + return SyncMargin * (SampleRate / Demodulator.SymbolLen); + } - printf( - "Audio band: %3.1f - %3.1f Hz, %3.1f Hz total\n", - FreqBin * Demodulator.FirstCarrier, - FreqBin * (Demodulator.FirstCarrier + Demodulator.CarrierSepar * - Demodulator.Carriers), - FreqBin * Demodulator.CarrierSepar * Demodulator.Carriers ); + Type BlockPeriod(void) { + return (SymbolsPerBlock * Demodulator.SymbolSepar) / SampleRate; + } - printf("Tuning tolerance = +/- %3.1f Hz, Sync. S/N threshold = %3.1f\n", - FreqBin * SyncMargin, SyncThreshold); - } + Type CharactersPerSecond(void) { + return BitsPerSymbol * SampleRate / + (SymbolsPerBlock*Demodulator.SymbolSepar); + } - Type BaudRate(void) { - return SampleRate / Demodulator.SymbolSepar; - } + Type SignalToNoiseRatio(void) { + return SyncSNR; + } - Type TuneMargin(void) { - return SyncMargin * (SampleRate / Demodulator.SymbolLen); - } + Type FrequencyOffset(void) { + return ( (int)SyncBestFreqOffset - + (int)FreqOffsets / 2) * (SampleRate / Demodulator.SymbolLen); + } - Type BlockPeriod(void) { - return (SymbolsPerBlock * Demodulator.SymbolSepar) / SampleRate; - } - - Type CharactersPerSecond(void) { - return BitsPerSymbol * SampleRate / - (SymbolsPerBlock*Demodulator.SymbolSepar); - } - - Type SignalToNoiseRatio(void) { - return SyncSNR; - } - - Type FrequencyOffset(void) { - return ( (int)SyncBestFreqOffset - - (int)FreqOffsets / 2) * (SampleRate / Demodulator.SymbolLen); - } - - Type TimeOffset(void) { - return ( (Type)SyncBestBlockPhase / SlicesPerSymbol) * - (Demodulator.SymbolSepar / SampleRate); - } + Type TimeOffset(void) { + return ( (Type)SyncBestBlockPhase / SlicesPerSymbol) * + (Demodulator.SymbolSepar / SampleRate); + } // process an audio batch: first the input processor, then the demodulator - template - int Process(InpType *Input, size_t InputLen) { - if (Converter.Process(Input, InputLen, InputBuffer) < 0) - return -1; - ProcessInputBuffer(); - return 0; - } + template + int Process(InpType *Input, size_t InputLen) { + if (Converter.Process(Input, InputLen, InputBuffer) < 0) + return -1; + ProcessInputBuffer(); + return 0; + } - void Flush(void) { - ProcessInputBuffer(); - size_t Idx; + void Flush(void) { + ProcessInputBuffer(); + size_t Idx; - for (Idx = InputBuffer.Len; Idx < InputProcessor.WindowLen; Idx++) - InputBuffer[Idx] = 0; - InputBuffer.Len = InputProcessor.WindowLen; - ProcessInputBuffer(); + for (Idx = InputBuffer.Len; Idx < InputProcessor.WindowLen; Idx++) + InputBuffer[Idx] = 0; + InputBuffer.Len = InputProcessor.WindowLen; + ProcessInputBuffer(); - for (Idx = 0; Idx < InputProcessor.WindowLen; Idx++) - InputBuffer[Idx] = 0; - size_t FlushLen = Demodulator.SymbolSepar * SymbolsPerBlock * - SyncIntegLen * 2; - for (Idx=0; Idx= InputProcessor.WindowLen) { - InputProcessor.Process(InputBuffer.Elem); - InputBuffer.Delete(0, InputProcessor.WindowLen); - size_t Idx; - for (Idx = 0; - Idx < InputProcessor.WindowLen; - Idx += Demodulator.SymbolSepar ) - ProcessSymbol(InputProcessor.Output+Idx); - } - } + void ProcessInputBuffer(void) { + while(InputBuffer.Len >= InputProcessor.WindowLen) { + InputProcessor.Process(InputBuffer.Elem); + InputBuffer.Delete(0, InputProcessor.WindowLen); + size_t Idx; + for (Idx = 0; + Idx < InputProcessor.WindowLen; + Idx += Demodulator.SymbolSepar ) + ProcessSymbol(InputProcessor.Output+Idx); + } + } // process (through the demodulator) an audio batch corresponding to one symbol // demodulator always uses audio batches corresponding to one symbol period - template - void ProcessSymbol(InpType *Input) { - Demodulator.Process(Input); - MFSK_SoftDecoder *DecoderPtr = Decoder; + template + void ProcessSymbol(InpType *Input) { + Demodulator.Process(Input); + MFSK_SoftDecoder *DecoderPtr = Decoder; - size_t Offset,Slice; - for (Slice = 0; Slice < SlicesPerSymbol; Slice++) { - LowPass3_Filter - *NoiseEnergyPtr = SyncNoiseEnergy.AbsPtr(BlockPhase); - LowPass3_Filter - *SignalPtr = SyncSignal.AbsPtr(BlockPhase); + size_t Offset,Slice; + for (Slice = 0; Slice < SlicesPerSymbol; Slice++) { + LowPass3_Filter + *NoiseEnergyPtr = SyncNoiseEnergy.AbsPtr(BlockPhase); + LowPass3_Filter + *SignalPtr = SyncSignal.AbsPtr(BlockPhase); - uint64_t *DecodeBlockPtr = DecodePipe[BlockPhase].CurrPtr(); + uint64_t *DecodeBlockPtr = DecodePipe[BlockPhase].CurrPtr(); - size_t BestSliceOffset = 0; - Type BestSliceSignal = 0; - Type NoiseEnergy = 0; - Type Signal; - Type Symbol[8]; + size_t BestSliceOffset = 0; + Type BestSliceSignal = 0; + Type NoiseEnergy = 0; + Type Signal; + Type Symbol[8]; - for (Offset = 0; Offset < FreqOffsets; Offset++) { - Demodulator.SoftDecode(Symbol, Slice, (int)Offset - (FreqOffsets / 2)); + for (Offset = 0; Offset < FreqOffsets; Offset++) { + Demodulator.SoftDecode(Symbol, Slice, (int)Offset - (FreqOffsets / 2)); - DecoderPtr->Input(Symbol); - DecoderPtr->Process(); - DecoderPtr->Output(DecodeBlockPtr); + DecoderPtr->Input(Symbol); + DecoderPtr->Process(); + DecoderPtr->Output(DecodeBlockPtr); - NoiseEnergy = DecoderPtr->NoiseEnergy; - NoiseEnergyPtr->Process(NoiseEnergy, SyncFilterWeight); + NoiseEnergy = DecoderPtr->NoiseEnergy; + NoiseEnergyPtr->Process(NoiseEnergy, SyncFilterWeight); - Signal = DecoderPtr->Signal; - SignalPtr->Process(Signal, SyncFilterWeight); - Signal = SignalPtr->Output; + Signal = DecoderPtr->Signal; + SignalPtr->Process(Signal, SyncFilterWeight); + Signal = SignalPtr->Output; - if (Signal > BestSliceSignal) { - BestSliceSignal = Signal; - BestSliceOffset = Offset; - } + if (Signal > BestSliceSignal) { + BestSliceSignal = Signal; + BestSliceOffset = Offset; + } - DecoderPtr++; - DecodeBlockPtr++; + DecoderPtr++; + DecodeBlockPtr++; - NoiseEnergyPtr++; - SignalPtr++; - } - DecodePipe[BlockPhase] += 1; + NoiseEnergyPtr++; + SignalPtr++; + } + DecodePipe[BlockPhase] += 1; - if (BlockPhase == SyncBestBlockPhase) { - SyncBestSignal = BestSliceSignal; - SyncBestFreqOffset = BestSliceOffset; - } else { - if (BestSliceSignal > SyncBestSignal) { - SyncBestSignal = BestSliceSignal; - SyncBestBlockPhase = BlockPhase; - SyncBestFreqOffset = BestSliceOffset; - } - } + if (BlockPhase == SyncBestBlockPhase) { + SyncBestSignal = BestSliceSignal; + SyncBestFreqOffset = BestSliceOffset; + } else { + if (BestSliceSignal > SyncBestSignal) { + SyncBestSignal = BestSliceSignal; + SyncBestBlockPhase = BlockPhase; + SyncBestFreqOffset = BestSliceOffset; + } + } - int Dist = (int)BlockPhase - (int)SyncBestBlockPhase; - if (Dist < 0) Dist += BlockPhases; + int Dist = (int)BlockPhase - (int)SyncBestBlockPhase; + if (Dist < 0) Dist += BlockPhases; - if (Dist == (int)(BlockPhases / 2)) { - Type BestNoise = sqrt( NoiseEnergyPtr->Output); - if (BestNoise == 0) SyncSNR = 0; - else SyncSNR = SyncBestSignal / BestNoise; -// printf( -// "%d, %6.3f/%6.3f = %5.2f\n", -// SyncBestFreqOffset, -// SyncBestSignal, -// BestNoise, -// SyncSNR ); - if (SyncSNR >= SyncThreshold) { - uint64_t *BestBlockPtr = - DecodePipe[SyncBestBlockPhase].CurrPtr(); - uint64_t Block = BestBlockPtr[SyncBestFreqOffset]; + if (Dist == (int)(BlockPhases / 2)) { + Type BestNoise = sqrt( NoiseEnergyPtr->Output); + if (BestNoise == 0) SyncSNR = 0; + else SyncSNR = SyncBestSignal / BestNoise; +// printf( +// "%d, %6.3f/%6.3f = %5.2f\n", +// SyncBestFreqOffset, +// SyncBestSignal, +// BestNoise, +// SyncSNR ); + if (SyncSNR >= SyncThreshold) { + uint64_t *BestBlockPtr = + DecodePipe[SyncBestBlockPhase].CurrPtr(); + uint64_t Block = BestBlockPtr[SyncBestFreqOffset]; - for ( size_t Byte = 0; Byte < BitsPerSymbol; Byte++) { - uint8_t Char = Block&0xFF; - Output.Write(Char); - Block >>= 8; - } - } - if (SyncSNR > 100) SyncSNR = 0.0; - } + for ( size_t Byte = 0; Byte < BitsPerSymbol; Byte++) { + uint8_t Char = Block&0xFF; + Output.Write(Char); + Block >>= 8; + } + } + if (SyncSNR > 100) SyncSNR = 0.0; + } - BlockPhase++; - } - if (BlockPhase >= BlockPhases) BlockPhase -= BlockPhases; - } + BlockPhase++; + } + if (BlockPhase >= BlockPhases) BlockPhase -= BlockPhases; + } }; +// unused code +// ===================================================================== +/* +template +class MFSK_Symbol +{ +public: + size_t BitsPerSymbol; + size_t Carriers; + + Type *Energy; + Type *Correction; + Type *Corrected; + + size_t Code; + Type PeakEnergy; + Type Background; + Type ScanThreshold; + size_t ScanIndex; + + MFSK_Symbol() { + Init(); + Default(); + } + + ~MFSK_Symbol() { Free(); } + + void Init(void) { + Energy = 0; + Correction = 0; + Corrected = 0; + } + + void Free(void) { + free(Energy); + Energy = 0; + free(Correction); + Correction = 0; + free(Corrected); + Corrected = 0; + } + + void Default(void) { + BitsPerSymbol = 5; + } + + int Preset(void) { + Carriers = Exp2(BitsPerSymbol); + if (ReallocArray(&Energy, Carriers) < 0) goto Error; + if (ReallocArray(&Correction, Carriers) < 0) goto Error; + if (ReallocArray(&Corrected, Carriers) < 0) goto Error; + return 0; + Error: + Free(); + return -1; + } + + void ClearCorrection(void) { + size_t Idx; + for (Idx = 0; Idx < Carriers; Idx++) + Correction[Idx] = 0; + } + + void AddCorrection(void) { + size_t Idx; + for (Idx = 0; Idx < Carriers; Idx++) + Corrected[Idx] = Energy[Idx] + Correction[Idx]; + } + + void AddCorrection(Type Weight) { + size_t Idx; + for (Idx = 0; Idx < Carriers; Idx++) + Corrected[Idx] = Energy[Idx] + Weight * Correction[Idx]; + } + + void MakeRandom(Type SignalToNoise = 1.0) { + Type Signal = 1.0; + Type Noise = 1.0 / SignalToNoise; + Type NoiseEnergy = Noise * Noise; + NoiseEnergy /= Carriers; + + size_t Idx; + double UniformNoise = 0; + for (Idx = 0; Idx < Carriers; Idx++) { + UniformNoise = ((double)rand()+1.0)/((double)RAND_MAX+1.0); + Energy[Idx] = NoiseEnergy * (-log(UniformNoise)); + } + + double Phase = 2 * M_PI * ((double)rand()+1.0)/((double)RAND_MAX+1.0); + Type NoiseAmpl = sqrt(Energy[Code]); + Signal += cos(Phase) * NoiseAmpl; + NoiseAmpl *= sin(Phase); + Energy[Code] = Signal*Signal + NoiseAmpl * NoiseAmpl; + ClearCorrection(); + AddCorrection(); + } + + void Decode(void) { + size_t Idx; + PeakEnergy = 0; + Code = 0; + Background = 0; + for (Idx=0; Idx PeakEnergy) { + PeakEnergy = Energy; + Code = Idx; + } + } + Background -= PeakEnergy; + Background /= (Carriers-1); + } + + void Scan_Init(Type Threshold = 4.0) { + ScanThreshold = Threshold * Background; + } + + int Scan_First(void) { + for (ScanIndex = 0; ScanIndex < Carriers; ScanIndex++) + if (Corrected[ScanIndex] > ScanThreshold) + return 0; + return -1; + } // 0 => OK, -1 => no data above threshold + + int Scan_Next(void) { + for ( ScanIndex++; ScanIndex < Carriers; ScanIndex++) + if (Corrected[ScanIndex]>ScanThreshold) + return 0; + if (Scan_First() < 0) + return -1; + return 1; + } // 0 => OK, 1 => wrapped around, -1 => no data above threshold + + void Print(void) { + size_t Idx; + printf("MFSK_Symbol: %d bits/%d carriers, Peak = %3.1f/%3.1f @ 0x%02X\n", + BitsPerSymbol,Carriers, + PeakEnergy,Background, Code); + printf("Energy:"); + for (Idx=0; Idx +class MFSK_Delay +{ +public: + size_t Len; + Type *Tap; + size_t TapPtr; + + MFSK_Delay() { Tap = 0; Len = 0; } + ~MFSK_Delay() { free(Tap); } + + int Preset(size_t NewLen) { + Len = NewLen; + if (ReallocArray(&Tap, Len) < 0) { + Len = 0; + return -1; + } + size_t Idx; + for (Idx = 0; Idx < Len; Idx++) + Tap[Idx] = 0; + TapPtr = 0; + return 0; + } + + void Process(Type Input) { + if (Len == 0) + return; + Tap[TapPtr] = Input; + TapPtr += 1; + if (TapPtr >= Len) TapPtr -= Len; + } + + void Process(Type Input, Type &Output) { + if (Len == 0) { + Output = Input; + return; + } + Output = Tap[TapPtr]; + Tap[TapPtr] = Input; + TapPtr += 1; + if (TapPtr >= Len) TapPtr -= Len; + } + + void Process(Type * Buffer, size_t BufferLen) { + size_t Idx; + Type Output; + for (Idx = 0; Idx < BufferLen; Idx++) { + Process(Buffer[Idx], Output); + Buffer[Idx] = Output; + } + } +}; + +template +class MFSK_FEC +{ +public: + size_t BitsPerSymbol; + size_t SymbolsPerLine; + size_t Dimensions; + size_t SymbolsPerBlock; + size_t CodeMask; + MFSK_Symbol *Symbol; + + MFSK_FEC() { + Init(); + Default(); + } + + ~MFSK_FEC() { + Free(); + } + + void Init(void) { + Symbol = 0; + } + + void Free(void) { + delete [] Symbol; + Symbol = 0; + } + + void Default(void) { + BitsPerSymbol = 5; + SymbolsPerLine = 4; + Dimensions = 3; + } + + int Preset(void) { + Free(); + SymbolsPerBlock = 1; + size_t Dim; + for (Dim = 0; Dim < Dimensions; Dim++) + SymbolsPerBlock *= SymbolsPerLine; + CodeMask = Exp2(BitsPerSymbol) - 1; + Symbol = new MFSK_Symbol[SymbolsPerBlock]; + if (Symbol == 0) return -1; + size_t Idx; + for (Idx = 0; Idx < SymbolsPerBlock; Idx++) { + Symbol[Idx].BitsPerSymbol = BitsPerSymbol; + if (Symbol[Idx].Preset() < 0) return -1; + } + return 0; + } + + void CalculateCheck(size_t Idx, size_t Step) { + size_t Check=0; + size_t LineIdx; + for ( LineIdx = 0; LineIdx < (SymbolsPerLine - 1); LineIdx++,Idx += Step) + Check -= Symbol[Idx].Code; + Check &= CodeMask; + Symbol[Idx].Code = Check; + } + + int Correct(void) { + return 0; + } + +}; + +template +class MFSK_LineCorrector +{ +public: + + size_t BitsPerSymbol; + size_t SymbolsPerLine; + size_t CodeMask; + + MFSK_Symbol **Symbol; + + MFSK_LineCorrector() { + Init(); + Default(); + } + + ~MFSK_LineCorrector() { + Free(); + } + + void Init(void) { + Symbol = 0; + } + + void Free(void) { + free(Symbol); + Symbol = 0; + } + + void Default(void) { + BitsPerSymbol = 5; + SymbolsPerLine = 4; + } + + int Preset(void) { + CodeMask = Exp2(BitsPerSymbol) - 1; + if (ReallocArray(&Symbol, SymbolsPerLine) < 0) goto Error; + return 0; + Error: + Free(); + return -1; + } + + void CorrectSymbol(size_t SymbolIdx) { + size_t Idx; + size_t Check = 0; + Type Corr = 0; + for (Idx = 0; Idx < SymbolsPerLine; Idx++) { + if (Idx == SymbolIdx) continue; + MFSK_Symbol *SymbolPtr = Symbol[Idx]; + Check -= SymbolPtr->Code; + Corr += SymbolPtr->PeakEnergy; // -SymbolPtr->Background; + } + Check &= CodeMask; + Symbol[SymbolIdx]->Correction[Check] += Corr; + } + + void CorrectSymbol_Slow(size_t SymbolIdx) { + size_t Idx; + for (Idx = 0; IdxScan_First()<0) break; + } + if (Idx < SymbolsPerLine) return; + for ( ; ; ) { + size_t Check = 0; + Type Corr = 0; + for (Idx = 0; Idx < SymbolsPerLine; Idx++) { + if (Idx == SymbolIdx) continue; + MFSK_Symbol *SymbolPtr = Symbol[Idx]; + size_t Code = SymbolPtr->ScanIndex; + Check -= Code; + Corr += SymbolPtr->Corrected[Code] - SymbolPtr->ScanThreshold; + } + Check &= CodeMask; + Symbol[SymbolIdx]->Correction[Check] += Corr; + for (Idx = 0; Idx < SymbolsPerLine; Idx++) { + if (Idx == SymbolIdx) continue; + if (Symbol[Idx]->Scan_Next() == 0) break; + } + if (Idx >= SymbolsPerLine) break; + } + } + + void Correct(size_t Iter = 8, Type Weight = 1.0) { + Weight /= (2 * SymbolsPerLine); + for ( ; Iter; Iter--) { + size_t Idx; + for (Idx = 0; Idx < SymbolsPerLine; Idx++) { + Symbol[Idx]->ClearCorrection(); + Symbol[Idx]->Scan_Init(); + } + for (Idx = 0; Idx < SymbolsPerLine; Idx++) + CorrectSymbol(Idx); + for (Idx = 0; Idx < SymbolsPerLine; Idx++) { + Symbol[Idx]->AddCorrection(Weight); + Symbol[Idx]->Decode(); + } + } + } + +} ; + +class MFSK_HardDecoder +{ +public: + size_t BitsPerSymbol; + size_t BitsPerCharacter; + size_t Symbols; + size_t SymbolsPerBlock; + + float Signal, NoiseEnergy; + uint8_t *OutputBlock; + bContestia = false; +// bRTTYM = false; + + static const uint64_t ScramblingCodeOlivia = 0xE257E6D0291574EC; + static const uint64_t ScramblingCodeContestia = 0xEDB88320L; + +private: + static const uint64_t ScramblingCode = ScramblingCodeOlivia; + + uint8_t *InputBuffer; + size_t InputPtr; + size_t InputWrap; + int8_t *FHT_Buffer; + +public: + MFSK_HardDecoder() { + Init(); + Default(); + } + ~MFSK_HardDecoder() { + Free(); + } + + void Default(void) { + BitsPerSymbol = 5; + BitsPerCharacter = 7; + } + + void Init(void) { + InputBuffer = 0; + FHT_Buffer = 0; + OutputBlock = 0; + } + + void Free(void) { + free(InputBuffer); + InputBuffer = 0; + free(FHT_Buffer); + FHT_Buffer = 0; + free(OutputBlock); + OutputBlock = 0; + } + + void Reset(void) { + size_t Idx; + for (Idx = 0; Idx < SymbolsPerBlock; Idx++) + InputBuffer[Idx] = 0; + InputPtr = 0; + } + + int Preset(void) { + Symbols = 1<>= Rotate; + Bit &= 1; + uint64_t CodeMask = 1; + CodeMask <<= CodeBit; + if (ScramblingCode&CodeMask) Bit ^= 1; + FHT_Buffer[TimeBit]= Bit ? -1 : 1; + CodeBit += 1; + CodeBit &= CodeWrap; + Rotate += 1; + if (Rotate >= BitsPerSymbol) Rotate -= BitsPerSymbol; + Ptr += 1; + Ptr &= InputWrap; + } + + FHT(FHT_Buffer,SymbolsPerBlock); + int32_t Peak = 0; + size_t PeakPos = 0; + int32_t SqrSum = 0; + for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { + int32_t Signal = FHT_Buffer[TimeBit]; + SqrSum += Signal * Signal; + if (abs(Signal) > abs(Peak)) { + Peak = Signal; + PeakPos = TimeBit; + } + } + + uint8_t Char = PeakPos; + if (Peak < 0) Char += SymbolsPerBlock; + SqrSum -= Peak * Peak; + OutputBlock[FreqBit] = Char; + NoiseEnergy += (float)SqrSum / (SymbolsPerBlock - 1); + Signal += abs(Peak); + } + + void Process(void) { + size_t FreqBit; + Signal = 0; + NoiseEnergy = 0; + for (FreqBit=0; FreqBit 0; ) { + PackedBuffer <<= 8; + FreqBit--; + PackedBuffer |= OutputBlock[FreqBit]; + } + return BitsPerSymbol; + } + + size_t Output(uint64_t *PackedBuffer) { + return Output(*PackedBuffer); + } + + void PrintOutputBlock(FILE *File=stdout) + { size_t FreqBit; + fprintf(File,"'"); + for (FreqBit=0; FreqBit=' ')&&(Char<127) ? Char:' '); } + fprintf(File,"', S/N = %5.1f/%4.1f",Signal,sqrt(NoiseEnergy)); + if (NoiseEnergy>0) fprintf(File," = %5.1f",Signal/sqrt(NoiseEnergy)); + fprintf(File,"\n"); } + + void PrintInputBuffer(void) { + size_t TimeBit; + size_t Ptr = InputPtr; + for (TimeBit = 0; TimeBit < SymbolsPerBlock; TimeBit++) { + printf("%2d: ",(int)TimeBit); + PrintBinary(InputBuffer[Ptr], BitsPerSymbol); + printf("\n"); + Ptr += 1; + Ptr &= InputWrap; + } + } + +}; + +*/ // ===================================================================== #endif // of __MFSK_H__ diff --git a/src/include/jalocha/pj_struc.h b/src/include/jalocha/pj_struc.h old mode 100755 new mode 100644 diff --git a/src/include/lookupcall.h b/src/include/lookupcall.h index 6aabf768..14150826 100644 --- a/src/include/lookupcall.h +++ b/src/include/lookupcall.h @@ -1,6 +1,20 @@ #ifndef LOOKUPCALL_H #define LOOKUPCALL_H +#include + +extern std::string lookup_latd; +extern std::string lookup_lond; +extern std::string lookup_addr1; +extern std::string lookup_addr2; +extern std::string lookup_qth; +extern std::string lookup_state; +extern std::string lookup_province; +extern std::string lookup_zip; +extern std::string lookup_country; + +extern void clear_Lookup(); + extern void CALLSIGNquery(); enum qrz_query_t { QRZ_EXIT = -1, QRZNONE, QRZNET, QRZCD, HAMCALLNET, QRZHTML, HAMCALLHTML }; diff --git a/src/include/modem.h b/src/include/modem.h index d289f911..d3411a64 100644 --- a/src/include/modem.h +++ b/src/include/modem.h @@ -123,7 +123,9 @@ public: unsigned get_cap(void) { return cap; } enum { CAP_AFC = 1 << 0, CAP_AFC_SR = 1 << 1, CAP_REV = 1 << 2, - CAP_IMG = 1 << 3, CAP_BW = 1 << 4 }; + CAP_IMG = 1 << 3, CAP_BW = 1 << 4, CAP_RX = 1 << 5, + CAP_TX = 1 << 6 + }; // for CW modem use only bool get_cwTrack(); @@ -231,6 +233,7 @@ extern modem *psk500r_modem; extern modem *rtty_modem; extern modem *olivia_modem; +extern modem *contestia_modem; extern modem *thor4_modem; extern modem *thor5_modem; @@ -256,6 +259,7 @@ extern modem *throbx4_modem; extern modem *wwv_modem; extern modem *anal_modem; +extern modem *ssb_modem; #endif diff --git a/src/include/psk.h b/src/include/psk.h index bc3b9dd2..ed664a6f 100644 --- a/src/include/psk.h +++ b/src/include/psk.h @@ -89,12 +89,12 @@ private: interleave *Rxinlv; interleave *Rxinlv2; interleave *Txinlv; - unsigned int bitshreg; + unsigned int bitshreg; int rxbitstate; //PSKR modes - Soft decoding unsigned char symbolpair[2]; - int fecmet; - int fecmet2; + double fecmet; + double fecmet2; double phase; double freqerr; @@ -102,15 +102,15 @@ private: double bitclk; double syncbuf[16]; double scope_pipe[2*PipeLen];//[PipeLen]; - unsigned int pipeptr; - unsigned int dcdshreg; + unsigned int pipeptr; + unsigned int dcdshreg; //PSKR modes - 2nd stream unsigned int dcdshreg2; int dcd; - int dcdbits; + int dcdbits; complex quality; - int acquire; + int acquire; viewpsk* pskviewer; pskeval* evalpsk; diff --git a/src/include/spot.h b/src/include/spot.h index 1ae58336..b0e46f1c 100644 --- a/src/include/spot.h +++ b/src/include/spot.h @@ -35,7 +35,7 @@ typedef void (*spot_recv_cb_t)(trx_mode mode, int afreq, const char* str, const typedef void (*spot_log_cb_t)(const char* call, const char* loc, long long freq, trx_mode mode, time_t rtime, void* data); -void spot_recv(char c, int decoder = -1, int afreq = 0); +void spot_recv(char c, int decoder = -1, int afreq = 0, int md = 0); void spot_log(const char* callsign, const char* locator = "", long long freq = 0LL, trx_mode mode = NUM_MODES, time_t rtime = -1L); void spot_manual(const char* callsign, const char* locator = "", diff --git a/src/include/ssb.h b/src/include/ssb.h new file mode 100644 index 00000000..677a6e2f --- /dev/null +++ b/src/include/ssb.h @@ -0,0 +1,43 @@ +// ---------------------------------------------------------------------------- +// ssb.h -- ssb modem +// +// Copyright (C) 2010 +// Dave Freese, W1HKJ +// +// 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 _SSB_H +#define _SSB_H + +#include "modem.h" + +#define ssb_SampleRate 8000 + +class ssb : public modem +{ +public: + ssb(); + ~ssb(); + void init(); + void rx_init(); + void tx_init(SoundBase *sc); + void restart(); + int rx_process(const double *buf, int len); + int tx_process(); +}; + +#endif diff --git a/src/include/timeops.h b/src/include/timeops.h index d1c73ae3..d2d7603d 100644 --- a/src/include/timeops.h +++ b/src/include/timeops.h @@ -11,8 +11,6 @@ #if !HAVE_CLOCK_GETTIME enum clockid_t { CLOCK_REALTIME, CLOCK_MONOTONIC }; int clock_gettime(clockid_t clock_id, struct timespec* tp); -#elif !defined(CLOCK_MONOTONIC) // cygwin 1.5 -# define CLOCK_MONOTONIC CLOCK_REALTIME #endif struct timespec operator+(const struct timespec &t0, const double &t); diff --git a/src/include/util.h b/src/include/util.h index c8beeada..9d09e68f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -177,7 +177,7 @@ const char* str2hex(const unsigned* str, size_t len); const char* uint2bin(unsigned u, size_t len); -#if !defined(NDEBUG) && defined(deprecated__) && defined(__GNUC__) && !defined(__MINGW32__) +#if 0 && !defined(NDEBUG) && defined(deprecated__) && defined(__GNUC__) && !defined(__MINGW32__) #include #include deprecated__ typeof(sprintf) sprintf; diff --git a/src/logbook/lgbook.cxx b/src/logbook/lgbook.cxx index 64f02ad0..df0bb5e8 100644 --- a/src/logbook/lgbook.cxx +++ b/src/logbook/lgbook.cxx @@ -166,7 +166,7 @@ btnSelectFreq->value(1); btnSelectBand->value(1); btnSelectMode->value(1); btnSelectQSOdate->value(1); -btnSelectTimeON->value(0); +btnSelectTimeON->value(1); btnSelectTimeOFF->value(1); btnSelectQth->value(0); btnSelectLOC->value(1); @@ -405,6 +405,7 @@ void create_logbook_dialogs() { } // Fl_Check_Button* btnSelectQSOdate { btnSelectTimeON = new Fl_Check_Button(412, 142, 70, 15, _("Time ON")); btnSelectTimeON->down_box(FL_DOWN_BOX); + btnSelectTimeON->value(1); } // Fl_Check_Button* btnSelectTimeON { btnSelectTimeOFF = new Fl_Check_Button(412, 162, 70, 15, _("Time OFF")); btnSelectTimeOFF->down_box(FL_DOWN_BOX); diff --git a/src/logbook/lgbook.fl b/src/logbook/lgbook.fl index 4d1cff68..00c3c0f3 100644 --- a/src/logbook/lgbook.fl +++ b/src/logbook/lgbook.fl @@ -1,5 +1,5 @@ # data file for the Fltk User Interface Designer (fluid) -version 1.0109 +version 1.0110 i18n_type 1 i18n_include "gettext.h" i18n_function _ @@ -20,7 +20,7 @@ Function {create_logbook_dialogs()} {open return_type void } { Fl_Window wExport { label {Export Setup} open - xywh {537 92 655 365} type Double hide modal + xywh {506 538 655 365} type Double modal visible } { Fl_Group {} { label {Select Records to Export} open @@ -52,7 +52,7 @@ Export_log();} xywh {405 340 100 20} } Fl_Group {} { - label {Select Fields to Export} + label {Select Fields to Export} open xywh {392 4 258 330} box ENGRAVED_FRAME align 21 } { Fl_Button btnClearAllFields { @@ -143,8 +143,8 @@ btnSelectNotes->value(1);} xywh {412 122 70 15} down_box DOWN_BOX value 1 } Fl_Check_Button btnSelectTimeON { - label {Time ON} - xywh {412 142 70 15} down_box DOWN_BOX + label {Time ON} selected + xywh {412 142 70 15} down_box DOWN_BOX value 1 } Fl_Check_Button btnSelectTimeOFF { label {Time OFF} @@ -238,7 +238,7 @@ btnSelectFreq->value(1); btnSelectBand->value(1); btnSelectMode->value(1); btnSelectQSOdate->value(1); -btnSelectTimeON->value(0); +btnSelectTimeON->value(1); btnSelectTimeOFF->value(1); btnSelectQth->value(0); btnSelectLOC->value(1); @@ -262,10 +262,10 @@ btnSelectTX_pwr->value(0);} } } Fl_Window dlgLogbook { - label Logbook open + label Logbook xywh {540 120 570 350} type Double color 47 resizable visible } { - Fl_Group editGroup {open selected + Fl_Group editGroup {open xywh {2 2 565 228} box ENGRAVED_FRAME color 52 } { Fl_Input inpDate_log { diff --git a/src/main.cxx b/src/main.cxx index 162d83e8..6359ad57 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -180,6 +180,7 @@ void set_platform_ui(void); double speed_test(int converter, unsigned repeat); static void setup_signal_handlers(void); static void checkdirectories(void); + static void arg_error(const char* name, const char* arg, bool missing) noreturn__; // TODO: find out why fldigi crashes on OS X if the wizard window is @@ -248,6 +249,8 @@ int main(int argc, char ** argv) main_window_title = PACKAGE_TARNAME; checkdirectories(); + check_nbems_dirs(); + try { debug::start(string(HomeDir).append("status_log.txt").c_str()); time_t t = time(NULL); diff --git a/src/mfsk/mfsk.cxx b/src/mfsk/mfsk.cxx index ac26d160..83608624 100644 --- a/src/mfsk/mfsk.cxx +++ b/src/mfsk/mfsk.cxx @@ -129,7 +129,7 @@ mfsk::~mfsk() mfsk::mfsk(trx_mode mfsk_mode) : modem() { - cap = CAP_AFC | CAP_REV; + cap |= CAP_AFC | CAP_REV; double bw, cf, flo, fhi; mode = mfsk_mode; diff --git a/src/misc/arq_io.cxx b/src/misc/arq_io.cxx index d7e00e49..eaf837e0 100644 --- a/src/misc/arq_io.cxx +++ b/src/misc/arq_io.cxx @@ -412,9 +412,10 @@ bool WRAP_auto_arqRx() LOG_DEBUG("%s", arqtext.c_str()); start_tx(); txstring.clear(); + return true; } } - return true; + return false; } //----------------------------------------------------------------------------- @@ -582,9 +583,11 @@ bool Socket_arqRx() start_tx(); } txstring.clear(); + cmdstring.clear(); + return true; } cmdstring.clear(); - return true; + return false; } catch (const SocketException& e) { arq_stop(); diff --git a/src/misc/configuration.cxx b/src/misc/configuration.cxx index 0c4737fd..fef17545 100644 --- a/src/misc/configuration.cxx +++ b/src/misc/configuration.cxx @@ -81,7 +81,7 @@ const char *szBands[] = { "1830", "3580", "7030", "7070", "10138", "14070", "18100", "21070", "21080", "24920", "28070", "28120", 0}; - +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 1 // Define stream I/O operators for non-builtin types. // Right now we have: Fl_Color, Fl_Font, RGB, and RGBI ostream& operator<<(ostream& out, const Fl_Color& c) @@ -106,6 +106,7 @@ istream& operator>>(istream& in, Fl_Font& f) f = static_cast(i); return in; } +#endif ostream& operator<<(ostream& out, const RGB& rgb) { return out << (int)rgb.R << ' ' << (int)rgb.G << ' ' << (int)rgb.B; @@ -416,6 +417,13 @@ void configuration::loadDefaults() cntOlivia_sinteg->value(oliviasinteg); btnOlivia_8bit->value(olivia8bit); +// CONTESTIA + mnuContestia_Tones->value(contestiatones); + mnuContestia_Bandwidth->value(contestiabw); + cntContestia_smargin->value(contestiasmargin); + cntContestia_sinteg->value(contestiasinteg); + btnContestia_8bit->value(contestia8bit); + chkDominoEX_FEC->value(DOMINOEX_FEC); btnmt63_interleave->value(mt63_interleave == 64); @@ -441,6 +449,7 @@ void configuration::saveDefaults() TxFontName = Fl::get_font_name(TxFontnbr); WaterfallFontName = Fl::get_font_name(WaterfallFontnbr); ViewerFontName = Fl::get_font_name(ViewerFontnbr); + FreqControlFontName = Fl::get_font_name(FreqControlFontnbr); writeDefaultsXML(); changed = false; @@ -975,5 +984,7 @@ void configuration::initFonts(void) WaterfallFontnbr = font_number(WaterfallFontName.c_str()); if (!ViewerFontName.empty()) ViewerFontnbr = font_number(ViewerFontName.c_str()); + if (!FreqControlFontName.empty()) + FreqControlFontnbr = font_number(FreqControlFontName.c_str()); } diff --git a/src/misc/lookupcall.cxx b/src/misc/lookupcall.cxx index 5d21d25c..f0a34a14 100644 --- a/src/misc/lookupcall.cxx +++ b/src/misc/lookupcall.cxx @@ -193,6 +193,7 @@ bool parseSessionKey(const string& sessionpage) bool parse_xml(const string& xmlpage) { +//printf("%s\n", xmlpage.c_str()); IrrXMLReader* xml = createIrrXMLReader(new IIrrXMLStringReader(xmlpage)); // If we got any result back, clear the session key so that it will be @@ -204,20 +205,7 @@ bool parse_xml(const string& xmlpage) qrzerror.clear(); clear_Lookup(); } -// strings for storing the data we want to get out of the file - string call, - fname, - name, - addr1, - addr2, - state, - zip, - country, - latd, - lond, - grid, - dob; - + TAG tag = QRZ_IGNORE; // parse the file until end reached @@ -230,7 +218,6 @@ bool parse_xml(const string& xmlpage) case QRZ_IGNORE: break; case QRZ_CALL: - call = xml->getNodeData(); break; case QRZ_FNAME: lookup_fname = xml->getNodeData(); @@ -239,8 +226,13 @@ bool parse_xml(const string& xmlpage) lookup_name = xml->getNodeData(); break; case QRZ_ADDR1: + { lookup_addr1 = xml->getNodeData(); + size_t apt = lookup_addr1.find("#"); + if (apt != string::npos) + lookup_addr1.erase(apt, lookup_addr1.length() - apt); break; + } case QRZ_ADDR2: lookup_addr2 = xml->getNodeData(); break; @@ -293,8 +285,8 @@ bool parse_xml(const string& xmlpage) else if (!strcmp("state", nodeName)) tag = QRZ_STATE; else if (!strcmp("zip", nodeName)) tag = QRZ_ZIP; else if (!strcmp("country", nodeName)) tag = QRZ_COUNTRY; - else if (!strcmp("latd", nodeName)) tag = QRZ_LATD; - else if (!strcmp("lond", nodeName)) tag = QRZ_LOND; + else if (!strcmp("lat", nodeName)) tag = QRZ_LATD; + else if (!strcmp("lon", nodeName)) tag = QRZ_LOND; else if (!strcmp("grid", nodeName)) tag = QRZ_GRID; else if (!strcmp("dob", nodeName)) tag = QRZ_DOB; else if (!strcmp("Alert", nodeName)) tag = QRZ_ALERT; diff --git a/src/misc/macroedit.cxx b/src/misc/macroedit.cxx index 6ba7f38a..247188e1 100644 --- a/src/misc/macroedit.cxx +++ b/src/misc/macroedit.cxx @@ -56,7 +56,7 @@ Fl_Button *btnMacroEditCancel = (Fl_Button *)0; Fl_Button *btnInsertMacro = (Fl_Button *)0; Fl_Input2 *macrotext = (Fl_Input2 *)0; Fl_Input2 *labeltext = (Fl_Input2 *)0; -static int widths[] = {110, 0}; +static int widths[] = {130, 0}; Fl_Hold_Browser *macroDefs=(Fl_Hold_Browser *)0; @@ -86,7 +86,9 @@ void loadBrowser(Fl_Widget *widget) { w->add(_("\tother name")); w->add(_("\tother QTH")); w->add(_("\tother RST")); - + w->add(_("\tmap on google")); + w->add(_("\tmap by value")); + w->add(LINE_SEP); w->add(_("\tclear RX pane")); @@ -149,12 +151,23 @@ void loadBrowser(Fl_Widget *widget) { w->add(_("\tchange macro defs file")); w->add(LINE_SEP); - assert(MODE_OLIVIA < MODE_RTTY); + assert(MODE_CONTESTIA < MODE_OLIVIA); char s[256]; - for (trx_mode i = 0; i <= MODE_OLIVIA; i++) { + for (trx_mode i = 0; i <= MODE_CONTESTIA; i++) { snprintf(s, sizeof(s), "", mode_info[i].sname); w->add(s); } + // add some Contestia macros + const char* contestia[] = { "250:8", "500:8", "500:16", "1000:8", "1000:16" }; + for (size_t i = 0; i < sizeof(contestia)/sizeof(*contestia); i++) { + snprintf(s, sizeof(s), "", mode_info[MODE_CONTESTIA].sname, contestia[i]); + w->add(s); + } + for (trx_mode i = MODE_CONTESTIA + 1; i <= MODE_OLIVIA; i++) { + snprintf(s, sizeof(s), "", mode_info[i].sname); + w->add(s); + } + assert(MODE_OLIVIA < MODE_RTTY); // add some Olivia macros const char* olivia[] = { "250:8", "500:8", "500:16", "1000:8", "1000:32" }; for (size_t i = 0; i < sizeof(olivia)/sizeof(*olivia); i++) { diff --git a/src/misc/macros.cxx b/src/misc/macros.cxx index 54bead21..cbc60ce5 100644 --- a/src/misc/macros.cxx +++ b/src/misc/macros.cxx @@ -128,6 +128,7 @@ void pSRCHUP(string&, size_t&); void pSRCHDN(string&, size_t&); void pGOHOME(string&, size_t&); void pGOFREQ(string&, size_t&); +void pMAPIT(string&, size_t&); //void pMACROS(string &, size_t &); @@ -191,6 +192,8 @@ MTAGS mtags[] = { {"", pSRCHDN}, {"", pGOHOME}, {"", pMAPIT}, {0, 0} }; @@ -673,6 +676,12 @@ void pMODEM(string &s, size_t &i) if (args.at(2) != DBL_MIN) set_rtty_bits((int)args[2]); break; + case MODE_CONTESTIA: // bandwidth, tones + if (args.at(0) != DBL_MIN) + set_contestia_bw((int)args[0]); + if (args.at(1) != DBL_MIN) + set_contestia_tones((int)args[1]); + break; case MODE_OLIVIA: // bandwidth, tones if (args.at(0) != DBL_MIN) set_olivia_bw((int)args[0]); @@ -723,6 +732,7 @@ void pLOCK(string &s, size_t &i) wf->xmtlock->value(!wf->xmtlock->value()); wf->xmtlock->damage(); + wf->xmtlock->do_callback(); } s.replace(i, endbracket - i + 1, ""); } @@ -986,6 +996,71 @@ void pEXEC(string& s, size_t& i) } #endif // !__MINGW32__ +void MAPIT(int how) +{ + float lat = 0, lon = 0; + string sCALL = inpCall->value(); + string sLOC = inpLoc->value(); + + string url = "http://maps.google.com/maps?q="; + +// if (lookup_addr1.empty() && lookup_addr2.empty() && +// lookup_state.empty() && lookup_country.empty()) { + if (how > 1 && !lookup_country.empty()) { + url.append(lookup_addr1).append(",").append(lookup_addr2).append(","); + url.append(lookup_state).append(",").append(lookup_country); + } else { + if (how > 0 && (!lookup_latd.empty() && !lookup_lond.empty())) { + url.append(lookup_latd).append(","); + url.append(lookup_lond); + } else { + if (sLOC.empty()) return; + if (sLOC.length() < 4) return; + if (sLOC.length() < 6) sLOC.append("aa"); + for (size_t i = 0; i < 6; i++) sLOC[i] = toupper(sLOC[i]); + if (sLOC[0] -'A' > 17 || sLOC[4] - 'A' > 23 || + sLOC[1] -'A' > 17 || sLOC[5] - 'A' > 23 || + !isdigit(sLOC[2]) || !isdigit(sLOC[3])) return; + lon = -180.0 + + (sLOC[0] - 'A') * 20 + + (sLOC[2] - '0') * 2 + + (sLOC[4] - 'A' + 0.5) / 12; + lat = -90.0 + + (sLOC[1] - 'A') * 10 + + (sLOC[3] - '0') + + (sLOC[5] - 'A' + 0.5) / 24; + char sdata[20]; + snprintf(sdata, sizeof(sdata),"%10.6f", lat); + url.append(sdata).append(","); + snprintf(sdata, sizeof(sdata),"%10.6f", lon); + url.append(sdata); + } + } + if (!sCALL.empty()) url.append("(").append(sCALL).append(")"); + else url.append("(nocall)"); + url.append("&t=p&z=10"); + cb_mnuVisitURL(NULL, (void*)url.c_str()); +} + +void pMAPIT(string &s, size_t &i) +{ + size_t endbracket = s.find('>',i); + string sVal = s.substr(i + 7, endbracket - i - 7); + if (sVal.length() > 0) { + if (sVal.compare(0,3,"adr") == 0) + REQ(MAPIT,2); + else if (sVal.compare(0,6,"latlon") == 0) + REQ(MAPIT,1); + else if (sVal.compare(0,3,"loc") == 0) + REQ(MAPIT,0); + else + REQ(MAPIT,2); + } else + REQ(MAPIT,2); + s.erase(i, s.find('>', i) + 1 - i); + expand = false; +} + void pSTOP(string &s, size_t &i) { s.erase(i, s.find('>', i) + 1 - i); diff --git a/src/misc/newinstall.cxx b/src/misc/newinstall.cxx index 18a70523..17c96e5e 100644 --- a/src/misc/newinstall.cxx +++ b/src/misc/newinstall.cxx @@ -395,7 +395,7 @@ void Wizard::create_wizard(void) b->callback(wizard_cb, this); set_icon_label(b); b->align(buttons[i].align | FL_ALIGN_INSIDE); - b->size(fl_width(get_icon_label_text(b)) + icon_pad * 2, b->h()); + b->size(static_cast(fl_width(get_icon_label_text(b)) + icon_pad * 2), b->h()); } set_active(prev, false); done->hide(); diff --git a/src/misc/status.cxx b/src/misc/status.cxx index 417b33af..e8c8184b 100644 --- a/src/misc/status.cxx +++ b/src/misc/status.cxx @@ -79,7 +79,7 @@ status progStatus = { 560, // int rigW 80, // int rigH 1000, // int carrier; - 3580000, // int noCATfreq; + 0, // int noCATfreq; "USB", // string noCATmode; "3000", // string noCATwidth; 1, // int mag; @@ -87,7 +87,7 @@ status progStatus = { NORMAL, // WFdisp::WFspeed -20, // reflevel -70, // ampspan - 40, // uint VIEWERnchars + 30, // uint VIEWERnchars 50, // uint VIEWERxpos 50, // uint VIEWERypos false, // bool VIEWERvisible diff --git a/src/misc/timeops.cxx b/src/misc/timeops.cxx index cdc036ea..f1b5d1f2 100644 --- a/src/misc/timeops.cxx +++ b/src/misc/timeops.cxx @@ -34,12 +34,24 @@ # include int clock_gettime(clockid_t clock_id, struct timespec* tp) { - struct timeval t; - if (gettimeofday(&t, NULL) != 0) + if (clock_id == CLOCK_REALTIME) { + struct timeval t; + if (unlikely(gettimeofday(&t, NULL) != 0)) + return -1; + tp->tv_sec = t.tv_sec; + tp->tv_nsec = t.tv_usec * 1000; + } +#ifdef __WOE32__ + else if (clock_id == CLOCK_MONOTONIC) { + int32_t msec = GetTickCount(); + tp->tv_sec = msec / 1000; + tp->tv_nsec = (msec % 1000) * 1000000; + } +#endif + else { + errno = EINVAL; return -1; - - tp->tv_sec = t.tv_sec; - tp->tv_nsec = t.tv_usec * 1000; + } return 0; } diff --git a/src/misc/xmlrpc.cxx b/src/misc/xmlrpc.cxx index 8bcbf497..f02bd88d 100644 --- a/src/misc/xmlrpc.cxx +++ b/src/misc/xmlrpc.cxx @@ -2173,7 +2173,7 @@ public: try { params->verifyEnd(2); - Fl_Text_Buffer* tbuf = ReceiveText->buffer(); + Fl_Text_Buffer_mod* tbuf = ReceiveText->buffer(); int len = tbuf->length(); int start = params->getInt(0, 0, len - 1); int n = params->getInt(1, -1, len - start); diff --git a/src/olivia/olivia.cxx b/src/olivia/olivia.cxx index 63cfcf88..db3fc26e 100755 --- a/src/olivia/olivia.cxx +++ b/src/olivia/olivia.cxx @@ -159,10 +159,11 @@ int olivia::tx_process() // to read any more. If stopflag is set, we will always read // whatever there is. if (stopflag || (Tx->GetReadReady() < Tx->BitsPerSymbol)) { - if ((c = get_tx_char()) == 0x03 || stopflag ) { + if (!stopflag && (c = get_tx_char()) == 0x03) stopflag = true; + if (stopflag) Tx->Stop(); - } else { + else { /* Replace un-representable characters with a dot */ if (c == -1) c = 0; @@ -190,6 +191,7 @@ int olivia::tx_process() if (!Tx->Running()) { cwid(); + stopflag = false; return -1; } @@ -232,7 +234,7 @@ int olivia::rx_process(const double *buf, int len) for (int i = frequency - Rx->Bandwidth/2; i < frequency - 1 + Rx->Bandwidth/2; i++) if (wf->Pwr(i) > sp) sp = wf->Pwr(i); - np = wf->Pwr(frequency + Rx->Bandwidth/2 + 2*Rx->Bandwidth/Rx->Tones); + np = wf->Pwr(static_cast(frequency + Rx->Bandwidth/2 + 2*Rx->Bandwidth/Rx->Tones)); if (np == 0) np = sp + 1e-8; sigpwr = decayavg( sigpwr, sp, 10); noisepwr = decayavg( noisepwr, np, 50); @@ -266,9 +268,10 @@ void olivia::restart() sinteg = progdefaults.oliviasinteg; samplerate = 8000; - + bandwidth = 125 * (1 << bw); + Tx->Tones = 2 * (1 << tones); - Tx->Bandwidth = 125 * (1 << bw); + Tx->Bandwidth = bandwidth; Tx->SampleRate = samplerate; Tx->OutputSampleRate = samplerate; txbasefreq = get_txfreq_woffset(); @@ -292,7 +295,7 @@ void olivia::restart() txfbuffer = new double[txbufferlen]; Rx->Tones = Tx->Tones; - Rx->Bandwidth = Tx->Bandwidth; + Rx->Bandwidth = bandwidth; Rx->SyncMargin = smargin; Rx->SyncIntegLen = sinteg; Rx->SyncThreshold = progStatus.sqlonoff ? @@ -320,19 +323,19 @@ void olivia::restart() metric = 0; sigpwr = 1e-10; noisepwr = 1e-8; -// Rx->PrintParameters(); + LOG_INFO("\nOlivia Rx parameters:\n%s", Rx->PrintParameters()); } void olivia::init() { - modem::init(); restart(); + modem::init(); set_scope_mode(Digiscope::BLANK); } olivia::olivia() { - cap = CAP_REV; + cap |= CAP_REV; txfbuffer = 0; samplerate = 8000; diff --git a/src/psk/psk.cxx b/src/psk/psk.cxx index 28f73c71..a0648d10 100644 --- a/src/psk/psk.cxx +++ b/src/psk/psk.cxx @@ -113,6 +113,7 @@ void psk::rx_init() afcmetric = 0.0; // interleaver, split incoming bit stream into two, one late by one bit rxbitstate = 0; + fecmet = fecmet2 = 0; } @@ -125,8 +126,8 @@ void psk::restart() void psk::init() { - modem::init(); restart(); + modem::init(); set_scope_mode(Digiscope::PHASE); initSN_IMD(); snratio = 1.0; @@ -160,7 +161,7 @@ psk::~psk() psk::psk(trx_mode pskmode) : modem() { - cap = CAP_AFC | CAP_AFC_SR; + cap |= CAP_AFC | CAP_AFC_SR; mode = pskmode; @@ -1018,20 +1019,20 @@ void psk::tx_char(unsigned char c) void psk::tx_flush() { + if (_pskr) { + for (int i = 0; i < dcdbits; i++) + tx_bit(0); + } // QPSK - flush the encoder if (_qpsk) { for (int i = 0; i < dcdbits; i++) tx_bit(0); // FEC : replace unmodulated carrier by an encoded sequence of zeros - } else if (_pskr) { - for (int i = 0; i < dcdbits; i++) - tx_bit(0); - } else { - // Standard BPSK postamble - // DCD off sequence (unmodulated carrier) - for (int i = 0; i < dcdbits; i++) - tx_symbol(2); } + // Standard BPSK postamble + // DCD off sequence (unmodulated carrier) + for (int i = 0; i < dcdbits; i++) + tx_symbol(2); } // Necessary to clear the interleaver before we start sending diff --git a/src/psk/viewpsk.cxx b/src/psk/viewpsk.cxx index 32d38aae..5592bc8c 100644 --- a/src/psk/viewpsk.cxx +++ b/src/psk/viewpsk.cxx @@ -151,7 +151,7 @@ void viewpsk::rx_bit(int ch, int bit) shreg[ch] = 0; if (c == '\n' || c == '\r') c = ' '; if (isprint(c)) { - REQ(&viewaddchr, ch, (int)frequency[ch], c); + REQ(&viewaddchr, ch, (int)frequency[ch], c, viewmode); timeout[ch] = now + progdefaults.VIEWERtimeout; } } diff --git a/src/rigcontrol/FreqControl.cxx b/src/rigcontrol/FreqControl.cxx index e0ab5951..9630eb4a 100644 --- a/src/rigcontrol/FreqControl.cxx +++ b/src/rigcontrol/FreqControl.cxx @@ -79,6 +79,7 @@ void cbSelectDigit (Fl_Widget *btn, void * nbr) cFreqControl::cFreqControl(int x, int y, int w, int h, const char *lbl): Fl_Group(x,y,w,h,"") { + font_number = FL_COURIER; ONCOLOR = FL_YELLOW; OFFCOLOR = FL_BLACK; SELCOLOR = fl_rgb_color(100, 100, 100); @@ -106,20 +107,20 @@ cFreqControl::cFreqControl(int x, int y, int w, int h, const char *lbl): fcHeight-4, " "); Digit[n]->box(FL_FLAT_BOX); - Digit[n]->labelfont(FL_COURIER); + Digit[n]->labelfont(font_number); Digit[n]->labelcolor(ONCOLOR); Digit[n]->color(OFFCOLOR, SELCOLOR); - Digit[n]->labelsize(fcHeight); + Digit[n]->labelsize(fcHeight-4); Digit[n]->callback(cbSelectDigit, (void *) n); mult[n] = max; max *= 10; } decbx = new Fl_Box(fcFirst + (nD - 3) * fcWidth + 2, fcTop + 2, pw, fcHeight-4,"."); decbx->box(FL_FLAT_BOX); - decbx->labelfont(FL_COURIER); + decbx->labelfont(font_number); decbx->labelcolor(ONCOLOR); decbx->color(OFFCOLOR); - decbx->labelsize(fcHeight); + decbx->labelsize(fcHeight-4); cbFunc = NULL; maxVal = max * 10 - 1; @@ -202,6 +203,15 @@ void cFreqControl::SetOFFCOLOR (uchar r, uchar g, uchar b) damage(); } +void cFreqControl::font(Fl_Font fnt) +{ + font_number = fnt; + for (int n = 0; n < nD; n++) + Digit[n]->labelfont(fnt); + decbx->labelfont(fnt); + damage(); +} + static void blink_point(Fl_Widget* w) { w->label(*w->label() ? "" : "."); diff --git a/src/rigcontrol/hamlib.cxx b/src/rigcontrol/hamlib.cxx index 5aebc3f3..99e02590 100644 --- a/src/rigcontrol/hamlib.cxx +++ b/src/rigcontrol/hamlib.cxx @@ -179,17 +179,17 @@ void hamlib_init_defaults() { progdefaults.HamRigModel = hamlib_get_rig_model(cboHamlibRig->index()); progdefaults.HamRigDevice = inpRIGdev->value(); - progdefaults.HamlibRetries = cntHamlibRetries->value(); - progdefaults.HamlibTimeout = cntHamlibTimeout->value(); - progdefaults.HamlibWriteDelay = cntHamlibWriteDelay->value(); - progdefaults.HamlibWait = cntHamlibWait->value(); + progdefaults.HamlibRetries = static_cast(cntHamlibRetries->value()); + progdefaults.HamlibTimeout = static_cast(cntHamlibTimeout->value()); + progdefaults.HamlibWriteDelay = static_cast(cntHamlibWriteDelay->value()); + progdefaults.HamlibWait = static_cast(cntHamlibWait->value()); progdefaults.HamlibCMDptt = btnHamlibCMDptt->value(); progdefaults.HamlibDTRplus = btnHamlibDTRplus->value(); progdefaults.HamlibRTSCTSflow = chkHamlibRTSCTSflow->value(); progdefaults.HamlibRTSplus = chkHamlibRTSplus->value(); progdefaults.HamlibXONXOFFflow = chkHamlibXONXOFFflow->value(); progdefaults.HamlibSideband = mnuSideband->value(); - progdefaults.HamRigStopbits = valHamRigStopbits->value(); + progdefaults.HamRigStopbits = static_cast(valHamRigStopbits->value()); progdefaults.HamRigBaudrate = mnuBaudRate->value(); progdefaults.HamlibCMDptt = btnHamlibCMDptt->value(); progdefaults.HamConfig = inpHamlibConfig->value(); diff --git a/src/rigcontrol/rigio.cxx b/src/rigcontrol/rigio.cxx index fdf405cc..a4245a3e 100644 --- a/src/rigcontrol/rigio.cxx +++ b/src/rigcontrol/rigio.cxx @@ -1001,15 +1001,15 @@ void rigCAT_init_defaults() { progdefaults.XmlRigDevice = inpXmlRigDevice->value(); progdefaults.XmlRigBaudrate = mnuXmlRigBaudrate->value(); - progdefaults.RigCatStopbits = valRigCatStopbits->value(); + progdefaults.RigCatStopbits = static_cast(valRigCatStopbits->value()); progdefaults.RigCatRTSplus = btnRigCatRTSplus->value(); progdefaults.RigCatDTRplus = btnRigCatDTRplus->value(); progdefaults.RigCatRTSptt = btnRigCatRTSptt->value(); progdefaults.RigCatDTRptt = btnRigCatDTRptt->value(); progdefaults.RigCatRTSCTSflow = chkRigCatRTSCTSflow->value(); - progdefaults.RigCatRetries = cntRigCatRetries->value(); - progdefaults.RigCatTimeout = cntRigCatTimeout->value(); - progdefaults.RigCatWait = cntRigCatWait->value(); + progdefaults.RigCatRetries = static_cast(cntRigCatRetries->value()); + progdefaults.RigCatTimeout = static_cast(cntRigCatTimeout->value()); + progdefaults.RigCatWait = static_cast(cntRigCatWait->value()); progdefaults.RigCatECHO = btnRigCatEcho->value(); progdefaults.RigCatCMDptt = btnRigCatCMDptt->value(); } @@ -1199,7 +1199,7 @@ static void *rigCAT_loop(void *args) sMode = rigCAT_getmode(); pthread_mutex_unlock(&rigCAT_mutex); - if ((freq > 0) && (freq != llFreq)) { + if ((freq >= 0) && (freq != llFreq)) { llFreq = freq; show_frequency(freq); wf->rfcarrier(freq); diff --git a/src/rigcontrol/rigsupport.cxx b/src/rigcontrol/rigsupport.cxx index cde060d5..99890da8 100644 --- a/src/rigcontrol/rigsupport.cxx +++ b/src/rigcontrol/rigsupport.cxx @@ -392,7 +392,9 @@ void qso_selectFreq() } // transceiver frequency if (freqlist[n].rfcarrier > 0) { - qsoFreqDisp->value(freqlist[n].rfcarrier); + qsoFreqDisp1->value(freqlist[n].rfcarrier); + qsoFreqDisp2->value(freqlist[n].rfcarrier); + qsoFreqDisp3->value(freqlist[n].rfcarrier); sendFreq(freqlist[n].rfcarrier); } // modem type & audio sub carrier @@ -479,7 +481,7 @@ bool init_NoRig_RigDialog() clearList(); buildlist(); - windowTitle = _("Rig Not Specified"); + windowTitle = _("Enter Xcvr Freq"); setTitle(); return true; diff --git a/src/rsid/rsid.cxx b/src/rsid/rsid.cxx index 3f226bb4..58241d05 100644 --- a/src/rsid/rsid.cxx +++ b/src/rsid/rsid.cxx @@ -120,15 +120,15 @@ LOG_FILE_SOURCE(debug::LOG_MODEM); ELEM_(47, THROBX_2, MODE_THROBX2) \ ELEM_(146, THROBX_4, MODE_THROBX4) \ \ - ELEM_(49, CONTESTIA_8_250, NUM_MODES) \ - ELEM_(50, CONTESTIA_16_500, NUM_MODES) \ - ELEM_(51, CONTESTIA_32_1000, NUM_MODES) \ - ELEM_(52, CONTESTIA_8_500, NUM_MODES) \ - ELEM_(53, CONTESTIA_16_1000, NUM_MODES) \ - ELEM_(54, CONTESTIA_4_500, NUM_MODES) \ - ELEM_(55, CONTESTIA_4_250, NUM_MODES) \ - ELEM_(117, CONTESTIA_8_1000, NUM_MODES) \ - ELEM_(169, CONTESTIA_8_125, NUM_MODES) \ + ELEM_(49, CONTESTIA_8_250, MODE_CONTESTIA) \ + ELEM_(50, CONTESTIA_16_500, MODE_CONTESTIA) \ + ELEM_(51, CONTESTIA_32_1000, MODE_CONTESTIA) \ + ELEM_(52, CONTESTIA_8_500, MODE_CONTESTIA) \ + ELEM_(53, CONTESTIA_16_1000, MODE_CONTESTIA) \ + ELEM_(54, CONTESTIA_4_500, MODE_CONTESTIA) \ + ELEM_(55, CONTESTIA_4_250, MODE_CONTESTIA) \ + ELEM_(117, CONTESTIA_8_1000, MODE_CONTESTIA) \ + ELEM_(169, CONTESTIA_8_125, MODE_CONTESTIA) \ \ ELEM_(56, VOICE, NUM_MODES) \ \ @@ -571,6 +571,52 @@ void cRsId::apply(int iSymbol, int iBin) progdefaults.oliviabw = 0; REQ(&set_olivia_tab_widgets); break; + // contestia parameters + case RSID_CONTESTIA_8_125: + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 0; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_4_250: + progdefaults.contestiatones = 1; + progdefaults.contestiabw = 1; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_8_250: + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 1; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_4_500: + progdefaults.contestiatones = 1; + progdefaults.contestiabw = 2; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_8_500: + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 2; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_16_500: + progdefaults.contestiatones = 3; + progdefaults.contestiabw = 2; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_8_1000: + progdefaults.contestiatones = 2; + progdefaults.contestiabw = 3; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_16_1000: + progdefaults.contestiatones = 3; + progdefaults.contestiabw = 3; + REQ(&set_contestia_tab_widgets); + break; + case RSID_CONTESTIA_32_1000: + progdefaults.contestiatones = 4; + progdefaults.contestiabw = 3; + REQ(&set_contestia_tab_widgets); + break; // mt63 case RSID_MT63_500_LG: case RSID_MT63_1000_LG: case RSID_MT63_2000_LG: progdefaults.mt63_interleave = 64; @@ -749,6 +795,29 @@ void cRsId::send(bool preRSID) rmode = RSID_OLIVIA_16_500; break; + case MODE_CONTESTIA: + if (progdefaults.contestiatones == 2 && progdefaults.contestiabw == 1) + rmode = RSID_CONTESTIA_8_250; + else if (progdefaults.contestiatones == 3 && progdefaults.contestiabw == 2) + rmode = RSID_CONTESTIA_16_500; + else if (progdefaults.contestiatones == 4 && progdefaults.contestiabw == 3) + rmode = RSID_CONTESTIA_32_1000; + else if (progdefaults.contestiatones == 2 && progdefaults.contestiabw == 2) + rmode = RSID_CONTESTIA_8_500; + else if (progdefaults.contestiatones == 3 && progdefaults.contestiabw == 3) + rmode = RSID_CONTESTIA_16_1000; + else if (progdefaults.contestiatones == 1 && progdefaults.contestiabw == 2) + rmode = RSID_CONTESTIA_4_500; + else if (progdefaults.contestiatones == 1 && progdefaults.contestiabw == 1) + rmode = RSID_CONTESTIA_4_250; + else if (progdefaults.contestiatones == 2 && progdefaults.contestiabw == 3) + rmode = RSID_CONTESTIA_8_1000; + else if (progdefaults.contestiatones == 2 && progdefaults.contestiabw == 0) + rmode = RSID_CONTESTIA_8_125; + else + rmode = RSID_CONTESTIA_16_500; + break; + case MODE_DOMINOEX4: if (progdefaults.DOMINOEX_FEC) rmode = RSID_DOMINOEX_4_FEC; diff --git a/src/spot/notify.cxx b/src/spot/notify.cxx index e908b0e4..f6f22b6c 100644 --- a/src/spot/notify.cxx +++ b/src/spot/notify.cxx @@ -467,8 +467,8 @@ static void notify_gui_to_event(notify_t& n) n.action.rx_marker = inpNotifyActionRXMarker->value(); n.action.macro = inpNotifyActionMacro->value(); n.action.program = inpNotifyActionProgram->value(); - n.action.trigger_limit = cntNotifyActionLimit->value(); - n.action.alert_timeout = cntNotifyActionDialogTimeout->value(); + n.action.trigger_limit = static_cast(cntNotifyActionLimit->value()); + n.action.alert_timeout = static_cast(cntNotifyActionDialogTimeout->value()); // filter if (chkNotifyFilterCall->value()) { @@ -491,7 +491,7 @@ static void notify_gui_to_event(notify_t& n) // dup n.dup_ignore = chkNotifyDupIgnore->value(); n.dup_ref = mnuNotifyDupWhich->value(); - n.dup.when = cntNotifyDupTime->value(); + n.dup.when = static_cast(cntNotifyDupTime->value()); n.dup.band = chkNotifyDupBand->value() ? NUM_BANDS : static_cast(0); n.dup.mode = chkNotifyDupMode->value() ? NUM_MODES : static_cast(0); } diff --git a/src/spot/spot.cxx b/src/spot/spot.cxx index 295457d3..56aa35ce 100644 --- a/src/spot/spot.cxx +++ b/src/spot/spot.cxx @@ -66,15 +66,19 @@ static tr1::unordered_map buffers; static cblist_t cblist; static rcblist_t rcblist; -void spot_recv(char c, int decoder, int afreq) +void spot_recv(char c, int decoder, int afreq, int md) { static trx_mode last_mode = NUM_MODES + 1; - if (decoder == -1) // mode without multiple decoders + if (decoder == -1) { // mode without multiple decoders decoder = active_modem->get_mode(); - if (last_mode != active_modem->get_mode()) { + if (last_mode != active_modem->get_mode()) { + buffers.clear(); + last_mode = active_modem->get_mode(); + } + } else if (last_mode != md) { buffers.clear(); - last_mode = active_modem->get_mode(); + last_mode = md; } if (afreq == 0) afreq = active_modem->get_freq(); diff --git a/src/ssb/ssb.cxx b/src/ssb/ssb.cxx new file mode 100644 index 00000000..5467c0ca --- /dev/null +++ b/src/ssb/ssb.cxx @@ -0,0 +1,81 @@ +// ---------------------------------------------------------------------------- +// ssb.cxx -- ssb modem +// +// Copyright (C) 2010 +// Dave Freese, W1HKJ +// +// 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 . +// ---------------------------------------------------------------------------- + +#include + +#include "ssb.h" +#include "modem.h" +#include "digiscope.h" +#include "fl_digi.h" + +#include "debug.h" + +#define ssb_bw 4 + +void ssb::tx_init(SoundBase *sc) +{ +} + +void ssb::rx_init() +{ + put_MODEstatus(mode); +} + +void ssb::init() +{ + modem::init(); + rx_init(); + set_scope_mode(Digiscope::BLANK); +} + +ssb::~ssb() +{ +} + +void ssb::restart() +{ + set_bandwidth(ssb_bw); +} + +ssb::ssb() +{ + mode = MODE_SSB; + samplerate = 8000; + cap &= ~CAP_TX; + restart(); +} + +// dummy process +int ssb::rx_process(const double *buf, int len) +{ + return 0; +} + +//===================================================================== +// ssb transmit +// dummy process +//===================================================================== + +int ssb::tx_process() +{ + return -1; +} diff --git a/src/thor/thor.cxx b/src/thor/thor.cxx index 521778ca..128060cf 100644 --- a/src/thor/thor.cxx +++ b/src/thor/thor.cxx @@ -151,7 +151,7 @@ thor::~thor() thor::thor(trx_mode md) { - cap = CAP_REV; + cap |= CAP_REV; mode = md; diff --git a/src/throb/throb.cxx b/src/throb/throb.cxx index 744f1962..d7ed701d 100644 --- a/src/throb/throb.cxx +++ b/src/throb/throb.cxx @@ -119,7 +119,7 @@ void throb::reset_syms() //call when switching from TX to RX or vice versa throb::throb(trx_mode throb_mode) : modem() { - cap = CAP_AFC | CAP_REV; + cap |= CAP_AFC | CAP_REV; double bw; double *fp = 0; @@ -342,7 +342,7 @@ int throb::findtones(complex *word, int &tone1, int &tone2) void throb::show_char(int c) { if (metric > progStatus.sldrSquelchValue || progStatus.sqlonoff == false) - put_rx_char(c); + put_rx_char(progdefaults.rx_lowercase ? tolower(c) : c); } void throb::decodechar(int tone1, int tone2) @@ -706,7 +706,7 @@ int throb::tx_process() } send(sym); - put_echo_char(c); + put_echo_char(progdefaults.rx_lowercase ? tolower(c) : c); return 0; } diff --git a/src/trx/modem.cxx b/src/trx/modem.cxx index 9d90afa1..5a02921b 100644 --- a/src/trx/modem.cxx +++ b/src/trx/modem.cxx @@ -85,6 +85,7 @@ modem *psk250r_modem = 0; modem *psk500r_modem = 0; modem *olivia_modem = 0; +modem *contestia_modem = 0; modem *rtty_modem = 0; @@ -112,6 +113,7 @@ modem *throbx4_modem = 0; modem *wwv_modem = 0; modem *anal_modem = 0; +modem *ssb_modem = 0; trx_mode modem::get_mode() { @@ -127,13 +129,14 @@ modem::modem() bool wfsb = wf->USB(); reverse = wfrev ^ !wfsb; historyON = false; - cap = 0; + cap = CAP_RX | CAP_TX; PTTphaseacc = 0.0; frequency = 1000.0; s2n_ncount = s2n_sum = s2n_sum2 = s2n_metric = 0.0; s2n_valid = false; } +// modem types CW and RTTY do not use the base init() void modem::init() { bool wfrev = wf->Reverse(); @@ -141,14 +144,7 @@ void modem::init() reverse = wfrev ^ !wfsb; if (progdefaults.StartAtSweetSpot) { -// if (active_modem == cw_modem) - if (this == cw_modem) - set_freq(progdefaults.CWsweetspot); -// else if (active_modem == rtty_modem) - else if (this == rtty_modem) - set_freq(progdefaults.RTTYsweetspot); - else - set_freq(progdefaults.PSKsweetspot); + set_freq(progdefaults.PSKsweetspot); } else if (progStatus.carrier != 0) { set_freq(progStatus.carrier); #if !BENCHMARK_MODE @@ -183,7 +179,9 @@ bool modem::freqlocked() double modem::get_txfreq(void) { - if (mailserver && progdefaults.PSKmailSweetSpot) + if (unlikely(!(cap & CAP_TX))) + return 0; + else if (mailserver && progdefaults.PSKmailSweetSpot) return progdefaults.PSKsweetspot; return tx_frequency; } diff --git a/src/waterfall/waterfall.cxx b/src/waterfall/waterfall.cxx index 75def7a0..acf0f523 100644 --- a/src/waterfall/waterfall.cxx +++ b/src/waterfall/waterfall.cxx @@ -71,6 +71,7 @@ #include "gettext.h" #include "rtty.h" #include "flslider2.h" +#include "debug.h" using namespace std; @@ -206,7 +207,7 @@ inline void WFdisp::makeMarker_(int width, const RGB* color, int freq, const RGB if (active_modem->get_mode() == MODE_RTTY) { // rtty has two bandwidth indicators on the waterfall // upper and lower frequency - int shift = (progdefaults.rtty_shift >= 0 ? + int shift = static_cast(progdefaults.rtty_shift >= 0 ? _SHIFT[progdefaults.rtty_shift] : progdefaults.rtty_custom_shift); int bw_limit_hi = (int)(shift / 2 + progdefaults.RTTY_BW / 2.0); int bw_limit_lo = (int)(shift / 2 - progdefaults.RTTY_BW / 2.0); @@ -285,10 +286,11 @@ void WFdisp::makeMarker() makeMarker_(marker_width, &RGBmarker, carrierfreq, clrMin, clrM, clrMax); if (unlikely(active_modem->freqlocked())) { - int txfreq = active_modem->get_txfreq(); + int txfreq = static_cast(active_modem->get_txfreq()); adjust_color_inv(RGBmarker.R, RGBmarker.G, RGBmarker.B, FL_BLACK, FL_RED); - makeMarker_(bandwidth / 2.0 + 1, &RGBmarker, txfreq, - clrMin, clrMin + (int)((double)txfreq + 0.5), clrMax); + makeMarker_( static_cast(bandwidth / 2.0 + 1), + &RGBmarker, txfreq, + clrMin, clrMin + (int)((double)txfreq + 0.5), clrMax); } if (!wantcursor) return; @@ -545,27 +547,29 @@ void WFdisp::redrawCursor() // cursormoved = true; } -void WFdisp::sig_data( double *sig, int len, int sr ) { +void WFdisp::sig_data( double *sig, int len, int sr ) +{ if (wfspeed == PAUSE) - return; + goto update_freq; -//if sound card sampling rate changed reset the waterfall buffer + // if sound card sampling rate changed reset the waterfall buffer if (srate != sr) { srate = sr; memset (circbuff, 0, FFT_LEN * 2 * sizeof(double)); ptrCB = 0; } - overload = false; - double overval, peak = 0.0; - for (int i = 0; i < len; i++) { - overval = fabs(circbuff[ptrCB] = sig[i]); - ptrCB = (ptrCB + 1) % (FFT_LEN *2); - if (overval > peak) peak = overval; + { + overload = false; + double overval, peak = 0.0; + for (int i = 0; i < len; i++) { + overval = fabs(circbuff[ptrCB] = sig[i]); + ptrCB = (ptrCB + 1) % (FFT_LEN *2); + if (overval > peak) peak = overval; + } + peakaudio = 0.1 * peak + 0.9 * peakaudio; } - peakaudio = 0.1 * peak + 0.9 * peakaudio; - if (mode == SCOPE) process_analog(circbuff, FFT_LEN * 2); else @@ -573,10 +577,8 @@ void WFdisp::sig_data( double *sig, int len, int sr ) { put_WARNstatus(peakaudio); +update_freq: static char szFrequency[14]; - -// if (usebands) -// rfc = (long long)(atof(cboBand->value()) * 1000.0); if (rfc != 0) { // use a boolean for the waterfall if (usb) dfreq = rfc + active_modem->get_txfreq(); @@ -587,11 +589,7 @@ void WFdisp::sig_data( double *sig, int len, int sr ) { dfreq = active_modem->get_txfreq(); snprintf(szFrequency, sizeof(szFrequency), "%-.0f", dfreq); } - FL_LOCK_D(); inpFreq->value(szFrequency); - FL_UNLOCK_D(); - - return; } @@ -694,7 +692,7 @@ int WFdisp::wfmag() { void WFdisp::drawScale() { - int fw = 60, xchar; + int fw = 60, xoff; static char szFreq[20]; double fr; uchar *pixmap; @@ -729,16 +727,16 @@ void WFdisp::drawScale() { snprintf(szFreq, sizeof(szFreq), "%7.1f", fr); fw = (int)fl_width(szFreq); if (progdefaults.wf_audioscale) - xchar = (int) (( (1000.0/step) * i - fw) / 2.0 - offset /step ); + xoff = (int) (( (1000.0/step) * i - fw) / 2.0 - offset /step ); else if (usb) - xchar = (int) ( ( (1000.0/step) * i - fw) / 2.0 - + xoff = (int) ( ( (1000.0/step) * i - fw) / 2.0 - (offset + rfc % 500) /step ); else - xchar = (int) ( ( (1000.0/step) * i - fw) / 2.0 - + xoff = (int) ( ( (1000.0/step) * i - fw) / 2.0 - (offset + 500 - rfc % 500) /step ); - if (xchar > 0 && xchar < w() - fw) - fl_draw(szFreq, x() + xchar, y() + 10 ); - if (xchar > w() - fw) break; + if (xoff > 0 && xoff < w() - fw) + fl_draw(szFreq, x() + xoff, y() + 10 ); + if (xoff > w() - fw) break; } } @@ -1107,6 +1105,9 @@ void qsy_cb(Fl_Widget *w, void *v) static vector qsy_stack; qrg_mode_t m; + wf->xmtlock->value(0); + wf->xmtlock->do_callback(); + if (Fl::event_button() != FL_RIGHT_MOUSE) { // store m.rfcarrier = wf->rfcarrier(); @@ -1171,6 +1172,11 @@ void xmtrcv_cb(Fl_Widget *w, void *vi) Fl_Light_Button *b = (Fl_Light_Button *)w; int v = b->value(); FL_UNLOCK_D(); + if (!(active_modem->get_cap() & modem::CAP_TX)) { + b->value(0); + restoreFocus(); + return; + } if (v == 1) { stopMacroTimer(); active_modem->set_stopflag(false); diff --git a/src/widgets/FTextRXTX.cxx b/src/widgets/FTextRXTX.cxx index 6084a078..523b3da1 100644 --- a/src/widgets/FTextRXTX.cxx +++ b/src/widgets/FTextRXTX.cxx @@ -1198,7 +1198,7 @@ void MVScrollbar::draw(void) // Convert stored scrollbar values to vertical positions and draw // lines inside the widget if they don't overlap with the knob area. for (vector::const_iterator i = marks.begin(); i != marks.end(); ++i) { - ypos = w() + H * i->pos / maximum(); + ypos = static_cast(w() + H * i->pos / maximum()); // Don't draw over slider knob if ((ypos > slider_y && ypos < slider_y + slider_h) || (ypos < slider_y + slider_h && ypos > slider_y)) diff --git a/src/widgets/FTextView.cxx b/src/widgets/FTextView.cxx index f07913bf..4e246ab3 100644 --- a/src/widgets/FTextView.cxx +++ b/src/widgets/FTextView.cxx @@ -67,12 +67,12 @@ FTextBase::FTextBase(int x, int y, int w, int h, const char *l) textsize(FL_NORMAL_SIZE); textcolor(FL_FOREGROUND_COLOR); - tbuf = new Fl_Text_Buffer; - sbuf = new Fl_Text_Buffer; + tbuf = new Fl_Text_Buffer_mod; + sbuf = new Fl_Text_Buffer_mod; - cursor_style(Fl_Text_Editor_mod::NORMAL_CURSOR); buffer(tbuf); highlight_data(sbuf, styles, NATTR, FTEXT_DEF, 0, 0); + cursor_style(Fl_Text_Editor_mod::NORMAL_CURSOR); wrap_mode(wrap, wrap_col); diff --git a/src/widgets/Fl_Text_Buffer_mod.cxx b/src/widgets/Fl_Text_Buffer_mod.cxx new file mode 100644 index 00000000..a778e81e --- /dev/null +++ b/src/widgets/Fl_Text_Buffer_mod.cxx @@ -0,0 +1,7 @@ +#include + +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# include "Fl_Text_Buffer_mod_1_1.cxx" +#elif FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +# include "Fl_Text_Buffer_mod_1_3.cxx" +#endif diff --git a/src/widgets/Fl_Text_Buffer_mod_1_1.cxx b/src/widgets/Fl_Text_Buffer_mod_1_1.cxx new file mode 100644 index 00000000..9fc64601 --- /dev/null +++ b/src/widgets/Fl_Text_Buffer_mod_1_1.cxx @@ -0,0 +1,2456 @@ +// +// "$Id: Fl_Text_Buffer.cxx 6011 2008-01-04 20:32:37Z matt $" +// +// Copyright 2001-2005 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#include +#include +#include "flstring.h" +#include +#include +#include "Fl_Text_Buffer_mod.H" + + +#define PREFERRED_GAP_SIZE 1024 +/* Initial size for the buffer gap (empty space +in the buffer where text might be inserted +if the user is typing sequential chars ) */ + +static void histogramCharacters( const char *string, int length, char hist[ 256 ], + int init ); +static void subsChars( char *string, int length, char fromChar, char toChar ); +static char chooseNullSubsChar( char hist[ 256 ] ); +static void insertColInLine( const char *line, char *insLine, int column, int insWidth, + int tabDist, int useTabs, char nullSubsChar, char *outStr, int *outLen, + int *endOffset ); +static void deleteRectFromLine( const char *line, int rectStart, int rectEnd, + int tabDist, int useTabs, char nullSubsChar, char *outStr, int *outLen, + int *endOffset ); +static void overlayRectInLine( const char *line, char *insLine, int rectStart, + int rectEnd, int tabDist, int useTabs, char nullSubsChar, char *outStr, + int *outLen, int *endOffset ); + +static void addPadding( char *string, int startIndent, int toIndent, + int tabDist, int useTabs, char nullSubsChar, int *charsAdded ); +static char *copyLine( const char* text, int *lineLen ); +static int countLines( const char *string ); +static int textWidth( const char *text, int tabDist, char nullSubsChar ); +static char *realignTabs( const char *text, int origIndent, int newIndent, + int tabDist, int useTabs, char nullSubsChar, int *newLength ); +static char *expandTabs( const char *text, int startIndent, int tabDist, + char nullSubsChar, int *newLen ); +static char *unexpandTabs( char *text, int startIndent, int tabDist, + char nullSubsChar, int *newLen ); +static int max( int i1, int i2 ); +static int min( int i1, int i2 ); + +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; +static Fl_Text_Buffer_mod* undowidget; +static int undoat; // points after insertion +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 + +static void undobuffersize(int n) { + if (n > undobufferlength) { + if (undobuffer) { + do {undobufferlength *= 2;} while (undobufferlength < n); + undobuffer = (char*)realloc(undobuffer, undobufferlength); + } else { + undobufferlength = n+9; + undobuffer = (char*)malloc(undobufferlength); + } + } +} + +/* +** Create an empty text buffer of a pre-determined size (use this to +** avoid unnecessary re-allocation if you know exactly how much the buffer +** will need to hold +*/ +Fl_Text_Buffer_mod::Fl_Text_Buffer_mod( int requestedSize ) { + mLength = 0; + mBuf = (char *)malloc( requestedSize + PREFERRED_GAP_SIZE ); + mGapStart = 0; + mGapEnd = PREFERRED_GAP_SIZE; + 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; + mNodifyProcs = NULL; + mCbArgs = NULL; + mNModifyProcs = 0; + mNPredeleteProcs = 0; + mPredeleteProcs = NULL; + mPredeleteCbArgs = NULL; + mCursorPosHint = 0; + mNullSubsChar = '\0'; + mCanUndo = 1; +#ifdef PURIFY +{ int i; for (i = mGapStart; i < mGapEnd; i++) mBuf[ i ] = '.'; } +#endif +} + +/* +** Free a text buffer +*/ +Fl_Text_Buffer_mod::~Fl_Text_Buffer_mod() { + free( mBuf ); + if ( mNModifyProcs != 0 ) { + delete[] mNodifyProcs; + delete[] mCbArgs; + } + if ( mNPredeleteProcs != 0 ) { + delete[] mPredeleteProcs; + delete[] mPredeleteCbArgs; + } +} + +/* +** Get the entire contents of a text buffer. Memory is allocated to contain +** the returned string, which the caller must free. +*/ +char * Fl_Text_Buffer_mod::text() { + char *t; + + t = (char *)malloc( mLength + 1 ); + memcpy( t, mBuf, mGapStart ); + memcpy( &t[ mGapStart ], &mBuf[ mGapEnd ], + mLength - mGapStart ); + t[ mLength ] = '\0'; + return t; +} + +/* +** Replace the entire contents of the text buffer +*/ +void Fl_Text_Buffer_mod::text( const char *t ) { + int insertedLength, deletedLength; + const char *deletedText; + + call_predelete_callbacks(0, length()); + + /* Save information for redisplay, and get rid of the old buffer */ + deletedText = text(); + deletedLength = mLength; + free( (void *)mBuf ); + + /* Start a new buffer with a gap of PREFERRED_GAP_SIZE in the center */ + insertedLength = strlen( t ); + mBuf = (char *)malloc( insertedLength + PREFERRED_GAP_SIZE ); + mLength = insertedLength; + mGapStart = insertedLength / 2; + mGapEnd = mGapStart + PREFERRED_GAP_SIZE; + memcpy( mBuf, t, mGapStart ); + memcpy( &mBuf[ mGapEnd ], &t[ mGapStart ], insertedLength - mGapStart ); +#ifdef PURIFY +{ int i; for ( i = mGapStart; i < mGapEnd; i++ ) mBuf[ i ] = '.'; } +#endif + + /* Zero all of the existing selections */ + update_selections( 0, deletedLength, 0 ); + + /* Call the saved display routine(s) to update the screen */ + call_modify_callbacks( 0, deletedLength, insertedLength, 0, deletedText ); + free( (void *)deletedText ); +} + +/* +** Return a copy of the text between "start" and "end" character positions +** from text buffer "buf". Positions start at 0, and the range does not +** include the character pointed to by "end" +*/ +char * Fl_Text_Buffer_mod::text_range( int start, int end ) { + char * s; + int copiedLength, part1Length; + + /* Make sure start and end are ok, and allocate memory for returned string. + If start is bad, return "", if end is bad, adjust it. */ + if ( start < 0 || start > mLength ) { + s = (char *)malloc( 1 ); + s[ 0 ] = '\0'; + return s; + } + if ( end < start ) { + int temp = start; + start = end; + end = temp; + } + if ( end > mLength ) + end = mLength; + copiedLength = end - start; + s = (char *)malloc( copiedLength + 1 ); + + /* Copy the text from the buffer to the returned string */ + if ( end <= mGapStart ) { + memcpy( s, &mBuf[ start ], copiedLength ); + } else if ( start >= mGapStart ) { + memcpy( s, &mBuf[ start + ( mGapEnd - mGapStart ) ], copiedLength ); + } else { + part1Length = mGapStart - start; + memcpy( s, &mBuf[ start ], part1Length ); + memcpy( &s[ part1Length ], &mBuf[ mGapEnd ], copiedLength - part1Length ); + } + s[ copiedLength ] = '\0'; + return s; +} + +/* +** Return the character at buffer position "pos". Positions start at 0. +*/ +char Fl_Text_Buffer_mod::character( int pos ) { + if ( pos < 0 || pos >= mLength ) + return '\0'; + if ( pos < mGapStart ) + return mBuf[ pos ]; + else + return mBuf[ pos + mGapEnd - mGapStart ]; +} + +/* +** Insert null-terminated string "text" at position "pos" in "buf" +*/ +void Fl_Text_Buffer_mod::insert( int pos, const char *s ) { + int nInserted; + + /* if pos is not contiguous to existing text, make it */ + if ( pos > mLength ) pos = mLength; + if ( pos < 0 ) pos = 0; + + /* Even if nothing is deleted, we must call these callbacks */ + call_predelete_callbacks( pos, 0 ); + + /* insert and redisplay */ + nInserted = insert_( pos, s ); + mCursorPosHint = pos + nInserted; + call_modify_callbacks( pos, 0, nInserted, 0, NULL ); +} + +/* +** Delete the characters between "start" and "end", and insert the +** null-terminated string "text" in their place in in "buf" +*/ +void Fl_Text_Buffer_mod::replace( int start, int end, const char *s ) { + const char * deletedText; + int nInserted; + + // Range check... + if (!s) return; + if (start < 0) start = 0; + if (end > mLength) end = mLength; + + call_predelete_callbacks( start, end-start ); + deletedText = text_range( start, end ); + remove_( start, end ); + //undoyankcut = undocut; + nInserted = insert_( start, s ); + mCursorPosHint = start + nInserted; + call_modify_callbacks( start, end - start, nInserted, 0, deletedText ); + free( (void *)deletedText ); +} + +void Fl_Text_Buffer_mod::remove( int start, int end ) { + const char * deletedText; + + /* Make sure the arguments make sense */ + if ( start > end ) { + int temp = start; + start = end; + end = temp; + } + if ( start > mLength ) start = mLength; + if ( start < 0 ) start = 0; + if ( end > mLength ) end = mLength; + if ( end < 0 ) end = 0; + + if (start == end) return; + + call_predelete_callbacks( start, end-start ); + /* Remove and redisplay */ + deletedText = text_range( start, end ); + remove_( start, end ); + mCursorPosHint = start; + call_modify_callbacks( start, end - start, 0, 0, deletedText ); + free( (void *)deletedText ); +} + +void Fl_Text_Buffer_mod::copy( Fl_Text_Buffer_mod *fromBuf, int fromStart, + int fromEnd, int toPos ) { + int copiedLength = fromEnd - fromStart; + int part1Length; + + /* Prepare the buffer to receive the new text. If the new text fits in + the current buffer, just move the gap (if necessary) to where + the text should be inserted. If the new text is too large, reallocate + the buffer with a gap large enough to accomodate the new text and a + gap of PREFERRED_GAP_SIZE */ + if ( copiedLength > mGapEnd - mGapStart ) + reallocate_with_gap( toPos, copiedLength + PREFERRED_GAP_SIZE ); + else if ( toPos != mGapStart ) + move_gap( toPos ); + + /* Insert the new text (toPos now corresponds to the start of the gap) */ + if ( fromEnd <= fromBuf->mGapStart ) { + memcpy( &mBuf[ toPos ], &fromBuf->mBuf[ fromStart ], copiedLength ); + } else if ( fromStart >= fromBuf->mGapStart ) { + memcpy( &mBuf[ toPos ], + &fromBuf->mBuf[ fromStart + ( fromBuf->mGapEnd - fromBuf->mGapStart ) ], + copiedLength ); + } else { + part1Length = fromBuf->mGapStart - fromStart; + memcpy( &mBuf[ toPos ], &fromBuf->mBuf[ fromStart ], part1Length ); + memcpy( &mBuf[ toPos + part1Length ], &fromBuf->mBuf[ fromBuf->mGapEnd ], + copiedLength - part1Length ); + } + mGapStart += copiedLength; + mLength += copiedLength; + update_selections( toPos, 0, copiedLength ); +} + +/* +** remove text according to the undo variables or insert text +** from the undo buffer +*/ +int Fl_Text_Buffer_mod::undo(int *cursorPos) { + if (undowidget != this || (!undocut && !undoinsert && !mCanUndo)) return 0; + + int ilen = undocut; + int xlen = undoinsert; + int b = undoat-xlen; + + if (xlen && undoyankcut && !ilen) { + ilen = undoyankcut; + } + + if (xlen && ilen) { + undobuffersize(ilen+1); + undobuffer[ilen] = 0; + char *tmp = strdup(undobuffer); + replace(b, undoat, tmp); + if (cursorPos) *cursorPos = mCursorPosHint; + free(tmp); + } + else if (xlen) { + remove(b, undoat); + if (cursorPos) *cursorPos = mCursorPosHint; + } + else if (ilen) { + undobuffersize(ilen+1); + undobuffer[ilen] = 0; + insert(undoat, undobuffer); + if (cursorPos) *cursorPos = mCursorPosHint; + undoyankcut = 0; + } + + return 1; +} + +/* +** let the undo system know if we can undo changes +*/ +void Fl_Text_Buffer_mod::canUndo(char flag) { + mCanUndo = flag; +} + +/* +** Insert "text" columnwise into buffer starting at displayed character +** position "column" on the line beginning at "startPos". Opens a rectangular +** space the width and height of "text", by moving all text to the right of +** "column" right. If charsInserted and charsDeleted are not NULL, the +** number of characters inserted and deleted in the operation (beginning +** at startPos) are returned in these arguments +*/ +void Fl_Text_Buffer_mod::insert_column( int column, int startPos, const char *s, + int *charsInserted, int *charsDeleted ) { + int nLines, lineStartPos, nDeleted, insertDeleted, nInserted; + const char *deletedText; + + nLines = countLines( s ); + lineStartPos = line_start( startPos ); + nDeleted = line_end( skip_lines( startPos, nLines ) ) - + lineStartPos; + call_predelete_callbacks( lineStartPos, nDeleted ); + deletedText = text_range( lineStartPos, lineStartPos + nDeleted ); + insert_column_( column, lineStartPos, s, &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; +} + +/* +** Overlay "text" between displayed character positions "rectStart" and +** "rectEnd" on the line beginning at "startPos". If charsInserted and +** charsDeleted are not NULL, the number of characters inserted and deleted +** in the operation (beginning at startPos) are returned in these arguments. +*/ +void Fl_Text_Buffer_mod::overlay_rectangular( int startPos, int rectStart, + int rectEnd, const char *s, int *charsInserted, int *charsDeleted ) { + int nLines, lineStartPos, nDeleted, insertDeleted, nInserted; + const char *deletedText; + + nLines = countLines( s ); + lineStartPos = line_start( startPos ); + nDeleted = line_end( skip_lines( startPos, nLines ) ) - + lineStartPos; + call_predelete_callbacks( lineStartPos, nDeleted ); + deletedText = text_range( lineStartPos, lineStartPos + nDeleted ); + overlay_rectangular_( lineStartPos, rectStart, rectEnd, s, &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; +} + +/* +** Replace a rectangular area in buf, given by "start", "end", "rectStart", +** and "rectEnd", with "text". If "text" is vertically longer than the +** rectangle, add extra lines to make room for it. +*/ +void Fl_Text_Buffer_mod::replace_rectangular( int start, int end, int rectStart, + int rectEnd, const char *s ) { + char *insPtr; + const char *deletedText; + char *insText = (char *)""; + int i, nInsertedLines, nDeletedLines, insLen, hint; + int insertDeleted, insertInserted, deleteInserted; + 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" */ + nInsertedLines = countLines( s ); + nDeletedLines = count_lines( start, end ); + if ( nInsertedLines < nDeletedLines ) { + insLen = strlen( s ); + insText = (char *)malloc( insLen + nDeletedLines - nInsertedLines + 1 ); + strcpy( insText, s ); + insPtr = insText + insLen; + for ( i = 0; i < nDeletedLines - nInsertedLines; i++ ) + *insPtr++ = '\n'; + *insPtr = '\0'; + } else if ( nDeletedLines < nInsertedLines ) { + linesPadded = nInsertedLines - nDeletedLines; + for ( 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 */ + deletedText = text_range( start, end ); + + /* Delete then insert */ + 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 ); +} + +/* +** Remove a rectangular swath of characters between character positions start +** and end and horizontal displayed-character offsets rectStart and rectEnd. +*/ +void Fl_Text_Buffer_mod::remove_rectangular( int start, int end, int rectStart, + int rectEnd ) { + const char * deletedText; + int nInserted; + + start = line_start( start ); + end = line_end( end ); + call_predelete_callbacks( start, end-start ); + deletedText = text_range( start, end ); + remove_rectangular_( start, end, rectStart, rectEnd, &nInserted, + &mCursorPosHint ); + call_modify_callbacks( start, end - start, nInserted, 0, deletedText ); + free( (void *) deletedText ); +} + +/* +** Clear a rectangular "hole" out of the buffer between character positions +** start and end and horizontal displayed-character offsets rectStart and +** rectEnd. +*/ +void Fl_Text_Buffer_mod::clear_rectangular( int start, int end, int rectStart, + int rectEnd ) { + int i, nLines; + char *newlineString; + + nLines = count_lines( start, end ); + newlineString = (char *)malloc( nLines + 1 ); + 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 ) { + int lineStart, selLeft, selRight, len; + char *textOut, *outPtr, *retabbedStr; + const char *textIn; + + start = line_start( start ); + end = line_end( end ); + textOut = (char *)malloc( ( end - start ) + 1 ); + lineStart = start; + outPtr = textOut; + while ( lineStart <= end ) { + rectangular_selection_boundaries( lineStart, rectStart, rectEnd, + &selLeft, &selRight ); + textIn = text_range( selLeft, selRight ); + 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 */ + retabbedStr = realignTabs( textOut, rectStart, 0, mTabDist, + mUseTabs, mNullSubsChar, &len ); + free( (void *) textOut ); + return retabbedStr; +} + +/* +** Set the hardware tab distance used by all displays for this buffer, +** and used in computing offsets for rectangular selection operations. +*/ +void Fl_Text_Buffer_mod::tab_distance( int tabDist ) { + const char * deletedText; + + /* First call the pre-delete callbacks with the previous tab setting + still active. */ + call_predelete_callbacks( 0, mLength ); + + /* Change the tab setting */ + mTabDist = tabDist; + + /* Force any display routines to redisplay everything (unfortunately, + this means copying the whole buffer contents to provide "deletedText" */ + deletedText = text(); + call_modify_callbacks( 0, mLength, mLength, 0, deletedText ); + free( (void *) deletedText ); +} + +void Fl_Text_Buffer_mod::select( int start, int end ) { + Fl_Text_Selection oldSelection = mPrimary; + + mPrimary.set( start, end ); + redisplay_selection( &oldSelection, &mPrimary ); +} + +void Fl_Text_Buffer_mod::unselect() { + Fl_Text_Selection 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 ); +} + +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 ); +} + +char * Fl_Text_Buffer_mod::selection_text() { + return selection_text_( &mPrimary ); +} + +void Fl_Text_Buffer_mod::remove_selection() { + remove_selection_( &mPrimary ); +} + +void Fl_Text_Buffer_mod::replace_selection( const char *s ) { + replace_selection_( &mPrimary, s ); +} + +void Fl_Text_Buffer_mod::secondary_select( int start, int end ) { + Fl_Text_Selection oldSelection = mSecondary; + + mSecondary.set( start, end ); + redisplay_selection( &oldSelection, &mSecondary ); +} + +void Fl_Text_Buffer_mod::secondary_unselect() { + Fl_Text_Selection 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 ); +} + +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 ); +} + +char * Fl_Text_Buffer_mod::secondary_selection_text() { + return selection_text_( &mSecondary ); +} + +void Fl_Text_Buffer_mod::remove_secondary_selection() { + remove_selection_( &mSecondary ); +} + +void Fl_Text_Buffer_mod::replace_secondary_selection( const char *s ) { + replace_selection_( &mSecondary, s ); +} + +void Fl_Text_Buffer_mod::highlight( int start, int end ) { + Fl_Text_Selection oldSelection = mHighlight; + + mHighlight.set( start, end ); + redisplay_selection( &oldSelection, &mHighlight ); +} + +void Fl_Text_Buffer_mod::unhighlight() { + Fl_Text_Selection 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 ); +} + +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 ); +} + +char * Fl_Text_Buffer_mod::highlight_text() { + return selection_text_( &mHighlight ); +} + +/* +** Add a callback routine to be called when the buffer is modified +*/ +void Fl_Text_Buffer_mod::add_modify_callback( Fl_Text_Modify_Cb bufModifiedCB, + void *cbArg ) { + Fl_Text_Modify_Cb * newModifyProcs; + void **newCBArgs; + int i; + + newModifyProcs = new Fl_Text_Modify_Cb [ mNModifyProcs + 1 ]; + newCBArgs = new void * [ mNModifyProcs + 1 ]; + for ( i = 0; i < mNModifyProcs; i++ ) { + newModifyProcs[ i + 1 ] = mNodifyProcs[ i ]; + newCBArgs[ i + 1 ] = mCbArgs[ i ]; + } + if ( mNModifyProcs != 0 ) { + delete [] mNodifyProcs; + delete [] mCbArgs; + } + newModifyProcs[ 0 ] = bufModifiedCB; + newCBArgs[ 0 ] = cbArg; + mNModifyProcs++; + mNodifyProcs = newModifyProcs; + mCbArgs = newCBArgs; +} + +void Fl_Text_Buffer_mod::remove_modify_callback( Fl_Text_Modify_Cb bufModifiedCB, + void *cbArg ) { + int i, toRemove = -1; + Fl_Text_Modify_Cb *newModifyProcs; + void **newCBArgs; + + /* find the matching callback to remove */ + for ( i = 0; i < mNModifyProcs; i++ ) { + if ( mNodifyProcs[ i ] == bufModifiedCB && mCbArgs[ i ] == cbArg ) { + toRemove = i; + break; + } + } + if ( toRemove == -1 ) { + Fl::error("Fl_Text_Buffer_mod::remove_modify_callback(): Can't find modify CB to remove"); + return; + } + + /* Allocate new lists for remaining callback procs and args (if + any are left) */ + mNModifyProcs--; + if ( mNModifyProcs == 0 ) { + mNModifyProcs = 0; + delete[] mNodifyProcs; + mNodifyProcs = NULL; + delete[] mCbArgs; + mCbArgs = NULL; + return; + } + newModifyProcs = new Fl_Text_Modify_Cb [ mNModifyProcs ]; + newCBArgs = new void * [ mNModifyProcs ]; + + /* copy out the remaining members and free the old lists */ + for ( i = 0; i < toRemove; i++ ) { + newModifyProcs[ i ] = mNodifyProcs[ i ]; + newCBArgs[ i ] = mCbArgs[ i ]; + } + for ( ; i < mNModifyProcs; i++ ) { + newModifyProcs[ i ] = mNodifyProcs[ i + 1 ]; + newCBArgs[ i ] = mCbArgs[ i + 1 ]; + } + delete[] mNodifyProcs; + delete[] mCbArgs; + mNodifyProcs = newModifyProcs; + mCbArgs = newCBArgs; +} + +/* +** Add a callback routine to be called before text is deleted from the buffer. +*/ +void Fl_Text_Buffer_mod::add_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, + void *cbArg) { + Fl_Text_Predelete_Cb *newPreDeleteProcs; + void **newCBArgs; + int i; + + newPreDeleteProcs = new Fl_Text_Predelete_Cb[ mNPredeleteProcs + 1 ]; + newCBArgs = new void * [ mNPredeleteProcs + 1 ]; + for ( i = 0; i < mNPredeleteProcs; i++ ) { + newPreDeleteProcs[i + 1] = mPredeleteProcs[i]; + newCBArgs[i + 1] = mPredeleteCbArgs[i]; + } + if (! mNPredeleteProcs != 0) { + delete [] mPredeleteProcs; + delete [] mPredeleteCbArgs; + } + newPreDeleteProcs[0] = bufPreDeleteCB; + newCBArgs[0] = cbArg; + mNPredeleteProcs++; + mPredeleteProcs = newPreDeleteProcs; + mPredeleteCbArgs = newCBArgs; +} + +void Fl_Text_Buffer_mod::remove_predelete_callback( + Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg) { + int i, toRemove = -1; + Fl_Text_Predelete_Cb *newPreDeleteProcs; + void **newCBArgs; + + /* find the matching callback to remove */ + for ( i = 0; i < mNPredeleteProcs; i++) { + if (mPredeleteProcs[i] == bufPreDeleteCB && + mPredeleteCbArgs[i] == cbArg) { + toRemove = i; + break; + } + } + if (toRemove == -1) { + Fl::error("Fl_Text_Buffer_mod::remove_predelete_callback(): Can't find pre-delete CB to remove"); + return; + } + + /* Allocate new lists for remaining callback procs and args (if + any are left) */ + mNPredeleteProcs--; + if (mNPredeleteProcs == 0) { + mNPredeleteProcs = 0; + delete[] mPredeleteProcs; + mPredeleteProcs = NULL; + delete[] mPredeleteCbArgs; + mPredeleteCbArgs = NULL; + return; + } + newPreDeleteProcs = new Fl_Text_Predelete_Cb [ mNPredeleteProcs ]; + newCBArgs = new void * [ mNPredeleteProcs ]; + + /* copy out the remaining members and free the old lists */ + for ( i = 0; i < toRemove; i++) { + newPreDeleteProcs[i] = mPredeleteProcs[i]; + newCBArgs[i] = mPredeleteCbArgs[i]; + } + for ( ; i < mNPredeleteProcs; i++) { + newPreDeleteProcs[i] = mPredeleteProcs[i+1]; + newCBArgs[i] = mPredeleteCbArgs[i+1]; + } + delete[] mPredeleteProcs; + delete[] mPredeleteCbArgs; + mPredeleteProcs = newPreDeleteProcs; + mPredeleteCbArgs = newCBArgs; +} + +/* +** Return the text from the entire line containing position "pos" +*/ +char * Fl_Text_Buffer_mod::line_text( int pos ) { + return text_range( line_start( pos ), line_end( pos ) ); +} + +/* +** Find the position of the start of the line containing position "pos" +*/ +int Fl_Text_Buffer_mod::line_start( int pos ) { + if ( !findchar_backward( pos, '\n', &pos ) ) + return 0; + return pos + 1; +} + +/* +** Find the position of the end of the line containing position "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) +*/ +int Fl_Text_Buffer_mod::line_end( int pos ) { + if ( !findchar_forward( pos, '\n', &pos ) ) + pos = mLength; + return pos; +} + +int Fl_Text_Buffer_mod::word_start( int pos ) { + while ( pos && ( isalnum( character( pos ) ) || character( pos ) == '_' ) ) { + pos--; + } + if ( !( isalnum( character( pos ) ) || character( pos ) == '_' ) ) pos++; + return pos; +} + +int Fl_Text_Buffer_mod::word_end( int pos ) { + while (pos < length() && (isalnum(character(pos)) || character(pos) == '_' )) { + pos++; + } + return pos; +} + +/* +** Get a character from the text buffer expanded into it's screen +** representation (which may be several characters for a tab or a +** control code). Returns the number of characters written to "outStr". +** "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 +*/ +int Fl_Text_Buffer_mod::expand_character( int pos, int indent, char *outStr ) { + return expand_character( character( pos ), indent, outStr, + mTabDist, mNullSubsChar ); +} + +/* +** Expand a single character from the text buffer into it's screen +** representation (which may be several characters for a tab or a +** control code). Returns the number of characters added to "outStr". +** "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 +*/ +int Fl_Text_Buffer_mod::expand_character( char c, int indent, char *outStr, int tabDist, + char nullSubsChar ) { + int i, nSpaces; + + /* Convert tabs to spaces */ + if ( c == '\t' ) { + nSpaces = tabDist - ( indent % tabDist ); + for ( i = 0; i < nSpaces; i++ ) + outStr[ i ] = ' '; + return nSpaces; + } + + /* Convert control codes to readable character sequences */ + /*... is this safe with international character sets? */ + 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 == nullSubsChar ) { + sprintf( outStr, "" ); + return 5; + } + + /* Otherwise, just return the character */ + *outStr = c; + return 1; +} + +/* +** Return the length in displayed characters of character "c" expanded +** for display (as discussed above in BufGetExpandedChar). If the +** buffer for which the character width is being measured is doing null +** substitution, nullSubsChar should be passed as that character (or nul +** to ignore). +*/ +int Fl_Text_Buffer_mod::character_width( char c, int indent, int tabDist, char nullSubsChar ) { + /* Note, this code must parallel that in Fl_Text_Buffer_mod::ExpandCharacter */ + if ( c == '\t' ) + return tabDist - ( indent % tabDist ); + else if ( ( ( unsigned char ) c ) <= 31 ) + return strlen( ControlCodeTable[ ( unsigned char ) c ] ) + 2; + else if ( c == 127 ) + return 5; + else if ( c == nullSubsChar ) + return 5; + return 1; +} + +/* +** Count the number of displayed characters between buffer position +** "lineStartPos" and "targetPos". (displayed characters are the characters +** shown on the screen to represent characters in the buffer, where tabs and +** control characters are expanded) +*/ +int Fl_Text_Buffer_mod::count_displayed_characters( int lineStartPos, int targetPos ) { + int pos, charCount = 0; + char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; + + pos = lineStartPos; + while ( pos < targetPos ) + charCount += expand_character( pos++, charCount, expandedChar ); + return charCount; +} + +/* +** Count forward from buffer position "startPos" in displayed characters +** (displayed characters are the characters shown on the screen to represent +** characters in the buffer, where tabs and control characters are expanded) +*/ +int Fl_Text_Buffer_mod::skip_displayed_characters( int lineStartPos, int nChars ) { + int pos, charCount = 0; + char c; + + pos = lineStartPos; + while ( charCount < nChars && pos < mLength ) { + c = character( pos ); + if ( c == '\n' ) + return pos; + charCount += character_width( c, charCount, mTabDist, mNullSubsChar ); + pos++; + } + return pos; +} + +/* +** Count the number of newlines between startPos and endPos in buffer "buf". +** The character at position "endPos" is not counted. +*/ +int Fl_Text_Buffer_mod::count_lines( int startPos, int endPos ) { + int pos, gapLen = mGapEnd - mGapStart; + int lineCount = 0; + + pos = startPos; + while ( pos < mGapStart ) { + if ( pos == endPos ) + return lineCount; + if ( mBuf[ pos++ ] == '\n' ) + lineCount++; + } + while ( pos < mLength ) { + if ( pos == endPos ) + return lineCount; + if ( mBuf[ pos++ + gapLen ] == '\n' ) + lineCount++; + } + return lineCount; +} + +/* +** Find the first character of the line "nLines" forward from "startPos" +** in "buf" and return its position +*/ +int Fl_Text_Buffer_mod::skip_lines( int startPos, int nLines ) { + int pos, gapLen = mGapEnd - mGapStart; + int lineCount = 0; + + if ( nLines == 0 ) + return startPos; + + pos = startPos; + while ( pos < mGapStart ) { + if ( mBuf[ pos++ ] == '\n' ) { + lineCount++; + if ( lineCount == nLines ) + return pos; + } + } + while ( pos < mLength ) { + if ( mBuf[ pos++ + gapLen ] == '\n' ) { + lineCount++; + if ( lineCount >= nLines ) + return pos; + } + } + return pos; +} + +/* +** Find the position of the first character of the line "nLines" backwards +** from "startPos" (not counting the character pointed to by "startpos" if +** that is a newline) in "buf". nLines == 0 means find the beginning of +** the line +*/ +int Fl_Text_Buffer_mod::rewind_lines( int startPos, int nLines ) { + int pos, gapLen = mGapEnd - mGapStart; + int lineCount = -1; + + pos = startPos - 1; + if ( pos <= 0 ) + return 0; + + while ( pos >= mGapStart ) { + if ( mBuf[ pos + gapLen ] == '\n' ) { + if ( ++lineCount >= nLines ) + return pos + 1; + } + pos--; + } + while ( pos >= 0 ) { + if ( mBuf[ pos ] == '\n' ) { + if ( ++lineCount >= nLines ) + return pos + 1; + } + pos--; + } + return 0; +} + +/* +** Search forwards in buffer for string "searchString", starting with the +** character "startPos", and returning the result in "foundPos" +** returns 1 if found, 0 if not. +*/ +int Fl_Text_Buffer_mod::search_forward( int startPos, const char *searchString, + int *foundPos, int matchCase ) +{ + if (!searchString) return 0; + int bp; + const char* sp; + while (startPos < length()) { + bp = startPos; + sp = searchString; + do { + if (!*sp) { *foundPos = startPos; return 1; } + } while ((matchCase ? character(bp++) == *sp++ : + toupper(character(bp++)) == toupper(*sp++)) + && bp < length()); + startPos++; + } + return 0; +} + +/* +** Search backwards in buffer for string "searchString", starting with the +** character BEFORE "startPos", returning the result in "foundPos" +** returns 1 if found, 0 if not. +*/ +int Fl_Text_Buffer_mod::search_backward( int startPos, const char *searchString, + int *foundPos, int matchCase ) +{ + 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; } + } while ((matchCase ? character(bp--) == *sp-- : + toupper(character(bp--)) == toupper(*sp--)) + && bp >= 0); + startPos--; + } + return 0; +} + +/* +** Search forwards in buffer for characters in "searchChars", starting +** with the character "startPos", and returning the result in "foundPos" +** returns 1 if found, 0 if not. +*/ +int Fl_Text_Buffer_mod::findchars_forward( int startPos, const char *searchChars, + int *foundPos ) { + int pos, gapLen = mGapEnd - mGapStart; + const char *c; + + 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; +} + +/* +** Search backwards in buffer for characters in "searchChars", starting +** with the character BEFORE "startPos", returning the result in "foundPos" +** returns 1 if found, 0 if not. +*/ +int Fl_Text_Buffer_mod::findchars_backward( int startPos, const char *searchChars, + int *foundPos ) { + int pos, gapLen = mGapEnd - mGapStart; + const char *c; + + if ( startPos == 0 ) { + *foundPos = 0; + return 0; + } + 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; +} + +/* +** A horrible design flaw in NEdit (from the very start, before we knew that +** NEdit would become so popular), is that it uses C NULL terminated strings +** to hold text. This means editing text containing NUL characters is not +** possible without special consideration. Here is the special consideration. +** The routines below maintain a special substitution-character which stands +** in for a null, and translates strings an buffers back and forth from/to +** the substituted form, figure out what to substitute, and figure out +** when we're in over our heads and no translation is possible. +*/ + +/* +** The primary routine for integrating new text into a text buffer with +** substitution of another character for ascii nuls. This substitutes null +** characters in the string in preparation for being copied or replaced +** into the buffer, and if neccessary, adjusts the buffer as well, in the +** event that the string contains the character it is currently using for +** substitution. Returns 0, if substitution is no longer possible +** because all non-printable characters are already in use. +*/ +int Fl_Text_Buffer_mod::substitute_null_characters( char *string, int len ) { + char histogram[ 256 ]; + + /* Find out what characters the string contains */ + histogramCharacters( string, len, histogram, 1 ); + + /* Does the string contain the null-substitute character? If so, re- + histogram the buffer text to find a character which is ok in both the + string and the buffer, and change the buffer's null-substitution + character. If none can be found, give up and return 0 */ + if ( histogram[ ( unsigned char ) mNullSubsChar ] != 0 ) { + char * bufString; + char newSubsChar; + bufString = (char*)text(); + histogramCharacters( bufString, mLength, histogram, 0 ); + newSubsChar = chooseNullSubsChar( histogram ); + if ( newSubsChar == '\0' ) + return 0; + subsChars( bufString, mLength, mNullSubsChar, newSubsChar ); + remove_( 0, mLength ); + insert_( 0, bufString ); + free( (void *) bufString ); + mNullSubsChar = newSubsChar; + } + + /* If the string contains null characters, substitute them with the + buffer's null substitution character */ + if ( histogram[ 0 ] != 0 ) + subsChars( string, len, '\0', mNullSubsChar ); + return 1; +} + +/* +** Convert strings obtained from buffers which contain null characters, which +** have been substituted for by a special substitution character, back to +** a null-containing string. There is no time penalty for calling this +** routine if no substitution has been done. +*/ +void Fl_Text_Buffer_mod::unsubstitute_null_characters( char *string ) { + register char * c, subsChar = mNullSubsChar; + + if ( subsChar == '\0' ) + return; + for ( c = string; *c != '\0'; c++ ) + if ( *c == subsChar ) + * c = '\0'; +} + +/* +** Create a pseudo-histogram of the characters in a string (don't actually +** count, because we don't want overflow, just mark the character's presence +** with a 1). If init is true, initialize the histogram before acumulating. +** if not, add the new data to an existing histogram. +*/ +static void histogramCharacters( const char *string, int length, char hist[ 256 ], + int init ) { + int i; + const char *c; + + if ( init ) + for ( i = 0; i < 256; i++ ) + hist[ i ] = 0; + for ( c = string; c < &string[ length ]; c++ ) + hist[ *( ( unsigned char * ) c ) ] |= 1; +} + +/* +** Substitute fromChar with toChar in string. +*/ +static void subsChars( char *string, int length, char fromChar, char toChar ) { + char * c; + + for ( c = string; c < &string[ length ]; c++ ) + if ( *c == fromChar ) * c = toChar; +} + +/* +** Search through ascii control characters in histogram in order of least +** likelihood of use, find an unused character to use as a stand-in for a +** null. If the character set is full (no available characters outside of +** the printable set, return the null character. +*/ +static char chooseNullSubsChar( char hist[ 256 ] ) { +#define N_REPLACEMENTS 25 + static unsigned char replacements[ N_REPLACEMENTS ] = {1, 2, 3, 4, 5, 6, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 11, 7}; + int i; + for ( i = 0; i < N_REPLACEMENTS; i++ ) + if ( hist[ replacements[ i ] ] == 0 ) + return replacements[ i ]; + return '\0'; +} + +/* +** Internal (non-redisplaying) version of BufInsert. Returns the length of +** text inserted (this is just strlen(text), however this calculation can be +** expensive and the length will be required by any caller who will continue +** on to call redisplay). pos must be contiguous with the existing text in +** the buffer (i.e. not past the end). +*/ +int Fl_Text_Buffer_mod::insert_( int pos, const char *s ) { + int insertedLength = strlen( s ); + + /* Prepare the buffer to receive the new text. If the new text fits in + the current buffer, just move the gap (if necessary) to where + the text should be inserted. If the new text is too large, reallocate + the buffer with a gap large enough to accomodate the new text and a + gap of PREFERRED_GAP_SIZE */ + if ( insertedLength > mGapEnd - mGapStart ) + reallocate_with_gap( pos, insertedLength + PREFERRED_GAP_SIZE ); + else if ( pos != mGapStart ) + move_gap( pos ); + + /* Insert the new text (pos now corresponds to the start of the gap) */ + memcpy( &mBuf[ pos ], s, insertedLength ); + mGapStart += insertedLength; + mLength += insertedLength; + update_selections( pos, 0, insertedLength ); + + if (mCanUndo) { + if ( undowidget==this && undoat==pos && undoinsert ) { + undoinsert += insertedLength; + } + else { + undoinsert = insertedLength; + undoyankcut = (undoat==pos) ? undocut : 0 ; + } + undoat = pos+insertedLength; + undocut = 0; + undowidget = this; + } + + return insertedLength; +} + +/* +** 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). +*/ +void Fl_Text_Buffer_mod::remove_( int start, int end ) { + /* if the gap is not contiguous to the area to remove, move it there */ + + if (mCanUndo) { + if ( undowidget==this && undoat==end && undocut ) { + undobuffersize( undocut+end-start+1 ); + memmove( undobuffer+end-start, undobuffer, undocut ); + undocut += end-start; + } + else { + undocut = end-start; + undobuffersize(undocut); + } + undoat = start; + undoinsert = 0; + undoyankcut = 0; + undowidget = this; + } + + if ( start > mGapStart ) { + if (mCanUndo) + memcpy( undobuffer, mBuf+(mGapEnd-mGapStart)+start, end-start ); + move_gap( start ); + } + else if ( end < mGapStart ) { + if (mCanUndo) + memcpy( undobuffer, mBuf+start, end-start ); + move_gap( end ); + } + else { + int prelen = mGapStart - start; + if (mCanUndo) { + memcpy( undobuffer, mBuf+start, prelen ); + memcpy( undobuffer+prelen, mBuf+mGapEnd, end-start-prelen); + } + } + + /* expand the gap to encompass the deleted characters */ + mGapEnd += end - mGapStart; + mGapStart -= mGapStart - start; + + /* update the length */ + mLength -= end - start; + + /* fix up any selections which might be affected by the change */ + update_selections( start, end - start, 0 ); +} + +/* +** Insert 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. "nDeleted" and +** "nInserted" return the number of characters deleted and inserted beginning +** at the start of the line containing "startPos". "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). +*/ +void Fl_Text_Buffer_mod::insert_column_( int column, int startPos, const char *insText, + int *nDeleted, int *nInserted, int *endPos ) { + int nLines, start, end, insWidth, lineStart, lineEnd; + int expReplLen, expInsLen, len, endOffset; + char *c, *outStr, *outPtr, *expText, *insLine; + const char *line; + const char *replText; + const char *insPtr; + + 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) */ + start = line_start( startPos ); + nLines = countLines( insText ) + 1; + insWidth = textWidth( insText, mTabDist, mNullSubsChar ); + end = line_end( skip_lines( start, nLines - 1 ) ); + replText = text_range( start, end ); + expText = expandTabs( replText, 0, mTabDist, mNullSubsChar, + &expReplLen ); + free( (void *) replText ); + free( (void *) expText ); + expText = expandTabs( insText, 0, mTabDist, mNullSubsChar, + &expInsLen ); + free( (void *) expText ); + 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 */ + outPtr = outStr; + lineStart = start; + insPtr = insText; + for (;;) { + lineEnd = line_end( lineStart ); + line = text_range( lineStart, lineEnd ); + insLine = copyLine( insPtr, &len ); + insPtr += len; + insertColInLine( line, insLine, column, insWidth, mTabDist, + mUseTabs, mNullSubsChar, outPtr, &len, &endOffset ); + free( (void *) line ); + free( (void *) insLine ); + for ( 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 ); +} + +/* +** Delete a rectangle of text without calling the modify callbacks. Returns +** the number of characters replacing those between start and end. Note that +** in some pathological cases, deleting can actually increase the size of +** the buffer because of tab expansions. "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) +*/ +void Fl_Text_Buffer_mod::remove_rectangular_( int start, int end, int rectStart, + int rectEnd, int *replaceLen, int *endPos ) { + int nLines, lineStart, lineEnd, len, endOffset; + char *outStr, *outPtr, *expText; + const char *s, *line; + + /* 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 ); + nLines = count_lines( start, end ) + 1; + s = text_range( start, end ); + expText = expandTabs( s, 0, mTabDist, mNullSubsChar, &len ); + free( (void *) s ); + free( (void *) expText ); + 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 */ + lineStart = start; + outPtr = outStr; + endOffset = 0; + while ( lineStart <= mLength && lineStart <= end ) { + lineEnd = line_end( lineStart ); + line = text_range( lineStart, lineEnd ); + deleteRectFromLine( line, rectStart, rectEnd, mTabDist, + mUseTabs, mNullSubsChar, 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 ); +} + +/* +** Overlay a rectangular area of text without calling the modify callbacks. +** "nDeleted" and "nInserted" return the number of characters deleted and +** inserted beginning at the start of the line containing "startPos". +** "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). +*/ +void Fl_Text_Buffer_mod::overlay_rectangular_(int startPos, int rectStart, + int rectEnd, const char *insText, + int *nDeleted, int *nInserted, + int *endPos ) { + int nLines, start, end, lineStart, lineEnd; + int expInsLen, len, endOffset; + char *c, *outStr, *outPtr, *expText, *insLine; + const char *line; + const char *insPtr; + + /* 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) */ + start = line_start( startPos ); + nLines = countLines( insText ) + 1; + end = line_end( skip_lines( start, nLines - 1 ) ); + expText = expandTabs( insText, 0, mTabDist, mNullSubsChar, + &expInsLen ); + free( (void *) expText ); + 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 */ + outPtr = outStr; + lineStart = start; + insPtr = insText; + for (;;) { + lineEnd = line_end( lineStart ); + line = text_range( lineStart, lineEnd ); + insLine = copyLine( insPtr, &len ); + insPtr += len; + overlayRectInLine( line, insLine, rectStart, rectEnd, mTabDist, + mUseTabs, mNullSubsChar, outPtr, &len, &endOffset ); + free( (void *) line ); + free( (void *) insLine ); + for ( 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 ); +} + +/* +** Insert characters from single-line string "insLine" in single-line string +** "line" at "column", leaving "insWidth" space before continuing line. +** "outLen" returns the number of characters written to "outStr", "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). +*/ +static void insertColInLine( const char *line, char *insLine, int column, int insWidth, + int tabDist, int useTabs, char nullSubsChar, char *outStr, int *outLen, + int *endOffset ) { + char * c, *outPtr, *retabbedStr; + const char *linePtr; + int indent, toIndent, len, postColIndent; + + /* copy the line up to "column" */ + outPtr = outStr; + indent = 0; + for ( linePtr = line; *linePtr != '\0'; linePtr++ ) { + len = Fl_Text_Buffer_mod::character_width( *linePtr, indent, tabDist, nullSubsChar ); + 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. */ + 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, nullSubsChar, &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' ) { + retabbedStr = realignTabs( insLine, 0, indent, tabDist, useTabs, + nullSubsChar, &len ); + for ( c = retabbedStr; *c != '\0'; c++ ) { + *outPtr++ = *c; + len = Fl_Text_Buffer_mod::character_width( *c, indent, tabDist, nullSubsChar ); + 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) */ + toIndent = column + insWidth + postColIndent - column; + addPadding( outPtr, indent, toIndent, tabDist, useTabs, nullSubsChar, &len ); + outPtr += len; + indent = toIndent; + + /* realign tabs for text beyond "column" and write it out */ + retabbedStr = realignTabs( linePtr, postColIndent, indent, tabDist, + useTabs, nullSubsChar, &len ); + strcpy( outPtr, retabbedStr ); + free( (void *) retabbedStr ); + *endOffset = outPtr - outStr; + *outLen = ( outPtr - outStr ) + len; +} + +/* +** Remove characters in single-line string "line" between displayed positions +** "rectStart" and "rectEnd", and write the result to "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. "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 nullSubsChar, char *outStr, int *outLen, + int *endOffset ) { + int indent, preRectIndent, postRectIndent, len; + const char *c; + char *retabbedStr, *outPtr; + + /* copy the line up to rectStart */ + outPtr = outStr; + indent = 0; + for ( c = line; *c != '\0'; c++ ) { + if ( indent > rectStart ) + break; + len = Fl_Text_Buffer_mod::character_width( *c, indent, tabDist, nullSubsChar ); + if ( indent + len > rectStart && ( indent == rectStart || *c == '\t' ) ) + break; + indent += len; + *outPtr++ = *c; + } + 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, nullSubsChar ); + 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, nullSubsChar, + &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 */ + retabbedStr = realignTabs( c, postRectIndent, indent, tabDist, useTabs, + nullSubsChar, &len ); + strcpy( outPtr, retabbedStr ); + free( (void *) retabbedStr ); + *endOffset = outPtr - outStr; + *outLen = ( outPtr - outStr ) + len; +} + +/* +** Overlay characters from single-line string "insLine" on single-line string +** "line" between displayed character offsets "rectStart" and "rectEnd". +** "outLen" returns the number of characters written to "outStr", "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). +*/ +static void overlayRectInLine( const char *line, char *insLine, int rectStart, + int rectEnd, int tabDist, int useTabs, char nullSubsChar, char *outStr, + int *outLen, int *endOffset ) { + char * c, *outPtr, *retabbedStr; + int inIndent, outIndent, len, postRectIndent; + const char *linePtr; + + /* copy the line up to "rectStart" */ + outPtr = outStr; + inIndent = outIndent = 0; + for ( linePtr = line; *linePtr != '\0'; linePtr++ ) { + len = Fl_Text_Buffer_mod::character_width( *linePtr, inIndent, tabDist, nullSubsChar ); + 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 */ + postRectIndent = rectEnd; + for ( ; *linePtr != '\0'; linePtr++ ) { + inIndent += Fl_Text_Buffer_mod::character_width( *linePtr, inIndent, tabDist, nullSubsChar ); + 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, nullSubsChar, + &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' ) { + retabbedStr = realignTabs( insLine, 0, rectStart, tabDist, useTabs, + nullSubsChar, &len ); + for ( c = retabbedStr; *c != '\0'; c++ ) { + *outPtr++ = *c; + len = Fl_Text_Buffer_mod::character_width( *c, outIndent, tabDist, nullSubsChar ); + 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, + nullSubsChar, &len ); + outPtr += len; + outIndent = postRectIndent; + + /* copy the text beyond "rectEnd" */ + strcpy( outPtr, linePtr ); + *endOffset = outPtr - outStr; + *outLen = ( outPtr - outStr ) + strlen( linePtr ); +} + +char * Fl_Text_Buffer_mod::selection_text_( Fl_Text_Selection *sel ) { + int start, end, isRect, rectStart, rectEnd; + char *s; + + /* If there's no selection, return an allocated empty string */ + if ( !sel->position( &start, &end, &isRect, &rectStart, &rectEnd ) ) { + 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 text_range( start, end ); +} + +void Fl_Text_Buffer_mod::remove_selection_( Fl_Text_Selection *sel ) { + int start, end; + int isRect, rectStart, rectEnd; + + if ( !sel->position( &start, &end, &isRect, &rectStart, &rectEnd ) ) + return; + if ( isRect ) + remove_rectangular( start, end, rectStart, rectEnd ); + else { + remove( start, end ); + //undoyankcut = undocut; + } +} + +void Fl_Text_Buffer_mod::replace_selection_( Fl_Text_Selection *sel, const char *s ) { + int start, end, isRect, rectStart, rectEnd; + Fl_Text_Selection oldSelection = *sel; + + /* If there's no selection, return */ + if ( !sel->position( &start, &end, &isRect, &rectStart, &rectEnd ) ) + return; + + /* Do the appropriate type of replace */ + if ( isRect ) + replace_rectangular( start, end, rectStart, rectEnd, s ); + else + replace( start, end, s ); + + /* Unselect (happens automatically in BufReplace, but BufReplaceRect + can't detect when the contents of a selection goes away) */ + sel->mSelected = 0; + redisplay_selection( &oldSelection, sel ); +} + +static void addPadding( char *string, int startIndent, int toIndent, + int tabDist, int useTabs, char nullSubsChar, int *charsAdded ) { + char * outPtr; + int len, indent; + + indent = startIndent; + outPtr = string; + if ( useTabs ) { + while ( indent < toIndent ) { + len = Fl_Text_Buffer_mod::character_width( '\t', indent, tabDist, nullSubsChar ); + if ( len > 1 && indent + len <= toIndent ) { + *outPtr++ = '\t'; + indent += len; + } else { + *outPtr++ = ' '; + indent++; + } + } + } else { + while ( indent < toIndent ) { + *outPtr++ = ' '; + indent++; + } + } + *charsAdded = outPtr - string; +} + +/* +** Call the stored modify callback procedure(s) for this buffer to update the +** changed area(s) on the screen and any other listeners. +*/ +void Fl_Text_Buffer_mod::call_modify_callbacks( int pos, int nDeleted, + int nInserted, int nRestyled, const char *deletedText ) { + int i; + + for ( i = 0; i < mNModifyProcs; i++ ) + ( *mNodifyProcs[ i ] ) ( pos, nInserted, nDeleted, nRestyled, + deletedText, mCbArgs[ i ] ); +} + +/* +** Call the stored pre-delete callback procedure(s) for this buffer to update +** the changed area(s) on the screen and any other listeners. +*/ +void Fl_Text_Buffer_mod::call_predelete_callbacks(int pos, int nDeleted) { + int i; + + for (i=0; imStart; + 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 */ + if ( !oldSelection->mSelected && !newSelection->mSelected ) + return; + if ( !oldSelection->mSelected ) { + call_modify_callbacks( newStart, 0, 0, newEnd - newStart, NULL ); + return; + } + if ( !newSelection->mSelected ) { + call_modify_callbacks( oldStart, 0, 0, oldEnd - oldStart, NULL ); + 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 ) { + call_modify_callbacks( oldStart, 0, 0, oldEnd - oldStart, NULL ); + call_modify_callbacks( newStart, 0, 0, newEnd - newStart, NULL ); + return; + } + + /* Otherwise, separate into 3 separate regions: ch1, and ch2 (the two + changed areas), and the unchanged area of their intersection, + and update only the changed area(s) */ + ch1Start = min( oldStart, newStart ); + ch2End = max( oldEnd, newEnd ); + ch1End = max( oldStart, newStart ); + ch2Start = min( oldEnd, newEnd ); + if ( ch1Start != ch1End ) + call_modify_callbacks( ch1Start, 0, 0, ch1End - ch1Start, NULL ); + if ( ch2Start != ch2End ) + call_modify_callbacks( ch2Start, 0, 0, ch2End - ch2Start, NULL ); +} + +void Fl_Text_Buffer_mod::move_gap( int pos ) { + int gapLen = mGapEnd - mGapStart; + + if ( pos > mGapStart ) + memmove( &mBuf[ mGapStart ], &mBuf[ mGapEnd ], + pos - mGapStart ); + else + memmove( &mBuf[ pos + gapLen ], &mBuf[ pos ], mGapStart - pos ); + mGapEnd += pos - mGapStart; + mGapStart += pos - mGapStart; +} + +/* +** reallocate the text storage in "buf" to have a gap starting at "newGapStart" +** and a gap size of "newGapLen", preserving the buffer's current contents. +*/ +void Fl_Text_Buffer_mod::reallocate_with_gap( int newGapStart, int newGapLen ) { + char * newBuf; + int newGapEnd; + + newBuf = (char *)malloc( mLength + newGapLen ); + newGapEnd = newGapStart + newGapLen; + if ( newGapStart <= mGapStart ) { + memcpy( newBuf, mBuf, newGapStart ); + memcpy( &newBuf[ newGapEnd ], &mBuf[ newGapStart ], + mGapStart - newGapStart ); + memcpy( &newBuf[ newGapEnd + mGapStart - newGapStart ], + &mBuf[ mGapEnd ], mLength - mGapStart ); + } else { /* newGapStart > mGapStart */ + memcpy( newBuf, mBuf, mGapStart ); + memcpy( &newBuf[ mGapStart ], &mBuf[ mGapEnd ], + newGapStart - mGapStart ); + memcpy( &newBuf[ newGapEnd ], + &mBuf[ mGapEnd + newGapStart - mGapStart ], + mLength - newGapStart ); + } + free( (void *) mBuf ); + mBuf = newBuf; + mGapStart = newGapStart; + mGapEnd = newGapEnd; +#ifdef PURIFY +{int i; for ( i = mGapStart; i < mGapEnd; i++ ) mBuf[ i ] = '.'; } +#endif +} + +/* +** Update all of the selections in "buf" for changes in the buffer's text +*/ +void Fl_Text_Buffer_mod::update_selections( int pos, int nDeleted, + int nInserted ) { + mPrimary.update( pos, nDeleted, nInserted ); + mSecondary.update( pos, nDeleted, nInserted ); + mHighlight.update( pos, nDeleted, nInserted ); +} + +/* +** Search forwards in buffer "buf" for character "searchChar", starting +** with the character "startPos", and returning the result in "foundPos" +** returns 1 if found, 0 if not. (The difference between this and +** 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) +*/ +int Fl_Text_Buffer_mod::findchar_forward( int startPos, char searchChar, + int *foundPos ) { + int pos, gapLen = mGapEnd - mGapStart; + + if (startPos < 0 || startPos >= mLength) { + *foundPos = mLength; + return 0; + } + + pos = startPos; + while ( pos < mGapStart ) { + if ( mBuf[ pos ] == searchChar ) { + *foundPos = pos; + return 1; + } + pos++; + } + while ( pos < mLength ) { + if ( mBuf[ pos + gapLen ] == searchChar ) { + *foundPos = pos; + return 1; + } + pos++; + } + *foundPos = mLength; + return 0; +} + +/* +** Search backwards in buffer "buf" for character "searchChar", starting +** with the character BEFORE "startPos", returning the result in "foundPos" +** returns 1 if found, 0 if not. (The difference between this and +** 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) +*/ +int Fl_Text_Buffer_mod::findchar_backward( int startPos, char searchChar, + int *foundPos ) { + int pos, gapLen = mGapEnd - mGapStart; + + if ( startPos <= 0 || startPos > mLength ) { + *foundPos = 0; + return 0; + } + pos = startPos - 1; + while ( pos >= mGapStart ) { + if ( mBuf[ pos + gapLen ] == searchChar ) { + *foundPos = pos; + return 1; + } + pos--; + } + while ( pos >= 0 ) { + if ( mBuf[ pos ] == searchChar ) { + *foundPos = pos; + return 1; + } + pos--; + } + *foundPos = 0; + return 0; +} + +/* +** Copy from "text" to end up to but not including newline (or end of "text") +** and return the copy as the function value, and the length of the line in +** "lineLen" +*/ +static char *copyLine( const char *text, int *lineLen ) { + int len = 0; + const char *c; + char *outStr; + + for ( c = text; *c != '\0' && *c != '\n'; c++ ) + len++; + outStr = (char *)malloc( len + 1 ); + strlcpy( outStr, text, len + 1); + *lineLen = len; + return outStr; +} + +/* +** Count the number of newlines in a null-terminated text string; +*/ +static int countLines( const char *string ) { + const char * c; + int lineCount = 0; + + for ( c = string; *c != '\0'; c++ ) + if ( *c == '\n' ) lineCount++; + return lineCount; +} + +/* +** Measure the width in displayed characters of string "text" +*/ +static int textWidth( const char *text, int tabDist, char nullSubsChar ) { + int width = 0, maxWidth = 0; + const char *c; + + for ( 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, nullSubsChar ); + } + if ( width > maxWidth ) + return width; + return maxWidth; +} + +/* +** Find 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. +*/ +void Fl_Text_Buffer_mod::rectangular_selection_boundaries( int lineStartPos, + int rectStart, int rectEnd, int *selStart, int *selEnd ) { + int pos, width, indent = 0; + char c; + + /* find the start of the selection */ + for ( pos = lineStartPos; pos < mLength; pos++ ) { + c = character( pos ); + if ( c == '\n' ) + break; + width = Fl_Text_Buffer_mod::character_width( c, indent, mTabDist, mNullSubsChar ); + if ( indent + width > rectStart ) { + if ( indent != rectStart && c != '\t' ) { + pos++; + indent += width; + } + break; + } + indent += width; + } + *selStart = pos; + + /* find the end */ + for ( ; pos < mLength; pos++ ) { + c = character( pos ); + if ( c == '\n' ) + break; + width = Fl_Text_Buffer_mod::character_width( c, indent, mTabDist, mNullSubsChar ); + indent += width; + if ( indent > rectEnd ) { + if ( indent - width != rectEnd && c != '\t' ) + pos++; + break; + } + } + *selEnd = pos; +} + +/* +** Adjust the space and tab characters from string "text" so that non-white +** characters remain stationary when the text is shifted from starting at +** "origIndent" to starting at "newIndent". Returns an allocated string +** which must be freed by the caller with XtFree. +*/ +static char *realignTabs( const char *text, int origIndent, int newIndent, + int tabDist, int useTabs, char nullSubsChar, int *newLength ) { + char * expStr, *outStr; + int len; + + /* If the tabs settings are the same, retain original tabs */ + 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 */ + expStr = expandTabs( text, origIndent, tabDist, nullSubsChar, &len ); + if ( !useTabs ) { + *newLength = len; + return expStr; + } + outStr = unexpandTabs( expStr, newIndent, tabDist, nullSubsChar, newLength ); + free( (void *) expStr ); + return outStr; +} + +/* +** Expand tabs to spaces for a block of text. The additional parameter +** "startIndent" if nonzero, indicates that the text is a rectangular selection +** beginning at column "startIndent" +*/ +static char *expandTabs( const char *text, int startIndent, int tabDist, + char nullSubsChar, int *newLen ) { + char * outStr, *outPtr; + const char *c; + int indent, len, outLen = 0; + + /* rehearse the expansion to figure out length for output string */ + indent = startIndent; + for ( c = text; *c != '\0'; c++ ) { + if ( *c == '\t' ) { + len = Fl_Text_Buffer_mod::character_width( *c, indent, tabDist, nullSubsChar ); + outLen += len; + indent += len; + } else if ( *c == '\n' ) { + indent = startIndent; + outLen++; + } else { + indent += Fl_Text_Buffer_mod::character_width( *c, indent, tabDist, nullSubsChar ); + outLen++; + } + } + + /* do the expansion */ + outStr = (char *)malloc( outLen + 1 ); + outPtr = outStr; + indent = startIndent; + for ( c = text; *c != '\0'; c++ ) { + if ( *c == '\t' ) { + len = Fl_Text_Buffer_mod::expand_character( *c, indent, outPtr, tabDist, nullSubsChar ); + outPtr += len; + indent += len; + } else if ( *c == '\n' ) { + indent = startIndent; + *outPtr++ = *c; + } else { + indent += Fl_Text_Buffer_mod::character_width( *c, indent, tabDist, nullSubsChar ); + *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, + char nullSubsChar, int *newLen ) { + char * outStr, *outPtr, *c, expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; + int indent, len; + + outStr = (char *)malloc( strlen( text ) + 1 ); + outPtr = outStr; + indent = startIndent; + for ( c = text; *c != '\0'; ) { + if ( *c == ' ' ) { + len = Fl_Text_Buffer_mod::expand_character( '\t', indent, expandedChar, tabDist, + nullSubsChar ); + 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; +} + +static int max( int i1, int i2 ) { + return i1 >= i2 ? i1 : i2; +} + +static int min( int i1, int i2 ) { + return i1 <= i2 ? i1 : i2; +} + +int +Fl_Text_Buffer_mod::insertfile(const char *file, int pos, int buflen) { + FILE *fp; int r; + if (!(fp = fopen(file, "r"))) return 1; + char *buffer = new char[buflen]; + for (; (r = fread(buffer, 1, buflen - 1, fp)) > 0; pos += r) { + buffer[r] = (char)0; + insert(pos, buffer); + } + + int e = ferror(fp) ? 2 : 0; + fclose(fp); + delete[] buffer; + return e; +} + +int +Fl_Text_Buffer_mod::outputfile(const char *file, int start, int end, int buflen) { + FILE *fp; + if (!(fp = fopen(file, "w"))) return 1; + for (int n; (n = min(end - start, buflen)); start += n) { + const char *p = text_range(start, start + n); + int r = fwrite(p, 1, n, fp); + free((void *)p); + if (r != n) break; + } + + int e = ferror(fp) ? 2 : 0; + fclose(fp); + return e; +} + + +// +// End of "$Id: Fl_Text_Buffer.cxx 6011 2008-01-04 20:32:37Z matt $". +// diff --git a/src/widgets/Fl_Text_Buffer_mod_1_3.cxx b/src/widgets/Fl_Text_Buffer_mod_1_3.cxx new file mode 100644 index 00000000..7a8d9960 --- /dev/null +++ b/src/widgets/Fl_Text_Buffer_mod_1_3.cxx @@ -0,0 +1,2293 @@ +// +// "$Id: Fl_Text_Buffer.cxx 7462 2010-04-06 23:00:56Z matt $" +// +// Copyright 2001-2009 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#include +#include +#include +#include "flstring.h" +#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. + + 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 ;-) + + */ + + +/* + \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) +{ + return i1 >= i2 ? i1 : i2; +} + +static int min(int i1, int i2) +{ + return i1 <= i2 ? i1 : 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; +static Fl_Text_Buffer_mod *undowidget; +static int undoat; // points after insertion +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 + +static void undobuffersize(int n) +{ + if (n > undobufferlength) { + if (undobuffer) { + do { + undobufferlength *= 2; + } while (undobufferlength < n); + undobuffer = (char *) realloc(undobuffer, undobufferlength); + } else { + undobufferlength = n + 9; + undobuffer = (char *) malloc(undobufferlength); + } + } +} + + +// unicode ok +Fl_Text_Buffer_mod::Fl_Text_Buffer_mod(int requestedSize, int preferredGapSize) +{ + mLength = 0; + mPreferredGapSize = preferredGapSize; + mBuf = (char *) malloc(requestedSize + mPreferredGapSize); + 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; + mNPredeleteProcs = 0; + mPredeleteProcs = NULL; + mPredeleteCbArgs = NULL; + mCursorPosHint = 0; + mCanUndo = 1; +#ifdef PURIFY + { + int i; + for (i = mGapStart; i < mGapEnd; i++) + mBuf[i] = '.'; + } +#endif +} + + +// unicode ok +Fl_Text_Buffer_mod::~Fl_Text_Buffer_mod() +{ + free(mBuf); + if (mNModifyProcs != 0) { + delete[]mModifyProcs; + delete[]mCbArgs; + } + if (mNPredeleteProcs != 0) { + delete[]mPredeleteProcs; + delete[]mPredeleteCbArgs; + } +} + + +// This function copies verbose whatever is in front and after the gap into a +// single buffer. +// - unicode ok +char *Fl_Text_Buffer_mod::text() const { + char *t = (char *) malloc(mLength + 1); + memcpy(t, mBuf, mGapStart); + memcpy(&t[mGapStart], &mBuf[mGapEnd], mLength - mGapStart); + t[mLength] = '\0'; + return t; +} + + +// unicode ok, functions called have not been verified yet +void Fl_Text_Buffer_mod::text(const char *t) +{ + call_predelete_callbacks(0, length()); + + /* Save information for redisplay, and get rid of the old buffer */ + const char *deletedText = text(); + int deletedLength = mLength; + free((void *) mBuf); + + /* Start a new buffer with a gap of mPreferredGapSize at the end */ + int insertedLength = strlen(t); + mBuf = (char *) malloc(insertedLength + mPreferredGapSize); + mLength = insertedLength; + 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); + + /* Call the saved display routine(s) to update the screen */ + call_modify_callbacks(0, deletedLength, insertedLength, 0, deletedText); + free((void *) deletedText); +} + + +// Creates a new buffer and copies verbose from around the gap. +// - unicode ok +char *Fl_Text_Buffer_mod::text_range(int start, int end) const { + char *s = NULL; + + /* Make sure start and end are ok, and allocate memory for returned string. + If start is bad, return "", if end is bad, adjust it. */ + if (start < 0 || start > mLength) + { + s = (char *) malloc(1); + s[0] = '\0'; + return s; + } + if (end < start) { + int temp = start; + start = end; + end = temp; + } + if (end > mLength) + end = mLength; + int copiedLength = end - start; + s = (char *) malloc(copiedLength + 1); + + /* Copy the text from the buffer to the returned string */ + if (end <= mGapStart) { + memcpy(s, &mBuf[start], copiedLength); + } else if (start >= mGapStart) { + memcpy(s, &mBuf[start + (mGapEnd - mGapStart)], copiedLength); + } else { + int part1Length = mGapStart - start; + 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 { + if (pos < 0 || pos >= mLength) + return '\0'; + const char *src = address(pos); + return fl_utf8decode(src, 0, 0); +} + + +// unicode ok, dependents not tested +void Fl_Text_Buffer_mod::insert(int pos, const char *text) +{ + /* if pos is not contiguous to existing text, make it */ + if (pos > mLength) + pos = mLength; + if (pos < 0) + pos = 0; + + /* Even if nothing is deleted, we must call these callbacks */ + call_predelete_callbacks(pos, 0); + + /* insert and redisplay */ + int nInserted = insert_(pos, text); + mCursorPosHint = pos + nInserted; + call_modify_callbacks(pos, 0, nInserted, 0, NULL); +} + + +// unicode ok, dependents not tested +void Fl_Text_Buffer_mod::replace(int start, int end, const char *text) +{ + // Range check... + if (!text) + return; + if (start < 0) + start = 0; + if (end > mLength) + end = mLength; + + call_predelete_callbacks(start, end - start); + const char *deletedText = text_range(start, end); + remove_(start, end); + int nInserted = insert_(start, text); + mCursorPosHint = start + nInserted; + call_modify_callbacks(start, end - start, nInserted, 0, deletedText); + free((void *) deletedText); +} + + +// unicode ok, dependents not tested +void Fl_Text_Buffer_mod::remove(int start, int end) +{ + /* Make sure the arguments make sense */ + if (start > end) { + int temp = start; + start = end; + end = temp; + } + if (start > mLength) + start = mLength; + if (start < 0) + start = 0; + if (end > mLength) + end = mLength; + if (end < 0) + end = 0; + + if (start == end) + return; + + call_predelete_callbacks(start, end - start); + /* Remove and redisplay */ + const char *deletedText = text_range(start, end); + remove_(start, end); + mCursorPosHint = start; + call_modify_callbacks(start, end - start, 0, 0, deletedText); + free((void *) deletedText); +} + +void Fl_Text_Buffer_mod::copy(Fl_Text_Buffer_mod * fromBuf, int fromStart, + int fromEnd, int toPos) +{ + int copiedLength = fromEnd - fromStart; + + /* Prepare the buffer to receive the new text. If the new text fits in + the current buffer, just move the gap (if necessary) to where + the text should be inserted. If the new text is too large, reallocate + the buffer with a gap large enough to accomodate the new text and a + gap of mPreferredGapSize */ + if (copiedLength > mGapEnd - mGapStart) + reallocate_with_gap(toPos, copiedLength + mPreferredGapSize); + else if (toPos != mGapStart) + move_gap(toPos); + + /* Insert the new text (toPos now corresponds to the start of the gap) */ + if (fromEnd <= fromBuf->mGapStart) { + memcpy(&mBuf[toPos], &fromBuf->mBuf[fromStart], copiedLength); + } else if (fromStart >= fromBuf->mGapStart) { + memcpy(&mBuf[toPos], + &fromBuf->mBuf[fromStart + + (fromBuf->mGapEnd - fromBuf->mGapStart)], + copiedLength); + } else { + int part1Length = fromBuf->mGapStart - fromStart; + memcpy(&mBuf[toPos], &fromBuf->mBuf[fromStart], part1Length); + memcpy(&mBuf[toPos + part1Length], + &fromBuf->mBuf[fromBuf->mGapEnd], copiedLength - part1Length); + } + mGapStart += copiedLength; + mLength += copiedLength; + update_selections(toPos, 0, copiedLength); +} + +int Fl_Text_Buffer_mod::undo(int *cursorPos) +{ + if (undowidget != this || !undocut && !undoinsert && !mCanUndo) + return 0; + + int ilen = undocut; + int xlen = undoinsert; + int b = undoat - xlen; + + if (xlen && undoyankcut && !ilen) { + ilen = undoyankcut; + } + + if (xlen && ilen) { + undobuffersize(ilen + 1); + undobuffer[ilen] = 0; + char *tmp = strdup(undobuffer); + replace(b, undoat, tmp); + if (cursorPos) + *cursorPos = mCursorPosHint; + free(tmp); + } else if (xlen) { + remove(b, undoat); + if (cursorPos) + *cursorPos = mCursorPosHint; + } else if (ilen) { + undobuffersize(ilen + 1); + undobuffer[ilen] = 0; + insert(undoat, undobuffer); + if (cursorPos) + *cursorPos = mCursorPosHint; + undoyankcut = 0; + } + + return 1; +} + + +// unicode ok +void Fl_Text_Buffer_mod::canUndo(char flag) +{ + mCanUndo = flag; +} + +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; +} + +void Fl_Text_Buffer_mod::tab_distance(int tabDist) +{ + /* First call the pre-delete callbacks with the previous tab setting + still active. */ + call_predelete_callbacks(0, mLength); + + /* Change the tab setting */ + mTabDist = tabDist; + + /* Force any display routines to redisplay everything (unfortunately, + this means copying the whole buffer contents to provide "deletedText" */ + const char *deletedText = text(); + call_modify_callbacks(0, mLength, mLength, 0, deletedText); + free((void *) deletedText); +} + +void Fl_Text_Buffer_mod::select(int start, int end) +{ + Fl_Text_Selection oldSelection = mPrimary; + + mPrimary.set(start, end); + redisplay_selection(&oldSelection, &mPrimary); +} + +void Fl_Text_Buffer_mod::unselect() +{ + Fl_Text_Selection 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); +} + +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); +} + +char *Fl_Text_Buffer_mod::selection_text() +{ + return selection_text_(&mPrimary); +} + +void Fl_Text_Buffer_mod::remove_selection() +{ + remove_selection_(&mPrimary); +} + +void Fl_Text_Buffer_mod::replace_selection(const char *text) +{ + replace_selection_(&mPrimary, text); +} + +void Fl_Text_Buffer_mod::secondary_select(int start, int end) +{ + Fl_Text_Selection oldSelection = mSecondary; + + mSecondary.set(start, end); + redisplay_selection(&oldSelection, &mSecondary); +} + +void Fl_Text_Buffer_mod::secondary_unselect() +{ + Fl_Text_Selection 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); +} + +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); +} + +char *Fl_Text_Buffer_mod::secondary_selection_text() +{ + return selection_text_(&mSecondary); +} + +void Fl_Text_Buffer_mod::remove_secondary_selection() +{ + remove_selection_(&mSecondary); +} + +void Fl_Text_Buffer_mod::replace_secondary_selection(const char *text) +{ + replace_selection_(&mSecondary, text); +} + +void Fl_Text_Buffer_mod::highlight(int start, int end) +{ + Fl_Text_Selection oldSelection = mHighlight; + + mHighlight.set(start, end); + redisplay_selection(&oldSelection, &mHighlight); +} + +void Fl_Text_Buffer_mod::unhighlight() +{ + Fl_Text_Selection 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); +} + +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); +} + +char *Fl_Text_Buffer_mod::highlight_text() +{ + return selection_text_(&mHighlight); +} + +void Fl_Text_Buffer_mod::add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, + void *cbArg) +{ + Fl_Text_Modify_Cb *newModifyProcs = + new Fl_Text_Modify_Cb[mNModifyProcs + 1]; + void **newCBArgs = new void *[mNModifyProcs + 1]; + for (int i = 0; i < mNModifyProcs; i++) { + newModifyProcs[i + 1] = mModifyProcs[i]; + newCBArgs[i + 1] = mCbArgs[i]; + } + if (mNModifyProcs != 0) { + delete[]mModifyProcs; + delete[]mCbArgs; + } + newModifyProcs[0] = bufModifiedCB; + newCBArgs[0] = cbArg; + mNModifyProcs++; + mModifyProcs = newModifyProcs; + mCbArgs = newCBArgs; +} + +void Fl_Text_Buffer_mod:: +remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void *cbArg) +{ + int i, toRemove = -1; + + /* find the matching callback to remove */ + for (i = 0; i < mNModifyProcs; i++) { + if (mModifyProcs[i] == bufModifiedCB && mCbArgs[i] == cbArg) { + toRemove = i; + break; + } + } + if (toRemove == -1) { + Fl::error + ("Fl_Text_Buffer_mod::remove_modify_callback(): Can't find modify CB to remove"); + return; + } + + /* Allocate new lists for remaining callback procs and args (if + any are left) */ + mNModifyProcs--; + if (mNModifyProcs == 0) { + mNModifyProcs = 0; + delete[]mModifyProcs; + mModifyProcs = NULL; + delete[]mCbArgs; + mCbArgs = NULL; + return; + } + Fl_Text_Modify_Cb *newModifyProcs = new Fl_Text_Modify_Cb[mNModifyProcs]; + void **newCBArgs = new void *[mNModifyProcs]; + + /* copy out the remaining members and free the old lists */ + for (i = 0; i < toRemove; i++) { + newModifyProcs[i] = mModifyProcs[i]; + newCBArgs[i] = mCbArgs[i]; + } + for (; i < mNModifyProcs; i++) { + newModifyProcs[i] = mModifyProcs[i + 1]; + newCBArgs[i] = mCbArgs[i + 1]; + } + delete[]mModifyProcs; + delete[]mCbArgs; + mModifyProcs = newModifyProcs; + mCbArgs = newCBArgs; +} + +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]; + void **newCBArgs = new void *[mNPredeleteProcs + 1]; + for (int i = 0; i < mNPredeleteProcs; i++) { + newPreDeleteProcs[i + 1] = mPredeleteProcs[i]; + newCBArgs[i + 1] = mPredeleteCbArgs[i]; + } + if (!mNPredeleteProcs != 0) { + delete[]mPredeleteProcs; + delete[]mPredeleteCbArgs; + } + newPreDeleteProcs[0] = bufPreDeleteCB; + newCBArgs[0] = cbArg; + mNPredeleteProcs++; + mPredeleteProcs = newPreDeleteProcs; + mPredeleteCbArgs = newCBArgs; +} + +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 */ + for (i = 0; i < mNPredeleteProcs; i++) { + if (mPredeleteProcs[i] == bufPreDeleteCB && + mPredeleteCbArgs[i] == cbArg) { + toRemove = i; + break; + } + } + if (toRemove == -1) { + Fl::error + ("Fl_Text_Buffer_mod::remove_predelete_callback(): Can't find pre-delete CB to remove"); + return; + } + + /* Allocate new lists for remaining callback procs and args (if + any are left) */ + mNPredeleteProcs--; + if (mNPredeleteProcs == 0) { + mNPredeleteProcs = 0; + delete[]mPredeleteProcs; + mPredeleteProcs = NULL; + delete[]mPredeleteCbArgs; + mPredeleteCbArgs = NULL; + return; + } + Fl_Text_Predelete_Cb *newPreDeleteProcs = + new Fl_Text_Predelete_Cb[mNPredeleteProcs]; + void **newCBArgs = new void *[mNPredeleteProcs]; + + /* copy out the remaining members and free the old lists */ + for (i = 0; i < toRemove; i++) { + newPreDeleteProcs[i] = mPredeleteProcs[i]; + newCBArgs[i] = mPredeleteCbArgs[i]; + } + for (; i < mNPredeleteProcs; i++) { + newPreDeleteProcs[i] = mPredeleteProcs[i + 1]; + newCBArgs[i] = mPredeleteCbArgs[i + 1]; + } + delete[]mPredeleteProcs; + delete[]mPredeleteCbArgs; + mPredeleteProcs = newPreDeleteProcs; + mPredeleteCbArgs = newCBArgs; +} + +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 { + if (!findchar_backward(pos, '\n', &pos)) + return 0; + return pos + 1; +} + +int Fl_Text_Buffer_mod::line_end(int pos) const { + if (!findchar_forward(pos, '\n', &pos)) + pos = mLength; + return pos; +} + +int Fl_Text_Buffer_mod::word_start(int pos) const { + // FIXME: character is ucs-4 + while (pos && (isalnum(character(pos)) || character(pos) == '_')) { + pos--; + } + // FIXME: character is ucs-4 + if (!(isalnum(character(pos)) || character(pos) == '_')) + pos++; + return pos; +} + +int Fl_Text_Buffer_mod::word_end(int pos) const { + // FIXME: character is ucs-4 + while (pos < length() && (isalnum(character(pos)) || character(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; + } + + /* 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) + return pos; + } + } + return pos; +} + +int Fl_Text_Buffer_mod::rewind_lines(int startPos, int nLines) +{ + int pos = startPos - 1; + if (pos <= 0) + return 0; + + int gapLen = mGapEnd - mGapStart; + int lineCount = -1; + while (pos >= mGapStart) { + if (mBuf[pos + gapLen] == '\n') { + if (++lineCount >= nLines) + return pos + 1; + } + pos--; + } + while (pos >= 0) { + if (mBuf[pos] == '\n') { + if (++lineCount >= nLines) + return pos + 1; + } + pos--; + } + return 0; +} + +int Fl_Text_Buffer_mod::search_forward(int startPos, const char *searchString, + int *foundPos, + int matchCase) const +{ + if (!searchString) + return 0; + int bp; + const char *sp; + while (startPos < length()) { + bp = startPos; + sp = searchString; + do { + if (!*sp) { + *foundPos = startPos; + return 1; + } + // FIXME: character is ucs-4 + } while ((matchCase ? character(bp++) == *sp++ : + toupper(character(bp++)) == toupper(*sp++)) + && bp < length()); + startPos++; + } + return 0; +} + +int Fl_Text_Buffer_mod::search_backward(int startPos, const char *searchString, + int *foundPos, + int matchCase) const { + 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; + } + // FIXME: character is ucs-4 + } while ((matchCase ? character(bp--) == *sp-- : + toupper(character(bp--)) == toupper(*sp--)) + && bp >= 0); + 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; +} + +int Fl_Text_Buffer_mod::insert_(int pos, const char *text) +{ + int insertedLength = strlen(text); + + /* Prepare the buffer to receive the new text. If the new text fits in + the current buffer, just move the gap (if necessary) to where + the text should be inserted. If the new text is too large, reallocate + the buffer with a gap large enough to accomodate the new text and a + gap of mPreferredGapSize */ + if (insertedLength > mGapEnd - mGapStart) + reallocate_with_gap(pos, insertedLength + mPreferredGapSize); + else if (pos != mGapStart) + move_gap(pos); + + /* Insert the new text (pos now corresponds to the start of the gap) */ + memcpy(&mBuf[pos], text, insertedLength); + mGapStart += insertedLength; + mLength += insertedLength; + update_selections(pos, 0, insertedLength); + + if (mCanUndo) { + if (undowidget == this && undoat == pos && undoinsert) { + undoinsert += insertedLength; + } else { + undoinsert = insertedLength; + undoyankcut = (undoat == pos) ? undocut : 0; + } + undoat = pos + insertedLength; + undocut = 0; + undowidget = this; + } + + return insertedLength; +} + +void Fl_Text_Buffer_mod::remove_(int start, int end) +{ + /* if the gap is not contiguous to the area to remove, move it there */ + + if (mCanUndo) { + if (undowidget == this && undoat == end && undocut) { + undobuffersize(undocut + end - start + 1); + memmove(undobuffer + end - start, undobuffer, undocut); + undocut += end - start; + } else { + undocut = end - start; + undobuffersize(undocut); + } + undoat = start; + undoinsert = 0; + undoyankcut = 0; + undowidget = this; + } + + if (start > mGapStart) { + if (mCanUndo) + memcpy(undobuffer, mBuf + (mGapEnd - mGapStart) + start, + end - start); + move_gap(start); + } else if (end < mGapStart) { + if (mCanUndo) + memcpy(undobuffer, mBuf + start, end - start); + move_gap(end); + } else { + int prelen = mGapStart - start; + if (mCanUndo) { + memcpy(undobuffer, mBuf + start, prelen); + memcpy(undobuffer + prelen, mBuf + mGapEnd, end - start - prelen); + } + } + + /* expand the gap to encompass the deleted characters */ + mGapEnd += end - mGapStart; + mGapStart -= mGapStart - start; + + /* update the length */ + mLength -= end - start; + + /* fix up any selections which might be affected by the change */ + 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) +{ + 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); +} + +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). + */ +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; + + 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; +} + +/** + 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). + */ +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); +} + + +char *Fl_Text_Buffer_mod::selection_text_(Fl_Text_Selection * sel) const { + int start, end, isRect, rectStart, rectEnd; + + /* If there's no selection, return an allocated empty string */ + if (!sel->position(&start, &end, &isRect, &rectStart, &rectEnd)) + { + 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 text_range(start, end); +} + +void Fl_Text_Buffer_mod::remove_selection_(Fl_Text_Selection * sel) +{ + int start, end, isRect, rectStart, rectEnd; + + if (!sel->position(&start, &end, &isRect, &rectStart, &rectEnd)) + return; + if (isRect) + remove_rectangular(start, end, rectStart, rectEnd); + else { + remove(start, end); + //undoyankcut = undocut; + } +} + + +void Fl_Text_Buffer_mod::replace_selection_(Fl_Text_Selection * sel, + const char *text) +{ + Fl_Text_Selection oldSelection = *sel; + + /* If there's no selection, return */ + int start, end, isRect, rectStart, rectEnd; + if (!sel->position(&start, &end, &isRect, &rectStart, &rectEnd)) + 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 + can't detect when the contents of a selection goes away) */ + sel->mSelected = 0; + 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; +} + +void Fl_Text_Buffer_mod::call_modify_callbacks(int pos, int nDeleted, + int nInserted, int nRestyled, + const char *deletedText) const { + 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 { + for (int i = 0; i < mNPredeleteProcs; i++) + (*mPredeleteProcs[i]) (pos, nDeleted, mPredeleteCbArgs[i]); +} void Fl_Text_Buffer_mod::redisplay_selection(Fl_Text_Selection * + oldSelection, + Fl_Text_Selection * + newSelection) const +{ + int oldStart, oldEnd, newStart, newEnd, ch1Start, ch1End, ch2Start, + ch2End; + + /* If either selection is rectangular, add an additional character to + the end of the selection to request the redraw routines to wipe out + the parts of the selection beyond the end of the line */ + oldStart = oldSelection->mStart; + 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 */ + if (!oldSelection->mSelected && !newSelection->mSelected) + return; + if (!oldSelection->mSelected) + { + call_modify_callbacks(newStart, 0, 0, newEnd - newStart, NULL); + return; + } + if (!newSelection->mSelected) { + call_modify_callbacks(oldStart, 0, 0, oldEnd - oldStart, NULL); + 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) { + call_modify_callbacks(oldStart, 0, 0, oldEnd - oldStart, NULL); + call_modify_callbacks(newStart, 0, 0, newEnd - newStart, NULL); + return; + } + + /* Otherwise, separate into 3 separate regions: ch1, and ch2 (the two + changed areas), and the unchanged area of their intersection, + and update only the changed area(s) */ + ch1Start = min(oldStart, newStart); + ch2End = max(oldEnd, newEnd); + ch1End = max(oldStart, newStart); + ch2Start = min(oldEnd, newEnd); + if (ch1Start != ch1End) + call_modify_callbacks(ch1Start, 0, 0, ch1End - ch1Start, NULL); + if (ch2Start != ch2End) + call_modify_callbacks(ch2Start, 0, 0, ch2End - ch2Start, NULL); +} + +void Fl_Text_Buffer_mod::move_gap(int pos) +{ + int gapLen = mGapEnd - mGapStart; + + if (pos > mGapStart) + memmove(&mBuf[mGapStart], &mBuf[mGapEnd], pos - mGapStart); + else + memmove(&mBuf[pos + gapLen], &mBuf[pos], mGapStart - pos); + mGapEnd += pos - mGapStart; + mGapStart += pos - mGapStart; +} + +void Fl_Text_Buffer_mod::reallocate_with_gap(int newGapStart, int newGapLen) +{ + char *newBuf = (char *) malloc(mLength + newGapLen); + int newGapEnd = newGapStart + newGapLen; + + if (newGapStart <= mGapStart) { + memcpy(newBuf, mBuf, newGapStart); + memcpy(&newBuf[newGapEnd], &mBuf[newGapStart], + mGapStart - newGapStart); + memcpy(&newBuf[newGapEnd + mGapStart - newGapStart], + &mBuf[mGapEnd], mLength - mGapStart); + } else { /* newGapStart > mGapStart */ + memcpy(newBuf, mBuf, mGapStart); + memcpy(&newBuf[mGapStart], &mBuf[mGapEnd], newGapStart - mGapStart); + memcpy(&newBuf[newGapEnd], + &mBuf[mGapEnd + newGapStart - mGapStart], + mLength - newGapStart); + } + free((void *) mBuf); + mBuf = newBuf; + mGapStart = newGapStart; + mGapEnd = newGapEnd; +#ifdef PURIFY + { + int i; + for (i = mGapStart; i < mGapEnd; i++) + mBuf[i] = '.'; + } +#endif +} + +void Fl_Text_Buffer_mod::update_selections(int pos, int nDeleted, + int nInserted) +{ + mPrimary.update(pos, nDeleted, nInserted); + mSecondary.update(pos, nDeleted, nInserted); + mHighlight.update(pos, nDeleted, nInserted); +} + + +int Fl_Text_Buffer_mod::findchar_forward(int startPos, char searchChar, + int *foundPos) const { + if (startPos < 0 || startPos >= mLength) + { + *foundPos = mLength; + return 0; + } + + int pos = startPos; + while (pos < mGapStart) { + if (mBuf[pos] == searchChar) { + *foundPos = pos; + return 1; + } + pos++; + } + + for (int gapLen = mGapEnd - mGapStart; pos < mLength; pos++) { + if (mBuf[pos + gapLen] == searchChar) { + *foundPos = pos; + return 1; + } + } + *foundPos = mLength; + return 0; +} + +int Fl_Text_Buffer_mod::findchar_backward(int startPos, char searchChar, + int *foundPos) const { + + if (startPos <= 0 || startPos > mLength) + { + *foundPos = 0; + return 0; + } + + int pos = startPos - 1; + for (int gapLen = mGapEnd - mGapStart; pos >= mGapStart; pos--) { + if (mBuf[pos + gapLen] == searchChar) { + *foundPos = pos; + 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) +{ + 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; +} + +/* + Counts the number of newlines in a null-terminated text string. + Unicode tested. + */ +static int countLines(const char *string) +{ + int lineCount = 0; + + for (const char *c = string; *c != '\0'; c++) + if (*c == '\n') + lineCount++; + return lineCount; +} + +/* + Measures the width in displayed characters of string \p text + */ +static int textWidth(const char *text, int tabDist) +{ + 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); + } + if (width > maxWidth) + return width; + return maxWidth; +} + +void Fl_Text_Buffer_mod::rectangular_selection_boundaries(int lineStartPos, + int rectStart, + int rectEnd, + int *selStart, + int *selEnd) const +{ + 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; + } + 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; + } + } + *selEnd = pos; +} + +/* + 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. + */ +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) +{ + 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; + insert(pos, buffer); + } + + int e = ferror(fp) ? 2 : 0; + fclose(fp); + delete[]buffer; + return e; +} + +int Fl_Text_Buffer_mod::outputfile(const char *file, int start, int end, + int buflen) +{ + FILE *fp; + if (!(fp = fl_fopen(file, "wb"))) + return 1; + for (int n; (n = min(end - start, buflen)); start += n) { + const char *p = text_range(start, start + n); + int r = fwrite(p, 1, n, fp); + free((void *) p); + if (r != n) + break; + } + + int e = ferror(fp) ? 2 : 0; + fclose(fp); + return e; +} + + +// +// End of "$Id: Fl_Text_Buffer.cxx 7462 2010-04-06 23:00:56Z matt $". +// diff --git a/src/widgets/Fl_Text_Display_mod.cxx b/src/widgets/Fl_Text_Display_mod.cxx index 4d852821..ae4baed4 100644 --- a/src/widgets/Fl_Text_Display_mod.cxx +++ b/src/widgets/Fl_Text_Display_mod.cxx @@ -1,3265 +1,7 @@ -// -// "$Id: Fl_Text_Display.cxx 5848 2007-05-20 16:18:31Z mike $" -// -// Copyright 2001-2006 by Bill Spitzak and others. -// Original code Copyright Mark Edel. Permission to distribute under -// the LGPL for the FLTK library granted by Mark Edel. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library 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 -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -// USA. -// -// Please report all bugs and problems on the following page: -// -// http://www.fltk.org/str.php -// - #include -#include -#include -#include "flstring.h" -#include -#include -#include -#include -#include "Fl_Text_Display_mod.H" -#include - -#undef min -#undef max - -// Text area margins. Left & right margins should be at least 3 so that -// there is some room for the overhanging parts of the cursor! -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 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 */ -#define FILL_MASK 0x0100 -#define SECONDARY_MASK 0x0200 -#define PRIMARY_MASK 0x0400 -#define HIGHLIGHT_MASK 0x0800 -#define BG_ONLY_MASK 0x1000 -#define TEXT_ONLY_MASK 0x2000 -#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 */ -#define MAX_DISP_LINE_LEN 1000 - -static int max( int i1, int i2 ); -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. */ -static int scroll_direction = 0; -static int scroll_amount = 0; -static int scroll_y = 0; -static int scroll_x = 0; - -// CET - FIXME -#define TMPFONTWIDTH 6 - -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; - RIGHT_MARGIN = DEFAULT_RIGHT_MARGIN; - - mMaxsize = 0; - damage_range1_start = damage_range1_end = -1; - damage_range2_start = damage_range2_end = -1; - dragPos = dragType = dragging = 0; - display_insert_position_hint = 0; - - color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR); - box(FL_DOWN_FRAME); - textsize((uchar)FL_NORMAL_SIZE); - textcolor(FL_FOREGROUND_COLOR); - textfont(FL_HELVETICA); - - 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(16); - scrollbar_align(FL_ALIGN_BOTTOM_RIGHT); - - mCursorOn = 0; - mCursorPos = 0; - mCursorOldY = -100; - mCursorToHint = NO_HINT; - mCursorStyle = NORMAL_CURSOR; - mCursorPreferredCol = -1; - mBuffer = 0; - mFirstChar = 0; - mLastChar = 0; - mNBufferLines = 0; - mTopLineNum = mTopLineNumHint = 1; - mAbsTopLineNum = 1; - mNeedAbsTopLineNum = 0; - mHorizOffset = mHorizOffsetHint = 0; - - mCursor_color = FL_FOREGROUND_COLOR; - - mFixedFontWidth = -1; - mStyleBuffer = 0; - mStyleTable = 0; - mNStyles = 0; - mNVisibleLines = 1; - mLineStarts = new int[mNVisibleLines]; - mLineStarts[0] = 0; - for (i=1; iremove_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) -*/ -void Fl_Text_Display_mod::buffer( Fl_Text_Buffer *buf ) { - /* If the text display is already displaying a buffer, clear it off - 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; - 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 */ - 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. -*/ -void -Fl_Text_Display_mod::highlight_data(Fl_Text_Buffer *styleBuffer, - 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; - - mStyleBuffer->canUndo(0); -#if 0 - // FIXME: this is in nedit code -- is it needed? - /* Call TextDSetFont to combine font information from style table and - primary font, adjust font-related parameters, and then redisplay */ - TextDSetFont(textD, textD->fontStruct); +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# include "Fl_Text_Display_mod_1_1.cxx" +#elif FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +# include "Fl_Text_Display_mod_1_3.cxx" #endif - damage(FL_DAMAGE_EXPOSE); -} - -#if 0 - // FIXME: this is in nedit code -- is it needed? -/* -** Change the (non highlight) font -*/ -void TextDSetFont(textDisp *textD, XFontStruct *fontStruct) { - Display *display = XtDisplay(textD->w); - int i, maxAscent = fontStruct->ascent, maxDescent = fontStruct->descent; - int width, height, fontWidth; - Pixel bgPixel, fgPixel, selectFGPixel, selectBGPixel; - Pixel highlightFGPixel, highlightBGPixel; - XGCValues values; - XFontStruct *styleFont; - - /* If font size changes, cursor will be redrawn in a new position */ - blankCursorProtrusions(textD); - - /* If there is a (syntax highlighting) style table in use, find the new - maximum font height for this text display */ - for (i=0; inStyles; i++) { - styleFont = textD->styleTable[i].font; - if (styleFont != NULL && styleFont->ascent > maxAscent) - maxAscent = styleFont->ascent; - if (styleFont != NULL && styleFont->descent > maxDescent) - maxDescent = styleFont->descent; - } - textD->ascent = maxAscent; - textD->descent = maxDescent; - - /* If all of the current fonts are fixed and match in width, compute */ - fontWidth = fontStruct->max_bounds.width; - if (fontWidth != fontStruct->min_bounds.width) - fontWidth = -1; - else { - for (i=0; inStyles; i++) { - styleFont = textD->styleTable[i].font; - if (styleFont != NULL && (styleFont->max_bounds.width != fontWidth || - styleFont->max_bounds.width != styleFont->min_bounds.width)) - fontWidth = -1; - } - } - textD->fixedFontWidth = fontWidth; - - /* Don't let the height dip below one line, or bad things can happen */ - if (textD->height < maxAscent + maxDescent) - textD->height = maxAscent + maxDescent; - - /* Change the font. In most cases, this means re-allocating the - affected GCs (they are shared with other widgets, and if the primary - font changes, must be re-allocated to change it). Unfortunately, - this requres recovering all of the colors from the existing GCs */ - textD->fontStruct = fontStruct; - XGetGCValues(display, textD->gc, GCForeground|GCBackground, &values); - fgPixel = values.foreground; - bgPixel = values.background; - XGetGCValues(display, textD->selectGC, GCForeground|GCBackground, &values); - selectFGPixel = values.foreground; - selectBGPixel = values.background; - XGetGCValues(display, textD->highlightGC,GCForeground|GCBackground,&values); - highlightFGPixel = values.foreground; - highlightBGPixel = values.background; - releaseGC(textD->w, textD->gc); - releaseGC(textD->w, textD->selectGC); - releaseGC(textD->w, textD->highlightGC); - releaseGC(textD->w, textD->selectBGGC); - releaseGC(textD->w, textD->highlightBGGC); - if (textD->lineNumGC != NULL) - releaseGC(textD->w, textD->lineNumGC); - textD->lineNumGC = NULL; - allocateFixedFontGCs(textD, fontStruct, bgPixel, fgPixel, selectFGPixel, - selectBGPixel, highlightFGPixel, highlightBGPixel); - XSetFont(display, textD->styleGC, fontStruct->fid); - - /* Do a full resize to force recalculation of font related parameters */ - width = textD->width; - height = textD->height; - textD->width = textD->height = 0; - TextDResize(textD, width, height); - - /* Redisplay */ - TextDRedisplayRect(textD, textD->left, textD->top, textD->width, - textD->height); - - /* Clean up line number area in case spacing has changed */ - draw_line_numbers(textD, True); -} - -int TextDMinFontWidth(textDisp *textD, Boolean considerStyles) { - int fontWidth = textD->fontStruct->max_bounds.width; - int i; - - if (considerStyles) { - for (i = 0; i < textD->nStyles; ++i) { - int thisWidth = (textD->styleTable[i].font)->min_bounds.width; - if (thisWidth < fontWidth) { - fontWidth = thisWidth; - } - } - } - return(fontWidth); -} - -int TextDMaxFontWidth(textDisp *textD, Boolean considerStyles) { - int fontWidth = textD->fontStruct->max_bounds.width; - int i; - - if (considerStyles) { - for (i = 0; i < textD->nStyles; ++i) { - int thisWidth = (textD->styleTable[i].font)->max_bounds.width; - if (thisWidth > fontWidth) { - fontWidth = thisWidth; - } - } - } - return(fontWidth); -} -#endif - -int Fl_Text_Display_mod::longest_vline() { - int longest = 0; - for (int i = 0; i < mNVisibleLines; i++) - longest = max(longest, measure_vline(i)); - return longest; -} - -/* -** Change the size of the displayed text area -*/ -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); -#endif // DEBUG - Fl_Widget::resize(X,Y,W,H); - if (!buffer()) return; - X += Fl::box_dx(box()); - 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; - - /* 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? - int hscrollbarvisible = mHScrollBar->visible(); - int vscrollbarvisible = mVScrollBar->visible(); - - // try without scrollbars first - mVScrollBar->clear_visible(); - mHScrollBar->clear_visible(); - - for (int again = 1; again;) { - 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) { - 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); - -#ifdef DEBUG - printf(" mNBufferLines=%d\n", mNBufferLines); -#endif // DEBUG - } - - /* reallocate and update the line starts array, which may have changed - size and / or contents. */ - int nvlines = (text_area.h + mMaxsize - 1) / mMaxsize; - if (nvlines < 1) nvlines = 1; - if (mNVisibleLines != nvlines) { - mNVisibleLines = nvlines; - 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) - { - if (!mVScrollBar->visible()) - again = 1; - mVScrollBar->set_visible(); - if (scrollbar_align() & FL_ALIGN_LEFT) { - text_area.x = X+scrollbar_width()+LEFT_MARGIN; - text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN; - mVScrollBar->resize(X, text_area.y-TOP_MARGIN, scrollbar_width(), - text_area.h+TOP_MARGIN+BOTTOM_MARGIN); - } else { - text_area.x = X+LEFT_MARGIN; - text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN; - mVScrollBar->resize(X+W-scrollbar_width(), text_area.y-TOP_MARGIN, - 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. - */ - /* 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) && - (mVScrollBar->visible() || longest_vline() > text_area.w)) - { - if (!mHScrollBar->visible()) { - mHScrollBar->set_visible(); - again = 1; // loop again to see if we now need vert. & recalc sizes - } - if (scrollbar_align() & FL_ALIGN_TOP) { - text_area.y = Y + scrollbar_width()+TOP_MARGIN; - text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN; - mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y, - text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width()); - } else { - text_area.y = Y+TOP_MARGIN; - text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN; - mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y+H-scrollbar_width(), - text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width()); - } - } - } - } - - // 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) - 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); - - // 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 || - 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 -*/ -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); - - fl_pop_clip(); -} - -void Fl_Text_Display_mod::redisplay_range(int startpos, int endpos) { - if (damage_range1_start == -1 && damage_range1_end == -1) { - damage_range1_start = startpos; - damage_range1_end = endpos; - } else if ((startpos >= damage_range1_start && startpos <= damage_range1_end) || - (endpos >= damage_range1_start && endpos <= damage_range1_end)) { - damage_range1_start = min(damage_range1_start, startpos); - damage_range1_end = max(damage_range1_end, endpos); - } else if (damage_range2_start == -1 && damage_range2_end == -1) { - damage_range2_start = startpos; - damage_range2_end = endpos; - } else { - damage_range2_start = min(damage_range2_start, startpos); - damage_range2_end = max(damage_range2_end, 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. -*/ -void Fl_Text_Display_mod::draw_range(int startpos, int 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; - - /* 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; - if ( !position_to_line( startpos, &startLine ) ) - startLine = mNVisibleLines - 1; - if ( endpos >= mLastChar ) { - lastLine = mNVisibleLines - 1; - } else { - if ( !position_to_line( endpos, &lastLine ) ) { - /* shouldn't happen */ - lastLine = mNVisibleLines - 1; - } - } - - /* Get the starting and ending positions within the lines */ - 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" */ - if ( startLine == lastLine ) { - draw_vline( startLine, 0, INT_MAX, startIndex, endIndex ); - return; - } - - /* Redisplay the first line from "start" */ - draw_vline( startLine, 0, INT_MAX, startIndex, INT_MAX ); - - /* Redisplay the lines in between at their full width */ - for ( i = startLine + 1; i < lastLine; i++ ) - draw_vline( i, 0, INT_MAX, 0, INT_MAX ); - - /* Redisplay the last line to "end" */ - draw_vline( lastLine, 0, INT_MAX, 0, endIndex ); -} - -/* -** Set the position of the text insertion cursor for text display -*/ -void Fl_Text_Display_mod::insert_position( int newPos ) { - /* make sure new position is ok, do nothing if it hasn't changed */ - 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); - - mCursorPos = newPos; - - /* draw cursor at its new position */ - redisplay_range(mCursorPos - 1, mCursorPos + 1); -} - -void Fl_Text_Display_mod::show_cursor(int b) { - mCursorOn = b; - redisplay_range(mCursorPos - 1, mCursorPos + 1); -} - -void Fl_Text_Display_mod::cursor_style(int style) { - mCursorStyle = style; - if (mCursorOn) show_cursor(); -} - -void Fl_Text_Display_mod::wrap_mode(int wrap, int wrapMargin) { - mWrapMargin = wrapMargin; - mContinuousWrap = wrap; - - 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 */ - 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(); - } else { - // No buffer, so just clear the state info for later... - mNBufferLines = 0; - mFirstChar = 0; - mTopLineNum = 1; - mAbsTopLineNum = 0; - } - - resize(x(), y(), w(), h()); -} - -/* -** Insert "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; - - mCursorToHint = pos + strlen( text ); - mBuffer->insert( pos, text ); - mCursorToHint = NO_HINT; -} - -/* -** Insert "text" (which must not contain newlines), overstriking the current -** cursor location. -*/ -void Fl_Text_Display_mod::overstrike(const char* text) { - int startPos = mCursorPos; - Fl_Text_Buffer *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; - - /* 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::character_width( *c, indent, buf->tab_distance(), buf->null_substitution_character() ); - endIndent = indent; - - /* find which characters to remove, and if necessary generate additional - padding to make up for removed control characters at the end */ - indent = startIndent; - for ( p = startPos; ; p++ ) { - if ( p == buf->length() ) - break; - ch = buf->character( p ); - if ( ch == '\n' ) - break; - indent += Fl_Text_Buffer::character_width( ch, indent, buf->tab_distance(), buf->null_substitution_character() ); - if ( indent == endIndent ) { - p++; - break; - } else if ( indent > endIndent ) { - if ( ch != '\t' ) { - p++; - paddedText = new char [ textLen + FL_TEXT_MAX_EXP_CHAR_LEN + 1 ]; - strcpy( paddedText, text ); - for ( i = 0; i < indent - endIndent; i++ ) - paddedText[ textLen + i ] = ' '; - paddedText[ textLen + i ] = '\0'; - } - break; - } - } - endPos = p; - - mCursorToHint = startPos + textLen; - buf->replace( startPos, endPos, paddedText == NULL ? text : paddedText ); - mCursorToHint = NO_HINT; - if ( paddedText != NULL ) - 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. -*/ - -int Fl_Text_Display_mod::position_to_xy( int pos, int* X, int* Y ) { - 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); - - /* 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 */ - 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++ ) { - charLen = Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, expandedChar, - mBuffer->tab_distance(), mBuffer->null_substitution_character() ); - charStyle = position_style( lineStartPos, lineLen, charIndex, - outIndex ); - xStep += string_width( expandedChar, charLen, charStyle ); - outIndex += charLen; - } - *X = xStep; - free((char *)lineStr); - 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 ) { - 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; - } - - retVal = position_to_line( pos, lineNum ); - if ( retVal ) { - *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 -*/ -int Fl_Text_Display_mod::in_selection( int X, int Y ) { - int row, column, pos = xy_to_position( X, Y, CHARACTER_POS ); - Fl_Text_Buffer *buf = mBuffer; - - 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); -} - -/* -** 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. -*/ -int Fl_Text_Display_mod::wrapped_column(int row, int column) { - 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); -} - -/* -** 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) { - 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. -*/ -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) { - 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); - } - - /* 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 (!position_to_xy( mCursorPos, &X, &Y )) { - scroll_(topLine, hOffset); - if (!position_to_xy( mCursorPos, &X, &Y )) { - #ifdef DEBUG - printf ("*** display_insert/position_to_xy # GIVE UP !\n"); fflush(stdout); - #endif // DEBUG - return; /* Give up, it's not worth it (but why does it fail?) */ - } - } - if (X > text_area.x + text_area.w) - 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); -} - -void Fl_Text_Display_mod::show_insert_position() { - display_insert_position_hint = 1; - resize(x(), y(), w(), h()); -} - -/* -** Cursor movement functions -*/ -int Fl_Text_Display_mod::move_right() { - if ( mCursorPos >= mBuffer->length() ) - return 0; - insert_position( mCursorPos + 1 ); - return 1; -} - -int Fl_Text_Display_mod::move_left() { - if ( mCursorPos <= 0 ) - return 0; - insert_position( mCursorPos - 1 ); - return 1; -} - -int Fl_Text_Display_mod::move_up() { - int lineStartPos, column, prevLineStartPos, newPos, visLineNum; - - /* Find the position of the start of the line. Use the line starts array - if possible */ - if ( position_to_line( mCursorPos, &visLineNum ) ) - lineStartPos = mLineStarts[ visLineNum ]; - else { - lineStartPos = line_start( mCursorPos ); - visLineNum = -1; - } - 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 ); - - /* 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)); - - /* move the cursor */ - insert_position( newPos ); - - /* if a preferred column wasn't aleady established, establish it */ - mCursorPreferredCol = column; - return 1; -} - -int Fl_Text_Display_mod::move_down() { - int lineStartPos, column, nextLineStartPos, 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 ); - mCursorPreferredCol = column; - 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. -*/ -int Fl_Text_Display_mod::count_lines(int startPos, int endPos, - bool startPosIsLineStart) { - int retLines, retPos, retLineStart, retLineEnd; - -#ifdef DEBUG - 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); - -#ifdef DEBUG - printf(" # after WLC: retPos=%d, retLines=%d, retLineStart=%d, retLineEnd=%d\n", - retPos, retLines, retLineStart, retLineEnd); -#endif // DEBUG - - 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. -*/ -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; -} - -/* -** 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) { - 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. -*/ -int Fl_Text_Display_mod::line_start(int pos) { - int retLines, retPos, retLineStart, retLineEnd; - - /* If we're not wrapping, use the more efficient BufStartOfLine */ - if (!mContinuousWrap) - return buffer()->line_start(pos); - - wrapped_line_counter(buffer(), buffer()->line_start(pos), pos, INT_MAX, true, 0, - &retPos, &retLines, &retLineStart, &retLineEnd); - 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 *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); - - 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) { - return c != '$' && c != '_' && (isspace(c) || ispunct(c)); -} - -void Fl_Text_Display_mod::next_word() { - int pos = insert_position(); - while (pos < buffer()->length() && !fl_isseparator(buffer()->character(pos))) { - pos++; - } - while (pos < buffer()->length() && fl_isseparator(buffer()->character(pos))) { - pos++; - } - - insert_position( pos ); -} - -void Fl_Text_Display_mod::previous_word() { - int pos = insert_position(); - if (pos==0) return; - pos--; - while (pos && fl_isseparator(buffer()->character(pos))) { - pos--; - } - while (pos && !fl_isseparator(buffer()->character(pos))) { - pos--; - } - if (fl_isseparator(buffer()->character(pos))) 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 -*/ -void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDeleted, - 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 *buf = textD->mBuffer; - int oldFirstChar = textD->mFirstChar; - int scrolled, origCursorPos = textD->mCursorPos; - int wrapModStart, wrapModEnd; - - /* 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 ); - } - - /* 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 ); - } - } 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(); - } - - /* 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; - textD->mCursorToHint = NO_HINT; - } else if ( textD->mCursorPos > pos ) { - if ( textD->mCursorPos < pos + nDeleted ) - textD->mCursorPos = pos; - 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); - if ( textD->mStyleBuffer ) /* See comments in extendRangeForStyleMods */ - 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; - if ( origCursorPos == startDispPos && textD->mCursorPos != startDispPos ) - startDispPos = min( startDispPos, origCursorPos - 1 ); - if ( linesInserted == linesDeleted ) { - if ( nInserted == 0 && nDeleted == 0 ) - endDispPos = pos + nRestyled; - else { - endDispPos = textD->mContinuousWrap ? wrapModEnd : - buf->line_end( pos + nInserted ) + 1; - // CET - FIXME if ( origCursorPos >= startDispPos && - // ( origCursorPos <= endDispPos || endDispPos == buf->length() ) ) - } - - if (linesInserted > 1) textD->draw_line_numbers(false); - } else { - endDispPos = textD->mLastChar + 1; - // 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 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 */ - if ( textD->mStyleBuffer ) - textD->extend_range_for_styles( &startDispPos, &endDispPos ); - - /* Redisplay computed range */ - 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. -*/ -void Fl_Text_Display_mod::maintain_absolute_top_line_number(int state) { - 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. -*/ -int Fl_Text_Display_mod::get_absolute_top_line_number() { - if (!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. -*/ -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); - } -} - -/* -** 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() { - return 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. -*/ -void Fl_Text_Display_mod::reset_absolute_top_line_number() { - 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( int pos, int *lineNum ) { - int i; - - *lineNum = 0; - if ( pos < mFirstChar ) return 0; - if ( pos > mLastChar ) { - if ( empty_vlines() ) { - if ( mLastChar < mBuffer->length() ) { - if ( !position_to_line( mLastChar, lineNum ) ) { - Fl::error("Fl_Text_Display_mod::position_to_line(): Consistency check ptvl failed"); - return 0; - } - return ++( *lineNum ) <= mNVisibleLines - 1; - } else { - position_to_line( mLastChar - 1, lineNum ); - return 1; - } - } - return 0; - } - - for ( i = mNVisibleLines - 1; i >= 0; i-- ) { - if ( mLineStarts[ i ] != -1 && pos >= mLineStarts[ i ] ) { - *lineNum = i; - return 1; - } - } - 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"). -*/ -void Fl_Text_Display_mod::draw_vline(int visLineNum, int leftClip, int rightClip, - int leftCharIndex, int rightCharIndex) { - Fl_Text_Buffer * 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); - - /* 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"); - 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++ ) { - charLen = charIndex >= lineLen ? 1 : - Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, - expandedChar, buf->tab_distance(), buf->null_substitution_character() ); - 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++ ) { - charLen = charIndex >= lineLen ? 1 : - Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, expandedChar, - buf->tab_distance(), buf->null_substitution_character() ); - charStyle = position_style( lineStartPos, lineLen, charIndex, - outIndex + dispIndexOffset ); - for ( i = 0; i < charLen; i++ ) { - 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 ]; - charWidth = string_width( &expandedChar[ i ], 1, 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++ ) { - charLen = charIndex >= lineLen ? 1 : - Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, expandedChar, - buf->tab_distance(), buf->null_substitution_character() ); - charStyle = position_style( lineStartPos, lineLen, charIndex, - outIndex + dispIndexOffset ); - for ( i = 0; i < charLen; i++ ) { - 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 ]; - charWidth = string_width( &expandedChar[ i ], 1, 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); -} - -/* -** 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; - - /* 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. */ - - 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 & (HIGHLIGHT_MASK | PRIMARY_MASK)) { - if (Fl::focus() == this) background = selection_color(); - else background = fl_color_average(color(), selection_color(), 0.5f); - } else background = color(); - - // foreground = fl_contrast(styleRec->color, background); - foreground = styleRec->color; - } else if (style & (HIGHLIGHT_MASK | PRIMARY_MASK)) { - if (Fl::focus() == this) background = selection_color(); - else background = fl_color_average(color(), selection_color(), 0.5f); - 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 ); - } - if (!(style & BG_ONLY_MASK)) { - fl_color( foreground ); - fl_font( font, fsize ); - fl_draw( string, nChars, X, Y + mMaxsize - fl_descent()); - } - - // 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 */ - /* - 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); - */ -} - -/* -** 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 ) { - /* 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)) { - fl_color(fl_color_average(color(), selection_color(), 0.5f)); - } else { - fl_color( color() ); - } - fl_rectf( X, Y, width, height ); - } else if ( style & HIGHLIGHT_MASK ) { - fl_color( fl_contrast(textcolor(), color()) ); - fl_rectf( X, Y, width, height ); - } else if ( style & PRIMARY_MASK ) { - fl_color( selection_color() ); - fl_rectf( X, Y, width, height ); - } else { - fl_color( color() ); - fl_rectf( X, Y, width, height ); - } -} - - - -/* -** Draw a cursor with top center at X, y. -*/ -void Fl_Text_Display_mod::draw_cursor( int X, int Y ) { - typedef struct { - int x1, y1, x2, y2; - } - Segment; - - Segment segs[ 5 ]; - int left, right, cursorWidth, midY; - // int fontWidth = mFontStruct->min_bounds.width, nSegs = 0; - int fontWidth = TMPFONTWIDTH; // CET - FIXME - 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. */ - 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; - segs[ 0 ].x1 = left; segs[ 0 ].y1 = bot; segs[ 0 ].x2 = X; segs[ 0 ].y2 = midY; - segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot; - segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = midY - 1; - segs[ 3 ].x1 = X; segs[ 3 ].y1 = midY - 1; segs[ 3 ].x2 = right; segs[ 3 ].y2 = bot; - nSegs = 4; - } else if ( mCursorStyle == NORMAL_CURSOR ) { - segs[ 0 ].x1 = left; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y; - segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot; - segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = right; segs[ 2 ].y2 = bot; - nSegs = 3; - } else if ( mCursorStyle == HEAVY_CURSOR ) { - segs[ 0 ].x1 = X - 1; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X - 1; segs[ 0 ].y2 = bot; - segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot; - segs[ 2 ].x1 = X + 1; segs[ 2 ].y1 = Y; segs[ 2 ].x2 = X + 1; segs[ 2 ].y2 = bot; - segs[ 3 ].x1 = left; segs[ 3 ].y1 = Y; segs[ 3 ].x2 = right; segs[ 3 ].y2 = Y; - segs[ 4 ].x1 = left; segs[ 4 ].y1 = bot; segs[ 4 ].x2 = right; segs[ 4 ].y2 = bot; - nSegs = 5; - } else if ( mCursorStyle == DIM_CURSOR ) { - midY = Y + fontHeight / 2; - segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X; segs[ 0 ].y2 = Y; - segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = X; segs[ 1 ].y2 = midY; - segs[ 2 ].x1 = X; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot; - nSegs = 3; - } else if ( mCursorStyle == BLOCK_CURSOR ) { - right = X + fontWidth; - segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y; - segs[ 1 ].x1 = right; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot; - segs[ 2 ].x1 = right; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot; - segs[ 3 ].x1 = X; segs[ 3 ].y1 = bot; segs[ 3 ].x2 = X; segs[ 3 ].y2 = 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 ) { - Fl_Text_Buffer * buf = mBuffer; - Fl_Text_Buffer *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 ) { - style = ( unsigned char ) styleBuf->character( pos ); - if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { - /* encountered "unfinished" style, trigger parsing */ - (mUnfinishedHighlightCB)( pos, mHighlightCBArg); - style = (unsigned char) styleBuf->character( pos); - } - } - if (buf->primary_selection()->includes(pos, lineStartPos, dispIndex)) - style |= PRIMARY_MASK; - if (buf->highlight_selection()->includes(pos, lineStartPos, dispIndex)) - style |= HIGHLIGHT_MASK; - if (buf->secondary_selection()->includes(pos, lineStartPos, dispIndex)) - 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 ) { - Fl_Font font; - int fsize; - - if ( 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 { - font = textfont(); - fsize = textsize(); - } - fl_font( font, fsize ); - - return ( int ) ( 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 ) { - int charIndex, lineStart, lineLen, fontHeight; - int charWidth, charLen, charStyle, visLineNum, xStep, outIndex; - char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - const char *lineStr; - - /* Find the visible line number corresponding to the Y coordinate */ - fontHeight = mMaxsize; - visLineNum = ( Y - text_area.y ) / fontHeight; - if ( visLineNum < 0 ) - 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++ ) { - charLen = Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, expandedChar, - mBuffer->tab_distance(), mBuffer->null_substitution_character() ); - 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; -} - -/* -** 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. -*/ -void Fl_Text_Display_mod::xy_to_rowcol( int X, int Y, int *row, - int *column, int posType ) { - 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; - *column = ( ( X - text_area.x ) + mHorizOffset + - ( 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) -*/ -void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { - int oldTopLineNum = mTopLineNum; - int oldFirstChar = mFirstChar; - int lineDelta = newTopLineNum - oldTopLineNum; - int nVisLines = mNVisibleLines; - int *lineStarts = mLineStarts; - int i, lastLineNum; - Fl_Text_Buffer *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) */ - lastLineNum = oldTopLineNum + nVisLines - 1; - if ( newTopLineNum < oldTopLineNum && newTopLineNum < -lineDelta ) { - mFirstChar = skip_lines( 0, newTopLineNum - 1, true ); - } else if ( newTopLineNum < oldTopLineNum ) { - mFirstChar = rewind_lines( mFirstChar, -lineDelta ); - } else if ( newTopLineNum < lastLineNum ) { - mFirstChar = lineStarts[ newTopLineNum - oldTopLineNum ]; - } else if ( newTopLineNum - lastLineNum < mNBufferLines - newTopLineNum ) { - mFirstChar = skip_lines( lineStarts[ nVisLines - 1 ], - 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-- ) - lineStarts[ i ] = lineStarts[ i + lineDelta ]; - calc_line_starts( 0, -lineDelta ); - } else if ( lineDelta > 0 && lineDelta < nVisLines ) { - for ( i = 0; i < nVisLines - lineDelta; i++ ) - lineStarts[ i ] = lineStarts[ i + lineDelta ]; - 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); -} - -/* -** 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; - 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 */ - if ( pos + charsDeleted < mFirstChar ) { - mTopLineNum += lineDelta; - for ( i = 0; i < nVisLines && lineStarts[i] != -1; i++ ) - lineStarts[ i ] += charDelta; - mFirstChar += charDelta; - mLastChar += charDelta; - *scrolled = 0; - return; - } - - /* The change began before the beginning of the displayed text, but - 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 ) { - mTopLineNum = max( 1, mTopLineNum + lineDelta ); - mFirstChar = rewind_lines( - lineStarts[ lineOfEnd ] + charDelta, lineOfEnd ); - /* Otherwise anchor on original line number and recount everything */ - } else { - if ( mTopLineNum > mNBufferLines + lineDelta ) { - mTopLineNum = 1; - mFirstChar = 0; - } else - mFirstChar = skip_lines( 0, mTopLineNum - 1, true ); - } - calc_line_starts( 0, nVisLines - 1 ); - /* calculate lastChar by finding the end of the last displayed line */ - calc_last_char(); - *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 */ - if ( pos <= mLastChar ) { - /* find line on which the change began */ - position_to_line( pos, &lineOfPos ); - /* salvage line starts after the changed area */ - if ( lineDelta == 0 ) { - for ( i = lineOfPos + 1; i < nVisLines && lineStarts[ i ] != -1; i++ ) - lineStarts[ i ] += charDelta; - } else if ( lineDelta > 0 ) { - for ( i = nVisLines - 1; i >= lineOfPos + lineDelta + 1; i-- ) - lineStarts[ i ] = lineStarts[ i - lineDelta ] + - ( 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 ); - } - /* fill in the missing line starts */ - if ( linesInserted >= 0 ) - calc_line_starts( lineOfPos + 1, lineOfPos + linesInserted ); - if ( lineDelta < 0 ) - calc_line_starts( nVisLines + lineDelta, nVisLines ); - /* calculate lastChar by finding the end of the last displayed line */ - calc_last_char(); - *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 */ - if ( empty_vlines() ) { - position_to_line( pos, &lineOfPos ); - calc_line_starts( lineOfPos, lineOfPos + linesInserted ); - calc_last_char(); - *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. -*/ -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; - if ( startLine < 0 ) startLine = 0; - 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 */ - 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 */ - 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 */ - if ( line == 0 || ( lineStarts[ line - 1 ] != bufLen && - lineEnd != nextLineStart ) ) { - lineStarts[ line ] = bufLen; - line++; - } - break; - } - 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. -*/ -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); -} - -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) { - /* 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 */ - if (mHorizOffset == horizOffset && mTopLineNum == topLineNum) - return; - - /* If the vertical scroll position has changed, update the line - 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); -} - -/* -** Update the minimum, maximum, slider size, page increment, and value -** for vertical scroll bar. -*/ -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 */ -#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->linesize(3); -} - -/* -** Update the minimum, maximum, slider size, page increment, and value -** for the horizontal scroll bar. -*/ -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 -*/ -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); -} - -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. -*/ -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')) { - snprintf(lineNumString, sizeof(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 -} - -static int max( int i1, int i2 ) { - return i1 >= i2 ? i1 : i2; -} - -static int min( int i1, int i2 ) { - return i1 <= i2 ? i1 : i2; -} - -/* -** Count the number of newlines in a null-terminated text string; -*/ -static int countlines( const char *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" -*/ -int Fl_Text_Display_mod::measure_vline( int visLineNum ) { - int i, width = 0, len, style, lineLen = vline_length( visLineNum ); - int charCount = 0, lineStartPos = mLineStarts[ visLineNum ]; - char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - - 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 ); - 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 true if there are lines visible with no corresponding buffer text -*/ -int Fl_Text_Display_mod::empty_vlines() { - 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 -*/ -int Fl_Text_Display_mod::vline_length( int visLineNum ) { - 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; - 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. -*/ -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 *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); - - - /* - ** 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->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 (visLineNumcopy(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. -** -** 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). -*/ -void Fl_Text_Display_mod::measure_deleted_lines(int pos, int nDeleted) { - int retPos, retLines, retLineStart, retLineEnd; - Fl_Text_Buffer *buf = buffer(); - int nVisLines = mNVisibleLines; - int *lineStarts = mLineStarts; - int countFrom, lineStart; - int 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]; - 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->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 *buf, int startPos, - int maxPos, int maxLines, bool startPosIsLineStart, int styleBufOffset, - int *retPos, int *retLines, int *retLineStart, int *retLineEnd, - bool countLastLineMissingNewLine) { - 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; - char nullSubsChar = buffer()->null_substitution_character(); - - /* 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++) { - 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::character_width(c, colNum, tabDist, nullSubsChar); - 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--) { - 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::character_width(c, colNum, tabDist, nullSubsChar); - 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) { - int charLen, style; - char expChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; - Fl_Text_Buffer *styleBuf = mStyleBuffer; - - charLen = Fl_Text_Buffer::expand_character(c, colNum, expChar, - buffer()->tab_distance(), buffer()->null_substitution_character()); - if (styleBuf == 0) { - style = 0; - } else { - style = (unsigned char)styleBuf->character(pos); - if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { - /* encountered "unfinished" style, trigger parsing */ - (mUnfinishedHighlightCB)(pos, mHighlightCBArg); - 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) { - 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); - return; -} - -/* -** 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. -*/ -int Fl_Text_Display_mod::wrap_uses_character(int lineEndPos) { - char c; - - if (!mContinuousWrap || lineEndPos == buffer()->length()) - return 1; - - c = buffer()->character(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(Fl_Text_Selection *sel, - int rangeStart, int rangeEnd) { - 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). -*/ -void Fl_Text_Display_mod::extend_range_for_styles( int *startpos, int *endpos ) { - Fl_Text_Selection * 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. */ - if ( sel->selected() ) { - if ( sel->start() < *startpos ) { - *startpos = sel->start(); - extended = 1; - } - if ( sel->end() > *endpos ) { - *endpos = sel->end(); - 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 ) - *endpos = mBuffer->line_end( *endpos ) + 1; -} - -// The draw() method. It tries to minimize what is draw as much as 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()); - // 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); - // CET - FIXME - save old cursor position instead and just draw side needed? - fl_push_clip(text_area.x-LEFT_MARGIN, - text_area.y, - text_area.w+LEFT_MARGIN+RIGHT_MARGIN, - text_area.h); - fl_rectf(text_area.x-LEFT_MARGIN, mCursorOldY, - LEFT_MARGIN, mMaxsize, color()); - fl_rectf(text_area.x+text_area.w, mCursorOldY, - RIGHT_MARGIN, mMaxsize, color()); - fl_pop_clip(); - } - - // draw the scrollbars - if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_CHILD)) { - mVScrollBar->damage(FL_DAMAGE_ALL); - mHScrollBar->damage(FL_DAMAGE_ALL); - } - update_child(*mVScrollBar); - update_child(*mHScrollBar); - - // draw all of the text - if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_EXPOSE)) { - //printf("drawing all text\n"); - int X, Y, W, H; - if (fl_clip_box(text_area.x, text_area.y, text_area.w, text_area.h, - X, Y, W, H)) { - // Draw text using the intersected clipping box... - // (this sets the clipping internally) - draw_text(X, Y, W, H); - } else { - // Draw the whole area... - draw_text(text_area.x, text_area.y, text_area.w, text_area.h); - } - } - else if (damage() & FL_DAMAGE_SCROLL) { - // draw some lines of text - fl_push_clip(text_area.x, text_area.y, - text_area.w, text_area.h); - //printf("drawing text from %d to %d\n", damage_range1_start, damage_range1_end); - draw_range(damage_range1_start, damage_range1_end); - if (damage_range2_end != -1) { - //printf("drawing text from %d to %d\n", damage_range2_start, damage_range2_end); - draw_range(damage_range2_start, damage_range2_end); - } - damage_range1_start = damage_range1_end = -1; - 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 ) { - 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!"); - //printf("drew cursor at pos: %d (%d,%d)\n", mCursorPos, X, Y); - mCursorOldY = Y; - fl_pop_clip(); - } - fl_pop_clip(); -} - -// this processes drag events due to mouse for Fl_Text_Display and -// also drags due to cursor movement with shift held down for -// Fl_Text_Editor -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) { - d->buffer()->select(d->dragPos, pos); - } else { - d->buffer()->select(pos, d->dragPos); - } - d->insert_position(pos); - } else if (d->dragType == Fl_Text_Display_mod::DRAG_WORD) { - if (pos >= d->dragPos) { - d->insert_position(d->word_end(pos)); - d->buffer()->select(d->word_start(d->dragPos), d->word_end(pos)); - } else { - d->insert_position(d->word_start(pos)); - d->buffer()->select(d->word_start(pos), d->word_end(d->dragPos)); - } - } else if (d->dragType == Fl_Text_Display_mod::DRAG_LINE) { - if (pos >= d->dragPos) { - d->insert_position(d->buffer()->line_end(pos)+1); - d->buffer()->select(d->buffer()->line_start(d->dragPos), - d->buffer()->line_end(pos)+1); - } else { - d->insert_position(d->buffer()->line_start(pos)); - d->buffer()->select(d->buffer()->line_start(pos), - d->buffer()->line_end(d->dragPos)+1); - } - } -} - -// 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; - } - fl_text_drag_me(pos, w); - Fl::repeat_timeout(.1, scroll_timer_cb, user_data); -} - - -int Fl_Text_Display_mod::handle(int event) { - if (!buffer()) return 0; - // This isn't very elegant! - if (!Fl::event_inside(text_area.x, text_area.y, text_area.w, text_area.h) && - !dragging && event != FL_LEAVE && event != FL_ENTER && - event != FL_MOVE && event != FL_FOCUS && event != FL_UNFOCUS && - event != FL_KEYBOARD && event != FL_KEYUP) { - return Fl_Group::handle(event); - } - - switch (event) { - case FL_ENTER: - case FL_MOVE: - if (active_r()) { - 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); - return 1; - } 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); - 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(); - return 1; - } - - case FL_DRAG: { - if (dragType < 0) return 1; - int X = Fl::event_x(), Y = Fl::event_y(), pos; - // 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); - fl_text_drag_me(pos, this); - } - return 1; - } - - case FL_RELEASE: { - 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: - if (buffer()->selected()) redraw(); - - 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; - } - - // Select all ? - if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='a') { - buffer()->select(0,buffer()->length()); - return 1; - } - - if (mVScrollBar->handle(event)) return 1; - if (mHScrollBar->handle(event)) return 1; - - break; - } - - return 0; -} - - -// -// End of "$Id: Fl_Text_Display.cxx 5848 2007-05-20 16:18:31Z mike $". -// diff --git a/src/widgets/Fl_Text_Display_mod_1_1.cxx b/src/widgets/Fl_Text_Display_mod_1_1.cxx new file mode 100644 index 00000000..29683426 --- /dev/null +++ b/src/widgets/Fl_Text_Display_mod_1_1.cxx @@ -0,0 +1,3265 @@ +// +// "$Id: Fl_Text_Display.cxx 5848 2007-05-20 16:18:31Z mike $" +// +// Copyright 2001-2006 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#include + +#include +#include +#include "flstring.h" +#include +#include +#include +#include "Fl_Text_Buffer_mod.H" +#include "Fl_Text_Display_mod.H" +#include + +#undef min +#undef max + +// Text area margins. Left & right margins should be at least 3 so that +// there is some room for the overhanging parts of the cursor! +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 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 */ +#define FILL_MASK 0x0100 +#define SECONDARY_MASK 0x0200 +#define PRIMARY_MASK 0x0400 +#define HIGHLIGHT_MASK 0x0800 +#define BG_ONLY_MASK 0x1000 +#define TEXT_ONLY_MASK 0x2000 +#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 */ +#define MAX_DISP_LINE_LEN 1000 + +static int max( int i1, int i2 ); +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. */ +static int scroll_direction = 0; +static int scroll_amount = 0; +static int scroll_y = 0; +static int scroll_x = 0; + +// CET - FIXME +#define TMPFONTWIDTH 6 + +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; + RIGHT_MARGIN = DEFAULT_RIGHT_MARGIN; + + mMaxsize = 0; + damage_range1_start = damage_range1_end = -1; + damage_range2_start = damage_range2_end = -1; + dragPos = dragType = dragging = 0; + display_insert_position_hint = 0; + + color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR); + box(FL_DOWN_FRAME); + textsize((uchar)FL_NORMAL_SIZE); + textcolor(FL_FOREGROUND_COLOR); + textfont(FL_HELVETICA); + + 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(16); + scrollbar_align(FL_ALIGN_BOTTOM_RIGHT); + + mCursorOn = 0; + mCursorPos = 0; + mCursorOldY = -100; + mCursorToHint = NO_HINT; + mCursorStyle = NORMAL_CURSOR; + mCursorPreferredCol = -1; + mBuffer = 0; + mFirstChar = 0; + mLastChar = 0; + mNBufferLines = 0; + mTopLineNum = mTopLineNumHint = 1; + mAbsTopLineNum = 1; + mNeedAbsTopLineNum = 0; + mHorizOffset = mHorizOffsetHint = 0; + + mCursor_color = FL_FOREGROUND_COLOR; + + mFixedFontWidth = -1; + mStyleBuffer = 0; + mStyleTable = 0; + mNStyles = 0; + mNVisibleLines = 1; + mLineStarts = new int[mNVisibleLines]; + mLineStarts[0] = 0; + for (i=1; iremove_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) +*/ +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 */ + if ( buf == mBuffer) return; + if ( mBuffer != 0 ) { + buffer_modified_cb( 0, 0, mBuffer->length(), 0, 0, this ); + 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 */ + 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. +*/ +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 ) { + mStyleBuffer = styleBuffer; + mStyleTable = styleTable; + mNStyles = nStyles; + mUnfinishedStyle = unfinishedStyle; + mUnfinishedHighlightCB = unfinishedHighlightCB; + mHighlightCBArg = cbArg; + + mStyleBuffer->canUndo(0); +#if 0 + // FIXME: this is in nedit code -- is it needed? + /* Call TextDSetFont to combine font information from style table and + primary font, adjust font-related parameters, and then redisplay */ + TextDSetFont(textD, textD->fontStruct); +#endif + damage(FL_DAMAGE_EXPOSE); +} + +#if 0 + // FIXME: this is in nedit code -- is it needed? +/* +** Change the (non highlight) font +*/ +void TextDSetFont(textDisp *textD, XFontStruct *fontStruct) { + Display *display = XtDisplay(textD->w); + int i, maxAscent = fontStruct->ascent, maxDescent = fontStruct->descent; + int width, height, fontWidth; + Pixel bgPixel, fgPixel, selectFGPixel, selectBGPixel; + Pixel highlightFGPixel, highlightBGPixel; + XGCValues values; + XFontStruct *styleFont; + + /* If font size changes, cursor will be redrawn in a new position */ + blankCursorProtrusions(textD); + + /* If there is a (syntax highlighting) style table in use, find the new + maximum font height for this text display */ + for (i=0; inStyles; i++) { + styleFont = textD->styleTable[i].font; + if (styleFont != NULL && styleFont->ascent > maxAscent) + maxAscent = styleFont->ascent; + if (styleFont != NULL && styleFont->descent > maxDescent) + maxDescent = styleFont->descent; + } + textD->ascent = maxAscent; + textD->descent = maxDescent; + + /* If all of the current fonts are fixed and match in width, compute */ + fontWidth = fontStruct->max_bounds.width; + if (fontWidth != fontStruct->min_bounds.width) + fontWidth = -1; + else { + for (i=0; inStyles; i++) { + styleFont = textD->styleTable[i].font; + if (styleFont != NULL && (styleFont->max_bounds.width != fontWidth || + styleFont->max_bounds.width != styleFont->min_bounds.width)) + fontWidth = -1; + } + } + textD->fixedFontWidth = fontWidth; + + /* Don't let the height dip below one line, or bad things can happen */ + if (textD->height < maxAscent + maxDescent) + textD->height = maxAscent + maxDescent; + + /* Change the font. In most cases, this means re-allocating the + affected GCs (they are shared with other widgets, and if the primary + font changes, must be re-allocated to change it). Unfortunately, + this requres recovering all of the colors from the existing GCs */ + textD->fontStruct = fontStruct; + XGetGCValues(display, textD->gc, GCForeground|GCBackground, &values); + fgPixel = values.foreground; + bgPixel = values.background; + XGetGCValues(display, textD->selectGC, GCForeground|GCBackground, &values); + selectFGPixel = values.foreground; + selectBGPixel = values.background; + XGetGCValues(display, textD->highlightGC,GCForeground|GCBackground,&values); + highlightFGPixel = values.foreground; + highlightBGPixel = values.background; + releaseGC(textD->w, textD->gc); + releaseGC(textD->w, textD->selectGC); + releaseGC(textD->w, textD->highlightGC); + releaseGC(textD->w, textD->selectBGGC); + releaseGC(textD->w, textD->highlightBGGC); + if (textD->lineNumGC != NULL) + releaseGC(textD->w, textD->lineNumGC); + textD->lineNumGC = NULL; + allocateFixedFontGCs(textD, fontStruct, bgPixel, fgPixel, selectFGPixel, + selectBGPixel, highlightFGPixel, highlightBGPixel); + XSetFont(display, textD->styleGC, fontStruct->fid); + + /* Do a full resize to force recalculation of font related parameters */ + width = textD->width; + height = textD->height; + textD->width = textD->height = 0; + TextDResize(textD, width, height); + + /* Redisplay */ + TextDRedisplayRect(textD, textD->left, textD->top, textD->width, + textD->height); + + /* Clean up line number area in case spacing has changed */ + draw_line_numbers(textD, True); +} + +int TextDMinFontWidth(textDisp *textD, Boolean considerStyles) { + int fontWidth = textD->fontStruct->max_bounds.width; + int i; + + if (considerStyles) { + for (i = 0; i < textD->nStyles; ++i) { + int thisWidth = (textD->styleTable[i].font)->min_bounds.width; + if (thisWidth < fontWidth) { + fontWidth = thisWidth; + } + } + } + return(fontWidth); +} + +int TextDMaxFontWidth(textDisp *textD, Boolean considerStyles) { + int fontWidth = textD->fontStruct->max_bounds.width; + int i; + + if (considerStyles) { + for (i = 0; i < textD->nStyles; ++i) { + int thisWidth = (textD->styleTable[i].font)->max_bounds.width; + if (thisWidth > fontWidth) { + fontWidth = thisWidth; + } + } + } + return(fontWidth); +} +#endif + +int Fl_Text_Display_mod::longest_vline() { + int longest = 0; + for (int i = 0; i < mNVisibleLines; i++) + longest = max(longest, measure_vline(i)); + return longest; +} + +/* +** Change the size of the displayed text area +*/ +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); +#endif // DEBUG + Fl_Widget::resize(X,Y,W,H); + if (!buffer()) return; + X += Fl::box_dx(box()); + 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; + + /* 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? + int hscrollbarvisible = mHScrollBar->visible(); + int vscrollbarvisible = mVScrollBar->visible(); + + // try without scrollbars first + mVScrollBar->clear_visible(); + mHScrollBar->clear_visible(); + + for (int again = 1; again;) { + 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) { + 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); + +#ifdef DEBUG + printf(" mNBufferLines=%d\n", mNBufferLines); +#endif // DEBUG + } + + /* reallocate and update the line starts array, which may have changed + size and / or contents. */ + int nvlines = (text_area.h + mMaxsize - 1) / mMaxsize; + if (nvlines < 1) nvlines = 1; + if (mNVisibleLines != nvlines) { + mNVisibleLines = nvlines; + 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) + { + if (!mVScrollBar->visible()) + again = 1; + mVScrollBar->set_visible(); + if (scrollbar_align() & FL_ALIGN_LEFT) { + text_area.x = X+scrollbar_width()+LEFT_MARGIN; + text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN; + mVScrollBar->resize(X, text_area.y-TOP_MARGIN, scrollbar_width(), + text_area.h+TOP_MARGIN+BOTTOM_MARGIN); + } else { + text_area.x = X+LEFT_MARGIN; + text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN; + mVScrollBar->resize(X+W-scrollbar_width(), text_area.y-TOP_MARGIN, + 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. + */ + /* 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) && + (mVScrollBar->visible() || longest_vline() > text_area.w)) + { + if (!mHScrollBar->visible()) { + mHScrollBar->set_visible(); + again = 1; // loop again to see if we now need vert. & recalc sizes + } + if (scrollbar_align() & FL_ALIGN_TOP) { + text_area.y = Y + scrollbar_width()+TOP_MARGIN; + text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN; + mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y, + text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width()); + } else { + text_area.y = Y+TOP_MARGIN; + text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN; + mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y+H-scrollbar_width(), + text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width()); + } + } + } + } + + // 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) + 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); + + // 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 || + 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 +*/ +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); + + fl_pop_clip(); +} + +void Fl_Text_Display_mod::redisplay_range(int startpos, int endpos) { + if (damage_range1_start == -1 && damage_range1_end == -1) { + damage_range1_start = startpos; + damage_range1_end = endpos; + } else if ((startpos >= damage_range1_start && startpos <= damage_range1_end) || + (endpos >= damage_range1_start && endpos <= damage_range1_end)) { + damage_range1_start = min(damage_range1_start, startpos); + damage_range1_end = max(damage_range1_end, endpos); + } else if (damage_range2_start == -1 && damage_range2_end == -1) { + damage_range2_start = startpos; + damage_range2_end = endpos; + } else { + damage_range2_start = min(damage_range2_start, startpos); + damage_range2_end = max(damage_range2_end, 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. +*/ +void Fl_Text_Display_mod::draw_range(int startpos, int 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; + + /* 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; + if ( !position_to_line( startpos, &startLine ) ) + startLine = mNVisibleLines - 1; + if ( endpos >= mLastChar ) { + lastLine = mNVisibleLines - 1; + } else { + if ( !position_to_line( endpos, &lastLine ) ) { + /* shouldn't happen */ + lastLine = mNVisibleLines - 1; + } + } + + /* Get the starting and ending positions within the lines */ + 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" */ + if ( startLine == lastLine ) { + draw_vline( startLine, 0, INT_MAX, startIndex, endIndex ); + return; + } + + /* Redisplay the first line from "start" */ + draw_vline( startLine, 0, INT_MAX, startIndex, INT_MAX ); + + /* Redisplay the lines in between at their full width */ + for ( i = startLine + 1; i < lastLine; i++ ) + draw_vline( i, 0, INT_MAX, 0, INT_MAX ); + + /* Redisplay the last line to "end" */ + draw_vline( lastLine, 0, INT_MAX, 0, endIndex ); +} + +/* +** Set the position of the text insertion cursor for text display +*/ +void Fl_Text_Display_mod::insert_position( int newPos ) { + /* make sure new position is ok, do nothing if it hasn't changed */ + 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); + + mCursorPos = newPos; + + /* draw cursor at its new position */ + redisplay_range(mCursorPos - 1, mCursorPos + 1); +} + +void Fl_Text_Display_mod::show_cursor(int b) { + mCursorOn = b; + redisplay_range(mCursorPos - 1, mCursorPos + 1); +} + +void Fl_Text_Display_mod::cursor_style(int style) { + mCursorStyle = style; + if (mCursorOn) show_cursor(); +} + +void Fl_Text_Display_mod::wrap_mode(int wrap, int wrapMargin) { + mWrapMargin = wrapMargin; + mContinuousWrap = wrap; + + 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 */ + 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(); + } else { + // No buffer, so just clear the state info for later... + mNBufferLines = 0; + mFirstChar = 0; + mTopLineNum = 1; + mAbsTopLineNum = 0; + } + + resize(x(), y(), w(), h()); +} + +/* +** Insert "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; + + mCursorToHint = pos + strlen( text ); + mBuffer->insert( pos, text ); + mCursorToHint = NO_HINT; +} + +/* +** Insert "text" (which must not contain newlines), overstriking the current +** cursor location. +*/ +void Fl_Text_Display_mod::overstrike(const char* 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; + + /* 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(), buf->null_substitution_character() ); + endIndent = indent; + + /* find which characters to remove, and if necessary generate additional + padding to make up for removed control characters at the end */ + indent = startIndent; + for ( p = startPos; ; p++ ) { + if ( p == buf->length() ) + break; + ch = buf->character( p ); + if ( ch == '\n' ) + break; + indent += Fl_Text_Buffer_mod::character_width( ch, indent, buf->tab_distance(), buf->null_substitution_character() ); + if ( indent == endIndent ) { + p++; + break; + } else if ( indent > endIndent ) { + if ( ch != '\t' ) { + p++; + paddedText = new char [ textLen + FL_TEXT_MAX_EXP_CHAR_LEN + 1 ]; + strcpy( paddedText, text ); + for ( i = 0; i < indent - endIndent; i++ ) + paddedText[ textLen + i ] = ' '; + paddedText[ textLen + i ] = '\0'; + } + break; + } + } + endPos = p; + + mCursorToHint = startPos + textLen; + buf->replace( startPos, endPos, paddedText == NULL ? text : paddedText ); + mCursorToHint = NO_HINT; + if ( paddedText != NULL ) + 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. +*/ + +int Fl_Text_Display_mod::position_to_xy( int pos, int* X, int* Y ) { + 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); + + /* 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 */ + 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++ ) { + charLen = Fl_Text_Buffer_mod::expand_character( lineStr[ charIndex ], outIndex, expandedChar, + mBuffer->tab_distance(), mBuffer->null_substitution_character() ); + charStyle = position_style( lineStartPos, lineLen, charIndex, + outIndex ); + xStep += string_width( expandedChar, charLen, charStyle ); + outIndex += charLen; + } + *X = xStep; + free((char *)lineStr); + 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 ) { + 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; + } + + retVal = position_to_line( pos, lineNum ); + if ( retVal ) { + *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 +*/ +int Fl_Text_Display_mod::in_selection( int X, int Y ) { + int row, column, pos = xy_to_position( X, Y, CHARACTER_POS ); + Fl_Text_Buffer_mod*buf = mBuffer; + + 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); +} + +/* +** 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. +*/ +int Fl_Text_Display_mod::wrapped_column(int row, int column) { + 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); +} + +/* +** 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) { + 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. +*/ +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) { + 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); + } + + /* 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 (!position_to_xy( mCursorPos, &X, &Y )) { + scroll_(topLine, hOffset); + if (!position_to_xy( mCursorPos, &X, &Y )) { + #ifdef DEBUG + printf ("*** display_insert/position_to_xy # GIVE UP !\n"); fflush(stdout); + #endif // DEBUG + return; /* Give up, it's not worth it (but why does it fail?) */ + } + } + if (X > text_area.x + text_area.w) + 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); +} + +void Fl_Text_Display_mod::show_insert_position() { + display_insert_position_hint = 1; + resize(x(), y(), w(), h()); +} + +/* +** Cursor movement functions +*/ +int Fl_Text_Display_mod::move_right() { + if ( mCursorPos >= mBuffer->length() ) + return 0; + insert_position( mCursorPos + 1 ); + return 1; +} + +int Fl_Text_Display_mod::move_left() { + if ( mCursorPos <= 0 ) + return 0; + insert_position( mCursorPos - 1 ); + return 1; +} + +int Fl_Text_Display_mod::move_up() { + int lineStartPos, column, prevLineStartPos, newPos, visLineNum; + + /* Find the position of the start of the line. Use the line starts array + if possible */ + if ( position_to_line( mCursorPos, &visLineNum ) ) + lineStartPos = mLineStarts[ visLineNum ]; + else { + lineStartPos = line_start( mCursorPos ); + visLineNum = -1; + } + 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 ); + + /* 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)); + + /* move the cursor */ + insert_position( newPos ); + + /* if a preferred column wasn't aleady established, establish it */ + mCursorPreferredCol = column; + return 1; +} + +int Fl_Text_Display_mod::move_down() { + int lineStartPos, column, nextLineStartPos, 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 ); + mCursorPreferredCol = column; + 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. +*/ +int Fl_Text_Display_mod::count_lines(int startPos, int endPos, + bool startPosIsLineStart) { + int retLines, retPos, retLineStart, retLineEnd; + +#ifdef DEBUG + 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); + +#ifdef DEBUG + printf(" # after WLC: retPos=%d, retLines=%d, retLineStart=%d, retLineEnd=%d\n", + retPos, retLines, retLineStart, retLineEnd); +#endif // DEBUG + + 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. +*/ +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; +} + +/* +** 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) { + 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. +*/ +int Fl_Text_Display_mod::line_start(int pos) { + int retLines, retPos, retLineStart, retLineEnd; + + /* If we're not wrapping, use the more efficient BufStartOfLine */ + if (!mContinuousWrap) + return buffer()->line_start(pos); + + wrapped_line_counter(buffer(), buffer()->line_start(pos), pos, INT_MAX, true, 0, + &retPos, &retLines, &retLineStart, &retLineEnd); + 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); + + 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) { + return c != '$' && c != '_' && (isspace(c) || ispunct(c)); +} + +void Fl_Text_Display_mod::next_word() { + int pos = insert_position(); + while (pos < buffer()->length() && !fl_isseparator(buffer()->character(pos))) { + pos++; + } + while (pos < buffer()->length() && fl_isseparator(buffer()->character(pos))) { + pos++; + } + + insert_position( pos ); +} + +void Fl_Text_Display_mod::previous_word() { + int pos = insert_position(); + if (pos==0) return; + pos--; + while (pos && fl_isseparator(buffer()->character(pos))) { + pos--; + } + while (pos && !fl_isseparator(buffer()->character(pos))) { + pos--; + } + if (fl_isseparator(buffer()->character(pos))) 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 +*/ +void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDeleted, + 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; + + /* 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 ); + } + + /* 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 ); + } + } 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(); + } + + /* 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; + textD->mCursorToHint = NO_HINT; + } else if ( textD->mCursorPos > pos ) { + if ( textD->mCursorPos < pos + nDeleted ) + textD->mCursorPos = pos; + 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); + if ( textD->mStyleBuffer ) /* See comments in extendRangeForStyleMods */ + 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; + if ( origCursorPos == startDispPos && textD->mCursorPos != startDispPos ) + startDispPos = min( startDispPos, origCursorPos - 1 ); + if ( linesInserted == linesDeleted ) { + if ( nInserted == 0 && nDeleted == 0 ) + endDispPos = pos + nRestyled; + else { + endDispPos = textD->mContinuousWrap ? wrapModEnd : + buf->line_end( pos + nInserted ) + 1; + // CET - FIXME if ( origCursorPos >= startDispPos && + // ( origCursorPos <= endDispPos || endDispPos == buf->length() ) ) + } + + if (linesInserted > 1) textD->draw_line_numbers(false); + } else { + endDispPos = textD->mLastChar + 1; + // 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 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 */ + if ( textD->mStyleBuffer ) + textD->extend_range_for_styles( &startDispPos, &endDispPos ); + + /* Redisplay computed range */ + 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. +*/ +void Fl_Text_Display_mod::maintain_absolute_top_line_number(int state) { + 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. +*/ +int Fl_Text_Display_mod::get_absolute_top_line_number() { + if (!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. +*/ +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); + } +} + +/* +** 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() { + return 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. +*/ +void Fl_Text_Display_mod::reset_absolute_top_line_number() { + 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( int pos, int *lineNum ) { + int i; + + *lineNum = 0; + if ( pos < mFirstChar ) return 0; + if ( pos > mLastChar ) { + if ( empty_vlines() ) { + if ( mLastChar < mBuffer->length() ) { + if ( !position_to_line( mLastChar, lineNum ) ) { + Fl::error("Fl_Text_Display_mod::position_to_line(): Consistency check ptvl failed"); + return 0; + } + return ++( *lineNum ) <= mNVisibleLines - 1; + } else { + position_to_line( mLastChar - 1, lineNum ); + return 1; + } + } + return 0; + } + + for ( i = mNVisibleLines - 1; i >= 0; i-- ) { + if ( mLineStarts[ i ] != -1 && pos >= mLineStarts[ i ] ) { + *lineNum = i; + return 1; + } + } + 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"). +*/ +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); + + /* 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"); + 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++ ) { + charLen = charIndex >= lineLen ? 1 : + Fl_Text_Buffer_mod::expand_character( lineStr[ charIndex ], outIndex, + expandedChar, buf->tab_distance(), buf->null_substitution_character() ); + 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++ ) { + charLen = charIndex >= lineLen ? 1 : + Fl_Text_Buffer_mod::expand_character( lineStr[ charIndex ], outIndex, expandedChar, + buf->tab_distance(), buf->null_substitution_character() ); + charStyle = position_style( lineStartPos, lineLen, charIndex, + outIndex + dispIndexOffset ); + for ( i = 0; i < charLen; i++ ) { + 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 ]; + charWidth = string_width( &expandedChar[ i ], 1, 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++ ) { + charLen = charIndex >= lineLen ? 1 : + Fl_Text_Buffer_mod::expand_character( lineStr[ charIndex ], outIndex, expandedChar, + buf->tab_distance(), buf->null_substitution_character() ); + charStyle = position_style( lineStartPos, lineLen, charIndex, + outIndex + dispIndexOffset ); + for ( i = 0; i < charLen; i++ ) { + 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 ]; + charWidth = string_width( &expandedChar[ i ], 1, 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); +} + +/* +** 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; + + /* 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. */ + + 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 & (HIGHLIGHT_MASK | PRIMARY_MASK)) { + if (Fl::focus() == this) background = selection_color(); + else background = fl_color_average(color(), selection_color(), 0.5f); + } else background = color(); + + // foreground = fl_contrast(styleRec->color, background); + foreground = styleRec->color; + } else if (style & (HIGHLIGHT_MASK | PRIMARY_MASK)) { + if (Fl::focus() == this) background = selection_color(); + else background = fl_color_average(color(), selection_color(), 0.5f); + 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 ); + } + if (!(style & BG_ONLY_MASK)) { + fl_color( foreground ); + fl_font( font, fsize ); + fl_draw( string, nChars, X, Y + mMaxsize - fl_descent()); + } + + // 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 */ + /* + 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); + */ +} + +/* +** 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 ) { + /* 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)) { + fl_color(fl_color_average(color(), selection_color(), 0.5f)); + } else { + fl_color( color() ); + } + fl_rectf( X, Y, width, height ); + } else if ( style & HIGHLIGHT_MASK ) { + fl_color( fl_contrast(textcolor(), color()) ); + fl_rectf( X, Y, width, height ); + } else if ( style & PRIMARY_MASK ) { + fl_color( selection_color() ); + fl_rectf( X, Y, width, height ); + } else { + fl_color( color() ); + fl_rectf( X, Y, width, height ); + } +} + + + +/* +** Draw a cursor with top center at X, y. +*/ +void Fl_Text_Display_mod::draw_cursor( int X, int Y ) { + typedef struct { + int x1, y1, x2, y2; + } + Segment; + + Segment segs[ 5 ]; + int left, right, cursorWidth, midY; + // int fontWidth = mFontStruct->min_bounds.width, nSegs = 0; + int fontWidth = TMPFONTWIDTH; // CET - FIXME + 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. */ + 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; + segs[ 0 ].x1 = left; segs[ 0 ].y1 = bot; segs[ 0 ].x2 = X; segs[ 0 ].y2 = midY; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = midY - 1; + segs[ 3 ].x1 = X; segs[ 3 ].y1 = midY - 1; segs[ 3 ].x2 = right; segs[ 3 ].y2 = bot; + nSegs = 4; + } else if ( mCursorStyle == NORMAL_CURSOR ) { + segs[ 0 ].x1 = left; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = right; segs[ 2 ].y2 = bot; + nSegs = 3; + } else if ( mCursorStyle == HEAVY_CURSOR ) { + segs[ 0 ].x1 = X - 1; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X - 1; segs[ 0 ].y2 = bot; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = X + 1; segs[ 2 ].y1 = Y; segs[ 2 ].x2 = X + 1; segs[ 2 ].y2 = bot; + segs[ 3 ].x1 = left; segs[ 3 ].y1 = Y; segs[ 3 ].x2 = right; segs[ 3 ].y2 = Y; + segs[ 4 ].x1 = left; segs[ 4 ].y1 = bot; segs[ 4 ].x2 = right; segs[ 4 ].y2 = bot; + nSegs = 5; + } else if ( mCursorStyle == DIM_CURSOR ) { + midY = Y + fontHeight / 2; + segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X; segs[ 0 ].y2 = Y; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = X; segs[ 1 ].y2 = midY; + segs[ 2 ].x1 = X; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot; + nSegs = 3; + } else if ( mCursorStyle == BLOCK_CURSOR ) { + right = X + fontWidth; + segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y; + segs[ 1 ].x1 = right; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = right; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot; + segs[ 3 ].x1 = X; segs[ 3 ].y1 = bot; segs[ 3 ].x2 = X; segs[ 3 ].y2 = 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 ) { + 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 ) { + style = ( unsigned char ) styleBuf->character( pos ); + if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { + /* encountered "unfinished" style, trigger parsing */ + (mUnfinishedHighlightCB)( pos, mHighlightCBArg); + style = (unsigned char) styleBuf->character( pos); + } + } + if (buf->primary_selection()->includes(pos, lineStartPos, dispIndex)) + style |= PRIMARY_MASK; + if (buf->highlight_selection()->includes(pos, lineStartPos, dispIndex)) + style |= HIGHLIGHT_MASK; + if (buf->secondary_selection()->includes(pos, lineStartPos, dispIndex)) + 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 ) { + Fl_Font font; + int fsize; + + if ( 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 { + font = textfont(); + fsize = textsize(); + } + fl_font( font, fsize ); + + return ( int ) ( 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 ) { + int charIndex, lineStart, lineLen, fontHeight; + int charWidth, charLen, charStyle, visLineNum, xStep, outIndex; + char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; + const char *lineStr; + + /* Find the visible line number corresponding to the Y coordinate */ + fontHeight = mMaxsize; + visLineNum = ( Y - text_area.y ) / fontHeight; + if ( visLineNum < 0 ) + 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++ ) { + charLen = Fl_Text_Buffer_mod::expand_character( lineStr[ charIndex ], outIndex, expandedChar, + mBuffer->tab_distance(), mBuffer->null_substitution_character() ); + 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; +} + +/* +** 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. +*/ +void Fl_Text_Display_mod::xy_to_rowcol( int X, int Y, int *row, + int *column, int posType ) { + 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; + *column = ( ( X - text_area.x ) + mHorizOffset + + ( 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) +*/ +void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { + int oldTopLineNum = mTopLineNum; + int oldFirstChar = mFirstChar; + int lineDelta = newTopLineNum - oldTopLineNum; + int nVisLines = mNVisibleLines; + 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) */ + lastLineNum = oldTopLineNum + nVisLines - 1; + if ( newTopLineNum < oldTopLineNum && newTopLineNum < -lineDelta ) { + mFirstChar = skip_lines( 0, newTopLineNum - 1, true ); + } else if ( newTopLineNum < oldTopLineNum ) { + mFirstChar = rewind_lines( mFirstChar, -lineDelta ); + } else if ( newTopLineNum < lastLineNum ) { + mFirstChar = lineStarts[ newTopLineNum - oldTopLineNum ]; + } else if ( newTopLineNum - lastLineNum < mNBufferLines - newTopLineNum ) { + mFirstChar = skip_lines( lineStarts[ nVisLines - 1 ], + 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-- ) + lineStarts[ i ] = lineStarts[ i + lineDelta ]; + calc_line_starts( 0, -lineDelta ); + } else if ( lineDelta > 0 && lineDelta < nVisLines ) { + for ( i = 0; i < nVisLines - lineDelta; i++ ) + lineStarts[ i ] = lineStarts[ i + lineDelta ]; + 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); +} + +/* +** 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; + 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 */ + if ( pos + charsDeleted < mFirstChar ) { + mTopLineNum += lineDelta; + for ( i = 0; i < nVisLines && lineStarts[i] != -1; i++ ) + lineStarts[ i ] += charDelta; + mFirstChar += charDelta; + mLastChar += charDelta; + *scrolled = 0; + return; + } + + /* The change began before the beginning of the displayed text, but + 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 ) { + mTopLineNum = max( 1, mTopLineNum + lineDelta ); + mFirstChar = rewind_lines( + lineStarts[ lineOfEnd ] + charDelta, lineOfEnd ); + /* Otherwise anchor on original line number and recount everything */ + } else { + if ( mTopLineNum > mNBufferLines + lineDelta ) { + mTopLineNum = 1; + mFirstChar = 0; + } else + mFirstChar = skip_lines( 0, mTopLineNum - 1, true ); + } + calc_line_starts( 0, nVisLines - 1 ); + /* calculate lastChar by finding the end of the last displayed line */ + calc_last_char(); + *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 */ + if ( pos <= mLastChar ) { + /* find line on which the change began */ + position_to_line( pos, &lineOfPos ); + /* salvage line starts after the changed area */ + if ( lineDelta == 0 ) { + for ( i = lineOfPos + 1; i < nVisLines && lineStarts[ i ] != -1; i++ ) + lineStarts[ i ] += charDelta; + } else if ( lineDelta > 0 ) { + for ( i = nVisLines - 1; i >= lineOfPos + lineDelta + 1; i-- ) + lineStarts[ i ] = lineStarts[ i - lineDelta ] + + ( 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 ); + } + /* fill in the missing line starts */ + if ( linesInserted >= 0 ) + calc_line_starts( lineOfPos + 1, lineOfPos + linesInserted ); + if ( lineDelta < 0 ) + calc_line_starts( nVisLines + lineDelta, nVisLines ); + /* calculate lastChar by finding the end of the last displayed line */ + calc_last_char(); + *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 */ + if ( empty_vlines() ) { + position_to_line( pos, &lineOfPos ); + calc_line_starts( lineOfPos, lineOfPos + linesInserted ); + calc_last_char(); + *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. +*/ +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; + if ( startLine < 0 ) startLine = 0; + 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 */ + 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 */ + 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 */ + if ( line == 0 || ( lineStarts[ line - 1 ] != bufLen && + lineEnd != nextLineStart ) ) { + lineStarts[ line ] = bufLen; + line++; + } + break; + } + 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. +*/ +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); +} + +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) { + /* 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 */ + if (mHorizOffset == horizOffset && mTopLineNum == topLineNum) + return; + + /* If the vertical scroll position has changed, update the line + 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); +} + +/* +** Update the minimum, maximum, slider size, page increment, and value +** for vertical scroll bar. +*/ +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 */ +#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->linesize(3); +} + +/* +** Update the minimum, maximum, slider size, page increment, and value +** for the horizontal scroll bar. +*/ +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 +*/ +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); +} + +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. +*/ +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')) { + snprintf(lineNumString, sizeof(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 +} + +static int max( int i1, int i2 ) { + return i1 >= i2 ? i1 : i2; +} + +static int min( int i1, int i2 ) { + return i1 <= i2 ? i1 : i2; +} + +/* +** Count the number of newlines in a null-terminated text string; +*/ +static int countlines( const char *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" +*/ +int Fl_Text_Display_mod::measure_vline( int visLineNum ) { + int i, width = 0, len, style, lineLen = vline_length( visLineNum ); + int charCount = 0, lineStartPos = mLineStarts[ visLineNum ]; + char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ]; + + 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 ); + 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 true if there are lines visible with no corresponding buffer text +*/ +int Fl_Text_Display_mod::empty_vlines() { + 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 +*/ +int Fl_Text_Display_mod::vline_length( int visLineNum ) { + 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; + 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. +*/ +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); + + + /* + ** 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->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 (visLineNumcopy(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. +** +** 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). +*/ +void Fl_Text_Display_mod::measure_deleted_lines(int pos, int nDeleted) { + int retPos, retLines, retLineStart, retLineEnd; + Fl_Text_Buffer_mod*buf = buffer(); + int nVisLines = mNVisibleLines; + int *lineStarts = mLineStarts; + int countFrom, lineStart; + int 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]; + 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->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) { + 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; + char nullSubsChar = buffer()->null_substitution_character(); + + /* 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++) { + 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(c, colNum, tabDist, nullSubsChar); + 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--) { + 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(c, colNum, tabDist, nullSubsChar); + 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) { + 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(), buffer()->null_substitution_character()); + if (styleBuf == 0) { + style = 0; + } else { + style = (unsigned char)styleBuf->character(pos); + if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { + /* encountered "unfinished" style, trigger parsing */ + (mUnfinishedHighlightCB)(pos, mHighlightCBArg); + 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) { + 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); + return; +} + +/* +** 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. +*/ +int Fl_Text_Display_mod::wrap_uses_character(int lineEndPos) { + char c; + + if (!mContinuousWrap || lineEndPos == buffer()->length()) + return 1; + + c = buffer()->character(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(Fl_Text_Selection *sel, + int rangeStart, int rangeEnd) { + 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). +*/ +void Fl_Text_Display_mod::extend_range_for_styles( int *startpos, int *endpos ) { + Fl_Text_Selection * 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. */ + if ( sel->selected() ) { + if ( sel->start() < *startpos ) { + *startpos = sel->start(); + extended = 1; + } + if ( sel->end() > *endpos ) { + *endpos = sel->end(); + 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 ) + *endpos = mBuffer->line_end( *endpos ) + 1; +} + +// The draw() method. It tries to minimize what is draw as much as 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()); + // 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); + // CET - FIXME - save old cursor position instead and just draw side needed? + fl_push_clip(text_area.x-LEFT_MARGIN, + text_area.y, + text_area.w+LEFT_MARGIN+RIGHT_MARGIN, + text_area.h); + fl_rectf(text_area.x-LEFT_MARGIN, mCursorOldY, + LEFT_MARGIN, mMaxsize, color()); + fl_rectf(text_area.x+text_area.w, mCursorOldY, + RIGHT_MARGIN, mMaxsize, color()); + fl_pop_clip(); + } + + // draw the scrollbars + if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_CHILD)) { + mVScrollBar->damage(FL_DAMAGE_ALL); + mHScrollBar->damage(FL_DAMAGE_ALL); + } + update_child(*mVScrollBar); + update_child(*mHScrollBar); + + // draw all of the text + if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_EXPOSE)) { + //printf("drawing all text\n"); + int X, Y, W, H; + if (fl_clip_box(text_area.x, text_area.y, text_area.w, text_area.h, + X, Y, W, H)) { + // Draw text using the intersected clipping box... + // (this sets the clipping internally) + draw_text(X, Y, W, H); + } else { + // Draw the whole area... + draw_text(text_area.x, text_area.y, text_area.w, text_area.h); + } + } + else if (damage() & FL_DAMAGE_SCROLL) { + // draw some lines of text + fl_push_clip(text_area.x, text_area.y, + text_area.w, text_area.h); + //printf("drawing text from %d to %d\n", damage_range1_start, damage_range1_end); + draw_range(damage_range1_start, damage_range1_end); + if (damage_range2_end != -1) { + //printf("drawing text from %d to %d\n", damage_range2_start, damage_range2_end); + draw_range(damage_range2_start, damage_range2_end); + } + damage_range1_start = damage_range1_end = -1; + 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 ) { + 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!"); + //printf("drew cursor at pos: %d (%d,%d)\n", mCursorPos, X, Y); + mCursorOldY = Y; + fl_pop_clip(); + } + fl_pop_clip(); +} + +// this processes drag events due to mouse for Fl_Text_Display and +// also drags due to cursor movement with shift held down for +// Fl_Text_Editor +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) { + d->buffer()->select(d->dragPos, pos); + } else { + d->buffer()->select(pos, d->dragPos); + } + d->insert_position(pos); + } else if (d->dragType == Fl_Text_Display_mod::DRAG_WORD) { + if (pos >= d->dragPos) { + d->insert_position(d->word_end(pos)); + d->buffer()->select(d->word_start(d->dragPos), d->word_end(pos)); + } else { + d->insert_position(d->word_start(pos)); + d->buffer()->select(d->word_start(pos), d->word_end(d->dragPos)); + } + } else if (d->dragType == Fl_Text_Display_mod::DRAG_LINE) { + if (pos >= d->dragPos) { + d->insert_position(d->buffer()->line_end(pos)+1); + d->buffer()->select(d->buffer()->line_start(d->dragPos), + d->buffer()->line_end(pos)+1); + } else { + d->insert_position(d->buffer()->line_start(pos)); + d->buffer()->select(d->buffer()->line_start(pos), + d->buffer()->line_end(d->dragPos)+1); + } + } +} + +// 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; + } + fl_text_drag_me(pos, w); + Fl::repeat_timeout(.1, scroll_timer_cb, user_data); +} + + +int Fl_Text_Display_mod::handle(int event) { + if (!buffer()) return 0; + // This isn't very elegant! + if (!Fl::event_inside(text_area.x, text_area.y, text_area.w, text_area.h) && + !dragging && event != FL_LEAVE && event != FL_ENTER && + event != FL_MOVE && event != FL_FOCUS && event != FL_UNFOCUS && + event != FL_KEYBOARD && event != FL_KEYUP) { + return Fl_Group::handle(event); + } + + switch (event) { + case FL_ENTER: + case FL_MOVE: + if (active_r()) { + 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); + return 1; + } 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); + 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(); + return 1; + } + + case FL_DRAG: { + if (dragType < 0) return 1; + int X = Fl::event_x(), Y = Fl::event_y(), pos; + // 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); + fl_text_drag_me(pos, this); + } + return 1; + } + + case FL_RELEASE: { + 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: + if (buffer()->selected()) redraw(); + + 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; + } + + // Select all ? + if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='a') { + buffer()->select(0,buffer()->length()); + return 1; + } + + if (mVScrollBar->handle(event)) return 1; + if (mHScrollBar->handle(event)) return 1; + + break; + } + + return 0; +} + + +// +// End of "$Id: Fl_Text_Display.cxx 5848 2007-05-20 16:18:31Z mike $". +// diff --git a/src/widgets/Fl_Text_Display_mod_1_3.cxx b/src/widgets/Fl_Text_Display_mod_1_3.cxx new file mode 100644 index 00000000..f00cace1 --- /dev/null +++ b/src/widgets/Fl_Text_Display_mod_1_3.cxx @@ -0,0 +1,3313 @@ +// +// "$Id: Fl_Text_Display.cxx 7462 2010-04-06 23:00:56Z matt $" +// +// Copyright 2001-2009 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#include +#include +#include +#include "flstring.h" +#include +#include +#include +#include "Fl_Text_Buffer_mod.H" +#include "Fl_Text_Display_mod.H" +#include + +#undef min +#undef max + +// Text area margins. Left & right margins should be at least 3 so that +// there is some room for the overhanging parts of the cursor! +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 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 */ +#define FILL_MASK 0x0100 +#define SECONDARY_MASK 0x0200 +#define PRIMARY_MASK 0x0400 +#define HIGHLIGHT_MASK 0x0800 +#define BG_ONLY_MASK 0x1000 +#define TEXT_ONLY_MASK 0x2000 +#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 */ +#define MAX_DISP_LINE_LEN 1000 + +static int max( int i1, int i2 ); +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. */ +static int scroll_direction = 0; +static int scroll_amount = 0; +static int scroll_y = 0; +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; + + TOP_MARGIN = DEFAULT_TOP_MARGIN; + BOTTOM_MARGIN = DEFAULT_BOTTOM_MARGIN; + LEFT_MARGIN = DEFAULT_LEFT_MARGIN; + RIGHT_MARGIN = DEFAULT_RIGHT_MARGIN; + + mMaxsize = 0; + damage_range1_start = damage_range1_end = -1; + damage_range2_start = damage_range2_end = -1; + dragPos = dragType = dragging = 0; + display_insert_position_hint = 0; + shortcut_ = 0; + + color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR); + 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; + mBuffer = 0; + mFirstChar = 0; + mLastChar = 0; + mNBufferLines = 0; + mTopLineNum = mTopLineNumHint = 1; + mAbsTopLineNum = 1; + mNeedAbsTopLineNum = 0; + mHorizOffset = mHorizOffsetHint = 0; + + mCursor_color = FL_FOREGROUND_COLOR; + + mFixedFontWidth = -1; + mStyleBuffer = 0; + mStyleTable = 0; + mNStyles = 0; + mNVisibleLines = 1; + mLineStarts = new int[mNVisibleLines]; + mLineStarts[0] = 0; + for (i=1; iremove_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) +*/ +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 */ + if ( buf == mBuffer) return; + if ( mBuffer != 0 ) { + buffer_modified_cb( 0, 0, mBuffer->length(), 0, 0, this ); + 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 */ + 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. +*/ +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 ) { + mStyleBuffer = styleBuffer; + mStyleTable = styleTable; + mNStyles = nStyles; + mUnfinishedStyle = unfinishedStyle; + mUnfinishedHighlightCB = unfinishedHighlightCB; + mHighlightCBArg = cbArg; + + mStyleBuffer->canUndo(0); + damage(FL_DAMAGE_EXPOSE); +} + +int Fl_Text_Display_mod::longest_vline() const { + int longest = 0; + for (int i = 0; i < mNVisibleLines; i++) + longest = max(longest, measure_vline(i)); + return longest; +} + +/** + Change the size of the displayed text area +*/ +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); +#endif // DEBUG + Fl_Widget::resize(X,Y,W,H); + if (!buffer()) return; + X += Fl::box_dx(box()); + 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; + + /* 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; + /* 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) { + 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); + +#ifdef DEBUG + printf(" mNBufferLines=%d\n", mNBufferLines); +#endif // DEBUG + } + + /* reallocate and update the line starts array, which may have changed + size and / or contents. */ + int nvlines = (text_area.h + mMaxsize - 1) / mMaxsize; + if (nvlines < 1) nvlines = 1; + if (mNVisibleLines != nvlines) { + mNVisibleLines = nvlines; + 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) + { + if (!mVScrollBar->visible()) + again = 1; + mVScrollBar->set_visible(); + if (scrollbar_align() & FL_ALIGN_LEFT) { + text_area.x = X+scrollbar_width()+LEFT_MARGIN; + text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN; + mVScrollBar->resize(X, text_area.y-TOP_MARGIN, scrollbar_width(), + text_area.h+TOP_MARGIN+BOTTOM_MARGIN); + } else { + text_area.x = X+LEFT_MARGIN; + text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN; + mVScrollBar->resize(X+W-scrollbar_width(), text_area.y-TOP_MARGIN, + 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. + */ + /* 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) && + (mVScrollBar->visible() || longest_vline() > text_area.w)) + { + if (!mHScrollBar->visible()) { + mHScrollBar->set_visible(); + again = 1; // loop again to see if we now need vert. & recalc sizes + } + if (scrollbar_align() & FL_ALIGN_TOP) { + text_area.y = Y + scrollbar_width()+TOP_MARGIN; + text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN; + mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y, + text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width()); + } else { + text_area.y = Y+TOP_MARGIN; + text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN; + mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y+H-scrollbar_width(), + text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width()); + } + } + } + } + + // 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) + 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); + + // 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 || + 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 +*/ +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); + + fl_pop_clip(); +} + +/** Marks text from start to end as needing a 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++; + } + } + if (damage_range1_start == -1 && damage_range1_end == -1) { + damage_range1_start = startpos; + damage_range1_end = endpos; + } else if ((startpos >= damage_range1_start && startpos <= damage_range1_end) || + (endpos >= damage_range1_start && endpos <= damage_range1_end)) { + damage_range1_start = min(damage_range1_start, startpos); + damage_range1_end = max(damage_range1_end, endpos); + } else if (damage_range2_start == -1 && damage_range2_end == -1) { + damage_range2_start = startpos; + damage_range2_end = endpos; + } else { + damage_range2_start = min(damage_range2_start, startpos); + damage_range2_end = max(damage_range2_end, 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. +*/ +void Fl_Text_Display_mod::draw_range(int startpos, int 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; + + /* 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; + if ( !position_to_line( startpos, &startLine ) ) + startLine = mNVisibleLines - 1; + if ( endpos >= mLastChar ) { + lastLine = mNVisibleLines - 1; + } else { + if ( !position_to_line( endpos, &lastLine ) ) { + /* shouldn't happen */ + lastLine = mNVisibleLines - 1; + } + } + + /* Get the starting and ending positions within the lines */ + 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" */ + if ( startLine == lastLine ) { + draw_vline( startLine, 0, INT_MAX, startIndex, endIndex ); + return; + } + + /* Redisplay the first line from "start" */ + draw_vline( startLine, 0, INT_MAX, startIndex, INT_MAX ); + + /* Redisplay the lines in between at their full width */ + for ( i = startLine + 1; i < lastLine; i++ ) + draw_vline( i, 0, INT_MAX, 0, INT_MAX ); + + /* Redisplay the last line to "end" */ + draw_vline( lastLine, 0, INT_MAX, 0, endIndex ); +} + +/** Sets the position of the text insertion cursor for text display */ +void Fl_Text_Display_mod::insert_position( int newPos ) { + /* make sure new position is ok, do nothing if it hasn't changed */ + 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 + + mCursorPos = newPos; + + /* draw cursor at its new position */ + redisplay_range(mCursorPos - 1, mCursorPos + 1); // FIXME utf8 +} +/** Shows the text cursor */ +void Fl_Text_Display_mod::show_cursor(int b) { + mCursorOn = b; + redisplay_range(mCursorPos - 1, mCursorPos + 1); // FIXME utf8 +} + +/** + 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. + +
+*/ +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; + + 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 */ + 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(); + } else { + // No buffer, so just clear the state info for later... + mNBufferLines = 0; + mFirstChar = 0; + 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; + + mCursorToHint = pos + strlen( text ); + mBuffer->insert( pos, text ); + mCursorToHint = NO_HINT; +} + +/** Replaces text at the current insert position.*/ +void Fl_Text_Display_mod::overstrike(const char* 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; + + /* 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() ); + endIndent = indent; + + /* find which characters to remove, and if necessary generate additional + padding to make up for removed control characters at the end */ + indent = startIndent; + for ( p = startPos; ; p++ ) { + if ( p == buf->length() ) + break; + // FIXME: character is ucs-4 + ch = buf->character( p ); + if ( ch == '\n' ) + break; + indent += Fl_Text_Buffer_mod::character_width( &ch, indent, buf->tab_distance() ); // FIXME: not unicode + if ( indent == endIndent ) { + p++; + break; + } else if ( indent > endIndent ) { + if ( ch != '\t' ) { + p++; + paddedText = new char [ textLen + FL_TEXT_MAX_EXP_CHAR_LEN + 1 ]; + strcpy( paddedText, text ); + for ( i = 0; i < indent - endIndent; i++ ) + paddedText[ textLen + i ] = ' '; + paddedText[ textLen + i ] = '\0'; + } + break; + } + } + endPos = p; + + mCursorToHint = startPos + textLen; + buf->replace( startPos, endPos, paddedText == NULL ? text : paddedText ); + mCursorToHint = NO_HINT; + if ( paddedText != NULL ) + 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. +*/ + +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); + + /* 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 */ + 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); + 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; + } + + retVal = position_to_line( pos, lineNum ); + if ( retVal ) { + *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 +*/ +int Fl_Text_Display_mod::in_selection( int X, int Y ) const { + int row, column, pos = xy_to_position( X, Y, CHARACTER_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); +} + +/** + 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. +*/ +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); +} + +/** + 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. +*/ +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) { + 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); + } + + /* 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 (!position_to_xy( mCursorPos, &X, &Y )) { + scroll_(topLine, hOffset); + if (!position_to_xy( mCursorPos, &X, &Y )) { + #ifdef DEBUG + printf ("*** display_insert/position_to_xy # GIVE UP !\n"); fflush(stdout); + #endif // DEBUG + return; /* Give up, it's not worth it (but why does it fail?) */ + } + } + if (X > text_area.x + text_area.w) + 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.*/ +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.*/ +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; + } + return 1; +} +/** Moves the current insert position left one character.*/ +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; + } + return 1; +} + +/** Moves the current insert position up one line.*/ +int Fl_Text_Display_mod::move_up() { + int lineStartPos, column, prevLineStartPos, newPos, visLineNum; + + /* Find the position of the start of the line. Use the line starts array + if possible */ + if ( position_to_line( mCursorPos, &visLineNum ) ) + lineStartPos = mLineStarts[ visLineNum ]; + else { + lineStartPos = line_start( mCursorPos ); + visLineNum = -1; + } + 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 ); + + /* 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)); + + /* 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; + return 1; +} + +/** Moves the current insert position down one line.*/ +int Fl_Text_Display_mod::move_down() { + int lineStartPos, column, nextLineStartPos, 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 ); + } + } + mCursorPreferredCol = column; + 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. +*/ +int Fl_Text_Display_mod::count_lines(int startPos, int endPos, + bool startPosIsLineStart) const { + int retLines, retPos, retLineStart, retLineEnd; + +#ifdef DEBUG + 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); + +#ifdef DEBUG + printf(" # after WLC: retPos=%d, retLines=%d, retLineStart=%d, retLineEnd=%d\n", + retPos, retLines, retLineStart, retLineEnd); +#endif // DEBUG + + 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. +*/ +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; +} + +/** + 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. +*/ +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); + + wrapped_line_counter(buffer(), buffer()->line_start(pos), pos, INT_MAX, true, 0, + &retPos, &retLines, &retLineStart, &retLineEnd); + 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); + + 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) { + return c != '$' && c != '_' && (isspace(c) || ispunct(c)); +} + +/** 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++; + } + + insert_position( pos ); +} + +/** 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++; + + 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 +*/ +void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDeleted, + 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; + + /* 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 ); + } + + /* 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 ); + } + } 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(); + } + + /* 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; + textD->mCursorToHint = NO_HINT; + } else if ( textD->mCursorPos > pos ) { + if ( textD->mCursorPos < pos + nDeleted ) + textD->mCursorPos = pos; + 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); + if ( textD->mStyleBuffer ) /* See comments in extendRangeForStyleMods */ + 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; + if ( origCursorPos == startDispPos && textD->mCursorPos != startDispPos ) + startDispPos = min( startDispPos, origCursorPos - 1 ); + if ( linesInserted == linesDeleted ) { + if ( nInserted == 0 && nDeleted == 0 ) + endDispPos = pos + nRestyled; + else { + endDispPos = textD->mContinuousWrap ? wrapModEnd : + buf->line_end( pos + nInserted ) + 1; + // CET - FIXME if ( origCursorPos >= startDispPos && + // ( origCursorPos <= endDispPos || endDispPos == buf->length() ) ) + } + + if (linesInserted > 1) textD->draw_line_numbers(false); + } else { + endDispPos = textD->mLastChar + 1; + // 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 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 */ + if ( textD->mStyleBuffer ) + textD->extend_range_for_styles( &startDispPos, &endDispPos ); + + /* Redisplay computed range */ + textD->redisplay_range( startDispPos, endDispPos ); // FIXME utf8 +} + +/** + 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(); +} + +/** + 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; +} + +/** + 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); + } +} + +/** + 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); +} + +/** + 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); +} + +/** + 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( int pos, int *lineNum ) const { + int i; + + *lineNum = 0; + if ( pos < mFirstChar ) return 0; + if ( pos > mLastChar ) { + if ( empty_vlines() ) { + if ( mLastChar < mBuffer->length() ) { + if ( !position_to_line( mLastChar, lineNum ) ) { + Fl::error("Fl_Text_Display_mod::position_to_line(): Consistency check ptvl failed"); + return 0; + } + return ++( *lineNum ) <= mNVisibleLines - 1; + } else { + position_to_line( mLastChar - 1, lineNum ); + return 1; + } + } + return 0; + } + + for ( i = mNVisibleLines - 1; i >= 0; i-- ) { + if ( mLineStarts[ i ] != -1 && pos >= mLineStarts[ i ] ) { + *lineNum = i; + return 1; + } + } + 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"). +*/ +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); + + /* 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); +} + +/** + 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; + + /* 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. */ + + 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(); + 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); + 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(); + 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); + 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 ); + } + if (!(style & BG_ONLY_MASK)) { + fl_color( foreground ); + fl_font( font, fsize ); + fl_draw( string, nChars, X, Y + mMaxsize - fl_descent()); + } + + // 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 */ + /* + 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); + */ +} + + +/** + 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 ) { + /* 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)) { + fl_color(fl_color_average(color(), selection_color(), 0.5f)); + } else { + fl_color( color() ); + } + } else { + fl_color( color() ); + } + fl_rectf( X, Y, width, height ); +} + + +/** + Draw a cursor with top center at X, y. +*/ +void Fl_Text_Display_mod::draw_cursor( int X, int Y ) { + typedef struct { + int x1, y1, x2, y2; + } + Segment; + + Segment segs[ 5 ]; + int left, right, cursorWidth, midY; + // int fontWidth = mFontStruct->min_bounds.width, nSegs = 0; + int fontWidth = TMPFONTWIDTH; // CET - FIXME + 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. */ + 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; + segs[ 0 ].x1 = left; segs[ 0 ].y1 = bot; segs[ 0 ].x2 = X; segs[ 0 ].y2 = midY; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = midY - 1; + segs[ 3 ].x1 = X; segs[ 3 ].y1 = midY - 1; segs[ 3 ].x2 = right; segs[ 3 ].y2 = bot; + nSegs = 4; + } else if ( mCursorStyle == NORMAL_CURSOR ) { + segs[ 0 ].x1 = left; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = right; segs[ 2 ].y2 = bot; + nSegs = 3; + } else if ( mCursorStyle == HEAVY_CURSOR ) { + segs[ 0 ].x1 = X - 1; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X - 1; segs[ 0 ].y2 = bot; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = X + 1; segs[ 2 ].y1 = Y; segs[ 2 ].x2 = X + 1; segs[ 2 ].y2 = bot; + segs[ 3 ].x1 = left; segs[ 3 ].y1 = Y; segs[ 3 ].x2 = right; segs[ 3 ].y2 = Y; + segs[ 4 ].x1 = left; segs[ 4 ].y1 = bot; segs[ 4 ].x2 = right; segs[ 4 ].y2 = bot; + nSegs = 5; + } else if ( mCursorStyle == DIM_CURSOR ) { + midY = Y + fontHeight / 2; + segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X; segs[ 0 ].y2 = Y; + segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = X; segs[ 1 ].y2 = midY; + segs[ 2 ].x1 = X; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot; + nSegs = 3; + } else if ( mCursorStyle == BLOCK_CURSOR ) { + right = X + fontWidth; + segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y; + segs[ 1 ].x1 = right; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot; + segs[ 2 ].x1 = right; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot; + segs[ 3 ].x1 = X; segs[ 3 ].y1 = bot; segs[ 3 ].x2 = X; segs[ 3 ].y2 = 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 { + 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 ); + if (style == mUnfinishedStyle && mUnfinishedHighlightCB) { + /* encountered "unfinished" style, trigger parsing */ + (mUnfinishedHighlightCB)( pos, mHighlightCBArg); + // FIXME: character is ucs-4 + style = (unsigned char) styleBuf->character( pos); + } + } + if (buf->primary_selection()->includes(pos, lineStartPos, dispIndex)) + style |= PRIMARY_MASK; + if (buf->highlight_selection()->includes(pos, lineStartPos, dispIndex)) + style |= HIGHLIGHT_MASK; + if (buf->secondary_selection()->includes(pos, lineStartPos, dispIndex)) + 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 ) { + 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 { + font = textfont(); + fsize = textsize(); + } + fl_font( font, fsize ); + + return ( int ) ( 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; + + /* Find the visible line number corresponding to the Y coordinate */ + fontHeight = mMaxsize; + visLineNum = ( Y - text_area.y ) / fontHeight; + if ( visLineNum < 0 ) + 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; +} + +/** + 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. +*/ +void Fl_Text_Display_mod::xy_to_rowcol( int X, int Y, int *row, + 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; + *column = ( ( X - text_area.x ) + mHorizOffset + + ( 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) +*/ +void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) { + int oldTopLineNum = mTopLineNum; + int oldFirstChar = mFirstChar; + int lineDelta = newTopLineNum - oldTopLineNum; + int nVisLines = mNVisibleLines; + 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) */ + lastLineNum = oldTopLineNum + nVisLines - 1; + if ( newTopLineNum < oldTopLineNum && newTopLineNum < -lineDelta ) { + mFirstChar = skip_lines( 0, newTopLineNum - 1, true ); + } else if ( newTopLineNum < oldTopLineNum ) { + mFirstChar = rewind_lines( mFirstChar, -lineDelta ); + } else if ( newTopLineNum < lastLineNum ) { + mFirstChar = lineStarts[ newTopLineNum - oldTopLineNum ]; + } else if ( newTopLineNum - lastLineNum < mNBufferLines - newTopLineNum ) { + mFirstChar = skip_lines( lineStarts[ nVisLines - 1 ], + 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-- ) + lineStarts[ i ] = lineStarts[ i + lineDelta ]; + calc_line_starts( 0, -lineDelta ); + } else if ( lineDelta > 0 && lineDelta < nVisLines ) { + for ( i = 0; i < nVisLines - lineDelta; i++ ) + lineStarts[ i ] = lineStarts[ i + lineDelta ]; + 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); +} + +/** + 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; + 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 */ + if ( pos + charsDeleted < mFirstChar ) { + mTopLineNum += lineDelta; + for ( i = 0; i < nVisLines && lineStarts[i] != -1; i++ ) + lineStarts[ i ] += charDelta; + mFirstChar += charDelta; + mLastChar += charDelta; + *scrolled = 0; + return; + } + + /* The change began before the beginning of the displayed text, but + 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 ) { + mTopLineNum = max( 1, mTopLineNum + lineDelta ); + mFirstChar = rewind_lines( + lineStarts[ lineOfEnd ] + charDelta, lineOfEnd ); + /* Otherwise anchor on original line number and recount everything */ + } else { + if ( mTopLineNum > mNBufferLines + lineDelta ) { + mTopLineNum = 1; + mFirstChar = 0; + } else + mFirstChar = skip_lines( 0, mTopLineNum - 1, true ); + } + calc_line_starts( 0, nVisLines - 1 ); + /* calculate lastChar by finding the end of the last displayed line */ + calc_last_char(); + *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 */ + if ( pos <= mLastChar ) { + /* find line on which the change began */ + position_to_line( pos, &lineOfPos ); + /* salvage line starts after the changed area */ + if ( lineDelta == 0 ) { + for ( i = lineOfPos + 1; i < nVisLines && lineStarts[ i ] != -1; i++ ) + lineStarts[ i ] += charDelta; + } else if ( lineDelta > 0 ) { + for ( i = nVisLines - 1; i >= lineOfPos + lineDelta + 1; i-- ) + lineStarts[ i ] = lineStarts[ i - lineDelta ] + + ( 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 ); + } + /* fill in the missing line starts */ + if ( linesInserted >= 0 ) + calc_line_starts( lineOfPos + 1, lineOfPos + linesInserted ); + if ( lineDelta < 0 ) + calc_line_starts( nVisLines + lineDelta, nVisLines ); + /* calculate lastChar by finding the end of the last displayed line */ + calc_last_char(); + *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 */ + if ( empty_vlines() ) { + position_to_line( pos, &lineOfPos ); + calc_line_starts( lineOfPos, lineOfPos + linesInserted ); + calc_last_char(); + *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. +*/ +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; + if ( startLine < 0 ) startLine = 0; + 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 */ + 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 */ + 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 */ + if ( line == 0 || ( lineStarts[ line - 1 ] != bufLen && + lineEnd != nextLineStart ) ) { + lineStarts[ line ] = bufLen; + line++; + } + break; + } + 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. +*/ +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.*/ +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) { + /* 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 */ + if (mHorizOffset == horizOffset && mTopLineNum == topLineNum) + return; + + /* If the vertical scroll position has changed, update the line + 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); +} + +/** + Update the minimum, maximum, slider size, page increment, and value + for vertical scroll bar. +*/ +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 */ +#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->linesize(3); +} + +/** + Update the minimum, maximum, slider size, page increment, and value + for the horizontal scroll bar. +*/ +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 +*/ +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); +} + +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. +*/ +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; + } +#endif +} + +static int max( int i1, int i2 ) { + return i1 >= i2 ? i1 : i2; +} + +static int min( int i1, int i2 ) { + return i1 <= i2 ? i1 : i2; +} + +/** + Count the number of newlines in a null-terminated text string; +*/ +static int countlines( const char *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" +*/ +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 ]; + + 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 true if there are lines visible with no corresponding buffer text +*/ +int Fl_Text_Display_mod::empty_vlines() const { + 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 +*/ +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; + 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. +*/ +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); + + + /* + ** 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 (visLineNumcopy(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. + + 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). +*/ +void Fl_Text_Display_mod::measure_deleted_lines(int pos, int nDeleted) { + 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 && + // 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); + return; +} + +/** + 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. +*/ +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()); +} + +/** + 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). +*/ +void Fl_Text_Display_mod::extend_range_for_styles( int *startpos, int *endpos ) { + Fl_Text_Selection * 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. */ + if ( sel->selected() ) { + if ( sel->start() < *startpos ) { + *startpos = sel->start(); + extended = 1; + } + if ( sel->end() > *endpos ) { + *endpos = sel->end(); + 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 ) + *endpos = mBuffer->line_end( *endpos ) + 1; +} + +// The draw() method. It tries to minimize what is draw as much as 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()); + // 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); + // CET - FIXME - save old cursor position instead and just draw side needed? + fl_push_clip(text_area.x-LEFT_MARGIN, + text_area.y, + text_area.w+LEFT_MARGIN+RIGHT_MARGIN, + text_area.h); + fl_rectf(text_area.x-LEFT_MARGIN, mCursorOldY, + LEFT_MARGIN, mMaxsize, color()); + fl_rectf(text_area.x+text_area.w, mCursorOldY, + RIGHT_MARGIN, mMaxsize, color()); + fl_pop_clip(); + } + + // draw the scrollbars + if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_CHILD)) { + mVScrollBar->damage(FL_DAMAGE_ALL); + mHScrollBar->damage(FL_DAMAGE_ALL); + } + update_child(*mVScrollBar); + update_child(*mHScrollBar); + + // draw all of the text + if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_EXPOSE)) { + //printf("drawing all text\n"); + int X, Y, W, H; + if (fl_clip_box(text_area.x, text_area.y, text_area.w, text_area.h, + X, Y, W, H)) { + // Draw text using the intersected clipping box... + // (this sets the clipping internally) + draw_text(X, Y, W, H); + } else { + // Draw the whole area... + draw_text(text_area.x, text_area.y, text_area.w, text_area.h); + } + } + else if (damage() & FL_DAMAGE_SCROLL) { + // draw some lines of text + fl_push_clip(text_area.x, text_area.y, + text_area.w, text_area.h); + //printf("drawing text from %d to %d\n", damage_range1_start, damage_range1_end); + draw_range(damage_range1_start, damage_range1_end); + if (damage_range2_end != -1) { + //printf("drawing text from %d to %d\n", damage_range2_start, damage_range2_end); + draw_range(damage_range2_start, damage_range2_end); + } + damage_range1_start = damage_range1_end = -1; + 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 ) { + 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!"); + //printf("drew cursor at pos: %d (%d,%d)\n", mCursorPos, X, Y); + mCursorOldY = Y; + fl_pop_clip(); + } + fl_pop_clip(); +} + +// this processes drag events due to mouse for Fl_Text_Display and +// also drags due to cursor movement with shift held down for +// Fl_Text_Editor +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) { + d->buffer()->select(d->dragPos, pos); + } else { + d->buffer()->select(pos, d->dragPos); + } + d->insert_position(pos); + } else if (d->dragType == Fl_Text_Display_mod::DRAG_WORD) { + if (pos >= d->dragPos) { + d->insert_position(d->word_end(pos)); + d->buffer()->select(d->word_start(d->dragPos), d->word_end(pos)); + } else { + d->insert_position(d->word_start(pos)); + d->buffer()->select(d->word_start(pos), d->word_end(d->dragPos)); + } + } else if (d->dragType == Fl_Text_Display_mod::DRAG_LINE) { + if (pos >= d->dragPos) { + d->insert_position(d->buffer()->line_end(pos)+1); + d->buffer()->select(d->buffer()->line_start(d->dragPos), + d->buffer()->line_end(pos)+1); + } else { + d->insert_position(d->buffer()->line_start(pos)); + d->buffer()->select(d->buffer()->line_start(pos), + d->buffer()->line_end(d->dragPos)+1); + } + } +} + +// 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; + } + fl_text_drag_me(pos, w); + Fl::repeat_timeout(.1, scroll_timer_cb, user_data); +} + + +int Fl_Text_Display_mod::handle(int event) { + if (!buffer()) return 0; + // This isn't very elegant! + if (!Fl::event_inside(text_area.x, text_area.y, text_area.w, text_area.h) && + !dragging && event != FL_LEAVE && event != FL_ENTER && + event != FL_MOVE && event != FL_FOCUS && event != FL_UNFOCUS && + event != FL_KEYBOARD && event != FL_KEYUP) { + return Fl_Group::handle(event); + } + + switch (event) { + case FL_ENTER: + case FL_MOVE: + if (active_r()) { + 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); + return 1; + } 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(); + 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++; + } + } + } + fl_text_drag_me(pos, this); + return 1; + } + + case FL_RELEASE: { + 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: + if (buffer()->selected()) { + int start, end; + if (buffer()->selection_position(&start, &end)) + redisplay_range(start, end); + } + if (buffer()->secondary_selected()) { + int start, end; + if (buffer()->secondary_selection_position(&start, &end)) + redisplay_range(start, end); + } + if (buffer()->highlight()) { + int start, end; + if (buffer()->highlight_position(&start, &end)) + 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; + } + + // Select all ? + if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='a') { + buffer()->select(0,buffer()->length()); + 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; + if (Fl::visible_focus() && handle(FL_FOCUS)) { + Fl::focus(this); + return 1; + } + break; + + } + + return 0; +} + + +// +// End of "$Id: Fl_Text_Display.cxx 7462 2010-04-06 23:00:56Z matt $". +// diff --git a/src/widgets/Fl_Text_Editor_mod.cxx b/src/widgets/Fl_Text_Editor_mod.cxx index 879202c8..6c786070 100644 --- a/src/widgets/Fl_Text_Editor_mod.cxx +++ b/src/widgets/Fl_Text_Editor_mod.cxx @@ -1,494 +1,7 @@ -// -// "$Id: Fl_Text_Editor.cxx 5671 2007-02-08 07:58:47Z matt $" -// -// Copyright 2001-2006 by Bill Spitzak and others. -// Original code Copyright Mark Edel. Permission to distribute under -// the LGPL for the FLTK library granted by Mark Edel. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library 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 -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -// USA. -// -// Please report all bugs and problems on the following page: -// -// http://www.fltk.org/str.php -// - #include -#include -#include -#include "flstring.h" -#include -#include -#include -#include "Fl_Text_Editor_mod.H" -#include - - -Fl_Text_Editor_mod::Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l) - : Fl_Text_Display_mod(X, Y, W, H, l) { - mCursorOn = 1; - insert_mode_ = 1; - key_bindings = 0; - - // handle the default key bindings - add_default_key_bindings(&key_bindings); - - // handle everything else - default_key_function(kf_default); -} - -Fl_Text_Editor_mod::Key_Binding* Fl_Text_Editor_mod::global_key_bindings = 0; - -// These are the default key bindings every widget should start with -static struct { - int key; - 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_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 }, - { FL_Up, 0, Fl_Text_Editor_mod::kf_move }, - { FL_Right, 0, Fl_Text_Editor_mod::kf_move }, - { FL_Down, 0, Fl_Text_Editor_mod::kf_move }, - { FL_Page_Up, 0, Fl_Text_Editor_mod::kf_move }, - { FL_Page_Down, 0, Fl_Text_Editor_mod::kf_move }, - { FL_Home, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_End, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Left, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Right, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Page_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Page_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, - { FL_Home, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_End, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Left, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Right, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Page_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Page_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, - { FL_Home, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_End, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_Left, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_Right, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_Page_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, - { FL_Page_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, -//{ FL_Clear, 0, Fl_Text_Editor_mod::delete_to_eol }, - { 'z', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, - { '/', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, - { 'x', FL_CTRL, Fl_Text_Editor_mod::kf_cut }, - { FL_Delete, FL_SHIFT, Fl_Text_Editor_mod::kf_cut }, - { 'c', FL_CTRL, Fl_Text_Editor_mod::kf_copy }, - { FL_Insert, FL_CTRL, Fl_Text_Editor_mod::kf_copy }, - { 'v', FL_CTRL, Fl_Text_Editor_mod::kf_paste }, - { FL_Insert, FL_SHIFT, Fl_Text_Editor_mod::kf_paste }, - { 'a', FL_CTRL, Fl_Text_Editor_mod::kf_select_all }, - -#ifdef __APPLE__ - // Define CMD+key accelerators... - { 'z', FL_COMMAND, Fl_Text_Editor_mod::kf_undo }, - { 'x', FL_COMMAND, Fl_Text_Editor_mod::kf_cut }, - { 'c', FL_COMMAND, Fl_Text_Editor_mod::kf_copy }, - { 'v', FL_COMMAND, Fl_Text_Editor_mod::kf_paste }, - { 'a', FL_COMMAND, Fl_Text_Editor_mod::kf_select_all }, -#endif // __APPLE__ - - { 0, 0, 0 } -}; - -void Fl_Text_Editor_mod::add_default_key_bindings(Key_Binding** list) { - for (int i = 0; default_key_bindings[i].key; i++) { - add_key_binding(default_key_bindings[i].key, - default_key_bindings[i].state, - default_key_bindings[i].func, - list); - } -} - -Fl_Text_Editor_mod::Key_Func -Fl_Text_Editor_mod::bound_key_function(int key, int state, Key_Binding* list) { - 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) - break; - if (!cur) return 0; - return cur->function; -} - -void -Fl_Text_Editor_mod::remove_all_key_bindings(Key_Binding** list) { - Key_Binding *cur, *next; - for (cur = *list; cur; cur = next) { - next = cur->next; - delete cur; - } - *list = 0; -} - -void -Fl_Text_Editor_mod::remove_key_binding(int key, int state, Key_Binding** list) { - Key_Binding *cur, *last = 0; - for (cur = *list; cur; last = cur, cur = cur->next) - if (cur->key == key && cur->state == state) break; - if (!cur) return; - if (last) last->next = cur->next; - else *list = cur->next; - delete cur; -} - -void -Fl_Text_Editor_mod::add_key_binding(int key, int state, Key_Func function, - Key_Binding** list) { - Key_Binding* kb = new Key_Binding; - kb->key = key; - kb->state = state; - kb->function = function; - kb->next = *list; - *list = kb; -} - -//////////////////////////////////////////////////////////////// - -#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()); - e->buffer()->remove_selection(); - } -} - -int Fl_Text_Editor_mod::kf_default(int c, Fl_Text_Editor_mod* e) { - if (!c || (!isprint(c) && c != '\t')) return 0; - char s[2] = "\0"; - s[0] = (char)c; - kill_selection(e); - if (e->insert_mode()) e->insert(s); - else e->overstrike(s); - e->show_insert_position(); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return 1; -} - -int Fl_Text_Editor_mod::kf_ignore(int, Fl_Text_Editor_mod*) { - return 0; // don't handle -} - -int Fl_Text_Editor_mod::kf_backspace(int, Fl_Text_Editor_mod* e) { - if (!e->buffer()->selected() && e->move_left()) - e->buffer()->select(e->insert_position(), e->insert_position()+1); - kill_selection(e); - e->show_insert_position(); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return 1; -} - -int Fl_Text_Editor_mod::kf_enter(int, Fl_Text_Editor_mod* e) { - kill_selection(e); - e->insert("\n"); - e->show_insert_position(); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return 1; -} - -extern void fl_text_drag_me(int pos, Fl_Text_Display_mod* d); - -int Fl_Text_Editor_mod::kf_move(int c, Fl_Text_Editor_mod* e) { - int i; - int selected = e->buffer()->selected(); - if (!selected) - e->dragPos = e->insert_position(); - e->buffer()->unselect(); - switch (c) { - case FL_Home: - e->insert_position(e->buffer()->line_start(e->insert_position())); - break; - case FL_End: - e->insert_position(e->buffer()->line_end(e->insert_position())); - break; - case FL_Left: - e->move_left(); - break; - case FL_Right: - e->move_right(); - break; - case FL_Up: - e->move_up(); - break; - case FL_Down: - e->move_down(); - break; - case FL_Page_Up: - for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_up(); - break; - case FL_Page_Down: - for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_down(); - break; - } - e->show_insert_position(); - return 1; -} - -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); - return 1; -} - -int Fl_Text_Editor_mod::kf_ctrl_move(int c, Fl_Text_Editor_mod* e) { - if (!e->buffer()->selected()) - e->dragPos = e->insert_position(); - if (c != FL_Up && c != FL_Down) { - e->buffer()->unselect(); - e->show_insert_position(); - } - switch (c) { - case FL_Home: - e->insert_position(0); - e->scroll(0, 0); - break; - case FL_End: - e->insert_position(e->buffer()->length()); - e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0); - break; - case FL_Left: - e->previous_word(); - break; - case FL_Right: - e->next_word(); - break; - case FL_Up: - e->scroll(e->mTopLineNum-1, e->mHorizOffset); - break; - case FL_Down: - e->scroll(e->mTopLineNum+1, e->mHorizOffset); - break; - case FL_Page_Up: - e->insert_position(e->mLineStarts[0]); - break; - case FL_Page_Down: - e->insert_position(e->mLineStarts[e->mNVisibleLines-2]); - break; - } - return 1; -} - -int Fl_Text_Editor_mod::kf_c_s_move(int c, Fl_Text_Editor_mod* e) { - kf_ctrl_move(c, e); - fl_text_drag_me(e->insert_position(), e); - return 1; -} - -int Fl_Text_Editor_mod::kf_home(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Home, e); -} - -int Fl_Text_Editor_mod::kf_end(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_End, e); -} - -int Fl_Text_Editor_mod::kf_left(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Left, e); -} - -int Fl_Text_Editor_mod::kf_up(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Up, e); -} - -int Fl_Text_Editor_mod::kf_right(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Right, e); -} - -int Fl_Text_Editor_mod::kf_down(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Down, e); -} - -int Fl_Text_Editor_mod::kf_page_up(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Page_Up, e); -} - -int Fl_Text_Editor_mod::kf_page_down(int, Fl_Text_Editor_mod* e) { - return kf_move(FL_Page_Down, e); -} - - -int Fl_Text_Editor_mod::kf_insert(int, Fl_Text_Editor_mod* e) { - e->insert_mode(e->insert_mode() ? 0 : 1); - return 1; -} - -int Fl_Text_Editor_mod::kf_delete(int, Fl_Text_Editor_mod* e) { - if (!e->buffer()->selected()) - e->buffer()->select(e->insert_position(), e->insert_position()+1); - kill_selection(e); - e->show_insert_position(); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return 1; -} - -int Fl_Text_Editor_mod::kf_copy(int, Fl_Text_Editor_mod* e) { - if (!e->buffer()->selected()) return 1; - const char *copy = e->buffer()->selection_text(); - if (*copy) Fl::copy(copy, strlen(copy), 1); - free((void*)copy); - e->show_insert_position(); - return 1; -} - -int Fl_Text_Editor_mod::kf_cut(int c, Fl_Text_Editor_mod* e) { - kf_copy(c, e); - kill_selection(e); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return 1; -} - -int Fl_Text_Editor_mod::kf_paste(int, Fl_Text_Editor_mod* e) { - kill_selection(e); - Fl::paste(*e, 1); - e->show_insert_position(); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return 1; -} - -int Fl_Text_Editor_mod::kf_select_all(int, Fl_Text_Editor_mod* e) { - e->buffer()->select(0, e->buffer()->length()); - return 1; -} - -int Fl_Text_Editor_mod::kf_undo(int , Fl_Text_Editor_mod* e) { - e->buffer()->unselect(); - int crsr; - int ret = e->buffer()->undo(&crsr); - e->insert_position(crsr); - e->show_insert_position(); - e->set_changed(); - if (e->when()&FL_WHEN_CHANGED) e->do_callback(); - return ret; -} - -int Fl_Text_Editor_mod::handle_key() { - // Call FLTK's rules to try to turn this into a printing character. - // This uses the right-hand ctrl key as a "compose prefix" and returns - // the changes that should be made to the text, as a number of - // bytes to delete and a string to insert: - int del; - if (Fl::compose(del)) { - if (del) buffer()->select(insert_position()-del, insert_position()); - kill_selection(this); - if (Fl::event_length()) { - if (insert_mode()) insert(Fl::event_text()); - else overstrike(Fl::event_text()); - } - show_insert_position(); - set_changed(); - if (when()&FL_WHEN_CHANGED) do_callback(); - return 1; - } - - int key = Fl::event_key(), state = Fl::event_state(), c = Fl::event_text()[0]; - state &= FL_SHIFT|FL_CTRL|FL_ALT|FL_META; // only care about these states - Key_Func f; - f = bound_key_function(key, state, global_key_bindings); - if (!f) f = bound_key_function(key, state, key_bindings); - if (f) return f(key, this); - if (default_key_function_ && !state) return default_key_function_(c, this); - return 0; -} - -void Fl_Text_Editor_mod::maybe_do_callback() { -// printf("Fl_Text_Editor_mod::maybe_do_callback()\n"); -// printf("changed()=%d, when()=%x\n", changed(), when()); - if (changed() || (when()&FL_WHEN_NOT_CHANGED)) do_callback(); -} - -int Fl_Text_Editor_mod::handle(int event) { - if (!buffer()) return 0; - - switch (event) { - case FL_FOCUS: - show_cursor(mCursorOn); // redraws the cursor - if (buffer()->selected()) redraw(); // Redraw selections... - Fl::focus(this); - return 1; - - case FL_UNFOCUS: - show_cursor(mCursorOn); // redraws the cursor - if (buffer()->selected()) redraw(); // Redraw selections... - case FL_HIDE: - if (when() & FL_WHEN_RELEASE) maybe_do_callback(); - return 1; - - case FL_KEYBOARD: - if (active_r() && window() && this == Fl::belowmouse()) - window()->cursor(FL_CURSOR_NONE); - return handle_key(); - - case FL_PASTE: - if (!Fl::event_text()) { - fl_beep(); - return 1; - } - buffer()->remove_selection(); - if (insert_mode()) insert(Fl::event_text()); - else overstrike(Fl::event_text()); - show_insert_position(); - set_changed(); - if (when()&FL_WHEN_CHANGED) do_callback(); - return 1; - - case FL_ENTER: -// MRS: WIN32 only? Need to test! -// case FL_MOVE: - show_cursor(mCursorOn); - return 1; - - case FL_PUSH: - if (Fl::event_button() == 2) { - // don't let the text_display see this event - if (Fl_Group::handle(event)) return 1; - dragType = -1; - Fl::paste(*this, 0); - Fl::focus(this); - set_changed(); - if (when()&FL_WHEN_CHANGED) do_callback(); - return 1; - } - break; - } - - return Fl_Text_Display_mod::handle(event); -} - -// -// End of "$Id: Fl_Text_Editor.cxx 5671 2007-02-08 07:58:47Z matt $". -// +#if FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR < 3 +# include "Fl_Text_Editor_mod_1_1.cxx" +#elif FLDIGI_FLTK_API_MAJOR == 1 && FLDIGI_FLTK_API_MINOR == 3 +# include "Fl_Text_Editor_mod_1_3.cxx" +#endif diff --git a/src/widgets/Fl_Text_Editor_mod_1_1.cxx b/src/widgets/Fl_Text_Editor_mod_1_1.cxx new file mode 100644 index 00000000..879202c8 --- /dev/null +++ b/src/widgets/Fl_Text_Editor_mod_1_1.cxx @@ -0,0 +1,494 @@ +// +// "$Id: Fl_Text_Editor.cxx 5671 2007-02-08 07:58:47Z matt $" +// +// Copyright 2001-2006 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#include + +#include +#include +#include "flstring.h" +#include +#include +#include +#include "Fl_Text_Editor_mod.H" +#include + + +Fl_Text_Editor_mod::Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l) + : Fl_Text_Display_mod(X, Y, W, H, l) { + mCursorOn = 1; + insert_mode_ = 1; + key_bindings = 0; + + // handle the default key bindings + add_default_key_bindings(&key_bindings); + + // handle everything else + default_key_function(kf_default); +} + +Fl_Text_Editor_mod::Key_Binding* Fl_Text_Editor_mod::global_key_bindings = 0; + +// These are the default key bindings every widget should start with +static struct { + int key; + 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_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 }, + { FL_Up, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Right, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Down, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Page_Up, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Page_Down, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Home, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_End, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Left, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Right, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Page_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Page_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Home, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_End, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Left, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Right, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Page_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Page_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Home, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_End, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Left, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Right, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Page_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Page_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, +//{ FL_Clear, 0, Fl_Text_Editor_mod::delete_to_eol }, + { 'z', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, + { '/', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, + { 'x', FL_CTRL, Fl_Text_Editor_mod::kf_cut }, + { FL_Delete, FL_SHIFT, Fl_Text_Editor_mod::kf_cut }, + { 'c', FL_CTRL, Fl_Text_Editor_mod::kf_copy }, + { FL_Insert, FL_CTRL, Fl_Text_Editor_mod::kf_copy }, + { 'v', FL_CTRL, Fl_Text_Editor_mod::kf_paste }, + { FL_Insert, FL_SHIFT, Fl_Text_Editor_mod::kf_paste }, + { 'a', FL_CTRL, Fl_Text_Editor_mod::kf_select_all }, + +#ifdef __APPLE__ + // Define CMD+key accelerators... + { 'z', FL_COMMAND, Fl_Text_Editor_mod::kf_undo }, + { 'x', FL_COMMAND, Fl_Text_Editor_mod::kf_cut }, + { 'c', FL_COMMAND, Fl_Text_Editor_mod::kf_copy }, + { 'v', FL_COMMAND, Fl_Text_Editor_mod::kf_paste }, + { 'a', FL_COMMAND, Fl_Text_Editor_mod::kf_select_all }, +#endif // __APPLE__ + + { 0, 0, 0 } +}; + +void Fl_Text_Editor_mod::add_default_key_bindings(Key_Binding** list) { + for (int i = 0; default_key_bindings[i].key; i++) { + add_key_binding(default_key_bindings[i].key, + default_key_bindings[i].state, + default_key_bindings[i].func, + list); + } +} + +Fl_Text_Editor_mod::Key_Func +Fl_Text_Editor_mod::bound_key_function(int key, int state, Key_Binding* list) { + 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) + break; + if (!cur) return 0; + return cur->function; +} + +void +Fl_Text_Editor_mod::remove_all_key_bindings(Key_Binding** list) { + Key_Binding *cur, *next; + for (cur = *list; cur; cur = next) { + next = cur->next; + delete cur; + } + *list = 0; +} + +void +Fl_Text_Editor_mod::remove_key_binding(int key, int state, Key_Binding** list) { + Key_Binding *cur, *last = 0; + for (cur = *list; cur; last = cur, cur = cur->next) + if (cur->key == key && cur->state == state) break; + if (!cur) return; + if (last) last->next = cur->next; + else *list = cur->next; + delete cur; +} + +void +Fl_Text_Editor_mod::add_key_binding(int key, int state, Key_Func function, + Key_Binding** list) { + Key_Binding* kb = new Key_Binding; + kb->key = key; + kb->state = state; + kb->function = function; + kb->next = *list; + *list = kb; +} + +//////////////////////////////////////////////////////////////// + +#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()); + e->buffer()->remove_selection(); + } +} + +int Fl_Text_Editor_mod::kf_default(int c, Fl_Text_Editor_mod* e) { + if (!c || (!isprint(c) && c != '\t')) return 0; + char s[2] = "\0"; + s[0] = (char)c; + kill_selection(e); + if (e->insert_mode()) e->insert(s); + else e->overstrike(s); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +int Fl_Text_Editor_mod::kf_ignore(int, Fl_Text_Editor_mod*) { + return 0; // don't handle +} + +int Fl_Text_Editor_mod::kf_backspace(int, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected() && e->move_left()) + e->buffer()->select(e->insert_position(), e->insert_position()+1); + kill_selection(e); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +int Fl_Text_Editor_mod::kf_enter(int, Fl_Text_Editor_mod* e) { + kill_selection(e); + e->insert("\n"); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +extern void fl_text_drag_me(int pos, Fl_Text_Display_mod* d); + +int Fl_Text_Editor_mod::kf_move(int c, Fl_Text_Editor_mod* e) { + int i; + int selected = e->buffer()->selected(); + if (!selected) + e->dragPos = e->insert_position(); + e->buffer()->unselect(); + switch (c) { + case FL_Home: + e->insert_position(e->buffer()->line_start(e->insert_position())); + break; + case FL_End: + e->insert_position(e->buffer()->line_end(e->insert_position())); + break; + case FL_Left: + e->move_left(); + break; + case FL_Right: + e->move_right(); + break; + case FL_Up: + e->move_up(); + break; + case FL_Down: + e->move_down(); + break; + case FL_Page_Up: + for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_up(); + break; + case FL_Page_Down: + for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_down(); + break; + } + e->show_insert_position(); + return 1; +} + +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); + return 1; +} + +int Fl_Text_Editor_mod::kf_ctrl_move(int c, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected()) + e->dragPos = e->insert_position(); + if (c != FL_Up && c != FL_Down) { + e->buffer()->unselect(); + e->show_insert_position(); + } + switch (c) { + case FL_Home: + e->insert_position(0); + e->scroll(0, 0); + break; + case FL_End: + e->insert_position(e->buffer()->length()); + e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0); + break; + case FL_Left: + e->previous_word(); + break; + case FL_Right: + e->next_word(); + break; + case FL_Up: + e->scroll(e->mTopLineNum-1, e->mHorizOffset); + break; + case FL_Down: + e->scroll(e->mTopLineNum+1, e->mHorizOffset); + break; + case FL_Page_Up: + e->insert_position(e->mLineStarts[0]); + break; + case FL_Page_Down: + e->insert_position(e->mLineStarts[e->mNVisibleLines-2]); + break; + } + return 1; +} + +int Fl_Text_Editor_mod::kf_c_s_move(int c, Fl_Text_Editor_mod* e) { + kf_ctrl_move(c, e); + fl_text_drag_me(e->insert_position(), e); + return 1; +} + +int Fl_Text_Editor_mod::kf_home(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Home, e); +} + +int Fl_Text_Editor_mod::kf_end(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_End, e); +} + +int Fl_Text_Editor_mod::kf_left(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Left, e); +} + +int Fl_Text_Editor_mod::kf_up(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Up, e); +} + +int Fl_Text_Editor_mod::kf_right(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Right, e); +} + +int Fl_Text_Editor_mod::kf_down(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Down, e); +} + +int Fl_Text_Editor_mod::kf_page_up(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Page_Up, e); +} + +int Fl_Text_Editor_mod::kf_page_down(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Page_Down, e); +} + + +int Fl_Text_Editor_mod::kf_insert(int, Fl_Text_Editor_mod* e) { + e->insert_mode(e->insert_mode() ? 0 : 1); + return 1; +} + +int Fl_Text_Editor_mod::kf_delete(int, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected()) + e->buffer()->select(e->insert_position(), e->insert_position()+1); + kill_selection(e); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +int Fl_Text_Editor_mod::kf_copy(int, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected()) return 1; + const char *copy = e->buffer()->selection_text(); + if (*copy) Fl::copy(copy, strlen(copy), 1); + free((void*)copy); + e->show_insert_position(); + return 1; +} + +int Fl_Text_Editor_mod::kf_cut(int c, Fl_Text_Editor_mod* e) { + kf_copy(c, e); + kill_selection(e); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +int Fl_Text_Editor_mod::kf_paste(int, Fl_Text_Editor_mod* e) { + kill_selection(e); + Fl::paste(*e, 1); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +int Fl_Text_Editor_mod::kf_select_all(int, Fl_Text_Editor_mod* e) { + e->buffer()->select(0, e->buffer()->length()); + return 1; +} + +int Fl_Text_Editor_mod::kf_undo(int , Fl_Text_Editor_mod* e) { + e->buffer()->unselect(); + int crsr; + int ret = e->buffer()->undo(&crsr); + e->insert_position(crsr); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return ret; +} + +int Fl_Text_Editor_mod::handle_key() { + // Call FLTK's rules to try to turn this into a printing character. + // This uses the right-hand ctrl key as a "compose prefix" and returns + // the changes that should be made to the text, as a number of + // bytes to delete and a string to insert: + int del; + if (Fl::compose(del)) { + if (del) buffer()->select(insert_position()-del, insert_position()); + kill_selection(this); + if (Fl::event_length()) { + if (insert_mode()) insert(Fl::event_text()); + else overstrike(Fl::event_text()); + } + show_insert_position(); + set_changed(); + if (when()&FL_WHEN_CHANGED) do_callback(); + return 1; + } + + int key = Fl::event_key(), state = Fl::event_state(), c = Fl::event_text()[0]; + state &= FL_SHIFT|FL_CTRL|FL_ALT|FL_META; // only care about these states + Key_Func f; + f = bound_key_function(key, state, global_key_bindings); + if (!f) f = bound_key_function(key, state, key_bindings); + if (f) return f(key, this); + if (default_key_function_ && !state) return default_key_function_(c, this); + return 0; +} + +void Fl_Text_Editor_mod::maybe_do_callback() { +// printf("Fl_Text_Editor_mod::maybe_do_callback()\n"); +// printf("changed()=%d, when()=%x\n", changed(), when()); + if (changed() || (when()&FL_WHEN_NOT_CHANGED)) do_callback(); +} + +int Fl_Text_Editor_mod::handle(int event) { + if (!buffer()) return 0; + + switch (event) { + case FL_FOCUS: + show_cursor(mCursorOn); // redraws the cursor + if (buffer()->selected()) redraw(); // Redraw selections... + Fl::focus(this); + return 1; + + case FL_UNFOCUS: + show_cursor(mCursorOn); // redraws the cursor + if (buffer()->selected()) redraw(); // Redraw selections... + case FL_HIDE: + if (when() & FL_WHEN_RELEASE) maybe_do_callback(); + return 1; + + case FL_KEYBOARD: + if (active_r() && window() && this == Fl::belowmouse()) + window()->cursor(FL_CURSOR_NONE); + return handle_key(); + + case FL_PASTE: + if (!Fl::event_text()) { + fl_beep(); + return 1; + } + buffer()->remove_selection(); + if (insert_mode()) insert(Fl::event_text()); + else overstrike(Fl::event_text()); + show_insert_position(); + set_changed(); + if (when()&FL_WHEN_CHANGED) do_callback(); + return 1; + + case FL_ENTER: +// MRS: WIN32 only? Need to test! +// case FL_MOVE: + show_cursor(mCursorOn); + return 1; + + case FL_PUSH: + if (Fl::event_button() == 2) { + // don't let the text_display see this event + if (Fl_Group::handle(event)) return 1; + dragType = -1; + Fl::paste(*this, 0); + Fl::focus(this); + set_changed(); + if (when()&FL_WHEN_CHANGED) do_callback(); + return 1; + } + break; + } + + return Fl_Text_Display_mod::handle(event); +} + +// +// End of "$Id: Fl_Text_Editor.cxx 5671 2007-02-08 07:58:47Z matt $". +// diff --git a/src/widgets/Fl_Text_Editor_mod_1_3.cxx b/src/widgets/Fl_Text_Editor_mod_1_3.cxx new file mode 100644 index 00000000..fe21dc0c --- /dev/null +++ b/src/widgets/Fl_Text_Editor_mod_1_3.cxx @@ -0,0 +1,620 @@ +// +// "$Id: Fl_Text_Editor.cxx 7462 2010-04-06 23:00:56Z matt $" +// +// Copyright 2001-2009 by Bill Spitzak and others. +// Original code Copyright Mark Edel. Permission to distribute under +// the LGPL for the FLTK library granted by Mark Edel. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library 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 +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +// USA. +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +#include +#include +#include "flstring.h" +#include +#include +#include +#include "Fl_Text_Editor_mod.H" +#include + + +/* Keyboard Control Matrix + +key\modifier plain Ctrl Alt Meta + left 1/1 13/9 0/13 0/9 + right 2/2 14/10 0/14 0/10 + up 3/19 21/7 0/15 0/17 + down 4/20 22/8 0/16 0/18 + home 9/5 17/0 0/0 0/0 + end 10/6 18/0 0/0 0/0 + page up 11/7 23/0 0/11 0/0 + page down 12/8 24/0 0/12 0/0 + (FLTK action / OS X action) + (adding the shift key extends the selection, all other combinations are no-op) + + 0: no-op + 1: move cursor to the left, at line beginning wrap to end of prev line, at doc start no-op + 2: move cursor to the right, at line end move to beginning of the next line, at doc end no-op + 3: move cursor up, at doc top no-op + 4: move cursor down, at doc bottom no-op + 5: scroll display to top of text (cursor unchanged) + 6: scroll display to end of text (cursor unchanged) + 7: scroll text down one page (cursor unchanged) + 8: scroll text up one page (cursor unchanged) + 9: move cursor to beginning of line + 10: move cursor to end of line + 11: move cursor up one page and scroll down + 12: move cursor down one page and scroll up + 13: move to the beginning of the word or the previous word + 14: move to the end of the word or the next word + 15: if start of line: start of prev line, else start of this line + 16: if end of line: end of next line, else end of this line + 17: move cursor to the beginning of the document + 18: move cursor to the end of the document + 19: move cursor up, at doc top: home, at doc start: no-op) + 20: move cursor down, at doc bot: end, at doc end: no-op) + 21: scroll text down one line (cursor unchanged) + 22: scroll text up one line (cursor unchanged) + 23: move cursor to the beginning of the top of the screen + 24: move cursor to the beginning of the bottom of the window +*/ + +/** The constructor creates a new text editor widget.*/ +Fl_Text_Editor_mod::Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l) + : Fl_Text_Display_mod(X, Y, W, H, l) { + mCursorOn = 1; + insert_mode_ = 1; + key_bindings = 0; + + // handle the default key bindings + add_default_key_bindings(&key_bindings); + + // handle everything else + default_key_function(kf_default); +} + +#ifndef FL_DOXYGEN +Fl_Text_Editor_mod::Key_Binding* Fl_Text_Editor_mod::global_key_bindings = 0; +#endif + +// These are the default key bindings every widget should start with +static struct { + int key; + 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_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 }, + { FL_Up, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Right, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Down, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Page_Up, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Page_Down, 0, Fl_Text_Editor_mod::kf_move }, + { FL_Home, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_End, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Left, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Right, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Page_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Page_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, + { FL_Home, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_End, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Left, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Right, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Page_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Page_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, + { FL_Home, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_End, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Left, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Right, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Page_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, + { FL_Page_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, +//{ FL_Clear, 0, Fl_Text_Editor_mod::delete_to_eol }, + { 'z', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, + { '/', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, + { 'x', FL_CTRL, Fl_Text_Editor_mod::kf_cut }, + { FL_Delete, FL_SHIFT, Fl_Text_Editor_mod::kf_cut }, + { 'c', FL_CTRL, Fl_Text_Editor_mod::kf_copy }, + { FL_Insert, FL_CTRL, Fl_Text_Editor_mod::kf_copy }, + { 'v', FL_CTRL, Fl_Text_Editor_mod::kf_paste }, + { FL_Insert, FL_SHIFT, Fl_Text_Editor_mod::kf_paste }, + { 'a', FL_CTRL, Fl_Text_Editor_mod::kf_select_all }, + +#ifdef __APPLE__ + // Define CMD+key accelerators... + { 'z', FL_COMMAND, Fl_Text_Editor_mod::kf_undo }, + { 'x', FL_COMMAND, Fl_Text_Editor_mod::kf_cut }, + { 'c', FL_COMMAND, Fl_Text_Editor_mod::kf_copy }, + { 'v', FL_COMMAND, Fl_Text_Editor_mod::kf_paste }, + { 'a', FL_COMMAND, Fl_Text_Editor_mod::kf_select_all }, + { FL_Left, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, + { FL_Right, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, + { FL_Up, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, + { FL_Down, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, + { FL_Left, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, + { FL_Right, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, + { FL_Up, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, + { FL_Down, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, +#endif // __APPLE__ + + { 0, 0, 0 } +}; + +/** Adds all of the default editor key bindings to the specified key binding list.*/ +void Fl_Text_Editor_mod::add_default_key_bindings(Key_Binding** list) { + for (int i = 0; default_key_bindings[i].key; i++) { + add_key_binding(default_key_bindings[i].key, + default_key_bindings[i].state, + default_key_bindings[i].func, + list); + } +} + +/** Returns the function associated with a key binding.*/ +Fl_Text_Editor_mod::Key_Func Fl_Text_Editor_mod::bound_key_function(int key, int state, Key_Binding* list) { + 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) + break; + if (!cur) return 0; + return cur->function; +} + +/** Removes all of the key bindings associated with the text editor or list.*/ +void Fl_Text_Editor_mod::remove_all_key_bindings(Key_Binding** list) { + Key_Binding *cur, *next; + for (cur = *list; cur; cur = next) { + next = cur->next; + delete cur; + } + *list = 0; +} + +/** Removes the key binding associated with the key "key" of state "state" */ +void Fl_Text_Editor_mod::remove_key_binding(int key, int state, Key_Binding** list) { + Key_Binding *cur, *last = 0; + for (cur = *list; cur; last = cur, cur = cur->next) + if (cur->key == key && cur->state == state) break; + if (!cur) return; + if (last) last->next = cur->next; + else *list = cur->next; + delete cur; +} +/** Adds a key of state "state" with the function "function" */ +void Fl_Text_Editor_mod::add_key_binding(int key, int state, Key_Func function, + Key_Binding** list) { + Key_Binding* kb = new Key_Binding; + kb->key = key; + kb->state = state; + kb->function = function; + kb->next = *list; + *list = kb; +} + +//////////////////////////////////////////////////////////////// + +#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()); + e->buffer()->remove_selection(); + } +} + +/** Inserts the text associated with the key */ +int Fl_Text_Editor_mod::kf_default(int c, Fl_Text_Editor_mod* e) { + if (!c || (!isprint(c) && c != '\t')) return 0; + char s[2] = "\0"; + s[0] = (char)c; + kill_selection(e); + if (e->insert_mode()) e->insert(s); + else e->overstrike(s); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +/** Ignores the keypress */ +int Fl_Text_Editor_mod::kf_ignore(int, Fl_Text_Editor_mod*) { + return 0; // don't handle +} +/** 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); + } + kill_selection(e); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +/** Inserts a newline at the current cursor position */ +int Fl_Text_Editor_mod::kf_enter(int, Fl_Text_Editor_mod* e) { + kill_selection(e); + e->insert("\n"); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +extern void fl_text_drag_me(int pos, Fl_Text_Display_mod* d); +/** Moves the text cursor in the direction indicated by key c.*/ +int Fl_Text_Editor_mod::kf_move(int c, Fl_Text_Editor_mod* e) { + int i; + int selected = e->buffer()->selected(); + if (!selected) + e->dragPos = e->insert_position(); + e->buffer()->unselect(); + switch (c) { + case FL_Home: + e->insert_position(e->buffer()->line_start(e->insert_position())); + break; + case FL_End: + e->insert_position(e->buffer()->line_end(e->insert_position())); + break; + case FL_Left: + e->move_left(); + break; + case FL_Right: + e->move_right(); + break; + case FL_Up: + e->move_up(); + break; + case FL_Down: + e->move_down(); + break; + case FL_Page_Up: + for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_up(); + break; + case FL_Page_Down: + for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_down(); + break; + } + e->show_insert_position(); + return 1; +} + +/** Extends the current selection in the direction of key c.*/ +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); + return 1; +} +/** Moves the current text cursor in the direction indicated by control key */ +int Fl_Text_Editor_mod::kf_ctrl_move(int c, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected()) + e->dragPos = e->insert_position(); + if (c != FL_Up && c != FL_Down) { + e->buffer()->unselect(); + e->show_insert_position(); + } + switch (c) { + case FL_Home: + e->insert_position(0); + e->scroll(0, 0); + break; + case FL_End: + e->insert_position(e->buffer()->length()); + e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0); + break; + case FL_Left: + e->previous_word(); + break; + case FL_Right: + e->next_word(); + break; + case FL_Up: + e->scroll(e->mTopLineNum-1, e->mHorizOffset); + break; + case FL_Down: + e->scroll(e->mTopLineNum+1, e->mHorizOffset); + break; + case FL_Page_Up: + e->insert_position(e->mLineStarts[0]); + break; + case FL_Page_Down: + e->insert_position(e->mLineStarts[e->mNVisibleLines-2]); + break; + } + return 1; +} + +/** Moves the current text cursor in the direction indicated by meta key */ +int Fl_Text_Editor_mod::kf_meta_move(int c, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected()) + e->dragPos = e->insert_position(); + if (c != FL_Up && c != FL_Down) { + e->buffer()->unselect(); + e->show_insert_position(); + } + switch (c) { + case FL_Up: // top of buffer + e->insert_position(0); + e->scroll(0, 0); + break; + case FL_Down: // end of buffer + e->insert_position(e->buffer()->length()); + e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0); + break; + case FL_Left: // beginning of line + kf_move(FL_Home, e); + break; + case FL_Right: // end of line + kf_move(FL_End, e); + break; + } + return 1; +} + +/** Extends the current selection in the direction indicated by meta key c. */ +int Fl_Text_Editor_mod::kf_m_s_move(int c, Fl_Text_Editor_mod* e) { + kf_meta_move(c, e); + fl_text_drag_me(e->insert_position(), e); + return 1; +} + +/** Extends the current selection in the direction indicated by control key c. */ +int Fl_Text_Editor_mod::kf_c_s_move(int c, Fl_Text_Editor_mod* e) { + kf_ctrl_move(c, e); + fl_text_drag_me(e->insert_position(), e); + return 1; +} + +/** Moves the text cursor to the beginning of the current line.*/ +int Fl_Text_Editor_mod::kf_home(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Home, e); +} + +/** Moves the text cursor to the end of the current line.*/ +int Fl_Text_Editor_mod::kf_end(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_End, e); +} + +/** Moves the text cursor one character to the left.*/ +int Fl_Text_Editor_mod::kf_left(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Left, e); +} + +/** Moves the text cursor one line up.*/ +int Fl_Text_Editor_mod::kf_up(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Up, e); +} + +/** Moves the text cursor one character to the right.*/ +int Fl_Text_Editor_mod::kf_right(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Right, e); +} +/** Moves the text cursor one line down.*/ +int Fl_Text_Editor_mod::kf_down(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Down, e); +} + +/** Moves the text cursor up one page.*/ +int Fl_Text_Editor_mod::kf_page_up(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Page_Up, e); +} + +/** Moves the text cursor down one page.*/ +int Fl_Text_Editor_mod::kf_page_down(int, Fl_Text_Editor_mod* e) { + return kf_move(FL_Page_Down, e); +} +/** Toggles the insert mode in the text editor.*/ +int Fl_Text_Editor_mod::kf_insert(int, Fl_Text_Editor_mod* e) { + e->insert_mode(e->insert_mode() ? 0 : 1); + return 1; +} + +/** 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); + } + + kill_selection(e); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +/** Does a copy of selected text or the current character in the current buffer.*/ +int Fl_Text_Editor_mod::kf_copy(int, Fl_Text_Editor_mod* e) { + if (!e->buffer()->selected()) return 1; + const char *copy = e->buffer()->selection_text(); + if (*copy) Fl::copy(copy, strlen(copy), 1); + free((void*)copy); + e->show_insert_position(); + return 1; +} + +/** Does a cut of selected text in the current buffer.*/ +int Fl_Text_Editor_mod::kf_cut(int c, Fl_Text_Editor_mod* e) { + kf_copy(c, e); + kill_selection(e); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +/** Does a paste of selected text in the current buffer.*/ +int Fl_Text_Editor_mod::kf_paste(int, Fl_Text_Editor_mod* e) { + kill_selection(e); + Fl::paste(*e, 1); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return 1; +} + +/** 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()); + 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(); + int crsr; + int ret = e->buffer()->undo(&crsr); + e->insert_position(crsr); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + return ret; +} + +/** Handles a key press in the editor */ +int Fl_Text_Editor_mod::handle_key() { + // Call FLTK's rules to try to turn this into a printing character. + // This uses the right-hand ctrl key as a "compose prefix" and returns + // the changes that should be made to the text, as a number of + // bytes to delete and a string to insert: + int del = 0; + if (Fl::compose(del)) { + if (del) buffer()->select(insert_position()-del, insert_position()); + kill_selection(this); + if (Fl::event_length()) { + if (insert_mode()) insert(Fl::event_text()); + else overstrike(Fl::event_text()); + } + show_insert_position(); + set_changed(); + if (when()&FL_WHEN_CHANGED) do_callback(); + return 1; + } + + int key = Fl::event_key(), state = Fl::event_state(), c = Fl::event_text()[0]; + state &= FL_SHIFT|FL_CTRL|FL_ALT|FL_META; // only care about these states + Key_Func f; + f = bound_key_function(key, state, global_key_bindings); + if (!f) f = bound_key_function(key, state, key_bindings); + if (f) return f(key, this); + if (default_key_function_ && !state) return default_key_function_(c, this); + return 0; +} + +/** does or does not a callback according to changed() and when() settings */ +void Fl_Text_Editor_mod::maybe_do_callback() { +// printf("Fl_Text_Editor_mod::maybe_do_callback()\n"); +// printf("changed()=%d, when()=%x\n", changed(), when()); + if (changed() || (when()&FL_WHEN_NOT_CHANGED)) do_callback(); +} + +int Fl_Text_Editor_mod::handle(int event) { + if (!buffer()) return 0; + + switch (event) { + case FL_FOCUS: + show_cursor(mCursorOn); // redraws the cursor + if (buffer()->selected()) redraw(); // Redraw selections... + Fl::focus(this); + return 1; + + case FL_UNFOCUS: + show_cursor(mCursorOn); // redraws the cursor + if (buffer()->selected()) redraw(); // Redraw selections... + case FL_HIDE: + if (when() & FL_WHEN_RELEASE) maybe_do_callback(); + return 1; + + case FL_KEYBOARD: + if (active_r() && window() && this == Fl::belowmouse()) + window()->cursor(FL_CURSOR_NONE); + return handle_key(); + + case FL_PASTE: + if (!Fl::event_text()) { + fl_beep(); + return 1; + } + buffer()->remove_selection(); + if (insert_mode()) insert(Fl::event_text()); + else overstrike(Fl::event_text()); + show_insert_position(); + set_changed(); + if (when()&FL_WHEN_CHANGED) do_callback(); + return 1; + + case FL_ENTER: +// MRS: WIN32 only? Need to test! +// case FL_MOVE: + show_cursor(mCursorOn); + return 1; + + case FL_PUSH: + if (Fl::event_button() == 2) { + // don't let the text_display see this event + if (Fl_Group::handle(event)) return 1; + dragType = -1; + Fl::paste(*this, 0); + Fl::focus(this); + set_changed(); + if (when()&FL_WHEN_CHANGED) do_callback(); + return 1; + } + break; + + case FL_SHORTCUT: + if (!(shortcut() ? Fl::test_shortcut(shortcut()) : test_shortcut())) + return 0; + if (Fl::visible_focus() && handle(FL_FOCUS)) { + Fl::focus(this); + return 1; + } + break; + } + + return Fl_Text_Display_mod::handle(event); +} + +// +// End of "$Id: Fl_Text_Editor.cxx 7462 2010-04-06 23:00:56Z matt $". +// diff --git a/src/widgets/flslider2.cxx b/src/widgets/flslider2.cxx index dd75668a..949952fc 100644 --- a/src/widgets/flslider2.cxx +++ b/src/widgets/flslider2.cxx @@ -36,7 +36,7 @@ inline static int handle_scroll(Fl_Valuator* w, int event) if (!dynamic_cast(w) && !dynamic_cast(w) && !(w->type() & FL_HOR_SLIDER)) d = -d; - w->value(w->clamp(w->increment(w->value(), -d))); + w->value(w->clamp(w->increment(w->value(), static_cast(-d)))); w->do_callback(); } return 1; diff --git a/src/wwv/analysis.cxx b/src/wwv/analysis.cxx index 6d66a0d9..78266e27 100644 --- a/src/wwv/analysis.cxx +++ b/src/wwv/analysis.cxx @@ -22,10 +22,9 @@ #include -#include -#include #include #include +#include #include "analysis.h" #include "modem.h" @@ -37,6 +36,9 @@ #include "main.h" #include "fl_digi.h" +#include "timeops.h" +#include "debug.h" + using namespace std; #define anal_BW 4 @@ -70,24 +72,49 @@ anal::~anal() void anal::restart() { - double fhi; - double flo; - - symbollen = analMaxSymLen; - set_bandwidth(anal_BW); - - fhi = anal_BW * 1.1 / samplerate; - flo = 0.0; - if (bpfilt) + double fhi = anal_BW * 1.1 / samplerate; + double flo = 0.0; + if (bpfilt) bpfilt->create_filter(flo, fhi); else bpfilt = new fftfilt(flo, fhi, 2048); + symbollen = analMaxSymLen; + set_bandwidth(anal_BW); + ffilt->setLength(4000); // average over last 1/2 second of samples favg->setLength(120); // average over last minute of samples - + sum = 0.0; fout_1 = fout_2 = 0.0; + restart_count = 64; + wf_freq = frequency; + + if (clock_gettime(CLOCK_REALTIME, &start_time) == -1) { + LOG_PERROR("clock_gettime"); + abort(); + } + struct tm tm; + gmtime_r(&start_time.tv_sec, &tm); + + FILE* out = fopen(analysisFilename.c_str(), "a"); + if (unlikely(!out)) { + LOG_PERROR("fopen"); + goto ret; + } + fprintf(out, "Time,Clock,Track,Freq\n" + ",,,,rf track frequency: %" PRIdMAX " %s\n" + ",,,,audio track frequency: %.0f\n" + ",,,,%02d-%02d-%02d\n", + (intmax_t)wf->rfcarrier(), (wf->USB() ? "USB" : "LSB"), + frequency, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); + fclose(out); + +ret: + if (clock_gettime(CLOCK_MONOTONIC, &start_time) == -1) { + LOG_PERROR("clock_gettime"); + abort(); + } } anal::anal() @@ -104,8 +131,9 @@ anal::anal() analysisFilename = HomeDir; analysisFilename.append("freqanalysis.csv"); - + pipeptr = 0; + cap &= ~CAP_TX; restart(); } @@ -132,9 +160,31 @@ complex anal::mixer(complex in) void anal::writeFile() { - analysisFile.open(analysisFilename.c_str(), ios::app); - analysisFile << wf->rfcarrier() << ", " << frequency << ", " << fout_2 << endl; - analysisFile.close(); + struct timespec elapsed, now; + // calculate elapsed time using the monotonic clock + if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) { + LOG_PERROR("clock_gettime"); + abort(); + } + elapsed = now - start_time; + // calculate wall clock time using the realtime clock + if (clock_gettime(CLOCK_REALTIME, &now) == -1) { + LOG_PERROR("clock_gettime"); + abort(); + } + struct tm tm; + gmtime_r(&now.tv_sec, &tm); + + FILE* out = fopen(analysisFilename.c_str(), "a"); + if (unlikely(!out)) { + LOG_PERROR("fopen"); + return; + } + fprintf(out, "%02d:%02d:%02d, %" PRIdMAX ".%03" PRIdMAX ", %f, %12.4f\n", + tm.tm_hour, tm.tm_min, tm.tm_sec, (intmax_t)elapsed.tv_sec, + (intmax_t)(elapsed.tv_nsec / 1000000), fout_2, + (wf->rfcarrier() + (wf->USB() ? 1.0 : -1.0) * (frequency + fout_2))); + fclose(out); } int anal::rx_process(const double *buf, int len) @@ -144,6 +194,8 @@ int anal::rx_process(const double *buf, int len) int n; static int dspcnt = symbollen; + if (wf_freq != frequency) restart(); + while (len-- > 0) { // create analytic signal from sound card input samples z.re = z.im = *buf++; @@ -152,32 +204,36 @@ int anal::rx_process(const double *buf, int len) z = mixer(z); // low pass filter using Windowed Sinc - Overlap-Add convolution filter n = bpfilt->run(z, &zp); - for (int i = 0; i < n; i++) { + + if (n) for (int i = 0; i < n; i++) { // measure phase difference between successive samples to determine // the frequency of the baseband signal (+anal_baud or -anal_baud) // see class complex definiton for operator % fin = (prevsmpl % zp[i]).arg() * samplerate / TWOPI; prevsmpl = zp[i]; + if (restart_count) restart_count--; + else { // filter using moving average filter - fout_1 = ffilt->run(fin); + fout_1 = ffilt->run(fin); // the values in the pipe are +/- 2 Hz ==> +/- 1.0 - pipe[pipeptr] = fout_1 / 4; - pipeptr = (pipeptr + 1) % symbollen; - dspcnt--; - if (dspcnt == 0) { - set_scope(pipe, symbollen, false); - pipe.next(); // change buffers + pipe[pipeptr] = fout_1 / 4; + pipeptr = (pipeptr + 1) % symbollen; + dspcnt--; + if (dspcnt == 0) { + set_scope(pipe, symbollen, false); + pipe.next(); // change buffers // filter using second moving average filter & display the result - fout_2 = favg->run(fout_1); - if (wf->USB()) - snprintf(msg1, sizeof(msg1), "Freq: %12.2f", wf->rfcarrier() + frequency + fout_2 ); - else - snprintf(msg1, sizeof(msg1), "Freq: %12.2f", wf->rfcarrier() - frequency - fout_2 ); - put_status(msg1); - writeFile(); + fout_2 = favg->run(fout_1); + if (wf->USB()) + snprintf(msg1, sizeof(msg1), "Freq: %12.2f", wf->rfcarrier() + frequency + fout_2 ); + else + snprintf(msg1, sizeof(msg1), "Freq: %12.2f", wf->rfcarrier() - frequency - fout_2 ); + put_status(msg1); + writeFile(); // reset the display counter & the pipe pointer - dspcnt = symbollen; - pipeptr = 0; + dspcnt = symbollen; + pipeptr = 0; + } } } } diff --git a/src/wwv/wwv.cxx b/src/wwv/wwv.cxx index 3268ad73..641a9332 100644 --- a/src/wwv/wwv.cxx +++ b/src/wwv/wwv.cxx @@ -88,6 +88,7 @@ wwv::wwv() : modem() vidfilter = new Cmovavg(16); + cap &= ~CAP_TX; }