From aa9fdacfea764feaf890151c21b1cc663315dedd Mon Sep 17 00:00:00 2001
From: Yona-TYT <simus.tyt@gmail.com>
Date: Thu, 29 Aug 2024 09:58:55 -0400
Subject: [PATCH] Open chat window from message ticker

---
 src/simutrans/gui/chat_frame.cc |  8 ++++++++
 src/simutrans/gui/chat_frame.h  |  2 ++
 src/simutrans/simmesg.cc        | 12 +++++++-----
 src/simutrans/simmesg.h         |  2 ++
 src/simutrans/simticker.cc      | 29 +++++++++++++++++++++++++++++
 src/simutrans/simticker.h       |  9 +++++++++
 6 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/src/simutrans/gui/chat_frame.cc b/src/simutrans/gui/chat_frame.cc
index 6548ab577..e70b9ee7b 100644
--- a/src/simutrans/gui/chat_frame.cc
+++ b/src/simutrans/gui/chat_frame.cc
@@ -577,3 +577,11 @@ void chat_frame_t::activate_whisper_to(const char* recipient)
 	last_count = 0xFFFFFFFFul; // update upside of infowin_event!
 	reactivate_input = true;
 }
+
+
+void chat_frame_t::open_tab(uint16 tab)
+{
+	tabs.set_active_tab_index(tab);
+	resize(scr_coord(0,0));
+	set_dirty();
+}
diff --git a/src/simutrans/gui/chat_frame.h b/src/simutrans/gui/chat_frame.h
index 9cb3f9a82..91ab864cd 100644
--- a/src/simutrans/gui/chat_frame.h
+++ b/src/simutrans/gui/chat_frame.h
@@ -76,6 +76,8 @@ public:
 
 	// theoretically, map roatation should not happen with netwrok games ...
 	void map_rotate90(sint16 /*new_ysize*/) OVERRIDE { fill_list(); }
+
+	void open_tab(uint16 tab);
 };
 
 #endif
diff --git a/src/simutrans/simmesg.cc b/src/simutrans/simmesg.cc
index 1b3629a20..f6c4644ed 100644
--- a/src/simutrans/simmesg.cc
+++ b/src/simutrans/simmesg.cc
@@ -401,12 +401,14 @@ void chat_message_t::add_chat_message(const char* text, sint8 channel, sint8 sen
 			bool company = channel == world()->get_active_player_nr();
 			if (player != world()->get_active_player()  ||  company) {
 				// so it is not a message sent from us
-				bool show_message = channel == -1; // message for all?
-				show_message |= company; // company message for us?
-				show_message |= recipient  &&  strcmp(recipient, env_t::nickname.c_str()) == 0; // private chat for us?
-				if(show_message) {
+				int show_message = -1;
+				show_message += channel == -1; // message for all?
+				show_message = (company ? 1 : show_message); // company message for us?
+				show_message += recipient  &&  strcmp(recipient, env_t::nickname.c_str()) == 0; // private chat for us?
+				show_message += show_message == 1 && !company; // Increase so that the private message tab is correct
+				if(show_message != -1) {
 					buf.printf("%s: %s", sender_.c_str(), text);
-					ticker::add_msg(buf, koord3d::invalid, PLAYER_FLAG|sender_nr);
+					ticker::add_chat_msg(buf, koord3d::invalid, PLAYER_FLAG|sender_nr, show_message, message_t::chat);
 					env_t::chat_unread_public++;
 					sound_play(sound_desc_t::message_sound, 255, TOOL_SOUND);
 				}
diff --git a/src/simutrans/simmesg.h b/src/simutrans/simmesg.h
index b612ed19c..9d341044a 100644
--- a/src/simutrans/simmesg.h
+++ b/src/simutrans/simmesg.h
@@ -38,6 +38,8 @@ struct message_node_t
 	PIXVAL get_player_color(karte_t*) const;
 
 	void open_msg_window(bool open_as_autoclose) const;
+
+	sint16 tab;
 };
 
 class message_t
diff --git a/src/simutrans/simticker.cc b/src/simutrans/simticker.cc
index f8e2181f2..862900663 100644
--- a/src/simutrans/simticker.cc
+++ b/src/simutrans/simticker.cc
@@ -11,6 +11,7 @@
 #include "tpl/slist_tpl.h"
 #include "utils/simstring.h"
 #include "gui/gui_theme.h"
+#include "gui/chat_frame.h"
 #include "world/simworld.h"
 #include "simmesg.h"
 #include "display/viewport.h"
@@ -118,6 +119,22 @@ void ticker::add_msg(const char* txt, koord3d pos, FLAGGED_PIXVAL color)
 }
 
 
+void ticker::add_chat_msg(const char* txt, koord3d pos, FLAGGED_PIXVAL color, uint16 tab, sint32 type)
+{
+	node n;
+	tstrncpy(n.msg, txt, lengthof(n.msg));
+	n.pos = pos;
+	n.color = color;
+	// set to default values
+	n.type  = type;
+	n.time  = 0;
+	n.image = IMG_EMPTY;
+	n.tab = tab;
+
+	add_msg_node(n);
+}
+
+
 void ticker::update()
 {
 	const int dx = X_DIST;
@@ -213,6 +230,18 @@ void ticker::process_click(int x)
 	if (list.empty()) {
 		return;
 	}
+	for(node & n : list) {
+		if(n.type == message_t::chat) {
+			// pop up for the win
+			chat_frame_t *si = (chat_frame_t*)win_get_magic(magic_chatframe);
+			if (si == NULL) {
+				si = new chat_frame_t();
+				create_win({ 0, 200 }, si, w_info, magic_chatframe);
+			}
+			si->open_tab(n.tab);
+			break;
+		}
+	}
 	clicked = &list.front();
 	if (list.get_count() > 1) {
 		for(node & n : list) {
diff --git a/src/simutrans/simticker.h b/src/simutrans/simticker.h
index 1a5f9de48..08afec21c 100644
--- a/src/simutrans/simticker.h
+++ b/src/simutrans/simticker.h
@@ -30,6 +30,15 @@ namespace ticker
 	 */
 	void add_msg(const char*, koord3d pos, FLAGGED_PIXVAL color);
 
+	/**
+	 * Add a message to the message list
+	 * @param pos    position of the event
+	 * @param color  message color
+	 * @param tab  open windows in this tab
+	 * @param type  message type
+	 */
+	void add_chat_msg(const char*, koord3d pos, FLAGGED_PIXVAL color, uint16 tab, sint32 type);
+
 	/**
 	 * Add a message in message_node_t format
 	 */
-- 
2.46.0

