HtmlEditor.cs 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781
  1. using System;
  2. using System.ComponentModel;
  3. using System.Configuration;
  4. using System.Drawing;
  5. using System.Globalization;
  6. #if VS2010
  7. using System.Linq;
  8. #endif
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Runtime.InteropServices;
  12. using System.IO;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using WinHtmlEditor.Properties;
  16. using System.Diagnostics;
  17. using mshtmlTextRange = mshtml.IHTMLTxtRange;
  18. using mshtmlSelection = mshtml.IHTMLSelectionObject;
  19. using mshtmlDocument = mshtml.HTMLDocument;
  20. using mshtmlBody = mshtml.HTMLBody;
  21. using mshtmlControlRange = mshtml.IHTMLControlRange;
  22. using mshtmlElement = mshtml.IHTMLElement;
  23. using mshtmlEventObject = mshtml.IHTMLEventObj;
  24. using mshtmlElementCollection = mshtml.IHTMLElementCollection;
  25. using mshtmlAnchorElement = mshtml.IHTMLAnchorElement;
  26. using mshtmlDomNode = mshtml.IHTMLDOMNode;
  27. using mshtmlStyle = mshtml.IHTMLStyle;
  28. using mshtmlIHTMLDocument2 = mshtml.IHTMLDocument2;
  29. using mshtmlTable = mshtml.IHTMLTable;
  30. using mshtmlTableCaption = mshtml.IHTMLTableCaption;
  31. using mshtmlTableRow = mshtml.IHTMLTableRow;
  32. using Pavonis.COM;
  33. using Pavonis.COM.IOleCommandTarget;
  34. using System.IO.Compression;
  35. namespace WinHtmlEditor
  36. {
  37. [Docking(DockingBehavior.Ask), ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch), ToolboxBitmap(typeof(HtmlEditor), "Resources.HTML.bmp")]
  38. public sealed partial class HtmlEditor : UserControl
  39. {
  40. /// <summary>
  41. /// Public event that is raised if an internal processing exception is found
  42. /// </summary>
  43. [Category("Exception"), Description("An Internal Processing Exception was encountered")]
  44. public event HtmlExceptionEventHandler HtmlException;
  45. /// <summary>
  46. /// Public event that is raised if navigation event is captured
  47. /// </summary>
  48. [Category("Navigation"), Description("A Navigation Event was encountered")]
  49. public event HtmlNavigationEventHandler HtmlNavigation;
  50. // browser html constan expressions
  51. private const string BLANK_HTML_PAGE = "about:blank";
  52. // define the tags being used by the application
  53. private const string ANCHOR_TAG = "A";
  54. private const string TABLE_TAG = "TABLE";
  55. private const string SELECT_TYPE_TEXT = "text";
  56. private const string SELECT_TYPE_CONTROL = "control";
  57. private const string SELECT_TYPE_NONE = "none";
  58. // define commands for mshtml execution execution
  59. private const string HTML_COMMAND_FONTNAME = "FontName";
  60. private const string HTML_COMMAND_FONTSIZE = "FontSize";
  61. private const string HTML_COMMAND_PRINT = "Print";
  62. private const string HTML_COMMAND_COPY = "Copy";
  63. private const string HTML_COMMAND_PASTE = "Paste";
  64. private const string HTML_COMMAND_CUT = "Cut";
  65. private const string HTML_COMMAND_DELETE = "Delete";
  66. private const string HTML_COMMAND_UNDO = "Undo";
  67. private const string HTML_COMMAND_REDO = "Redo";
  68. private const string HTML_COMMAND_REMOVE_FORMAT = "RemoveFormat";
  69. private const string HTML_COMMAND_JUSTIFY_CENTER = "JustifyCenter";
  70. private const string HTML_COMMAND_JUSTIFY_FULL = "JustifyFull";
  71. private const string HTML_COMMAND_JUSTIFY_LEFT = "JustifyLeft";
  72. private const string HTML_COMMAND_JUSTIFY_RIGHT = "JustifyRight";
  73. private const string HTML_COMMAND_UNDERLINE = "Underline";
  74. private const string HTML_COMMAND_ITALIC = "Italic";
  75. private const string HTML_COMMAND_BOLD = "Bold";
  76. private const string HTML_COMMAND_BACK_COLOR = "BackColor";
  77. private const string HTML_COMMAND_FORE_COLOR = "ForeColor";
  78. private const string HTML_COMMAND_STRIKE_THROUGH = "StrikeThrough";
  79. private const string HTML_COMMAND_CREATE_LINK = "CreateLink";
  80. private const string HTML_COMMAND_UNLINK = "Unlink";
  81. private const string HTML_COMMAND_INSERT_HORIZONTAL_RULE = "InsertHorizontalRule";
  82. private const string HTML_COMMAND_INSERT_IMAGE = "InsertImage";
  83. private const string HTML_COMMAND_OUTDENT = "Outdent";
  84. private const string HTML_COMMAND_INDENT = "Indent";
  85. private const string HTML_COMMAND_INSERT_UNORDERED_LIST = "InsertUnorderedList";
  86. private const string HTML_COMMAND_INSERT_ORDERED_LIST = "InsertOrderedList";
  87. private const string HTML_COMMAND_SUPERSCRIPT = "Superscript";
  88. private const string HTML_COMMAND_SUBSCRIPT = "Subscript";
  89. private const string HTML_COMMAND_SELECT_ALL = "SelectAll";
  90. // internal command constants
  91. private const string INTERNAL_COMMAND_NEW = "New";
  92. private const string INTERNAL_COMMAND_OPEN = "Open";
  93. private const string INTERNAL_COMMAND_PRINT = "Print";
  94. private const string INTERNAL_COMMAND_SAVE = "Save";
  95. private const string INTERNAL_COMMAND_PREVIEW = "Preview";
  96. private const string INTERNAL_COMMAND_SHOWHTML = "ShowHTML";
  97. private const string INTERNAL_COMMAND_COPY = "Copy";
  98. private const string INTERNAL_COMMAND_PASTE = "Paste";
  99. private const string INTERNAL_COMMAND_CUT = "Cut";
  100. private const string INTERNAL_COMMAND_DELETE = "Delete";
  101. private const string INTERNAL_COMMAND_UNDO = "Undo";
  102. private const string INTERNAL_COMMAND_REDO = "Redo";
  103. private const string INTERNAL_COMMAND_FIND = "Find";
  104. private const string INTERNAL_COMMAND_Replace = "Replace";
  105. private const string INTERNAL_COMMAND_REMOVE_FORMAT = "RemoveFormat";
  106. private const string INTERNAL_COMMAND_JUSTIFYCENTER = "JustifyCenter";
  107. private const string INTERNAL_COMMAND_JUSTIFYFULL = "JustifyFull";
  108. private const string INTERNAL_COMMAND_JUSTIFYLEFT = "JustifyLeft";
  109. private const string INTERNAL_COMMAND_JUSTIFYRIGHT = "JustifyRight";
  110. private const string INTERNAL_COMMAND_UNDERLINE = "Underline";
  111. private const string INTERNAL_COMMAND_ITALIC = "Italic";
  112. private const string INTERNAL_COMMAND_BOLD = "Bold";
  113. private const string INTERNAL_COMMAND_BACKCOLOR = "BackColor";
  114. private const string INTERNAL_COMMAND_FORECOLOR = "ForeColor";
  115. private const string INTERNAL_COMMAND_STRIKETHROUGH = "StrikeThrough";
  116. private const string INTERNAL_COMMAND_CREATELINK = "CreateLink";
  117. private const string INTERNAL_COMMAND_UNLINK = "Unlink";
  118. private const string INTERNAL_COMMAND_INSERTTABLE = "InsertTable";
  119. private const string INTERNAL_COMMAND_TABLEPROPERTIES = "TableModify";
  120. private const string INTERNAL_COMMAND_TABLEINSERTROW = "TableInsertRow";
  121. private const string INTERNAL_COMMAND_TABLEDELETEROW = "TableDeleteRow";
  122. private const string INTERNAL_COMMAND_INSERTIMAGE = "InsertImage";
  123. private const string INTERNAL_COMMAND_INSERTHORIZONTALRULE = "InsertHorizontalRule";
  124. private const string INTERNAL_COMMAND_OUTDENT = "Outdent";
  125. private const string INTERNAL_COMMAND_INDENT = "Indent";
  126. private const string INTERNAL_COMMAND_INSERTUNORDEREDLIST = "InsertUnorderedList";
  127. private const string INTERNAL_COMMAND_INSERTORDEREDLIST = "InsertOrderedList";
  128. private const string INTERNAL_COMMAND_SUPERSCRIPT = "Superscript";
  129. private const string INTERNAL_COMMAND_SUBSCRIPT = "Subscript";
  130. private const string INTERNAL_COMMAND_SELECTALL = "SelectAll";
  131. private const string INTERNAL_COMMAND_WORDCOUNT = "WordCount";
  132. private const string INTERNAL_COMMAND_INSERTDATE = "InsertDate";
  133. private const string INTERNAL_COMMAND_INSERTTIME = "InsertTime";
  134. private const string INTERNAL_COMMAND_CLEARWORD = "ClearWord";
  135. private const string INTERNAL_COMMAND_AUTOLAYOUT = "AutoLayout";
  136. private const string INTERNAL_COMMAND_ABOUT = "About";
  137. // constants for displaying the HTML dialog
  138. private const string CONTENT_EDITABLE_INHERIT = "inherit";
  139. private const string HTML_TITLE_EDIT = "编辑Html源码";
  140. private const string DEFAULT_HTML_TEXT = "";
  141. private const string BODY_PARSE_PRE_EXPRESSION = @"(<body).*?(</body)";
  142. private const string BODY_PARSE_EXPRESSION = @"(?<bodyOpen>(<body).*?>)(?<innerBody>.*?)(?<bodyClose>(</body\s*>))";
  143. private const string BODY_DEFAULT_TAG = @"<Body></Body>";
  144. private const string BODY_TAG_PARSE_MATCH = @"${bodyOpen}${bodyClose}";
  145. private const string BODY_INNER_PARSE_MATCH = @"${innerBody}";
  146. // browser constants and commands
  147. private object EMPTY_PARAMETER;
  148. // internal body property values
  149. private bool _readOnly;
  150. private HtmlFontProperty _bodyFont;
  151. private Color _bodyBackColor;
  152. private Color _bodyForeColor;
  153. private int[] _customColors;
  154. private readonly NavigateActionOption _navigateWindow;
  155. private DisplayScrollBarOption _scrollBars;
  156. private bool _autoWordWrap;
  157. // default values used to reset values
  158. private readonly Color _defaultBodyBackColor;
  159. private readonly Color _defaultBodyForeColor;
  160. private readonly HtmlFontProperty _defaultFont;
  161. // internal property values
  162. private string _bodyText;
  163. private string _bodyHtml;
  164. private string _bodyUrl;
  165. private bool _toolbarVisible;
  166. private bool _statusbarVisible;
  167. private bool _wbVisible;
  168. // document and body elements
  169. private mshtmlDocument document;
  170. private mshtmlIHTMLDocument2 _doc;
  171. // find and replace internal text range
  172. private mshtmlTextRange _findRange;
  173. private volatile bool rebaseUrlsNeeded;
  174. private volatile bool loading;
  175. private volatile bool codeNavigate;
  176. private mshtmlBody body;
  177. private bool _updatingFontName;
  178. private bool _updatingFontSize;
  179. public class EnterKeyEventArgs : EventArgs
  180. {
  181. public EnterKeyEventArgs()
  182. {
  183. Cancel = false;
  184. }
  185. public bool Cancel { get; set; }
  186. }
  187. public event EventHandler<EnterKeyEventArgs> EnterKeyEvent;
  188. public HtmlEditor()
  189. {
  190. InitializeComponent();
  191. // define the default values
  192. // browser constants and commands
  193. EMPTY_PARAMETER = System.Reflection.Missing.Value;
  194. // default values used to reset values
  195. _defaultBodyBackColor = Color.White;
  196. _defaultBodyForeColor = Color.Black;
  197. _defaultFont = new HtmlFontProperty(Font);
  198. _navigateWindow = NavigateActionOption.Default;
  199. _scrollBars = DisplayScrollBarOption.Auto;
  200. _autoWordWrap = true;
  201. _toolbarVisible = true;
  202. _statusbarVisible = true;
  203. _wbVisible = true;
  204. _bodyText = DEFAULT_HTML_TEXT;
  205. _bodyHtml = DEFAULT_HTML_TEXT;
  206. _bodyBackColor = _defaultBodyBackColor;
  207. _bodyForeColor = _defaultBodyForeColor;
  208. _bodyFont = _defaultFont;
  209. // load the blank Html page to load the MsHtml object model
  210. BrowserCodeNavigate(BLANK_HTML_PAGE);
  211. // after load ensure document marked as editable
  212. ReadOnly = _readOnly;
  213. ScrollBars = _scrollBars;
  214. SetupComboFontSize();
  215. SynchFont(string.Empty);
  216. }
  217. /// <summary>
  218. /// Method used to navigate to the required page
  219. /// Call made sync using a loading variable
  220. /// </summary>
  221. private void BrowserCodeNavigate(string url)
  222. {
  223. // once navigated to the href page wait until successful
  224. // need to do this to ensure properties are all correctly set
  225. codeNavigate = true;
  226. loading = true;
  227. // perform the navigation
  228. wb.Navigate(url);
  229. // wait for the navigate to complete using the loading variable
  230. // DoEvents needs to be called to enable the DocumentComplete to execute
  231. while (loading)
  232. {
  233. Application.DoEvents();
  234. Thread.Sleep(0);
  235. }
  236. } //BrowserCodeNavigate
  237. [Description("The Inner HTML of the contents"), Category("Textual")]
  238. public string BodyInnerHTML
  239. {
  240. get
  241. {
  242. _bodyText = body.innerText;
  243. _bodyHtml = body.innerHTML;
  244. return _bodyHtml;
  245. }
  246. set
  247. {
  248. try
  249. {
  250. // clear the defined body url
  251. _bodyUrl = string.Empty;
  252. if (value.IsNull()) value = string.Empty;
  253. // set the body property
  254. body.innerHTML = value;
  255. // set the body text and html
  256. _bodyText = body.innerText;
  257. _bodyHtml = body.innerHTML;
  258. // if needed rebase urls
  259. RebaseAnchorUrl();
  260. }
  261. catch (Exception ex)
  262. {
  263. throw new HtmlEditorException("内部HTML的身体不能设置.", "SetInnerHtml", ex);
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// Method to remove references to about:blank from the anchors
  269. /// </summary>
  270. private void RebaseAnchorUrl()
  271. {
  272. if (rebaseUrlsNeeded)
  273. {
  274. // review the anchors and remove any references to about:blank
  275. mshtmlElementCollection anchors = body.getElementsByTagName(ANCHOR_TAG);
  276. foreach (mshtmlElement element in anchors)
  277. {
  278. try
  279. {
  280. var anchor = (mshtmlAnchorElement)element;
  281. string href = anchor.href;
  282. // see if href need updating
  283. if (!href.IsNull() && Regex.IsMatch(href, BLANK_HTML_PAGE, RegexOptions.IgnoreCase))
  284. {
  285. anchor.href = href.Replace(BLANK_HTML_PAGE, string.Empty);
  286. }
  287. }
  288. catch (Exception)
  289. {
  290. // ignore any errors
  291. }
  292. }
  293. }
  294. } //RebaseAnchorUrl
  295. /// <summary>
  296. /// Property defining the base text for the body
  297. /// The HTML value can be used at runtime
  298. /// </summary>
  299. [Category("Textual"), Description("设置初始正文文本")]
  300. [DefaultValue(DEFAULT_HTML_TEXT)]
  301. public string BodyInnerText
  302. {
  303. get
  304. {
  305. _bodyText = body.innerText;
  306. _bodyHtml = body.innerHTML;
  307. return _bodyText;
  308. }
  309. set
  310. {
  311. try
  312. {
  313. // clear the defined body url
  314. _bodyUrl = string.Empty;
  315. if (value.IsNull()) value = string.Empty;
  316. // set the body property
  317. body.innerText = value;
  318. // set the body text and html
  319. _bodyText = body.innerText;
  320. _bodyHtml = body.innerHTML;
  321. }
  322. catch (Exception ex)
  323. {
  324. throw new HtmlEditorException("内文的身体不能设置.", "SetInnerText", ex);
  325. }
  326. }
  327. } //BodyInnerText
  328. /// <summary>
  329. /// Property returning any Url that was used to load the current document
  330. /// </summary>
  331. [Category("Textual"), Description("Url used to load the Document")]
  332. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
  333. public string DocumentUrl
  334. {
  335. get
  336. {
  337. //return document.baseUrl;
  338. return _bodyUrl;
  339. }
  340. }
  341. [Category("Appearance"), DefaultValue("false"), Description("设置或获取一个值,决定按下回车键的时候是插入BR还是P")]
  342. public bool EnterToBR { get; set; }
  343. /// <summary>
  344. /// Property defining the editable status of the text
  345. /// </summary>
  346. [Category("RuntimeDisplay"), Description("标记的内容为只读")]
  347. [DefaultValue(false)]
  348. public bool ReadOnly
  349. {
  350. get
  351. {
  352. return _readOnly;
  353. }
  354. set
  355. {
  356. _readOnly = value;
  357. // define the document editable property
  358. body.contentEditable = (!_readOnly).ToString();
  359. // define the menu bar state
  360. tsTopToolBar.Enabled = (!_readOnly);
  361. // define whether the IE cntext menu should be shown
  362. wb.IsWebBrowserContextMenuEnabled = _readOnly;
  363. }
  364. } //ReadOnly
  365. /// <summary>
  366. /// Property defining the body tag of the html
  367. /// On set operation the body attributes are redefined
  368. /// </summary>
  369. [Category("Textual"), Description("Complete Document including Body Tag")]
  370. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
  371. public string BodyHtml
  372. {
  373. get
  374. {
  375. // set the read only property before return
  376. body.contentEditable = CONTENT_EDITABLE_INHERIT;
  377. string html = _doc.body.outerHTML.Trim();
  378. ReadOnly = _readOnly;
  379. return html;
  380. }
  381. set
  382. {
  383. // clear the defined body url
  384. _bodyUrl = string.Empty;
  385. // define some local working variables
  386. string bodyElement = string.Empty;
  387. string innerHtml = string.Empty;
  388. try
  389. {
  390. // ensure have body open and close tags
  391. if (Regex.IsMatch(value, BODY_PARSE_PRE_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline))
  392. {
  393. // define a regular expression for the Html Body parsing and obtain the match expression
  394. var expression = new Regex(BODY_PARSE_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.ExplicitCapture);
  395. var match = expression.Match(value);
  396. // see if a match was found
  397. if (match.Success)
  398. {
  399. // extract the body tag and the inner html
  400. bodyElement = match.Result(BODY_TAG_PARSE_MATCH);
  401. innerHtml = match.Result(BODY_INNER_PARSE_MATCH);
  402. // remove whitespaces from the body and inner html tags
  403. bodyElement = bodyElement.Trim();
  404. innerHtml = innerHtml.Trim();
  405. }
  406. }
  407. // ensure body was set
  408. if (bodyElement == string.Empty)
  409. {
  410. // assume the Html given is an inner html with no body
  411. bodyElement = BODY_DEFAULT_TAG;
  412. innerHtml = value.Trim();
  413. }
  414. // first navigate to a blank page to reset the html header
  415. BrowserCodeNavigate(BLANK_HTML_PAGE);
  416. // replace the body tag with the one passed in
  417. var oldBodyNode = (mshtmlDomNode)document.body;
  418. var newBodyNode = (mshtmlDomNode)document.createElement(bodyElement);
  419. oldBodyNode.replaceNode(newBodyNode);
  420. // define the new inner html and body objects
  421. body = (mshtmlBody)document.body;
  422. body.innerHTML = innerHtml;
  423. // now all successfully loaded need to review the body attributes
  424. _bodyText = body.innerText;
  425. _bodyHtml = body.innerHTML;
  426. // set and define the appropriate properties
  427. // this will set the appropriate read only property
  428. DefineBodyAttributes();
  429. // if needed rebase urls
  430. RebaseAnchorUrl();
  431. }
  432. catch (Exception ex)
  433. {
  434. throw new HtmlEditorException("Outer Html for the body cannot be set.", "SetBodyHtml", ex);
  435. }
  436. }
  437. } //BodyHtml
  438. /// <summary>
  439. /// Defines all the body attributes once a document has been loaded
  440. /// </summary>
  441. private void DefineBodyAttributes()
  442. {
  443. // define the body colors based on the new body html
  444. _bodyBackColor = GeneralUtil.IsNull(body.bgColor) ? _defaultBodyBackColor : ColorTranslator.FromHtml((string)body.bgColor);
  445. _bodyForeColor = GeneralUtil.IsNull(body.text) ? _defaultBodyForeColor : ColorTranslator.FromHtml((string)body.text);
  446. // define the font object based on current font of new document
  447. // deafult used unless a style on the body modifies the value
  448. mshtmlStyle bodyStyle = body.style;
  449. if (!bodyStyle.IsNull())
  450. {
  451. string fontName = _bodyFont.Name;
  452. HtmlFontSize fontSize = _bodyFont.Size;
  453. bool fontBold = _bodyFont.Bold;
  454. bool fontItalic = _bodyFont.Italic;
  455. // define the font name if defined in the style
  456. if (!bodyStyle.fontFamily.IsNull()) fontName = bodyStyle.fontFamily;
  457. if (!GeneralUtil.IsNull(bodyStyle.fontSize)) fontSize = HtmlFontConversion.StyleSizeToHtml(bodyStyle.fontSize.ToString());
  458. if (!bodyStyle.fontWeight.IsNull()) fontBold = HtmlFontConversion.IsStyleBold(bodyStyle.fontWeight);
  459. if (!bodyStyle.fontStyle.IsNull()) fontItalic = HtmlFontConversion.IsStyleItalic(bodyStyle.fontStyle);
  460. bool fontUnderline = bodyStyle.textDecorationUnderline;
  461. // define the new font object and set the property
  462. _bodyFont = new HtmlFontProperty(fontName, fontSize, fontBold, fontItalic, fontUnderline);
  463. BodyFont = _bodyFont;
  464. }
  465. // define the content based on the current value
  466. ReadOnly = _readOnly;
  467. ScrollBars = _scrollBars;
  468. AutoWordWrap = _autoWordWrap;
  469. } //DefineBodyAttributes
  470. /// <summary>
  471. /// Property defining whether scroll bars should be displayed
  472. /// </summary>
  473. [Category("RuntimeDisplay"), Description("Controls the Display of Scrolls Bars")]
  474. [DefaultValue(DisplayScrollBarOption.Auto)]
  475. public DisplayScrollBarOption ScrollBars
  476. {
  477. get
  478. {
  479. return _scrollBars;
  480. }
  481. set
  482. {
  483. _scrollBars = value;
  484. // define the document scroll bar visibility
  485. body.scroll = _scrollBars.ToString();
  486. }
  487. } //ScrollBars
  488. /// <summary>
  489. /// Property defining whether words will be auto wrapped
  490. /// </summary>
  491. [Category("RuntimeDisplay"), Description("Controls the auto wrapping of content")]
  492. [DefaultValue(true)]
  493. public bool AutoWordWrap
  494. {
  495. get
  496. {
  497. return _autoWordWrap;
  498. }
  499. set
  500. {
  501. _autoWordWrap = value;
  502. // define the document word wrap property
  503. body.noWrap = !_autoWordWrap;
  504. }
  505. } //AutoWordWrap
  506. /// <summary>
  507. /// Property for the base font to use for text editing
  508. /// Reset and Serialize values defined
  509. /// </summary>
  510. [Category("Textual"), Description("Defines the base Font object for the Body")]
  511. [RefreshProperties(RefreshProperties.All)]
  512. public HtmlFontProperty BodyFont
  513. {
  514. get
  515. {
  516. return _bodyFont;
  517. }
  518. set
  519. {
  520. // set the new value using the default if set to null
  521. _bodyFont = HtmlFontProperty.IsNotNull(value) ? value : _defaultFont;
  522. // set the font attributes based on any body styles
  523. mshtmlStyle bodyStyle = body.style;
  524. if (!bodyStyle.IsNull())
  525. {
  526. if (HtmlFontProperty.IsEqual(_bodyFont, _defaultFont))
  527. {
  528. // ensure no values are set in the Body style
  529. if (!bodyStyle.fontFamily.IsNull()) bodyStyle.fontFamily = string.Empty;
  530. if (!GeneralUtil.IsNull(bodyStyle.fontSize)) bodyStyle.fontSize = string.Empty;
  531. if (!bodyStyle.fontWeight.IsNull()) bodyStyle.fontWeight = string.Empty;
  532. }
  533. else
  534. {
  535. // set the body styles based on the defined value
  536. bodyStyle.fontFamily = _bodyFont.Name;
  537. bodyStyle.fontSize = HtmlFontConversion.HtmlFontSizeString(_bodyFont.Size);
  538. bodyStyle.fontWeight = HtmlFontConversion.HtmlFontBoldString(_bodyFont.Bold);
  539. }
  540. }
  541. }
  542. } //BodyFont
  543. public bool ShouldSerializeBodyFont()
  544. {
  545. return (HtmlFontProperty.IsNotEqual(_bodyFont, _defaultFont));
  546. } //ShouldSerializeBodyFont
  547. public void ResetBodyFont()
  548. {
  549. this.BodyFont = _defaultFont;
  550. } //ResetBodyFont
  551. [Category("Appearance"), Description("Controls the visible of ToolBar"), DefaultValue("true")]
  552. public bool ShowToolBar
  553. {
  554. get
  555. {
  556. return _toolbarVisible;
  557. }
  558. set
  559. {
  560. _toolbarVisible = value;
  561. this.tsTopToolBar.Visible = _toolbarVisible;
  562. }
  563. }
  564. [Category("Appearance"), Description("Controls the visible of StatusStrip"), DefaultValue("true")]
  565. public bool ShowStatusBar
  566. {
  567. get
  568. {
  569. return _statusbarVisible;
  570. }
  571. set
  572. {
  573. _statusbarVisible = value;
  574. ssHtml.Visible = _statusbarVisible;
  575. }
  576. }
  577. [Category("Appearance"), Description("Controls the visible of html editor"), DefaultValue("true")]
  578. public bool ShowWb
  579. {
  580. get
  581. {
  582. return _wbVisible;
  583. }
  584. set
  585. {
  586. _wbVisible = value;
  587. wb.Visible = _wbVisible;
  588. }
  589. }
  590. [Category("Appearance"), Description("Controls the enable of shortcut key"), DefaultValue("true")]
  591. public bool WebBrowserShortcutsEnabled
  592. {
  593. get
  594. {
  595. return wb.WebBrowserShortcutsEnabled;
  596. }
  597. set
  598. {
  599. wb.WebBrowserShortcutsEnabled = value;
  600. }
  601. }
  602. #region public method
  603. #region Find and Replace Operations
  604. /// <summary>
  605. /// Dialog to allow the user to perform a find and replace
  606. /// </summary>
  607. public void FindReplacePrompt(bool findOrReplace)
  608. {
  609. // define a default value for the text to find
  610. mshtmlTextRange range = GetTextRange();
  611. string initText = string.Empty;
  612. if (!range.IsNull())
  613. {
  614. string findText = range.text;
  615. if (!findText.IsNull()) initText = findText.Trim();
  616. }
  617. // prompt the user for the new href
  618. using (var dialog = new FindReplaceForm(initText,
  619. FindReplaceReset,
  620. FindFirst,
  621. FindNext,
  622. FindReplaceOne,
  623. FindReplaceAll, findOrReplace))
  624. {
  625. DefineDialogProperties(dialog);
  626. dialog.ShowDialog(ParentForm);
  627. }
  628. } //FindReplacePrompt
  629. /// <summary>
  630. /// Method to reset the find and replace options to initialize a new search
  631. /// </summary>
  632. public void FindReplaceReset()
  633. {
  634. // reset the range being worked with
  635. _findRange = body.createTextRange();
  636. document.selection.empty();
  637. } //FindReplaceReset
  638. /// <summary>
  639. /// Method to find the first occurrence of the given text string
  640. /// Uses false case for the search options
  641. /// </summary>
  642. public bool FindFirst(string findText)
  643. {
  644. return FindFirst(findText, false, false);
  645. } //FindFirst
  646. /// <summary>
  647. /// Method to find the first occurrence of the given text string
  648. /// </summary>
  649. public bool FindFirst(string findText, bool matchWhole, bool matchCase)
  650. {
  651. // reset the text search range prior to making any calls
  652. FindReplaceReset();
  653. // calls the Find Next once search has been initialized
  654. return FindNext(findText, matchWhole, matchCase);
  655. } //FindFirst
  656. /// <summary>
  657. /// Method to find the next occurrence of a given text string
  658. /// Assumes a previous search was made
  659. /// Uses false case for the search options
  660. /// </summary>
  661. public bool FindNext(string findText)
  662. {
  663. return FindNext(findText, false, false);
  664. } //FindNext
  665. /// <summary>
  666. /// Method to find the next occurrence of a given text string
  667. /// Assumes a previous search was made
  668. /// </summary>
  669. public bool FindNext(string findText, bool matchWhole, bool matchCase)
  670. {
  671. return (!FindText(findText, matchWhole, matchCase).IsNull());
  672. } //FindNext
  673. /// <summary>
  674. /// Replace the first occurrence of the given string with the other
  675. /// Uses false case for the search options
  676. /// </summary>
  677. public bool FindReplaceOne(string findText, string replaceText)
  678. {
  679. return FindReplaceOne(findText, replaceText);
  680. } //FindReplaceOne
  681. /// <summary>
  682. /// Method to replace the first occurrence of the given string with the other
  683. /// </summary>
  684. public bool FindReplaceOne(string findText, string replaceText, bool matchWhole, bool matchCase)
  685. {
  686. // find the given text within the find range
  687. mshtmlTextRange replaceRange = FindText(findText, matchWhole, matchCase);
  688. if (!replaceRange.IsNull())
  689. {
  690. // if text found perform a replace
  691. replaceRange.text = replaceText;
  692. replaceRange.select();
  693. // replace made to return success
  694. return true;
  695. }
  696. // no replace was made so return false
  697. return false;
  698. } //FindReplaceOne
  699. /// <summary>
  700. /// Method to replace all the occurrence of the given string with the other
  701. /// Uses false case for the search options
  702. /// </summary>
  703. public int FindReplaceAll(string findText, string replaceText)
  704. {
  705. return FindReplaceAll(findText, replaceText, false, false);
  706. } //FindReplaceAll
  707. /// <summary>
  708. /// Method to replace all the occurrences of the given string with the other
  709. /// </summary>
  710. public int FindReplaceAll(string findText, string replaceText, bool matchWhole, bool matchCase)
  711. {
  712. int found = 0;
  713. mshtmlTextRange replaceRange;
  714. do
  715. {
  716. // find the given text within the find range
  717. replaceRange = FindText(findText, matchWhole, matchCase);
  718. // if found perform a replace
  719. if (!replaceRange.IsNull())
  720. {
  721. replaceRange.text = replaceText;
  722. found++;
  723. }
  724. } while (!replaceRange.IsNull());
  725. // return count of items repalced
  726. return found;
  727. } //FindReplaceAll
  728. /// <summary>
  729. /// Method to perform the actual find of the given text
  730. /// </summary>
  731. private mshtmlTextRange FindText(string findText, bool matchWhole, bool matchCase)
  732. {
  733. // define the search options
  734. int searchOptions = 0;
  735. if (matchWhole) searchOptions = searchOptions + 2;
  736. if (matchCase) searchOptions = searchOptions + 4;
  737. // perform the search operation
  738. if (!_findRange.text.IsNull() && _findRange.findText(findText, _findRange.text.Length, searchOptions))
  739. {
  740. // select the found text within the document
  741. _findRange.select();
  742. // limit the new find range to be from the newly found text
  743. var foundRange = (mshtmlTextRange)document.selection.createRange();
  744. _findRange = body.createTextRange();
  745. _findRange.setEndPoint("StartToEnd", foundRange);
  746. // text found so return this selection
  747. return foundRange;
  748. }
  749. // reset the find ranges
  750. FindReplaceReset();
  751. // no text found so return null range
  752. return null;
  753. } //FindText
  754. #endregion
  755. /// <summary>
  756. /// 在当前工具栏上新加一个按钮
  757. /// </summary>
  758. /// <param name="buttom">按钮</param>
  759. /// <returns></returns>
  760. public bool AddToobarButtom(ToolStripButton buttom)
  761. {
  762. buttom.DisplayStyle = ToolStripItemDisplayStyle.Image;
  763. return (tsTopToolBar.Items.Add(buttom) > 0);
  764. }
  765. /// <summary>
  766. /// 重置为空白页
  767. /// </summary>
  768. public void Navigate()
  769. {
  770. NavigateToUrl("about:blank");
  771. }
  772. /// <summary>
  773. /// 将指定的统一资源定位符 (URL) 处的文档加载到 System.Windows.Forms.WebBrowser 控件中,替换上一个文档。
  774. /// </summary>
  775. /// <param name="url">指定的统一资源定位符 (URL)</param>
  776. public void NavigateToUrl(string url)
  777. {
  778. wb.Navigate(url);
  779. }
  780. /// <summary>
  781. /// Method to allow the user to load a document by navigation
  782. /// A new window can optionally be specified
  783. /// </summary>
  784. public void NavigateToUrl(string url, bool newWindow)
  785. {
  786. if (newWindow)
  787. {
  788. // open the Url in a new window
  789. wb.Navigate(url, true);
  790. }
  791. else
  792. {
  793. // if no new window required call the normal navigate method
  794. NavigateToUrl(url);
  795. }
  796. } //NavigateToUrl
  797. /// <summary>
  798. /// The currently selected text/controls will be replaced by the given HTML code.
  799. /// If nothing is selected, the HTML code is inserted at the cursor position
  800. /// </summary>
  801. /// <param name="sHtml"></param>
  802. public void PasteIntoSelection(string sHtml)
  803. {
  804. HTMLEditHelper.DOMDocument = _doc;
  805. HTMLEditHelper.PasteIntoSelection(sHtml);
  806. }
  807. /// <summary>
  808. /// 统计Html编辑器内容的字数
  809. /// </summary>
  810. /// <returns>字数</returns>
  811. public int WordCount()
  812. {
  813. Debug.Assert(wb.Document != null, "wb.Document != null");
  814. Debug.Assert(wb.Document.Body != null, "wb.Document.Body != null");
  815. #if VS2010
  816. return wb.Document.Body.InnerText.IsNull()
  817. ? 0
  818. : Regex.Split(wb.Document.Body.InnerText, @"\s").
  819. Sum(si => Regex.Matches(si, @"[\u0000-\u00ff]+").Count + si.Count(c => (int) c > 0x00FF));
  820. #else
  821. if (wb.Document.Body.InnerText.IsNull())
  822. return 0;
  823. var sec = Regex.Split(wb.Document.Body.InnerText, @"\s");
  824. int count = 0;
  825. foreach (var si in sec)
  826. {
  827. int ci = Regex.Matches(si, @"[\u0000-\u00ff]+").Count;
  828. foreach (var c in si)
  829. if (c > 0x00FF) ci++;
  830. count += ci;
  831. }
  832. return count;
  833. #endif
  834. }
  835. /// <summary>
  836. /// Method to allow the user to view the html contents
  837. /// </summary>
  838. public void ShowHTML()
  839. {
  840. using (var dialog = new EditHtmlForm())
  841. {
  842. dialog.HTML = BodyInnerHTML;
  843. dialog.ReadOnly = false;
  844. dialog.SetCaption(HTML_TITLE_EDIT);
  845. DefineDialogProperties(dialog);
  846. if (dialog.ShowDialog(ParentForm) == DialogResult.OK)
  847. {
  848. BodyInnerHTML = dialog.HTML;
  849. }
  850. }
  851. }
  852. /// <summary>
  853. /// Method to allow the user to preview the html page
  854. /// </summary>
  855. public void Preview()
  856. {
  857. bool isPreview = (body.contentEditable == "true");
  858. for (int i = 0; i < tsTopToolBar.Items.Count; i++)
  859. {
  860. tsTopToolBar.Items[i].Enabled = !isPreview;
  861. }
  862. if(isPreview)
  863. {
  864. body.contentEditable = "false";
  865. cmsHtml.Enabled = false;
  866. tsbPreview.Enabled = true;
  867. }
  868. else
  869. {
  870. cmsHtml.Enabled = true;
  871. body.contentEditable = "true";
  872. }
  873. }
  874. /// <summary>
  875. /// New document
  876. /// </summary>
  877. public void New()
  878. {
  879. BodyHtml = "";
  880. } //New
  881. /// <summary>
  882. /// Method to allow the user to persist the Html stream to a file
  883. /// </summary>
  884. public void Save()
  885. {
  886. using (var dialog = new SaveFileDialog
  887. {
  888. FileName = "",
  889. Filter = Resources.SaveFilter,
  890. Title = Resources.SaveTitle
  891. })
  892. {
  893. if (dialog.ShowDialog() == DialogResult.OK)
  894. {
  895. var writer = new StreamWriter(dialog.FileName,false,Encoding.UTF8);
  896. writer.Write(BodyHtml);
  897. writer.Close();
  898. }
  899. }
  900. } //New
  901. /// <summary>
  902. /// Method to allow the user to select a file and read the contents into the Html stream
  903. /// </summary>
  904. public void Open()
  905. {
  906. using (var dialog = new OpenFileDialog
  907. {
  908. FileName = "",
  909. Filter = Resources.OpenFilter,
  910. Title = Resources.OpenTitle
  911. })
  912. {
  913. if (dialog.ShowDialog() == DialogResult.OK)
  914. {
  915. var reader = new StreamReader(dialog.FileName, Encoding.Default);
  916. BodyHtml = reader.ReadToEnd();
  917. reader.Close();
  918. }
  919. }
  920. } //Open
  921. /// <summary>
  922. /// Method to print the html text using the document print command
  923. /// Print preview is not supported
  924. /// </summary>
  925. public void Print()
  926. {
  927. ExecuteCommandDocument(HTML_COMMAND_PRINT);
  928. } //Print
  929. /// <summary>
  930. /// Method to copy the currently selected text to the clipboard
  931. /// </summary>
  932. public void Copy()
  933. {
  934. ExecuteCommandDocument(HTML_COMMAND_COPY);
  935. } //Copy
  936. /// <summary>
  937. /// Method to cut the currently selected text to the clipboard
  938. /// </summary>
  939. public void Cut()
  940. {
  941. ExecuteCommandDocument(HTML_COMMAND_CUT);
  942. } //Cut
  943. /// <summary>
  944. /// Method to paste the currently selected text from the clipboard
  945. /// </summary>
  946. public void Paste()
  947. {
  948. ExecuteCommandDocument(HTML_COMMAND_PASTE);
  949. } //Paste
  950. /// <summary>
  951. /// Method to delete the currently selected text from the screen
  952. /// </summary>
  953. public void Delete()
  954. {
  955. ExecuteCommandDocument(HTML_COMMAND_DELETE);
  956. } //Delete
  957. /// <summary>
  958. /// Method to undo former commands
  959. /// </summary>
  960. public void Undo()
  961. {
  962. ExecuteCommandDocument(HTML_COMMAND_UNDO);
  963. FormatSelectionChange();
  964. } //Undo
  965. /// <summary>
  966. /// Method to redo former undo
  967. /// </summary>
  968. public void Redo()
  969. {
  970. ExecuteCommandDocument(HTML_COMMAND_REDO);
  971. FormatSelectionChange();
  972. } //Redo
  973. /// <summary>
  974. /// Method to find string
  975. /// </summary>
  976. public void Find()
  977. {
  978. Focus();
  979. // SendKeys.SendWait("^f");
  980. FindReplacePrompt(true);
  981. }
  982. /// <summary>
  983. /// Method to find string
  984. /// </summary>
  985. public void Replace()
  986. {
  987. Focus();
  988. // SendKeys.SendWait("^f");
  989. SendKeys.SendWait("^h");
  990. }
  991. /// <summary>
  992. /// Method to remove format
  993. /// </summary>
  994. public void RemoveFormat()
  995. {
  996. ExecuteCommandDocument(HTML_COMMAND_REMOVE_FORMAT);
  997. } //RemoveFormat
  998. /// <summary>
  999. /// Method to define the font justification as CENTER
  1000. /// </summary>
  1001. public void JustifyCenter()
  1002. {
  1003. ExecuteCommandRange(HTML_COMMAND_JUSTIFY_CENTER, null);
  1004. FormatSelectionChange();
  1005. } //JustifyCenter
  1006. /// <summary>
  1007. /// Method to define the font justification as FULL
  1008. /// </summary>
  1009. public void JustifyFull()
  1010. {
  1011. ExecuteCommandRange(HTML_COMMAND_JUSTIFY_FULL, null);
  1012. FormatSelectionChange();
  1013. } //JustifyFull
  1014. /// <summary>
  1015. /// Method to define the font justification as LEFT
  1016. /// </summary>
  1017. public void JustifyLeft()
  1018. {
  1019. ExecuteCommandRange(HTML_COMMAND_JUSTIFY_LEFT, null);
  1020. FormatSelectionChange();
  1021. } //JustifyLeft
  1022. /// <summary>
  1023. /// Method to define the font justification as RIGHT
  1024. /// </summary>
  1025. public void JustifyRight()
  1026. {
  1027. ExecuteCommandRange(HTML_COMMAND_JUSTIFY_RIGHT, null);
  1028. FormatSelectionChange();
  1029. } //JustifyRight
  1030. /// <summary>
  1031. /// Method using the document to toggle the selection with a underline tag
  1032. /// </summary>
  1033. public void Underline()
  1034. {
  1035. ExecuteCommandRange(HTML_COMMAND_UNDERLINE, null);
  1036. FormatSelectionChange();
  1037. } //Underline
  1038. /// <summary>
  1039. /// Method using the document to toggle the selection with a italic tag
  1040. /// </summary>
  1041. public void Italic()
  1042. {
  1043. ExecuteCommandRange(HTML_COMMAND_ITALIC, null);
  1044. FormatSelectionChange();
  1045. } //Italic
  1046. /// <summary>
  1047. /// Method using the document to toggle the selection with a bold tag
  1048. /// </summary>
  1049. public void Bold()
  1050. {
  1051. ExecuteCommandRange(HTML_COMMAND_BOLD, null);
  1052. FormatSelectionChange();
  1053. } //Bold
  1054. /// <summary>
  1055. /// Method using the exec command to define the back color properties for the selected tag
  1056. /// </summary>
  1057. public void FormatBackColor(Color color)
  1058. {
  1059. // Use the COLOR object to set the property BackColor
  1060. string colorHtml = color != Color.Empty ? ColorTranslator.ToHtml(color) : null;
  1061. ExecuteCommandRange(HTML_COMMAND_BACK_COLOR, colorHtml);
  1062. }
  1063. /// <summary>
  1064. /// Method to display the system color dialog
  1065. /// Use use to set the selected text Back Color
  1066. /// </summary>
  1067. public void FormatBackColorPrompt()
  1068. {
  1069. // display the Color dialog and use the selected color to modify text
  1070. using (var colorDialog = new ColorDialog())
  1071. {
  1072. colorDialog.AnyColor = true;
  1073. colorDialog.SolidColorOnly = true;
  1074. colorDialog.AllowFullOpen = true;
  1075. colorDialog.Color = GetBackColor();
  1076. colorDialog.CustomColors = _customColors;
  1077. if (colorDialog.ShowDialog(ParentForm) == DialogResult.OK)
  1078. {
  1079. _customColors = colorDialog.CustomColors;
  1080. FormatBackColor(colorDialog.Color);
  1081. }
  1082. }
  1083. } //FormatBackColorPrompt
  1084. /// <summary>
  1085. /// Method using the exec command to define the color properties for the selected tag
  1086. /// </summary>
  1087. public void FormatFontColor(Color color)
  1088. {
  1089. // Use the COLOR object to set the property ForeColor
  1090. string colorHtml = color != Color.Empty ? ColorTranslator.ToHtml(color) : null;
  1091. ExecuteCommandRange(HTML_COMMAND_FORE_COLOR, colorHtml);
  1092. } //FormatFontColor
  1093. /// <summary>
  1094. /// Method to display the system color dialog
  1095. /// Use use to set the selected text Color
  1096. /// </summary>
  1097. public void FormatFontColorPrompt()
  1098. {
  1099. // display the Color dialog and use the selected color to modify text
  1100. using (var colorDialog = new ColorDialog())
  1101. {
  1102. colorDialog.AnyColor = true;
  1103. colorDialog.SolidColorOnly = true;
  1104. colorDialog.AllowFullOpen = true;
  1105. colorDialog.Color = GetFontColor();
  1106. colorDialog.CustomColors = _customColors;
  1107. if (colorDialog.ShowDialog(ParentForm) == DialogResult.OK)
  1108. {
  1109. _customColors = colorDialog.CustomColors;
  1110. FormatFontColor(colorDialog.Color);
  1111. }
  1112. }
  1113. } //FormatFontColorPrompt
  1114. /// <summary>
  1115. /// Method using the document to toggle the selection with a StrikeThrough tag
  1116. /// </summary>
  1117. public void StrikeThrough()
  1118. {
  1119. ExecuteCommandRange(HTML_COMMAND_STRIKE_THROUGH, null);
  1120. FormatSelectionChange();
  1121. } //StrikeThrough
  1122. /// <summary>
  1123. /// Method using the document to create link
  1124. /// </summary>
  1125. public void CreateLink()
  1126. {
  1127. ExecuteCommandRange(HTML_COMMAND_CREATE_LINK, null);
  1128. } //CreateLink
  1129. /// <summary>
  1130. /// Method using the document to remove link
  1131. /// </summary>
  1132. public void UnLink()
  1133. {
  1134. ExecuteCommandRange(HTML_COMMAND_UNLINK, null);
  1135. FormatSelectionChange();
  1136. } //UnLink
  1137. /// <summary>
  1138. /// Method to insert a image and prompt a user for the link
  1139. /// Calls the public InsertImage method
  1140. /// </summary>
  1141. public void InsertImage()
  1142. {
  1143. ExecuteCommandDocumentPrompt(HTML_COMMAND_INSERT_IMAGE);
  1144. } //InsertImage
  1145. /// <summary>
  1146. ///
  1147. /// </summary>
  1148. public void AutoLayout()
  1149. {
  1150. if (!BodyInnerHTML.IsNullOrEmpty())
  1151. BodyInnerHTML = Regex.Replace(BodyInnerHTML, "(<P.*?>)[\\s]*(&nbsp;){0,}[\\s]*", "$1  ",
  1152. RegexOptions.IgnoreCase | RegexOptions.Multiline);
  1153. } //AutoLayout
  1154. /// <summary>
  1155. /// Create a new focus method that ensure the body gets the focus
  1156. /// Should be called when text processing command are called
  1157. /// </summary>
  1158. public new bool Focus()
  1159. {
  1160. // have the return value be the focus return from the user control
  1161. bool focus = base.Focus();
  1162. // try to set the focus to the web browser
  1163. try
  1164. {
  1165. wb.Focus();
  1166. if (!body.IsNull()) body.focus();
  1167. }
  1168. catch (Exception)
  1169. {
  1170. // ignore errors
  1171. }
  1172. return focus;
  1173. } //Focus
  1174. #region Table Processing Operations
  1175. /// <summary>
  1176. /// Method to create a table class
  1177. /// Insert method then works on this table
  1178. /// </summary>
  1179. public void TableInsert(HtmlTableProperty tableProperties)
  1180. {
  1181. // call the private insert table method with a null table entry
  1182. ProcessTable(null, tableProperties);
  1183. } //TableInsert
  1184. /// <summary>
  1185. /// Method to modify a tables properties
  1186. /// Ensure a table is currently selected or insertion point is within a table
  1187. /// </summary>
  1188. public bool TableModify(HtmlTableProperty tableProperties)
  1189. {
  1190. // define the Html Table element
  1191. mshtmlTable table = GetTableElement();
  1192. // if a table has been selected then process
  1193. if (!table.IsNull())
  1194. {
  1195. ProcessTable(table, tableProperties);
  1196. return true;
  1197. }
  1198. return false;
  1199. } //TableModify
  1200. /// <summary>
  1201. /// Method to present to the user the table properties dialog
  1202. /// Uses all the default properties for the table based on an insert operation
  1203. /// </summary>
  1204. public void TableInsertPrompt()
  1205. {
  1206. // if user has selected a table create a reference
  1207. var table = GetFirstControl() as mshtmlTable;
  1208. ProcessTablePrompt(table);
  1209. } //TableInsertPrompt
  1210. /// <summary>
  1211. /// Method to present to the user the table properties dialog
  1212. /// Ensure a table is currently selected or insertion point is within a table
  1213. /// </summary>
  1214. public bool TableModifyPrompt()
  1215. {
  1216. // define the Html Table element
  1217. mshtmlTable table = GetTableElement();
  1218. // if a table has been selected then process
  1219. if (!table.IsNull())
  1220. {
  1221. ProcessTablePrompt(table);
  1222. return true;
  1223. }
  1224. return false;
  1225. } //TableModifyPrompt
  1226. /// <summary>
  1227. /// Method to insert a new row into the table
  1228. /// Based on the current user row and insertion after
  1229. /// </summary>
  1230. public void TableInsertRow()
  1231. {
  1232. // see if a table selected or insertion point inside a table
  1233. mshtmlTable table;
  1234. mshtmlTableRow row;
  1235. GetTableElement(out table, out row);
  1236. // process according to table being defined
  1237. if (!table.IsNull() && !row.IsNull())
  1238. {
  1239. try
  1240. {
  1241. // find the existing row the user is on and perform the insertion
  1242. int index = row.rowIndex + 1;
  1243. var insertedRow = (mshtmlTableRow)table.insertRow(index);
  1244. // add the new columns to the end of each row
  1245. int numberCols = row.cells.length;
  1246. for (int idxCol = 0; idxCol < numberCols; idxCol++)
  1247. {
  1248. insertedRow.insertCell();
  1249. }
  1250. }
  1251. catch (Exception ex)
  1252. {
  1253. throw new HtmlEditorException("Unable to insert a new Row", "TableinsertRow", ex);
  1254. }
  1255. }
  1256. else
  1257. {
  1258. throw new HtmlEditorException("Row not currently selected within the table", "TableInsertRow");
  1259. }
  1260. } //TableInsertRow
  1261. /// <summary>
  1262. /// Method to delete the currently selected row
  1263. /// Operation based on the current user row location
  1264. /// </summary>
  1265. public void TableDeleteRow()
  1266. {
  1267. // see if a table selected or insertion point inside a table
  1268. mshtmlTable table;
  1269. mshtmlTableRow row;
  1270. GetTableElement(out table, out row);
  1271. // process according to table being defined
  1272. if (!table.IsNull() && !row.IsNull())
  1273. {
  1274. try
  1275. {
  1276. // find the existing row the user is on and perform the deletion
  1277. int index = row.rowIndex;
  1278. table.deleteRow(index);
  1279. }
  1280. catch (Exception ex)
  1281. {
  1282. throw new HtmlEditorException("Unable to delete the selected Row", "TableDeleteRow", ex);
  1283. }
  1284. }
  1285. else
  1286. {
  1287. throw new HtmlEditorException("Row not currently selected within the table", "TableDeleteRow");
  1288. }
  1289. } //TableDeleteRow
  1290. /// <summary>
  1291. /// Method to present to the user the table properties dialog
  1292. /// Uses all the default properties for the table based on an insert operation
  1293. /// </summary>
  1294. private void ProcessTablePrompt(mshtmlTable table)
  1295. {
  1296. using (var dialog = new TablePropertyForm())
  1297. {
  1298. // define the base set of table properties
  1299. HtmlTableProperty tableProperties = GetTableProperties(table);
  1300. // set the dialog properties
  1301. dialog.TableProperties = tableProperties;
  1302. DefineDialogProperties(dialog);
  1303. // based on the user interaction perform the neccessary action
  1304. if (dialog.ShowDialog(ParentForm) == DialogResult.OK)
  1305. {
  1306. tableProperties = dialog.TableProperties;
  1307. if (table.IsNull()) TableInsert(tableProperties);
  1308. else ProcessTable(table, tableProperties);
  1309. }
  1310. }
  1311. } // ProcessTablePrompt
  1312. /// <summary>
  1313. /// Method to insert a basic table
  1314. /// Will honour the existing table if passed in
  1315. /// </summary>
  1316. private void ProcessTable(mshtmlTable table, HtmlTableProperty tableProperties)
  1317. {
  1318. try
  1319. {
  1320. // obtain a reference to the body node and indicate table present
  1321. var bodyNode = (mshtmlDomNode)document.body;
  1322. bool tableCreated = false;
  1323. // ensure a table node has been defined to work with
  1324. if (table.IsNull())
  1325. {
  1326. // create the table and indicate it was created
  1327. table = (mshtmlTable)document.createElement(TABLE_TAG);
  1328. tableCreated = true;
  1329. }
  1330. // define the table border, width, cell padding and spacing
  1331. table.border = tableProperties.BorderSize;
  1332. if (tableProperties.TableWidth > 0) table.width = (tableProperties.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", tableProperties.TableWidth) : string.Format("{0}%", tableProperties.TableWidth);
  1333. else table.width = string.Empty;
  1334. table.align = tableProperties.TableAlignment != HorizontalAlignOption.Default ? tableProperties.TableAlignment.ToString().ToLower() : string.Empty;
  1335. table.cellPadding = tableProperties.CellPadding.ToString(CultureInfo.InvariantCulture);
  1336. table.cellSpacing = tableProperties.CellSpacing.ToString(CultureInfo.InvariantCulture);
  1337. // define the given table caption and alignment
  1338. string caption = tableProperties.CaptionText;
  1339. mshtmlTableCaption tableCaption = table.caption;
  1340. if (!caption.IsNullOrEmpty())
  1341. {
  1342. // ensure table caption correctly defined
  1343. if (tableCaption.IsNull()) tableCaption = table.createCaption();
  1344. ((mshtmlElement)tableCaption).innerText = caption;
  1345. if (tableProperties.CaptionAlignment != HorizontalAlignOption.Default) tableCaption.align = tableProperties.CaptionAlignment.ToString().ToLower();
  1346. if (tableProperties.CaptionLocation != VerticalAlignOption.Default) tableCaption.vAlign = tableProperties.CaptionLocation.ToString().ToLower();
  1347. }
  1348. else
  1349. {
  1350. // if no caption specified remove the existing one
  1351. if (!tableCaption.IsNull())
  1352. {
  1353. // prior to deleting the caption the contents must be cleared
  1354. ((mshtmlElement)tableCaption).innerText = null;
  1355. table.deleteCaption();
  1356. }
  1357. }
  1358. // determine the number of rows one has to insert
  1359. int numberRows, numberCols;
  1360. if (tableCreated)
  1361. {
  1362. numberRows = Math.Max((int)tableProperties.TableRows, 1);
  1363. }
  1364. else
  1365. {
  1366. numberRows = Math.Max((int)tableProperties.TableRows, 1) - table.rows.length;
  1367. }
  1368. // layout the table structure in terms of rows and columns
  1369. table.cols = tableProperties.TableColumns;
  1370. if (tableCreated)
  1371. {
  1372. // this section is an optimization based on creating a new table
  1373. // the section below works but not as efficiently
  1374. numberCols = Math.Max((int)tableProperties.TableColumns, 1);
  1375. // insert the appropriate number of rows
  1376. mshtmlTableRow tableRow;
  1377. for (int idxRow = 0; idxRow < numberRows; idxRow++)
  1378. {
  1379. tableRow = (mshtmlTableRow)table.insertRow();
  1380. // add the new columns to the end of each row
  1381. for (int idxCol = 0; idxCol < numberCols; idxCol++)
  1382. {
  1383. tableRow.insertCell();
  1384. }
  1385. }
  1386. }
  1387. else
  1388. {
  1389. // if the number of rows is increasing insert the decrepency
  1390. if (numberRows > 0)
  1391. {
  1392. // insert the appropriate number of rows
  1393. for (int idxRow = 0; idxRow < numberRows; idxRow++)
  1394. {
  1395. table.insertRow();
  1396. }
  1397. }
  1398. else
  1399. {
  1400. // remove the extra rows from the table
  1401. for (int idxRow = numberRows; idxRow < 0; idxRow++)
  1402. {
  1403. table.deleteRow(table.rows.length - 1);
  1404. }
  1405. }
  1406. // have the rows constructed
  1407. // now ensure the columns are correctly defined for each row
  1408. mshtmlElementCollection rows = table.rows;
  1409. foreach (mshtmlTableRow tableRow in rows)
  1410. {
  1411. numberCols = Math.Max((int)tableProperties.TableColumns, 1) - tableRow.cells.length;
  1412. if (numberCols > 0)
  1413. {
  1414. // add the new column to the end of each row
  1415. for (int idxCol = 0; idxCol < numberCols; idxCol++)
  1416. {
  1417. tableRow.insertCell();
  1418. }
  1419. }
  1420. else
  1421. {
  1422. // reduce the number of cells in the given row
  1423. // remove the extra rows from the table
  1424. for (int idxCol = numberCols; idxCol < 0; idxCol++)
  1425. {
  1426. tableRow.deleteCell(tableRow.cells.length - 1);
  1427. }
  1428. }
  1429. }
  1430. }
  1431. // if the table was created then it requires insertion into the DOM
  1432. // otherwise property changes are sufficient
  1433. if (tableCreated)
  1434. {
  1435. // table processing all complete so insert into the DOM
  1436. var tableNode = (mshtmlDomNode)table;
  1437. var tableElement = (mshtmlElement)table;
  1438. mshtmlTextRange textRange = GetTextRange();
  1439. // final insert dependant on what user has selected
  1440. if (!textRange.IsNull())
  1441. {
  1442. // text range selected so overwrite with a table
  1443. try
  1444. {
  1445. string selectedText = textRange.text;
  1446. if (!selectedText.IsNull())
  1447. {
  1448. // place selected text into first cell
  1449. var tableRow = (mshtmlTableRow)table.rows.item(0, null);
  1450. ((mshtmlElement)tableRow.cells.item(0, null)).innerText = selectedText;
  1451. }
  1452. textRange.pasteHTML(tableElement.outerHTML);
  1453. }
  1454. catch (Exception ex)
  1455. {
  1456. throw new HtmlEditorException("Invalid Text selection for the Insertion of a Table.", "ProcessTable", ex);
  1457. }
  1458. }
  1459. else
  1460. {
  1461. mshtmlControlRange controlRange = GetAllControls();
  1462. if (!controlRange.IsNull())
  1463. {
  1464. // overwrite any controls the user has selected
  1465. try
  1466. {
  1467. // clear the selection and insert the table
  1468. // only valid if multiple selection is enabled
  1469. for (int idx = 1; idx < controlRange.length; idx++)
  1470. {
  1471. controlRange.remove(idx);
  1472. }
  1473. controlRange.item(0).outerHTML = tableElement.outerHTML;
  1474. // this should work with initial count set to zero
  1475. // controlRange.add((mshtmlControlElement)table);
  1476. }
  1477. catch (Exception ex)
  1478. {
  1479. throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "ProcessTable", ex);
  1480. }
  1481. }
  1482. else
  1483. {
  1484. // insert the table at the end of the HTML
  1485. bodyNode.appendChild(tableNode);
  1486. }
  1487. }
  1488. }
  1489. else
  1490. {
  1491. // table has been correctly defined as being the first selected item
  1492. // need to remove other selected items
  1493. mshtmlControlRange controlRange = GetAllControls();
  1494. if (!controlRange.IsNull())
  1495. {
  1496. // clear the controls selected other than than the first table
  1497. // only valid if multiple selection is enabled
  1498. for (int idx = 1; idx < controlRange.length; idx++)
  1499. {
  1500. controlRange.remove(idx);
  1501. }
  1502. }
  1503. }
  1504. }
  1505. catch (Exception ex)
  1506. {
  1507. // throw an exception indicating table structure change error
  1508. throw new HtmlEditorException("Unable to modify Html Table properties.", "ProcessTable", ex);
  1509. }
  1510. } //ProcessTable
  1511. /// <summary>
  1512. /// Method to determine if the current selection is a table
  1513. /// If found will return the table element
  1514. /// </summary>
  1515. private void GetTableElement(out mshtmlTable table, out mshtmlTableRow row)
  1516. {
  1517. row = null;
  1518. mshtmlTextRange range = GetTextRange();
  1519. try
  1520. {
  1521. // first see if the table element is selected
  1522. table = GetFirstControl() as mshtmlTable;
  1523. // if table not selected then parse up the selection tree
  1524. if (table.IsNull() && !range.IsNull())
  1525. {
  1526. var element = range.parentElement();
  1527. // parse up the tree until the table element is found
  1528. while (!element.IsNull() && table.IsNull())
  1529. {
  1530. element = element.parentElement;
  1531. // extract the Table properties
  1532. var htmlTable = element as mshtmlTable;
  1533. if (!htmlTable.IsNull())
  1534. {
  1535. table = htmlTable;
  1536. }
  1537. // extract the Row properties
  1538. var htmlTableRow = element as mshtmlTableRow;
  1539. if (!htmlTableRow.IsNull())
  1540. {
  1541. row = htmlTableRow;
  1542. }
  1543. }
  1544. }
  1545. }
  1546. catch (Exception)
  1547. {
  1548. // have unknown error so set return to null
  1549. table = null;
  1550. row = null;
  1551. }
  1552. } //GetTableElement
  1553. /// <summary>
  1554. /// Method to return the currently selected Html Table Element
  1555. /// </summary>
  1556. private mshtmlTable GetTableElement()
  1557. {
  1558. // define the table and row elements and obtain there values
  1559. mshtmlTable table;
  1560. mshtmlTableRow row;
  1561. GetTableElement(out table, out row);
  1562. // return the defined table element
  1563. return table;
  1564. }
  1565. /// <summary>
  1566. /// Given an Html Table Element determines the table properties
  1567. /// Returns the properties as an HtmlTableProperty class
  1568. /// </summary>
  1569. private HtmlTableProperty GetTableProperties(mshtmlTable table)
  1570. {
  1571. // define a set of base table properties
  1572. var tableProperties = new HtmlTableProperty(true);
  1573. // if user has selected a table extract those properties
  1574. if (!table.IsNull())
  1575. {
  1576. try
  1577. {
  1578. // have a table so extract the properties
  1579. mshtmlTableCaption caption = table.caption;
  1580. // if have a caption persist the values
  1581. if (!caption.IsNull())
  1582. {
  1583. tableProperties.CaptionText = ((mshtmlElement)table.caption).innerText;
  1584. if (!caption.align.IsNull()) tableProperties.CaptionAlignment = (HorizontalAlignOption)typeof(HorizontalAlignOption).TryParseEnum(caption.align, HorizontalAlignOption.Default);
  1585. if (!caption.vAlign.IsNull()) tableProperties.CaptionLocation = (VerticalAlignOption)typeof(VerticalAlignOption).TryParseEnum(caption.vAlign, VerticalAlignOption.Default);
  1586. }
  1587. // look at the table properties
  1588. if (!GeneralUtil.IsNull(table.border)) tableProperties.BorderSize =GeneralUtil.TryParseByte(table.border.ToString(),tableProperties.BorderSize);
  1589. if (!table.align.IsNull()) tableProperties.TableAlignment = (HorizontalAlignOption)typeof(HorizontalAlignOption).TryParseEnum(table.align, HorizontalAlignOption.Default);
  1590. // define the table rows and columns
  1591. int rows = Math.Min(table.rows.length, Byte.MaxValue);
  1592. int cols = Math.Min(table.cols, Byte.MaxValue);
  1593. if (cols == 0 && rows > 0)
  1594. {
  1595. // cols value not set to get the maxiumn number of cells in the rows
  1596. foreach (mshtmlTableRow tableRow in table.rows)
  1597. {
  1598. cols = Math.Max(cols, tableRow.cells.length);
  1599. }
  1600. }
  1601. tableProperties.TableRows = (byte)Math.Min(rows, byte.MaxValue);
  1602. tableProperties.TableColumns = (byte)Math.Min(cols, byte.MaxValue);
  1603. // define the remaining table properties
  1604. if (!GeneralUtil.IsNull(table.cellPadding)) tableProperties.CellPadding = GeneralUtil.TryParseByte(table.cellPadding.ToString(),tableProperties.CellPadding);
  1605. if (!GeneralUtil.IsNull(table.cellSpacing)) tableProperties.CellSpacing = GeneralUtil.TryParseByte(table.cellSpacing.ToString(),tableProperties.CellSpacing);
  1606. if (!GeneralUtil.IsNull(table.width))
  1607. {
  1608. string tableWidth = table.width.ToString();
  1609. if (tableWidth.TrimEnd(null).EndsWith("%"))
  1610. {
  1611. tableProperties.TableWidth = tableWidth.Remove(tableWidth.LastIndexOf("%", StringComparison.Ordinal), 1).TryParseUshort(tableProperties.TableWidth);
  1612. tableProperties.TableWidthMeasurement = MeasurementOption.Percent;
  1613. }
  1614. else
  1615. {
  1616. tableProperties.TableWidth = tableWidth.TryParseUshort(tableProperties.TableWidth);
  1617. tableProperties.TableWidthMeasurement = MeasurementOption.Pixel;
  1618. }
  1619. }
  1620. else
  1621. {
  1622. tableProperties.TableWidth = 0;
  1623. tableProperties.TableWidthMeasurement = MeasurementOption.Pixel;
  1624. }
  1625. }
  1626. catch (Exception ex)
  1627. {
  1628. // throw an exception indicating table structure change be determined
  1629. throw new HtmlEditorException("Unable to determine Html Table properties.", "GetTableProperties", ex);
  1630. }
  1631. }
  1632. // return the table properties
  1633. return tableProperties;
  1634. } //GetTableProperties
  1635. /// <summary>
  1636. /// Method to return a table defintion based on the user selection
  1637. /// If table selected (or insertion point within table) returns these values
  1638. /// </summary>
  1639. public void GetTableDefinition(out HtmlTableProperty table, out bool tableFound)
  1640. {
  1641. // see if a table selected or insertion point inside a table
  1642. mshtmlTable htmlTable = GetTableElement();
  1643. // process according to table being defined
  1644. if (htmlTable.IsNull())
  1645. {
  1646. table = new HtmlTableProperty(true);
  1647. tableFound = false;
  1648. }
  1649. else
  1650. {
  1651. table = GetTableProperties(htmlTable);
  1652. tableFound = true;
  1653. }
  1654. } //GetTableDefinition
  1655. /// <summary>
  1656. /// Method to determine if the insertion point or selection is a table
  1657. /// </summary>
  1658. private bool IsParentTable()
  1659. {
  1660. // see if a table selected or insertion point inside a table
  1661. mshtmlTable htmlTable = GetTableElement();
  1662. // process according to table being defined
  1663. if (htmlTable.IsNull())
  1664. {
  1665. return false;
  1666. }
  1667. return true;
  1668. } //IsParentTable
  1669. /// <summary>
  1670. /// Method to insert a horizontal line in the body
  1671. /// If have a control range rather than text range one could overwrite the controls with the line
  1672. /// </summary>
  1673. public void InsertLine()
  1674. {
  1675. mshtmlTextRange range = GetTextRange();
  1676. if (range != null)
  1677. {
  1678. ExecuteCommandRange(range, HTML_COMMAND_INSERT_HORIZONTAL_RULE, null);
  1679. }
  1680. else
  1681. {
  1682. throw new HtmlEditorException("Invalid Selection for Line insertion.", "InsertLine");
  1683. }
  1684. } //InsertLine
  1685. /// <summary>
  1686. /// Method to Tab the current line to the left
  1687. /// </summary>
  1688. public void Outdent()
  1689. {
  1690. ExecuteCommandRange(HTML_COMMAND_OUTDENT, null);
  1691. } //Outdent
  1692. /// <summary>
  1693. /// Method to Tab the current line to the right
  1694. /// </summary>
  1695. public void Indent()
  1696. {
  1697. ExecuteCommandRange(HTML_COMMAND_INDENT, null);
  1698. } //Indent
  1699. /// <summary>
  1700. /// Method to insert an unordered list
  1701. /// </summary>
  1702. public void InsertUnorderedList()
  1703. {
  1704. ExecuteCommandRange(HTML_COMMAND_INSERT_UNORDERED_LIST, null);
  1705. FormatSelectionChange();
  1706. } //InsertUnorderedList
  1707. /// <summary>
  1708. /// Method to insert an ordered list
  1709. /// </summary>
  1710. public void InsertOrderedList()
  1711. {
  1712. ExecuteCommandRange(HTML_COMMAND_INSERT_ORDERED_LIST, null);
  1713. FormatSelectionChange();
  1714. } //InsertOrderedList
  1715. /// <summary>
  1716. /// Method using the document to toggle the selection with a Superscript tag
  1717. /// </summary>
  1718. public void Superscript()
  1719. {
  1720. ExecuteCommandRange(HTML_COMMAND_SUPERSCRIPT, null);
  1721. FormatSelectionChange();
  1722. } //Superscript
  1723. /// <summary>
  1724. /// Method using the document to toggle the selection with a Subscript tag
  1725. /// </summary>
  1726. public void Subscript()
  1727. {
  1728. ExecuteCommandRange(HTML_COMMAND_SUBSCRIPT, null);
  1729. FormatSelectionChange();
  1730. } //Subscript
  1731. /// <summary>
  1732. /// Method to select the entire document contents
  1733. /// </summary>
  1734. public void SelectAll()
  1735. {
  1736. ExecuteCommandDocument(HTML_COMMAND_SELECT_ALL);
  1737. } //SelectAll
  1738. #endregion
  1739. #endregion
  1740. private void tsbNew_Click(object sender, EventArgs e)
  1741. {
  1742. var button = (ToolStripButton)sender;
  1743. var command = (string)button.Tag;
  1744. ProcessCommand(command);
  1745. }
  1746. private void tsbOpen_Click(object sender, EventArgs e)
  1747. {
  1748. var button = (ToolStripButton)sender;
  1749. var command = (string)button.Tag;
  1750. ProcessCommand(command);
  1751. }
  1752. private void tsbSave_Click(object sender, EventArgs e)
  1753. {
  1754. var button = (ToolStripButton)sender;
  1755. var command = (string)button.Tag;
  1756. ProcessCommand(command);
  1757. }
  1758. private void tsbPreview_Click(object sender, EventArgs e)
  1759. {
  1760. var button = (ToolStripButton)sender;
  1761. var command = (string)button.Tag;
  1762. ProcessCommand(command);
  1763. }
  1764. private void tsbShowHTML_Click(object sender, EventArgs e)
  1765. {
  1766. var button = (ToolStripButton)sender;
  1767. var command = (string)button.Tag;
  1768. ProcessCommand(command);
  1769. }
  1770. private void tsbCopy_Click(object sender, EventArgs e)
  1771. {
  1772. var button = (ToolStripButton)sender;
  1773. var command = (string)button.Tag;
  1774. ProcessCommand(command);
  1775. }
  1776. private void tsbCut_Click(object sender, EventArgs e)
  1777. {
  1778. var button = (ToolStripButton)sender;
  1779. var command = (string)button.Tag;
  1780. ProcessCommand(command);
  1781. }
  1782. private void tsbPaste_Click(object sender, EventArgs e)
  1783. {
  1784. var button = (ToolStripButton)sender;
  1785. var command = (string)button.Tag;
  1786. ProcessCommand(command);
  1787. }
  1788. private void tsbDelete_Click(object sender, EventArgs e)
  1789. {
  1790. var button = (ToolStripButton)sender;
  1791. var command = (string)button.Tag;
  1792. ProcessCommand(command);
  1793. }
  1794. private void tsbFind_Click(object sender, EventArgs e)
  1795. {
  1796. var button = (ToolStripButton)sender;
  1797. var command = (string)button.Tag;
  1798. ProcessCommand(command);
  1799. }
  1800. /// <summary>
  1801. /// Method to ensure dialog resembles the user form characteristics
  1802. /// </summary>
  1803. private void DefineDialogProperties(Form dialog)
  1804. {
  1805. // set ambient control properties
  1806. if (!ParentForm.IsNull())
  1807. {
  1808. dialog.Font = ParentForm.Font;
  1809. dialog.ForeColor = ParentForm.ForeColor;
  1810. dialog.BackColor = ParentForm.BackColor;
  1811. dialog.Cursor = ParentForm.Cursor;
  1812. dialog.RightToLeft = ParentForm.RightToLeft;
  1813. }
  1814. // define location and control style as system
  1815. dialog.StartPosition = FormStartPosition.CenterParent;
  1816. } //DefineDialogProperties
  1817. /// <summary>
  1818. /// Method to determine if the tag name is of the correct type
  1819. /// A string comparision is made whilst ignoring case
  1820. /// </summary>
  1821. private bool IsStringEqual(string tagText, string tagType)
  1822. {
  1823. return (String.Compare(tagText, tagType, StringComparison.OrdinalIgnoreCase) == 0);
  1824. } //IsStringEqual
  1825. private void tsbRemoveFormat_Click(object sender, EventArgs e)
  1826. {
  1827. var button = (ToolStripButton)sender;
  1828. var command = (string)button.Tag;
  1829. ProcessCommand(command);
  1830. }
  1831. private void tsbJustifyCenter_Click(object sender, EventArgs e)
  1832. {
  1833. var button = (ToolStripButton)sender;
  1834. var command = (string)button.Tag;
  1835. ProcessCommand(command);
  1836. }
  1837. private void tsbJustifyLeft_Click(object sender, EventArgs e)
  1838. {
  1839. var button = (ToolStripButton)sender;
  1840. var command = (string)button.Tag;
  1841. ProcessCommand(command);
  1842. }
  1843. private void tsbJustifyRight_Click(object sender, EventArgs e)
  1844. {
  1845. var button = (ToolStripButton)sender;
  1846. var command = (string)button.Tag;
  1847. ProcessCommand(command);
  1848. }
  1849. private void tsbUnderline_Click(object sender, EventArgs e)
  1850. {
  1851. var button = (ToolStripButton)sender;
  1852. var command = (string)button.Tag;
  1853. ProcessCommand(command);
  1854. }
  1855. private void tsbItalic_Click(object sender, EventArgs e)
  1856. {
  1857. var button = (ToolStripButton)sender;
  1858. var command = (string)button.Tag;
  1859. ProcessCommand(command);
  1860. }
  1861. private void tsbBold_Click(object sender, EventArgs e)
  1862. {
  1863. var button = (ToolStripButton)sender;
  1864. var command = (string)button.Tag;
  1865. ProcessCommand(command);
  1866. }
  1867. private void tsbStrikeThrough_Click(object sender, EventArgs e)
  1868. {
  1869. var button = (ToolStripButton)sender;
  1870. var command = (string)button.Tag;
  1871. ProcessCommand(command);
  1872. }
  1873. private void tsbCreateLink_Click(object sender, EventArgs e)
  1874. {
  1875. var button = (ToolStripButton)sender;
  1876. var command = (string)button.Tag;
  1877. ProcessCommand(command);
  1878. }
  1879. private void tsbUnlink_Click(object sender, EventArgs e)
  1880. {
  1881. var button = (ToolStripButton)sender;
  1882. var command = (string)button.Tag;
  1883. ProcessCommand(command);
  1884. }
  1885. private void tsbInsertImage_Click(object sender, EventArgs e)
  1886. {
  1887. var button = (ToolStripButton)sender;
  1888. var command = (string)button.Tag;
  1889. ProcessCommand(command);
  1890. }
  1891. private void tsbInsertHorizontalRule_Click(object sender, EventArgs e)
  1892. {
  1893. var button = (ToolStripButton)sender;
  1894. var command = (string)button.Tag;
  1895. ProcessCommand(command);
  1896. }
  1897. private void tsbOutdent_Click(object sender, EventArgs e)
  1898. {
  1899. var button = (ToolStripButton)sender;
  1900. var command = (string)button.Tag;
  1901. ProcessCommand(command);
  1902. }
  1903. private void tsbIndent_Click(object sender, EventArgs e)
  1904. {
  1905. var button = (ToolStripButton)sender;
  1906. var command = (string)button.Tag;
  1907. ProcessCommand(command);
  1908. }
  1909. private void tsbInsertUnorderedList_Click(object sender, EventArgs e)
  1910. {
  1911. var button = (ToolStripButton)sender;
  1912. var command = (string)button.Tag;
  1913. ProcessCommand(command);
  1914. }
  1915. private void tsbInsertOrderedList_Click(object sender, EventArgs e)
  1916. {
  1917. var button = (ToolStripButton)sender;
  1918. var command = (string)button.Tag;
  1919. ProcessCommand(command);
  1920. }
  1921. private void tsbAbout_Click(object sender, EventArgs e)
  1922. {
  1923. var button = (ToolStripButton)sender;
  1924. var command = (string)button.Tag;
  1925. ProcessCommand(command);
  1926. }
  1927. private void tsbJustifyFull_Click(object sender, EventArgs e)
  1928. {
  1929. var button = (ToolStripButton)sender;
  1930. var command = (string)button.Tag;
  1931. ProcessCommand(command);
  1932. }
  1933. private void tsmiInsertTable_Click(object sender, EventArgs e)
  1934. {
  1935. var menuItem = (ToolStripMenuItem)sender;
  1936. var command = (string)menuItem.Tag;
  1937. ProcessCommand(command);
  1938. }
  1939. private void tsbWordCount_Click(object sender, EventArgs e)
  1940. {
  1941. var button = (ToolStripButton)sender;
  1942. var command = (string)button.Tag;
  1943. ProcessCommand(command);
  1944. }
  1945. private void tsbSuperscript_Click(object sender, EventArgs e)
  1946. {
  1947. var button = (ToolStripButton)sender;
  1948. var command = (string)button.Tag;
  1949. ProcessCommand(command);
  1950. }
  1951. private void tsbSubscript_Click(object sender, EventArgs e)
  1952. {
  1953. var button = (ToolStripButton)sender;
  1954. var command = (string)button.Tag;
  1955. ProcessCommand(command);
  1956. }
  1957. private void tsbPrint_Click(object sender, EventArgs e)
  1958. {
  1959. var button = (ToolStripButton)sender;
  1960. var command = (string)button.Tag;
  1961. ProcessCommand(command);
  1962. }
  1963. private void tsbDate_Click(object sender, EventArgs e)
  1964. {
  1965. var button = (ToolStripButton)sender;
  1966. var command = (string)button.Tag;
  1967. ProcessCommand(command);
  1968. }
  1969. private void tsbTime_Click(object sender, EventArgs e)
  1970. {
  1971. var button = (ToolStripButton)sender;
  1972. var command = (string)button.Tag;
  1973. ProcessCommand(command);
  1974. }
  1975. private void tsbSpellCheck_Click(object sender, EventArgs e)
  1976. {
  1977. Debug.Assert(wb.Document != null, "wb.Document != null");
  1978. Debug.Assert(wb.Document.Body != null, "wb.Document.Body != null");
  1979. spellCheck.Text = wb.Document.Body.InnerHtml;
  1980. spellCheck.SpellCheck();
  1981. }
  1982. private void tsbAutoLayout_Click(object sender, EventArgs e)
  1983. {
  1984. var button = (ToolStripButton)sender;
  1985. var command = (string)button.Tag;
  1986. ProcessCommand(command);
  1987. }
  1988. private void tsbUndo_Click(object sender, EventArgs e)
  1989. {
  1990. var button = (ToolStripButton)sender;
  1991. var command = (string)button.Tag;
  1992. ProcessCommand(command);
  1993. }
  1994. private void tsbRedo_Click(object sender, EventArgs e)
  1995. {
  1996. var button = (ToolStripButton)sender;
  1997. var command = (string)button.Tag;
  1998. ProcessCommand(command);
  1999. }
  2000. private void tsbClearWord_Click(object sender, EventArgs e)
  2001. {
  2002. var button = (ToolStripButton)sender;
  2003. var command = (string)button.Tag;
  2004. ProcessCommand(command);
  2005. }
  2006. /// <summary>
  2007. /// Method to perform the process of showing the context menus
  2008. /// </summary>
  2009. private void DocumentContextMenu(object sender, HtmlElementEventArgs e)
  2010. {
  2011. // if in readonly mode display the standard context menu
  2012. // otherwise display the editing context menu
  2013. if (!_readOnly)
  2014. {
  2015. // should disable inappropriate commands
  2016. tsmiTable.Visible = IsParentTable();
  2017. // display the text processing context menu
  2018. cmsHtml.Show(wb, e.MousePosition);
  2019. // cancel the standard menu and event bubbling
  2020. e.BubbleEvent = false;
  2021. e.ReturnValue = false;
  2022. }
  2023. } //DocumentContextMenu
  2024. /// <summary>
  2025. /// Method to perform the process of selection change
  2026. /// </summary>
  2027. private void DocumentSelectionChange(object sender, EventArgs e)
  2028. {
  2029. // if not in readonly mode process the selection change
  2030. if (!_readOnly)
  2031. {
  2032. FormatSelectionChange();
  2033. }
  2034. } //DocumentSelectionChange
  2035. /// <summary>
  2036. /// Ensures the toolbar is correctly displaying state
  2037. /// </summary>
  2038. private void FormatSelectionChange()
  2039. {
  2040. // don't process until bowser is create.
  2041. if (wb.IsHandleCreated == false)
  2042. return;
  2043. SetupKeyListener();
  2044. tsbBold.Checked = IsBold();
  2045. tsbItalic.Checked = IsItalic();
  2046. tsbUnderline.Checked = IsUnderline();
  2047. tsbStrikeThrough.Checked = IsStrikeThrough();
  2048. tsbInsertOrderedList.Checked = IsOrderedList();
  2049. tsbInsertUnorderedList.Checked = IsUnorderedList();
  2050. tsbJustifyLeft.Checked = IsJustifyLeft();
  2051. tsbJustifyCenter.Checked = IsJustifyCenter();
  2052. tsbJustifyRight.Checked = IsJustifyRight();
  2053. tsbJustifyFull.Checked = IsJustifyFull();
  2054. tsbSubscript.Checked = IsSubscript();
  2055. tsbSuperscript.Checked = IsSuperscript();
  2056. tsbUndo.Enabled = IsUndo();
  2057. tsbRedo.Enabled = IsRedo();
  2058. tsbUnlink.Enabled = IsUnlink();
  2059. UpdateFontComboBox();
  2060. UpdateFontSizeComboBox();
  2061. }
  2062. /// <summary>
  2063. /// Method to perform the process of key being pressed
  2064. /// </summary>
  2065. private void DocumentKeyPress(object sender, EventArgs e)
  2066. {
  2067. // define the event object being processes and review the key being pressed
  2068. mshtmlEventObject eventObject = document.parentWindow.@event;
  2069. if (eventObject.ctrlKey)
  2070. {
  2071. switch (eventObject.keyCode)
  2072. {
  2073. case 65:
  2074. SelectAll();
  2075. eventObject.returnValue = true;
  2076. break;
  2077. case 70:
  2078. FindReplacePrompt(true);
  2079. eventObject.returnValue = false;
  2080. break;
  2081. case 72:
  2082. FindReplacePrompt(false);
  2083. eventObject.returnValue = false;
  2084. break;
  2085. }
  2086. }
  2087. if ((eventObject.keyCode != 13)) return;
  2088. if (EnterToBR)
  2089. {
  2090. var range = GetTextRange();
  2091. Debug.Assert(range != null, "range != null");
  2092. range.pasteHTML(!eventObject.shiftKey ? "<br>" : "<P>&nbsp;</P>");
  2093. range.collapse();
  2094. range.@select();
  2095. }
  2096. if (!eventObject.shiftKey)
  2097. {
  2098. eventObject.returnValue = SetupKeyListener();
  2099. }
  2100. eventObject.returnValue = true;
  2101. } //DocumentKeyPress
  2102. private void Selector_TableSizeSelected(object sender, TableSizeEventArgs e)
  2103. {
  2104. // if user has selected a table create a reference
  2105. var table = GetFirstControl() as mshtmlTable;
  2106. // define the base set of table properties
  2107. HtmlTableProperty tableProperties = GetTableProperties(table);
  2108. tableProperties.TableRows =(byte)e.SelectedSize.Height;
  2109. tableProperties.TableColumns = (byte)e.SelectedSize.Width;
  2110. if (table.IsNull()) TableInsert(tableProperties);
  2111. else ProcessTable(table, tableProperties);
  2112. }
  2113. private void DropDown_Opening(object sender, CancelEventArgs e)
  2114. {
  2115. var c = tsddbInsertTable.DropDown as ToolStripTableSizeSelector;
  2116. if (c != null)
  2117. {
  2118. c.Selector.SelectedSize = new Size(0, 0);
  2119. c.Selector.VisibleRange = new Size(10, 10);
  2120. }
  2121. }
  2122. /// <summary>
  2123. /// 初始化工具栏和邮件菜单
  2124. /// </summary>
  2125. private void InitUi()
  2126. {
  2127. //初始化插入表格部分
  2128. var dropDown = new ToolStripTableSizeSelector();
  2129. dropDown.Opening += DropDown_Opening;
  2130. dropDown.Selector.TableSizeSelected += Selector_TableSizeSelected;
  2131. tsddbInsertTable.DropDown = dropDown;
  2132. var tsmiInsertTable = new ToolStripMenuItem
  2133. {
  2134. Image = Resources.InsertTable,
  2135. Name = "tsmiInsertTable",
  2136. Size = new Size(152, 22),
  2137. Text = Resources.strInsertTable,
  2138. Tag = "InsertTable"
  2139. };
  2140. tsmiInsertTable.Click += tsmiInsertTable_Click;
  2141. tsddbInsertTable.DropDownItems.Add(tsmiInsertTable);
  2142. string removeButton = ConfigurationManager.AppSettings["removeButtons"];
  2143. if (!removeButton.IsNullOrEmpty())
  2144. {
  2145. var removeButtons = removeButton.Split(',');
  2146. foreach (var button in removeButtons)
  2147. {
  2148. foreach (var item in tsTopToolBar.Items)
  2149. {
  2150. if (item is ToolStripButton)
  2151. {
  2152. var tsb = item as ToolStripButton;
  2153. if (String.CompareOrdinal(tsb.Tag.ToString(), button) == 0)
  2154. {
  2155. tsb.Visible = false;
  2156. break;
  2157. }
  2158. }
  2159. else if (item is ToolStripDropDownButton)
  2160. {
  2161. var tsddb = item as ToolStripDropDownButton;
  2162. if (String.CompareOrdinal(tsddb.Tag.ToString(), button) == 0)
  2163. {
  2164. tsddb.Visible = false;
  2165. break;
  2166. }
  2167. }
  2168. else if (item is ToolStripFontComboBox)
  2169. {
  2170. var tsfcb = item as ToolStripFontComboBox;
  2171. if (String.CompareOrdinal(tsfcb.Tag.ToString(), button) == 0)
  2172. {
  2173. tsfcb.Visible = false;
  2174. break;
  2175. }
  2176. }
  2177. else if (item is ToolStripComboBox)
  2178. {
  2179. var tscb = item as ToolStripComboBox;
  2180. if (String.CompareOrdinal(tscb.Tag.ToString(), button) == 0)
  2181. {
  2182. tscb.Visible = false;
  2183. break;
  2184. }
  2185. }
  2186. }
  2187. }
  2188. }
  2189. string removeMenu = ConfigurationManager.AppSettings["removeMenus"];
  2190. if (!string.IsNullOrEmpty(removeMenu))
  2191. {
  2192. var removeMenus = removeMenu.Split(',');
  2193. foreach (var menu in removeMenus)
  2194. {
  2195. foreach (var item in cmsHtml.Items)
  2196. {
  2197. if (item is ToolStripMenuItem)
  2198. {
  2199. var tsmi = item as ToolStripMenuItem;
  2200. if (String.CompareOrdinal(tsmi.Tag.ToString(), menu) == 0)
  2201. {
  2202. tsmi.Visible = false;
  2203. break;
  2204. }
  2205. }
  2206. }
  2207. }
  2208. }
  2209. }
  2210. private void HtmlEditor_Load(object sender, EventArgs e)
  2211. {
  2212. tsTopToolBar.Dock = DockStyle.Top;
  2213. InitUi();
  2214. HTMLEditHelper.DOMDocument = _doc;
  2215. Focus();
  2216. }
  2217. #if VS2010
  2218. public static string ClearWord(string sourceText, bool bIgnoreFont = true, bool bRemoveStyles = true, bool cleanWordKeepsStructure = true)
  2219. {
  2220. return ClearWordNoDefult(sourceText, bIgnoreFont, bRemoveStyles, cleanWordKeepsStructure);
  2221. }
  2222. #endif
  2223. private static String DeCompressWMZOREMZFile(String wmzoremzFile)
  2224. {
  2225. MemoryStream decompressStream = new MemoryStream(File.ReadAllBytes(wmzoremzFile));
  2226. GZipStream gzipStream = new GZipStream(decompressStream, CompressionMode.Decompress);
  2227. MemoryStream outStream = new MemoryStream();
  2228. int readCount;
  2229. byte[] data = new byte[2048];
  2230. do
  2231. {
  2232. readCount = gzipStream.Read(data, 0, data.Length);
  2233. outStream.Write(data, 0, readCount);
  2234. } while (readCount == 2048);
  2235. String imgFile = Path.GetDirectoryName(wmzoremzFile) + "\\" + Path.GetFileNameWithoutExtension(wmzoremzFile) +
  2236. (Path.GetExtension(wmzoremzFile) == ".wmz"
  2237. ? ".wmf"
  2238. : ".emf");
  2239. File.WriteAllBytes(imgFile, outStream.GetBuffer());
  2240. return imgFile;
  2241. }
  2242. public static string ReplaceWordImageFile(string sourceText)
  2243. {
  2244. var reg = new Regex(@"(?is)<v:imagedata[^>]*?src=(['""\s]?)((?:(?!topics)[^'""\s])*)\1[^>]*?>", RegexOptions.IgnoreCase);
  2245. foreach (Match m in reg.Matches(sourceText))
  2246. {
  2247. switch (Path.GetExtension(m.Groups[2].Value))
  2248. {
  2249. case ".wmz":
  2250. case ".emz":
  2251. {
  2252. string temp = m.Groups[0].Value.Replace(m.Groups[2].Value,
  2253. DeCompressWMZOREMZFile(m.Groups[2].Value.StartsWith("file:///")
  2254. ? m.Groups[2].Value.Substring(8)
  2255. : m.Groups[2].Value));
  2256. sourceText = sourceText.Replace(m.Groups[0].Value, temp);
  2257. }
  2258. break;
  2259. }
  2260. }
  2261. sourceText = Regex.Replace(sourceText, @"v:imagedata", "img");
  2262. return sourceText;
  2263. }
  2264. public static string ClearWordNoDefult(string sourceText, bool bIgnoreFont, bool bRemoveStyles, bool cleanWordKeepsStructure)
  2265. {
  2266. sourceText = ReplaceWordImageFile(sourceText);
  2267. sourceText = Regex.Replace(sourceText, @"<o:p>\s*<\/o:p>", "");
  2268. sourceText = Regex.Replace(sourceText, @"<o:p>.*?<\/o:p>", " ");
  2269. // Remove mso-xxx styles.
  2270. sourceText = Regex.Replace(sourceText, @"\s*mso-[^:]+:[^;""'>]+;?", "", RegexOptions.IgnoreCase);
  2271. // Remove margin styles.
  2272. sourceText = Regex.Replace(sourceText, @"\s*MARGIN: 0cm 0cm 0pt\s*;", "", RegexOptions.IgnoreCase);
  2273. sourceText = Regex.Replace(sourceText, @"\s*MARGIN: 0cm 0cm 0pt\s*""", "\"", RegexOptions.IgnoreCase);
  2274. sourceText = Regex.Replace(sourceText, @"\s*TEXT-INDENT: 0cm\s*;", "", RegexOptions.IgnoreCase);
  2275. sourceText = Regex.Replace(sourceText, @"\s*TEXT-INDENT: 0cm\s*""", "\"", RegexOptions.IgnoreCase);
  2276. sourceText = Regex.Replace(sourceText, @"\s*TEXT-ALIGN: [^\s;]+;?""", "\"", RegexOptions.IgnoreCase);
  2277. sourceText = Regex.Replace(sourceText, @"\s*PAGE-BREAK-BEFORE: [^\s;]+;?""", "\"", RegexOptions.IgnoreCase);
  2278. sourceText = Regex.Replace(sourceText, @"\s*FONT-VARIANT: [^\s;]+;?""", "\"", RegexOptions.IgnoreCase);
  2279. sourceText = Regex.Replace(sourceText, @"\s*tab-stops:[^;""]*;?", "", RegexOptions.IgnoreCase);
  2280. sourceText = Regex.Replace(sourceText, @"\s*tab-stops:[^""]*", "", RegexOptions.IgnoreCase);
  2281. // Remove FONT face attributes.
  2282. if (bIgnoreFont)
  2283. {
  2284. sourceText = Regex.Replace(sourceText, @"\s*face=""[^""]*""", "", RegexOptions.IgnoreCase);
  2285. sourceText = Regex.Replace(sourceText, @"\s*face=[^ >]*", "", RegexOptions.IgnoreCase);
  2286. sourceText = Regex.Replace(sourceText, @"\s*FONT-FAMILY:[^;""]*;?", "", RegexOptions.IgnoreCase);
  2287. }
  2288. // Remove Class attributes
  2289. sourceText = Regex.Replace(sourceText, @"<(\w[^>]*) class=([^ |>]*)([^>]*)", "<$1$3", RegexOptions.IgnoreCase);
  2290. // Remove styles.
  2291. if (bRemoveStyles)
  2292. sourceText = Regex.Replace(sourceText, @"<(\w[^>]*) style=""([^\""]*)""([^>]*)", "<$1$3", RegexOptions.IgnoreCase);
  2293. // Remove empty styles.
  2294. sourceText = Regex.Replace(sourceText, @"\s*style=""\s*""", "", RegexOptions.IgnoreCase);
  2295. sourceText = Regex.Replace(sourceText, @"<SPAN\s*[^>]*>\s* \s*<\/SPAN>", " ", RegexOptions.IgnoreCase);
  2296. sourceText = Regex.Replace(sourceText, @"<SPAN\s*[^>]*><\/SPAN>", "", RegexOptions.IgnoreCase);
  2297. // Remove Lang attributes
  2298. sourceText = Regex.Replace(sourceText, @"<(\w[^>]*) lang=([^ |>]*)([^>]*)", "<$1$3", RegexOptions.IgnoreCase);
  2299. sourceText = Regex.Replace(sourceText, @"<SPAN\s*>(.*?)<\/SPAN>", "$1", RegexOptions.IgnoreCase);
  2300. sourceText = Regex.Replace(sourceText, @"<FONT\s*>(.*?)<\/FONT>", "$1", RegexOptions.IgnoreCase);
  2301. // Remove XML elements and declarations
  2302. sourceText = Regex.Replace(sourceText, @"<\\?\?xml[^>]*>", "", RegexOptions.IgnoreCase);
  2303. // Remove Tags with XML namespace declarations: <o:p><\/o:p>
  2304. sourceText = Regex.Replace(sourceText, @"<\/?\w+:[^>]*>", "", RegexOptions.IgnoreCase);
  2305. // Remove comments [SF BUG-1481861].
  2306. sourceText = Regex.Replace(sourceText, @"<\!--.*?-->/", "");
  2307. sourceText = Regex.Replace(sourceText, @"<(U|I|STRIKE)> <\/\1>", " ");
  2308. sourceText = Regex.Replace(sourceText, @"<H\d>\s*<\/H\d>", "", RegexOptions.IgnoreCase);
  2309. // Remove "display:none" tags.
  2310. sourceText = Regex.Replace(sourceText, @"<(\w+)[^>]*\sstyle=""[^""]*DISPLAY\s?:\s?none(.*?)<\/\1>", "", RegexOptions.IgnoreCase);
  2311. // Remove language tags
  2312. sourceText = Regex.Replace(sourceText, @"<(\w[^>]*) language=([^ |>]*)([^>]*)", "<$1$3", RegexOptions.IgnoreCase);
  2313. // Remove onmouseover and onmouseout events (from MS Word comments effect)
  2314. sourceText = Regex.Replace(sourceText, @"<(\w[^>]*) onmouseover=""([^\""]*)""([^>]*)", "<$1$3", RegexOptions.IgnoreCase);
  2315. sourceText = Regex.Replace(sourceText, @"<(\w[^>]*) onmouseout=""([^\""]*)""([^>]*)", "<$1$3", RegexOptions.IgnoreCase);
  2316. if (cleanWordKeepsStructure)
  2317. {
  2318. // The original <Hn> tag send from Word is something like this: <Hn style="margin-top:0px;margin-bottom:0px">
  2319. sourceText = Regex.Replace(sourceText, @"<H(\d)([^>]*)>", "<h$1>", RegexOptions.IgnoreCase);
  2320. // Word likes to insert extra <font> tags, when using MSIE. (Wierd).
  2321. sourceText = Regex.Replace(sourceText, @"<(H\d)><FONT[^>]*>(.*?)<\/FONT><\/\1>", @"<$1>$2<\/$1>", RegexOptions.IgnoreCase);
  2322. sourceText = Regex.Replace(sourceText, @"<(H\d)><EM>(.*?)<\/EM><\/\1>", @"<$1>$2<\/$1>", RegexOptions.IgnoreCase);
  2323. }
  2324. else
  2325. {
  2326. sourceText = Regex.Replace(sourceText, @"<H1([^>]*)>", @"<div$1><b><font size=""6"">", RegexOptions.IgnoreCase);
  2327. sourceText = Regex.Replace(sourceText, @"<H2([^>]*)>", @"<div$1><b><font size=""5"">", RegexOptions.IgnoreCase);
  2328. sourceText = Regex.Replace(sourceText, @"<H3([^>]*)>", @"<div$1><b><font size=""4"">", RegexOptions.IgnoreCase);
  2329. sourceText = Regex.Replace(sourceText, @"<H4([^>]*)>", @"<div$1><b><font size=""3"">", RegexOptions.IgnoreCase);
  2330. sourceText = Regex.Replace(sourceText, @"<H5([^>]*)>", @"<div$1><b><font size=""2"">", RegexOptions.IgnoreCase);
  2331. sourceText = Regex.Replace(sourceText, @"<H6([^>]*)>", @"<div$1><b><font size=""1"">", RegexOptions.IgnoreCase);
  2332. sourceText = Regex.Replace(sourceText, @"<\/H\d>", @"<\/font><\/b><\/div>", RegexOptions.IgnoreCase);
  2333. // Transform <P> to <DIV>
  2334. //var re = new Regex(@"(<P)([^>]*>.*?)(<\/P>)", RegexOptions.IgnoreCase); // Different because of a IE 5.0 error
  2335. sourceText = Regex.Replace(sourceText, @"(<P)([^>]*>.*?)(<\/P>)", @"<div$2<\/div>", RegexOptions.IgnoreCase);
  2336. // Remove empty tags (three times, just to be sure).
  2337. // This also removes any empty anchor
  2338. sourceText = Regex.Replace(sourceText, @"<([^\s>]+)(\s[^>]*)?>\s*<\/\1>", "");
  2339. sourceText = Regex.Replace(sourceText, @"<([^\s>]+)(\s[^>]*)?>\s*<\/\1>", "");
  2340. sourceText = Regex.Replace(sourceText, @"<([^\s>]+)(\s[^>]*)?>\s*<\/\1>", "");
  2341. }
  2342. return sourceText;
  2343. }
  2344. private void spellCheck_DeletedWord(object sender, NetSpell.SpellChecker.SpellingEventArgs e)
  2345. {
  2346. var htmlDocument = wb.Document;
  2347. if (htmlDocument != null) if (htmlDocument.Body != null) htmlDocument.Body.InnerHtml = e.Text;
  2348. }
  2349. private void spellCheck_ReplacedWord(object sender, NetSpell.SpellChecker.ReplaceWordEventArgs e)
  2350. {
  2351. Debug.Assert(wb.Document != null, "wb.Document != null");
  2352. Debug.Assert(wb.Document.Body != null, "wb.Document.Body != null");
  2353. wb.Document.Body.InnerHtml = e.Text;
  2354. }
  2355. #region Fonts and Color Handling
  2356. //To handle the fact that setting SelectedIndex property calls SelectedIndexChanged event
  2357. //We set this value to true whenever SelectedIndex is set.
  2358. private bool _internalCall;
  2359. private void SetupComboFontSize()
  2360. {
  2361. tscbFontSize.Items.Add("1 (8 pt)");
  2362. tscbFontSize.Items.Add("2 (10 pt)");
  2363. tscbFontSize.Items.Add("3 (12 pt)");
  2364. tscbFontSize.Items.Add("4 (14 pt)");
  2365. tscbFontSize.Items.Add("5 (18 pt)");
  2366. tscbFontSize.Items.Add("6 (24 pt)");
  2367. tscbFontSize.Items.Add("7 (36 pt)");
  2368. tscbFontSize.Click += tscbFontSize_Click;
  2369. tscbFontSize.SelectedIndexChanged += tscbFontSize_SelectedIndexChanged;
  2370. }
  2371. private void tscbFontSize_Click(object sender, EventArgs e)
  2372. {
  2373. _internalCall = false;
  2374. }
  2375. /// <summary>
  2376. /// Set editor's current selection to the value of the font size combo box.
  2377. /// Ignore if the timer is currently updating the font size to synchronize
  2378. /// the font size combo box with the editor's current selection.
  2379. /// </summary>
  2380. /// <param name="sender">the sender</param>
  2381. /// <param name="e">EventArgs</param>
  2382. private void tscbFontSize_SelectedIndexChanged(object sender, EventArgs e)
  2383. {
  2384. try
  2385. {
  2386. //Fontsize changed 1 to 7
  2387. if ((tscbFontSize.SelectedIndex > -1) && (!_internalCall))
  2388. {
  2389. if (_updatingFontSize) return;
  2390. int obj = tscbFontSize.SelectedIndex + 1;
  2391. switch (obj)
  2392. {
  2393. case 1:
  2394. FontSize = FontSize.One;
  2395. break;
  2396. case 2:
  2397. FontSize = FontSize.Two;
  2398. break;
  2399. case 3:
  2400. FontSize = FontSize.Three;
  2401. break;
  2402. case 4:
  2403. FontSize = FontSize.Four;
  2404. break;
  2405. case 5:
  2406. FontSize = FontSize.Five;
  2407. break;
  2408. case 6:
  2409. FontSize = FontSize.Six;
  2410. break;
  2411. case 7:
  2412. FontSize = FontSize.Seven;
  2413. break;
  2414. default:
  2415. FontSize = FontSize.Seven;
  2416. break;
  2417. }
  2418. Focus();
  2419. }
  2420. _internalCall = false;
  2421. }
  2422. catch
  2423. {
  2424. }
  2425. }
  2426. /// <summary>
  2427. /// Called when the font combo box has changed.
  2428. /// Ignores the event when the timer is updating the font combo Box
  2429. /// to synchronize the editor selection with the font combo box.
  2430. /// </summary>
  2431. /// <param name="sender">the sender</param>
  2432. /// <param name="e">EventArgs</param>
  2433. private void tsfcbFontName_SelectedIndexChanged(object sender, EventArgs e)
  2434. {
  2435. if (_updatingFontName) return;
  2436. FontFamily ff;
  2437. try
  2438. {
  2439. ff = new FontFamily(tsfcbFontName.Text);
  2440. }
  2441. catch (Exception)
  2442. {
  2443. _updatingFontName = true;
  2444. tsfcbFontName.Text = FontName.GetName(0);
  2445. _updatingFontName = false;
  2446. return;
  2447. }
  2448. FontName = ff;
  2449. Focus();
  2450. }
  2451. #endregion
  2452. #region Private variables and methods
  2453. private void SynchFont(string sTagName)
  2454. {
  2455. //Times Roman New
  2456. object obj = QueryCommandRange(HTML_COMMAND_FONTNAME);
  2457. if (obj.IsNull())
  2458. return;
  2459. string fontname = obj.ToString();
  2460. obj = QueryCommandRange(HTML_COMMAND_FONTSIZE);
  2461. if (obj.IsNull())
  2462. return;
  2463. //Could indicate a headingxxx, P, or BODY
  2464. _internalCall = true;
  2465. if (obj.ToString().Length > 0)
  2466. tscbFontSize.SelectedIndex = Convert.ToInt32(obj) - 1; //x (x - 1)
  2467. else
  2468. AdjustForHeading(sTagName);
  2469. tsfcbFontName.Text = fontname;
  2470. }
  2471. private void AdjustForHeading(string sTag)
  2472. {
  2473. if (sTag.IsNullOrEmpty())
  2474. return;
  2475. int index;
  2476. if (sTag == "H1")
  2477. index = 5; //24pt
  2478. else if (sTag == "H2")
  2479. index = 4; //18pt
  2480. else if (sTag == "H3")
  2481. index = 3; //14pt
  2482. else if (sTag == "H4")
  2483. index = 2; //12pt
  2484. else if (sTag == "H5")
  2485. index = 1; //10pt
  2486. else if (sTag == "H6")
  2487. index = 0; //8pt
  2488. else
  2489. return; //do nothing
  2490. _internalCall = true;
  2491. tscbFontSize.SelectedIndex = index;
  2492. }
  2493. /// <summary>
  2494. /// Set up a key listener on the body once.
  2495. /// The key listener checks for specific key strokes and takes
  2496. /// special action in certain cases.
  2497. /// </summary>
  2498. /// <returns></returns>
  2499. private bool SetupKeyListener()
  2500. {
  2501. // handle enter code cancellation
  2502. bool cancel = false;
  2503. if (!EnterKeyEvent.IsNull())
  2504. {
  2505. var args = new EnterKeyEventArgs();
  2506. EnterKeyEvent(this, args);
  2507. cancel = args.Cancel;
  2508. }
  2509. return cancel;
  2510. }
  2511. /// <summary>
  2512. /// Determine whether the current block is left justified.
  2513. /// </summary>
  2514. /// <returns>true if left justified, otherwise false</returns>
  2515. public bool IsJustifyLeft()
  2516. {
  2517. return ExecuteCommandQuery(HTML_COMMAND_JUSTIFY_LEFT);
  2518. }
  2519. /// <summary>
  2520. /// Determine whether the current block is right justified.
  2521. /// </summary>
  2522. /// <returns>true if right justified, otherwise false</returns>
  2523. public bool IsJustifyRight()
  2524. {
  2525. return ExecuteCommandQuery(HTML_COMMAND_JUSTIFY_RIGHT);
  2526. }
  2527. /// <summary>
  2528. /// Determine whether the current block is center justified.
  2529. /// </summary>
  2530. /// <returns>true if center justified, false otherwise</returns>
  2531. public bool IsJustifyCenter()
  2532. {
  2533. return ExecuteCommandQuery(HTML_COMMAND_JUSTIFY_CENTER);
  2534. }
  2535. /// <summary>
  2536. /// Determine whether the current block is full justified.
  2537. /// </summary>
  2538. /// <returns>true if full justified, false otherwise</returns>
  2539. public bool IsJustifyFull()
  2540. {
  2541. return ExecuteCommandQuery(HTML_COMMAND_JUSTIFY_FULL);
  2542. }
  2543. /// <summary>
  2544. /// Determine whether the current selection is in Bold mode.
  2545. /// </summary>
  2546. /// <returns>whether or not the current selection is Bold</returns>
  2547. public bool IsBold()
  2548. {
  2549. return ExecuteCommandQuery(HTML_COMMAND_BOLD);
  2550. }
  2551. /// <summary>
  2552. /// Determine whether the current selection is in Italic mode.
  2553. /// </summary>
  2554. /// <returns>whether or not the current selection is Italicized</returns>
  2555. public bool IsItalic()
  2556. {
  2557. return ExecuteCommandQuery(HTML_COMMAND_ITALIC);
  2558. }
  2559. /// <summary>
  2560. /// Determine whether the current selection is in Underline mode.
  2561. /// </summary>
  2562. /// <returns>whether or not the current selection is Underlined</returns>
  2563. public bool IsUnderline()
  2564. {
  2565. return ExecuteCommandQuery(HTML_COMMAND_UNDERLINE);
  2566. }
  2567. /// <summary>
  2568. /// Determine whether the current selection is in StrikeThrough mode.
  2569. /// </summary>
  2570. /// <returns>whether or not the current selection is StrikeThrough</returns>
  2571. public bool IsStrikeThrough()
  2572. {
  2573. return ExecuteCommandQuery(HTML_COMMAND_STRIKE_THROUGH);
  2574. }
  2575. /// <summary>
  2576. /// Determine whether the current selection is in Subscript mode.
  2577. /// </summary>
  2578. /// <returns>whether or not the current selection is Subscript</returns>
  2579. public bool IsSubscript()
  2580. {
  2581. return ExecuteCommandQuery(HTML_COMMAND_SUBSCRIPT);
  2582. }
  2583. /// <summary>
  2584. /// Determine whether the current selection is in Superscript mode.
  2585. /// </summary>
  2586. /// <returns>whether or not the current selection is Superscript</returns>
  2587. public bool IsSuperscript()
  2588. {
  2589. return ExecuteCommandQuery(HTML_COMMAND_SUPERSCRIPT);
  2590. }
  2591. /// <summary>
  2592. /// Determine whether the current paragraph is an ordered list.
  2593. /// </summary>
  2594. /// <returns>true if current paragraph is ordered, false otherwise</returns>
  2595. public bool IsOrderedList()
  2596. {
  2597. return ExecuteCommandQuery(HTML_COMMAND_INSERT_ORDERED_LIST);
  2598. }
  2599. /// <summary>
  2600. /// Determine whether the current paragraph is an unordered list.
  2601. /// </summary>
  2602. /// <returns>true if current paragraph is ordered, false otherwise</returns>
  2603. public bool IsUnorderedList()
  2604. {
  2605. return ExecuteCommandQuery(HTML_COMMAND_INSERT_UNORDERED_LIST);
  2606. }
  2607. /// <summary>
  2608. /// Determine whether the current block can undo.
  2609. /// </summary>
  2610. /// <returns>true if current block can undo, false otherwise</returns>
  2611. public bool IsUndo()
  2612. {
  2613. return _doc.queryCommandEnabled(HTML_COMMAND_UNDO);
  2614. }
  2615. /// <summary>
  2616. /// Determine whether the current block can redo.
  2617. /// </summary>
  2618. /// <returns>true if current block can redo, false otherwise</returns>
  2619. public bool IsRedo()
  2620. {
  2621. return _doc.queryCommandEnabled(HTML_COMMAND_REDO);
  2622. }
  2623. /// <summary>
  2624. /// Determine whether the current block can unlink.
  2625. /// </summary>
  2626. /// <returns>true if current block can unlink, false otherwise</returns>
  2627. public bool IsUnlink()
  2628. {
  2629. return ExecuteCommandQuery(HTML_COMMAND_UNLINK, true);
  2630. }
  2631. /// <summary>
  2632. /// Update the font size combo box.
  2633. /// Sets a flag to indicate that the combo box is updating, and should
  2634. /// not update the editor's selection.
  2635. /// </summary>
  2636. private void UpdateFontSizeComboBox()
  2637. {
  2638. if (!tscbFontSize.Focused)
  2639. {
  2640. int foo;
  2641. switch (FontSize)
  2642. {
  2643. case FontSize.One:
  2644. foo = 0;
  2645. break;
  2646. case FontSize.Two:
  2647. foo = 1;
  2648. break;
  2649. case FontSize.Three:
  2650. foo = 2;
  2651. break;
  2652. case FontSize.Four:
  2653. foo = 3;
  2654. break;
  2655. case FontSize.Five:
  2656. foo = 4;
  2657. break;
  2658. case FontSize.Six:
  2659. foo = 5;
  2660. break;
  2661. case FontSize.Seven:
  2662. foo = 6;
  2663. break;
  2664. case FontSize.NA:
  2665. foo = -1;
  2666. break;
  2667. default:
  2668. foo = 2;
  2669. break;
  2670. }
  2671. //string fontsize = Convert.ToString(foo);
  2672. if (foo != tscbFontSize.SelectedIndex)
  2673. {
  2674. _updatingFontSize = true;
  2675. tscbFontSize.SelectedIndex = foo;
  2676. _updatingFontSize = false;
  2677. }
  2678. }
  2679. }
  2680. /// <summary>
  2681. /// Update the font combo box.
  2682. /// Sets a flag to indicate that the combo box is updating, and should
  2683. /// not update the editor's selection.
  2684. /// </summary>
  2685. private void UpdateFontComboBox()
  2686. {
  2687. if (!tsfcbFontName.Focused)
  2688. {
  2689. FontFamily fam = FontName;
  2690. if (!fam.IsNull())
  2691. {
  2692. string fontname = fam.Name;
  2693. if (fontname != tsfcbFontName.Text)
  2694. {
  2695. _updatingFontName = true;
  2696. tsfcbFontName.Text = fontname;
  2697. _updatingFontName = false;
  2698. }
  2699. }
  2700. }
  2701. }
  2702. /// <summary>
  2703. /// Get/Set the current font size.
  2704. /// </summary>
  2705. [Browsable(false)]
  2706. public FontSize FontSize
  2707. {
  2708. get
  2709. {
  2710. if (QueryCommandRange(HTML_COMMAND_FONTSIZE).IsNull())
  2711. return FontSize.NA;
  2712. switch (QueryCommandRange(HTML_COMMAND_FONTSIZE).ToString())
  2713. {
  2714. case "1":
  2715. return FontSize.One;
  2716. case "2":
  2717. return FontSize.Two;
  2718. case "3":
  2719. return FontSize.Three;
  2720. case "4":
  2721. return FontSize.Four;
  2722. case "5":
  2723. return FontSize.Five;
  2724. case "6":
  2725. return FontSize.Six;
  2726. case "7":
  2727. return FontSize.Seven;
  2728. default:
  2729. return FontSize.NA;
  2730. }
  2731. }
  2732. set
  2733. {
  2734. int sz;
  2735. switch (value)
  2736. {
  2737. case FontSize.One:
  2738. sz = 1;
  2739. break;
  2740. case FontSize.Two:
  2741. sz = 2;
  2742. break;
  2743. case FontSize.Three:
  2744. sz = 3;
  2745. break;
  2746. case FontSize.Four:
  2747. sz = 4;
  2748. break;
  2749. case FontSize.Five:
  2750. sz = 5;
  2751. break;
  2752. case FontSize.Six:
  2753. sz = 6;
  2754. break;
  2755. case FontSize.Seven:
  2756. sz = 7;
  2757. break;
  2758. default:
  2759. sz = 7;
  2760. break;
  2761. }
  2762. if (!wb.Document.IsNull())
  2763. wb.Document.ExecCommand(HTML_COMMAND_FONTSIZE, false, sz.ToString(CultureInfo.InvariantCulture));
  2764. }
  2765. }
  2766. /// <summary>
  2767. /// Get/Set the current font name.
  2768. /// </summary>
  2769. [Browsable(false)]
  2770. public FontFamily FontName
  2771. {
  2772. get
  2773. {
  2774. var name = QueryCommandRange(HTML_COMMAND_FONTNAME) as string;
  2775. if (name.IsNull()) return null;
  2776. return new FontFamily(name);
  2777. }
  2778. set
  2779. {
  2780. if (!value.IsNull() && !wb.Document.IsNull())
  2781. {
  2782. wb.Document.ExecCommand(HTML_COMMAND_FONTNAME, false, value.Name);
  2783. }
  2784. }
  2785. }
  2786. #endregion
  2787. private void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  2788. {
  2789. string url = e.Url.ToString();
  2790. if (!codeNavigate)
  2791. {
  2792. // call the appropriate event processing
  2793. var navigateArgs = new HtmlNavigationEventArgs(url);
  2794. OnHtmlNavigation(navigateArgs);
  2795. // process the event based on the navigation option
  2796. if (navigateArgs.Cancel)
  2797. {
  2798. // cancel the navigation
  2799. e.Cancel = true;
  2800. }
  2801. else if (_navigateWindow == NavigateActionOption.NewWindow)
  2802. {
  2803. // cancel the current navigation and load url into a new window
  2804. e.Cancel = true;
  2805. NavigateToUrl(url, true);
  2806. }
  2807. else
  2808. {
  2809. // continue with current navigation
  2810. e.Cancel = false;
  2811. }
  2812. }
  2813. else
  2814. {
  2815. // TODO Should ensure the following are no executed for the editor navigation
  2816. // Scripts
  2817. // Java
  2818. // ActiveX Controls
  2819. // Behaviors
  2820. // Dialogs
  2821. // continue with current navigation
  2822. e.Cancel = false;
  2823. }
  2824. }
  2825. /// <summary>
  2826. /// Processing for the HtmlNavigation event
  2827. /// </summary>
  2828. private void OnHtmlNavigation(HtmlNavigationEventArgs args)
  2829. {
  2830. if (!HtmlNavigation.IsNull())
  2831. {
  2832. HtmlNavigation(this, args);
  2833. }
  2834. } //OnHtmlNavigation
  2835. #region MsHtml Command Processing
  2836. /// <summary>
  2837. /// Performs a query of the command state
  2838. /// </summary>
  2839. private bool ExecuteCommandQuery(string command, bool isEnabled=false)
  2840. {
  2841. // obtain the selected range object and query command
  2842. mshtmlTextRange range = GetTextRange();
  2843. return ExecuteCommandQuery(range, command, isEnabled);
  2844. } //ExecuteCommandQuery
  2845. /// <summary>
  2846. /// Executes the queryCommandState on the selected range (given the range)
  2847. /// </summary>
  2848. private bool ExecuteCommandQuery(mshtmlTextRange range, string command, bool isEnabled = false)
  2849. {
  2850. // set the initial state as false
  2851. bool retValue = false;
  2852. try
  2853. {
  2854. if (!range.IsNull())
  2855. {
  2856. // ensure command is a valid command and then enabled for the selection
  2857. if (range.queryCommandSupported(command))
  2858. {
  2859. if (isEnabled)
  2860. {
  2861. retValue = range.queryCommandEnabled(command);
  2862. }
  2863. else
  2864. {
  2865. if (range.queryCommandEnabled(command))
  2866. {
  2867. // mark the selection with the appropriate tag
  2868. retValue = range.queryCommandState(command);
  2869. }
  2870. }
  2871. }
  2872. }
  2873. }
  2874. catch (Exception ex)
  2875. {
  2876. // Unknown error so inform user
  2877. throw new HtmlEditorException("Unknown MSHTML Error.", command, ex);
  2878. }
  2879. // return the value
  2880. return retValue;
  2881. } // ExecuteCommandQuery
  2882. /// <summary>
  2883. /// Executes the execCommand on the selected range
  2884. /// </summary>
  2885. private void ExecuteCommandRange(string command, object data)
  2886. {
  2887. // obtain the selected range object and execute command
  2888. mshtmlTextRange range = GetTextRange();
  2889. ExecuteCommandRange(range, command, data);
  2890. } // ExecuteCommandRange
  2891. /// <summary>
  2892. /// Executes the execCommand on the selected range (given the range)
  2893. /// </summary>
  2894. private void ExecuteCommandRange(mshtmlTextRange range, string command, object data)
  2895. {
  2896. try
  2897. {
  2898. if (!range.IsNull())
  2899. {
  2900. // ensure command is a valid command and then enabled for the selection
  2901. if (range.queryCommandSupported(command))
  2902. {
  2903. if (range.queryCommandEnabled(command))
  2904. {
  2905. // mark the selection with the appropriate tag
  2906. range.execCommand(command, false, data);
  2907. }
  2908. }
  2909. }
  2910. }
  2911. catch (Exception ex)
  2912. {
  2913. // Unknown error so inform user
  2914. throw new HtmlEditorException("Unknown MSHTML Error.", command, ex);
  2915. }
  2916. } // ExecuteCommandRange
  2917. /// <summary>
  2918. /// Executes the execCommand on the document with a system prompt
  2919. /// </summary>
  2920. private void ExecuteCommandDocumentPrompt(string command)
  2921. {
  2922. ExecuteCommandDocument(command, true);
  2923. } // ExecuteCommandDocumentPrompt
  2924. /// <summary>
  2925. /// Executes the execCommand on the document with a system prompt
  2926. /// </summary>
  2927. private void ExecuteCommandDocument(string command, bool prompt = false)
  2928. {
  2929. try
  2930. {
  2931. // ensure command is a valid command and then enabled for the selection
  2932. if (document.queryCommandSupported(command))
  2933. {
  2934. // if (document.queryCommandEnabled(command)) {}
  2935. // Test fails with a COM exception if command is Print
  2936. // execute the given command
  2937. document.execCommand(command, prompt, null);
  2938. }
  2939. }
  2940. catch (Exception ex)
  2941. {
  2942. // Unknown error so inform user
  2943. throw new HtmlEditorException("Unknown MSHTML Error.", command, ex);
  2944. }
  2945. } // ExecuteCommandDocumentPrompt
  2946. /// <summary>
  2947. /// Determines the value of the command
  2948. /// </summary>
  2949. private object QueryCommandRange(string command)
  2950. {
  2951. // obtain the selected range object and execute command
  2952. mshtmlTextRange range = GetTextRange();
  2953. return !range.IsNull()? QueryCommandRange(range, command) : null;
  2954. } // QueryCommandRange
  2955. /// <summary>
  2956. /// Determines the value of the command
  2957. /// </summary>
  2958. private object QueryCommandRange(mshtmlTextRange range, string command)
  2959. {
  2960. object retValue = null;
  2961. if (!range.IsNull())
  2962. {
  2963. try
  2964. {
  2965. // ensure command is a valid command and then enabled for the selection
  2966. if (range.queryCommandSupported(command))
  2967. {
  2968. if (range.queryCommandEnabled(command))
  2969. {
  2970. retValue = range.queryCommandValue(command);
  2971. }
  2972. }
  2973. }
  2974. catch (Exception)
  2975. {
  2976. // have unknown error so set return to null
  2977. retValue = null;
  2978. }
  2979. }
  2980. // return the obtained value
  2981. return retValue;
  2982. } //QueryCommandRange
  2983. /// <summary>
  2984. /// Gets the selected Html Range Element
  2985. /// </summary>
  2986. private mshtmlTextRange GetTextRange()
  2987. {
  2988. // define the selected range object
  2989. mshtmlTextRange range = null;
  2990. try
  2991. {
  2992. // calculate the text range based on user selection
  2993. mshtmlSelection selection = document.selection;
  2994. if (IsStringEqual(selection.type, SELECT_TYPE_TEXT) || IsStringEqual(selection.type, SELECT_TYPE_NONE))
  2995. {
  2996. range = (mshtmlTextRange)selection.createRange();
  2997. }
  2998. }
  2999. catch (Exception)
  3000. {
  3001. // have unknown error so set return to null
  3002. range = null;
  3003. }
  3004. return range;
  3005. } // GetTextRange
  3006. /// <summary>
  3007. /// Determines the color of the selected text range
  3008. /// Returns default value if not text selected
  3009. /// </summary>
  3010. private Color GetFontColor()
  3011. {
  3012. object fontColor = QueryCommandRange(HTML_COMMAND_FORE_COLOR);
  3013. return (fontColor is Int32) ? ColorTranslator.FromWin32((int)fontColor) : _bodyForeColor;
  3014. } //GetFontColor
  3015. /// <summary>
  3016. /// Determines the color of the selected text range
  3017. /// Returns default value if not text selected
  3018. /// </summary>
  3019. private Color GetBackColor()
  3020. {
  3021. object fontColor = QueryCommandRange(HTML_COMMAND_BACK_COLOR);
  3022. return (fontColor is Int32) ? ColorTranslator.FromWin32((int)fontColor) : _bodyBackColor;
  3023. } //GetFontColor
  3024. /// <summary>
  3025. /// Gets the first selected Html Control as an Element
  3026. /// </summary>
  3027. private mshtmlElement GetFirstControl()
  3028. {
  3029. // define the selected range object
  3030. mshtmlElement control = null;
  3031. try
  3032. {
  3033. // calculate the first control based on the user selection
  3034. mshtmlSelection selection = document.selection;
  3035. if (IsStringEqual(selection.type, SELECT_TYPE_CONTROL))
  3036. {
  3037. var range = (mshtmlControlRange)selection.createRange();
  3038. if (range.length > 0) control = range.item(0);
  3039. }
  3040. }
  3041. catch (Exception)
  3042. {
  3043. // have unknown error so set return to null
  3044. control = null;
  3045. }
  3046. return control;
  3047. } // GetFirstControl
  3048. /// <summary>
  3049. /// Gets all the selected Html Controls as a Control Range
  3050. /// </summary>
  3051. /// <returns></returns>
  3052. private mshtmlControlRange GetAllControls()
  3053. {
  3054. // define the selected range object
  3055. mshtmlControlRange range = null;
  3056. try
  3057. {
  3058. // calculate the first control based on the user selection
  3059. mshtmlSelection selection = document.selection;
  3060. if (IsStringEqual(selection.type, SELECT_TYPE_CONTROL))
  3061. {
  3062. range = (mshtmlControlRange)selection.createRange();
  3063. }
  3064. }
  3065. catch (Exception)
  3066. {
  3067. // have unknow error so set return to null
  3068. range = null;
  3069. }
  3070. return range;
  3071. } //GetAllControls
  3072. #endregion
  3073. /// <summary>
  3074. /// Method to process the toolbar command and handle error exception
  3075. /// </summary>
  3076. private void ProcessCommand(string command)
  3077. {
  3078. try
  3079. {
  3080. // Evaluate the Button property to determine which button was clicked.
  3081. switch (command)
  3082. {
  3083. case INTERNAL_COMMAND_NEW:
  3084. // New document
  3085. New();
  3086. break;
  3087. case INTERNAL_COMMAND_OPEN:
  3088. // Open a selected file
  3089. Open();
  3090. break;
  3091. case INTERNAL_COMMAND_PRINT:
  3092. // Print the current document
  3093. Print();
  3094. break;
  3095. case INTERNAL_COMMAND_SAVE:
  3096. // Saves the current document
  3097. Save();
  3098. break;
  3099. case INTERNAL_COMMAND_PREVIEW:
  3100. // Preview the html page
  3101. Preview();
  3102. break;
  3103. case INTERNAL_COMMAND_SHOWHTML:
  3104. // View the html contents
  3105. ShowHTML();
  3106. break;
  3107. case INTERNAL_COMMAND_COPY:
  3108. // Browser COPY command
  3109. Copy();
  3110. break;
  3111. case INTERNAL_COMMAND_CUT:
  3112. // Browser CUT command
  3113. Cut();
  3114. break;
  3115. case INTERNAL_COMMAND_PASTE:
  3116. // Browser PASTE command
  3117. Paste();
  3118. break;
  3119. case INTERNAL_COMMAND_DELETE:
  3120. // Browser DELETE command
  3121. Delete();
  3122. break;
  3123. case INTERNAL_COMMAND_UNDO:
  3124. // Undo the previous editing
  3125. Undo();
  3126. break;
  3127. case INTERNAL_COMMAND_REDO:
  3128. // Redo the previous undo
  3129. Redo();
  3130. break;
  3131. case INTERNAL_COMMAND_FIND:
  3132. //Find the string u input
  3133. Find();
  3134. break;
  3135. case INTERNAL_COMMAND_Replace:
  3136. //Find the string u input
  3137. Replace();
  3138. break;
  3139. case INTERNAL_COMMAND_REMOVE_FORMAT:
  3140. // Browser REMOVEFORMAT command
  3141. RemoveFormat();
  3142. break;
  3143. case INTERNAL_COMMAND_JUSTIFYCENTER:
  3144. // Justify Center
  3145. JustifyCenter();
  3146. break;
  3147. case INTERNAL_COMMAND_JUSTIFYFULL:
  3148. // Justify Full
  3149. JustifyFull();
  3150. break;
  3151. case INTERNAL_COMMAND_JUSTIFYLEFT:
  3152. // Justify Left
  3153. JustifyLeft();
  3154. break;
  3155. case INTERNAL_COMMAND_JUSTIFYRIGHT:
  3156. // Justify Right
  3157. JustifyRight();
  3158. break;
  3159. case INTERNAL_COMMAND_UNDERLINE:
  3160. // Selection UNDERLINE command
  3161. Underline();
  3162. break;
  3163. case INTERNAL_COMMAND_ITALIC:
  3164. // Selection ITALIC command
  3165. Italic();
  3166. break;
  3167. case INTERNAL_COMMAND_BOLD:
  3168. // Selection BOLD command
  3169. Bold();
  3170. break;
  3171. case INTERNAL_COMMAND_FORECOLOR:
  3172. // FORECOLOR style creation
  3173. FormatFontColor(tscpForeColor.Color);
  3174. break;
  3175. case INTERNAL_COMMAND_BACKCOLOR:
  3176. // BACKCOLOR style creation
  3177. FormatBackColor(tscpBackColor.Color);
  3178. break;
  3179. case INTERNAL_COMMAND_STRIKETHROUGH:
  3180. // Selection STRIKETHROUGH command
  3181. StrikeThrough();
  3182. break;
  3183. case INTERNAL_COMMAND_CREATELINK:
  3184. // Selection CREATELINK command
  3185. CreateLink();
  3186. break;
  3187. case INTERNAL_COMMAND_UNLINK:
  3188. // Selection UNLINK command
  3189. UnLink();
  3190. break;
  3191. case INTERNAL_COMMAND_INSERTTABLE:
  3192. // Display a dialog to enable the user to insert a table
  3193. TableInsertPrompt();
  3194. break;
  3195. case INTERNAL_COMMAND_TABLEPROPERTIES:
  3196. // Display a dialog to enable the user to modify a table
  3197. TableModifyPrompt();
  3198. break;
  3199. case INTERNAL_COMMAND_TABLEINSERTROW:
  3200. // Display a dialog to enable the user to modify a table
  3201. TableInsertRow();
  3202. break;
  3203. case INTERNAL_COMMAND_TABLEDELETEROW:
  3204. // Display a dialog to enable the user to modify a table
  3205. TableDeleteRow();
  3206. break;
  3207. case INTERNAL_COMMAND_INSERTIMAGE:
  3208. // Display a dialog to enable the user to insert a image
  3209. InsertImage();
  3210. break;
  3211. case INTERNAL_COMMAND_INSERTHORIZONTALRULE:
  3212. // Horizontal Line
  3213. InsertLine();
  3214. break;
  3215. case INTERNAL_COMMAND_OUTDENT:
  3216. // Tab Left
  3217. Outdent();
  3218. break;
  3219. case INTERNAL_COMMAND_INDENT:
  3220. // Tab Right
  3221. Indent();
  3222. break;
  3223. case INTERNAL_COMMAND_INSERTUNORDEREDLIST:
  3224. // Unordered List
  3225. InsertUnorderedList();
  3226. break;
  3227. case INTERNAL_COMMAND_INSERTORDEREDLIST:
  3228. // Ordered List
  3229. InsertOrderedList();
  3230. break;
  3231. case INTERNAL_COMMAND_SUPERSCRIPT:
  3232. // Selection SUPERSCRIPT command
  3233. Superscript();
  3234. break;
  3235. case INTERNAL_COMMAND_SUBSCRIPT:
  3236. // Selection SUBSCRIPT command
  3237. Subscript();
  3238. break;
  3239. case INTERNAL_COMMAND_SELECTALL:
  3240. // Selects all document content
  3241. SelectAll();
  3242. break;
  3243. case INTERNAL_COMMAND_WORDCOUNT:
  3244. // Word count
  3245. int wordCount = WordCount();
  3246. tsslWordCount.Text = string.Format("字数:{0}", wordCount);
  3247. break;
  3248. case INTERNAL_COMMAND_INSERTDATE:
  3249. // Insert date
  3250. PasteIntoSelection(DateTime.Now.ToLongDateString());
  3251. break;
  3252. case INTERNAL_COMMAND_INSERTTIME:
  3253. // Insert time
  3254. PasteIntoSelection(DateTime.Now.ToLongTimeString());
  3255. break;
  3256. case INTERNAL_COMMAND_CLEARWORD:
  3257. // Clear word
  3258. if (BodyInnerHTML != null)
  3259. {
  3260. Debug.Assert(wb.Document != null, "wb.Document != null");
  3261. Debug.Assert(wb.Document.Body != null, "wb.Document.Body != null");
  3262. #if VS2010
  3263. wb.Document.Body.InnerHtml = ClearWord(BodyInnerHTML);
  3264. #else
  3265. wb.Document.Body.InnerHtml = ClearWordNoDefult(BodyInnerHTML, true, true, true);
  3266. #endif
  3267. }
  3268. break;
  3269. case INTERNAL_COMMAND_AUTOLAYOUT:
  3270. // Auto Layout
  3271. AutoLayout();
  3272. break;
  3273. case INTERNAL_COMMAND_ABOUT:
  3274. if (MessageBox.Show(Resources.AboutInfo, Resources.AboutText, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
  3275. {
  3276. try
  3277. {
  3278. Process.Start("http://www.lyfz.net/WinHtmlEditor/");
  3279. }
  3280. catch(Exception ex) {
  3281. MessageBox.Show("打开网址时,因系统资源管理器异常,打开失败。错误原因:"+ex.Message);
  3282. }
  3283. }
  3284. break;
  3285. default:
  3286. throw new HtmlEditorException("Unknown Operation Being Performed.", command);
  3287. }
  3288. }
  3289. catch (HtmlEditorException ex)
  3290. {
  3291. // process the html exception
  3292. OnHtmlException(new HtmlExceptionEventArgs(ex.Operation, ex));
  3293. }
  3294. catch (Exception ex)
  3295. {
  3296. // process the exception
  3297. OnHtmlException(new HtmlExceptionEventArgs(null, ex));
  3298. }
  3299. // ensure web browser has the focus after command execution
  3300. Focus();
  3301. } //ProcessCommand
  3302. /// <summary>
  3303. /// Method to raise an event if a delegeate is assigned for handling exceptions
  3304. /// </summary>
  3305. private void OnHtmlException(HtmlExceptionEventArgs args)
  3306. {
  3307. if (HtmlException.IsNull())
  3308. {
  3309. // obtain the message and operation
  3310. // concatenate the message with any inner message
  3311. string operation = args.Operation;
  3312. Exception ex = args.ExceptionObject;
  3313. string message = ex.Message;
  3314. if (!ex.InnerException.IsNull())
  3315. {
  3316. message = string.Format("{0}\n{1}", message, ex.InnerException.Message);
  3317. }
  3318. // define the title for the internal message box
  3319. string title;
  3320. if (operation.IsNullOrEmpty())
  3321. {
  3322. title = "Unknown Error";
  3323. }
  3324. else
  3325. {
  3326. title = operation + " Error";
  3327. }
  3328. // display the error message box
  3329. MessageBox.Show(this, message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
  3330. }
  3331. else
  3332. {
  3333. HtmlException(this, args);
  3334. }
  3335. }
  3336. private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  3337. {
  3338. // get access to the HTMLDocument
  3339. var htmlDocument = wb.Document;
  3340. if (!htmlDocument.IsNull())
  3341. {
  3342. document = (mshtmlDocument)htmlDocument.DomDocument;
  3343. _doc = (mshtmlIHTMLDocument2)htmlDocument.DomDocument;
  3344. // at this point the document and body has been loaded
  3345. // so define the event handler for the context menu
  3346. htmlDocument.ContextMenuShowing += DocumentContextMenu;
  3347. htmlDocument.AttachEventHandler("onselectionchange", DocumentSelectionChange);
  3348. htmlDocument.AttachEventHandler("onkeydown", DocumentKeyPress);
  3349. }
  3350. body = (mshtmlBody)document.body;
  3351. // COM Interop Start
  3352. // once browsing has completed there is the need to setup some options
  3353. // need to ensure URLs are not modified when html is pasted
  3354. int hResult;
  3355. // try to obtain the command target for the web browser document
  3356. try
  3357. {
  3358. // cast the document to a command target
  3359. var target = (IOleCommandTarget)document;
  3360. // set the appropriate no url fixups on paste
  3361. hResult = target.Exec(ref CommandGroup.CGID_MSHTML, (int)CommandId.IDM_NOFIXUPURLSONPASTE, (int)CommandOption.OLECMDEXECOPT_DONTPROMPTUSER, ref EMPTY_PARAMETER, ref EMPTY_PARAMETER);
  3362. }
  3363. // catch any exception and map back to the HRESULT
  3364. catch (Exception ex)
  3365. {
  3366. hResult = Marshal.GetHRForException(ex);
  3367. }
  3368. // test the HRESULT for a valid operation
  3369. rebaseUrlsNeeded = hResult != HRESULT.S_OK;
  3370. // COM Interop End
  3371. // signalled complete
  3372. codeNavigate = false;
  3373. loading = false;
  3374. // after navigation define the document Url
  3375. string url = e.Url.ToString();
  3376. _bodyUrl = IsStringEqual(url, BLANK_HTML_PAGE) ? string.Empty : url;
  3377. }
  3378. /// <summary>
  3379. /// Called when a key is pressed on the font size combo box.
  3380. /// The font size in the boxy box is set to the key press value.
  3381. /// </summary>
  3382. /// <param name="sender">the sender</param>
  3383. /// <param name="e">KeyPressEventArgs</param>
  3384. private void tscbFontSize_KeyPress(object sender, KeyPressEventArgs e)
  3385. {
  3386. if (Char.IsNumber(e.KeyChar))
  3387. {
  3388. e.Handled = true;
  3389. if (e.KeyChar <= '7' && e.KeyChar > '0')
  3390. tscbFontSize.SelectedIndex = GeneralUtil.ParseInt(e.KeyChar) - 1;
  3391. }
  3392. else if (!Char.IsControl(e.KeyChar))
  3393. {
  3394. e.Handled = true;
  3395. }
  3396. }
  3397. /// <summary>
  3398. /// General Context Meun processing method
  3399. /// Calls the ProcessCommand with the selected command Tag Text
  3400. /// </summary>
  3401. private void ContextEditorClick(object sender, EventArgs e)
  3402. {
  3403. var menuItem = (ToolStripMenuItem)sender;
  3404. var command = (string)menuItem.Tag;
  3405. ProcessCommand(command);
  3406. } //contextEditorClick
  3407. private void tscpBackColor_SelectedColorChanged(object sender, EventArgs e)
  3408. {
  3409. var tscp = (ToolStripColorPicker)sender;
  3410. var command = (string)tscp.Tag;
  3411. ProcessCommand(command);
  3412. }
  3413. private void tscpForeColor_SelectedColorChanged(object sender, EventArgs e)
  3414. {
  3415. var tscp = (ToolStripColorPicker)sender;
  3416. var command = (string)tscp.Tag;
  3417. ProcessCommand(command);
  3418. }
  3419. }
  3420. }