00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <pgrichedit.h>
00023
00024 #include "../global.h"
00025 #include "../widgets/textrenderer.h"
00026
00027
00028
00029 #include "messagedialog.h"
00030
00031
00032 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) :
00033 ASC_PG_Dialog(parent, r, windowtitle, MODAL, style), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL)
00034 {
00035
00036
00037 int buttonWidth = min( 120, r.Width() / 2 - 20 );
00038 PG_Rect btn1 = PG_Rect( r.Width() / 2 - buttonWidth - 10, r.Height() - 35, buttonWidth, 30 );
00039
00040 my_btnok = new PG_Button(this, btn1, btn1text);
00041 my_btnok->SetID(1);
00042 my_btnok->sigClick.connect(slot(*this, &MessageDialog::handleButton));
00043 my_btnok->activateHotkey( 0 );
00044
00045 PG_Rect btn2 = btn1;
00046 btn2.x = r.Width() / 2 + 10;
00047
00048 my_btncancel = new PG_Button(this, btn2, btn2text);
00049 my_btncancel->SetID(2);
00050 my_btncancel->sigClick.connect(slot(*this, &MessageDialog::handleButton));
00051 my_btncancel->activateHotkey( 0 );
00052
00053 Init(windowtext, textalign, style);
00054 }
00055
00056 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) :
00057 ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL)
00058 {
00059
00060 int buttonWidth = min( 120, r.Width() - 20 );
00061 PG_Rect btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
00062
00063 my_btnok = new PG_Button(this, btn1, btn1text);
00064 my_btnok->SetID(1);
00065 my_btnok->sigClick.connect(slot(*this, &MessageDialog::handleButton));
00066 my_btnok->activateHotkey( 0 );
00067
00068 Init(windowtext, textalign, style);
00069 }
00070
00071 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) :
00072 ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL)
00073 {
00074
00075 Init(windowtext, textalign, style);
00076 }
00077
00078 bool MessageDialog::eventKeyDown (const SDL_KeyboardEvent *key)
00079 {
00080 if ( !defaultKeysActive )
00081 return false;
00082
00083 if ( key->keysym.sym == SDLK_ESCAPE ) {
00084 quitModalLoop(10);
00085 return true;
00086 }
00087 if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
00088 quitModalLoop(11);
00089 return true;
00090 }
00091 if ( key->keysym.sym == SDLK_SPACE ) {
00092 quitModalLoop(12);
00093 return true;
00094 }
00095
00096 return false;
00097 }
00098
00099 void MessageDialog::EnableDefaultKeys( bool enable )
00100 {
00101 defaultKeysActive = enable;
00102 }
00103
00104 PG_Widget* MessageDialog::getTextBox()
00105 {
00106 return my_textbox;
00107 }
00108
00109 MessageDialog::~MessageDialog() {
00110
00111
00112 }
00113
00114 void MessageDialog::Init(const std::string& windowtext, int textalign, const std::string& style) {
00115
00116 my_textbox = new TextRenderer(this, PG_Rect(10, 40, my_width-20, my_height-50 - 40));
00117
00118
00119 my_textbox->SetText(windowtext);
00120
00121
00122
00123 my_msgalign = textalign;
00124
00125 LoadThemeStyle(style);
00126 }
00127
00128 void MessageDialog::LoadThemeStyle(const std::string& widgettype) {
00129 PG_Window::LoadThemeStyle(widgettype);
00130
00131 if ( my_btnok )
00132 my_btnok->LoadThemeStyle(widgettype, "Button1");
00133
00134 if(my_btncancel) {
00135 my_btncancel->LoadThemeStyle(widgettype, "Button2");
00136 }
00137 }
00138
00139 bool MessageDialog::handleButton(PG_Button* button)
00140 {
00141 quitModalLoop( button ? button->GetID() : 0 );
00142 return true;
00143 }
00144
00145
00146
00147
00148
00149
00150 PG_Rect calcMessageBoxSize( const ASCString& message )
00151 {
00152 int counter = 0;
00153 for ( int i = 0; i< message.length(); ++i)
00154 if ( message[i] == '\n' )
00155 counter++;
00156
00157 return PG_Rect( -1, -1, 500, 150 + counter * 20 );
00158 }
00159
00160
00161
00162 void errorMessageDialog( const ASCString& message )
00163 {
00164 PG_Rect size = calcMessageBoxSize(message);
00165 MessageDialog msg( NULL, size, "Error", message, "OK", PG_Label::CENTER, "ErrorMessage" );
00166 msg.Show();
00167 msg.RunModal();
00168 }
00169
00170 void warningMessageDialog( const ASCString& message )
00171 {
00172 PG_Rect size = calcMessageBoxSize(message);
00173 MessageDialog msg( NULL, size, "Warning", message, "OK", PG_Label::CENTER, "WarningMessage" );
00174 msg.Show();
00175 msg.RunModal();
00176 }
00177
00178 void infoMessageDialog( const ASCString& message )
00179 {
00180 PG_Rect size = calcMessageBoxSize(message);
00181 MessageDialog msg( NULL, size, "Information", message, "OK" );
00182 msg.Show();
00183 msg.RunModal();
00184 }
00185
00186
00187 int new_choice_dlg(const ASCString& title, const ASCString& leftButton, const ASCString& rightButton )
00188 {
00189 PG_Rect size = calcMessageBoxSize(title);
00190 MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window" );
00191 msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
00192 msg.getTextBox()->SetText(title);
00193 msg.EnableDefaultKeys( false );
00194
00195 msg.Show();
00196
00197 return msg.RunModal();
00198 }
00199
00200