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
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include "trayIcon.h"
#include "../library/resources.h"
#include "smallDialogs.h"
#include "../library/statusHandler.h"
#include <wx/taskbar.h>
enum Selection
{
CONTEXT_RESTORE,
CONTEXT_ABOUT
};
class MinimizeToTray::TaskBarImpl : public wxTaskBarIcon
{
public:
TaskBarImpl(MinimizeToTray* parent) : parent_(parent) {}
void parentHasDied()
{
parent_ = NULL;
}
private:
virtual wxMenu* CreatePopupMenu()
{
if (!parent_)
return NULL;
wxMenu* contextMenu = new wxMenu;
contextMenu->Append(CONTEXT_ABOUT, _("&About..."));
contextMenu->AppendSeparator();
contextMenu->Append(CONTEXT_RESTORE, _("&Restore"));
//event handling
contextMenu->Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MinimizeToTray::OnContextMenuSelection), NULL, parent_);
return contextMenu; //ownership transferred to library
}
MinimizeToTray* parent_;
};
MinimizeToTray::MinimizeToTray(wxTopLevelWindow* callerWnd, wxWindow* secondWnd) :
callerWnd_(callerWnd),
secondWnd_(secondWnd),
trayIcon(new TaskBarImpl(this))
{
trayIcon->SetIcon(*GlobalResources::getInstance().programIcon, wxT("FreeFileSync"));
trayIcon->Connect(wxEVT_TASKBAR_LEFT_DCLICK, wxCommandEventHandler(MinimizeToTray::OnDoubleClick), NULL, this); //register double-click
if (callerWnd_)
callerWnd_->Hide();
if (secondWnd_)
secondWnd_->Hide();
}
MinimizeToTray::~MinimizeToTray()
{
resumeFromTray();
}
void MinimizeToTray::resumeFromTray() //remove trayIcon and restore windows: MinimizeToTray is now a zombie object...
{
if (trayIcon)
{
if (secondWnd_)
secondWnd_->Show();
if (callerWnd_) //usecase: avoid dialog flashing in batch silent mode
{
callerWnd_->Iconize(false);
callerWnd_->Show();
callerWnd_->Raise();
callerWnd_->SetFocus();
}
trayIcon->RemoveIcon(); //hide icon until final deletion takes place
trayIcon->Disconnect(wxEVT_TASKBAR_LEFT_DCLICK, wxCommandEventHandler(MinimizeToTray::OnDoubleClick), NULL, this);
//delayed destruction: delete during next idle loop iteration (handle late window messages, e.g. when double-clicking)
if (!wxPendingDelete.Member(trayIcon))
wxPendingDelete.Append(trayIcon);
trayIcon->parentHasDied(); //TaskBarImpl (potentially) has longer lifetime than MinimizeToTray: avoid callback!
trayIcon = NULL; //avoid reentrance
}
}
void MinimizeToTray::setToolTip(const wxString& toolTipText)
{
if (trayIcon)
trayIcon->SetIcon(*GlobalResources::getInstance().programIcon, toolTipText);
}
void MinimizeToTray::keepHidden()
{
callerWnd_ = NULL;
secondWnd_ = NULL;
}
void MinimizeToTray::OnContextMenuSelection(wxCommandEvent& event)
{
const Selection eventId = static_cast<Selection>(event.GetId());
switch (eventId)
{
case CONTEXT_ABOUT:
{
AboutDlg* aboutDlg = new AboutDlg(NULL);
aboutDlg->ShowModal();
aboutDlg->Destroy();
}
break;
case CONTEXT_RESTORE:
resumeFromTray();
}
}
void MinimizeToTray::OnDoubleClick(wxCommandEvent& event)
{
resumeFromTray();
}
|