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