この章では、各種ダイアログを説明します。
#include <wx/wx.h>
class Message : public wxFrame
/*
クラス名をMessageBoxにしたいところですが、UbuntuとMintにaptでインストールしたWxWidgetsは3.0.2です。
3.0.2の場合は、MessageBoxというクラス名は識別子の衝突を起こすようです。
なお、WindowsとmacOSにインストールされるwxWidgets3.0.4では、MessageBoxというクラス名も使えます。
*/
{
public:
Message();
void ShowMessage(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Message::Message() : wxFrame(NULL, -1, "Message")
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
SetSizer(vbox);
wxButton *button = new wxButton(this, 1001, "Show Message");
hbox->Add(button, 0, wxALIGN_CENTER);
vbox->Add(hbox, 1, wxALIGN_CENTER);
Bind(wxEVT_BUTTON, &Message::ShowMessage, this, 1001);
}
void Message::ShowMessage(wxCommandEvent & event)
{
wxMessageBox("This is a MessageBox");
/*
wxMessageBoxに最小限必要な引数は、メッセージボックスに表示される文字列だけです。
次のようにタイトル、ボタンの種類、アイコンの種類を指定することもできます。
wxMessageBox("This is a MessageBox", "Title", wxOK | wxICON_INFORMATION);
*/
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Message *message = new Message();
message->Show();
return true;
}
#include <wx/wx.h>
class MessageDialog : public wxFrame
{
public:
MessageDialog();
void Info (wxCommandEvent & event);
void Error (wxCommandEvent & event);
void Question(wxCommandEvent & event);
void Alert (wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
MessageDialog::MessageDialog() : wxFrame(NULL, -1, "MessageDialog")
{
wxGridSizer *grid = new wxGridSizer(2, 2, 0, 0);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
vbox->Add(grid, 1, wxEXPAND);
SetSizer(vbox);
wxButton *info = new wxButton(this, 101, "Info");
wxButton *error = new wxButton(this, 102, "Error");
wxButton *question = new wxButton(this, 103, "Question");
wxButton *alert = new wxButton(this, 104, "Alert");
grid->Add(info, 0, wxALIGN_CENTER);
grid->Add(error, 0, wxALIGN_CENTER);
grid->Add(question, 0, wxALIGN_CENTER);
grid->Add(alert, 0, wxALIGN_CENTER);
Bind(wxEVT_BUTTON, &MessageDialog::Info, this, 101);
Bind(wxEVT_BUTTON, &MessageDialog::Error, this, 102);
Bind(wxEVT_BUTTON, &MessageDialog::Question, this, 103);
Bind(wxEVT_BUTTON, &MessageDialog::Alert, this, 104);
}
void MessageDialog::Info(wxCommandEvent & event)
{
wxMessageDialog *dial = new wxMessageDialog(NULL, "Download completed", "Info");
/* 最小限必要な引数は以下のとおりです
wxMessageDialog *dial = new wxMessageDialog(NULL, "Download completed");
*/
dial->ShowModal();
}
void MessageDialog::Error(wxCommandEvent & event)
{
wxMessageDialog *dial = new wxMessageDialog(NULL, "Error loading file", "Error", wxICON_ERROR);
dial->ShowModal();
}
void MessageDialog::Question(wxCommandEvent & event)
{
wxMessageDialog *dial = new wxMessageDialog(NULL, "Are you sure to quit?", "Question", wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
dial->ShowModal();
}
void MessageDialog::Alert(wxCommandEvent & event)
{
wxMessageDialog *dial = new wxMessageDialog(NULL, "Unallowed operation", "Exclamation", wxICON_EXCLAMATION);
dial->ShowModal();
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
MessageDialog *messagedialog = new MessageDialog();
messagedialog->Show();
return true;
}
#include <wx/wx.h>
class FileDialog : public wxFrame
{
public:
FileDialog();
void OnOpen(wxCommandEvent & event);
void OnQuit(wxCommandEvent & event);
wxStaticText *text;
};
class App : public wxApp
{
virtual bool OnInit();
};
FileDialog::FileDialog() : wxFrame(NULL, -1, "FileDialog")
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
text = new wxStaticText(this, -1, wxT("未選択"));
hbox->Add(text, 0, wxALIGN_CENTER);
vbox->Add(hbox, 1, wxLEFT, 20);
SetSizer(vbox);
wxMenuBar *menubar = new wxMenuBar();
wxMenu *file = new wxMenu();
SetMenuBar(menubar);
file->Append(wxID_OPEN);
file->AppendSeparator();
file->Append(wxID_EXIT);
menubar->Append(file, "File");
Bind(wxEVT_MENU, &FileDialog::OnOpen, this, wxID_OPEN);
Bind(wxEVT_MENU, &FileDialog::OnQuit, this, wxID_EXIT);
}
void FileDialog::OnOpen(wxCommandEvent & event)
{
wxFileDialog *openfiledialog = new wxFileDialog(this);
if (openfiledialog->ShowModal() == wxID_OK) {
text->SetLabel(openfiledialog->GetFilename());
} else {
text->SetLabel(wxT("未選択"));
}
}
void FileDialog::OnQuit(wxCommandEvent & event)
{
Close();
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
FileDialog *filedialog = new FileDialog();
filedialog->Show();
return true;
}
#include <wx/wx.h>
#include <wx/fontdlg.h>
class FontDialog : public wxFrame
{
public:
FontDialog();
wxStaticText *text;
void OnFont(wxCommandEvent & event);
void OnQuit(wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
FontDialog::FontDialog() : wxFrame(NULL, -1, "FontDialog")
{
wxMenuBar *menubar = new wxMenuBar();
wxMenu *file = new wxMenu();
wxMenu *format = new wxMenu();
menubar->Append(file, "File");
menubar->Append(format, "Format");
file ->Append(wxID_EXIT);
format ->Append(101, "Font");
SetMenuBar(menubar);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
SetSizer(vbox);
text = new wxStaticText(this, 1, "Hello wxWidgets!");
hbox->Add(text, 0, wxALIGN_CENTER);
vbox->Add(hbox, 1, wxLEFT, 20);
Bind(wxEVT_MENU, &FontDialog::OnFont, this, 101);
Bind(wxEVT_MENU, &FontDialog::OnQuit, this, wxID_EXIT);
}
void FontDialog::OnFont(wxCommandEvent & event)
{
wxFontDialog *fontdialog = new wxFontDialog(this);
if (fontdialog->ShowModal() == wxID_OK) {
text->SetFont(fontdialog->GetFontData().GetChosenFont());
}
}
void FontDialog::OnQuit(wxCommandEvent & event)
{
Close();
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
FontDialog *fontdialog = new FontDialog();
fontdialog->Show();
return true;
}
#include <wx/wx.h>
#include <wx/colordlg.h>
class ColorDialog : public wxFrame
{
public:
ColorDialog();
wxPanel *panel;
void OnColor(wxCommandEvent & event);
void OnQuit (wxCommandEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
ColorDialog::ColorDialog() : wxFrame(NULL, -1, "ColorDialog")
{
wxMenuBar *menubar = new wxMenuBar();
SetMenuBar(menubar);
wxMenu *file = new wxMenu();
wxMenu *format = new wxMenu();
menubar ->Append(file, "File");
menubar ->Append(format, "Format");
file ->Append(wxID_EXIT);
format ->Append(101, "Color");
panel = new wxPanel(this);
panel ->SetBackgroundColour(wxColour("green"));
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
vbox ->Add(panel, 1, wxALL | wxEXPAND, 10);
SetSizer(vbox);
Bind(wxEVT_MENU, &ColorDialog::OnColor, this, 101);
Bind(wxEVT_MENU, &ColorDialog::OnQuit, this, wxID_EXIT);
}
void ColorDialog::OnColor(wxCommandEvent & event)
{
wxColourDialog *colordialog = new wxColourDialog(this);
if (colordialog->ShowModal() == wxID_OK) {
panel->SetBackgroundColour(colordialog->GetColourData().GetColour());
/*
WindowsとmacOSの場合は、次のリフレッシュコードが必要です。
UbuntuとMintでは、リフレッシュコードは必要ありませんが、
記述していても問題ありません。
*/
panel->Refresh();
}
}
void ColorDialog::OnQuit (wxCommandEvent & event)
{
Close();
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
ColorDialog *colordialog = new ColorDialog();
colordialog->Show();
return true;
}