~ubuntu-branches/ubuntu/hoary/kvirc/hoary

« back to all changes in this revision

Viewing changes to src/kvilib/kvi_opaquelabel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2004-12-14 15:32:19 UTC
  • mfrom: (0.2.1 upstream) (1.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041214153219-fdink3gyp2s20b6g
Tags: 2:2.1.3.1-2
* Change Recommends on xmms to a Suggests.
* Rebuild against KDE 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//   File : kvi_opaquelabel.cpp
3
 
//   Last major modification : Thu Jan 23 2000 02:15:12 by Szymon Stefanek
4
 
//
5
 
//   This file is part of the KVirc irc client distribution
6
 
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
7
 
//
8
 
//   This program is FREE software. You can redistribute it and/or
9
 
//   modify it under the terms of the GNU General Public License
10
 
//   as published by the Free Software Foundation; either version 2
11
 
//   of the License, or (at your opinion) any later version.
12
 
//
13
 
//   This program is distributed in the HOPE that it will be USEFUL,
14
 
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 
//   See the GNU General Public License for more details.
17
 
//
18
 
//   You should have received a copy of the GNU General Public License
19
 
//   along with this program. If not, write to the Free Software Foundation,
20
 
//   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
 
//
22
 
 
23
 
#include "kvi_opaquelabel.h"
24
 
#include "kvi_xutils.h"
25
 
#include "kvi_defines.h"
26
 
#include "kvi_malloc.h"
27
 
 
28
 
#include <qfontmetrics.h>
29
 
#include <qpainter.h>
30
 
 
31
 
#define KVI_CHAN_LABEL_BORDER 2
32
 
 
33
 
#ifdef COMPILE_USE_AA_FONTS
34
 
        XftFont        * g_pXftFont;
35
 
        XftDraw        * g_pXftDraw;
36
 
        extern int qt_use_xft (void); // qpainter_x11.cpp
37
 
        extern void *qt_ft_font (const QFont *f); // qfont_x11.cpp
38
 
        extern XftDraw * qt_lookup_ft_draw (Drawable draw, bool paintEventClipOn, QRegion *crgn);
39
 
#endif
40
 
 
41
 
//
42
 
//   A non themeable label
43
 
//
44
 
 
45
 
KviOpaqueLabel::KviOpaqueLabel(QWidget *parent,const char *name)
46
 
:QLabel(parent,name)
47
 
{
48
 
}
49
 
 
50
 
KviOpaqueLabel::KviOpaqueLabel(const QString &text,QWidget *parent,const char *name)
51
 
:QLabel(text,parent,name)
52
 
{
53
 
}
54
 
 
55
 
KviOpaqueLabel::~KviOpaqueLabel()
56
 
{
57
 
}
58
 
 
59
 
// Kill these methods
60
 
 
61
 
 
62
 
void KviOpaqueLabel::setMask(const QBitmap &)
63
 
{
64
 
}
65
 
 
66
 
void KviOpaqueLabel::setMask(const QRegion &)
67
 
{
68
 
}
69
 
 
70
 
void KviOpaqueLabel::setAutoMask(bool)
71
 
{
72
 
}
73
 
 
74
 
//
75
 
//   A non themeable (from outside) label
76
 
//   Used for the channel labels
77
 
//
78
 
 
79
 
KviChanLabel::KviChanLabel(const char * text,QWidget *parent,const char *name)
80
 
:QWidget(parent,name)
81
 
{
82
 
        m_bCanEdit = false;
83
 
        m_szText = text ? text : "";
84
 
        m_iFirstVisibleChar = 0;
85
 
        m_textColor = colorGroup().text();
86
 
        m_backColor = colorGroup().button();
87
 
        m_pPixPointer = 0;
88
 
        setBackgroundMode(NoBackground);
89
 
        
90
 
        m_bLeft = new QToolButton(LeftArrow,this);
91
 
        m_bRight = new QToolButton(RightArrow,this);
92
 
 
93
 
        m_bLeft->setFocusPolicy(QWidget::NoFocus);
94
 
        m_bLeft->setAutoRepeat(true);
95
 
 
96
 
        m_bRight->setFocusPolicy(QWidget::NoFocus);
97
 
        m_bRight->setAutoRepeat(true);
98
 
 
99
 
        m_bLeft->hide();
100
 
        m_bRight->hide();
101
 
 
102
 
        connect(m_bLeft,SIGNAL(pressed()),this,SLOT(scrollLeft()));
103
 
        connect(m_bRight,SIGNAL(pressed()),this,SLOT(scrollRight()));
104
 
}
105
 
 
106
 
KviChanLabel::~KviChanLabel()
107
 
{
108
 
        delete m_bLeft,m_bRight;
109
 
}
110
 
 
111
 
const char * KviChanLabel::strippedText()
112
 
{
113
 
        KviStr text;
114
 
        char * p = m_szText.ptr();
115
 
        while(*p){
116
 
                switch(*p){
117
 
                        case KVI_TEXT_BOLD:
118
 
                        case KVI_TEXT_REVERSE:
119
 
                        case KVI_TEXT_RESET:
120
 
                        case KVI_TEXT_UNDERLINE:
121
 
                                p++;
122
 
                        break;
123
 
                        case KVI_TEXT_COLOR:
124
 
                                p++;
125
 
                                if((*p)&&(*p >= '0')&&(*p <= '9')){
126
 
                                        p++;
127
 
                                        if((*p)&&(*p >= '0')&&(*p <= '9'))
128
 
                                                p++;
129
 
                                        if((*p)&&(*p == ',')){
130
 
                                                p++;
131
 
                                                if((*p)&&(*p >= '0')&&(*p <= '9'))
132
 
                                                        p++;
133
 
                                                if((*p)&&(*p >= '0')&&(*p <= '9'))
134
 
                                                        p++;
135
 
                                        }
136
 
                                }
137
 
                        break;
138
 
                        default:
139
 
                                text.append(*p);
140
 
                                p++;
141
 
                        break;
142
 
                }
143
 
        }
144
 
        p = (char *)kvi_malloc(text.len()+1);
145
 
        qstrcpy(p, text.ptr());
146
 
        return p;
147
 
}
148
 
 
149
 
void KviChanLabel::setBackgroundPixmapPointer(QPixmap * pix)
150
 
{
151
 
        m_pPixPointer = pix;
152
 
        update();
153
 
}
154
 
 
155
 
void KviChanLabel::setTextColor(QColor &clr)
156
 
{
157
 
        m_textColor = clr;
158
 
        update();
159
 
}
160
 
 
161
 
void KviChanLabel::setBackColor(QColor &clr)
162
 
{
163
 
        m_backColor = clr;
164
 
        update();
165
 
}
166
 
 
167
 
void KviChanLabel::paintEvent(QPaintEvent *e)
168
 
{
169
 
        QPixmap * pix = new QPixmap(width(),height());
170
 
        QColorGroup g = colorGroup();
171
 
        Display * dpy = qt_xdisplay();
172
 
        GC the_gc = XCreateGC(dpy,pix->handle(),0,0);
173
 
 
174
 
        if(m_pPixPointer)
175
 
        {
176
 
                XSetTile(dpy,the_gc,m_pPixPointer->handle());
177
 
                XSetFillStyle(dpy,the_gc,FillTiled);
178
 
        } else {
179
 
                XSetForeground(dpy,the_gc,m_backColor.pixel());
180
 
                XSetBackground(dpy,the_gc,m_backColor.pixel());
181
 
                XSetFillStyle(dpy,the_gc,FillSolid);
182
 
        }
183
 
        XFillRectangle(dpy,pix->handle(),the_gc,0,0,width(),height());
184
 
 
185
 
        kvi_xDraw3dRect(dpy,the_gc,pix->handle(),colorGroup(),0,0,width() - 1,height() - 1,true);
186
 
 
187
 
        KviStr pT(strippedText());
188
 
        if(m_iFirstVisibleChar < 0)m_iFirstVisibleChar = 0;
189
 
        KviStr t(pT.right(pT.len()-m_iFirstVisibleChar));
190
 
        if(fontMetrics().width(pT.ptr()) < width()){
191
 
                m_bLeft->hide();
192
 
                m_bRight->hide();
193
 
        } else {
194
 
                QSize bs;
195
 
                bs.setHeight(height());
196
 
                bs.setWidth(height()*5/6); // 5/8 - reciprocal golden mean
197
 
                m_bLeft->resize(bs);
198
 
                m_bRight->resize(bs);
199
 
                int buttonWidth = m_bRight->width();
200
 
                int x = width() - buttonWidth;
201
 
                m_bLeft->move(x-buttonWidth,0);
202
 
                m_bLeft->setEnabled((m_iFirstVisibleChar != 0));
203
 
                m_bRight->move(x,0);
204
 
                m_bRight->setEnabled(fontMetrics().width(t.ptr()) >= width()-m_bLeft->width()-m_bRight->width());
205
 
                m_bLeft->show();
206
 
                m_bRight->show();
207
 
        }
208
 
 
209
 
        if(t.len())
210
 
        {
211
 
//              const char * t = m_szText.ptr();
212
 
                XSetForeground(dpy,the_gc,m_textColor.pixel());
213
 
#ifdef COMPILE_USE_AA_FONTS
214
 
                if(qt_use_xft())
215
 
                {
216
 
                        g_pXftFont = (XftFont *)qt_ft_font(&(font()));
217
 
                        g_pXftDraw = qt_lookup_ft_draw (pix->handle(),false,0);
218
 
                        if(!g_pXftDraw)
219
 
                        {
220
 
                                XSetFont(dpy,the_gc,font().handle());
221
 
                                g_pXftFont = 0;
222
 
                        }
223
 
                } else {
224
 
#endif
225
 
                        XSetFont(dpy,the_gc,font().handle());
226
 
#ifdef COMPILE_USE_AA_FONTS
227
 
                        g_pXftFont = 0;
228
 
                        g_pXftDraw = 0;
229
 
                }
230
 
                if(g_pXftFont){
231
 
                        XftColor color;
232
 
                        QColor * clr = &m_textColor;
233
 
                        color.color.red = clr->red() | clr->red() << 8;
234
 
                        color.color.green = clr->green() | clr->green() << 8;
235
 
                        color.color.blue = clr->blue() | clr->blue() << 8;
236
 
                        color.color.alpha = 0xffff;
237
 
                        color.pixel = clr->pixel();
238
 
                        XftDrawString8(g_pXftDraw,&color,g_pXftFont,KVI_CHAN_LABEL_BORDER,
239
 
                                (height() >> 1) + (fontMetrics().height() >> 1) - (fontMetrics().descent() + 1),(unsigned char *)t.ptr(),t.len());
240
 
                } else
241
 
#endif
242
 
                        XDrawString(dpy,pix->handle(),the_gc,KVI_CHAN_LABEL_BORDER,
243
 
                                (height() >> 1) + (fontMetrics().height() >> 1) - (fontMetrics().descent() + 1),t.ptr(),t.len());
244
 
        }
245
 
 
246
 
        XCopyArea(dpy,pix->handle(),this->handle(),the_gc,0,0,width(),height(),0,0);
247
 
 
248
 
        delete pix;
249
 
        XFreeGC(dpy,the_gc);
250
 
}
251
 
 
252
 
void KviChanLabel::setText(const char * text)
253
 
{
254
 
        m_szText = text ? text : "";
255
 
        m_iFirstVisibleChar = 0;
256
 
        update();
257
 
}
258
 
 
259
 
void KviChanLabel::scrollLeft()
260
 
{
261
 
        if(m_iFirstVisibleChar)m_iFirstVisibleChar--;
262
 
        update();
263
 
}
264
 
 
265
 
void KviChanLabel::scrollRight()
266
 
{
267
 
        KviStr pT(strippedText());
268
 
        KviStr t(pT.right(pT.len()-m_iFirstVisibleChar));
269
 
 
270
 
        if(fontMetrics().width(t.ptr()) >= width()-m_bLeft->width()-m_bRight->width())
271
 
                m_iFirstVisibleChar++;
272
 
        update();
273
 
}
274
 
 
275
 
void KviChanLabel::mousePressEvent(QMouseEvent *e)
276
 
{
277
 
        if(e->button() & RightButton)emit rightClicked();
278
 
        else QWidget::mousePressEvent(e);
279
 
}
280
 
 
281
 
void KviChanLabel::mouseDoubleClickEvent(QMouseEvent *)
282
 
{
283
 
        m_bCanEdit = true;
284
 
}
285
 
 
286
 
void KviChanLabel::mouseReleaseEvent(QMouseEvent *)
287
 
288
 
        if(m_bCanEdit)
289
 
        {
290
 
                emit doubleClicked();
291
 
                m_bCanEdit = false;
292
 
        }
293
 
}
294
 
 
295
 
QSize KviChanLabel::sizeHint()
296
 
{
297
 
        QFontMetrics fm = fontMetrics();
298
 
        return QSize(100,fm.height() + (KVI_CHAN_LABEL_BORDER << 1));
299
 
}
300
 
 
301
 
#include "m_kvi_opaquelabel.moc"