diff --git a/Gtk4/gtk160_wayland_test/CMakeLists.txt b/Gtk4/gtk160_wayland_test/CMakeLists.txt index e5a1163..7c94b8a 100644 --- a/Gtk4/gtk160_wayland_test/CMakeLists.txt +++ b/Gtk4/gtk160_wayland_test/CMakeLists.txt @@ -26,7 +26,7 @@ link_directories (${GTK_LIBRARY_DIRS}) # set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES) #Source files -set(SOURCE_FILE src/main.cpp src/MainWin.cpp) +set(SOURCE_FILE src/main.cpp src/MainWin.cpp src/TestWin.cpp) #Compile Resource diff --git a/Gtk4/gtk160_wayland_test/res/style.css b/Gtk4/gtk160_wayland_test/res/style.css index df4f5ec..f937b6e 100644 --- a/Gtk4/gtk160_wayland_test/res/style.css +++ b/Gtk4/gtk160_wayland_test/res/style.css @@ -1,12 +1,12 @@ /* A slim headerbar style for widgets */ -.titlebar { +.mytitlebar { min-height: 10px; margin: 0px; } .ctrl_btn { min-height: 8px; - min-width: 8px; + min-width: 13px; margin: 0px; font-weight: 900; border-radius: 100%; diff --git a/Gtk4/gtk160_wayland_test/src/MainWin.cpp b/Gtk4/gtk160_wayland_test/src/MainWin.cpp index 3a4f022..24c59a6 100644 --- a/Gtk4/gtk160_wayland_test/src/MainWin.cpp +++ b/Gtk4/gtk160_wayland_test/src/MainWin.cpp @@ -1 +1,65 @@ -#include "MainWin.h" \ No newline at end of file +#include "MainWin.h" +#include "TestWin.h" + +struct _MainWin +{ + GtkApplicationWindow parent_instance; + GtkWidget *main_box, *btn_box; + GtkWidget *btn_test; + GtkWidget *label_space; + TestWin *test_win; +}; + +G_DEFINE_TYPE(MainWin, main_win, GTK_TYPE_APPLICATION_WINDOW) + +static void btntest_clicked(GtkWidget *btn, MainWin *self) +{ + gboolean test_win_showed = gtk_widget_get_visible(GTK_WIDGET(self->test_win)); + if (test_win_showed) + { + gtk_widget_set_visible(GTK_WIDGET(self->test_win), FALSE); + } + else + { + gtk_widget_set_visible(GTK_WIDGET(self->test_win), TRUE); + gtk_window_present(GTK_WINDOW(self->test_win)); + } +} + +static void main_win_init(MainWin *self) +{ + // Initalize window + gtk_window_set_default_size(GTK_WINDOW(self), 800, 450); + gtk_window_set_default_icon_name("org.gtk.daleclack"); + gtk_window_set_title(GTK_WINDOW(self), "Wayland window test"); + + // Create child window + self->test_win = test_win_new(GTK_WINDOW(self)); + gtk_widget_set_visible(GTK_WIDGET(self->test_win), FALSE); + + // Create Box and buttons + self->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); + self->btn_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5); + self->btn_test = gtk_button_new_with_label("Test"); + self->label_space = gtk_label_new(" "); + g_signal_connect(self->btn_test, "clicked", G_CALLBACK(btntest_clicked), self); + + // Pack widgets + gtk_widget_set_hexpand(self->label_space, TRUE); + gtk_widget_set_vexpand(self->label_space, TRUE); + gtk_widget_set_halign(self->btn_test, GTK_ALIGN_START); + gtk_box_append(GTK_BOX(self->btn_box), self->btn_test); + gtk_box_append(GTK_BOX(self->main_box), self->label_space); + gtk_box_append(GTK_BOX(self->main_box), self->btn_box); + gtk_window_set_child(GTK_WINDOW(self), self->main_box); +} + +static void main_win_class_init(MainWinClass *self) +{ +} + +MainWin *main_win_new(GtkApplication *app) +{ + return Main_Win(g_object_new(main_win_get_type(), + "application", app, NULL)); +} \ No newline at end of file diff --git a/Gtk4/gtk160_wayland_test/src/MainWin.h b/Gtk4/gtk160_wayland_test/src/MainWin.h index 03c286d..39b3428 100644 --- a/Gtk4/gtk160_wayland_test/src/MainWin.h +++ b/Gtk4/gtk160_wayland_test/src/MainWin.h @@ -1,3 +1,7 @@ #pragma once -#include \ No newline at end of file +#include + +G_DECLARE_FINAL_TYPE(MainWin, main_win, Main, Win, GtkApplicationWindow) + +MainWin *main_win_new(GtkApplication *app); \ No newline at end of file diff --git a/Gtk4/gtk160_wayland_test/src/TestWin.cpp b/Gtk4/gtk160_wayland_test/src/TestWin.cpp new file mode 100644 index 0000000..98aa86a --- /dev/null +++ b/Gtk4/gtk160_wayland_test/src/TestWin.cpp @@ -0,0 +1,60 @@ +#include "TestWin.h" + +struct _TestWin +{ + GtkWindow parent_instance; + GtkWidget *header; + GtkWidget *btn_close, *btn_mini; +}; + +G_DEFINE_TYPE(TestWin, test_win, GTK_TYPE_WINDOW) + +static void test_win_hide(TestWin *self) +{ + gtk_widget_set_visible(GTK_WIDGET(self), FALSE); +} + +static void test_win_init(TestWin *self) +{ + gtk_window_set_title(GTK_WINDOW(self), "Test Window 1"); + // Create widgets + self->header = gtk_header_bar_new(); + gtk_widget_set_css_classes(self->header, NULL); + gtk_widget_add_css_class(self->header, "mytitlebar"); + gtk_window_set_titlebar(GTK_WINDOW(self), self->header); + + // Initalize headerbar + gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(self->header), FALSE); + + // Add a close button + self->btn_close = gtk_button_new_with_label("\u00d7"); + gtk_widget_set_css_classes(self->btn_close, NULL); + gtk_widget_add_css_class(self->btn_close, "ctrl_btn"); + gtk_header_bar_pack_start(GTK_HEADER_BAR(self->header), self->btn_close); + + // Add a minimize button + self->btn_mini = gtk_button_new_with_label("-"); + gtk_widget_set_css_classes(self->btn_mini, NULL); + gtk_widget_add_css_class(self->btn_mini, "ctrl_btn"); + gtk_header_bar_pack_start(GTK_HEADER_BAR(self->header), self->btn_mini); + + // Add style for headerbar + GtkCssProvider *provider = gtk_css_provider_new(); + gtk_css_provider_load_from_resource(provider, "/org/gtk/daleclack/style.css"); + gtk_style_context_add_provider_for_display(gtk_widget_get_display(GTK_WIDGET(self)), + GTK_STYLE_PROVIDER(provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + + g_signal_connect_swapped(self->btn_close, "clicked", G_CALLBACK(test_win_hide), self); + g_signal_connect_swapped(self->btn_mini, "clicked", G_CALLBACK(test_win_hide), self); +} + +static void test_win_class_init(TestWinClass *klass) +{ +} + +TestWin *test_win_new(GtkWindow *parent) +{ + return Test_Win(g_object_new(test_win_get_type(), + "transient-for", parent, NULL)); +} diff --git a/Gtk4/gtk160_wayland_test/src/TestWin.h b/Gtk4/gtk160_wayland_test/src/TestWin.h new file mode 100644 index 0000000..0f5127f --- /dev/null +++ b/Gtk4/gtk160_wayland_test/src/TestWin.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +G_DECLARE_FINAL_TYPE(TestWin, test_win, Test, Win, GtkWindow) + +TestWin *test_win_new(GtkWindow *parent); \ No newline at end of file diff --git a/Gtk4/gtk160_wayland_test/src/main.cpp b/Gtk4/gtk160_wayland_test/src/main.cpp index 2fc8bad..a8a4383 100644 --- a/Gtk4/gtk160_wayland_test/src/main.cpp +++ b/Gtk4/gtk160_wayland_test/src/main.cpp @@ -6,41 +6,8 @@ using json = nlohmann::json; static void gtkmain(GtkApplication *app, gpointer user_data) { - GtkWidget *window, *header; - GtkWidget *btn_close, *btn_mini; - GtkWidget *img_close, *img_mini; - - // Create widgets - window = gtk_application_window_new(app); - header = gtk_header_bar_new(); - - const char *title_classes[] = {"titlebar", ""}; - gtk_widget_set_css_classes(header, title_classes); - gtk_window_set_titlebar(GTK_WINDOW(window), header); - - // Initalize headerbar - gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(header), FALSE); - const char *btn_classes[] = {"ctrl_btn",""}; - - // Add a close button - btn_close = gtk_button_new_with_label("\u00d7"); - gtk_widget_set_css_classes(btn_close, btn_classes); - gtk_header_bar_pack_start(GTK_HEADER_BAR(header), btn_close); - - // Add a minimize button - btn_mini = gtk_button_new_with_label("-"); - gtk_widget_set_css_classes(btn_mini, btn_classes); - gtk_header_bar_pack_start(GTK_HEADER_BAR(header), btn_mini); - - // Add style for headerbar - GtkCssProvider *provider = gtk_css_provider_new(); - gtk_css_provider_load_from_resource(provider, "/org/gtk/daleclack/style.css"); - gtk_style_context_add_provider_for_display(gtk_widget_get_display(window), - GTK_STYLE_PROVIDER(provider), - GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - - g_signal_connect_swapped(btn_close, "clicked", G_CALLBACK(gtk_window_close), window); - g_signal_connect_swapped(btn_mini, "clicked", G_CALLBACK(gtk_window_minimize), window); + // Create window + MainWin *window = main_win_new(app); // show the window gtk_window_present(GTK_WINDOW(window)); }