SimpleBrowser.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. void Copy();
  96. // notifications
  97. enum NotificationType { // Note: SimpleBrowser does NOT support the
  98. // common notifications (NM_CLICK, etc.)
  99. BeforeNavigate2, // set *LRESULT=TRUE to cancel navigation
  100. DocumentComplete,
  101. DownloadBegin,
  102. ProgressChange,
  103. DownloadComplete,
  104. NavigateComplete2,
  105. StatusTextChange,
  106. TitleChange,
  107. PrintTemplateInstantiation,
  108. PrintTemplateTeardown
  109. };
  110. class Notification { // all notifications pass this structure
  111. public:
  112. Notification(HWND hwnd,UINT ID,NotificationType type);
  113. ~Notification();
  114. NMHDR hdr; // hdr.hwndFrom = SimpleBrowser's HWND
  115. // hdr.idFrom = SimpleBrowser's control ID
  116. // hdr.code = <NavigationType>
  117. CString URL; // BeforeNavigate2
  118. // DocumentComplete
  119. // NavigateComplete2
  120. CString frame; // BeforeNavigate2
  121. void *post_data; // BeforeNavigate2
  122. int post_data_size;
  123. CString headers; // BeforeNavigate2
  124. int progress; // ProgressChange
  125. int progress_max; // ProgressChange
  126. CString text; // StatusTextChange
  127. // TitleChange
  128. };
  129. bool ParsePostData(CString post_data, // parse URL-encoded POST data
  130. CStringArray &names, // string into list of names
  131. CStringArray &values); // and values; returns true if
  132. // successful
  133. //{{AFX_VIRTUAL(SimpleBrowser)
  134. public:
  135. virtual void PostNcDestroy();
  136. //}}AFX_VIRTUAL
  137. protected:
  138. //{{AFX_MSG(SimpleBrowser)
  139. afx_msg void OnSize(UINT nType, int cx, int cy);
  140. afx_msg BOOL PreTranslateMessage(MSG *pMsg);
  141. //}}AFX_MSG
  142. void _OnBeforeNavigate2(LPDISPATCH lpDisp,
  143. VARIANT FAR *URL,
  144. VARIANT FAR *Flags,
  145. VARIANT FAR *TargetFrameName,
  146. VARIANT FAR *PostData,
  147. VARIANT FAR *Headers,
  148. VARIANT_BOOL *Cancel);
  149. void _OnDownloadBegin();
  150. void _OnProgressChange(long progress,long progress_max);
  151. void _OnDownloadComplete();
  152. void _OnDocumentComplete(LPDISPATCH lpDisp,VARIANT FAR* URL);
  153. void _OnNavigateComplete2(LPDISPATCH lpDisp,VARIANT FAR* URL);
  154. void _OnStatusTextChange(BSTR bstrText);
  155. void _OnTitleChange(BSTR bstrText);
  156. void _OnPrintTemplateInstantiation(LPDISPATCH pDisp);
  157. void _OnPrintTemplateTeardown(LPDISPATCH pDisp);
  158. DECLARE_MESSAGE_MAP()
  159. DECLARE_EVENTSINK_MAP()
  160. private:
  161. CWnd _BrowserWindow; // browser window
  162. protected:
  163. IWebBrowser2 *_Browser; // browser control
  164. bool _Ready; // document ready
  165. // (initial navigation completed)
  166. private:
  167. IDispatch *_BrowserDispatch; // browser control
  168. // dispatch interface
  169. CString _Content; // current content set via Write()/Clear()
  170. void _ContentWrite();
  171. };
  172. #endif