Make the custom headerbar as MyTitleBar class

This commit is contained in:
daleclack 2024-03-20 19:58:29 +08:00
parent 3af5f3f338
commit 44d32e8b2b
3 changed files with 58 additions and 40 deletions

View File

@ -1,20 +1,64 @@
#include "MyTitleBar.h"
struct _MyTitleBar{
struct _MyTitleBar
{
GObject parent_instance;
GtkWindow *ctrl_window;
GtkWidget *ctrl_window;
GtkWidget *header;
GtkWidget *btn_close, *btn_mini;
};
G_DEFINE_TYPE(MyTitleBar, my_titlebar, G_TYPE_OBJECT)
static void ctrl_win_hide(MyTitleBar *self)
{
gtk_widget_set_visible(self->ctrl_window, FALSE);
}
void my_titlebar_set_window(MyTitleBar *self, GtkWidget *window)
{
gtk_window_set_titlebar(GTK_WINDOW(window), self->header);
self->ctrl_window = window;
}
static void my_titlebar_init(MyTitleBar *self)
{
// Create widgets
self->header = gtk_header_bar_new();
gtk_widget_set_css_classes(self->header, NULL);
gtk_widget_add_css_class(self->header, "mytitlebar");
// 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->header)),
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_signal_connect_swapped(self->btn_close, "clicked", G_CALLBACK(ctrl_win_hide), self);
g_signal_connect_swapped(self->btn_mini, "clicked", G_CALLBACK(ctrl_win_hide), self);
}
static void my_titlebar_class_init(MyTitleBarClass *klass)
{
}
MyTitleBar *my_titlebar_new()
{
return My_TitleBar(g_object_new(my_titlebar_get_type(), NULL));
}

View File

@ -2,4 +2,8 @@
#include <gtk/gtk.h>
G_DECLARE_FINAL_TYPE(MyTitleBar, my_titlebar, My, TitleBar, GObject)
G_DECLARE_FINAL_TYPE(MyTitleBar, my_titlebar, My, TitleBar, GObject)
void my_titlebar_set_window(MyTitleBar *title_bar, GtkWidget *window);
MyTitleBar *my_titlebar_new();

View File

@ -1,52 +1,22 @@
#include "TestWin.h"
#include "MyTitleBar.h"
struct _TestWin
{
GtkWindow parent_instance;
GtkWidget *header;
GtkWidget *btn_close, *btn_mini;
MyTitleBar *header;
};
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)
{
// Initalize window
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);
// Add a custom headerbar
self->header = my_titlebar_new();
my_titlebar_set_window(self->header, GTK_WIDGET(self));
}
static void test_win_class_init(TestWinClass *klass)