1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2004-2009  The Mana World Development Team
4  *  Copyright (C) 2009-2010  The Mana Developers
5  *  Copyright (C) 2011-2019  The ManaPlus Developers
6  *  Copyright (C) 2019-2021  Andrei Karas
7  *
8  *  This file is part of The ManaPlus Client.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef GUI_WIDGETS_CHATINPUT_H
25 #define GUI_WIDGETS_CHATINPUT_H
26 
27 #include "gui/windows/chatwindow.h"
28 
29 #include "configuration.h"
30 
31 #include "gui/windows/emotewindow.h"
32 
33 #include "gui/widgets/textfield.h"
34 
35 #include "localconsts.h"
36 
37 /**
38  * The chat input hides when it loses focus. It is also invisible by default.
39  */
40 class ChatInput final : public TextField
41 {
42     public:
ChatInput(ChatWindow * const window)43         explicit ChatInput(ChatWindow *const window) :
44             TextField(window, std::string(),
45                 LoseFocusOnTab_false, nullptr, std::string(), false),
46             mWindow(window),
47             mFocusGaining(false)
48         {
49             setVisible(Visible_false);
50             addFocusListener(this);
51         }
52 
A_DELETE_COPY(ChatInput)53         A_DELETE_COPY(ChatInput)
54 
55         /**
56          * Called if the chat input loses focus. It will set itself to
57          * invisible as result.
58          */
59         void focusLost(const Event &event) override final
60         {
61             TextField::focusLost(event);
62             if (mFocusGaining || !config.getBoolValue("protectChatFocus"))
63             {
64                 processVisible(Visible_false);
65                 if (chatWindow != nullptr)
66                     chatWindow->updateVisibility();
67                 mFocusGaining = false;
68                 return;
69             }
70             mFocusGaining = true;
71             requestFocus();
72             mFocusGaining = false;
73         }
74 
processVisible(const Visible n)75         void processVisible(const Visible n)
76         {
77             if (mWindow == nullptr || isVisible() == (n == Visible_true))
78                 return;
79 
80             if (n == Visible_false)
81                 mFocusGaining = true;
82             setVisible(n);
83             if (config.getBoolValue("hideChatInput")
84                 || config.getBoolValue("showEmotesButton"))
85             {
86                 mWindow->adjustTabSize();
87             }
88             if (emoteWindow != nullptr)
89             {
90                 emoteWindow->hide();
91             }
92         }
93 
unprotectFocus()94         void unprotectFocus()
95         { mFocusGaining = true; }
96 
setVisible(Visible visible)97         void setVisible(Visible visible)
98         {
99             TextField::setVisible(visible);
100         }
101 
102     private:
103         ChatWindow *mWindow;
104         bool mFocusGaining;
105 };
106 
107 #endif  // GUI_WIDGETS_CHATINPUT_H
108