[go: up one dir, main page]

File: appwin.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (281 lines) | stat: -rw-r--r-- 6,278 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
/*
 *
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/*
 * $Id: appwin.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao 
 *  All rights reserved.
 *.
 *
 *  Manage application windows
 *
 */

#if defined(F_ID) || defined(DEBUG)
char *fl_id_evt = "$Id: appwin.c,v 1.0 2002/05/08 05:16:30 zhao Release $";
#endif

#include "forms.h"

/*****************************************************************
 * Application window management. Data structure is grossly wrong
 * ****TODO******
 **************************************************************{**/


/* some constant handlers */
static int
handle_mappingnotify(XEvent * xev, void *user_data)
{
    XRefreshKeyboardMapping((XMappingEvent *) xev);
    return 0;
}


static void
remove_app_win(FL_WIN * appwin)
{
    FL_WIN *fwin;

#if (FL_DEBUG >= ML_DEBUG)
    M_info("RemoveAppwin", "deleting 0x%lx", appwin->win);
#endif
    if (fl_app_win == appwin)
    {
	fl_app_win = appwin->next;
    }
    else
    {
	for (fwin = fl_app_win; fwin && fwin->next != appwin; fwin = fwin->next)
	    ;
	if (fwin)
	    fwin->next = fwin->next->next;
    }
    appwin->win = 0;
    fl_free(appwin);
}

/* given a window, find the correct structure, create if necessary */
static FL_WIN *
find_fl_win_struct(register Window win)
{
    register FL_WIN *fwin = fl_app_win, *lwin = fl_app_win;

    for (; fwin && fwin->win != win; lwin = fwin, fwin = fwin->next)
	;

    if (!fwin)
    {
#if (FL_DEBUG >= ML_DEBUG)
	M_info("AppwinFind", "Creating for 0x%lx", win);
#endif
	/* append to the end */
	fwin = fl_calloc(1, sizeof(*fwin));
	fwin->next = 0;
	fwin->win = win;
	/* default handlers */
	fwin->callback[MappingNotify] = handle_mappingnotify;
	if (!fl_app_win)
	    fl_app_win = fwin;
	else
	    lwin->next = fwin;
    }
    return fwin;
}


FL_APPEVENT_CB
fl_set_preemptive_callback(Window win, FL_APPEVENT_CB pcb, void *data)
{
    FL_WIN *fwin;
    FL_APPEVENT_CB old = 0;

    if (!(fwin = find_fl_win_struct(win)))
	return 0;
    old = fwin->pre_emptive;
    fwin->pre_emptive = pcb;
    fwin->pre_emptive_data = data;
    return old;
}

/*  add an event handler for window */
FL_APPEVENT_CB
fl_add_event_callback(Window win, int ev,
		      FL_APPEVENT_CB wincb, void *user_data)
{
    FL_WIN *fwin;
    int i, nev;
    FL_APPEVENT_CB old = 0;

    if (ev < 0 || ev >= LASTEvent)
	return 0;

    if (!(fwin = find_fl_win_struct(win)))
    {
	M_err("AddEventCallback", "Bad Window x%lx", win);
	return 0;
    }

    /* ev <= 1 means all events. Also events start from 2 */
    nev = ev;
    if (ev < 2)
    {
	ev = 2;
	nev = LASTEvent - 1;
    }

    for (i = ev; i <= nev; i++)
    {
	old = fwin->callback[i];
	fwin->callback[i] = wincb;
	fwin->user_data[i] = user_data;
    }
    return old;
}

void
fl_remove_event_callback(Window win, int ev)
{
    FL_WIN *fwin;

    if (ev >= LASTEvent)
	return;

    if (!(fwin = find_fl_win_struct(win)))
	return;

    if (ev >= 2)
    {
	fwin->callback[ev] = 0;
	fwin->user_data[ev] = 0;
    }
    else
    {
	fwin->win = 0;		/* window deactivated */
	remove_app_win(fwin);
    }
}

typedef struct
{
    int event;
    unsigned long mask;
}
EMS;

static EMS ems[] =
{
    {CirculateNotify, StructureNotifyMask},
    {ConfigureNotify, StructureNotifyMask},
    {DestroyNotify, StructureNotifyMask},
    {CreateNotify, StructureNotifyMask},
    {GravityNotify, StructureNotifyMask},
    {MapNotify, StructureNotifyMask},
    {ReparentNotify, StructureNotifyMask},
    {UnmapNotify, StructureNotifyMask},

    {CirculateRequest, SubstructureRedirectMask},
    {ConfigureRequest, SubstructureRedirectMask},
    {MapRequest, SubstructureRedirectMask},

    {ResizeRequest, ResizeRedirectMask},


    {Expose, ExposureMask},

    {EnterNotify, EnterWindowMask},
    {LeaveNotify, LeaveWindowMask},
    {KeyPress, KeyPressMask},
    {KeyRelease, KeyReleaseMask},
    {ButtonPress, ButtonPressMask /* | OwnerGrabButtonMask */ },
    {ButtonRelease, ButtonReleaseMask /* | OwnerGrabButtonMask */ },
{MotionNotify, PointerMotionMask | ButtonMotionMask | PointerMotionHintMask},

    {FocusIn, FocusChangeMask},
    {FocusOut, FocusChangeMask},

    {KeymapNotify, KeymapStateMask},
    {PropertyNotify, PropertyChangeMask},
    {VisibilityNotify, VisibilityChangeMask},
    {ColormapNotify, ColormapChangeMask},

 /* non-maskable events */
    {MappingNotify, 0},
    {SelectionNotify, 0},
    {SelectionRequest, 0},
    {SelectionClear, 0},
    {ClientMessage, 0},
    {GraphicsExpose, 0},
    {NoExpose, 0}
};

unsigned long
fl_xevent_to_mask(int event)
{
    register EMS *em = ems, *eme = ems + sizeof(ems) / sizeof(ems[0]);

    for (; em < eme; em++)
	if (em->event == event)
	    return em->mask;
    return 0;
}

void
fl_activate_event_callbacks(Window win)
{
    FL_WIN *fwin = fl_app_win;
    int i;
    unsigned long mask;

    for (; fwin && fwin->win != win; fwin = fwin->next)
	;

    if (!fwin)
    {
	M_err("ActiveEventCB", "Unknown window 0x%lx", win);
	return;
    }

    /* solicit all registered events */
    for (mask = i = 0; i < LASTEvent; i++)
	if (fwin->callback[i])
	    mask |= fl_xevent_to_mask(i);
    XSelectInput(flx->display, win, mask);
}


void
fl_winclose(Window win)
{
    XEvent xev;

    XUnmapWindow(flx->display, win);
    XDestroyWindow(flx->display, win);

    XSync(flx->display, 0);
    while (XCheckWindowEvent(flx->display, win, FL_ALL_MASKS, &xev))
	fl_xevent_name("Eaten", &xev);

    fl_remove_event_callback(win, 0);
}