]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
Changed my mind and used FNSTRINGCAST instead
[wxWidgets.git] / src / html / htmlwin.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmlwin.cpp
3// Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
6// Licence: wxWindows Licence
7/////////////////////////////////////////////////////////////////////////////
8
9
10#ifdef __GNUG__
11#pragma implementation
12#endif
13
14#include <wx/wxprec.h>
15
16#include "wx/defs.h"
17#if wxUSE_HTML
18
19#ifdef __BORDLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WXPRECOMP
24#include <wx/wx.h>
25#endif
26
27#include <wx/html/htmlwin.h>
28
29#include <wx/html/forcelink.h>
30
31///// This is my own wxBusyCursor. It works only with one window.
32
33#if (defined __WXGTK__) && (wxVERSION_NUMBER < 2100)
34class wxLocalBusyCursor
35#else
36class wxLocalBusyCursor : public wxBusyCursor
37#endif
38{
39 private:
40 wxWindow *m_Wnd;
41 public:
42#if (defined __WXGTK__) && (wxVERSION_NUMBER < 2100)
43 wxLocalBusyCursor(wxWindow *w) {m_Wnd = w; m_Wnd -> SetCursor(*wxHOURGLASS_CURSOR);}
44 ~wxLocalBusyCursor() {m_Wnd -> SetCursor(*wxSTANDARD_CURSOR);}
45#else
46 wxLocalBusyCursor(wxWindow *w) : wxBusyCursor() {}
47#endif
48};
49
50
51
52
53//-----------------------------------------------------------------------------
54// wxHtmlWindow
55//-----------------------------------------------------------------------------
56
57
58
59#include <wx/arrimpl.cpp>
60WX_DEFINE_OBJARRAY(HtmlHistoryArray)
61
62
63wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
64 const wxString& name, bool scrollable) : wxScrolledWindow(parent, id, pos, size, wxVSCROLL, name)
65{
66 m_tmpMouseMoved = FALSE;
67 m_tmpCanDraw = TRUE;
68 m_FS = new wxFileSystem();
69 m_RelatedStatusBar = -1;
70 m_RelatedFrame = NULL;
71 m_TitleFormat = "%s";
72 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
73 m_Cell = NULL;
74 m_Parser = new wxHtmlWinParser(this);
75 m_Parser -> SetFS(m_FS);
76 SetBorders(10);
77 m_HistoryPos = -1;
78 m_HistoryOn = TRUE;
79 m_Scrollable = scrollable;
80 SetPage("<html><body></body></html>");
81}
82
83
84
85wxHtmlWindow::~wxHtmlWindow()
86{
87 HistoryClear();
88
89 if (m_Cell) delete m_Cell;
90
91 wxList *parser_data = m_Parser -> GetTempData();
92 if (parser_data) delete parser_data;
93
94 delete m_Parser;
95 delete m_FS;
96}
97
98
99
100void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
101{
102 m_RelatedFrame = frame;
103 m_TitleFormat = format;
104}
105
106
107
108void wxHtmlWindow::SetRelatedStatusBar(int bar)
109{
110 m_RelatedStatusBar = bar;
111}
112
113
114
115void wxHtmlWindow::SetFonts(wxString normal_face, int normal_italic_mode, wxString fixed_face, int fixed_italic_mode, int *sizes)
116{
117 m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes);
118 if (!m_OpenedPage.IsEmpty()) LoadPage(m_OpenedPage);
119}
120
121
122
123bool wxHtmlWindow::SetPage(const wxString& source)
124{
125 wxClientDC *dc = new wxClientDC(this);
126
127 dc -> SetMapMode(wxMM_TEXT);
128 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
129 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
130 m_Parser -> SetDC(dc);
f61815af
GL
131 if (m_Cell) {
132 delete m_Cell;
133 m_Cell = NULL;
134 }
5526e819
VS
135 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
136 delete dc;
137 m_Cell -> SetIndent(m_Borders, HTML_INDENT_ALL, HTML_UNITS_PIXELS);
138 m_Cell -> SetAlignHor(HTML_ALIGN_CENTER);
139 CreateLayout();
140 Refresh();
141 return TRUE;
142}
143
144
145bool wxHtmlWindow::LoadPage(const wxString& location)
146{
147 wxFSFile *f;
148 bool rt_val;
149 wxLocalBusyCursor b(this);
150
151 m_tmpCanDraw = FALSE;
152 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
153 int x, y;
154 ViewStart(&x, &y);
155 m_History[m_HistoryPos].SetPos(y);
156 }
157
158 if (location[0] == '#') { // local anchor
159 wxString anch = location.Mid(1) /*1 to end*/;
160 m_tmpCanDraw = TRUE;
161 rt_val = ScrollToAnchor(anch);
162 }
163
164 else {
165 // load&display it:
166 if (m_RelatedStatusBar != -1) {
167 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
168 Refresh();
169 }
170
171 f = m_FS -> OpenFile(location);
172 if (f == NULL) {
173 wxString err;
174
175 err.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location);
5526e819 176 m_tmpCanDraw = TRUE;
483ff5a5
VS
177 Refresh();
178 wxMessageBox(err, "Error");
5526e819
VS
179 return FALSE;
180 }
181
182 else {
183 wxNode *node;
184 wxString src = wxEmptyString;
185
186 if (m_RelatedStatusBar != -1) {
187 wxString msg = _("Loading : ") + location;
188 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
189 Refresh();
190 }
191
192 node = m_Filters.GetFirst();
193 while (node){
194 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
195 if (h -> CanRead(*f)) {
196 src = h -> ReadFile(*f);
197 break;
198 }
199 node = node -> GetNext();
200 }
a76015e6
VS
201 if (src == wxEmptyString) {
202 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
203 src = m_DefaultFilter -> ReadFile(*f);
204 }
5526e819
VS
205
206 m_FS -> ChangePathTo(f -> GetLocation());
207 rt_val = SetPage(src);
208 m_OpenedPage = f -> GetLocation();
209 if (f -> GetAnchor() != wxEmptyString) {
210 m_tmpCanDraw = TRUE;
211 ScrollToAnchor(f -> GetAnchor());
212 m_tmpCanDraw = FALSE;
213 }
214
215 delete f;
216
217 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
218 }
219 }
220
221 if (m_HistoryOn) { // add this page to history there:
222 int c = m_History.GetCount() - (m_HistoryPos + 1);
223
224 m_HistoryPos++;
225 for (int i = 0; i < c; i++)
226 m_History.Remove(m_HistoryPos);
227 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
228 }
229
230 m_tmpCanDraw = TRUE;
231 Refresh();
232 return rt_val;
233}
234
235
236
237bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
238{
239 const wxHtmlCell *c = m_Cell -> Find(HTML_COND_ISANCHOR, &anchor);
240 if (!c) return FALSE;
241 else {
242 int y;
243
244 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
245 Scroll(-1, y / HTML_SCROLL_STEP);
246 m_OpenedAnchor = anchor;
247 return TRUE;
248 }
249}
250
251
252void wxHtmlWindow::SetTitle(const wxString& title)
253{
254 if (m_RelatedFrame) {
255 wxString tit;
256 tit.Printf(m_TitleFormat, title.c_str());
257 m_RelatedFrame -> SetTitle(tit);
258 }
259}
260
261
262
263
264
265void wxHtmlWindow::CreateLayout()
266{
267 int ClientWidth, ClientHeight;
268
269 if (!m_Cell) return;
270 GetClientSize(&ClientWidth, &ClientHeight);
271 m_Cell -> Layout(ClientWidth);
272 if (m_Scrollable)
273 SetScrollbars(HTML_SCROLL_STEP, HTML_SCROLL_STEP,
274 m_Cell -> GetWidth() / HTML_SCROLL_STEP,
275 m_Cell -> GetHeight() / HTML_SCROLL_STEP
276 /*cheat: top-level frag is always container*/ );
277}
278
279
280
281void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
282{
283 wxString oldpath;
284 wxString tmp;
285
286 if (path != wxEmptyString) {
287 oldpath = cfg -> GetPath();
288 cfg -> SetPath(path);
289 }
290
291 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
292 m_Parser -> m_FontFaceFixed = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
293 m_Parser -> m_FontFaceNormal = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
294 m_Parser -> m_ItalicModeFixed = cfg -> Read("wxHtmlWindow/ItalicModeFixed", m_Parser -> m_ItalicModeFixed);
295 m_Parser -> m_ItalicModeNormal = cfg -> Read("wxHtmlWindow/ItalicModeNormal", m_Parser -> m_ItalicModeNormal);
296 for (int i = 0; i < 7; i++) {
297 tmp.Printf("wxHtmlWindow/FontsSize%i", i);
298 m_Parser -> m_FontsSizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
299 }
300
301 if (path != wxEmptyString)
302 cfg -> SetPath(oldpath);
303}
304
305
306
307void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
308{
309 wxString oldpath;
310 wxString tmp;
311
312 if (path != wxEmptyString) {
313 oldpath = cfg -> GetPath();
314 cfg -> SetPath(path);
315 }
316
317 cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders);
318 cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
319 cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
320 cfg -> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser -> m_ItalicModeFixed);
321 cfg -> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser -> m_ItalicModeNormal);
322 for (int i = 0; i < 7; i++) {
323 tmp.Printf("wxHtmlWindow/FontsSize%i", i);
324 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
325 }
326
327 if (path != wxEmptyString)
328 cfg -> SetPath(oldpath);
329}
330
331
332
333bool wxHtmlWindow::HistoryBack()
334{
335 wxString a, l;
336
337 if (m_HistoryPos < 1) return FALSE;
338
339 m_HistoryPos--;
340
341 l = m_History[m_HistoryPos].GetPage();
342 a = m_History[m_HistoryPos].GetAnchor();
343 m_HistoryOn = FALSE;
344 if (a == wxEmptyString) LoadPage(l);
345 else LoadPage(l + "#" + a);
346 m_HistoryOn = TRUE;
347 Scroll(0, m_History[m_HistoryPos].GetPos());
348 Refresh();
349 return TRUE;
350}
351
352
353
354bool wxHtmlWindow::HistoryForward()
355{
356 wxString a, l;
357
358 if (m_HistoryPos == -1) return FALSE;
359 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
360
361 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
362
363 m_HistoryPos++;
364 l = m_History[m_HistoryPos].GetPage();
365 a = m_History[m_HistoryPos].GetAnchor();
366 m_HistoryOn = FALSE;
367 if (a == wxEmptyString) LoadPage(l);
368 else LoadPage(l + "#" + a);
369 m_HistoryOn = TRUE;
370 Scroll(0, m_History[m_HistoryPos].GetPos());
371 Refresh();
372 return TRUE;
373}
374
375
376
377void wxHtmlWindow::HistoryClear()
378{
379 m_History.Empty();
380 m_HistoryPos = -1;
381}
382
383
384
385wxList wxHtmlWindow::m_Filters;
a76015e6
VS
386wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
387
388void wxHtmlWindow::CleanUpStatics()
389{
390 if (m_DefaultFilter) delete m_DefaultFilter;
391 m_DefaultFilter = NULL;
392}
393
394
5526e819
VS
395
396void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
397{
398 m_Filters.DeleteContents(TRUE);
399 m_Filters.Append(filter);
400}
401
402
403
404
405void wxHtmlWindow::OnLinkClicked(const wxString& link)
406{
407 LoadPage(link);
408}
409
410
411
412void wxHtmlWindow::OnDraw(wxDC& dc)
413{
414 int x, y;
415 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
416 int v_y, v_h;
417
418 if (!m_tmpCanDraw) return;
419 dc.SetMapMode(wxMM_TEXT);
420#if defined(_MSC_VER) && (_MSC_VER == 1200)
421 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
422#endif
423 dc.SetBackgroundMode(wxTRANSPARENT);
424 ViewStart(&x, &y);
425
426 while (upd) {
427 v_y = upd.GetY();
428 v_h = upd.GetH();
429 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * HTML_SCROLL_STEP + v_y, y * HTML_SCROLL_STEP + v_h + v_y);
430 upd++;
431 }
432}
433
434
435
436
437void wxHtmlWindow::OnSize(wxSizeEvent& event)
438{
439 wxScrolledWindow::OnSize(event);
440 CreateLayout();
441}
442
443
444
445void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
446{
447 int dummy;
448 int sty, szy, cliy;
449
450 ViewStart(&dummy, &sty);
451 GetClientSize(&dummy, &cliy); cliy /= HTML_SCROLL_STEP;
452 GetVirtualSize(&dummy, &szy); szy /= HTML_SCROLL_STEP;
453
454 switch (event.KeyCode()) {
455 case WXK_PAGEUP :
456 case WXK_PRIOR :
457 Scroll(-1, sty - cliy);
458 break;
459 case WXK_PAGEDOWN :
460 case WXK_NEXT :
461 Scroll(-1, sty + cliy);
462 break;
463 case WXK_HOME :
464 Scroll(-1, 0);
465 break;
466 case WXK_END :
467 Scroll(-1, szy - cliy);
468 break;
469 case WXK_UP :
470 Scroll(-1, sty - 1);
471 break;
472 case WXK_DOWN :
473 Scroll(-1, sty + 1);
474 break;
475 }
476}
477
478
479
480void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
481{
482 m_tmpMouseMoved = TRUE;
483
484 if (event.ButtonDown()) {
485 int sx, sy;
486 wxPoint pos;
487 wxString lnk;
488
489 ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP;
490 pos = event.GetPosition();
491
492 if (m_Cell)
493 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3));
494 }
495}
496
497
498
499void wxHtmlWindow::OnIdle(wxIdleEvent& event)
500{
501 static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW);
502
503 if (m_tmpMouseMoved && (m_Cell != NULL)) {
504 int sx, sy;
505 int x, y;
506 wxString lnk;
507
508 ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP;
509 wxGetMousePosition(&x, &y);
510 ScreenToClient(&x, &y);
511 lnk = m_Cell -> GetLink(sx + x, sy + y);
512
513 if (lnk == wxEmptyString) {
514 SetCursor(cur_arrow);
515 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
516 }
517 else {
518 SetCursor(cur_hand);
519 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar);
520 }
521 m_tmpMouseMoved = FALSE;
522 }
523}
524
525
526
527
528IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
529
530BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
531 EVT_SIZE(wxHtmlWindow::OnSize)
532 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
533 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
534 EVT_IDLE(wxHtmlWindow::OnIdle)
535 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
536END_EVENT_TABLE()
537
538
539
540
541
a76015e6
VS
542// A module to allow initialization/cleanup
543// without calling these functions from app.cpp or from
544// the user's application.
545
546class wxHtmlWinModule: public wxModule
547{
548DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
549public:
550 wxHtmlWinModule() : wxModule() {}
551 bool OnInit() { return TRUE; }
552 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
553};
554
555IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
556
5526e819
VS
557
558
559
560///// default mod handlers are forced there:
561
562FORCE_LINK(mod_layout)
563FORCE_LINK(mod_fonts)
564FORCE_LINK(mod_image)
565FORCE_LINK(mod_list)
566FORCE_LINK(mod_pre)
567FORCE_LINK(mod_hline)
568FORCE_LINK(mod_links)
569FORCE_LINK(mod_tables)
570
571
483ff5a5 572#endif