- Added a python wrapper module for WINGs
[wmaker-crm.git] / WINGs / python / WINGs.py
blobb131e58cdc3494f78f2f471bd0f9c071a0b2334c
1 #!/usr/bin/env python
3 import sys
4 import wings
5 import exceptions
6 from types import *
8 # check about None as action for buttonAction/windowCloseAction ...
10 ################################################################################
11 # Exceptions
12 ################################################################################
13 class Error(exceptions.Exception):
14 def __init__(self, msg):
15 self.msg = msg
16 def __str__(self):
17 return self.msg
18 __repr__ = __str__
20 class WMTimer:
21 def __init__(self, milliseconds, callback, cdata=None, persistent=0):
22 if persistent:
23 self._o = wings.pyWMAddPersistentTimerHandler(milliseconds, (callback, cdata))
24 else:
25 self._o = wings.pyWMAddTimerHandler(milliseconds, (callback, cdata))
27 def __del__(self):
28 wings.pyWMDeleteTimerHandler(self._o)
29 #delete = __del__
31 class WMPersistentTimer(WMTimer):
32 def __init__(self, milliseconds, callback, cdata=None):
33 WMTimer.__init__(self, milliseconds, callback, cdata, persistent=1)
36 class WMScreen:
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
53 def mainLoop(self):
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)
65 def size(self):
66 return (self.width, self.height)
69 class WMView:
70 pass
73 class WMWidget(WMView):
74 def __init__(self):
75 self._o = None
76 if self.__class__ == WMWidget:
77 raise Error, "a WMWidget can't be instantiated directly"
79 def __del__(self):
80 if (self._o != None):
81 wings.WMDestroyWidget(self._o)
83 def resize(self, width, height):
84 wings.WMResizeWidget(self._o, width, height)
86 def move(self, x, y):
87 wings.WMMoveWidget(self._o, x, y)
89 def realize(self):
90 wings.WMRealizeWidget(self._o)
92 def show(self):
93 wings.WMMapWidget(self._o)
95 def hide(self):
96 wings.WMUnmapWidget(self._o)
98 def redisplay(self):
99 wings.WMRedisplayWidget(self._o)
101 def width(self):
102 return wings.WMWidgetWidth(self._o)
104 def height(self):
105 return wings.WMWidgetHeight(self._o)
107 def screen(self):
108 return wings.WMWidgetScreen(self._o)
110 def view(self):
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)
152 self.setTitle(title)
154 def setRelief(self, relief):
155 wings.WMSetFrameRelief(self._o, relief)
157 def setTitle(self, title=None):
158 title = title or ""
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)
169 self.setText(text)
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):
179 text = text or ""
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)
198 def text(self):
199 return wings.WMGetLabelText(self._o)
201 def font(self):
202 return wings.WMGetLabelFont(self._o)
204 def image(self):
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)
254 def isEnabled(self):
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)
290 if group:
291 wings.WMGroupButtons(group._o, self._o)
294 class WMListItem:
295 pass
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)
319 def sortItems(self):
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')
349 def __init__(self):
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)
361 else:
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)
369 text = text or ""
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))
378 def delegate(self):
379 return wings.pyWMGetTextFieldDelegate(self._o)
381 def text(self):
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)
431 def font(self):
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
454 # Alingments
455 WALeft = wings.WALeft
456 WACenter = wings.WACenter
457 WARight = wings.WARight
458 WAJustified = wings.WAJustified # not valid for textfields
460 # Image positions
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
469 # Relief types
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
479 # TextField events
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__":
492 def quit(obj, data):
493 #sys.exit()
494 scr.breakMainLoop()
496 def click(btn, list):
497 print win.width(), win.height()
498 print list.selectedItemRow()
499 win2.show()
500 scr.runModalLoop(win2.view())
501 print txt2.text()
503 def sayhi(btn, data):
504 print "hi"
506 def breakLoop(btn, data):
507 #sys.exit()
508 scr.breakModalLoop()
509 win2.hide()
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:
519 if object == txt:
520 object.setFocusTo(txt2)
521 else:
522 object.setFocusTo(txt)
523 print "didEndEditing:", object, data, action, txt.text()
525 def tcb(one):
526 old = list.selectedItemRow()
527 list.selectItem(list.index)
528 list.unselectItem(old)
529 list.index = (list.index+1) % 3
530 #print one
532 scr = WMScreen("foobar")
533 win = WMWindow(scr, "aWindow")
534 win.setCloseAction(quit)
535 win.setTitle("test window")
536 win.resize(400, 180)
537 win.setInitialPosition((scr.width-win.width())/2, (scr.height-win.height())/2)
539 btn = WMCommandButton(win)
540 btn.setText("Click Me")
541 btn.resize(100, 25)
542 btn.move(20, 20)
543 btn.show()
545 sw = WMSwitchButton(win)
546 sw.setText("Some option")
547 sw.resize(100, 25)
548 sw.move(20, 50)
549 sw.show()
551 radios = []
552 r = None
553 j = 0
554 for i in ["One", "Two", "Four"]:
555 r = WMRadioButton(win, r)
556 radios.append(r)
557 r.show()
558 r.setText(i)
559 r.move(20, 70+j*25)
560 r.resize(100, 25)
561 j=j+1
563 sw.setAction(sayhi)
565 list = WMList(win)
566 list.resize(100,100)
567 list.move(130, 20)
568 list.addItem("one")
569 list.addItem("two")
570 list.addItem("three")
571 list.allowMultipleSelection(1)
572 list.show()
573 list.index = 0
575 txtdel = WMTextFieldDelegate()
576 txtdel.data = 'mydata'
577 txtdel.didBeginEditing = dbe
578 txtdel.didEndEditing = dee
579 txtdel.didChange = dc
581 txt = WMTextField(win, "abc")
582 txt.resize(95, 20)
583 txt.move(295, 20)
584 txt.setDelegate(txtdel)
585 txt.show()
586 txt2 = WMTextField(win, "01234567890")
587 txt2.resize(95, 20)
588 txt2.move(295, 45)
589 txt2.setDelegate(txtdel)
590 txt2.show()
592 txt.setNextText(txt2)
593 txt2.setNextText(txt)
595 label = WMLabel(win, "Text1:")
596 label.setTextAlignment(WARight)
597 label.move(240, 20)
598 label.resize(55, 20)
599 label.show()
601 label2 = WMLabel(win, "mytext2:")
602 label2.setTextAlignment(WARight)
603 label2.move(240, 45)
604 label2.resize(55, 20)
605 label2.show()
607 btn.setAction(click, list)
609 frame = WMFrame(win, "My Frame")
610 frame.resize(150, 50)
611 frame.move(240, 70)
612 #frame.setRelief(WRPushed)
613 frame.show()
615 ebtn = WMCommandButton(win)
616 ebtn.setText("Exit")
617 ebtn.resize(100, 25)
618 ebtn.move(290, 147)
619 ebtn.setAction(quit)
620 ebtn.show()
622 win.realize()
623 win.show()
625 timer = WMPersistentTimer(1000, tcb, win)
626 #del(timer)
627 #timer.delete()
629 win2 = WMPanel(win, "anotherWindow", WMTitledWindowMask)
630 win2.setTitle("transient test window")
631 win2.resize(150, 50)
632 win2.setInitialPosition((scr.width-win2.width())/2, (scr.height-win2.height())/2)
634 btn7 = WMCommandButton(win2)
635 btn7.setText("Dismiss")
636 btn7.resize(100, 25)
637 btn7.move(27, 10)
638 btn7.show()
639 btn7.setAction(breakLoop)
641 win2.realize()
643 scr.mainLoop()