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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/*
* newsscroller.cpp
*
* Copyright (c) 2000, 2001 Frerich Raabe <[email protected]>
* Copyright (c) 2001 Malte Starostik <[email protected]>
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
* accompanying file 'COPYING'.
*/
#include <dcopclient.h>
#include <tqpainter.h>
#include <tqregexp.h>
#include <tqtimer.h>
#include <kcursor.h>
#include <kstandarddirs.h>
#include <kurldrag.h>
#include <tdemessagebox.h>
#include "configaccess.h"
#include "newsscroller.h"
#include "newsengine.h"
#include <tdeapplication.h>
class Headline
{
public:
Headline(NewsScroller *scroller, const Article::Ptr &article)
: m_scroller(scroller),
m_article(article),
m_normal(0),
m_highlighted(0)
{
};
virtual ~Headline()
{
reset();
}
Article::Ptr article() const { return m_article; }
int width() { return pixmap()->width(); }
int height() { return pixmap()->height(); }
TQPixmap *pixmap(bool highlighted = false, bool underlineHighlighted = true)
{
TQPixmap *result = highlighted ? m_highlighted : m_normal;
if (!result) {
const TQFontMetrics &metrics = m_scroller->fontMetrics();
int w, h;
if (m_scroller->m_cfg->showIcons()) {
w = m_article->newsSource()->icon().width() + 4 + metrics.width(m_article->headline());
h = TQMAX(metrics.height(), m_article->newsSource()->icon().height());
} else {
w = metrics.width(m_article->headline());
h = metrics.height();
}
if (ConfigAccess::rotated(static_cast<ConfigAccess::Direction>(m_scroller->m_cfg->scrollingDirection())))
result = new TQPixmap(h, w);
else
result = new TQPixmap(w, h);
result->fill(m_scroller->m_cfg->backgroundColor());
TQPainter p(result);
TQFont f = m_scroller->font();
if (highlighted)
f.setUnderline(underlineHighlighted);
p.setFont(f);
p.setPen(highlighted ? m_scroller->m_cfg->highlightedColor() : m_scroller->m_cfg->foregroundColor());
if (ConfigAccess::rotated(static_cast<ConfigAccess::Direction>(m_scroller->m_cfg->scrollingDirection()))) {
if (m_scroller->m_cfg->scrollingDirection() == ConfigAccess::UpRotated) {
// Note that rotation also
// changes the coordinate space
//
p.rotate(90.0);
if (m_scroller->m_cfg->showIcons()) {
p.drawPixmap(0, -m_article->newsSource()->icon().height(), m_article->newsSource()->icon());
p.drawText(m_article->newsSource()->icon().width() + 4, -metrics.descent(), m_article->headline());
} else
p.drawText(0, -metrics.descent(), m_article->headline());
} else {
p.rotate(-90.0);
if (m_scroller->m_cfg->showIcons()) {
p.drawPixmap(-w, h - m_article->newsSource()->icon().height(), m_article->newsSource()->icon());
p.drawText(-w + m_article->newsSource()->icon().width() + 4, h - metrics.descent(), m_article->headline());
} else
p.drawText(-w, h - metrics.descent(), m_article->headline());
}
} else {
if (m_scroller->m_cfg->showIcons()) {
p.drawPixmap(0,
(result->height() - m_article->newsSource()->icon().height()) / 2,
m_article->newsSource()->icon());
p.drawText(m_article->newsSource()->icon().width() + 4, result->height() - metrics.descent(), m_article->headline());
} else
p.drawText(0, result->height() - metrics.descent(), m_article->headline());
}
if (highlighted)
m_highlighted = result;
else
m_normal = result;
}
return result;
}
void reset()
{
delete m_normal;
m_normal = 0;
delete m_highlighted;
m_highlighted = 0;
}
private:
NewsScroller *m_scroller;
Article::Ptr m_article;
TQPixmap *m_normal;
TQPixmap *m_highlighted;
};
NewsScroller::NewsScroller(TQWidget *parent, ConfigAccess *cfg, const char *name)
: TQFrame(parent, name, WNoAutoErase),
m_cfg(cfg),
m_scrollTimer(new TQTimer(this)),
m_activeHeadline(0),
m_mouseDrag(false),
m_totalStepping(0.0)
{
if (!kapp->dcopClient()->isAttached())
kapp->dcopClient()->attach();
setFrameStyle(StyledPanel | Sunken);
m_headlines.setAutoDelete(true);
connect(m_scrollTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotTimeout()));
setAcceptDrops(true);
reset();
}
TQSize NewsScroller::sizeHint() const
{
return TQSize(fontMetrics().width(TQString::fromLatin1("X")) * 20, fontMetrics().height() * 2);
}
TQSizePolicy NewsScroller::sizePolicy() const
{
return TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
}
void NewsScroller::clear()
{
m_headlines.clear();
reset();
}
void NewsScroller::dragEnterEvent(TQDragEnterEvent* event)
{
event->accept(TQTextDrag::canDecode(event));
}
void NewsScroller::dropEvent(TQDropEvent* event)
{
TQString newSourceUrl;
if ( TQTextDrag::decode(event, newSourceUrl) ) {
// <HACK>
// This is just for http://www.webreference.com/services/news/
newSourceUrl = newSourceUrl.replace(TQRegExp(
TQString::fromLatin1("^view-source:http%3A//")),
TQString::fromLatin1("http://"));
// </HACK>
newSourceUrl = newSourceUrl.stripWhiteSpace();
if (!isHeadline(newSourceUrl) && KMessageBox::questionYesNo(this, i18n("<p>Do you really want to add '%1' to"
" the list of news sources?</p>")
.arg(newSourceUrl), TQString(), i18n("Add"), KStdGuiItem::cancel()) == KMessageBox::Yes) {
TDEConfig cfg(TQString::fromLatin1("knewsticker_panelappletrc"), false, false);
ConfigAccess configFrontend(&cfg);
TQStringList newsSources = configFrontend.newsSources();
TQString name = i18n("Unknown");
if (newsSources.contains(name))
for (unsigned int i = 0; ; i++)
if (!newsSources.contains(i18n("Unknown %1").arg(i))) {
name = i18n("Unknown %1").arg(i);
break;
}
newsSources += name;
configFrontend.setNewsSource(NewsSourceBase::Data(name, newSourceUrl));
configFrontend.setNewsSources(newsSources);
TQByteArray data;
kapp->dcopClient()->send("knewsticker", "KNewsTicker", "reparseConfig()", data);
}
}
}
bool NewsScroller::isHeadline(const TQString &location) const
{
for (Headline *h = m_headlines.first(); h; h = m_headlines.next())
if (h->article()->address() == location)
return true;
return false;
}
void NewsScroller::addHeadline(Article::Ptr article)
{
for (unsigned int i = 0; i < m_cfg->filters().count(); i++)
if (m_cfg->filter(i).matches(article))
return;
m_headlines.append(new Headline(this, article));
}
void NewsScroller::scroll(int distance, bool interpret_directions)
{
unsigned int t_dir;
if ( interpret_directions ) t_dir = m_cfg->scrollingDirection();
else t_dir = m_cfg->horizontalScrolling() ? ConfigAccess::Left : ConfigAccess::Up;
switch (t_dir) {
case ConfigAccess::Left:
m_offset -= distance;
if (m_offset <= - scrollWidth())
m_offset = m_offset + scrollWidth() - m_separator.width();
break;
case ConfigAccess::Right:
m_offset += distance;
if (m_offset >= contentsRect().width())
m_offset = m_offset + m_separator.width() - scrollWidth();
break;
case ConfigAccess::Up:
case ConfigAccess::UpRotated:
m_offset -= distance;
if (m_offset <= - scrollHeight())
m_offset = m_offset + scrollHeight() - m_separator.height();
break;
case ConfigAccess::Down:
case ConfigAccess::DownRotated:
m_offset += distance;
if (m_offset >= contentsRect().height())
m_offset = m_offset + m_separator.height() - scrollHeight();
}
TQPoint pt = mapFromGlobal(TQCursor::pos());
if (contentsRect().contains(pt))
updateActive(pt);
update();
}
void NewsScroller::enterEvent(TQEvent *)
{
if (m_cfg->slowedScrolling() && m_cfg->scrollingSpeed() > 1)
m_scrollTimer->changeInterval(speedAsInterval(m_cfg->scrollingSpeed() / 2));
}
void NewsScroller::mousePressEvent(TQMouseEvent *e)
{
if (e->button() == TQt::LeftButton || e->button() == TQt::MidButton) {
m_dragPos = e->pos();
if (m_activeHeadline)
m_tempHeadline = m_activeHeadline->article()->headline();
}
}
void NewsScroller::mouseReleaseEvent(TQMouseEvent *e)
{
if ((e->button() == TQt::LeftButton || e->button() == TQt::MidButton)
&& m_activeHeadline && m_activeHeadline->article()->headline() == m_tempHeadline
&& !m_mouseDrag) {
m_activeHeadline->article()->open();
m_tempHeadline = TQString();
}
if (e->button() == TQt::RightButton)
emit(contextMenu());
if (m_mouseDrag) {
m_mouseDrag = false;
if (m_cfg->scrollingSpeed())
m_scrollTimer->start(speedAsInterval(m_cfg->scrollingSpeed()));
}
}
void NewsScroller::mouseMoveEvent(TQMouseEvent *e)
{
// Are we in a drag phase?
if (!m_mouseDrag) {
// If not, check whether we need to start a drag.
int dragDistance = 0;
if (m_cfg->horizontalScrolling())
dragDistance = TQABS(e->x() - m_dragPos.x());
else
dragDistance = TQABS(e->y() - m_dragPos.y());
m_mouseDrag = (e->state() & TQt::LeftButton != 0) &&
dragDistance >= TDEGlobal::config()->readNumEntry("StartDragDist", TDEApplication::startDragDistance());
if (m_mouseDrag) // Stop the scroller if we just started a drag.
m_scrollTimer->stop();
} else {
// If yes, move the scroller accordingly.
bool createDrag;
if (m_cfg->horizontalScrolling()) {
scroll(m_dragPos.x() - e->x(), false);
m_dragPos = e->pos();
createDrag = e->y() < 0 || e->y() > height();
} else {
scroll(m_dragPos.y() - e->y(), false);
m_dragPos = e->pos();
createDrag = e->x() < 0 || e->x() > width();
}
m_dragPos = e->pos();
if (createDrag && m_activeHeadline) {
KURL::List url;
url.append(m_activeHeadline->article()->address());
TQDragObject *drag = new KURLDrag(url, this);
drag->setPixmap(m_activeHeadline->article()->newsSource()->icon());
drag->drag();
m_mouseDrag = false;
if (m_cfg->scrollingSpeed())
m_scrollTimer->start(speedAsInterval(m_cfg->scrollingSpeed()));
}
}
if (updateActive(e->pos()))
update();
}
void NewsScroller::wheelEvent(TQWheelEvent *e)
{
// ### This 11 - m_cfg->mouseWheelSpeed() could be eliminated by swapping
// the labels of the TQSlider. :]
int distance = tqRound(TQABS(e->delta()) / (11 - m_cfg->mouseWheelSpeed()));
int direction = e->delta() > 0 ? -1 : 1;
for (int i = 0; i < distance; i++)
scroll(direction);
TQFrame::wheelEvent(e);
}
void NewsScroller::leaveEvent(TQEvent *)
{
if (m_cfg->slowedScrolling() && m_cfg->scrollingSpeed() > 1)
m_scrollTimer->changeInterval(speedAsInterval(m_cfg->scrollingSpeed()));
if (m_activeHeadline) {
m_activeHeadline = 0;
update();
}
}
void NewsScroller::drawContents(TQPainter *p)
{
if (!scrollWidth() || // No news and no "No News Available": uninitialized
m_headlines.isEmpty()) // Happens when we're currently fetching new news
return;
TQPixmap buffer(contentsRect().width(), contentsRect().height());
buffer.fill(m_cfg->backgroundColor());
int pos = m_offset;
// Paste in all the separator bitmaps (" +++ ")
if (horizontal()) {
while (pos > 0)
pos -= scrollWidth() - (m_headlines.isEmpty() ? m_separator.width() : 0);
do {
bitBlt(&buffer, pos, (contentsRect().height() - m_separator.height()) / 2, &m_separator);
pos += m_separator.width();
} while (m_headlines.isEmpty() && pos < contentsRect().width());
} else {
while (pos > 0)
pos -= scrollHeight() - (m_headlines.isEmpty() ? 0 : m_separator.height());
do {
bitBlt(&buffer, (contentsRect().width() - m_separator.width()) / 2, pos, &m_separator);
pos += m_separator.height();
} while (m_headlines.isEmpty() && pos < contentsRect().height());
}
// Now do the headlines themselves
do {
TQPtrListIterator<Headline> it(m_headlines);
for(; *it; ++it) {
if (horizontal()) {
if (pos + (*it)->width() >= 0)
bitBlt(&buffer, pos, (contentsRect().height() - (*it)->height()) / 2, (*it)->pixmap(*it == m_activeHeadline, m_cfg->underlineHighlighted()));
pos += (*it)->width();
if (pos + m_separator.width() >= 0)
bitBlt(&buffer, pos, (contentsRect().height() - m_separator.height()) / 2, &m_separator);
pos += m_separator.width();
if (pos >= contentsRect().width())
break;
} else {
if (pos + (*it)->height() >= 0)
bitBlt(&buffer, (contentsRect().width() - (*it)->width()) / 2, pos, (*it)->pixmap(*it == m_activeHeadline, m_cfg->underlineHighlighted()));
pos += (*it)->height();
if (pos + m_separator.height() >= 0)
bitBlt(&buffer, (contentsRect().width() - m_separator.width()) / 2, pos, &m_separator);
pos += m_separator.height();
if (pos > contentsRect().height())
break;
}
}
/*
* Break out if we reached the bottom of the window before the end of the
* list of headlines.
*/
if (*it)
break;
}
while ((m_cfg->horizontalScrolling() && pos < contentsRect().width()) || pos < contentsRect().height());
p->drawPixmap(0, 0, buffer);
}
void NewsScroller::reset(bool bSeparatorOnly)
{
setFont(m_cfg->font());
m_scrollTimer->stop();
if (m_cfg->scrollingSpeed())
m_scrollTimer->start(speedAsInterval(m_cfg->scrollingSpeed()));
TQString sep = m_headlines.isEmpty() ? i18n(" +++ No News Available +++") : TQString::fromLatin1(" +++ ");
int w = fontMetrics().width(sep);
int h = fontMetrics().height();
if (rotated())
m_separator.resize(h, w);
else
m_separator.resize(w, h);
m_separator.fill(m_cfg->backgroundColor());
TQPainter p(&m_separator);
p.setFont(font());
p.setPen(m_cfg->foregroundColor());
if(rotated()) {
if (m_cfg->scrollingDirection() == ConfigAccess::UpRotated) {
p.rotate(90.0);
p.drawText(0, -fontMetrics().descent(),sep);
} else {
p.rotate(-90.0);
p.drawText(-w, h-fontMetrics().descent(),sep);
}
} else
p.drawText(0, m_separator.height() - fontMetrics().descent(), sep);
p.end();
if (!bSeparatorOnly)
for (TQPtrListIterator<Headline> it(m_headlines); *it; ++it)
(*it)->reset();
switch (m_cfg->scrollingDirection()) {
case ConfigAccess::Left:
m_offset = contentsRect().width();
break;
case ConfigAccess::Right:
m_offset = - scrollWidth();
break;
case ConfigAccess::Up:
case ConfigAccess::UpRotated:
m_offset = contentsRect().height();
break;
case ConfigAccess::Down:
case ConfigAccess::DownRotated:
m_offset = - scrollHeight();
}
update();
}
int NewsScroller::scrollWidth() const
{
int result = (m_headlines.count() + 1) * m_separator.width();
for (TQPtrListIterator<Headline> it(m_headlines); *it; ++it)
result += (*it)->width();
return result;
}
int NewsScroller::scrollHeight() const
{
int result = (m_headlines.count() + 1) * m_separator.height();
for (TQPtrListIterator<Headline> it(m_headlines); *it; ++it)
result += (*it)->height();
return result;
}
bool NewsScroller::updateActive(const TQPoint &pt)
{
int pos = m_offset;
Headline *headline = 0;
if (!m_headlines.isEmpty()) {
while (pos > 0)
if (horizontal())
pos -= scrollWidth() - m_separator.width();
else
pos -= scrollHeight() - m_separator.height();
do {
TQPtrListIterator<Headline> it(m_headlines);
for (; (headline = *it); ++it) {
TQRect rect;
if (horizontal()) {
pos += m_separator.width();
rect.moveTopLeft(TQPoint(pos, (contentsRect().height() - (*it)->height()) / 2));
pos += (*it)->width();
} else {
pos += m_separator.height();
rect.moveTopLeft(TQPoint((contentsRect().width() - (*it)->width()) / 2, pos));
pos += (*it)->height();
}
rect.setSize(TQSize((*it)->width(), (*it)->height()));
if (m_mouseDrag)
if (horizontal()) {
rect.setTop(0);
rect.setHeight(height());
} else {
rect.setLeft(0);
rect.setWidth(width());
}
if (rect.contains(pt))
break;
}
if (*it)
break;
}
while ((m_cfg->horizontalScrolling() && pos < contentsRect().width()) || pos < contentsRect().height());
}
if (m_activeHeadline == headline)
return false;
if ((m_activeHeadline = headline))
setCursor(KCursor::handCursor());
else
unsetCursor();
return true;
}
void NewsScroller::slotTimeout()
{
m_totalStepping += m_stepping;
if ( m_totalStepping >= 1.0 ) {
const int distance = static_cast<int>( m_totalStepping );
m_totalStepping -= distance;
scroll( distance );
}
}
int NewsScroller::speedAsInterval( int speed )
{
Q_ASSERT( speed > 0 );
static const int MaxScreenUpdates = 25;
if ( speed <= MaxScreenUpdates ) {
m_stepping = 1.0;
return 1000 / speed;
}
m_stepping = speed / MaxScreenUpdates;
return 1000 / MaxScreenUpdates;
}
#include "newsscroller.moc"
|