- Check whether libXft is at least version 2.1.2 else refuse to compile.
[wmaker-crm.git] / WINGs / python / WINGs.py
blobdbdccde2a6f53b35cee60f6c5c4d8300bc3218ff
1 #!/usr/bin/env python
3 import sys
4 import wings
6 # Some useful constants
8 False = 0
9 True = 1
11 # check about None as action for buttonAction/windowCloseAction ...
13 ################################################################################
14 # Exceptions
15 ################################################################################
16 from exceptions import Exception, StandardError
18 class Error(StandardError):
19 pass
21 del Exception, StandardError
23 class WMTimer:
24 def __init__(self, milliseconds, callback, cdata=None, persistent=False):
25 if persistent:
26 self._o = wings.pyWMAddPersistentTimerHandler(milliseconds, (callback, cdata))
27 else:
28 self._o = wings.pyWMAddTimerHandler(milliseconds, (callback, cdata))
29 self.__WMDeleteTimerHandler = wings.pyWMDeleteTimerHandler
31 def __del__(self):
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)
40 class WMScreen:
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)
46 if not self._o:
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
63 try:
64 del(self.__dict__[name])
65 except KeyError:
66 raise AttributeError, "%s instance has no attribute '%s'" % \
67 (self.__class__.__name__, name)
69 def mainLoop(self):
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)
81 def size(self):
82 return (self.width, self.height)
85 class WMView:
86 pass
89 class WMWidget(WMView):
90 def __init__(self):
91 if self.__class__ == WMWidget:
92 raise Error, "a WMWidget can't be instantiated directly"
93 self._o = None
94 self.__WMDestroyWidget = wings.WMDestroyWidget
96 def __del__(self):
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)
106 def realize(self):
107 wings.WMRealizeWidget(self._o)
109 def show(self):
110 wings.WMMapWidget(self._o)
112 def hide(self):
113 wings.WMUnmapWidget(self._o)
115 def redisplay(self):
116 wings.WMRedisplayWidget(self._o)
118 def width(self):
119 return wings.WMWidgetWidth(self._o)
121 def height(self):
122 return wings.WMWidgetHeight(self._o)
124 def screen(self):
125 return wings.WMWidgetScreen(self._o)
127 def view(self):
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)
169 self.setTitle(title)
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)
184 self.setText(text)
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)
211 def text(self):
212 return wings.WMGetLabelText(self._o)
214 def font(self):
215 return wings.WMGetLabelFont(self._o)
217 def image(self):
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)
265 def isEnabled(self):
266 return wings.WMGetButtonEnabled(self._o)
268 def setImageDimsWhenDisabled(self, flag):
269 # bool(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)
301 if group:
302 wings.WMGroupButtons(group._o, self._o)
305 class WMListItem:
306 pass
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):
315 # bool(flag)
316 wings.WMSetListAllowEmptySelection(self._o, flag)
318 def allowMultipleSelection(self, flag):
319 # bool(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)
328 def sortItems(self):
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')
354 def __init__(self):
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)
366 else:
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))
381 def delegate(self):
382 return wings.pyWMGetTextFieldDelegate(self._o)
384 def text(self):
385 return wings.WMGetTextFieldText(self._o)
387 def setEditable(self, flag):
388 # bool(flag)
389 wings.WMSetTextFieldEditable(self._o, flag)
391 def setBordered(self, flag):
392 # bool(flag)
393 wings.WMSetTextFieldBordered(self._o, flag)
395 def setBeveled(self, flag):
396 # bool(flag)
397 wings.WMSetTextFieldBeveled(self._o, flag)
399 def setSecure(self, flag):
400 # bool(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)
430 def font(self):
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
453 # Alingments
454 WALeft = wings.WALeft
455 WACenter = wings.WACenter
456 WARight = wings.WARight
457 WAJustified = wings.WAJustified # not valid for textfields
459 # Image positions
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
468 # Relief types
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
478 # TextField events
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__":
491 def quit(obj, data):
492 #sys.exit()
493 scr.breakMainLoop()
495 def click(btn, list):
496 print win.width(), win.height()
497 print list.selectedItemRow()
498 win2.show()
499 scr.runModalLoop(win2.view())
500 print txt2.text()
502 def sayhi(btn, data):
503 print "hi"
505 def breakLoop(btn, data):
506 #sys.exit()
507 scr.breakModalLoop()
508 win2.hide()
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:
518 if object == txt:
519 object.setFocusTo(txt2)
520 else:
521 object.setFocusTo(txt)
522 print "didEndEditing:", object, data, action, object.text()
524 def tcb(one):
525 old = list.selectedItemRow()
526 list.selectItem(list.index)
527 list.unselectItem(old)
528 list.index = (list.index+1) % 3
529 #print one
531 scr = WMScreen("foobar")
532 win = WMWindow(scr, "aWindow")
533 win.setCloseAction(quit)
534 win.setTitle("test window")
535 win.resize(400, 180)
536 win.setInitialPosition((scr.width-win.width())/2, (scr.height-win.height())/2)
538 btn = WMCommandButton(win)
539 btn.setText("Click Me")
540 btn.resize(100, 25)
541 btn.move(20, 20)
542 btn.show()
544 sw = WMSwitchButton(win)
545 sw.setText("Some option")
546 sw.resize(100, 25)
547 sw.move(20, 50)
548 sw.show()
550 radios = []
551 r = None
552 j = 0
553 for i in ["One", "Two", "Four"]:
554 r = WMRadioButton(win, r)
555 radios.append(r)
556 r.show()
557 r.setText(i)
558 r.move(20, 70+j*25)
559 r.resize(100, 25)
560 j=j+1
562 sw.setAction(sayhi)
564 list = WMList(win)
565 list.resize(100,100)
566 list.move(130, 20)
567 list.addItem("one")
568 list.addItem("two")
569 list.addItem("three")
570 list.allowMultipleSelection(1)
571 list.show()
572 list.index = 0
574 txtdel = WMTextFieldDelegate()
575 txtdel.data = 'mydata'
576 txtdel.didBeginEditing = dbe
577 txtdel.didEndEditing = dee
578 txtdel.didChange = dc
580 txt = WMTextField(win, "abc")
581 txt.resize(95, 20)
582 txt.move(295, 20)
583 txt.setDelegate(txtdel)
584 txt.show()
585 txt2 = WMTextField(win, "01234567890")
586 txt2.resize(95, 20)
587 txt2.move(295, 45)
588 txt2.setDelegate(txtdel)
589 txt2.show()
591 txt.setNextText(txt2)
592 txt2.setNextText(txt)
594 label = WMLabel(win, "Text1:")
595 label.setTextAlignment(WARight)
596 label.move(240, 20)
597 label.resize(55, 20)
598 label.show()
600 label2 = WMLabel(win, "mytext2:")
601 label2.setTextAlignment(WARight)
602 label2.move(240, 45)
603 label2.resize(55, 20)
604 label2.show()
606 btn.setAction(click, list)
608 frame = WMFrame(win, "My Frame")
609 frame.resize(150, 50)
610 frame.move(240, 70)
611 #frame.setRelief(WRPushed)
612 frame.show()
614 ebtn = WMCommandButton(win)
615 ebtn.setText("Exit")
616 ebtn.resize(100, 25)
617 ebtn.move(290, 147)
618 ebtn.setAction(quit)
619 ebtn.show()
621 win.realize()
622 win.show()
624 timer = WMPersistentTimer(1000, tcb, win)
625 #del(timer)
626 #timer.delete()
628 win2 = WMPanel(win, "anotherWindow", WMTitledWindowMask)
629 win2.setTitle("transient test window")
630 win2.resize(150, 50)
631 win2.setInitialPosition((scr.width-win2.width())/2, (scr.height-win2.height())/2)
633 btn7 = WMCommandButton(win2)
634 btn7.setText("Dismiss")
635 btn7.resize(100, 25)
636 btn7.move(27, 10)
637 btn7.show()
638 btn7.setAction(breakLoop)
640 win2.realize()
642 scr.mainLoop()