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