1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipdlg.cpp
3 // Purpose: implementation of wxTipDialog
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_STARTUP_TIPS
30 #include "wx/button.h"
31 #include "wx/checkbox.h"
32 #include "wx/statbox.h"
33 #include "wx/dialog.h"
36 #include "wx/textctrl.h"
37 #include "wx/statbmp.h"
38 #include "wx/stattext.h"
40 #include "wx/settings.h"
43 #include "wx/statline.h"
44 #include "wx/artprov.h"
46 #include "wx/tipdlg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static const int wxID_NEXT_TIP
= 32000; // whatever
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 /* Macro for avoiding #ifdefs when value have to be different depending on size of
59 device we display on - take it from something like wxDesktopPolicy in the future
62 #if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
65 #define wxLARGESMALL(large,small) large
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // an implementation which takes the tips from the text file - each line
75 class WXDLLIMPEXP_ADV wxFileTipProvider
: public wxTipProvider
78 wxFileTipProvider(const wxString
& filename
, size_t currentTip
);
80 virtual wxString
GetTip();
83 wxTextFile m_textfile
;
85 wxDECLARE_NO_COPY_CLASS(wxFileTipProvider
);
87 #endif // wxUSE_TEXTFILE
90 // TODO an implementation which takes the tips from the given registry key
91 class WXDLLIMPEXP_ADV wxRegTipProvider
: public wxTipProvider
94 wxRegTipProvider(const wxString
& keyname
);
96 virtual wxString
GetTip();
99 // Empty implementation for now to keep the linker happy
100 wxString
wxRegTipProvider::GetTip()
102 return wxEmptyString
;
107 // the dialog we show in wxShowTip()
108 class WXDLLIMPEXP_ADV wxTipDialog
: public wxDialog
111 wxTipDialog(wxWindow
*parent
,
112 wxTipProvider
*tipProvider
,
115 // the tip dialog has "Show tips on startup" checkbox - return true if it
116 // was checked (or wasn't unchecked)
117 bool ShowTipsOnStartup() const { return m_checkbox
->GetValue(); }
119 // sets the (next) tip text
120 void SetTipText() { m_text
->SetValue(m_tipProvider
->GetTip()); }
122 // "Next" button handler
123 void OnNextTip(wxCommandEvent
& WXUNUSED(event
)) { SetTipText(); }
126 wxTipProvider
*m_tipProvider
;
129 wxCheckBox
*m_checkbox
;
131 DECLARE_EVENT_TABLE()
132 wxDECLARE_NO_COPY_CLASS(wxTipDialog
);
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 wxFileTipProvider::wxFileTipProvider(const wxString
& filename
,
145 : wxTipProvider(currentTip
), m_textfile(filename
)
150 wxString
wxFileTipProvider::GetTip()
152 size_t count
= m_textfile
.GetLineCount();
155 return _("Tips not available, sorry!");
160 // Comments start with a # symbol.
161 // Loop reading lines until get the first one that isn't a comment.
162 // The max number of loop executions is the number of lines in the
163 // textfile so that can't go into an eternal loop in the [oddball]
164 // case of a comment-only tips file, or the developer has vetoed
165 // them all via PreprecessTip().
166 for ( size_t i
=0; i
< count
; i
++ )
168 // The current tip may be at the last line of the textfile, (or
169 // past it, if the number of lines in the textfile changed, such
170 // as changing to a different textfile, with less tips). So check
171 // to see at last line of text file, (or past it)...
172 if ( m_currentTip
>= count
)
174 // .. and if so, wrap back to line 0.
178 // Read the tip, and increment the current tip counter.
179 tip
= m_textfile
.GetLine(m_currentTip
++);
181 // Allow a derived class's overrided virtual to modify the tip
182 // now if so desired.
183 tip
= PreprocessTip(tip
);
185 // Break if tip isn't a comment, and isn't an empty string
186 // (or only stray space characters).
187 if ( !tip
.StartsWith(wxT("#")) && (tip
.Trim() != wxEmptyString
) )
193 // If tip starts with '_(', then it is a gettext string of format
194 // _("My \"global\" tip text") so first strip off the leading '_("'...
195 if ( tip
.StartsWith(wxT("_(\"" ), &tip
))
197 //...and strip off the trailing '")'...
198 tip
= tip
.BeforeLast(wxT('\"'));
199 // ...and replace escaped quotes
200 tip
.Replace(wxT("\\\""), wxT("\""));
202 // and translate it as requested
203 tip
= wxGetTranslation(tip
);
208 #endif // wxUSE_TEXTFILE
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 BEGIN_EVENT_TABLE(wxTipDialog
, wxDialog
)
215 EVT_BUTTON(wxID_NEXT_TIP
, wxTipDialog::OnNextTip
)
218 wxTipDialog::wxTipDialog(wxWindow
*parent
,
219 wxTipProvider
*tipProvider
,
221 : wxDialog(GetParentForModalDialog(parent
), wxID_ANY
, _("Tip of the Day"),
222 wxDefaultPosition
, wxDefaultSize
,
223 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
226 m_tipProvider
= tipProvider
;
227 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
229 // 1) create all controls in tab order
231 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
, _("Did you know..."));
235 wxFont font
= text
->GetFont();
236 font
.SetPointSize(int(1.6 * font
.GetPointSize()));
237 font
.SetWeight(wxFONTWEIGHT_BOLD
);
241 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
242 wxDefaultPosition
, wxSize(200, 160),
246 wxTE_RICH2
| // a hack to get rid of vert scrollbar
247 wxDEFAULT_CONTROL_BORDER
249 #if defined(__WXMSW__)
250 m_text
->SetFont(wxFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
));
253 //#if defined(__WXPM__)
255 // The only way to get icons into an OS/2 static bitmap control
259 // vBitmap.SetId(wxICON_TIP); // OS/2 specific bitmap method--OS/2 wxBitmaps all have an ID.
260 // // and for StatBmp's under OS/2 it MUST be a valid resource ID.
262 // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap);
266 wxIcon icon
= wxArtProvider::GetIcon(wxART_TIP
, wxART_CMN_DIALOG
);
267 wxStaticBitmap
*bmp
= new wxStaticBitmap(this, wxID_ANY
, icon
);
271 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _("&Show tips at startup"));
272 m_checkbox
->SetValue(showAtStartup
);
273 m_checkbox
->SetFocus();
275 // smart phones does not support or do not waste space for wxButtons
276 #ifndef __SMARTPHONE__
277 wxButton
*btnNext
= new wxButton(this, wxID_NEXT_TIP
, _("&Next Tip"));
280 // smart phones does not support or do not waste space for wxButtons
281 #ifndef __SMARTPHONE__
282 wxButton
*btnClose
= new wxButton(this, wxID_CLOSE
);
283 SetAffirmativeId(wxID_CLOSE
);
287 // 2) put them in boxes
289 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
291 wxBoxSizer
*icon_text
= new wxBoxSizer( wxHORIZONTAL
);
292 icon_text
->Add( bmp
, 0, wxCENTER
);
293 icon_text
->Add( text
, 1, wxCENTER
| wxLEFT
, wxLARGESMALL(20,0) );
294 topsizer
->Add( icon_text
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
296 topsizer
->Add( m_text
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, wxLARGESMALL(10,0) );
298 wxBoxSizer
*bottom
= new wxBoxSizer( wxHORIZONTAL
);
300 topsizer
->Add( m_checkbox
, 0, wxCENTER
|wxTOP
);
302 bottom
->Add( m_checkbox
, 0, wxCENTER
);
304 // smart phones does not support or do not waste space for wxButtons
305 #ifdef __SMARTPHONE__
306 SetRightMenu(wxID_NEXT_TIP
, _("Next"));
307 SetLeftMenu(wxID_CLOSE
);
310 bottom
->Add( 10,10,1 );
311 bottom
->Add( btnNext
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
312 bottom
->Add( btnClose
, 0, wxCENTER
| wxLEFT
, wxLARGESMALL(10,0) );
316 topsizer
->Add( bottom
, 0, wxCENTER
| wxALL
, 5 );
318 topsizer
->Add( bottom
, 0, wxEXPAND
| wxALL
, wxLARGESMALL(10,0) );
322 SetSizer( topsizer
);
324 topsizer
->SetSizeHints( this );
325 topsizer
->Fit( this );
327 Centre(wxBOTH
| wxCENTER_FRAME
);
330 // ----------------------------------------------------------------------------
331 // our public interface
332 // ----------------------------------------------------------------------------
335 wxTipProvider
*wxCreateFileTipProvider(const wxString
& filename
,
338 return new wxFileTipProvider(filename
, currentTip
);
340 #endif // wxUSE_TEXTFILE
342 bool wxShowTip(wxWindow
*parent
,
343 wxTipProvider
*tipProvider
,
346 wxTipDialog
dlg(parent
, tipProvider
, showAtStartup
);
349 return dlg
.ShowTipsOnStartup();
352 #endif // wxUSE_STARTUP_TIPS