Commit 609e04dd authored by Matthias Clasen's avatar Matthias Clasen

gtk-demo: Cosmetic changes

Clean up the code of many examples in minor ways, fix some memory
leaks, and avoid the use of dialogs where a regular toplevel works
just as well.
parent be4a2523
......@@ -98,7 +98,7 @@ EXTRA_DIST += \
data/source.svg \
data/symbolic-source.svg \
demo.gresource.xml \
$(RESOURCES) \
$(resource_files) \
org.gtk.Demo.gschema.xml \
demos.h.win32
......@@ -130,81 +130,10 @@ gtk3_demo_application_SOURCES = \
gtk3_demo_application_LDADD = $(LDADDS)
demo_resources.c: demo.gresource.xml $(RESOURCES)
$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate-source $(srcdir)/demo.gresource.xml
RESOURCES = \
$(demos) \
main.ui \
application.ui \
demo.ui \
menus.ui \
stack.ui \
revealer.ui \
theming.ui \
listbox.ui \
popover.ui \
alphatest.png \
apple-red.png \
brick.png \
brick2.png \
background.jpg \
floppybuddy.gif \
glarea-fragment.glsl \
glarea-vertex.glsl \
gnome-applets.png \
gnome-calendar.png \
gnome-foot.png \
gnome-fs-directory.png \
gnome-fs-regular.png \
gnome-gimp.png \
gnome-gmush.png \
gnome-gsame.png \
gnu-keys.png \
messages.txt \
css_accordion.css \
css_basics.css \
css_multiplebgs.css \
css_pixbufs.css \
css_shadows.css \
cssview.css \
reset.css \
alias_cursor.png \
all_scroll_cursor.png \
cell_cursor.png \
col_resize_cursor.png \
copy_cursor.png \
crosshair_cursor.png \
default_cursor.png \
e_resize_cursor.png \
ew_resize_cursor.png \
grabbing_cursor.png \
grab_cursor.png \
hand_cursor.png \
help_cursor.png \
move_cursor.png \
ne_resize_cursor.png \
nesw_resize_cursor.png \
none_cursor.png \
no_drop_cursor.png \
not_allowed_cursor.png \
n_resize_cursor.png \
ns_resize_cursor.png \
nw_resize_cursor.png \
nwse_resize_cursor.png \
pointer_cursor.png \
progress_cursor.png \
row_resize_cursor.png \
se_resize_cursor.png \
s_resize_cursor.png \
sw_resize_cursor.png \
text_cursor.png \
vertical_text_cursor.png \
wait_cursor.png \
w_resize_cursor.png \
zoom_in_cursor.png \
zoom_out_cursor.png
resource_files = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies $(builddir)/demo.gresource.xml)
demo_resources.c: demo.gresource.xml $(resource_files)
$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate-source $(srcdir)/demo.gresource.xml
iconthemedir = $(datadir)/icons/hicolor
......
......@@ -168,7 +168,7 @@ do_assistant (GtkWidget *do_widget)
{
assistant = gtk_assistant_new ();
gtk_window_set_default_size (GTK_WINDOW (assistant), -1, 300);
gtk_window_set_default_size (GTK_WINDOW (assistant), -1, 300);
gtk_window_set_screen (GTK_WINDOW (assistant),
gtk_widget_get_screen (do_widget));
......
......@@ -5,16 +5,13 @@
#include <gtk/gtk.h>
static GtkBuilder *builder;
static void
quit_activate (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *window = user_data;
window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
gtk_widget_destroy (window);
}
......@@ -23,8 +20,11 @@ about_activate (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkWidget *window = user_data;
GtkBuilder *builder;
GtkWidget *about_dlg;
builder = g_object_get_data (G_OBJECT (window), "builder");
about_dlg = GTK_WIDGET (gtk_builder_get_object (builder, "aboutdialog1"));
gtk_dialog_run (GTK_DIALOG (about_dlg));
gtk_widget_hide (about_dlg);
......@@ -55,6 +55,8 @@ do_builder (GtkWidget *do_widget)
if (!window)
{
GtkBuilder *builder;
builder = gtk_builder_new_from_resource ("/builder/demo.ui");
gtk_builder_connect_signals (builder, NULL);
......@@ -69,7 +71,7 @@ do_builder (GtkWidget *do_widget)
actions = (GActionGroup*)g_simple_action_group_new ();
g_action_map_add_action_entries (G_ACTION_MAP (actions),
win_entries, G_N_ELEMENTS (win_entries),
NULL);
window);
gtk_widget_insert_action_group (window, "win", actions);
accel_group = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
......@@ -109,18 +111,14 @@ do_builder (GtkWidget *do_widget)
item = (GtkWidget*)gtk_builder_get_object (builder, "about_item");
gtk_widget_add_accelerator (item, "activate", accel_group,
GDK_KEY_F7, 0, GTK_ACCEL_VISIBLE);
g_object_set_data_full (G_OBJECT(window), "builder", builder, g_object_unref);
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -56,7 +56,7 @@ do_button_box (GtkWidget *do_widget)
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Button Boxes demo");
gtk_window_set_title (GTK_WINDOW (window), "Button Boxes");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed),
......@@ -129,14 +129,9 @@ do_button_box (GtkWidget *do_widget)
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -13,8 +13,6 @@
#include <gtk/gtk.h>
#include <string.h>
static GtkWidget *window = NULL;
void
copy_button_clicked (GtkWidget *button,
gpointer user_data)
......@@ -107,12 +105,12 @@ drag_begin (GtkWidget *widget,
}
void
drag_data_get (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint time,
gpointer data)
drag_data_get (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint time,
gpointer data)
{
GdkPixbuf *pixbuf;
......@@ -202,6 +200,8 @@ button_press (GtkWidget *widget,
GtkWidget *
do_clipboard (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *vbox, *hbox;
......@@ -325,10 +325,7 @@ do_clipboard (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -32,9 +32,7 @@ response_cb (GtkDialog *dialog,
gpointer user_data)
{
if (response_id == GTK_RESPONSE_OK)
{
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
}
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
gtk_widget_destroy (GTK_WIDGET (dialog));
}
......@@ -49,10 +47,8 @@ change_color_callback (GtkWidget *button,
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (dialog), &color);
g_signal_connect (dialog,
"response",
G_CALLBACK (response_cb),
NULL);
g_signal_connect (dialog, "response",
G_CALLBACK (response_cb), NULL);
gtk_widget_show_all (dialog);
}
......@@ -88,7 +84,6 @@ do_colorsel (GtkWidget *do_widget)
* Create the color swatch area
*/
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
......@@ -113,14 +108,9 @@ do_colorsel (GtkWidget *do_widget)
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -321,8 +321,7 @@ do_combobox (GtkWidget *do_widget)
gtk_window_set_title (GTK_WINDOW (window), "Combo Boxes");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed),
&window);
G_CALLBACK (gtk_widget_destroyed), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
......@@ -400,8 +399,7 @@ do_combobox (GtkWidget *do_widget)
gtk_tree_path_free (path);
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
/* A GtkComboBoxEntry with validation.
*/
/* A GtkComboBoxEntry with validation */
frame = gtk_frame_new ("Editable");
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
......@@ -441,14 +439,9 @@ do_combobox (GtkWidget *do_widget)
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -6,8 +6,6 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static void
apply_css (GtkWidget *widget, GtkStyleProvider *provider)
{
......@@ -19,6 +17,8 @@ apply_css (GtkWidget *widget, GtkStyleProvider *provider)
GtkWidget *
do_css_accordion (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *container, *child;
......@@ -63,10 +63,7 @@ do_css_accordion (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -7,8 +7,6 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static void
show_parsing_error (GtkCssProvider *provider,
GtkCssSection *section,
......@@ -64,6 +62,8 @@ apply_css (GtkWidget *widget, GtkStyleProvider *provider)
GtkWidget *
do_css_basics (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *container, *child;
......@@ -112,10 +112,7 @@ do_css_basics (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -7,8 +7,6 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static void
show_parsing_error (GtkCssProvider *provider,
GtkCssSection *section,
......@@ -82,6 +80,8 @@ apply_css (GtkWidget *widget, GtkStyleProvider *provider)
GtkWidget *
do_css_multiplebgs (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *paned, *container, *child;
......@@ -163,10 +163,7 @@ do_css_multiplebgs (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -6,8 +6,6 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static void
show_parsing_error (GtkCssProvider *provider,
GtkCssSection *section,
......@@ -63,6 +61,8 @@ apply_css (GtkWidget *widget, GtkStyleProvider *provider)
GtkWidget *
do_css_pixbufs (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *paned, *container, *child;
......@@ -95,15 +95,13 @@ do_css_pixbufs (GtkWidget *do_widget)
NULL);
provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
container = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (paned), container);
child = gtk_text_view_new_with_buffer (text);
gtk_container_add (GTK_CONTAINER (container), child);
g_signal_connect (text,
"changed",
G_CALLBACK (css_text_changed),
provider);
g_signal_connect (text, "changed",
G_CALLBACK (css_text_changed), provider);
bytes = g_resources_lookup_data ("/css_pixbufs/gtk.css", 0, NULL);
gtk_text_buffer_set_text (text, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
......@@ -120,10 +118,7 @@ do_css_pixbufs (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -5,8 +5,6 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static void
show_parsing_error (GtkCssProvider *provider,
GtkCssSection *section,
......@@ -86,6 +84,8 @@ create_toolbar (void)
GtkWidget *
do_css_shadows (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *paned, *container, *child;
......@@ -117,15 +117,13 @@ do_css_shadows (GtkWidget *do_widget)
NULL);
provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
container = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (paned), container);
child = gtk_text_view_new_with_buffer (text);
gtk_container_add (GTK_CONTAINER (container), child);
g_signal_connect (text,
"changed",
G_CALLBACK (css_text_changed),
provider);
g_signal_connect (text, "changed",
G_CALLBACK (css_text_changed), provider);
bytes = g_resources_lookup_data ("/css_shadows/gtk.css", 0, NULL);
gtk_text_buffer_set_text (text, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
......@@ -142,10 +140,7 @@ do_css_shadows (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -152,14 +152,9 @@ do_cursors (GtkWidget *do_widget)
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
......
......@@ -111,7 +111,9 @@ do_dialog (GtkWidget *do_widget)
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Dialogs and Message Boxes");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
frame = gtk_frame_new ("Dialogs");
......@@ -163,14 +165,9 @@ do_dialog (GtkWidget *do_widget)
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -212,7 +212,8 @@ do_drawingarea (GtkWidget *do_widget)
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (close_window), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
......
......@@ -14,8 +14,6 @@
#include <string.h>
#include <stdlib.h>
static GtkWidget *window = NULL;
typedef struct
{
gint number;
......@@ -335,6 +333,8 @@ add_columns (GtkTreeView *treeview,
GtkWidget *
do_editable_cells (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *vbox;
......@@ -345,7 +345,6 @@ do_editable_cells (GtkWidget *do_widget)
GtkTreeModel *items_model;
GtkTreeModel *numbers_model;
/* create window, etc */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
......@@ -406,10 +405,7 @@ do_editable_cells (GtkWidget *do_widget)
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
gtk_widget_destroy (window);
return window;
}
......@@ -8,56 +8,48 @@
#include <glib/gi18n.h>
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
GtkWidget *
do_entry_buffer (GtkWidget *do_widget)
{
GtkWidget *content_area;
static GtkWidget *window = NULL;
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *entry;
GtkEntryBuffer *buffer;
if (!window)
{
window = gtk_dialog_new_with_buttons ("Entry Buffer",
GTK_WINDOW (do_widget),
0,
_("_Close"),
GTK_RESPONSE_NONE,
NULL);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (window, "response",
G_CALLBACK (gtk_widget_destroy), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "Entries share a buffer. Typing in one is reflected in the other.");
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
/* Create a buffer */
buffer = gtk_entry_buffer_new (NULL, 0);
/* Create our first entry */
entry = gtk_entry_new_with_buffer (buffer);
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
/* Create the second entry */
entry = gtk_entry_new_with_buffer (buffer);
gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
g_object_unref (buffer);
}
{
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Entry Buffer");
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label),
"Entries share a buffer. Typing in one is reflected in the other.");
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
/* Create a buffer */
buffer = gtk_entry_buffer_new (NULL, 0);
/* Create our first entry */
entry = gtk_entry_new_with_buffer (buffer);
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
/* Create the second entry */
entry = gtk_entry_new_with_buffer (buffer);
gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
g_object_unref (buffer);
}
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
......
......@@ -8,8 +8,6 @@
#include <glib/gi18n.h>
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
/* Creates a tree model containing the completions */
GtkTreeModel *
create_completion_model (void)
......@@ -38,7 +36,7 @@ create_completion_model (void)
GtkWidget *
do_entry_completion (GtkWidget *do_widget)
{
GtkWidget *content_area;
static GtkWidget *window = NULL;
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *entry;
......@@ -47,23 +45,17 @@ do_entry_completion (GtkWidget *do_widget)
if (!window)
{
window = gtk_dialog_new_with_buttons ("Entry Completion",
GTK_WINDOW (do_widget),
0,
_("_Close"),
GTK_RESPONSE_NONE,
NULL);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Entry Completion");
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (window, "response",
G_CALLBACK (gtk_widget_destroy), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);