summaryrefslogtreecommitdiff
path: root/ui/trayIcon.cpp
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:02:17 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:02:17 +0200
commitb9203ee84953006547f4afd58f405874c87bf0dc (patch)
tree9e41f1533f120e9268e86658c52458630ffd718a /ui/trayIcon.cpp
parent3.0 (diff)
downloadFreeFileSync-b9203ee84953006547f4afd58f405874c87bf0dc.tar.gz
FreeFileSync-b9203ee84953006547f4afd58f405874c87bf0dc.tar.bz2
FreeFileSync-b9203ee84953006547f4afd58f405874c87bf0dc.zip
3.1
Diffstat (limited to 'ui/trayIcon.cpp')
-rw-r--r--ui/trayIcon.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/ui/trayIcon.cpp b/ui/trayIcon.cpp
new file mode 100644
index 00000000..42847eb0
--- /dev/null
+++ b/ui/trayIcon.cpp
@@ -0,0 +1,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();
+}
bgstack15