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
|
/*
This file is part of Advanced Strategic Command; http://www.asc-hq.de
Copyright (C) 1994-1999 Martin Bickel and Marc Schellenberger
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; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#include <pgrichedit.h>
#include <pgcheckbutton.h>
#include "../global.h"
#include "../widgets/textrenderer.h"
#include "messagedialog.h"
MessageDialog::MessageDialog(PG_Widget* parent, const PG_Rect& r, const std::string& windowtitle, const std::string& windowtext, const std::string& btn1text, const std::string& btn2text, PG_Label::TextAlign textalign, const std::string& style, bool rememberCheckbox) :
ASC_PG_Dialog(parent, r, windowtitle, MODAL, style), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL)
{
int buttonWidth = min( 120, r.Width() / 2 - 20 );
PG_Rect btn1;
int bttncount;
if ( btn2text.size() > 0 ) {
btn1 = PG_Rect( r.Width() / 2 - buttonWidth - 10, r.Height() - 35, buttonWidth, 30 );
bttncount = 2;
} else {
btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
bttncount = 1;
}
if( rememberCheckbox )
checkbox = new PG_CheckButton( this, PG_Rect( 10, r.Height() - 65, r.Width()-20, 20 ), bttncount == 2 ? "Remember choice" : "don't show again" );
else
checkbox = NULL;
my_btnok = new PG_Button(this, btn1, btn1text);
my_btnok->SetID(1);
my_btnok->sigClick.connect(slot(*this, &MessageDialog::handleButton));
my_btnok->activateHotkey( 0 );
if ( bttncount == 2 ) {
PG_Rect btn2 = btn1;
btn2.x = r.Width() / 2 + 10;
my_btncancel = new PG_Button(this, btn2, btn2text);
my_btncancel->SetID(2);
my_btncancel->sigClick.connect(slot(*this, &MessageDialog::handleButton));
my_btncancel->activateHotkey( 0 );
}
Init(windowtext, textalign, style);
}
MessageDialog::MessageDialog(PG_Widget* parent, const PG_Rect& r, const std::string& windowtitle, const std::string& windowtext, const std::string& btn1text, PG_Label::TextAlign textalign, const std::string& style) :
ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL), checkbox(NULL)
{
int buttonWidth = min( 120, r.Width() - 20 );
PG_Rect btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
my_btnok = new PG_Button(this, btn1, btn1text);
my_btnok->SetID(1);
my_btnok->sigClick.connect(slot(*this, &MessageDialog::handleButton));
my_btnok->activateHotkey( 0 );
Init(windowtext, textalign, style);
}
MessageDialog::MessageDialog(PG_Widget* parent, const PG_Rect& r, const std::string& windowtitle, const std::string& windowtext, PG_Label::TextAlign textalign, const std::string& style) :
ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL), checkbox(NULL)
{
Init(windowtext, textalign, style);
}
bool MessageDialog::remberChoice()
{
if ( checkbox && checkbox->GetPressed() )
return true;
else
return false;
}
bool MessageDialog::eventKeyDown (const SDL_KeyboardEvent *key)
{
if ( !defaultKeysActive )
return false;
if ( key->keysym.sym == SDLK_ESCAPE ) {
quitModalLoop(10);
return true;
}
if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
quitModalLoop(11);
return true;
}
if ( key->keysym.sym == SDLK_SPACE ) {
quitModalLoop(12);
return true;
}
return false;
}
void MessageDialog::EnableDefaultKeys( bool enable )
{
defaultKeysActive = enable;
}
PG_Widget* MessageDialog::getTextBox()
{
return my_textbox;
}
MessageDialog::~MessageDialog() {
// delete my_btnok;
// delete my_btncancel;
}
void MessageDialog::Init(const std::string& windowtext, int textalign, const std::string& style)
{
int heightDelta = 40;
if (checkbox )
heightDelta += 25;
my_textbox = new TextRenderer(this, PG_Rect(10, 40, my_width-20, my_height-50 - heightDelta));
// my_textbox->SendToBack();
// my_textbox->SetTransparency(255);
my_textbox->SetText(windowtext);
my_msgalign = textalign;
LoadThemeStyle(style);
}
void MessageDialog::LoadThemeStyle(const std::string& widgettype) {
PG_Window::LoadThemeStyle(widgettype);
if ( my_btnok )
my_btnok->LoadThemeStyle(widgettype, "Button1");
if(my_btncancel) {
my_btncancel->LoadThemeStyle(widgettype, "Button2");
}
}
bool MessageDialog::handleButton(PG_Button* button)
{
quitModalLoop( button ? button->GetID() : 0 );
return true;
}
PG_Rect calcMessageBoxSize( const ASCString& message )
{
int counter = 0;
for ( int i = 0; i< message.length(); ++i)
if ( message[i] == '\n' )
counter++;
return PG_Rect( -1, -1, 500, 150 + counter * 20 );
}
void errorMessageDialog( const ASCString& message )
{
PG_Rect size = calcMessageBoxSize(message);
MessageDialog msg( NULL, size, "Error", message, "OK", PG_Label::CENTER, "ErrorMessage" );
msg.Show();
msg.RunModal();
}
void warningMessageDialog( const ASCString& message )
{
PG_Rect size = calcMessageBoxSize(message);
MessageDialog msg( NULL, size, "Warning", message, "OK", PG_Label::CENTER, "WarningMessage" );
msg.Show();
msg.RunModal();
}
void infoMessageDialog( const ASCString& message )
{
PG_Rect size = calcMessageBoxSize(message);
MessageDialog msg( NULL, size, "Information", message, "OK" );
msg.Show();
msg.RunModal();
}
int new_choice_dlg(const ASCString& title, const ASCString& leftButton, const ASCString& rightButton )
{
PG_Rect size = calcMessageBoxSize(title);
MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window" );
msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
msg.getTextBox()->SetText(title);
msg.EnableDefaultKeys( false );
msg.Show();
// PG_Widget::UpdateScreen();
return msg.RunModal();
}
int new_choice_dlg(const ASCString& title, const ASCString& shortTitle, const ASCString& leftButton, const ASCString& rightButton, bool& saveResult )
{
PG_Rect size = calcMessageBoxSize(title);
MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window", true );
if ( title.size() < 30 )
msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
msg.getTextBox()->SetText(title);
msg.EnableDefaultKeys( false );
msg.Show();
// PG_Widget::UpdateScreen();
int result = msg.RunModal();
saveResult = msg.remberChoice();
return result;
}
|