1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #include "messagedialog.hh"
10 #include "property.hh"
11 #include "eventdispatcher.hh"
18 DialogButton MessageDialog::_default_result("", false);
20 class DialogButtonWidget : public Button {
22 const DialogButton &_res;
24 DialogButtonWidget(Widget *parent, MessageDialog *dia,
25 const DialogButton &b)
32 setMaxSize(Size(0,0));
34 setHighlighted(b.isDefault());
38 virtual void buttonPressHandler(const XButtonEvent &e) {
39 // limit to the left button
40 if (e.button == Button1)
41 Button::buttonPressHandler(e);
43 virtual void clickHandler(unsigned int) {
44 _dia->setResult(_res);
49 MessageDialog::MessageDialog(int screen, EventDispatcher *ed, ustring title,
51 : Widget(screen, ed, Widget::Vertical)
56 MessageDialog::MessageDialog(EventDispatcher *ed, ustring title,
58 : Widget(DefaultScreen(**display), ed, Widget::Vertical)
63 MessageDialog::MessageDialog(Widget *parent, ustring title, ustring caption)
64 : Widget(parent, Widget::Vertical)
69 void MessageDialog::init(const ustring &title, const ustring &caption)
71 _label = new Label(this);
73 _label->setHighlighted(true);
74 _button_holder = new Widget(this, Widget::Horizontal);
75 _button_holder->show();
76 _return = XKeysymToKeycode(**display, XStringToKeysym("Return"));
77 _escape = XKeysymToKeycode(**display, XStringToKeysym("Escape"));
78 _result = &_default_result;
80 setEventMask(eventMask() | KeyPressMask);
81 _label->setText(caption);
83 otk::Property::set(window(), otk::Property::atoms.net_wm_name,
84 otk::Property::utf8, title);
85 otk::Property::set(window(), otk::Property::atoms.wm_name,
86 otk::Property::ascii, otk::ustring(title.c_str(), false));
88 // set WM Protocols on the window
90 protocols[0] = Property::atoms.wm_protocols;
91 protocols[1] = Property::atoms.wm_delete_window;
92 XSetWMProtocols(**display, window(), protocols, 2);
95 MessageDialog::~MessageDialog()
97 if (visible()) hide();
98 delete _button_holder;
102 const DialogButton& MessageDialog::run()
108 dispatcher()->dispatchEvents();
110 Timer::dispatchTimers(); // fire pending events
115 void MessageDialog::show()
117 std::vector<DialogButton>::const_iterator it, end = _buttons.end();
118 for (it = _buttons.begin(); it != end; ++it)
119 _button_widgets.push_back(new DialogButtonWidget(_button_holder,
125 r = parent()->area();
127 r = Rect(Point(0, 0), display->screenInfo(screen())->size());
130 size.flags = PMinSize | PPosition;
131 size.min_width = minSize().width();
132 size.min_height = minSize().height();
134 Size dest = area().size();
135 if (dest.width() < 200 || dest.height() < 100) {
136 if (dest.width() < 200 && dest.height() < 100) dest = Size(200, 100);
137 else if (dest.width() < 200) dest = Size(200, dest.height());
138 else dest = Size(dest.width(), 100);
142 // center it above its parent
143 move(Point(r.x() + (r.width() - dest.width()) / 2,
144 r.y() + (r.height() - dest.height()) / 2));
146 XSetWMNormalHints(**display, window(), &size);
151 void MessageDialog::hide()
154 std::for_each(_button_widgets.begin(), _button_widgets.end(),
158 void MessageDialog::keyPressHandler(const XKeyEvent &e)
160 if (e.keycode == _return) {
161 std::vector<DialogButton>::const_iterator it, end = _buttons.end();
162 for (it = _buttons.begin(); it != end; ++it)
163 if (it->isDefault()) {
168 } else if (e.keycode == _escape) {
173 void MessageDialog::clientMessageHandler(const XClientMessageEvent &e)
175 EventHandler::clientMessageHandler(e);
176 if (e.message_type == Property::atoms.wm_protocols &&
177 static_cast<Atom>(e.data.l[0]) == Property::atoms.wm_delete_window)