]>
Commit | Line | Data |
---|---|---|
1f780e48 RD |
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 | ## frame.SetAutoLayout(True) | |
80 | ## | |
81 | ## | |
82 | ## def OnNotebookChanging(self, event): | |
83 | ## if event.GetSelection() == 0: # Going to the edit page | |
84 | ## pass # self._textEditor.Refresh() | |
85 | ## elif event.GetSelection() == 1: # Going to the browser page | |
86 | ## text = self._textEditor.GetText() | |
87 | ## if wx.Platform == '__WXMSW__': | |
88 | ## path = os.path.join(tempfile.gettempdir(), "temp.html") | |
89 | ## file = open(path, 'w') | |
90 | ## file.write(text) | |
91 | ## file.close() | |
92 | ## self._browserCtrl.Navigate("file://" + path) | |
93 | ## else: | |
94 | ## self._browserCtrl.SetPage(text) | |
95 | ## event.Skip() | |
96 | ||
97 | ||
98 | class HtmlService(CodeEditor.CodeService): | |
99 | ||
100 | ||
101 | def __init__(self): | |
102 | CodeEditor.CodeService.__init__(self) | |
103 | ||
104 | ||
105 | class HtmlCtrl(CodeEditor.CodeCtrl): | |
106 | ||
107 | ||
108 | def __init__(self, parent, ID = -1, style = wx.NO_FULL_REPAINT_ON_RESIZE): | |
109 | CodeEditor.CodeCtrl.__init__(self, parent, ID, style) | |
110 | self.SetLexer(wx.stc.STC_LEX_HTML) | |
111 | self.SetProperty("fold.html", "1") | |
112 | ||
113 | def GetMatchingBraces(self): | |
114 | return "<>[]{}()" | |
115 | ||
116 | def CanWordWrap(self): | |
117 | return True | |
118 | ||
119 | ||
120 | def SetViewDefaults(self): | |
121 | CodeEditor.CodeCtrl.SetViewDefaults(self, configPrefix = "Html", hasWordWrap = False, hasTabs = True) | |
122 | ||
123 | ||
124 | def GetFontAndColorFromConfig(self): | |
125 | return CodeEditor.CodeCtrl.GetFontAndColorFromConfig(self, configPrefix = "Html") | |
126 | ||
127 | ||
128 | def UpdateStyles(self): | |
129 | CodeEditor.CodeCtrl.UpdateStyles(self) | |
130 | ||
131 | if not self.GetFont(): | |
132 | return | |
133 | ||
134 | faces = { 'font' : self.GetFont().GetFaceName(), | |
135 | 'size' : self.GetFont().GetPointSize(), | |
136 | 'size2': self.GetFont().GetPointSize() - 2, | |
137 | 'color' : "%02x%02x%02x" % (self.GetFontColor().Red(), self.GetFontColor().Green(), self.GetFontColor().Blue()) | |
138 | } | |
139 | ||
140 | # White space | |
141 | self.StyleSetSpec(wx.stc.STC_H_DEFAULT, "face:%(font)s,fore:#000000,face:%(font)s,size:%(size)d" % faces) | |
142 | # Comment | |
143 | self.StyleSetSpec(wx.stc.STC_H_COMMENT, "face:%(font)s,fore:#007F00,italic,face:%(font)s,size:%(size)d" % faces) | |
144 | # Number | |
145 | self.StyleSetSpec(wx.stc.STC_H_NUMBER, "face:%(font)s,fore:#007F7F,size:%(size)d" % faces) | |
146 | # String | |
147 | self.StyleSetSpec(wx.stc.STC_H_SINGLESTRING, "face:%(font)s,fore:#7F007F,face:%(font)s,size:%(size)d" % faces) | |
148 | self.StyleSetSpec(wx.stc.STC_H_DOUBLESTRING, "face:%(font)s,fore:#7F007F,face:%(font)s,size:%(size)d" % faces) | |
149 | # Tag | |
150 | self.StyleSetSpec(wx.stc.STC_H_TAG, "face:%(font)s,fore:#00007F,bold,size:%(size)d" % faces) | |
151 | # Attributes | |
152 | self.StyleSetSpec(wx.stc.STC_H_ATTRIBUTE, "face:%(font)s,fore:#00007F,bold,size:%(size)d" % faces) | |
153 | ||
154 | ||
155 | class HtmlOptionsPanel(STCTextEditor.TextOptionsPanel): | |
156 | ||
157 | def __init__(self, parent, id): | |
158 | STCTextEditor.TextOptionsPanel.__init__(self, parent, id, configPrefix = "Html", label = "HTML", hasWordWrap = True, hasTabs = True) | |
159 | ||
160 | ||
161 | HTMLKEYWORDS = [ | |
162 | "A", "ABBR", "ACRONYM", "ADDRESS", "APPLET", "AREA", "B", "BASE", "BASEFONT", "BDO", "BIG", "BLOCKQUOTE", | |
163 | "BODY", "BR", "BUTTON", "CAPTION", "CENTER", "CITE", "CODE", "COL", "COLGROUP", "DD", "DEL", "DFN", "DIR", | |
164 | "DIV", "DL", "DT", "EM", "FIELDSET", "FONT", "FORM", "FRAME", "FRAMESET", "H1", "H2", "H3", "H4", "H5", "H6", | |
165 | "HEAD", "HR", "HTML", "I", "IFRAME", "IMG", "INPUT", "INS", "ISINDEX", "KBD", "LABEL", "LEGEND", "LI", "LINK", | |
166 | "MAP", "MENU", "META", "NOFRAMES", "NOSCRIPT", "OBJECT", "OL", "OPTGROUP", "OPTION", "P", "PARAM", | |
167 | "PRE", "Q", "S", "SAMP", "SCRIPT", "SELECT", "SMALL", "SPAN", "STRIKE", "STRONG", "STYLE", "SUB", "SUP", | |
168 | "TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR", "TT", "U", "UL", "VAR", "XML", | |
169 | "XMLNS", "ACCEPT-CHARSET", "ACCEPT", "ACCESSKEY", "ACTION", "ALIGN", "ALINK", "ALT", | |
170 | "ARCHIVE", "AXIS", "BACKGROUND", "BGCOLOR", "BORDER", "CELLPADDING", "CELLSPACING", "CHAR", | |
171 | "CHAROFF", "CHARSET", "CHECKED", "CLASS", "CLASSID", "CLEAR", "CODEBASE", "CODETYPE", | |
172 | "COLOR", "COLS", "COLSPAN", "COMPACT", "CONTENT", "COORDS", "DATA", "DATAFLD", "DATAFORMATAS", | |
173 | "DATAPAGESIZE", "DATASRC", "DATETIME", "DECLARE", "DEFER", "DISABLED", "ENCTYPE", | |
174 | "EVENT", "FACE", "FOR", "FRAMEBORDER", "HEADERS", "HEIGHT", "HREF", "HREFLANG", "HSPACE", | |
175 | "HTTP-EQUIV", "ID", "ISMAP", "LANG", "LANGUAGE", "LEFTMARGIN", "LONGDESC", | |
176 | "MARGINWIDTH", "MARGINHEIGHT", "MAXLENGTH", "MEDIA", "METHOD", "MULTIPLE", "NAME", "NOHREF", | |
177 | "NORESIZE", "NOSHADE", "NOWRAP", "ONBLUR", "ONCHANGE", "ONCLICK", "ONDBLCLICK", | |
178 | "ONFOCUS", "ONKEYDOWN", "ONKEYPRESS", "ONKEYUP", "ONLOAD", "ONMOUSEDOWN", "ONMOUSEMOVE", | |
179 | "ONMOUSEOVER", "ONMOUSEOUT", "ONMOUSEUP", "ONRESET", "ONSELECT", "ONSUBMIT", "ONUNLOAD", | |
180 | "PROFILE", "PROMPT", "READONLY", "REL", "REV", "ROWS", "ROWSPAN", "RULES", "SCHEME", "SCOPE", | |
181 | "SELECTED", "SHAPE", "SIZE", "SRC", "STANDBY", "START", "SUMMARY", "TABINDEX", | |
182 | "TARGET", "TOPMARGIN", "TYPE", "USEMAP", "VALIGN", "VALUE", "VALUETYPE", | |
183 | "VERSION", "VLINK", "VSPACE", "WIDTH", "TEXT", "PASSWORD", "CHECKBOX", "RADIO", "SUBMIT", "RESET", | |
184 | "FILE", "HIDDEN", "IMAGE", "PUBLIC", "!DOCTYPE", | |
185 | "ADD_DATE", "LAST_MODIFIED", "LAST_VISIT" | |
186 | ] | |
187 | ||
188 | ||
189 | #---------------------------------------------------------------------------- | |
190 | # Icon Bitmaps - generated by encode_bitmaps.py | |
191 | #---------------------------------------------------------------------------- | |
192 | from wx import ImageFromStream, BitmapFromImage | |
193 | from wx import EmptyIcon | |
194 | import cStringIO | |
195 | ||
196 | ||
197 | def getHTMLData(): | |
198 | return \ | |
199 | '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\ | |
200 | \x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\ | |
201 | \x00\x00\xcdIDAT8\x8dcd`f\xf8\xcf@\x01`\xfc\x7f\xa3\x87"\x03X\xfe}\xbeI\x89~\ | |
202 | \x06&\x8at\x0f\n\x03\x18\xe4\x954\xff\xc3\x00\x8c-\xaf\xa4\xf9_\xc7\xc0\xfc\ | |
203 | \xbf\x93\xab\xf7\xff\xff\xff\xff\xff70\xb6\xfe\x7f\xed\xce\x93\xff\xd7\xee<\ | |
204 | \xf9\xafc`\x0eW\xf3\xf5\xd7\xff\xff,\x0f\x1f^gPP\xd6B1\xf4\xc1\xddk\x0c\xf6\ | |
205 | \xb6\x16\x0c{wma````x\xf7\xfc\x06\xc3\xea\xa5\xb3\x198\xd8X\x18\xbe~|\x06W\ | |
206 | \xc7\xc5\xca\xc0\xc0\xc2\xc0\xc0\xc0P\\\x9c\xcf\xf0\xf4\xc5\x1b\x86\x15K\x97\ | |
207 | \xc2%Y\xd9y\xe0lF\x0e1\x86C\x87\x8e0\x88\x88\x8a3\xfccD\x88\xe3\xf4\x026\xf6\ | |
208 | \xa9c{\xfe_<s\x18\xc5\x9b\xf2J\x9a\xff\x19\xff\x9eN\xa5(!\r|4\x0e\x03\x03\ | |
209 | \x00R\xe4{\xe74\x9e\xbb\xd1\x00\x00\x00\x00IEND\xaeB`\x82' | |
210 | ||
211 | ||
212 | def getHTMLBitmap(): | |
213 | return BitmapFromImage(getHTMLImage()) | |
214 | ||
215 | def getHTMLImage(): | |
216 | stream = cStringIO.StringIO(getHTMLData()) | |
217 | return ImageFromStream(stream) | |
218 | ||
219 | def getHTMLIcon(): | |
220 | icon = EmptyIcon() | |
221 | icon.CopyFromBitmap(getHTMLBitmap()) | |
222 | return icon |