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