summaryrefslogtreecommitdiffstats
path: root/ksokoban/ModalLabel.cpp
blob: 47d4c49b745651c6e299265706f0bfcb2f857a7d (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
/*
 *  ksokoban - a Sokoban game for KDE
 *  Copyright (C) 1998  Anders Widell  <awl@hem.passagen.se>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ModalLabel.h"

#include <tqlabel.h>
#include <tqfont.h>
#include <kapplication.h>
#include <kglobalsettings.h>
#include <tqwidgetlist.h>
#include <tqstring.h>

#include "ModalLabel.moc"

ModalLabel::ModalLabel(const TQString &text, TQWidget *tqparent,
		       const char *name, WFlags f)
  : TQLabel(text, tqparent, name, f) {
  TQFont font(KGlobalSettings::generalFont().family(), 24, TQFont::Bold);
  TQFontMetrics fontMet(font);

  TQString currentLine;
  TQRect bounds;
  int lineLen, width=0, height=0;

  for (int linePos=0; linePos < (int) text.length(); linePos += lineLen+1) {

    lineLen = text.find('\n', linePos);
    if (lineLen < 0) lineLen = text.length() - linePos;
    else lineLen -= linePos;

    currentLine = text.mid(linePos, lineLen);
    bounds = fontMet.boundingRect(currentLine);

    if (bounds.width() > width) width = bounds.width();
    height += bounds.height();
  }

  width += 32;
  height += 32;

  if (width < 300) width = 300;
  if (height < 75) height = 75;

  tqsetAlignment (AlignCenter);
  setFrameStyle (TQFrame::Panel | TQFrame::Raised);
  setLineWidth (4);
  setFont (font);
  move (tqparent->width ()/2 - width/2, tqparent->height ()/2 - height/2);
  resize (width, height);
  show ();

  TQWidgetList  *list = TQApplication::tqallWidgets();
  TQWidgetListIt it( *list );
  while (it.current()) {
    it.current()->installEventFilter (this);
    ++it;
  }
  delete list;

  completed_ = false;
  startTimer (1000);
}

void
ModalLabel::timerEvent (TQTimerEvent *) {
  completed_ = true;
}

bool
ModalLabel::eventFilter (TQObject *, TQEvent *e) {
  switch (e->type()) {
    case TQEvent::MouseButtonPress:
    case TQEvent::MouseButtonRelease:
    case TQEvent::MouseButtonDblClick:
    case TQEvent::MouseMove:
    case TQEvent::KeyPress:
    case TQEvent::KeyRelease:
    case TQEvent::Accel:
      //case TQEvent::DragEnter:
    case TQEvent::DragMove:
    case TQEvent::DragLeave:
    case TQEvent::Drop:
      //case TQEvent::DragResponse:

      //kdDebug << "Ate event" << endl;
    return true;
    break;
  default:
    return false;
  }
}

void
ModalLabel::message (const TQString &text, TQWidget *tqparent) {
  KApplication *app = KApplication::kApplication ();
  ModalLabel cl (text, tqparent);

  while (!cl.completed_) app->processOneEvent ();
}