1 /////////////////////////////////////////////////////////////////////////////
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/html/winpars.h"
20 #include "wx/html/htmlcell.h"
21 #include "wx/filesys.h"
22 #include "wx/html/htmlfilt.h"
23 #include "wx/filename.h"
24 #include "wx/bitmap.h"
26 class wxHtmlProcessor
;
27 class wxHtmlWinModule
;
28 class wxHtmlHistoryArray
;
29 class wxHtmlProcessorList
;
30 class WXDLLIMPEXP_HTML wxHtmlWinAutoScrollTimer
;
33 // wxHtmlWindow flags:
34 #define wxHW_SCROLLBAR_NEVER 0x0002
35 #define wxHW_SCROLLBAR_AUTO 0x0004
36 #define wxHW_NO_SELECTION 0x0008
38 #define wxHW_DEFAULT_STYLE wxHW_SCROLLBAR_AUTO
41 // enums for wxHtmlWindow::OnOpeningURL
42 enum wxHtmlOpeningStatus
49 // ----------------------------------------------------------------------------
51 // (This is probably the only class you will directly use.)
52 // Purpose of this class is to display HTML page (either local
53 // file or downloaded via HTTP protocol) in a window. Width of
54 // window is constant - given in constructor - virtual height
55 // is changed dynamicly depending on page size. Once the
56 // window is created you can set it's content by calling
57 // SetPage(text) or LoadPage(filename).
58 // ----------------------------------------------------------------------------
60 class WXDLLIMPEXP_HTML wxHtmlWindow
: public wxScrolledWindow
62 DECLARE_DYNAMIC_CLASS(wxHtmlWindow
)
63 friend class wxHtmlWinModule
;
66 wxHtmlWindow() { Init(); }
67 wxHtmlWindow(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
68 const wxPoint
& pos
= wxDefaultPosition
,
69 const wxSize
& size
= wxDefaultSize
,
70 long style
= wxHW_DEFAULT_STYLE
,
71 const wxString
& name
= wxT("htmlWindow"))
74 Create(parent
, id
, pos
, size
, style
, name
);
78 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
79 const wxPoint
& pos
= wxDefaultPosition
,
80 const wxSize
& size
= wxDefaultSize
,
81 long style
= wxHW_SCROLLBAR_AUTO
,
82 const wxString
& name
= wxT("htmlWindow"));
84 // Set HTML page and display it. !! source is HTML document itself,
85 // it is NOT address/filename of HTML document. If you want to
86 // specify document location, use LoadPage() istead
87 // Return value : false if an error occurred, true otherwise
88 virtual bool SetPage(const wxString
& source
);
90 // Append to current page
91 bool AppendToPage(const wxString
& source
);
93 // Load HTML page from given location. Location can be either
94 // a) /usr/wxGTK2/docs/html/wx.htm
95 // b) http://www.somewhere.uk/document.htm
96 // c) ftp://ftp.somesite.cz/pub/something.htm
97 // In case there is no prefix (http:,ftp:), the method
98 // will try to find it itself (1. local file, then http or ftp)
99 // After the page is loaded, the method calls SetPage() to display it.
100 // Note : you can also use path relative to previously loaded page
101 // Return value : same as SetPage
102 virtual bool LoadPage(const wxString
& location
);
104 // Loads HTML page from file
105 bool LoadFile(const wxFileName
& filename
);
107 // Returns full location of opened page
108 wxString
GetOpenedPage() const {return m_OpenedPage
;}
109 // Returns anchor within opened page
110 wxString
GetOpenedAnchor() const {return m_OpenedAnchor
;}
111 // Returns <TITLE> of opened page or empty string otherwise
112 wxString
GetOpenedPageTitle() const {return m_OpenedPageTitle
;}
114 // Sets frame in which page title will be displayed. Format is format of
115 // frame title, e.g. "HtmlHelp : %s". It must contain exactly one %s
116 void SetRelatedFrame(wxFrame
* frame
, const wxString
& format
);
117 wxFrame
* GetRelatedFrame() const {return m_RelatedFrame
;}
120 // After(!) calling SetRelatedFrame, this sets statusbar slot where messages
121 // will be displayed. Default is -1 = no messages.
122 void SetRelatedStatusBar(int bar
);
123 #endif // wxUSE_STATUSBAR
125 // Sets fonts to be used when displaying HTML page.
126 void SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
127 const int *sizes
= NULL
);
129 // Sets font sizes to be relative to the given size or the system
130 // default size; use either specified or default font
131 void SetStandardFonts(int size
= -1,
132 const wxString
& normal_face
= wxEmptyString
,
133 const wxString
& fixed_face
= wxEmptyString
);
135 // Sets space between text and window borders.
136 void SetBorders(int b
) {m_Borders
= b
;}
138 // Sets the bitmap to use for background (currnetly it will be tiled,
139 // when/if we have CSS support we could add other possibilities...)
140 void SetBackgroundImage(const wxBitmap
& bmpBg
) { m_bmpBg
= bmpBg
; }
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
);
147 virtual void WriteCustomization(wxConfigBase
*cfg
, wxString path
= wxEmptyString
);
149 // Goes to previous/next page (in browsing history)
150 // Returns true if successful, false otherwise
152 bool HistoryForward();
153 bool HistoryCanBack();
154 bool HistoryCanForward();
158 // Returns pointer to conteiners/cells structure.
159 // It should be used ONLY when printing
160 wxHtmlContainerCell
* GetInternalRepresentation() const {return m_Cell
;}
163 static void AddFilter(wxHtmlFilter
*filter
);
165 // Returns a pointer to the parser.
166 wxHtmlWinParser
*GetParser() const { return m_Parser
; }
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
);
176 // Sets the title of the window
177 // (depending on the information passed to SetRelatedFrame() method)
178 virtual void OnSetTitle(const wxString
& title
);
180 // Called when the mouse hovers over a cell: (x, y) are logical coords
181 // Default behaviour is to do nothing at all
182 virtual void OnCellMouseHover(wxHtmlCell
*cell
, wxCoord x
, wxCoord y
);
184 // Called when user clicks on a cell. Default behavior is to call
185 // OnLinkClicked() if this cell corresponds to a hypertext link
186 virtual void OnCellClicked(wxHtmlCell
*cell
,
187 wxCoord x
, wxCoord y
,
188 const wxMouseEvent
& event
);
190 // Called when user clicked on hypertext link. Default behavior is to
191 // call LoadPage(loc)
192 virtual void OnLinkClicked(const wxHtmlLinkInfo
& link
);
194 // Called when wxHtmlWindow wants to fetch data from an URL (e.g. when
195 // loading a page or loading an image). The data are downloaded if and only if
196 // OnOpeningURL returns true. If OnOpeningURL returns wxHTML_REDIRECT,
197 // it must set *redirect to the new URL
198 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
199 const wxString
& WXUNUSED(url
),
200 wxString
*WXUNUSED(redirect
)) const
201 { return wxHTML_OPEN
; }
204 // Helper functions to select parts of page:
205 void SelectWord(const wxPoint
& pos
);
206 void SelectLine(const wxPoint
& pos
);
209 // Convert selection to text:
210 wxString
SelectionToText() { return DoSelectionToText(m_selection
); }
212 // Converts current page to text:
214 #endif // wxUSE_CLIPBOARD
216 virtual void OnInternalIdle();
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)
224 // Return value : true if anchor exists, false otherwise
225 bool ScrollToAnchor(const wxString
& anchor
);
227 // Prepares layout (= fill m_PosX, m_PosY for fragments) based on
228 // actual size of window. This method also setup scrollbars
231 void OnEraseBackground(wxEraseEvent
& event
);
232 void OnPaint(wxPaintEvent
& event
);
233 void OnSize(wxSizeEvent
& event
);
234 void OnMouseMove(wxMouseEvent
& event
);
235 void OnMouseDown(wxMouseEvent
& event
);
236 void OnMouseUp(wxMouseEvent
& event
);
238 void OnKeyUp(wxKeyEvent
& event
);
239 void OnDoubleClick(wxMouseEvent
& event
);
240 void OnCopy(wxCommandEvent
& event
);
241 void OnMouseEnter(wxMouseEvent
& event
);
242 void OnMouseLeave(wxMouseEvent
& event
);
243 #endif // wxUSE_CLIPBOARD
245 // Returns new filter (will be stored into m_DefaultFilter variable)
246 virtual wxHtmlFilter
*GetDefaultFilter() {return new wxHtmlFilterPlainText
;}
248 // cleans static variables
249 static void CleanUpStatics();
251 // Returns true if text selection is enabled (wxClipboard must be available
252 // and wxHW_NO_SELECTION not used)
253 bool IsSelectionEnabled() const;
261 // Copies selection to clipboard if the clipboard support is available
263 // returns true if anything was copied to clipboard, false otherwise
264 bool CopySelection(ClipboardType t
= Secondary
);
267 // Automatic scrolling during selection:
268 void StopAutoScrolling();
269 #endif // wxUSE_CLIPBOARD
272 wxString
DoSelectionToText(wxHtmlSelection
*sel
);
274 // This is pointer to the first cell in parsed data. (Note: the first cell
275 // is usually top one = all other cells are sub-cells of this one)
276 wxHtmlContainerCell
*m_Cell
;
277 // parser which is used to parse HTML input.
278 // Each wxHtmlWindow has it's own parser because sharing one global
279 // parser would be problematic (because of reentrancy)
280 wxHtmlWinParser
*m_Parser
;
281 // contains name of actualy opened page or empty string if no page opened
282 wxString m_OpenedPage
;
283 // contains name of current anchor within m_OpenedPage
284 wxString m_OpenedAnchor
;
285 // contains title of actualy opened page or empty string if no <TITLE> tag
286 wxString m_OpenedPageTitle
;
287 // class for opening files (file system)
290 wxFrame
*m_RelatedFrame
;
291 wxString m_TitleFormat
;
293 // frame in which page title should be displayed & number of it's statusbar
294 // reserved for usage with this html window
295 int m_RelatedStatusBar
;
296 #endif // wxUSE_STATUSBAR
298 // borders (free space between text and window borders)
299 // defaults to 10 pixels.
304 // current text selection or NULL
305 wxHtmlSelection
*m_selection
;
307 // true if the user is dragging mouse to select text
308 bool m_makingSelection
;
311 // time of the last doubleclick event, used to detect tripleclicks
312 // (tripleclicks are used to select whole line):
313 wxLongLong m_lastDoubleClick
;
315 // helper class to automatically scroll the window if the user is selecting
316 // text and the mouse leaves wxHtmlWindow:
317 wxHtmlWinAutoScrollTimer
*m_timerAutoScroll
;
318 #endif // wxUSE_CLIPBOARD
321 // window content for double buffered rendering:
322 wxBitmap
*m_backBuffer
;
324 // background image, may be invalid
327 // variables used when user is selecting text
328 wxPoint m_tmpSelFromPos
;
329 wxHtmlCell
*m_tmpSelFromCell
;
331 // contains last link name
332 wxHtmlLinkInfo
*m_tmpLastLink
;
333 // contains the last (terminal) cell which contained the mouse
334 wxHtmlCell
*m_tmpLastCell
;
335 // if >0 contents of the window is not redrawn
336 // (in order to avoid ugly blinking)
337 int m_tmpCanDrawLocks
;
339 // list of HTML filters
340 static wxList m_Filters
;
341 // this filter is used when no filter is able to read some file
342 static wxHtmlFilter
*m_DefaultFilter
;
344 // html processors array:
345 wxHtmlProcessorList
*m_Processors
;
346 static wxHtmlProcessorList
*m_GlobalProcessors
;
349 wxHtmlHistoryArray
*m_History
;
351 // if this FLAG is false, items are not added to history
354 // a flag indicated if mouse moved
355 // (if true we will try to change cursor in last call to OnIdle)
356 bool m_tmpMouseMoved
;
358 // a flag set if we need to erase background in OnPaint() (otherwise this
359 // is supposed to have been done in OnEraseBackground())
360 bool m_eraseBgInOnPaint
;
362 DECLARE_EVENT_TABLE()
363 DECLARE_NO_COPY_CLASS(wxHtmlWindow
)
369 #endif // _WX_HTMLWIN_H_