~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/cursing/TextBox.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2002-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# TextBox.py
 
23
#
 
24
# DESCRIPTION:
 
25
#
 
26
# NOTES:
 
27
#
 
28
 
 
29
 
 
30
import curses, sio
 
31
 
 
32
from constants import *
 
33
from Control import Control
 
34
 
 
35
class TextBox(Control):
 
36
  """
 
37
    An example textbox control.  Allows the user to enter text.  Only
 
38
    basic editing though
 
39
  """
 
40
  def __init__(self, Parent, TextboxName, Y, X, W, DefaultValue="", Password=0,
 
41
         **properties):
 
42
    Control.__init__(self, Parent, TextboxName, **properties)
 
43
    self.PARENT = Parent
 
44
    self.CLASS = "TEXTBOX"
 
45
    self.PASSWORD = Password
 
46
    self.X = X
 
47
    self.Y = Y
 
48
    self.W = W
 
49
    self.H = 1
 
50
    self.TEXT = str(DefaultValue)
 
51
    self.CANGETFOCUS = 1
 
52
    self.FOCUS = 0
 
53
    self.DEPTH = 0  # our position in the text: 0..N
 
54
    self.MAXLENGTH = -1   # if -1, text length is unlimited
 
55
 
 
56
    self.SetMethod("SYSPAINT", self.Paint)
 
57
    self.SetMethod("SYSGOTFOCUS", self.GotFocus)
 
58
    self.SetMethod("SYSLOSTFOCUS", self.LostFocus)
 
59
    self.SetMethod("SYSRUN", self.Run)
 
60
    self.SetMethod("VERIFY", self.BogusVerify)
 
61
    self.SetMethod("LOSTFOCUS", None)
 
62
    self.SetMethod("GOTFOCUS", None)
 
63
    self.last_act = 1  # 1 = forward, 0 = backward
 
64
    self.laststart = 0
 
65
    self.EDITABLE = 1
 
66
 
 
67
    self.SetColor('TEXTBOX', 'TEXTBOX_FOCUS')
 
68
 
 
69
  def Run(self, v1, v2, v3):
 
70
    Container = self.PARENT.Screen()
 
71
    if self.EDITABLE:
 
72
##      try:
 
73
##        curses.curs_set(1)
 
74
##      except:
 
75
##        pass
 
76
      pass
 
77
    if v1 :
 
78
      Depth = self.DEPTH
 
79
      offset = (v3 - self.X)
 
80
      textlen = len(self.TEXT)
 
81
      if Depth < self.W:
 
82
        Depth = offset
 
83
      else:
 
84
        Depth = Depth -  (self.W- offset)
 
85
      if Depth < 0:
 
86
        Depth = 0
 
87
      elif Depth > textlen:
 
88
        Depth = textlen
 
89
      self.DEPTH = Depth
 
90
      self.Paint(0,0,0)
 
91
    while 1:
 
92
      ch = Container.GetChar()
 
93
      curses.curs_set(1)
 
94
      if self.PARENT.BreakOrder(ch) :
 
95
        try:
 
96
          curses.curs_set(0)
 
97
        except:
 
98
          pass
 
99
        return 1
 
100
 
 
101
      ch = self.PARENT.KeystrokeHook(ch)
 
102
      if ch == None:
 
103
#        try:
 
104
#          curses.curs_set(0)
 
105
#        except:
 
106
#          pass
 
107
        continue
 
108
      elif ch == -1:
 
109
        try:
 
110
          curses.curs_set(0)
 
111
        except:
 
112
          pass
 
113
        return
 
114
      else:
 
115
 
 
116
        Depth = self.DEPTH
 
117
        Text = self.TEXT
 
118
        global BACKWARDS
 
119
        if ch in (Container.TokDownArrow, Container.TokNextField, Container.TokActivate, Container.TokUpArrow):
 
120
          BACKWARDS = 0
 
121
          if ch == Container.TokUpArrow:
 
122
            BACKWARDS = 1
 
123
          if self.ExecMethod( "VERIFY", self.TEXT, None, None) :
 
124
##            try:
 
125
##              curses.curs_set(0)
 
126
##            except:
 
127
##              pass
 
128
            return
 
129
        elif ch == Container.TokLeftArrow:
 
130
          Depth = Depth - 1
 
131
          self.last_act = 0
 
132
          if Depth < 0:
 
133
            Depth = 0
 
134
        elif ch == Container.TokHomeKey:
 
135
          Depth = 0
 
136
        elif ch == Container.TokEndKey:
 
137
          Depth = len(Text)
 
138
        elif ch == Container.TokRightArrow:
 
139
          Depth = Depth + 1
 
140
          self.last_act = 1
 
141
          if Depth > len(Text):
 
142
            Depth = len(Text)
 
143
        elif ch == Container.TokBackspace:
 
144
          Depth = Depth - 1
 
145
          if Depth < 0:
 
146
            Depth = 0
 
147
          Text = Text[0:Depth] + Text[Depth+1:len(Text)]
 
148
        else:
 
149
          if ch <= 256 and self.EDITABLE:
 
150
            if Depth == len(Text):    # at end
 
151
              Text = Text + chr(ch)
 
152
            elif Depth == 0:    # at the beginning
 
153
              if len(Text) == 0:
 
154
                Text = chr(ch)
 
155
              else:
 
156
                Text = chr(ch) + Text
 
157
            else:         # insert text
 
158
              Text = Text[:Depth] + chr(ch) + Text[Depth:]
 
159
            Depth = Depth + 1
 
160
            self.last_act = 1
 
161
        self.DEPTH = Depth
 
162
        self.TEXT = Text
 
163
        self.Paint(None, None, None)
 
164
 
 
165
  def BogusVerify(self, TheControl, TheText, v3):
 
166
    return 1
 
167
 
 
168
  def Paint(self, v1, v2, v3):
 
169
    Container = self.PARENT.SCREEN
 
170
    try:
 
171
      Text = self.TEXT
 
172
      TextLen = len(self.TEXT)
 
173
    except:
 
174
      Text = ""
 
175
      TextLen = 0
 
176
 
 
177
    if self.FOCUS:
 
178
      color = self.FOCUSCOLOR
 
179
    else:
 
180
      color = self.COLOR
 
181
 
 
182
    if self.PASSWORD:
 
183
      Text = "*" * TextLen
 
184
 
 
185
    W = self.W
 
186
    Depth = self.DEPTH
 
187
    Visuals = ''
 
188
 
 
189
    if TextLen <= W or Depth < W:
 
190
      Visuals = Text + ((W - TextLen) * "_")
 
191
      if len(Visuals) > W:
 
192
        Visuals = Visuals[:W-1] + ">"
 
193
      Container.PrintAt(self.Y, self.X, Visuals, color)
 
194
      Container.GotoYX(self.Y, self.X+ Depth)
 
195
    else:
 
196
      if Depth >= W:
 
197
        Visuals = "<" + Text[Depth - (W - 1):] + (((W - 1) - TextLen) * "_")
 
198
        if len(Visuals) > W:
 
199
          Visuals = Visuals[:W]
 
200
        Container.PrintAt(self.Y, self.X, Visuals, self.FOCUS and self.FOCUSCOLOR or self.COLOR)
 
201
        Container.GotoYX(self.Y, self.X+  W)
 
202
 
 
203
  def SetCursor(self, location):
 
204
    self.DEPTH = location
 
205
    Container = self.PARENT.Screen()
 
206
    curses.curs_set(1)
 
207
    Container.GotoYX(self.Y, self.X+ self.DEPTH)
 
208
    self.Paint(None, None, None)
 
209
 
 
210
  def LostFocus(self, v1, v2, v3):
 
211
    self.FOCUS = 0
 
212
    curses.curs_set(0)
 
213
    if self.PARENT.VISIBLE:
 
214
      self.Paint(0,0,0)
 
215
 
 
216
  def GotFocus(self, v1, v2, v3):
 
217
    self.FOCUS = 1
 
218
    Container = self.PARENT.Screen()
 
219
    curses.curs_set(1)
 
220
    Container.GotoYX(self.Y, self.X+ self.DEPTH)
 
221
    self.Paint(None, None, None)
 
222
 
 
223
  def SetValue(self, newText, repaint=1):
 
224
    self.TEXT = newText
 
225
    if self.PARENT.VISIBLE:
 
226
      self.Paint(0,0,0)
 
227
 
 
228
  def GetValue(self):
 
229
    return self.TEXT
 
230
 
 
231