ユーザーが操作できる GUI パーツのことをコントロールと言います。そしてコントロールは、ユーザーの操作を受けて、イベントというものを発します。このイベントと、何かの処理を結びつけることによって、アプリケーションは動作するようになります。
wxWidgets では、GUI パーツのことをウィジェット(widget)と呼んでいます。
#include <wx/wx.h>
class Button : public wxFrame
{
public:
Button();
void OnQuit(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Button::Button() : wxFrame(NULL, -1, "Button")
{
wxPanel *panel = new wxPanel(this, -1);
wxButton *button = new wxButton(panel, wxID_EXIT, "Quit");
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
hbox->Add(button, 0, wxALIGN_CENTER);
vbox->Add(hbox, 1, wxALIGN_CENTER);
panel->SetSizer(vbox);
Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Button::OnQuit));
}
void Button::OnQuit(wxCommandEvent & event)
{
Close();
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Button *button = new Button();
button->Show();
return true;
}
#include <wx/wx.h>
class CheckBox : public wxFrame
{
public:
CheckBox();
wxCheckBox *checkbox;
void OnCheck(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
CheckBox::CheckBox() : wxFrame(NULL, -1, "CheckBox")
{
checkbox = new wxCheckBox(this, 1001, "Show title");
checkbox->SetValue(true);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
hbox->Add(checkbox, 0, wxALIGN_CENTER);
vbox->Add(hbox, 1, wxALIGN_CENTER);
SetSizer(vbox);
Connect(1001, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(CheckBox::OnCheck));
}
void CheckBox::OnCheck(wxCommandEvent & event)
{
if (checkbox->GetValue())
{
this->SetTitle("CheckBox");
} else {
this->SetTitle("");
}
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
CheckBox *checkbox = new CheckBox();
checkbox->Show();
return true;
}
#include <wx/wx.h>
#include <wx/slider.h>
class Slider : public wxFrame
{
public:
Slider();
wxStaticText *text;
wxSlider *slider;
void OnSlide(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Slider::Slider() : wxFrame(NULL, -1, "Slider")
{
wxPanel *panel = new wxPanel(this);
text = new wxStaticText(panel, -1, "50");
slider = new wxSlider(panel, 1001, 50, 0, 100);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
panel->SetSizer(hbox);
vbox->Add(text, 0, wxALIGN_CENTER);
vbox->Add(slider, 0, wxALL | wxEXPAND, 10);
hbox->Add(vbox, 1, wxALIGN_CENTER);
Connect(1001, wxEVT_SLIDER, wxCommandEventHandler(Slider::OnSlide));
}
void Slider::OnSlide(wxCommandEvent & event)
{
text->SetLabel(std::to_string(slider->GetValue()));
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Slider *slider = new Slider();
slider->Show();
return true;
}
#include <wx/wx.h>
#include <wx/tglbtn.h>
class ToggleButton : public wxFrame
{
public:
ToggleButton();
void OnToggleRed (wxCommandEvent & event);
void OnToggleGreen(wxCommandEvent & event);
void OnToggleBlue (wxCommandEvent & event);
protected:
wxToggleButton *redbutton;
wxToggleButton *greenbutton;
wxToggleButton *bluebutton;
wxPanel *colorpanel;
wxColour *color;
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
ToggleButton::ToggleButton() : wxFrame(NULL, -1, "ToggleButton")
{
wxPanel *panel = new wxPanel(this, -1);
redbutton = new wxToggleButton(panel, 1001, "Red",
wxPoint(20, 20));
greenbutton = new wxToggleButton(panel, 1002, "Green",
wxPoint(20, 70));
bluebutton = new wxToggleButton(panel, 1003, "Blue",
wxPoint(20, 120));
colorpanel = new wxPanel(panel, -1, wxPoint(150,20),
wxSize(110, 110),
wxSUNKEN_BORDER);
color = new wxColour(0, 0, 0);
colorpanel->SetBackgroundColour(color->GetAsString());
Connect(1001, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEventHandler(ToggleButton::OnToggleRed));
Connect(1002, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEventHandler(ToggleButton::OnToggleGreen));
Connect(1003, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEventHandler(ToggleButton::OnToggleBlue));
}
void ToggleButton::OnToggleRed(wxCommandEvent & event)
{
unsigned char green = color->Green();
unsigned char blue = color->Blue();
if (color->Red())
{
color->Set(0, green, blue);
} else {
color->Set(255, green, blue);
}
colorpanel->SetBackgroundColour(color->GetAsString());
Refresh();
}
void ToggleButton::OnToggleGreen(wxCommandEvent & event)
{
unsigned char red = color->Red();
unsigned char blue = color->Blue();
if (color->Green())
{
color->Set(red, 0, blue);
} else {
color->Set(red, 255, blue);
}
colorpanel->SetBackgroundColour(color->GetAsString());
Refresh();
}
void ToggleButton::OnToggleBlue(wxCommandEvent & event)
{
unsigned char red = color->Red();
unsigned char green = color->Green();
if (color->Blue())
{
color->Set(red, green, 0 );
} else {
color->Set(red, green, 255);
}
colorpanel->SetBackgroundColour(color->GetAsString());
Refresh();
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
ToggleButton *togglebutton = new ToggleButton();
togglebutton->Center();
togglebutton->Show();
return true;
}
#include <wx/wx.h>
class RadioBox : public wxFrame
{
public:
RadioBox();
wxRadioBox *radiobox;
void OnRadio(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
RadioBox::RadioBox() : wxFrame(NULL, -1, "RadioBox")
{
wxArrayString colors;
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");
radiobox = new wxRadioBox(this, 1001, "Red", wxDefaultPosition, wxDefaultSize, colors, 3, wxRA_HORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
vbox->Add(radiobox, 1, wxALL | wxEXPAND, 20);
SetSizer(vbox);
Connect(1001, wxEVT_RADIOBOX, wxCommandEventHandler(RadioBox::OnRadio));
}
void RadioBox::OnRadio(wxCommandEvent & event)
{
radiobox->SetLabel(radiobox->GetStringSelection());
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
RadioBox *radiobox = new RadioBox();
radiobox->Show();
return true;
}
#include <wx/wx.h>
class Choice : public wxFrame
{
public:
Choice();
wxChoice *choice;
wxStaticText *text;
void OnChoice(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Choice::Choice() : wxFrame(NULL, -1, "Choice")
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
SetSizer(vbox);
wxArrayString systems;
systems.Add("Windows");
systems.Add("macOS");
systems.Add("Linux Mint");
choice = new wxChoice(this, 1001, wxDefaultPosition, wxDefaultSize, systems);
text = new wxStaticText(this, -1, "Windows");
hbox->Add(text, 0, wxALIGN_CENTER);
vbox->Add(choice, 0, wxLEFT|wxTOP|wxRIGHT|wxEXPAND, 20);
vbox->Add(hbox, 1, wxALIGN_CENTER);
Connect(1001, wxEVT_CHOICE, wxCommandEventHandler(Choice::OnChoice));
}
void Choice::OnChoice(wxCommandEvent & event)
{
text->SetLabel(choice->GetStringSelection());
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Choice *choice = new Choice();
choice->Show();
return true;
}
#include <wx/wx.h>
class ComboBox : public wxFrame
{
public:
ComboBox();
private:
wxComboBox *combobox;
wxStaticText *text;
void OnComboBox(wxCommandEvent & event);
};
class App : public wxApp
{
virtual bool OnInit();
};
ComboBox::ComboBox() : wxFrame(NULL, -1, "ComboBox")
{
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
SetSizer(vbox);
wxArrayString systems;
systems.Add("windows");
systems.Add("macOS");
systems.Add("Linux Mint");
combobox = new wxComboBox(this, 1001, "お選びください", wxDefaultPosition, wxDefaultSize, systems);
text = new wxStaticText(this , -1, "未選択");
hbox->Add(text, 1, wxALIGN_CENTER);
vbox->Add(combobox, 0, wxLEFT|wxTOP|wxRIGHT | wxEXPAND, 20);
vbox->Add(hbox, 1, wxALIGN_CENTER);
Connect(1001, wxEVT_COMBOBOX, wxCommandEventHandler(ComboBox::OnComboBox));
}
void ComboBox::OnComboBox(wxCommandEvent & event)
{
text->SetLabel(combobox->GetStringSelection());
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
ComboBox *combobox = new ComboBox();
combobox->Show();
return true;
}
#include <wx/wx.h>
class ListBox : public wxFrame
{
public:
ListBox();
void OnListBox(wxCommandEvent & event);
wxListBox *listbox;
wxStaticText *text;
};
class App : public wxApp
{
virtual bool OnInit();
};
ListBox::ListBox() : wxFrame(NULL, -1, "ListBox")
{
wxPanel *panel = new wxPanel(this);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
panel->SetSizer(vbox);
wxArrayString systems;
systems.Add("Windows");
systems.Add("MacOS");
systems.Add("Linux Mint");
listbox = new wxListBox(panel, 1001, wxDefaultPosition, wxDefaultSize, systems, wxLB_SINGLE);
text = new wxStaticText(panel, -1, wxT("未選択"));
hbox->Add(text);
vbox->Add(listbox, 1, wxALL|wxEXPAND, 20);
vbox->Add(text, 0, wxBOTTOM|wxALIGN_CENTER, 20);
Connect(1001, wxEVT_LISTBOX, wxCommandEventHandler(ListBox::OnListBox));
}
void ListBox::OnListBox(wxCommandEvent & event)
{
text->SetLabel(listbox->GetStringSelection());
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
ListBox *listbox = new ListBox();
listbox->Show();
return true;
}
#include <wx/wx.h>
#include <wx/treectrl.h>
class TreeCtrl : public wxFrame
{
public:
TreeCtrl();
void OnTreeCtrl(wxCommandEvent & event);
wxTreeCtrl *treectrl;
wxStaticText *text;
};
class App : public wxApp
{
virtual bool OnInit();
};
TreeCtrl::TreeCtrl() : wxFrame(NULL, -1, "TreeControl")
{
wxPanel *panel = new wxPanel(this);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(vbox);
treectrl = new wxTreeCtrl(panel, 1001);
text = new wxStaticText(panel, -1, wxT("未選択"));
hbox->Add(text);
vbox->Add(treectrl, 1, wxALL | wxEXPAND, 10);
vbox->Add(hbox, 0, wxBOTTOM | wxALIGN_CENTER, 10);
wxTreeItemId root = treectrl->AddRoot("Programming Languages");
wxTreeItemId script = treectrl->AppendItem(root, "Scripting languages");
treectrl->AppendItem(script, "Python");
treectrl->AppendItem(script, "JavaScript");
treectrl->AppendItem(script, "PHP");
wxTreeItemId compile = treectrl->AppendItem(root, "Compiled languages");
treectrl->AppendItem(compile, "C/C++");
treectrl->AppendItem(compile, "C#");
treectrl->AppendItem(compile, "Java");
Connect(1001, wxEVT_TREE_SEL_CHANGED, wxCommandEventHandler(TreeCtrl::OnTreeCtrl));
}
void TreeCtrl::OnTreeCtrl(wxCommandEvent & event)
{
text->SetLabel(treectrl->GetItemText(treectrl->GetSelection()));
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
TreeCtrl *treectrl = new TreeCtrl();
treectrl->Show();
return true;
}