-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrayIcon.cpp
114 lines (93 loc) · 2.93 KB
/
TrayIcon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "stdafx.h"
#include "TrayIcon.h"
#include "Application.h"
#include "resource.h"
using namespace PowerMateTray;
TrayIcon::TrayIcon( Application* application )
: m_Application( application )
, m_Menu( NULL )
, m_UpdateMenuItem( NULL )
, m_BusyCount( 0 )
, m_IsMenuShowing( false )
{
SetIcon( wxICON( LOGO_ICON ), "Initializing PowerMateTray..." );
// Connect Events
Connect( wxID_ANY, wxEVT_TASKBAR_CLICK, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
Connect( wxID_ANY, wxEVT_TASKBAR_LEFT_UP, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
Connect( EventIDs::Exit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( TrayIcon::OnMenuExit ), NULL, this );
}
TrayIcon::~TrayIcon()
{
// Disconnect Events
Disconnect( wxID_ANY, wxEVT_TASKBAR_CLICK, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
Disconnect( wxID_ANY, wxEVT_TASKBAR_LEFT_UP, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
Disconnect( EventIDs::Exit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( TrayIcon::OnMenuExit ), NULL, this );
}
void TrayIcon::Initialize()
{
Refresh();
}
void TrayIcon::Cleanup()
{
RemoveIcon();
delete m_Menu;
m_Menu = NULL;
}
void TrayIcon::OnTrayIconClick( wxTaskBarIconEvent& evt )
{
if ( m_Menu )
{
m_IsMenuShowing = true;
PopupMenu( m_Menu );
m_IsMenuShowing = false;
}
}
void TrayIcon::OnMenuExit( wxCommandEvent& evt )
{
wxExit();
}
void TrayIcon::BeginBusy()
{
if ( ++m_BusyCount > 1 )
return;
// Disconnect events
Disconnect( wxID_ANY, wxEVT_TASKBAR_CLICK, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
Disconnect( wxID_ANY, wxEVT_TASKBAR_LEFT_UP, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
// Clear the current menu and change the icon to notify the user that things are happening
SetIcon( wxICON( BUSY_ICON ), wxString( m_Application->m_Title.c_str() ) + " Refreshing..." );
}
void TrayIcon::EndBusy()
{
if ( --m_BusyCount > 0 )
return;
assert( m_BusyCount == 0 );
// Re-connect events
Connect( wxID_ANY, wxEVT_TASKBAR_CLICK, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
Connect( wxID_ANY, wxEVT_TASKBAR_LEFT_UP, wxTaskBarIconEventHandler( TrayIcon::OnTrayIconClick ), NULL, this );
// Set the icon back
SetIcon(wxICON(LOGO_ICON), m_Application->m_Title.c_str());
}
void TrayIcon::Refresh()
{
BeginBusy();
{
if ( !m_Menu )
{
m_Menu = new wxMenu();
m_Menu->Append( new wxMenuItem( m_Menu, EventIDs::Exit, wxString( wxT("Exit PowerMateTray v" VERSION_STRING) ) , wxEmptyString, wxITEM_NORMAL ) );
}
else
{
wxMenuItemList list = m_Menu->GetMenuItems();
for ( wxMenuItemList::const_iterator itr = list.begin(), end = list.end(); itr != end; ++itr )
{
if ( (*itr)->GetId() < EventIDs::First || (*itr)->GetId() > EventIDs::Last )
{
m_Menu->Remove( *itr );
delete *itr;
}
}
}
}
EndBusy();
}