| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: htmlwin.h |
| 3 | // Purpose: wxHtmlWindow class for parsing & displaying HTML |
| 4 | // Author: Vaclav Slavik |
| 5 | // RCS-ID: $Id$ |
| 6 | // Copyright: (c) 1999 Vaclav Slavik |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | |
| 11 | #ifndef _WX_HTMLWIN_H_ |
| 12 | #define _WX_HTMLWIN_H_ |
| 13 | |
| 14 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 15 | #pragma interface "htmlwin.h" |
| 16 | #endif |
| 17 | |
| 18 | #include "wx/defs.h" |
| 19 | #if wxUSE_HTML |
| 20 | |
| 21 | #include "wx/window.h" |
| 22 | #include "wx/scrolwin.h" |
| 23 | #include "wx/config.h" |
| 24 | #include "wx/html/winpars.h" |
| 25 | #include "wx/html/htmlcell.h" |
| 26 | #include "wx/filesys.h" |
| 27 | #include "wx/html/htmlfilt.h" |
| 28 | #include "wx/filename.h" |
| 29 | #include "wx/bitmap.h" |
| 30 | |
| 31 | class wxHtmlProcessor; |
| 32 | class wxHtmlWinModule; |
| 33 | class wxHtmlHistoryArray; |
| 34 | class wxHtmlProcessorList; |
| 35 | class WXDLLIMPEXP_HTML wxHtmlWinAutoScrollTimer; |
| 36 | |
| 37 | |
| 38 | // wxHtmlWindow flags: |
| 39 | #define wxHW_SCROLLBAR_NEVER 0x0002 |
| 40 | #define wxHW_SCROLLBAR_AUTO 0x0004 |
| 41 | #define wxHW_NO_SELECTION 0x0008 |
| 42 | |
| 43 | #define wxHW_DEFAULT_STYLE wxHW_SCROLLBAR_AUTO |
| 44 | |
| 45 | |
| 46 | // enums for wxHtmlWindow::OnOpeningURL |
| 47 | enum wxHtmlOpeningStatus |
| 48 | { |
| 49 | wxHTML_OPEN, |
| 50 | wxHTML_BLOCK, |
| 51 | wxHTML_REDIRECT |
| 52 | }; |
| 53 | |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | // wxHtmlWindow |
| 56 | // (This is probably the only class you will directly use.) |
| 57 | // Purpose of this class is to display HTML page (either local |
| 58 | // file or downloaded via HTTP protocol) in a window. Width of |
| 59 | // window is constant - given in constructor - virtual height |
| 60 | // is changed dynamicly depending on page size. Once the |
| 61 | // window is created you can set it's content by calling |
| 62 | // SetPage(text) or LoadPage(filename). |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | |
| 65 | class WXDLLIMPEXP_HTML wxHtmlWindow : public wxScrolledWindow |
| 66 | { |
| 67 | DECLARE_DYNAMIC_CLASS(wxHtmlWindow) |
| 68 | friend class wxHtmlWinModule; |
| 69 | |
| 70 | public: |
| 71 | wxHtmlWindow() { Init(); } |
| 72 | wxHtmlWindow(wxWindow *parent, wxWindowID id = wxID_ANY, |
| 73 | const wxPoint& pos = wxDefaultPosition, |
| 74 | const wxSize& size = wxDefaultSize, |
| 75 | long style = wxHW_DEFAULT_STYLE, |
| 76 | const wxString& name = wxT("htmlWindow")) |
| 77 | { |
| 78 | Init(); |
| 79 | Create(parent, id, pos, size, style, name); |
| 80 | } |
| 81 | ~wxHtmlWindow(); |
| 82 | |
| 83 | bool Create(wxWindow *parent, wxWindowID id = wxID_ANY, |
| 84 | const wxPoint& pos = wxDefaultPosition, |
| 85 | const wxSize& size = wxDefaultSize, |
| 86 | long style = wxHW_SCROLLBAR_AUTO, |
| 87 | const wxString& name = wxT("htmlWindow")); |
| 88 | |
| 89 | // Set HTML page and display it. !! source is HTML document itself, |
| 90 | // it is NOT address/filename of HTML document. If you want to |
| 91 | // specify document location, use LoadPage() istead |
| 92 | // Return value : false if an error occured, true otherwise |
| 93 | bool SetPage(const wxString& source); |
| 94 | |
| 95 | // Append to current page |
| 96 | bool AppendToPage(const wxString& source); |
| 97 | |
| 98 | // Load HTML page from given location. Location can be either |
| 99 | // a) /usr/wxGTK2/docs/html/wx.htm |
| 100 | // b) http://www.somewhere.uk/document.htm |
| 101 | // c) ftp://ftp.somesite.cz/pub/something.htm |
| 102 | // In case there is no prefix (http:,ftp:), the method |
| 103 | // will try to find it itself (1. local file, then http or ftp) |
| 104 | // After the page is loaded, the method calls SetPage() to display it. |
| 105 | // Note : you can also use path relative to previously loaded page |
| 106 | // Return value : same as SetPage |
| 107 | virtual bool LoadPage(const wxString& location); |
| 108 | |
| 109 | // Loads HTML page from file |
| 110 | bool LoadFile(const wxFileName& filename); |
| 111 | |
| 112 | // Returns full location of opened page |
| 113 | wxString GetOpenedPage() const {return m_OpenedPage;} |
| 114 | // Returns anchor within opened page |
| 115 | wxString GetOpenedAnchor() const {return m_OpenedAnchor;} |
| 116 | // Returns <TITLE> of opened page or empty string otherwise |
| 117 | wxString GetOpenedPageTitle() const {return m_OpenedPageTitle;} |
| 118 | |
| 119 | // Sets frame in which page title will be displayed. Format is format of |
| 120 | // frame title, e.g. "HtmlHelp : %s". It must contain exactly one %s |
| 121 | void SetRelatedFrame(wxFrame* frame, const wxString& format); |
| 122 | wxFrame* GetRelatedFrame() const {return m_RelatedFrame;} |
| 123 | |
| 124 | #if wxUSE_STATUSBAR |
| 125 | // After(!) calling SetRelatedFrame, this sets statusbar slot where messages |
| 126 | // will be displayed. Default is -1 = no messages. |
| 127 | void SetRelatedStatusBar(int bar); |
| 128 | #endif // wxUSE_STATUSBAR |
| 129 | |
| 130 | // Sets fonts to be used when displaying HTML page. |
| 131 | void SetFonts(wxString normal_face, wxString fixed_face, |
| 132 | const int *sizes = NULL); |
| 133 | |
| 134 | // Sets font sizes to be relative to the given size or the system |
| 135 | // default size; use either specified or default font |
| 136 | void SetStandardFonts(int size = -1, |
| 137 | const wxString& normal_face = wxEmptyString, |
| 138 | const wxString& fixed_face = wxEmptyString); |
| 139 | |
| 140 | // Sets space between text and window borders. |
| 141 | void SetBorders(int b) {m_Borders = b;} |
| 142 | |
| 143 | // Sets the bitmap to use for background (currnetly it will be tiled, |
| 144 | // when/if we have CSS support we could add other possibilities...) |
| 145 | void SetBackgroundImage(const wxBitmap& bmpBg) { m_bmpBg = bmpBg; } |
| 146 | |
| 147 | // Saves custom settings into cfg config. it will use the path 'path' |
| 148 | // if given, otherwise it will save info into currently selected path. |
| 149 | // saved values : things set by SetFonts, SetBorders. |
| 150 | virtual void ReadCustomization(wxConfigBase *cfg, wxString path = wxEmptyString); |
| 151 | // ... |
| 152 | virtual void WriteCustomization(wxConfigBase *cfg, wxString path = wxEmptyString); |
| 153 | |
| 154 | // Goes to previous/next page (in browsing history) |
| 155 | // Returns true if successful, false otherwise |
| 156 | bool HistoryBack(); |
| 157 | bool HistoryForward(); |
| 158 | bool HistoryCanBack(); |
| 159 | bool HistoryCanForward(); |
| 160 | // Resets history |
| 161 | void HistoryClear(); |
| 162 | |
| 163 | // Returns pointer to conteiners/cells structure. |
| 164 | // It should be used ONLY when printing |
| 165 | wxHtmlContainerCell* GetInternalRepresentation() const {return m_Cell;} |
| 166 | |
| 167 | // Adds input filter |
| 168 | static void AddFilter(wxHtmlFilter *filter); |
| 169 | |
| 170 | // Returns a pointer to the parser. |
| 171 | wxHtmlWinParser *GetParser() const { return m_Parser; } |
| 172 | |
| 173 | // Adds HTML processor to this instance of wxHtmlWindow: |
| 174 | void AddProcessor(wxHtmlProcessor *processor); |
| 175 | // Adds HTML processor to wxHtmlWindow class as whole: |
| 176 | static void AddGlobalProcessor(wxHtmlProcessor *processor); |
| 177 | |
| 178 | |
| 179 | // -- Callbacks -- |
| 180 | |
| 181 | // Sets the title of the window |
| 182 | // (depending on the information passed to SetRelatedFrame() method) |
| 183 | virtual void OnSetTitle(const wxString& title); |
| 184 | |
| 185 | // Called when the mouse hovers over a cell: (x, y) are logical coords |
| 186 | // Default behaviour is to do nothing at all |
| 187 | virtual void OnCellMouseHover(wxHtmlCell *cell, wxCoord x, wxCoord y); |
| 188 | |
| 189 | // Called when user clicks on a cell. Default behavior is to call |
| 190 | // OnLinkClicked() if this cell corresponds to a hypertext link |
| 191 | virtual void OnCellClicked(wxHtmlCell *cell, |
| 192 | wxCoord x, wxCoord y, |
| 193 | const wxMouseEvent& event); |
| 194 | |
| 195 | // Called when user clicked on hypertext link. Default behavior is to |
| 196 | // call LoadPage(loc) |
| 197 | virtual void OnLinkClicked(const wxHtmlLinkInfo& link); |
| 198 | |
| 199 | // Called when wxHtmlWindow wants to fetch data from an URL (e.g. when |
| 200 | // loading a page or loading an image). The data are downloaded if and only if |
| 201 | // OnOpeningURL returns true. If OnOpeningURL returns wxHTML_REDIRECT, |
| 202 | // it must set *redirect to the new URL |
| 203 | virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type), |
| 204 | const wxString& WXUNUSED(url), |
| 205 | wxString *WXUNUSED(redirect)) const |
| 206 | { return wxHTML_OPEN; } |
| 207 | |
| 208 | #if wxUSE_CLIPBOARD |
| 209 | // Helper functions to select parts of page: |
| 210 | void SelectWord(const wxPoint& pos); |
| 211 | void SelectLine(const wxPoint& pos); |
| 212 | void SelectAll(); |
| 213 | |
| 214 | // Convert selection to text: |
| 215 | wxString SelectionToText() { return DoSelectionToText(m_selection); } |
| 216 | |
| 217 | // Converts current page to text: |
| 218 | wxString ToText(); |
| 219 | #endif // wxUSE_CLIPBOARD |
| 220 | |
| 221 | protected: |
| 222 | void Init(); |
| 223 | |
| 224 | // Scrolls to anchor of this name. (Anchor is #news |
| 225 | // or #features etc. it is part of address sometimes: |
| 226 | // http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/index.html#news) |
| 227 | // Return value : true if anchor exists, false otherwise |
| 228 | bool ScrollToAnchor(const wxString& anchor); |
| 229 | |
| 230 | // Prepares layout (= fill m_PosX, m_PosY for fragments) based on |
| 231 | // actual size of window. This method also setup scrollbars |
| 232 | void CreateLayout(); |
| 233 | |
| 234 | void OnEraseBackground(wxEraseEvent& event); |
| 235 | void OnPaint(wxPaintEvent& event); |
| 236 | void OnSize(wxSizeEvent& event); |
| 237 | void OnMouseMove(wxMouseEvent& event); |
| 238 | void OnMouseDown(wxMouseEvent& event); |
| 239 | void OnMouseUp(wxMouseEvent& event); |
| 240 | #if wxUSE_CLIPBOARD |
| 241 | void OnKeyUp(wxKeyEvent& event); |
| 242 | void OnDoubleClick(wxMouseEvent& event); |
| 243 | void OnCopy(wxCommandEvent& event); |
| 244 | void OnMouseEnter(wxMouseEvent& event); |
| 245 | void OnMouseLeave(wxMouseEvent& event); |
| 246 | #endif // wxUSE_CLIPBOARD |
| 247 | |
| 248 | virtual void OnInternalIdle(); |
| 249 | |
| 250 | // Returns new filter (will be stored into m_DefaultFilter variable) |
| 251 | virtual wxHtmlFilter *GetDefaultFilter() {return new wxHtmlFilterPlainText;} |
| 252 | |
| 253 | // cleans static variables |
| 254 | static void CleanUpStatics(); |
| 255 | |
| 256 | // Returns true if text selection is enabled (wxClipboard must be available |
| 257 | // and wxHW_NO_SELECTION not used) |
| 258 | bool IsSelectionEnabled() const; |
| 259 | |
| 260 | enum ClipboardType |
| 261 | { |
| 262 | Primary, |
| 263 | Secondary |
| 264 | }; |
| 265 | |
| 266 | // Copies selection to clipboard if the clipboard support is available |
| 267 | // |
| 268 | // returns true if anything was copied to clipboard, false otherwise |
| 269 | bool CopySelection(ClipboardType t = Secondary); |
| 270 | |
| 271 | #if wxUSE_CLIPBOARD |
| 272 | // Automatic scrolling during selection: |
| 273 | void StopAutoScrolling(); |
| 274 | #endif // wxUSE_CLIPBOARD |
| 275 | |
| 276 | protected: |
| 277 | wxString DoSelectionToText(wxHtmlSelection *sel); |
| 278 | |
| 279 | // This is pointer to the first cell in parsed data. (Note: the first cell |
| 280 | // is usually top one = all other cells are sub-cells of this one) |
| 281 | wxHtmlContainerCell *m_Cell; |
| 282 | // parser which is used to parse HTML input. |
| 283 | // Each wxHtmlWindow has it's own parser because sharing one global |
| 284 | // parser would be problematic (because of reentrancy) |
| 285 | wxHtmlWinParser *m_Parser; |
| 286 | // contains name of actualy opened page or empty string if no page opened |
| 287 | wxString m_OpenedPage; |
| 288 | // contains name of current anchor within m_OpenedPage |
| 289 | wxString m_OpenedAnchor; |
| 290 | // contains title of actualy opened page or empty string if no <TITLE> tag |
| 291 | wxString m_OpenedPageTitle; |
| 292 | // class for opening files (file system) |
| 293 | wxFileSystem* m_FS; |
| 294 | |
| 295 | wxFrame *m_RelatedFrame; |
| 296 | wxString m_TitleFormat; |
| 297 | #if wxUSE_STATUSBAR |
| 298 | // frame in which page title should be displayed & number of it's statusbar |
| 299 | // reserved for usage with this html window |
| 300 | int m_RelatedStatusBar; |
| 301 | #endif // wxUSE_STATUSBAR |
| 302 | |
| 303 | // borders (free space between text and window borders) |
| 304 | // defaults to 10 pixels. |
| 305 | int m_Borders; |
| 306 | |
| 307 | int m_Style; |
| 308 | |
| 309 | // current text selection or NULL |
| 310 | wxHtmlSelection *m_selection; |
| 311 | |
| 312 | // true if the user is dragging mouse to select text |
| 313 | bool m_makingSelection; |
| 314 | |
| 315 | #if wxUSE_CLIPBOARD |
| 316 | // time of the last doubleclick event, used to detect tripleclicks |
| 317 | // (tripleclicks are used to select whole line): |
| 318 | wxLongLong m_lastDoubleClick; |
| 319 | |
| 320 | // helper class to automatically scroll the window if the user is selecting |
| 321 | // text and the mouse leaves wxHtmlWindow: |
| 322 | wxHtmlWinAutoScrollTimer *m_timerAutoScroll; |
| 323 | #endif // wxUSE_CLIPBOARD |
| 324 | |
| 325 | private: |
| 326 | // window content for double buffered rendering: |
| 327 | wxBitmap *m_backBuffer; |
| 328 | |
| 329 | // background image, may be invalid |
| 330 | wxBitmap m_bmpBg; |
| 331 | |
| 332 | // variables used when user is selecting text |
| 333 | wxPoint m_tmpSelFromPos; |
| 334 | wxHtmlCell *m_tmpSelFromCell; |
| 335 | |
| 336 | // a flag indicated if mouse moved |
| 337 | // (if true we will try to change cursor in last call to OnIdle) |
| 338 | bool m_tmpMouseMoved; |
| 339 | // contains last link name |
| 340 | wxHtmlLinkInfo *m_tmpLastLink; |
| 341 | // contains the last (terminal) cell which contained the mouse |
| 342 | wxHtmlCell *m_tmpLastCell; |
| 343 | // if >0 contents of the window is not redrawn |
| 344 | // (in order to avoid ugly blinking) |
| 345 | int m_tmpCanDrawLocks; |
| 346 | |
| 347 | // list of HTML filters |
| 348 | static wxList m_Filters; |
| 349 | // this filter is used when no filter is able to read some file |
| 350 | static wxHtmlFilter *m_DefaultFilter; |
| 351 | |
| 352 | wxHtmlHistoryArray *m_History; |
| 353 | // browser history |
| 354 | int m_HistoryPos; |
| 355 | // if this FLAG is false, items are not added to history |
| 356 | bool m_HistoryOn; |
| 357 | |
| 358 | // html processors array: |
| 359 | wxHtmlProcessorList *m_Processors; |
| 360 | static wxHtmlProcessorList *m_GlobalProcessors; |
| 361 | |
| 362 | DECLARE_EVENT_TABLE() |
| 363 | DECLARE_NO_COPY_CLASS(wxHtmlWindow) |
| 364 | }; |
| 365 | |
| 366 | |
| 367 | #endif // wxUSE_HTML |
| 368 | |
| 369 | #endif // _WX_HTMLWIN_H_ |
| 370 | |