summaryrefslogtreecommitdiff
path: root/src/gui/chat.h
blob: bb742a77158f97c15aab3d414bf4d765e9fb4382 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _TMW_CHAT_H
#define _TMW_CHAT_H

#include <list>
#include <string>
#include <map>

#include <guichan/actionlistener.hpp>
#include <guichan/keylistener.hpp>

#include "window.h"

#include "../guichanfwd.h"

class BrowserBox;
class ScrollArea;
class TabbedArea;
class Channel;
class ItemLinkHandler;

enum
{
    BY_GM     = 0,
    BY_PLAYER = 1,
    BY_OTHER  = 2,
    BY_SERVER = 3,
    BY_CHANNEL = 4,
    BY_LOGGER
};

/**
 * The chat window.
 *
 * \ingroup Interface
 */
class ChatWindow : public Window,
                   public gcn::ActionListener,
                   public gcn::KeyListener
{
    public:
        /**
         * Constructor.
         */
        ChatWindow();

        /**
         *
         */
        ~ChatWindow();

        /**
         * Called when the widget changes size. Used for adapting the size of
         * the tabbed area.
         */
        void widgetResized(const gcn::Event &event);

        /**
         * Gets the focused tab's name
         */
        const std::string& getFocused() const;

        /**
         * Clear the tab with the given name
         */
        void clearTab(const std::string &tab);

        /**
         * Adds a line of text to our message list. Parameters:
         *
         * @param line Text message.
         * @param own  Type of message (usually the owner-type).
         * @param channelName which channel to send the message to.
         */
        void chatLog(std::string line,
                     int own = BY_SERVER,
                     std::string channelName = "");

        /**
         * Performs action.
         */
        void action(const gcn::ActionEvent &event);

        /**
         * Request focus for typing chat message.
         */
        void requestChatFocus();

        /**
         * Checks whether ChatWindow is Focused or not.
         */
        bool isInputFocused();

        /**
         * Determines whether the message is a command or message, then
         * sends the given message to the game server to be said, or to the
         * command handler
         *
         * @param msg  The message text which is to be sent.
         *
         */
        void chatSend(const std::string &msg);

        /** Called to remove the channel from the channel manager */
        void removeChannel(short channelId);

        void removeChannel(const std::string &channelName);

        void removeChannel(Channel *channel);

        /** Called to create a new channel tab */
        void createNewChannelTab(const std::string &channelName);

        /** Called to output text to a specific channel */
        void sendToChannel(short channel,
                           const std::string &user,
                           const std::string &msg);

        /** Called when key is pressed */
        void keyPressed(gcn::KeyEvent &event);

        /** Called to set current text */
        void setInputText(std::string input_str);

        /** Called to add item to chat */
        void addItemText(int itemid, const std::string &item);

        /** Override to reset mTmpVisible */
        void setVisible(bool visible);

        /** Check if tab with that name already exists */
        bool tabExists(const std::string &tabName);

        void logic();

    private:
        bool mTmpVisible;

        int mItems;
        int mItemsKeep;

        struct CHATLOG
        {
            std::string nick;
            std::string text;
            int own;
        };

        /**
         * A structure combining a BrowserBox with its ScrollArea.
         */
        struct ChatArea
        {
            ChatArea(BrowserBox *b, ScrollArea *s):
                browser(b), scroll(s)
            {}

            BrowserBox *browser;
            ScrollArea *scroll;
        };

        /** Used for showing item popup on clicking links **/
        ItemLinkHandler *mItemLinkHandler;

        /** Tabbed area for holding each channel. */
        TabbedArea *mChatTabs;

        /** Input box for typing chat messages. */
        gcn::TextField *mChatInput;

        typedef std::map<const std::string, ChatArea> ChannelMap;
        /** Map each tab to its browser and scroll area. */
        ChannelMap mChannels;

        typedef std::list<std::string> History;
        typedef History::iterator HistoryIterator;
        History mHistory;           /**< Command history. */
        HistoryIterator mCurHist;   /**< History iterator. */
};

extern ChatWindow *chatWindow;

#endif