1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/html/htmlwin.h
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_HTMLWIN_H_
11 #define _WX_HTMLWIN_H_
16 #include "wx/window.h"
17 #include "wx/scrolwin.h"
18 #include "wx/config.h"
19 #include "wx/stopwatch.h"
20 #include "wx/html/winpars.h"
21 #include "wx/html/htmlcell.h"
22 #include "wx/filesys.h"
23 #include "wx/html/htmlfilt.h"
24 #include "wx/filename.h"
25 #include "wx/bitmap.h"
27 class wxHtmlProcessor
;
28 class wxHtmlWinModule
;
29 class wxHtmlHistoryArray
;
30 class wxHtmlProcessorList
;
31 class WXDLLIMPEXP_FWD_HTML wxHtmlWinAutoScrollTimer
;
32 class WXDLLIMPEXP_FWD_HTML wxHtmlCellEvent
;
33 class WXDLLIMPEXP_FWD_HTML wxHtmlLinkEvent
;
34 class WXDLLIMPEXP_FWD_CORE wxStatusBar
;
36 // wxHtmlWindow flags:
37 #define wxHW_SCROLLBAR_NEVER 0x0002
38 #define wxHW_SCROLLBAR_AUTO 0x0004
39 #define wxHW_NO_SELECTION 0x0008
41 #define wxHW_DEFAULT_STYLE wxHW_SCROLLBAR_AUTO
43 /// Enum for wxHtmlWindow::OnOpeningURL and wxHtmlWindowInterface::OnOpeningURL
44 enum wxHtmlOpeningStatus
46 /// Open the requested URL
48 /// Do not open the URL
50 /// Redirect to another URL (returned from OnOpeningURL)
55 Abstract interface to a HTML rendering window (such as wxHtmlWindow or
56 wxHtmlListBox) that is passed to wxHtmlWinParser. It encapsulates all
57 communication from the parser to the window.
59 class WXDLLIMPEXP_HTML wxHtmlWindowInterface
63 wxHtmlWindowInterface() {}
64 virtual ~wxHtmlWindowInterface() {}
67 Called by the parser to set window's title to given text.
69 virtual void SetHTMLWindowTitle(const wxString
& title
) = 0;
72 Called when a link is clicked.
74 @param link information about the clicked link
76 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
) = 0;
79 Called when the parser needs to open another URL (e.g. an image).
81 @param type Type of the URL request (e.g. image)
82 @param url URL the parser wants to open
83 @param redirect If the return value is wxHTML_REDIRECT, then the
84 URL to redirect to will be stored in this variable
85 (the pointer must never be NULL)
87 @return indicator of how to treat the request
89 virtual wxHtmlOpeningStatus
OnHTMLOpeningURL(wxHtmlURLType type
,
91 wxString
*redirect
) const = 0;
94 Converts coordinates @a pos relative to given @a cell to
95 physical coordinates in the window.
97 virtual wxPoint
HTMLCoordsToWindow(wxHtmlCell
*cell
,
98 const wxPoint
& pos
) const = 0;
100 /// Returns the window used for rendering (may be NULL).
101 virtual wxWindow
* GetHTMLWindow() = 0;
103 /// Returns background colour to use by default.
104 virtual wxColour
GetHTMLBackgroundColour() const = 0;
106 /// Sets window's background to colour @a clr.
107 virtual void SetHTMLBackgroundColour(const wxColour
& clr
) = 0;
109 /// Sets window's background to given bitmap.
110 virtual void SetHTMLBackgroundImage(const wxBitmap
& bmpBg
) = 0;
112 /// Sets status bar text.
113 virtual void SetHTMLStatusText(const wxString
& text
) = 0;
115 /// Type of mouse cursor
118 /// Standard mouse cursor (typically an arrow)
120 /// Cursor shown over links
122 /// Cursor shown over selectable text
127 Returns mouse cursor of given @a type.
129 virtual wxCursor
GetHTMLCursor(HTMLCursor type
) const = 0;
133 Helper class that implements part of mouse handling for wxHtmlWindow and
134 wxHtmlListBox. Cursor changes and clicking on links are handled, text
137 class WXDLLIMPEXP_HTML wxHtmlWindowMouseHelper
143 @param iface Interface to the owner window.
145 wxHtmlWindowMouseHelper(wxHtmlWindowInterface
*iface
);
150 It is not really needed in this case but at leats it prevents gcc from
151 complaining about its absence.
153 virtual ~wxHtmlWindowMouseHelper() { }
155 /// Returns true if the mouse moved since the last call to HandleIdle
156 bool DidMouseMove() const { return m_tmpMouseMoved
; }
158 /// Call this from EVT_MOTION event handler
159 void HandleMouseMoved();
162 Call this from EVT_LEFT_UP handler (or, alternatively, EVT_LEFT_DOWN).
164 @param rootCell HTML cell inside which the click occured. This doesn't
165 have to be the leaf cell, it can be e.g. toplevel
166 container, but the mouse must be inside the container's
167 area, otherwise the event would be ignored.
168 @param pos Mouse position in coordinates relative to @a cell
169 @param event The event that triggered the call
171 bool HandleMouseClick(wxHtmlCell
*rootCell
,
172 const wxPoint
& pos
, const wxMouseEvent
& event
);
175 Call this from OnInternalIdle of the HTML displaying window. Handles
176 mouse movements and must be used together with HandleMouseMoved.
178 @param rootCell HTML cell inside which the click occured. This doesn't
179 have to be the leaf cell, it can be e.g. toplevel
180 container, but the mouse must be inside the container's
181 area, otherwise the event would be ignored.
182 @param pos Current mouse position in coordinates relative to
185 void HandleIdle(wxHtmlCell
*rootCell
, const wxPoint
& pos
);
188 Called by HandleIdle when the mouse hovers over a cell. Default
189 behaviour is to do nothing.
191 @param cell the cell the mouse is over
192 @param x, y coordinates of mouse relative to the cell
194 virtual void OnCellMouseHover(wxHtmlCell
*cell
, wxCoord x
, wxCoord y
);
197 Called by HandleMouseClick when the user clicks on a cell.
198 Default behaviour is to call wxHtmlWindowInterface::OnLinkClicked()
199 if this cell corresponds to a hypertext link.
201 @param cell the cell the mouse is over
202 @param x, y coordinates of mouse relative to the cell
203 @param event The event that triggered the call
206 @return true if a link was clicked, false otherwise.
208 virtual bool OnCellClicked(wxHtmlCell
*cell
,
209 wxCoord x
, wxCoord y
,
210 const wxMouseEvent
& event
);
213 // this flag indicates if the mouse moved (used by HandleIdle)
214 bool m_tmpMouseMoved
;
215 // contains last link name
216 wxHtmlLinkInfo
*m_tmpLastLink
;
217 // contains the last (terminal) cell which contained the mouse
218 wxHtmlCell
*m_tmpLastCell
;
221 wxHtmlWindowInterface
*m_interface
;
224 // ----------------------------------------------------------------------------
226 // (This is probably the only class you will directly use.)
227 // Purpose of this class is to display HTML page (either local
228 // file or downloaded via HTTP protocol) in a window. Width of
229 // window is constant - given in constructor - virtual height
230 // is changed dynamicly depending on page size. Once the
231 // window is created you can set it's content by calling
232 // SetPage(text) or LoadPage(filename).
233 // ----------------------------------------------------------------------------
235 class WXDLLIMPEXP_HTML wxHtmlWindow
: public wxScrolledWindow
,
236 public wxHtmlWindowInterface
,
237 public wxHtmlWindowMouseHelper
239 DECLARE_DYNAMIC_CLASS(wxHtmlWindow
)
240 friend class wxHtmlWinModule
;
243 wxHtmlWindow() : wxHtmlWindowMouseHelper(this) { Init(); }
244 wxHtmlWindow(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
245 const wxPoint
& pos
= wxDefaultPosition
,
246 const wxSize
& size
= wxDefaultSize
,
247 long style
= wxHW_DEFAULT_STYLE
,
248 const wxString
& name
= wxT("htmlWindow"))
249 : wxHtmlWindowMouseHelper(this)
252 Create(parent
, id
, pos
, size
, style
, name
);
254 virtual ~wxHtmlWindow();
256 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
257 const wxPoint
& pos
= wxDefaultPosition
,
258 const wxSize
& size
= wxDefaultSize
,
259 long style
= wxHW_SCROLLBAR_AUTO
,
260 const wxString
& name
= wxT("htmlWindow"));
262 // Set HTML page and display it. !! source is HTML document itself,
263 // it is NOT address/filename of HTML document. If you want to
264 // specify document location, use LoadPage() istead
265 // Return value : false if an error occurred, true otherwise
266 virtual bool SetPage(const wxString
& source
);
268 // Append to current page
269 bool AppendToPage(const wxString
& source
);
271 // Load HTML page from given location. Location can be either
272 // a) /usr/wxGTK2/docs/html/wx.htm
273 // b) http://www.somewhere.uk/document.htm
274 // c) ftp://ftp.somesite.cz/pub/something.htm
275 // In case there is no prefix (http:,ftp:), the method
276 // will try to find it itself (1. local file, then http or ftp)
277 // After the page is loaded, the method calls SetPage() to display it.
278 // Note : you can also use path relative to previously loaded page
279 // Return value : same as SetPage
280 virtual bool LoadPage(const wxString
& location
);
282 // Loads HTML page from file
283 bool LoadFile(const wxFileName
& filename
);
285 // Returns full location of opened page
286 wxString
GetOpenedPage() const {return m_OpenedPage
;}
287 // Returns anchor within opened page
288 wxString
GetOpenedAnchor() const {return m_OpenedAnchor
;}
289 // Returns <TITLE> of opened page or empty string otherwise
290 wxString
GetOpenedPageTitle() const {return m_OpenedPageTitle
;}
292 // Sets frame in which page title will be displayed. Format is format of
293 // frame title, e.g. "HtmlHelp : %s". It must contain exactly one %s
294 void SetRelatedFrame(wxFrame
* frame
, const wxString
& format
);
295 wxFrame
* GetRelatedFrame() const {return m_RelatedFrame
;}
298 // After(!) calling SetRelatedFrame, this sets statusbar slot where messages
299 // will be displayed. Default is -1 = no messages.
300 void SetRelatedStatusBar(int index
);
301 void SetRelatedStatusBar(wxStatusBar
*, int index
= 0);
302 #endif // wxUSE_STATUSBAR
304 // Sets fonts to be used when displaying HTML page.
305 void SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
306 const int *sizes
= NULL
);
308 // Sets font sizes to be relative to the given size or the system
309 // default size; use either specified or default font
310 void SetStandardFonts(int size
= -1,
311 const wxString
& normal_face
= wxEmptyString
,
312 const wxString
& fixed_face
= wxEmptyString
);
314 // Sets space between text and window borders.
315 void SetBorders(int b
) {m_Borders
= b
;}
317 // Sets the bitmap to use for background (currnetly it will be tiled,
318 // when/if we have CSS support we could add other possibilities...)
319 void SetBackgroundImage(const wxBitmap
& bmpBg
) { m_bmpBg
= bmpBg
; }
322 // Saves custom settings into cfg config. it will use the path 'path'
323 // if given, otherwise it will save info into currently selected path.
324 // saved values : things set by SetFonts, SetBorders.
325 virtual void ReadCustomization(wxConfigBase
*cfg
, wxString path
= wxEmptyString
);
327 virtual void WriteCustomization(wxConfigBase
*cfg
, wxString path
= wxEmptyString
);
328 #endif // wxUSE_CONFIG
330 // Goes to previous/next page (in browsing history)
331 // Returns true if successful, false otherwise
333 bool HistoryForward();
334 bool HistoryCanBack();
335 bool HistoryCanForward();
339 // Returns pointer to conteiners/cells structure.
340 // It should be used ONLY when printing
341 wxHtmlContainerCell
* GetInternalRepresentation() const {return m_Cell
;}
344 static void AddFilter(wxHtmlFilter
*filter
);
346 // Returns a pointer to the parser.
347 wxHtmlWinParser
*GetParser() const { return m_Parser
; }
349 // Adds HTML processor to this instance of wxHtmlWindow:
350 void AddProcessor(wxHtmlProcessor
*processor
);
351 // Adds HTML processor to wxHtmlWindow class as whole:
352 static void AddGlobalProcessor(wxHtmlProcessor
*processor
);
357 // Sets the title of the window
358 // (depending on the information passed to SetRelatedFrame() method)
359 virtual void OnSetTitle(const wxString
& title
);
361 // Called when user clicked on hypertext link. Default behaviour is to
362 // call LoadPage(loc)
363 virtual void OnLinkClicked(const wxHtmlLinkInfo
& link
);
365 // Called when wxHtmlWindow wants to fetch data from an URL (e.g. when
366 // loading a page or loading an image). The data are downloaded if and only if
367 // OnOpeningURL returns true. If OnOpeningURL returns wxHTML_REDIRECT,
368 // it must set *redirect to the new URL
369 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
370 const wxString
& WXUNUSED(url
),
371 wxString
*WXUNUSED(redirect
)) const
372 { return wxHTML_OPEN
; }
375 // Helper functions to select parts of page:
376 void SelectWord(const wxPoint
& pos
);
377 void SelectLine(const wxPoint
& pos
);
380 // Convert selection to text:
381 wxString
SelectionToText() { return DoSelectionToText(m_selection
); }
383 // Converts current page to text:
385 #endif // wxUSE_CLIPBOARD
387 virtual void OnInternalIdle();
389 /// Returns standard HTML cursor as used by wxHtmlWindow
390 static wxCursor
GetDefaultHTMLCursor(HTMLCursor type
);
395 // Scrolls to anchor of this name. (Anchor is #news
396 // or #features etc. it is part of address sometimes:
397 // http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/index.html#news)
398 // Return value : true if anchor exists, false otherwise
399 bool ScrollToAnchor(const wxString
& anchor
);
401 // Prepares layout (= fill m_PosX, m_PosY for fragments) based on
402 // actual size of window. This method also setup scrollbars
405 void OnPaint(wxPaintEvent
& event
);
406 void OnEraseBackground(wxEraseEvent
& event
);
407 void OnSize(wxSizeEvent
& event
);
408 void OnMouseMove(wxMouseEvent
& event
);
409 void OnMouseDown(wxMouseEvent
& event
);
410 void OnMouseUp(wxMouseEvent
& event
);
412 void OnKeyUp(wxKeyEvent
& event
);
413 void OnDoubleClick(wxMouseEvent
& event
);
414 void OnCopy(wxCommandEvent
& event
);
415 void OnClipboardEvent(wxClipboardTextEvent
& event
);
416 void OnMouseEnter(wxMouseEvent
& event
);
417 void OnMouseLeave(wxMouseEvent
& event
);
418 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
419 #endif // wxUSE_CLIPBOARD
421 // Returns new filter (will be stored into m_DefaultFilter variable)
422 virtual wxHtmlFilter
*GetDefaultFilter() {return new wxHtmlFilterPlainText
;}
424 // cleans static variables
425 static void CleanUpStatics();
427 // Returns true if text selection is enabled (wxClipboard must be available
428 // and wxHW_NO_SELECTION not used)
429 bool IsSelectionEnabled() const;
437 // Copies selection to clipboard if the clipboard support is available
439 // returns true if anything was copied to clipboard, false otherwise
440 bool CopySelection(ClipboardType t
= Secondary
);
443 // Automatic scrolling during selection:
444 void StopAutoScrolling();
445 #endif // wxUSE_CLIPBOARD
447 wxString
DoSelectionToText(wxHtmlSelection
*sel
);
450 // wxHtmlWindowInterface methods:
451 virtual void SetHTMLWindowTitle(const wxString
& title
);
452 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
);
453 virtual wxHtmlOpeningStatus
OnHTMLOpeningURL(wxHtmlURLType type
,
455 wxString
*redirect
) const;
456 virtual wxPoint
HTMLCoordsToWindow(wxHtmlCell
*cell
,
457 const wxPoint
& pos
) const;
458 virtual wxWindow
* GetHTMLWindow();
459 virtual wxColour
GetHTMLBackgroundColour() const;
460 virtual void SetHTMLBackgroundColour(const wxColour
& clr
);
461 virtual void SetHTMLBackgroundImage(const wxBitmap
& bmpBg
);
462 virtual void SetHTMLStatusText(const wxString
& text
);
463 virtual wxCursor
GetHTMLCursor(HTMLCursor type
) const;
465 // implementation of SetPage()
466 bool DoSetPage(const wxString
& source
);
469 // This is pointer to the first cell in parsed data. (Note: the first cell
470 // is usually top one = all other cells are sub-cells of this one)
471 wxHtmlContainerCell
*m_Cell
;
472 // parser which is used to parse HTML input.
473 // Each wxHtmlWindow has its own parser because sharing one global
474 // parser would be problematic (because of reentrancy)
475 wxHtmlWinParser
*m_Parser
;
476 // contains name of actually opened page or empty string if no page opened
477 wxString m_OpenedPage
;
478 // contains name of current anchor within m_OpenedPage
479 wxString m_OpenedAnchor
;
480 // contains title of actually opened page or empty string if no <TITLE> tag
481 wxString m_OpenedPageTitle
;
482 // class for opening files (file system)
485 // frame in which page title should be displayed & number of its statusbar
486 // reserved for usage with this html window
487 wxFrame
*m_RelatedFrame
;
489 int m_RelatedStatusBarIndex
;
490 wxStatusBar
* m_RelatedStatusBar
;
491 #endif // wxUSE_STATUSBAR
492 wxString m_TitleFormat
;
494 // borders (free space between text and window borders)
495 // defaults to 10 pixels.
498 // current text selection or NULL
499 wxHtmlSelection
*m_selection
;
501 // true if the user is dragging mouse to select text
502 bool m_makingSelection
;
505 // time of the last doubleclick event, used to detect tripleclicks
506 // (tripleclicks are used to select whole line):
507 wxMilliClock_t m_lastDoubleClick
;
509 // helper class to automatically scroll the window if the user is selecting
510 // text and the mouse leaves wxHtmlWindow:
511 wxHtmlWinAutoScrollTimer
*m_timerAutoScroll
;
512 #endif // wxUSE_CLIPBOARD
515 // erase the window background using m_bmpBg or just solid colour if we
516 // don't have any background image
517 void DoEraseBackground(wxDC
& dc
);
519 // window content for double buffered rendering, may be invalid until it is
520 // really initialized in OnPaint()
521 wxBitmap m_backBuffer
;
523 // background image, may be invalid
526 // variables used when user is selecting text
527 wxPoint m_tmpSelFromPos
;
528 wxHtmlCell
*m_tmpSelFromCell
;
530 // if >0 contents of the window is not redrawn
531 // (in order to avoid ugly blinking)
532 int m_tmpCanDrawLocks
;
534 // list of HTML filters
535 static wxList m_Filters
;
536 // this filter is used when no filter is able to read some file
537 static wxHtmlFilter
*m_DefaultFilter
;
539 // html processors array:
540 wxHtmlProcessorList
*m_Processors
;
541 static wxHtmlProcessorList
*m_GlobalProcessors
;
544 wxHtmlHistoryArray
*m_History
;
546 // if this FLAG is false, items are not added to history
549 // Flag used to communicate between OnPaint() and OnEraseBackground(), see
550 // the comments near its use.
551 bool m_isBgReallyErased
;
553 // standard mouse cursors
554 static wxCursor
*ms_cursorLink
;
555 static wxCursor
*ms_cursorText
;
557 DECLARE_EVENT_TABLE()
558 wxDECLARE_NO_COPY_CLASS(wxHtmlWindow
);
561 class WXDLLIMPEXP_FWD_HTML wxHtmlCellEvent
;
563 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_HTML
, wxEVT_HTML_CELL_CLICKED
, wxHtmlCellEvent
);
564 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_HTML
, wxEVT_HTML_CELL_HOVER
, wxHtmlCellEvent
);
565 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_HTML
, wxEVT_HTML_LINK_CLICKED
, wxHtmlLinkEvent
);
569 * Html cell window event
572 class WXDLLIMPEXP_HTML wxHtmlCellEvent
: public wxCommandEvent
576 wxHtmlCellEvent(wxEventType commandType
, int id
,
577 wxHtmlCell
*cell
, const wxPoint
&pt
,
578 const wxMouseEvent
&ev
)
579 : wxCommandEvent(commandType
, id
)
584 m_bLinkWasClicked
= false;
587 wxHtmlCell
* GetCell() const { return m_cell
; }
588 wxPoint
GetPoint() const { return m_pt
; }
589 wxMouseEvent
GetMouseEvent() const { return m_mouseEvent
; }
591 void SetLinkClicked(bool linkclicked
) { m_bLinkWasClicked
=linkclicked
; }
592 bool GetLinkClicked() const { return m_bLinkWasClicked
; }
594 // default copy ctor, assignment operator and dtor are ok
595 virtual wxEvent
*Clone() const { return new wxHtmlCellEvent(*this); }
599 wxMouseEvent m_mouseEvent
;
602 bool m_bLinkWasClicked
;
604 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHtmlCellEvent
)
613 class WXDLLIMPEXP_HTML wxHtmlLinkEvent
: public wxCommandEvent
617 wxHtmlLinkEvent(int id
, const wxHtmlLinkInfo
&linkinfo
)
618 : wxCommandEvent(wxEVT_HTML_LINK_CLICKED
, id
)
620 m_linkInfo
= linkinfo
;
623 const wxHtmlLinkInfo
&GetLinkInfo() const { return m_linkInfo
; }
625 // default copy ctor, assignment operator and dtor are ok
626 virtual wxEvent
*Clone() const { return new wxHtmlLinkEvent(*this); }
629 wxHtmlLinkInfo m_linkInfo
;
631 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHtmlLinkEvent
)
635 typedef void (wxEvtHandler::*wxHtmlCellEventFunction
)(wxHtmlCellEvent
&);
636 typedef void (wxEvtHandler::*wxHtmlLinkEventFunction
)(wxHtmlLinkEvent
&);
638 #define wxHtmlCellEventHandler(func) \
639 wxEVENT_HANDLER_CAST(wxHtmlCellEventFunction, func)
640 #define wxHtmlLinkEventHandler(func) \
641 wxEVENT_HANDLER_CAST(wxHtmlLinkEventFunction, func)
643 #define EVT_HTML_CELL_CLICKED(id, fn) \
644 wx__DECLARE_EVT1(wxEVT_HTML_CELL_CLICKED, id, wxHtmlCellEventHandler(fn))
645 #define EVT_HTML_CELL_HOVER(id, fn) \
646 wx__DECLARE_EVT1(wxEVT_HTML_CELL_HOVER, id, wxHtmlCellEventHandler(fn))
647 #define EVT_HTML_LINK_CLICKED(id, fn) \
648 wx__DECLARE_EVT1(wxEVT_HTML_LINK_CLICKED, id, wxHtmlLinkEventHandler(fn))
651 // old wxEVT_COMMAND_* constants
652 #define wxEVT_COMMAND_HTML_CELL_CLICKED wxEVT_HTML_CELL_CLICKED
653 #define wxEVT_COMMAND_HTML_CELL_HOVER wxEVT_HTML_CELL_HOVER
654 #define wxEVT_COMMAND_HTML_LINK_CLICKED wxEVT_HTML_LINK_CLICKED
658 #endif // _WX_HTMLWIN_H_