]> git.saurik.com Git - wxWidgets.git/blame - src/generic/tipdlg.cpp
Guard against comparing invalid iterators, which produces an assertion
[wxWidgets.git] / src / generic / tipdlg.cpp
CommitLineData
c50f1fb9 1///////////////////////////////////////////////////////////////////////////////
f43255e8 2// Name: src/generic/tipdlg.cpp
c50f1fb9
VZ
3// Purpose: implementation of wxTipDialog
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 28.06.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
65571936 9// Licence: wxWindows licence
c50f1fb9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c50f1fb9
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_STARTUP_TIPS
28
29#ifndef WX_PRECOMP
30 #include "wx/button.h"
31 #include "wx/checkbox.h"
32 #include "wx/statbox.h"
c50f1fb9 33 #include "wx/dialog.h"
dfe1eee3
VZ
34 #include "wx/icon.h"
35 #include "wx/intl.h"
dfe1eee3 36 #include "wx/textctrl.h"
3bb0b01c 37 #include "wx/statbmp.h"
9c884972 38 #include "wx/stattext.h"
92afa2b1 39 #include "wx/sizer.h"
c50f1fb9
VZ
40#endif // WX_PRECOMP
41
42#include "wx/statline.h"
91f43f15 43#include "wx/artprov.h"
94f53923 44#include "wx/settings.h"
c50f1fb9
VZ
45
46#include "wx/tipdlg.h"
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
d8d18184 52static const int wxID_NEXT_TIP = 32000; // whatever
c50f1fb9 53
8b3d02a1
WS
54// ---------------------------------------------------------------------------
55// macros
56// ---------------------------------------------------------------------------
57
58/* Macro for avoiding #ifdefs when value have to be different depending on size of
9a357011 59 device we display on - take it from something like wxDesktopPolicy in the future
8b3d02a1
WS
60 */
61
62#if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
64#else
65 #define wxLARGESMALL(large,small) large
66#endif
67
c50f1fb9
VZ
68// ----------------------------------------------------------------------------
69// private classes
70// ----------------------------------------------------------------------------
71
72// an implementation which takes the tips from the text file - each line
73// represents a tip
12f190b0 74class WXDLLIMPEXP_ADV wxFileTipProvider : public wxTipProvider
c50f1fb9
VZ
75{
76public:
77 wxFileTipProvider(const wxString& filename, size_t currentTip);
78
79 virtual wxString GetTip();
80
81private:
82 wxTextFile m_textfile;
fc7a2a60
VZ
83
84 DECLARE_NO_COPY_CLASS(wxFileTipProvider)
c50f1fb9
VZ
85};
86
87#ifdef __WIN32__
88// TODO an implementation which takes the tips from the given registry key
12f190b0 89class WXDLLIMPEXP_ADV wxRegTipProvider : public wxTipProvider
c50f1fb9
VZ
90{
91public:
92 wxRegTipProvider(const wxString& keyname);
93
94 virtual wxString GetTip();
95};
06d7fdef
RD
96
97// Empty implementation for now to keep the linker happy
98wxString wxRegTipProvider::GetTip()
99{
abceee76 100 return wxEmptyString;
06d7fdef
RD
101}
102
c50f1fb9
VZ
103#endif // __WIN32__
104
105// the dialog we show in wxShowTip()
12f190b0 106class WXDLLIMPEXP_ADV wxTipDialog : public wxDialog
c50f1fb9
VZ
107{
108public:
109 wxTipDialog(wxWindow *parent,
110 wxTipProvider *tipProvider,
111 bool showAtStartup);
112
dabbc6a5 113 // the tip dialog has "Show tips on startup" checkbox - return true if it
c50f1fb9
VZ
114 // was checked (or wasn't unchecked)
115 bool ShowTipsOnStartup() const { return m_checkbox->GetValue(); }
116
117 // sets the (next) tip text
118 void SetTipText() { m_text->SetValue(m_tipProvider->GetTip()); }
119
120 // "Next" button handler
121 void OnNextTip(wxCommandEvent& WXUNUSED(event)) { SetTipText(); }
122
123private:
124 wxTipProvider *m_tipProvider;
125
126 wxTextCtrl *m_text;
127 wxCheckBox *m_checkbox;
128
129 DECLARE_EVENT_TABLE()
22f3361e 130 DECLARE_NO_COPY_CLASS(wxTipDialog)
c50f1fb9
VZ
131};
132
133// ============================================================================
134// implementation
135// ============================================================================
136
137// ----------------------------------------------------------------------------
138// wxFileTipProvider
139// ----------------------------------------------------------------------------
140
141wxFileTipProvider::wxFileTipProvider(const wxString& filename,
142 size_t currentTip)
143 : wxTipProvider(currentTip), m_textfile(filename)
144{
145 m_textfile.Open();
146}
147
148wxString wxFileTipProvider::GetTip()
149{
150 size_t count = m_textfile.GetLineCount();
151 if ( !count )
c50f1fb9 152 {
70373b5a 153 return _("Tips not available, sorry!");
c50f1fb9 154 }
dabbc6a5 155
70373b5a
JS
156 wxString tip;
157
158 // Comments start with a # symbol.
159 // Loop reading lines until get the first one that isn't a comment.
dabbc6a5
DS
160 // The max number of loop executions is the number of lines in the
161 // textfile so that can't go into an eternal loop in the [oddball]
162 // case of a comment-only tips file, or the developer has vetoed
70373b5a
JS
163 // them all via PreprecessTip().
164 for ( size_t i=0; i < count; i++ )
dabbc6a5
DS
165 {
166 // The current tip may be at the last line of the textfile, (or
167 // past it, if the number of lines in the textfile changed, such
70373b5a
JS
168 // as changing to a different textfile, with less tips). So check
169 // to see at last line of text file, (or past it)...
170 if ( m_currentTip >= count )
171 {
172 // .. and if so, wrap back to line 0.
173 m_currentTip = 0;
dabbc6a5
DS
174 }
175
70373b5a
JS
176 // Read the tip, and increment the current tip counter.
177 tip = m_textfile.GetLine(m_currentTip++);
dabbc6a5
DS
178
179 // Allow a derived class's overrided virtual to modify the tip
70373b5a 180 // now if so desired.
dabbc6a5
DS
181 tip = PreprocessTip(tip);
182
70373b5a
JS
183 // Break if tip isn't a comment, and isn't an empty string
184 // (or only stray space characters).
185 if ( !tip.StartsWith(wxT("#")) && (tip.Trim() != wxEmptyString) )
186 {
187 break;
188 }
189 }
dabbc6a5 190
70373b5a
JS
191 // If tip starts with '_(', then it is a gettext string of format
192 // _("My \"global\" tip text") so first strip off the leading '_("'...
193 if ( tip.StartsWith(wxT("_(\"" ), &tip))
194 {
195 //...and strip off the trailing '")'...
196 tip = tip.BeforeLast(wxT('\"'));
dabbc6a5
DS
197 // ...and replace escaped quotes
198 tip.Replace(wxT("\\\""), wxT("\""));
321fe98c
VZ
199
200 // and translate it as requested
201 tip = wxGetTranslation(tip);
dabbc6a5
DS
202 }
203
204 return tip;
c50f1fb9
VZ
205}
206
207// ----------------------------------------------------------------------------
208// wxTipDialog
209// ----------------------------------------------------------------------------
210
211BEGIN_EVENT_TABLE(wxTipDialog, wxDialog)
65fd5cb0 212 EVT_BUTTON(wxID_NEXT_TIP, wxTipDialog::OnNextTip)
eb3e6de3 213 EVT_BUTTON(wxID_CLOSE, wxTipDialog::OnCancel)
c50f1fb9
VZ
214END_EVENT_TABLE()
215
216wxTipDialog::wxTipDialog(wxWindow *parent,
217 wxTipProvider *tipProvider,
218 bool showAtStartup)
dabbc6a5 219 : wxDialog(parent, wxID_ANY, _("Tip of the Day"),
c50f1fb9 220 wxDefaultPosition, wxDefaultSize,
f43255e8 221 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
aa66250b 222 )
c50f1fb9
VZ
223{
224 m_tipProvider = tipProvider;
94f53923 225 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c50f1fb9 226
92afa2b1 227 // 1) create all controls in tab order
abceee76 228
9a357011 229 // smart phones does not support or do not waste space for wxButtons
8b3d02a1 230#ifndef __SMARTPHONE__
eb3e6de3 231 wxButton *btnClose = new wxButton(this, wxID_CLOSE);
8b3d02a1 232#endif
abceee76 233
dabbc6a5 234 m_checkbox = new wxCheckBox(this, wxID_ANY, _("&Show tips at startup"));
92afa2b1 235 m_checkbox->SetValue(showAtStartup);
abceee76 236
9a357011 237 // smart phones does not support or do not waste space for wxButtons
8b3d02a1 238#ifndef __SMARTPHONE__
e16ebee6 239 wxButton *btnNext = new wxButton(this, wxID_NEXT_TIP, _("&Next Tip"));
8b3d02a1 240#endif
c50f1fb9 241
4e4688be
VS
242 wxStaticText *text = new wxStaticText(this, wxID_ANY, _("Did you know..."));
243
94f53923
JS
244 if (!isPda)
245 {
246 wxFont font = text->GetFont();
247 font.SetPointSize(int(1.6 * font.GetPointSize()));
248 font.SetWeight(wxFONTWEIGHT_BOLD);
249 text->SetFont(font);
250 }
c50f1fb9 251
dabbc6a5 252 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
92afa2b1 253 wxDefaultPosition, wxSize(200, 160),
abceee76
VZ
254 wxTE_MULTILINE |
255 wxTE_READONLY |
58ec2255 256 wxTE_NO_VSCROLL |
e111012a 257 wxTE_RICH2 | // a hack to get rid of vert scrollbar
aa66250b
JS
258 wxDEFAULT_CONTROL_BORDER
259 );
e16ebee6 260#if defined(__WXMSW__)
58ec2255 261 m_text->SetFont(wxFont(12, wxSWISS, wxNORMAL, wxNORMAL));
e16ebee6 262#endif
c50f1fb9 263
e225b106 264//#if defined(__WXPM__)
1c4fbbf1
DW
265 //
266 // The only way to get icons into an OS/2 static bitmap control
267 //
e225b106 268// wxBitmap vBitmap;
1c4fbbf1 269
e225b106
DW
270// vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
271// // and for StatBmp's under OS/2 it MUST be a valid resource ID.
272//
dabbc6a5 273// wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
e225b106
DW
274//
275//#else
1c4fbbf1 276
91f43f15 277 wxIcon icon = wxArtProvider::GetIcon(wxART_TIP, wxART_CMN_DIALOG);
dabbc6a5 278 wxStaticBitmap *bmp = new wxStaticBitmap(this, wxID_ANY, icon);
c50f1fb9 279
e225b106 280//#endif
1c4fbbf1 281
92afa2b1 282 // 2) put them in boxes
c50f1fb9 283
92afa2b1 284 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
abceee76 285
92afa2b1
RR
286 wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
287 icon_text->Add( bmp, 0, wxCENTER );
8b3d02a1
WS
288 icon_text->Add( text, 1, wxCENTER | wxLEFT, wxLARGESMALL(20,0) );
289 topsizer->Add( icon_text, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
abceee76 290
8b3d02a1 291 topsizer->Add( m_text, 1, wxEXPAND | wxLEFT|wxRIGHT, wxLARGESMALL(10,0) );
92afa2b1
RR
292
293 wxBoxSizer *bottom = new wxBoxSizer( wxHORIZONTAL );
94f53923
JS
294 if (isPda)
295 topsizer->Add( m_checkbox, 0, wxCENTER|wxTOP );
296 else
297 bottom->Add( m_checkbox, 0, wxCENTER );
8b3d02a1 298
9a357011
WS
299 // smart phones does not support or do not waste space for wxButtons
300#ifdef __SMARTPHONE__
301 SetRightMenu(wxID_NEXT_TIP, _("Next"));
eb3e6de3 302 SetLeftMenu(wxID_CLOSE);
9a357011 303#else
94f53923
JS
304 if (!isPda)
305 bottom->Add( 10,10,1 );
8b3d02a1
WS
306 bottom->Add( btnNext, 0, wxCENTER | wxLEFT, wxLARGESMALL(10,0) );
307 bottom->Add( btnClose, 0, wxCENTER | wxLEFT, wxLARGESMALL(10,0) );
308#endif
309
94f53923
JS
310 if (isPda)
311 topsizer->Add( bottom, 0, wxCENTER | wxALL, 5 );
312 else
313 topsizer->Add( bottom, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
c50f1fb9
VZ
314
315 SetTipText();
abceee76 316
92afa2b1 317 SetSizer( topsizer );
abceee76 318
92afa2b1
RR
319 topsizer->SetSizeHints( this );
320 topsizer->Fit( this );
c50f1fb9
VZ
321
322 Centre(wxBOTH | wxCENTER_FRAME);
c50f1fb9
VZ
323}
324
325// ----------------------------------------------------------------------------
326// our public interface
327// ----------------------------------------------------------------------------
328
329wxTipProvider *wxCreateFileTipProvider(const wxString& filename,
330 size_t currentTip)
331{
332 return new wxFileTipProvider(filename, currentTip);
333}
334
335bool wxShowTip(wxWindow *parent,
336 wxTipProvider *tipProvider,
337 bool showAtStartup)
338{
339 wxTipDialog dlg(parent, tipProvider, showAtStartup);
340 dlg.ShowModal();
341
342 return dlg.ShowTipsOnStartup();
343}
344
345#endif // wxUSE_STARTUP_TIPS