Add live option to change windows layout
authorHugo Villeneuve <hugo@hugovil.com>
Tue, 22 Oct 2013 02:36:40 +0000 (22:36 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Wed, 6 Nov 2013 02:50:17 +0000 (21:50 -0500)
Restart UI when changing layout. This is working, altough the main window
temporarily disapear in doing so.

src/app-config.c
src/app-config.h
src/emugtk.c
src/emugtk.h
src/filemenu.c
src/viewmenu.c

index 6d65df9..58f76e5 100644 (file)
@@ -51,11 +51,12 @@ app_config_init(void)
        cfg->clear_ram_on_file_load = false;
 
        /* UI settings */
+       cfg->layout = UI_LAYOUT1;
        cfg->win_width = 640;
        cfg->win_height = 480;
        cfg->hpane_pos = 100;
        cfg->vpane_pos = 200;
-       cfg->vpane_mem_pos = 200;
+       cfg->main_pane_pos = 200;
 }
 
 static int
@@ -82,12 +83,19 @@ app_config_load_from_key_file(GKeyFile *kf)
                                    &cfg->clear_ram_on_file_load);
 
        /* ui */
+       app_config_key_file_get_int(kf, "ui", "layout",  &cfg->layout);
+
+       if ((cfg->layout != UI_LAYOUT1) && (cfg->layout != UI_LAYOUT2)) {
+               log_fail_no_exit("Invalid layout, defaulting to layout 1");
+               cfg->layout = UI_LAYOUT1;
+       }
+
        app_config_key_file_get_int(kf, "ui", "win_width",  &cfg->win_width);
        app_config_key_file_get_int(kf, "ui", "win_height", &cfg->win_height);
        app_config_key_file_get_int(kf, "ui", "hpane_pos",  &cfg->hpane_pos);
        app_config_key_file_get_int(kf, "ui", "vpane_pos",  &cfg->vpane_pos);
-       app_config_key_file_get_int(kf, "ui", "vpane_mem_pos",
-                                   &cfg->vpane_mem_pos);
+       app_config_key_file_get_int(kf, "ui", "main_pane_pos",
+                                   &cfg->main_pane_pos);
 }
 
 static char *
@@ -164,12 +172,13 @@ app_config_save(void)
 
                g_string_append(buf, "\n[ui]\n");
 
+               g_string_append_printf(buf, "layout=%d\n", cfg->layout);
                g_string_append_printf(buf, "win_width=%d\n", cfg->win_width);
                g_string_append_printf(buf, "win_height=%d\n", cfg->win_height);
                g_string_append_printf(buf, "hpane_pos=%d\n", cfg->hpane_pos);
                g_string_append_printf(buf, "vpane_pos=%d\n", cfg->vpane_pos);
-               g_string_append_printf(buf, "vpane_mem_pos=%d\n",
-                                      cfg->vpane_mem_pos);
+               g_string_append_printf(buf, "main_pane_pos=%d\n",
+                                      cfg->main_pane_pos);
 
                file_path = app_config_get_file_path();
 
index f6c27b9..c655041 100644 (file)
 #ifndef APP_CONFIG_H
 #define APP_CONFIG_H 1
 
+/*
+ *    Layout1:              Layout2:
+ *
+ * REGS | PROGRAM           |         | IRAM
+ * --------------      REGS | PROGRAM | ----
+ *     IRAM                 |         | XRAM
+ * --------------
+ *     XRAM
+ */
+enum layout_t {
+       UI_LAYOUT1 = 1,
+       UI_LAYOUT2,
+};
+
 struct app_config_t
 {
        /* Emulation options */
        int clear_ram_on_file_load;
 
        /* UI settings */
+       int layout; /* UI Layout 1 or 2 */
        int win_width;
        int win_height;
-       int hpane_pos;
-       int vpane_pos;     /* Registers and memory windows. */
-       int vpane_mem_pos; /* Internal and external memory windows. */
+       int hpane_pos;     /* For registers and program windows. */
+       int vpane_pos; /* For internal and external memory windows. */
+       int main_pane_pos; /* Between hpane and vpane. */
 };
 
 int
index 3ca0c04..1c114fc 100644 (file)
@@ -50,6 +50,7 @@
 
 static int running;
 static int running_function_tag;
+static int restart_gui = true;
 
 GtkWidget *mainwin;
 
@@ -248,25 +249,43 @@ mainwin_configure_event(GtkWindow *window, GdkEvent *event, gpointer data)
 static void
 hpaned_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
 {
-       GtkWidget *hpaned = data;
+       GtkWidget *paned = data;
 
-       cfg->hpane_pos = gtk_paned_get_position(GTK_PANED(hpaned));
+       cfg->hpane_pos = gtk_paned_get_position(GTK_PANED(paned));
 }
 
 static void
 vpaned_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
 {
-       GtkWidget *vpaned = data;
+       GtkWidget *paned = data;
 
-       cfg->vpane_pos = gtk_paned_get_position(GTK_PANED(vpaned));
+       cfg->vpane_pos = gtk_paned_get_position(GTK_PANED(paned));
 }
 
 static void
-vpaned_mem_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
+main_paned_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
 {
-       GtkWidget *vpaned = data;
+       GtkWidget *paned = data;
 
-       cfg->vpane_mem_pos = gtk_paned_get_position(GTK_PANED(vpaned));
+       cfg->main_pane_pos = gtk_paned_get_position(GTK_PANED(paned));
+}
+
+void
+emugtk_restart_gui(void)
+{
+       emugtk_stop_running();
+
+       gtk_widget_destroy(mainwin);
+
+       restart_gui = true;
+}
+
+void
+emugtk_quit_gui(void)
+{
+       gtk_main_quit();
+
+       restart_gui = false;
 }
 
 /*
@@ -290,7 +309,7 @@ vpaned_mem_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
  * |  |                                                               |  |
  * |  |---------------------------------------------------------------|  |
  * |  |                                                               |  |
- * |  |  vpaned                                                       |  |
+ * |  |  main_paned                                                   |  |
  * |  |  +---------------------------------------------------------+  |  |
  * |  |  |                                                         |  |  |
  * |  |  |  hpaned                                                 |  |  |
@@ -305,7 +324,7 @@ vpaned_mem_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
  * |  |  |                                                         |  |  |
  * |  |  |--------------------------***-----------------------------  |  |
  * |  |  |                                                         |  |  |
- * |  |  |  vpaned_mem                                             |  |  |
+ * |  |  |  vpaned                                                 |  |  |
  * |  |  |  +---------------------------------------------------+  |  |  |
  * |  |  |  |                                                   |  |  |  |
  * |  |  |  |  scrollwin                                        |  |  |  |
@@ -339,7 +358,7 @@ emugtk_window_init(void)
        GtkWidget *scrollwin;
        GtkWidget *hpaned;
        GtkWidget *vpaned;
-       GtkWidget *vpaned_mem;
+       GtkWidget *main_paned;
 
        mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(mainwin), PACKAGE);
@@ -354,31 +373,11 @@ emugtk_window_init(void)
        g_signal_connect(G_OBJECT(mainwin), "configure-event",
                         G_CALLBACK(mainwin_configure_event), NULL);
 
-       /*
-        * vbox contains the menu bar and body_vbox (for all remaining
-        * items).
-        */
-       vbox = gtk_vbox_new(FALSE, 1);
-
        /* Creating the menu bar. */
        menu_bar = AddMenu();
-       /* Adding menu bar to vbox */
-       gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 1);
 
        /* Creating the buttons bar. */
        buttons_bar = AddButtons();
-       /* Adding buttons bar to vbox */
-       gtk_box_pack_start(GTK_BOX(vbox), buttons_bar, FALSE, FALSE, 1);
-
-       /*
-        * vpaned will contain:
-        *   Top:    registers and disassembly windows.
-        *   Bottom: memory windows
-        */
-       vpaned = gtk_vpaned_new();
-       gtk_paned_set_position(GTK_PANED(vpaned), cfg->vpane_pos);
-       g_signal_connect(G_OBJECT(vpaned), "notify::position",
-                        G_CALLBACK(vpaned_notify_event), vpaned);
 
        /* hpaned will contain registers and disassembly windows. */
        hpaned = gtk_hpaned_new();
@@ -394,29 +393,49 @@ emugtk_window_init(void)
        scrollwin = pgmwin_init();
        gtk_paned_pack2(GTK_PANED(hpaned), scrollwin, TRUE, FALSE);
 
-       gtk_paned_pack1(GTK_PANED(vpaned), hpaned, FALSE, FALSE);
-
-       vpaned_mem = gtk_vpaned_new();
-       gtk_paned_set_position(GTK_PANED(vpaned_mem), cfg->vpane_mem_pos);
-       g_signal_connect(G_OBJECT(vpaned_mem), "notify::position",
-                        G_CALLBACK(vpaned_mem_notify_event), vpaned_mem);
+       vpaned = gtk_vpaned_new();
+       gtk_paned_set_position(GTK_PANED(vpaned), cfg->vpane_pos);
+       g_signal_connect(G_OBJECT(vpaned), "notify::position",
+                        G_CALLBACK(vpaned_notify_event), vpaned);
 
        /* Internal memory dump frame. */
        scrollwin = memwin_init("Internal memory (IRAM)", INT_MEM_ID);
-       gtk_paned_pack1(GTK_PANED(vpaned_mem), scrollwin, FALSE, FALSE);
+       gtk_paned_pack1(GTK_PANED(vpaned), scrollwin, FALSE, FALSE);
 
        /* External memory dump frame. */
        scrollwin = memwin_init("External memory (XRAM)", EXT_MEM_ID);
-       gtk_paned_pack2(GTK_PANED(vpaned_mem), scrollwin, TRUE, FALSE);
+       gtk_paned_pack2(GTK_PANED(vpaned), scrollwin, TRUE, FALSE);
+
+       /*
+        * main_paned will contain two groups:
+        *   group1:    registers and disassembly windows.
+        *   group2:    memory windows
+        */
+       if (cfg->layout == UI_LAYOUT1)
+               main_paned = gtk_vpaned_new();
+       else
+               main_paned = gtk_hpaned_new();
 
-       gtk_paned_pack2(GTK_PANED(vpaned), vpaned_mem, TRUE, FALSE);
+       gtk_paned_set_position(GTK_PANED(main_paned), cfg->main_pane_pos);
+       g_signal_connect(G_OBJECT(main_paned), "notify::position",
+                        G_CALLBACK(main_paned_notify_event), main_paned);
+       gtk_paned_pack1(GTK_PANED(main_paned), hpaned, FALSE, FALSE);
+       gtk_paned_pack2(GTK_PANED(main_paned), vpaned, TRUE, FALSE);
 
-       /* Adding vpaned window to vbox */
-       gtk_box_pack_start(GTK_BOX(vbox), vpaned, true, true, 1);
+       /*
+        * vbox contains the menu bar and body_vbox (for all remaining
+        * items).
+        */
+       vbox = gtk_vbox_new(FALSE, 1);
+       gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 1);
+       gtk_box_pack_start(GTK_BOX(vbox), buttons_bar, FALSE, FALSE, 1);
+       gtk_box_pack_start(GTK_BOX(vbox), main_paned, true, true, 1);
 
        /* Adding the vbox to the main window. */
        gtk_container_add(GTK_CONTAINER(mainwin), vbox);
 
+       g_signal_connect(mainwin, "destroy", G_CALLBACK(emugtk_quit_gui), NULL);
+
        gtk_widget_show_all(mainwin);
 }
 
@@ -436,7 +455,7 @@ emugtk_new_file(char *file)
 
        LoadHexFile(file);
 
-       emugtk_Reset();
+       emugtk_Reset(); /* Use app-config->clear_ram_on_file_load */
        emugtk_UpdateDisplay();
 }
 
@@ -452,14 +471,18 @@ main(int argc, char **argv)
 
        gtk_init(&argc, &argv);
 
-       emugtk_window_init();
-
        if (options.filename != NULL)
-               emugtk_new_file(options.filename);
+               LoadHexFile(options.filename);
        else
-               emugtk_Reset();
+               cpu8051_Reset();
 
-       gtk_main();
+       while (restart_gui == true) {
+               log_info("Init GUI");
+
+               emugtk_window_init();
+               emugtk_UpdateDisplay();
+               gtk_main();
+       }
 
        log_info("Terminate");
 
index 35ac1e4..8e47427 100644 (file)
@@ -33,4 +33,10 @@ emugtk_new_file(char *file);
 void
 emugtk_UpdateDisplay(void);
 
+void
+emugtk_restart_gui(void);
+
+void
+emugtk_quit_gui(void);
+
 #endif /* EMUGTK_H */
index 47e18aa..d2dae9c 100644 (file)
@@ -107,7 +107,7 @@ FileOpenEvent(GtkObject *object, gpointer data)
 static void
 FileQuitEvent(gchar *string)
 {
-       gtk_main_quit();
+       emugtk_quit_gui();
 }
 
 void
index b167274..3024877 100644 (file)
 #include "emugtk.h" /* For AddMenuSeparator() function. */
 #include "messagebox.h"
 #include "viewmenu.h"
+#include "app-config.h"
 
-static void
-ViewMenuExternalDump(gchar *string)
+extern struct app_config_t *cfg;
+
+void toggle_layout(GtkWidget *widget, gpointer data)
 {
-       ShowMessage("External Memory Dump", "Not implemented yet!",
-                   GTK_JUSTIFY_CENTER, MESSAGE_DIALOG_NORMAL_FONT);
+       int id;
+
+        id = GPOINTER_TO_UINT(data);
+
+       if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
+               log_info("  Switching to layout %d", id);
+               cfg->layout = id;
+               emugtk_restart_gui();
+       }
 }
 
-static void
-ViewMenuInternalDump(gchar *string)
+void
+view_add_layout_submenu(GtkWidget *parent)
 {
-       ShowMessage("Internal Memory Dump", "Not implemented yet!",
-                   GTK_JUSTIFY_CENTER, MESSAGE_DIALOG_NORMAL_FONT);
+       GtkWidget *submenu;
+       GtkWidget *layout;
+       GtkWidget *layout1;
+       GtkWidget *layout2;
+       GSList *group = NULL;
+
+       submenu = gtk_menu_new();
+
+       layout  = gtk_menu_item_new_with_label("Layout");
+
+       layout1 = gtk_radio_menu_item_new_with_label(group, "Layout1");
+       group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(layout1));
+       layout2 = gtk_radio_menu_item_new_with_label(group, "Layout2");
+
+       if (cfg->layout == UI_LAYOUT1)
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(layout1), TRUE);
+       else
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(layout2), TRUE);
+
+       g_signal_connect(G_OBJECT(layout1), "activate",
+                        G_CALLBACK(toggle_layout), (gpointer) UI_LAYOUT1);
+       g_signal_connect(G_OBJECT(layout2), "activate",
+                        G_CALLBACK(toggle_layout), (gpointer) UI_LAYOUT2);
+
+       gtk_menu_item_set_submenu(GTK_MENU_ITEM(layout), submenu);
+       gtk_menu_shell_append(GTK_MENU_SHELL(submenu), layout1);
+       gtk_menu_shell_append(GTK_MENU_SHELL(submenu), layout2);
+       gtk_menu_shell_append(GTK_MENU_SHELL(parent), layout);
 }
 
 void
@@ -49,27 +84,23 @@ ViewAddMenu(GtkWidget *menu_bar)
 {
        GtkWidget *item;
        GtkWidget *menu;
+       GtkWidget *view;
 
        menu = gtk_menu_new();
 
-       /* Create the 'Viewmenu External Memory Dump' item. */
-       item = gtk_menu_item_new_with_label("External Memory Dump");
+       view = gtk_menu_item_new_with_label("View");
+
+       item = gtk_menu_item_new_with_label("Internal Memory");
+       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+
+       item = gtk_menu_item_new_with_label("External Memory");
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-       /* Attach the callback functions to the activate signal. */
-       g_signal_connect(item, "activate", G_CALLBACK(ViewMenuExternalDump),
-                        NULL);
 
        AddMenuSeparator(menu);
 
-       /* Create the 'Viewmenu Internal Memory Dump' item. */
-       item = gtk_menu_item_new_with_label("Internal Memory Dump");
-       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-       /* Attach the callback functions to the activate signal. */
-       g_signal_connect(item, "activate", G_CALLBACK(ViewMenuInternalDump),
-                        NULL);
-
-       /* Adding submenu title. */
-       item = gtk_menu_item_new_with_label("View");
-       gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
-       gtk_menu_shell_append((GtkMenuShell *) menu_bar, item);
+       /* Add layout submenu */
+       view_add_layout_submenu(menu);
+
+       gtk_menu_item_set_submenu(GTK_MENU_ITEM(view), menu);
+       gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), view);
 }