2011-06-17 15:33:18 +00:00
|
|
|
/* widget-factory: a collection of widgets in a single page, for easy
|
|
|
|
* theming
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Canonical Ltd
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-17 15:33:18 +00:00
|
|
|
*
|
|
|
|
* Authored by Andrea Cimitan <andrea.cimitan@canonical.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-02-09 12:57:11 +00:00
|
|
|
#include "config.h"
|
2011-06-17 15:33:18 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2011-07-25 14:20:56 +00:00
|
|
|
static void
|
2013-11-04 21:49:05 +00:00
|
|
|
activate_toggle (GSimpleAction *action,
|
|
|
|
GVariant *parameter,
|
|
|
|
gpointer user_data)
|
2011-07-25 14:20:56 +00:00
|
|
|
{
|
2013-11-04 21:49:05 +00:00
|
|
|
GVariant *state;
|
2011-07-25 14:20:56 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
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
|
|
|
|
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),
|
2011-07-25 14:20:56 +00:00
|
|
|
NULL);
|
2013-11-04 21:49:05 +00:00
|
|
|
|
|
|
|
g_simple_action_set_state (action, state);
|
2011-07-25 14:20:56 +00:00
|
|
|
}
|
|
|
|
|
2012-02-09 12:57:11 +00:00
|
|
|
static void
|
2013-11-04 21:49:05 +00:00
|
|
|
activate_about (GSimpleAction *action,
|
|
|
|
GVariant *parameter,
|
|
|
|
gpointer user_data)
|
2012-02-09 12:57:11 +00:00
|
|
|
{
|
2013-11-06 17:50:31 +00:00
|
|
|
GtkApplication *app = user_data;
|
2012-02-09 12:57:11 +00:00
|
|
|
const gchar *authors[] = {
|
|
|
|
"Andrea Cimitan",
|
2012-02-15 14:13:57 +00:00
|
|
|
"Cosimo Cecchi",
|
|
|
|
NULL
|
2012-02-09 12:57:11 +00:00
|
|
|
};
|
|
|
|
|
2013-11-06 17:50:31 +00:00
|
|
|
gtk_show_about_dialog (GTK_WINDOW (gtk_application_get_active_window (app)),
|
2012-02-09 12:57:11 +00:00
|
|
|
"program-name", "GTK+ Widget Factory",
|
|
|
|
"version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
|
|
|
|
PACKAGE_VERSION,
|
|
|
|
gtk_get_major_version (),
|
|
|
|
gtk_get_minor_version (),
|
|
|
|
gtk_get_micro_version ()),
|
2013-11-06 17:51:29 +00:00
|
|
|
"copyright", "(C) 1997-2013 The GTK+ Team",
|
2012-02-09 12:57:11 +00:00
|
|
|
"license-type", GTK_LICENSE_LGPL_2_1,
|
|
|
|
"website", "http://www.gtk.org",
|
|
|
|
"comments", "Program to demonstrate GTK+ themes and widgets",
|
|
|
|
"authors", authors,
|
2013-11-05 21:39:50 +00:00
|
|
|
"logo-icon-name", "gtk3-widget-factory",
|
2012-02-09 12:57:11 +00:00
|
|
|
"title", "About GTK+ Widget Factory",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2012-09-09 20:02:15 +00:00
|
|
|
static void
|
2013-11-04 21:49:05 +00:00
|
|
|
activate_quit (GSimpleAction *action,
|
|
|
|
GVariant *parameter,
|
|
|
|
gpointer user_data)
|
2012-09-09 20:02:15 +00:00
|
|
|
{
|
2013-11-04 21:49:05 +00:00
|
|
|
GtkApplication *app = user_data;
|
|
|
|
GtkWidget *win;
|
|
|
|
GList *list, *next;
|
|
|
|
|
|
|
|
list = gtk_application_get_windows (app);
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
win = list->data;
|
|
|
|
next = list->next;
|
2012-09-09 20:02:15 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
gtk_widget_destroy (GTK_WIDGET (win));
|
2012-09-09 20:02:15 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
list = next;
|
|
|
|
}
|
2012-09-09 20:02:15 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 12:48:27 +00:00
|
|
|
static void
|
|
|
|
spin_value_changed (GtkAdjustment *adjustment, GtkWidget *label)
|
|
|
|
{
|
|
|
|
GtkWidget *w;
|
|
|
|
gint v;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
v = (int)gtk_adjustment_get_value (adjustment);
|
|
|
|
|
|
|
|
if ((v % 3) == 0)
|
|
|
|
{
|
|
|
|
text = g_strdup_printf ("%d is a multiple of 3", v);
|
|
|
|
gtk_label_set_label (GTK_LABEL (label), text);
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
|
|
|
|
w = gtk_widget_get_ancestor (label, GTK_TYPE_REVEALER);
|
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (w), (v % 3) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dismiss (GtkWidget *button)
|
|
|
|
{
|
|
|
|
GtkWidget *w;
|
|
|
|
|
|
|
|
w = gtk_widget_get_ancestor (button, GTK_TYPE_REVEALER);
|
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (w), FALSE);
|
|
|
|
}
|
|
|
|
|
2013-11-10 06:15:59 +00:00
|
|
|
static gint pulse_time = 250;
|
2013-11-11 12:22:51 +00:00
|
|
|
static guint pulse_id = 0;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
pulse_it (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
gtk_progress_bar_pulse (GTK_PROGRESS_BAR (widget));
|
|
|
|
|
|
|
|
pulse_id = g_timeout_add (pulse_time, (GSourceFunc)pulse_it, widget);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
2013-11-10 06:15:59 +00:00
|
|
|
|
|
|
|
static void
|
2013-11-11 12:22:51 +00:00
|
|
|
update_pulse_time (GtkAdjustment *adjustment, GtkWidget *widget)
|
2013-11-10 06:15:59 +00:00
|
|
|
{
|
|
|
|
gdouble value;
|
|
|
|
|
|
|
|
value = gtk_adjustment_get_value (adjustment);
|
|
|
|
|
|
|
|
/* vary between 50 and 450 */
|
|
|
|
pulse_time = 50 + 4 * value;
|
|
|
|
|
2013-11-11 12:22:51 +00:00
|
|
|
if (value == 100 && pulse_id != 0)
|
|
|
|
{
|
|
|
|
g_source_remove (pulse_id);
|
|
|
|
pulse_id = 0;
|
|
|
|
}
|
|
|
|
else if (value < 100 && pulse_id == 0)
|
|
|
|
{
|
|
|
|
pulse_id = g_timeout_add (pulse_time, (GSourceFunc)pulse_it, widget);
|
|
|
|
}
|
2013-11-02 20:13:33 +00:00
|
|
|
}
|
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
static void
|
|
|
|
startup (GApplication *app)
|
2011-06-17 15:33:18 +00:00
|
|
|
{
|
|
|
|
GtkBuilder *builder;
|
2013-11-04 21:49:05 +00:00
|
|
|
GMenuModel *appmenu;
|
2011-06-17 15:33:18 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
builder = gtk_builder_new ();
|
|
|
|
gtk_builder_add_from_resource (builder, "/ui/widget-factory.ui", NULL);
|
|
|
|
|
|
|
|
appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
|
2011-06-17 15:33:18 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
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 }
|
|
|
|
};
|
2011-06-17 15:33:18 +00:00
|
|
|
|
|
|
|
builder = gtk_builder_new ();
|
2012-02-09 14:06:18 +00:00
|
|
|
gtk_builder_add_from_resource (builder, "/ui/widget-factory.ui", NULL);
|
2011-06-17 15:33:18 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
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);
|
2011-06-17 15:33:18 +00:00
|
|
|
|
2013-11-11 12:22:51 +00:00
|
|
|
widget = (GtkWidget *)gtk_builder_get_object (builder, "progressbar3");
|
|
|
|
pulse_id = g_timeout_add (250, (GSourceFunc)pulse_it, widget);
|
2013-11-10 06:15:59 +00:00
|
|
|
g_signal_connect (gtk_builder_get_object (builder, "adjustment1"),
|
|
|
|
"value-changed",
|
2013-11-11 12:22:51 +00:00
|
|
|
G_CALLBACK (update_pulse_time), widget);
|
2013-11-02 20:13:33 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
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");
|
|
|
|
adj = (GtkAdjustment *) gtk_builder_get_object (builder, "adjustment2");
|
|
|
|
g_signal_connect (adj, "value-changed", G_CALLBACK (spin_value_changed), widget);
|
|
|
|
|
|
|
|
gtk_widget_show_all (GTK_WIDGET (window));
|
2011-07-25 14:20:56 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
g_object_unref (builder);
|
|
|
|
}
|
2012-09-09 20:02:15 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GtkApplication *app;
|
|
|
|
static GActionEntry app_entries[] = {
|
|
|
|
{ "about", activate_about, NULL, NULL, NULL },
|
|
|
|
{ "quit", activate_quit, NULL, NULL, NULL },
|
|
|
|
};
|
2012-09-09 20:02:15 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
gtk_init (&argc, &argv);
|
2012-02-09 12:57:11 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
app = gtk_application_new ("org.gtk.WidgetFactory", 0);
|
2013-04-23 12:48:27 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
g_action_map_add_action_entries (G_ACTION_MAP (app),
|
|
|
|
app_entries, G_N_ELEMENTS (app_entries),
|
|
|
|
app);
|
2013-04-23 12:48:27 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
|
|
|
|
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
2011-06-17 15:33:18 +00:00
|
|
|
|
2013-11-04 21:49:05 +00:00
|
|
|
g_application_run (G_APPLICATION (app), argc, argv);
|
2011-06-17 15:33:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|