summaryrefslogtreecommitdiffstats
path: root/kicker/extensions/dockbar/dockcontainer.cpp
blob: f2117346d91f2c48798d3fe141df08c0982bf9f2 (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
/*****************************************************************

Copyright (c) 2000 Matthias Elter <elter@kde.org>

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 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.

******************************************************************/

#include <qwidget.h>
#include <qtooltip.h>
#include <kwin.h>
#include <qvalidator.h>
#include <kinputdialog.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kdebug.h>

#include "dockcontainer.h"
#include "dockcontainer.moc"

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>


DockContainer::DockContainer( QString command, QWidget *parent,
                              QString resname, QString resclass, bool undocked_style )
    : QFrame( parent, resname.ascii(),
              undocked_style ? WStyle_Customize |
              WStyle_StaysOnTop | WStyle_Tool |
              WStyle_NoBorder | WX11BypassWM : 0 ),
      _embeddedWinId(0),
      _command(command),
      _resName(resname),
      _resClass(resclass)
{
    XSelectInput( qt_xdisplay(), winId(),
		  KeyPressMask | KeyReleaseMask |
		  ButtonPressMask | ButtonReleaseMask |
		  KeymapStateMask |
		  ButtonMotionMask |
		  PointerMotionMask |
		  EnterWindowMask | LeaveWindowMask |
		  FocusChangeMask |
		  ExposureMask |
		  StructureNotifyMask |
		  SubstructureRedirectMask |
		  SubstructureNotifyMask );
    if (!undocked_style) {
        setFrameStyle(StyledPanel | Raised);
        setLineWidth(border());
        QToolTip::add(this, command);
    } else {
        setFrameStyle(StyledPanel | Plain);
        setLineWidth(1);
    }
    resize(sz(),sz());
}

void DockContainer::embed( WId id )
{
    if( id == _embeddedWinId || id == 0)
        return;
    QRect geom = KWin::windowInfo(id,NET::WMKDEFrameStrut).frameGeometry();

    // does the same as KWM::prepareForSwallowing()
    XWithdrawWindow( qt_xdisplay(), id, qt_xscreen() );
    while( KWin::windowInfo(id, NET::XAWMState).mappingState() != NET::Withdrawn );

    XReparentWindow( qt_xdisplay(), id, winId(), 0, 0 );

    // resize if window is bigger than frame
    if( (geom.width() > width()) ||
        (geom.height() > height()) )
        XResizeWindow( qt_xdisplay(), id, width(), height() );
    else
        XMoveWindow(qt_xdisplay(), id,
                    (sz() -  geom.width())/2 - border(),
                    (sz() - geom.height())/2 - border());
    XMapWindow( qt_xdisplay(), id );
    XUngrabButton( qt_xdisplay(), AnyButton, AnyModifier, winId() );

    _embeddedWinId = id;
}

void DockContainer::unembed()
{
    if( _embeddedWinId )
        XReparentWindow( qt_xdisplay(), _embeddedWinId, qt_xrootwin(), 0, 0 );
}

void DockContainer::kill()
{
    if ( _embeddedWinId ) {
        XKillClient( qt_xdisplay(), _embeddedWinId );
        _embeddedWinId = 0; // in case the window does not exist anymore..
    }
    else emit embeddedWindowDestroyed(this); /* enable killing of empty windows.. */
}

bool DockContainer::x11Event( XEvent *e )
{
    switch( e->type ) {
    case DestroyNotify:
	if( e->xdestroywindow.window == _embeddedWinId || _embeddedWinId == 0) {
	    _embeddedWinId = 0;
	    emit embeddedWindowDestroyed(this);
	}
	break;
    case UnmapNotify:
        if ( e->xunmap.window == _embeddedWinId ) {
            kdDebug() << "Unmap Notify !!! I hate smart dockapps as wmpinboard " << command() << endl;
            _embeddedWinId = 0;
        }
        break;
    case ReparentNotify:
	if( _embeddedWinId &&
	    (e->xreparent.window == _embeddedWinId) &&
	    (e->xreparent.parent != winId()) ) {
	    _embeddedWinId = 0;
	}
	else if( e->xreparent.parent == winId() ) {
	    _embeddedWinId = e->xreparent.window;
	    embed( _embeddedWinId );
	}
	break;
    }

    return false;
}

void DockContainer::askNewCommand(bool bad_command)
{
    bool ok;
    QString title( i18n("Enter Command Line for Applet %1.%2").arg(resName()).arg(resClass()) );
    QString description( i18n("This applet does not behave correctly and the dockbar was unable to "
                              "find the command line necessary to launch it the next time KDE starts up") );
    QString cmd;

    /*
      I was not able to figure out why valgrind complains inside the getText call..
      (invalid read of size 1 in Xmb.. functions)
    */
    if (bad_command) {
        cmd = KInputDialog::getText( title, description,
                                     command(), &ok, this );
    } else {
        cmd = KInputDialog::getText( title, QString::null,
                                     command(), &ok, this );
    }
    if (ok) { _command = cmd; emit settingsChanged(this); }
}

void DockContainer::popupMenu(QPoint p)
{
    int r;
    {
        KPopupMenu pm(this);
        pm.insertItem( i18n("Kill This Applet"), 0);
        pm.insertItem( i18n("Change Command"), 1);
        r = pm.exec(p);
        /* pm is destroyed now .. if it is destroyed later,
           there is a risk that kill() double-frees it */
    }
    switch (r) {
        case 0: {
            kill();
        } break;
        case 1: {
            askNewCommand(false);
        } break;
    }
}

int& DockContainer::sz() {
    static int _sz = 66;
    return _sz;
}

int& DockContainer::border() {
    static int _border = 1;
    return _border;
}