| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/generic/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 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_STARTUP_TIPS |
| 28 | |
| 29 | #ifndef WX_PRECOMP |
| 30 | #include "wx/button.h" |
| 31 | #include "wx/checkbox.h" |
| 32 | #include "wx/statbox.h" |
| 33 | #include "wx/dialog.h" |
| 34 | #include "wx/icon.h" |
| 35 | #include "wx/intl.h" |
| 36 | #include "wx/textctrl.h" |
| 37 | #include "wx/statbmp.h" |
| 38 | #include "wx/stattext.h" |
| 39 | #include "wx/sizer.h" |
| 40 | #include "wx/settings.h" |
| 41 | #endif // WX_PRECOMP |
| 42 | |
| 43 | #include "wx/statline.h" |
| 44 | #include "wx/artprov.h" |
| 45 | |
| 46 | #include "wx/tipdlg.h" |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | // constants |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | |
| 52 | static const int wxID_NEXT_TIP = 32000; // whatever |
| 53 | |
| 54 | // --------------------------------------------------------------------------- |
| 55 | // macros |
| 56 | // --------------------------------------------------------------------------- |
| 57 | |
| 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 |
| 60 | */ |
| 61 | |
| 62 | #if defined(__SMARTPHONE__) |
| 63 | #define wxLARGESMALL(large,small) small |
| 64 | #else |
| 65 | #define wxLARGESMALL(large,small) large |
| 66 | #endif |
| 67 | |
| 68 | // ---------------------------------------------------------------------------- |
| 69 | // private classes |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | |
| 72 | // an implementation which takes the tips from the text file - each line |
| 73 | // represents a tip |
| 74 | #if wxUSE_TEXTFILE |
| 75 | class WXDLLIMPEXP_ADV wxFileTipProvider : public wxTipProvider |
| 76 | { |
| 77 | public: |
| 78 | wxFileTipProvider(const wxString& filename, size_t currentTip); |
| 79 | |
| 80 | virtual wxString GetTip(); |
| 81 | |
| 82 | private: |
| 83 | wxTextFile m_textfile; |
| 84 | |
| 85 | wxDECLARE_NO_COPY_CLASS(wxFileTipProvider); |
| 86 | }; |
| 87 | #endif // wxUSE_TEXTFILE |
| 88 | |
| 89 | #ifdef __WIN32__ |
| 90 | // TODO an implementation which takes the tips from the given registry key |
| 91 | class WXDLLIMPEXP_ADV wxRegTipProvider : public wxTipProvider |
| 92 | { |
| 93 | public: |
| 94 | wxRegTipProvider(const wxString& keyname); |
| 95 | |
| 96 | virtual wxString GetTip(); |
| 97 | }; |
| 98 | |
| 99 | // Empty implementation for now to keep the linker happy |
| 100 | wxString wxRegTipProvider::GetTip() |
| 101 | { |
| 102 | return wxEmptyString; |
| 103 | } |
| 104 | |
| 105 | #endif // __WIN32__ |
| 106 | |
| 107 | // the dialog we show in wxShowTip() |
| 108 | class WXDLLIMPEXP_ADV wxTipDialog : public wxDialog |
| 109 | { |
| 110 | public: |
| 111 | wxTipDialog(wxWindow *parent, |
| 112 | wxTipProvider *tipProvider, |
| 113 | bool showAtStartup); |
| 114 | |
| 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(); } |
| 118 | |
| 119 | // sets the (next) tip text |
| 120 | void SetTipText() { m_text->SetValue(m_tipProvider->GetTip()); } |
| 121 | |
| 122 | // "Next" button handler |
| 123 | void OnNextTip(wxCommandEvent& WXUNUSED(event)) { SetTipText(); } |
| 124 | |
| 125 | private: |
| 126 | wxTipProvider *m_tipProvider; |
| 127 | |
| 128 | wxTextCtrl *m_text; |
| 129 | wxCheckBox *m_checkbox; |
| 130 | |
| 131 | DECLARE_EVENT_TABLE() |
| 132 | wxDECLARE_NO_COPY_CLASS(wxTipDialog); |
| 133 | }; |
| 134 | |
| 135 | // ============================================================================ |
| 136 | // implementation |
| 137 | // ============================================================================ |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | // wxFileTipProvider |
| 141 | // ---------------------------------------------------------------------------- |
| 142 | #if wxUSE_TEXTFILE |
| 143 | wxFileTipProvider::wxFileTipProvider(const wxString& filename, |
| 144 | size_t currentTip) |
| 145 | : wxTipProvider(currentTip), m_textfile(filename) |
| 146 | { |
| 147 | m_textfile.Open(); |
| 148 | } |
| 149 | |
| 150 | wxString wxFileTipProvider::GetTip() |
| 151 | { |
| 152 | size_t count = m_textfile.GetLineCount(); |
| 153 | if ( !count ) |
| 154 | { |
| 155 | return _("Tips not available, sorry!"); |
| 156 | } |
| 157 | |
| 158 | wxString tip; |
| 159 | |
| 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++ ) |
| 167 | { |
| 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 ) |
| 173 | { |
| 174 | // .. and if so, wrap back to line 0. |
| 175 | m_currentTip = 0; |
| 176 | } |
| 177 | |
| 178 | // Read the tip, and increment the current tip counter. |
| 179 | tip = m_textfile.GetLine(m_currentTip++); |
| 180 | |
| 181 | // Allow a derived class's overrided virtual to modify the tip |
| 182 | // now if so desired. |
| 183 | tip = PreprocessTip(tip); |
| 184 | |
| 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) ) |
| 188 | { |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 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)) |
| 196 | { |
| 197 | //...and strip off the trailing '")'... |
| 198 | tip = tip.BeforeLast(wxT('\"')); |
| 199 | // ...and replace escaped quotes |
| 200 | tip.Replace(wxT("\\\""), wxT("\"")); |
| 201 | |
| 202 | // and translate it as requested |
| 203 | tip = wxGetTranslation(tip); |
| 204 | } |
| 205 | |
| 206 | return tip; |
| 207 | } |
| 208 | #endif // wxUSE_TEXTFILE |
| 209 | |
| 210 | // ---------------------------------------------------------------------------- |
| 211 | // wxTipDialog |
| 212 | // ---------------------------------------------------------------------------- |
| 213 | |
| 214 | BEGIN_EVENT_TABLE(wxTipDialog, wxDialog) |
| 215 | EVT_BUTTON(wxID_NEXT_TIP, wxTipDialog::OnNextTip) |
| 216 | END_EVENT_TABLE() |
| 217 | |
| 218 | wxTipDialog::wxTipDialog(wxWindow *parent, |
| 219 | wxTipProvider *tipProvider, |
| 220 | bool showAtStartup) |
| 221 | : wxDialog(GetParentForModalDialog(parent, 0), wxID_ANY, _("Tip of the Day"), |
| 222 | wxDefaultPosition, wxDefaultSize, |
| 223 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER |
| 224 | ) |
| 225 | { |
| 226 | m_tipProvider = tipProvider; |
| 227 | bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA); |
| 228 | |
| 229 | // 1) create all controls in tab order |
| 230 | |
| 231 | wxStaticText *text = new wxStaticText(this, wxID_ANY, _("Did you know...")); |
| 232 | |
| 233 | if (!isPda) |
| 234 | { |
| 235 | wxFont font = text->GetFont(); |
| 236 | font.SetPointSize(int(1.6 * font.GetPointSize())); |
| 237 | font.SetWeight(wxFONTWEIGHT_BOLD); |
| 238 | text->SetFont(font); |
| 239 | } |
| 240 | |
| 241 | m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
| 242 | wxDefaultPosition, wxSize(200, 160), |
| 243 | wxTE_MULTILINE | |
| 244 | wxTE_READONLY | |
| 245 | wxTE_NO_VSCROLL | |
| 246 | wxTE_RICH2 | // a hack to get rid of vert scrollbar |
| 247 | wxDEFAULT_CONTROL_BORDER |
| 248 | ); |
| 249 | #if defined(__WXMSW__) |
| 250 | m_text->SetFont(wxFont(12, wxSWISS, wxNORMAL, wxNORMAL)); |
| 251 | #endif |
| 252 | |
| 253 | //#if defined(__WXPM__) |
| 254 | // |
| 255 | // The only way to get icons into an OS/2 static bitmap control |
| 256 | // |
| 257 | // wxBitmap vBitmap; |
| 258 | |
| 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. |
| 261 | // |
| 262 | // wxStaticBitmap* bmp = new wxStaticBitmap(this, wxID_ANY, vBitmap); |
| 263 | // |
| 264 | //#else |
| 265 | |
| 266 | wxIcon icon = wxArtProvider::GetIcon(wxART_TIP, wxART_CMN_DIALOG); |
| 267 | wxStaticBitmap *bmp = new wxStaticBitmap(this, wxID_ANY, icon); |
| 268 | |
| 269 | //#endif |
| 270 | |
| 271 | m_checkbox = new wxCheckBox(this, wxID_ANY, _("&Show tips at startup")); |
| 272 | m_checkbox->SetValue(showAtStartup); |
| 273 | m_checkbox->SetFocus(); |
| 274 | |
| 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")); |
| 278 | #endif |
| 279 | |
| 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); |
| 284 | #endif |
| 285 | |
| 286 | |
| 287 | // 2) put them in boxes |
| 288 | |
| 289 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
| 290 | |
| 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) ); |
| 295 | |
| 296 | topsizer->Add( m_text, 1, wxEXPAND | wxLEFT|wxRIGHT, wxLARGESMALL(10,0) ); |
| 297 | |
| 298 | wxBoxSizer *bottom = new wxBoxSizer( wxHORIZONTAL ); |
| 299 | if (isPda) |
| 300 | topsizer->Add( m_checkbox, 0, wxCENTER|wxTOP ); |
| 301 | else |
| 302 | bottom->Add( m_checkbox, 0, wxCENTER ); |
| 303 | |
| 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); |
| 308 | #else |
| 309 | if (!isPda) |
| 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) ); |
| 313 | #endif |
| 314 | |
| 315 | if (isPda) |
| 316 | topsizer->Add( bottom, 0, wxCENTER | wxALL, 5 ); |
| 317 | else |
| 318 | topsizer->Add( bottom, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) ); |
| 319 | |
| 320 | SetTipText(); |
| 321 | |
| 322 | SetSizer( topsizer ); |
| 323 | |
| 324 | topsizer->SetSizeHints( this ); |
| 325 | topsizer->Fit( this ); |
| 326 | |
| 327 | Centre(wxBOTH | wxCENTER_FRAME); |
| 328 | } |
| 329 | |
| 330 | // ---------------------------------------------------------------------------- |
| 331 | // our public interface |
| 332 | // ---------------------------------------------------------------------------- |
| 333 | |
| 334 | #if wxUSE_TEXTFILE |
| 335 | wxTipProvider *wxCreateFileTipProvider(const wxString& filename, |
| 336 | size_t currentTip) |
| 337 | { |
| 338 | return new wxFileTipProvider(filename, currentTip); |
| 339 | } |
| 340 | #endif // wxUSE_TEXTFILE |
| 341 | |
| 342 | bool wxShowTip(wxWindow *parent, |
| 343 | wxTipProvider *tipProvider, |
| 344 | bool showAtStartup) |
| 345 | { |
| 346 | wxTipDialog dlg(parent, tipProvider, showAtStartup); |
| 347 | dlg.ShowModal(); |
| 348 | |
| 349 | return dlg.ShowTipsOnStartup(); |
| 350 | } |
| 351 | |
| 352 | #endif // wxUSE_STARTUP_TIPS |