~jbicha/unity-2d/fix-launcher-quicklist-naming-lp949636

« back to all changes in this revision

Viewing changes to panel/lib/panel.cpp

  • Committer: Aurelien Gateau
  • Date: 2010-10-06 13:52:55 UTC
  • Revision ID: aurelien.gateau@canonical.com-20101006135255-j4gsdm7k3grh0ydh
Merged in panel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of unity-qt
 
3
 *
 
4
 * Copyright 2010 Canonical Ltd.
 
5
 *
 
6
 * Authors:
 
7
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 
8
 *
 
9
 * License: LGPL v3
 
10
 */
 
11
// Self
 
12
#include "panel.h"
 
13
 
 
14
// Local
 
15
#include "applet.h"
 
16
 
 
17
// Qt
 
18
#include <QApplication>
 
19
#include <QDebug>
 
20
#include <QDesktopWidget>
 
21
#include <QHBoxLayout>
 
22
#include <QX11Info>
 
23
 
 
24
// X
 
25
#include <X11/Xlib.h>
 
26
#include <X11/Xatom.h>
 
27
 
 
28
namespace UnityQt
 
29
{
 
30
 
 
31
struct PanelPrivate
 
32
{
 
33
    Panel* q;
 
34
    Panel::Edge m_edge;
 
35
    QHBoxLayout* m_layout;
 
36
 
 
37
    void updateStrut()
 
38
    {
 
39
        QDesktopWidget* desktop = QApplication::desktop();
 
40
        const QRect screen = desktop->screenGeometry(q);
 
41
        const QRect available = desktop->availableGeometry(q);
 
42
        QRect rect;
 
43
 
 
44
        Atom atom = XInternAtom(QX11Info::display(), "_NET_WM_STRUT_PARTIAL", False);
 
45
 
 
46
        ulong struts[12];
 
47
        switch (m_edge) {
 
48
        case Panel::LeftEdge:
 
49
            rect = QRect(screen.left(), available.top(), q->width(), available.height());
 
50
            struts = {
 
51
                q->width(), 0, 0, 0,
 
52
                available.top(), available.bottom(), 0, 0,
 
53
                0, 0, 0, 0
 
54
                };
 
55
            break;
 
56
        case Panel::TopEdge:
 
57
            rect = QRect(screen.left(), screen.top(), screen.width(), q->height());
 
58
            struts = {
 
59
                0, 0, q->height(), 0,
 
60
                0, 0, 0, 0,
 
61
                screen.left(), screen.right(), 0, 0
 
62
                };
 
63
            break;
 
64
        }
 
65
 
 
66
        q->setGeometry(rect);
 
67
 
 
68
        XChangeProperty(QX11Info::display(), q->effectiveWinId(), atom,
 
69
                        XA_CARDINAL, 32, PropModeReplace,
 
70
                        (unsigned char *) &struts, 12);
 
71
    }
 
72
 
 
73
    void updateLayoutDirection()
 
74
    {
 
75
        QBoxLayout::Direction direction;
 
76
        switch(m_edge) {
 
77
        case Panel::TopEdge:
 
78
            direction = QApplication::isRightToLeft() ? QBoxLayout::RightToLeft : QBoxLayout::LeftToRight;
 
79
            break;
 
80
        case Panel::LeftEdge:
 
81
            direction = QBoxLayout::TopToBottom;
 
82
            break;
 
83
        }
 
84
        m_layout->setDirection(direction);
 
85
    }
 
86
 
 
87
    void updateEdge()
 
88
    {
 
89
        updateStrut();
 
90
        updateLayoutDirection();
 
91
    }
 
92
};
 
93
 
 
94
Panel::Panel(QWidget* parent)
 
95
: QWidget(parent)
 
96
, d(new PanelPrivate)
 
97
{
 
98
    d->q = this;
 
99
    d->m_edge = Panel::TopEdge;
 
100
    d->m_layout = new QHBoxLayout(this);
 
101
    d->m_layout->setMargin(0);
 
102
    d->m_layout->setSpacing(0);
 
103
    setAttribute(Qt::WA_X11NetWmWindowTypeDock);
 
104
}
 
105
 
 
106
Panel::~Panel()
 
107
{
 
108
    delete d;
 
109
}
 
110
 
 
111
void Panel::setEdge(Panel::Edge edge)
 
112
{
 
113
    d->m_edge = edge;
 
114
    if (isVisible()) {
 
115
        d->updateEdge();
 
116
    }
 
117
}
 
118
 
 
119
Panel::Edge Panel::edge() const
 
120
{
 
121
    return d->m_edge;
 
122
}
 
123
 
 
124
void Panel::showEvent(QShowEvent* event)
 
125
{
 
126
    QWidget::showEvent(event);
 
127
    d->updateEdge();
 
128
}
 
129
 
 
130
void Panel::addWidget(QWidget* widget)
 
131
{
 
132
    d->m_layout->addWidget(widget);
 
133
}
 
134
 
 
135
void Panel::addSpacer()
 
136
{
 
137
    d->m_layout->addStretch();
 
138
}
 
139
 
 
140
} // namespace
 
141
 
 
142
#include "panel.moc"