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