2008-08-11 22:45:11 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD 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, version 2.
|
|
|
|
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
/** @file network_chat_gui.cpp GUI for handling chat messages. */
|
|
|
|
|
|
|
|
#include <stdarg.h> /* va_list */
|
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../date_func.h"
|
|
|
|
#include "../gfx_func.h"
|
|
|
|
#include "../strings_func.h"
|
|
|
|
#include "../blitter/factory.hpp"
|
|
|
|
#include "../console_func.h"
|
|
|
|
#include "../video/video_driver.hpp"
|
|
|
|
#include "../table/sprites.h"
|
|
|
|
#include "../querystring_gui.h"
|
|
|
|
#include "../town.h"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "network_internal.h"
|
|
|
|
#include "network_client.h"
|
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* The draw buffer must be able to contain the chat message, client name and the "[All]" message,
|
2008-08-12 12:23:05 +00:00
|
|
|
* some spaces and possible translations of [All] to other languages. */
|
|
|
|
assert_compile((int)DRAW_STRING_BUFFER >= (int)NETWORK_CHAT_LENGTH + NETWORK_NAME_LENGTH + 40);
|
2008-08-12 11:21:37 +00:00
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
enum {
|
2009-04-11 10:38:00 +00:00
|
|
|
NETWORK_CHAT_LINE_SPACING = 3,
|
2008-08-11 22:45:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ChatMessage {
|
2008-08-12 12:23:05 +00:00
|
|
|
char message[DRAW_STRING_BUFFER];
|
2009-02-09 02:09:47 +00:00
|
|
|
TextColour colour;
|
2008-08-11 22:45:11 +00:00
|
|
|
Date end_date;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* used for chat window */
|
2008-08-12 12:23:05 +00:00
|
|
|
static ChatMessage *_chatmsg_list = NULL;
|
2008-08-11 22:45:11 +00:00
|
|
|
static bool _chatmessage_dirty = false;
|
|
|
|
static bool _chatmessage_visible = false;
|
|
|
|
static bool _chat_tab_completion_active;
|
2008-08-12 12:23:05 +00:00
|
|
|
static uint MAX_CHAT_MESSAGES = 0;
|
2008-08-11 22:45:11 +00:00
|
|
|
|
|
|
|
/* The chatbox grows from the bottom so the coordinates are pixels from
|
|
|
|
* the left and pixels from the bottom. The height is the maximum height */
|
2008-08-12 12:23:05 +00:00
|
|
|
static PointDimension _chatmsg_box;
|
|
|
|
static uint8 *_chatmessage_backup = NULL;
|
2008-08-11 22:45:11 +00:00
|
|
|
|
|
|
|
static inline uint GetChatMessageCount()
|
|
|
|
{
|
|
|
|
uint i = 0;
|
|
|
|
for (; i < MAX_CHAT_MESSAGES; i++) {
|
|
|
|
if (_chatmsg_list[i].message[0] == '\0') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a text message to the 'chat window' to be shown
|
2009-02-09 02:57:15 +00:00
|
|
|
* @param colour The colour this message is to be shown in
|
2008-08-11 22:45:11 +00:00
|
|
|
* @param duration The duration of the chat message in game-days
|
|
|
|
* @param message message itself in printf() style
|
|
|
|
*/
|
2009-02-09 02:09:47 +00:00
|
|
|
void CDECL NetworkAddChatMessage(TextColour colour, uint8 duration, const char *message, ...)
|
2008-08-11 22:45:11 +00:00
|
|
|
{
|
2008-08-12 12:23:05 +00:00
|
|
|
char buf[DRAW_STRING_BUFFER];
|
2008-08-11 22:45:11 +00:00
|
|
|
const char *bufp;
|
|
|
|
va_list va;
|
|
|
|
uint msg_count;
|
|
|
|
uint16 lines;
|
|
|
|
|
|
|
|
va_start(va, message);
|
|
|
|
vsnprintf(buf, lengthof(buf), message, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2008-08-12 12:23:05 +00:00
|
|
|
Utf8TrimString(buf, DRAW_STRING_BUFFER);
|
2008-08-11 22:45:11 +00:00
|
|
|
|
|
|
|
/* Force linebreaks for strings that are too long */
|
2009-10-13 20:19:34 +00:00
|
|
|
lines = GB(FormatStringLinebreaks(buf, lastof(buf), _chatmsg_box.width - 8), 0, 16) + 1;
|
2008-08-11 22:45:11 +00:00
|
|
|
if (lines >= MAX_CHAT_MESSAGES) return;
|
|
|
|
|
|
|
|
msg_count = GetChatMessageCount();
|
|
|
|
/* We want to add more chat messages than there is free space for, remove 'old' */
|
|
|
|
if (lines > MAX_CHAT_MESSAGES - msg_count) {
|
|
|
|
int i = lines - (MAX_CHAT_MESSAGES - msg_count);
|
|
|
|
memmove(&_chatmsg_list[0], &_chatmsg_list[i], sizeof(_chatmsg_list[0]) * (msg_count - i));
|
|
|
|
msg_count = MAX_CHAT_MESSAGES - lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (bufp = buf; lines != 0; lines--) {
|
|
|
|
ChatMessage *cmsg = &_chatmsg_list[msg_count++];
|
2008-11-02 11:20:15 +00:00
|
|
|
strecpy(cmsg->message, bufp, lastof(cmsg->message));
|
2008-08-11 22:45:11 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* The default colour for a message is company colour. Replace this with
|
2008-08-11 22:45:11 +00:00
|
|
|
* white for any additional lines */
|
2009-06-01 11:43:36 +00:00
|
|
|
cmsg->colour = (bufp == buf && (colour & IS_PALETTE_COLOUR)) ? colour : TC_WHITE;
|
2008-08-11 22:45:11 +00:00
|
|
|
cmsg->end_date = _date + duration;
|
|
|
|
|
|
|
|
bufp += strlen(bufp) + 1; // jump to 'next line' in the formatted string
|
|
|
|
}
|
|
|
|
|
|
|
|
_chatmessage_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkInitChatMessage()
|
|
|
|
{
|
2008-08-12 12:23:05 +00:00
|
|
|
MAX_CHAT_MESSAGES = _settings_client.gui.network_chat_box_height;
|
|
|
|
|
|
|
|
_chatmsg_list = ReallocT(_chatmsg_list, _settings_client.gui.network_chat_box_height);
|
|
|
|
_chatmsg_box.x = 10;
|
2009-11-02 10:15:48 +00:00
|
|
|
_chatmsg_box.y = 3 * FONT_HEIGHT_NORMAL;
|
2008-08-12 12:23:05 +00:00
|
|
|
_chatmsg_box.width = _settings_client.gui.network_chat_box_width;
|
2009-04-11 10:38:00 +00:00
|
|
|
_chatmsg_box.height = _settings_client.gui.network_chat_box_height * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) + 2;
|
2008-08-15 22:06:58 +00:00
|
|
|
_chatmessage_backup = ReallocT(_chatmessage_backup, _chatmsg_box.width * _chatmsg_box.height * BlitterFactoryBase::GetCurrentBlitter()->GetBytesPerPixel());
|
2008-08-12 12:23:05 +00:00
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
|
|
|
|
_chatmsg_list[i].message[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Hide the chatbox */
|
|
|
|
void NetworkUndrawChatMessage()
|
|
|
|
{
|
2009-06-18 17:16:29 +00:00
|
|
|
/* Sometimes we also need to hide the cursor
|
|
|
|
* This is because both textmessage and the cursor take a shot of the
|
|
|
|
* screen before drawing.
|
|
|
|
* Now the textmessage takes his shot and paints his data before the cursor
|
|
|
|
* does, so in the shot of the cursor is the screen-data of the textmessage
|
|
|
|
* included when the cursor hangs somewhere over the textmessage. To
|
|
|
|
* avoid wrong repaints, we undraw the cursor in that case, and everything
|
|
|
|
* looks nicely ;)
|
|
|
|
* (and now hope this story above makes sense to you ;))
|
|
|
|
*/
|
|
|
|
if (_cursor.visible &&
|
|
|
|
_cursor.draw_pos.x + _cursor.draw_size.x >= _chatmsg_box.x &&
|
|
|
|
_cursor.draw_pos.x <= _chatmsg_box.x + _chatmsg_box.width &&
|
|
|
|
_cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _chatmsg_box.y - _chatmsg_box.height &&
|
|
|
|
_cursor.draw_pos.y <= _screen.height - _chatmsg_box.y) {
|
|
|
|
UndrawMouseCursor();
|
|
|
|
}
|
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
if (_chatmessage_visible) {
|
|
|
|
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
|
|
|
int x = _chatmsg_box.x;
|
|
|
|
int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
|
|
|
|
int width = _chatmsg_box.width;
|
|
|
|
int height = _chatmsg_box.height;
|
|
|
|
if (y < 0) {
|
|
|
|
height = max(height + y, min(_chatmsg_box.height, _screen.height));
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
if (x + width >= _screen.width) {
|
|
|
|
width = _screen.width - x;
|
|
|
|
}
|
|
|
|
if (width <= 0 || height <= 0) return;
|
|
|
|
|
|
|
|
_chatmessage_visible = false;
|
|
|
|
/* Put our 'shot' back to the screen */
|
|
|
|
blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
|
|
|
|
/* And make sure it is updated next time */
|
|
|
|
_video_driver->MakeDirty(x, y, width, height);
|
|
|
|
|
|
|
|
_chatmessage_dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a message is expired every day */
|
|
|
|
void NetworkChatMessageDailyLoop()
|
|
|
|
{
|
|
|
|
for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
|
|
|
|
ChatMessage *cmsg = &_chatmsg_list[i];
|
|
|
|
if (cmsg->message[0] == '\0') continue;
|
|
|
|
|
|
|
|
/* Message has expired, remove from the list */
|
|
|
|
if (cmsg->end_date < _date) {
|
|
|
|
/* Move the remaining messages over the current message */
|
|
|
|
if (i != MAX_CHAT_MESSAGES - 1) memmove(cmsg, cmsg + 1, sizeof(*cmsg) * (MAX_CHAT_MESSAGES - i - 1));
|
|
|
|
|
|
|
|
/* Mark the last item as empty */
|
|
|
|
_chatmsg_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0';
|
|
|
|
_chatmessage_dirty = true;
|
|
|
|
|
|
|
|
/* Go one item back, because we moved the array 1 to the left */
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Draw the chat message-box */
|
|
|
|
void NetworkDrawChatMessage()
|
|
|
|
{
|
|
|
|
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
|
|
|
if (!_chatmessage_dirty) return;
|
|
|
|
|
|
|
|
/* First undraw if needed */
|
|
|
|
NetworkUndrawChatMessage();
|
|
|
|
|
|
|
|
if (_iconsole_mode == ICONSOLE_FULL) return;
|
|
|
|
|
|
|
|
/* Check if we have anything to draw at all */
|
|
|
|
uint count = GetChatMessageCount();
|
|
|
|
if (count == 0) return;
|
|
|
|
|
|
|
|
int x = _chatmsg_box.x;
|
|
|
|
int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
|
|
|
|
int width = _chatmsg_box.width;
|
|
|
|
int height = _chatmsg_box.height;
|
|
|
|
if (y < 0) {
|
|
|
|
height = max(height + y, min(_chatmsg_box.height, _screen.height));
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
if (x + width >= _screen.width) {
|
|
|
|
width = _screen.width - x;
|
|
|
|
}
|
|
|
|
if (width <= 0 || height <= 0) return;
|
|
|
|
|
2008-08-15 22:06:58 +00:00
|
|
|
assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel()));
|
2008-08-11 22:45:11 +00:00
|
|
|
|
|
|
|
/* Make a copy of the screen as it is before painting (for undraw) */
|
|
|
|
blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
|
|
|
|
|
|
|
|
_cur_dpi = &_screen; // switch to _screen painting
|
|
|
|
|
|
|
|
/* Paint a half-transparent box behind the chat messages */
|
|
|
|
GfxFillRect(
|
|
|
|
_chatmsg_box.x,
|
2009-04-11 10:38:00 +00:00
|
|
|
_screen.height - _chatmsg_box.y - count * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) - 2,
|
2008-08-11 22:45:11 +00:00
|
|
|
_chatmsg_box.x + _chatmsg_box.width - 1,
|
|
|
|
_screen.height - _chatmsg_box.y - 2,
|
2009-02-09 02:57:15 +00:00
|
|
|
PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR // black, but with some alpha for background
|
2008-08-11 22:45:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* Paint the chat messages starting with the lowest at the bottom */
|
2009-04-11 10:38:00 +00:00
|
|
|
for (uint y = FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING; count-- != 0; y += (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING)) {
|
2009-03-21 22:46:17 +00:00
|
|
|
DrawString(_chatmsg_box.x + 3, _chatmsg_box.x + _chatmsg_box.width - 1, _screen.height - _chatmsg_box.y - y + 1, _chatmsg_list[count].message, _chatmsg_list[count].colour);
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the data is updated next flush */
|
|
|
|
_video_driver->MakeDirty(x, y, width, height);
|
|
|
|
|
|
|
|
_chatmessage_visible = true;
|
|
|
|
_chatmessage_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SendChat(const char *buf, DestType type, int dest)
|
|
|
|
{
|
|
|
|
if (StrEmpty(buf)) return;
|
|
|
|
if (!_network_server) {
|
2008-12-29 10:37:53 +00:00
|
|
|
SEND_COMMAND(PACKET_CLIENT_CHAT)((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, 0);
|
2008-08-11 22:45:11 +00:00
|
|
|
} else {
|
2008-12-22 12:59:31 +00:00
|
|
|
NetworkServerSendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, CLIENT_ID_SERVER);
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 20:03:12 +00:00
|
|
|
/** Widget numbers of the chat window. */
|
|
|
|
enum NetWorkChatWidgets {
|
|
|
|
NWCW_CLOSE,
|
|
|
|
NWCW_BACKGROUND,
|
2009-09-25 18:15:41 +00:00
|
|
|
NWCW_DESTINATION,
|
2009-05-05 20:03:12 +00:00
|
|
|
NWCW_TEXTBOX,
|
|
|
|
NWCW_SENDBUTTON,
|
|
|
|
};
|
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
struct NetworkChatWindow : public QueryStringBaseWindow {
|
|
|
|
DestType dtype;
|
2009-09-25 18:15:41 +00:00
|
|
|
StringID dest_string;
|
2008-08-11 22:45:11 +00:00
|
|
|
int dest;
|
|
|
|
|
2009-10-07 17:36:33 +00:00
|
|
|
NetworkChatWindow(const WindowDesc *desc, DestType type, int dest) : QueryStringBaseWindow(NETWORK_CHAT_LENGTH)
|
2008-08-11 22:45:11 +00:00
|
|
|
{
|
|
|
|
this->dtype = type;
|
|
|
|
this->dest = dest;
|
|
|
|
this->afilter = CS_ALPHANUMERAL;
|
|
|
|
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0);
|
|
|
|
|
2009-09-25 18:15:41 +00:00
|
|
|
static const StringID chat_captions[] = {
|
|
|
|
STR_NETWORK_CHAT_ALL_CAPTION,
|
|
|
|
STR_NETWORK_CHAT_COMPANY_CAPTION,
|
|
|
|
STR_NETWORK_CHAT_CLIENT_CAPTION
|
|
|
|
};
|
|
|
|
assert((uint)this->dtype < lengthof(chat_captions));
|
|
|
|
this->dest_string = chat_captions[this->dtype];
|
2008-08-11 22:45:11 +00:00
|
|
|
|
2009-10-07 17:36:33 +00:00
|
|
|
this->InitNested(desc, type);
|
2008-08-11 22:45:11 +00:00
|
|
|
|
2009-09-25 18:15:41 +00:00
|
|
|
this->SetFocusedWidget(NWCW_TEXTBOX);
|
|
|
|
InvalidateWindowData(WC_NEWS_WINDOW, 0, this->height);
|
|
|
|
_chat_tab_completion_active = false;
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 17:36:33 +00:00
|
|
|
~NetworkChatWindow()
|
2008-08-11 22:45:11 +00:00
|
|
|
{
|
|
|
|
InvalidateWindowData(WC_NEWS_WINDOW, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the next item of the list of things that can be auto-completed.
|
|
|
|
* @param item The current indexed item to return. This function can, and most
|
|
|
|
* likely will, alter item, to skip empty items in the arrays.
|
|
|
|
* @return Returns the char that matched to the index.
|
|
|
|
*/
|
|
|
|
const char *ChatTabCompletionNextItem(uint *item)
|
|
|
|
{
|
|
|
|
static char chat_tab_temp_buffer[64];
|
|
|
|
|
|
|
|
/* First, try clients */
|
2008-12-23 11:55:46 +00:00
|
|
|
if (*item < MAX_CLIENT_SLOTS) {
|
2009-05-22 15:13:50 +00:00
|
|
|
/* Skip inactive clients */
|
|
|
|
NetworkClientInfo *ci;
|
|
|
|
FOR_ALL_CLIENT_INFOS_FROM(ci, *item) {
|
|
|
|
*item = ci->index;
|
|
|
|
return ci->client_name;
|
2008-12-23 20:52:27 +00:00
|
|
|
}
|
|
|
|
*item = MAX_CLIENT_SLOTS;
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Then, try townnames
|
|
|
|
* Not that the following assumes all town indices are adjacent, ie no
|
2009-03-14 18:16:29 +00:00
|
|
|
* towns have been deleted. */
|
2009-05-17 11:17:53 +00:00
|
|
|
if (*item < (uint)MAX_CLIENT_SLOTS + Town::GetPoolSize()) {
|
2008-08-11 22:45:11 +00:00
|
|
|
const Town *t;
|
|
|
|
|
2008-12-23 11:55:46 +00:00
|
|
|
FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_SLOTS) {
|
2008-08-11 22:45:11 +00:00
|
|
|
/* Get the town-name via the string-system */
|
|
|
|
SetDParam(0, t->index);
|
2009-07-20 11:21:57 +00:00
|
|
|
GetString(chat_tab_temp_buffer, STR_TOWN_NAME, lastof(chat_tab_temp_buffer));
|
2008-08-11 22:45:11 +00:00
|
|
|
return &chat_tab_temp_buffer[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find what text to complete. It scans for a space from the left and marks
|
|
|
|
* the word right from that as to complete. It also writes a \0 at the
|
|
|
|
* position of the space (if any). If nothing found, buf is returned.
|
|
|
|
*/
|
|
|
|
static char *ChatTabCompletionFindText(char *buf)
|
|
|
|
{
|
|
|
|
char *p = strrchr(buf, ' ');
|
|
|
|
if (p == NULL) return buf;
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
return p + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if we can auto-complete the current text of the user.
|
|
|
|
*/
|
|
|
|
void ChatTabCompletion()
|
|
|
|
{
|
|
|
|
static char _chat_tab_completion_buf[NETWORK_CHAT_LENGTH];
|
|
|
|
assert(this->edit_str_size == lengthof(_chat_tab_completion_buf));
|
|
|
|
|
|
|
|
Textbuf *tb = &this->text;
|
|
|
|
size_t len, tb_len;
|
|
|
|
uint item;
|
|
|
|
char *tb_buf, *pre_buf;
|
|
|
|
const char *cur_name;
|
|
|
|
bool second_scan = false;
|
|
|
|
|
|
|
|
item = 0;
|
|
|
|
|
|
|
|
/* Copy the buffer so we can modify it without damaging the real data */
|
|
|
|
pre_buf = (_chat_tab_completion_active) ? strdup(_chat_tab_completion_buf) : strdup(tb->buf);
|
|
|
|
|
|
|
|
tb_buf = ChatTabCompletionFindText(pre_buf);
|
|
|
|
tb_len = strlen(tb_buf);
|
|
|
|
|
|
|
|
while ((cur_name = ChatTabCompletionNextItem(&item)) != NULL) {
|
|
|
|
item++;
|
|
|
|
|
|
|
|
if (_chat_tab_completion_active) {
|
|
|
|
/* We are pressing TAB again on the same name, is there an other name
|
2009-03-14 18:16:29 +00:00
|
|
|
* that starts with this? */
|
2008-08-11 22:45:11 +00:00
|
|
|
if (!second_scan) {
|
|
|
|
size_t offset;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
/* If we are completing at the begin of the line, skip the ': ' we added */
|
|
|
|
if (tb_buf == pre_buf) {
|
|
|
|
offset = 0;
|
2008-10-22 19:12:10 +00:00
|
|
|
length = (tb->size - 1) - 2;
|
2008-08-11 22:45:11 +00:00
|
|
|
} else {
|
|
|
|
/* Else, find the place we are completing at */
|
|
|
|
offset = strlen(pre_buf) + 1;
|
2008-10-22 19:12:10 +00:00
|
|
|
length = (tb->size - 1) - offset;
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare if we have a match */
|
|
|
|
if (strlen(cur_name) == length && strncmp(cur_name, tb->buf + offset, length) == 0) second_scan = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now any match we make on _chat_tab_completion_buf after this, is perfect */
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(cur_name);
|
|
|
|
if (tb_len < len && strncasecmp(cur_name, tb_buf, tb_len) == 0) {
|
|
|
|
/* Save the data it was before completion */
|
|
|
|
if (!second_scan) snprintf(_chat_tab_completion_buf, lengthof(_chat_tab_completion_buf), "%s", tb->buf);
|
|
|
|
_chat_tab_completion_active = true;
|
|
|
|
|
|
|
|
/* Change to the found name. Add ': ' if we are at the start of the line (pretty) */
|
|
|
|
if (pre_buf == tb_buf) {
|
|
|
|
snprintf(tb->buf, this->edit_str_size, "%s: ", cur_name);
|
|
|
|
} else {
|
|
|
|
snprintf(tb->buf, this->edit_str_size, "%s %s", pre_buf, cur_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the textbuffer */
|
|
|
|
UpdateTextBufferSize(&this->text);
|
|
|
|
|
|
|
|
this->SetDirty();
|
|
|
|
free(pre_buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (second_scan) {
|
|
|
|
/* We walked all posibilities, and the user presses tab again.. revert to original text */
|
|
|
|
strcpy(tb->buf, _chat_tab_completion_buf);
|
|
|
|
_chat_tab_completion_active = false;
|
|
|
|
|
|
|
|
/* Update the textbuffer */
|
|
|
|
UpdateTextBufferSize(&this->text);
|
|
|
|
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
free(pre_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnPaint()
|
|
|
|
{
|
|
|
|
this->DrawWidgets();
|
2009-01-08 22:33:54 +00:00
|
|
|
this->DrawEditBox(NWCW_TEXTBOX);
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
2009-11-02 10:15:48 +00:00
|
|
|
virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
|
|
|
|
{
|
|
|
|
Point pt = { (_screen.width - max(sm_width, desc->default_width)) / 2, _screen.height - sm_height - FindWindowById(WC_STATUS_BAR, 0)->height };
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2009-11-22 18:28:14 +00:00
|
|
|
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
2009-09-25 18:15:41 +00:00
|
|
|
{
|
|
|
|
if (widget != NWCW_DESTINATION) return;
|
2009-10-07 19:35:05 +00:00
|
|
|
|
|
|
|
if (this->dtype == DESTTYPE_CLIENT) {
|
|
|
|
SetDParamStr(0, NetworkFindClientInfoFromClientID((ClientID)this->dest)->client_name);
|
|
|
|
}
|
|
|
|
Dimension d = GetStringBoundingBox(this->dest_string);
|
|
|
|
d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
|
|
|
|
d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
|
|
|
|
*size = maxdim(*size, d);
|
2009-09-25 18:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DrawWidget(const Rect &r, int widget) const
|
|
|
|
{
|
|
|
|
if (widget != NWCW_DESTINATION) return;
|
|
|
|
|
2009-10-07 19:35:05 +00:00
|
|
|
if (this->dtype == DESTTYPE_CLIENT) {
|
|
|
|
SetDParamStr(0, NetworkFindClientInfoFromClientID((ClientID)this->dest)->client_name);
|
|
|
|
}
|
2009-09-25 18:15:41 +00:00
|
|
|
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, this->dest_string, TC_BLACK, SA_RIGHT);
|
|
|
|
}
|
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
virtual void OnClick(Point pt, int widget)
|
|
|
|
{
|
|
|
|
switch (widget) {
|
2009-01-08 22:33:54 +00:00
|
|
|
/* Send */
|
|
|
|
case NWCW_SENDBUTTON: SendChat(this->text.buf, this->dtype, this->dest);
|
2008-08-11 22:45:11 +00:00
|
|
|
/* FALLTHROUGH */
|
2009-01-08 22:33:54 +00:00
|
|
|
case NWCW_CLOSE: /* Cancel */ delete this; break;
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnMouseLoop()
|
|
|
|
{
|
2009-01-08 22:33:54 +00:00
|
|
|
this->HandleEditBox(NWCW_TEXTBOX);
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual EventState OnKeyPress(uint16 key, uint16 keycode)
|
|
|
|
{
|
|
|
|
EventState state = ES_NOT_HANDLED;
|
|
|
|
if (keycode == WKC_TAB) {
|
|
|
|
ChatTabCompletion();
|
2009-03-31 00:57:21 +00:00
|
|
|
state = ES_HANDLED;
|
2008-08-11 22:45:11 +00:00
|
|
|
} else {
|
|
|
|
_chat_tab_completion_active = false;
|
2009-01-08 22:33:54 +00:00
|
|
|
switch (this->HandleEditBoxKey(NWCW_TEXTBOX, key, keycode, state)) {
|
2008-09-14 10:30:31 +00:00
|
|
|
default: NOT_REACHED();
|
2008-10-25 19:59:11 +00:00
|
|
|
case HEBR_EDITING: {
|
2008-09-14 10:30:31 +00:00
|
|
|
Window *osk = FindWindowById(WC_OSK, 0);
|
2009-09-30 21:07:54 +00:00
|
|
|
if (osk != NULL && osk->parent == this) osk->InvalidateData();
|
2008-09-14 10:30:31 +00:00
|
|
|
} break;
|
2008-10-25 19:59:11 +00:00
|
|
|
case HEBR_CONFIRM:
|
2008-08-11 22:45:11 +00:00
|
|
|
SendChat(this->text.buf, this->dtype, this->dest);
|
|
|
|
/* FALLTHROUGH */
|
2008-10-25 19:59:11 +00:00
|
|
|
case HEBR_CANCEL: delete this; break;
|
2009-02-09 01:22:29 +00:00
|
|
|
case HEBR_NOT_FOCUSED: break;
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
2009-01-03 13:59:05 +00:00
|
|
|
|
|
|
|
virtual void OnOpenOSKWindow(int wid)
|
|
|
|
{
|
2009-09-25 18:15:41 +00:00
|
|
|
ShowOnScreenKeyboard(this, wid, NWCW_CLOSE, NWCW_SENDBUTTON);
|
2009-01-03 13:59:05 +00:00
|
|
|
}
|
2009-10-07 17:36:33 +00:00
|
|
|
|
|
|
|
virtual void OnInvalidateData(int data)
|
|
|
|
{
|
|
|
|
if (data == this->dest) delete this;
|
|
|
|
}
|
2008-08-11 22:45:11 +00:00
|
|
|
};
|
|
|
|
|
2009-05-05 20:03:57 +00:00
|
|
|
static const NWidgetPart _nested_chat_window_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_GREY, NWCW_CLOSE),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, NWCW_BACKGROUND),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-09-25 18:15:41 +00:00
|
|
|
NWidget(WWT_TEXT, COLOUR_GREY, NWCW_DESTINATION), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NULL, STR_NULL),
|
|
|
|
NWidget(WWT_EDITBOX, COLOUR_GREY, NWCW_TEXTBOX), SetMinimalSize(100, 12), SetPadding(1, 0, 1, 0), SetResize(1, 0),
|
|
|
|
SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
|
2009-08-05 17:59:21 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, NWCW_SENDBUTTON), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NETWORK_CHAT_SEND, STR_NULL),
|
2009-05-05 20:03:57 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2009-05-05 20:33:05 +00:00
|
|
|
static const WindowDesc _chat_window_desc(
|
2009-11-28 13:54:28 +00:00
|
|
|
WDP_MANUAL, WDP_MANUAL, 640, 14, // x, y, width, height
|
2009-05-05 20:33:05 +00:00
|
|
|
WC_SEND_NETWORK_MSG, WC_NONE,
|
2009-11-24 17:28:29 +00:00
|
|
|
0,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_chat_window_widgets, lengthof(_nested_chat_window_widgets)
|
2009-05-05 20:33:05 +00:00
|
|
|
);
|
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
void ShowNetworkChatQueryWindow(DestType type, int dest)
|
|
|
|
{
|
2009-10-07 17:36:33 +00:00
|
|
|
DeleteWindowByClass(WC_SEND_NETWORK_MSG);
|
|
|
|
new NetworkChatWindow(&_chat_window_desc, type, dest);
|
2008-08-11 22:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ENABLE_NETWORK */
|