From 0016611ea6b66388e7068becdb0d71a0d8d14169 Mon Sep 17 00:00:00 2001
From: Yona-TYT <yonatan.el.amigo@gmail.com>
Date: Sun, 1 Jan 2023 00:31:47 -0400
Subject: [PATCH] ADD Default theme for paksets

---
 src/simutrans/dataobj/environment.cc |  6 +++++
 src/simutrans/dataobj/environment.h  |  3 +++
 src/simutrans/gui/pakselector.cc     | 13 ++++++++++
 src/simutrans/gui/savegame_frame.cc  | 10 +++++++
 src/simutrans/gui/savegame_frame.h   |  2 ++
 src/simutrans/gui/themeselector.cc   | 39 +++++++++++++++++++++++++++-
 src/simutrans/gui/themeselector.h    |  1 +
 7 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/src/simutrans/dataobj/environment.cc b/src/simutrans/dataobj/environment.cc
index 7935ac6e8..59e5da5df 100644
--- a/src/simutrans/dataobj/environment.cc
+++ b/src/simutrans/dataobj/environment.cc
@@ -178,6 +178,8 @@ sint8 env_t::show_money_message;
 uint8 env_t::gui_player_color_dark = 1;
 uint8 env_t::gui_player_color_bright = 4;
 
+bool env_t::pakset_theme = true;
+
 #ifndef __ANDROID__
 std::string env_t::fontname = FONT_PATH_X "prop.fnt";
 uint8 env_t::fontsize = 11;
@@ -284,6 +286,9 @@ void env_t::init()
 	// maximum speedup set to 1000 (effectively no limit)
 	max_acceleration=50;
 
+	// Activate the gui theme from the pakset
+	//pakset_theme = true;
+
 #ifdef MULTI_THREAD
 	num_threads = dr_get_max_threads();
 #else
@@ -366,6 +371,7 @@ void env_t::rdwr(loadsave_t *file)
 	file->rdwr_bool( hide_with_transparency );
 	file->rdwr_byte( hide_buildings );
 	file->rdwr_bool( hide_trees );
+	file->rdwr_bool(pakset_theme);
 
 	file->rdwr_long( message_flags[0] );
 	file->rdwr_long( message_flags[1] );
diff --git a/src/simutrans/dataobj/environment.h b/src/simutrans/dataobj/environment.h
index b41fd84d3..8db2c347c 100644
--- a/src/simutrans/dataobj/environment.h
+++ b/src/simutrans/dataobj/environment.h
@@ -220,6 +220,9 @@ public:
 	/// cut through the map when following convois?
 	static uint8 follow_convoi_underground;
 
+	/// Activate the gui theme from the pakset
+	static bool pakset_theme;
+
 	///which messages to display where?
 	/**
 	 * message_flags[i] is bitfield, where bit is set if message should be show at location i,
diff --git a/src/simutrans/gui/pakselector.cc b/src/simutrans/gui/pakselector.cc
index 6ccb72219..86515f5dc 100644
--- a/src/simutrans/gui/pakselector.cc
+++ b/src/simutrans/gui/pakselector.cc
@@ -68,6 +68,19 @@ bool pakselector_t::item_action(const char *fullpath)
 	env_t::pak_name = (str_get_filename(fullpath, true)+PATH_SEPARATOR);
 	env_t::default_settings.set_with_private_paks( false );
 
+	if(env_t::pakset_theme){
+		static cbuffer_t pakset_dir;
+		static cbuffer_t addons_dir;
+
+		pakset_dir.clear();
+		pakset_dir.printf("%stheme/themes.tab", env_t::pak_dir.c_str());
+
+		addons_dir.clear();
+		addons_dir.printf("addons/%stheme/themes.tab", env_t::pak_name.c_str());
+
+		gui_theme_t::themes_init(pakset_dir,true,true);
+	}
+
 	return true;
 }
 
diff --git a/src/simutrans/gui/savegame_frame.cc b/src/simutrans/gui/savegame_frame.cc
index e05c882d6..3354738ac 100644
--- a/src/simutrans/gui/savegame_frame.cc
+++ b/src/simutrans/gui/savegame_frame.cc
@@ -89,6 +89,11 @@ savegame_frame_t::savegame_frame_t(const char *suffix, bool only_directories, co
 
 	new_component<gui_fill_t>();
 
+	checkbox.init( button_t::square_automatic, "");
+	checkbox.set_visible(false);
+	checkbox.add_listener( this );
+	bottom_left_frame.add_component( &checkbox );
+
 	add_table(2,1)->set_force_equal_columns(true);
 	savebutton.init( button_t::roundbox | button_t::flexible, "Ok" );
 	savebutton.add_listener( this );
@@ -97,6 +102,8 @@ savegame_frame_t::savegame_frame_t(const char *suffix, bool only_directories, co
 	cancelbutton.init( button_t::roundbox | button_t::flexible, "Cancel" );
 	cancelbutton.add_listener( this );
 	add_component( &cancelbutton );
+
+
 	end_table();
 
 	end_table();
@@ -501,6 +508,9 @@ bool savegame_frame_t::action_triggered(gui_action_creator_t *component, value_t
 		cancel_action(buf);
 		destroy_win(this);
 	}
+	else if(component == &checkbox) {
+		mark_action(buf);
+	}
 	else {
 		// File in list selected
 		//--------------------------
diff --git a/src/simutrans/gui/savegame_frame.h b/src/simutrans/gui/savegame_frame.h
index 372960caf..ffc514e69 100644
--- a/src/simutrans/gui/savegame_frame.h
+++ b/src/simutrans/gui/savegame_frame.h
@@ -94,6 +94,7 @@ protected:
 	gui_textinput_t  input;         //@< Filename input field
 	button_t         savebutton;    //@< Save button
 	button_t         cancelbutton;  //@< Cancel button
+	button_t         checkbox;      //@< Checkbox
 	gui_label_t      fnlabel;       //@< Static file name label
 	gui_aligned_container_t
 	                 top_frame,     //@< Contains input field
@@ -124,6 +125,7 @@ protected:
 	virtual bool cancel_action ( const char * /*fullpath*/ ) { return true; } // Callback for cancel button click
 	virtual bool del_action    ( const char *   fullpath   );                 // Callback for delete button click
 	virtual bool ok_action     ( const char * /*fullpath*/ ) { return true; } // Callback for ok button click
+	virtual bool mark_action   ( const char * /*fullpath*/ ) { return true; } // Callback for checkbox click
 
 	virtual bool check_file         ( const char *filename, const char *suffix );
 
diff --git a/src/simutrans/gui/themeselector.cc b/src/simutrans/gui/themeselector.cc
index ae0b50afe..c70c07e28 100644
--- a/src/simutrans/gui/themeselector.cc
+++ b/src/simutrans/gui/themeselector.cc
@@ -27,6 +27,10 @@ themeselector_t::themeselector_t() :
 	delete_enabled = false;
 	label_enabled  = false;
 
+	checkbox.pressed = env_t::pakset_theme;
+	checkbox.set_text( translator::translate( "Use default theme from pakset" ) );
+	checkbox.set_visible(true);
+	
 	set_name( translator::translate( "Theme selector" ) );
 	fnlabel.set_text_pointer( translator::translate( "Select a theme for display" ) );
 	if( undo.empty() ) {
@@ -56,6 +60,33 @@ bool themeselector_t::item_action(const char *fullpath)
 }
 
 
+bool themeselector_t::mark_action(const char *)
+{
+	cbuffer_t buf;
+	buf.clear();
+	buf.printf("%d",env_t::pakset_theme);
+	dr_copy(buf, buf.len());
+
+	env_t::pakset_theme = !env_t::pakset_theme;
+
+	if(env_t::pakset_theme) {
+		static cbuffer_t pakset_dir;
+		static cbuffer_t addons_dir;
+
+		pakset_dir.clear();
+		pakset_dir.printf("%stheme/themes.tab", env_t::pak_dir.c_str());
+
+		addons_dir.clear();
+		addons_dir.printf("addons/%stheme/themes.tab", env_t::pak_name.c_str());
+
+		item_action(pakset_dir);
+	}
+	else {
+		item_action(undo.c_str());
+	}
+	return true;
+}
+
 
 // Ok button was pressed
 bool themeselector_t::ok_action(const char *)
@@ -113,14 +144,20 @@ void themeselector_t::fill_list()
 		}
 
 		delete[] i.button->get_text(); // free up default allocation.
+
+
 		i.button->set_typ(button_t::roundbox_state | button_t::flexible);
+		if(env_t::pakset_theme) {
+			i.button->disable();
+		}
+		
 		i.button->set_text(i.label->get_text_pointer());
 		i.button->pressed = !strcmp( env_t::default_theme.c_str(), i.info ); // mark current theme
 		i.label->set_text_pointer( NULL ); // remove reference to prevent conflicts at delete[]
 
 		// Get a new label buffer since the original is now owned by i.button.
 		// i.label->set_text_pointer( strdup( get_filename(i.info).c_str() ) );
-
+		
 	}
 
 	if(entries.get_count() <= this->num_sections+1) {
diff --git a/src/simutrans/gui/themeselector.h b/src/simutrans/gui/themeselector.h
index 7c80bf2da..0a25a45d9 100644
--- a/src/simutrans/gui/themeselector.h
+++ b/src/simutrans/gui/themeselector.h
@@ -23,6 +23,7 @@ protected:
 	bool        item_action   ( const char *fullpath ) OVERRIDE;
 	bool        ok_action     ( const char *fullpath ) OVERRIDE;
 	bool        cancel_action ( const char *fullpath ) OVERRIDE;
+	bool        mark_action ( const char *fullpath ) OVERRIDE;
 	const char* get_info      ( const char *fname    ) OVERRIDE;
 	bool        check_file    ( const char *filename, const char *suffix ) OVERRIDE;
 	void        fill_list       ( void ) OVERRIDE;
-- 
2.39.0

