GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/chatinput.h Lines: 9 31 29.0 %
Date: 2018-09-20 Branches: 3 52 5.8 %

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