]> git.saurik.com Git - wxWidgets.git/blob - src/generic/notifmsgg.cpp
fe90a69f450a0ff95b04bb1b4811478800b476d5
[wxWidgets.git] / src / generic / notifmsgg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/notifmsgg.cpp
3 // Purpose: generic implementation of wxGenericNotificationMessage
4 // Author: Vadim Zeitlin
5 // Created: 2007-11-24
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef wxUSE_LIBHILDON
27 #define wxUSE_LIBHILDON 0
28 #endif
29
30 #ifndef wxUSE_LIBHILDON2
31 #define wxUSE_LIBHILDON2 0
32 #endif
33
34 #if wxUSE_NOTIFICATION_MESSAGE && (!wxUSE_LIBHILDON || !wxUSE_LIBHILDON2)
35
36 #ifndef WX_PRECOMP
37 #include "wx/dialog.h"
38 #include "wx/timer.h"
39 #include "wx/sizer.h"
40 #include "wx/statbmp.h"
41 #endif //WX_PRECOMP
42
43 #include "wx/artprov.h"
44
45 // even if the platform has the native implementation, we still normally want
46 // to use the generic one (unless it's totally unsuitable for the target UI as
47 // is the case of Hildon) because it may provide more features, so include
48 // wx/generic/notifmsg.h to get wxGenericNotificationMessage declaration even
49 // if wx/notifmsg.h only declares wxNotificationMessage itself (if it already
50 // uses the generic version, the second inclusion will do no harm)
51 #include "wx/notifmsg.h"
52 #include "wx/generic/notifmsg.h"
53
54 // ----------------------------------------------------------------------------
55 // wxNotificationMessageDialog
56 // ----------------------------------------------------------------------------
57
58 class wxNotificationMessageDialog : public wxDialog
59 {
60 public:
61 wxNotificationMessageDialog(wxWindow *parent,
62 const wxString& text,
63 int timeout,
64 int flags);
65
66 void Set(wxWindow *parent,
67 const wxString& text,
68 int timeout,
69 int flags);
70
71 bool IsAutomatic() const { return m_timer.IsRunning(); }
72 void SetDeleteOnHide() { m_deleteOnHide = true; }
73
74 private:
75 void OnClose(wxCloseEvent& event);
76 void OnTimer(wxTimerEvent& event);
77
78 // if true, delete the dialog when it should disappear, otherwise just hide
79 // it (initially false)
80 bool m_deleteOnHide;
81
82 // timer which will hide this dialog when it expires, if it's not running
83 // it means we were created without timeout
84 wxTimer m_timer;
85
86
87 DECLARE_EVENT_TABLE()
88 wxDECLARE_NO_COPY_CLASS(wxNotificationMessageDialog);
89 };
90
91 // ============================================================================
92 // wxNotificationMessageDialog implementation
93 // ============================================================================
94
95 BEGIN_EVENT_TABLE(wxNotificationMessageDialog, wxDialog)
96 EVT_CLOSE(wxNotificationMessageDialog::OnClose)
97
98 EVT_TIMER(wxID_ANY, wxNotificationMessageDialog::OnTimer)
99 END_EVENT_TABLE()
100
101 wxNotificationMessageDialog::wxNotificationMessageDialog(wxWindow *parent,
102 const wxString& text,
103 int timeout,
104 int flags)
105 : wxDialog(parent, wxID_ANY, _("Notice"),
106 wxDefaultPosition, wxDefaultSize,
107 0 /* no caption, no border styles */),
108 m_timer(this)
109 {
110 m_deleteOnHide = false;
111
112 Set(parent, text, timeout, flags);
113 }
114
115 void
116 wxNotificationMessageDialog::Set(wxWindow * WXUNUSED(parent),
117 const wxString& text,
118 int timeout,
119 int flags)
120 {
121 wxSizer * const sizerTop = new wxBoxSizer(wxHORIZONTAL);
122 if ( flags & wxICON_MASK )
123 {
124 sizerTop->Add(new wxStaticBitmap
125 (
126 this,
127 wxID_ANY,
128 wxArtProvider::GetMessageBoxIcon(flags)
129 ),
130 wxSizerFlags().Centre().Border());
131 }
132
133 sizerTop->Add(CreateTextSizer(text), wxSizerFlags(1).Border());
134 SetSizerAndFit(sizerTop);
135
136 if ( timeout != wxGenericNotificationMessage::Timeout_Never )
137 {
138 // wxTimer uses ms, timeout is in seconds
139 m_timer.Start(timeout*1000, true /* one shot only */);
140 }
141 else if ( m_timer.IsRunning() )
142 {
143 m_timer.Stop();
144 }
145 }
146
147 void wxNotificationMessageDialog::OnClose(wxCloseEvent& event)
148 {
149 if ( m_deleteOnHide )
150 {
151 // we don't need to keep this dialog alive any more
152 Destroy();
153 }
154 else // don't really close, just hide, as we can be shown again later
155 {
156 event.Veto();
157
158 Hide();
159 }
160 }
161
162 void wxNotificationMessageDialog::OnTimer(wxTimerEvent& WXUNUSED(event))
163 {
164 if ( m_deleteOnHide )
165 Destroy();
166 else
167 Hide();
168 }
169
170 // ============================================================================
171 // wxGenericNotificationMessage implementation
172 // ============================================================================
173
174 int wxGenericNotificationMessage::ms_timeout = 10;
175
176 /* static */ void wxGenericNotificationMessage::SetDefaultTimeout(int timeout)
177 {
178 wxASSERT_MSG( timeout > 0,
179 "negative or zero default timeout doesn't make sense" );
180
181 ms_timeout = timeout;
182 }
183
184 void wxGenericNotificationMessage::Init()
185 {
186 m_dialog = NULL;
187 }
188
189 wxGenericNotificationMessage::~wxGenericNotificationMessage()
190 {
191 if ( m_dialog->IsAutomatic() )
192 {
193 // we want to allow the user to create an automatically hidden
194 // notification just by creating a local wxGenericNotificationMessage object
195 // and so we shouldn't hide the notification when this object goes out
196 // of scope
197 m_dialog->SetDeleteOnHide();
198 }
199 else // manual dialog, hide it immediately
200 {
201 // OTOH for permanently shown dialogs only the code can hide them and
202 // if the object is deleted, we must do it now as it won't be
203 // accessible programmatically any more
204 delete m_dialog;
205 }
206 }
207
208 bool wxGenericNotificationMessage::Show(int timeout)
209 {
210 if ( timeout == Timeout_Auto )
211 {
212 timeout = GetDefaultTimeout();
213 }
214
215 if ( !m_dialog )
216 {
217 m_dialog = new wxNotificationMessageDialog
218 (
219 GetParent(),
220 GetFullMessage(),
221 timeout,
222 GetFlags()
223 );
224 }
225 else // update the existing dialog
226 {
227 m_dialog->Set(GetParent(), GetFullMessage(), timeout, GetFlags());
228 }
229
230 m_dialog->Show();
231
232 return true;
233 }
234
235 bool wxGenericNotificationMessage::Close()
236 {
237 if ( !m_dialog )
238 return false;
239
240 m_dialog->Hide();
241
242 return true;
243 }
244
245 #endif // wxUSE_NOTIFICATION_MESSAGE && (!wxUSE_LIBHILDON || !wxUSE_LIBHILDON2)