]>
Commit | Line | Data |
---|---|---|
30fc5e8f RD |
1 | |
2 | import wx | |
3 | import wx.richtext as rt | |
4 | import images | |
5 | ||
6 | #---------------------------------------------------------------------- | |
7 | ||
8 | class RichTextFrame(wx.Frame): | |
9 | def __init__(self, *args, **kw): | |
10 | wx.Frame.__init__(self, *args, **kw) | |
11 | ||
12 | self.MakeMenuBar() | |
13 | self.MakeToolBar() | |
14 | self.CreateStatusBar() | |
15 | self.SetStatusText("Welcome to wx.richtext.RichTextCtrl!") | |
16 | ||
17 | self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER); | |
18 | wx.CallAfter(self.rtc.SetFocus) | |
19 | ||
08893e7e | 20 | self.rtc.Freeze() |
30fc5e8f RD |
21 | self.rtc.BeginSuppressUndo() |
22 | ||
23 | self.rtc.BeginParagraphSpacing(0, 20) | |
24 | ||
25 | self.rtc.BeginAlignment(rt.TEXT_ALIGNMENT_CENTRE) | |
26 | self.rtc.BeginBold() | |
27 | ||
28 | self.rtc.BeginFontSize(14) | |
29 | self.rtc.WriteText("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images") | |
30 | self.rtc.EndFontSize() | |
31 | self.rtc.Newline() | |
32 | ||
33 | self.rtc.BeginItalic() | |
34 | self.rtc.WriteText("by Julian Smart") | |
35 | self.rtc.EndItalic() | |
36 | ||
37 | self.rtc.EndBold() | |
38 | ||
39 | self.rtc.Newline() | |
40 | self.rtc.WriteImage(images.get_rt_zebraImage()) | |
41 | ||
42 | self.rtc.EndAlignment() | |
43 | ||
44 | self.rtc.Newline() | |
45 | self.rtc.Newline() | |
46 | ||
47 | self.rtc.WriteText("What can you do with this thing? ") | |
48 | self.rtc.WriteImage(images.get_rt_smileyImage()) | |
49 | self.rtc.WriteText(" Well, you can change text ") | |
50 | ||
51 | self.rtc.BeginTextColour((255, 0, 0)) | |
52 | self.rtc.WriteText("colour, like this red bit.") | |
53 | self.rtc.EndTextColour() | |
54 | ||
55 | self.rtc.BeginTextColour((0, 0, 255)) | |
56 | self.rtc.WriteText(" And this blue bit.") | |
57 | self.rtc.EndTextColour() | |
58 | ||
59 | self.rtc.WriteText(" Naturally you can make things ") | |
60 | self.rtc.BeginBold() | |
61 | self.rtc.WriteText("bold ") | |
62 | self.rtc.EndBold() | |
63 | self.rtc.BeginItalic() | |
64 | self.rtc.WriteText("or italic ") | |
65 | self.rtc.EndItalic() | |
66 | self.rtc.BeginUnderline() | |
67 | self.rtc.WriteText("or underlined.") | |
68 | self.rtc.EndUnderline() | |
69 | ||
70 | self.rtc.BeginFontSize(14) | |
71 | self.rtc.WriteText(" Different font sizes on the same line is allowed, too.") | |
72 | self.rtc.EndFontSize() | |
73 | ||
74 | self.rtc.WriteText(" Next we'll show an indented paragraph.") | |
75 | ||
76 | self.rtc.BeginLeftIndent(60) | |
77 | self.rtc.Newline() | |
78 | ||
79 | self.rtc.WriteText("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable.") | |
80 | self.rtc.EndLeftIndent() | |
81 | ||
82 | self.rtc.Newline() | |
83 | ||
84 | self.rtc.WriteText("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40).") | |
85 | ||
86 | self.rtc.BeginLeftIndent(100, -40) | |
87 | self.rtc.Newline() | |
88 | ||
cf0d4bfc | 89 | self.rtc.WriteText("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable.") |
30fc5e8f RD |
90 | self.rtc.EndLeftIndent() |
91 | ||
92 | self.rtc.Newline() | |
93 | ||
cf0d4bfc | 94 | self.rtc.WriteText("Numbered bullets are possible, again using sub-indents:") |
30fc5e8f RD |
95 | |
96 | self.rtc.BeginNumberedBullet(1, 100, 60) | |
97 | self.rtc.Newline() | |
98 | ||
99 | self.rtc.WriteText("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later.") | |
100 | self.rtc.EndNumberedBullet() | |
101 | ||
102 | self.rtc.BeginNumberedBullet(2, 100, 60) | |
103 | self.rtc.Newline() | |
104 | ||
105 | self.rtc.WriteText("This is my second item.") | |
106 | self.rtc.EndNumberedBullet() | |
107 | ||
108 | self.rtc.Newline() | |
109 | ||
110 | self.rtc.WriteText("The following paragraph is right-indented:") | |
111 | ||
112 | self.rtc.BeginRightIndent(200) | |
113 | self.rtc.Newline() | |
114 | ||
115 | self.rtc.WriteText("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable.") | |
116 | self.rtc.EndRightIndent() | |
117 | ||
118 | self.rtc.Newline() | |
119 | ||
120 | self.rtc.WriteText("The following paragraph is right-aligned with 1.5 line spacing:") | |
121 | ||
122 | self.rtc.BeginAlignment(rt.TEXT_ALIGNMENT_RIGHT) | |
123 | self.rtc.BeginLineSpacing(rt.TEXT_ATTR_LINE_SPACING_HALF) | |
124 | self.rtc.Newline() | |
125 | ||
126 | self.rtc.WriteText("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable.") | |
127 | self.rtc.EndLineSpacing() | |
128 | self.rtc.EndAlignment() | |
129 | ||
130 | self.rtc.Newline() | |
131 | self.rtc.WriteText("Other notable features of wxRichTextCtrl include:") | |
132 | ||
133 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
134 | self.rtc.Newline() | |
135 | self.rtc.WriteText("Compatibility with wxTextCtrl API") | |
136 | self.rtc.EndSymbolBullet() | |
137 | ||
138 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
139 | self.rtc.Newline() | |
140 | self.rtc.WriteText("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()") | |
141 | self.rtc.EndSymbolBullet() | |
142 | ||
143 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
144 | self.rtc.Newline() | |
145 | self.rtc.WriteText("XML loading and saving") | |
146 | self.rtc.EndSymbolBullet() | |
147 | ||
148 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
149 | self.rtc.Newline() | |
150 | self.rtc.WriteText("Undo/Redo, with batching option and Undo suppressing") | |
151 | self.rtc.EndSymbolBullet() | |
152 | ||
153 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
154 | self.rtc.Newline() | |
155 | self.rtc.WriteText("Clipboard copy and paste") | |
156 | self.rtc.EndSymbolBullet() | |
157 | ||
158 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
159 | self.rtc.Newline() | |
160 | self.rtc.WriteText("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles") | |
161 | self.rtc.EndSymbolBullet() | |
162 | ||
163 | self.rtc.BeginSymbolBullet('*', 100, 60) | |
164 | self.rtc.Newline() | |
165 | self.rtc.WriteText("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on") | |
166 | self.rtc.EndSymbolBullet() | |
167 | ||
168 | self.rtc.Newline() | |
169 | ||
170 | self.rtc.WriteText("Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!") | |
171 | ||
172 | self.rtc.EndParagraphSpacing() | |
173 | ||
174 | self.rtc.EndSuppressUndo() | |
08893e7e RD |
175 | self.rtc.Thaw() |
176 | ||
30fc5e8f RD |
177 | |
178 | def OnFileOpen(self, evt): | |
fe45b493 RD |
179 | # This gives us a string suitable for the file dialog based on |
180 | # the file handlers that are loaded | |
181 | wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=False) | |
30fc5e8f | 182 | dlg = wx.FileDialog(self, "Choose a filename", |
fe45b493 | 183 | wildcard=wildcard, |
30fc5e8f RD |
184 | style=wx.OPEN) |
185 | if dlg.ShowModal() == wx.ID_OK: | |
186 | path = dlg.GetPath() | |
187 | if path: | |
fe45b493 RD |
188 | fileType = types[dlg.GetFilterIndex()] |
189 | self.rtc.LoadFile(path, fileType) | |
30fc5e8f RD |
190 | dlg.Destroy() |
191 | ||
192 | ||
193 | def OnFileSave(self, evt): | |
194 | if not self.rtc.GetFilename(): | |
195 | self.OnFileSaveAs(evt) | |
196 | return | |
197 | self.rtc.SaveFile() | |
fe45b493 | 198 | |
30fc5e8f RD |
199 | |
200 | def OnFileSaveAs(self, evt): | |
fe45b493 RD |
201 | wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=True) |
202 | ||
30fc5e8f | 203 | dlg = wx.FileDialog(self, "Choose a filename", |
fe45b493 | 204 | wildcard=wildcard, |
88535d8e | 205 | style=wx.SAVE) |
30fc5e8f RD |
206 | if dlg.ShowModal() == wx.ID_OK: |
207 | path = dlg.GetPath() | |
208 | if path: | |
fe45b493 RD |
209 | fileType = types[dlg.GetFilterIndex()] |
210 | ext = rt.RichTextBuffer.FindHandlerByType(fileType).GetExtension() | |
211 | if not path.endswith(ext): | |
212 | path += '.' + ext | |
213 | self.rtc.SaveFile(path, fileType) | |
e2e0b76a RD |
214 | dlg.Destroy() |
215 | ||
30fc5e8f | 216 | |
fe45b493 RD |
217 | def OnFileViewHTML(self, evt): |
218 | # Get an instance of the html file handler, use it to save the | |
219 | # document to a StringIO stream, and then display the | |
220 | # resulting html text in a dialog with a HtmlWindow. | |
221 | handler = rt.RichTextHTMLHandler() | |
222 | handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) | |
223 | handler.SetFontSizeMapping([7,9,11,12,14,22,100]) | |
224 | ||
225 | import cStringIO | |
226 | stream = cStringIO.StringIO() | |
227 | if not handler.SaveStream(self.rtc.GetBuffer(), stream): | |
228 | return | |
229 | ||
230 | import wx.html | |
231 | dlg = wx.Dialog(self, title="HTML", style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) | |
232 | html = wx.html.HtmlWindow(dlg, size=(500,400), style=wx.BORDER_SUNKEN) | |
233 | html.SetPage(stream.getvalue()) | |
234 | btn = wx.Button(dlg, wx.ID_CANCEL) | |
235 | sizer = wx.BoxSizer(wx.VERTICAL) | |
236 | sizer.Add(html, 1, wx.ALL|wx.EXPAND, 5) | |
237 | sizer.Add(btn, 0, wx.ALL|wx.CENTER, 10) | |
238 | dlg.SetSizer(sizer) | |
239 | sizer.Fit(dlg) | |
240 | ||
241 | dlg.ShowModal() | |
242 | ||
243 | handler.DeleteTemporaryImages() | |
244 | ||
30fc5e8f RD |
245 | |
246 | ||
247 | def OnFileExit(self, evt): | |
248 | self.Close(True) | |
249 | ||
250 | ||
251 | def OnBold(self, evt): | |
252 | self.rtc.ApplyBoldToSelection() | |
253 | ||
254 | def OnItalic(self, evt): | |
255 | self.rtc.ApplyItalicToSelection() | |
256 | ||
257 | def OnUnderline(self, evt): | |
258 | self.rtc.ApplyUnderlineToSelection() | |
259 | ||
260 | def OnAlignLeft(self, evt): | |
261 | self.rtc.ApplyAlignmentToSelection(rt.TEXT_ALIGNMENT_LEFT) | |
262 | ||
263 | def OnAlignRight(self, evt): | |
264 | self.rtc.ApplyAlignmentToSelection(rt.TEXT_ALIGNMENT_RIGHT) | |
265 | ||
266 | def OnAlignCenter(self, evt): | |
267 | self.rtc.ApplyAlignmentToSelection(rt.TEXT_ALIGNMENT_CENTRE) | |
268 | ||
269 | def OnIndentMore(self, evt): | |
270 | attr = rt.RichTextAttr() | |
271 | attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT) | |
272 | ip = self.rtc.GetInsertionPoint() | |
273 | if self.rtc.GetStyle(ip, attr): | |
274 | r = rt.RichTextRange(ip, ip) | |
275 | if self.rtc.HasSelection(): | |
276 | r = self.rtc.GetSelectionRange() | |
277 | ||
278 | attr.SetLeftIndent(attr.GetLeftIndent() + 100) | |
279 | attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT) | |
280 | self.rtc.SetStyle(r, attr) | |
281 | ||
282 | ||
283 | def OnIndentLess(self, evt): | |
284 | attr = rt.RichTextAttr() | |
285 | attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT) | |
286 | ip = self.rtc.GetInsertionPoint() | |
287 | if self.rtc.GetStyle(ip, attr): | |
288 | r = rt.RichTextRange(ip, ip) | |
289 | if self.rtc.HasSelection(): | |
290 | r = self.rtc.GetSelectionRange() | |
291 | ||
292 | if attr.GetLeftIndent() >= 100: | |
293 | attr.SetLeftIndent(attr.GetLeftIndent() - 100) | |
294 | attr.SetFlags(rt.TEXT_ATTR_LEFT_INDENT) | |
295 | self.rtc.SetStyle(r, attr) | |
296 | ||
297 | ||
298 | def OnParagraphSpacingMore(self, evt): | |
299 | attr = rt.RichTextAttr() | |
300 | attr.SetFlags(rt.TEXT_ATTR_PARA_SPACING_AFTER) | |
301 | ip = self.rtc.GetInsertionPoint() | |
302 | if self.rtc.GetStyle(ip, attr): | |
303 | r = rt.RichTextRange(ip, ip) | |
304 | if self.rtc.HasSelection(): | |
305 | r = self.rtc.GetSelectionRange() | |
306 | ||
307 | attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20); | |
308 | attr.SetFlags(rt.TEXT_ATTR_PARA_SPACING_AFTER) | |
309 | self.rtc.SetStyle(r, attr) | |
310 | ||
311 | ||
312 | def OnParagraphSpacingLess(self, evt): | |
313 | attr = rt.RichTextAttr() | |
314 | attr.SetFlags(rt.TEXT_ATTR_PARA_SPACING_AFTER) | |
315 | ip = self.rtc.GetInsertionPoint() | |
316 | if self.rtc.GetStyle(ip, attr): | |
317 | r = rt.RichTextRange(ip, ip) | |
318 | if self.rtc.HasSelection(): | |
319 | r = self.rtc.GetSelectionRange() | |
320 | ||
321 | if attr.GetParagraphSpacingAfter() >= 20: | |
322 | attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20); | |
323 | attr.SetFlags(rt.TEXT_ATTR_PARA_SPACING_AFTER) | |
324 | self.rtc.SetStyle(r, attr) | |
325 | ||
326 | ||
327 | def OnLineSpacingSingle(self, evt): | |
328 | attr = rt.RichTextAttr() | |
329 | attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) | |
330 | ip = self.rtc.GetInsertionPoint() | |
331 | if self.rtc.GetStyle(ip, attr): | |
332 | r = rt.RichTextRange(ip, ip) | |
333 | if self.rtc.HasSelection(): | |
334 | r = self.rtc.GetSelectionRange() | |
335 | ||
336 | attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) | |
337 | attr.SetLineSpacing(10) | |
338 | self.rtc.SetStyle(r, attr) | |
339 | ||
340 | ||
341 | def OnLineSpacingHalf(self, evt): | |
342 | attr = rt.RichTextAttr() | |
343 | attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) | |
344 | ip = self.rtc.GetInsertionPoint() | |
345 | if self.rtc.GetStyle(ip, attr): | |
346 | r = rt.RichTextRange(ip, ip) | |
347 | if self.rtc.HasSelection(): | |
348 | r = self.rtc.GetSelectionRange() | |
349 | ||
350 | attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) | |
351 | attr.SetLineSpacing(15) | |
352 | self.rtc.SetStyle(r, attr) | |
353 | ||
354 | ||
355 | def OnLineSpacingDouble(self, evt): | |
356 | attr = rt.RichTextAttr() | |
357 | attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) | |
358 | ip = self.rtc.GetInsertionPoint() | |
359 | if self.rtc.GetStyle(ip, attr): | |
360 | r = rt.RichTextRange(ip, ip) | |
361 | if self.rtc.HasSelection(): | |
362 | r = self.rtc.GetSelectionRange() | |
363 | ||
364 | attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING) | |
365 | attr.SetLineSpacing(20) | |
366 | self.rtc.SetStyle(r, attr) | |
367 | ||
368 | ||
369 | def OnFont(self, evt): | |
370 | if not self.rtc.HasSelection(): | |
371 | return | |
372 | ||
373 | r = self.rtc.GetSelectionRange() | |
374 | fontData = wx.FontData() | |
b97ff8ed | 375 | fontData.EnableEffects(False) |
30fc5e8f RD |
376 | attr = rt.RichTextAttr() |
377 | attr.SetFlags(rt.TEXT_ATTR_FONT) | |
378 | if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr): | |
379 | fontData.SetInitialFont(attr.CreateFont()) | |
380 | ||
381 | dlg = wx.FontDialog(self, fontData) | |
382 | if dlg.ShowModal() == wx.ID_OK: | |
383 | fontData = dlg.GetFontData() | |
384 | font = fontData.GetChosenFont() | |
385 | if font: | |
386 | attr.SetFlags(rt.TEXT_ATTR_FONT) | |
387 | attr.SetFont(font) | |
388 | self.rtc.SetStyle(r, attr) | |
389 | dlg.Destroy() | |
390 | ||
391 | ||
b97ff8ed RD |
392 | def OnColour(self, evt): |
393 | colourData = wx.ColourData() | |
394 | attr = rt.RichTextAttr() | |
395 | attr.SetFlags(rt.TEXT_ATTR_TEXT_COLOUR) | |
396 | if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr): | |
397 | colourData.SetColour(attr.GetTextColour()) | |
398 | ||
399 | dlg = wx.ColourDialog(self, colourData) | |
400 | if dlg.ShowModal() == wx.ID_OK: | |
401 | colourData = dlg.GetColourData() | |
402 | colour = colourData.GetColour() | |
403 | if colour: | |
404 | if not self.rtc.HasSelection(): | |
405 | self.rtc.BeginTextColour(colour) | |
406 | else: | |
407 | r = self.rtc.GetSelectionRange() | |
408 | attr.SetFlags(rt.TEXT_ATTR_TEXT_COLOUR) | |
409 | attr.SetTextColour(colour) | |
410 | self.rtc.SetStyle(r, attr) | |
411 | dlg.Destroy() | |
412 | ||
413 | ||
414 | ||
30fc5e8f RD |
415 | def OnUpdateBold(self, evt): |
416 | evt.Check(self.rtc.IsSelectionBold()) | |
417 | ||
418 | def OnUpdateItalic(self, evt): | |
419 | evt.Check(self.rtc.IsSelectionItalics()) | |
420 | ||
421 | def OnUpdateUnderline(self, evt): | |
422 | evt.Check(self.rtc.IsSelectionUnderlined()) | |
423 | ||
424 | def OnUpdateAlignLeft(self, evt): | |
425 | evt.Check(self.rtc.IsSelectionAligned(rt.TEXT_ALIGNMENT_LEFT)) | |
426 | ||
427 | def OnUpdateAlignCenter(self, evt): | |
428 | evt.Check(self.rtc.IsSelectionAligned(rt.TEXT_ALIGNMENT_CENTRE)) | |
429 | ||
430 | def OnUpdateAlignRight(self, evt): | |
431 | evt.Check(self.rtc.IsSelectionAligned(rt.TEXT_ALIGNMENT_RIGHT)) | |
432 | ||
433 | ||
434 | def ForwardEvent(self, evt): | |
435 | # The RichTextCtrl can handle menu and update events for undo, | |
436 | # redo, cut, copy, paste, delete, and select all, so just | |
437 | # forward the event to it. | |
438 | self.rtc.ProcessEvent(evt) | |
439 | ||
440 | ||
441 | def MakeMenuBar(self): | |
442 | def doBind(item, handler, updateUI=None): | |
443 | self.Bind(wx.EVT_MENU, handler, item) | |
444 | if updateUI is not None: | |
445 | self.Bind(wx.EVT_UPDATE_UI, updateUI, item) | |
446 | ||
447 | fileMenu = wx.Menu() | |
448 | doBind( fileMenu.Append(-1, "&Open\tCtrl+O", "Open a file"), | |
449 | self.OnFileOpen ) | |
450 | doBind( fileMenu.Append(-1, "&Save\tCtrl+S", "Save a file"), | |
451 | self.OnFileSave ) | |
452 | doBind( fileMenu.Append(-1, "&Save As...\tF12", "Save to a new file"), | |
453 | self.OnFileSaveAs ) | |
454 | fileMenu.AppendSeparator() | |
455 | doBind( fileMenu.Append(-1, "&View as HTML", "View HTML"), | |
456 | self.OnFileViewHTML) | |
457 | fileMenu.AppendSeparator() | |
458 | doBind( fileMenu.Append(-1, "E&xit\tCtrl+Q", "Quit this program"), | |
459 | self.OnFileExit ) | |
460 | ||
461 | editMenu = wx.Menu() | |
462 | doBind( editMenu.Append(wx.ID_UNDO, "&Undo\tCtrl+Z"), | |
463 | self.ForwardEvent, self.ForwardEvent) | |
464 | doBind( editMenu.Append(wx.ID_REDO, "&Redo\tCtrl+Y"), | |
465 | self.ForwardEvent, self.ForwardEvent ) | |
466 | editMenu.AppendSeparator() | |
467 | doBind( editMenu.Append(wx.ID_CUT, "Cu&t\tCtrl+X"), | |
468 | self.ForwardEvent, self.ForwardEvent ) | |
469 | doBind( editMenu.Append(wx.ID_COPY, "&Copy\tCtrl+C"), | |
470 | self.ForwardEvent, self.ForwardEvent) | |
471 | doBind( editMenu.Append(wx.ID_PASTE, "&Paste\tCtrl+V"), | |
472 | self.ForwardEvent, self.ForwardEvent) | |
473 | doBind( editMenu.Append(wx.ID_CLEAR, "&Delete\tDel"), | |
474 | self.ForwardEvent, self.ForwardEvent) | |
475 | editMenu.AppendSeparator() | |
476 | doBind( editMenu.Append(wx.ID_SELECTALL, "Select A&ll\tCtrl+A"), | |
477 | self.ForwardEvent, self.ForwardEvent ) | |
478 | ||
479 | #doBind( editMenu.AppendSeparator(), ) | |
480 | #doBind( editMenu.Append(-1, "&Find...\tCtrl+F"), ) | |
481 | #doBind( editMenu.Append(-1, "&Replace...\tCtrl+R"), ) | |
482 | ||
483 | formatMenu = wx.Menu() | |
484 | doBind( formatMenu.AppendCheckItem(-1, "&Bold\tCtrl+B"), | |
485 | self.OnBold, self.OnUpdateBold) | |
486 | doBind( formatMenu.AppendCheckItem(-1, "&Italic\tCtrl+I"), | |
487 | self.OnItalic, self.OnUpdateItalic) | |
488 | doBind( formatMenu.AppendCheckItem(-1, "&Underline\tCtrl+U"), | |
489 | self.OnUnderline, self.OnUpdateUnderline) | |
490 | formatMenu.AppendSeparator() | |
491 | doBind( formatMenu.AppendCheckItem(-1, "L&eft Align"), | |
492 | self.OnAlignLeft, self.OnUpdateAlignLeft) | |
493 | doBind( formatMenu.AppendCheckItem(-1, "&Centre"), | |
494 | self.OnAlignCenter, self.OnUpdateAlignCenter) | |
495 | doBind( formatMenu.AppendCheckItem(-1, "&Right Align"), | |
496 | self.OnAlignRight, self.OnUpdateAlignRight) | |
497 | formatMenu.AppendSeparator() | |
498 | doBind( formatMenu.Append(-1, "Indent &More"), self.OnIndentMore) | |
499 | doBind( formatMenu.Append(-1, "Indent &Less"), self.OnIndentLess) | |
500 | formatMenu.AppendSeparator() | |
501 | doBind( formatMenu.Append(-1, "Increase Paragraph &Spacing"), self.OnParagraphSpacingMore) | |
502 | doBind( formatMenu.Append(-1, "Decrease &Paragraph Spacing"), self.OnParagraphSpacingLess) | |
503 | formatMenu.AppendSeparator() | |
504 | doBind( formatMenu.Append(-1, "Normal Line Spacing"), self.OnLineSpacingSingle) | |
505 | doBind( formatMenu.Append(-1, "1.5 Line Spacing"), self.OnLineSpacingHalf) | |
506 | doBind( formatMenu.Append(-1, "Double Line Spacing"), self.OnLineSpacingDouble) | |
507 | formatMenu.AppendSeparator() | |
508 | doBind( formatMenu.Append(-1, "&Font..."), self.OnFont) | |
509 | ||
510 | mb = wx.MenuBar() | |
511 | mb.Append(fileMenu, "&File") | |
512 | mb.Append(editMenu, "&Edit") | |
513 | mb.Append(formatMenu, "F&ormat") | |
514 | self.SetMenuBar(mb) | |
515 | ||
516 | ||
517 | def MakeToolBar(self): | |
518 | def doBind(item, handler, updateUI=None): | |
519 | self.Bind(wx.EVT_TOOL, handler, item) | |
520 | if updateUI is not None: | |
521 | self.Bind(wx.EVT_UPDATE_UI, updateUI, item) | |
522 | ||
523 | tbar = self.CreateToolBar() | |
524 | doBind( tbar.AddTool(-1, images.get_rt_openBitmap(), | |
525 | shortHelpString="Open"), self.OnFileOpen) | |
526 | doBind( tbar.AddTool(-1, images.get_rt_saveBitmap(), | |
527 | shortHelpString="Save"), self.OnFileSave) | |
528 | tbar.AddSeparator() | |
529 | doBind( tbar.AddTool(wx.ID_CUT, images.get_rt_cutBitmap(), | |
530 | shortHelpString="Cut"), self.ForwardEvent, self.ForwardEvent) | |
531 | doBind( tbar.AddTool(wx.ID_COPY, images.get_rt_copyBitmap(), | |
532 | shortHelpString="Copy"), self.ForwardEvent, self.ForwardEvent) | |
533 | doBind( tbar.AddTool(wx.ID_PASTE, images.get_rt_pasteBitmap(), | |
534 | shortHelpString="Paste"), self.ForwardEvent, self.ForwardEvent) | |
535 | tbar.AddSeparator() | |
536 | doBind( tbar.AddTool(wx.ID_UNDO, images.get_rt_undoBitmap(), | |
537 | shortHelpString="Undo"), self.ForwardEvent, self.ForwardEvent) | |
538 | doBind( tbar.AddTool(wx.ID_REDO, images.get_rt_redoBitmap(), | |
539 | shortHelpString="Redo"), self.ForwardEvent, self.ForwardEvent) | |
540 | tbar.AddSeparator() | |
541 | doBind( tbar.AddTool(-1, images.get_rt_boldBitmap(), isToggle=True, | |
542 | shortHelpString="Bold"), self.OnBold, self.OnUpdateBold) | |
543 | doBind( tbar.AddTool(-1, images.get_rt_italicBitmap(), isToggle=True, | |
544 | shortHelpString="Italic"), self.OnItalic, self.OnUpdateItalic) | |
545 | doBind( tbar.AddTool(-1, images.get_rt_underlineBitmap(), isToggle=True, | |
546 | shortHelpString="Underline"), self.OnUnderline, self.OnUpdateUnderline) | |
547 | tbar.AddSeparator() | |
548 | doBind( tbar.AddTool(-1, images.get_rt_alignleftBitmap(), isToggle=True, | |
549 | shortHelpString="Align Left"), self.OnAlignLeft, self.OnUpdateAlignLeft) | |
550 | doBind( tbar.AddTool(-1, images.get_rt_centreBitmap(), isToggle=True, | |
551 | shortHelpString="Center"), self.OnAlignCenter, self.OnUpdateAlignCenter) | |
552 | doBind( tbar.AddTool(-1, images.get_rt_alignrightBitmap(), isToggle=True, | |
553 | shortHelpString="Align Right"), self.OnAlignRight, self.OnUpdateAlignRight) | |
554 | tbar.AddSeparator() | |
555 | doBind( tbar.AddTool(-1, images.get_rt_indentlessBitmap(), | |
556 | shortHelpString="Indent Less"), self.OnIndentLess) | |
557 | doBind( tbar.AddTool(-1, images.get_rt_indentmoreBitmap(), | |
558 | shortHelpString="Indent More"), self.OnIndentMore) | |
559 | tbar.AddSeparator() | |
560 | doBind( tbar.AddTool(-1, images.get_rt_fontBitmap(), | |
561 | shortHelpString="Font"), self.OnFont) | |
b97ff8ed RD |
562 | doBind( tbar.AddTool(-1, images.get_rt_colourBitmap(), |
563 | shortHelpString="Font Colour"), self.OnColour) | |
30fc5e8f RD |
564 | |
565 | tbar.Realize() | |
566 | ||
567 | ||
568 | ||
569 | #---------------------------------------------------------------------- | |
570 | ||
fe45b493 | 571 | |
30fc5e8f RD |
572 | class TestPanel(wx.Panel): |
573 | def __init__(self, parent, log): | |
574 | self.log = log | |
575 | wx.Panel.__init__(self, parent, -1) | |
576 | ||
577 | b = wx.Button(self, -1, "Show the RichTextCtrl sample", (50,50)) | |
578 | self.Bind(wx.EVT_BUTTON, self.OnButton, b) | |
579 | ||
fe45b493 RD |
580 | self.AddRTCHandlers() |
581 | ||
582 | ||
583 | def AddRTCHandlers(self): | |
584 | # make sure we haven't already added them. | |
585 | if rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_HTML) is not None: | |
586 | return | |
587 | ||
588 | # This would normally go in your app's OnInit method. I'm | |
589 | # not sure why these file handlers are not loaded by | |
590 | # default by the C++ richtext code, I guess it's so you | |
591 | # can change the name or extension if you wanted... | |
592 | rt.RichTextBuffer.AddHandler(rt.RichTextHTMLHandler()) | |
593 | rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler()) | |
594 | ||
595 | # ...like this | |
596 | rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="Other XML", | |
597 | ext="ox", | |
598 | type=99)) | |
599 | ||
600 | # This is needed for the view as HTML option since we tell it | |
601 | # to store the images in the memory file system. | |
602 | wx.FileSystem.AddHandler(wx.MemoryFSHandler()) | |
603 | ||
30fc5e8f RD |
604 | |
605 | def OnButton(self, evt): | |
606 | win = RichTextFrame(self, -1, "wx.richtext.RichTextCtrl", | |
607 | size=(700, 500), | |
608 | style = wx.DEFAULT_FRAME_STYLE) | |
609 | win.Show(True) | |
610 | ||
fe45b493 | 611 | # give easy access to the demo's PyShell if it's running |
30fc5e8f RD |
612 | self.rtfrm = win |
613 | self.rtc = win.rtc | |
614 | ||
fe45b493 RD |
615 | |
616 | ||
30fc5e8f RD |
617 | #---------------------------------------------------------------------- |
618 | ||
619 | def runTest(frame, nb, log): | |
620 | win = TestPanel(nb, log) | |
621 | return win | |
622 | ||
623 | #---------------------------------------------------------------------- | |
624 | ||
625 | ||
626 | ||
627 | overview = """<html><body> | |
628 | <h2><center>wx.richtext.RichTextCtrl</center></h2> | |
629 | ||
630 | </body></html> | |
631 | """ | |
632 | ||
633 | ||
634 | ||
635 | if __name__ == '__main__': | |
636 | import sys,os | |
637 | import run | |
638 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) | |
639 |