SimpleBrowser.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /////////////////////////////////////////////////////////////////////////////
  2. // SimpleBrowser: Web browser control
  3. /////////////////////////////////////////////////////////////////////////////
  4. #if !defined(SimpleBrowser_defined)
  5. #define SimpleBrowser_defined
  6. #include "mshtml.h"
  7. class SimpleBrowser : public CWnd {
  8. public:
  9. // construction and creation
  10. SimpleBrowser();
  11. virtual ~SimpleBrowser();
  12. BOOL Create(DWORD dwStyle,
  13. const RECT& rect,
  14. CWnd* pParentWnd,
  15. UINT nID);
  16. // create browser directly
  17. BOOL CreateFromControl(CWnd *pParentWnd,UINT nID,DWORD dwStyle = WS_CHILD | WS_VISIBLE);
  18. // create browser in place of dialog control; the dialog control
  19. // identified by nID will be destroyed, and the browser will take
  20. // its place
  21. // controls
  22. void Navigate(LPCTSTR URL);
  23. // navigate to URL
  24. void Write(LPCTSTR string);
  25. // append string to current document; note that the WebBrowser control tolerates
  26. // poorly formed documents, like:
  27. // <html><body>....
  28. // --- no trailing body or html tags
  29. // <html><body>...</body></html><html><body>...</body></html>...
  30. // --- multiple documents
  31. void Clear();
  32. // clear current document
  33. void NavigateResource(int resource_ID);
  34. // navigate to HTML document resource
  35. void GoBack(); // navigate backward one item
  36. // in the history list
  37. void GoForward(); // navigate forward one item
  38. // in the history list
  39. void GoHome(); // navigate to current
  40. // home or start page
  41. void Refresh(); // refresh contents
  42. void Stop(); // stop current activity
  43. void Print(LPCTSTR header = _T("&w&b&b&p"),
  44. LPCTSTR footer = _T("&d &t"));
  45. // start printing contents; uses same 'metacharacters' for header and
  46. // footer as Internet Explorer; see IE Page Setup dialog
  47. void PrintPreview(); // print preview
  48. bool GetBusy(); // returns true if browser
  49. // busy downloading or other
  50. // activity
  51. CString GetLocationName(); // get name of location currently
  52. // being browsed (title, if HTML
  53. // page; UNC path if file)
  54. CString GetLocationURL(); // get URL of location currently
  55. // being browsed
  56. READYSTATE GetReadyState(); // get browser ready state
  57. bool GetSilent(); // get/set silent property
  58. void PutSilent(bool silent = false); // (if true, dialog and message
  59. // boxes may not be shown)
  60. IHTMLDocument2 *GetDocument(); // get document interface; returns NULL
  61. // if interface is not available
  62. // (which is the case if you've navigated to
  63. // something that's NOT an HTML document,
  64. // like an Excel spreadsheet, which the
  65. // WebBrowser control is perfectly willing
  66. // to host)
  67. // events (overridables)
  68. virtual bool OnBeforeNavigate2(CString URL,
  69. CString frame,
  70. void *post_data,int post_data_size,
  71. CString headers);
  72. // called before navigation begins; URL is destination, frame
  73. // is frame name ("" if none), post_data is HTTP POST data (NULL if none),
  74. // and headers are HTTP headers sent to server;
  75. // return true to cancel navigation, false to continue
  76. virtual void OnDocumentComplete(CString URL);
  77. // navigation to document complete; URL is location
  78. virtual void OnDownloadBegin();
  79. // navigation operation begins
  80. virtual void OnProgressChange(int progress,int progress_max);
  81. // navigation progress update
  82. virtual void OnDownloadComplete();
  83. // navigation operation completed
  84. virtual void OnNavigateComplete2(CString URL);
  85. // navigation to hyperlink complete; URL is location
  86. // (URL = string if NavigateString or NavigateResource are used)
  87. virtual void OnStatusTextChange(CString text);
  88. // status text has changed
  89. virtual void OnTitleChange(CString text);
  90. // title has changed
  91. virtual void OnPrintTemplateInstantiation();
  92. // print template has been instantiated (printing has begun)
  93. virtual void OnPrintTemplateTeardown();
  94. // print template is being destroyed (printing has completed)
  95. // notifications
  96. enum NotificationType { // Note: SimpleBrowser does NOT support the
  97. // common notifications (NM_CLICK, etc.)
  98. BeforeNavigate2, // set *LRESULT=TRUE to cancel navigation
  99. DocumentComplete,
  100. DownloadBegin,
  101. ProgressChange,
  102. DownloadComplete,
  103. NavigateComplete2,
  104. StatusTextChange,
  105. TitleChange,
  106. PrintTemplateInstantiation,
  107. PrintTemplateTeardown
  108. };
  109. class Notification { // all notifications pass this structure
  110. public:
  111. Notification(HWND hwnd,UINT ID,NotificationType type);
  112. ~Notification();
  113. NMHDR hdr; // hdr.hwndFrom = SimpleBrowser's HWND
  114. // hdr.idFrom = SimpleBrowser's control ID
  115. // hdr.code = <NavigationType>
  116. CString URL; // BeforeNavigate2
  117. // DocumentComplete
  118. // NavigateComplete2
  119. CString frame; // BeforeNavigate2
  120. void *post_data; // BeforeNavigate2
  121. int post_data_size;
  122. CString headers; // BeforeNavigate2
  123. int progress; // ProgressChange
  124. int progress_max; // ProgressChange
  125. CString text; // StatusTextChange
  126. // TitleChange
  127. };
  128. bool ParsePostData(CString post_data, // parse URL-encoded POST data
  129. CStringArray &names, // string into list of names
  130. CStringArray &values); // and values; returns true if
  131. // successful
  132. //{{AFX_VIRTUAL(SimpleBrowser)
  133. public:
  134. virtual void PostNcDestroy();
  135. //}}AFX_VIRTUAL
  136. protected:
  137. //{{AFX_MSG(SimpleBrowser)
  138. afx_msg void OnSize(UINT nType, int cx, int cy);
  139. afx_msg BOOL PreTranslateMessage(MSG *pMsg);
  140. //}}AFX_MSG
  141. void _OnBeforeNavigate2(LPDISPATCH lpDisp,
  142. VARIANT FAR *URL,
  143. VARIANT FAR *Flags,
  144. VARIANT FAR *TargetFrameName,
  145. VARIANT FAR *PostData,
  146. VARIANT FAR *Headers,
  147. VARIANT_BOOL *Cancel);
  148. void _OnDownloadBegin();
  149. void _OnProgressChange(long progress,long progress_max);
  150. void _OnDownloadComplete();
  151. void _OnDocumentComplete(LPDISPATCH lpDisp,VARIANT FAR* URL);
  152. void _OnNavigateComplete2(LPDISPATCH lpDisp,VARIANT FAR* URL);
  153. void _OnStatusTextChange(BSTR bstrText);
  154. void _OnTitleChange(BSTR bstrText);
  155. void _OnPrintTemplateInstantiation(LPDISPATCH pDisp);
  156. void _OnPrintTemplateTeardown(LPDISPATCH pDisp);
  157. DECLARE_MESSAGE_MAP()
  158. DECLARE_EVENTSINK_MAP()
  159. private:
  160. CWnd _BrowserWindow; // browser window
  161. protected:
  162. IWebBrowser2 *_Browser; // browser control
  163. bool _Ready; // document ready
  164. // (initial navigation completed)
  165. private:
  166. IDispatch *_BrowserDispatch; // browser control
  167. // dispatch interface
  168. CString _Content; // current content set via Write()/Clear()
  169. void _ContentWrite();
  170. };
  171. #endif