~vcs-imports/aethyra/client

« back to all changes in this revision

Viewing changes to src/bindings/guichan/dialogs/selectiondialog.cpp

  • Committer: Tametomo
  • Date: 2010-10-07 21:11:23 UTC
  • Revision ID: git-v1:dd62b9e123b6d7e4b0efb9b634dc1596d81bfea6
Instead of automatically defaulting to OpenGL, start the client in SDL
mode, then allow the user to select whether they want to use SDL or OpenGl.
Issue originally brought up by Srauls.

Also updated Code::Blocks dependencies to match up with a forthcoming
Windows devpack.

TODO: Eventually make restarting the client doable from within the running
instance, instead of relying on the user to do so themselves.

Signed-off-by: Tametomo <irarice@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Aethyra
 
3
 *  Copyright (C) 2010  Aethyra Development Team
 
4
 *
 
5
 *  This file is part of Aethyra.
 
6
 *
 
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
 
10
 *  any later version.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 */
 
21
 
 
22
#include <guichan/font.hpp>
 
23
 
 
24
#include "selectiondialog.h"
 
25
 
 
26
#include "../handlers/wordtextwraphandler.h"
 
27
 
 
28
#include "../widgets/button.h"
 
29
#include "../widgets/container.h"
 
30
#include "../widgets/radiobutton.h"
 
31
#include "../widgets/textbox.h"
 
32
 
 
33
#include "../../../core/utils/gettext.h"
 
34
#include "../../../core/utils/stringutils.h"
 
35
 
 
36
int SelectionDialog::mInstances = 0;
 
37
 
 
38
SelectionDialog::SelectionDialog(const std::string &title, const std::string &msg,
 
39
                                 Window *parent, bool modal):
 
40
    Window(title, modal, parent)
 
41
{
 
42
    // Create a globally unique id for this dialog
 
43
    mDialogKey = strprintf("selection%d", mInstances);
 
44
    mInstances++;
 
45
 
 
46
    mTextBox = new TextBox(new WordTextWrapHandler());
 
47
    mTextBox->setEditable(false);
 
48
    mTextBox->setOpaque(false);
 
49
    mTextBox->setTextWrapped(msg, 260);
 
50
 
 
51
    mOkButton = new Button(_("OK"), "ok", this);
 
52
 
 
53
    fontChanged();
 
54
 
 
55
    add(mTextBox);
 
56
    add(mOkButton);
 
57
 
 
58
    if (getParent())
 
59
    {
 
60
        setLocationRelativeTo(getParent());
 
61
        getParent()->moveToTop(this);
 
62
    }
 
63
 
 
64
    // Don't set the dialog to visible until there's an option to select
 
65
}
 
66
 
 
67
void SelectionDialog::fontChanged()
 
68
{
 
69
    Window::fontChanged();
 
70
    reflow();
 
71
}
 
72
 
 
73
void SelectionDialog::reflow()
 
74
{
 
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());
 
80
 
 
81
    if (width < mTextBox->getMinWidth())
 
82
        width = mTextBox->getMinWidth();
 
83
    if (width < mOkButton->getWidth())
 
84
        width = mOkButton->getWidth();
 
85
 
 
86
    setContentSize(mTextBox->getMinWidth() + fontHeight, height + fontHeight +
 
87
                   mOkButton->getHeight());
 
88
    mTextBox->setPosition(getPadding(), getPadding());
 
89
 
 
90
    for (size_t i = 0; i < mRadioButtons.size(); i++)
 
91
    {
 
92
        mRadioButtons[i]->setPosition(getPadding(), radioHeight + (i *
 
93
                                      fontHeight));
 
94
    }
 
95
 
 
96
    mOkButton->setPosition((width - mOkButton->getWidth()) / 2, height +
 
97
                           (2 * mOkButton->getSpacing()));
 
98
}
 
99
 
 
100
void SelectionDialog::close()
 
101
{
 
102
    Window::close();
 
103
    windowContainer->scheduleDelete(this);
 
104
}
 
105
 
 
106
unsigned int SelectionDialog::getNumRows()
 
107
{
 
108
    return (mTextBox->getNumberOfRows() + mRadioButtons.size());
 
109
}
 
110
 
 
111
void SelectionDialog::action(const gcn::ActionEvent &event)
 
112
{
 
113
    // Proxy button events to our listeners
 
114
    ActionListenerIterator i;
 
115
    for (i = mActionListeners.begin(); i != mActionListeners.end(); ++i)
 
116
        (*i)->action(event);
 
117
 
 
118
    // Can we receive anything else anyway?
 
119
    if (event.getId() == "ok")
 
120
    {
 
121
        for (size_t i = 0; i < mRadioButtons.size(); i++)
 
122
        {
 
123
            if (mRadioButtons[i]->isSelected())
 
124
            {
 
125
                setActionEventId(mRadioButtons[i]->getActionEventId());
 
126
                distributeActionEvent();
 
127
                break;
 
128
            }
 
129
        }
 
130
        close();
 
131
    }
 
132
}
 
133
 
 
134
void SelectionDialog::addOption(std::string key, std::string label)
 
135
{
 
136
    RadioButton *radio = new RadioButton(label, mDialogKey, false);
 
137
    radio->setActionEventId(key);
 
138
    mRadioButtons.push_back(radio);
 
139
    add(radio);
 
140
    reflow();
 
141
 
 
142
    // Set to visible now that there's an option to select
 
143
    setVisible(true);
 
144
 
 
145
    mRadioButtons[0]->setSelected(true);
 
146
    mOkButton->requestFocus();
 
147
}