gtk-demo: Finish peg solitaire demo

The restart button was simply not implemented.
While we are at it, make the pegs a bit bigger,
so clicking them is easier.
This commit is contained in:
Matthias Clasen 2020-08-09 18:27:18 -04:00
parent da0955feeb
commit e5247b7cbb

View File

@ -6,8 +6,6 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
/* Create an object for the pegs that get moved around in the game.
*
* We implement the GdkPaintable interface for them, so we can use GtkPicture
@ -56,11 +54,25 @@ solitaire_peg_get_flags (GdkPaintable *paintable)
return GDK_PAINTABLE_STATIC_CONTENTS | GDK_PAINTABLE_STATIC_SIZE;
}
static int
solitaire_peg_get_intrinsic_width (GdkPaintable *paintable)
{
return 32;
}
static int
solitaire_peg_get_intrinsic_height (GdkPaintable *paintable)
{
return 32;
}
static void
solitaire_peg_paintable_init (GdkPaintableInterface *iface)
{
iface->snapshot = solitaire_peg_snapshot;
iface->get_flags = solitaire_peg_get_flags;
iface->get_intrinsic_width = solitaire_peg_get_intrinsic_width;
iface->get_intrinsic_height = solitaire_peg_get_intrinsic_height;
}
/* When defining the GType, we need to implement the GdkPaintable interface */
@ -259,32 +271,15 @@ drop_drop (GtkDropTarget *target,
return TRUE;
}
GtkWidget *
do_peg_solitaire (GtkWidget *do_widget)
static void
create_board (GtkWidget *window)
{
if (!window)
{
GtkWidget *header;
GtkWidget *restart;
GtkWidget *grid;
GtkWidget *image;
int x, y;
GtkDragSource *source;
GtkDropTarget *target;
restart = gtk_button_new_from_icon_name ("view-refresh-symbolic");
g_signal_connect (restart, "clicked", G_CALLBACK (restart), NULL);
header = gtk_header_bar_new ();
gtk_header_bar_pack_start (GTK_HEADER_BAR (header), restart);
window = gtk_window_new ();
gtk_window_set_display (GTK_WINDOW (window),
gtk_widget_get_display (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Peg Solitaire");
gtk_window_set_titlebar (GTK_WINDOW (window), header);
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
grid = gtk_grid_new ();
gtk_widget_set_halign (grid, GTK_ALIGN_CENTER);
gtk_widget_set_valign (grid, GTK_ALIGN_CENTER);
@ -302,6 +297,7 @@ do_peg_solitaire (GtkWidget *do_widget)
continue;
image = gtk_image_new ();
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
if (x != 3 || y != 3)
{
SolitairePeg *peg = solitaire_peg_new ();
@ -344,6 +340,40 @@ do_peg_solitaire (GtkWidget *do_widget)
}
}
static void
restart_game (GtkButton *button,
GtkWidget *window)
{
create_board (window);
}
GtkWidget *
do_peg_solitaire (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *header;
GtkWidget *restart;
window = gtk_window_new ();
restart = gtk_button_new_from_icon_name ("view-refresh-symbolic");
g_signal_connect (restart, "clicked", G_CALLBACK (restart_game), window);
header = gtk_header_bar_new ();
gtk_header_bar_pack_start (GTK_HEADER_BAR (header), restart);
gtk_window_set_display (GTK_WINDOW (window),
gtk_widget_get_display (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Peg Solitaire");
gtk_window_set_titlebar (GTK_WINDOW (window), header);
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
create_board (window);
}
if (!gtk_widget_get_visible (window))
gtk_widget_show (window);
else