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