widget-factory: port to GtkApplication and GtkHeaderBar

This commit is contained in:
William Jon McCann 2013-11-04 16:49:05 -05:00
parent 1fd74de214
commit 42acaac7d6
2 changed files with 166 additions and 530 deletions

View File

@ -24,19 +24,38 @@
#include <gtk/gtk.h>
static void
dark_toggled (GtkCheckMenuItem *item, gpointer data)
activate_toggle (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
gboolean dark;
GVariant *state;
dark = gtk_check_menu_item_get_active (item);
g_object_set (gtk_settings_get_default (),
"gtk-application-prefer-dark-theme", dark,
NULL);
state = g_action_get_state (G_ACTION (action));
g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
g_variant_unref (state);
}
static void
show_about (GtkMenuItem *item, GtkWidget *window)
change_theme_state (GSimpleAction *action,
GVariant *state,
gpointer user_data)
{
GtkSettings *settings = gtk_settings_get_default ();
g_object_set (G_OBJECT (settings),
"gtk-application-prefer-dark-theme",
g_variant_get_boolean (state),
NULL);
g_simple_action_set_state (action, state);
}
static void
activate_about (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkWidget *window = user_data;
GdkPixbuf *pixbuf;
const gchar *authors[] = {
"Andrea Cimitan",
@ -66,16 +85,24 @@ show_about (GtkMenuItem *item, GtkWidget *window)
}
static void
on_page_toggled (GtkToggleButton *button,
GtkNotebook *pages)
activate_quit (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
gint page;
GtkApplication *app = user_data;
GtkWidget *win;
GList *list, *next;
if (!gtk_toggle_button_get_active (button))
return;
list = gtk_application_get_windows (app);
while (list)
{
win = list->data;
next = list->next;
page = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "page"));
gtk_notebook_set_current_page (pages, page);
gtk_widget_destroy (GTK_WIDGET (win));
list = next;
}
}
static void
@ -114,58 +141,78 @@ pulse_it (GtkWidget *widget)
return TRUE;
}
int
main (int argc, char *argv[])
static void
startup (GApplication *app)
{
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *widget;
GtkWidget *notebook;
gboolean dark = FALSE;
GtkAdjustment *adj;
gtk_init (&argc, &argv);
if (argc > 1 && (g_strcmp0 (argv[1], "--dark") == 0))
dark = TRUE;
GMenuModel *appmenu;
builder = gtk_builder_new ();
gtk_builder_add_from_resource (builder, "/ui/widget-factory.ui", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
gtk_builder_connect_signals (builder, NULL);
appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
widget = (GtkWidget*) gtk_builder_get_object (builder, "progressbar3");
gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu);
g_object_unref (builder);
}
static void
activate (GApplication *app)
{
GtkBuilder *builder;
GtkWindow *window;
GtkWidget *widget;
GtkAdjustment *adj;
static GActionEntry win_entries[] = {
{ "dark", activate_toggle, NULL, "false", change_theme_state }
};
builder = gtk_builder_new ();
gtk_builder_add_from_resource (builder, "/ui/widget-factory.ui", NULL);
window = (GtkWindow *)gtk_builder_get_object (builder, "window");
gtk_application_add_window (GTK_APPLICATION (app), window);
g_action_map_add_action_entries (G_ACTION_MAP (window),
win_entries, G_N_ELEMENTS (win_entries),
window);
widget = (GtkWidget *)gtk_builder_get_object (builder, "progressbar3");
g_timeout_add (250, (GSourceFunc)pulse_it, widget);
widget = (GtkWidget*) gtk_builder_get_object (builder, "darkmenuitem");
g_signal_connect (widget, "toggled", G_CALLBACK (dark_toggled), NULL);
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), dark);
notebook = (GtkWidget*) gtk_builder_get_object (builder, "toplevel_notebook");
widget = (GtkWidget*) gtk_builder_get_object (builder, "togglepage1");
g_object_set_data (G_OBJECT (widget), "page", GINT_TO_POINTER (0));
g_signal_connect (widget, "toggled", G_CALLBACK (on_page_toggled), notebook);
widget = (GtkWidget*) gtk_builder_get_object (builder, "togglepage2");
g_object_set_data (G_OBJECT (widget), "page", GINT_TO_POINTER (1));
g_signal_connect (widget, "toggled", G_CALLBACK (on_page_toggled), notebook);
widget = (GtkWidget*) gtk_builder_get_object (builder, "aboutmenuitem");
g_signal_connect (widget, "activate", G_CALLBACK (show_about), window);
widget = (GtkWidget*) gtk_builder_get_object (builder, "page2dismiss");
widget = (GtkWidget *)gtk_builder_get_object (builder, "page2dismiss");
g_signal_connect (widget, "clicked", G_CALLBACK (dismiss), NULL);
widget = (GtkWidget*) gtk_builder_get_object (builder, "page2note");
widget = (GtkWidget *)gtk_builder_get_object (builder, "page2note");
adj = (GtkAdjustment *) gtk_builder_get_object (builder, "adjustment2");
g_signal_connect (adj, "value-changed",
G_CALLBACK (spin_value_changed), widget);
g_signal_connect (adj, "value-changed", G_CALLBACK (spin_value_changed), widget);
g_object_unref (G_OBJECT (builder));
gtk_widget_show_all (GTK_WIDGET (window));
gtk_widget_show (window);
gtk_main ();
g_object_unref (builder);
}
int
main (int argc, char *argv[])
{
GtkApplication *app;
static GActionEntry app_entries[] = {
{ "about", activate_about, NULL, NULL, NULL },
{ "quit", activate_quit, NULL, NULL, NULL },
};
gtk_init (&argc, &argv);
app = gtk_application_new ("org.gtk.WidgetFactory", 0);
g_action_map_add_action_entries (G_ACTION_MAP (app),
app_entries, G_N_ELEMENTS (app_entries),
app);
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
g_application_run (G_APPLICATION (app), argc, argv);
return 0;
}

View File

@ -1,6 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<menu id="appmenu">
<section>
<item>
<attribute name="label" translatable="yes">About</attribute>
<attribute name="action">app.about</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="action">app.quit</attribute>
<attribute name="accel">&lt;Primary&gt;q</attribute>
</item>
</section>
</menu>
<menu id='gear_menu'>
<section>
<item>
<attribute name='label' translatable='yes'>Dark Theme</attribute>
<attribute name='action'>win.dark</attribute>
</item>
</section>
</menu>
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">100</property>
<property name="value">50</property>
@ -72,495 +95,53 @@ Suspendisse feugiat quam quis dolor accumsan cursus. </property>
Spanish
Uyghur</property>
</object>
<object class="GtkWindow" id="window">
<object class="GtkApplicationWindow" id="window">
<property name="can_focus">False</property>
<property name="title">GTK+ Widget Factory</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<signal name="delete-event" handler="gtk_false" swapped="no"/>
<child type="titlebar">
<object class="GtkHeaderBar" id="headerbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="show-close-button">True</property>
<child type="title">
<object class="GtkStackSwitcher" id="stack_switcher">
<property name="visible">True</property>
<property name="stack">toplevel_stack</property>
</object>
</child>
<child>
<object class="GtkMenuButton" id="gear_menu_button">
<property name="visible">True</property>
<property name="valign">center</property>
<property name="can_focus">False</property>
<property name="menu_model">gear_menu</property>
<style>
<class name="image-button"/>
</style>
<child>
<object class="GtkImage" id="gear_image">
<property name="visible">True</property>
<property name="icon_size">1</property>
<property name="icon_name">emblem-system-symbolic</property>
</object>
</child>
</object>
<packing>
<property name="pack_type">end</property>
</packing>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkMenuBar" id="menubar1">
<object class="GtkStack" id="toplevel_stack">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">_File</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem101">
<property name="label" translatable="yes">_New</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem102">
<property name="label" translatable="yes">_Open</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem103">
<property name="label" translatable="yes">_Save</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem104">
<property name="label" translatable="yes">Save _As</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem105">
<property name="label" translatable="yes">_Quit</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
<signal name="activate" handler="gtk_main_quit" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">_Edit</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem106">
<property name="label" translatable="yes">Cu_t</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem107">
<property name="label" translatable="yes">_Copy</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem108">
<property name="label" translatable="yes">_Paste</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem109">
<property name="label" translatable="yes">_Delete</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="togglesmenuitem">
<property name="label">Checks &amp; Radios</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
<property name="use_stock">False</property>
<child type="submenu">
<object class="GtkMenu" id="togglessubmenu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkCheckMenuItem" id="checkmenuitem1">
<property name="label">_Check</property>
<property name="active">True</property>
<property name="visible">True</property>
<property name="sensitive">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="checkmenuitem2">
<property name="label">_Check</property>
<property name="active">True</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="checkmenuitem3">
<property name="label">_Check</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="inconsistent">True</property>
<property name="sensitive">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="checkmenuitem4">
<property name="label">_Check</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="sensitive">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="checkmenuitem5">
<property name="label">_Check</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="checkmenuitem6">
<property name="label">_Check</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="inconsistent">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem">
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="radiomenuitem1">
<property name="label">_Radio</property>
<property name="active">True</property>
<property name="visible">True</property>
<property name="sensitive">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="radiomenuitem2">
<property name="label">_Radio</property>
<property name="active">True</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="radiomenuitem3">
<property name="label">_Radio</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="inconsistent">True</property>
<property name="sensitive">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="radiomenuitem4">
<property name="label">_Radio</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="sensitive">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="radiomenuitem5">
<property name="label">_Radio</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkRadioMenuItem" id="radiomenuitem6">
<property name="label">_Radio</property>
<property name="active">False</property>
<property name="visible">True</property>
<property name="inconsistent">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">_View</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="view-menu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkCheckMenuItem" id="darkmenuitem">
<property name="label">_Dark theme</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="aboutmenuitem">
<property name="label" translatable="yes">_About</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkToolbar" id="toolbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<style>
<class name="primary-toolbar"/>
</style>
<child>
<object class="GtkToolButton" id="toolbutton2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">_Save</property>
<property name="use_underline">True</property>
<property name="icon-name">document-save</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">Open</property>
<property name="use_underline">True</property>
<property name="icon-name">document-open</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkSeparatorToolItem" id="toolbutton3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">Find</property>
<property name="use_underline">True</property>
<property name="icon_name">edit-find</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton4a">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<style>
<class name="raised"/>
</style>
<child>
<object class="GtkBox" id="extrabox2">
<property name="visible">True</property>
<property name="orientation">horizontal</property>
<property name="homogeneous">True</property>
<property name="halign">center</property>
<property name="hexpand">True</property>
<style>
<class name="linked"/>
</style>
<child>
<object class="GtkRadioButton" id="togglepage1">
<property name="visible">True</property>
<property name="label">Page 1</property>
<property name="draw-indicator">False</property>
</object>
</child>
<child>
<object class="GtkRadioButton" id="togglepage2">
<property name="visible">True</property>
<property name="label">Page 2</property>
<property name="draw-indicator">False</property>
<property name="group">togglepage1</property>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<child>
<object class="GtkSearchEntry" id="entry3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
<property name="placeholder-text" translatable="yes">search...</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkNotebook" id="toplevel_notebook">
<property name="visible">True</property>
<property name="show-tabs">False</property>
<child>
<object class="GtkBox" id="page1">
<property name="visible">True</property>
@ -2159,6 +1740,10 @@ Suspendisse feugiat quam quis dolor accumsan cursus. </property>
</packing>
</child>
</object>
<packing>
<property name="name">page1</property>
<property name="title" translatable="yes">Page 1</property>
</packing>
</child>
<child>
<object class="GtkOverlay" id="page2">
@ -2317,6 +1902,10 @@ Suspendisse feugiat quam quis dolor accumsan cursus. </property>
</object>
</child>
</object>
<packing>
<property name="name">page2</property>
<property name="title" translatable="yes">Page 2</property>
</packing>
</child>
</object>
</child>