summaryrefslogtreecommitdiff
path: root/src/msimplewindowframe.cpp
blob: ff12e03ab25e9eb165e973d79d96547757e001c5 (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
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of mcompositor.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "msimplewindowframe.h"

#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QPalette>
#include <QLinearGradient>
#include <QDesktopWidget>
#include <QX11Info>
#include <QtDebug>
#include <QPushButton>

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

QPixmap *MSimpleWindowFrame::homepix  = 0;
QPixmap *MSimpleWindowFrame::closepix = 0;

class SystemButton: public QLabel
{
    Q_OBJECT
public:
    SystemButton(const QPixmap &iconId, QWidget *p = 0)
        : QLabel(p) {
        setPixmap(iconId);
    }

    virtual void mouseReleaseEvent(QMouseEvent *e) {
        emit clicked();
        QLabel::mouseReleaseEvent(e);
    }

signals:
    void clicked();
};

MSimpleWindowFrame::MSimpleWindowFrame(Qt::HANDLE w)
    : QWidget(0),
      managed_window(w)
{
    // use failsafe images
    if (!homepix)
        homepix  = new QPixmap(":/images/homepix.png");
    if (!closepix)
        closepix = new QPixmap(":/images/closepix.png");

    // construct toolbar
    QHBoxLayout *l = new QHBoxLayout();
    l->setContentsMargins(0, 0, 0, 0);
    homesb = new SystemButton(*homepix, this);
    l->addWidget(homesb);
    l->addStretch();
    SystemButton *closesb = new SystemButton(*closepix, this);
    l->addWidget(closesb);

    connect(homesb, SIGNAL(clicked()), SLOT(minimizeWindow()));
    connect(closesb, SIGNAL(clicked()), SLOT(closeWindow()));

    QPalette palette;
    palette.setColor(QPalette::Background, Qt::black);
    setAutoFillBackground(true);
    setPalette(palette);

    QVBoxLayout *vb = new QVBoxLayout();
    vb->setContentsMargins(0, 0, 0, 0);
    window_area = new QWidget(this);
    vb->addLayout(l);
    vb->addWidget(window_area);
    setLayout(vb);

    // give suggested size for new windows
    QSize minimum = l->minimumSize();
    int wheight = QApplication::desktop()->height() - minimum.height();
    window_size = QSize(QApplication::desktop()->width(), wheight - 10);
    window_area->setMinimumSize(window_size);

    // prevent leaks for now. decorator handles this anyway
    // XTextProperty p;
//     if(XGetWMName(QX11Info::display(), w, &p))
//         XSetWMName(QX11Info::display(), winId(), &p);

    // resize to fit the whole screen
    resize(QApplication::desktop()->size());
}

MSimpleWindowFrame::~MSimpleWindowFrame()
{
    // QObject frees allocated resources
}

void MSimpleWindowFrame::setDialogDecoration(bool dialogDecor)
{
    dialogDecor ? homesb->hide() : homesb->show();
}

const QSize &MSimpleWindowFrame::suggestedWindowSize() const
{
    return window_size;
}

Qt::HANDLE MSimpleWindowFrame::windowArea()
{
    return window_area->winId();
}

Qt::HANDLE MSimpleWindowFrame::managedWindow()
{
    return managed_window;
}

void MSimpleWindowFrame::minimizeWindow()
{
    XEvent e;
    memset(&e, 0, sizeof(e));

    e.xclient.type = ClientMessage;
    e.xclient.message_type = XInternAtom(QX11Info::display(), "WM_CHANGE_STATE",
                                         False);
    e.xclient.display = QX11Info::display();
    e.xclient.window = managed_window;
    e.xclient.format = 32;
    e.xclient.data.l[0] = IconicState;
    e.xclient.data.l[1] = 0;
    e.xclient.data.l[2] = 0;
    e.xclient.data.l[3] = 0;
    e.xclient.data.l[4] = 0;
    XSendEvent(QX11Info::display(), QX11Info::appRootWindow(),
               False, (SubstructureNotifyMask | SubstructureRedirectMask), &e);

    XSync(QX11Info::display(), FALSE);
}

void MSimpleWindowFrame::closeWindow()
{
    XEvent e;
    memset(&e, 0, sizeof(e));

    e.xclient.type         = ClientMessage;
    e.xclient.window       = managed_window;
    e.xclient.message_type = XInternAtom(QX11Info::display(),
                                         "_NET_CLOSE_WINDOW", False);
    e.xclient.format       = 32;
    e.xclient.data.l[0]    = CurrentTime;
    e.xclient.data.l[1]    = QX11Info::appRootWindow();
    XSendEvent(QX11Info::display(), QX11Info::appRootWindow(),
               False, SubstructureRedirectMask, &e);

    XSync(QX11Info::display(), FALSE);
}

void MSimpleWindowFrame::setParentFrame(Qt::HANDLE frame)
{
    parent_frame = frame;
}

#include "msimplewindowframe.moc"