GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/chatinput.h Lines: 7 28 25.0 %
Date: 2017-11-29 Branches: 5 50 10.0 %

Line Branch Exec Source
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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef GUI_WIDGETS_CHATINPUT_H
24
#define GUI_WIDGETS_CHATINPUT_H
25
26
#include "gui/windows/chatwindow.h"
27
28
#include "configuration.h"
29
30
#include "gui/windows/emotewindow.h"
31
32
#include "gui/widgets/textfield.h"
33
34
#include "localconsts.h"
35
36
/**
37
 * The chat input hides when it loses focus. It is also invisible by default.
38
 */
39
4
class ChatInput final : public TextField
40
{
41
    public:
42
4
        explicit ChatInput(ChatWindow *const window) :
43
            TextField(window, "", LoseFocusOnTab_false),
44
            mWindow(window),
45

28
            mFocusGaining(false)
46
        {
47
4
            setVisible(Visible_false);
48
4
            addFocusListener(this);
49
4
        }
50
51
        A_DELETE_COPY(ChatInput)
52
53
        /**
54
         * Called if the chat input loses focus. It will set itself to
55
         * invisible as result.
56
         */
57
        void focusLost(const Event &event) override final
58
        {
59
            TextField::focusLost(event);
60
            if (mFocusGaining || !config.getBoolValue("protectChatFocus"))
61
            {
62
                processVisible(Visible_false);
63
                if (chatWindow != nullptr)
64
                    chatWindow->updateVisibility();
65
                mFocusGaining = false;
66
                return;
67
            }
68
            mFocusGaining = true;
69
            requestFocus();
70
            mFocusGaining = false;
71
        }
72
73
        void processVisible(const Visible n)
74
        {
75
            if (mWindow == nullptr || isVisible() == (n == Visible_true))
76
                return;
77
78
            if (n == Visible_false)
79
                mFocusGaining = true;
80
            setVisible(n);
81
            if (config.getBoolValue("hideChatInput")
82
                || config.getBoolValue("showEmotesButton"))
83
            {
84
                mWindow->adjustTabSize();
85
            }
86
            if (emoteWindow != nullptr)
87
            {
88
                emoteWindow->hide();
89
            }
90
        }
91
92
        void unprotectFocus()
93
        { mFocusGaining = true; }
94
95
        void setVisible(Visible visible)
96
        {
97
4
            TextField::setVisible(visible);
98
        }
99
100
    private:
101
        ChatWindow *mWindow;
102
        bool mFocusGaining;
103
};
104
105
#endif  // GUI_WIDGETS_CHATINPUT_H