summaryrefslogtreecommitdiff
path: root/ui/tray_icon.cpp
blob: 278f1888309efb46cf0a9557a68276a711c15cd8 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// **************************************************************************
// * 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-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "tray_icon.h"
#include "../library/resources.h"
#include "small_dlgs.h"
#include "../shared/i18n.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);
}


void fillRange(wxImage& img, int pixelFirst, int pixelLast, const wxColor& col)
{
    const int pixelCount = img.GetWidth() >= 0 ? img.GetWidth() * img.GetHeight() : -1;

    if (0 <= pixelFirst && pixelFirst < pixelLast && pixelLast <= pixelCount)
    {
        unsigned char* const bytesBegin = img.GetData() + pixelFirst * 3;
        unsigned char* const bytesEnd   = img.GetData() + pixelLast  * 3;

        for (unsigned char* bytePos = bytesBegin; bytePos < bytesEnd; bytePos += 3)
        {
            bytePos[0] = col.Red  ();
            bytePos[1] = col.Green();
            bytePos[2] = col.Blue ();
        }

        if (img.HasAlpha()) //make progress indicator fully opaque:
            std::fill(img.GetAlpha() + pixelFirst, img.GetAlpha() + pixelLast, wxIMAGE_ALPHA_OPAQUE);
    }
}

wxIcon generateIcon(double percent) //generate icon with progress indicator
{
#ifdef FFS_WIN
    static const wxBitmap trayIcon = GlobalResources::instance().getImage(wxT("FFS_tray_win.png"));
#elif defined FFS_LINUX
    static const wxBitmap trayIcon = GlobalResources::instance().getImage(wxT("FFS_tray_linux.png"));
#endif

    const int pixelCount = trayIcon.GetWidth() * trayIcon.GetHeight();
    const int startFillPixel = std::min(roundNum(percent / 100.0 * pixelCount), pixelCount);

    //minor optimization
    static std::pair<int, wxIcon> buffer = std::make_pair(-1, wxNullIcon);
    if (buffer.first == startFillPixel)
        return buffer.second;

    wxIcon genIcon;

    wxImage genImage(trayIcon.ConvertToImage());
    if (genImage.GetWidth()  > 0 &&
        genImage.GetHeight() > 0)
    {
        //fill black border row
        if (startFillPixel <= pixelCount - genImage.GetWidth())
        {
            /*
                    --------
                    ---bbbbb
                    bbbbSyyy  S : start yellow remainder
                    yyyyyyyy
            */
            int bStart = startFillPixel - genImage.GetWidth();
            if (bStart % genImage.GetWidth() != 0) //add one more black pixel, see ascii-art
                --bStart;
            fillRange(genImage, std::max(bStart, 0), startFillPixel, *wxBLACK);
        }
        else if (startFillPixel != pixelCount)
        {
            //special handling for last row
            /*
                    --------
                    --------
                    ---bbbbb
                    ---bSyyy  S : start yellow remainder
            */
            int bStart = startFillPixel - genImage.GetWidth() - 1;
            int bEnd = (bStart / genImage.GetWidth() + 1) * (genImage.GetWidth());

            fillRange(genImage, std::max(bStart, 0), bEnd, *wxBLACK);
            fillRange(genImage, startFillPixel - 1, startFillPixel, *wxBLACK);
        }

        //fill yellow remainder
        fillRange(genImage, startFillPixel, pixelCount, wxColour(240, 200, 0));

        /*
                const int indicatorWidth  = genImage.GetWidth() * .4;
                const int indicatorXBegin = std::ceil((genImage.GetWidth() - indicatorWidth) / 2.0);
                const int indicatorYBegin = genImage.GetHeight() - indicatorHeight;

                //draw progress indicator: do NOT use wxDC::DrawRectangle! Doesn't respect alpha in Windows, but does in Linux!
                //We need a simple, working solution:

                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] = 240; //red
                        pixelBegin[1] = 200; //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);
                }
        */
        genIcon.CopyFromBitmap(wxBitmap(genImage));
    }
    else //fallback
        genIcon.CopyFromBitmap(trayIcon);

    //fill buffer
    buffer.first  = startFillPixel;
    buffer.second = genIcon;
    return genIcon;
}
}


//------------------------------------------------------------------------------------------------
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 caller
    }

    MinimizeToTray* parent_;
};


MinimizeToTray::MinimizeToTray(wxTopLevelWindow* callerWnd, wxTopLevelWindow* secondWnd) :
    callerWnd_(callerWnd),
    secondWnd_(secondWnd),
    trayIcon(new TaskBarImpl(this))
{
    trayIcon->SetIcon(generateIcon(0), 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_->Iconize(false);
            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);
        trayIcon->parentHasDied(); //TaskBarImpl (potentially) has longer lifetime than MinimizeToTray: avoid callback!

        //use wxWidgets 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 = NULL; //avoid reentrance
    }
}


void MinimizeToTray::setToolTip(const wxString& toolTipText, double percent)
{
    if (trayIcon)
        trayIcon->SetIcon(generateIcon(percent), 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:
            zen::showAboutDialog();
            break;
        case CONTEXT_RESTORE:
            resumeFromTray();
    }
}


void MinimizeToTray::OnDoubleClick(wxCommandEvent& event)
{
    resumeFromTray();
}

bgstack15