summaryrefslogtreecommitdiffstats
path: root/src/xosd.cpp
blob: 45d079978a19b8e88f74e34101bc650c021e0601 (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
#include "xosd.h"

#include <qpainter.h>
#include <qbitmap.h>
#include <qstring.h>
#include <qfont.h>
#include <qfontmetrics.h>

#include <qwidget.h>
#include <klocale.h>
#include <netwm.h>
#include <netwm_def.h>
#include <kwin.h>

#include <kdebug.h>
#include <stdlib.h>

xosd::xosd(QWidget *parent, const char *name) : QWidget(parent, name, WStyle_Customize |  WRepaintNoErase | WStyle_NoBorder | WDestructiveClose | WResizeNoErase | WMouseNoMask | WStyle_StaysOnTop | WX11BypassWM)
{
  info = new NETWinInfo( qt_xdisplay(), winId(), qt_xrootwin(), NET::WMState);
  info->setDesktop( NETWinInfo::OnAllDesktops );
  hide();
  KWin::setState(winId(), NET::SkipTaskbar | NET::SkipPager);
  setCaption("kooldock xosd window");
}

xosd::~xosd()
{
}

void xosd::setColor(const QString& color)
{
  fontColor=color;
}

void xosd::setShadowColor(const QString& color)
{
  shadowColor=color;
}

void xosd::setOrientation(int orientation, int w, int mh)
{
  fOrientation=orientation;
  dw=w;
  rdh=mh;
  if (fCleaner==1)
  {
  //font is set, so program can get its height to prepare background buffer
    QFontMetrics *metrics = new QFontMetrics(f);
    h = metrics->height();
  
    bgBuffer = QPixmap(dw, h);
    maskBuffer = QPixmap(dw, h, true);
    bgBuffer = QPixmap::grabWindow(qt_xrootwin(), 0, 0, dw, h);
    lastX=0;
    lastY=0;
  }
  
}

//Enables/disables dynamic background
void xosd::setClear(int nClean)
{
  fCleaner = nClean;
}

void xosd::setShadowOffset(int off)
{
  shadowOffset=off;
}

void xosd::setText(const QString& t)
{
   //setMask(QRegion(0,0,0,0));
   // NOTICE: it appears that with WX11BypassWM, resize() doesn't flick the widget anymore :)
   // so, setMask() is no more needed.
   // -- Matias
  resize(0,0);
  text = t;
  QFontMetrics *metrics = new QFontMetrics(f);
  w = metrics->width(text);
  w = w + shadowOffset + 5; // 5 pixels more
  h = metrics->height();
  yOffset = metrics->height()-metrics->descent();
  update();
}

void xosd::setFont(const QString& font)
{
  f.setFamily(font);
}

void xosd::setItalic()
{
  f.setItalic(TRUE);
}

void xosd::setBold()
{
  f.setBold(TRUE);
}

void xosd::setSize(int size)
{
  f.setPointSize(size);
  fontSize = size;
}

/*int xosd::w()
{
  QFontMetrics *metrics = new QFontMetrics(f);
  return (metrics->width(text) + shadowOffset + 5);

}

int xosd::h()
{
  QFontMetrics *metrics = new QFontMetrics(f);
  return metrics->height();
}*/

void xosd::paintEvent(QPaintEvent *)
{
  int i, j;

  resize(w,h);
	
  QPixmap pm(size());
  QBitmap bm(size());
  QPainter p;
  
  //Drawing text
  p.begin(&pm, this);
  if (fCleaner==1)
  {
    //Dynamic background - look cleaner
    bitBlt(&pm, 0, 0, &bgBuffer, lastX, 0, w, h);
  }
  else
  {
    //One colour background - work faster
    p.fillRect (rect(), QColor(shadowColor));
  }
   
  p.setPen( QColor(shadowColor) );
  p.setFont(f);
  p.drawText(shadowOffset, yOffset + shadowOffset, text, AlignCenter);	// draw shadow text
  
   // now, draw normal text
  p.setPen( QColor(fontColor) );
   
  p.drawText(1, yOffset, text, AlignCenter);	// draw front text
	
  p.end();
  
  //Drawing mask
  p.begin(&bm, this);
   // now we must draw the text with black color for making the mask
  p.setPen( Qt::white );
  p.fillRect (rect(), Qt::black);
  p.setFont(f);
  for (i=-fCleaner;i<=fCleaner;i++)
  {
    for (j=-fCleaner;j<=fCleaner;j++)
    {
      p.drawText(shadowOffset+i, yOffset + shadowOffset+j, text, AlignCenter);	// shadow
      p.drawText(1+i, yOffset+j, text, AlignCenter);	// front
    }
  }
  p.end();
  bitBlt(this, 0, 0, &pm);	// update the widget
  if (fCleaner==1) bitBlt(&maskBuffer, 0, 0, &bm);
  
  info->setState(NETWinInfo::SkipTaskbar | NETWinInfo::SkipPager, NETWinInfo::SkipTaskbar | NETWinInfo::SkipPager);
  setMask(bm);
}

void xosd::move2(int x, int y)
{

  //bit block transfer don't work propertly with too less cursor position changes
  if (abs(lastY-y)>2 || abs(lastX-x)>2)
  {
    if (fCleaner==1) 
    {
      QPixmap tmpBuffer;
      //window is not hidden
      if (y!=rdh)
      {
        tmpBuffer = QPixmap::grabWindow(qt_xrootwin(), 0, y, dw, h);
        if (lastY!=rdh)
        {
        //fill background covered with the text with cached version
          if (fOrientation==0 ) //horizontal
          {
            bitBlt(&tmpBuffer, lastX, 0, &maskBuffer, 0, 0, w, h, Qt::AndROP);//copying part, which hides only the text
            bitBlt(&bgBuffer, lastX, 0, &maskBuffer, 0, 0, w, h, Qt::NotAndROP);//so the background won't be covered by
            bitBlt(&tmpBuffer, lastX, 0, &bgBuffer, lastX, 0, w, h, Qt::OrROP);//currently visible text.
          }
          if (fOrientation==1) //vertical
          {
            if (y<lastY && (y+h)>lastY)
            {
              bitBlt(&tmpBuffer, lastX, lastY-y, &maskBuffer, 0, 0, w, h, Qt::AndROP);
              bitBlt(&bgBuffer, lastX, 0, &maskBuffer, 0, 0, w, h, Qt::NotAndROP);
              bitBlt(&tmpBuffer, lastX, lastY-y, &bgBuffer, lastX, 0, w, h, Qt::OrROP);
            }
            if (y>lastY && (y-lastY)<h)
            {
              bitBlt(&tmpBuffer, lastX, 0, &maskBuffer, 0, 0, w, h, Qt::AndROP);
              bitBlt(&bgBuffer, lastX, y-lastY, &maskBuffer, 0, 0, w, h, Qt::NotAndROP);
              bitBlt(&tmpBuffer, lastX, 0, &bgBuffer, lastX, y-lastY, w, h, Qt::OrROP);
            }
          }
        }
    
      }
      bitBlt(&bgBuffer,0,0,&tmpBuffer);
      lastX=x;
      paintEvent(NULL);
    }
    
    move(x,y);
    lastX=x;
    lastY=y;
  }
}