1 //  SuperTuxKart - a fun racing game with go-kart
2 //  Copyright (C) 2010-2015 Marianne Gagnon
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 3
7 //  of the License, or (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 
19 #ifndef HEADER_CONFIRM_DIALOG_HPP
20 #define HEADER_CONFIRM_DIALOG_HPP
21 
22 #include "guiengine/modaldialog.hpp"
23 #include "utils/cpp2011.hpp"
24 #include "utils/leak_check.hpp"
25 
26 /**
27  * \brief Generic dialog to ask the user to confirm something, or to show a simple message box
28  * \ingroup states_screens
29  */
30 class MessageDialog : public GUIEngine::ModalDialog
31 {
32 public:
33 
34     /**
35      * \brief Listener interface to get notified of whether the user chose to confirm or cancel
36      * \ingroup states_screens
37      */
38     class IConfirmDialogListener
39     {
40     public:
41 
42         LEAK_CHECK()
43 
IConfirmDialogListener()44         IConfirmDialogListener() {}
~IConfirmDialogListener()45         virtual ~IConfirmDialogListener() {}
46 
47         /** \brief Implement to be notified of dialog confirmed.
48           * \note  The dialog is not closed automatically, close it in the callback if this
49           *        behavior is desired.
50           */
onConfirm()51         virtual void onConfirm() { ModalDialog::dismiss(); };
52 
53         /** \brief Implement to be notified of dialog cancelled.
54           * \note  The default implementation is to close the modal dialog, but you may override
55           *        this method to change the behavior.
56           */
onCancel()57         virtual void onCancel() { ModalDialog::dismiss(); };
58 
59         /**
60           * \brief Optional callback
61           */
onDialogUpdate(float dt)62         virtual void onDialogUpdate(float dt) {}
63     };
64 
65     enum MessageDialogType { MESSAGE_DIALOG_OK, MESSAGE_DIALOG_CONFIRM,
66                              MESSAGE_DIALOG_OK_CANCEL, MESSAGE_DIALOG_YESNO };
67 
68     MessageDialogType m_type;
69 
70 private:
71 
72     IConfirmDialogListener* m_listener;
73     bool m_own_listener;
74     irr::core::stringw m_msg;
75     void doInit(bool from_queue);
76 
77     /** If set this will set the focus on 'cancel'/'no'
78      *  instead of "yes"/"ok". */
79     bool m_focus_on_cancel;
80 
81 public:
82 
83     /**
84       * \param msg Message to display in the dialog
85       * \param listener A listener object to notify when the user made a choice.
86       * \param If set to true, 'listener' will be owned by this dialog and deleted
87       *        along with the dialog.
88       */
89     MessageDialog(const irr::core::stringw &msg, MessageDialogType type,
90                   IConfirmDialogListener* listener, bool delete_listener,
91                   bool from_queue = false, float width = 0.6f, float height = 0.6f);
92 
93     /**
94       * Variant of MessageDialog where cancelling is not possible (i.e. just shows a message box with OK)
95       * \param msg Message to display in the dialog
96       */
97     MessageDialog(const irr::core::stringw &msg, bool from_queue = false);
98 
99     ~MessageDialog();
100 
101     virtual void onEnterPressedInternal() OVERRIDE;
102     virtual void onUpdate(float dt) OVERRIDE;
103     virtual void load() OVERRIDE;
104 
105     GUIEngine::EventPropagation processEvent(const std::string& eventSource) OVERRIDE;
106 
107     virtual void loadedFromFile() OVERRIDE;
108 
109     /** Calling this will make sure that the focus is set on the 'cancel' or
110      * 'no'. */
setFocusCancel()111     void setFocusCancel() {m_focus_on_cancel = true; }
112     virtual void init() OVERRIDE;
113 };
114 
115 
116 #endif
117