wxWidgets/samples/taskbarbutton/taskbarbutton.cpp
Chaobin, Zhang c27103f8e1 Work around the limitation of windows API when setting thumbnail toolbar buttons.
- New API: InsertThumbBarButton, AppendThumbBarButton, RemoveThumbBarButton.
- Though MSDN said that "Buttons cannot be added or deleted later, so this must
  be the full defined set. Buttons also cannot be reordered.", we can work
  around it by: when first time adding button, initialize all of the possible
  seven buttons and hide them, except the button adding. In the next time adding
  button, just show it, which can make it looks like it is added on the fly.

Author: Chaobin Zhang

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77583 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2014-09-10 09:09:29 +00:00

375 lines
11 KiB
C++

/////////////////////////////////////////////////////////////////////////////
// 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"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#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 = (++counter) % WXSIZEOF(colours);
return icon;
}
} // namespace
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
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;
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
MyFrame *frame = new MyFrame("wxTaskBarButton App");
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, wxT("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, wxT("SetThumbnailTooltip"));
m_textCtrl = new wxTextCtrl(panel, wxID_ANY);
wxButton *btn = new wxButton(panel, ThumbnailTooltipSetBtn, wxT("Set"));
sttSizer->Add(m_textCtrl, 1, wxEXPAND | wxALL, 2);
sttSizer->Add(btn, 1, wxEXPAND | wxALL, 2);
// SetProgressState section.
wxStaticBoxSizer *spsSizer =
new wxStaticBoxSizer(wxVERTICAL, panel, wxT("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, wxT("SetOverlayIcon"));
wxButton *setOverlayIconBtn =
new wxButton(panel, SetOverlayIconBtn, wxT("Set Overlay Icon"));
wxButton *clearOverlayIconBtn =
new wxButton(panel, ClearOverlayIconBtn, wxT("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, wxT("SetThumbnailClip"));
wxButton *setThumbnailClipBtn =
new wxButton(panel, SetThumbnailClipBtn, wxT("Set Thumbnail Clip"));
wxButton *restoreThumbnailClipBtn =
new wxButton(panel, RestoreThumbnailClipBtn,
wxT("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, wxT("ThumbBar Buttons"));
wxButton *addThumbBarButtonBtn =
new wxButton(panel, AddThumbBarButtonBtn, wxT("Add ThumbBar Button"));
wxButton *showThumbnailToolbarBtn =
new wxButton(panel, RemoveThumbBarButtonBtn,
wxT("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, wxT("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_CHANGED(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();
MSWGetTaskBarButton()->RemoveThumbBarButton(button);
}
void MyFrame::OnThumbnailToolbarBtnClicked(wxCommandEvent& event)
{
wxLogMessage("Thumbnail Toolbar Button %d is clicked.", event.GetId());
}