1
# ------------------------------------------------------------------------------
2
#
3
#  Copyright (c) 2008, Enthought, Inc.
4
#  All rights reserved.
5
#
6
#  This software is provided without warranty under the terms of the BSD
7
#  license included in LICENSE.txt and may be redistributed only
8
#  under the conditions described in the aforementioned license.  The license
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
10
#
11
#  Thanks for using Enthought open source!
12
#
13
#  Author: David C. Morrill
14
#  Date:   10/07/2004
15
#
16
# ------------------------------------------------------------------------------
17

18 11
""" Defines the text editor factory for all traits toolkit backends.
19
"""
20

21

22

23 11
from traits.api import Dict, Str, Any, Bool
24

25
# CIRCULAR IMPORT FIXME: Importing from the source rather than traits.ui.api
26
# to avoid circular imports, as this EditorFactory will be part of
27
# traits.ui.api as well.
28 11
from ..view import View
29

30 11
from ..group import Group
31

32 11
from ..ui_traits import AView
33

34 11
from ..editor_factory import EditorFactory
35

36
# -------------------------------------------------------------------------
37
#  Define a simple identity mapping:
38
# -------------------------------------------------------------------------
39

40

41 11
class _Identity(object):
42
    """ A simple identity mapping.
43
    """
44

45 11
    def __call__(self, value):
46 8
        return value
47

48

49
# -------------------------------------------------------------------------
50
#  Trait definitions:
51
# -------------------------------------------------------------------------
52

53
# Mapping from user input text to other value
54 11
mapping_trait = Dict(Str, Any)
55

56
# Function used to evaluate textual user input
57 11
evaluate_trait = Any(_Identity())
58

59
# -------------------------------------------------------------------------
60
#  'ToolkitEditorFactory' class:
61
# -------------------------------------------------------------------------
62

63

64 11
class ToolkitEditorFactory(EditorFactory):
65
    """ Editor factory for text editors.
66
    """
67

68
    # -------------------------------------------------------------------------
69
    #  Trait definitions:
70
    # -------------------------------------------------------------------------
71

72
    #: Dictionary that maps user input to other values
73 11
    mapping = mapping_trait
74

75
    #: Is user input set on every keystroke?
76 11
    auto_set = Bool(True)
77

78
    #: Is user input set when the Enter key is pressed?
79 11
    enter_set = Bool(False)
80

81
    #: Is multi-line text allowed?
82 11
    multi_line = Bool(True)
83

84
    #: Is editor readonly (will use custom / default editor appearance with readonly flag set to true)
85
    #: in contrasrt with readonly style for item when completely another edito
86
    #: is used
87 11
    read_only = Bool(False)
88

89
    #: Is user input unreadable? (e.g., for a password)
90 11
    password = Bool(False)
91

92
    #: Function to evaluate textual user input
93 11
    evaluate = evaluate_trait
94

95
    #: The object trait containing the function used to evaluate user input
96 11
    evaluate_name = Str()
97

98
    #: The optional view to display when a read-only text editor is clicked:
99 11
    view = AView
100

101
    #: In a read-only text editor, allow selection and copying of the text.
102 11
    readonly_allow_selection = Bool(False)
103

104
    #: Grayed-out placeholder text to be displayed when the editor is empty.
105 11
    placeholder = Str()
106

107
    # -------------------------------------------------------------------------
108
    #  Traits view definition:
109
    # -------------------------------------------------------------------------
110

111 11
    traits_view = View(
112
        [
113
            "auto_set{Set value when text is typed}",
114
            "enter_set{Set value when enter is pressed}",
115
            "multi_line{Allow multiple lines of text}",
116
            "<extras>",
117
            "|options:[Options]>",
118
        ]
119
    )
120

121 11
    extras = Group("password{Is this a password field?}")
122

123

124
# Define the TextEditor class.
125 11
TextEditor = ToolkitEditorFactory

Read our documentation on viewing source code .

Loading