File: MwHandle.c

package info (click to toggle)
mowitz 0.2.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,168 kB
  • ctags: 4,214
  • sloc: ansic: 30,869; sh: 9,051; makefile: 169
file content (325 lines) | stat: -rw-r--r-- 8,897 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

/*
 * MwHandle.c - MwHandle widget
 */

#include <stdio.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include "MwUtils.h"
#include "MwXutils.h"
#include "MwHandleP.h"
#include <X11/Shell.h>

#define MULTI_CLICK_TIME 500

/****************************************************************
 *
 * Full class record constant
 *
 ****************************************************************/

/* Private Data */

static char translations[] =
	"<Btn1Down>:	handle-start()	\n"
	"<Btn1Motion>:	handle-drag()	\n"
	"<Btn1Up>:	handle-release()";


#define offset(field) XtOffsetOf(MwHandleRec, field)
static XtResource resources[] = {
	{
		XtNborderWidth,				/* name */
		XtCBorderWidth,				/* class */
		XtRDimension,				/* type */
		sizeof(Dimension),			/* size */
		XtOffsetOf(RectObjRec,rectangle.border_width), /* offset */
		XtRImmediate,				/* default_type */
		(XtPointer)0				/* default_addr */
	}, {
		XtNhandleVictim,
		XtCHandleVictim,
		XtRWidget,
		sizeof(Widget),
		offset(handle.victim),
		XtRImmediate,
		(XtPointer)None,
	}, {
		XtNattachCallback,
		XtCAttachCallback,
		XtRCallback,
		sizeof(XtPointer),
		offset(handle.attach_callbacks),
		XtRCallback,
		(XtPointer)NULL
	}, {
		XtNdetachCallback,
		XtCDetachCallback,
		XtRCallback,
		sizeof(XtPointer),
		offset(handle.detach_callbacks),
		XtRCallback,
		(XtPointer)NULL
	}, {
		XtNwidth,
		XtCWidth,
		XtRDimension,
		sizeof(Dimension),
		XtOffsetOf(RectObjRec,rectangle.width),
		XtRImmediate,
		(XtPointer)9
	}, {
		XtNshadowWidth,
		XtCShadowWidth,
		XtRDimension,
		sizeof(Dimension),
		offset(frame.shadowWidth),
		XtRImmediate,
		(XtPointer)1
	}, {
		XtNshadowType,
		XtCShadowType,
		XtRShadowType,
		sizeof(XtShadowType),
		offset(frame.type),
		XtRImmediate,
		(XtPointer)Raised
	}
};
#undef offset

static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static void handle_start(Widget, XEvent *, String *, Cardinal *),
	 handle_drag(Widget, XEvent *, String *, Cardinal *),
	 handle_release(Widget, XEvent *, String *, Cardinal *);
static void Initialize(Widget, Widget, ArgList, Cardinal *);

static XtActionsRec actionsList[] = {
	{"handle-start", handle_start},
	{"handle-drag", handle_drag},
	{"handle-release", handle_release},
};

#define SuperClass ((MwFrameWidgetClass)&mwFrameClassRec)

MwHandleClassRec mwHandleClassRec = {
  {
    (WidgetClass) SuperClass, 		/* superclass		  */	
    "MwHandle",				/* class_name		  */
    sizeof(MwHandleRec),		/* size			  */
    NULL,				/* class_initialize	  */
    NULL,				/* class_part_initialize  */
    FALSE,				/* class_inited		  */
    Initialize,				/* initialize		  */
    NULL,				/* initialize_hook	  */
    XtInheritRealize,			/* realize		  */
    actionsList,			/* actions		  */
    XtNumber(actionsList),		/* num_actions		  */
    resources,				/* resources		  */
    XtNumber(resources),		/* resource_count	  */
    NULLQUARK,				/* xrm_class		  */
    FALSE,				/* compress_motion	  */
    TRUE,				/* compress_exposure	  */
    TRUE,				/* compress_enterleave    */
    FALSE,				/* visible_interest	  */
    NULL,				/* destroy		  */
    XtInheritResize,			/* resize		  */
    XtInheritExpose,			/* expose		  */
    SetValues,				/* set_values		  */
    NULL,				/* set_values_hook	  */
    XtInheritSetValuesAlmost,		/* set_values_almost	  */
    NULL,				/* get_values_hook	  */
    NULL,				/* accept_focus		  */
    XtVersion,				/* version		  */
    NULL,				/* callback_private	  */
    translations,			/* tm_table		  */
    XtInheritQueryGeometry,		/* query_geometry	  */
    XtInheritDisplayAccelerator,	/* display_accelerator	  */
    NULL				/* extension		  */
  },
  {
/* composite_class fields */
    /* geometry_manager   */    NULL,
    /* change_managed     */    NULL,
    /* insert_child       */    XtInheritInsertChild,   /* TODO? */
    /* delete_child       */    XtInheritDeleteChild,   /* TODO? */
    /* extension          */    NULL
  },
  {
/* Frame class fields */
    /* extension          */    NULL,
  },
  {
    0,                                     /* field not used    */
  },  /* MwHandleClass fields initialization */
};

  /* for public consumption */
WidgetClass mwHandleWidgetClass = (WidgetClass) &mwHandleClassRec;

static void Initialize(Widget request, Widget new,
			ArgList args, Cardinal *num_args)
{
	MwHandleWidget cbw = (MwHandleWidget) new;
	cbw->handle.parent = None;
	cbw->handle.detached = False;
	cbw->handle.shell = None;
	cbw->handle.old_x = 0;
	cbw->handle.old_y = 0;
	cbw->handle.oldtime = 0;
}


static int bogosity(Display *d, XErrorEvent *e)
{
	return 0;
}

static void handle_start(Widget w, XEvent *event, String *p, Cardinal *n)
{
	MwHandleWidget hw = (MwHandleWidget)w;
	Time newtime = event->xbutton.time;
	int (*old_handler)(Display *, XErrorEvent *);
	if (hw->handle.detached) {
		XRaiseWindow(XtDisplay(w), XtWindow(hw->handle.shell));
		if (abs((long)newtime-(long)hw->handle.oldtime)
				< MULTI_CLICK_TIME) {
			XReparentWindow(XtDisplay(w),
				XtWindow(hw->handle.victim),
				XtWindow(hw->handle.parent),
				hw->handle.old_x, hw->handle.old_y);
			XtDestroyWidget(hw->handle.shell);

			/* We must now undo all the magic we did below */
			/* set mappedWhenManaged to the same value as before */
			XtSetMappedWhenManaged(hw->handle.victim,
						hw->handle.mwm);
			/* remanage the widget if it was previously managed */
			if (hw->handle.managed)
				XtManageChild(hw->handle.victim);
			/* Magic undone */

			hw->handle.detached = 0;

			/* I keep getting X errors here and don't really
			   see any reason why; this is a workaround */
			old_handler = XSetErrorHandler(bogosity);
			XtCallCallbackList(w, hw->handle.attach_callbacks,
				(XtPointer)hw->handle.victim);
			XFlush(XtDisplay(w));
			XSync(XtDisplay(w), False);
			XSetErrorHandler(old_handler);
		}
	}
	hw->handle.oldtime = newtime;
}

static void handle_drag(Widget w, XEvent *event, String *p, Cardinal *n)
{
	MwHandleWidget hw = (MwHandleWidget)w;
	Window root, child;
	int root_x, root_y, win_x, win_y;
	unsigned int mask;
	Dimension width, height;
	if (hw->handle.victim == None) return;

	XQueryPointer(XtDisplay(w), XtWindow(w),
		&root, &child,
		&root_x, &root_y,
		&win_x, &win_y,
		&mask);
	if (!hw->handle.detached) {
		int owner_events = False;
	        unsigned int event_mask = ButtonReleaseMask | PointerMotionMask |
                        EnterWindowMask | LeaveWindowMask;
	        int pointer_mode = GrabModeAsync;
	        int keyboard_mode = GrabModeAsync;
	        static Cursor cursor = None;
	        Time now = CurrentTime;
		hw->handle.parent = XtParent(hw->handle.victim);/* Rudegrid */
		XtVaGetValues(hw->handle.victim,
			XtNx, &(hw->handle.old_x),
			XtNy, &(hw->handle.old_y),
			XtNwidth, &width,
			XtNheight, &height,
			(char *)0);
		hw->handle.shell = XtVaCreatePopupShell("handle_shell",
			overrideShellWidgetClass, w,
			XtNx, root_x,
			XtNy, root_y,
			XtNwidth, width,
			XtNheight, height,
			(char *)0);

		XtRealizeWidget(hw->handle.shell);
		XtPopup(hw->handle.shell, XtGrabNone);

		XReparentWindow(XtDisplay(w),
			XtWindow(hw->handle.victim),
			XtWindow(hw->handle.shell), 0, 0);

		/* And now, a bit of magic */
		/* remember if the widget was mappenWhenManaged... */
		XtVaGetValues(hw->handle.victim,
			XtNmappedWhenManaged, &(hw->handle.mwm),
			(char *)0);
		/* ... and if it was managed */
		hw->handle.managed = XtIsManaged(hw->handle.victim);
		XtSetMappedWhenManaged(hw->handle.victim, False);
		/* must unmanage the child to keep it from being resized
		   by the parent. */
		XtUnmanageChild(hw->handle.victim);
		/* this has probably unmapped the widget, so remap it */
		XtMapWidget(hw->handle.victim);
		/* End of magic */

		XtCallCallbackList(w, hw->handle.detach_callbacks,
			(XtPointer)hw->handle.victim);
		XtGrabPointer(w, owner_events, event_mask,
			pointer_mode, keyboard_mode, None, cursor, now);
		hw->handle.detached = 1;

	} else {
		XtVaSetValues(hw->handle.shell,
			XtNx, root_x,
			XtNy, root_y,
			(char *)0);
	}
}

static void handle_release(Widget w, XEvent *event, String *p, Cardinal *n)
{
	XtUngrabPointer(w, CurrentTime);
}

static Boolean SetValues (Widget current, Widget request, Widget new,
			ArgList args, Cardinal *num_args)
{
	return True;
}

Widget MwMakeHandle(Widget pw, Widget victim,
		void (*detach)(Widget, XtPointer, XtPointer),
		void(*attach)(Widget, XtPointer, XtPointer))
{
	Widget w;
	Pixmap pm;
	Pixel color;
	XtVaGetValues(pw,
		XtNbackground, &color,
		(char *)0);
	pm = MwLoadPixmap(XtDisplay(pw), color, "handlebg.xpm");
	w = XtVaCreateManagedWidget("handle",
		mwHandleWidgetClass, pw,
		XtNforeground, color,
		XtNbackgroundPixmap, pm,
		XtNshadowWidth, 1,
		XtNhandleVictim, victim,
		XtNlabel, "",
		(char *)0);
	XtAddCallback(w, XtNattachCallback, attach, (XtPointer)victim);
	XtAddCallback(w, XtNdetachCallback, detach, (XtPointer)victim);
	return w;
}