]> git.saurik.com Git - wxWidgets.git/blame - include/wx/html/htmlwin.h
Steps towards implementing native-style, non-top-level toolbars on Mac
[wxWidgets.git] / include / wx / html / htmlwin.h
CommitLineData
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 27class wxHtmlProcessor;
892aeafc
VS
28class wxHtmlWinModule;
29class wxHtmlHistoryArray;
30class wxHtmlProcessorList;
6acba9a7 31class 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
bc55e31b 41/// Enum for wxHtmlWindow::OnOpeningURL and wxHtmlWindowInterface::OnOpeningURL
6cc4e6b8
VS
42enum wxHtmlOpeningStatus
43{
bc55e31b 44 /// Open the requested URL
6cc4e6b8 45 wxHTML_OPEN,
bc55e31b 46 /// Do not open the URL
6cc4e6b8 47 wxHTML_BLOCK,
bc55e31b 48 /// Redirect to another URL (returned from OnOpeningURL)
6cc4e6b8
VS
49 wxHTML_REDIRECT
50};
51
bc55e31b
VS
52/**
53 Abstract interface to a HTML rendering window (such as wxHtmlWindow or
54 wxHtmlListBox) that is passed to wxHtmlWinParser. It encapsulates all
55 communication from the parser to the window.
56 */
57class WXDLLIMPEXP_HTML wxHtmlWindowInterface
58{
59public:
60 /// Ctor
61 wxHtmlWindowInterface() {}
62 virtual ~wxHtmlWindowInterface() {}
63
64 /**
65 Called by the parser to set window's title to given text.
66 */
67 virtual void SetHTMLWindowTitle(const wxString& title) = 0;
68
69 /**
70 Called when a link is clicked.
71
72 @param link information about the clicked link
73 */
74 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo& link) = 0;
75
76 /**
77 Called when the parser needs to open another URL (e.g. an image).
78
79 @param type Type of the URL request (e.g. image)
80 @param url URL the parser wants to open
81 @param redirect If the return value is wxHTML_REDIRECT, then the
82 URL to redirect to will be stored in this variable
83 (the pointer must never be NULL)
84
85 @return indicator of how to treat the request
86 */
87 virtual wxHtmlOpeningStatus OnHTMLOpeningURL(wxHtmlURLType type,
88 const wxString& url,
89 wxString *redirect) const = 0;
90
91 /**
92 Converts coordinates @a pos relative to given @a cell to
93 physical coordinates in the window.
94 */
95 virtual wxPoint HTMLCoordsToWindow(wxHtmlCell *cell,
96 const wxPoint& pos) const = 0;
97
98 /// Returns the window used for rendering (may be NULL).
99 virtual wxWindow* GetHTMLWindow() = 0;
100
101 /// Returns background colour to use by default.
102 virtual wxColour GetHTMLBackgroundColour() const = 0;
103
104 /// Sets window's background to colour @a clr.
105 virtual void SetHTMLBackgroundColour(const wxColour& clr) = 0;
106
107 /// Sets window's background to given bitmap.
108 virtual void SetHTMLBackgroundImage(const wxBitmap& bmpBg) = 0;
109
110 /// Sets status bar text.
111 virtual void SetHTMLStatusText(const wxString& text) = 0;
112};
113
114/**
115 Helper class that implements part of mouse handling for wxHtmlWindow and
116 wxHtmlListBox. Cursor changes and clicking on links are handled, text
117 selection is not.
118 */
119class WXDLLIMPEXP_HTML wxHtmlWindowMouseHelper
120{
121public:
122 /**
123 Ctor.
124
125 @param iface Interface to the owner window.
126 */
127 wxHtmlWindowMouseHelper(wxHtmlWindowInterface *iface);
128
129 /// Returns true if the mouse moved since the last call to HandleIdle
130 bool DidMouseMove() const { return m_tmpMouseMoved; }
131
132 /// Call this from EVT_MOTION event handler
133 void HandleMouseMoved();
134
135 /**
136 Call this from EVT_LEFT_UP handler (or, alternatively, EVT_LEFT_DOWN).
137
138 @param rootCell HTML cell inside which the click occured. This doesn't
139 have to be the leaf cell, it can be e.g. toplevel
140 container, but the mouse must be inside the container's
141 area, otherwise the event would be ignored.
142 @param pos Mouse position in coordinates relative to @a cell
143 @param event The event that triggered the call
144 */
145 bool HandleMouseClick(wxHtmlCell *rootCell,
146 const wxPoint& pos, const wxMouseEvent& event);
147
148 /**
149 Call this from OnInternalIdle of the HTML displaying window. Handles
150 mouse movements and must be used together with HandleMouseMoved.
151
152 @param rootCell HTML cell inside which the click occured. This doesn't
153 have to be the leaf cell, it can be e.g. toplevel
154 container, but the mouse must be inside the container's
155 area, otherwise the event would be ignored.
156 @param pos Current mouse position in coordinates relative to
157 @a cell
158 */
159 void HandleIdle(wxHtmlCell *rootCell, const wxPoint& pos);
160
161 /**
162 Called by HandleIdle when the mouse hovers over a cell. Default
163 behaviour is to do nothing.
164
165 @param cell the cell the mouse is over
166 @param x, y coordinates of mouse relative to the cell
167 */
168 virtual void OnCellMouseHover(wxHtmlCell *cell, wxCoord x, wxCoord y);
169
170 /**
171 Called by HandleMouseClick when the user clicks on a cell.
172 Default behavior is to call wxHtmlWindowInterface::OnLinkClicked()
173 if this cell corresponds to a hypertext link.
174
175 @param cell the cell the mouse is over
176 @param x, y coordinates of mouse relative to the cell
177 @param event The event that triggered the call
178
179
180 @return true if a link was clicked, false otherwise.
181 */
182 virtual bool OnCellClicked(wxHtmlCell *cell,
183 wxCoord x, wxCoord y,
184 const wxMouseEvent& event);
185
186protected:
187 // this flag indicates if the mouse moved (used by HandleIdle)
188 bool m_tmpMouseMoved;
189 // contains last link name
190 wxHtmlLinkInfo *m_tmpLastLink;
191 // contains the last (terminal) cell which contained the mouse
192 wxHtmlCell *m_tmpLastCell;
193
194private:
195 wxHtmlWindowInterface *m_interface;
196};
197
f65a786f 198// ----------------------------------------------------------------------------
5526e819 199// wxHtmlWindow
3ef01ce5 200// (This is probably the only class you will directly use.)
5526e819 201// Purpose of this class is to display HTML page (either local
f65a786f
VS
202// file or downloaded via HTTP protocol) in a window. Width of
203// window is constant - given in constructor - virtual height
204// is changed dynamicly depending on page size. Once the
205// window is created you can set it's content by calling
3ef01ce5 206// SetPage(text) or LoadPage(filename).
f65a786f 207// ----------------------------------------------------------------------------
5526e819 208
bc55e31b
VS
209class WXDLLIMPEXP_HTML wxHtmlWindow : public wxScrolledWindow,
210 public wxHtmlWindowInterface,
211 private wxHtmlWindowMouseHelper
5526e819
VS
212{
213 DECLARE_DYNAMIC_CLASS(wxHtmlWindow)
66806a0b 214 friend class wxHtmlWinModule;
5526e819 215
97494971 216public:
bc55e31b 217 wxHtmlWindow() : wxHtmlWindowMouseHelper(this) { Init(); }
6953da00 218 wxHtmlWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
bfb9ee96 219 const wxPoint& pos = wxDefaultPosition,
97494971 220 const wxSize& size = wxDefaultSize,
f65a786f 221 long style = wxHW_DEFAULT_STYLE,
4f417130 222 const wxString& name = wxT("htmlWindow"))
bc55e31b 223 : wxHtmlWindowMouseHelper(this)
4f417130
VS
224 {
225 Init();
226 Create(parent, id, pos, size, style, name);
227 }
97494971
VS
228 ~wxHtmlWindow();
229
6953da00 230 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
4f417130
VS
231 const wxPoint& pos = wxDefaultPosition,
232 const wxSize& size = wxDefaultSize,
233 long style = wxHW_SCROLLBAR_AUTO,
234 const wxString& name = wxT("htmlWindow"));
235
97494971
VS
236 // Set HTML page and display it. !! source is HTML document itself,
237 // it is NOT address/filename of HTML document. If you want to
238 // specify document location, use LoadPage() istead
3103e8a9 239 // Return value : false if an error occurred, true otherwise
04f109ec 240 virtual bool SetPage(const wxString& source);
574c939e 241
39029898
VS
242 // Append to current page
243 bool AppendToPage(const wxString& source);
97494971
VS
244
245 // Load HTML page from given location. Location can be either
246 // a) /usr/wxGTK2/docs/html/wx.htm
247 // b) http://www.somewhere.uk/document.htm
248 // c) ftp://ftp.somesite.cz/pub/something.htm
249 // In case there is no prefix (http:,ftp:), the method
250 // will try to find it itself (1. local file, then http or ftp)
251 // After the page is loaded, the method calls SetPage() to display it.
252 // Note : you can also use path relative to previously loaded page
253 // Return value : same as SetPage
38caaa61 254 virtual bool LoadPage(const wxString& location);
97494971 255
903972f9
VS
256 // Loads HTML page from file
257 bool LoadFile(const wxFileName& filename);
258
97494971
VS
259 // Returns full location of opened page
260 wxString GetOpenedPage() const {return m_OpenedPage;}
261 // Returns anchor within opened page
262 wxString GetOpenedAnchor() const {return m_OpenedAnchor;}
263 // Returns <TITLE> of opened page or empty string otherwise
264 wxString GetOpenedPageTitle() const {return m_OpenedPageTitle;}
265
266 // Sets frame in which page title will be displayed. Format is format of
267 // frame title, e.g. "HtmlHelp : %s". It must contain exactly one %s
268 void SetRelatedFrame(wxFrame* frame, const wxString& format);
269 wxFrame* GetRelatedFrame() const {return m_RelatedFrame;}
270
67a99992 271#if wxUSE_STATUSBAR
97494971
VS
272 // After(!) calling SetRelatedFrame, this sets statusbar slot where messages
273 // will be displayed. Default is -1 = no messages.
274 void SetRelatedStatusBar(int bar);
67a99992 275#endif // wxUSE_STATUSBAR
97494971
VS
276
277 // Sets fonts to be used when displaying HTML page.
fbfb8bcc 278 void SetFonts(const wxString& normal_face, const wxString& fixed_face,
4eecf115 279 const int *sizes = NULL);
97494971 280
10e5c7ea
VS
281 // Sets font sizes to be relative to the given size or the system
282 // default size; use either specified or default font
283 void SetStandardFonts(int size = -1,
284 const wxString& normal_face = wxEmptyString,
285 const wxString& fixed_face = wxEmptyString);
6953da00 286
97494971
VS
287 // Sets space between text and window borders.
288 void SetBorders(int b) {m_Borders = b;}
289
97e490f8
VZ
290 // Sets the bitmap to use for background (currnetly it will be tiled,
291 // when/if we have CSS support we could add other possibilities...)
292 void SetBackgroundImage(const wxBitmap& bmpBg) { m_bmpBg = bmpBg; }
293
97494971
VS
294 // Saves custom settings into cfg config. it will use the path 'path'
295 // if given, otherwise it will save info into currently selected path.
296 // saved values : things set by SetFonts, SetBorders.
297 virtual void ReadCustomization(wxConfigBase *cfg, wxString path = wxEmptyString);
298 // ...
299 virtual void WriteCustomization(wxConfigBase *cfg, wxString path = wxEmptyString);
300
301 // Goes to previous/next page (in browsing history)
6953da00 302 // Returns true if successful, false otherwise
97494971
VS
303 bool HistoryBack();
304 bool HistoryForward();
305 bool HistoryCanBack();
306 bool HistoryCanForward();
307 // Resets history
308 void HistoryClear();
309
310 // Returns pointer to conteiners/cells structure.
311 // It should be used ONLY when printing
312 wxHtmlContainerCell* GetInternalRepresentation() const {return m_Cell;}
313
314 // Adds input filter
315 static void AddFilter(wxHtmlFilter *filter);
316
6cc4e6b8
VS
317 // Returns a pointer to the parser.
318 wxHtmlWinParser *GetParser() const { return m_Parser; }
319
320 // Adds HTML processor to this instance of wxHtmlWindow:
321 void AddProcessor(wxHtmlProcessor *processor);
322 // Adds HTML processor to wxHtmlWindow class as whole:
323 static void AddGlobalProcessor(wxHtmlProcessor *processor);
324
97e490f8 325
6cc4e6b8
VS
326 // -- Callbacks --
327
328 // Sets the title of the window
329 // (depending on the information passed to SetRelatedFrame() method)
330 virtual void OnSetTitle(const wxString& title);
331
f6010d8f 332 // Called when user clicked on hypertext link. Default behavior is to
97494971
VS
333 // call LoadPage(loc)
334 virtual void OnLinkClicked(const wxHtmlLinkInfo& link);
574c939e
KB
335
336 // Called when wxHtmlWindow wants to fetch data from an URL (e.g. when
337 // loading a page or loading an image). The data are downloaded if and only if
6953da00 338 // OnOpeningURL returns true. If OnOpeningURL returns wxHTML_REDIRECT,
6cc4e6b8 339 // it must set *redirect to the new URL
574c939e
KB
340 virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type),
341 const wxString& WXUNUSED(url),
342 wxString *WXUNUSED(redirect)) const
6cc4e6b8 343 { return wxHTML_OPEN; }
6953da00 344
15622068
VZ
345#if wxUSE_CLIPBOARD
346 // Helper functions to select parts of page:
347 void SelectWord(const wxPoint& pos);
348 void SelectLine(const wxPoint& pos);
349 void SelectAll();
6953da00 350
5d3f80be
VS
351 // Convert selection to text:
352 wxString SelectionToText() { return DoSelectionToText(m_selection); }
353
354 // Converts current page to text:
355 wxString ToText();
97e490f8 356#endif // wxUSE_CLIPBOARD
6953da00 357
6f02a879
VZ
358 virtual void OnInternalIdle();
359
bc55e31b 360
97494971 361protected:
4f417130
VS
362 void Init();
363
97494971
VS
364 // Scrolls to anchor of this name. (Anchor is #news
365 // or #features etc. it is part of address sometimes:
366 // http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/index.html#news)
6953da00 367 // Return value : true if anchor exists, false otherwise
97494971
VS
368 bool ScrollToAnchor(const wxString& anchor);
369
bfb9ee96 370 // Prepares layout (= fill m_PosX, m_PosY for fragments) based on
97494971
VS
371 // actual size of window. This method also setup scrollbars
372 void CreateLayout();
373
1338c59a
VS
374 void OnEraseBackground(wxEraseEvent& event);
375 void OnPaint(wxPaintEvent& event);
97494971 376 void OnSize(wxSizeEvent& event);
31d8b4ad 377 void OnMouseMove(wxMouseEvent& event);
adf2eb2d
VS
378 void OnMouseDown(wxMouseEvent& event);
379 void OnMouseUp(wxMouseEvent& event);
61233023
VS
380#if wxUSE_CLIPBOARD
381 void OnKeyUp(wxKeyEvent& event);
31eefb99 382 void OnDoubleClick(wxMouseEvent& event);
61233023 383 void OnCopy(wxCommandEvent& event);
1338c59a
VS
384 void OnMouseEnter(wxMouseEvent& event);
385 void OnMouseLeave(wxMouseEvent& event);
d659d703 386#endif // wxUSE_CLIPBOARD
97494971
VS
387
388 // Returns new filter (will be stored into m_DefaultFilter variable)
389 virtual wxHtmlFilter *GetDefaultFilter() {return new wxHtmlFilterPlainText;}
390
892aeafc 391 // cleans static variables
97494971 392 static void CleanUpStatics();
97494971 393
f65a786f
VS
394 // Returns true if text selection is enabled (wxClipboard must be available
395 // and wxHW_NO_SELECTION not used)
396 bool IsSelectionEnabled() const;
397
61233023
VS
398 enum ClipboardType
399 {
400 Primary,
401 Secondary
402 };
d659d703
VZ
403
404 // Copies selection to clipboard if the clipboard support is available
4bdce112
VZ
405 //
406 // returns true if anything was copied to clipboard, false otherwise
5de65c69 407 bool CopySelection(ClipboardType t = Secondary);
0994d968 408
d659d703 409#if wxUSE_CLIPBOARD
0994d968 410 // Automatic scrolling during selection:
1338c59a 411 void StopAutoScrolling();
d659d703 412#endif // wxUSE_CLIPBOARD
61233023 413
977b867e 414 wxString DoSelectionToText(wxHtmlSelection *sel);
6953da00 415
73de5077 416private:
bc55e31b
VS
417 // wxHtmlWindowInterface methods:
418 virtual void SetHTMLWindowTitle(const wxString& title);
419 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo& link);
420 virtual wxHtmlOpeningStatus OnHTMLOpeningURL(wxHtmlURLType type,
421 const wxString& url,
422 wxString *redirect) const;
423 virtual wxPoint HTMLCoordsToWindow(wxHtmlCell *cell,
424 const wxPoint& pos) const;
425 virtual wxWindow* GetHTMLWindow();
426 virtual wxColour GetHTMLBackgroundColour() const;
427 virtual void SetHTMLBackgroundColour(const wxColour& clr);
428 virtual void SetHTMLBackgroundImage(const wxBitmap& bmpBg);
429 virtual void SetHTMLStatusText(const wxString& text);
430
73de5077
VS
431 // implementation of SetPage()
432 bool DoSetPage(const wxString& source);
433
434protected:
1338c59a
VS
435 // This is pointer to the first cell in parsed data. (Note: the first cell
436 // is usually top one = all other cells are sub-cells of this one)
97494971 437 wxHtmlContainerCell *m_Cell;
892aeafc
VS
438 // parser which is used to parse HTML input.
439 // Each wxHtmlWindow has it's own parser because sharing one global
440 // parser would be problematic (because of reentrancy)
97494971 441 wxHtmlWinParser *m_Parser;
892aeafc 442 // contains name of actualy opened page or empty string if no page opened
97494971 443 wxString m_OpenedPage;
892aeafc 444 // contains name of current anchor within m_OpenedPage
97494971 445 wxString m_OpenedAnchor;
892aeafc 446 // contains title of actualy opened page or empty string if no <TITLE> tag
97494971 447 wxString m_OpenedPageTitle;
892aeafc 448 // class for opening files (file system)
97494971 449 wxFileSystem* m_FS;
97494971
VS
450
451 wxFrame *m_RelatedFrame;
452 wxString m_TitleFormat;
67a99992 453#if wxUSE_STATUSBAR
892aeafc
VS
454 // frame in which page title should be displayed & number of it's statusbar
455 // reserved for usage with this html window
97494971 456 int m_RelatedStatusBar;
67a99992 457#endif // wxUSE_STATUSBAR
97494971 458
892aeafc
VS
459 // borders (free space between text and window borders)
460 // defaults to 10 pixels.
97494971 461 int m_Borders;
97494971
VS
462
463 int m_Style;
464
adf2eb2d
VS
465 // current text selection or NULL
466 wxHtmlSelection *m_selection;
467
468 // true if the user is dragging mouse to select text
469 bool m_makingSelection;
470
1338c59a 471#if wxUSE_CLIPBOARD
0994d968
VS
472 // time of the last doubleclick event, used to detect tripleclicks
473 // (tripleclicks are used to select whole line):
33f81fc7 474 wxMilliClock_t m_lastDoubleClick;
0994d968
VS
475
476 // helper class to automatically scroll the window if the user is selecting
477 // text and the mouse leaves wxHtmlWindow:
1338c59a 478 wxHtmlWinAutoScrollTimer *m_timerAutoScroll;
d659d703 479#endif // wxUSE_CLIPBOARD
1338c59a 480
97494971 481private:
1338c59a
VS
482 // window content for double buffered rendering:
483 wxBitmap *m_backBuffer;
d659d703 484
97e490f8
VZ
485 // background image, may be invalid
486 wxBitmap m_bmpBg;
487
adf2eb2d
VS
488 // variables used when user is selecting text
489 wxPoint m_tmpSelFromPos;
490 wxHtmlCell *m_tmpSelFromCell;
d659d703 491
892aeafc
VS
492 // if >0 contents of the window is not redrawn
493 // (in order to avoid ugly blinking)
97494971 494 int m_tmpCanDrawLocks;
97494971 495
892aeafc 496 // list of HTML filters
97494971 497 static wxList m_Filters;
892aeafc 498 // this filter is used when no filter is able to read some file
97494971 499 static wxHtmlFilter *m_DefaultFilter;
97494971 500
518ba663
VZ
501 // html processors array:
502 wxHtmlProcessorList *m_Processors;
503 static wxHtmlProcessorList *m_GlobalProcessors;
504
892aeafc 505 // browser history
518ba663 506 wxHtmlHistoryArray *m_History;
97494971 507 int m_HistoryPos;
892aeafc 508 // if this FLAG is false, items are not added to history
97494971 509 bool m_HistoryOn;
bfb9ee96 510
518ba663
VZ
511 // a flag set if we need to erase background in OnPaint() (otherwise this
512 // is supposed to have been done in OnEraseBackground())
513 bool m_eraseBgInOnPaint;
5526e819 514
69941f05 515 DECLARE_EVENT_TABLE()
22f3361e 516 DECLARE_NO_COPY_CLASS(wxHtmlWindow)
69941f05 517};
5526e819
VS
518
519
d659d703 520#endif // wxUSE_HTML
69941f05
VS
521
522#endif // _WX_HTMLWIN_H_
19193a2c 523