summaryrefslogtreecommitdiff
path: root/ui/trayIcon.cpp
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:04:59 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:04:59 +0200
commitf570e2f2685aa43aa518c2f8578391c1847cddbe (patch)
treeb9376b3a7e807c5e0c4cf3d5615c14034d9675d6 /ui/trayIcon.cpp
parent3.2 (diff)
downloadFreeFileSync-f570e2f2685aa43aa518c2f8578391c1847cddbe.tar.gz
FreeFileSync-f570e2f2685aa43aa518c2f8578391c1847cddbe.tar.bz2
FreeFileSync-f570e2f2685aa43aa518c2f8578391c1847cddbe.zip
3.3
Diffstat (limited to 'ui/trayIcon.cpp')
-rw-r--r--ui/trayIcon.cpp87
1 files changed, 84 insertions, 3 deletions
diff --git a/ui/trayIcon.cpp b/ui/trayIcon.cpp
index 478d135a..921fe79f 100644
--- a/ui/trayIcon.cpp
+++ b/ui/trayIcon.cpp
@@ -1,8 +1,8 @@
#include "trayIcon.h"
#include "../library/resources.h"
#include "smallDialogs.h"
-//#include "../library/statusHandler.h"
#include <wx/taskbar.h>
+#include <cmath>
enum Selection
@@ -89,10 +89,90 @@ void MinimizeToTray::resumeFromTray() //remove trayIcon and restore windows: Mi
}
-void MinimizeToTray::setToolTip(const wxString& toolTipText)
+namespace
+{
+inline
+int roundNum(double d) //little rounding function
+{
+ return static_cast<int>(d < 0 ? d - .5 : d + .5);
+}
+
+
+wxIcon generateIcon(size_t percent) //generate icon with progress indicator
+{
+ percent = std::min(percent, static_cast<size_t>(100u)); //handle invalid input
+
+#ifdef FFS_WIN
+ static const wxBitmap trayIcon = GlobalResources::getInstance().getImageByName(wxT("FFS_tray_win.png"));
+#elif defined FFS_LINUX
+ static const wxBitmap trayIcon = GlobalResources::getInstance().getImageByName(wxT("FFS_tray_linux.png"));
+#endif
+
+ const int indicatorHeight = roundNum((trayIcon.GetHeight() * percent) / 100.0);
+
+ //minor optimization
+ static std::pair<int, wxIcon> buffer = std::make_pair(-1, wxNullIcon);
+ if (buffer.first == indicatorHeight)
+ return buffer.second;
+
+ if ( trayIcon.GetWidth() > 0 &&
+ trayIcon.GetHeight() > 0)
+ {
+ static const int indicatorWidth = trayIcon.GetWidth() * .25;
+ const int indicatorXBegin = ceil((trayIcon.GetWidth() - indicatorWidth) / 2.0);
+ const int indicatorYBegin = trayIcon.GetHeight() - indicatorHeight;
+
+ wxImage genImage(trayIcon.ConvertToImage());
+
+ //draw progress indicator: do NOT use wxDC::DrawRectangle! Doesn't respect alpha in Windows, but does in Linux!
+ //We need a simple, working solution:
+ unsigned char* const data = genImage.GetData();
+ for (int row = indicatorYBegin; row < genImage.GetHeight(); ++row)
+ {
+ for (int col = indicatorXBegin; col < indicatorXBegin + indicatorWidth; ++col)
+ {
+ unsigned char* const pixelBegin = data + (row * genImage.GetWidth() + col) * 3;
+ pixelBegin[0] = 255; //red
+ pixelBegin[1] = 255; //green
+ pixelBegin[2] = 0; //blue
+ }
+ }
+
+ if (genImage.HasAlpha())
+ {
+ unsigned char* const alpha = genImage.GetAlpha();
+ //make progress indicator fully opaque:
+ for (int row = indicatorYBegin; row < genImage.GetHeight(); ++row)
+ ::memset(alpha + row * genImage.GetWidth() + indicatorXBegin, wxIMAGE_ALPHA_OPAQUE, indicatorWidth);
+ }
+
+ wxIcon genIcon;
+ genIcon.CopyFromBitmap(wxBitmap(genImage));
+
+ //fill buffer
+ buffer.first = indicatorHeight;
+ buffer.second = genIcon;
+
+ return genIcon;
+ }
+
+ //fallback
+ wxIcon defaultIcon;
+ defaultIcon.CopyFromBitmap(trayIcon);
+
+ //fill buffer
+ buffer.first = indicatorHeight;
+ buffer.second = defaultIcon;
+
+ return defaultIcon;
+}
+}
+
+
+void MinimizeToTray::setToolTip(const wxString& toolTipText, size_t percent)
{
if (trayIcon)
- trayIcon->SetIcon(*GlobalResources::getInstance().programIcon, toolTipText);
+ trayIcon->SetIcon(generateIcon(percent), toolTipText);
}
@@ -125,3 +205,4 @@ void MinimizeToTray::OnDoubleClick(wxCommandEvent& event)
{
resumeFromTray();
}
+
bgstack15