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