Advanced Strategic Command
messagedialog.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include <pgrichedit.h>
23 #include <pgcheckbutton.h>
24 
25 #include "../global.h"
26 #include "../widgets/textrenderer.h"
27 
28 
29 
30 #include "messagedialog.h"
31 
32 
33 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) :
34  ASC_PG_Dialog(parent, r, windowtitle, MODAL, style), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL)
35 {
36 
37 
38  int buttonWidth = min( 120, r.Width() / 2 - 20 );
39  PG_Rect btn1;
40 
41  int bttncount;
42 
43  if ( btn2text.size() > 0 ) {
44  btn1 = PG_Rect( r.Width() / 2 - buttonWidth - 10, r.Height() - 35, buttonWidth, 30 );
45  bttncount = 2;
46  } else {
47  btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
48  bttncount = 1;
49  }
50 
51 
52 
53  if( rememberCheckbox )
54  checkbox = new PG_CheckButton( this, PG_Rect( 10, r.Height() - 65, r.Width()-20, 20 ), bttncount == 2 ? "Remember choice" : "don't show again" );
55  else
56  checkbox = NULL;
57 
58  my_btnok = new PG_Button(this, btn1, btn1text);
59  my_btnok->SetID(1);
60  my_btnok->sigClick.connect( sigc::mem_fun(*this, &MessageDialog::handleButton));
61  my_btnok->activateHotkey( 0 );
62 
63  if ( bttncount == 2 ) {
64  PG_Rect btn2 = btn1;
65  btn2.x = r.Width() / 2 + 10;
66 
67  my_btncancel = new PG_Button(this, btn2, btn2text);
68  my_btncancel->SetID(2);
69  my_btncancel->sigClick.connect(sigc::mem_fun(*this, &MessageDialog::handleButton));
70  my_btncancel->activateHotkey( 0 );
71  }
72 
73  Init(windowtext, textalign, style);
74 }
75 
76 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) :
77  ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL), checkbox(NULL)
78 {
79 
80  int buttonWidth = min( 120, r.Width() - 20 );
81  PG_Rect btn1 = PG_Rect( r.Width() / 2 - buttonWidth/2, r.Height() - 40, buttonWidth, 30 );
82 
83  my_btnok = new PG_Button(this, btn1, btn1text);
84  my_btnok->SetID(1);
85  my_btnok->sigClick.connect(sigc::mem_fun(*this, &MessageDialog::handleButton));
86  my_btnok->activateHotkey( 0 );
87 
88  Init(windowtext, textalign, style);
89 }
90 
91 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) :
92  ASC_PG_Dialog(parent, r, windowtitle, MODAL, style ), defaultKeysActive(true), my_btnok(NULL), my_btncancel(NULL), checkbox(NULL)
93 {
94 
95  Init(windowtext, textalign, style);
96 }
97 
98 
100 {
101  if ( checkbox && checkbox->GetPressed() )
102  return true;
103  else
104  return false;
105 }
106 
107 
108 bool MessageDialog::eventKeyDown (const SDL_KeyboardEvent *key)
109 {
110  if ( !defaultKeysActive )
111  return false;
112 
113  if ( key->keysym.sym == SDLK_ESCAPE ) {
114  quitModalLoop(10);
115  return true;
116  }
117  if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
118  quitModalLoop(11);
119  return true;
120  }
121  if ( key->keysym.sym == SDLK_SPACE ) {
122  quitModalLoop(12);
123  return true;
124  }
125 
126  return false;
127 }
128 
130 {
131  defaultKeysActive = enable;
132 }
133 
135 {
136  return my_textbox;
137 }
138 
140  // delete my_btnok;
141  // delete my_btncancel;
142 }
143 
144 void MessageDialog::Init(const std::string& windowtext, int textalign, const std::string& style)
145 {
146  int heightDelta = 40;
147  if (checkbox )
148  heightDelta += 25;
149 
150  my_textbox = new TextRenderer(this, PG_Rect(10, 40, my_width-20, my_height-50 - heightDelta));
151  // my_textbox->SendToBack();
152  // my_textbox->SetTransparency(255);
153  my_textbox->SetText(windowtext);
154 
155 
156 
157  my_msgalign = textalign;
158 
159  LoadThemeStyle(style);
160 }
161 
162 void MessageDialog::SetText( const std::string& text )
163 {
164  if ( my_textbox && my_textbox->GetText() != text )
165  my_textbox->SetText( text );
166 }
167 
168 void MessageDialog::LoadThemeStyle(const std::string& widgettype) {
169  PG_Window::LoadThemeStyle(widgettype);
170 
171  if ( my_btnok )
172  my_btnok->LoadThemeStyle(widgettype, "Button1");
173 
174  if(my_btncancel) {
175  my_btncancel->LoadThemeStyle(widgettype, "Button2");
176  }
177 }
178 
179 bool MessageDialog::handleButton(PG_Button* button)
180 {
181  quitModalLoop( button ? button->GetID() : 0 );
182  return true;
183 }
184 
185 
186 
187 
188 
189 
190 PG_Rect calcMessageBoxSize( const ASCString& message )
191 {
192  int counter = 0;
193  for ( int i = 0; i< message.length(); ++i)
194  if ( message[i] == '\n' )
195  counter++;
196 
197  return PG_Rect( -1, -1, 500, 150 + counter * 20 );
198 }
199 
200 
201 
202 void errorMessageDialog( const ASCString& message )
203 {
204  PG_Rect size = calcMessageBoxSize(message);
205  MessageDialog msg( NULL, size, "Error", message, "OK", PG_Label::CENTER, "ErrorMessage" );
206  msg.Show();
207  msg.RunModal();
208 }
209 
210 void warningMessageDialog( const ASCString& message )
211 {
212  PG_Rect size = calcMessageBoxSize(message);
213  MessageDialog msg( NULL, size, "Warning", message, "OK", PG_Label::CENTER, "WarningMessage" );
214  msg.Show();
215  msg.RunModal();
216 }
217 
218 void infoMessageDialog( const ASCString& message )
219 {
220  PG_Rect size = calcMessageBoxSize(message);
221  MessageDialog msg( NULL, size, "Information", message, "OK" );
222  msg.Show();
223  msg.RunModal();
224 }
225 
226 
227 int new_choice_dlg(const ASCString& title, const ASCString& leftButton, const ASCString& rightButton )
228 {
229  PG_Rect size = calcMessageBoxSize(title);
230  MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window" );
231  msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
232  msg.getTextBox()->SetText(title);
233  msg.EnableDefaultKeys( false );
234 
235  msg.Show();
236  // PG_Widget::UpdateScreen();
237  return msg.RunModal();
238 }
239 
240 
241 int new_choice_dlg(const ASCString& title, const ASCString& shortTitle, const ASCString& leftButton, const ASCString& rightButton, bool& saveResult )
242 {
243 
244  PG_Rect size = calcMessageBoxSize(title);
245  MessageDialog msg( NULL, size,"", "", leftButton, rightButton, PG_Label::CENTER, "Window", true );
246 
247  if ( title.size() < 30 )
248  msg.getTextBox()->SetFontSize( msg.getTextBox()->GetFontSize() + 3 );
249 
250  msg.getTextBox()->SetText(title);
251  msg.EnableDefaultKeys( false );
252 
253  msg.Show();
254  // PG_Widget::UpdateScreen();
255 
256 
257  int result = msg.RunModal();
258  saveResult = msg.remberChoice();
259 
260  return result;
261 }
bool eventKeyDown(const SDL_KeyboardEvent *key)
PG_CheckButton * checkbox
void SetText(const std::string &text)
virtual const PG_String & GetText()
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void SetText(const std::string &text)
void warningMessageDialog(const ASCString &message)
bool quitModalLoop(int value)
Definition: paradialog.cpp:545
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
void infoMessageDialog(const ASCString &message)
void LoadThemeStyle(const std::string &widgettype)
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=PG_Label::CENTER, const std::string &style="MessageBox", bool rememberCheckbox=false)
Creates a PopUp with 2 Buttons.
void EnableDefaultKeys(bool enable)
PG_Button * my_btnok
Definition: messagedialog.h:99
PG_Button * my_btncancel
int new_choice_dlg(const ASCString &title, const ASCString &leftButton, const ASCString &rightButton)
virtual bool handleButton(PG_Button *button)
Checks if button is pressed.
PG_Rect calcMessageBoxSize(const ASCString &message)
const T & min(const T &a, const T &b, const T &c)
Definition: misc.h:80
PG_Widget * getTextBox()
void errorMessageDialog(const ASCString &message)