patch for bug 83532 (partial)



yo!!!
I have a working patch for glines that allows to choose between three field sizes. I'm posting it here to receive some feedback, as I'm not very experienced with gtk+. so, have a look and tell me where I can improve on it. I'll attach the next one to the bug.
a few notes:
- this patch has been obtained with -uwi to make it as small as possible, as
  there are lots of tedious changes of case for those #define'd constants
  (HFIELDSIZE, VFIELDSIZE, NCOLORS) that are now variables. so, sorry, I don't
  think this patch will apply as it is.
- I still have made no change to the score system (taking into account the
  different sizes)
- I have borrowed lots of ideas (and code as well) from callum's rewrite of
  same-gnome. :)
thanks in advance!
ciao, marco
--- glines.h-orig	2005-02-01 19:31:47 +0000
+++ glines.h	2005-03-16 18:59:47 +0000
@@ -3,6 +3,21 @@
 
 #define STEPSIZE 4
 
+enum {
+  UNSET = 0,
+  SMALL = 1,
+  MEDIUM,
+  LARGE,
+  MAX_SIZE,
+};
+
+/* Keep this in sync with the enum above. */
+const
+gint field_sizes[MAX_SIZE][3] = {{-1, -1, -1}, /* This is a dummy entry. */
+				  { 9,  9, 7},  /* SMALL */
+			 	  {15 , 9, 7},  /* MEDIUM */
+				  {20, 15, 7}}; /* LARGE */
+
 typedef struct
 {
 	int num;
--- glines.c-orig	2005-01-31 22:48:31 +0000
+++ glines.c	2005-03-18 01:20:31 +0000
@@ -52,18 +52,25 @@
 #define KEY_SAVED_PREVIEW "/apps/glines/saved/preview"
 #define KEY_WIDTH "/apps/glines/saved/width"
 #define KEY_HEIGHT "/apps/glines/saved/height"
+#define KEY_SIZE "/apps/glines/preferences/size"
 
-#define NCOLORS 7
 #define NPIECES 3
-#define HFIELDSIZE 9
-#define VFIELDSIZE 9
+#define MAXFIELDSIZE 30
+#define DEFAULT_GAME_SIZE SMALL
+
+gint hfieldsize;
+gint vfieldsize;
+gint ncolors;
+gint game_size = UNSET;
+gint drawing_area_width = 0;
+gboolean pref_dialog_done = FALSE;
 
 GConfClient *conf_client = NULL;
 
 GtkWidget *draw_area;
-static GtkWidget *app, *appbar, *pref_dialog;
+static GtkWidget *app, *appbar, *pref_dialog, *gridframe;
 GtkWidget *preview_widgets[NPIECES];
-field_props field[HFIELDSIZE * VFIELDSIZE];
+field_props field[MAXFIELDSIZE * MAXFIELDSIZE];
 
 /* Pre-rendering image data prepared from file. */
 GamesPreimage *ball_preimage = NULL;
@@ -82,8 +89,8 @@
 int target = -1;
 int inmove = 0;
 int score = 0;
-int cursor_x = HFIELDSIZE / 2;
-int cursor_y = VFIELDSIZE / 2;
+int cursor_x = MAXFIELDSIZE / 2;
+int cursor_y = MAXFIELDSIZE / 2;
 gboolean show_cursor = FALSE;
 
 int boxsize;
@@ -100,7 +107,6 @@
 int move_timeout = 100;
 int animate_id = 0;
 int preview[NPIECES];
-int response;
 char * ball_filename;
 GtkWidget *scorelabel;
 scoretable sctab[] = {{5, 10}, {6, 12}, {7, 18}, {8, 28}, {9, 42}, {10, 82}, {11, 108}, {12, 138}, {13, 172}, {14, 210}, {0,0}};
@@ -1187,6 +1193,44 @@
 }
 
 static void
+set_sizes (gint size)
+{
+	hfieldsize = field_sizes[size][0];
+	vfieldsize = field_sizes[size][1];
+	ncolors    = field_sizes[size][2];
+	game_size  = size;
+
+	gconf_client_set_int (conf_client, KEY_SIZE, size, NULL);
+
+	if (gridframe)
+		games_grid_frame_set (GAMES_GRID_FRAME (gridframe),
+				      hfieldsize, vfieldsize);
+}
+
+static void
+size_callback (GtkWidget *widget, gpointer data)
+{
+	if (pref_dialog_done)
+		gconf_client_set_int (conf_client, KEY_SIZE, GPOINTER_TO_INT (data), NULL);
+}
+
+static void
+size_changed_cb (GConfClient *client,
+		 guint cnxn_id,
+		 GConfEntry *entry,
+		 gpointer user_data)
+{
+	gint size_tmp;
+	size_tmp = gconf_client_get_int (conf_client, KEY_SIZE, NULL);
+
+	if (size_tmp != game_size) {
+		set_sizes (size_tmp);
+		reset_game ();
+		start_game ();
+	}
+}
+
+static void
 load_theme ()
 {
 	load_image (ball_filename, &ball_preimage);
@@ -1246,6 +1290,7 @@
 	GtkWidget *w, *omenu, *l, *fv;
 	GtkWidget *frame;
 	GtkWidget *table;
+	GtkWidget *button;
 
 	if (! pref_dialog)
 		{
@@ -1294,13 +1339,48 @@
 			gtk_table_attach_defaults (GTK_TABLE (table), w, 1, 2, 1, 2);
 
 
-			frame = games_frame_new (_("General"));
+			frame = games_frame_new (_("Field size"));
 			fv = gtk_vbox_new (FALSE, FALSE);
 			gtk_box_set_spacing (GTK_BOX (fv), 6);
 			gtk_container_add (GTK_CONTAINER (frame), fv);
 			gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pref_dialog)->vbox), frame, 
 					    FALSE, FALSE, 0);
 
+			button = gtk_radio_button_new_with_label (NULL, _("Small") );
+			if (game_size == SMALL)
+				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
+			g_signal_connect (G_OBJECT (button), "clicked", 
+					  G_CALLBACK (size_callback), (gpointer) SMALL);
+
+			gtk_container_add (GTK_CONTAINER (fv), button);
+
+			button = gtk_radio_button_new_with_label
+				(gtk_radio_button_get_group (GTK_RADIO_BUTTON(button)),
+				 _("Medium") );
+			
+			if (game_size == MEDIUM)
+				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
+			g_signal_connect (G_OBJECT (button), "clicked", 
+					  G_CALLBACK (size_callback), (gpointer) MEDIUM);
+			gtk_container_add (GTK_CONTAINER (fv), button);
+
+			button = gtk_radio_button_new_with_label
+				(gtk_radio_button_get_group (GTK_RADIO_BUTTON(button)),
+				 _("Large") );
+			if (game_size == LARGE)
+				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
+			g_signal_connect (G_OBJECT (button), "clicked", 
+					  G_CALLBACK (size_callback), (gpointer) LARGE);
+			gtk_container_add (GTK_CONTAINER (fv), button);
+
+
+			frame = games_frame_new (_("General"));
+			fv = gtk_vbox_new (FALSE, FALSE);
+			gtk_box_set_spacing (GTK_BOX (fv), 6);
+			gtk_container_add (GTK_CONTAINER (frame), fv);
+			gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pref_dialog)->vbox),
+					    frame, FALSE, FALSE, 0);
+			
 			fast_moves_toggle_button = 
 				gtk_check_button_new_with_label ( _("Use fast moves") );
 			if (move_timeout == 10) 
@@ -1315,6 +1395,8 @@
 			gtk_container_add (GTK_CONTAINER (fv), fast_moves_toggle_button);
 
 			gtk_widget_show_all (pref_dialog);
+
+			pref_dialog_done = TRUE;
 		}
 	
 	gtk_window_present (GTK_WINDOW (pref_dialog));
@@ -1326,6 +1408,8 @@
 	gconf_client_set_int (conf_client, KEY_WIDTH, event->width, NULL);
 	gconf_client_set_int (conf_client, KEY_HEIGHT, event->height, NULL);
 
+	drawing_area_width = event->width;
+
 	return FALSE;
 }
 
@@ -1571,7 +1655,7 @@
 }
 
 static void
-init_config (int argc, char ** argv)
+init_config (int argc, char ** argv, gint requested_size)
 {
 	conf_client = gconf_client_get_default ();
 
@@ -1593,6 +1677,11 @@
 				 KEY_BACKGROUND_COLOR,
 				 bg_color_changed_cb,
 				 NULL, NULL, NULL);
+	gconf_client_notify_add (conf_client,
+				 KEY_SIZE,
+				 size_changed_cb,
+				 NULL, NULL, NULL);
+
 
 	/* These are here because they are only loaded once. */
 	width = gconf_client_get_int (conf_client,
@@ -1602,18 +1691,37 @@
 	height = gconf_client_get_int (conf_client,
 				       KEY_HEIGHT, NULL);
 	height = MAX (height, MIN_HEIGHT);
+
+	if (requested_size != -1) 
+		game_size = requested_size;
+	else {
+		game_size = gconf_client_get_int (conf_client,
+						  KEY_SIZE, NULL);
+		if (game_size == UNSET)
+			game_size = DEFAULT_GAME_SIZE;
+	}
+	
+	game_size = CLAMP (game_size, SMALL, MAX_SIZE - 1);
+	set_sizes (game_size);
 }
 
 int
 main (int argc, char *argv [])
 {
 	GtkWidget *alignment;   
-	GtkWidget *gridframe, *label;
+	GtkWidget *label;
 	GtkWidget *vbox, *table, *hbox;
 	GtkWidget *preview_hbox;
 	GnomeClient *client;
 	int i;
 	
+	static gint requested_size  = -1;
+	
+	static const struct poptOption options[] = {
+		{ "size", 'z', POPT_ARG_INT, &requested_size, 0, 
+		  N_("Game size (1=small, 3=large)"), N_("NUMBER") },
+		{ NULL, '\0', 0, NULL, 0, NULL, NULL }};
+
 	gnome_score_init ("glines");
 
 	srand (time (NULL));
@@ -1625,10 +1733,10 @@
 	gnome_program_init ("glines", VERSION,
 			    LIBGNOMEUI_MODULE,
 			    argc, argv,
-			    GNOME_PARAM_POPT_TABLE, NULL,
+			    GNOME_PARAM_POPT_TABLE, options,
 			    GNOME_PARAM_APP_DATADIR, DATADIR, NULL);
 
-        init_config (argc, argv);
+        init_config (argc, argv, requested_size);
 
 	gnome_window_icon_set_default_from_file (GNOME_ICONDIR"/glines.png");
 	client = gnome_master_client ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]