]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/tool/HtmlEditor.py
Tweaks for demos on MacOSX
[wxWidgets.git] / wxPython / samples / ide / activegrid / tool / HtmlEditor.py
1 #----------------------------------------------------------------------------
2 # Name: HtmlEditor.py
3 # Purpose: Abstract Code Editor for pydocview tbat uses the Styled Text Control
4 #
5 # Author: Peter Yared
6 #
7 # Created: 8/15/04
8 # CVS-ID: $Id$
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
12
13
14 import wx
15 import os.path
16 import string
17 import STCTextEditor
18 import CodeEditor
19 _ = wx.GetTranslation
20
21
22 class HtmlDocument(CodeEditor.CodeDocument):
23
24 pass
25
26
27 class HtmlView(CodeEditor.CodeView):
28
29
30 def GetCtrlClass(self):
31 """ Used in split window to instantiate new instances """
32 return HtmlCtrl
33
34
35 def GetAutoCompleteHint(self):
36 pos = self.GetCtrl().GetCurrentPos()
37 if pos == 0:
38 return None, None
39
40 validLetters = string.letters + string.digits + '_!-'
41 word = ''
42 while (True):
43 pos = pos - 1
44 if pos < 0:
45 break
46 char = chr(self.GetCtrl().GetCharAt(pos))
47 if char not in validLetters:
48 break
49 word = char + word
50
51 return None, word
52
53
54 def GetAutoCompleteDefaultKeywords(self):
55 return HTMLKEYWORDS
56
57
58 ## def _CreateControl(self, parent, id):
59 ## import wx # wxBug: When inlining the import of the appropriate html control below, have to specifically import wx for some reason
60 ## self._notebook = wx.Notebook(parent, wx.NewId(), style = wx.NB_BOTTOM)
61 ## self._textEditor = HtmlCtrl(self._notebook, id)
62 ## if wx.Platform =='__WXMSW__':
63 ## import wxPython.iewin
64 ## self._browserCtrl = wxPython.iewin.wxIEHtmlWin(self._notebook, -1, style = wx.NO_FULL_REPAINT_ON_RESIZE)
65 ## else:
66 ## import wx.html
67 ## self._browserCtrl = wx.html.HtmlWindow(self._notebook, -1, style = wx.NO_FULL_REPAINT_ON_RESIZE)
68 ## self._notebook.AddPage(self._textEditor, _("Edit"))
69 ## self._notebook.AddPage(self._browserCtrl, _("View"))
70 ## self._insertMode = True
71 ## wx.EVT_NOTEBOOK_PAGE_CHANGED(self._notebook, self._notebook.GetId(), self.OnNotebookChanging)
72 ## return self._textEditor
73 ##
74 ##
75 ## def _CreateSizer(self, frame):
76 ## sizer = wx.BoxSizer(wx.HORIZONTAL)
77 ## sizer.Add(self._notebook, 1, wx.EXPAND)
78 ## frame.SetSizer(sizer)
79 ##
80 ##
81 ## def OnNotebookChanging(self, event):
82 ## if event.GetSelection() == 0: # Going to the edit page
83 ## pass # self._textEditor.Refresh()
84 ## elif event.GetSelection() == 1: # Going to the browser page
85 ## text = self._textEditor.GetText()
86 ## if wx.Platform == '__WXMSW__':
87 ## path = os.path.join(tempfile.gettempdir(), "temp.html")
88 ## file = open(path, 'w')
89 ## file.write(text)
90 ## file.close()
91 ## self._browserCtrl.Navigate("file://" + path)
92 ## else:
93 ## self._browserCtrl.SetPage(text)
94 ## event.Skip()
95
96
97 class HtmlService(CodeEditor.CodeService):
98
99
100 def __init__(self):
101 CodeEditor.CodeService.__init__(self)
102
103
104 class HtmlCtrl(CodeEditor.CodeCtrl):
105
106
107 def __init__(self, parent, id=-1, style=wx.NO_FULL_REPAINT_ON_RESIZE):
108 CodeEditor.CodeCtrl.__init__(self, parent, id, style)
109 self.SetLexer(wx.stc.STC_LEX_HTML)
110 self.SetProperty("fold.html", "1")
111
112
113 def GetMatchingBraces(self):
114 return "<>[]{}()"
115
116
117 def CanWordWrap(self):
118 return True
119
120
121 def SetViewDefaults(self):
122 CodeEditor.CodeCtrl.SetViewDefaults(self, configPrefix = "Html", hasWordWrap = True, hasTabs = True)
123
124
125 def GetFontAndColorFromConfig(self):
126 return CodeEditor.CodeCtrl.GetFontAndColorFromConfig(self, configPrefix = "Html")
127
128
129 def UpdateStyles(self):
130 CodeEditor.CodeCtrl.UpdateStyles(self)
131
132 if not self.GetFont():
133 return
134
135 faces = { 'font' : self.GetFont().GetFaceName(),
136 'size' : self.GetFont().GetPointSize(),
137 'size2': self.GetFont().GetPointSize() - 2,
138 'color' : "%02x%02x%02x" % (self.GetFontColor().Red(), self.GetFontColor().Green(), self.GetFontColor().Blue())
139 }
140
141 # White space
142 self.StyleSetSpec(wx.stc.STC_H_DEFAULT, "face:%(font)s,fore:#000000,face:%(font)s,size:%(size)d" % faces)
143 # Comment
144 self.StyleSetSpec(wx.stc.STC_H_COMMENT, "face:%(font)s,fore:#007F00,italic,face:%(font)s,size:%(size)d" % faces)
145 # Number
146 self.StyleSetSpec(wx.stc.STC_H_NUMBER, "face:%(font)s,fore:#007F7F,size:%(size)d" % faces)
147 # String
148 self.StyleSetSpec(wx.stc.STC_H_SINGLESTRING, "face:%(font)s,fore:#7F007F,face:%(font)s,size:%(size)d" % faces)
149 self.StyleSetSpec(wx.stc.STC_H_DOUBLESTRING, "face:%(font)s,fore:#7F007F,face:%(font)s,size:%(size)d" % faces)
150 # Tag
151 self.StyleSetSpec(wx.stc.STC_H_TAG, "face:%(font)s,fore:#00007F,bold,size:%(size)d" % faces)
152 # Attributes
153 self.StyleSetSpec(wx.stc.STC_H_ATTRIBUTE, "face:%(font)s,fore:#00007F,bold,size:%(size)d" % faces)
154
155
156 class HtmlOptionsPanel(STCTextEditor.TextOptionsPanel):
157
158 def __init__(self, parent, id):
159 STCTextEditor.TextOptionsPanel.__init__(self, parent, id, configPrefix = "Html", label = "HTML", hasWordWrap = True, hasTabs = True)
160
161
162 def GetIcon(self):
163 return getHTMLIcon()
164
165
166 HTMLKEYWORDS = [
167 "A", "ABBR", "ACRONYM", "ADDRESS", "APPLET", "AREA", "B", "BASE", "BASEFONT", "BDO", "BIG", "BLOCKQUOTE",
168 "BODY", "BR", "BUTTON", "CAPTION", "CENTER", "CITE", "CODE", "COL", "COLGROUP", "DD", "DEL", "DFN", "DIR",
169 "DIV", "DL", "DT", "EM", "FIELDSET", "FONT", "FORM", "FRAME", "FRAMESET", "H1", "H2", "H3", "H4", "H5", "H6",
170 "HEAD", "HR", "HTML", "I", "IFRAME", "IMG", "INPUT", "INS", "ISINDEX", "KBD", "LABEL", "LEGEND", "LI", "LINK",
171 "MAP", "MENU", "META", "NOFRAMES", "NOSCRIPT", "OBJECT", "OL", "OPTGROUP", "OPTION", "P", "PARAM",
172 "PRE", "Q", "S", "SAMP", "SCRIPT", "SELECT", "SMALL", "SPAN", "STRIKE", "STRONG", "STYLE", "SUB", "SUP",
173 "TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR", "TT", "U", "UL", "VAR", "XML",
174 "XMLNS", "ACCEPT-CHARSET", "ACCEPT", "ACCESSKEY", "ACTION", "ALIGN", "ALINK", "ALT",
175 "ARCHIVE", "AXIS", "BACKGROUND", "BGCOLOR", "BORDER", "CELLPADDING", "CELLSPACING", "CHAR",
176 "CHAROFF", "CHARSET", "CHECKED", "CLASS", "CLASSID", "CLEAR", "CODEBASE", "CODETYPE",
177 "COLOR", "COLS", "COLSPAN", "COMPACT", "CONTENT", "COORDS", "DATA", "DATAFLD", "DATAFORMATAS",
178 "DATAPAGESIZE", "DATASRC", "DATETIME", "DECLARE", "DEFER", "DISABLED", "ENCTYPE",
179 "EVENT", "FACE", "FOR", "FRAMEBORDER", "HEADERS", "HEIGHT", "HREF", "HREFLANG", "HSPACE",
180 "HTTP-EQUIV", "ID", "ISMAP", "LANG", "LANGUAGE", "LEFTMARGIN", "LONGDESC",
181 "MARGINWIDTH", "MARGINHEIGHT", "MAXLENGTH", "MEDIA", "METHOD", "MULTIPLE", "NAME", "NOHREF",
182 "NORESIZE", "NOSHADE", "NOWRAP", "ONBLUR", "ONCHANGE", "ONCLICK", "ONDBLCLICK",
183 "ONFOCUS", "ONKEYDOWN", "ONKEYPRESS", "ONKEYUP", "ONLOAD", "ONMOUSEDOWN", "ONMOUSEMOVE",
184 "ONMOUSEOVER", "ONMOUSEOUT", "ONMOUSEUP", "ONRESET", "ONSELECT", "ONSUBMIT", "ONUNLOAD",
185 "PROFILE", "PROMPT", "READONLY", "REL", "REV", "ROWS", "ROWSPAN", "RULES", "SCHEME", "SCOPE",
186 "SELECTED", "SHAPE", "SIZE", "SRC", "STANDBY", "START", "SUMMARY", "TABINDEX",
187 "TARGET", "TOPMARGIN", "TYPE", "USEMAP", "VALIGN", "VALUE", "VALUETYPE",
188 "VERSION", "VLINK", "VSPACE", "WIDTH", "TEXT", "PASSWORD", "CHECKBOX", "RADIO", "SUBMIT", "RESET",
189 "FILE", "HIDDEN", "IMAGE", "PUBLIC", "!DOCTYPE",
190 "ADD_DATE", "LAST_MODIFIED", "LAST_VISIT"
191 ]
192
193
194 #----------------------------------------------------------------------------
195 # Icon Bitmaps - generated by encode_bitmaps.py
196 #----------------------------------------------------------------------------
197 from wx import ImageFromStream, BitmapFromImage
198 import cStringIO
199
200
201 def getHTMLData():
202 return \
203 '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
204 \x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
205 \x00\x01\xeeIDAT8\x8d}\x92?h\x13a\x18\xc6\x7fw=\x8d\xb4\x98\xa9-uP\x1a\xc9p$\
206 \xdc`\xd0C\x8d\x8b)\xc5!Z\x11\xcc\xd0A\xd0*\xa8\x93\x8b8\x18\x11\x14RD\x07\t\
207 N\xfe\xc1\x0cRAtS\x1c,\xcd\x10\x8c\xd8S\xba$9"\x11L\r\x96\x92\xa4.\xda#i\xa5\
208 \xe7p^r\x97?\xbe\xd3\xf1~\xdf\xf3{\x9f\xe7\xbbW\xf8T(\x998j\xb9V\x07 _\xde\
209 \x0e\xc0\xad\x19U\xe0?%\x01\x0c\xef\x19owv\xf9\xff\x01\x96\x88N\x850\x92)\
210 \xf3\xde\x95s}!Rgcx\xdb\x9f\xd6\xf7Z\x13"\xa7\xcf\x00\xf4\x85t\x01\xea\x9b\
211 \xedV\xfa\xd53\x00\xb2z\xb3\x7f\x84\xe5Z\xbd\x15\xc1)>x,\x04\x84,\xc0\xed\'\
212 \xfd\x01\x9dB\xdb:\xc0\x8a\xb1E\xa3\xb2A8\xe0!\x9cL\x99\x00\x83;\x83D\x0fxQ\
213 \x15Y\xe8\x19\xc1\x16\xff\xfe\xf2\x11V\xaf\xb1\x03\x90G\xe0\xf5\xe7\n\xd5\
214 \xf58\xb0\xc4\xfc"\xbcL\xbf7c\x91#\x82h\xff\xae\xb5\xa6{\xf2\xdc\x9bi\x17\
215 \xf8\xc6\x85\xaf\x9c\xbf:\x03\xc0\xe8P\x82\x8bwN\xa2\xe5\x8a\xa6\xe8\x9cjW\
216 \xf1\xed\x1c`M\x05P\x94\xa7=\xf3\xcf\xa6&\x91\x8c_\x85\xd6c\xad\x18[\xae\x0b\
217 \'\xf6\xef\xe6h4\r\xc0\xcf\x1f\xd0\xa8l0:\x94 \x937\x00\xc8\xe4\r\xeb\r:\x85\
218 \xe3J\x0cy\xe41\xde\xb1\xbb\xd4\xbf\x97\x11\x07|\x00T\xcbz\x97\x0b\xb1\x97\
219 \xb5jY\xa71\xf6\x0e-Wb65\xc9\x8b\xf9\xe7,\xaenZg\xebq\xd7])\xab7\xc9\xea\xee\
220 \x8c\xdaB\x90\xf8u\xbde\x13n\xb6\x96I[\x08\xa2N$(~\x8b#\xfb\x12H\x1f\x1e^\
221 \xeaZQ-W4\x0f\x9f\xaa\x01~\x8eO\r\x92\xc9\x1b\xc8>KlC\xbc{!\x1c\xf0\xf4\x8e\
222 \xa0*\xb2\x90|\xb4\xcf\xe1\xa0-v\xd6\xe5\xb3\xd3\x08\x828\xd0\x8b\x01X\xcb\
223 \xa2\xe5J\xdc\x7f\xe0o\xc3\'\n\x84\x03\x1eb\x91C\xa8\x8a,\xfc\x05\xf6\x0e\
224 \xbfa\x1f\xe7Z\xfb\x00\x00\x00\x00IEND\xaeB`\x82'
225
226
227 def getHTMLBitmap():
228 return BitmapFromImage(getHTMLImage())
229
230 def getHTMLImage():
231 stream = cStringIO.StringIO(getHTMLData())
232 return ImageFromStream(stream)
233
234 def getHTMLIcon():
235 return wx.IconFromBitmap(getHTMLBitmap())