diff options
Diffstat (limited to 'ui/trayIcon.cpp')
-rw-r--r-- | ui/trayIcon.cpp | 179 |
1 files changed, 92 insertions, 87 deletions
diff --git a/ui/trayIcon.cpp b/ui/trayIcon.cpp index 921fe79f..b7f74d1d 100644 --- a/ui/trayIcon.cpp +++ b/ui/trayIcon.cpp @@ -1,10 +1,99 @@ +// ************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl.html * +// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) * +// ************************************************************************** +// #include "trayIcon.h" #include "../library/resources.h" #include "smallDialogs.h" #include <wx/taskbar.h> #include <cmath> +#include <wx/image.h> +#include <wx/menu.h> +#include <wx/icon.h> //req. by Linux + +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; +} +} + + +//------------------------------------------------------------------------------------------------ enum Selection { CONTEXT_RESTORE, @@ -46,7 +135,7 @@ MinimizeToTray::MinimizeToTray(wxTopLevelWindow* callerWnd, wxWindow* secondWnd) secondWnd_(secondWnd), trayIcon(new TaskBarImpl(this)) { - trayIcon->SetIcon(*GlobalResources::getInstance().programIcon, wxT("FreeFileSync")); + trayIcon->SetIcon(generateIcon(0), wxT("FreeFileSync")); trayIcon->Connect(wxEVT_TASKBAR_LEFT_DCLICK, wxCommandEventHandler(MinimizeToTray::OnDoubleClick), NULL, this); //register double-click if (callerWnd_) @@ -89,86 +178,6 @@ void MinimizeToTray::resumeFromTray() //remove trayIcon and restore windows: Mi } -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) @@ -189,12 +198,8 @@ void MinimizeToTray::OnContextMenuSelection(wxCommandEvent& event) switch (eventId) { case CONTEXT_ABOUT: - { - AboutDlg* aboutDlg = new AboutDlg(NULL); - aboutDlg->ShowModal(); - aboutDlg->Destroy(); - } - break; + FreeFileSync::showAboutDialog(); + break; case CONTEXT_RESTORE: resumeFromTray(); } |