Fl_Text_Buffer.H 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. //
  2. // "$Id: Fl_Text_Buffer.H 12356 2017-07-26 12:32:13Z AlbrechtS $"
  3. //
  4. // Header file for Fl_Text_Buffer class.
  5. //
  6. // Copyright 2001-2017 by Bill Spitzak and others.
  7. // Original code Copyright Mark Edel. Permission to distribute under
  8. // the LGPL for the FLTK library granted by Mark Edel.
  9. //
  10. // Please report all bugs and problems on the following page:
  11. //
  12. // http://www.fltk.org/str.php
  13. //
  14. // Please report all bugs and problems on the following page:
  15. //
  16. // http://www.fltk.org/str.php
  17. //
  18. /* \file
  19. Fl_Text_Buffer, Fl_Text_Selection widget . */
  20. #ifndef FL_TEXT_BUFFER_H
  21. #define FL_TEXT_BUFFER_H
  22. #include <stdarg.h> /* va_start/end */
  23. #undef ASSERT_UTF8
  24. #ifdef ASSERT_UTF8
  25. # include <assert.h>
  26. # define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0);
  27. # define IS_UTF8_ALIGNED2(a, b) if (b>=0 && b<a->length()) assert(fl_utf8len(a->byte_at(b))>0);
  28. #else
  29. # define IS_UTF8_ALIGNED(a)
  30. # define IS_UTF8_ALIGNED2(a, b)
  31. #endif
  32. /*
  33. "character size" is the size of a UTF-8 character in bytes
  34. "character width" is the width of a Unicode character in pixels
  35. "column" was orginally defined as a character offset from the left margin.
  36. It was identical to the byte offset. In UTF-8, we have neither a byte offset
  37. nor truly fixed width fonts (*). Column could be a pixel value multiplied with
  38. an average character width (which is a bearable approximation).
  39. * in Unicode, there are no fixed width fonts! Even if the ASCII characters may
  40. happen to be all the same width in pixels, Chinese characters surely are not.
  41. There are plenty of exceptions, like ligatures, that make special handling of
  42. "fixed" character widths a nightmare. I decided to remove all references to
  43. fixed fonts and see "columns" as a multiple of the average width of a
  44. character in the main font.
  45. - Matthias
  46. */
  47. /* Maximum length in characters of a tab or control character expansion
  48. of a single buffer character */
  49. #define FL_TEXT_MAX_EXP_CHAR_LEN 20
  50. #include "Fl_Export.H"
  51. /**
  52. \class Fl_Text_Selection
  53. \brief This is an internal class for Fl_Text_Buffer to manage text selections.
  54. All methods use byte (not UTF-8 character) offsets and start at 0. This
  55. class works correctly with UTF-8 strings assuming that the parameters
  56. for all calls are on character boundaries.
  57. If the selection is inactive (not currently used), then selected()
  58. returns \p false and start() and end() return 0 (zero).
  59. The stored offsets are in ascending order, hence the following conditions
  60. are true (pseudo code):
  61. \code
  62. if ( !selected() ) : (start() == 0) && (end() == 0) && (start() == end())
  63. if ( selected() ) : start() < end()
  64. always : 0 <= start() <= end()
  65. always : length() == end() - start()
  66. \endcode
  67. The selection size in bytes can always (unconditionally) be computed by
  68. \code
  69. int size = sel->end() - sel->start();
  70. \endcode
  71. \see length()
  72. \note
  73. The \b protected member variables \p mStart and \p mEnd are not
  74. necessarily 0 (zero) if mSelected == \p false because they are
  75. not cleared when \p selected(false) is called (as of Jul 2017).
  76. This may be changed in the future.
  77. */
  78. class FL_EXPORT Fl_Text_Selection {
  79. friend class Fl_Text_Buffer;
  80. public:
  81. // Sets the selection range and selected().
  82. void set(int startpos, int endpos);
  83. // Updates a selection after text was modified.
  84. void update(int pos, int nDeleted, int nInserted);
  85. /**
  86. \brief Returns the byte offset to the first selected character.
  87. The returned offset is only valid if selected() returns true.
  88. If the selection is not valid the returned offset is 0 since FLTK 1.4.0.
  89. \note In FLTK 1.3.x the returned offset could be non-zero even if
  90. selected() would have returned 0.
  91. \return byte offset or 0 if not selected.
  92. */
  93. int start() const { return mSelected ? mStart : 0; }
  94. /**
  95. \brief Returns the byte offset to the character after the last selected character.
  96. The returned offset is only valid if selected() returns true (non-zero).
  97. The offset is 0 if no text is selected (since FLTK 1.4.0).
  98. \note In FLTK 1.3.x the returned offset could be non-zero even if
  99. selected() would have returned 0.
  100. \return byte offset or 0 if not selected.
  101. */
  102. int end() const { return mSelected ? mEnd : 0; }
  103. /**
  104. \brief Returns true if any text is selected.
  105. \return \p true if any text has been selected, or \p false
  106. if no text is selected.
  107. */
  108. bool selected() const { return mSelected; }
  109. /**
  110. \brief Modifies the 'selected' flag.
  111. \param b new flag
  112. */
  113. void selected(bool b) { mSelected = b; }
  114. /**
  115. \brief Returns the size in bytes of the selection.
  116. This is a convenience method. It always returns the same as
  117. \code
  118. end() - start()
  119. \endcode
  120. and it returns 0 if selected() == false.
  121. \return size in bytes or 0 if not selected.
  122. \since FLTK 1.4.0
  123. */
  124. int length() const { return mSelected ? mEnd - mStart : 0; }
  125. // Returns true if position \p pos is in this Fl_Text_Selection.
  126. int includes(int pos) const;
  127. // Returns true if selected() and the positions of this selection.
  128. int position(int *startpos, int *endpos) const;
  129. protected:
  130. int mStart; ///< byte offset to the first selected character
  131. int mEnd; ///< byte offset to the character after the last selected character
  132. bool mSelected; ///< this flag is set if any text is selected
  133. };
  134. typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
  135. int nRestyled, const char* deletedText,
  136. void* cbArg);
  137. typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
  138. /**
  139. \brief This class manages Unicode text displayed in one or more Fl_Text_Display widgets.
  140. All text in Fl_Text_Buffer must be encoded in UTF-8. All indices used in the
  141. function calls must be aligned to the start of a UTF-8 sequence. All indices
  142. and pointers returned will be aligned. All functions that return a single
  143. character will return that in an unsiged int in UCS-4 encoding.
  144. The Fl_Text_Buffer class is used by the Fl_Text_Display
  145. and Fl_Text_Editor to manage complex text data and is based upon the
  146. excellent NEdit text editor engine - see http://www.nedit.org/.
  147. */
  148. class FL_EXPORT Fl_Text_Buffer {
  149. public:
  150. /**
  151. Create an empty text buffer of a pre-determined size.
  152. \param requestedSize use this to avoid unnecessary re-allocation
  153. if you know exactly how much the buffer will need to hold
  154. \param preferredGapSize Initial size for the buffer gap (empty space
  155. in the buffer where text might be inserted
  156. if the user is typing sequential characters)
  157. */
  158. Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
  159. /**
  160. Frees a text buffer
  161. */
  162. ~Fl_Text_Buffer();
  163. /**
  164. \brief Returns the number of bytes in the buffer.
  165. \return size of text in bytes
  166. */
  167. int length() const { return mLength; }
  168. /**
  169. \brief Get a copy of the entire contents of the text buffer.
  170. Memory is allocated to contain the returned string, which the caller
  171. must free.
  172. \return newly allocated text buffer - must be free'd, text is UTF-8
  173. */
  174. char* text() const;
  175. /**
  176. Replaces the entire contents of the text buffer.
  177. \param text Text must be valid UTF-8. If null, an empty string is substituted.
  178. */
  179. void text(const char* text);
  180. /**
  181. \brief Get a copy of a part of the text buffer.
  182. Return a copy of the text between \p start and \p end character positions
  183. from text buffer \p buf. Positions start at 0, and the range does not
  184. include the character pointed to by \p end.
  185. When you are done with the text, free it using the free() function.
  186. \param start byte offset to first character
  187. \param end byte offset after last character in range
  188. \return newly allocated text buffer - must be free'd, text is UTF-8
  189. */
  190. char* text_range(int start, int end) const;
  191. /**
  192. Returns the character at the specified position \p pos in the buffer.
  193. Positions start at 0.
  194. \param pos byte offset into buffer, \p pos must be at a UTF-8 character boundary
  195. \return Unicode UCS-4 encoded character
  196. */
  197. unsigned int char_at(int pos) const;
  198. /**
  199. Returns the raw byte at the specified position pos in the buffer.
  200. Positions start at 0.
  201. \param pos byte offset into buffer
  202. \return unencoded raw byte
  203. */
  204. char byte_at(int pos) const;
  205. /**
  206. Convert a byte offset in buffer into a memory address.
  207. \param pos byte offset into buffer
  208. \return byte offset converted to a memory address
  209. */
  210. const char *address(int pos) const
  211. { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
  212. /**
  213. Convert a byte offset in buffer into a memory address.
  214. \param pos byte offset into buffer
  215. \return byte offset converted to a memory address
  216. */
  217. char *address(int pos)
  218. { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
  219. /**
  220. Inserts null-terminated string \p text at position \p pos.
  221. \param pos insertion position as byte offset (must be UTF-8 character aligned)
  222. \param text UTF-8 encoded and nul terminated text
  223. */
  224. void insert(int pos, const char* text);
  225. /**
  226. Appends the text string to the end of the buffer.
  227. \param t UTF-8 encoded and nul terminated text
  228. */
  229. void append(const char* t) { insert(length(), t); }
  230. void vprintf(const char *fmt, va_list ap);
  231. void printf(const char* fmt, ...);
  232. /**
  233. Deletes a range of characters in the buffer.
  234. \param start byte offset to first character to be removed
  235. \param end byte offset to character after last character to be removed
  236. */
  237. void remove(int start, int end);
  238. /**
  239. Deletes the characters between \p start and \p end, and inserts the
  240. null-terminated string \p text in their place in the buffer.
  241. \param start byte offset to first character to be removed and new insert position
  242. \param end byte offset to character after last character to be removed
  243. \param text UTF-8 encoded and nul terminated text
  244. */
  245. void replace(int start, int end, const char *text);
  246. /**
  247. Copies text from another Fl_Text_Buffer to this one.
  248. \param fromBuf source text buffer, may be the same as this
  249. \param fromStart byte offset into buffer
  250. \param fromEnd byte offset into buffer
  251. \param toPos destination byte offset into buffer
  252. */
  253. void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
  254. /**
  255. Undo text modification according to the undo variables or insert text
  256. from the undo buffer
  257. */
  258. int undo(int *cp=0);
  259. /**
  260. Lets the undo system know if we can undo changes
  261. */
  262. void canUndo(char flag=1);
  263. /**
  264. Inserts a file at the specified position.
  265. Returns
  266. - 0 on success
  267. - non-zero on error (strerror() contains reason)
  268. - 1 indicates open for read failed (no data loaded)
  269. - 2 indicates error occurred while reading data (data was partially loaded)
  270. File can be UTF-8 or CP1252 encoded.
  271. If the input file is not UTF-8 encoded, the Fl_Text_Buffer widget will
  272. contain data transcoded to UTF-8. By default, the message
  273. Fl_Text_Buffer::file_encoding_warning_message
  274. will warn the user about this.
  275. \see input_file_was_transcoded and transcoding_warning_action.
  276. */
  277. int insertfile(const char *file, int pos, int buflen = 128*1024);
  278. /**
  279. Appends the named file to the end of the buffer. See also insertfile().
  280. */
  281. int appendfile(const char *file, int buflen = 128*1024)
  282. { return insertfile(file, length(), buflen); }
  283. /**
  284. Loads a text file into the buffer. See also insertfile().
  285. */
  286. int loadfile(const char *file, int buflen = 128*1024)
  287. { select(0, length()); remove_selection(); return appendfile(file, buflen); }
  288. /**
  289. Writes the specified portions of the text buffer to a file.
  290. Returns
  291. - 0 on success
  292. - non-zero on error (strerror() contains reason)
  293. - 1 indicates open for write failed (no data saved)
  294. - 2 indicates error occurred while writing data (data was partially saved)
  295. \see savefile(const char *file, int buflen)
  296. */
  297. int outputfile(const char *file, int start, int end, int buflen = 128*1024);
  298. /**
  299. Saves a text file from the current buffer.
  300. Returns
  301. - 0 on success
  302. - non-zero on error (strerror() contains reason)
  303. - 1 indicates open for write failed (no data saved)
  304. - 2 indicates error occurred while writing data (data was partially saved)
  305. \see outputfile(const char *file, int start, int end, int buflen)
  306. */
  307. int savefile(const char *file, int buflen = 128*1024)
  308. { return outputfile(file, 0, length(), buflen); }
  309. /**
  310. Gets the tab width.
  311. The tab width is measured in characters. The pixel position is
  312. calculated using an average character width.
  313. */
  314. int tab_distance() const { return mTabDist; }
  315. /**
  316. Set the hardware tab distance (width) used by all displays for this buffer,
  317. and used in computing offsets for rectangular selection operations.
  318. */
  319. void tab_distance(int tabDist);
  320. /**
  321. Selects a range of characters in the buffer.
  322. */
  323. void select(int start, int end);
  324. /**
  325. Returns a non-zero value if text has been selected, 0 otherwise.
  326. */
  327. int selected() const { return mPrimary.selected(); }
  328. /**
  329. Cancels any previous selection on the primary text selection object.
  330. */
  331. void unselect();
  332. /**
  333. Gets the selection position.
  334. */
  335. int selection_position(int* start, int* end);
  336. /**
  337. Returns the currently selected text.
  338. When you are done with the text, free it using the free() function.
  339. */
  340. char* selection_text();
  341. /**
  342. Removes the text in the primary selection.
  343. */
  344. void remove_selection();
  345. /**
  346. Replaces the text in the primary selection.
  347. */
  348. void replace_selection(const char* text);
  349. /**
  350. Selects a range of characters in the secondary selection.
  351. */
  352. void secondary_select(int start, int end);
  353. /**
  354. Returns a non-zero value if text has been selected in the secondary
  355. text selection, 0 otherwise.
  356. */
  357. int secondary_selected() { return mSecondary.selected(); }
  358. /**
  359. Clears any selection in the secondary text selection object.
  360. */
  361. void secondary_unselect();
  362. /**
  363. Returns the current selection in the secondary text selection object.
  364. */
  365. int secondary_selection_position(int* start, int* end);
  366. /**
  367. Returns the text in the secondary selection.
  368. When you are done with the text, free it using the free() function.
  369. */
  370. char* secondary_selection_text();
  371. /**
  372. Removes the text from the buffer corresponding to the secondary text
  373. selection object.
  374. */
  375. void remove_secondary_selection();
  376. /**
  377. Replaces the text from the buffer corresponding to the secondary
  378. text selection object with the new string \p text.
  379. */
  380. void replace_secondary_selection(const char* text);
  381. /**
  382. Highlights the specified text within the buffer.
  383. */
  384. void highlight(int start, int end);
  385. /**
  386. Returns the highlighted text.
  387. When you are done with the text, free it using the free() function.
  388. */
  389. int highlight() { return mHighlight.selected(); }
  390. /**
  391. Unhighlights text in the buffer.
  392. */
  393. void unhighlight();
  394. /**
  395. Highlights the specified text between \p start and \p end within the buffer.
  396. */
  397. int highlight_position(int* start, int* end);
  398. /**
  399. Returns the highlighted text.
  400. When you are done with the text, free it using the free() function.
  401. */
  402. char* highlight_text();
  403. /**
  404. Adds a callback function that is called whenever the text buffer is modified.
  405. The callback function is declared as follows:
  406. \code
  407. typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
  408. int nRestyled, const char* deletedText,
  409. void* cbArg);
  410. \endcode
  411. */
  412. void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
  413. /**
  414. Removes a modify callback.
  415. */
  416. void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
  417. /**
  418. Calls all modify callbacks that have been registered using
  419. the add_modify_callback() method.
  420. */
  421. void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
  422. /**
  423. Adds a callback routine to be called before text is deleted from the buffer.
  424. */
  425. void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
  426. /**
  427. Removes a callback routine \p bufPreDeleteCB associated with argument \p cbArg
  428. to be called before text is deleted from the buffer.
  429. */
  430. void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
  431. /**
  432. Calls the stored pre-delete callback procedure(s) for this buffer to update
  433. the changed area(s) on the screen and any other listeners.
  434. */
  435. void call_predelete_callbacks() { call_predelete_callbacks(0, 0); }
  436. /**
  437. Returns the text from the entire line containing the specified
  438. character position.
  439. When you are done with the text, free it using the free() function.
  440. \param pos byte index into buffer
  441. \return copy of UTF-8 text, must be free'd
  442. */
  443. char* line_text(int pos) const;
  444. /**
  445. Returns the position of the start of the line containing position \p pos.
  446. \param pos byte index into buffer
  447. \return byte offset to line start
  448. */
  449. int line_start(int pos) const;
  450. /**
  451. Finds and returns the position of the end of the line containing position
  452. \p pos (which is either a pointer to the newline character ending the line
  453. or a pointer to one character beyond the end of the buffer).
  454. \param pos byte index into buffer
  455. \return byte offset to line end
  456. */
  457. int line_end(int pos) const;
  458. /**
  459. Returns the position corresponding to the start of the word.
  460. \param pos byte index into buffer
  461. \return byte offset to word start
  462. */
  463. int word_start(int pos) const;
  464. /**
  465. Returns the position corresponding to the end of the word.
  466. \param pos byte index into buffer
  467. \return byte offset to word end
  468. */
  469. int word_end(int pos) const;
  470. /**
  471. Count the number of displayed characters between buffer position
  472. \p lineStartPos and \p targetPos.
  473. Displayed characters are the characters shown on the screen to represent
  474. characters in the buffer, where tabs and control characters are expanded.
  475. */
  476. int count_displayed_characters(int lineStartPos, int targetPos) const;
  477. /**
  478. Count forward from buffer position \p startPos in displayed characters.
  479. Displayed characters are the characters shown on the screen to represent
  480. characters in the buffer, where tabs and control characters are expanded.
  481. \param lineStartPos byte offset into buffer
  482. \param nChars number of bytes that are sent to the display
  483. \return byte offset in input after all output bytes are sent
  484. */
  485. int skip_displayed_characters(int lineStartPos, int nChars);
  486. /**
  487. Counts the number of newlines between \p startPos and \p endPos in buffer.
  488. The character at position \p endPos is not counted.
  489. */
  490. int count_lines(int startPos, int endPos) const;
  491. /**
  492. Finds the first character of the line \p nLines forward from \p startPos
  493. in the buffer and returns its position.
  494. */
  495. int skip_lines(int startPos, int nLines);
  496. /**
  497. Finds and returns the position of the first character of the line \p nLines
  498. backwards from \p startPos (not counting the character pointed to by
  499. \p startpos if that is a newline) in the buffer.
  500. \p nLines == 0 means find the beginning of the line.
  501. */
  502. int rewind_lines(int startPos, int nLines);
  503. /**
  504. Finds the next occurrence of the specified character.
  505. Search forwards in buffer for character \p searchChar, starting
  506. with the character \p startPos, and returning the result in \p foundPos.
  507. Returns 1 if found, 0 if not.
  508. The difference between this and search_forward() is that it's optimized
  509. for single characters. The overall performance of the text widget is
  510. dependent on its ability to count lines quickly, hence searching for a
  511. single character: newline.
  512. \param startPos byte offset to start position
  513. \param searchChar UCS-4 character that we want to find
  514. \param foundPos byte offset where the character was found
  515. \return 1 if found, 0 if not
  516. */
  517. int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
  518. /**
  519. Search backwards in buffer \p buf for character \p searchChar, starting
  520. with the character \e before \p startPos, returning the result in \p foundPos.
  521. Returns 1 if found, 0 if not. The difference between this and
  522. search_backward() is that it's optimized for single characters. The
  523. overall performance of the text widget is dependent on its ability to
  524. count lines quickly, hence searching for a single character: newline.
  525. \param startPos byte offset to start position
  526. \param searchChar UCS-4 character that we want to find
  527. \param foundPos byte offset where the character was found
  528. \return 1 if found, 0 if not
  529. */
  530. int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
  531. /**
  532. Search forwards in buffer for string \p searchString, starting with the
  533. character \p startPos, and returning the result in \p foundPos.
  534. Returns 1 if found, 0 if not.
  535. \param startPos byte offset to start position
  536. \param searchString UTF-8 string that we want to find
  537. \param foundPos byte offset where the string was found
  538. \param matchCase if set, match character case
  539. \return 1 if found, 0 if not
  540. */
  541. int search_forward(int startPos, const char* searchString, int* foundPos,
  542. int matchCase = 0) const;
  543. /**
  544. Search backwards in buffer for string \p searchString, starting with
  545. the character \e at \p startPos, returning the result in \p foundPos.
  546. Returns 1 if found, 0 if not.
  547. \param startPos byte offset to start position
  548. \param searchString UTF-8 string that we want to find
  549. \param foundPos byte offset where the string was found
  550. \param matchCase if set, match character case
  551. \return 1 if found, 0 if not
  552. */
  553. int search_backward(int startPos, const char* searchString, int* foundPos,
  554. int matchCase = 0) const;
  555. /**
  556. Returns the primary selection.
  557. */
  558. const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
  559. /**
  560. Returns the primary selection.
  561. */
  562. Fl_Text_Selection* primary_selection() { return &mPrimary; }
  563. /**
  564. Returns the secondary selection.
  565. */
  566. const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
  567. /**
  568. Returns the current highlight selection.
  569. */
  570. const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
  571. /**
  572. Returns the index of the previous character.
  573. \param ix index to the current character
  574. */
  575. int prev_char(int ix) const;
  576. int prev_char_clipped(int ix) const;
  577. /**
  578. Returns the index of the next character.
  579. \param ix index to the current character
  580. */
  581. int next_char(int ix) const;
  582. int next_char_clipped(int ix) const;
  583. /**
  584. Align an index into the buffer to the current or previous UTF-8 boundary.
  585. */
  586. int utf8_align(int) const;
  587. /**
  588. \brief true if the loaded file has been transcoded to UTF-8.
  589. */
  590. int input_file_was_transcoded;
  591. /** This message may be displayed using the fl_alert() function when a file
  592. which was not UTF-8 encoded is input.
  593. */
  594. static const char* file_encoding_warning_message;
  595. /**
  596. \brief Pointer to a function called after reading a non UTF-8 encoded file.
  597. This function is called after reading a file if the file content
  598. was transcoded to UTF-8. Its default implementation calls fl_alert()
  599. with the text of \ref file_encoding_warning_message. No warning message is
  600. displayed if this pointer is set to NULL. Use \ref input_file_was_transcoded
  601. to be informed if file input required transcoding to UTF-8.
  602. */
  603. void (*transcoding_warning_action)(Fl_Text_Buffer*);
  604. bool is_word_separator(int pos) const;
  605. protected:
  606. /**
  607. Calls the stored modify callback procedure(s) for this buffer to update the
  608. changed area(s) on the screen and any other listeners.
  609. */
  610. void call_modify_callbacks(int pos, int nDeleted, int nInserted,
  611. int nRestyled, const char* deletedText) const;
  612. /**
  613. Calls the stored pre-delete callback procedure(s) for this buffer to update
  614. the changed area(s) on the screen and any other listeners.
  615. */
  616. void call_predelete_callbacks(int pos, int nDeleted) const;
  617. /**
  618. Internal (non-redisplaying) version of insert().
  619. Returns the length of text inserted (this is just strlen(\p text), however
  620. this calculation can be expensive and the length will be required by any
  621. caller who will continue on to call redisplay). \p pos must be contiguous
  622. with the existing text in the buffer (i.e. not past the end).
  623. \return the number of bytes inserted
  624. */
  625. int insert_(int pos, const char* text);
  626. /**
  627. Internal (non-redisplaying) version of remove().
  628. Removes the contents of the buffer between \p start and \p end (and moves
  629. the gap to the site of the delete).
  630. */
  631. void remove_(int start, int end);
  632. /**
  633. Calls the stored redisplay procedure(s) for this buffer to update the
  634. screen for a change in a selection.
  635. */
  636. void redisplay_selection(Fl_Text_Selection* oldSelection,
  637. Fl_Text_Selection* newSelection) const;
  638. /**
  639. Move the gap to start at a new position.
  640. */
  641. void move_gap(int pos);
  642. /**
  643. Reallocates the text storage in the buffer to have a gap starting at \p newGapStart
  644. and a gap size of \p newGapLen, preserving the buffer's current contents.
  645. */
  646. void reallocate_with_gap(int newGapStart, int newGapLen);
  647. char* selection_text_(Fl_Text_Selection* sel) const;
  648. /**
  649. Removes the text from the buffer corresponding to \p sel.
  650. */
  651. void remove_selection_(Fl_Text_Selection* sel);
  652. /**
  653. Replaces the \p text in selection \p sel.
  654. */
  655. void replace_selection_(Fl_Text_Selection* sel, const char* text);
  656. /**
  657. Updates all of the selections in the buffer for changes in the buffer's text
  658. */
  659. void update_selections(int pos, int nDeleted, int nInserted);
  660. Fl_Text_Selection mPrimary; /**< highlighted areas */
  661. Fl_Text_Selection mSecondary; /**< highlighted areas */
  662. Fl_Text_Selection mHighlight; /**< highlighted areas */
  663. int mLength; /**< length of the text in the buffer (the length
  664. of the buffer itself must be calculated:
  665. gapEnd - gapStart + length) */
  666. char* mBuf; /**< allocated memory where the text is stored */
  667. int mGapStart; /**< points to the first character of the gap */
  668. int mGapEnd; /**< points to the first character after the gap */
  669. // The hardware tab distance used by all displays for this buffer,
  670. // and used in computing offsets for rectangular selection operations.
  671. int mTabDist; /**< equiv. number of characters in a tab */
  672. int mNModifyProcs; /**< number of modify-redisplay procs attached */
  673. Fl_Text_Modify_Cb *mModifyProcs;/**< procedures to call when buffer is
  674. modified to redisplay contents */
  675. void** mCbArgs; /**< caller arguments for modifyProcs above */
  676. int mNPredeleteProcs; /**< number of pre-delete procs attached */
  677. Fl_Text_Predelete_Cb *mPredeleteProcs; /**< procedure to call before text is deleted
  678. from the buffer; at most one is supported. */
  679. void **mPredeleteCbArgs; /**< caller argument for pre-delete proc above */
  680. int mCursorPosHint; /**< hint for reasonable cursor position after
  681. a buffer modification operation */
  682. char mCanUndo; /**< if this buffer is used for attributes, it must
  683. not do any undo calls */
  684. int mPreferredGapSize; /**< the default allocation for the text gap is 1024
  685. bytes and should only be increased if frequent
  686. and large changes in buffer size are expected */
  687. };
  688. #endif
  689. //
  690. // End of "$Id: Fl_Text_Buffer.H 12356 2017-07-26 12:32:13Z AlbrechtS $".
  691. //