summaryrefslogtreecommitdiff
path: root/components/custom/TextArea.qml
blob: 92263fe6ed00ccf2b541195e0aeea514f0b67002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import QtQuick 1.0
import "./styles/default" as DefaultStyles
import "./behaviors"    // TextEditMouseBehavior

FocusScope {
    id: textArea

    property alias text: textEdit.text
    property alias placeholderText: placeholderTextComponent.text
    property alias font: textEdit.font
    property bool passwordMode: false
    property bool readOnly: textEdit.readOnly // read only
    property int inputHint; // values tbd   (alias to TextEdit.inputMethodHints?
    property alias selectedText: textEdit.selectedText
    property alias selectionEnd: textEdit.selectionEnd
    property alias selectionStart: textEdit.selectionStart
    property alias horizontalAlignment: textEdit.horizontalAlignment
    property alias verticalAlignment: textEdit.verticalAlignment
    property alias wrapMode: textEdit.wrapMode  //mm Missing from spec
    property alias textFormat: textEdit.textFormat
    property alias cursorPosition: textEdit.cursorPosition

    property color textColor: syspal.text
    property color backgroundColor: syspal.base
    property alias containsMouse: mouseEditBehavior.containsMouse

    property Component background: defaultStyle.background
    property Component hints: defaultStyle.hints

    property int minimumWidth: defaultStyle.minimumWidth
    property int minimumHeight: defaultStyle.minimumHeight

    property int leftMargin: defaultStyle.leftMargin
    property int topMargin: defaultStyle.topMargin
    property int rightMargin: defaultStyle.rightMargin
    property int bottomMargin: defaultStyle.bottomMargin

    function copy() {
        textEdit.copy()
    }

    function paste() {
        textEdit.paste()
    }

    function cut() {
        textEdit.cut()
    }

    function forceActiveFocus() {
        textEdit.forceActiveFocus()
    }

    function select(start, end) {
        textEdit.select(start, end)
    }

    function selectAll() {
        textEdit.selectAll()
    }

    function selectWord() {
        textEdit.selectWord()
    }

    function positionAt(x, y) {
        var p = mapToItem(textEdit, x, y);
        return textEdit.positionAt(p.x, p.y);
    }

    function positionToRectangle(pos) {
        var p = mapToItem(textEdit, pos.x, pos.y);
        return textEdit.positionToRectangle(p);
    }

    width: Math.max(minimumWidth,
                    Math.max(textEdit.width, placeholderTextComponent.width) + leftMargin + rightMargin)
    height: Math.max(minimumHeight,
                     Math.max(textEdit.height, placeholderTextComponent.height) + topMargin + bottomMargin)


    // Implementation

    property alias activeFocus: textEdit.activeFocus // Forward active focus
    property alias desktopBehavior: mouseEditBehavior.desktopBehavior
    property alias _hints: hintsLoader.item
    clip: true

    SystemPalette { id: syspal }
    Loader { id: hintsLoader; sourceComponent: hints }
    Loader { sourceComponent: background; anchors.fill: parent }

    Flickable { //mm is FocusScope, so TextArea's root doesn't need to be, no?
        id: flickable
        clip: true

        anchors.fill: parent
        anchors.leftMargin: leftMargin
        anchors.topMargin: topMargin
        anchors.rightMargin: rightMargin
        anchors.bottomMargin: bottomMargin

        function ensureVisible(r) {
            if (contentX >= r.x)
                contentX = r.x;
            else if (contentX+width <= r.x+r.width)
                contentX = r.x+r.width-width;
            if (contentY >= r.y)
                contentY = r.y;
            else if (contentY+height <= r.y+r.height)
                contentY = r.y+r.height-height;
        }

        TextEdit { // see QTBUG-14936
            id: textEdit
            font.pixelSize: _hints.fontPixelSize
            font.bold: _hints.fontBold

            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right

            color: enabled ? textColor: Qt.tint(textColor, "#80ffffff")
            wrapMode: desktopBehavior ? TextEdit.NoWrap : TextEdit.WordWrap
            onCursorRectangleChanged: flickable.ensureVisible(cursorRectangle)

            onActiveFocusChanged: activeFocus ? openSoftwareInputPanel() : closeSoftwareInputPanel()
        }
    }

    Text {
        id: placeholderTextComponent
        x: leftMargin; y: topMargin
        font: textEdit.font
        opacity: !textEdit.text.length && !textEdit.activeFocus ? 1 : 0
        color: "gray"
        clip: true
        text: "Enter text"
        Behavior on opacity { NumberAnimation { duration: 90 } }
    }


    TextEditMouseBehavior {
        id: mouseEditBehavior
        anchors.fill: parent
        textEdit: textEdit
        desktopBehavior: false
        copyPasteButtons: ButtonBlock {
            opacity: 0  // initially hidden
            Behavior on opacity { NumberAnimation { duration: 100 } }
        }
    }

    DefaultStyles.TextFieldStyle { id: defaultStyle }
}