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