implemented URLs blocking in wxHtmlWindow
[wxWidgets.git] / include / wx / html / htmlwin.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlwin.h
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifndef _WX_HTMLWIN_H_
12 #define _WX_HTMLWIN_H_
13
14 #ifdef __GNUG__
15 #pragma interface "htmlwin.h"
16 #endif
17
18 #include "wx/defs.h"
19 #if wxUSE_HTML
20
21 #include "wx/window.h"
22 #include "wx/scrolwin.h"
23 #include "wx/config.h"
24 #include "wx/treectrl.h"
25 #include "wx/html/winpars.h"
26 #include "wx/html/htmlcell.h"
27 #include "wx/filesys.h"
28 #include "wx/html/htmlfilt.h"
29
30 class wxHtmlProcessor;
31 class wxHtmlWinModule;
32 class wxHtmlHistoryArray;
33 class wxHtmlProcessorList;
34
35 //--------------------------------------------------------------------------------
36 // wxHtmlWindow
37 // (This is probably the only class you will directly use.)
38 // Purpose of this class is to display HTML page (either local
39 // file or downloaded via HTTP protocol) in a window. Width
40 // of window is constant - given in constructor - virtual height
41 // is changed dynamicly depending on page size.
42 // Once the window is created you can set it's content by calling
43 // SetPage(text) or LoadPage(filename).
44 //--------------------------------------------------------------------------------
45
46 class WXDLLEXPORT wxHtmlWindow : public wxScrolledWindow
47 {
48 DECLARE_DYNAMIC_CLASS(wxHtmlWindow)
49 friend class wxHtmlWinModule;
50
51 public:
52 wxHtmlWindow() { Init(); }
53 wxHtmlWindow(wxWindow *parent, wxWindowID id = -1,
54 const wxPoint& pos = wxDefaultPosition,
55 const wxSize& size = wxDefaultSize,
56 long style = wxHW_SCROLLBAR_AUTO,
57 const wxString& name = wxT("htmlWindow"))
58 {
59 Init();
60 Create(parent, id, pos, size, style, name);
61 }
62 ~wxHtmlWindow();
63
64 bool Create(wxWindow *parent, wxWindowID id = -1,
65 const wxPoint& pos = wxDefaultPosition,
66 const wxSize& size = wxDefaultSize,
67 long style = wxHW_SCROLLBAR_AUTO,
68 const wxString& name = wxT("htmlWindow"));
69
70 // Set HTML page and display it. !! source is HTML document itself,
71 // it is NOT address/filename of HTML document. If you want to
72 // specify document location, use LoadPage() istead
73 // Return value : FALSE if an error occured, TRUE otherwise
74 bool SetPage(const wxString& source);
75
76 // Append to current page
77 bool AppendToPage(const wxString& source);
78
79 // Load HTML page from given location. Location can be either
80 // a) /usr/wxGTK2/docs/html/wx.htm
81 // b) http://www.somewhere.uk/document.htm
82 // c) ftp://ftp.somesite.cz/pub/something.htm
83 // In case there is no prefix (http:,ftp:), the method
84 // will try to find it itself (1. local file, then http or ftp)
85 // After the page is loaded, the method calls SetPage() to display it.
86 // Note : you can also use path relative to previously loaded page
87 // Return value : same as SetPage
88 virtual bool LoadPage(const wxString& location);
89
90 // Returns full location of opened page
91 wxString GetOpenedPage() const {return m_OpenedPage;}
92 // Returns anchor within opened page
93 wxString GetOpenedAnchor() const {return m_OpenedAnchor;}
94 // Returns <TITLE> of opened page or empty string otherwise
95 wxString GetOpenedPageTitle() const {return m_OpenedPageTitle;}
96
97 // Sets frame in which page title will be displayed. Format is format of
98 // frame title, e.g. "HtmlHelp : %s". It must contain exactly one %s
99 void SetRelatedFrame(wxFrame* frame, const wxString& format);
100 wxFrame* GetRelatedFrame() const {return m_RelatedFrame;}
101
102 // After(!) calling SetRelatedFrame, this sets statusbar slot where messages
103 // will be displayed. Default is -1 = no messages.
104 void SetRelatedStatusBar(int bar);
105
106 // Sets fonts to be used when displaying HTML page.
107 void SetFonts(wxString normal_face, wxString fixed_face, const int *sizes);
108
109 // Sets the title of the window
110 // (depending on the information passed to SetRelatedFrame() method)
111 virtual void OnSetTitle(const wxString& title);
112
113 // Sets space between text and window borders.
114 void SetBorders(int b) {m_Borders = b;}
115
116 // Saves custom settings into cfg config. it will use the path 'path'
117 // if given, otherwise it will save info into currently selected path.
118 // saved values : things set by SetFonts, SetBorders.
119 virtual void ReadCustomization(wxConfigBase *cfg, wxString path = wxEmptyString);
120 // ...
121 virtual void WriteCustomization(wxConfigBase *cfg, wxString path = wxEmptyString);
122
123 // Goes to previous/next page (in browsing history)
124 // Returns TRUE if successful, FALSE otherwise
125 bool HistoryBack();
126 bool HistoryForward();
127 bool HistoryCanBack();
128 bool HistoryCanForward();
129 // Resets history
130 void HistoryClear();
131
132 // Returns pointer to conteiners/cells structure.
133 // It should be used ONLY when printing
134 wxHtmlContainerCell* GetInternalRepresentation() const {return m_Cell;}
135
136 // Adds input filter
137 static void AddFilter(wxHtmlFilter *filter);
138
139 // Called when the mouse hovers over a cell: (x, y) are logical coords
140 //
141 // Default behaviour is to do nothing at all
142 virtual void OnCellMouseHover(wxHtmlCell *cell, wxCoord x, wxCoord y);
143
144 // Called when user clicks on a cell. Default behavior is to call
145 // OnLinkClicked() if this cell corresponds to a hypertext link
146 virtual void OnCellClicked(wxHtmlCell *cell,
147 wxCoord x, wxCoord y,
148 const wxMouseEvent& event);
149
150 // Called when user clicked on hypertext link. Default behavior is to
151 // call LoadPage(loc)
152 virtual void OnLinkClicked(const wxHtmlLinkInfo& link);
153
154 // Called when wxHtmlWindow wants to fetch data from an URL (e.g. when loading
155 // a page or loading an image). The data are downloaded if and only if
156 // OnOpeningURL returns TRUE.
157 virtual bool OnOpeningURL(const wxString& url) const { return TRUE; }
158
159 // Returns a pointer to the parser.
160 wxHtmlWinParser *GetParser() const { return m_Parser; }
161
162 // Adds HTML processor to this instance of wxHtmlWindow:
163 void AddProcessor(wxHtmlProcessor *processor);
164 // Adds HTML processor to wxHtmlWindow class as whole:
165 static void AddGlobalProcessor(wxHtmlProcessor *processor);
166
167 // what would we do with it?
168 virtual bool AcceptsFocusFromKeyboard() const { return FALSE; }
169
170 protected:
171 void Init();
172
173 // Scrolls to anchor of this name. (Anchor is #news
174 // or #features etc. it is part of address sometimes:
175 // http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/index.html#news)
176 // Return value : TRUE if anchor exists, FALSE otherwise
177 bool ScrollToAnchor(const wxString& anchor);
178
179 // Prepares layout (= fill m_PosX, m_PosY for fragments) based on
180 // actual size of window. This method also setup scrollbars
181 void CreateLayout();
182
183 void OnDraw(wxDC& dc);
184 void OnSize(wxSizeEvent& event);
185 void OnMouseEvent(wxMouseEvent& event);
186 void OnIdle(wxIdleEvent& event);
187
188 // Returns new filter (will be stored into m_DefaultFilter variable)
189 virtual wxHtmlFilter *GetDefaultFilter() {return new wxHtmlFilterPlainText;}
190
191 // cleans static variables
192 static void CleanUpStatics();
193
194 protected:
195 // This is pointer to the first cell in parsed data.
196 // (Note: the first cell is usually top one = all other cells are sub-cells of this one)
197 wxHtmlContainerCell *m_Cell;
198 // parser which is used to parse HTML input.
199 // Each wxHtmlWindow has it's own parser because sharing one global
200 // parser would be problematic (because of reentrancy)
201 wxHtmlWinParser *m_Parser;
202 // contains name of actualy opened page or empty string if no page opened
203 wxString m_OpenedPage;
204 // contains name of current anchor within m_OpenedPage
205 wxString m_OpenedAnchor;
206 // contains title of actualy opened page or empty string if no <TITLE> tag
207 wxString m_OpenedPageTitle;
208 // class for opening files (file system)
209 wxFileSystem* m_FS;
210
211 wxFrame *m_RelatedFrame;
212 wxString m_TitleFormat;
213 // frame in which page title should be displayed & number of it's statusbar
214 // reserved for usage with this html window
215 int m_RelatedStatusBar;
216
217 // borders (free space between text and window borders)
218 // defaults to 10 pixels.
219 int m_Borders;
220
221 int m_Style;
222
223 private:
224 // a flag indicated if mouse moved
225 // (if TRUE we will try to change cursor in last call to OnIdle)
226 bool m_tmpMouseMoved;
227 // contains last link name
228 wxHtmlLinkInfo *m_tmpLastLink;
229 // contains the last (terminal) cell which contained the mouse
230 wxHtmlCell *m_tmpLastCell;
231 // if >0 contents of the window is not redrawn
232 // (in order to avoid ugly blinking)
233 int m_tmpCanDrawLocks;
234
235 // list of HTML filters
236 static wxList m_Filters;
237 // this filter is used when no filter is able to read some file
238 static wxHtmlFilter *m_DefaultFilter;
239
240 static wxCursor *s_cur_hand;
241 static wxCursor *s_cur_arrow;
242
243 wxHtmlHistoryArray *m_History;
244 // browser history
245 int m_HistoryPos;
246 // if this FLAG is false, items are not added to history
247 bool m_HistoryOn;
248
249 // html processors array:
250 wxHtmlProcessorList *m_Processors;
251 static wxHtmlProcessorList *m_GlobalProcessors;
252
253 DECLARE_EVENT_TABLE()
254 };
255
256
257 #endif
258
259 #endif // _WX_HTMLWIN_H_
260