8 # check about None as action for buttonAction/windowCloseAction ...
10 ################################################################################
12 ################################################################################
13 class Error(exceptions
.Exception):
14 def __init__(self
, msg
):
21 def __init__(self
, milliseconds
, callback
, cdata
=None, persistent
=0):
23 self
._o
= wings
.pyWMAddPersistentTimerHandler(milliseconds
, (callback
, cdata
))
25 self
._o
= wings
.pyWMAddTimerHandler(milliseconds
, (callback
, cdata
))
28 wings
.pyWMDeleteTimerHandler(self
._o
)
31 class WMPersistentTimer(WMTimer
):
32 def __init__(self
, milliseconds
, callback
, cdata
=None):
33 WMTimer
.__init
__(self
, milliseconds
, callback
, cdata
, persistent
=1)
37 __readonly
= ('display', 'width', 'height', 'depth')
39 def __init__(self
, appname
, display
="", simpleapp
=0):
40 wings
.WMInitializeApplication(appname
, len(sys
.argv
), sys
.argv
)
41 self
._o
= wings
.pyWMOpenScreen(display
, simpleapp
)
42 self
.__dict
__['display'] = wings
.WMScreenDisplay(self
._o
)
43 self
.__dict
__['width'] = wings
.WMScreenWidth(self
._o
)
44 self
.__dict
__['height'] = wings
.WMScreenHeight(self
._o
)
45 self
.__dict
__['depth'] = wings
.WMScreenDepth(self
._o
)
47 def __setattr__(self
, name
,value
):
48 if name
in self
.__readonly
:
49 #raise AttributeError, "'%s' is a read-only WMScreen attribute" % name
50 raise Error
, "'%s' is a read-only WMScreen attribute" % name
51 self
.__dict
__[name
] = value
54 wings
.pyWMScreenMainLoop(self
._o
)
56 def breakMainLoop(self
):
57 wings
.pyWMBreakScreenMainLoop(self
._o
)
59 def runModalLoop(self
, view
):
60 wings
.pyWMRunModalLoop(self
._o
, view
)
62 def breakModalLoop(self
):
63 wings
.WMBreakModalLoop(self
._o
)
66 return (self
.width
, self
.height
)
73 class WMWidget(WMView
):
76 if self
.__class
__ == WMWidget
:
77 raise Error
, "a WMWidget can't be instantiated directly"
81 wings
.WMDestroyWidget(self
._o
)
83 def resize(self
, width
, height
):
84 wings
.WMResizeWidget(self
._o
, width
, height
)
87 wings
.WMMoveWidget(self
._o
, x
, y
)
90 wings
.WMRealizeWidget(self
._o
)
93 wings
.WMMapWidget(self
._o
)
96 wings
.WMUnmapWidget(self
._o
)
99 wings
.WMRedisplayWidget(self
._o
)
102 return wings
.WMWidgetWidth(self
._o
)
105 return wings
.WMWidgetHeight(self
._o
)
108 return wings
.WMWidgetScreen(self
._o
)
111 return wings
.WMWidgetView(self
._o
)
113 def setFocusTo(self
, other
):
114 wings
.WMSetFocusToWidget(other
._o
)
117 class WMWindow(WMWidget
):
118 def __init__(self
, screen
, name
, style
=wings
.WMTitledWindowMask
119 |wings
.WMClosableWindowMask|wings
.WMMiniaturizableWindowMask
120 |wings
.WMResizableWindowMask
):
121 WMWidget
.__init
__(self
)
122 self
._o
= wings
.WMCreateWindowWithStyle(screen
._o
, name
, style
)
124 def setMinSize(self
, minWidth
, minHeight
):
125 wings
.WMSetWindowMinSize(self
._o
, minWidth
, minHeight
)
127 def setMaxSize(self
, maxWidth
, maxHeight
):
128 wings
.WMSetWindowMaxSize(self
._o
, maxWidth
, maxHeight
)
130 def setInitialPosition(self
, x
, y
):
131 wings
.WMSetWindowInitialPosition(self
._o
, x
, y
)
133 def setTitle(self
, title
):
134 wings
.WMSetWindowTitle(self
._o
, title
)
136 def setCloseAction(self
, action
, data
=None):
137 if action
!=None and (not callable(action
)):
138 raise Error
, "action needs to be a callable object or None"
139 wings
.pyWMSetWindowCloseAction(self
._o
, (self
, action
, data
))
142 class WMPanel(WMWindow
):
143 def __init__(self
, owner
, name
, style
=wings
.WMTitledWindowMask
144 |wings
.WMClosableWindowMask|wings
.WMResizableWindowMask
):
145 WMWidget
.__init
__(self
)
146 self
._o
= wings
.WMCreatePanelWithStyleForWindow(owner
._o
, name
, style
)
148 class WMFrame(WMWidget
):
149 def __init__(self
, parent
, title
=None):
150 WMWidget
.__init
__(self
)
151 self
._o
= wings
.WMCreateFrame(parent
._o
)
154 def setRelief(self
, relief
):
155 wings
.WMSetFrameRelief(self
._o
, relief
)
157 def setTitle(self
, title
=None):
159 assert type(title
)==StringType
160 wings
.WMSetFrameTitle(self
._o
, title
)
162 def setTitlePosition(self
, position
):
163 wings
.WMSetFrameTitlePosition(self
._o
, position
)
165 class WMLabel(WMWidget
):
166 def __init__(self
, parent
, text
=None):
167 WMWidget
.__init
__(self
)
168 self
._o
= wings
.WMCreateLabel(parent
._o
)
171 def setWraps(self
, flag
):
172 assert type(flag
)==IntType
173 wings
.WMSetLabelWraps(self
._o
, flag
)
175 def setRelief(self
, relief
):
176 wings
.WMSetLabelRelief(self
._o
, relief
)
178 def setText(self
, text
=None):
180 assert type(text
)==StringType
181 wings
.WMSetLabelText(self
._o
, text
)
183 def setTextColor(self
, color
):
184 wings
.WMSetLabelTextColor(self
._o
, color
)
186 def setFont(self
, font
):
187 wings
.WMSetLabelFont(self
._o
, font
)
189 def setTextAlignment(self
, alignment
):
190 wings
.WMSetLabelTextAlignment(self
._o
, alignment
)
192 def setImage(self
, image
):
193 wings
.WMSetLabelImage(self
._o
, image
)
195 def setImagePosition(self
, position
):
196 wings
.WMSetLabelImagePosition(self
._o
, position
)
199 return wings
.WMGetLabelText(self
._o
)
202 return wings
.WMGetLabelFont(self
._o
)
205 return wings
.WMGetLabelImage(self
._o
)
208 class WMBox(WMWidget
):
209 def __init__(self
, parent
):
210 WMWidget
.__init
__(self
)
211 self
._o
= wings
.WMCreateBox(parent
._o
)
213 def setHorizontal(self
, flag
):
214 assert type(flag
)==IntType
215 wings
.WMSetBoxHorizontal(self
._o
, flag
)
217 def setBorderWidth(self
, width
):
218 assert type(width
)==IntType
219 wings
.WMSetBoxBorderWidth(self
._o
, width
)
221 def addSubview(self
, view
, expand
, fill
, minSize
, maxSize
, space
):
222 wings
.WMAddBoxSubview(self
._o
, view
, expand
, fill
, minSize
, maxSixe
, space
)
224 def addSubviewAtEnd(self
, view
, expand
, fill
, minSize
, maxSize
, space
):
225 wings
.WMAddBoxSubviewAtEnd(self
._o
, view
, expand
, fill
, minSize
, maxSixe
, space
)
227 def removeSubview(self
, view
):
228 wings
.WMRemoveBoxSubview(self
._o
, view
)
231 class WMButton(WMWidget
): # not for user instantiation
232 def __init__(self
, parent
):
233 WMWidget
.__init
__(self
)
234 if self
.__class
__ == WMButton
:
235 raise Error
, "a WMButton can't be instantiated directly"
238 def setText(self
, text
):
239 assert type(text
) == StringType
240 wings
.WMSetButtonText(self
._o
, text
)
242 def setAction(self
, action
, data
=None):
243 if action
!=None and (not callable(action
)):
244 raise Error
, "action needs to be a callable object or None"
245 wings
.pyWMSetButtonAction(self
._o
, (self
, action
, data
))
247 def performClick(self
):
248 wings
.WMPerformButtonClick(self
._o
)
250 def setEnabled(self
, flag
):
251 assert type(flag
) == IntType
252 wings
.WMSetButtonEnabled(self
._o
, flag
)
255 return wings
.WMGetButtonEnabled(self
._o
)
257 def setImageDimsWhenDisabled(self
, flag
):
258 assert type(flag
) == IntType
259 wings
.WMSetButtonImageDimsWhenDisabled(self
._o
, flag
)
261 def setImage(self
, image
):
262 wings
.WMSetButtonImage(self_
.o
, image
)
264 def setAlternateImage(self
, image
):
265 wings
.WMSetButtonAltImage(self
._o
, image
)
267 def setImagePosition(self
, position
):
268 wings
.WMSetButtonImagePosition(self
._o
, position
)
270 def setImageDefault(self
):
271 wings
.WMSetButtonImageDefault(self
._o
)
274 class WMCommandButton(WMButton
):
275 def __init__(self
, parent
):
276 WMButton
.__init
__(self
, parent
)
277 self
._o
= wings
.WMCreateCommandButton(parent
._o
)
280 class WMSwitchButton(WMButton
):
281 def __init__(self
, parent
):
282 WMButton
.__init
__(self
, parent
)
283 self
._o
= wings
.WMCreateSwitchButton(parent
._o
)
286 class WMRadioButton(WMButton
):
287 def __init__(self
, parent
, group
=None):
288 WMButton
.__init
__(self
, parent
)
289 self
._o
= wings
.WMCreateRadioButton(parent
._o
)
291 wings
.WMGroupButtons(group
._o
, self
._o
)
298 class WMList(WMWidget
):
299 def __init__(self
, parent
):
300 WMWidget
.__init
__(self
)
301 self
._o
= wings
.WMCreateList(parent
._o
)
303 def allowEmptySelection(self
, flag
):
304 assert type(flag
) == IntType
305 wings
.WMSetListAllowEmptySelection(self
._o
, flag
)
307 def allowMultipleSelection(self
, flag
):
308 assert type(flag
) == IntType
309 wings
.WMSetListAllowMultipleSelection(self
._o
, flag
)
311 def addItem(self
, item
):
312 assert type(item
) == StringType
313 wings
.WMAddListItem(self
._o
, item
)
315 def insertItem(self
, row
, item
):
316 assert type(row
) == IntType
and type(item
) == StringType
317 wings
.WMAddListItem(self
._o
, item
)
320 wings
.WMSortListItems(self
._o
)
322 def rowWithTitle(self
, title
):
323 assert type(title
) == StringType
324 return wings
.WMFindRowOfListItemWithTitle(self
._o
, title
)
326 def selectedItemRow(self
):
327 return wings
.WMGetListSelectedItemRow(self
._o
)
329 def selectedItem(self
):
330 return wings
.WMGetListSelectedItem(self
._o
)
332 def removeItem(self
, index
):
333 assert type(index
)==IntType
334 wings
.WMRemoveListItem(self
._o
, index
)
336 def selectItem(self
, index
):
337 assert type(index
)==IntType
338 wings
.WMSelectListItem(self
._o
, index
)
340 def unselectItem(self
, index
):
341 assert type(index
)==IntType
342 wings
.WMUnselectListItem(self
._o
, index
)
345 class WMTextFieldDelegate
:
346 __callbacks
= ('didBeginEditing', 'didChange', 'didEndEditing',
347 'shouldBeginEditing', 'shouldEndEditing')
350 self
.__dict
__['data'] = None
351 self
.__dict
__['didBeginEditing'] = None
352 self
.__dict
__['didChange'] = None
353 self
.__dict
__['didEndEditing'] = None
354 self
.__dict
__['shouldBeginEditing'] = None
355 self
.__dict
__['shouldEndEditing'] = None
357 def __setattr__(self
, name
,value
):
358 if name
in self
.__callbacks
and value
!=None and (not callable(value
)):
359 #raise AttributeError, "%s.%s needs to be a callable object or None" % (self.__class__.__name__, name)
360 raise Error
, "%s.%s needs to be a callable object or None" % (self
.__class
__.__name
__, name
)
362 self
.__dict
__[name
] = value
365 class WMTextField(WMWidget
):
366 def __init__(self
, parent
, text
=None):
367 WMWidget
.__init
__(self
)
368 self
._o
= wings
.WMCreateTextField(parent
._o
)
370 assert type(text
) == StringType
371 wings
.WMSetTextFieldText(self
._o
, text
)
373 def setDelegate(self
, delegate
):
374 if delegate
.__class
__ != WMTextFieldDelegate
:
375 raise Error
, "textfield delegate must be of type 'WMTextFieldDelegate'"
376 wings
.pyWMSetTextFieldDelegate(self
._o
, (self
, delegate
))
379 return wings
.pyWMGetTextFieldDelegate(self
._o
)
382 return wings
.WMGetTextFieldText(self
._o
)
384 def setEditable(self
, flag
):
385 assert type(flag
) == IntType
386 wings
.WMSetTextFieldEditable(self
._o
, flag
)
388 def setBordered(self
, flag
):
389 assert type(flag
) == IntType
390 wings
.WMSetTextFieldBordered(self
._o
, flag
)
392 def setBeveled(self
, flag
):
393 assert type(flag
) == IntType
394 wings
.WMSetTextFieldBeveled(self
._o
, flag
)
396 def setSecure(self
, flag
):
397 assert type(flag
) == IntType
398 wings
.WMSetTextFieldSecure(self
._o
, flag
)
400 def setCursorPosition(self
, position
):
401 assert type(position
) == IntType
402 wings
.WMSetTextFieldCursorPosition(self
._o
, position
)
404 def setNextText(self
, next
):
405 wings
.WMSetTextFieldNextTextField(self
._o
, next
._o
)
407 def setPreviousText(self
, previous
):
408 wings
.WMSetTextFieldPrevTextField(self
._o
, previous
._o
)
410 def setTextAlignment(self
, alignment
):
411 wings
.WMSetTextFieldAlignment(self
._o
, alignment
)
413 def isEditable(self
):
414 return wings
.WMGetTextFieldEditable(self
._o
)
416 def insertText(self
, text
, position
):
417 assert type(text
)==StringType
and type(position
)==IntType
418 wings
.WMInsertTextFieldText(self
._o
, text
, position
)
420 def deleteText(self
, start
, count
):
421 assert type(start
)==IntType
and type(count
)==IntType
422 wings
.WMDeleteTextFieldRange(self
._o
, wings
.wmkrange(start
, count
))
424 def selectText(self
, start
, count
):
425 assert type(start
)==IntType
and type(count
)==IntType
426 wings
.WMSelectTextFieldRange(self
._o
, wings
.wmkrange(start
, count
))
428 def setFont(self
, font
):
429 wings
.WMSetTextFieldFont(self
._o
, font
)
432 return wings
.WMGettextFieldFont(self
._o
)
435 ################################################################################
436 # wrap the WINGs constants so we don't need wings.constant_name
437 ################################################################################
439 # WMWindow title style
440 WMTitledWindowMask
= wings
.WMTitledWindowMask
441 WMClosableWindowMask
= wings
.WMClosableWindowMask
442 WMMiniaturizableWindowMask
= wings
.WMMiniaturizableWindowMask
443 WMResizableWindowMask
= wings
.WMResizableWindowMask
445 # WMFrame title positions
446 WTPNoTitle
= wings
.WTPNoTitle
447 WTPAboveTop
= wings
.WTPAboveTop
448 WTPAtTop
= wings
.WTPAtTop
449 WTPBelowTop
= wings
.WTPBelowTop
450 WTPAboveBottom
= wings
.WTPAboveBottom
451 WTPAtBottom
= wings
.WTPAtBottom
452 WTPBelowBottom
= wings
.WTPBelowBottom
455 WALeft
= wings
.WALeft
456 WACenter
= wings
.WACenter
457 WARight
= wings
.WARight
458 WAJustified
= wings
.WAJustified
# not valid for textfields
461 WIPNoImage
= wings
.WIPNoImage
462 WIPImageOnly
= wings
.WIPImageOnly
463 WIPLeft
= wings
.WIPLeft
464 WIPRight
= wings
.WIPRight
465 WIPBelow
= wings
.WIPBelow
466 WIPAbove
= wings
.WIPAbove
467 WIPOverlaps
= wings
.WIPOverlaps
470 WRFlat
= wings
.WRFlat
471 WRSimple
= wings
.WRSimple
472 WRRaised
= wings
.WRRaised
473 WRSunken
= wings
.WRSunken
474 WRGroove
= wings
.WRGroove
475 WRRidge
= wings
.WRRidge
476 WRPushed
= wings
.WRPushed
480 WMReturnTextMovement
= wings
.WMReturnTextMovement
481 WMEscapeTextMovement
= wings
.WMEscapeTextMovement
482 WMIllegalTextMovement
= wings
.WMIllegalTextMovement
483 WMTabTextMovement
= wings
.WMTabTextMovement
484 WMBacktabTextMovement
= wings
.WMBacktabTextMovement
485 WMLeftTextMovement
= wings
.WMLeftTextMovement
486 WMRightTextMovement
= wings
.WMRightTextMovement
487 WMUpTextMovement
= wings
.WMUpTextMovement
488 WMDownTextMovement
= wings
.WMDownTextMovement
491 if __name__
== "__main__":
496 def click(btn
, list):
497 print win
.width(), win
.height()
498 print list.selectedItemRow()
500 scr
.runModalLoop(win2
.view())
503 def sayhi(btn
, data
):
506 def breakLoop(btn
, data
):
511 def dc(object, data
, action
):
512 print "didChnage:", object, data
, action
514 def dbe(object, data
, action
):
515 print "didBeginEditing:", object, data
, action
517 def dee(object, data
, action
):
518 if action
== wings
.WMReturnTextMovement
:
520 object.setFocusTo(txt2
)
522 object.setFocusTo(txt
)
523 print "didEndEditing:", object, data
, action
, txt
.text()
526 old
= list.selectedItemRow()
527 list.selectItem(list.index
)
528 list.unselectItem(old
)
529 list.index
= (list.index
+1) % 3
532 scr
= WMScreen("foobar")
533 win
= WMWindow(scr
, "aWindow")
534 win
.setCloseAction(quit
)
535 win
.setTitle("test window")
537 win
.setInitialPosition((scr
.width
-win
.width())/2, (scr
.height
-win
.height())/2)
539 btn
= WMCommandButton(win
)
540 btn
.setText("Click Me")
545 sw
= WMSwitchButton(win
)
546 sw
.setText("Some option")
554 for i
in ["One", "Two", "Four"]:
555 r
= WMRadioButton(win
, r
)
570 list.addItem("three")
571 list.allowMultipleSelection(1)
575 txtdel
= WMTextFieldDelegate()
576 txtdel
.data
= 'mydata'
577 txtdel
.didBeginEditing
= dbe
578 txtdel
.didEndEditing
= dee
579 txtdel
.didChange
= dc
581 txt
= WMTextField(win
, "abc")
584 txt
.setDelegate(txtdel
)
586 txt2
= WMTextField(win
, "01234567890")
589 txt2
.setDelegate(txtdel
)
592 txt
.setNextText(txt2
)
593 txt2
.setNextText(txt
)
595 label
= WMLabel(win
, "Text1:")
596 label
.setTextAlignment(WARight
)
601 label2
= WMLabel(win
, "mytext2:")
602 label2
.setTextAlignment(WARight
)
604 label2
.resize(55, 20)
607 btn
.setAction(click
, list)
609 frame
= WMFrame(win
, "My Frame")
610 frame
.resize(150, 50)
612 #frame.setRelief(WRPushed)
615 ebtn
= WMCommandButton(win
)
625 timer
= WMPersistentTimer(1000, tcb
, win
)
629 win2
= WMPanel(win
, "anotherWindow", WMTitledWindowMask
)
630 win2
.setTitle("transient test window")
632 win2
.setInitialPosition((scr
.width
-win2
.width())/2, (scr
.height
-win2
.height())/2)
634 btn7
= WMCommandButton(win2
)
635 btn7
.setText("Dismiss")
639 btn7
.setAction(breakLoop
)