Building a gtk application

I often see references to how this or that is implemented in qt, but gtk is good too. I write in C and don't want to learn C ++ standards every three years to be on top of it. A simple sishechki is enough for me and to make graphic software, the choice fell on gtk.



I use the documentation for it in the devhelp program. So, let's begin.



The first thing to do is to create an application that will be visible on the dbus bus.



GtkApplication *app;

static void app_activate_cb ( GtkApplication *app, gpointer user_data ) {
}

int main ( int argc, char **argv ) {
	app = gtk_application_new ( "com.xverizex.configurator", G_APPLICATION_FLAGS_NONE );
	g_application_register ( ( GApplication * ) app, NULL, NULL );
	g_signal_connect ( app, "activate", G_CALLBACK ( app_activate_cb ), NULL );
	return g_application_run ( ( GApplication * ) app, argc, argv );
}


After the application has been activated, you need to fill it with widgets. I create a structure in this file that will store all the widgets that will change in appearance if you specify a different color scheme.



Here is the structure:



struct widgets {
	GtkWidget *main_window;
	GtkWidget *notebook;
	GtkWidget *tree_view_sources;
	GtkWidget *label_tab_sources;
	GtkWidget *header_bar;
	GtkWidget *search_entry;
	GtkWidget *button_add_item;
	GtkWidget *tree_view_reactions;
	GtkWidget *label_tab_reactions;
	GtkWidget *window_add_source;
        ...
        GtkWidget *STUBS;


at the end I create a special variable called STUBS to which I do not assign anything. There are a lot of widgets in the application and you need some way to give the desired widgets the desired name. This is how I do it.



static void set_theme_name ( const char *name ) {
	struct widgets **p = ( struct widgets ** ) &w;
	for ( int i = 0; p[i] != NULL; i++ ) {
		gtk_widget_set_name ( ( GtkWidget * ) p[i], name );
	}
	char buf[255];
	snprintf ( buf, 255, "%s_info", name );
	gtk_widget_set_name ( w.label_info_db_settings, buf );
	gtk_widget_set_name ( w.label_info_mail_settings, buf );
}


for those widgets that still need a special design, I already assign them separate names. In this function, I go through all the widgets and give them a theme name.

The following code is used to create the menu.



static void create_actions ( void ) {
	const GActionEntry entries[] = {
		{ "quit", action_activate_quit },
		{ "settings", action_settings },
		{ "apply_settings", action_activate_apply_settings },
		{ "select_light_theme", action_activate_select_light_theme },
		{ "select_dark_theme", action_activate_select_dark_theme },
		{ "select_standard_theme", action_activate_select_standard_theme }
	};
	g_action_map_add_action_entries ( G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), NULL );
}


And then we assign these commands to the menu.



	pop_menu = g_menu_new ( );
	GMenu *menu_app = g_menu_new ( );
	GMenu *menu_themes = g_menu_new ( );
	g_menu_append ( menu_app, "", "app.settings" );
	g_menu_append ( menu_app, "", "app.apply_settings" );
	g_menu_append ( menu_themes, " ", "app.select_light_theme" );
	g_menu_append ( menu_themes, " ", "app.select_dark_theme" );
	g_menu_append ( menu_themes, " ", "app.select_standard_theme" );
	g_menu_append_submenu ( menu_app, " ", ( GMenuModel * ) menu_themes );
	g_menu_append ( menu_app, "", "app.quit" );

	gtk_application_set_app_menu ( app, ( GMenuModel * ) menu_app );


As a result, after peeing this application, it turns into something wonderful and beautiful.



image



All Articles