summaryrefslogtreecommitdiff
path: root/shared/custom_button.cpp
blob: 0c4c30195dc69786e9d5c21c461d115a6ab815d3 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// **************************************************************************
// * 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 "custom_button.h"
#include <wx/dcmemory.h>
#include <wx/image.h>
#include <algorithm>
#include <limits>
#include <cmath>


namespace
{
bool isEqual(const wxBitmap& lhs, const wxBitmap& rhs)
{
    if (lhs.IsOk() != rhs.IsOk())
        return false;

    if (!lhs.IsOk())
        return true;

    const int pixelCount = lhs.GetWidth() * lhs.GetHeight();
    if (pixelCount != rhs.GetWidth() * rhs.GetHeight())
        return false;

    wxImage imLhs = lhs.ConvertToImage();
    wxImage imRhs = rhs.ConvertToImage();

    if (imLhs.HasAlpha() != imRhs.HasAlpha())
        return false;

    if (imLhs.HasAlpha())
    {
        if (!std::equal(imLhs.GetAlpha(), imLhs.GetAlpha() + pixelCount, imRhs.GetAlpha()))
            return false;
    }

    return std::equal(imLhs.GetData(), imLhs.GetData() + pixelCount * 3, imRhs.GetData());
}
}


void setBitmapLabel(wxBitmapButton& button, const wxBitmap& bmp)
{
    if (!isEqual(button.GetBitmapLabel(), bmp))
        button.SetBitmapLabel(bmp);
}


wxButtonWithImage::wxButtonWithImage(wxWindow* parent,
                                     wxWindowID id,
                                     const wxString& label,
                                     const wxPoint& pos,
                                     const wxSize& size,
                                     long style,
                                     const wxValidator& validator,
                                     const wxString& name) :
    wxBitmapButton(parent, id, wxNullBitmap, pos, size, style | wxBU_AUTODRAW, validator, name),
    m_spaceAfter(0),
    m_spaceBefore(0)
{
    setTextLabel(label);
}


void wxButtonWithImage::setBitmapFront(const wxBitmap& bitmap, unsigned spaceAfter)
{
    if (!isEqual(bitmap, bitmapFront) || spaceAfter != m_spaceAfter) //avoid flicker
    {
        bitmapFront  = bitmap;
        m_spaceAfter = spaceAfter;
        refreshButtonLabel();
    }
}


void wxButtonWithImage::setTextLabel(const wxString& text)
{
    if (text != textLabel) //avoid flicker
    {
        textLabel = text;
        wxBitmapButton::SetLabel(text);
        refreshButtonLabel();
    }
}


void wxButtonWithImage::setBitmapBack(const wxBitmap& bitmap, unsigned spaceBefore)
{
    if (!isEqual(bitmap, bitmapBack) || spaceBefore != m_spaceBefore) //avoid flicker
    {
        bitmapBack    = bitmap;
        m_spaceBefore = spaceBefore;
        refreshButtonLabel();
    }
}


void makeWhiteTransparent(wxImage& image) //assume black text on white background
{
    unsigned char* alphaFirst = image.GetAlpha();
    if (alphaFirst)
    {
        unsigned char* alphaLast = alphaFirst + image.GetWidth() * image.GetHeight();

        //dist(black, white)
        double distBlackWhite = std::sqrt(3.0 * 255 * 255);

        const unsigned char* bytePos = image.GetData();

        for (unsigned char* j = alphaFirst; j != alphaLast; ++j)
        {
            unsigned char r = *bytePos++; //
            unsigned char g = *bytePos++; //each pixel consists of three chars
            unsigned char b = *bytePos++; //

            //dist((r,g,b), white)
            double distColWhite = std::sqrt((255.0 - r) * (255.0 - r) + (255.0 - g) * (255.0 - g) + (255.0 - b) * (255.0 - b));

            //black(0,0,0) becomes fully opaque(255), while white(255,255,255) becomes transparent(0)
            *j = distColWhite / distBlackWhite * wxIMAGE_ALPHA_OPAQUE;
        }
    }
}


wxSize getSizeNeeded(const wxString& text, wxFont& font)
{
    wxCoord width, height;
    wxMemoryDC dc;

    wxString textFormatted = text;
    textFormatted.Replace(wxT("&"), wxT(""), false); //remove accelerator
    dc.GetMultiLineTextExtent(textFormatted, &width, &height , NULL, &font);
    return wxSize(width, height);
}

/*
inline
void linearInterpolationHelper(const int shift, wxImage& img)
{
    unsigned char* const data = img.GetData();
    const int width = img.GetWidth();
    if (width < 2)
        return;

    const float intensity = 0.25;

    for (int y = 1; y < img.GetHeight() - 1; ++y)
    {
        float back   = 0;
        float middle = 0;
        float front  = 0;

        unsigned char* location           = data + 3 * (y * width) + shift;
        const unsigned char* const endPos = location + 3 * width;

        middle = (*location + *(location - 3 * width) + *(location + 3 * width)) / 3;;
        front  = (*(location + 3) + *(location + 3 * (1 - width)) + *(location + 3 * (1 + width))) / 3;
        *location += ((middle + front) / 2 - *location) * intensity;
        location += 3;

        while (location < endPos - 3)
        {
            back   = middle;
            middle = front;
            front  = (*(location + 3) + *(location + 3 * (1 - width)) + *(location + 3 * (1 + width))) / 3;
            *location += ((back + middle + front) / 3 - *location) * intensity;
            location += 3;
        }

        back   = middle;
        middle = front;
        *location += ((back + middle) / 2 - *location) * intensity;
    }
}


void linearInterpolation(wxImage& img)
{
    linearInterpolationHelper(0, img); //red channel
    linearInterpolationHelper(1, img); //green channel
    linearInterpolationHelper(2, img); //blue channel
}
*/


wxBitmap wxButtonWithImage::createBitmapFromText(const wxString& text)
{
    //wxDC::DrawLabel() doesn't respect alpha channel at all => apply workaround to calculate alpha values manually:

    if (text.empty())
        return wxBitmap();

    wxFont currentFont = wxBitmapButton::GetFont();
    wxColor textColor  = wxBitmapButton::GetForegroundColour();

    wxSize sizeNeeded = getSizeNeeded(text, currentFont);
    wxBitmap newBitmap(sizeNeeded.GetWidth(), sizeNeeded.GetHeight());

    {
        wxMemoryDC dc;
        dc.SelectObject(newBitmap);

        //set up white background
        dc.SetBackground(*wxWHITE_BRUSH);
        dc.Clear();

        //find position of accelerator
        int indexAccel = -1;
        size_t accelPos;
        wxString textLabelFormatted = text;
        if ((accelPos = text.find(wxT("&"))) != wxString::npos)
        {
            textLabelFormatted.Replace(wxT("&"), wxT(""), false); //remove accelerator
            indexAccel = static_cast<int>(accelPos);
        }

        dc.SetTextForeground(*wxBLACK); //for use in makeWhiteTransparent
        dc.SetTextBackground(*wxWHITE); //
        dc.SetFont(currentFont);

        dc.DrawLabel(textLabelFormatted, wxNullBitmap, wxRect(0, 0, newBitmap.GetWidth(), newBitmap.GetHeight()), wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL, indexAccel);

        dc.SelectObject(wxNullBitmap);
    }

    //add alpha channel to image
    wxImage finalImage(newBitmap.ConvertToImage());
    finalImage.SetAlpha();

    //linearInterpolation(finalImage);

    //calculate values for alpha channel
    makeWhiteTransparent(finalImage);

    //now apply real text color
    unsigned char* bytePos = finalImage.GetData();
    const int pixelCount = finalImage.GetWidth() * finalImage.GetHeight();
    for (int i = 0; i < pixelCount; ++ i)
    {
        *bytePos++ = textColor.Red();
        *bytePos++ = textColor.Green();
        *bytePos++ = textColor.Blue();
    }

    return wxBitmap(finalImage);
}


//copy one image into another, allowing arbitrary overlapping! (pos may contain negative numbers)
void writeToImage(const wxImage& source, const wxPoint pos, wxImage& target)
{
    //determine startpositions in source and target image, as well as width and height to be copied
    wxPoint posSrc, posTrg;
    int width, height;

    //X-axis
    if (pos.x < 0)
    {
        posSrc.x = -pos.x;
        posTrg.x = 0;
        width = std::min(pos.x + source.GetWidth(), target.GetWidth());
    }
    else
    {
        posSrc.x = 0;
        posTrg.x = pos.x;
        width = std::min(target.GetWidth() - pos.x, source.GetWidth());
    }

    //Y-axis
    if (pos.y < 0)
    {
        posSrc.y = -pos.y;
        posTrg.y = 0;
        height = std::min(pos.y + source.GetHeight(), target.GetHeight());
    }
    else
    {
        posSrc.y = 0;
        posTrg.y = pos.y;
        height = std::min(target.GetHeight() - pos.y, source.GetHeight());
    }


    if (width > 0 && height > 0)
    {
        {
            //copy source to target respecting overlapping parts
            const unsigned char*       sourcePtr    = source.GetData() + 3 * (posSrc.x + posSrc.y * source.GetWidth());
            const unsigned char* const sourcePtrEnd = source.GetData() + 3 * (posSrc.x + (posSrc.y + height) * source.GetWidth());
            unsigned char*             targetPtr    = target.GetData() + 3 * (posTrg.x + posTrg.y * target.GetWidth());

            while (sourcePtr < sourcePtrEnd)
            {
                memcpy(targetPtr, sourcePtr, 3 * width);
                sourcePtr += 3 * source.GetWidth();
                targetPtr += 3 * target.GetWidth();
            }
        }

        //handle different cases concerning alpha channel
        if (source.HasAlpha())
        {
            if (!target.HasAlpha())
            {
                target.SetAlpha();
                unsigned char* alpha = target.GetAlpha();
                memset(alpha, wxIMAGE_ALPHA_OPAQUE, target.GetWidth() * target.GetHeight());
            }

            //copy alpha channel
            const unsigned char*       sourcePtr    = source.GetAlpha() + (posSrc.x + posSrc.y * source.GetWidth());
            const unsigned char* const sourcePtrEnd = source.GetAlpha() + (posSrc.x + (posSrc.y + height) * source.GetWidth());
            unsigned char*             targetPtr    = target.GetAlpha() + (posTrg.x + posTrg.y * target.GetWidth());

            while (sourcePtr < sourcePtrEnd)
            {
                memcpy(targetPtr, sourcePtr, width);
                sourcePtr += source.GetWidth();
                targetPtr += target.GetWidth();
            }
        }
        else if (target.HasAlpha())
        {
            unsigned char*             targetPtr    = target.GetAlpha() + (posTrg.x + posTrg.y * target.GetWidth());
            const unsigned char* const targetPtrEnd = target.GetAlpha() + (posTrg.x + (posTrg.y + height) * target.GetWidth());

            while (targetPtr < targetPtrEnd)
            {
                memset(targetPtr, wxIMAGE_ALPHA_OPAQUE, width);
                targetPtr += target.GetWidth();
            }
        }
    }
}


namespace
{
inline
wxSize getSize(const wxBitmap& bmp)
{
    return bmp.IsOk() ? wxSize(bmp.GetWidth(), bmp.GetHeight()) : wxSize(0, 0);
}
}


void wxButtonWithImage::refreshButtonLabel()
{
    wxBitmap bitmapText = createBitmapFromText(textLabel);

    wxSize szFront = getSize(bitmapFront); //
    wxSize szText  = getSize(bitmapText);  //make sure to NOT access null-bitmaps!
    wxSize szBack  = getSize(bitmapBack);  //

    //calculate dimensions of new button
    const int height = std::max(std::max(szFront.GetHeight(), szText.GetHeight()), szBack.GetHeight());
    const int width  = szFront.GetWidth() + m_spaceAfter + szText.GetWidth() + m_spaceBefore + szBack.GetWidth();

    //create a transparent image
    wxImage transparentImage(width, height, false);
    transparentImage.SetAlpha();
    unsigned char* alpha = transparentImage.GetAlpha();
    ::memset(alpha, wxIMAGE_ALPHA_TRANSPARENT, width * height);

    //wxDC::DrawLabel() unfortunately isn't working for transparent images on Linux, so we need to use custom image-concatenation
    if (bitmapFront.IsOk())
        writeToImage(bitmapFront.ConvertToImage(),
                     wxPoint(0, (transparentImage.GetHeight() - bitmapFront.GetHeight()) / 2),
                     transparentImage);

    if (bitmapText.IsOk())
        writeToImage(bitmapText.ConvertToImage(),
                     wxPoint(szFront.GetWidth() + m_spaceAfter, (transparentImage.GetHeight() - bitmapText.GetHeight()) / 2),
                     transparentImage);

    if (bitmapBack.IsOk())
        writeToImage(bitmapBack.ConvertToImage(),
                     wxPoint(szFront.GetWidth() + m_spaceAfter + szText.GetWidth() + m_spaceBefore, (transparentImage.GetHeight() - bitmapBack.GetHeight()) / 2),
                     transparentImage);

    //adjust button size
    wxSize minSize = GetMinSize();

    //SetMinSize() instead of SetSize() is needed here for wxWindows layout determination to work corretly
    wxBitmapButton::SetMinSize(wxSize(std::max(width + 10, minSize.GetWidth()), std::max(height + 5, minSize.GetHeight())));

    //finally set bitmap
    wxBitmapButton::SetBitmapLabel(wxBitmap(transparentImage));
}
bgstack15