summaryrefslogtreecommitdiff
path: root/src/gui/customserverdialog.cpp
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-11-21 21:07:59 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2012-01-10 20:31:56 +0100
commit98be39a094cc20f513da1847c553513682e4eeae (patch)
treebf3ac3591cf80c14e484328d50d7b553d18244e2 /src/gui/customserverdialog.cpp
parent683da50e37fbbb2e3f70a4421a06dabd9bb912d8 (diff)
downloadmana-client-98be39a094cc20f513da1847c553513682e4eeae.tar.gz
mana-client-98be39a094cc20f513da1847c553513682e4eeae.tar.bz2
mana-client-98be39a094cc20f513da1847c553513682e4eeae.tar.xz
mana-client-98be39a094cc20f513da1847c553513682e4eeae.zip
Made addition of custom servers be done in a separate window.
Now the first window the user can see is a list of servers which can double-clicked, making it all less cluttered. This commit also makes custom servers able to now have their own titles and their own description, just as the official ones. I also fixed the add entry button being registered twice to the action listener, and the fact that the description wasn't updated properly at windows loading and when adding/removing an entry. Resolves: Mana-Mantis #237. Reviewed-by: Ablu
Diffstat (limited to 'src/gui/customserverdialog.cpp')
-rw-r--r--src/gui/customserverdialog.cpp194
1 files changed, 194 insertions, 0 deletions
diff --git a/src/gui/customserverdialog.cpp b/src/gui/customserverdialog.cpp
new file mode 100644
index 00000000..304665d6
--- /dev/null
+++ b/src/gui/customserverdialog.cpp
@@ -0,0 +1,194 @@
+/*
+ * The Mana Client
+ * Copyright (C) 2011 The Mana Developers
+ *
+ * This file is part of The Mana Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gui/customserverdialog.h"
+
+#include "configuration.h"
+
+#include "gui/okdialog.h"
+#include "gui/sdlinput.h"
+#include "gui/serverdialog.h"
+
+#include "gui/widgets/button.h"
+#include "gui/widgets/dropdown.h"
+#include "gui/widgets/label.h"
+#include "gui/widgets/layout.h"
+#include "gui/widgets/textfield.h"
+
+#include "utils/gettext.h"
+
+std::string TypeListModel::getElementAt(int elementIndex)
+{
+ if (elementIndex == 0)
+ return "TmwAthena";
+ else if (elementIndex == 1)
+ return "ManaServ";
+ else
+ return "Unknown";
+}
+
+CustomServerDialog::CustomServerDialog(ServerDialog *parent):
+ Window(_("Add a custom Server"), true, static_cast<Window*>(parent)),
+ mServerDialog(parent)
+{
+ setWindowName("CustomServerDialog");
+
+ Label *nameLabel = new Label(_("Name:"));
+ Label *serverAdressLabel = new Label(_("Address:"));
+ Label *portLabel = new Label(_("Port:"));
+ Label *typeLabel = new Label(_("Server type:"));
+ Label *descriptionLabel = new Label(_("Description:"));
+ mServerAddressField = new TextField(std::string());
+ mPortField = new TextField(std::string());
+
+ mTypeListModel = new TypeListModel();
+ mTypeField = new DropDown(mTypeListModel);
+ mTypeField->setSelected(0); // TmwAthena by default for now.
+
+ mNameField = new TextField(std::string());
+ mDescriptionField = new TextField(std::string());
+
+ mOkButton = new Button(_("Ok"), "addServer", this);
+ mCancelButton = new Button(_("Cancel"), "cancel", this);
+
+ mServerAddressField->addActionListener(this);
+ mPortField->addActionListener(this);
+
+ place(0, 0, nameLabel);
+ place(1, 0, mNameField, 3).setPadding(3);
+ place(0, 1, serverAdressLabel);
+ place(1, 1, mServerAddressField, 3).setPadding(3);
+ place(0, 2, portLabel);
+ place(1, 2, mPortField, 3).setPadding(3);
+ place(0, 3, typeLabel);
+ place(3, 3, mTypeField, 1).setPadding(3);
+ place(0, 4, descriptionLabel);
+ place(1, 4, mDescriptionField, 3).setPadding(3);
+ place(3, 5, mOkButton);
+ place(2, 5, mCancelButton);
+
+ // Do this manually instead of calling reflowLayout so we can enforce a
+ // minimum width.
+ int width = 0, height = 0;
+ getLayout().reflow(width, height);
+ if (width < 400)
+ {
+ width = 400;
+ getLayout().reflow(width, height);
+ }
+ if (height < 120)
+ {
+ height = 120;
+ getLayout().reflow(width, height);
+ }
+
+ setContentSize(width, height);
+
+ setMinWidth(getWidth());
+ setMinHeight(getHeight());
+ setDefaultSize(getWidth(), getHeight(), ImageRect::CENTER);
+
+ setResizable(false);
+ addKeyListener(this);
+
+ loadWindowState();
+
+ setVisible(true);
+
+ mNameField->requestFocus();
+}
+
+CustomServerDialog::~CustomServerDialog()
+{
+ delete mTypeListModel;
+}
+
+void CustomServerDialog::logic()
+{
+ Window::logic();
+}
+
+void CustomServerDialog::action(const gcn::ActionEvent &event)
+{
+ if (event.getId() == "ok")
+ {
+ // Give focus back to the server dialog.
+ mServerAddressField->requestFocus();
+ }
+ if (event.getId() == "addServer")
+ {
+ // Check the given information
+ if (mServerAddressField->getText().empty()
+ || mPortField->getText().empty())
+ {
+ OkDialog *dlg = new OkDialog(_("Error"),
+ _("Please at least type both the address and the port "
+ "of the server."));
+ dlg->addActionListener(this);
+ }
+ else
+ {
+ mCancelButton->setEnabled(false);
+ mOkButton->setEnabled(false);
+
+ ServerInfo serverInfo;
+ serverInfo.name = mNameField->getText();
+ serverInfo.description = mDescriptionField->getText();
+ serverInfo.hostname = mServerAddressField->getText();
+ serverInfo.port = (short) atoi(mPortField->getText().c_str());
+ switch (mTypeField->getSelected())
+ {
+ case 0:
+ serverInfo.type = ServerInfo::TMWATHENA;
+ break;
+ case 1:
+ serverInfo.type = ServerInfo::MANASERV;
+ break;
+ default:
+ serverInfo.type = ServerInfo::UNKNOWN;
+ }
+
+ // Tell the server has to be saved
+ serverInfo.save = true;
+
+ //Add server
+ mServerDialog->saveCustomServers(serverInfo);
+ scheduleDelete();
+ }
+ }
+ else if (event.getId() == "cancel")
+ {
+ scheduleDelete();
+ }
+}
+
+void CustomServerDialog::keyPressed(gcn::KeyEvent &keyEvent)
+{
+ gcn::Key key = keyEvent.getKey();
+
+ if (key.getValue() == Key::ESCAPE)
+ {
+ scheduleDelete();
+ }
+ else if (key.getValue() == Key::ENTER)
+ {
+ action(gcn::ActionEvent(NULL, mOkButton->getActionEventId()));
+ }
+}