]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/wxpTag.py
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 #----------------------------------------------------------------------
13 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 # o Updated for V2.5 compatability
21 This module contains a wxHtmlTagHandler that knows how to build
22 and place wxPython widgets onto wxHtmlWindow web pages.
24 You don\'t need to use anything in this module directly, just
25 importing it will create the tag handler and add it to any
26 wxHtmlWinParsers created from that time forth.
28 Tags of the following form are recognised::
30 <WXP class="classname" [module="modulename"] [width="num"] [height="num"]>
31 <PARAM name="parameterName" value="parameterValue>
35 where modulename is the name of a module (possibly in package
36 notation) to import and classname is the name of a class in that
37 module to create an instance of. If the module tag-attribute is not
38 given or is an empty string, then wx is used. The width and height
39 attributes are expected to be integers and will be passed to the
40 __init__ method of the class as a wxSize object named size. However,
41 if the width attribute ends with the percent (%) symbol then the value
42 will be used as a percentage of the available width and the
43 wxHtmlWindow will manage the size.
45 The name-value pairs in all the nested PARAM tags are packaged up as
46 strings into a python dictionary and passed to the __init__ method of
47 the class as keyword arguments. This means that they are all
48 accessible from the __init__ method as regular parameters, or you use
49 the special Python \*\*kw syntax in your __init__ method to get the
52 Some parameter values are special and if they are present then they will
53 be converted from strings to alternate datatypes. They are:
55 id If the value of id can be converted to an integer, it will
56 be. Otherwise it is assumed to be the name of an integer
57 variable in the module.
59 colours Any value of the form "#123ABC" will automatically be
60 converted to a wxColour object.
62 Py Types Any value begining with "(", "[" or "{" are expected to
63 be a Python tuple, list, or dictionary and eval()
64 will be used to convert them to that type. If the
65 eval() fails then the original string value will be
68 wx Types Any value begining with "wx" is expected to be an attempt
69 to create a wxPython object, such as a wxSize, etc.
70 The eval() will be used to try and construct the
71 object and if it fails then the original string value
76 <wxp module="wx" class="Button">
77 <param name="label" value="Click here">
78 <param name="id" value="ID_OK">
81 Both the begining and ending WXP tags are required.
83 In the future support will be added for another tag that can be
84 embedded between the two begining and ending WXP tags and will
85 facilitate calling methods of the widget to help initialize it.
86 Additionally, support may be added to fetch the module from a web
87 server as is done with java applets.
90 #----------------------------------------------------------------------
98 #----------------------------------------------------------------------
103 #----------------------------------------------------------------------
105 class wxpTagHandler(wx
.html
.HtmlWinTagHandler
):
107 wx
.html
.HtmlWinTagHandler
.__init
__(self
)
110 def GetSupportedTags(self
):
111 return WXPTAG
+','+PARAMTAG
114 def HandleTag(self
, tag
):
117 return self
.HandleWxpTag(tag
)
118 elif name
== PARAMTAG
:
119 return self
.HandleParamTag(tag
)
121 raise ValueError, 'unknown tag: ' + name
124 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
= 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
) not in [ types
.ClassType
, types
.TypeType
]:
144 raise TypeError, "WXP tag attribute CLASS must name a class"
146 # now look for width and height
149 if tag
.HasParam('WIDTH'):
150 width
= tag
.GetParam('WIDTH')
152 self
.ctx
.floatWidth
= int(width
[:-1], 0)
153 width
= self
.ctx
.floatWidth
156 if tag
.HasParam('HEIGHT'):
157 height
= int(tag
.GetParam('HEIGHT'))
158 self
.ctx
.kwargs
['size'] = wx
.Size(width
, height
)
160 # parse up to the closing tag, and gather any nested Param tags.
164 parent
= self
.GetParser().GetWindowInterface().GetHTMLWindow()
166 obj
= apply(self
.ctx
.classObj
,
171 # add it to the HtmlWindow
172 self
.GetParser().GetContainer().InsertCell(
173 wx
.html
.HtmlWidgetCell(obj
, self
.ctx
.floatWidth
))
179 def HandleParamTag(self
, tag
):
180 if not tag
.HasParam('NAME'):
183 name
= tag
.GetParam('NAME')
185 if tag
.HasParam('VALUE'):
186 value
= tag
.GetParam('VALUE')
188 # check for a param named 'id'
194 theID
= getattr(self
.ctx
.classMod
, value
)
198 # check for something that should be evaluated
199 elif value
and value
[0] in '[{(' or value
[:2] == 'wx':
202 value
= eval(value
, self
.ctx
.classMod
.__dict
__)
206 # convert to wx.Colour
207 elif value
and value
[0] == '#':
209 red
= int('0x'+value
[1:3], 16)
210 green
= int('0x'+value
[3:5], 16)
211 blue
= int('0x'+value
[5:], 16)
212 value
= wx
.Color(red
, green
, blue
)
217 self
.ctx
.kwargs
[str(name
)] = value
221 #----------------------------------------------------------------------
222 # just a place to hold some values
233 #----------------------------------------------------------------------
234 # Function to assist with importing packages
235 def _my_import(name
):
236 mod
= __import__(name
)
237 components
= name
.split('.')
238 for comp
in components
[1:]:
239 mod
= getattr(mod
, comp
)
243 #----------------------------------------------------------------------
244 # Function to parse a param string (of the form 'item=value item2="value etc"'
245 # and creates a dictionary
246 def _param2dict(param
):
247 i
= 0; j
= 0; s
= len(param
); d
= {}
249 while i
<s
and param
[i
] == " " : i
= i
+1
252 while j
<s
and param
[j
] != "=": j
=j
+1
257 if (param
[i
] == '"'):
259 while j
<s
and param
[j
] != '"' : j
=j
+1
262 elif (param
[i
] != " "):
264 while j
<s
and param
[j
] != " " : j
=j
+1
272 #----------------------------------------------------------------------
276 wx
.html
.HtmlWinParser_AddTagHandler(wxpTagHandler
)