richtext.cpp 79 KB

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