fixed crash in wxHtmlHelpController if the help window is still open
[wxWidgets.git] / src / html / helpfrm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/helpfrm.cpp
3 // Purpose: wxHtmlHelpFrame
4 // Notes: Based on htmlhelp.cpp, implementing a monolithic
5 // HTML Help controller class, by Vaclav Slavik
6 // Author: Harm van der Heijden and Vaclav Slavik
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h"
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_WXHTML_HELP
21
22 #ifndef WX_PRECOMP
23 #include "wx/object.h"
24 #include "wx/dynarray.h"
25 #include "wx/intl.h"
26 #include "wx/log.h"
27 #if wxUSE_STREAMS
28 #include "wx/stream.h"
29 #endif
30
31 #include "wx/sizer.h"
32
33 #include "wx/bmpbuttn.h"
34 #include "wx/statbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/menu.h"
37 #include "wx/settings.h"
38 #include "wx/msgdlg.h"
39 #include "wx/textctrl.h"
40 #include "wx/toolbar.h"
41 #include "wx/choicdlg.h"
42 #include "wx/filedlg.h"
43 #endif // WX_PRECOMP
44
45 #include "wx/html/helpfrm.h"
46 #include "wx/html/helpctrl.h"
47 #include "wx/notebook.h"
48 #include "wx/imaglist.h"
49 #include "wx/treectrl.h"
50 #include "wx/tokenzr.h"
51 #include "wx/wfstream.h"
52 #include "wx/html/htmlwin.h"
53 #include "wx/busyinfo.h"
54 #include "wx/progdlg.h"
55 #include "wx/fontenum.h"
56 #include "wx/artprov.h"
57 #include "wx/spinctrl.h"
58 #include "wx/app.h"
59
60 IMPLEMENT_DYNAMIC_CLASS(wxHtmlHelpFrame, wxFrame)
61
62 BEGIN_EVENT_TABLE(wxHtmlHelpFrame, wxFrame)
63 EVT_ACTIVATE(wxHtmlHelpFrame::OnActivate)
64 EVT_CLOSE(wxHtmlHelpFrame::OnCloseWindow)
65 #ifdef __WXMAC__
66 EVT_MENU(wxID_CLOSE, wxHtmlHelpFrame::OnClose)
67 EVT_MENU(wxID_ABOUT, wxHtmlHelpFrame::OnAbout)
68 EVT_MENU(wxID_HELP_CONTENTS, wxHtmlHelpFrame::OnAbout)
69 #endif
70 END_EVENT_TABLE()
71
72 wxHtmlHelpFrame::wxHtmlHelpFrame(wxWindow* parent, wxWindowID id, const wxString& title,
73 int style, wxHtmlHelpData* data,
74 wxConfigBase *config, const wxString& rootpath)
75 {
76 Init(data);
77 Create(parent, id, title, style, config, rootpath);
78 }
79
80 void wxHtmlHelpFrame::Init(wxHtmlHelpData* data)
81 {
82 // Simply pass the pointer on to the help window
83 m_Data = data;
84 m_HtmlHelpWin = NULL;
85 m_helpController = (wxHtmlHelpController*) NULL;
86 }
87
88 void wxHtmlHelpFrame::SetController(wxHtmlHelpController* controller)
89 {
90 m_helpController = controller;
91 if ( m_HtmlHelpWin )
92 m_HtmlHelpWin->SetController(controller);
93 }
94
95 // Create: builds the GUI components.
96 bool wxHtmlHelpFrame::Create(wxWindow* parent, wxWindowID id,
97 const wxString& WXUNUSED(title), int style,
98 wxConfigBase *config, const wxString& rootpath)
99 {
100 m_HtmlHelpWin = new wxHtmlHelpWindow(m_Data);
101 m_HtmlHelpWin->SetController(m_helpController);
102 if ( config)
103 m_HtmlHelpWin->UseConfig(config, rootpath);
104
105 wxFrame::Create(parent, id, _("Help"),
106 wxPoint(m_HtmlHelpWin->GetCfgData().x, m_HtmlHelpWin->GetCfgData().y),
107 wxSize(m_HtmlHelpWin->GetCfgData().w, m_HtmlHelpWin->GetCfgData().h),
108 wxDEFAULT_FRAME_STYLE, wxT("wxHtmlHelp"));
109 #if wxUSE_STATUSBAR
110 CreateStatusBar();
111 #endif
112 m_HtmlHelpWin->Create(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
113 wxTAB_TRAVERSAL|wxNO_BORDER, style);
114
115 GetPosition(& (m_HtmlHelpWin->GetCfgData().x), & (m_HtmlHelpWin->GetCfgData()).y);
116
117 SetIcon(wxArtProvider::GetIcon(wxART_HELP, wxART_HELP_BROWSER));
118
119 // On the Mac, each modeless frame must have a menubar.
120 // TODO: add more menu items, and perhaps add a style to show
121 // the menubar: compulsory on the Mac, optional elsewhere.
122 #ifdef __WXMAC__
123 wxMenuBar* menuBar = new wxMenuBar;
124
125 wxMenu* fileMenu = new wxMenu;
126 fileMenu->Append(wxID_HTML_OPENFILE, _("&Open..."));
127 fileMenu->AppendSeparator();
128 fileMenu->Append(wxID_CLOSE, _("&Close"));
129
130 wxMenu* helpMenu = new wxMenu;
131 helpMenu->Append(wxID_ABOUT, _("&About..."));
132 // Ensures we don't get an empty help menu
133 helpMenu->Append(wxID_HELP_CONTENTS, _("&About..."));
134
135 menuBar->Append(fileMenu,_("&File"));
136 menuBar->Append(helpMenu,_("&Help"));
137 SetMenuBar(menuBar);
138 #endif
139
140 m_HtmlHelpWin->GetHtmlWindow()->SetRelatedFrame(this, m_TitleFormat);
141 #if wxUSE_STATUSBAR
142 m_HtmlHelpWin->GetHtmlWindow()->SetRelatedStatusBar(0);
143 #endif
144 return true;
145 }
146
147 wxHtmlHelpFrame::~wxHtmlHelpFrame()
148 {
149 }
150
151 void wxHtmlHelpFrame::SetTitleFormat(const wxString& format)
152 {
153 if (GetHelpWindow() && GetHelpWindow()->GetHtmlWindow())
154 GetHelpWindow()->GetHtmlWindow()->SetRelatedFrame(this, format);
155 m_TitleFormat = format;
156 }
157
158 /*
159 EVENT HANDLING :
160 */
161
162
163 void wxHtmlHelpFrame::OnActivate(wxActivateEvent& event)
164 {
165 // This saves one mouse click when using the
166 // wxHTML for context sensitive help systems
167 #ifndef __WXGTK__
168 // NB: wxActivateEvent is a bit broken in wxGTK
169 // and is sometimes sent when it should not be
170 if (event.GetActive() && m_HtmlHelpWin)
171 m_HtmlHelpWin->GetHtmlWindow()->SetFocus();
172 #endif
173
174 event.Skip();
175 }
176
177 void wxHtmlHelpFrame::OnCloseWindow(wxCloseEvent& evt)
178 {
179 if (!IsIconized())
180 {
181 GetSize(& (m_HtmlHelpWin->GetCfgData().w), &(m_HtmlHelpWin->GetCfgData().h));
182 GetPosition(& (m_HtmlHelpWin->GetCfgData().x), & (m_HtmlHelpWin->GetCfgData().y));
183 }
184
185 #ifdef __WXGTK__
186 if (IsGrabbed())
187 {
188 RemoveGrab();
189 }
190 #endif
191
192 if (m_HtmlHelpWin->GetSplitterWindow() && m_HtmlHelpWin->GetCfgData().navig_on)
193 m_HtmlHelpWin->GetCfgData().sashpos = m_HtmlHelpWin->GetSplitterWindow()->GetSashPosition();
194
195 if (m_helpController && m_helpController->IsKindOf(CLASSINFO(wxHtmlHelpController)))
196 {
197 ((wxHtmlHelpController*) m_helpController)->OnCloseFrame(evt);
198 }
199
200 evt.Skip();
201 }
202
203 // Make the help controller's frame 'modal' if
204 // needed
205 void wxHtmlHelpFrame::AddGrabIfNeeded()
206 {
207 // So far, wxGTK only
208 #ifdef __WXGTK__
209 bool needGrab = false;
210
211 // Check if there are any modal windows present,
212 // in which case we need to add a grab.
213 for ( wxWindowList::iterator it = wxTopLevelWindows.begin();
214 it != wxTopLevelWindows.end();
215 ++it )
216 {
217 wxWindow *win = *it;
218 wxDialog *dialog = wxDynamicCast(win, wxDialog);
219
220 if (dialog && dialog->IsModal())
221 needGrab = true;
222 }
223
224 if (needGrab)
225 AddGrab();
226 #endif // __WXGTK__
227 }
228
229 // For compatibility
230 void wxHtmlHelpFrame::UseConfig(wxConfigBase *config, const wxString& rootPath)
231 {
232 if (m_HtmlHelpWin)
233 m_HtmlHelpWin->UseConfig(config, rootPath);
234 }
235
236 #ifdef __WXMAC__
237 void wxHtmlHelpFrame::OnClose(wxCommandEvent& WXUNUSED(event))
238 {
239 Close(true);
240 }
241
242 void wxHtmlHelpFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
243 {
244 wxMessageBox(wxT("wxWidgets HTML Help Viewer (c) 1998-2006, Vaclav Slavik et al"), wxT("HelpView"),
245 wxICON_INFORMATION|wxOK, this);
246 }
247 #endif
248
249 // we don't want to prevent the app from closing just because a help window
250 // remains opened
251 bool wxHtmlHelpFrame::ShouldPreventAppExit() const
252 {
253 return (wx_const_cast(wxHtmlHelpFrame*, this) == wxTheApp->GetTopWindow());
254 }
255
256 #endif // wxUSE_WXHTML_HELP