クラス Button は Label を利用して次のように定義すればよい。
class Button : public Label {
public:
Button(Xwindow* w, int _x0, int _y0, int _width, int _height,
char* str);
void redraw();
void buttonDown(int x, int y);
void buttonUp(int x, int y);
private:
bool button_pressed; // ボタンの状態を表す
};
各メソッドの中身は、simple.cc のイベント・ループ内での処理をイベントの種類ごとに分割したものである。
Button::Button(Xwindow* w, int _x0, int _y0, int _width, int _height,
char* str)
: Label(w, _x0, _y0, _width + 2, _height + 2, str)
{
button_pressed = false;
}
void Button::redraw()
{
if (button_pressed) {
ボタンが押されているときの絵を描く。
}
else {
Label::redraw();
ラベルの下に影を描く。
}
}
void Button::buttonDown(int x, int y)
{
button_pressed = true;
clearAndRedraw() を呼んで再描画。
}
void Button::buttonUp(int x, int y)
{
if (マウス位置がボタンの内側にある)
quit() を呼んで終了。
else {
button_pressed = false;
clearAndRedraw() を呼んで再描画。
}
}
Copyright (C) 1999-2000 Shigeru Chiba
Email: chiba@is.tsukuba.ac.jp