Package pyjamas :: Package ui :: Module TextBoxBase
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.TextBoxBase

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from pyjamas import DOM 
 16  from pyjamas import Factory 
 17  from __pyjamas__ import console, JS 
 18  from FocusWidget import FocusWidget 
 19  from pyjamas.ui import Event 
 20   
21 -class TextBoxBase(FocusWidget):
22 ALIGN_CENTER = "center" 23 ALIGN_JUSTIFY = "justify" 24 ALIGN_LEFT = "left" 25 ALIGN_RIGHT = "right" 26
27 - def __init__(self, element, **kwargs):
28 self.changeListeners = [] 29 self.currentEvent = None 30 31 FocusWidget.__init__(self, element, **kwargs) 32 self.sinkEvents(Event.ONCHANGE)
33
34 - def addChangeListener(self, listener):
35 self.changeListeners.append(listener)
36
37 - def cancelKey(self):
38 if self.currentEvent is not None: 39 DOM.eventPreventDefault(self.currentEvent)
40
41 - def getCursorPos(self):
42 element = self.getElement() 43 try: 44 return element.selectionStart 45 except: 46 return 0
47
48 - def getName(self):
49 return DOM.getAttribute(self.getElement(), "name")
50
51 - def getSelectedText(self):
52 start = self.getCursorPos() 53 length = self.getSelectionLength() 54 text = self.getText() 55 return text[start:start + length]
56
57 - def getSelectionLength(self):
58 element = self.getElement() 59 try: 60 return element.selectionEnd - element.selectionStart 61 except: 62 return 0
63 64 # have to override Focus here for TextBoxBase 65 # because FocusWidget (actually FocusMixin) assumes that 66 # CreateFocusable has been used to create the element. 67 # in "input box" type scenarios, it hasn't: it's created 68 # by TextBox class etc.
69 - def setFocus(self, focused):
70 if (focused): 71 self.getElement().focus() 72 else: 73 self.getElement().blur()
74
75 - def getText(self):
76 return DOM.getAttribute(self.getElement(), "value")
77
78 - def onBrowserEvent(self, event):
79 FocusWidget.onBrowserEvent(self, event) 80 81 type = DOM.eventGetType(event) 82 if type == "change": 83 for listener in self.changeListeners: 84 if hasattr(listener, 'onChange'): listener.onChange(self) 85 else: listener(self)
86
87 - def removeChangeListener(self, listener):
88 self.changeListeners.remove(listener)
89
90 - def selectAll(self):
91 length = len(self.getText()) 92 if length > 0: 93 self.setSelectionRange(0, length)
94
95 - def setCursorPos(self, pos):
96 self.setSelectionRange(pos, 0)
97
98 - def setKey(self, key):
99 if self.currentEvent is not None: 100 DOM.eventSetKeyCode(self.currentEvent, key)
101
102 - def setName(self, name):
103 DOM.setAttribute(self.getElement(), "name", name)
104
105 - def setSelectionRange(self, pos, length):
106 if length < 0: 107 # throw new IndexOutOfBoundsException("Length must be a positive integer. Length: " + length); 108 console.error("Length must be a positive integer. Length: " + length) 109 110 if (pos < 0) or (length + pos > len(self.getText())): 111 #throw new IndexOutOfBoundsException("From Index: " + pos + " To Index: " + (pos + length) + " Text Length: " + getText().length()); 112 console.error("From Index: %d " % pos + " To Index: %d " % (pos + length) + " Text Length: %d " % len(self.getText())) 113 114 element = self.getElement() 115 element.setSelectionRange(pos, pos + length)
116
117 - def setText(self, text):
118 DOM.setAttribute(self.getElement(), "value", str(text))
119
120 - def setTextAlignment(self, align):
121 DOM.setStyleAttribute(self.getElement(), "textAlign", align)
122 123 # TODO: work out if TextBoxBase is appropriate to create in Factory. 124 # Factory.registerClass('pyjamas.ui.TextBoxBase', TextBoxBase) 125