]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
1. Implemented support for different icons for different states (expanded,
[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 // 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
32
33 //-----------------------------------------------------------------------------
34 // wxHtmlWindow
35 //-----------------------------------------------------------------------------
36
37
38
39 #include <wx/arrimpl.cpp>
40 WX_DEFINE_OBJARRAY(HtmlHistoryArray)
41
42
43 wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
44 long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, wxVSCROLL, name)
45 {
46 m_tmpMouseMoved = FALSE;
47 m_tmpCanDraw = TRUE;
48 m_FS = new wxFileSystem();
49 m_RelatedStatusBar = -1;
50 m_RelatedFrame = NULL;
51 m_TitleFormat = "%s";
52 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
53 m_Cell = NULL;
54 m_Parser = new wxHtmlWinParser(this);
55 m_Parser -> SetFS(m_FS);
56 SetBorders(10);
57 m_HistoryPos = -1;
58 m_HistoryOn = TRUE;
59 m_Style = style;
60 SetPage("<html><body></body></html>");
61 }
62
63
64
65 wxHtmlWindow::~wxHtmlWindow()
66 {
67 HistoryClear();
68
69 if (m_Cell) delete m_Cell;
70
71 wxList *parser_data = m_Parser -> GetTempData();
72 if (parser_data) delete parser_data;
73
74 delete m_Parser;
75 delete m_FS;
76 }
77
78
79
80 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
81 {
82 m_RelatedFrame = frame;
83 m_TitleFormat = format;
84 }
85
86
87
88 void wxHtmlWindow::SetRelatedStatusBar(int bar)
89 {
90 m_RelatedStatusBar = bar;
91 }
92
93
94
95 void wxHtmlWindow::SetFonts(wxString normal_face, int normal_italic_mode, wxString fixed_face, int fixed_italic_mode, int *sizes)
96 {
97 m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes);
98 if (!m_OpenedPage.IsEmpty()) LoadPage(m_OpenedPage);
99 }
100
101
102
103 bool wxHtmlWindow::SetPage(const wxString& source)
104 {
105 wxClientDC *dc = new wxClientDC(this);
106
107 dc -> SetMapMode(wxMM_TEXT);
108 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
109 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
110 m_Parser -> SetDC(dc);
111 if (m_Cell) {
112 delete m_Cell;
113 m_Cell = NULL;
114 }
115 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
116 delete dc;
117 m_Cell -> SetIndent(m_Borders, HTML_INDENT_ALL, HTML_UNITS_PIXELS);
118 m_Cell -> SetAlignHor(HTML_ALIGN_CENTER);
119 CreateLayout();
120 Refresh();
121 return TRUE;
122 }
123
124
125 bool wxHtmlWindow::LoadPage(const wxString& location)
126 {
127 wxFSFile *f;
128 bool rt_val;
129 wxBusyCursor b;
130
131 m_tmpCanDraw = FALSE;
132 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
133 int x, y;
134 ViewStart(&x, &y);
135 m_History[m_HistoryPos].SetPos(y);
136 }
137
138 if (location[0] == '#') { // local anchor
139 wxString anch = location.Mid(1) /*1 to end*/;
140 m_tmpCanDraw = TRUE;
141 rt_val = ScrollToAnchor(anch);
142 }
143
144 else {
145 // load&display it:
146 if (m_RelatedStatusBar != -1) {
147 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
148 Refresh();
149 }
150
151 f = m_FS -> OpenFile(location);
152 if (f == NULL) {
153 wxString err;
154
155 err.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location);
156 m_tmpCanDraw = TRUE;
157 Refresh();
158 wxMessageBox(err, "Error");
159 return FALSE;
160 }
161
162 else {
163 wxNode *node;
164 wxString src = wxEmptyString;
165
166 if (m_RelatedStatusBar != -1) {
167 wxString msg = _("Loading : ") + location;
168 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
169 Refresh();
170 }
171
172 node = m_Filters.GetFirst();
173 while (node){
174 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
175 if (h -> CanRead(*f)) {
176 src = h -> ReadFile(*f);
177 break;
178 }
179 node = node -> GetNext();
180 }
181 if (src == wxEmptyString) {
182 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
183 src = m_DefaultFilter -> ReadFile(*f);
184 }
185
186 m_FS -> ChangePathTo(f -> GetLocation());
187 rt_val = SetPage(src);
188 m_OpenedPage = f -> GetLocation();
189 if (f -> GetAnchor() != wxEmptyString) {
190 m_tmpCanDraw = TRUE;
191 ScrollToAnchor(f -> GetAnchor());
192 m_tmpCanDraw = FALSE;
193 }
194
195 delete f;
196
197 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
198 }
199 }
200
201 if (m_HistoryOn) { // add this page to history there:
202 int c = m_History.GetCount() - (m_HistoryPos + 1);
203
204 m_HistoryPos++;
205 for (int i = 0; i < c; i++)
206 m_History.Remove(m_HistoryPos);
207 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
208 }
209
210 m_tmpCanDraw = TRUE;
211 Refresh();
212 return rt_val;
213 }
214
215
216
217 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
218 {
219 const wxHtmlCell *c = m_Cell -> Find(HTML_COND_ISANCHOR, &anchor);
220 if (!c) return FALSE;
221 else {
222 int y;
223
224 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
225 Scroll(-1, y / HTML_SCROLL_STEP);
226 m_OpenedAnchor = anchor;
227 return TRUE;
228 }
229 }
230
231
232 void wxHtmlWindow::SetTitle(const wxString& title)
233 {
234 if (m_RelatedFrame) {
235 wxString tit;
236 tit.Printf(m_TitleFormat, title.c_str());
237 m_RelatedFrame -> SetTitle(tit);
238 }
239 }
240
241
242
243
244
245 void wxHtmlWindow::CreateLayout()
246 {
247 int ClientWidth, ClientHeight;
248
249 if (!m_Cell) return;
250
251 if (m_Style == wxHW_SCROLLBAR_NEVER) {
252 SetScrollbars(1, 1, 0, 0); // always off
253 GetClientSize(&ClientWidth, &ClientHeight);
254 m_Cell -> Layout(ClientWidth);
255 }
256
257 else {
258 GetClientSize(&ClientWidth, &ClientHeight);
259 #ifndef __WXMSW__
260 // VS : this looks extremely ugly under windoze, better fix needed!
261 SetScrollbars(1, 1, 0, ClientHeight * 2); // always on
262 #endif
263 GetClientSize(&ClientWidth, &ClientHeight);
264 m_Cell -> Layout(ClientWidth);
265 if (ClientHeight < m_Cell -> GetHeight()) {
266 SetScrollbars(HTML_SCROLL_STEP, HTML_SCROLL_STEP,
267 m_Cell -> GetWidth() / HTML_SCROLL_STEP,
268 m_Cell -> GetHeight() / HTML_SCROLL_STEP
269 /*cheat: top-level frag is always container*/);
270 }
271 else { /* we fit into window, no need for scrollbars */
272 SetScrollbars(1, 1, 0, 0); // disable...
273 GetClientSize(&ClientWidth, &ClientHeight);
274 m_Cell -> Layout(ClientWidth); // ...and relayout
275 }
276 }
277 }
278
279
280
281 void 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
307 void 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
333 bool 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
354 bool 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
377 void wxHtmlWindow::HistoryClear()
378 {
379 m_History.Empty();
380 m_HistoryPos = -1;
381 }
382
383
384
385 wxList wxHtmlWindow::m_Filters;
386 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
387
388 void wxHtmlWindow::CleanUpStatics()
389 {
390 if (m_DefaultFilter) delete m_DefaultFilter;
391 m_DefaultFilter = NULL;
392 wxNode* node = m_Filters.GetFirst();
393 m_Filters.DeleteContents(TRUE);
394 m_Filters.Clear();
395
396 }
397
398
399
400 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
401 {
402 m_Filters.Append(filter);
403 }
404
405
406
407
408 void wxHtmlWindow::OnLinkClicked(const wxString& link)
409 {
410 LoadPage(link);
411 }
412
413
414
415 void wxHtmlWindow::OnDraw(wxDC& dc)
416 {
417 int x, y;
418 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
419 int v_y, v_h;
420
421 if (!m_tmpCanDraw) return;
422 dc.SetMapMode(wxMM_TEXT);
423 #if defined(_MSC_VER) && (_MSC_VER == 1200)
424 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
425 #endif
426 dc.SetBackgroundMode(wxTRANSPARENT);
427 ViewStart(&x, &y);
428
429 while (upd) {
430 v_y = upd.GetY();
431 v_h = upd.GetH();
432 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * HTML_SCROLL_STEP + v_y, y * HTML_SCROLL_STEP + v_h + v_y);
433 upd++;
434 }
435 }
436
437
438
439
440 void wxHtmlWindow::OnSize(wxSizeEvent& event)
441 {
442 wxScrolledWindow::OnSize(event);
443 CreateLayout();
444 }
445
446
447
448 void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
449 {
450 int dummy;
451 int sty, szy, cliy;
452
453 ViewStart(&dummy, &sty);
454 GetClientSize(&dummy, &cliy); cliy /= HTML_SCROLL_STEP;
455 GetVirtualSize(&dummy, &szy); szy /= HTML_SCROLL_STEP;
456
457 switch (event.KeyCode()) {
458 case WXK_PAGEUP :
459 case WXK_PRIOR :
460 Scroll(-1, sty - cliy);
461 break;
462 case WXK_PAGEDOWN :
463 case WXK_NEXT :
464 Scroll(-1, sty + cliy);
465 break;
466 case WXK_HOME :
467 Scroll(-1, 0);
468 break;
469 case WXK_END :
470 Scroll(-1, szy - cliy);
471 break;
472 case WXK_UP :
473 Scroll(-1, sty - 1);
474 break;
475 case WXK_DOWN :
476 Scroll(-1, sty + 1);
477 break;
478 }
479 }
480
481
482
483 void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
484 {
485 m_tmpMouseMoved = TRUE;
486
487 if (event.ButtonDown()) {
488 int sx, sy;
489 wxPoint pos;
490 wxString lnk;
491
492 ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP;
493 pos = event.GetPosition();
494
495 if (m_Cell)
496 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3));
497 }
498 }
499
500
501
502 void wxHtmlWindow::OnIdle(wxIdleEvent& event)
503 {
504 static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW);
505
506 if (m_tmpMouseMoved && (m_Cell != NULL)) {
507 int sx, sy;
508 int x, y;
509 wxString lnk;
510
511 ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP;
512 wxGetMousePosition(&x, &y);
513 ScreenToClient(&x, &y);
514 lnk = m_Cell -> GetLink(sx + x, sy + y);
515
516 if (lnk == wxEmptyString) {
517 SetCursor(cur_arrow);
518 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
519 }
520 else {
521 SetCursor(cur_hand);
522 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar);
523 }
524 m_tmpMouseMoved = FALSE;
525 }
526 }
527
528
529
530
531 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
532
533 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
534 EVT_SIZE(wxHtmlWindow::OnSize)
535 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
536 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
537 EVT_IDLE(wxHtmlWindow::OnIdle)
538 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
539 END_EVENT_TABLE()
540
541
542
543
544
545 // A module to allow initialization/cleanup
546 // without calling these functions from app.cpp or from
547 // the user's application.
548
549 class wxHtmlWinModule: public wxModule
550 {
551 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
552 public:
553 wxHtmlWinModule() : wxModule() {}
554 bool OnInit() { return TRUE; }
555 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
556 };
557
558 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
559
560
561
562
563 ///// default mod handlers are forced there:
564
565 FORCE_LINK(mod_layout)
566 FORCE_LINK(mod_fonts)
567 FORCE_LINK(mod_image)
568 FORCE_LINK(mod_list)
569 FORCE_LINK(mod_pre)
570 FORCE_LINK(mod_hline)
571 FORCE_LINK(mod_links)
572 FORCE_LINK(mod_tables)
573
574
575 #endif