all repos — openbox @ 8269fc2b3965d12ba308caa554bfa7ee037fba13

openbox fork - make it a bit more like ryudo

otk/messagedialog.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#include "config.h"

#include "messagedialog.hh"
#include "assassin.hh"
#include "button.hh"
#include "label.hh"
#include "display.hh"
#include "property.hh"
#include "eventdispatcher.hh"
#include "timer.hh"

#include <algorithm>

namespace otk {

DialogButton MessageDialog::_default_result("", false);

class DialogButtonWidget : public Button {
  MessageDialog *_dia;
  const DialogButton &_res;
public:
  DialogButtonWidget(Widget *parent, MessageDialog *dia,
		     const DialogButton &b)
    : Button(parent),
      _dia(dia),
      _res(b)
  {
    assert(dia);
    setBevel(1);
    setMaxSize(Size(0,0));
    setText(b.label());
    setHighlighted(b.isDefault());
    show();
  }

  virtual void buttonPressHandler(const XButtonEvent &e) {
    // limit to the left button
    if (e.button == Button1)
      Button::buttonPressHandler(e);
  }
  virtual void clickHandler(unsigned int) {
    _dia->setResult(_res);
    _dia->hide();
  }
};

MessageDialog::MessageDialog(int screen, EventDispatcher *ed, ustring title,
			     ustring caption)
  : Widget(screen, ed, Widget::Vertical)
{
  init(title, caption);
}

MessageDialog::MessageDialog(EventDispatcher *ed, ustring title,
			     ustring caption)
  : Widget(DefaultScreen(**display), ed, Widget::Vertical)
{
  init(title, caption);
}

MessageDialog::MessageDialog(Widget *parent, ustring title, ustring caption)
  : Widget(parent, Widget::Vertical)
{
  init(title, caption);
}

void MessageDialog::init(const ustring &title, const ustring &caption)
{
  _label = new Label(this);
  _label->show();
  _label->setHighlighted(true);
  _button_holder = new Widget(this, Widget::Horizontal);
  _button_holder->show();
  _return = XKeysymToKeycode(**display, XStringToKeysym("Return"));
  _escape = XKeysymToKeycode(**display, XStringToKeysym("Escape"));
  _result = &_default_result;

  setEventMask(eventMask() | KeyPressMask);
  _label->setText(caption);
  if (title.utf8())
    otk::Property::set(window(), otk::Property::atoms.net_wm_name,
		       otk::Property::utf8, title);
  otk::Property::set(window(), otk::Property::atoms.wm_name,
		     otk::Property::ascii, otk::ustring(title.c_str(), false));

  // set WM Protocols on the window
  Atom protocols[2];
  protocols[0] = Property::atoms.wm_protocols;
  protocols[1] = Property::atoms.wm_delete_window;
  XSetWMProtocols(**display, window(), protocols, 2);
}

MessageDialog::~MessageDialog()
{
  if (visible()) hide();
  delete _button_holder;
  delete _label;
}

const DialogButton& MessageDialog::run()
{
  if (!visible())
    show();

  while (visible()) {
    dispatcher()->dispatchEvents();
    if (visible())
      Timer::dispatchTimers(); // fire pending events
  }
  return *_result;
}

void MessageDialog::focus()
{
  if (visible())
    XSetInputFocus(**display, window(), None, CurrentTime);
}

void MessageDialog::show()
{
  std::vector<DialogButton>::const_iterator it, end = _buttons.end();
  for (it = _buttons.begin(); it != end; ++it)
    _button_widgets.push_back(new DialogButtonWidget(_button_holder,
						     this, *it));

  Rect r;

  if (parent())
    r = parent()->area();
  else
    r = Rect(Point(0, 0), display->screenInfo(screen())->size());
  
  XSizeHints size;
  size.flags = PMinSize | PPosition | PWinGravity;
  size.min_width = minSize().width();
  size.min_height = minSize().height();
  size.win_gravity = CenterGravity;

  Size dest = minSize();
  if (dest.width() < 200 || dest.height() < 100) {
    if (dest.width() < 200 && dest.height() < 100) dest = Size(200, 100);
    else if (dest.width() < 200) dest = Size(200, dest.height());
    else dest = Size(dest.width(), 100);
    resize(dest);
  }

  // center it above its parent
  move(Point(r.x() + (r.width() - dest.width()) / 2,
	     r.y() + (r.height() - dest.height()) / 2));
  
  XSetWMNormalHints(**display, window(), &size);
  
  Widget::show();
}

void MessageDialog::hide()
{
  Widget::hide();
  std::for_each(_button_widgets.begin(), _button_widgets.end(),
		PointerAssassin());
}

void MessageDialog::keyPressHandler(const XKeyEvent &e)
{
  if (e.keycode == _return) {
    std::vector<DialogButton>::const_iterator it, end = _buttons.end();
    for (it = _buttons.begin(); it != end; ++it)
      if (it->isDefault()) {
	_result = &(*it);
	hide();
	break;
      }
  } else if (e.keycode == _escape) {
    hide();
  }
}

void MessageDialog::clientMessageHandler(const XClientMessageEvent &e)
{
  EventHandler::clientMessageHandler(e);
  if (e.message_type == Property::atoms.wm_protocols &&
      static_cast<Atom>(e.data.l[0]) == Property::atoms.wm_delete_window)
    hide();
}

}