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