root/trunk/RBPrivate/Prototypes/RBwin32/WindowBase.py

Revision 352, 6.3 kB (checked in by sholloway, 6 years ago)

*** empty log message ***

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 import sys
27 import win32gui
28 import win32api
29 import win32con
30 from Foundation.AspectOriented import Aspect
31
32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 #~ Definitions
34 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35
36 WinStr = win32gui.UNICODE and unicode or str
37
38 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39
40 class WindowFactory(object):
41     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42     #~ Constants / Variables / Etc.
43     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
45     WindowAtom = None
46     messagemap = {}
47
48     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49     #~ Public Methods
50     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
52     def __init__(self, WindowAtom=None):
53         self.WindowAtom = WindowAtom
54
55     def __call__(self, *args, **kw):
56         return self.Create(*args, **kw)
57
58     def Create(self, title, style, pos, size, parent, menu=0, reserved=None):
59         args = (WinStr(title), style)
60         args += pos + size
61         args += (parent, menu, self.GetInstance(), reserved)
62         return win32gui.CreateWindow(self.WindowAtom, *args)
63
64     def GetInstance():
65         try: result = win32gui.GetModuleHandle(None)
66         except AttributeError: result = sys.hinst
67         return result
68     GetInstance = staticmethod(GetInstance)
69
70 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
72 class RegisteredWindowFactory(WindowFactory):
73     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74     #~ Constants / Variables / Etc.
75     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76
77     style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
78     background = win32con.COLOR_WINDOW + 1
79     cursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW)
80     messagemap = {win32con.WM_DESTROY: lambda hwnd, msg, wparam, lparam: win32gui.PostQuitMessage(0)}
81
82     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83     #~ Public Methods
84     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85
86     def __init__(self, WindowClassName=None, RegsiterClass=1):
87         WindowFactory.__init__(self, None)
88         if WindowClassName is not None:
89             self.name = WindowClassName
90         if RegsiterClass:
91             self.Register()
92
93     def __del__(self):
94         if WindowAtom is not None:
95             win32gui.UnregisterClass(WindowAtom, self.GetInstance())
96
97     def Register(self):
98         wc = win32gui.WNDCLASS()
99         wc.lpfnWndProc = self.messagemap
100
101         wc.hInstance = self.GetInstance()
102         wc.style = self.style
103         wc.hbrBackground = self.background
104         wc.hCursor = self.cursor
105         wc.lpszClassName = WinStr('_RBF_' + self.name)
106         self.WindowAtom = win32gui.RegisterClass(wc)
107
108 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109
110 class win32BindCall(object):
111     def __init__(self, bound, *args):
112         self.bound = bound
113         self.args = args
114     def __call__(self, *args, **kw):
115         return self.bound(*(self.args + args), **kw)
116
117 class win32method(object):
118     def __init__(self, bound):
119         self.bound = bound
120     def __get__(self, instance, Class):
121         if instance: return win32BindCall(self.bound, instance.WindowHandle)
122         else: return self.bound
123
124 class Win32guiAspect(Aspect.Aspect):
125     GetClientRect = win32method(win32gui.GetClientRect)
126     ShowWindow = win32method(win32gui.ShowWindow)
127     UpdateWindow = win32method(win32gui.UpdateWindow)
128     DestroyWindow = win32method(win32gui.DestroyWindow)
129     GetWindowText = win32method(win32gui.GetWindowText)
130     SetWindowText = win32method(win32gui.SetWindowText)
131     # Aliases
132     Refresh = UpdateWindow
133     Destroy = DestroyWindow
134     GetText = GetWindowText
135     SetText = SetWindowText
136     def Show(self, bShow=win32con.SW_SHOW):
137         if bShow == 1: self.ShowWindow(win32con.SW_SHOW)
138         elif bShow == 0: self.ShowWindow(win32con.SW_HIDE)
139         else: self.ShowWindow(bShow)
140
141 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142
143 class WindowBase(object):
144     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145     #~ Public Methods
146     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147
148     def __init__(self, parent, title, pos, size, style):
149         if parent:
150             assert isinstance(parent, WindowBase)
151             parent = long(parent.WindowHandle)
152         else: parent = 0
153         self.WindowHandle = self.windowfactory(title, style, pos, size, parent)
154         if self.messagemap is None:
155             self.messagemap = self.windowfactory.messagemap.copy()
156         else:
157             self._SetMessageMap(self.messagemap)
158
159     def __repr__(self):
160         return "<%s hwnd=%s>" % (self.__class__.__name__, self.WindowHandle)
161
162     def __eq__(self, other):
163         if isinstance(other, WindowBase):
164             return self.WindowHandle == other.WindowHandle
165         else: return self.WindowHandle == other
166     def __ne__(self, other):
167         return not self.__eq__(other)
168
169     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170     #~ Public Methods
171     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172
173     messagemap = None
174
175     def _GetMessageMap(self):
176         return win32api.GetWindowLong(self.WindowHandle, win32con.GWL_WNDPROC)
177     def _SetMessageMap(self, messagemap):
178         return win32gui.SetWindowLong(self.WindowHandle, win32con.GWL_WNDPROC, messagemap)
179     def UpdateMessageMap(self, messagemap):
180         self.messagemap.update(messagemap)
181         return self._SetMessageMap(messagemap)
182     def SetMessageMap(self, messagemap):
183         self.messagemap.clear()
184         return self.UpdateMessageMap(messagemap)
185
186
Note: See TracBrowser for help on using the browser.