/******************************************************************************\
* GTK-NoCSD                                                                    *
* Copyright (C) 2025-2026 MorsMortium                                          *
* This program 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 3 of the License, or            *
* (at your option) any later version.                                          *
*                                                                              *
* This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.        *
\******************************************************************************/

// Simple example app for all 4 types of windows.

// gcc Test.c -o Test $(pkg-config --cflags --libs gtk4)
// gcc Test.c -o Test -DUSE_ADWAITA $(pkg-config --cflags --libs libadwaita-1)
// gcc Test.c -o Test $(pkg-config --cflags --libs gtk+-3.0)
// gcc Test.c -o Test -DUSE_HANDY $(pkg-config --cflags --libs libhandy-1)

#include <gtk/gtk.h>
#include <stdbool.h>

#ifdef USE_ADWAITA
#include <adwaita.h>
#endif
#ifdef USE_HANDY
#include <handy.h>
#endif

static gboolean TestAddButton(gpointer Data) {
	// Add a button to the header bar

	// Create button
	GtkWidget *Button = gtk_button_new_with_label("Button");
	gtk_widget_set_visible(Button, true);

	// Add it per type
#ifdef USE_ADWAITA
	AdwHeaderBar *Header = ADW_HEADER_BAR(Data);
	adw_header_bar_pack_start(Header, Button);
#elif USE_HANDY
	HdyHeaderBar *Header = HDY_HEADER_BAR(Data);
	hdy_header_bar_pack_start(Header, Button);
#else
	GtkHeaderBar *Header = GTK_HEADER_BAR(Data);
	gtk_header_bar_pack_start(Header, Button);
#endif

	return G_SOURCE_REMOVE;
}

static void TestActivate(GtkApplication *Application, gpointer Data) {
	// Create window, display it and set up the button adding function

	// Create windows with header bars
#ifdef USE_ADWAITA
	GtkWidget *Window = GTK_WIDGET(adw_application_window_new(Application));
	GtkWidget *Header = adw_header_bar_new();
	adw_application_window_set_content(ADW_APPLICATION_WINDOW(Window), Header);
#elif USE_HANDY
	GtkWidget *Window = GTK_WIDGET(hdy_application_window_new());
	gtk_window_set_application(GTK_WINDOW(Window), Application);
	GtkWidget *Header = hdy_header_bar_new();
	hdy_header_bar_set_decoration_layout(HDY_HEADER_BAR(Header),
		"menu:minimize,maximize,close");
	hdy_header_bar_set_show_close_button(HDY_HEADER_BAR(Header), true);
	gtk_container_add(GTK_CONTAINER(Window), Header);
#else
	GtkWidget *Window = gtk_application_window_new(Application);
	GtkWidget *Header = gtk_header_bar_new();
	gtk_header_bar_set_decoration_layout(GTK_HEADER_BAR(Header),
		"menu:minimize,maximize,close");
	gtk_window_set_titlebar(GTK_WINDOW(Window), Header);
#if !GTK_CHECK_VERSION(4, 0, 0)
	gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(Header), true);
#endif
#endif

	// Set up common window and header bar properties
	gtk_widget_set_vexpand(Header, true);
	gtk_widget_set_valign(Header, GTK_ALIGN_START);
	gtk_widget_set_visible(Header, true);
	gtk_window_set_default_size(GTK_WINDOW(Window), 480, 480);

	// Add button after 5 seconds, show window
	g_timeout_add(5000, TestAddButton, Header);
	gtk_window_present(GTK_WINDOW(Window));
}

int main(int argc, char **argv) {
	// Create application and set up window creating function

#ifdef USE_ADWAITA
	adw_init();
	AdwApplication *Application = adw_application_new(
		"org.TheWorldMachine.GTK_NoCSD", G_APPLICATION_DEFAULT_FLAGS);
#else
	GtkApplication *Application = gtk_application_new(
		"org.TheWorldMachine.GTK_NoCSD", G_APPLICATION_DEFAULT_FLAGS);
#endif

	g_signal_connect(Application, "activate", G_CALLBACK(TestActivate), NULL);
	int Status = g_application_run(G_APPLICATION(Application), argc, argv);
	g_object_unref(Application);

	return Status;
}
