The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2562 lines
75KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // a word or space that can't be broken down any further
  19. struct TextAtom
  20. {
  21. //==============================================================================
  22. String atomText;
  23. float width;
  24. int numChars;
  25. //==============================================================================
  26. bool isWhitespace() const noexcept { return CharacterFunctions::isWhitespace (atomText[0]); }
  27. bool isNewLine() const noexcept { return atomText[0] == '\r' || atomText[0] == '\n'; }
  28. String getText (const juce_wchar passwordCharacter) const
  29. {
  30. if (passwordCharacter == 0)
  31. return atomText;
  32. return String::repeatedString (String::charToString (passwordCharacter),
  33. atomText.length());
  34. }
  35. String getTrimmedText (const juce_wchar passwordCharacter) const
  36. {
  37. if (passwordCharacter == 0)
  38. return atomText.substring (0, numChars);
  39. if (isNewLine())
  40. return String::empty;
  41. return String::repeatedString (String::charToString (passwordCharacter), numChars);
  42. }
  43. };
  44. //==============================================================================
  45. // a run of text with a single font and colour
  46. class TextEditor::UniformTextSection
  47. {
  48. public:
  49. UniformTextSection (const String& text, const Font& f, const Colour col, const juce_wchar passwordChar)
  50. : font (f), colour (col)
  51. {
  52. initialiseAtoms (text, passwordChar);
  53. }
  54. UniformTextSection (const UniformTextSection& other)
  55. : font (other.font), colour (other.colour)
  56. {
  57. atoms.ensureStorageAllocated (other.atoms.size());
  58. for (int i = 0; i < other.atoms.size(); ++i)
  59. atoms.add (new TextAtom (*other.atoms.getUnchecked(i)));
  60. }
  61. ~UniformTextSection() {} // (no need to delete the atoms, as they're explicitly deleted by the caller)
  62. void clear()
  63. {
  64. for (int i = atoms.size(); --i >= 0;)
  65. delete atoms.getUnchecked (i);
  66. atoms.clear();
  67. }
  68. void append (const UniformTextSection& other, const juce_wchar passwordChar)
  69. {
  70. if (other.atoms.size() > 0)
  71. {
  72. int i = 0;
  73. if (TextAtom* const lastAtom = atoms.getLast())
  74. {
  75. if (! CharacterFunctions::isWhitespace (lastAtom->atomText.getLastCharacter()))
  76. {
  77. TextAtom* const first = other.atoms.getUnchecked(0);
  78. if (! CharacterFunctions::isWhitespace (first->atomText[0]))
  79. {
  80. lastAtom->atomText += first->atomText;
  81. lastAtom->numChars = (uint16) (lastAtom->numChars + first->numChars);
  82. lastAtom->width = font.getStringWidthFloat (lastAtom->getText (passwordChar));
  83. delete first;
  84. ++i;
  85. }
  86. }
  87. }
  88. atoms.ensureStorageAllocated (atoms.size() + other.atoms.size() - i);
  89. while (i < other.atoms.size())
  90. {
  91. atoms.add (other.atoms.getUnchecked(i));
  92. ++i;
  93. }
  94. }
  95. }
  96. UniformTextSection* split (const int indexToBreakAt, const juce_wchar passwordChar)
  97. {
  98. UniformTextSection* const section2 = new UniformTextSection (String::empty, font, colour, passwordChar);
  99. int index = 0;
  100. for (int i = 0; i < atoms.size(); ++i)
  101. {
  102. TextAtom* const atom = atoms.getUnchecked(i);
  103. const int nextIndex = index + atom->numChars;
  104. if (index == indexToBreakAt)
  105. {
  106. for (int j = i; j < atoms.size(); ++j)
  107. section2->atoms.add (atoms.getUnchecked (j));
  108. for (int j = atoms.size(); --j >= i;)
  109. atoms.remove (j);
  110. break;
  111. }
  112. else if (indexToBreakAt >= index && indexToBreakAt < nextIndex)
  113. {
  114. TextAtom* const secondAtom = new TextAtom();
  115. secondAtom->atomText = atom->atomText.substring (indexToBreakAt - index);
  116. secondAtom->width = font.getStringWidthFloat (secondAtom->getText (passwordChar));
  117. secondAtom->numChars = (uint16) secondAtom->atomText.length();
  118. section2->atoms.add (secondAtom);
  119. atom->atomText = atom->atomText.substring (0, indexToBreakAt - index);
  120. atom->width = font.getStringWidthFloat (atom->getText (passwordChar));
  121. atom->numChars = (uint16) (indexToBreakAt - index);
  122. for (int j = i + 1; j < atoms.size(); ++j)
  123. section2->atoms.add (atoms.getUnchecked (j));
  124. for (int j = atoms.size(); --j > i;)
  125. atoms.remove (j);
  126. break;
  127. }
  128. index = nextIndex;
  129. }
  130. return section2;
  131. }
  132. void appendAllText (MemoryOutputStream& mo) const
  133. {
  134. for (int i = 0; i < atoms.size(); ++i)
  135. mo << atoms.getUnchecked(i)->atomText;
  136. }
  137. void appendSubstring (MemoryOutputStream& mo, const Range<int> range) const
  138. {
  139. int index = 0;
  140. for (int i = 0; i < atoms.size(); ++i)
  141. {
  142. const TextAtom* const atom = atoms.getUnchecked (i);
  143. const int nextIndex = index + atom->numChars;
  144. if (range.getStart() < nextIndex)
  145. {
  146. if (range.getEnd() <= index)
  147. break;
  148. const Range<int> r ((range - index).getIntersectionWith (Range<int> (0, (int) atom->numChars)));
  149. if (! r.isEmpty())
  150. mo << atom->atomText.substring (r.getStart(), r.getEnd());
  151. }
  152. index = nextIndex;
  153. }
  154. }
  155. int getTotalLength() const noexcept
  156. {
  157. int total = 0;
  158. for (int i = atoms.size(); --i >= 0;)
  159. total += atoms.getUnchecked(i)->numChars;
  160. return total;
  161. }
  162. void setFont (const Font& newFont, const juce_wchar passwordChar)
  163. {
  164. if (font != newFont)
  165. {
  166. font = newFont;
  167. for (int i = atoms.size(); --i >= 0;)
  168. {
  169. TextAtom* const atom = atoms.getUnchecked(i);
  170. atom->width = newFont.getStringWidthFloat (atom->getText (passwordChar));
  171. }
  172. }
  173. }
  174. //==============================================================================
  175. Font font;
  176. Colour colour;
  177. Array <TextAtom*> atoms;
  178. private:
  179. void initialiseAtoms (const String& textToParse, const juce_wchar passwordChar)
  180. {
  181. String::CharPointerType text (textToParse.getCharPointer());
  182. while (! text.isEmpty())
  183. {
  184. size_t numChars = 0;
  185. String::CharPointerType start (text);
  186. // create a whitespace atom unless it starts with non-ws
  187. if (text.isWhitespace() && *text != '\r' && *text != '\n')
  188. {
  189. do
  190. {
  191. ++text;
  192. ++numChars;
  193. }
  194. while (text.isWhitespace() && *text != '\r' && *text != '\n');
  195. }
  196. else
  197. {
  198. if (*text == '\r')
  199. {
  200. ++text;
  201. ++numChars;
  202. if (*text == '\n')
  203. {
  204. ++start;
  205. ++text;
  206. }
  207. }
  208. else if (*text == '\n')
  209. {
  210. ++text;
  211. ++numChars;
  212. }
  213. else
  214. {
  215. while (! (text.isEmpty() || text.isWhitespace()))
  216. {
  217. ++text;
  218. ++numChars;
  219. }
  220. }
  221. }
  222. TextAtom* const atom = new TextAtom();
  223. atom->atomText = String (start, numChars);
  224. atom->width = font.getStringWidthFloat (atom->getText (passwordChar));
  225. atom->numChars = (uint16) numChars;
  226. atoms.add (atom);
  227. }
  228. }
  229. UniformTextSection& operator= (const UniformTextSection& other);
  230. JUCE_LEAK_DETECTOR (UniformTextSection)
  231. };
  232. //==============================================================================
  233. class TextEditor::Iterator
  234. {
  235. public:
  236. Iterator (const Array <UniformTextSection*>& sectionList,
  237. const float wrapWidth,
  238. const juce_wchar passwordChar)
  239. : indexInText (0),
  240. lineY (0),
  241. lineHeight (0),
  242. maxDescent (0),
  243. atomX (0),
  244. atomRight (0),
  245. atom (0),
  246. currentSection (nullptr),
  247. sections (sectionList),
  248. sectionIndex (0),
  249. atomIndex (0),
  250. wordWrapWidth (wrapWidth),
  251. passwordCharacter (passwordChar)
  252. {
  253. jassert (wordWrapWidth > 0);
  254. if (sections.size() > 0)
  255. {
  256. currentSection = sections.getUnchecked (sectionIndex);
  257. if (currentSection != nullptr)
  258. beginNewLine();
  259. }
  260. }
  261. Iterator (const Iterator& other)
  262. : indexInText (other.indexInText),
  263. lineY (other.lineY),
  264. lineHeight (other.lineHeight),
  265. maxDescent (other.maxDescent),
  266. atomX (other.atomX),
  267. atomRight (other.atomRight),
  268. atom (other.atom),
  269. currentSection (other.currentSection),
  270. sections (other.sections),
  271. sectionIndex (other.sectionIndex),
  272. atomIndex (other.atomIndex),
  273. wordWrapWidth (other.wordWrapWidth),
  274. passwordCharacter (other.passwordCharacter),
  275. tempAtom (other.tempAtom)
  276. {
  277. }
  278. //==============================================================================
  279. bool next()
  280. {
  281. if (atom == &tempAtom)
  282. {
  283. const int numRemaining = tempAtom.atomText.length() - tempAtom.numChars;
  284. if (numRemaining > 0)
  285. {
  286. tempAtom.atomText = tempAtom.atomText.substring (tempAtom.numChars);
  287. atomX = 0;
  288. if (tempAtom.numChars > 0)
  289. lineY += lineHeight;
  290. indexInText += tempAtom.numChars;
  291. GlyphArrangement g;
  292. g.addLineOfText (currentSection->font, atom->getText (passwordCharacter), 0.0f, 0.0f);
  293. int split;
  294. for (split = 0; split < g.getNumGlyphs(); ++split)
  295. if (shouldWrap (g.getGlyph (split).getRight()))
  296. break;
  297. if (split > 0 && split <= numRemaining)
  298. {
  299. tempAtom.numChars = (uint16) split;
  300. tempAtom.width = g.getGlyph (split - 1).getRight();
  301. atomRight = atomX + tempAtom.width;
  302. return true;
  303. }
  304. }
  305. }
  306. bool forceNewLine = false;
  307. if (sectionIndex >= sections.size())
  308. {
  309. moveToEndOfLastAtom();
  310. return false;
  311. }
  312. else if (atomIndex >= currentSection->atoms.size() - 1)
  313. {
  314. if (atomIndex >= currentSection->atoms.size())
  315. {
  316. if (++sectionIndex >= sections.size())
  317. {
  318. moveToEndOfLastAtom();
  319. return false;
  320. }
  321. atomIndex = 0;
  322. currentSection = sections.getUnchecked (sectionIndex);
  323. }
  324. else
  325. {
  326. const TextAtom* const lastAtom = currentSection->atoms.getUnchecked (atomIndex);
  327. if (! lastAtom->isWhitespace())
  328. {
  329. // handle the case where the last atom in a section is actually part of the same
  330. // word as the first atom of the next section...
  331. float right = atomRight + lastAtom->width;
  332. float lineHeight2 = lineHeight;
  333. float maxDescent2 = maxDescent;
  334. for (int section = sectionIndex + 1; section < sections.size(); ++section)
  335. {
  336. const UniformTextSection* const s = sections.getUnchecked (section);
  337. if (s->atoms.size() == 0)
  338. break;
  339. const TextAtom* const nextAtom = s->atoms.getUnchecked (0);
  340. if (nextAtom->isWhitespace())
  341. break;
  342. right += nextAtom->width;
  343. lineHeight2 = jmax (lineHeight2, s->font.getHeight());
  344. maxDescent2 = jmax (maxDescent2, s->font.getDescent());
  345. if (shouldWrap (right))
  346. {
  347. lineHeight = lineHeight2;
  348. maxDescent = maxDescent2;
  349. forceNewLine = true;
  350. break;
  351. }
  352. if (s->atoms.size() > 1)
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. if (atom != nullptr)
  359. {
  360. atomX = atomRight;
  361. indexInText += atom->numChars;
  362. if (atom->isNewLine())
  363. beginNewLine();
  364. }
  365. atom = currentSection->atoms.getUnchecked (atomIndex);
  366. atomRight = atomX + atom->width;
  367. ++atomIndex;
  368. if (shouldWrap (atomRight) || forceNewLine)
  369. {
  370. if (atom->isWhitespace())
  371. {
  372. // leave whitespace at the end of a line, but truncate it to avoid scrolling
  373. atomRight = jmin (atomRight, wordWrapWidth);
  374. }
  375. else
  376. {
  377. atomRight = atom->width;
  378. if (shouldWrap (atomRight)) // atom too big to fit on a line, so break it up..
  379. {
  380. tempAtom = *atom;
  381. tempAtom.width = 0;
  382. tempAtom.numChars = 0;
  383. atom = &tempAtom;
  384. if (atomX > 0)
  385. beginNewLine();
  386. return next();
  387. }
  388. beginNewLine();
  389. return true;
  390. }
  391. }
  392. return true;
  393. }
  394. void beginNewLine()
  395. {
  396. atomX = 0;
  397. lineY += lineHeight;
  398. int tempSectionIndex = sectionIndex;
  399. int tempAtomIndex = atomIndex;
  400. const UniformTextSection* section = sections.getUnchecked (tempSectionIndex);
  401. lineHeight = section->font.getHeight();
  402. maxDescent = section->font.getDescent();
  403. float x = (atom != nullptr) ? atom->width : 0;
  404. while (! shouldWrap (x))
  405. {
  406. if (tempSectionIndex >= sections.size())
  407. break;
  408. bool checkSize = false;
  409. if (tempAtomIndex >= section->atoms.size())
  410. {
  411. if (++tempSectionIndex >= sections.size())
  412. break;
  413. tempAtomIndex = 0;
  414. section = sections.getUnchecked (tempSectionIndex);
  415. checkSize = true;
  416. }
  417. const TextAtom* const nextAtom = section->atoms.getUnchecked (tempAtomIndex);
  418. if (nextAtom == nullptr)
  419. break;
  420. x += nextAtom->width;
  421. if (shouldWrap (x) || nextAtom->isNewLine())
  422. break;
  423. if (checkSize)
  424. {
  425. lineHeight = jmax (lineHeight, section->font.getHeight());
  426. maxDescent = jmax (maxDescent, section->font.getDescent());
  427. }
  428. ++tempAtomIndex;
  429. }
  430. }
  431. //==============================================================================
  432. void draw (Graphics& g, const UniformTextSection*& lastSection) const
  433. {
  434. if (passwordCharacter != 0 || ! atom->isWhitespace())
  435. {
  436. if (lastSection != currentSection)
  437. {
  438. lastSection = currentSection;
  439. g.setColour (currentSection->colour);
  440. g.setFont (currentSection->font);
  441. }
  442. jassert (atom->getTrimmedText (passwordCharacter).isNotEmpty());
  443. GlyphArrangement ga;
  444. ga.addLineOfText (currentSection->font,
  445. atom->getTrimmedText (passwordCharacter),
  446. atomX, (float) roundToInt (lineY + lineHeight - maxDescent));
  447. ga.draw (g);
  448. }
  449. }
  450. void drawSelection (Graphics& g, const Range<int> selected) const
  451. {
  452. const int startX = roundToInt (indexToX (selected.getStart()));
  453. const int endX = roundToInt (indexToX (selected.getEnd()));
  454. const int y = roundToInt (lineY);
  455. const int nextY = roundToInt (lineY + lineHeight);
  456. g.fillRect (startX, y, endX - startX, nextY - y);
  457. }
  458. void drawUnderline (Graphics& g, const Range<int> underline, const Colour colour) const
  459. {
  460. const int startX = roundToInt (indexToX (underline.getStart()));
  461. const int endX = roundToInt (indexToX (underline.getEnd()));
  462. const int baselineY = roundToInt (lineY + currentSection->font.getAscent() + 0.5f);
  463. Graphics::ScopedSaveState state (g);
  464. g.reduceClipRegion (Rectangle<int> (startX, baselineY, endX - startX, 1));
  465. g.fillCheckerBoard (Rectangle<int> (endX, baselineY + 1), 3, 1, colour, Colours::transparentBlack);
  466. }
  467. void drawSelectedText (Graphics& g,
  468. const Range<int> selected,
  469. const Colour selectedTextColour) const
  470. {
  471. if (passwordCharacter != 0 || ! atom->isWhitespace())
  472. {
  473. GlyphArrangement ga;
  474. ga.addLineOfText (currentSection->font,
  475. atom->getTrimmedText (passwordCharacter),
  476. atomX, (float) roundToInt (lineY + lineHeight - maxDescent));
  477. if (selected.getEnd() < indexInText + atom->numChars)
  478. {
  479. GlyphArrangement ga2 (ga);
  480. ga2.removeRangeOfGlyphs (0, selected.getEnd() - indexInText);
  481. ga.removeRangeOfGlyphs (selected.getEnd() - indexInText, -1);
  482. g.setColour (currentSection->colour);
  483. ga2.draw (g);
  484. }
  485. if (selected.getStart() > indexInText)
  486. {
  487. GlyphArrangement ga2 (ga);
  488. ga2.removeRangeOfGlyphs (selected.getStart() - indexInText, -1);
  489. ga.removeRangeOfGlyphs (0, selected.getStart() - indexInText);
  490. g.setColour (currentSection->colour);
  491. ga2.draw (g);
  492. }
  493. g.setColour (selectedTextColour);
  494. ga.draw (g);
  495. }
  496. }
  497. //==============================================================================
  498. float indexToX (const int indexToFind) const
  499. {
  500. if (indexToFind <= indexInText)
  501. return atomX;
  502. if (indexToFind >= indexInText + atom->numChars)
  503. return atomRight;
  504. GlyphArrangement g;
  505. g.addLineOfText (currentSection->font,
  506. atom->getText (passwordCharacter),
  507. atomX, 0.0f);
  508. if (indexToFind - indexInText >= g.getNumGlyphs())
  509. return atomRight;
  510. return jmin (atomRight, g.getGlyph (indexToFind - indexInText).getLeft());
  511. }
  512. int xToIndex (const float xToFind) const
  513. {
  514. if (xToFind <= atomX || atom->isNewLine())
  515. return indexInText;
  516. if (xToFind >= atomRight)
  517. return indexInText + atom->numChars;
  518. GlyphArrangement g;
  519. g.addLineOfText (currentSection->font,
  520. atom->getText (passwordCharacter),
  521. atomX, 0.0f);
  522. const int numGlyphs = g.getNumGlyphs();
  523. int j;
  524. for (j = 0; j < numGlyphs; ++j)
  525. {
  526. const PositionedGlyph& pg = g.getGlyph(j);
  527. if ((pg.getLeft() + pg.getRight()) / 2 > xToFind)
  528. break;
  529. }
  530. return indexInText + j;
  531. }
  532. //==============================================================================
  533. bool getCharPosition (const int index, float& cx, float& cy, float& lineHeightFound)
  534. {
  535. while (next())
  536. {
  537. if (indexInText + atom->numChars > index)
  538. {
  539. cx = indexToX (index);
  540. cy = lineY;
  541. lineHeightFound = lineHeight;
  542. return true;
  543. }
  544. }
  545. cx = atomX;
  546. cy = lineY;
  547. lineHeightFound = lineHeight;
  548. return false;
  549. }
  550. //==============================================================================
  551. int indexInText;
  552. float lineY, lineHeight, maxDescent;
  553. float atomX, atomRight;
  554. const TextAtom* atom;
  555. const UniformTextSection* currentSection;
  556. private:
  557. const Array <UniformTextSection*>& sections;
  558. int sectionIndex, atomIndex;
  559. const float wordWrapWidth;
  560. const juce_wchar passwordCharacter;
  561. TextAtom tempAtom;
  562. Iterator& operator= (const Iterator&);
  563. void moveToEndOfLastAtom()
  564. {
  565. if (atom != nullptr)
  566. {
  567. atomX = atomRight;
  568. if (atom->isNewLine())
  569. {
  570. atomX = 0.0f;
  571. lineY += lineHeight;
  572. }
  573. }
  574. }
  575. bool shouldWrap (const float x) const noexcept
  576. {
  577. return (x - 0.0001f) >= wordWrapWidth;
  578. }
  579. JUCE_LEAK_DETECTOR (Iterator)
  580. };
  581. //==============================================================================
  582. class TextEditor::InsertAction : public UndoableAction
  583. {
  584. public:
  585. InsertAction (TextEditor& ed,
  586. const String& newText,
  587. const int insertPos,
  588. const Font& newFont,
  589. const Colour newColour,
  590. const int oldCaret,
  591. const int newCaret)
  592. : owner (ed),
  593. text (newText),
  594. insertIndex (insertPos),
  595. oldCaretPos (oldCaret),
  596. newCaretPos (newCaret),
  597. font (newFont),
  598. colour (newColour)
  599. {
  600. }
  601. bool perform()
  602. {
  603. owner.insert (text, insertIndex, font, colour, 0, newCaretPos);
  604. return true;
  605. }
  606. bool undo()
  607. {
  608. owner.remove (Range<int> (insertIndex, insertIndex + text.length()), 0, oldCaretPos);
  609. return true;
  610. }
  611. int getSizeInUnits()
  612. {
  613. return text.length() + 16;
  614. }
  615. private:
  616. TextEditor& owner;
  617. const String text;
  618. const int insertIndex, oldCaretPos, newCaretPos;
  619. const Font font;
  620. const Colour colour;
  621. JUCE_DECLARE_NON_COPYABLE (InsertAction)
  622. };
  623. //==============================================================================
  624. class TextEditor::RemoveAction : public UndoableAction
  625. {
  626. public:
  627. RemoveAction (TextEditor& ed,
  628. const Range<int> rangeToRemove,
  629. const int oldCaret,
  630. const int newCaret,
  631. const Array <UniformTextSection*>& oldSections)
  632. : owner (ed),
  633. range (rangeToRemove),
  634. oldCaretPos (oldCaret),
  635. newCaretPos (newCaret),
  636. removedSections (oldSections)
  637. {
  638. }
  639. ~RemoveAction()
  640. {
  641. for (int i = removedSections.size(); --i >= 0;)
  642. {
  643. ScopedPointer<UniformTextSection> section (removedSections.getUnchecked (i));
  644. section->clear();
  645. }
  646. }
  647. bool perform()
  648. {
  649. owner.remove (range, 0, newCaretPos);
  650. return true;
  651. }
  652. bool undo()
  653. {
  654. owner.reinsert (range.getStart(), removedSections);
  655. owner.moveCaretTo (oldCaretPos, false);
  656. return true;
  657. }
  658. int getSizeInUnits()
  659. {
  660. int n = 16;
  661. for (int i = removedSections.size(); --i >= 0;)
  662. n += removedSections.getUnchecked (i)->getTotalLength();
  663. return n;
  664. }
  665. private:
  666. TextEditor& owner;
  667. const Range<int> range;
  668. const int oldCaretPos, newCaretPos;
  669. Array <UniformTextSection*> removedSections;
  670. JUCE_DECLARE_NON_COPYABLE (RemoveAction)
  671. };
  672. //==============================================================================
  673. class TextEditor::TextHolderComponent : public Component,
  674. public Timer,
  675. public ValueListener
  676. {
  677. public:
  678. TextHolderComponent (TextEditor& ed) : owner (ed)
  679. {
  680. setWantsKeyboardFocus (false);
  681. setInterceptsMouseClicks (false, true);
  682. setMouseCursor (MouseCursor::ParentCursor);
  683. owner.getTextValue().addListener (this);
  684. }
  685. ~TextHolderComponent()
  686. {
  687. owner.getTextValue().removeListener (this);
  688. }
  689. void paint (Graphics& g)
  690. {
  691. owner.drawContent (g);
  692. }
  693. void restartTimer()
  694. {
  695. startTimer (350);
  696. }
  697. void timerCallback()
  698. {
  699. owner.timerCallbackInt();
  700. }
  701. void valueChanged (Value&)
  702. {
  703. owner.textWasChangedByValue();
  704. }
  705. private:
  706. TextEditor& owner;
  707. JUCE_DECLARE_NON_COPYABLE (TextHolderComponent)
  708. };
  709. //==============================================================================
  710. class TextEditorViewport : public Viewport
  711. {
  712. public:
  713. TextEditorViewport (TextEditor& ed)
  714. : owner (ed), lastWordWrapWidth (0), rentrant (false)
  715. {
  716. }
  717. void visibleAreaChanged (const Rectangle<int>&)
  718. {
  719. if (! rentrant) // it's rare, but possible to get into a feedback loop as the viewport's scrollbars
  720. // appear and disappear, causing the wrap width to change.
  721. {
  722. const float wordWrapWidth = owner.getWordWrapWidth();
  723. if (wordWrapWidth != lastWordWrapWidth)
  724. {
  725. lastWordWrapWidth = wordWrapWidth;
  726. rentrant = true;
  727. owner.updateTextHolderSize();
  728. rentrant = false;
  729. }
  730. }
  731. }
  732. private:
  733. TextEditor& owner;
  734. float lastWordWrapWidth;
  735. bool rentrant;
  736. JUCE_DECLARE_NON_COPYABLE (TextEditorViewport)
  737. };
  738. //==============================================================================
  739. namespace TextEditorDefs
  740. {
  741. const int textChangeMessageId = 0x10003001;
  742. const int returnKeyMessageId = 0x10003002;
  743. const int escapeKeyMessageId = 0x10003003;
  744. const int focusLossMessageId = 0x10003004;
  745. const int maxActionsPerTransaction = 100;
  746. static int getCharacterCategory (const juce_wchar character)
  747. {
  748. return CharacterFunctions::isLetterOrDigit (character)
  749. ? 2 : (CharacterFunctions::isWhitespace (character) ? 0 : 1);
  750. }
  751. }
  752. //==============================================================================
  753. TextEditor::TextEditor (const String& name,
  754. const juce_wchar passwordChar)
  755. : Component (name),
  756. borderSize (1, 1, 1, 3),
  757. readOnly (false),
  758. multiline (false),
  759. wordWrap (false),
  760. returnKeyStartsNewLine (false),
  761. popupMenuEnabled (true),
  762. selectAllTextWhenFocused (false),
  763. scrollbarVisible (true),
  764. wasFocused (false),
  765. keepCaretOnScreen (true),
  766. tabKeyUsed (false),
  767. menuActive (false),
  768. valueTextNeedsUpdating (false),
  769. consumeEscAndReturnKeys (true),
  770. leftIndent (4),
  771. topIndent (4),
  772. lastTransactionTime (0),
  773. currentFont (14.0f),
  774. totalNumChars (0),
  775. caretPosition (0),
  776. passwordCharacter (passwordChar),
  777. dragType (notDragging)
  778. {
  779. setOpaque (true);
  780. setMouseCursor (MouseCursor::IBeamCursor);
  781. addAndMakeVisible (viewport = new TextEditorViewport (*this));
  782. viewport->setViewedComponent (textHolder = new TextHolderComponent (*this));
  783. viewport->setWantsKeyboardFocus (false);
  784. viewport->setScrollBarsShown (false, false);
  785. setWantsKeyboardFocus (true);
  786. setCaretVisible (true);
  787. }
  788. TextEditor::~TextEditor()
  789. {
  790. if (wasFocused)
  791. if (ComponentPeer* const peer = getPeer())
  792. peer->dismissPendingTextInput();
  793. textValue.removeListener (textHolder);
  794. textValue.referTo (Value());
  795. clearInternal (0);
  796. viewport = nullptr;
  797. textHolder = nullptr;
  798. }
  799. //==============================================================================
  800. void TextEditor::newTransaction()
  801. {
  802. lastTransactionTime = Time::getApproximateMillisecondCounter();
  803. undoManager.beginNewTransaction();
  804. }
  805. bool TextEditor::undoOrRedo (const bool shouldUndo)
  806. {
  807. if (! isReadOnly())
  808. {
  809. newTransaction();
  810. if (shouldUndo ? undoManager.undo()
  811. : undoManager.redo())
  812. {
  813. scrollToMakeSureCursorIsVisible();
  814. repaint();
  815. textChanged();
  816. return true;
  817. }
  818. }
  819. return false;
  820. }
  821. bool TextEditor::undo() { return undoOrRedo (true); }
  822. bool TextEditor::redo() { return undoOrRedo (false); }
  823. //==============================================================================
  824. void TextEditor::setMultiLine (const bool shouldBeMultiLine,
  825. const bool shouldWordWrap)
  826. {
  827. if (multiline != shouldBeMultiLine
  828. || wordWrap != (shouldWordWrap && shouldBeMultiLine))
  829. {
  830. multiline = shouldBeMultiLine;
  831. wordWrap = shouldWordWrap && shouldBeMultiLine;
  832. viewport->setScrollBarsShown (scrollbarVisible && multiline,
  833. scrollbarVisible && multiline);
  834. viewport->setViewPosition (0, 0);
  835. resized();
  836. scrollToMakeSureCursorIsVisible();
  837. }
  838. }
  839. bool TextEditor::isMultiLine() const
  840. {
  841. return multiline;
  842. }
  843. void TextEditor::setScrollbarsShown (bool shown)
  844. {
  845. if (scrollbarVisible != shown)
  846. {
  847. scrollbarVisible = shown;
  848. shown = shown && isMultiLine();
  849. viewport->setScrollBarsShown (shown, shown);
  850. }
  851. }
  852. void TextEditor::setReadOnly (const bool shouldBeReadOnly)
  853. {
  854. if (readOnly != shouldBeReadOnly)
  855. {
  856. readOnly = shouldBeReadOnly;
  857. enablementChanged();
  858. }
  859. }
  860. bool TextEditor::isReadOnly() const
  861. {
  862. return readOnly || ! isEnabled();
  863. }
  864. bool TextEditor::isTextInputActive() const
  865. {
  866. return ! isReadOnly();
  867. }
  868. void TextEditor::setReturnKeyStartsNewLine (const bool shouldStartNewLine)
  869. {
  870. returnKeyStartsNewLine = shouldStartNewLine;
  871. }
  872. void TextEditor::setTabKeyUsedAsCharacter (const bool shouldTabKeyBeUsed)
  873. {
  874. tabKeyUsed = shouldTabKeyBeUsed;
  875. }
  876. void TextEditor::setPopupMenuEnabled (const bool b)
  877. {
  878. popupMenuEnabled = b;
  879. }
  880. void TextEditor::setSelectAllWhenFocused (const bool b)
  881. {
  882. selectAllTextWhenFocused = b;
  883. }
  884. //==============================================================================
  885. void TextEditor::setFont (const Font& newFont)
  886. {
  887. currentFont = newFont;
  888. scrollToMakeSureCursorIsVisible();
  889. }
  890. void TextEditor::applyFontToAllText (const Font& newFont)
  891. {
  892. currentFont = newFont;
  893. const Colour overallColour (findColour (textColourId));
  894. for (int i = sections.size(); --i >= 0;)
  895. {
  896. UniformTextSection* const uts = sections.getUnchecked (i);
  897. uts->setFont (newFont, passwordCharacter);
  898. uts->colour = overallColour;
  899. }
  900. coalesceSimilarSections();
  901. updateTextHolderSize();
  902. scrollToMakeSureCursorIsVisible();
  903. repaint();
  904. }
  905. void TextEditor::colourChanged()
  906. {
  907. setOpaque (findColour (backgroundColourId).isOpaque());
  908. repaint();
  909. }
  910. void TextEditor::lookAndFeelChanged()
  911. {
  912. if (isCaretVisible())
  913. {
  914. setCaretVisible (false);
  915. setCaretVisible (true);
  916. updateCaretPosition();
  917. }
  918. }
  919. void TextEditor::setCaretVisible (const bool shouldCaretBeVisible)
  920. {
  921. if (shouldCaretBeVisible && ! isReadOnly())
  922. {
  923. if (caret == nullptr)
  924. textHolder->addChildComponent (caret = getLookAndFeel().createCaretComponent (this));
  925. }
  926. else
  927. {
  928. caret = nullptr;
  929. }
  930. }
  931. void TextEditor::updateCaretPosition()
  932. {
  933. if (caret != nullptr)
  934. caret->setCaretPosition (getCaretRectangle().translated (leftIndent, topIndent));
  935. }
  936. TextEditor::LengthAndCharacterRestriction::LengthAndCharacterRestriction (int maxLen, const String& chars)
  937. : allowedCharacters (chars), maxLength (maxLen)
  938. {}
  939. String TextEditor::LengthAndCharacterRestriction::filterNewText (TextEditor& ed, const String& newInput)
  940. {
  941. String t (newInput);
  942. if (allowedCharacters.isNotEmpty())
  943. t = t.retainCharacters (allowedCharacters);
  944. if (maxLength > 0)
  945. t = t.substring (0, maxLength - (ed.getTotalNumChars() - ed.getHighlightedRegion().getLength()));
  946. return t;
  947. }
  948. void TextEditor::setInputFilter (InputFilter* newFilter, bool takeOwnership)
  949. {
  950. inputFilter.set (newFilter, takeOwnership);
  951. }
  952. void TextEditor::setInputRestrictions (const int maxLen,
  953. const String& chars)
  954. {
  955. setInputFilter (new LengthAndCharacterRestriction (maxLen, chars), true);
  956. }
  957. void TextEditor::setTextToShowWhenEmpty (const String& text, const Colour& colourToUse)
  958. {
  959. textToShowWhenEmpty = text;
  960. colourForTextWhenEmpty = colourToUse;
  961. }
  962. void TextEditor::setPasswordCharacter (const juce_wchar newPasswordCharacter)
  963. {
  964. if (passwordCharacter != newPasswordCharacter)
  965. {
  966. passwordCharacter = newPasswordCharacter;
  967. applyFontToAllText (currentFont);
  968. }
  969. }
  970. void TextEditor::setScrollBarThickness (const int newThicknessPixels)
  971. {
  972. viewport->setScrollBarThickness (newThicknessPixels);
  973. }
  974. //==============================================================================
  975. void TextEditor::clear()
  976. {
  977. clearInternal (0);
  978. updateTextHolderSize();
  979. undoManager.clearUndoHistory();
  980. }
  981. void TextEditor::setText (const String& newText,
  982. const bool sendTextChangeMessage)
  983. {
  984. const int newLength = newText.length();
  985. if (newLength != getTotalNumChars() || getText() != newText)
  986. {
  987. textValue = newText;
  988. int oldCursorPos = caretPosition;
  989. const bool cursorWasAtEnd = oldCursorPos >= getTotalNumChars();
  990. clearInternal (0);
  991. insert (newText, 0, currentFont, findColour (textColourId), 0, caretPosition);
  992. // if you're adding text with line-feeds to a single-line text editor, it
  993. // ain't gonna look right!
  994. jassert (multiline || ! newText.containsAnyOf ("\r\n"));
  995. if (cursorWasAtEnd && ! isMultiLine())
  996. oldCursorPos = getTotalNumChars();
  997. moveCaretTo (oldCursorPos, false);
  998. if (sendTextChangeMessage)
  999. textChanged();
  1000. updateTextHolderSize();
  1001. scrollToMakeSureCursorIsVisible();
  1002. undoManager.clearUndoHistory();
  1003. repaint();
  1004. }
  1005. }
  1006. //==============================================================================
  1007. void TextEditor::updateValueFromText()
  1008. {
  1009. if (valueTextNeedsUpdating)
  1010. {
  1011. valueTextNeedsUpdating = false;
  1012. textValue = getText();
  1013. }
  1014. }
  1015. Value& TextEditor::getTextValue()
  1016. {
  1017. updateValueFromText();
  1018. return textValue;
  1019. }
  1020. void TextEditor::textWasChangedByValue()
  1021. {
  1022. if (textValue.getValueSource().getReferenceCount() > 1)
  1023. setText (textValue.getValue());
  1024. }
  1025. //==============================================================================
  1026. void TextEditor::textChanged()
  1027. {
  1028. updateTextHolderSize();
  1029. if (listeners.size() > 0)
  1030. postCommandMessage (TextEditorDefs::textChangeMessageId);
  1031. if (textValue.getValueSource().getReferenceCount() > 1)
  1032. {
  1033. valueTextNeedsUpdating = false;
  1034. textValue = getText();
  1035. }
  1036. }
  1037. void TextEditor::returnPressed() { postCommandMessage (TextEditorDefs::returnKeyMessageId); }
  1038. void TextEditor::escapePressed() { postCommandMessage (TextEditorDefs::escapeKeyMessageId); }
  1039. void TextEditor::addListener (TextEditorListener* const l) { listeners.add (l); }
  1040. void TextEditor::removeListener (TextEditorListener* const l) { listeners.remove (l); }
  1041. //==============================================================================
  1042. void TextEditor::timerCallbackInt()
  1043. {
  1044. if (hasKeyboardFocus (false) && ! isCurrentlyBlockedByAnotherModalComponent())
  1045. wasFocused = true;
  1046. const unsigned int now = Time::getApproximateMillisecondCounter();
  1047. if (now > lastTransactionTime + 200)
  1048. newTransaction();
  1049. }
  1050. void TextEditor::repaintText (const Range<int> range)
  1051. {
  1052. if (! range.isEmpty())
  1053. {
  1054. float x = 0, y = 0, lh = currentFont.getHeight();
  1055. const float wordWrapWidth = getWordWrapWidth();
  1056. if (wordWrapWidth > 0)
  1057. {
  1058. Iterator i (sections, wordWrapWidth, passwordCharacter);
  1059. i.getCharPosition (range.getStart(), x, y, lh);
  1060. const int y1 = (int) y;
  1061. int y2;
  1062. if (range.getEnd() >= getTotalNumChars())
  1063. {
  1064. y2 = textHolder->getHeight();
  1065. }
  1066. else
  1067. {
  1068. i.getCharPosition (range.getEnd(), x, y, lh);
  1069. y2 = (int) (y + lh * 2.0f);
  1070. }
  1071. textHolder->repaint (0, y1, textHolder->getWidth(), y2 - y1);
  1072. }
  1073. }
  1074. }
  1075. //==============================================================================
  1076. void TextEditor::moveCaret (int newCaretPos)
  1077. {
  1078. if (newCaretPos < 0)
  1079. newCaretPos = 0;
  1080. else if (newCaretPos > getTotalNumChars())
  1081. newCaretPos = getTotalNumChars();
  1082. if (newCaretPos != getCaretPosition())
  1083. {
  1084. caretPosition = newCaretPos;
  1085. textHolder->restartTimer();
  1086. scrollToMakeSureCursorIsVisible();
  1087. updateCaretPosition();
  1088. }
  1089. }
  1090. int TextEditor::getCaretPosition() const
  1091. {
  1092. return caretPosition;
  1093. }
  1094. void TextEditor::setCaretPosition (const int newIndex)
  1095. {
  1096. moveCaretTo (newIndex, false);
  1097. }
  1098. void TextEditor::moveCaretToEnd()
  1099. {
  1100. moveCaretTo (std::numeric_limits<int>::max(), false);
  1101. }
  1102. void TextEditor::scrollEditorToPositionCaret (const int desiredCaretX,
  1103. const int desiredCaretY)
  1104. {
  1105. updateCaretPosition();
  1106. const Rectangle<int> caretPos (getCaretRectangle());
  1107. int vx = caretPos.getX() - desiredCaretX;
  1108. int vy = caretPos.getY() - desiredCaretY;
  1109. if (desiredCaretX < jmax (1, proportionOfWidth (0.05f)))
  1110. vx += desiredCaretX - proportionOfWidth (0.2f);
  1111. else if (desiredCaretX > jmax (0, viewport->getMaximumVisibleWidth() - (wordWrap ? 2 : 10)))
  1112. vx += desiredCaretX + (isMultiLine() ? proportionOfWidth (0.2f) : 10) - viewport->getMaximumVisibleWidth();
  1113. vx = jlimit (0, jmax (0, textHolder->getWidth() + 8 - viewport->getMaximumVisibleWidth()), vx);
  1114. if (! isMultiLine())
  1115. {
  1116. vy = viewport->getViewPositionY();
  1117. }
  1118. else
  1119. {
  1120. vy = jlimit (0, jmax (0, textHolder->getHeight() - viewport->getMaximumVisibleHeight()), vy);
  1121. if (desiredCaretY < 0)
  1122. vy = jmax (0, desiredCaretY + vy);
  1123. else if (desiredCaretY > jmax (0, viewport->getMaximumVisibleHeight() - topIndent - caretPos.getHeight()))
  1124. vy += desiredCaretY + 2 + caretPos.getHeight() + topIndent - viewport->getMaximumVisibleHeight();
  1125. }
  1126. viewport->setViewPosition (vx, vy);
  1127. }
  1128. Rectangle<int> TextEditor::getCaretRectangle()
  1129. {
  1130. float cursorX, cursorY;
  1131. float cursorHeight = currentFont.getHeight(); // (in case the text is empty and the call below doesn't set this value)
  1132. getCharPosition (caretPosition, cursorX, cursorY, cursorHeight);
  1133. return Rectangle<int> (roundToInt (cursorX), roundToInt (cursorY), 2, roundToInt (cursorHeight));
  1134. }
  1135. //==============================================================================
  1136. enum { rightEdgeSpace = 2 };
  1137. float TextEditor::getWordWrapWidth() const
  1138. {
  1139. return wordWrap ? (float) (viewport->getMaximumVisibleWidth() - (leftIndent + rightEdgeSpace + 1))
  1140. : std::numeric_limits<float>::max();
  1141. }
  1142. void TextEditor::updateTextHolderSize()
  1143. {
  1144. const float wordWrapWidth = getWordWrapWidth();
  1145. if (wordWrapWidth > 0)
  1146. {
  1147. float maxWidth = 0.0f;
  1148. Iterator i (sections, wordWrapWidth, passwordCharacter);
  1149. while (i.next())
  1150. maxWidth = jmax (maxWidth, i.atomRight);
  1151. const int w = leftIndent + roundToInt (maxWidth);
  1152. const int h = topIndent + roundToInt (jmax (i.lineY + i.lineHeight,
  1153. currentFont.getHeight()));
  1154. textHolder->setSize (w + rightEdgeSpace, h + 1); // (allows a bit of space for the cursor to be at the right-hand-edge)
  1155. }
  1156. }
  1157. int TextEditor::getTextWidth() const { return textHolder->getWidth(); }
  1158. int TextEditor::getTextHeight() const { return textHolder->getHeight(); }
  1159. void TextEditor::setIndents (const int newLeftIndent, const int newTopIndent)
  1160. {
  1161. leftIndent = newLeftIndent;
  1162. topIndent = newTopIndent;
  1163. }
  1164. void TextEditor::setBorder (const BorderSize<int>& border)
  1165. {
  1166. borderSize = border;
  1167. resized();
  1168. }
  1169. BorderSize<int> TextEditor::getBorder() const
  1170. {
  1171. return borderSize;
  1172. }
  1173. void TextEditor::setScrollToShowCursor (const bool shouldScrollToShowCursor)
  1174. {
  1175. keepCaretOnScreen = shouldScrollToShowCursor;
  1176. }
  1177. void TextEditor::scrollToMakeSureCursorIsVisible()
  1178. {
  1179. updateCaretPosition();
  1180. if (keepCaretOnScreen)
  1181. {
  1182. int x = viewport->getViewPositionX();
  1183. int y = viewport->getViewPositionY();
  1184. const Rectangle<int> caretPos (getCaretRectangle());
  1185. const int relativeCursorX = caretPos.getX() - x;
  1186. const int relativeCursorY = caretPos.getY() - y;
  1187. if (relativeCursorX < jmax (1, proportionOfWidth (0.05f)))
  1188. {
  1189. x += relativeCursorX - proportionOfWidth (0.2f);
  1190. }
  1191. else if (relativeCursorX > jmax (0, viewport->getMaximumVisibleWidth() - (wordWrap ? 2 : 10)))
  1192. {
  1193. x += relativeCursorX + (isMultiLine() ? proportionOfWidth (0.2f) : 10) - viewport->getMaximumVisibleWidth();
  1194. }
  1195. x = jlimit (0, jmax (0, textHolder->getWidth() + 8 - viewport->getMaximumVisibleWidth()), x);
  1196. if (! isMultiLine())
  1197. {
  1198. y = (getHeight() - textHolder->getHeight() - topIndent) / -2;
  1199. }
  1200. else
  1201. {
  1202. if (relativeCursorY < 0)
  1203. {
  1204. y = jmax (0, relativeCursorY + y);
  1205. }
  1206. else if (relativeCursorY > jmax (0, viewport->getMaximumVisibleHeight() - topIndent - caretPos.getHeight()))
  1207. {
  1208. y += relativeCursorY + 2 + caretPos.getHeight() + topIndent - viewport->getMaximumVisibleHeight();
  1209. }
  1210. }
  1211. viewport->setViewPosition (x, y);
  1212. }
  1213. }
  1214. void TextEditor::moveCaretTo (const int newPosition,
  1215. const bool isSelecting)
  1216. {
  1217. if (isSelecting)
  1218. {
  1219. moveCaret (newPosition);
  1220. const Range<int> oldSelection (selection);
  1221. if (dragType == notDragging)
  1222. {
  1223. if (abs (getCaretPosition() - selection.getStart()) < abs (getCaretPosition() - selection.getEnd()))
  1224. dragType = draggingSelectionStart;
  1225. else
  1226. dragType = draggingSelectionEnd;
  1227. }
  1228. if (dragType == draggingSelectionStart)
  1229. {
  1230. if (getCaretPosition() >= selection.getEnd())
  1231. dragType = draggingSelectionEnd;
  1232. selection = Range<int>::between (getCaretPosition(), selection.getEnd());
  1233. }
  1234. else
  1235. {
  1236. if (getCaretPosition() < selection.getStart())
  1237. dragType = draggingSelectionStart;
  1238. selection = Range<int>::between (getCaretPosition(), selection.getStart());
  1239. }
  1240. repaintText (selection.getUnionWith (oldSelection));
  1241. }
  1242. else
  1243. {
  1244. dragType = notDragging;
  1245. repaintText (selection);
  1246. moveCaret (newPosition);
  1247. selection = Range<int>::emptyRange (getCaretPosition());
  1248. }
  1249. }
  1250. int TextEditor::getTextIndexAt (const int x, const int y)
  1251. {
  1252. return indexAtPosition ((float) (x + viewport->getViewPositionX() - leftIndent),
  1253. (float) (y + viewport->getViewPositionY() - topIndent));
  1254. }
  1255. void TextEditor::insertTextAtCaret (const String& t)
  1256. {
  1257. String newText (inputFilter != nullptr ? inputFilter->filterNewText (*this, t) : t);
  1258. if (isMultiLine())
  1259. newText = newText.replace ("\r\n", "\n");
  1260. else
  1261. newText = newText.replaceCharacters ("\r\n", " ");
  1262. const int insertIndex = selection.getStart();
  1263. const int newCaretPos = insertIndex + newText.length();
  1264. remove (selection, getUndoManager(),
  1265. newText.isNotEmpty() ? newCaretPos - 1 : newCaretPos);
  1266. insert (newText, insertIndex, currentFont, findColour (textColourId),
  1267. getUndoManager(), newCaretPos);
  1268. textChanged();
  1269. }
  1270. void TextEditor::setHighlightedRegion (const Range<int>& newSelection)
  1271. {
  1272. moveCaretTo (newSelection.getStart(), false);
  1273. moveCaretTo (newSelection.getEnd(), true);
  1274. }
  1275. //==============================================================================
  1276. void TextEditor::copy()
  1277. {
  1278. if (passwordCharacter == 0)
  1279. {
  1280. const String selectedText (getHighlightedText());
  1281. if (selectedText.isNotEmpty())
  1282. SystemClipboard::copyTextToClipboard (selectedText);
  1283. }
  1284. }
  1285. void TextEditor::paste()
  1286. {
  1287. if (! isReadOnly())
  1288. {
  1289. const String clip (SystemClipboard::getTextFromClipboard());
  1290. if (clip.isNotEmpty())
  1291. insertTextAtCaret (clip);
  1292. }
  1293. }
  1294. void TextEditor::cut()
  1295. {
  1296. if (! isReadOnly())
  1297. {
  1298. moveCaret (selection.getEnd());
  1299. insertTextAtCaret (String::empty);
  1300. }
  1301. }
  1302. //==============================================================================
  1303. void TextEditor::drawContent (Graphics& g)
  1304. {
  1305. const float wordWrapWidth = getWordWrapWidth();
  1306. if (wordWrapWidth > 0)
  1307. {
  1308. g.setOrigin (leftIndent, topIndent);
  1309. const Rectangle<int> clip (g.getClipBounds());
  1310. Colour selectedTextColour;
  1311. Iterator i (sections, wordWrapWidth, passwordCharacter);
  1312. while (i.lineY + 200.0 < clip.getY() && i.next())
  1313. {}
  1314. if (! selection.isEmpty())
  1315. {
  1316. g.setColour (findColour (highlightColourId).withMultipliedAlpha (hasKeyboardFocus (true) ? 1.0f : 0.5f));
  1317. selectedTextColour = findColour (highlightedTextColourId);
  1318. Iterator i2 (i);
  1319. while (i2.next() && i2.lineY < clip.getBottom())
  1320. {
  1321. if (i2.lineY + i2.lineHeight >= clip.getY()
  1322. && selection.intersects (Range<int> (i2.indexInText, i2.indexInText + i2.atom->numChars)))
  1323. {
  1324. i2.drawSelection (g, selection);
  1325. }
  1326. }
  1327. }
  1328. const UniformTextSection* lastSection = nullptr;
  1329. while (i.next() && i.lineY < clip.getBottom())
  1330. {
  1331. if (i.lineY + i.lineHeight >= clip.getY())
  1332. {
  1333. if (selection.intersects (Range<int> (i.indexInText, i.indexInText + i.atom->numChars)))
  1334. {
  1335. i.drawSelectedText (g, selection, selectedTextColour);
  1336. lastSection = nullptr;
  1337. }
  1338. else
  1339. {
  1340. i.draw (g, lastSection);
  1341. }
  1342. }
  1343. }
  1344. for (int j = underlinedSections.size(); --j >= 0;)
  1345. {
  1346. const Range<int> underlinedSection = underlinedSections.getReference (j);
  1347. Iterator i2 (sections, wordWrapWidth, passwordCharacter);
  1348. while (i2.next() && i2.lineY < clip.getBottom())
  1349. {
  1350. if (i2.lineY + i2.lineHeight >= clip.getY()
  1351. && underlinedSection.intersects (Range<int> (i2.indexInText, i2.indexInText + i2.atom->numChars)))
  1352. {
  1353. i2.drawUnderline (g, underlinedSection, findColour (textColourId));
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. void TextEditor::paint (Graphics& g)
  1360. {
  1361. getLookAndFeel().fillTextEditorBackground (g, getWidth(), getHeight(), *this);
  1362. }
  1363. void TextEditor::paintOverChildren (Graphics& g)
  1364. {
  1365. if (textToShowWhenEmpty.isNotEmpty()
  1366. && (! hasKeyboardFocus (false))
  1367. && getTotalNumChars() == 0)
  1368. {
  1369. g.setColour (colourForTextWhenEmpty);
  1370. g.setFont (getFont());
  1371. if (isMultiLine())
  1372. g.drawText (textToShowWhenEmpty, getLocalBounds(),
  1373. Justification::centred, true);
  1374. else
  1375. g.drawText (textToShowWhenEmpty,
  1376. leftIndent, 0, viewport->getWidth() - leftIndent, getHeight(),
  1377. Justification::centredLeft, true);
  1378. }
  1379. getLookAndFeel().drawTextEditorOutline (g, getWidth(), getHeight(), *this);
  1380. }
  1381. //==============================================================================
  1382. void TextEditor::addPopupMenuItems (PopupMenu& m, const MouseEvent*)
  1383. {
  1384. const bool writable = ! isReadOnly();
  1385. if (passwordCharacter == 0)
  1386. {
  1387. m.addItem (StandardApplicationCommandIDs::cut, TRANS("Cut"), writable);
  1388. m.addItem (StandardApplicationCommandIDs::copy, TRANS("Copy"), ! selection.isEmpty());
  1389. m.addItem (StandardApplicationCommandIDs::paste, TRANS("Paste"), writable);
  1390. }
  1391. m.addItem (StandardApplicationCommandIDs::del, TRANS("Delete"), writable);
  1392. m.addSeparator();
  1393. m.addItem (StandardApplicationCommandIDs::selectAll, TRANS("Select All"));
  1394. m.addSeparator();
  1395. if (getUndoManager() != nullptr)
  1396. {
  1397. m.addItem (StandardApplicationCommandIDs::undo, TRANS("Undo"), undoManager.canUndo());
  1398. m.addItem (StandardApplicationCommandIDs::redo, TRANS("Redo"), undoManager.canRedo());
  1399. }
  1400. }
  1401. void TextEditor::performPopupMenuAction (const int menuItemID)
  1402. {
  1403. switch (menuItemID)
  1404. {
  1405. case StandardApplicationCommandIDs::cut: cutToClipboard(); break;
  1406. case StandardApplicationCommandIDs::copy: copyToClipboard(); break;
  1407. case StandardApplicationCommandIDs::paste: pasteFromClipboard(); break;
  1408. case StandardApplicationCommandIDs::del: cut(); break;
  1409. case StandardApplicationCommandIDs::selectAll: selectAll(); break;
  1410. case StandardApplicationCommandIDs::undo: undo(); break;
  1411. case StandardApplicationCommandIDs::redo: redo(); break;
  1412. default: break;
  1413. }
  1414. }
  1415. static void textEditorMenuCallback (int menuResult, TextEditor* editor)
  1416. {
  1417. if (editor != nullptr && menuResult != 0)
  1418. editor->performPopupMenuAction (menuResult);
  1419. }
  1420. //==============================================================================
  1421. void TextEditor::mouseDown (const MouseEvent& e)
  1422. {
  1423. beginDragAutoRepeat (100);
  1424. newTransaction();
  1425. if (wasFocused || ! selectAllTextWhenFocused)
  1426. {
  1427. if (! (popupMenuEnabled && e.mods.isPopupMenu()))
  1428. {
  1429. moveCaretTo (getTextIndexAt (e.x, e.y),
  1430. e.mods.isShiftDown());
  1431. }
  1432. else
  1433. {
  1434. PopupMenu m;
  1435. m.setLookAndFeel (&getLookAndFeel());
  1436. addPopupMenuItems (m, &e);
  1437. m.showMenuAsync (PopupMenu::Options(),
  1438. ModalCallbackFunction::forComponent (textEditorMenuCallback, this));
  1439. }
  1440. }
  1441. }
  1442. void TextEditor::mouseDrag (const MouseEvent& e)
  1443. {
  1444. if (wasFocused || ! selectAllTextWhenFocused)
  1445. if (! (popupMenuEnabled && e.mods.isPopupMenu()))
  1446. moveCaretTo (getTextIndexAt (e.x, e.y), true);
  1447. }
  1448. void TextEditor::mouseUp (const MouseEvent& e)
  1449. {
  1450. newTransaction();
  1451. textHolder->restartTimer();
  1452. if (wasFocused || ! selectAllTextWhenFocused)
  1453. if (e.mouseWasClicked() && ! (popupMenuEnabled && e.mods.isPopupMenu()))
  1454. moveCaret (getTextIndexAt (e.x, e.y));
  1455. wasFocused = true;
  1456. }
  1457. void TextEditor::mouseDoubleClick (const MouseEvent& e)
  1458. {
  1459. int tokenEnd = getTextIndexAt (e.x, e.y);
  1460. int tokenStart = 0;
  1461. if (e.getNumberOfClicks() > 3)
  1462. {
  1463. tokenEnd = getTotalNumChars();
  1464. }
  1465. else
  1466. {
  1467. const String t (getText());
  1468. const int totalLength = getTotalNumChars();
  1469. while (tokenEnd < totalLength)
  1470. {
  1471. // (note the slight bodge here - it's because iswalnum only checks for alphabetic chars in the current locale)
  1472. const juce_wchar c = t [tokenEnd];
  1473. if (CharacterFunctions::isLetterOrDigit (c) || c > 128)
  1474. ++tokenEnd;
  1475. else
  1476. break;
  1477. }
  1478. tokenStart = tokenEnd;
  1479. while (tokenStart > 0)
  1480. {
  1481. // (note the slight bodge here - it's because iswalnum only checks for alphabetic chars in the current locale)
  1482. const juce_wchar c = t [tokenStart - 1];
  1483. if (CharacterFunctions::isLetterOrDigit (c) || c > 128)
  1484. --tokenStart;
  1485. else
  1486. break;
  1487. }
  1488. if (e.getNumberOfClicks() > 2)
  1489. {
  1490. while (tokenEnd < totalLength)
  1491. {
  1492. const juce_wchar c = t [tokenEnd];
  1493. if (c != '\r' && c != '\n')
  1494. ++tokenEnd;
  1495. else
  1496. break;
  1497. }
  1498. while (tokenStart > 0)
  1499. {
  1500. const juce_wchar c = t [tokenStart - 1];
  1501. if (c != '\r' && c != '\n')
  1502. --tokenStart;
  1503. else
  1504. break;
  1505. }
  1506. }
  1507. }
  1508. moveCaretTo (tokenEnd, false);
  1509. moveCaretTo (tokenStart, true);
  1510. }
  1511. void TextEditor::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  1512. {
  1513. if (! viewport->useMouseWheelMoveIfNeeded (e, wheel))
  1514. Component::mouseWheelMove (e, wheel);
  1515. }
  1516. //==============================================================================
  1517. bool TextEditor::moveCaretWithTransaction (const int newPos, const bool selecting)
  1518. {
  1519. newTransaction();
  1520. moveCaretTo (newPos, selecting);
  1521. return true;
  1522. }
  1523. bool TextEditor::moveCaretLeft (bool moveInWholeWordSteps, bool selecting)
  1524. {
  1525. int pos = getCaretPosition();
  1526. if (moveInWholeWordSteps)
  1527. pos = findWordBreakBefore (pos);
  1528. else
  1529. --pos;
  1530. return moveCaretWithTransaction (pos, selecting);
  1531. }
  1532. bool TextEditor::moveCaretRight (bool moveInWholeWordSteps, bool selecting)
  1533. {
  1534. int pos = getCaretPosition();
  1535. if (moveInWholeWordSteps)
  1536. pos = findWordBreakAfter (pos);
  1537. else
  1538. ++pos;
  1539. return moveCaretWithTransaction (pos, selecting);
  1540. }
  1541. bool TextEditor::moveCaretUp (bool selecting)
  1542. {
  1543. if (! isMultiLine())
  1544. return moveCaretToStartOfLine (selecting);
  1545. const Rectangle<float> caretPos (getCaretRectangle().toFloat());
  1546. return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - 1.0f), selecting);
  1547. }
  1548. bool TextEditor::moveCaretDown (bool selecting)
  1549. {
  1550. if (! isMultiLine())
  1551. return moveCaretToEndOfLine (selecting);
  1552. const Rectangle<float> caretPos (getCaretRectangle().toFloat());
  1553. return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + 1.0f), selecting);
  1554. }
  1555. bool TextEditor::pageUp (bool selecting)
  1556. {
  1557. if (! isMultiLine())
  1558. return moveCaretToStartOfLine (selecting);
  1559. const Rectangle<float> caretPos (getCaretRectangle().toFloat());
  1560. return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - viewport->getViewHeight()), selecting);
  1561. }
  1562. bool TextEditor::pageDown (bool selecting)
  1563. {
  1564. if (! isMultiLine())
  1565. return moveCaretToEndOfLine (selecting);
  1566. const Rectangle<float> caretPos (getCaretRectangle().toFloat());
  1567. return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + viewport->getViewHeight()), selecting);
  1568. }
  1569. void TextEditor::scrollByLines (int deltaLines)
  1570. {
  1571. if (ScrollBar* scrollbar = viewport->getVerticalScrollBar())
  1572. scrollbar->moveScrollbarInSteps (deltaLines);
  1573. }
  1574. bool TextEditor::scrollDown()
  1575. {
  1576. scrollByLines (-1);
  1577. return true;
  1578. }
  1579. bool TextEditor::scrollUp()
  1580. {
  1581. scrollByLines (1);
  1582. return true;
  1583. }
  1584. bool TextEditor::moveCaretToTop (bool selecting)
  1585. {
  1586. return moveCaretWithTransaction (0, selecting);
  1587. }
  1588. bool TextEditor::moveCaretToStartOfLine (bool selecting)
  1589. {
  1590. const Rectangle<float> caretPos (getCaretRectangle().toFloat());
  1591. return moveCaretWithTransaction (indexAtPosition (0.0f, caretPos.getY()), selecting);
  1592. }
  1593. bool TextEditor::moveCaretToEnd (bool selecting)
  1594. {
  1595. return moveCaretWithTransaction (getTotalNumChars(), selecting);
  1596. }
  1597. bool TextEditor::moveCaretToEndOfLine (bool selecting)
  1598. {
  1599. const Rectangle<float> caretPos (getCaretRectangle().toFloat());
  1600. return moveCaretWithTransaction (indexAtPosition ((float) textHolder->getWidth(), caretPos.getY()), selecting);
  1601. }
  1602. bool TextEditor::deleteBackwards (bool moveInWholeWordSteps)
  1603. {
  1604. if (moveInWholeWordSteps)
  1605. moveCaretTo (findWordBreakBefore (getCaretPosition()), true);
  1606. else if (selection.isEmpty() && selection.getStart() > 0)
  1607. selection.setStart (selection.getEnd() - 1);
  1608. cut();
  1609. return true;
  1610. }
  1611. bool TextEditor::deleteForwards (bool /*moveInWholeWordSteps*/)
  1612. {
  1613. if (selection.isEmpty() && selection.getStart() < getTotalNumChars())
  1614. selection.setEnd (selection.getStart() + 1);
  1615. cut();
  1616. return true;
  1617. }
  1618. bool TextEditor::copyToClipboard()
  1619. {
  1620. newTransaction();
  1621. copy();
  1622. return true;
  1623. }
  1624. bool TextEditor::cutToClipboard()
  1625. {
  1626. newTransaction();
  1627. copy();
  1628. cut();
  1629. return true;
  1630. }
  1631. bool TextEditor::pasteFromClipboard()
  1632. {
  1633. newTransaction();
  1634. paste();
  1635. return true;
  1636. }
  1637. bool TextEditor::selectAll()
  1638. {
  1639. newTransaction();
  1640. moveCaretTo (getTotalNumChars(), false);
  1641. moveCaretTo (0, true);
  1642. return true;
  1643. }
  1644. //==============================================================================
  1645. void TextEditor::setEscapeAndReturnKeysConsumed (bool shouldBeConsumed) noexcept
  1646. {
  1647. consumeEscAndReturnKeys = shouldBeConsumed;
  1648. }
  1649. bool TextEditor::keyPressed (const KeyPress& key)
  1650. {
  1651. if (isReadOnly() && key != KeyPress ('c', ModifierKeys::commandModifier, 0))
  1652. return false;
  1653. if (! TextEditorKeyMapper<TextEditor>::invokeKeyFunction (*this, key))
  1654. {
  1655. if (key == KeyPress::returnKey)
  1656. {
  1657. newTransaction();
  1658. if (returnKeyStartsNewLine)
  1659. insertTextAtCaret ("\n");
  1660. else
  1661. {
  1662. returnPressed();
  1663. return consumeEscAndReturnKeys;
  1664. }
  1665. }
  1666. else if (key.isKeyCode (KeyPress::escapeKey))
  1667. {
  1668. newTransaction();
  1669. moveCaretTo (getCaretPosition(), false);
  1670. escapePressed();
  1671. return consumeEscAndReturnKeys;
  1672. }
  1673. else if (key.getTextCharacter() >= ' '
  1674. || (tabKeyUsed && (key.getTextCharacter() == '\t')))
  1675. {
  1676. insertTextAtCaret (String::charToString (key.getTextCharacter()));
  1677. lastTransactionTime = Time::getApproximateMillisecondCounter();
  1678. }
  1679. else
  1680. {
  1681. return false;
  1682. }
  1683. }
  1684. return true;
  1685. }
  1686. bool TextEditor::keyStateChanged (const bool isKeyDown)
  1687. {
  1688. if (! isKeyDown)
  1689. return false;
  1690. #if JUCE_WINDOWS
  1691. if (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0).isCurrentlyDown())
  1692. return false; // We need to explicitly allow alt-F4 to pass through on Windows
  1693. #endif
  1694. if ((! consumeEscAndReturnKeys)
  1695. && (KeyPress (KeyPress::escapeKey).isCurrentlyDown()
  1696. || KeyPress (KeyPress::returnKey).isCurrentlyDown()))
  1697. return false;
  1698. // (overridden to avoid forwarding key events to the parent)
  1699. return ! ModifierKeys::getCurrentModifiers().isCommandDown();
  1700. }
  1701. //==============================================================================
  1702. void TextEditor::focusGained (FocusChangeType)
  1703. {
  1704. newTransaction();
  1705. if (selectAllTextWhenFocused)
  1706. {
  1707. moveCaretTo (0, false);
  1708. moveCaretTo (getTotalNumChars(), true);
  1709. }
  1710. repaint();
  1711. updateCaretPosition();
  1712. if (ComponentPeer* const peer = getPeer())
  1713. if (! isReadOnly())
  1714. peer->textInputRequired (peer->globalToLocal (getScreenPosition()));
  1715. }
  1716. void TextEditor::focusLost (FocusChangeType)
  1717. {
  1718. newTransaction();
  1719. wasFocused = false;
  1720. textHolder->stopTimer();
  1721. underlinedSections.clear();
  1722. if (ComponentPeer* const peer = getPeer())
  1723. peer->dismissPendingTextInput();
  1724. updateCaretPosition();
  1725. postCommandMessage (TextEditorDefs::focusLossMessageId);
  1726. repaint();
  1727. }
  1728. //==============================================================================
  1729. void TextEditor::resized()
  1730. {
  1731. viewport->setBoundsInset (borderSize);
  1732. viewport->setSingleStepSizes (16, roundToInt (currentFont.getHeight()));
  1733. updateTextHolderSize();
  1734. if (isMultiLine())
  1735. updateCaretPosition();
  1736. else
  1737. scrollToMakeSureCursorIsVisible();
  1738. }
  1739. void TextEditor::handleCommandMessage (const int commandId)
  1740. {
  1741. Component::BailOutChecker checker (this);
  1742. switch (commandId)
  1743. {
  1744. case TextEditorDefs::textChangeMessageId:
  1745. listeners.callChecked (checker, &TextEditorListener::textEditorTextChanged, (TextEditor&) *this);
  1746. break;
  1747. case TextEditorDefs::returnKeyMessageId:
  1748. listeners.callChecked (checker, &TextEditorListener::textEditorReturnKeyPressed, (TextEditor&) *this);
  1749. break;
  1750. case TextEditorDefs::escapeKeyMessageId:
  1751. listeners.callChecked (checker, &TextEditorListener::textEditorEscapeKeyPressed, (TextEditor&) *this);
  1752. break;
  1753. case TextEditorDefs::focusLossMessageId:
  1754. updateValueFromText();
  1755. listeners.callChecked (checker, &TextEditorListener::textEditorFocusLost, (TextEditor&) *this);
  1756. break;
  1757. default:
  1758. jassertfalse;
  1759. break;
  1760. }
  1761. }
  1762. void TextEditor::enablementChanged()
  1763. {
  1764. repaint();
  1765. }
  1766. void TextEditor::setTemporaryUnderlining (const Array <Range<int> >& newUnderlinedSections)
  1767. {
  1768. underlinedSections = newUnderlinedSections;
  1769. repaint();
  1770. }
  1771. //==============================================================================
  1772. UndoManager* TextEditor::getUndoManager() noexcept
  1773. {
  1774. return readOnly ? nullptr : &undoManager;
  1775. }
  1776. void TextEditor::clearInternal (UndoManager* const um)
  1777. {
  1778. remove (Range<int> (0, getTotalNumChars()), um, caretPosition);
  1779. }
  1780. void TextEditor::insert (const String& text,
  1781. const int insertIndex,
  1782. const Font& font,
  1783. const Colour colour,
  1784. UndoManager* const um,
  1785. const int caretPositionToMoveTo)
  1786. {
  1787. if (text.isNotEmpty())
  1788. {
  1789. if (um != nullptr)
  1790. {
  1791. if (um->getNumActionsInCurrentTransaction() > TextEditorDefs::maxActionsPerTransaction)
  1792. newTransaction();
  1793. um->perform (new InsertAction (*this, text, insertIndex, font, colour,
  1794. caretPosition, caretPositionToMoveTo));
  1795. }
  1796. else
  1797. {
  1798. repaintText (Range<int> (insertIndex, getTotalNumChars())); // must do this before and after changing the data, in case
  1799. // a line gets moved due to word wrap
  1800. int index = 0;
  1801. int nextIndex = 0;
  1802. for (int i = 0; i < sections.size(); ++i)
  1803. {
  1804. nextIndex = index + sections.getUnchecked (i)->getTotalLength();
  1805. if (insertIndex == index)
  1806. {
  1807. sections.insert (i, new UniformTextSection (text, font, colour, passwordCharacter));
  1808. break;
  1809. }
  1810. else if (insertIndex > index && insertIndex < nextIndex)
  1811. {
  1812. splitSection (i, insertIndex - index);
  1813. sections.insert (i + 1, new UniformTextSection (text, font, colour, passwordCharacter));
  1814. break;
  1815. }
  1816. index = nextIndex;
  1817. }
  1818. if (nextIndex == insertIndex)
  1819. sections.add (new UniformTextSection (text, font, colour, passwordCharacter));
  1820. coalesceSimilarSections();
  1821. totalNumChars = -1;
  1822. valueTextNeedsUpdating = true;
  1823. updateTextHolderSize();
  1824. moveCaretTo (caretPositionToMoveTo, false);
  1825. repaintText (Range<int> (insertIndex, getTotalNumChars()));
  1826. }
  1827. }
  1828. }
  1829. void TextEditor::reinsert (const int insertIndex,
  1830. const Array <UniformTextSection*>& sectionsToInsert)
  1831. {
  1832. int index = 0;
  1833. int nextIndex = 0;
  1834. for (int i = 0; i < sections.size(); ++i)
  1835. {
  1836. nextIndex = index + sections.getUnchecked (i)->getTotalLength();
  1837. if (insertIndex == index)
  1838. {
  1839. for (int j = sectionsToInsert.size(); --j >= 0;)
  1840. sections.insert (i, new UniformTextSection (*sectionsToInsert.getUnchecked(j)));
  1841. break;
  1842. }
  1843. else if (insertIndex > index && insertIndex < nextIndex)
  1844. {
  1845. splitSection (i, insertIndex - index);
  1846. for (int j = sectionsToInsert.size(); --j >= 0;)
  1847. sections.insert (i + 1, new UniformTextSection (*sectionsToInsert.getUnchecked(j)));
  1848. break;
  1849. }
  1850. index = nextIndex;
  1851. }
  1852. if (nextIndex == insertIndex)
  1853. {
  1854. for (int j = 0; j < sectionsToInsert.size(); ++j)
  1855. sections.add (new UniformTextSection (*sectionsToInsert.getUnchecked(j)));
  1856. }
  1857. coalesceSimilarSections();
  1858. totalNumChars = -1;
  1859. valueTextNeedsUpdating = true;
  1860. }
  1861. void TextEditor::remove (Range<int> range,
  1862. UndoManager* const um,
  1863. const int caretPositionToMoveTo)
  1864. {
  1865. if (! range.isEmpty())
  1866. {
  1867. int index = 0;
  1868. for (int i = 0; i < sections.size(); ++i)
  1869. {
  1870. const int nextIndex = index + sections.getUnchecked(i)->getTotalLength();
  1871. if (range.getStart() > index && range.getStart() < nextIndex)
  1872. {
  1873. splitSection (i, range.getStart() - index);
  1874. --i;
  1875. }
  1876. else if (range.getEnd() > index && range.getEnd() < nextIndex)
  1877. {
  1878. splitSection (i, range.getEnd() - index);
  1879. --i;
  1880. }
  1881. else
  1882. {
  1883. index = nextIndex;
  1884. if (index > range.getEnd())
  1885. break;
  1886. }
  1887. }
  1888. index = 0;
  1889. if (um != nullptr)
  1890. {
  1891. Array <UniformTextSection*> removedSections;
  1892. for (int i = 0; i < sections.size(); ++i)
  1893. {
  1894. if (range.getEnd() <= range.getStart())
  1895. break;
  1896. UniformTextSection* const section = sections.getUnchecked (i);
  1897. const int nextIndex = index + section->getTotalLength();
  1898. if (range.getStart() <= index && range.getEnd() >= nextIndex)
  1899. removedSections.add (new UniformTextSection (*section));
  1900. index = nextIndex;
  1901. }
  1902. if (um->getNumActionsInCurrentTransaction() > TextEditorDefs::maxActionsPerTransaction)
  1903. newTransaction();
  1904. um->perform (new RemoveAction (*this, range, caretPosition,
  1905. caretPositionToMoveTo, removedSections));
  1906. }
  1907. else
  1908. {
  1909. Range<int> remainingRange (range);
  1910. for (int i = 0; i < sections.size(); ++i)
  1911. {
  1912. UniformTextSection* const section = sections.getUnchecked (i);
  1913. const int nextIndex = index + section->getTotalLength();
  1914. if (remainingRange.getStart() <= index && remainingRange.getEnd() >= nextIndex)
  1915. {
  1916. sections.remove(i);
  1917. section->clear();
  1918. delete section;
  1919. remainingRange.setEnd (remainingRange.getEnd() - (nextIndex - index));
  1920. if (remainingRange.isEmpty())
  1921. break;
  1922. --i;
  1923. }
  1924. else
  1925. {
  1926. index = nextIndex;
  1927. }
  1928. }
  1929. coalesceSimilarSections();
  1930. totalNumChars = -1;
  1931. valueTextNeedsUpdating = true;
  1932. moveCaretTo (caretPositionToMoveTo, false);
  1933. repaintText (Range<int> (range.getStart(), getTotalNumChars()));
  1934. }
  1935. }
  1936. }
  1937. //==============================================================================
  1938. String TextEditor::getText() const
  1939. {
  1940. MemoryOutputStream mo;
  1941. mo.preallocate ((size_t) getTotalNumChars());
  1942. for (int i = 0; i < sections.size(); ++i)
  1943. sections.getUnchecked (i)->appendAllText (mo);
  1944. return mo.toString();
  1945. }
  1946. String TextEditor::getTextInRange (const Range<int>& range) const
  1947. {
  1948. if (range.isEmpty())
  1949. return String::empty;
  1950. MemoryOutputStream mo;
  1951. mo.preallocate ((size_t) jmin (getTotalNumChars(), range.getLength()));
  1952. int index = 0;
  1953. for (int i = 0; i < sections.size(); ++i)
  1954. {
  1955. const UniformTextSection* const s = sections.getUnchecked (i);
  1956. const int nextIndex = index + s->getTotalLength();
  1957. if (range.getStart() < nextIndex)
  1958. {
  1959. if (range.getEnd() <= index)
  1960. break;
  1961. s->appendSubstring (mo, range - index);
  1962. }
  1963. index = nextIndex;
  1964. }
  1965. return mo.toString();
  1966. }
  1967. String TextEditor::getHighlightedText() const
  1968. {
  1969. return getTextInRange (selection);
  1970. }
  1971. int TextEditor::getTotalNumChars() const
  1972. {
  1973. if (totalNumChars < 0)
  1974. {
  1975. totalNumChars = 0;
  1976. for (int i = sections.size(); --i >= 0;)
  1977. totalNumChars += sections.getUnchecked (i)->getTotalLength();
  1978. }
  1979. return totalNumChars;
  1980. }
  1981. bool TextEditor::isEmpty() const
  1982. {
  1983. return getTotalNumChars() == 0;
  1984. }
  1985. void TextEditor::getCharPosition (const int index, float& cx, float& cy, float& lineHeight) const
  1986. {
  1987. const float wordWrapWidth = getWordWrapWidth();
  1988. if (wordWrapWidth > 0 && sections.size() > 0)
  1989. {
  1990. Iterator i (sections, wordWrapWidth, passwordCharacter);
  1991. i.getCharPosition (index, cx, cy, lineHeight);
  1992. }
  1993. else
  1994. {
  1995. cx = cy = 0;
  1996. lineHeight = currentFont.getHeight();
  1997. }
  1998. }
  1999. int TextEditor::indexAtPosition (const float x, const float y)
  2000. {
  2001. const float wordWrapWidth = getWordWrapWidth();
  2002. if (wordWrapWidth > 0)
  2003. {
  2004. Iterator i (sections, wordWrapWidth, passwordCharacter);
  2005. while (i.next())
  2006. {
  2007. if (i.lineY + i.lineHeight > y)
  2008. {
  2009. if (i.lineY > y)
  2010. return jmax (0, i.indexInText - 1);
  2011. if (i.atomX >= x)
  2012. return i.indexInText;
  2013. if (x < i.atomRight)
  2014. return i.xToIndex (x);
  2015. }
  2016. }
  2017. }
  2018. return getTotalNumChars();
  2019. }
  2020. //==============================================================================
  2021. int TextEditor::findWordBreakAfter (const int position) const
  2022. {
  2023. const String t (getTextInRange (Range<int> (position, position + 512)));
  2024. const int totalLength = t.length();
  2025. int i = 0;
  2026. while (i < totalLength && CharacterFunctions::isWhitespace (t[i]))
  2027. ++i;
  2028. const int type = TextEditorDefs::getCharacterCategory (t[i]);
  2029. while (i < totalLength && type == TextEditorDefs::getCharacterCategory (t[i]))
  2030. ++i;
  2031. while (i < totalLength && CharacterFunctions::isWhitespace (t[i]))
  2032. ++i;
  2033. return position + i;
  2034. }
  2035. int TextEditor::findWordBreakBefore (const int position) const
  2036. {
  2037. if (position <= 0)
  2038. return 0;
  2039. const int startOfBuffer = jmax (0, position - 512);
  2040. const String t (getTextInRange (Range<int> (startOfBuffer, position)));
  2041. int i = position - startOfBuffer;
  2042. while (i > 0 && CharacterFunctions::isWhitespace (t [i - 1]))
  2043. --i;
  2044. if (i > 0)
  2045. {
  2046. const int type = TextEditorDefs::getCharacterCategory (t [i - 1]);
  2047. while (i > 0 && type == TextEditorDefs::getCharacterCategory (t [i - 1]))
  2048. --i;
  2049. }
  2050. jassert (startOfBuffer + i >= 0);
  2051. return startOfBuffer + i;
  2052. }
  2053. //==============================================================================
  2054. void TextEditor::splitSection (const int sectionIndex,
  2055. const int charToSplitAt)
  2056. {
  2057. jassert (sections[sectionIndex] != nullptr);
  2058. sections.insert (sectionIndex + 1,
  2059. sections.getUnchecked (sectionIndex)->split (charToSplitAt, passwordCharacter));
  2060. }
  2061. void TextEditor::coalesceSimilarSections()
  2062. {
  2063. for (int i = 0; i < sections.size() - 1; ++i)
  2064. {
  2065. UniformTextSection* const s1 = sections.getUnchecked (i);
  2066. UniformTextSection* const s2 = sections.getUnchecked (i + 1);
  2067. if (s1->font == s2->font
  2068. && s1->colour == s2->colour)
  2069. {
  2070. s1->append (*s2, passwordCharacter);
  2071. sections.remove (i + 1);
  2072. delete s2;
  2073. --i;
  2074. }
  2075. }
  2076. }