3
* Copyright (C) 2010 Aethyra Development Team
5
* This file is part of Aethyra.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include <guichan/font.hpp>
24
#include "selectiondialog.h"
26
#include "../handlers/wordtextwraphandler.h"
28
#include "../widgets/button.h"
29
#include "../widgets/container.h"
30
#include "../widgets/radiobutton.h"
31
#include "../widgets/textbox.h"
33
#include "../../../core/utils/gettext.h"
34
#include "../../../core/utils/stringutils.h"
36
int SelectionDialog::mInstances = 0;
38
SelectionDialog::SelectionDialog(const std::string &title, const std::string &msg,
39
Window *parent, bool modal):
40
Window(title, modal, parent)
42
// Create a globally unique id for this dialog
43
mDialogKey = strprintf("selection%d", mInstances);
46
mTextBox = new TextBox(new WordTextWrapHandler());
47
mTextBox->setEditable(false);
48
mTextBox->setOpaque(false);
49
mTextBox->setTextWrapped(msg, 260);
51
mOkButton = new Button(_("OK"), "ok", this);
60
setLocationRelativeTo(getParent());
61
getParent()->moveToTop(this);
64
// Don't set the dialog to visible until there's an option to select
67
void SelectionDialog::fontChanged()
69
Window::fontChanged();
73
void SelectionDialog::reflow()
75
const int numRows = mTextBox->getNumberOfRows();
76
const int fontHeight = getFont()->getHeight();
77
const int radioHeight = (numRows * fontHeight) + getPadding();
78
const int height = getNumRows() * fontHeight;
79
int width = getFont()->getWidth(getCaption());
81
if (width < mTextBox->getMinWidth())
82
width = mTextBox->getMinWidth();
83
if (width < mOkButton->getWidth())
84
width = mOkButton->getWidth();
86
setContentSize(mTextBox->getMinWidth() + fontHeight, height + fontHeight +
87
mOkButton->getHeight());
88
mTextBox->setPosition(getPadding(), getPadding());
90
for (size_t i = 0; i < mRadioButtons.size(); i++)
92
mRadioButtons[i]->setPosition(getPadding(), radioHeight + (i *
96
mOkButton->setPosition((width - mOkButton->getWidth()) / 2, height +
97
(2 * mOkButton->getSpacing()));
100
void SelectionDialog::close()
103
windowContainer->scheduleDelete(this);
106
unsigned int SelectionDialog::getNumRows()
108
return (mTextBox->getNumberOfRows() + mRadioButtons.size());
111
void SelectionDialog::action(const gcn::ActionEvent &event)
113
// Proxy button events to our listeners
114
ActionListenerIterator i;
115
for (i = mActionListeners.begin(); i != mActionListeners.end(); ++i)
118
// Can we receive anything else anyway?
119
if (event.getId() == "ok")
121
for (size_t i = 0; i < mRadioButtons.size(); i++)
123
if (mRadioButtons[i]->isSelected())
125
setActionEventId(mRadioButtons[i]->getActionEventId());
126
distributeActionEvent();
134
void SelectionDialog::addOption(std::string key, std::string label)
136
RadioButton *radio = new RadioButton(label, mDialogKey, false);
137
radio->setActionEventId(key);
138
mRadioButtons.push_back(radio);
142
// Set to visible now that there's an option to select
145
mRadioButtons[0]->setSelected(true);
146
mOkButton->requestFocus();