richtext.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/richtext/richtext.cpp
  3. // Purpose: wxWidgets rich text editor sample
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 2005-10-02
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #include "wx/fontdlg.h"
  27. #include "wx/splitter.h"
  28. #include "wx/sstream.h"
  29. #include "wx/html/htmlwin.h"
  30. #include "wx/stopwatch.h"
  31. #include "wx/sysopt.h"
  32. #if wxUSE_FILESYSTEM
  33. #include "wx/filesys.h"
  34. #include "wx/fs_mem.h"
  35. #endif
  36. #if wxUSE_HELP
  37. #include "wx/cshelp.h"
  38. #endif
  39. #ifndef wxHAS_IMAGES_IN_RESOURCES
  40. #include "../sample.xpm"
  41. #endif
  42. #include "bitmaps/smiley.xpm"
  43. // #include "bitmaps/idea.xpm"
  44. #include "bitmaps/zebra.xpm"
  45. #include "bitmaps/open.xpm"
  46. #include "bitmaps/save.xpm"
  47. #include "bitmaps/copy.xpm"
  48. #include "bitmaps/cut.xpm"
  49. #include "bitmaps/paste.xpm"
  50. #include "bitmaps/undo.xpm"
  51. #include "bitmaps/redo.xpm"
  52. #include "bitmaps/bold.xpm"
  53. #include "bitmaps/italic.xpm"
  54. #include "bitmaps/underline.xpm"
  55. #include "bitmaps/alignleft.xpm"
  56. #include "bitmaps/alignright.xpm"
  57. #include "bitmaps/centre.xpm"
  58. #include "bitmaps/font.xpm"
  59. #include "bitmaps/indentless.xpm"
  60. #include "bitmaps/indentmore.xpm"
  61. #include "wx/richtext/richtextctrl.h"
  62. #include "wx/richtext/richtextstyles.h"
  63. #include "wx/richtext/richtextxml.h"
  64. #include "wx/richtext/richtexthtml.h"
  65. #include "wx/richtext/richtextformatdlg.h"
  66. #include "wx/richtext/richtextsymboldlg.h"
  67. #include "wx/richtext/richtextstyledlg.h"
  68. #include "wx/richtext/richtextprint.h"
  69. #include "wx/richtext/richtextimagedlg.h"
  70. // A custom field type
  71. class wxRichTextFieldTypePropertiesTest: public wxRichTextFieldTypeStandard
  72. {
  73. public:
  74. wxRichTextFieldTypePropertiesTest(const wxString& name, const wxString& label, int displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE):
  75. wxRichTextFieldTypeStandard(name, label, displayStyle)
  76. {
  77. }
  78. wxRichTextFieldTypePropertiesTest(const wxString& name, const wxBitmap& bitmap, int displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE):
  79. wxRichTextFieldTypeStandard(name, bitmap, displayStyle)
  80. {
  81. }
  82. virtual bool CanEditProperties(wxRichTextField* WXUNUSED(obj)) const { return true; }
  83. virtual bool EditProperties(wxRichTextField* WXUNUSED(obj), wxWindow* WXUNUSED(parent), wxRichTextBuffer* WXUNUSED(buffer))
  84. {
  85. wxString label = GetLabel();
  86. wxMessageBox(wxString::Format(wxT("Editing %s"), label.c_str()));
  87. return true;
  88. }
  89. virtual wxString GetPropertiesMenuLabel(wxRichTextField* WXUNUSED(obj)) const
  90. {
  91. return GetLabel();
  92. }
  93. };
  94. // A custom composite field type
  95. class wxRichTextFieldTypeCompositeTest: public wxRichTextFieldTypePropertiesTest
  96. {
  97. public:
  98. wxRichTextFieldTypeCompositeTest(const wxString& name, const wxString& label):
  99. wxRichTextFieldTypePropertiesTest(name, label, wxRICHTEXT_FIELD_STYLE_COMPOSITE)
  100. {
  101. }
  102. virtual bool UpdateField(wxRichTextBuffer* buffer, wxRichTextField* obj)
  103. {
  104. if (buffer)
  105. {
  106. wxRichTextAttr attr(buffer->GetAttributes());
  107. attr.GetTextBoxAttr().Reset();
  108. attr.SetParagraphSpacingAfter(0);
  109. attr.SetLineSpacing(10);
  110. obj->SetAttributes(attr);
  111. }
  112. obj->GetChildren().Clear();
  113. wxRichTextParagraph* para = new wxRichTextParagraph;
  114. wxRichTextPlainText* text = new wxRichTextPlainText(GetLabel());
  115. para->AppendChild(text);
  116. obj->AppendChild(para);
  117. return true;
  118. }
  119. };
  120. // ----------------------------------------------------------------------------
  121. // resources
  122. // ----------------------------------------------------------------------------
  123. // ----------------------------------------------------------------------------
  124. // private classes
  125. // ----------------------------------------------------------------------------
  126. // Define a new application type, each program should derive a class from wxApp
  127. class MyRichTextCtrl: public wxRichTextCtrl
  128. {
  129. public:
  130. MyRichTextCtrl( wxWindow* parent, wxWindowID id = -1, const wxString& value = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
  131. long style = wxRE_MULTILINE, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxTextCtrlNameStr):
  132. wxRichTextCtrl(parent, id, value, pos, size, style, validator, name)
  133. {
  134. m_lockId = 0;
  135. m_locked = false;
  136. }
  137. void SetLockId(long id) { m_lockId = id; }
  138. long GetLockId() const { return m_lockId; }
  139. void BeginLock() { m_lockId ++; m_locked = true; }
  140. void EndLock() { m_locked = false; }
  141. bool IsLocked() const { return m_locked; }
  142. static void SetEnhancedDrawingHandler();
  143. /**
  144. Prepares the content just before insertion (or after buffer reset). Called by the same function in wxRichTextBuffer.
  145. Currently is only called if undo mode is on.
  146. */
  147. virtual void PrepareContent(wxRichTextParagraphLayoutBox& container);
  148. /**
  149. Can we delete this range?
  150. Sends an event to the control.
  151. */
  152. virtual bool CanDeleteRange(wxRichTextParagraphLayoutBox& container, const wxRichTextRange& range) const;
  153. /**
  154. Can we insert content at this position?
  155. Sends an event to the control.
  156. */
  157. virtual bool CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const;
  158. /**
  159. Finds a table, either selected or near the cursor
  160. */
  161. wxRichTextTable* FindTable() const;
  162. /**
  163. Helper for FindTable()
  164. */
  165. wxRichTextObject* FindCurrentPosition() const;
  166. long m_lockId;
  167. bool m_locked;
  168. };
  169. // Define a new application type, each program should derive a class from wxApp
  170. class MyApp : public wxApp
  171. {
  172. public:
  173. // override base class virtuals
  174. // ----------------------------
  175. // this one is called on application startup and is a good place for the app
  176. // initialization (doing it here and not in the ctor allows to have an error
  177. // return: if OnInit() returns false, the application terminates)
  178. virtual bool OnInit();
  179. virtual int OnExit();
  180. void CreateStyles();
  181. wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
  182. wxRichTextStyleSheet* m_styleSheet;
  183. #if wxUSE_PRINTING_ARCHITECTURE
  184. wxRichTextPrinting* GetPrinting() const { return m_printing; }
  185. wxRichTextPrinting* m_printing;
  186. #endif
  187. };
  188. // Define a new frame type: this is going to be our main frame
  189. class MyFrame : public wxFrame
  190. {
  191. public:
  192. // ctor(s)
  193. MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
  194. const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE);
  195. // event handlers (these functions should _not_ be virtual)
  196. void OnQuit(wxCommandEvent& event);
  197. void OnAbout(wxCommandEvent& event);
  198. void OnOpen(wxCommandEvent& event);
  199. void OnSave(wxCommandEvent& event);
  200. void OnSaveAs(wxCommandEvent& event);
  201. void OnBold(wxCommandEvent& event);
  202. void OnItalic(wxCommandEvent& event);
  203. void OnUnderline(wxCommandEvent& event);
  204. void OnStrikethrough(wxCommandEvent& event);
  205. void OnSuperscript(wxCommandEvent& event);
  206. void OnSubscript(wxCommandEvent& event);
  207. void OnUpdateBold(wxUpdateUIEvent& event);
  208. void OnUpdateItalic(wxUpdateUIEvent& event);
  209. void OnUpdateUnderline(wxUpdateUIEvent& event);
  210. void OnUpdateStrikethrough(wxUpdateUIEvent& event);
  211. void OnUpdateSuperscript(wxUpdateUIEvent& event);
  212. void OnUpdateSubscript(wxUpdateUIEvent& event);
  213. void OnAlignLeft(wxCommandEvent& event);
  214. void OnAlignCentre(wxCommandEvent& event);
  215. void OnAlignRight(wxCommandEvent& event);
  216. void OnUpdateAlignLeft(wxUpdateUIEvent& event);
  217. void OnUpdateAlignCentre(wxUpdateUIEvent& event);
  218. void OnUpdateAlignRight(wxUpdateUIEvent& event);
  219. void OnIndentMore(wxCommandEvent& event);
  220. void OnIndentLess(wxCommandEvent& event);
  221. void OnFont(wxCommandEvent& event);
  222. void OnImage(wxCommandEvent& event);
  223. void OnUpdateImage(wxUpdateUIEvent& event);
  224. void OnParagraph(wxCommandEvent& event);
  225. void OnFormat(wxCommandEvent& event);
  226. void OnUpdateFormat(wxUpdateUIEvent& event);
  227. void OnInsertSymbol(wxCommandEvent& event);
  228. void OnLineSpacingHalf(wxCommandEvent& event);
  229. void OnLineSpacingDouble(wxCommandEvent& event);
  230. void OnLineSpacingSingle(wxCommandEvent& event);
  231. void OnParagraphSpacingMore(wxCommandEvent& event);
  232. void OnParagraphSpacingLess(wxCommandEvent& event);
  233. void OnNumberList(wxCommandEvent& event);
  234. void OnBulletsAndNumbering(wxCommandEvent& event);
  235. void OnItemizeList(wxCommandEvent& event);
  236. void OnRenumberList(wxCommandEvent& event);
  237. void OnPromoteList(wxCommandEvent& event);
  238. void OnDemoteList(wxCommandEvent& event);
  239. void OnClearList(wxCommandEvent& event);
  240. void OnTableAddColumn(wxCommandEvent& event);
  241. void OnTableAddRow(wxCommandEvent& event);
  242. void OnTableDeleteColumn(wxCommandEvent& event);
  243. void OnTableDeleteRow(wxCommandEvent& event);
  244. void OnTableFocusedUpdateUI(wxUpdateUIEvent& event);
  245. void OnTableHasCellsUpdateUI(wxUpdateUIEvent& event);
  246. void OnReload(wxCommandEvent& event);
  247. void OnViewHTML(wxCommandEvent& event);
  248. void OnSwitchStyleSheets(wxCommandEvent& event);
  249. void OnManageStyles(wxCommandEvent& event);
  250. void OnInsertURL(wxCommandEvent& event);
  251. void OnURL(wxTextUrlEvent& event);
  252. void OnStyleSheetReplacing(wxRichTextEvent& event);
  253. #if wxUSE_PRINTING_ARCHITECTURE
  254. void OnPrint(wxCommandEvent& event);
  255. void OnPreview(wxCommandEvent& event);
  256. #endif
  257. void OnPageSetup(wxCommandEvent& event);
  258. void OnInsertImage(wxCommandEvent& event);
  259. void OnSetFontScale(wxCommandEvent& event);
  260. void OnSetDimensionScale(wxCommandEvent& event);
  261. protected:
  262. // Forward command events to the current rich text control, if any
  263. bool ProcessEvent(wxEvent& event);
  264. // Write text
  265. void WriteInitialText();
  266. private:
  267. // any class wishing to process wxWidgets events must use this macro
  268. wxDECLARE_EVENT_TABLE();
  269. MyRichTextCtrl* m_richTextCtrl;
  270. };
  271. // ----------------------------------------------------------------------------
  272. // constants
  273. // ----------------------------------------------------------------------------
  274. // IDs for the controls and the menu commands
  275. enum
  276. {
  277. // menu items
  278. ID_Quit = wxID_EXIT,
  279. ID_About = wxID_ABOUT,
  280. ID_FORMAT_BOLD = 100,
  281. ID_FORMAT_ITALIC,
  282. ID_FORMAT_UNDERLINE,
  283. ID_FORMAT_STRIKETHROUGH,
  284. ID_FORMAT_SUPERSCRIPT,
  285. ID_FORMAT_SUBSCRIPT,
  286. ID_FORMAT_FONT,
  287. ID_FORMAT_IMAGE,
  288. ID_FORMAT_PARAGRAPH,
  289. ID_FORMAT_CONTENT,
  290. ID_RELOAD,
  291. ID_INSERT_SYMBOL,
  292. ID_INSERT_URL,
  293. ID_INSERT_IMAGE,
  294. ID_FORMAT_ALIGN_LEFT,
  295. ID_FORMAT_ALIGN_CENTRE,
  296. ID_FORMAT_ALIGN_RIGHT,
  297. ID_FORMAT_INDENT_MORE,
  298. ID_FORMAT_INDENT_LESS,
  299. ID_FORMAT_PARAGRAPH_SPACING_MORE,
  300. ID_FORMAT_PARAGRAPH_SPACING_LESS,
  301. ID_FORMAT_LINE_SPACING_HALF,
  302. ID_FORMAT_LINE_SPACING_DOUBLE,
  303. ID_FORMAT_LINE_SPACING_SINGLE,
  304. ID_FORMAT_NUMBER_LIST,
  305. ID_FORMAT_BULLETS_AND_NUMBERING,
  306. ID_FORMAT_ITEMIZE_LIST,
  307. ID_FORMAT_RENUMBER_LIST,
  308. ID_FORMAT_PROMOTE_LIST,
  309. ID_FORMAT_DEMOTE_LIST,
  310. ID_FORMAT_CLEAR_LIST,
  311. ID_TABLE_ADD_COLUMN,
  312. ID_TABLE_ADD_ROW,
  313. ID_TABLE_DELETE_COLUMN,
  314. ID_TABLE_DELETE_ROW,
  315. ID_SET_FONT_SCALE,
  316. ID_SET_DIMENSION_SCALE,
  317. ID_VIEW_HTML,
  318. ID_SWITCH_STYLE_SHEETS,
  319. ID_MANAGE_STYLES,
  320. ID_PRINT,
  321. ID_PREVIEW,
  322. ID_PAGE_SETUP,
  323. ID_RICHTEXT_CTRL,
  324. ID_RICHTEXT_STYLE_LIST,
  325. ID_RICHTEXT_STYLE_COMBO
  326. };
  327. // ----------------------------------------------------------------------------
  328. // event tables and other macros for wxWidgets
  329. // ----------------------------------------------------------------------------
  330. // the event tables connect the wxWidgets events with the functions (event
  331. // handlers) which process them. It can be also done at run-time, but for the
  332. // simple menu events like this the static method is much simpler.
  333. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  334. EVT_MENU(ID_Quit, MyFrame::OnQuit)
  335. EVT_MENU(ID_About, MyFrame::OnAbout)
  336. EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
  337. EVT_MENU(wxID_SAVE, MyFrame::OnSave)
  338. EVT_MENU(wxID_SAVEAS, MyFrame::OnSaveAs)
  339. EVT_MENU(ID_FORMAT_BOLD, MyFrame::OnBold)
  340. EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
  341. EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
  342. EVT_MENU(ID_FORMAT_STRIKETHROUGH, MyFrame::OnStrikethrough)
  343. EVT_MENU(ID_FORMAT_SUPERSCRIPT, MyFrame::OnSuperscript)
  344. EVT_MENU(ID_FORMAT_SUBSCRIPT, MyFrame::OnSubscript)
  345. EVT_UPDATE_UI(ID_FORMAT_BOLD, MyFrame::OnUpdateBold)
  346. EVT_UPDATE_UI(ID_FORMAT_ITALIC, MyFrame::OnUpdateItalic)
  347. EVT_UPDATE_UI(ID_FORMAT_UNDERLINE, MyFrame::OnUpdateUnderline)
  348. EVT_UPDATE_UI(ID_FORMAT_STRIKETHROUGH, MyFrame::OnUpdateStrikethrough)
  349. EVT_UPDATE_UI(ID_FORMAT_SUPERSCRIPT, MyFrame::OnUpdateSuperscript)
  350. EVT_UPDATE_UI(ID_FORMAT_SUBSCRIPT, MyFrame::OnUpdateSubscript)
  351. EVT_MENU(ID_FORMAT_ALIGN_LEFT, MyFrame::OnAlignLeft)
  352. EVT_MENU(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnAlignCentre)
  353. EVT_MENU(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnAlignRight)
  354. EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT, MyFrame::OnUpdateAlignLeft)
  355. EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnUpdateAlignCentre)
  356. EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnUpdateAlignRight)
  357. EVT_MENU(ID_FORMAT_FONT, MyFrame::OnFont)
  358. EVT_MENU(ID_FORMAT_IMAGE, MyFrame::OnImage)
  359. EVT_MENU(ID_FORMAT_PARAGRAPH, MyFrame::OnParagraph)
  360. EVT_MENU(ID_FORMAT_CONTENT, MyFrame::OnFormat)
  361. EVT_UPDATE_UI(ID_FORMAT_CONTENT, MyFrame::OnUpdateFormat)
  362. EVT_UPDATE_UI(ID_FORMAT_FONT, MyFrame::OnUpdateFormat)
  363. EVT_UPDATE_UI(ID_FORMAT_IMAGE, MyFrame::OnUpdateImage)
  364. EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH, MyFrame::OnUpdateFormat)
  365. EVT_MENU(ID_FORMAT_INDENT_MORE, MyFrame::OnIndentMore)
  366. EVT_MENU(ID_FORMAT_INDENT_LESS, MyFrame::OnIndentLess)
  367. EVT_MENU(ID_FORMAT_LINE_SPACING_HALF, MyFrame::OnLineSpacingHalf)
  368. EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE, MyFrame::OnLineSpacingSingle)
  369. EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE, MyFrame::OnLineSpacingDouble)
  370. EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE, MyFrame::OnParagraphSpacingMore)
  371. EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS, MyFrame::OnParagraphSpacingLess)
  372. EVT_MENU(ID_RELOAD, MyFrame::OnReload)
  373. EVT_MENU(ID_INSERT_SYMBOL, MyFrame::OnInsertSymbol)
  374. EVT_MENU(ID_INSERT_URL, MyFrame::OnInsertURL)
  375. EVT_MENU(ID_INSERT_IMAGE, MyFrame::OnInsertImage)
  376. EVT_MENU(ID_FORMAT_NUMBER_LIST, MyFrame::OnNumberList)
  377. EVT_MENU(ID_FORMAT_BULLETS_AND_NUMBERING, MyFrame::OnBulletsAndNumbering)
  378. EVT_MENU(ID_FORMAT_ITEMIZE_LIST, MyFrame::OnItemizeList)
  379. EVT_MENU(ID_FORMAT_RENUMBER_LIST, MyFrame::OnRenumberList)
  380. EVT_MENU(ID_FORMAT_PROMOTE_LIST, MyFrame::OnPromoteList)
  381. EVT_MENU(ID_FORMAT_DEMOTE_LIST, MyFrame::OnDemoteList)
  382. EVT_MENU(ID_FORMAT_CLEAR_LIST, MyFrame::OnClearList)
  383. EVT_MENU(ID_TABLE_ADD_COLUMN, MyFrame::OnTableAddColumn)
  384. EVT_MENU(ID_TABLE_ADD_ROW, MyFrame::OnTableAddRow)
  385. EVT_MENU(ID_TABLE_DELETE_COLUMN, MyFrame::OnTableDeleteColumn)
  386. EVT_MENU(ID_TABLE_DELETE_ROW, MyFrame::OnTableDeleteRow)
  387. EVT_UPDATE_UI_RANGE(ID_TABLE_ADD_COLUMN, ID_TABLE_ADD_ROW, MyFrame::OnTableFocusedUpdateUI)
  388. EVT_UPDATE_UI_RANGE(ID_TABLE_DELETE_COLUMN, ID_TABLE_DELETE_ROW, MyFrame::OnTableHasCellsUpdateUI)
  389. EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
  390. EVT_MENU(ID_SWITCH_STYLE_SHEETS, MyFrame::OnSwitchStyleSheets)
  391. EVT_MENU(ID_MANAGE_STYLES, MyFrame::OnManageStyles)
  392. #if wxUSE_PRINTING_ARCHITECTURE
  393. EVT_MENU(ID_PRINT, MyFrame::OnPrint)
  394. EVT_MENU(ID_PREVIEW, MyFrame::OnPreview)
  395. #endif
  396. EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
  397. EVT_TEXT_URL(wxID_ANY, MyFrame::OnURL)
  398. EVT_RICHTEXT_STYLESHEET_REPLACING(wxID_ANY, MyFrame::OnStyleSheetReplacing)
  399. EVT_MENU(ID_SET_FONT_SCALE, MyFrame::OnSetFontScale)
  400. EVT_MENU(ID_SET_DIMENSION_SCALE, MyFrame::OnSetDimensionScale)
  401. wxEND_EVENT_TABLE()
  402. // Create a new application object: this macro will allow wxWidgets to create
  403. // the application object during program execution (it's better than using a
  404. // static object for many reasons) and also implements the accessor function
  405. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  406. // not wxApp)
  407. IMPLEMENT_APP(MyApp)
  408. // ============================================================================
  409. // implementation
  410. // ============================================================================
  411. // ----------------------------------------------------------------------------
  412. // the application class
  413. // ----------------------------------------------------------------------------
  414. // 'Main program' equivalent: the program execution "starts" here
  415. bool MyApp::OnInit()
  416. {
  417. if ( !wxApp::OnInit() )
  418. return false;
  419. #if wxUSE_HELP
  420. wxHelpProvider::Set(new wxSimpleHelpProvider);
  421. #endif
  422. m_styleSheet = new wxRichTextStyleSheet;
  423. #if wxUSE_PRINTING_ARCHITECTURE
  424. m_printing = new wxRichTextPrinting(wxT("Test Document"));
  425. m_printing->SetFooterText(wxT("@TITLE@"), wxRICHTEXT_PAGE_ALL, wxRICHTEXT_PAGE_CENTRE);
  426. m_printing->SetFooterText(wxT("Page @PAGENUM@"), wxRICHTEXT_PAGE_ALL, wxRICHTEXT_PAGE_RIGHT);
  427. #endif
  428. CreateStyles();
  429. MyRichTextCtrl::SetEnhancedDrawingHandler();
  430. // Add extra handlers (plain text is automatically added)
  431. wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
  432. wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler);
  433. // Add field types
  434. wxRichTextBuffer::AddFieldType(new wxRichTextFieldTypePropertiesTest(wxT("rectangle"), wxT("RECTANGLE"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_RECTANGLE));
  435. wxRichTextFieldTypeStandard* s1 = new wxRichTextFieldTypeStandard(wxT("begin-section"), wxT("SECTION"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_START_TAG);
  436. s1->SetBackgroundColour(*wxBLUE);
  437. wxRichTextFieldTypeStandard* s2 = new wxRichTextFieldTypeStandard(wxT("end-section"), wxT("SECTION"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_END_TAG);
  438. s2->SetBackgroundColour(*wxBLUE);
  439. wxRichTextFieldTypeStandard* s3 = new wxRichTextFieldTypeStandard(wxT("bitmap"), wxBitmap(paste_xpm), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_NO_BORDER);
  440. wxRichTextBuffer::AddFieldType(s1);
  441. wxRichTextBuffer::AddFieldType(s2);
  442. wxRichTextBuffer::AddFieldType(s3);
  443. wxRichTextFieldTypeCompositeTest* s4 = new wxRichTextFieldTypeCompositeTest(wxT("composite"), wxT("This is a field value"));
  444. wxRichTextBuffer::AddFieldType(s4);
  445. // Add image handlers
  446. #if wxUSE_LIBPNG
  447. wxImage::AddHandler( new wxPNGHandler );
  448. #endif
  449. #if wxUSE_LIBJPEG
  450. wxImage::AddHandler( new wxJPEGHandler );
  451. #endif
  452. #if wxUSE_GIF
  453. wxImage::AddHandler( new wxGIFHandler );
  454. #endif
  455. #if wxUSE_FILESYSTEM
  456. wxFileSystem::AddHandler( new wxMemoryFSHandler );
  457. #endif
  458. // create the main application window
  459. wxSize size = wxGetDisplaySize();
  460. size.Scale(0.75, 0.75);
  461. MyFrame *frame = new MyFrame(wxT("wxRichTextCtrl Sample"), wxID_ANY, wxDefaultPosition, size);
  462. #if wxUSE_PRINTING_ARCHITECTURE
  463. m_printing->SetParentWindow(frame);
  464. #endif
  465. // and show it (the frames, unlike simple controls, are not shown when
  466. // created initially)
  467. frame->Show(true);
  468. // success: wxApp::OnRun() will be called which will enter the main message
  469. // loop and the application will run. If we returned false here, the
  470. // application would exit immediately.
  471. return true;
  472. }
  473. int MyApp::OnExit()
  474. {
  475. #if wxUSE_PRINTING_ARCHITECTURE
  476. delete m_printing;
  477. #endif
  478. delete m_styleSheet;
  479. return 0;
  480. }
  481. void MyApp::CreateStyles()
  482. {
  483. // Paragraph styles
  484. wxFont romanFont(12, wxROMAN, wxNORMAL, wxNORMAL);
  485. wxFont swissFont(12, wxSWISS, wxNORMAL, wxNORMAL);
  486. wxRichTextParagraphStyleDefinition* normalPara = new wxRichTextParagraphStyleDefinition(wxT("Normal"));
  487. wxRichTextAttr normalAttr;
  488. normalAttr.SetFontFaceName(romanFont.GetFaceName());
  489. normalAttr.SetFontSize(12);
  490. // Let's set all attributes for this style
  491. normalAttr.SetFlags(wxTEXT_ATTR_FONT | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR|wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|
  492. wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
  493. wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER);
  494. normalPara->SetStyle(normalAttr);
  495. m_styleSheet->AddParagraphStyle(normalPara);
  496. wxRichTextParagraphStyleDefinition* indentedPara = new wxRichTextParagraphStyleDefinition(wxT("Indented"));
  497. wxRichTextAttr indentedAttr;
  498. indentedAttr.SetFontFaceName(romanFont.GetFaceName());
  499. indentedAttr.SetFontSize(12);
  500. indentedAttr.SetLeftIndent(100, 0);
  501. // We only want to affect indentation
  502. indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
  503. indentedPara->SetStyle(indentedAttr);
  504. m_styleSheet->AddParagraphStyle(indentedPara);
  505. wxRichTextParagraphStyleDefinition* indentedPara2 = new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
  506. wxRichTextAttr indentedAttr2;
  507. indentedAttr2.SetFontFaceName(romanFont.GetFaceName());
  508. indentedAttr2.SetFontSize(12);
  509. indentedAttr2.SetFontWeight(wxFONTWEIGHT_BOLD);
  510. indentedAttr2.SetTextColour(*wxRED);
  511. indentedAttr2.SetFontSize(12);
  512. indentedAttr2.SetLeftIndent(100, 0);
  513. // We want to affect indentation, font and text colour
  514. indentedAttr2.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_FONT|wxTEXT_ATTR_TEXT_COLOUR);
  515. indentedPara2->SetStyle(indentedAttr2);
  516. m_styleSheet->AddParagraphStyle(indentedPara2);
  517. wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
  518. wxRichTextAttr flIndentedAttr;
  519. flIndentedAttr.SetFontFaceName(swissFont.GetFaceName());
  520. flIndentedAttr.SetFontSize(12);
  521. flIndentedAttr.SetLeftIndent(100, -100);
  522. // We only want to affect indentation
  523. flIndentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
  524. flIndentedPara->SetStyle(flIndentedAttr);
  525. m_styleSheet->AddParagraphStyle(flIndentedPara);
  526. // Character styles
  527. wxRichTextCharacterStyleDefinition* boldDef = new wxRichTextCharacterStyleDefinition(wxT("Bold"));
  528. wxRichTextAttr boldAttr;
  529. boldAttr.SetFontFaceName(romanFont.GetFaceName());
  530. boldAttr.SetFontSize(12);
  531. boldAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
  532. // We only want to affect boldness
  533. boldAttr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
  534. boldDef->SetStyle(boldAttr);
  535. m_styleSheet->AddCharacterStyle(boldDef);
  536. wxRichTextCharacterStyleDefinition* italicDef = new wxRichTextCharacterStyleDefinition(wxT("Italic"));
  537. wxRichTextAttr italicAttr;
  538. italicAttr.SetFontFaceName(romanFont.GetFaceName());
  539. italicAttr.SetFontSize(12);
  540. italicAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
  541. // We only want to affect italics
  542. italicAttr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
  543. italicDef->SetStyle(italicAttr);
  544. m_styleSheet->AddCharacterStyle(italicDef);
  545. wxRichTextCharacterStyleDefinition* redDef = new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
  546. wxRichTextAttr redAttr;
  547. redAttr.SetFontFaceName(romanFont.GetFaceName());
  548. redAttr.SetFontSize(12);
  549. redAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
  550. redAttr.SetTextColour(*wxRED);
  551. // We only want to affect colour, weight and face
  552. redAttr.SetFlags(wxTEXT_ATTR_FONT_FACE|wxTEXT_ATTR_FONT_WEIGHT|wxTEXT_ATTR_TEXT_COLOUR);
  553. redDef->SetStyle(redAttr);
  554. m_styleSheet->AddCharacterStyle(redDef);
  555. wxRichTextListStyleDefinition* bulletList = new wxRichTextListStyleDefinition(wxT("Bullet List 1"));
  556. int i;
  557. for (i = 0; i < 10; i++)
  558. {
  559. wxString bulletText;
  560. if (i == 0)
  561. bulletText = wxT("standard/circle");
  562. else if (i == 1)
  563. bulletText = wxT("standard/square");
  564. else if (i == 2)
  565. bulletText = wxT("standard/circle");
  566. else if (i == 3)
  567. bulletText = wxT("standard/square");
  568. else
  569. bulletText = wxT("standard/circle");
  570. bulletList->SetAttributes(i, (i+1)*60, 60, wxTEXT_ATTR_BULLET_STYLE_STANDARD, bulletText);
  571. }
  572. m_styleSheet->AddListStyle(bulletList);
  573. wxRichTextListStyleDefinition* numberedList = new wxRichTextListStyleDefinition(wxT("Numbered List 1"));
  574. for (i = 0; i < 10; i++)
  575. {
  576. long numberStyle;
  577. if (i == 0)
  578. numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
  579. else if (i == 1)
  580. numberStyle = wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
  581. else if (i == 2)
  582. numberStyle = wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
  583. else if (i == 3)
  584. numberStyle = wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
  585. else
  586. numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
  587. numberStyle |= wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT;
  588. numberedList->SetAttributes(i, (i+1)*60, 60, numberStyle);
  589. }
  590. m_styleSheet->AddListStyle(numberedList);
  591. wxRichTextListStyleDefinition* outlineList = new wxRichTextListStyleDefinition(wxT("Outline List 1"));
  592. for (i = 0; i < 10; i++)
  593. {
  594. long numberStyle;
  595. if (i < 4)
  596. numberStyle = wxTEXT_ATTR_BULLET_STYLE_OUTLINE|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
  597. else
  598. numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
  599. outlineList->SetAttributes(i, (i+1)*120, 120, numberStyle);
  600. }
  601. m_styleSheet->AddListStyle(outlineList);
  602. }
  603. // ----------------------------------------------------------------------------
  604. // main frame
  605. // ----------------------------------------------------------------------------
  606. // frame constructor
  607. MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
  608. const wxSize& size, long style)
  609. : wxFrame(NULL, id, title, pos, size, style)
  610. {
  611. #ifdef __WXMAC__
  612. SetWindowVariant(wxWINDOW_VARIANT_SMALL);
  613. #endif
  614. // set the frame icon
  615. SetIcon(wxICON(sample));
  616. // create a menu bar
  617. wxMenu *fileMenu = new wxMenu;
  618. // the "About" item should be in the help menu
  619. wxMenu *helpMenu = new wxMenu;
  620. helpMenu->Append(ID_About, wxT("&About\tF1"), wxT("Show about dialog"));
  621. fileMenu->Append(wxID_OPEN, wxT("&Open\tCtrl+O"), wxT("Open a file"));
  622. fileMenu->Append(wxID_SAVE, wxT("&Save\tCtrl+S"), wxT("Save a file"));
  623. fileMenu->Append(wxID_SAVEAS, wxT("&Save As...\tF12"), wxT("Save to a new file"));
  624. fileMenu->AppendSeparator();
  625. fileMenu->Append(ID_RELOAD, wxT("&Reload Text\tF2"), wxT("Reload the initial text"));
  626. fileMenu->AppendSeparator();
  627. fileMenu->Append(ID_PAGE_SETUP, wxT("Page Set&up..."), wxT("Page setup"));
  628. #if wxUSE_PRINTING_ARCHITECTURE
  629. fileMenu->Append(ID_PRINT, wxT("&Print...\tCtrl+P"), wxT("Print"));
  630. fileMenu->Append(ID_PREVIEW, wxT("Print Pre&view"), wxT("Print preview"));
  631. #endif
  632. fileMenu->AppendSeparator();
  633. fileMenu->Append(ID_VIEW_HTML, wxT("&View as HTML"), wxT("View HTML"));
  634. fileMenu->AppendSeparator();
  635. fileMenu->Append(ID_Quit, wxT("E&xit\tAlt+X"), wxT("Quit this program"));
  636. wxMenu* editMenu = new wxMenu;
  637. editMenu->Append(wxID_UNDO, _("&Undo\tCtrl+Z"));
  638. editMenu->Append(wxID_REDO, _("&Redo\tCtrl+Y"));
  639. editMenu->AppendSeparator();
  640. editMenu->Append(wxID_CUT, _("Cu&t\tCtrl+X"));
  641. editMenu->Append(wxID_COPY, _("&Copy\tCtrl+C"));
  642. editMenu->Append(wxID_PASTE, _("&Paste\tCtrl+V"));
  643. editMenu->AppendSeparator();
  644. editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A"));
  645. editMenu->AppendSeparator();
  646. editMenu->Append(ID_SET_FONT_SCALE, _("Set &Text Scale..."));
  647. editMenu->Append(ID_SET_DIMENSION_SCALE, _("Set &Dimension Scale..."));
  648. wxMenu* formatMenu = new wxMenu;
  649. formatMenu->AppendCheckItem(ID_FORMAT_BOLD, _("&Bold\tCtrl+B"));
  650. formatMenu->AppendCheckItem(ID_FORMAT_ITALIC, _("&Italic\tCtrl+I"));
  651. formatMenu->AppendCheckItem(ID_FORMAT_UNDERLINE, _("&Underline\tCtrl+U"));
  652. formatMenu->AppendSeparator();
  653. formatMenu->AppendCheckItem(ID_FORMAT_STRIKETHROUGH, _("Stri&kethrough"));
  654. formatMenu->AppendCheckItem(ID_FORMAT_SUPERSCRIPT, _("Superscrip&t"));
  655. formatMenu->AppendCheckItem(ID_FORMAT_SUBSCRIPT, _("Subscrip&t"));
  656. formatMenu->AppendSeparator();
  657. formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_LEFT, _("L&eft Align"));
  658. formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
  659. formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
  660. formatMenu->AppendSeparator();
  661. formatMenu->Append(ID_FORMAT_INDENT_MORE, _("Indent &More"));
  662. formatMenu->Append(ID_FORMAT_INDENT_LESS, _("Indent &Less"));
  663. formatMenu->AppendSeparator();
  664. formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE, _("Increase Paragraph &Spacing"));
  665. formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS, _("Decrease &Paragraph Spacing"));
  666. formatMenu->AppendSeparator();
  667. formatMenu->Append(ID_FORMAT_LINE_SPACING_SINGLE, _("Normal Line Spacing"));
  668. formatMenu->Append(ID_FORMAT_LINE_SPACING_HALF, _("1.5 Line Spacing"));
  669. formatMenu->Append(ID_FORMAT_LINE_SPACING_DOUBLE, _("Double Line Spacing"));
  670. formatMenu->AppendSeparator();
  671. formatMenu->Append(ID_FORMAT_FONT, _("&Font..."));
  672. formatMenu->Append(ID_FORMAT_IMAGE, _("Image Property"));
  673. formatMenu->Append(ID_FORMAT_PARAGRAPH, _("&Paragraph..."));
  674. formatMenu->Append(ID_FORMAT_CONTENT, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
  675. formatMenu->AppendSeparator();
  676. formatMenu->Append(ID_SWITCH_STYLE_SHEETS, _("&Switch Style Sheets"));
  677. formatMenu->Append(ID_MANAGE_STYLES, _("&Manage Styles"));
  678. wxMenu* listsMenu = new wxMenu;
  679. listsMenu->Append(ID_FORMAT_BULLETS_AND_NUMBERING, _("Bullets and &Numbering..."));
  680. listsMenu->AppendSeparator();
  681. listsMenu->Append(ID_FORMAT_NUMBER_LIST, _("Number List"));
  682. listsMenu->Append(ID_FORMAT_ITEMIZE_LIST, _("Itemize List"));
  683. listsMenu->Append(ID_FORMAT_RENUMBER_LIST, _("Renumber List"));
  684. listsMenu->Append(ID_FORMAT_PROMOTE_LIST, _("Promote List Items"));
  685. listsMenu->Append(ID_FORMAT_DEMOTE_LIST, _("Demote List Items"));
  686. listsMenu->Append(ID_FORMAT_CLEAR_LIST, _("Clear List Formatting"));
  687. wxMenu* tableMenu = new wxMenu;
  688. tableMenu->Append(ID_TABLE_ADD_COLUMN, _("&Add Column"));
  689. tableMenu->Append(ID_TABLE_ADD_ROW, _("Add &Row"));
  690. tableMenu->Append(ID_TABLE_DELETE_COLUMN, _("Delete &Column"));
  691. tableMenu->Append(ID_TABLE_DELETE_ROW, _("&Delete Row"));
  692. wxMenu* insertMenu = new wxMenu;
  693. insertMenu->Append(ID_INSERT_SYMBOL, _("&Symbol...\tCtrl+I"));
  694. insertMenu->Append(ID_INSERT_URL, _("&URL..."));
  695. insertMenu->Append(ID_INSERT_IMAGE, _("&Image..."));
  696. // now append the freshly created menu to the menu bar...
  697. wxMenuBar *menuBar = new wxMenuBar();
  698. menuBar->Append(fileMenu, wxT("&File"));
  699. menuBar->Append(editMenu, wxT("&Edit"));
  700. menuBar->Append(formatMenu, wxT("F&ormat"));
  701. menuBar->Append(listsMenu, wxT("&Lists"));
  702. menuBar->Append(tableMenu, wxT("&Tables"));
  703. menuBar->Append(insertMenu, wxT("&Insert"));
  704. menuBar->Append(helpMenu, wxT("&Help"));
  705. // ... and attach this menu bar to the frame
  706. SetMenuBar(menuBar);
  707. // create a status bar just for fun (by default with 1 pane only)
  708. // but don't create it on limited screen space (WinCE)
  709. bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
  710. #if wxUSE_STATUSBAR
  711. if ( !is_pda )
  712. {
  713. CreateStatusBar(2);
  714. SetStatusText(wxT("Welcome to wxRichTextCtrl!"));
  715. }
  716. #endif
  717. wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
  718. SetSizer(sizer);
  719. // On Mac, don't create a 'native' wxToolBar because small bitmaps are not supported by native
  720. // toolbars. On Mac, a non-native, small-bitmap toolbar doesn't show unless it is explicitly
  721. // managed, hence the use of sizers. In a real application, use larger icons for the main
  722. // toolbar to avoid the need for this workaround. Or, use the toolbar in a container window
  723. // as part of a more complex hierarchy, and the toolbar will automatically be non-native.
  724. wxSystemOptions::SetOption(wxT("mac.toolbar.no-native"), 1);
  725. wxToolBar* toolBar = new wxToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  726. wxNO_BORDER|wxTB_FLAT|wxTB_NODIVIDER|wxTB_NOALIGN);
  727. sizer->Add(toolBar, 0, wxEXPAND);
  728. toolBar->AddTool(wxID_OPEN, wxEmptyString, wxBitmap(open_xpm), _("Open"));
  729. toolBar->AddTool(wxID_SAVEAS, wxEmptyString, wxBitmap(save_xpm), _("Save"));
  730. toolBar->AddSeparator();
  731. toolBar->AddTool(wxID_CUT, wxEmptyString, wxBitmap(cut_xpm), _("Cut"));
  732. toolBar->AddTool(wxID_COPY, wxEmptyString, wxBitmap(copy_xpm), _("Copy"));
  733. toolBar->AddTool(wxID_PASTE, wxEmptyString, wxBitmap(paste_xpm), _("Paste"));
  734. toolBar->AddSeparator();
  735. toolBar->AddTool(wxID_UNDO, wxEmptyString, wxBitmap(undo_xpm), _("Undo"));
  736. toolBar->AddTool(wxID_REDO, wxEmptyString, wxBitmap(redo_xpm), _("Redo"));
  737. toolBar->AddSeparator();
  738. toolBar->AddCheckTool(ID_FORMAT_BOLD, wxEmptyString, wxBitmap(bold_xpm), wxNullBitmap, _("Bold"));
  739. toolBar->AddCheckTool(ID_FORMAT_ITALIC, wxEmptyString, wxBitmap(italic_xpm), wxNullBitmap, _("Italic"));
  740. toolBar->AddCheckTool(ID_FORMAT_UNDERLINE, wxEmptyString, wxBitmap(underline_xpm), wxNullBitmap, _("Underline"));
  741. toolBar->AddSeparator();
  742. toolBar->AddCheckTool(ID_FORMAT_ALIGN_LEFT, wxEmptyString, wxBitmap(alignleft_xpm), wxNullBitmap, _("Align Left"));
  743. toolBar->AddCheckTool(ID_FORMAT_ALIGN_CENTRE, wxEmptyString, wxBitmap(centre_xpm), wxNullBitmap, _("Centre"));
  744. toolBar->AddCheckTool(ID_FORMAT_ALIGN_RIGHT, wxEmptyString, wxBitmap(alignright_xpm), wxNullBitmap, _("Align Right"));
  745. toolBar->AddSeparator();
  746. toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxEmptyString, wxBitmap(indentless_xpm), _("Indent Less"));
  747. toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxEmptyString, wxBitmap(indentmore_xpm), _("Indent More"));
  748. toolBar->AddSeparator();
  749. toolBar->AddTool(ID_FORMAT_FONT, wxEmptyString, wxBitmap(font_xpm), _("Font"));
  750. toolBar->AddSeparator();
  751. wxRichTextStyleComboCtrl* combo = new wxRichTextStyleComboCtrl(toolBar, ID_RICHTEXT_STYLE_COMBO, wxDefaultPosition, wxSize(160, -1), wxCB_READONLY);
  752. toolBar->AddControl(combo);
  753. toolBar->Realize();
  754. wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_LIVE_UPDATE);
  755. sizer->Add(splitter, 1, wxEXPAND);
  756. wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
  757. wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
  758. wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
  759. m_richTextCtrl = new MyRichTextCtrl(splitter, ID_RICHTEXT_CTRL, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxVSCROLL|wxHSCROLL/*|wxWANTS_CHARS*/);
  760. wxASSERT(!m_richTextCtrl->GetBuffer().GetAttributes().HasFontPixelSize());
  761. wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
  762. m_richTextCtrl->SetFont(font);
  763. wxASSERT(!m_richTextCtrl->GetBuffer().GetAttributes().HasFontPixelSize());
  764. m_richTextCtrl->SetMargins(10, 10);
  765. m_richTextCtrl->SetStyleSheet(wxGetApp().GetStyleSheet());
  766. combo->SetStyleSheet(wxGetApp().GetStyleSheet());
  767. combo->SetRichTextCtrl(m_richTextCtrl);
  768. combo->UpdateStyles();
  769. wxRichTextStyleListCtrl* styleListCtrl = new wxRichTextStyleListCtrl(splitter, ID_RICHTEXT_STYLE_LIST);
  770. wxSize display = wxGetDisplaySize();
  771. if ( is_pda && ( display.GetWidth() < display.GetHeight() ) )
  772. {
  773. splitter->SplitHorizontally(m_richTextCtrl, styleListCtrl);
  774. }
  775. else
  776. {
  777. int width = GetClientSize().GetWidth() * 0.8;
  778. splitter->SplitVertically(m_richTextCtrl, styleListCtrl, width);
  779. splitter->SetSashGravity(0.8);
  780. }
  781. Layout();
  782. splitter->UpdateSize();
  783. styleListCtrl->SetStyleSheet(wxGetApp().GetStyleSheet());
  784. styleListCtrl->SetRichTextCtrl(m_richTextCtrl);
  785. styleListCtrl->UpdateStyles();
  786. WriteInitialText();
  787. }
  788. // Write text
  789. void MyFrame::WriteInitialText()
  790. {
  791. MyRichTextCtrl& r = *m_richTextCtrl;
  792. r.SetDefaultStyle(wxRichTextAttr());
  793. r.Freeze();
  794. r.BeginSuppressUndo();
  795. r.BeginParagraphSpacing(0, 20);
  796. r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
  797. r.BeginBold();
  798. r.BeginFontSize(14);
  799. wxString lineBreak = (wxChar) 29;
  800. r.WriteText(wxString(wxT("Welcome to wxRichTextCtrl, a wxWidgets control")) + lineBreak + wxT("for editing and presenting styled text and images\n"));
  801. r.EndFontSize();
  802. r.BeginItalic();
  803. r.WriteText(wxT("by Julian Smart"));
  804. r.EndItalic();
  805. r.EndBold();
  806. r.Newline();
  807. r.WriteImage(wxBitmap(zebra_xpm));
  808. r.Newline();
  809. r.Newline();
  810. r.EndAlignment();
  811. #if 0
  812. r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
  813. r.WriteText(wxString(wxT("This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side.")));
  814. r.Newline();
  815. r.EndAlignment();
  816. #endif
  817. r.BeginAlignment(wxTEXT_ALIGNMENT_LEFT);
  818. wxRichTextAttr imageAttr;
  819. imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT);
  820. r.WriteText(wxString(wxT("This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side.")));
  821. r.WriteImage(wxBitmap(zebra_xpm), wxBITMAP_TYPE_PNG, imageAttr);
  822. imageAttr.GetTextBoxAttr().GetTop().SetValue(200);
  823. imageAttr.GetTextBoxAttr().GetTop().SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
  824. imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_RIGHT);
  825. r.WriteImage(wxBitmap(zebra_xpm), wxBITMAP_TYPE_PNG, imageAttr);
  826. r.WriteText(wxString(wxT("This is a simple test for a floating right image test. The zebra image should be placed at the right side of the current buffer and all the text should flow around it at the left side. This is a simple test for a floating left image test. The zebra image should be placed at the right side of the current buffer and all the text should flow around it at the left side. This is a simple test for a floating left image test. The zebra image should be placed at the right side of the current buffer and all the text should flow around it at the left side.")));
  827. r.EndAlignment();
  828. r.Newline();
  829. r.WriteText(wxT("What can you do with this thing? "));
  830. r.WriteImage(wxBitmap(smiley_xpm));
  831. r.WriteText(wxT(" Well, you can change text "));
  832. r.BeginTextColour(*wxRED);
  833. r.WriteText(wxT("colour, like this red bit."));
  834. r.EndTextColour();
  835. wxRichTextAttr backgroundColourAttr;
  836. backgroundColourAttr.SetBackgroundColour(*wxGREEN);
  837. backgroundColourAttr.SetTextColour(*wxBLUE);
  838. r.BeginStyle(backgroundColourAttr);
  839. r.WriteText(wxT(" And this blue on green bit."));
  840. r.EndStyle();
  841. r.WriteText(wxT(" Naturally you can make things "));
  842. r.BeginBold();
  843. r.WriteText(wxT("bold "));
  844. r.EndBold();
  845. r.BeginItalic();
  846. r.WriteText(wxT("or italic "));
  847. r.EndItalic();
  848. r.BeginUnderline();
  849. r.WriteText(wxT("or underlined."));
  850. r.EndUnderline();
  851. r.BeginFontSize(14);
  852. r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
  853. r.EndFontSize();
  854. r.WriteText(wxT(" Next we'll show an indented paragraph."));
  855. r.Newline();
  856. r.BeginLeftIndent(60);
  857. r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
  858. r.Newline();
  859. r.EndLeftIndent();
  860. r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
  861. r.Newline();
  862. r.BeginLeftIndent(100, -40);
  863. r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
  864. r.Newline();
  865. r.EndLeftIndent();
  866. r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
  867. r.Newline();
  868. r.BeginNumberedBullet(1, 100, 60);
  869. r.WriteText(wxT("This is my first item. Note that wxRichTextCtrl can apply numbering and bullets automatically based on list styles, but this list is formatted explicitly by setting indents."));
  870. r.Newline();
  871. r.EndNumberedBullet();
  872. r.BeginNumberedBullet(2, 100, 60);
  873. r.WriteText(wxT("This is my second item."));
  874. r.Newline();
  875. r.EndNumberedBullet();
  876. r.WriteText(wxT("The following paragraph is right-indented:"));
  877. r.Newline();
  878. r.BeginRightIndent(200);
  879. r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
  880. r.Newline();
  881. r.EndRightIndent();
  882. r.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
  883. r.Newline();
  884. r.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT);
  885. r.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
  886. r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
  887. r.Newline();
  888. r.EndLineSpacing();
  889. r.EndAlignment();
  890. wxArrayInt tabs;
  891. tabs.Add(400);
  892. tabs.Add(600);
  893. tabs.Add(800);
  894. tabs.Add(1000);
  895. wxRichTextAttr attr;
  896. attr.SetFlags(wxTEXT_ATTR_TABS);
  897. attr.SetTabs(tabs);
  898. r.SetDefaultStyle(attr);
  899. r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
  900. r.Newline();
  901. r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
  902. r.Newline();
  903. r.BeginSymbolBullet(wxT('*'), 100, 60);
  904. r.WriteText(wxT("Compatibility with wxTextCtrl API"));
  905. r.Newline();
  906. r.EndSymbolBullet();
  907. r.BeginSymbolBullet(wxT('*'), 100, 60);
  908. r.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
  909. r.Newline();
  910. r.EndSymbolBullet();
  911. r.BeginSymbolBullet(wxT('*'), 100, 60);
  912. r.WriteText(wxT("XML loading and saving"));
  913. r.Newline();
  914. r.EndSymbolBullet();
  915. r.BeginSymbolBullet(wxT('*'), 100, 60);
  916. r.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
  917. r.Newline();
  918. r.EndSymbolBullet();
  919. r.BeginSymbolBullet(wxT('*'), 100, 60);
  920. r.WriteText(wxT("Clipboard copy and paste"));
  921. r.Newline();
  922. r.EndSymbolBullet();
  923. r.BeginSymbolBullet(wxT('*'), 100, 60);
  924. r.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
  925. r.Newline();
  926. r.EndSymbolBullet();
  927. r.BeginSymbolBullet(wxT('*'), 100, 60);
  928. r.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
  929. r.Newline();
  930. r.EndSymbolBullet();
  931. // Make a style suitable for showing a URL
  932. wxRichTextAttr urlStyle;
  933. urlStyle.SetTextColour(*wxBLUE);
  934. urlStyle.SetFontUnderlined(true);
  935. r.WriteText(wxT("wxRichTextCtrl can also display URLs, such as this one: "));
  936. r.BeginStyle(urlStyle);
  937. r.BeginURL(wxT("http://www.wxwidgets.org"));
  938. r.WriteText(wxT("The wxWidgets Web Site"));
  939. r.EndURL();
  940. r.EndStyle();
  941. r.WriteText(wxT(". Click on the URL to generate an event."));
  942. r.Newline();
  943. r.WriteText(wxT("Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!\n"));
  944. r.EndParagraphSpacing();
  945. #if 1
  946. {
  947. // Add a text box
  948. r.Newline();
  949. wxRichTextAttr attr;
  950. attr.GetTextBoxAttr().GetMargins().GetLeft().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
  951. attr.GetTextBoxAttr().GetMargins().GetTop().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
  952. attr.GetTextBoxAttr().GetMargins().GetRight().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
  953. attr.GetTextBoxAttr().GetMargins().GetBottom().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
  954. attr.GetTextBoxAttr().GetBorder().SetColour(*wxBLACK);
  955. attr.GetTextBoxAttr().GetBorder().SetWidth(1, wxTEXT_ATTR_UNITS_PIXELS);
  956. attr.GetTextBoxAttr().GetBorder().SetStyle(wxTEXT_BOX_ATTR_BORDER_SOLID);
  957. wxRichTextBox* textBox = r.WriteTextBox(attr);
  958. r.SetFocusObject(textBox);
  959. r.WriteText(wxT("This is a text box. Just testing! Once more unto the breach, dear friends, once more..."));
  960. r.SetFocusObject(NULL); // Set the focus back to the main buffer
  961. r.SetInsertionPointEnd();
  962. }
  963. #endif
  964. #if 1
  965. {
  966. // Add a table
  967. r.Newline();
  968. wxRichTextAttr attr;
  969. attr.GetTextBoxAttr().GetMargins().GetLeft().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
  970. attr.GetTextBoxAttr().GetMargins().GetTop().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
  971. attr.GetTextBoxAttr().GetMargins().GetRight().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
  972. attr.GetTextBoxAttr().GetMargins().GetBottom().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
  973. attr.GetTextBoxAttr().GetPadding() = attr.GetTextBoxAttr().GetMargins();
  974. attr.GetTextBoxAttr().GetBorder().SetColour(*wxBLACK);
  975. attr.GetTextBoxAttr().GetBorder().SetWidth(1, wxTEXT_ATTR_UNITS_PIXELS);
  976. attr.GetTextBoxAttr().GetBorder().SetStyle(wxTEXT_BOX_ATTR_BORDER_SOLID);
  977. wxRichTextAttr cellAttr = attr;
  978. cellAttr.GetTextBoxAttr().GetWidth().SetValue(200, wxTEXT_ATTR_UNITS_PIXELS);
  979. cellAttr.GetTextBoxAttr().GetHeight().SetValue(150, wxTEXT_ATTR_UNITS_PIXELS);
  980. wxRichTextTable* table = r.WriteTable(6, 4, attr, cellAttr);
  981. int i, j;
  982. for (j = 0; j < table->GetRowCount(); j++)
  983. {
  984. for (i = 0; i < table->GetColumnCount(); i++)
  985. {
  986. wxString msg = wxString::Format(wxT("This is cell %d, %d"), (j+1), (i+1));
  987. r.SetFocusObject(table->GetCell(j, i));
  988. r.WriteText(msg);
  989. }
  990. }
  991. // Demonstrate colspan and rowspan
  992. wxRichTextCell* cell = table->GetCell(1, 0);
  993. cell->SetColSpan(2);
  994. r.SetFocusObject(cell);
  995. cell->Clear();
  996. r.WriteText("This cell spans 2 columns");
  997. cell = table->GetCell(1, 3);
  998. cell->SetRowSpan(2);
  999. r.SetFocusObject(cell);
  1000. cell->Clear();
  1001. r.WriteText("This cell spans 2 rows");
  1002. cell = table->GetCell(2, 1);
  1003. cell->SetColSpan(2);
  1004. cell->SetRowSpan(3);
  1005. r.SetFocusObject(cell);
  1006. cell->Clear();
  1007. r.WriteText("This cell spans 2 columns and 3 rows");
  1008. r.SetFocusObject(NULL); // Set the focus back to the main buffer
  1009. r.SetInsertionPointEnd();
  1010. }
  1011. #endif
  1012. r.Newline(); r.Newline();
  1013. wxRichTextProperties properties;
  1014. r.WriteText(wxT("This is a rectangle field: "));
  1015. r.WriteField(wxT("rectangle"), properties);
  1016. r.WriteText(wxT(" and a begin section field: "));
  1017. r.WriteField(wxT("begin-section"), properties);
  1018. r.WriteText(wxT("This is text between the two tags."));
  1019. r.WriteField(wxT("end-section"), properties);
  1020. r.WriteText(wxT(" Now a bitmap. "));
  1021. r.WriteField(wxT("bitmap"), properties);
  1022. r.WriteText(wxT(" Before we go, here's a composite field: ***"));
  1023. wxRichTextField* field = r.WriteField(wxT("composite"), properties);
  1024. field->UpdateField(& r.GetBuffer()); // Creates the composite value (sort of a text box)
  1025. r.WriteText(wxT("*** End of composite field."));
  1026. r.Newline();
  1027. r.EndSuppressUndo();
  1028. // Add some locked content first - needs Undo to be enabled
  1029. {
  1030. r.BeginLock();
  1031. r.WriteText(wxString(wxT("This is a locked object.")));
  1032. r.EndLock();
  1033. r.WriteText(wxString(wxT(" This is unlocked text. ")));
  1034. r.BeginLock();
  1035. r.WriteText(wxString(wxT("More locked content.")));
  1036. r.EndLock();
  1037. r.Newline();
  1038. // Flush the Undo buffer
  1039. r.GetCommandProcessor()->ClearCommands();
  1040. }
  1041. r.Thaw();
  1042. }
  1043. // event handlers
  1044. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  1045. {
  1046. // true is to force the frame to close
  1047. Close(true);
  1048. }
  1049. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  1050. {
  1051. wxString msg;
  1052. msg.Printf( wxT("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
  1053. wxMessageBox(msg, wxT("About wxRichTextCtrl Sample"), wxOK | wxICON_INFORMATION, this);
  1054. }
  1055. // Forward command events to the current rich text control, if any
  1056. bool MyFrame::ProcessEvent(wxEvent& event)
  1057. {
  1058. if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent)))
  1059. {
  1060. // Problem: we can get infinite recursion because the events
  1061. // climb back up to this frame, and repeat.
  1062. // Assume that command events don't cause another command event
  1063. // to be called, so we can rely on inCommand not being overwritten
  1064. static int s_eventType = 0;
  1065. static wxWindowID s_id = 0;
  1066. if (s_id != event.GetId() && s_eventType != event.GetEventType())
  1067. {
  1068. s_eventType = event.GetEventType();
  1069. s_id = event.GetId();
  1070. wxWindow* focusWin = wxFindFocusDescendant(this);
  1071. if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event))
  1072. {
  1073. //s_command = NULL;
  1074. s_eventType = 0;
  1075. s_id = 0;
  1076. return true;
  1077. }
  1078. s_eventType = 0;
  1079. s_id = 0;
  1080. }
  1081. else
  1082. {
  1083. return false;
  1084. }
  1085. }
  1086. return wxFrame::ProcessEvent(event);
  1087. }
  1088. void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
  1089. {
  1090. wxString path;
  1091. wxString filename;
  1092. wxArrayInt fileTypes;
  1093. wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
  1094. if (!filter.empty())
  1095. filter += wxT("|");
  1096. filter += wxT("All files (*.*)|*.*");
  1097. wxFileDialog dialog(this,
  1098. _("Choose a filename"),
  1099. path,
  1100. filename,
  1101. filter,
  1102. wxFD_OPEN);
  1103. if (dialog.ShowModal() == wxID_OK)
  1104. {
  1105. wxString path = dialog.GetPath();
  1106. if (!path.empty())
  1107. {
  1108. int filterIndex = dialog.GetFilterIndex();
  1109. int fileType = (filterIndex < (int) fileTypes.GetCount())
  1110. ? fileTypes[filterIndex]
  1111. : wxRICHTEXT_TYPE_TEXT;
  1112. m_richTextCtrl->LoadFile(path, fileType);
  1113. }
  1114. }
  1115. }
  1116. void MyFrame::OnSave(wxCommandEvent& event)
  1117. {
  1118. if (m_richTextCtrl->GetFilename().empty())
  1119. {
  1120. OnSaveAs(event);
  1121. return;
  1122. }
  1123. m_richTextCtrl->SaveFile();
  1124. }
  1125. void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
  1126. {
  1127. wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
  1128. wxString path;
  1129. wxString filename;
  1130. wxFileDialog dialog(this,
  1131. _("Choose a filename"),
  1132. path,
  1133. filename,
  1134. filter,
  1135. wxFD_SAVE);
  1136. if (dialog.ShowModal() == wxID_OK)
  1137. {
  1138. wxString path = dialog.GetPath();
  1139. if (!path.empty())
  1140. {
  1141. wxBusyCursor busy;
  1142. wxStopWatch stopwatch;
  1143. m_richTextCtrl->SaveFile(path);
  1144. long t = stopwatch.Time();
  1145. wxLogDebug(wxT("Saving took %ldms"), t);
  1146. wxMessageBox(wxString::Format(wxT("Saving took %ldms"), t));
  1147. }
  1148. }
  1149. }
  1150. void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
  1151. {
  1152. m_richTextCtrl->ApplyBoldToSelection();
  1153. }
  1154. void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
  1155. {
  1156. m_richTextCtrl->ApplyItalicToSelection();
  1157. }
  1158. void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
  1159. {
  1160. m_richTextCtrl->ApplyUnderlineToSelection();
  1161. }
  1162. void MyFrame::OnStrikethrough(wxCommandEvent& WXUNUSED(event))
  1163. {
  1164. m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_STRIKETHROUGH);
  1165. }
  1166. void MyFrame::OnSuperscript(wxCommandEvent& WXUNUSED(event))
  1167. {
  1168. m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_SUPERSCRIPT);
  1169. }
  1170. void MyFrame::OnSubscript(wxCommandEvent& WXUNUSED(event))
  1171. {
  1172. m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_SUBSCRIPT);
  1173. }
  1174. void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
  1175. {
  1176. event.Check(m_richTextCtrl->IsSelectionBold());
  1177. }
  1178. void MyFrame::OnUpdateItalic(wxUpdateUIEvent& event)
  1179. {
  1180. event.Check(m_richTextCtrl->IsSelectionItalics());
  1181. }
  1182. void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
  1183. {
  1184. event.Check(m_richTextCtrl->IsSelectionUnderlined());
  1185. }
  1186. void MyFrame::OnUpdateStrikethrough(wxUpdateUIEvent& event)
  1187. {
  1188. event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_STRIKETHROUGH));
  1189. }
  1190. void MyFrame::OnUpdateSuperscript(wxUpdateUIEvent& event)
  1191. {
  1192. event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_SUPERSCRIPT));
  1193. }
  1194. void MyFrame::OnUpdateSubscript(wxUpdateUIEvent& event)
  1195. {
  1196. event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_SUBSCRIPT));
  1197. }
  1198. void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
  1199. {
  1200. m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
  1201. }
  1202. void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
  1203. {
  1204. m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
  1205. }
  1206. void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
  1207. {
  1208. m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
  1209. }
  1210. void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent& event)
  1211. {
  1212. event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT));
  1213. }
  1214. void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent& event)
  1215. {
  1216. event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
  1217. }
  1218. void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent& event)
  1219. {
  1220. event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
  1221. }
  1222. void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
  1223. {
  1224. wxRichTextRange range;
  1225. if (m_richTextCtrl->HasSelection())
  1226. range = m_richTextCtrl->GetSelectionRange();
  1227. else
  1228. range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
  1229. int pages = wxRICHTEXT_FORMAT_FONT;
  1230. wxRichTextFormattingDialog formatDlg(pages, this);
  1231. formatDlg.SetOptions(wxRichTextFormattingDialog::Option_AllowPixelFontSize);
  1232. formatDlg.GetStyle(m_richTextCtrl, range);
  1233. if (formatDlg.ShowModal() == wxID_OK)
  1234. {
  1235. formatDlg.ApplyStyle(m_richTextCtrl, range, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
  1236. }
  1237. }
  1238. void MyFrame::OnImage(wxCommandEvent& WXUNUSED(event))
  1239. {
  1240. wxRichTextRange range;
  1241. wxASSERT(m_richTextCtrl->HasSelection());
  1242. range = m_richTextCtrl->GetSelectionRange();
  1243. wxASSERT(range.ToInternal().GetLength() == 1);
  1244. wxRichTextImage* image = wxDynamicCast(m_richTextCtrl->GetFocusObject()->GetLeafObjectAtPosition(range.GetStart()), wxRichTextImage);
  1245. if (image)
  1246. {
  1247. wxRichTextObjectPropertiesDialog imageDlg(image, this);
  1248. if (imageDlg.ShowModal() == wxID_OK)
  1249. {
  1250. imageDlg.ApplyStyle(m_richTextCtrl);
  1251. }
  1252. }
  1253. }
  1254. void MyFrame::OnParagraph(wxCommandEvent& WXUNUSED(event))
  1255. {
  1256. wxRichTextRange range;
  1257. if (m_richTextCtrl->HasSelection())
  1258. range = m_richTextCtrl->GetSelectionRange();
  1259. else
  1260. range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
  1261. int pages = wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
  1262. wxRichTextFormattingDialog formatDlg(pages, this);
  1263. formatDlg.GetStyle(m_richTextCtrl, range);
  1264. if (formatDlg.ShowModal() == wxID_OK)
  1265. {
  1266. formatDlg.ApplyStyle(m_richTextCtrl, range);
  1267. }
  1268. }
  1269. void MyFrame::OnFormat(wxCommandEvent& WXUNUSED(event))
  1270. {
  1271. wxRichTextRange range;
  1272. if (m_richTextCtrl->HasSelection())
  1273. range = m_richTextCtrl->GetSelectionRange();
  1274. else
  1275. range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
  1276. int pages = wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
  1277. wxRichTextFormattingDialog formatDlg(pages, this);
  1278. formatDlg.GetStyle(m_richTextCtrl, range);
  1279. if (formatDlg.ShowModal() == wxID_OK)
  1280. {
  1281. formatDlg.ApplyStyle(m_richTextCtrl, range);
  1282. }
  1283. }
  1284. void MyFrame::OnUpdateFormat(wxUpdateUIEvent& event)
  1285. {
  1286. event.Enable(m_richTextCtrl->HasSelection());
  1287. }
  1288. void MyFrame::OnUpdateImage(wxUpdateUIEvent& event)
  1289. {
  1290. wxRichTextRange range;
  1291. wxRichTextObject *obj;
  1292. range = m_richTextCtrl->GetSelectionRange();
  1293. if (range.ToInternal().GetLength() == 1)
  1294. {
  1295. obj = m_richTextCtrl->GetFocusObject()->GetLeafObjectAtPosition(range.GetStart());
  1296. if (obj && obj->IsKindOf(CLASSINFO(wxRichTextImage)))
  1297. {
  1298. event.Enable(true);
  1299. return;
  1300. }
  1301. }
  1302. event.Enable(false);
  1303. }
  1304. void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
  1305. {
  1306. wxRichTextAttr attr;
  1307. attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
  1308. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1309. {
  1310. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1311. if (m_richTextCtrl->HasSelection())
  1312. range = m_richTextCtrl->GetSelectionRange();
  1313. attr.SetLeftIndent(attr.GetLeftIndent() + 100);
  1314. attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
  1315. m_richTextCtrl->SetStyle(range, attr);
  1316. }
  1317. }
  1318. void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
  1319. {
  1320. wxRichTextAttr attr;
  1321. attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
  1322. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1323. {
  1324. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1325. if (m_richTextCtrl->HasSelection())
  1326. range = m_richTextCtrl->GetSelectionRange();
  1327. if (attr.GetLeftIndent() > 0)
  1328. {
  1329. attr.SetLeftIndent(wxMax(0, attr.GetLeftIndent() - 100));
  1330. m_richTextCtrl->SetStyle(range, attr);
  1331. }
  1332. }
  1333. }
  1334. void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
  1335. {
  1336. wxRichTextAttr attr;
  1337. attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
  1338. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1339. {
  1340. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1341. if (m_richTextCtrl->HasSelection())
  1342. range = m_richTextCtrl->GetSelectionRange();
  1343. attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
  1344. attr.SetLineSpacing(15);
  1345. m_richTextCtrl->SetStyle(range, attr);
  1346. }
  1347. }
  1348. void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
  1349. {
  1350. wxRichTextAttr attr;
  1351. attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
  1352. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1353. {
  1354. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1355. if (m_richTextCtrl->HasSelection())
  1356. range = m_richTextCtrl->GetSelectionRange();
  1357. attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
  1358. attr.SetLineSpacing(20);
  1359. m_richTextCtrl->SetStyle(range, attr);
  1360. }
  1361. }
  1362. void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
  1363. {
  1364. wxRichTextAttr attr;
  1365. attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
  1366. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1367. {
  1368. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1369. if (m_richTextCtrl->HasSelection())
  1370. range = m_richTextCtrl->GetSelectionRange();
  1371. attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
  1372. attr.SetLineSpacing(0); // Can also use 10
  1373. m_richTextCtrl->SetStyle(range, attr);
  1374. }
  1375. }
  1376. void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
  1377. {
  1378. wxRichTextAttr attr;
  1379. attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
  1380. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1381. {
  1382. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1383. if (m_richTextCtrl->HasSelection())
  1384. range = m_richTextCtrl->GetSelectionRange();
  1385. attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20);
  1386. attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
  1387. m_richTextCtrl->SetStyle(range, attr);
  1388. }
  1389. }
  1390. void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
  1391. {
  1392. wxRichTextAttr attr;
  1393. attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
  1394. if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
  1395. {
  1396. wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
  1397. if (m_richTextCtrl->HasSelection())
  1398. range = m_richTextCtrl->GetSelectionRange();
  1399. if (attr.GetParagraphSpacingAfter() >= 20)
  1400. {
  1401. attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
  1402. attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
  1403. m_richTextCtrl->SetStyle(range, attr);
  1404. }
  1405. }
  1406. }
  1407. void MyFrame::OnReload(wxCommandEvent& WXUNUSED(event))
  1408. {
  1409. m_richTextCtrl->Clear();
  1410. WriteInitialText();
  1411. }
  1412. void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
  1413. {
  1414. wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
  1415. wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
  1416. dialog.SetSizer(boxSizer);
  1417. wxHtmlWindow* win = new wxHtmlWindow(& dialog, wxID_ANY, wxDefaultPosition, wxSize(500, 400), wxSUNKEN_BORDER);
  1418. boxSizer->Add(win, 1, wxALL, 5);
  1419. wxButton* cancelButton = new wxButton(& dialog, wxID_CANCEL, wxT("&Close"));
  1420. boxSizer->Add(cancelButton, 0, wxALL|wxCENTRE, 5);
  1421. wxString text;
  1422. wxStringOutputStream strStream(& text);
  1423. wxRichTextHTMLHandler htmlHandler;
  1424. htmlHandler.SetFlags(wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY);
  1425. wxArrayInt fontSizeMapping;
  1426. fontSizeMapping.Add(7);
  1427. fontSizeMapping.Add(9);
  1428. fontSizeMapping.Add(11);
  1429. fontSizeMapping.Add(12);
  1430. fontSizeMapping.Add(14);
  1431. fontSizeMapping.Add(22);
  1432. fontSizeMapping.Add(100);
  1433. htmlHandler.SetFontSizeMapping(fontSizeMapping);
  1434. if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream))
  1435. {
  1436. win->SetPage(text);
  1437. }
  1438. boxSizer->Fit(& dialog);
  1439. dialog.ShowModal();
  1440. // Now delete the temporary in-memory images
  1441. htmlHandler.DeleteTemporaryImages();
  1442. }
  1443. // Demonstrates how you can change the style sheets and have the changes
  1444. // reflected in the control content without wiping out character formatting.
  1445. void MyFrame::OnSwitchStyleSheets(wxCommandEvent& WXUNUSED(event))
  1446. {
  1447. static wxRichTextStyleSheet* gs_AlternateStyleSheet = NULL;
  1448. wxRichTextStyleListCtrl *styleList = (wxRichTextStyleListCtrl*) FindWindow(ID_RICHTEXT_STYLE_LIST);
  1449. wxRichTextStyleComboCtrl* styleCombo = (wxRichTextStyleComboCtrl*) FindWindow(ID_RICHTEXT_STYLE_COMBO);
  1450. wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
  1451. // One-time creation of an alternate style sheet
  1452. if (!gs_AlternateStyleSheet)
  1453. {
  1454. gs_AlternateStyleSheet = new wxRichTextStyleSheet(*sheet);
  1455. // Make some modifications
  1456. for (int i = 0; i < (int) gs_AlternateStyleSheet->GetParagraphStyleCount(); i++)
  1457. {
  1458. wxRichTextParagraphStyleDefinition* def = gs_AlternateStyleSheet->GetParagraphStyle(i);
  1459. if (def->GetStyle().HasTextColour())
  1460. def->GetStyle().SetTextColour(*wxBLUE);
  1461. if (def->GetStyle().HasAlignment())
  1462. {
  1463. if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
  1464. def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
  1465. else if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT)
  1466. def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
  1467. }
  1468. if (def->GetStyle().HasLeftIndent())
  1469. {
  1470. def->GetStyle().SetLeftIndent(def->GetStyle().GetLeftIndent() * 2);
  1471. }
  1472. }
  1473. }
  1474. // Switch sheets
  1475. wxRichTextStyleSheet* tmp = gs_AlternateStyleSheet;
  1476. gs_AlternateStyleSheet = sheet;
  1477. sheet = tmp;
  1478. m_richTextCtrl->SetStyleSheet(sheet);
  1479. m_richTextCtrl->ApplyStyleSheet(sheet); // Makes the control reflect the new style definitions
  1480. styleList->SetStyleSheet(sheet);
  1481. styleList->UpdateStyles();
  1482. styleCombo->SetStyleSheet(sheet);
  1483. styleCombo->UpdateStyles();
  1484. }
  1485. void MyFrame::OnManageStyles(wxCommandEvent& WXUNUSED(event))
  1486. {
  1487. wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
  1488. int flags = wxRICHTEXT_ORGANISER_CREATE_STYLES|wxRICHTEXT_ORGANISER_EDIT_STYLES;
  1489. wxRichTextStyleOrganiserDialog dlg(flags, sheet, NULL, this, wxID_ANY, _("Style Manager"));
  1490. dlg.ShowModal();
  1491. }
  1492. void MyFrame::OnInsertSymbol(wxCommandEvent& WXUNUSED(event))
  1493. {
  1494. wxRichTextAttr attr;
  1495. attr.SetFlags(wxTEXT_ATTR_FONT);
  1496. m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr);
  1497. wxString currentFontName;
  1498. if (attr.HasFont() && attr.GetFont().IsOk())
  1499. currentFontName = attr.GetFont().GetFaceName();
  1500. // Don't set the initial font in the dialog (so the user is choosing
  1501. // 'normal text', i.e. the current font) but do tell the dialog
  1502. // what 'normal text' is.
  1503. wxSymbolPickerDialog dlg(wxT("*"), wxEmptyString, currentFontName, this);
  1504. if (dlg.ShowModal() == wxID_OK)
  1505. {
  1506. if (dlg.HasSelection())
  1507. {
  1508. long insertionPoint = m_richTextCtrl->GetInsertionPoint();
  1509. m_richTextCtrl->WriteText(dlg.GetSymbol());
  1510. if (!dlg.UseNormalFont())
  1511. {
  1512. wxFont font(attr.GetFont());
  1513. font.SetFaceName(dlg.GetFontName());
  1514. attr.SetFont(font);
  1515. m_richTextCtrl->SetStyle(insertionPoint, insertionPoint+1, attr);
  1516. }
  1517. }
  1518. }
  1519. }
  1520. void MyFrame::OnNumberList(wxCommandEvent& WXUNUSED(event))
  1521. {
  1522. if (m_richTextCtrl->HasSelection())
  1523. {
  1524. wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
  1525. m_richTextCtrl->SetListStyle(range, wxT("Numbered List 1"), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
  1526. }
  1527. }
  1528. void MyFrame::OnBulletsAndNumbering(wxCommandEvent& WXUNUSED(event))
  1529. {
  1530. wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
  1531. int flags = wxRICHTEXT_ORGANISER_BROWSE_NUMBERING;
  1532. wxRichTextStyleOrganiserDialog dlg(flags, sheet, m_richTextCtrl, this, wxID_ANY, _("Bullets and Numbering"));
  1533. if (dlg.ShowModal() == wxID_OK)
  1534. {
  1535. if (dlg.GetSelectedStyleDefinition())
  1536. dlg.ApplyStyle();
  1537. }
  1538. }
  1539. void MyFrame::OnItemizeList(wxCommandEvent& WXUNUSED(event))
  1540. {
  1541. if (m_richTextCtrl->HasSelection())
  1542. {
  1543. wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
  1544. m_richTextCtrl->SetListStyle(range, wxT("Bullet List 1"));
  1545. }
  1546. }
  1547. void MyFrame::OnRenumberList(wxCommandEvent& WXUNUSED(event))
  1548. {
  1549. if (m_richTextCtrl->HasSelection())
  1550. {
  1551. wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
  1552. m_richTextCtrl->NumberList(range, NULL, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
  1553. }
  1554. }
  1555. void MyFrame::OnPromoteList(wxCommandEvent& WXUNUSED(event))
  1556. {
  1557. if (m_richTextCtrl->HasSelection())
  1558. {
  1559. wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
  1560. m_richTextCtrl->PromoteList(1, range, NULL);
  1561. }
  1562. }
  1563. void MyFrame::OnDemoteList(wxCommandEvent& WXUNUSED(event))
  1564. {
  1565. if (m_richTextCtrl->HasSelection())
  1566. {
  1567. wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
  1568. m_richTextCtrl->PromoteList(-1, range, NULL);
  1569. }
  1570. }
  1571. void MyFrame::OnClearList(wxCommandEvent& WXUNUSED(event))
  1572. {
  1573. if (m_richTextCtrl->HasSelection())
  1574. {
  1575. wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
  1576. m_richTextCtrl->ClearListStyle(range);
  1577. }
  1578. }
  1579. void MyFrame::OnTableAddColumn(wxCommandEvent& WXUNUSED(event))
  1580. {
  1581. wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
  1582. if (table)
  1583. {
  1584. wxRichTextAttr cellAttr = table->GetCell(0, 0)->GetAttributes();
  1585. table->AddColumns(table->GetColumnCount(), 1, cellAttr);
  1586. }
  1587. }
  1588. void MyFrame::OnTableAddRow(wxCommandEvent& WXUNUSED(event))
  1589. {
  1590. wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
  1591. if (table)
  1592. {
  1593. wxRichTextAttr cellAttr = table->GetCell(0, 0)->GetAttributes();
  1594. table->AddRows(table->GetRowCount(), 1, cellAttr);
  1595. }
  1596. }
  1597. void MyFrame::OnTableDeleteColumn(wxCommandEvent& WXUNUSED(event))
  1598. {
  1599. wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
  1600. if (table)
  1601. {
  1602. int col = table->GetFocusedCell().GetCol();
  1603. if (col == -1)
  1604. {
  1605. col = table->GetColumnCount() - 1;
  1606. }
  1607. table->DeleteColumns(col, 1);
  1608. }
  1609. }
  1610. void MyFrame::OnTableDeleteRow(wxCommandEvent& WXUNUSED(event))
  1611. {
  1612. wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
  1613. if (table)
  1614. {
  1615. int row = table->GetFocusedCell().GetRow();
  1616. if (row == -1)
  1617. {
  1618. row = table->GetRowCount() - 1;
  1619. }
  1620. table->DeleteRows(row, 1);
  1621. }
  1622. }
  1623. void MyFrame::OnTableFocusedUpdateUI(wxUpdateUIEvent& event)
  1624. {
  1625. event.Enable(m_richTextCtrl->FindTable() != NULL);
  1626. }
  1627. void MyFrame::OnTableHasCellsUpdateUI(wxUpdateUIEvent& event)
  1628. {
  1629. bool enable(false);
  1630. wxRichTextTable* table = wxDynamicCast(m_richTextCtrl->FindTable(), wxRichTextTable);
  1631. if (table)
  1632. {
  1633. if (event.GetId() == ID_TABLE_DELETE_COLUMN)
  1634. {
  1635. enable = table->GetColumnCount() > 1;
  1636. }
  1637. else
  1638. {
  1639. enable = table->GetRowCount() > 1;
  1640. }
  1641. }
  1642. event.Enable(enable);
  1643. }
  1644. void MyFrame::OnInsertURL(wxCommandEvent& WXUNUSED(event))
  1645. {
  1646. wxString url = wxGetTextFromUser(_("URL:"), _("Insert URL"));
  1647. if (!url.IsEmpty())
  1648. {
  1649. // Make a style suitable for showing a URL
  1650. wxRichTextAttr urlStyle;
  1651. urlStyle.SetTextColour(*wxBLUE);
  1652. urlStyle.SetFontUnderlined(true);
  1653. m_richTextCtrl->BeginStyle(urlStyle);
  1654. m_richTextCtrl->BeginURL(url);
  1655. m_richTextCtrl->WriteText(url);
  1656. m_richTextCtrl->EndURL();
  1657. m_richTextCtrl->EndStyle();
  1658. }
  1659. }
  1660. void MyFrame::OnInsertImage(wxCommandEvent& WXUNUSED(event))
  1661. {
  1662. wxFileDialog dialog(this, _("Choose an image"), "", "", "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png|JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg");
  1663. if (dialog.ShowModal() == wxID_OK)
  1664. {
  1665. wxString path = dialog.GetPath();
  1666. wxImage image;
  1667. if (image.LoadFile(path) && image.GetType() != wxBITMAP_TYPE_INVALID)
  1668. m_richTextCtrl->WriteImage(path, image.GetType());
  1669. }
  1670. }
  1671. void MyFrame::OnURL(wxTextUrlEvent& event)
  1672. {
  1673. wxMessageBox(event.GetString());
  1674. }
  1675. // Veto style sheet replace events when loading from XML, since we want
  1676. // to keep the original style sheet.
  1677. void MyFrame::OnStyleSheetReplacing(wxRichTextEvent& event)
  1678. {
  1679. event.Veto();
  1680. }
  1681. #if wxUSE_PRINTING_ARCHITECTURE
  1682. void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
  1683. {
  1684. wxGetApp().GetPrinting()->PrintBuffer(m_richTextCtrl->GetBuffer());
  1685. }
  1686. void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
  1687. {
  1688. wxGetApp().GetPrinting()->PreviewBuffer(m_richTextCtrl->GetBuffer());
  1689. }
  1690. #endif
  1691. void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
  1692. {
  1693. wxDialog dialog(this, wxID_ANY, wxT("Testing"), wxPoint(10, 10), wxSize(400, 300), wxDEFAULT_DIALOG_STYLE);
  1694. wxNotebook* nb = new wxNotebook(& dialog, wxID_ANY, wxPoint(5, 5), wxSize(300, 250));
  1695. wxPanel* panel = new wxPanel(nb, wxID_ANY, wxDefaultPosition, wxDefaultSize);
  1696. wxPanel* panel2 = new wxPanel(nb, wxID_ANY, wxDefaultPosition, wxDefaultSize);
  1697. new wxRichTextCtrl(panel, wxID_ANY, wxEmptyString, wxPoint(5, 5), wxSize(200, 150), wxVSCROLL|wxTE_READONLY);
  1698. nb->AddPage(panel, wxT("Page 1"));
  1699. new wxRichTextCtrl(panel2, wxID_ANY, wxEmptyString, wxPoint(5, 5), wxSize(200, 150), wxVSCROLL|wxTE_READONLY);
  1700. nb->AddPage(panel2, wxT("Page 2"));
  1701. new wxButton(& dialog, wxID_OK, wxT("OK"), wxPoint(5, 180));
  1702. dialog.ShowModal();
  1703. // wxGetApp().GetPrinting()->PageSetup();
  1704. }
  1705. void MyFrame::OnSetFontScale(wxCommandEvent& WXUNUSED(event))
  1706. {
  1707. wxString value = wxString::Format(wxT("%g"), m_richTextCtrl->GetFontScale());
  1708. wxString text = wxGetTextFromUser(wxT("Enter a text scale factor:"), wxT("Text Scale Factor"), value, wxGetTopLevelParent(this));
  1709. if (!text.IsEmpty() && value != text)
  1710. {
  1711. double scale = 1.0;
  1712. wxSscanf(text, wxT("%lf"), & scale);
  1713. m_richTextCtrl->SetFontScale(scale, true);
  1714. }
  1715. }
  1716. void MyFrame::OnSetDimensionScale(wxCommandEvent& WXUNUSED(event))
  1717. {
  1718. wxString value = wxString::Format(wxT("%g"), m_richTextCtrl->GetDimensionScale());
  1719. wxString text = wxGetTextFromUser(wxT("Enter a dimension scale factor:"), wxT("Dimension Scale Factor"), value, wxGetTopLevelParent(this));
  1720. if (!text.IsEmpty() && value != text)
  1721. {
  1722. double scale = 1.0;
  1723. wxSscanf(text, wxT("%lf"), & scale);
  1724. m_richTextCtrl->SetDimensionScale(scale, true);
  1725. }
  1726. }
  1727. void MyRichTextCtrl::PrepareContent(wxRichTextParagraphLayoutBox& container)
  1728. {
  1729. if (IsLocked())
  1730. {
  1731. // Lock all content that's about to be added to the control
  1732. wxRichTextObjectList::compatibility_iterator node = container.GetChildren().GetFirst();
  1733. while (node)
  1734. {
  1735. wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
  1736. if (para)
  1737. {
  1738. wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst();
  1739. while (childNode)
  1740. {
  1741. wxRichTextObject* obj = childNode->GetData();
  1742. obj->GetProperties().SetProperty(wxT("Lock"), m_lockId);
  1743. childNode = childNode->GetNext();
  1744. }
  1745. }
  1746. node = node->GetNext();
  1747. }
  1748. }
  1749. }
  1750. bool MyRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox& container, const wxRichTextRange& range) const
  1751. {
  1752. long i;
  1753. for (i = range.GetStart(); i < range.GetEnd(); i++)
  1754. {
  1755. wxRichTextObject* obj = container.GetLeafObjectAtPosition(i);
  1756. if (obj && obj->GetProperties().HasProperty(wxT("Lock")))
  1757. {
  1758. return false;
  1759. }
  1760. }
  1761. return true;
  1762. }
  1763. bool MyRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const
  1764. {
  1765. wxRichTextObject* child1 = container.GetLeafObjectAtPosition(pos);
  1766. wxRichTextObject* child2 = container.GetLeafObjectAtPosition(pos-1);
  1767. long lock1 = -1, lock2 = -1;
  1768. if (child1 && child1->GetProperties().HasProperty(wxT("Lock")))
  1769. lock1 = child1->GetProperties().GetPropertyLong(wxT("Lock"));
  1770. if (child2 && child2->GetProperties().HasProperty(wxT("Lock")))
  1771. lock2 = child2->GetProperties().GetPropertyLong(wxT("Lock"));
  1772. if (lock1 != -1 && lock1 == lock2)
  1773. return false;
  1774. // Don't allow insertion before a locked object if it's at the beginning of the buffer.
  1775. if (pos == 0 && lock1 != -1)
  1776. return false;
  1777. return true;
  1778. }
  1779. class wxRichTextEnhancedDrawingHandler: public wxRichTextDrawingHandler
  1780. {
  1781. public:
  1782. wxRichTextEnhancedDrawingHandler()
  1783. {
  1784. SetName(wxT("enhanceddrawing"));
  1785. m_lockBackgroundColour = wxColour(220, 220, 220);
  1786. }
  1787. /**
  1788. Returns @true if this object has virtual attributes that we can provide.
  1789. */
  1790. virtual bool HasVirtualAttributes(wxRichTextObject* obj) const;
  1791. /**
  1792. Provides virtual attributes that we can provide.
  1793. */
  1794. virtual bool GetVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const;
  1795. /**
  1796. Gets the count for mixed virtual attributes for individual positions within the object.
  1797. For example, individual characters within a text object may require special highlighting.
  1798. */
  1799. virtual int GetVirtualSubobjectAttributesCount(wxRichTextObject* WXUNUSED(obj)) const { return 0; }
  1800. /**
  1801. Gets the mixed virtual attributes for individual positions within the object.
  1802. For example, individual characters within a text object may require special highlighting.
  1803. Returns the number of virtual attributes found.
  1804. */
  1805. virtual int GetVirtualSubobjectAttributes(wxRichTextObject* WXUNUSED(obj), wxArrayInt& WXUNUSED(positions), wxRichTextAttrArray& WXUNUSED(attributes)) const { return 0; }
  1806. /**
  1807. Do we have virtual text for this object? Virtual text allows an application
  1808. to replace characters in an object for editing and display purposes, for example
  1809. for highlighting special characters.
  1810. */
  1811. virtual bool HasVirtualText(const wxRichTextPlainText* WXUNUSED(obj)) const { return false; }
  1812. /**
  1813. Gets the virtual text for this object.
  1814. */
  1815. virtual bool GetVirtualText(const wxRichTextPlainText* WXUNUSED(obj), wxString& WXUNUSED(text)) const { return false; }
  1816. wxColour m_lockBackgroundColour;
  1817. };
  1818. bool wxRichTextEnhancedDrawingHandler::HasVirtualAttributes(wxRichTextObject* obj) const
  1819. {
  1820. return obj->GetProperties().HasProperty(wxT("Lock"));
  1821. }
  1822. bool wxRichTextEnhancedDrawingHandler::GetVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const
  1823. {
  1824. if (obj->GetProperties().HasProperty(wxT("Lock")))
  1825. {
  1826. attr.SetBackgroundColour(m_lockBackgroundColour);
  1827. return true;
  1828. }
  1829. return false;
  1830. }
  1831. void MyRichTextCtrl::SetEnhancedDrawingHandler()
  1832. {
  1833. wxRichTextBuffer::AddDrawingHandler(new wxRichTextEnhancedDrawingHandler);
  1834. }
  1835. wxRichTextObject* MyRichTextCtrl::FindCurrentPosition() const
  1836. {
  1837. long position = -1;
  1838. if (HasSelection()) // First see if there's a selection
  1839. {
  1840. wxRichTextRange range = GetSelectionRange();
  1841. if (range.ToInternal().GetLength() == 1)
  1842. {
  1843. position = range.GetStart();
  1844. }
  1845. }
  1846. if (position == -1) // Failing that, near cursor
  1847. {
  1848. position = GetAdjustedCaretPosition(GetCaretPosition());
  1849. }
  1850. wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(position);
  1851. return obj;
  1852. }
  1853. wxRichTextTable* MyRichTextCtrl::FindTable() const
  1854. {
  1855. wxRichTextObject* obj = FindCurrentPosition();
  1856. // It could be a table or a cell (or neither)
  1857. wxRichTextTable* table = wxDynamicCast(obj, wxRichTextTable);
  1858. if (table)
  1859. {
  1860. return table;
  1861. }
  1862. while (obj)
  1863. {
  1864. obj = obj->GetParent();
  1865. wxRichTextTable* table = wxDynamicCast(obj, wxRichTextTable);
  1866. if (table)
  1867. {
  1868. return table;
  1869. }
  1870. }
  1871. return NULL;
  1872. }