]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipdlg.cpp
Typos.
[wxWidgets.git] / src / generic / tipdlg.cpp
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__
21 #pragma implementation "tipdlg.h"
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"
37 #include "wx/statbmp.h"
38 #include "wx/dialog.h"
39 #include "wx/icon.h"
40 #include "wx/intl.h"
41 #include "wx/layout.h"
42 #include "wx/settings.h"
43 #include "wx/textctrl.h"
44 #endif // WX_PRECOMP
45
46 #include "wx/statline.h"
47
48 #include "wx/tipdlg.h"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 static const int wxID_NEXT_TIP = -100; // whatever
55
56 // ----------------------------------------------------------------------------
57 // private classes
58 // ----------------------------------------------------------------------------
59
60 // an implementation which takes the tips from the text file - each line
61 // represents a tip
62 class WXDLLEXPORT wxFileTipProvider : public wxTipProvider
63 {
64 public:
65 wxFileTipProvider(const wxString& filename, size_t currentTip);
66
67 virtual wxString GetTip();
68
69 private:
70 wxTextFile m_textfile;
71 };
72
73 #ifdef __WIN32__
74 // TODO an implementation which takes the tips from the given registry key
75 class WXDLLEXPORT wxRegTipProvider : public wxTipProvider
76 {
77 public:
78 wxRegTipProvider(const wxString& keyname);
79
80 virtual wxString GetTip();
81 };
82 #endif // __WIN32__
83
84 // the dialog we show in wxShowTip()
85 class WXDLLEXPORT wxTipDialog : public wxDialog
86 {
87 public:
88 wxTipDialog(wxWindow *parent,
89 wxTipProvider *tipProvider,
90 bool showAtStartup);
91
92 // the tip dialog has "Show tips on startup" checkbox - return TRUE if it
93 // was checked (or wasn't unchecked)
94 bool ShowTipsOnStartup() const { return m_checkbox->GetValue(); }
95
96 // sets the (next) tip text
97 void SetTipText() { m_text->SetValue(m_tipProvider->GetTip()); }
98
99 // "Next" button handler
100 void OnNextTip(wxCommandEvent& WXUNUSED(event)) { SetTipText(); }
101
102 private:
103 wxTipProvider *m_tipProvider;
104
105 wxTextCtrl *m_text;
106 wxCheckBox *m_checkbox;
107
108 DECLARE_EVENT_TABLE()
109 };
110
111 // ============================================================================
112 // implementation
113 // ============================================================================
114
115 // ----------------------------------------------------------------------------
116 // wxFileTipProvider
117 // ----------------------------------------------------------------------------
118
119 wxFileTipProvider::wxFileTipProvider(const wxString& filename,
120 size_t currentTip)
121 : wxTipProvider(currentTip), m_textfile(filename)
122 {
123 m_textfile.Open();
124 }
125
126 wxString wxFileTipProvider::GetTip()
127 {
128 size_t count = m_textfile.GetLineCount();
129 if ( !count )
130 return _("Tips not available, sorry!");
131
132 // notice that it may be greater, actually, if we remembered it from the
133 // last time and the number of tips changed
134 if ( m_currentTip == count )
135 {
136 // wrap
137 m_currentTip = 0;
138 }
139
140 return m_textfile.GetLine(m_currentTip++);
141 }
142
143 // ----------------------------------------------------------------------------
144 // wxTipDialog
145 // ----------------------------------------------------------------------------
146
147 BEGIN_EVENT_TABLE(wxTipDialog, wxDialog)
148 EVT_BUTTON(wxID_NEXT_TIP, OnNextTip)
149 END_EVENT_TABLE()
150
151 wxTipDialog::wxTipDialog(wxWindow *parent,
152 wxTipProvider *tipProvider,
153 bool showAtStartup)
154 : wxDialog(parent, -1, _("Tip of the Day"),
155 wxDefaultPosition, wxDefaultSize,
156 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
157 {
158 m_tipProvider = tipProvider;
159
160 wxSize sizeBtn = GetStandardButtonSize();
161 wxLayoutConstraints *c;
162
163 // create the controls in the right order, then set the constraints
164 wxButton *btnClose = new wxButton(this, wxID_CANCEL, _("&Close"));
165 m_checkbox = new wxCheckBox(this, -1, _("&Show tips at startup"));
166 wxButton *btnNext = new wxButton(this, wxID_NEXT_TIP, _("&Next"));
167
168 wxTextCtrl *text = new wxTextCtrl(this, -1, _("Did you know..."),
169 wxDefaultPosition, wxDefaultSize,
170 wxTE_READONLY | wxNO_BORDER);
171 text->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxBOLD));
172 text->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE));
173
174 m_text = new wxTextCtrl(this, -1, _T(""),
175 wxDefaultPosition, wxDefaultSize,
176 wxTE_MULTILINE | wxTE_READONLY | wxSUNKEN_BORDER);
177 m_text->SetFont(wxFont(14, wxROMAN, wxNORMAL, wxNORMAL));
178
179 #ifdef __WXMSW__
180 wxIcon icon("wxICON_TIP");
181 #else
182 #include "wx/generic/tip.xpm"
183 wxIcon icon(tipIcon);
184 #endif
185
186 wxStaticBitmap *bmp = new wxStaticBitmap(this, -1, icon);
187
188 const int iconSize = icon.GetWidth();
189
190 c = new wxLayoutConstraints;
191 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
192 c->left.RightOf(bmp, 2*LAYOUT_X_MARGIN);
193 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
194 c->height.Absolute(2*text->GetSize().GetHeight());
195 text->SetConstraints(c);
196
197 c = new wxLayoutConstraints;
198 c->centreY.SameAs(text, wxCentreY);
199 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
200 c->width.Absolute(iconSize);
201 c->height.Absolute(iconSize);
202 bmp->SetConstraints(c);
203
204 c = new wxLayoutConstraints;
205 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_X_MARGIN);
206 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
207 c->width.Absolute(sizeBtn.GetWidth());
208 c->height.Absolute(sizeBtn.GetHeight());
209 btnClose->SetConstraints(c);
210
211 c = new wxLayoutConstraints;
212 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_X_MARGIN);
213 c->right.LeftOf(btnClose, 2*LAYOUT_X_MARGIN);
214 c->width.Absolute(sizeBtn.GetWidth());
215 c->height.Absolute(sizeBtn.GetHeight());
216 btnNext->SetConstraints(c);
217
218 c = new wxLayoutConstraints;
219 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_X_MARGIN);
220 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
221 c->width.AsIs();
222 c->height.AsIs();
223 m_checkbox->SetConstraints(c);
224 m_checkbox->SetValue(showAtStartup);
225
226 c = new wxLayoutConstraints;
227 c->top.Below(text);
228 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
229 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
230 c->bottom.Above(btnClose, -2*LAYOUT_Y_MARGIN);
231 m_text->SetConstraints(c);
232
233 SetTipText();
234
235 Centre(wxBOTH | wxCENTER_FRAME);
236
237 wxSize size(5*sizeBtn.GetWidth(), 10*sizeBtn.GetHeight());
238 SetSize(size);
239 SetSizeHints(size.x, size.y);
240
241 SetAutoLayout(TRUE);
242 }
243
244 // ----------------------------------------------------------------------------
245 // our public interface
246 // ----------------------------------------------------------------------------
247
248 wxTipProvider *wxCreateFileTipProvider(const wxString& filename,
249 size_t currentTip)
250 {
251 return new wxFileTipProvider(filename, currentTip);
252 }
253
254 bool wxShowTip(wxWindow *parent,
255 wxTipProvider *tipProvider,
256 bool showAtStartup)
257 {
258 wxTipDialog dlg(parent, tipProvider, showAtStartup);
259 dlg.ShowModal();
260
261 return dlg.ShowTipsOnStartup();
262 }
263
264 #endif // wxUSE_STARTUP_TIPS
265