trunk 20080912
[gitenigma.git] / lib / gui / emessage.cpp
blobe0e5e1741cb53f3b49114a994fb1b17d39070950
1 #include <lib/gui/emessage.h>
3 #include <lib/gui/elabel.h>
4 #include <lib/gui/ebutton.h>
5 #include <lib/gui/eskin.h>
6 #include <lib/gdi/font.h>
7 #include <lib/base/i18n.h>
9 eMessageBox::eMessageBox(eString message, eString caption, int flags, int def, int timeout )
10 :eWindow(0), timer(0), sectimer(0), icon(0), def(0), timeout(timeout)
12 if ( timeout )
14 timer = new eTimer(eApp);
15 switch(def)
17 case btYes:
18 CONNECT( timer->timeout, eMessageBox::pressedYes );
19 break;
20 case btNo:
21 CONNECT( timer->timeout, eMessageBox::pressedNo );
22 break;
23 case btOK:
24 CONNECT( timer->timeout, eMessageBox::pressedOK );
25 break;
26 default:
27 case btCancel:
28 CONNECT( timer->timeout, eMessageBox::pressedCancel );
29 break;
32 setText(caption);
33 int fontsize=eSkin::getActive()->queryValue("fontsize", 20);
34 int posx = eSkin::getActive()->queryValue("eMessageBox.pos.x", 100);
35 int posy = eSkin::getActive()->queryValue("eMessageBox.pos.y", 70);
36 move(ePoint(posx, posy));
37 resize(eSize(450, 430));
39 if ( flags > 15 ) // we have to draw an icon
41 gPixmap *pm=0;
42 switch ( flags & ~15 )
44 case iconInfo:
45 pm = eSkin::getActive()->queryImage( "icon_info" );
46 break;
47 case iconQuestion:
48 pm = eSkin::getActive()->queryImage( "icon_question" );
49 break;
50 case iconWarning:
51 pm = eSkin::getActive()->queryImage( "icon_warning" );
52 break;
53 case iconError:
54 pm = eSkin::getActive()->queryImage( "icon_error" );
55 break;
57 if (pm)
59 icon = new eLabel(this);
60 icon->setPixmap( pm );
61 icon->pixmap_position=ePoint(0,0);
62 icon->resize( eSize(pm->x, pm->y) );
63 icon->setBlitFlags( BF_ALPHATEST );
67 text=new eLabel(this);
68 text->setText(message);
69 text->resize( eSize( clientrect.width(), clientrect.height() ));
70 text->setFlags( RS_WRAP|eLabel::flagVCenter );
71 eSize txtSize=text->getExtend();
72 txtSize+=eSize(15,10); // the given Size of the Text is okay... but the renderer sucks...
73 text->resize(txtSize);
75 // here the two labels ( icon, text) has the correct size.. now we calc the border
77 eSize ext;
79 if ( icon )
81 if ( icon->getSize().height() > text->getSize().height() )
83 eDebug("icon is higher");
84 eSize s = icon->getSize();
85 icon->move( ePoint( 20, 20 ) );
86 text->move( ePoint( 20 + s.width() + 20, icon->getPosition().y() + s.height() / 2 - txtSize.height() / 2 ) );
87 ext.setHeight( icon->getPosition().y() + icon->getSize().height() + 20 );
89 else
91 eDebug("text is higher");
92 text->move( ePoint( 20 + icon->getSize().width() + 20 , 20 ) );
93 icon->move( ePoint( 20, text->getPosition().y() + text->getSize().height() / 2 - icon->getSize().height() / 2 ) );
94 ext.setHeight( text->getPosition().y() + text->getSize().height() + 20 );
96 ext.setWidth( text->getPosition().x() + text->getSize().width() + 20 );
98 else
100 text->move( ePoint(20, 20) );
101 ext.setWidth( text->getPosition().x() + text->getSize().width()+20 );
102 ext.setHeight( text->getPosition().y() + text->getSize().height() + 20 );
105 if (ext.width()<150)
106 ext.setWidth(150);
108 int xpos=20;
110 if ( flags & 15)
112 for (int i=btOK; i<btMax; i<<=1)
113 if (flags & i)
115 eButton *b=new eButton(this);
116 b->resize(eSize(size.width(), fontsize+4));
117 const char *t="", *shortcut="";
118 switch (i)
120 case btOK: t=_("OK"); shortcut="green"; CONNECT(b->selected, eMessageBox::pressedOK); break;
121 case btCancel: t=_("Cancel"); shortcut="red"; CONNECT(b->selected, eMessageBox::pressedCancel); break;
122 case btYes: t=_("Yes"); shortcut="green"; CONNECT(b->selected, eMessageBox::pressedYes); break;
123 case btNo: t=_("No"); shortcut="red"; CONNECT(b->selected, eMessageBox::pressedNo); break;
125 b->setProperty("shortcut", shortcut);
126 b->setText(t);
127 eSize bSize=b->getExtend();
128 bSize.setWidth( bSize.width() * 2 );
129 bSize.setHeight( fontsize + 4 + 10 );
130 b->resize(bSize);
131 b->move( ePoint( xpos, ext.height() ) );
133 b->loadDeco();
135 if (def == i)
136 this->def = b;
138 xpos += bSize.width()+20;
141 if ( timeout )
143 lTimeout = new eLabel(this);
144 lTimeout->setText( eString().sprintf("%d", timeout) );
145 lTimeout->resize( eSize(50, fontsize+4+10) );
146 lTimeout->move( ePoint( xpos+10, ext.height() ) );
147 lTimeout->setFlags(eLabel::flagVCenter);
148 xpos += lTimeout->getSize().width()+10;
151 if ( xpos+20 > ext.width() )
152 cresize( eSize( xpos+20, ext.height() + fontsize + 4 + 10 + 20 ) );
153 else
154 cresize( eSize( ext.width(), ext.height() + fontsize + 4 + 10 + 20 ) );
156 else
157 cresize( ext );
158 zOrderRaise();
161 int eMessageBox::eventHandler( const eWidgetEvent &e )
163 switch (e.type)
165 case eWidgetEvent::evtAction:
166 if ( timer && timer->isActive() )
168 timer->stop();
169 delete sectimer;
170 sectimer=0;
171 updateTimeoutLabel();
173 break;
174 case eWidgetEvent::execBegin:
175 if ( def )
176 setFocus(def);
177 if ( timeout )
179 timer->start(timeout*1000, true);
180 delete sectimer;
181 sectimer = new eTimer(eApp);
182 CONNECT( sectimer->timeout, eMessageBox::updateTimeoutLabel );
183 sectimer->start(1000);
185 default:
186 break;
188 return eWindow::eventHandler( e );
191 void eMessageBox::pressedOK()
193 if ( in_loop )
194 close(btOK);
195 else
196 hide();
199 void eMessageBox::pressedCancel()
201 if ( in_loop )
202 close(btCancel);
203 else
204 hide();
207 void eMessageBox::pressedYes()
209 if ( in_loop )
210 close(btYes);
211 else
212 hide();
215 void eMessageBox::pressedNo()
217 if ( in_loop )
218 close(btNo);
219 else
220 hide();
223 void eMessageBox::updateTimeoutLabel()
225 if ( timeout )
227 --timeout;
228 lTimeout->setText(eString().sprintf("%d", timeout));
230 else
231 lTimeout->hide();
234 eMessageBox::~eMessageBox()
236 delete timer;
237 delete sectimer;