File: xxWindow.cc

package info (click to toggle)
xtide 2.9.5-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,996 kB
  • ctags: 2,141
  • sloc: cpp: 20,379; sh: 1,044; makefile: 224; yacc: 114; lex: 58
file content (281 lines) | stat: -rw-r--r-- 7,916 bytes parent folder | download | duplicates (3)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// $Id: xxWindow.cc 2641 2007-09-02 21:31:02Z flaterco $

/*  xxWindow  An XTide window.

    Copyright (C) 1998  David Flater.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "xtide.hh"


static void xxWindowCloseHandler (Widget w unusedParameter,
                                  XtPointer client_data,
                                  XEvent *event,
                                  Boolean *continue_dispatch unusedParameter) {
  xxWindow *f ((xxWindow *)client_data);
  if (f->noClose)
    return;
  if (event->type == ClientMessage) {
    XClientMessageEvent *ev ((XClientMessageEvent *) event);
    // Window manager close.
    if (ev->message_type == xxX::protocolAtom &&
	ev->data.l[0] == (int)(xxX::killAtom))
      delete f->dismiss();
  }
}


xxWindow::xxWindow (int &argc, char **argv):
  noClose(false),
  _containerType(noContainer),
  _grabKind(XtGrabNone),
  _iconWindow(0),
  _isRealized(false) {

  popup = xxX::wrap (xxX::connect (argc, argv));
  // Can't set colors until after the settings have been processed!
  // See xxRoot::xxRoot.
  XtAddEventHandler (popup->widget(), NoEventMask, True,
    xxWindowCloseHandler, (XtPointer)this);
  setIcon();
}


xxWindow::xxWindow (const xxWidget &parent,
                    ContainerType containerType,
                    XtGrabKind grabKind):
  noClose(false),
  _containerType(containerType),
  _grabKind(grabKind),
  _iconWindow(0),
  _isRealized(false) {

  Global::root->dup (this);
  {
    // I have to specify this stuff explicitly under Openwin.
    // Probably has to do with using non-default colormap.
    Arg args[5] = {
      {XtNdepth, (XtArgVal)xxX::colordepth},
      {XtNvisual, (XtArgVal)xxX::visual},
      {XtNcolormap, (XtArgVal)xxX::colormap},
      {XtNborderColor, (XtArgVal)0},
      {XtNbackground, (XtArgVal)0}
    };
    popup = xxX::wrap (XtCreatePopupShell ("XTide",
					   topLevelShellWidgetClass,
					   parent.widget(),
					   args,
					   5));
  }{
    Arg args[2] =  {
      {XtNbackground, (XtArgVal)xxX::pixels[Colors::background]},
      {XtNforeground, (XtArgVal)xxX::pixels[Colors::foreground]}
    };
    XtSetValues (popup->widget(), args, 2);
    switch (_containerType) {
    case noContainer:
      break;
    case boxContainer:
      container = xxX::wrap (XtCreateManagedWidget ("",
						    boxWidgetClass,
						    popup->widget(),
						    args,
						    2));
      break;
    case formContainer:
      container = xxX::wrap (XtCreateManagedWidget ("",
						    formWidgetClass,
						    popup->widget(),
						    args,
						    2));
      break;
    default:
      assert (false);
    }
  }
  XtAddEventHandler (popup->widget(), NoEventMask, True,
    xxWindowCloseHandler, (XtPointer)this);
  setIcon();
}


xxWindow * const xxWindow::dismiss() {
  return this;
}


// This should never be called for xxRoot since it dies only on exit().
xxWindow::~xxWindow() {
  unrealize();
  Global::root->release (this);
  if (_iconWindow)
    XDestroyWindow (xxX::display, _iconWindow);
}


void xxWindow::move (const Dstr &geometry) {
  int xoff, yoff;
  if (sscanf (geometry.aschar(), "%d%d", &xoff, &yoff) == 2) {
    bool negx ((xoff < 0 || (xoff == 0 && geometry[0] == '-')));
    bool negy ((yoff < 0 || (yoff == 0 && geometry.strrchr('-') > 0)));
    if (negx || negy) {
      Dimension width, height;
      Arg args[2] = {
	{XtNwidth, (XtArgVal)(&width)},
	{XtNheight, (XtArgVal)(&height)}
      };
      XtGetValues (popup->widget(), args, 2);
      if (negx)
	xoff += (int)(DisplayWidth(xxX::display,xxX::screenNum)) - width;
      if (negy)
	yoff += (int)(DisplayHeight(xxX::display,xxX::screenNum)) - height;
    }
    XMoveWindow (xxX::display, XtWindow(popup->widget()), xoff, yoff);
  }
}


void xxWindow::globalRedraw() {
  Arg args[2] =  {
    {XtNbackground, (XtArgVal)xxX::pixels[Colors::background]},
    {XtNforeground, (XtArgVal)xxX::pixels[Colors::foreground]}
  };
  assert (popup.get());
  XtSetValues (popup->widget(), args, 2);
  if (container.get())
    XtSetValues (container->widget(), args, 2);
}


void xxWindow::fixSize() {
  assert (_isRealized);
  Dimension width, height;
  Arg args[2] = {
    {XtNwidth, (XtArgVal)&width},
    {XtNheight, (XtArgVal)&height}
  };
  Widget popupWidget (popup->widget());
  XtGetValues (popupWidget, args, 2);
  XSizeHints sizeHint;
  sizeHint.min_width = sizeHint.max_width = width;
  sizeHint.min_height = sizeHint.max_height = height;
  sizeHint.flags = PMinSize | PMaxSize;
  XSetWMNormalHints (xxX::display, XtWindow(popupWidget), &sizeHint);
}


void xxWindow::setMinSize (Dimension width, Dimension height) {
  assert (_isRealized);
  XSizeHints sizeHint;
  sizeHint.min_width = width;
  sizeHint.min_height = height;
  sizeHint.flags = PMinSize;
  XSetWMNormalHints (xxX::display, XtWindow(popup->widget()), &sizeHint);
}


void xxWindow::widthNoSmaller() {
  assert (_isRealized);
  Dimension width;
  Arg args[1] = {
    {XtNwidth, (XtArgVal)&width}
  };
  Widget popupWidget (popup->widget());
  XtGetValues (popupWidget, args, 1);
  XSizeHints sizeHint;
  sizeHint.min_width = width;
  sizeHint.min_height = 0;
  sizeHint.flags = PMinSize;
  XSetWMNormalHints (xxX::display, XtWindow(popupWidget), &sizeHint);
}


void xxWindow::realize() {
  if (!_isRealized) {
    Widget popupWidget (popup->widget());
    XtPopup (popupWidget, _grabKind);
    XSetWMProtocols (xxX::display, XtWindow(popupWidget), &xxX::killAtom, 1);
    _isRealized = true;
    setTitle_NET_WM();
  }
}


void xxWindow::unrealize() {
  if (_isRealized) {
    XtPopdown (popup->widget());
    _isRealized = false;
  }
}


void xxWindow::setIcon() {
  XSetWindowAttributes xswa;
  xswa.background_pixmap = xxX::iconPixmap;
  _iconWindow = XCreateWindow (xxX::display,
                               xxX::rootWindow,
			       0,
                               0,
                               xxX::iconSize,
                               xxX::iconSize,
                               0,
                               xxX::iconColordepth,
                               InputOutput,
                               xxX::iconVisual,
                               CWBackPixmap,
                               &xswa);
  Arg args[1] = {
    {XtNiconWindow, (XtArgVal)_iconWindow}
  };
  XtSetValues (popup->widget(), args, 1);
}


void xxWindow::setTitle (const Dstr &title) {
  _title = "XTide: ";
  _title += title;

  Arg args[2] = {
    {XtNtitle, (XtArgVal)_title.aschar()},
    {XtNiconName, (XtArgVal)_title.aschar()}
  };
  XtSetValues (popup->widget(), args, 2);

  setTitle_NET_WM();
}


// In cases where a non-default locale is in use, the expected
// encoding of XtNtitle is ambiguous.  The Window Manager
// Specification added _NET_WM_NAME to get around this.  It's always
// in UTF-8.  However, not all window managers support it.

void xxWindow::setTitle_NET_WM () {
  if (_isRealized && !_title.isNull()) {
    Dstr temp (_title);
    temp.utf8();
    XChangeProperty (xxX::display,
                     XtWindow(popup->widget()),
		     XInternAtom(xxX::display, "_NET_WM_NAME", False),
		     XInternAtom(xxX::display, "UTF8_STRING", False),
		     8,
                     PropModeReplace,
                     (unsigned char *)temp.aschar(),
                     temp.length());
  }
}

// Cleanup2006 Done