Debian Codegraph @

src/imports/Components/2.0/TextArea.qml

raw
1 /*
2 * Copyright 2015-2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components as Lomiri
19 import Lomiri.Components.Popups
20
21 /*!
22 \qmltype TextArea
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri
25 \brief The TextArea item displays a block of editable, scrollable, formatted
26 text.
27
28 The TextArea supports fix-size and auto-expanding modes. In fix-size mode the
29 content is scrolled when exceeds the boundaries and can be scrolled both horizontally
30 and vertically, depending on the contentWidth and contentHeight set. The following
31 example will scroll the editing area both horizontally and vertically:
32
33 \qml
34 TextArea {
35 width: units.gu(20)
36 height: units.gu(12)
37 contentWidth: units.gu(30)
38 contentHeight: units.gu(60)
39 }
40 \endqml
41
42 The auto-expand mode is realized using two properties: autoSize and maximumLineCount.
43 Setting autoSize will set implicitHeight to one line, and the height will follow
44 the line count, meaning when lines are added the area will expand and when
45 removed the area will shrink. The maximumLineCount specifies how much the
46 editor should be expanded. If this value is set to 0, the area will always
47 expand vertically to fit the content. When autoSize is set, the contentHeight
48 property value is ignored, and the expansion only happens vertically.
49
50 \qml
51 TextArea {
52 width: units.gu(20)
53 height: units.gu(12)
54 contentWidth: units.gu(30)
55 autoSize: true
56 maximumLineCount: 0
57 }
58 \endqml
59
60 TextArea comes with 30 grid-units implicit width and one line height on auto-sizing
61 mode and 4 lines on fixed-mode. The line size is calculated from the font size and the
62 ovarlay and frame spacing specified in the style.
63
64 \section2 Scrolling and text selection
65 The input is activated when the tap or mouse is released after being pressed
66 over the component.
67
68 Scrolling the editing area can happen when the size is fixed or in auto-sizing mode when
69 the content size is bigger than the visible area. The scrolling is realized by swipe
70 gestures, or by navigating the cursor.
71
72 The content can be selected in the following ways:
73 \list
74 \li - double tapping/left mouse clicking over the content, when the word that
75 had been tapped over will be selected
76 \li - by pressing and dragging the selection cursor over the text input. Note
77 that there has to be a delay of approx. 200 ms between the press and drag
78 gesture, time when the input switches from scroll mode to selection mode
79 \endlist
80
81 The input is focused (activated) upon tap/left mouse button release. The cursor
82 will be placed at the position the mouse/tap point at release time. If the click
83 is happening on a selected area, the selection will be cleared. Long press above
84 a selected area brings up the clipboard context menu. When the long press happens
85 over a non-selected area, the cursor will be moved to the position and the component
86 enters in selection mode. The selection mode can also be activated by tapping and
87 keeping the tap/mouse over for approx 300 ms. If there is a move during this time,
88 the component enters into scrolling mode. The mode is exited once the scrolling
89 finishes. During the scrolling mode the selected text is preserved.
90
91 \note During text selection all interactive parent Flickables are turned off.
92 */
93
94 Lomiri.StyledItem {
95 id: control
96 implicitWidth: units.gu(30)
97 implicitHeight: (autoSize) ? internal.minimumSize : internal.linesHeight(4)
98 activeFocusOnTab: true
99
100 // new properties
101 /*!
102 The property presents whether the TextArea is highlighted or not. By
103 default the TextArea gets highlighted when gets the focus, so can accept
104 text input. This property allows to control the highlight separately from
105 the focused behavior.
106 */
107 property bool highlighted
108 /*!
109 Text that appears when there is no focus and no content in the component
110 (hint text).
111
112 \qmlproperty string placeholderText
113 */
114 property alias placeholderText: hint.text
115
116 /*!
117 This property contains the text that is displayed on the screen. May differ
118 from the text property value when TextEdit.RichText format is selected.
119
120 \qmlproperty string displayText
121 */
122 readonly property alias displayText: internal.displayText
123
124 /*!
125 The property drives whether text selection should happen with the mouse or
126 not. The default value is true.
127 \qmlproperty bool selectByMouse
128 */
129 property alias selectByMouse: editor.selectByMouse
130
131 /*!
132 \deprecated
133 This property specifies whether the text area expands following the entered
134 text or not. The default value is false.
135 The property is deprecated, use autoSize instead
136 */
137 property bool autoExpand
138 /*! \internal */
139 onAutoExpandChanged: console.debug("WARNING: autoExpand deprecated, use autoSize instead.")
140
141 /*!
142 This property specifies whether the text area sizes following the line count
143 or not. The default value is false.
144 */
145 property bool autoSize: false
146
147 /*!
148 The property holds the maximum amount of lines to expand when autoSize is
149 enabled. The value of 0 does not put any upper limit and the control will
150 expand forever.
151
152 The default value is 5 lines.
153 */
154 property int maximumLineCount: 5
155
156 // altered TextEdit properties
157 /*!
158 The property folds the width of the text editing content. This can be equal or
159 bigger than the frame width minus the spacing between the frame and the input
160 area defined in the current theme. The default value is the same as the visible
161 input area's width.
162 */
163 property real contentWidth: control.width - 2 * internal.frameSpacing
164
165 /*!
166 The property folds the height of the text editing content. This can be equal or
167 bigger than the frame height minus the spacing between the frame and the input
168 area defined in the current theme. The default value is the same as the visible
169 input area's height.
170 */
171 property real contentHeight: control.height - 2 * internal.frameSpacing
172
173 /*!
174 The property overrides the default popover of the TextArea. When set, the
175 TextArea will open the given popover instead of the default one defined.
176 The popover can either be a component or a URL to be loaded.
177 */
178 property var popover
179
180 // forwarded properties
181 /*!
182 This property specifies a base URL which is used to resolve relative URLs within
183 the text. The default value is the url of the QML file instantiating the TextArea
184 item.
185
186 \qmlproperty url baseUrl
187 */
188 property alias baseUrl: editor.baseUrl
189
190 /*!
191 Returns true if the TextArea is writable and the content of the clipboard is
192 suitable for pasting into the TextArea.
193
194 \qmlproperty bool canPaste
195 */
196 property alias canPaste: editor.canPaste
197
198 /*!
199 Returns true if the TextArea is writable and there are undone operations that
200 can be redone.
201
202 \qmlproperty bool canRedo
203 */
204 property alias canRedo: editor.canRedo
205
206 /*!
207 Returns true if the TextArea is writable and there are previous operations
208 that can be undone.
209
210 \qmlproperty bool canUndo
211 */
212 property alias canUndo: editor.canUndo
213
214 /*!
215 The text color.
216
217 \qmlproperty color color
218 */
219 property alias color: editor.color
220
221 /*!
222 The delegate for the cursor in the TextArea.
223
224 If you set a cursorDelegate for a TextArea, this delegate will be used for
225 drawing the cursor instead of the standard cursor. An instance of the delegate
226 will be created and managed by the text edit when a cursor is needed, and
227 the x and y properties of delegate instance will be set so as to be one pixel
228 before the top left of the current character.
229
230 Note that the root item of the delegate component must be a QQuickItem or
231 QQuickItem derived item.
232 */
233 property Component cursorDelegate: null
234
235 /*!
236 The position of the cursor in the TextArea.
237
238 \qmlproperty int cursorPosition
239 */
240 property alias cursorPosition: editor.cursorPosition
241
242 /*!
243 The rectangle where the standard text cursor is rendered within the text
244 edit. Read-only.
245
246 The position and height of a custom cursorDelegate are updated to follow
247 the cursorRectangle automatically when it changes. The width of the delegate
248 is unaffected by changes in the cursor rectangle.
249
250 \qmlproperty rectangle cursorRectangle
251 */
252 property alias cursorRectangle: editor.cursorRectangle
253
254 /*!
255 If true the text edit shows a cursor.
256
257 This property is set and unset when the text edit gets active focus, but it
258 can also be set directly (useful, for example, if a KeyProxy might forward
259 keys to it).
260
261 \qmlproperty bool cursorVisible
262 */
263 property alias cursorVisible: editor.cursorVisible
264
265 /*!
266 Presents the effective horizontal alignment that can be different from the one
267 specified at horizontalAlignment due to layout mirroring.
268
269 \qmlproperty enumeration effectiveHorizontalAlignment
270 */
271 property alias effectiveHorizontalAlignment: editor.effectiveHorizontalAlignment
272
273 /*!
274 The property holds the font used by the editing.
275
276 \qmlproperty font font
277 */
278 property alias font: editor.font
279
280 /*!
281 Sets the horizontal alignment of the text within the TextAre item's width
282 and height. By default, the text alignment follows the natural alignment
283 of the text, for example text that is read from left to right will be
284 aligned to the left.
285
286 Valid values for effectiveHorizontalAlignment are:
287 \list
288 \li TextEdit.AlignLeft (default)
289 \li TextEdit.AlignRight
290 \li TextEdit.AlignHCenter
291 \li TextEdit.AlignJustify
292 \endlist
293
294 \qmlproperty enumeration horizontalAlignment
295 */
296 property alias horizontalAlignment: editor.horizontalAlignment
297
298 /*!
299 This property holds whether the TextArea has partial text input from an
300 input method.
301
302 While it is composing an input method may rely on mouse or key events
303 from the TextArea to edit or commit the partial text. This property can
304 be used to determine when to disable events handlers that may interfere
305 with the correct operation of an input method.
306
307 \qmlproperty bool inputMethodComposing
308 */
309 property alias inputMethodComposing: editor.inputMethodComposing
310
311 /*!
312 Exposes the QQuickTextDocument which can be used to implement advanced
313 formatting and syntax highlighting (QSyntaxHighlighter) with C++.
314 \qmlproperty var textDocument
315 */
316 property alias textDocument: editor.textDocument
317
318 /*!
319 Exposes the paintedWidth of the underlying text component. It's required
320 if the application uses its own Flickable for scrolling.
321 */
322 readonly property real paintedWidth: editor.paintedWidth + internal.frameSpacing
323
324 /*!
325 Exposes the paintedHeight of the underlying text component. It's required
326 if the application uses its own Flickable for scrolling.
327 */
328 readonly property real paintedHeight: editor.paintedHeight + internal.frameSpacing
329
330 /*!
331 Provides hints to the input method about the expected content of the text
332 edit and how it should operate.
333
334 The value is a bit-wise combination of flags or Qt.ImhNone if no hints are set.
335
336 Flags that alter behaviour are:
337 \list
338 \li Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords.
339 \li Qt.ImhSensitiveData - Typed text should not be stored by the active input method in any persistent storage like predictive user dictionary.
340 \li Qt.ImhNoAutoUppercase - The input method should not try to automatically switch to upper case when a sentence ends.
341 \li Qt.ImhPreferNumbers - Numbers are preferred (but not required).
342 \li Qt.ImhPreferUppercase - Upper case letters are preferred (but not required).
343 \li Qt.ImhPreferLowercase - Lower case letters are preferred (but not required).
344 \li Qt.ImhNoPredictiveText - Do not use predictive text (i.e. dictionary lookup) while typing.
345 \li Qt.ImhDate - The text editor functions as a date field.
346 \li Qt.ImhTime - The text editor functions as a time field.
347 \endlist
348 Flags that restrict input (exclusive flags) are:
349
350 \list
351 \li Qt.ImhDigitsOnly - Only digits are allowed.
352 \li Qt.ImhFormattedNumbersOnly - Only number input is allowed. This includes decimal point and minus sign.
353 \li Qt.ImhUppercaseOnly - Only upper case letter input is allowed.
354 \li Qt.ImhLowercaseOnly - Only lower case letter input is allowed.
355 \li Qt.ImhDialableCharactersOnly - Only characters suitable for phone dialing are allowed.
356 \li Qt.ImhEmailCharactersOnly - Only characters suitable for email addresses are allowed.
357 \li Qt.ImhUrlCharactersOnly - Only characters suitable for URLs are allowed.
358 \endlist
359 Masks:
360
361 \list
362 \li Qt.ImhExclusiveInputMask - This mask yields nonzero if any of the exclusive flags are used.
363 \endlist
364
365 \qmlproperty enumeration inputMethodHints
366 */
367 property alias inputMethodHints: editor.inputMethodHints
368
369 /*!
370 Returns the total number of plain text characters in the TextArea item.
371
372 As this number doesn't include any formatting markup it may not be the
373 same as the length of the string returned by the text property.
374
375 This property can be faster than querying the length the text property
376 as it doesn't require any copying or conversion of the TextArea's internal
377 string data.
378
379 \qmlproperty int length
380 */
381 property alias length: editor.length
382
383 /*!
384 Returns the total number of lines in the TextArea item.
385
386 \qmlproperty int lineCount
387 */
388 property alias lineCount: editor.lineCount
389
390 /*!
391 Specifies how text should be selected using a mouse.
392 \list
393 \li TextEdit.SelectCharacters - The selection is updated with individual characters. (Default)
394 \li TextEdit.SelectWords - The selection is updated with whole words.
395 \endlist
396 This property only applies when selectByMouse is true.
397
398 \qmlproperty enumeration mouseSelectionMode
399 */
400 property alias mouseSelectionMode: editor.mouseSelectionMode
401
402 /*!
403 Whether the TextArea should keep the selection visible when it loses active
404 focus to another item in the scene. By default this is set to true.
405
406 \qmlproperty bool persistentSelection
407 */
408 property alias persistentSelection: editor.persistentSelection
409
410 /*!
411 Whether the user can interact with the TextArea item. If this property is set
412 to true the text cannot be edited by user interaction.
413
414 By default this property is false.
415
416 \qmlproperty bool readOnly
417 */
418 property alias readOnly: editor.readOnly
419
420 /*!
421 Override the default rendering type for this component.
422
423 Supported render types are:
424 \list
425 \li Text.QtRendering - the default
426 \li Text.NativeRendering
427 \endlist
428 Select Text.NativeRendering if you prefer text to look native on the target
429 platform and do not require advanced features such as transformation of the
430 text. Using such features in combination with the NativeRendering render type
431 will lend poor and sometimes pixelated results.
432
433 \qmlproperty enumeration renderType
434 */
435 property alias renderType: editor.renderType
436
437 /*!
438 This read-only property provides the text currently selected in the text edit.
439
440 \qmlproperty string selectedText
441 */
442 property alias selectedText: editor.selectedText
443
444 /*!
445 The selected text color, used in selections.
446
447 \qmlproperty color selectedTextColor
448 */
449 property alias selectedTextColor: editor.selectedTextColor
450
451 /*!
452 The text highlight color, used behind selections.
453
454 \qmlproperty color selectionColor
455 */
456 property alias selectionColor: editor.selectionColor
457
458 /*!
459 The cursor position after the last character in the current selection.
460
461 This property is read-only. To change the selection, use select(start, end),
462 selectAll(), or selectWord().
463
464 See also selectionStart, cursorPosition, and selectedText.
465
466 \qmlproperty int selectionEnd
467 */
468 property alias selectionEnd: editor.selectionEnd
469
470 /*!
471 The cursor position before the first character in the current selection.
472
473 This property is read-only. To change the selection, use select(start, end),
474 selectAll(), or selectWord().
475
476 See also selectionEnd, cursorPosition, and selectedText.
477
478 \qmlproperty int selectionStart
479 */
480 property alias selectionStart: editor.selectionStart
481
482 /*!
483 The text to display. If the text format is AutoText the text edit will
484 automatically determine whether the text should be treated as rich text.
485 This determination is made using Qt::mightBeRichText().
486
487 \qmlproperty string text
488 */
489 property alias text: editor.text
490
491 /*!
492 The way the text property should be displayed.
493 \list
494 \li TextEdit.AutoText
495 \li TextEdit.PlainText
496 \li TextEdit.RichText
497 \endlist
498 The default is TextEdit.PlainText. If the text format is TextEdit.AutoText
499 the text edit will automatically determine whether the text should be treated
500 as rich text. This determination is made using Qt::mightBeRichText().
501
502 \qmlproperty enumeration textFormat
503 */
504 property alias textFormat: editor.textFormat
505
506 /*!
507 Sets the vertical alignment of the text within the TextAres item's width
508 and height. By default, the text alignment follows the natural alignment
509 of the text.
510
511 Valid values for verticalAlignment are:
512
513 \list
514 \li TextEdit.AlignTop (default)
515 \li TextEdit.AlignBottom
516 \li TextEdit.AlignVCenter
517 \endlist
518
519 \qmlproperty enumeration verticalAlignment
520 */
521 property alias verticalAlignment: editor.verticalAlignment
522
523 /*!
524 Set this property to wrap the text to the TextEdit item's width. The text
525 will only wrap if an explicit width has been set.
526 \list
527 \li TextEdit.NoWrap - no wrapping will be performed. If the text contains
528 insufficient newlines, then implicitWidth will exceed a set width.
529 \li TextEdit.WordWrap - wrapping is done on word boundaries only. If a word
530 is too long, implicitWidth will exceed a set width.
531 \li TextEdit.WrapAnywhere - wrapping is done at any point on a line, even
532 if it occurs in the middle of a word.
533 \li TextEdit.Wrap - if possible, wrapping occurs at a word boundary; otherwise
534 it will occur at the appropriate point on the line, even in the middle of a word.
535 \endlist
536 The default is TextEdit.Wrap
537
538 \qmlproperty enumeration wrapMode
539 */
540 property alias wrapMode:editor.wrapMode
541
542 // signals
543 /*!
544 This handler is called when the user clicks on a link embedded in the text.
545 The link must be in rich text or HTML format and the link string provides
546 access to the particular link.
547 */
548 signal linkActivated(string link)
549
550 // functions
551 /*!
552 Copies the currently selected text to the system clipboard.
553 */
554 function copy()
555 {
556 editor.copy();
557 }
558
559 /*!
560 Moves the currently selected text to the system clipboard.
561 */
562 function cut()
563 {
564 editor.cut();
565 }
566
567 /*!
568 Removes active text selection.
569 */
570 function deselect()
571 {
572 editor.deselect();
573 }
574
575 /*!
576 Inserts text into the TextArea at position.
577 */
578 function insert(position, text)
579 {
580 editor.insert(position, text);
581 }
582
583 /*!
584 Appends a new paragraph with text to the end of the TextArea.
585
586 In order to append without inserting a new paragraph, call TextArea.insert(TextArea.length, text) instead.
587 */
588 function append(text)
589 {
590 editor.append(text);
591 }
592
593 /*!
594 Returns the text position closest to pixel position (x, y).
595
596 Position 0 is before the first character, position 1 is after the first
597 character but before the second, and so on until position text.length,
598 which is after all characters.
599 */
600 function positionAt(x, y)
601 {
602 return editor.positionAt(x, y);
603 }
604
605 /*!
606 Returns true if the natural reading direction of the editor text found
607 between positions start and end is right to left.
608 */
609 function isRightToLeft(start, end)
610 {
611 return editor.isRightToLeft(start, end)
612 }
613
614 /*!
615 Moves the cursor to position and updates the selection according to the
616 optional mode parameter. (To only move the cursor, set the cursorPosition
617 property.)
618
619 When this method is called it additionally sets either the selectionStart
620 or the selectionEnd (whichever was at the previous cursor position) to the
621 specified position. This allows you to easily extend and contract the selected
622 text range.
623
624 The selection mode specifies whether the selection is updated on a per character
625 or a per word basis. If not specified the selection mode will default to whatever
626 is given in the mouseSelectionMode property.
627 */
628 function moveCursorSelection(position, mode)
629 {
630 if (mode === undefined)
631 editor.moveCursorSelection(position, mouseSelectionMode);
632 else
633 editor.moveCursorSelection(position, mode);
634 }
635
636 /*!
637 Places the clipboard or the data given as parameter into the text input.
638 The selected text will be replaces with the data.
639 */
640 function paste(data) {
641 if ((data !== undefined) && (typeof data === "string") && !editor.readOnly) {
642 var selTxt = editor.selectedText;
643 var txt = editor.text;
644 var pos = (selTxt !== "") ? txt.indexOf(selTxt) : editor.cursorPosition
645 if (selTxt !== "") {
646 editor.text = txt.substring(0, pos) + data + txt.substr(pos + selTxt.length);
647 } else {
648 editor.text = txt.substring(0, pos) + data + txt.substr(pos);
649 }
650 editor.cursorPosition = pos + data.length;
651 } else
652 editor.paste();
653 }
654
655 /*!
656 Returns the rectangle at the given position in the text. The x, y, and height
657 properties correspond to the cursor that would describe that position.
658 */
659 function positionToRectangle(position)
660 {
661 return editor.positionToRectangle(position);
662 }
663
664 /*!
665 Redoes the last operation if redo is \l{canRedo}{available}.
666 */
667 function redo()
668 {
669 editor.redo();
670 }
671
672 /*!
673 Causes the text from start to end to be selected.
674
675 If either start or end is out of range, the selection is not changed.
676
677 After calling this, selectionStart will become the lesser and selectionEnd
678 will become the greater (regardless of the order passed to this method).
679
680 See also selectionStart and selectionEnd.
681 */
682 function select(start, end)
683 {
684 editor.select(start, end);
685 }
686
687 /*!
688 Causes all text to be selected.
689 */
690 function selectAll()
691 {
692 editor.selectAll();
693 }
694
695 /*!
696 Causes the word closest to the current cursor position to be selected.
697 */
698 function selectWord()
699 {
700 editor.selectWord();
701 }
702
703 /*!
704 Returns the section of text that is between the start and end positions.
705
706 The returned text will be formatted according the textFormat property.
707 */
708 function getFormattedText(start, end)
709 {
710 return editor.getFormattedText(start, end);
711 }
712
713 /*!
714 Returns the section of text that is between the start and end positions.
715
716 The returned text does not include any rich text formatting. A getText(0, length)
717 will result in the same value as displayText.
718 */
719 function getText(start, end)
720 {
721 return editor.getText(start, end);
722 }
723
724 /*!
725 Removes the section of text that is between the start and end positions
726 from the TextArea.
727 */
728 function remove(start, end)
729 {
730 return editor.remove(start, end);
731 }
732
733 /*!
734 Undoes the last operation if undo is \l{canUndo}{available}. Deselects
735 any current selection, and updates the selection start to the current
736 cursor position.
737 */
738 function undo()
739 {
740 editor.undo();
741 }
742
743 // logic
744 Component.onCompleted: {
745 /*!\internal - to remove warnings */
746 editor.linkActivated.connect(control.linkActivated);
747 // init component's height
748 internal.frameSize()
749 }
750
751
752 // activation area on mouse click
753 // the editor activates automatically when pressed in the editor control,
754 // however that one can be slightly spaced to the main control area
755 MouseArea {
756 anchors.fill: parent
757 enabled: internal.frameSpacing > 0
758 acceptedButtons: Qt.LeftButton | Qt.RightButton
759 // activate input when pressed on the frame
760 preventStealing: false
761 Lomiri.Mouse.forwardTo: [inputHandler]
762 cursorShape: Qt.IBeamCursor
763 }
764
765 //internals
766 activeFocusOnPress: true
767
768 // Escape should close the context menu even if the menu takes no input focus
769 Keys.onEscapePressed: function(event) {
770 if (activeFocus && inputHandler.popover) {
771 PopupUtils.close(inputHandler.popover)
772 } else {
773 event.accepted = false
774 }
775 }
776
777 LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
778 LayoutMirroring.childrenInherit: true
779
780 QtObject {
781 id: internal
782 // public property locals enabling aliasing
783 property string displayText: {
784 var plainText = editor.getText(0, editor.length);
785 if (editor.hasOwnProperty('preeditText'))
786 plainText += editor.preeditText;
787 return plainText;
788 }
789 property real frameSpacing: control.__styleInstance.frameSpacing
790 property real minimumSize: units.gu(4)
791 property real scrollbarSpacing: rightScrollbar.__interactive ? units.gu(2) : 0
792
793 function linesHeight(lines)
794 {
795 return inputHandler.lineSize * lines + 2 * frameSpacing;
796 }
797
798 function frameSize()
799 {
800 if (control.autoSize) {
801 var max = (control.maximumLineCount <= 0) ?
802 control.lineCount :
803 Math.min(control.maximumLineCount, control.lineCount);
804 control.height = linesHeight(Lomiri.MathUtils.clamp(control.lineCount, 1, max));
805 }
806 }
807 }
808
809 // grab Enter/Return keys which may be stolen from parent components of TextArea
810 // due to forwarded keys from TextEdit
811 Keys.onPressed: function(event) {
812 if (readOnly)
813 return;
814 if ((event.key === Qt.Key_Enter) || (event.key === Qt.Key_Return)) {
815 if (editor.textFormat === TextEdit.RichText) {
816 // FIXME: use control.paste("<br />") instead when paste() gets sich text support
817 editor.insert(editor.cursorPosition, "<br />");
818 } else {
819 control.paste("\n");
820 }
821 event.accepted = true;
822 } else {
823 event.accepted = false;
824 }
825 }
826 Keys.onReleased: function (event) {
827 event.accepted = (event.key === Qt.Key_Enter) || (event.key === Qt.Key_Return);
828 }
829
830 // holding default values
831 Lomiri.Label { id: fontHolder }
832
833 //hint
834 Lomiri.Label {
835 id: hint
836 anchors {
837 fill: parent
838 margins: internal.frameSpacing
839 }
840 // hint is shown till user types something in the field
841 visible: (editor.text === "") && !editor.inputMethodComposing
842 color: theme.palette.normal.baseText
843 font: editor.font
844 elide: Text.ElideRight
845 wrapMode: Text.WordWrap
846 }
847
848 //scrollbars and flickable
849 Flickable {
850 id: flicker
851 objectName: "input_scroller"
852 anchors {
853 fill: parent
854 margins: internal.frameSpacing
855 }
856 clip: true
857 contentWidth: editor.paintedWidth
858 contentHeight: editor.paintedHeight
859 // do not allow rebounding
860 boundsBehavior: Flickable.StopAtBounds
861
862 // editor
863 // Images are not shown when text contains <img> tags
864 // bug to watch: https://bugreports.qt-project.org/browse/QTBUG-27071
865 TextEdit {
866 objectName: "text_input"
867 readOnly: false
868 id: editor
869 width: control.contentWidth - internal.scrollbarSpacing
870 height: Math.max(control.contentHeight, editor.contentHeight)
871 wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere
872 mouseSelectionMode: TextEdit.SelectWords
873 persistentSelection: true
874 selectByMouse: true
875 cursorDelegate: TextCursor {
876 handler: inputHandler
877 }
878 color: control.__styleInstance.color
879 selectedTextColor: control.__styleInstance.selectedTextColor
880 selectionColor: control.__styleInstance.selectionColor
881 font.pixelSize: FontUtils.sizeToPixels("medium")
882 // forward keys to the root element so it can be captured outside of it
883 // as well as to InputHandler to handle PageUp/PageDown keys
884 Keys.forwardTo: [control, inputHandler]
885
886 // autosize handling
887 onLineCountChanged: internal.frameSize()
888
889 // input selection and navigation handling
890 Lomiri.Mouse.forwardTo: [inputHandler]
891 InputHandler {
892 id: inputHandler
893 anchors.fill: parent
894 main: control
895 input: editor
896 flickable: flicker
897 }
898 }
899 }
900
901 /*! \internal */
902 property Item __rightScrollbar: rightScrollbar
903 Scrollbar {
904 id: rightScrollbar
905 flickableItem: flicker
906 // Attach right inside the frame
907 // Flickable uses anchors.margins relative to the frame
908 // rather than *Margin which would scroll with the content
909 anchors.topMargin: -internal.frameSpacing
910 anchors.rightMargin: -internal.frameSpacing
911 anchors.bottomMargin: -internal.frameSpacing
912 }
913
914 styleName: "TextAreaStyle"
915 }