File: taskbarbutton.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (435 lines) | stat: -rw-r--r-- 13,793 bytes parent folder | download | duplicates (4)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/////////////////////////////////////////////////////////////////////////////
// Name:        taskbarbutton.cpp
// Purpose:     wxTaskBarButton sample
// Author:      Chaobin Zhang <zhchbin@gmail.com>
// Created:     2014-04-30
// Copyright:   (c) 2014 wxWidgets development team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#include "wx/wxprec.h"


#ifndef WX_PRECOMP
    #include "wx/progdlg.h"
    #include "wx/wx.h"
#endif

#include "wx/stdpaths.h"
#include "wx/taskbarbutton.h"

enum
{
    ProgressValueSlider = wxID_HIGHEST,
    VisibilityRadio,
    ThumbnailTooltipSetBtn,
    ProgressStateChoice,
    SetOverlayIconBtn,
    ClearOverlayIconBtn,
    SetThumbnailClipBtn,
    RestoreThumbnailClipBtn,
    AddThumbBarButtonBtn,
    RemoveThumbBarButtonBtn,
};

enum
{
    ThumbnailToolbarBtn_0 = wxID_HIGHEST + 100,
    ThumbnailToolbarBtn_1,
    ThumbnailToolbarBtn_2,
    ThumbnailToolbarBtn_3,
    ThumbnailToolbarBtn_4,
    ThumbnailToolbarBtn_5,
    ThumbnailToolbarBtn_6
};

namespace {

wxBitmap CreateBitmap(const wxColour& colour, int w, int h)
{
    wxMemoryDC dc;
    wxBitmap bmp(w, h);
    dc.SelectObject(bmp);

    // Draw transparent background
    wxColour magic(255, 0, 255);
    wxBrush magicBrush(magic);
    dc.SetBrush(magicBrush);
    dc.SetPen(*wxTRANSPARENT_PEN);
    dc.DrawRectangle(0, 0, w, h);

    // Draw image content
    dc.SetBrush(wxBrush(colour));
    dc.DrawCircle(h / 2, h / 2 + 1, h / 2);
    dc.SelectObject(wxNullBitmap);

    // Finalize transparency with a mask
    wxMask *mask = new wxMask(bmp, magic);
    bmp.SetMask(mask);
    return bmp;
}

wxIcon CreateRandomIcon()
{
    static int counter = 0;
    static const wxColour* colours[] =
    {
        wxBLACK,
        wxWHITE,
        wxRED,
        wxBLUE,
        wxGREEN,
        wxCYAN,
        wxLIGHT_GREY
    };

    wxIcon icon;
    icon.CopyFromBitmap(CreateBitmap(*(colours[counter]), 16, 16));
    counter += 1;
    counter = counter % WXSIZEOF(colours);
    return icon;
}

}  // namespace

class MyApp : public wxApp
{
public:
    virtual bool OnInit() wxOVERRIDE;
};

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title);

private:
    wxDECLARE_EVENT_TABLE();

    void OnSetProgressValue(wxScrollEvent& WXUNUSED(event));
    void OnVisibilityChange(wxCommandEvent& WXUNUSED(event));
    void OnSetThumbnailTooltipBtn(wxCommandEvent& WXUNUSED(event));
    void OnChoice(wxCommandEvent& event);
    void OnSetOverlayIcon(wxCommandEvent& WXUNUSED(event));
    void OnClearOverlayIcon(wxCommandEvent& WXUNUSED(event));
    void OnSetOrRestoreThumbnailClip(wxCommandEvent& event);
    void OnAddThubmBarButton(wxCommandEvent& WXUNUSED(event));
    void OnRemoveThubmBarButton(wxCommandEvent& WXUNUSED(event));
    void OnThumbnailToolbarBtnClicked(wxCommandEvent& event);

    wxSlider *m_slider;
    wxRadioBox *m_visibilityRadioBox;
    wxTextCtrl *m_textCtrl;
    wxChoice *m_stateChoice;

    typedef wxVector<wxThumbBarButton*> wxThumbBarButtons;
    wxThumbBarButtons m_thumbBarButtons;
};

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    wxTaskBarJumpList jumpList;
    wxTaskBarJumpListItem *item1 = new wxTaskBarJumpListItem(
        NULL,
        wxTASKBAR_JUMP_LIST_TASK,
        "Task 1",
        wxStandardPaths::Get().GetExecutablePath(),
        wxEmptyString,
        "Test Task",
        wxStandardPaths::Get().GetExecutablePath(),
        0);
    wxTaskBarJumpListItem *item2 = new wxTaskBarJumpListItem(
        NULL,
        wxTASKBAR_JUMP_LIST_TASK,
        "Task 2",
        wxStandardPaths::Get().GetExecutablePath(),
        wxEmptyString,
        "Test Task",
        wxStandardPaths::Get().GetExecutablePath(),
        0);
    jumpList.GetTasks().Append(item1);
    jumpList.GetTasks().Append(
        new wxTaskBarJumpListItem(NULL, wxTASKBAR_JUMP_LIST_SEPARATOR));
    jumpList.GetTasks().Append(item2);
    jumpList.ShowRecentCategory();
    jumpList.ShowFrequentCategory();

    wxTaskBarJumpListCategory* customCategory =
        new wxTaskBarJumpListCategory(&jumpList, "Custom");
    wxTaskBarJumpListItem* item3 = new wxTaskBarJumpListItem(
        customCategory,
        wxTASKBAR_JUMP_LIST_DESTINATION,
        "Help",
        wxStandardPaths::Get().GetExecutablePath(),
        "--help",
        "wxTaskBarButton help.",
        wxStandardPaths::Get().GetExecutablePath(),
        0);
    customCategory->Append(item3);
    jumpList.AddCustomCategory(customCategory);

    const wxTaskBarJumpListCategory& frequentCategory =
        jumpList.GetFrequentCategory();
    const wxTaskBarJumpListItems& frequentItems = frequentCategory.GetItems();
    for ( size_t i = 0; i < frequentItems.size(); ++i )
    {
        wxLogMessage(frequentItems[i]->GetFilePath());
    }

    const wxTaskBarJumpListCategory& recentCategory =
        jumpList.GetRecentCategory();
    const wxTaskBarJumpListItems& recentItems = recentCategory.GetItems();
    for ( size_t i = 0; i < recentItems.size(); ++i )
    {
        wxLogMessage(recentItems[i]->GetFilePath());
    }

    MyFrame *frame = new MyFrame("wxTaskBarButton App");
    if ( !frame->MSWGetTaskBarButton() )
    {
        wxLogError("Task bar button API is not available on this system, sorry.");
        return false;
    }

    frame->Show(true);

    return true;
}

MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
{
    wxPanel *panel = new wxPanel(this);
    wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
    wxFlexGridSizer *gs = new wxFlexGridSizer(4, 2, 10, 10);

    // SetProgressValue section.
    wxStaticBoxSizer *spvSizer =
        new wxStaticBoxSizer(wxVERTICAL, panel, "SetProgressValue");
    int flags = wxSL_MIN_MAX_LABELS | wxSL_VALUE_LABEL | wxSL_AUTOTICKS;
    m_slider = new wxSlider(spvSizer->GetStaticBox(), ProgressValueSlider,
                            0, 0, 100,
                            wxDefaultPosition, wxSize(250, -1),
                            flags);
    m_slider->SetTickFreq(10);
    spvSizer->Add(m_slider);

    // Show/Hide in Taskbar section.
    const wxString labels[] =
    {
        "&Show in Taskbar",
        "&Hide in Taskbar"
    };
    m_visibilityRadioBox = new wxRadioBox(panel, VisibilityRadio, "Visibility:",
                                          wxDefaultPosition, wxDefaultSize,
                                          WXSIZEOF(labels), labels,
                                          1, wxRA_SPECIFY_ROWS);
   // SetThumbnailTooltip section.
    wxStaticBoxSizer *sttSizer =
        new wxStaticBoxSizer(wxVERTICAL, panel, "SetThumbnailTooltip");
    m_textCtrl = new wxTextCtrl(panel, wxID_ANY);
    wxButton *btn = new wxButton(panel, ThumbnailTooltipSetBtn, "Set");
    sttSizer->Add(m_textCtrl, 1, wxEXPAND | wxALL, 2);
    sttSizer->Add(btn, 1, wxEXPAND | wxALL, 2);

    // SetProgressState section.
    wxStaticBoxSizer *spsSizer =
        new wxStaticBoxSizer(wxVERTICAL, panel, "SetProgressState");
    const wxString choices[] =
    {
        "wxNoProgress",
        "wxIndeterminate",
        "wxNormal",
        "wxError",
        "wxPaused"
    };
    m_stateChoice = new wxChoice(panel, ProgressStateChoice,
                                 wxDefaultPosition, wxDefaultSize,
                                 WXSIZEOF(choices), choices);
    spsSizer->Add(m_stateChoice, 0, wxALL | wxGROW, 5);

    // SetOverlayIcon section.
    wxStaticBoxSizer *soiSizer =
        new wxStaticBoxSizer(wxVERTICAL, panel, "SetOverlayIcon");
    wxButton *setOverlayIconBtn =
        new wxButton(panel, SetOverlayIconBtn, "Set Overlay Icon");
    wxButton *clearOverlayIconBtn =
        new wxButton(panel, ClearOverlayIconBtn, "Clear Overlay Icon");
    soiSizer->Add(setOverlayIconBtn, 1, wxEXPAND | wxALL, 2);
    soiSizer->Add(clearOverlayIconBtn, 1, wxEXPAND | wxALL, 2);

    // SetThumbnailClip section.
    wxStaticBoxSizer *stcSizer =
        new wxStaticBoxSizer(wxVERTICAL, panel, "SetThumbnailClip");
    wxButton *setThumbnailClipBtn =
        new wxButton(panel, SetThumbnailClipBtn, "Set Thumbnail Clip");
    wxButton *restoreThumbnailClipBtn =
        new wxButton(panel, RestoreThumbnailClipBtn,
                     "Restore Thumbnail Clip");
    stcSizer->Add(setThumbnailClipBtn, 1, wxEXPAND | wxALL, 2);
    stcSizer->Add(restoreThumbnailClipBtn, 1, wxEXPAND | wxALL, 2);

    // Thumbnail Toolbar Buttons section.
    wxStaticBoxSizer *ttbSizer =
        new wxStaticBoxSizer(wxVERTICAL, panel, "ThumbBar Buttons");
    wxButton *addThumbBarButtonBtn =
        new wxButton(panel, AddThumbBarButtonBtn, "Add ThumbBar Button");
    wxButton *showThumbnailToolbarBtn =
        new wxButton(panel, RemoveThumbBarButtonBtn,
                     "Remove Last ThumbBar Button");
    ttbSizer->Add(addThumbBarButtonBtn, 1, wxEXPAND | wxALL, 2);
    ttbSizer->Add(showThumbnailToolbarBtn, 1, wxEXPAND | wxALL, 2);

    gs->Add(spvSizer, 0, wxEXPAND);
    gs->Add(m_visibilityRadioBox, 0, wxEXPAND);
    gs->Add(sttSizer, 0, wxEXPAND);
    gs->Add(spsSizer, 0, wxEXPAND);
    gs->Add(soiSizer, 0, wxEXPAND);
    gs->Add(stcSizer, 0, wxEXPAND);
    gs->Add(ttbSizer, 0, wxEXPAND);

    wxStaticText *text = new wxStaticText(
        panel, wxID_ANY, "Welcome to wxTaskBarButton sample");
    mainSizer->Add(text, 0, wxALIGN_CENTRE_HORIZONTAL);
    mainSizer->Add(gs);

    panel->SetSizer(mainSizer);

    SetIcon(wxICON(sample));
    SetSize(537, 420);
    Centre();
}

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_COMMAND_SCROLL(ProgressValueSlider, MyFrame::OnSetProgressValue)
    EVT_RADIOBOX(VisibilityRadio, MyFrame::OnVisibilityChange)
    EVT_BUTTON(ThumbnailTooltipSetBtn, MyFrame::OnSetThumbnailTooltipBtn)
    EVT_CHOICE(ProgressStateChoice, MyFrame::OnChoice)
    EVT_BUTTON(SetOverlayIconBtn, MyFrame::OnSetOverlayIcon)
    EVT_BUTTON(ClearOverlayIconBtn, MyFrame::OnClearOverlayIcon)
    EVT_BUTTON(SetThumbnailClipBtn, MyFrame::OnSetOrRestoreThumbnailClip)
    EVT_BUTTON(RestoreThumbnailClipBtn, MyFrame::OnSetOrRestoreThumbnailClip)
    EVT_BUTTON(AddThumbBarButtonBtn, MyFrame::OnAddThubmBarButton)
    EVT_BUTTON(RemoveThumbBarButtonBtn, MyFrame::OnRemoveThubmBarButton)
    EVT_BUTTON(ThumbnailToolbarBtn_0, MyFrame::OnThumbnailToolbarBtnClicked)
    EVT_BUTTON(ThumbnailToolbarBtn_1, MyFrame::OnThumbnailToolbarBtnClicked)
    EVT_BUTTON(ThumbnailToolbarBtn_2, MyFrame::OnThumbnailToolbarBtnClicked)
    EVT_BUTTON(ThumbnailToolbarBtn_3, MyFrame::OnThumbnailToolbarBtnClicked)
    EVT_BUTTON(ThumbnailToolbarBtn_4, MyFrame::OnThumbnailToolbarBtnClicked)
    EVT_BUTTON(ThumbnailToolbarBtn_5, MyFrame::OnThumbnailToolbarBtnClicked)
    EVT_BUTTON(ThumbnailToolbarBtn_6, MyFrame::OnThumbnailToolbarBtnClicked)
wxEND_EVENT_TABLE()

void MyFrame::OnSetProgressValue(wxScrollEvent& WXUNUSED(event))
{
    static bool s_hasRangeSet = false;
    if ( !s_hasRangeSet )
    {
      MSWGetTaskBarButton()->SetProgressRange(100);
      s_hasRangeSet = true;
    }

    MSWGetTaskBarButton()->SetProgressValue(m_slider->GetValue());
}

void MyFrame::OnVisibilityChange(wxCommandEvent& WXUNUSED(event))
{
    if ( m_visibilityRadioBox->GetSelection() == 0 )
        MSWGetTaskBarButton()->Show();
    else
        MSWGetTaskBarButton()->Hide();
}

void MyFrame::OnSetThumbnailTooltipBtn(wxCommandEvent& WXUNUSED(event))
{
    MSWGetTaskBarButton()->SetThumbnailTooltip(m_textCtrl->GetLineText(0));
}

void MyFrame::OnChoice(wxCommandEvent& event)
{
    int sel = event.GetSelection();
    wxTaskBarButtonState state;
    switch(sel)
    {
        case 0:
            state = wxTASKBAR_BUTTON_NO_PROGRESS;
            break;
        case 1:
            state = wxTASKBAR_BUTTON_INDETERMINATE;
            break;
        case 2:
            state = wxTASKBAR_BUTTON_NORMAL;
            break;
        case 3:
            state = wxTASKBAR_BUTTON_ERROR;
            break;
        case 4:
            state = wxTASKBAR_BUTTON_PAUSED;
            break;
        default:
            state = wxTASKBAR_BUTTON_NO_PROGRESS;
            break;
    }

    MSWGetTaskBarButton()->SetProgressValue(m_slider->GetValue());
    MSWGetTaskBarButton()->SetProgressState(state);
}

void MyFrame::OnSetOverlayIcon(wxCommandEvent& WXUNUSED(event))
{
    MSWGetTaskBarButton()->SetOverlayIcon(CreateRandomIcon());
}

void MyFrame::OnClearOverlayIcon(wxCommandEvent& WXUNUSED(event))
{
    MSWGetTaskBarButton()->SetOverlayIcon(wxNullIcon);
}

void MyFrame::OnSetOrRestoreThumbnailClip(wxCommandEvent& event)
{
    wxRect rect;
    if ( event.GetId() == SetThumbnailClipBtn )
    {
        static const int CLIP_LENGTH = 100;
        int height, width;
        GetClientSize(&width, &height);
        rect.SetX((width - CLIP_LENGTH) / 2);
        rect.SetY((height - CLIP_LENGTH) / 2);
        rect.SetHeight(CLIP_LENGTH);
        rect.SetWidth(CLIP_LENGTH);
    }

    MSWGetTaskBarButton()->SetThumbnailClip(rect);
}

void MyFrame::OnAddThubmBarButton(wxCommandEvent& WXUNUSED(event))
{
    if ( m_thumbBarButtons.size() >= 7 )
        return;

    wxThumbBarButton *button =
        new wxThumbBarButton(m_thumbBarButtons.size() + ThumbnailToolbarBtn_0 ,
                             CreateRandomIcon());
    MSWGetTaskBarButton()->AppendThumbBarButton(button);
    m_thumbBarButtons.push_back(button);
}

void MyFrame::OnRemoveThubmBarButton(wxCommandEvent& WXUNUSED(event))
{
    if ( m_thumbBarButtons.empty() )
        return;

    wxThumbBarButton* button = m_thumbBarButtons.back();
    m_thumbBarButtons.pop_back();
    delete MSWGetTaskBarButton()->RemoveThumbBarButton(button);
}

void MyFrame::OnThumbnailToolbarBtnClicked(wxCommandEvent& event)
{
    wxLogMessage("Thumbnail Toolbar Button %d is clicked.", event.GetId());
}