Update title bar for text editor

This commit is contained in:
daleclack 2024-03-30 23:04:56 +08:00
parent b81414180b
commit cadb560664
3 changed files with 17 additions and 6 deletions

View File

@ -3,18 +3,20 @@
#include "../json_nlohmann/json.hpp"
#include <cstring>
#include <fstream>
#include "MyTitleBar.h"
using json = nlohmann::json;
struct _TextEditor
{
GtkApplicationWindow parent_instance;
GtkWidget *header;
MyTitleBar *header;
GtkWidget *main_box;
GtkWidget *text_view, *scrolled_win;
GtkTextBuffer *text_buffer;
GtkWidget *menu_btn, *popover;
GtkBuilder *menu_builder, *expander_builder;
// Input keyboard
GtkWidget *expander;
GtkWidget *btns[26];
@ -301,9 +303,9 @@ static void text_editor_init(TextEditor *self)
json_file.close();
// Use headerbar for title and more info
self->header = gtk_header_bar_new();
self->header = my_titlebar_new();
gtk_window_set_title(GTK_WINDOW(self), "Text editor");
gtk_window_set_titlebar(GTK_WINDOW(self), self->header);
my_titlebar_set_window(self->header, self);
gtk_window_set_default_size(GTK_WINDOW(self), width, height);
// Add Actions for menu
@ -323,8 +325,7 @@ static void text_editor_init(TextEditor *self)
// Add menu button
self->menu_btn = gtk_menu_button_new();
gtk_menu_button_set_icon_name(GTK_MENU_BUTTON(self->menu_btn), "open-menu");
gtk_header_bar_pack_end(GTK_HEADER_BAR(self->header), self->menu_btn);
gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(self->header), TRUE);
my_titlebar_pack_end(self->header, self->menu_btn);
// Add a menu
self->menu_builder = gtk_builder_new_from_resource("/org/gtk/daleclack/text_menu.xml");
@ -341,7 +342,7 @@ static void text_editor_init(TextEditor *self)
// Add Search Button
self->btnsearch = gtk_toggle_button_new();
gtk_button_set_icon_name(GTK_BUTTON(self->btnsearch), "find");
gtk_header_bar_pack_end(GTK_HEADER_BAR(self->header), self->btnsearch);
my_titlebar_pack_end(self->header, self->btnsearch);
// Add a search bar
self->search_bar = gtk_search_bar_new();

View File

@ -26,6 +26,11 @@ void _my_titlebar_set_window(MyTitleBar *self, GtkWidget *window)
self->ctrl_window = window;
}
void _my_titlebar_pack_end(MyTitleBar *self, GtkWidget *widget)
{
gtk_header_bar_pack_end(GTK_HEADER_BAR(self->header), widget);
}
static void my_titlebar_init(MyTitleBar *self)
{
// Create widgets

View File

@ -2,11 +2,16 @@
#include <gtk/gtk.h>
// For any style of pointer
#define my_titlebar_set_window(title_bar, window) \
_my_titlebar_set_window(My_TitleBar(title_bar), GTK_WIDGET(window))
#define my_titlebar_pack_end(title_bar, widget) \
_my_titlebar_pack_end(My_TitleBar(title_bar), GTK_WIDGET(widget))
G_DECLARE_FINAL_TYPE(MyTitleBar, my_titlebar, My, TitleBar, GObject)
void _my_titlebar_set_window(MyTitleBar *title_bar, GtkWidget *window);
void _my_titlebar_pack_end(MyTitleBar *title_bar, GtkWidget *widget);
MyTitleBar *my_titlebar_new();