6 # Some useful constants
11 # check about None as action for buttonAction/windowCloseAction ...
13 ################################################################################
15 ################################################################################
16 from exceptions
import Exception, StandardError
18 class Error(StandardError):
21 del Exception, StandardError
24 def __init__(self
, milliseconds
, callback
, cdata
=None, persistent
=False):
26 self
._o
= wings
.pyWMAddPersistentTimerHandler(milliseconds
, (callback
, cdata
))
28 self
._o
= wings
.pyWMAddTimerHandler(milliseconds
, (callback
, cdata
))
29 self
.__WMDeleteTimerHandler
= wings
.pyWMDeleteTimerHandler
32 self
.__WMDeleteTimerHandler
(self
._o
)
35 class WMPersistentTimer(WMTimer
):
36 def __init__(self
, milliseconds
, callback
, cdata
=None):
37 WMTimer
.__init
__(self
, milliseconds
, callback
, cdata
, persistent
=True)
41 __readonly
= ('display', 'width', 'height', 'depth')
43 def __init__(self
, appname
, display
="", simpleapp
=False):
44 wings
.WMInitializeApplication(appname
, len(sys
.argv
), sys
.argv
)
45 self
._o
= wings
.pyWMOpenScreen(display
, simpleapp
)
47 raise Error
, "Cannot open display %s" % display
48 self
.__dict
__['display'] = wings
.WMScreenDisplay(self
._o
)
49 self
.__dict
__['width'] = wings
.WMScreenWidth(self
._o
)
50 self
.__dict
__['height'] = wings
.WMScreenHeight(self
._o
)
51 self
.__dict
__['depth'] = wings
.WMScreenDepth(self
._o
)
53 def __setattr__(self
, name
,value
):
54 if name
in self
.__readonly
:
55 #raise AttributeError, "'%s' is a read-only WMScreen attribute" % name
56 raise Error
, "'%s' is a read-only WMScreen attribute" % name
57 self
.__dict
__[name
] = value
59 def __delattr__(self
, name
):
60 if name
in self
.__readonly
:
61 #raise AttributeError, "'%s' attribute cannot be deleted from WMScreen instance" % name
62 raise Error
, "'%s' attribute cannot be deleted from WMScreen instance" % name
64 del(self
.__dict
__[name
])
66 raise AttributeError, "%s instance has no attribute '%s'" % \
67 (self
.__class
__.__name
__, name
)
70 wings
.pyWMScreenMainLoop(self
._o
)
72 def breakMainLoop(self
):
73 wings
.pyWMBreakScreenMainLoop(self
._o
)
75 def runModalLoop(self
, view
):
76 wings
.pyWMRunModalLoop(self
._o
, view
)
78 def breakModalLoop(self
):
79 wings
.WMBreakModalLoop(self
._o
)
82 return (self
.width
, self
.height
)
89 class WMWidget(WMView
):
91 if self
.__class
__ == WMWidget
:
92 raise Error
, "a WMWidget can't be instantiated directly"
94 self
.__WMDestroyWidget
= wings
.WMDestroyWidget
97 if self
._o
is not None:
98 self
.__WMDestroyWidget
(self
._o
)
100 def resize(self
, width
, height
):
101 wings
.WMResizeWidget(self
._o
, width
, height
)
103 def move(self
, x
, y
):
104 wings
.WMMoveWidget(self
._o
, x
, y
)
107 wings
.WMRealizeWidget(self
._o
)
110 wings
.WMMapWidget(self
._o
)
113 wings
.WMUnmapWidget(self
._o
)
116 wings
.WMRedisplayWidget(self
._o
)
119 return wings
.WMWidgetWidth(self
._o
)
122 return wings
.WMWidgetHeight(self
._o
)
125 return wings
.WMWidgetScreen(self
._o
)
128 return wings
.WMWidgetView(self
._o
)
130 def setFocusTo(self
, other
):
131 wings
.WMSetFocusToWidget(other
._o
)
134 class WMWindow(WMWidget
):
135 def __init__(self
, screen
, name
, style
=wings
.WMTitledWindowMask
136 |wings
.WMClosableWindowMask|wings
.WMMiniaturizableWindowMask
137 |wings
.WMResizableWindowMask
):
138 WMWidget
.__init
__(self
)
139 self
._o
= wings
.WMCreateWindowWithStyle(screen
._o
, name
, style
)
141 def setMinSize(self
, minWidth
, minHeight
):
142 wings
.WMSetWindowMinSize(self
._o
, minWidth
, minHeight
)
144 def setMaxSize(self
, maxWidth
, maxHeight
):
145 wings
.WMSetWindowMaxSize(self
._o
, maxWidth
, maxHeight
)
147 def setInitialPosition(self
, x
, y
):
148 wings
.WMSetWindowInitialPosition(self
._o
, x
, y
)
150 def setTitle(self
, title
):
151 wings
.WMSetWindowTitle(self
._o
, title
)
153 def setCloseAction(self
, action
, data
=None):
154 if action
!=None and (not callable(action
)):
155 raise Error
, "action needs to be a callable object or None"
156 wings
.pyWMSetWindowCloseAction(self
._o
, (self
, action
, data
))
159 class WMPanel(WMWindow
):
160 def __init__(self
, owner
, name
, style
=wings
.WMTitledWindowMask
161 |wings
.WMClosableWindowMask|wings
.WMResizableWindowMask
):
162 WMWidget
.__init
__(self
)
163 self
._o
= wings
.WMCreatePanelWithStyleForWindow(owner
._o
, name
, style
)
165 class WMFrame(WMWidget
):
166 def __init__(self
, parent
, title
=None):
167 WMWidget
.__init
__(self
)
168 self
._o
= wings
.WMCreateFrame(parent
._o
)
171 def setRelief(self
, relief
):
172 wings
.WMSetFrameRelief(self
._o
, relief
)
174 def setTitle(self
, title
=""):
175 wings
.WMSetFrameTitle(self
._o
, title
)
177 def setTitlePosition(self
, position
):
178 wings
.WMSetFrameTitlePosition(self
._o
, position
)
180 class WMLabel(WMWidget
):
181 def __init__(self
, parent
, text
=None):
182 WMWidget
.__init
__(self
)
183 self
._o
= wings
.WMCreateLabel(parent
._o
)
186 def setWraps(self
, flag
):
187 # bool(flag) for python2.2
188 wings
.WMSetLabelWraps(self
._o
, flag
)
190 def setRelief(self
, relief
):
191 wings
.WMSetLabelRelief(self
._o
, relief
)
193 def setText(self
, text
=""):
194 wings
.WMSetLabelText(self
._o
, text
)
196 def setTextColor(self
, color
):
197 wings
.WMSetLabelTextColor(self
._o
, color
)
199 def setFont(self
, font
):
200 wings
.WMSetLabelFont(self
._o
, font
)
202 def setTextAlignment(self
, alignment
):
203 wings
.WMSetLabelTextAlignment(self
._o
, alignment
)
205 def setImage(self
, image
):
206 wings
.WMSetLabelImage(self
._o
, image
)
208 def setImagePosition(self
, position
):
209 wings
.WMSetLabelImagePosition(self
._o
, position
)
212 return wings
.WMGetLabelText(self
._o
)
215 return wings
.WMGetLabelFont(self
._o
)
218 return wings
.WMGetLabelImage(self
._o
)
221 class WMBox(WMWidget
):
222 def __init__(self
, parent
):
223 WMWidget
.__init
__(self
)
224 self
._o
= wings
.WMCreateBox(parent
._o
)
226 def setHorizontal(self
, flag
):
227 # bool(flag) for python2.2
228 wings
.WMSetBoxHorizontal(self
._o
, flag
)
230 def setBorderWidth(self
, width
):
231 wings
.WMSetBoxBorderWidth(self
._o
, width
)
233 def addSubview(self
, view
, expand
, fill
, minSize
, maxSize
, space
):
234 wings
.WMAddBoxSubview(self
._o
, view
, expand
, fill
, minSize
, maxSixe
, space
)
236 def addSubviewAtEnd(self
, view
, expand
, fill
, minSize
, maxSize
, space
):
237 wings
.WMAddBoxSubviewAtEnd(self
._o
, view
, expand
, fill
, minSize
, maxSixe
, space
)
239 def removeSubview(self
, view
):
240 wings
.WMRemoveBoxSubview(self
._o
, view
)
243 class WMButton(WMWidget
): # not for user instantiation
244 def __init__(self
, parent
):
245 WMWidget
.__init
__(self
)
246 if self
.__class
__ == WMButton
:
247 raise Error
, "a WMButton can't be instantiated directly"
250 def setText(self
, text
=""):
251 wings
.WMSetButtonText(self
._o
, text
)
253 def setAction(self
, action
, data
=None):
254 if action
!=None and (not callable(action
)):
255 raise Error
, "action needs to be a callable object or None"
256 wings
.pyWMSetButtonAction(self
._o
, (self
, action
, data
))
258 def performClick(self
):
259 wings
.WMPerformButtonClick(self
._o
)
261 def setEnabled(self
, flag
):
262 # bool(flag) for python2.2
263 wings
.WMSetButtonEnabled(self
._o
, flag
)
266 return wings
.WMGetButtonEnabled(self
._o
)
268 def setImageDimsWhenDisabled(self
, flag
):
270 wings
.WMSetButtonImageDimsWhenDisabled(self
._o
, flag
)
272 def setImage(self
, image
):
273 wings
.WMSetButtonImage(self_
.o
, image
)
275 def setAlternateImage(self
, image
):
276 wings
.WMSetButtonAltImage(self
._o
, image
)
278 def setImagePosition(self
, position
):
279 wings
.WMSetButtonImagePosition(self
._o
, position
)
281 def setImageDefault(self
):
282 wings
.WMSetButtonImageDefault(self
._o
)
285 class WMCommandButton(WMButton
):
286 def __init__(self
, parent
):
287 WMButton
.__init
__(self
, parent
)
288 self
._o
= wings
.WMCreateCommandButton(parent
._o
)
291 class WMSwitchButton(WMButton
):
292 def __init__(self
, parent
):
293 WMButton
.__init
__(self
, parent
)
294 self
._o
= wings
.WMCreateSwitchButton(parent
._o
)
297 class WMRadioButton(WMButton
):
298 def __init__(self
, parent
, group
=None):
299 WMButton
.__init
__(self
, parent
)
300 self
._o
= wings
.WMCreateRadioButton(parent
._o
)
302 wings
.WMGroupButtons(group
._o
, self
._o
)
309 class WMList(WMWidget
):
310 def __init__(self
, parent
):
311 WMWidget
.__init
__(self
)
312 self
._o
= wings
.WMCreateList(parent
._o
)
314 def allowEmptySelection(self
, flag
):
316 wings
.WMSetListAllowEmptySelection(self
._o
, flag
)
318 def allowMultipleSelection(self
, flag
):
320 wings
.WMSetListAllowMultipleSelection(self
._o
, flag
)
322 def addItem(self
, item
):
323 wings
.WMAddListItem(self
._o
, item
)
325 def insertItem(self
, row
, item
):
326 wings
.WMInsertListItem(self
._o
, row
, item
)
329 wings
.WMSortListItems(self
._o
)
331 def rowWithTitle(self
, title
):
332 return wings
.WMFindRowOfListItemWithTitle(self
._o
, title
)
334 def selectedItemRow(self
):
335 return wings
.WMGetListSelectedItemRow(self
._o
)
337 def selectedItem(self
):
338 return wings
.WMGetListSelectedItem(self
._o
)
340 def removeItem(self
, index
):
341 wings
.WMRemoveListItem(self
._o
, index
)
343 def selectItem(self
, index
):
344 wings
.WMSelectListItem(self
._o
, index
)
346 def unselectItem(self
, index
):
347 wings
.WMUnselectListItem(self
._o
, index
)
350 class WMTextFieldDelegate
:
351 __callbacks
= ('didBeginEditing', 'didChange', 'didEndEditing',
352 'shouldBeginEditing', 'shouldEndEditing')
355 self
.__dict
__['data'] = None
356 self
.__dict
__['didBeginEditing'] = None
357 self
.__dict
__['didChange'] = None
358 self
.__dict
__['didEndEditing'] = None
359 self
.__dict
__['shouldBeginEditing'] = None
360 self
.__dict
__['shouldEndEditing'] = None
362 def __setattr__(self
, name
,value
):
363 if name
in self
.__callbacks
and value
!=None and (not callable(value
)):
364 #raise AttributeError, "%s.%s needs to be a callable object or None" % (self.__class__.__name__, name)
365 raise Error
, "%s.%s needs to be a callable object or None" % (self
.__class
__.__name
__, name
)
367 self
.__dict
__[name
] = value
370 class WMTextField(WMWidget
):
371 def __init__(self
, parent
, text
=""):
372 WMWidget
.__init
__(self
)
373 self
._o
= wings
.WMCreateTextField(parent
._o
)
374 wings
.WMSetTextFieldText(self
._o
, text
)
376 def setDelegate(self
, delegate
):
377 if delegate
.__class
__ != WMTextFieldDelegate
:
378 raise Error
, "textfield delegate must be of type 'WMTextFieldDelegate'"
379 wings
.pyWMSetTextFieldDelegate(self
._o
, (self
, delegate
))
382 return wings
.pyWMGetTextFieldDelegate(self
._o
)
385 return wings
.WMGetTextFieldText(self
._o
)
387 def setEditable(self
, flag
):
389 wings
.WMSetTextFieldEditable(self
._o
, flag
)
391 def setBordered(self
, flag
):
393 wings
.WMSetTextFieldBordered(self
._o
, flag
)
395 def setBeveled(self
, flag
):
397 wings
.WMSetTextFieldBeveled(self
._o
, flag
)
399 def setSecure(self
, flag
):
401 wings
.WMSetTextFieldSecure(self
._o
, flag
)
403 def setCursorPosition(self
, position
):
404 wings
.WMSetTextFieldCursorPosition(self
._o
, position
)
406 def setNextText(self
, next
):
407 wings
.WMSetTextFieldNextTextField(self
._o
, next
._o
)
409 def setPreviousText(self
, previous
):
410 wings
.WMSetTextFieldPrevTextField(self
._o
, previous
._o
)
412 def setTextAlignment(self
, alignment
):
413 wings
.WMSetTextFieldAlignment(self
._o
, alignment
)
415 def isEditable(self
):
416 return wings
.WMGetTextFieldEditable(self
._o
)
418 def insertText(self
, text
, position
):
419 wings
.WMInsertTextFieldText(self
._o
, text
, position
)
421 def deleteText(self
, start
, count
):
422 wings
.WMDeleteTextFieldRange(self
._o
, wings
.wmkrange(start
, count
))
424 def selectText(self
, start
, count
):
425 wings
.WMSelectTextFieldRange(self
._o
, wings
.wmkrange(start
, count
))
427 def setFont(self
, font
):
428 wings
.WMSetTextFieldFont(self
._o
, font
)
431 return wings
.WMGetTextFieldFont(self
._o
)
434 ################################################################################
435 # wrap the WINGs constants so we don't need wings.constant_name
436 ################################################################################
438 # WMWindow title style
439 WMTitledWindowMask
= wings
.WMTitledWindowMask
440 WMClosableWindowMask
= wings
.WMClosableWindowMask
441 WMMiniaturizableWindowMask
= wings
.WMMiniaturizableWindowMask
442 WMResizableWindowMask
= wings
.WMResizableWindowMask
444 # WMFrame title positions
445 WTPNoTitle
= wings
.WTPNoTitle
446 WTPAboveTop
= wings
.WTPAboveTop
447 WTPAtTop
= wings
.WTPAtTop
448 WTPBelowTop
= wings
.WTPBelowTop
449 WTPAboveBottom
= wings
.WTPAboveBottom
450 WTPAtBottom
= wings
.WTPAtBottom
451 WTPBelowBottom
= wings
.WTPBelowBottom
454 WALeft
= wings
.WALeft
455 WACenter
= wings
.WACenter
456 WARight
= wings
.WARight
457 WAJustified
= wings
.WAJustified
# not valid for textfields
460 WIPNoImage
= wings
.WIPNoImage
461 WIPImageOnly
= wings
.WIPImageOnly
462 WIPLeft
= wings
.WIPLeft
463 WIPRight
= wings
.WIPRight
464 WIPBelow
= wings
.WIPBelow
465 WIPAbove
= wings
.WIPAbove
466 WIPOverlaps
= wings
.WIPOverlaps
469 WRFlat
= wings
.WRFlat
470 WRSimple
= wings
.WRSimple
471 WRRaised
= wings
.WRRaised
472 WRSunken
= wings
.WRSunken
473 WRGroove
= wings
.WRGroove
474 WRRidge
= wings
.WRRidge
475 WRPushed
= wings
.WRPushed
479 WMReturnTextMovement
= wings
.WMReturnTextMovement
480 WMEscapeTextMovement
= wings
.WMEscapeTextMovement
481 WMIllegalTextMovement
= wings
.WMIllegalTextMovement
482 WMTabTextMovement
= wings
.WMTabTextMovement
483 WMBacktabTextMovement
= wings
.WMBacktabTextMovement
484 WMLeftTextMovement
= wings
.WMLeftTextMovement
485 WMRightTextMovement
= wings
.WMRightTextMovement
486 WMUpTextMovement
= wings
.WMUpTextMovement
487 WMDownTextMovement
= wings
.WMDownTextMovement
490 if __name__
== "__main__":
495 def click(btn
, list):
496 print win
.width(), win
.height()
497 print list.selectedItemRow()
499 scr
.runModalLoop(win2
.view())
502 def sayhi(btn
, data
):
505 def breakLoop(btn
, data
):
510 def dc(object, data
, action
):
511 print "didChange:", object, data
, action
513 def dbe(object, data
, action
):
514 print "didBeginEditing:", object, data
, action
516 def dee(object, data
, action
):
517 if action
== wings
.WMReturnTextMovement
:
519 object.setFocusTo(txt2
)
521 object.setFocusTo(txt
)
522 print "didEndEditing:", object, data
, action
, object.text()
525 old
= list.selectedItemRow()
526 list.selectItem(list.index
)
527 list.unselectItem(old
)
528 list.index
= (list.index
+1) % 3
531 scr
= WMScreen("foobar")
532 win
= WMWindow(scr
, "aWindow")
533 win
.setCloseAction(quit
)
534 win
.setTitle("test window")
536 win
.setInitialPosition((scr
.width
-win
.width())/2, (scr
.height
-win
.height())/2)
538 btn
= WMCommandButton(win
)
539 btn
.setText("Click Me")
544 sw
= WMSwitchButton(win
)
545 sw
.setText("Some option")
553 for i
in ["One", "Two", "Four"]:
554 r
= WMRadioButton(win
, r
)
569 list.addItem("three")
570 list.allowMultipleSelection(1)
574 txtdel
= WMTextFieldDelegate()
575 txtdel
.data
= 'mydata'
576 txtdel
.didBeginEditing
= dbe
577 txtdel
.didEndEditing
= dee
578 txtdel
.didChange
= dc
580 txt
= WMTextField(win
, "abc")
583 txt
.setDelegate(txtdel
)
585 txt2
= WMTextField(win
, "01234567890")
588 txt2
.setDelegate(txtdel
)
591 txt
.setNextText(txt2
)
592 txt2
.setNextText(txt
)
594 label
= WMLabel(win
, "Text1:")
595 label
.setTextAlignment(WARight
)
600 label2
= WMLabel(win
, "mytext2:")
601 label2
.setTextAlignment(WARight
)
603 label2
.resize(55, 20)
606 btn
.setAction(click
, list)
608 frame
= WMFrame(win
, "My Frame")
609 frame
.resize(150, 50)
611 #frame.setRelief(WRPushed)
614 ebtn
= WMCommandButton(win
)
624 timer
= WMPersistentTimer(1000, tcb
, win
)
628 win2
= WMPanel(win
, "anotherWindow", WMTitledWindowMask
)
629 win2
.setTitle("transient test window")
631 win2
.setInitialPosition((scr
.width
-win2
.width())/2, (scr
.height
-win2
.height())/2)
633 btn7
= WMCommandButton(win2
)
634 btn7
.setText("Dismiss")
638 btn7
.setAction(breakLoop
)