File: window.c

package info (click to toggle)
ion2 20040729-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,828 kB
  • ctags: 3,466
  • sloc: ansic: 28,432; makefile: 473; sh: 230; perl: 16
file content (255 lines) | stat: -rw-r--r-- 5,184 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
/*
 * ion/ioncore/window.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2004. 
 *
 * Ion 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 of the License, or
 * (at your option) any later version.
 */

#include "common.h"
#include "objp.h"
#include "global.h"
#include "window.h"
#include "focus.h"
#include "rootwin.h"
#include "stacking.h"
#include "minmax.h"


/*{{{ Dynfuns */


void window_draw(WWindow *wwin, bool complete)
{
    CALL_DYN(window_draw, wwin, (wwin, complete));
}


void window_insstr(WWindow *wwin, const char *buf, size_t n)
{
    CALL_DYN(window_insstr, wwin, (wwin, buf, n));
}


int window_press(WWindow *wwin, XButtonEvent *ev, WRegion **reg_ret)
{
    int area=0;
    CALL_DYN_RET(area, int, window_press, wwin, (wwin, ev, reg_ret));
    return area;
}


void window_release(WWindow *wwin)
{
    CALL_DYN(window_release, wwin, (wwin));
}


bool region_reparent(WRegion *reg, WWindow *par, const WRectangle *geom)
{
    bool ret=FALSE;
    CALL_DYN_RET(ret, bool, region_reparent, reg, (reg, par, geom));
    return ret;
}


/*}}}*/
    

/*{{{ Init, create */


bool window_init(WWindow *wwin, WWindow *parent, Window win,
                 const WRectangle *geom)
{
    wwin->win=win;
    wwin->xic=NULL;
    wwin->keep_on_top_list=NULL;
    region_init(&(wwin->region), (WRegion*)parent, geom);
    if(win!=None){
        XSaveContext(wglobal.dpy, win, wglobal.win_context, (XPointer)wwin);
        if(parent!=NULL)
            stacking_init_window(parent, win);
    }
    return TRUE;
}


bool window_init_new(WWindow *wwin, WWindow *parent, const WRectangle *geom)
{
    Window win;
    
    win=create_simple_window(ROOTWIN_OF(parent), parent->win, geom);
    if(win==None)
        return FALSE;
    /* window_init does not fail */
    return window_init(wwin, parent, win, geom);
}


void window_deinit(WWindow *wwin)
{
    region_deinit((WRegion*)wwin);
    
    if(wwin->xic!=NULL)
        XDestroyIC(wwin->xic);

    if(wwin->win!=None){
        XDeleteContext(wglobal.dpy, wwin->win, wglobal.win_context);
        XDestroyWindow(wglobal.dpy, wwin->win);
    }
}


/*}}}*/


/*{{{ Find, X Window -> WRegion */


WRegion *find_window(Window win)
{
    WRegion *reg;
    
    if(XFindContext(wglobal.dpy, win, wglobal.win_context,
                    (XPointer*)&reg)!=0)
        return NULL;
    
    return reg;
}


WRegion *find_window_t(Window win, const WObjDescr *descr)
{
    WRegion *reg=find_window(win);
    
    if(reg==NULL)
        return NULL;
    
    if(!wobj_is((WObj*)reg, descr))
        return NULL;
    
    return reg;
}


/*}}}*/


/*{{{ Region dynfuns */


static void reparent_or_fit_window(WWindow *wwin, WWindow *parent,
                                   const WRectangle *geom)
{
    bool move=(REGION_GEOM(wwin).x!=geom->x ||
               REGION_GEOM(wwin).y!=geom->y);
    int w=maxof(1, geom->w);
    int h=maxof(1, geom->h);

    if(parent!=NULL){
        region_detach_parent((WRegion*)wwin);
        XReparentWindow(wglobal.dpy, wwin->win, parent->win, geom->x, geom->y);
        XResizeWindow(wglobal.dpy, wwin->win, w, h);
        region_attach_parent((WRegion*)wwin, (WRegion*)parent);
    }else{
        XMoveResizeWindow(wglobal.dpy, wwin->win, geom->x, geom->y, w, h);
    }
    
    REGION_GEOM(wwin)=*geom;

    if(move)
        region_notify_subregions_move(&(wwin->region));
}


bool reparent_window(WWindow *wwin, WWindow *parent, const WRectangle *geom)
{
    if(!same_rootwin((WRegion*)wwin, (WRegion*)parent))
        return FALSE;
    reparent_or_fit_window(wwin, parent, geom);
    return TRUE;
}


void window_fit(WWindow *wwin, const WRectangle *geom)
{
    reparent_or_fit_window(wwin, NULL, geom);
}


void window_map(WWindow *wwin)
{
    XMapWindow(wglobal.dpy, wwin->win);
    MARK_REGION_MAPPED(wwin);
}


void window_unmap(WWindow *wwin)
{
    XUnmapWindow(wglobal.dpy, wwin->win);
    MARK_REGION_UNMAPPED(wwin);
}


void window_set_focus_to(WWindow *wwin, bool warp)
{
    if(warp)
        do_warp((WRegion*)wwin);
    SET_FOCUS(wwin->win);
}


void do_restack_window(Window win, Window other, int stack_mode)
{
    XWindowChanges wc;
    int wcmask;
    
    wcmask=CWStackMode;
    wc.stack_mode=stack_mode;
    if((wc.sibling=other)!=None)
        wcmask|=CWSibling;

    XConfigureWindow(wglobal.dpy, win, wcmask, &wc);
}


Window window_restack(WWindow *wwin, Window other, int mode)
{
    do_restack_window(wwin->win, other, mode);
    return wwin->win;
}


Window window_x_window(const WWindow *wwin)
{
    return wwin->win;
}


/*}}}*/


/*{{{Dynamic function table and class implementation */


static DynFunTab window_dynfuntab[]={
    {region_fit, window_fit},
    {region_map, window_map},
    {region_unmap, window_unmap},
    {region_set_focus_to, window_set_focus_to},
    {(DynFun*)region_reparent, (DynFun*)reparent_window},
    {(DynFun*)region_restack, (DynFun*)window_restack},
    {(DynFun*)region_x_window, (DynFun*)window_x_window},
    END_DYNFUNTAB
};


IMPLOBJ(WWindow, WRegion, window_deinit, window_dynfuntab);

    
/*}}}*/