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