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