]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/wxpTag.py
a4f372ec3fa770d1d68516cc4e98fb723d4e5206
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.wxpTag
3 # Purpose: A wxHtmlTagHandler that knows how to build and place
4 # wxPython widgets onto web pages.
8 # Created: 13-Sept-1999
10 # Copyright: (c) 1999 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
17 This module contains a wxHtmlTagHandler that knows how to build
18 and place wxPython widgets onto wxHtmlWindow web pages.
20 You don\'t need to use anything in this module directly, just
21 importing it will create the tag handler and add it to any
22 wxHtmlWinParsers created from that time forth.
24 Tags of the following form are recognised:
26 <WXP class="classname" [module="modulename"] [width="num"] [height="num"]>
27 <PARAM name="parameterName" value="parameterValue>
31 where modulename is the name of a module (possibly in package
32 notation) to import and classname is the name of a class in that
33 module to create an instance of. If the module tag-attribute is not
34 given or is an empty string, then wxPython.wx is used. The width and
35 height attributes are expected to be integers and will be passed to
36 the __init__ method of the class as a wxSize object named size.
37 However, if the width attribute ends with the percent (%) symbol then
38 the value will be used as a percentage of the available width and the
39 wxHtmlWindow will manage the size.
41 The name-value pairs in all the nested PARAM tags are packaged up as
42 strings into a python dictionary and passed to the __init__ method of
43 the class as keyword arguments. This means that they are all
44 accessible from the __init__ method as regular parameters, or you use
45 the special Python **kw syntax in your __init__ method to get the
48 Some parameter values are special and if they are present then they will
49 be converted from strings to alternate datatypes. They are:
51 id If the value of id can be converted to an integer, it will
52 be. Otherwise it is assumed to be the name of an integer
53 variable in the module.
55 colours Any value of the form "#123ABC" will automatically be
56 converted to a wxColour object.
58 Py Types Any value begining with "(", "[" or "{" are expected to
59 be a Python tuple, list, or dictionary and eval()
60 will be used to convert them to that type. If the
61 eval() fails then the original string value will be
64 wx Types Any value begining with "wx" is expected to be an attempt
65 to create a wxPython object, such as a wxSize, etc.
66 The eval() will be used to try and construct the
67 object and if it fails then the original string value
72 <wxp module="" class="wxButton">
73 <param name="label" value="Click here">
74 <param name="id" value="wxID_OK">
77 Both the begining and ending WXP tags are required.
79 In the future support will be added for another tag that can be
80 embedded between the two begining and ending WXP tags and will
81 facilitate calling methods of the widget to help initialize it.
82 Additionally, support may be added to fetch the module from a web
83 server as is done with java applets.
86 #----------------------------------------------------------------------
88 from wxPython
.wx
import *
89 from wxPython
.html
import *
95 #----------------------------------------------------------------------
100 #----------------------------------------------------------------------
102 class wxpTagHandler(wxHtmlWinTagHandler
):
104 wxHtmlWinTagHandler
.__init
__(self
)
107 def GetSupportedTags(self
):
108 return WXPTAG
+','+PARAMTAG
111 def HandleTag(self
, tag
):
114 return self
.HandleWxpTag(tag
)
115 elif name
== PARAMTAG
:
116 return self
.HandleParamTag(tag
)
118 raise ValueError, 'unknown tag: ' + name
121 def HandleWxpTag(self
, tag
):
125 # create a new context object
126 self
.ctx
= _Context()
128 # find and import the module
130 if tag
.HasParam('MODULE'):
131 modName
= tag
.GetParam('MODULE')
133 self
.ctx
.classMod
= _my_import(modName
)
135 self
.ctx
.classMod
= wxPython
.wx
137 # find and verify the class
138 if not tag
.HasParam('CLASS'):
139 raise AttributeError, "WXP tag requires a CLASS attribute"
141 className
= tag
.GetParam('CLASS')
142 self
.ctx
.classObj
= getattr(self
.ctx
.classMod
, className
)
143 if type(self
.ctx
.classObj
) != types
.ClassType
:
144 raise TypeError, "WXP tag attribute CLASS must name a class"
148 # now look for width and height
151 if tag
.HasParam('WIDTH'):
152 width
= tag
.GetParam('WIDTH')
154 self
.ctx
.floatWidth
= string
.atoi(width
[:-1], 0)
155 width
= self
.ctx
.floatWidth
157 width
= string
.atoi(width
)
158 if tag
.HasParam('HEIGHT'):
159 height
= string
.atoi(tag
.GetParam('HEIGHT'))
160 self
.ctx
.kwargs
['size'] = wxSize(width
, height
)
166 obj
= apply(self
.ctx
.classObj
,
167 (self
.GetParser().GetWindow(),),
171 # add it to the HtmlWindow
172 self
.GetParser().GetContainer().InsertCell(wxHtmlWidgetCell(obj
, self
.ctx
.floatWidth
))
179 def HandleParamTag(self
, tag
):
183 if not tag
.HasParam('NAME'):
186 name
= tag
.GetParam('NAME')
188 if tag
.HasParam('VALUE'):
189 value
= tag
.GetParam('VALUE')
191 # check for a param named 'id'
195 theID
= string
.atoi(value
)
197 theID
= getattr(self
.ctx
.classMod
, value
)
201 # check for something that should be evaluated
202 elif value
[0] in '[{(' or value
[:2] == 'wx':
205 value
= eval(value
, self
.ctx
.classMod
.__dict
__)
209 # convert to wxColour
210 elif value
[0] == '#':
212 red
= string
.atoi('0x'+value
[1:3], 16)
213 green
= string
.atoi('0x'+value
[3:5], 16)
214 blue
= string
.atoi('0x'+value
[5:], 16)
215 value
= wxColor(red
, green
, blue
)
219 self
.ctx
.kwargs
[name
] = value
223 #----------------------------------------------------------------------
224 # just a place to hold some values
235 #----------------------------------------------------------------------
236 # Function to assist with importing packages
237 def _my_import(name
):
238 mod
= __import__(name
)
239 components
= string
.split(name
, '.')
240 for comp
in components
[1:]:
241 mod
= getattr(mod
, comp
)
245 #----------------------------------------------------------------------
246 # Function to parse a param string (of the form 'item=value item2="value etc"'
247 # and creates a dictionary
248 def _param2dict(param
):
249 i
= 0; j
= 0; s
= len(param
); d
= {}
251 while i
<s
and param
[i
] == " " : i
= i
+1
254 while j
<s
and param
[j
] != "=": j
=j
+1
259 if (param
[i
] == '"'):
261 while j
<s
and param
[j
] != '"' : j
=j
+1
264 elif (param
[i
] != " "):
266 while j
<s
and param
[j
] != " " : j
=j
+1
274 #----------------------------------------------------------------------
278 wxHtmlWinParser_AddTagHandler(wxpTagHandler
)