]> git.saurik.com Git - wxWidgets.git/blame - src/generic/helpwxht.cpp
fixed compilation problem with old gcc releases
[wxWidgets.git] / src / generic / helpwxht.cpp
CommitLineData
29ea4a29 1/////////////////////////////////////////////////////////////////////////////
6adaedf0
JS
2// Name: helpwxht.cpp
3// Purpose: A help controller using the wxHTML classes
29ea4a29
KB
4// Author: Karsten Ballueder
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Karsten Ballueder
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13# pragma implementation "helpwxht.h"
14#endif
15
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19# pragma hdrstop
20#endif
21
3a8c693a 22#if wxUSE_WXHTML_HELP
4f84c635 23
29ea4a29 24#ifndef WX_PRECOMP
f6bcfd97
BP
25 #include "wx/string.h"
26 #include "wx/utils.h"
27 #include "wx/list.h"
28 #include "wx/intl.h"
29 #include "wx/layout.h"
30 #include "wx/combobox.h"
31 #include "wx/button.h"
29ea4a29
KB
32#endif
33
34#include "wx/helpbase.h"
35#include "wx/generic/helpwxht.h"
36#include "wx/html/htmlwin.h"
37
38#include <stdio.h>
39#include <ctype.h>
585ae8cb 40#ifndef __MWERKS__
29ea4a29 41#include <sys/stat.h>
585ae8cb 42#endif
29ea4a29 43
004fd0c8 44#if !defined(__WINDOWS__) && !defined(__OS2__)
29ea4a29
KB
45# include <unistd.h>
46#endif
47
eb1d2336
SC
48#ifdef __WXMAC__
49 #include "wx/mac/private.h"
50#endif
51
29ea4a29 52IMPLEMENT_CLASS(wxHelpControllerHtml, wxHTMLHelpControllerBase)
4f84c635 53
29ea4a29 54/**
8dd71e2b 55 This class implements help via wxHTML.
29ea4a29
KB
56 It requires the name of a directory containing the documentation
57 and a file mapping numerical Section numbers to relative URLS.
58*/
59
8dd71e2b
KB
60class wxForceHtmlFilter : public wxHtmlFilter
61{
62public:
420ec58a 63 virtual wxString ReadFile(const wxFSFile& file) const
8dd71e2b
KB
64 {
65 wxInputStream *s = file.GetStream();
66 char *src;
67 wxString doc;
68
69 if (s == NULL) return wxEmptyString;
4ba80ec7
KB
70 src = new char[s -> GetSize()+1];
71 src[s -> GetSize()] = 0;
72 s -> Read(src, s -> GetSize());
8dd71e2b
KB
73 doc = src;
74 delete [] src;
75 return doc;
76 }
004fd0c8 77
420ec58a 78 virtual bool CanRead(const wxFSFile& file) const
8dd71e2b
KB
79 {
80 wxString filename = file.GetLocation();
81 if(filename.Length() >= 5 &&
82 (
83 filename.Right(4).MakeUpper() == ".HTM" ||
84 filename.Right(5).MakeUpper() == ".HTML"))
85 return TRUE;
86 else
87 return FALSE;
88 }
89};
90
91#define FRAME_WIDTH 500
4f84c635 92#define FRAME_HEIGHT 400
29ea4a29
KB
93#define LAYOUT_X_MARGIN 2
94#define LAYOUT_Y_MARGIN 2
95#define OFFSET 10
8dd71e2b
KB
96#define BUTTON_WIDTH 70
97#define MAX_COMBO_ENTRIES 25
4f84c635 98
29ea4a29
KB
99class wxHelpFrame : public wxFrame
100{
101public:
102 wxHelpFrame(wxWindow *parent, int id, const wxString &title,
103 const wxPoint &pos, const wxSize &size,
104 wxHelpControllerHtml *controller);
105 ~wxHelpFrame();
106 void OnClose(wxCloseEvent &ev);
8dd71e2b 107 void OnButton(wxCommandEvent &ev);
29ea4a29
KB
108 bool LoadPage(const wxString &url) { return m_htmlwin->LoadPage(url); }
109private:
110 wxHelpControllerHtml *m_controller;
111 wxHtmlWindow *m_htmlwin;
8dd71e2b
KB
112 wxHtmlFilter *m_filter;
113 wxComboBox *m_combo;
114 long m_IdBack, m_IdFwd, m_IdContents, m_IdCombo, m_IdSearch;
29ea4a29
KB
115 DECLARE_EVENT_TABLE()
116};
117
118BEGIN_EVENT_TABLE(wxHelpFrame, wxFrame)
119 EVT_CLOSE(wxHelpFrame::OnClose)
8dd71e2b 120 EVT_BUTTON(-1, wxHelpFrame::OnButton)
29ea4a29
KB
121END_EVENT_TABLE()
122
8dd71e2b
KB
123
124void
125wxHelpFrame::OnButton(wxCommandEvent &ev)
126{
127 long id =ev.GetId();
128
129 if(id == m_IdBack)
130 m_htmlwin->HistoryBack();
131 else if(id == m_IdFwd)
132 m_htmlwin->HistoryForward();
133 else if(id == m_IdContents)
134 m_controller->DisplayContents();
135 else if(id == m_IdSearch)
136 {
137 wxString str = m_combo->GetValue();
ff8b6290 138 if(m_combo->FindString(str) == -1 && m_combo->GetCount() < MAX_COMBO_ENTRIES)
8dd71e2b
KB
139 m_combo->Append(str);
140 m_controller->KeywordSearch(str);
141 }
142}
143
29ea4a29
KB
144wxHelpFrame::wxHelpFrame(wxWindow *parent, int id,
145 const wxString &title,
146 const wxPoint &pos, const wxSize &size,
147 wxHelpControllerHtml *controller)
148 : wxFrame(parent, id, title, pos, size)
149{
150
151 m_controller = controller;
4f84c635 152 m_htmlwin = new wxHtmlWindow(this,-1,wxDefaultPosition,wxSize(FRAME_WIDTH,
29ea4a29 153 FRAME_HEIGHT));
4f84c635 154
8dd71e2b
KB
155 m_IdBack = wxWindow::NewControlId();
156 m_IdFwd = wxWindow::NewControlId();
157 m_IdContents = wxWindow::NewControlId();
158 m_IdCombo = wxWindow::NewControlId();
159 m_IdSearch = wxWindow::NewControlId();
160
161 wxButton *btn_back = new wxButton(this, m_IdBack, _("Back"));
162 wxButton *btn_fwd = new wxButton(this, m_IdFwd, _("Forward"));
163 wxButton *btn_contents = new wxButton(this, m_IdContents, _("Contents"));
164 m_combo = new wxComboBox(this, m_IdCombo);
165 wxButton *btn_search = new wxButton(this, m_IdSearch, _("Search"));
004fd0c8 166
8dd71e2b
KB
167 m_filter = new wxForceHtmlFilter;
168
29ea4a29
KB
169 wxLayoutConstraints *c;
170
171 c = new wxLayoutConstraints;
172 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
8dd71e2b 173 c->width.Absolute(BUTTON_WIDTH);
29ea4a29 174 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
8dd71e2b
KB
175 c->height.AsIs();
176 btn_back->SetConstraints(c);
177
178 c = new wxLayoutConstraints;
179 c->left.SameAs(btn_back, wxRight, 2*LAYOUT_X_MARGIN);
180 c->width.Absolute(BUTTON_WIDTH);
181 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
182 c->height.AsIs();
183 btn_fwd->SetConstraints(c);
004fd0c8 184
8dd71e2b
KB
185 c = new wxLayoutConstraints;
186 c->left.SameAs(btn_fwd, wxRight, 2*LAYOUT_X_MARGIN);
187 c->width.Absolute(BUTTON_WIDTH);
188 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
189 c->height.AsIs();
190 btn_contents->SetConstraints(c);
191
192 c = new wxLayoutConstraints;
193 c->left.SameAs(btn_contents, wxRight, 2*LAYOUT_X_MARGIN);
194 c->width.Absolute(3*BUTTON_WIDTH);
195 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
196 c->height.AsIs();
197 m_combo->SetConstraints(c);
198
199 c = new wxLayoutConstraints;
200 c->left.SameAs(m_combo, wxRight, 2*LAYOUT_X_MARGIN);
201 c->width.Absolute(BUTTON_WIDTH);
202 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
203 c->height.AsIs();
204 btn_search->SetConstraints(c);
205
206
207 c = new wxLayoutConstraints;
208 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
209 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
210 c->top.SameAs(btn_back, wxBottom, 2*LAYOUT_Y_MARGIN);
29ea4a29
KB
211 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
212 m_htmlwin->SetConstraints(c);
213 SetAutoLayout(TRUE);
8dd71e2b 214 CreateStatusBar();
004fd0c8 215
8dd71e2b
KB
216 m_htmlwin->SetRelatedFrame(this, title);
217 m_htmlwin->SetRelatedStatusBar(0);
218 m_htmlwin->AddFilter(m_filter);
219
7b28757f
JS
220#ifdef __WXMOTIF__
221 // Motif needs a nudge to get it to resize properly
222 // when shown
223 wxSizeEvent event(size, GetId());
224 GetEventHandler()->ProcessEvent(event);
225#endif
226
29ea4a29
KB
227 Show(TRUE);
228}
229
230wxHelpFrame::~wxHelpFrame()
231{
232}
233
234void
5e0201ea 235wxHelpFrame::OnClose(wxCloseEvent &WXUNUSED(ev))
29ea4a29
KB
236{
237 wxASSERT(m_controller);
238 m_controller->m_Frame = NULL;
239 bool newFrame;
240 int x,y;
241 GetPosition(&x,&y);
242
243 m_controller->GetFrameParameters(NULL, NULL, &newFrame);
244 m_controller->SetFrameParameters(GetTitle(), GetSize(),
245 wxPoint(x,y),
246 newFrame);
247 Destroy();
248}
249
250wxHelpControllerHtml::wxHelpControllerHtml(void)
251{
252 m_Frame = NULL;
253 m_offset = 0;
4f84c635 254
8dd71e2b 255 SetFrameParameters(_("Help: %s"),
29ea4a29
KB
256 wxSize(FRAME_WIDTH, FRAME_HEIGHT),
257 wxDefaultPosition);
258}
259
260wxHelpControllerHtml::~wxHelpControllerHtml(void)
261{
262 if(m_Frame && ! m_NewFrameEachTime)
263 m_Frame->Close();
264}
265
266
267#ifdef __WXMSW__
268# define SEP '\\'
269#else
270# define SEP '/'
271#endif
272
273bool
f6bcfd97 274wxHelpControllerHtml::DisplayHelp(const wxString &relativeURL)
29ea4a29
KB
275{
276 wxBusyCursor b; // display a busy cursor
277
278 wxString url;
eb1d2336 279 wxString mapfileurl = m_MapFile ;
ce976691 280#if defined(__WXMAC__) && !defined(__DARWIN__)
eb1d2336
SC
281 mapfileurl = wxMac2UnixFilename(m_MapFile) ;
282#endif
283 url << mapfileurl << SEP<< relativeURL;
29ea4a29
KB
284 if(! m_Frame || m_NewFrameEachTime)
285 {
286 m_Frame = new wxHelpFrame(NULL, -1, m_FrameTitle,
287 m_FramePosition+wxPoint(m_offset,m_offset),
288 m_FrameSize,
289 this);
290 if(m_NewFrameEachTime)
291 {
292 m_offset += OFFSET;
293 if(m_offset > 200)
294 m_offset = 0;
295 }
4f84c635 296
29ea4a29 297 }
8dd71e2b 298 m_Frame->Raise();
29ea4a29
KB
299 return m_Frame->LoadPage(url);
300}
301
302
303void
304wxHelpControllerHtml::SetFrameParameters(const wxString &title,
305 const wxSize &size,
306 const wxPoint &pos,
307 bool newFrame)
308{
309 m_FrameTitle = title;
310 m_FrameSize = size;
311 m_FramePosition = pos;
312 m_NewFrameEachTime = newFrame;
313}
314
4ba80ec7 315wxFrame *
259d1674
VZ
316wxHelpControllerHtml::GetFrameParameters(wxSize *size,
317 wxPoint *pos,
318 bool *newframe)
29ea4a29
KB
319{
320 if(size) *size = m_FrameSize;
321 if(pos) *pos = m_FramePosition;
322 if(newframe) *newframe = m_NewFrameEachTime;
4ba80ec7 323 return m_Frame;
29ea4a29 324}
4f84c635 325
3a8c693a
VZ
326#endif // wxUSE_WXHTML_HELP
327