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