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