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