[virt-tools-list] [PATCH virt-viewer] window: Factor out common code for toolbar items
Pavel Grunt
pgrunt at redhat.com
Tue Jun 28 15:10:50 UTC 2016
Create toolbar widget in the loop
---
src/virt-viewer-window.c | 121 ++++++++++++++++++++++++++++++++---------------
1 file changed, 83 insertions(+), 38 deletions(-)
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index 1ebb423..b276ae8 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -1062,56 +1062,101 @@ virt_viewer_window_menu_help_about(GtkWidget *menu G_GNUC_UNUSED,
g_object_unref(G_OBJECT(about));
}
+typedef struct {
+ GtkWidget *icon;
+ const gchar *icon_name;
+ const gchar *label;
+ const gchar *tooltip;
+ const gboolean sensitive;
+ const gboolean show_label;
+ const GCallback callback;
+} VirtViewerToolbarButton;
+
+static void
+virt_viewer_window_toolbar_add_button(VirtViewerWindow *self,
+ const VirtViewerToolbarButton *tb_button,
+ GtkWidget **dest_widget)
+{
+ VirtViewerWindowPrivate *priv = self->priv;
+ GtkToolItem *button = gtk_tool_button_new(tb_button->icon, tb_button->label);
+
+ gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(button), tb_button->icon_name);
+ gtk_tool_item_set_tooltip_text(button, tb_button->tooltip);
+ gtk_tool_item_set_is_important(button, tb_button->show_label);
+ gtk_widget_set_sensitive(GTK_WIDGET(button), tb_button->sensitive);
+ gtk_widget_show_all(GTK_WIDGET(button));
+ gtk_toolbar_insert(GTK_TOOLBAR(priv->toolbar), button, 0);
+ g_signal_connect(button, "clicked", tb_button->callback, self);
+
+ if (dest_widget != NULL)
+ *dest_widget = GTK_WIDGET(button);
+}
static void
virt_viewer_window_toolbar_setup(VirtViewerWindow *self)
{
- GtkWidget *button;
GtkWidget *overlay;
VirtViewerWindowPrivate *priv = self->priv;
+ guint i;
+
+ const struct {
+ const VirtViewerToolbarButton tb_button;
+ GtkWidget **dest_widget;
+ } toolbar_buttons[] = {
+ { /* Close connection */
+ {
+ NULL,
+ "window-close",
+ NULL,
+ _("Disconnect"),
+ TRUE,
+ FALSE,
+ G_CALLBACK(virt_viewer_window_menu_file_quit),
+ },
+ NULL,
+ },{ /* USB Device selection */
+ {
+ gtk_image_new_from_resource(VIRT_VIEWER_RESOURCE_PREFIX"/icons/24x24/virt-viewer-usb.png"),
+ NULL,
+ _("USB device selection"),
+ _("USB device selection"),
+ TRUE,
+ FALSE,
+ G_CALLBACK(virt_viewer_window_menu_file_usb_device_selection),
+ },
+ &priv->toolbar_usb_device_selection,
+ },{ /* Send key */
+ {
+ NULL,
+ "preferences-desktop-keyboard-shortcuts",
+ NULL,
+ _("Send key combination"),
+ FALSE,
+ FALSE,
+ G_CALLBACK(virt_viewer_window_toolbar_send_key),
+ },
+ &priv->toolbar_send_key,
+ },{ /* Leave fullscreen */
+ {
+ NULL,
+ "view-restore",
+ _("Leave fullscreen"),
+ _("Leave fullscreen"),
+ TRUE,
+ TRUE,
+ G_CALLBACK(virt_viewer_window_toolbar_leave_fullscreen),
+ },
+ NULL,
+ },
+ };
priv->toolbar = g_object_ref(gtk_toolbar_new());
gtk_toolbar_set_show_arrow(GTK_TOOLBAR(priv->toolbar), FALSE);
gtk_widget_set_no_show_all(priv->toolbar, TRUE);
gtk_toolbar_set_style(GTK_TOOLBAR(priv->toolbar), GTK_TOOLBAR_BOTH_HORIZ);
- /* Close connection */
- button = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
- gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(button), "window-close");
- gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Disconnect"));
- gtk_widget_show(button);
- gtk_toolbar_insert(GTK_TOOLBAR(priv->toolbar), GTK_TOOL_ITEM (button), 0);
- g_signal_connect(button, "clicked", G_CALLBACK(virt_viewer_window_menu_file_quit), self);
-
- /* USB Device selection */
- button = gtk_image_new_from_resource(VIRT_VIEWER_RESOURCE_PREFIX"/icons/24x24/virt-viewer-usb.png");
- button = GTK_WIDGET(gtk_tool_button_new(button, NULL));
- gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), _("USB device selection"));
- gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("USB device selection"));
- gtk_toolbar_insert(GTK_TOOLBAR(priv->toolbar), GTK_TOOL_ITEM(button), 0);
- g_signal_connect(button, "clicked", G_CALLBACK(virt_viewer_window_menu_file_usb_device_selection), self);
- priv->toolbar_usb_device_selection = button;
- gtk_widget_show_all(button);
-
- /* Send key */
- button = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
- gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(button), "preferences-desktop-keyboard-shortcuts");
- gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Send key combination"));
- gtk_widget_show(button);
- gtk_toolbar_insert(GTK_TOOLBAR(priv->toolbar), GTK_TOOL_ITEM(button), 0);
- g_signal_connect(button, "clicked", G_CALLBACK(virt_viewer_window_toolbar_send_key), self);
- gtk_widget_set_sensitive(button, FALSE);
- priv->toolbar_send_key = button;
-
- /* Leave fullscreen */
- button = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
- gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(button), "view-restore");
- gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), _("Leave fullscreen"));
- gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("Leave fullscreen"));
- gtk_tool_item_set_is_important(GTK_TOOL_ITEM(button), TRUE);
- gtk_widget_show(button);
- gtk_toolbar_insert(GTK_TOOLBAR(priv->toolbar), GTK_TOOL_ITEM(button), 0);
- g_signal_connect(button, "clicked", G_CALLBACK(virt_viewer_window_toolbar_leave_fullscreen), self);
+ for (i = 0; i < G_N_ELEMENTS(toolbar_buttons); i++)
+ virt_viewer_window_toolbar_add_button(self, &(toolbar_buttons[i].tb_button), toolbar_buttons[i].dest_widget);
priv->revealer = virt_viewer_timed_revealer_new(priv->toolbar);
overlay = GTK_WIDGET(gtk_builder_get_object(priv->builder, "viewer-overlay"));
--
2.9.0
More information about the virt-tools-list
mailing list