messagedialog.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #include <pgrichedit.h>
00023 #include <pgcheckbutton.h>
00024 
00025 #include "../global.h"
00026 #include "../widgets/textrenderer.h"
00027 
00028 
00029 
00030 #include "messagedialog.h"
00031 
00032 
00033 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) :
00034       ASC_PG_Dialog(parent, r, windowtitle, MODAL, style), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL)
00035 {
00036 
00037 
00038    int buttonWidth = min( 120, r.Width() / 2 - 20 );
00039    PG_Rect btn1;
00040 
00041    int bttncount;
00042 
00043    if ( btn2text.size() > 0 ) {
00044       btn1 = PG_Rect( r.Width() / 2 - buttonWidth - 10, r.Height() - 35, buttonWidth, 30 );
00045       bttncount = 2;
00046    } else {
00047       btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
00048       bttncount = 1;
00049    }
00050 
00051       
00052 
00053    if( rememberCheckbox ) 
00054       checkbox = new PG_CheckButton( this, PG_Rect( 10, r.Height() - 65, r.Width()-20, 20 ), bttncount == 2 ? "Remember choice" : "don't show again" );
00055    else
00056       checkbox = NULL;
00057 
00058    my_btnok = new PG_Button(this, btn1, btn1text);
00059    my_btnok->SetID(1);
00060    my_btnok->sigClick.connect(slot(*this, &MessageDialog::handleButton));
00061    my_btnok->activateHotkey( 0 );
00062 
00063    if ( bttncount == 2 ) {
00064       PG_Rect btn2 = btn1;
00065       btn2.x = r.Width() / 2 + 10;
00066 
00067       my_btncancel = new PG_Button(this, btn2, btn2text);
00068       my_btncancel->SetID(2);
00069       my_btncancel->sigClick.connect(slot(*this, &MessageDialog::handleButton));
00070       my_btncancel->activateHotkey( 0 );
00071    }
00072 
00073    Init(windowtext, textalign, style);
00074 }
00075 
00076 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) :
00077       ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL), checkbox(NULL)
00078 {
00079 
00080    int buttonWidth = min( 120, r.Width() - 20 );
00081    PG_Rect btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
00082 
00083    my_btnok = new PG_Button(this, btn1, btn1text);
00084    my_btnok->SetID(1);
00085    my_btnok->sigClick.connect(slot(*this, &MessageDialog::handleButton));
00086    my_btnok->activateHotkey( 0 );
00087 
00088    Init(windowtext, textalign, style);
00089 }
00090 
00091 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) :
00092       ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL), checkbox(NULL)
00093 {
00094 
00095    Init(windowtext, textalign, style);
00096 }
00097 
00098 
00099 bool MessageDialog::remberChoice()
00100 {
00101    if ( checkbox && checkbox->GetPressed() )
00102       return true;
00103    else
00104       return false;
00105 }
00106 
00107 
00108 bool MessageDialog::eventKeyDown (const SDL_KeyboardEvent *key)
00109 {
00110    if ( !defaultKeysActive )
00111       return false;
00112 
00113    if (  key->keysym.sym == SDLK_ESCAPE ) {
00114       quitModalLoop(10);
00115       return true;
00116    }
00117    if (  key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
00118       quitModalLoop(11);
00119       return true;
00120    }
00121    if (  key->keysym.sym == SDLK_SPACE ) {
00122       quitModalLoop(12);
00123       return true;
00124    }
00125 
00126    return false;
00127 }
00128 
00129 void MessageDialog::EnableDefaultKeys( bool enable )
00130 {
00131    defaultKeysActive = enable;
00132 }
00133 
00134 PG_Widget* MessageDialog::getTextBox()
00135 { 
00136    return my_textbox; 
00137 }
00138 
00139 MessageDialog::~MessageDialog() {
00140    // delete my_btnok;
00141    // delete my_btncancel;
00142 }
00143 
00144 void MessageDialog::Init(const std::string& windowtext, int textalign, const std::string& style) 
00145 {
00146    int heightDelta = 40;
00147    if (checkbox )
00148       heightDelta += 25;
00149 
00150    my_textbox = new TextRenderer(this, PG_Rect(10, 40, my_width-20, my_height-50 - heightDelta));
00151    // my_textbox->SendToBack();
00152    // my_textbox->SetTransparency(255);
00153    my_textbox->SetText(windowtext);
00154 
00155 
00156 
00157    my_msgalign = textalign;
00158 
00159    LoadThemeStyle(style);
00160 }
00161 
00162 void MessageDialog::SetText( const std::string& text ) 
00163 {
00164    if ( my_textbox && my_textbox->GetText() != text )
00165       my_textbox->SetText( text );
00166 }
00167 
00168 void MessageDialog::LoadThemeStyle(const std::string& widgettype) {
00169    PG_Window::LoadThemeStyle(widgettype);
00170 
00171    if ( my_btnok )
00172       my_btnok->LoadThemeStyle(widgettype, "Button1");
00173 
00174    if(my_btncancel) {
00175       my_btncancel->LoadThemeStyle(widgettype, "Button2");
00176    }
00177 }
00178 
00179 bool MessageDialog::handleButton(PG_Button* button)
00180 {
00181    quitModalLoop( button ? button->GetID() : 0 );
00182    return true;
00183 }
00184 
00185 
00186 
00187 
00188 
00189 
00190 PG_Rect calcMessageBoxSize( const ASCString& message )
00191 {
00192    int counter = 0;
00193    for ( int i = 0; i< message.length(); ++i)
00194       if ( message[i] == '\n' )
00195          counter++;
00196 
00197    return PG_Rect( -1, -1, 500, 150 + counter * 20 );
00198 }
00199 
00200 
00201 
00202 void errorMessageDialog( const ASCString& message )
00203 {
00204    PG_Rect size = calcMessageBoxSize(message);
00205    MessageDialog msg( NULL, size, "Error", message, "OK", PG_Label::CENTER, "ErrorMessage" );
00206    msg.Show();
00207    msg.RunModal();
00208 }
00209 
00210 void warningMessageDialog( const ASCString& message )
00211 {
00212    PG_Rect size = calcMessageBoxSize(message);
00213    MessageDialog msg( NULL, size, "Warning", message, "OK", PG_Label::CENTER, "WarningMessage" );
00214    msg.Show();
00215    msg.RunModal();
00216 }
00217 
00218 void infoMessageDialog( const ASCString& message )
00219 {
00220    PG_Rect size = calcMessageBoxSize(message);
00221    MessageDialog msg( NULL, size, "Information", message, "OK" );
00222    msg.Show();
00223    msg.RunModal();
00224 }
00225 
00226 
00227 int  new_choice_dlg(const ASCString& title, const ASCString& leftButton, const ASCString& rightButton )
00228 {
00229    PG_Rect size = calcMessageBoxSize(title);
00230    MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window" );
00231    msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
00232    msg.getTextBox()->SetText(title);
00233    msg.EnableDefaultKeys( false );
00234       
00235    msg.Show();
00236    // PG_Widget::UpdateScreen();
00237    return msg.RunModal();
00238 }
00239 
00240 
00241 int  new_choice_dlg(const ASCString& title, const ASCString& shortTitle, const ASCString& leftButton, const ASCString& rightButton, bool& saveResult )
00242 {
00243 
00244    PG_Rect size = calcMessageBoxSize(title);
00245    MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window", true );
00246    
00247    if ( title.size() < 30 )
00248       msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
00249 
00250    msg.getTextBox()->SetText(title);
00251    msg.EnableDefaultKeys( false );
00252       
00253    msg.Show();
00254    // PG_Widget::UpdateScreen();
00255 
00256 
00257    int result =  msg.RunModal();
00258    saveResult = msg.remberChoice();
00259 
00260    return result;
00261 }

Generated on Mon May 21 01:26:35 2012 for Advanced Strategic Command by  doxygen 1.5.1