summaryrefslogtreecommitdiff
path: root/src/FbTk/FbWindow.cc
blob: 5417c09cb940e9f81cc2108a8a38aaab4f492409 (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// FbWindow.cc for FbTk - fluxbox toolkit
// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: FbWindow.cc,v 1.38 2004/09/09 14:29:10 akir Exp $

#include "FbWindow.hh"
#include "FbPixmap.hh"

#include "EventManager.hh"
#include "Color.hh"
#include "App.hh"
#include "Transparent.hh"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#include <X11/Xutil.h>
#include <X11/Xatom.h>

#ifdef HAVE_CASSERT
  #include <cassert>
#else
  #include <assert.h>
#endif

namespace FbTk {

Display *FbWindow::s_display = 0;

FbWindow::FbWindow():m_parent(0), m_screen_num(0), m_window(0), m_x(0), m_y(0), 
                     m_width(0), m_height(0), m_border_width(0), m_depth(0), m_destroy(true),
                     m_buffer_pm(0) {

    if (s_display == 0)
        s_display = App::instance()->display();
}

FbWindow::FbWindow(const FbWindow& the_copy):m_parent(the_copy.parent()), 
                                             m_screen_num(the_copy.screenNumber()), m_window(the_copy.window()), 
                                             m_x(the_copy.x()), m_y(the_copy.y()), 
                                             m_width(the_copy.width()), m_height(the_copy.height()),
                                             m_border_width(the_copy.borderWidth()), 
                                             m_depth(the_copy.depth()), m_destroy(true),
                                             m_buffer_pm(0) {
    if (s_display == 0)
        s_display = App::instance()->display();

    the_copy.m_window = 0;
}

FbWindow::FbWindow(int screen_num,
                   int x, int y, 
                   unsigned int width, unsigned int height, 
                   long eventmask, 
                   bool override_redirect,
                   bool save_unders,
                   int depth,
                   int class_type):
    m_parent(0),
    m_screen_num(screen_num),
    m_destroy(true),
    m_buffer_pm(0) {
	
    create(RootWindow(FbTk::App::instance()->display(), screen_num), 
           x, y, width, height, eventmask,
           override_redirect, save_unders, depth, class_type);
};

FbWindow::FbWindow(const FbWindow &parent,
                   int x, int y, unsigned int width, unsigned int height, 
                   long eventmask,
                   bool override_redirect,
                   bool save_unders,
                   int depth, int class_type):
    m_parent(&parent),
    m_screen_num(parent.screenNumber()), 
    m_destroy(true),
    m_buffer_pm(0) { 

    create(parent.window(), x, y, width, height, eventmask, 
           override_redirect, save_unders, depth, class_type);
	
	
};

FbWindow::FbWindow(Window client):m_parent(0), 
                                  m_screen_num(0),
                                  m_window(0),
                                  m_x(0), m_y(0),
                                  m_width(1), m_height(1),
                                  m_border_width(0),
                                  m_depth(0),
                                  m_destroy(false),  // don't destroy this window
                                  m_buffer_pm(0) {

    if (s_display == 0)
        s_display = App::instance()->display();

    setNew(client);
}

FbWindow::~FbWindow() {

    if (m_window != 0) {
        // so we don't get any dangling eventhandler for this window
        FbTk::EventManager::instance()->remove(m_window); 
        if (m_destroy)
            XDestroyWindow(s_display, m_window);     
    }
}


void FbWindow::setBackgroundColor(const FbTk::Color &bg_color) {
    XSetWindowBackground(s_display, m_window, bg_color.pixel());
}

void FbWindow::setBackgroundPixmap(Pixmap bg_pixmap) {
    XSetWindowBackgroundPixmap(s_display, m_window, bg_pixmap);
}

void FbWindow::setBorderColor(const FbTk::Color &border_color) {
    XSetWindowBorder(s_display, m_window, border_color.pixel());
}

void FbWindow::setBorderWidth(unsigned int size) {	
    XSetWindowBorderWidth(s_display, m_window, size);
    m_border_width = size;
}

void FbWindow::setName(const char *name) {
    XStoreName(s_display, m_window, name);
}

void FbWindow::setEventMask(long mask) {
    XSelectInput(s_display, m_window, mask);
}

void FbWindow::clear() {
    XClearWindow(s_display, m_window);
}

void FbWindow::clearArea(int x, int y, 
                         unsigned int width, unsigned int height, 
                         bool exposures) {
    XClearArea(s_display, window(), x, y, width, height, exposures);
}

void FbWindow::updateTransparent(int the_x, int the_y, unsigned int the_width, unsigned int the_height) {
#ifdef HAVE_XRENDER
    if (width() == 0 || height() == 0)
        return;

    if (the_width == 0 || the_height == 0) {
        the_width = width();
        the_height = height();
    }

    if (the_x < 0 || the_y < 0) {
        the_x = 0;
        the_y = 0;
    }

    if (!m_transparent.get())
        return;

    // update source and destination if needed
    Pixmap root = FbPixmap::getRootPixmap(screenNumber());
    if (m_transparent->source() != root)
        m_transparent->setSource(root, screenNumber());

    if (m_buffer_pm) {
        if (m_transparent->dest() != m_buffer_pm) {
            m_transparent->setDest(m_buffer_pm, screenNumber());
        }
    } else if (m_transparent->dest() != window())
        m_transparent->setDest(window(), screenNumber());


    // get root position

    const FbWindow *root_parent = parent();
    // our position in parent ("root")
    int root_x = x() + borderWidth(), root_y = y() + borderWidth();
    if (root_parent != 0) {
        root_x += root_parent->x() + root_parent->borderWidth();
        root_y += root_parent->y() + root_parent->borderWidth();
        while (root_parent->parent() != 0) {
            root_parent = root_parent->parent();
            root_x += root_parent->x() + root_parent->borderWidth();
            root_y += root_parent->y() + root_parent->borderWidth();
        }

    } // else toplevel window so we already have x, y set

    // render background image from root pos to our window
    m_transparent->render(root_x + the_x, root_y + the_y,
                          the_x, the_y,
                          the_width, the_height);
#endif // HAVE_XRENDER
}

void FbWindow::setAlpha(unsigned char alpha) {
#ifdef HAVE_XRENDER
    if (m_transparent.get() == 0 && alpha < 255) {
        m_transparent.reset(new Transparent(FbPixmap::getRootPixmap(screenNumber()), window(), alpha, screenNumber()));
    } else if (alpha < 255 && alpha != m_transparent->alpha())
        m_transparent->setAlpha(alpha);
    else if (alpha == 255)
        m_transparent.reset(0); // destroy transparent object
#endif // HAVE_XRENDER
}

unsigned char FbWindow::alpha() const {
#ifdef HAVE_XRENDER
    if (m_transparent.get())
        return m_transparent->alpha();
#endif // HAVE_XRENDER
    return 255;
}

FbWindow &FbWindow::operator = (const FbWindow &win) {
    m_parent = win.parent();
    m_screen_num = win.screenNumber();
    m_window = win.window();
    m_x = win.x();
    m_y = win.y();
    m_width = win.width();
    m_height = win.height();
    m_border_width = win.borderWidth();
    m_depth = win.depth();
    // take over this window
    win.m_window = 0;
    return *this;
}

FbWindow &FbWindow::operator = (Window win) {
    setNew(win);	
    return *this;
}

void FbWindow::setNew(Window win) {
    if (s_display == 0)
        s_display = App::instance()->display();

    if (m_window != 0 && m_destroy)
        XDestroyWindow(s_display, m_window);

    m_window = win;

    if (m_window != 0) {
        updateGeometry();
        XWindowAttributes attr;
        attr.screen = 0;
        //get screen number
        if (XGetWindowAttributes(s_display,
                                 m_window,
                                 &attr) != 0 && attr.screen != 0) {
            m_screen_num = XScreenNumberOfScreen(attr.screen);
            if (attr.width <= 0)
                m_width = 1;
            else
                m_width = attr.width;

            if (attr.height <= 0)
                m_height = 1;
            else
                m_height = attr.height;

            m_x = attr.x;
            m_y = attr.y;
            m_depth = attr.depth;
            m_border_width = attr.border_width;
        }
        
    }
}

void FbWindow::show() {
    XMapWindow(s_display, m_window);
}

void FbWindow::showSubwindows() {
    XMapSubwindows(s_display, m_window);
}

void FbWindow::hide() {
    XUnmapWindow(s_display, m_window);
}

void FbWindow::lower() {
    XLowerWindow(s_display, window());
}

void FbWindow::raise() {
    XRaiseWindow(s_display, window());
}

void FbWindow::setInputFocus(int revert_to, int time) {
    XSetInputFocus(s_display, window(), revert_to, time);
}

void FbWindow::setCursor(Cursor cur) {
    XDefineCursor(s_display, window(), cur); 
}

void FbWindow::unsetCursor() {
    XUndefineCursor(s_display, window());
}

void FbWindow::reparent(const FbWindow &parent, int x, int y) {
    XReparentWindow(s_display, window(), parent.window(), x, y);
    m_parent = &parent;
    updateGeometry();
}

std::string FbWindow::textProperty(Atom property) const {
    XTextProperty text_prop;
    char ** stringlist;
    int count;
    std::string ret;

    if (XGetTextProperty(s_display, window(), &text_prop, property) == 0)
        return "";

    if (text_prop.value == 0 || text_prop.nitems == 0)
        return "";

    if (text_prop.encoding != XA_STRING) {
        // still returns a "StringList" despite the different name
        if (XmbTextPropertyToTextList(s_display, &text_prop, &stringlist, &count) == 0 || count == 0)
            return "";
    } else {
        if (XTextPropertyToStringList(&text_prop, &stringlist, &count) == 0 || count == 0)
            return "";

    }

    ret = stringlist[0];
    XFreeStringList(stringlist);
    return ret;
}

bool FbWindow::property(Atom property,
                        long long_offset, long long_length,
                        bool do_delete,
                        Atom req_type,
                        Atom *actual_type_return,
                        int *actual_format_return,
                        unsigned long *nitems_return,
                        unsigned long *bytes_after_return,
                        unsigned char **prop_return) const {
    if (XGetWindowProperty(s_display, window(), 
                           property, long_offset, long_length, do_delete, 
                           req_type, actual_type_return,
                           actual_format_return, nitems_return,
                           bytes_after_return, prop_return) == Success)
        return true;

    return false;
}

void FbWindow::changeProperty(Atom property, Atom type,
                              int format,
                              int mode,
                              unsigned char *data,
                              int nelements) {
    
    XChangeProperty(s_display, m_window, property, type,
                    format, mode, 
                    data, nelements);
}

int FbWindow::screenNumber() const {
    return m_screen_num;
}

long FbWindow::eventMask() const {
    XWindowAttributes attrib;
    XGetWindowAttributes(s_display, window(), 
                         &attrib);
    return attrib.your_event_mask;

}

void FbWindow::setBufferPixmap(Pixmap pm) {
    m_buffer_pm = pm;
}

void FbWindow::updateGeometry() {
    if (m_window == 0)
        return;

    Window root;
    unsigned int border_width, depth;
    XGetGeometry(s_display, m_window, &root, &m_x, &m_y,
                 (unsigned int *)&m_width, (unsigned int *)&m_height, 
                 &border_width, &depth);
    m_depth = depth;
}

void FbWindow::create(Window parent, int x, int y,
                      unsigned int width, unsigned int height, 
                      long eventmask, bool override_redirect,
                      bool save_unders, int depth, int class_type) {
                     

    if (s_display == 0)
        s_display = FbTk::App::instance()->display();

    m_border_width = 0;

    long valmask = CWEventMask;
    XSetWindowAttributes values;
    values.event_mask = eventmask;

    if (override_redirect) {        
        valmask |= CWOverrideRedirect;
        values.override_redirect = True;
    }

    if (save_unders) {
        valmask |= CWSaveUnder;
        values.save_under = True;
    }

    m_window = XCreateWindow(s_display, parent, x, y, width, height,
                             0, // border width  
                             depth, // depth
                             class_type, // class
                             CopyFromParent, // visual
                             valmask, // create mask
                             &values); // create atrribs
    
    assert(m_window);

    updateGeometry();
    FbWindow::setBackgroundColor(Color("gray", screenNumber()));
}

bool operator == (Window win, const FbWindow &fbwin) {
    return win == fbwin.window();
}

};