From 9241cbf30ed9ab77bcdb568aa6cdf44ad47edc8c Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Tue, 8 Apr 2014 22:32:20 -0400 Subject: [PATCH] Allow to override window geometry on the command line --- src/common/options.c | 5 +++++ src/common/options.h | 1 + src/gtk/main.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/common/options.c b/src/common/options.c index 59c5d26..c3d91d1 100644 --- a/src/common/options.c +++ b/src/common/options.c @@ -42,6 +42,7 @@ static const char args_doc[] = "[FILENAME]"; /* The options we understand. */ static struct argp_option argp_options[] = { {"debug", 'd', "level", 0, "Produce debugging output", 0}, + {"geometry", 'g', "pos", 0, "Set geometry", 0}, {"pram", 'p', "size", 0, "Set program memory size", 0}, {"xram", 'x', "size", 0, "Set external ram size (default is 1024)", 0}, @@ -121,6 +122,9 @@ parse_opt(int key, char *arg, struct argp_state *state) case 'd': decode_debug_option(arg, state); break; + case 'g': + options.g = arg; + break; case 'i': decode_memory_size(arg, state, INT_MEM_ID); break; @@ -166,6 +170,7 @@ parse_command_line_options(int argc, char *argv[]) /* Setting default values. */ options.filename = NULL; + options.g = NULL; options.pram_size = PGM_MEM_DEFAULT_SIZE; options.iram_size = INT_MEM_MAX_SIZE; options.xram_size = EXT_MEM_DEFAULT_SIZE; diff --git a/src/common/options.h b/src/common/options.h index 22dd1bc..f5c4452 100644 --- a/src/common/options.h +++ b/src/common/options.h @@ -14,6 +14,7 @@ #define PACKAGE_DESCRIPTION "Emulator for 8051 family microcontrollers" struct options_t { + char *g; int pram_size; /* Maximum program memory size. */ int iram_size; /* Maximum internal ram size. */ int xram_size; /* Maximum external ram size. */ diff --git a/src/gtk/main.c b/src/gtk/main.c index d20d744..3a2ccd8 100644 --- a/src/gtk/main.c +++ b/src/gtk/main.c @@ -403,6 +403,21 @@ emugtk_create_memory_paned(void) } } +static void +emugtk_set_geometry_hints(GtkWidget *window) +{ + GdkGeometry hints; + + hints.min_width = 100; + hints.min_height = 100; + + /* Set reference point to top left corner */ + hints.win_gravity = GDK_GRAVITY_NORTH_WEST; + + gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &hints, + GDK_HINT_MIN_SIZE); +} + /* * mainwin * +---------------------------------------------------------------------+ @@ -467,6 +482,7 @@ emugtk_create_memory_paned(void) static void emugtk_window_init(void) { + int geometry_ok = false; int id; GtkWidget *vbox; GtkWidget *menu_bar; @@ -480,8 +496,6 @@ emugtk_window_init(void) mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(mainwin), PACKAGE_NAME); - gtk_window_set_default_size(GTK_WINDOW(mainwin), - cfg->win_width, cfg->win_height); gtk_container_set_border_width(GTK_CONTAINER(mainwin), 0); /* Window DESTROY event. */ @@ -554,6 +568,32 @@ emugtk_window_init(void) g_signal_connect(mainwin, "destroy", G_CALLBACK(emugtk_quit_gui), NULL); + emugtk_set_geometry_hints(mainwin); + + /* + * If either a size or a position can be extracted from the geometry + * string, gtk_window_parse_geometry() returns TRUE and calls + * gtk_window_set_default_size() and/or gtk_window_move() to resize/move + * the window. + */ + if (options.g != NULL) { + geometry_ok = gtk_window_parse_geometry(GTK_WINDOW(mainwin), + options.g); + if (!geometry_ok) + log_err("Failed to parse geometry argument: %s", + options.g); + } + + /* + * If geometry was not specified, or was improperly parsed, use + * saved window size. + */ + if (geometry_ok == false) { + log_err("Use saved window size"); + gtk_window_set_default_size(GTK_WINDOW(mainwin), + cfg->win_width, cfg->win_height); + } + gtk_widget_show_all(mainwin); emugtk_window_init_complete = true; -- 2.20.1