Did somework on the generic dialogs,
[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/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/statbmp.h"
43 #include "wx/sizer.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, wxTipDialog::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 // 1) create all controls in tab order
168
169 wxButton *btnClose = new wxButton(this, wxID_CANCEL, _("&Close"));
170
171 m_checkbox = new wxCheckBox(this, -1, _("&Show tips at startup"));
172 m_checkbox->SetValue(showAtStartup);
173
174 wxButton *btnNext = new wxButton(this, wxID_NEXT_TIP, _("&Next"));
175
176 wxTextCtrl *text = new wxTextCtrl(this, -1, _("Did you know..."),
177 wxDefaultPosition, wxDefaultSize,
178 wxTE_READONLY | wxNO_BORDER);
179 text->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxBOLD));
180 text->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE));
181
182 m_text = new wxTextCtrl(this, -1, _T(""),
183 wxDefaultPosition, wxSize(200, 160),
184 wxTE_MULTILINE | wxTE_READONLY | wxSUNKEN_BORDER);
185 m_text->SetFont(wxFont(14, wxROMAN, wxNORMAL, wxNORMAL));
186
187 #if defined(__WXMSW__) || defined(__WXPM__)
188 wxIcon icon("wxICON_TIP");
189 #else
190 #include "wx/generic/tip.xpm"
191 wxIcon icon(tipIcon);
192 #endif
193 wxStaticBitmap *bmp = new wxStaticBitmap(this, -1, icon);
194
195 // 2) put them in boxes
196
197 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
198
199 wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
200 icon_text->Add( bmp, 0, wxCENTER );
201 icon_text->Add( text, 1, wxCENTER | wxLEFT, 10 );
202 topsizer->Add( icon_text, 0, wxEXPAND | wxALL, 10 );
203
204 topsizer->Add( m_text, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
205
206 wxBoxSizer *bottom = new wxBoxSizer( wxHORIZONTAL );
207 bottom->Add( m_checkbox, 0, wxCENTER );
208 bottom->Add( btnNext, 0, wxCENTER | wxLEFT, 10 );
209 bottom->Add( btnClose, 0, wxCENTER | wxLEFT, 10 );
210 topsizer->Add( bottom, 0, wxALIGN_RIGHT | wxALL, 10 );
211
212 SetTipText();
213
214 SetAutoLayout(TRUE);
215 SetSizer( topsizer );
216
217 topsizer->SetSizeHints( this );
218 topsizer->Fit( this );
219
220 Centre(wxBOTH | wxCENTER_FRAME);
221
222 }
223
224 // ----------------------------------------------------------------------------
225 // our public interface
226 // ----------------------------------------------------------------------------
227
228 wxTipProvider *wxCreateFileTipProvider(const wxString& filename,
229 size_t currentTip)
230 {
231 return new wxFileTipProvider(filename, currentTip);
232 }
233
234 bool wxShowTip(wxWindow *parent,
235 wxTipProvider *tipProvider,
236 bool showAtStartup)
237 {
238 wxTipDialog dlg(parent, tipProvider, showAtStartup);
239 dlg.ShowModal();
240
241 return dlg.ShowTipsOnStartup();
242 }
243
244 #endif // wxUSE_STARTUP_TIPS
245