Fix bug with children not resizing with main window
[emu8051.git] / src / emugtk.c
index e6d98dd..7742788 100644 (file)
@@ -22,6 +22,9 @@
 #include <stdio.h>
 #include "config.h"
 
+#include <stdint.h>
+#include <stdbool.h>
+
 #include <gtk/gtk.h>
 
 #include "common.h"
 #include "regwin.h"
 #include "pgmwin.h"
 #include "memwin.h"
+#include "app-config.h"
+
+#define BUTTONS_BORDER 2
 
-static int RunningState;
-static int RunFuncTag;
+static int running;
+static int running_function_tag;
 static GtkWidget *mainwin;
 
+extern struct app_config_t *cfg;
+
 /* Signal DestroyEvent */
 static void
 WindowDestroyEvent(GtkWidget *widget, gpointer data)
@@ -56,27 +64,133 @@ WindowDestroyEvent(GtkWidget *widget, gpointer data)
        gtk_main_quit();
 }
 
+/* Step out of running state */
+static void
+emugtk_stop_running()
+{
+       if (running) {
+#ifdef EMU8051_DEBUG
+               printf("emugtk_StopRunning()\n");
+#endif
+               g_source_remove(running_function_tag);
+               running = 0;
+               regwin_Show();
+               pgmwin_Disasm();
+               memwin_DumpD("00");
+       }
+}
+
+/* Running function called when idle from gtk_main */
+static gboolean
+emugtk_running(gpointer data)
+{
+       cpu8051_Exec();
+       if (IsBreakpoint(cpu8051.pc)) {
+#ifdef EMU8051_DEBUG
+               g_print("Breakpoint Hit, stopping!\n");
+#endif
+               emugtk_stop_running();
+       }
+
+       return TRUE;
+}
+
+/* Get in the running state */
+static void
+emugtk_start_running(void)
+{
+       if (!running) {
+#ifdef EMU8051_DEBUG
+               printf("emugtk_StartRunning()\n");
+#endif
+               running_function_tag = g_idle_add(emugtk_running, 0);
+               running = 1;
+       }
+}
+
 /* Taken from the Gxine source code. */
 static GtkWidget *
-AddPixButton(GtkWidget *box, gchar **pixmap_array)
+button_add_pix(GtkWidget *box, char **xpm)
 {
        GtkWidget *button, *icon;
-       GdkPixmap *image;
-       GdkBitmap *transparent;
 
        button = gtk_button_new();
        gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NORMAL);
-       image = gdk_pixmap_colormap_create_from_xpm_d(
-               NULL, gdk_colormap_get_system(),
-               &transparent, NULL, pixmap_array);
-       icon = gtk_pixmap_new(image, transparent);
+
+       icon = gtk_image_new_from_pixbuf(
+               gdk_pixbuf_new_from_xpm_data((const char **) xpm));
        gtk_container_add(GTK_CONTAINER(button), icon);
 
-       gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, BUTTONS_BORDER);
 
        return button;
 }
 
+/* CPU reset and Gtk UI update */
+static void
+emugtk_Reset(void)
+{
+       cpu8051_Reset();
+       regwin_Show();
+       pgmwin_Disasm();
+       memwin_DumpD("00");
+}
+
+/* Signal ResetEvent (ResetButton) */
+static void
+emugtk_ResetEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+#ifdef EMU8051_DEBUG
+       g_print("emugtk_ResetEvent(...)\n");
+#endif
+       emugtk_stop_running();
+       emugtk_Reset();
+}
+
+/* CPU Step and Gtk UI update */
+static void
+emugtk_Step(void)
+{
+       cpu8051_Exec();
+       regwin_Show();
+       pgmwin_Disasm();
+       memwin_DumpD("00");
+}
+
+/* Signal RunEvent (RunButton) */
+static void
+emugtk_RunEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+#ifdef EMU8051_DEBUG
+       g_print("emugtk_RunEvent(...)\n");
+#endif
+       if (running)
+               emugtk_stop_running();
+       else
+               emugtk_start_running();
+}
+
+/* Signal StopEvent (StopButton) */
+static void
+emugtk_StopEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+#ifdef EMU8051_DEBUG
+       g_print("emugtk_StopEvent(...)\n");
+#endif
+       emugtk_stop_running();
+}
+
+/* Signal StepEvent (StepButton) */
+static void
+emugtk_StepEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+#ifdef EMU8051_DEBUG
+       g_print("emugtk_StepEvent(...)\n");
+#endif
+       emugtk_stop_running();
+       emugtk_Step();
+}
+
 /* Creates the Reset, Run, Stop and Step buttons. */
 static GtkWidget *
 AddButtons(void)
@@ -88,33 +202,33 @@ AddButtons(void)
        button_hbox = gtk_hbox_new(FALSE, 0);
 
        /* Creating the RESET button. */
-       button = AddPixButton(button_hbox, reset_xpm);
-       gtk_signal_connect(GTK_OBJECT(button), "clicked",
-                          GTK_SIGNAL_FUNC(emugtk_ResetEvent),
-                          NULL);
+       button = button_add_pix(button_hbox, reset_xpm);
+       g_signal_connect(button, "clicked",
+                        G_CALLBACK(emugtk_ResetEvent),
+                        NULL);
 
        /* Creating the RUN button. */
-       button = AddPixButton(button_hbox, run_xpm);
-       gtk_signal_connect(GTK_OBJECT(button), "clicked",
-                          GTK_SIGNAL_FUNC(emugtk_RunEvent),
-                          NULL);
+       button = button_add_pix(button_hbox, run_xpm);
+       g_signal_connect(button, "clicked",
+                        G_CALLBACK(emugtk_RunEvent),
+                        NULL);
 
        /* Creating STOP button. */
-       button = AddPixButton(button_hbox, stop_xpm);
-       gtk_signal_connect(GTK_OBJECT(button), "clicked",
-                          GTK_SIGNAL_FUNC(emugtk_StopEvent),
-                          NULL);
+       button = button_add_pix(button_hbox, stop_xpm);
+       g_signal_connect(GTK_OBJECT(button), "clicked",
+                        G_CALLBACK(emugtk_StopEvent),
+                        NULL);
 
        /* Creating STEP button. */
-       button = AddPixButton(button_hbox, step_xpm);
-       gtk_signal_connect(GTK_OBJECT(button), "clicked",
-                          GTK_SIGNAL_FUNC(emugtk_StepEvent),
-                          NULL);
+       button = button_add_pix(button_hbox, step_xpm);
+       g_signal_connect(GTK_OBJECT(button), "clicked",
+                        G_CALLBACK(emugtk_StepEvent),
+                        NULL);
 
        return button_hbox;
 }
 
-GtkWidget *
+static GtkWidget *
 AddMenu(void)
 {
        GtkWidget *menu_bar;
@@ -136,77 +250,190 @@ AddMenu(void)
        return menu_bar;
 }
 
+static int
+mainwin_configure_event(GtkWindow *window, GdkEvent *event, gpointer data)
+{
+       //event->configure.x;
+       //event->configure.y;
+       cfg->win_width = event->configure.width;
+       cfg->win_height = event->configure.height;
+
+       /*
+        * Important:
+        * Returning false allows event to propagate to children. If not, they
+        * will not be resized when we resize the main window.
+        */
+       return FALSE;
+}
+
+static void
+hpaned_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
+{
+       GtkWidget *hpaned = data;
+
+       cfg->hpane_pos = gtk_paned_get_position(GTK_PANED(hpaned));
+}
+
+static void
+vpaned_notify_event(GtkWindow *window, GdkEvent *event, gpointer data)
+{
+       GtkWidget *vpaned = data;
+
+       cfg->vpane_pos = gtk_paned_get_position(GTK_PANED(vpaned));
+}
+
+/*
+ *  mainwin
+ * +---------------------------------------------------------------------+
+ * |                                                                     |
+ * |  vbox                                                               |
+ * |  +---------------------------------------------------------------+  |
+ * |  |                                                               |  |
+ * |  |  menu_bar                                                     |  |
+ * |  |  +----------------------+                                     |  |
+ * |  |  | File  View  Help     |                                     |  |
+ * |  |  +----------------------+                                     |  |
+ * |  |                                                               |  |
+ * |  |---------------------------------------------------------------|  |
+ * |  |                                                               |  |
+ * |  |  buttons_bar                                                  |  |
+ * |  |  +-----------------------+                                    |  |
+ * |  |  | RST  RUN  STOP  STEP  |                                    |  |
+ * |  |  +-----------------------+                                    |  |
+ * |  |                                                               |  |
+ * |  |---------------------------------------------------------------|  |
+ * |  |                                                               |  |
+ * |  |  vpaned                                                       |  |
+ * |  |  +---------------------------------------------------------+  |  |
+ * |  |  |                                                         |  |  |
+ * |  |  |  hpaned                                                 |  |  |
+ * |  |  |  +---------------------------------------------------+  |  |  |
+ * |  |  |  |                        |                          |  |  |  |
+ * |  |  |  |  scrollwin             |  scrollwin               |  |  |  |
+ * |  |  |  |  +------------------+  *  +--------------------+  |  |  |  |
+ * |  |  |  |  | REGISTERS window |  *  | Disassembly window |  |  |  |  |
+ * |  |  |  |  +------------------+  |  +--------------------+  |  |  |  |
+ * |  |  |  |                        |                          |  |  |  |
+ * |  |  |  +---------------------------------------------------+  |  |  |
+ * |  |  |                                                         |  |  |
+ * |  |  |--------------------------***-----------------------------  |  |
+ * |  |  |                                                         |  |  |
+ * |  |  |  scrollwin                                              |  |  |
+ * |  |  |  +---------------------------------------------------+  |  |  |
+ * |  |  |  | Memory window                                     |  |  |  |
+ * |  |  |  +---------------------------------------------------+  |  |  |
+ * |  |  |                                                         |  |  |
+ * |  |  +---------------------------------------------------------+  |  |
+ * |  |                                                               |  |
+ * |  |                                                               |  |
+ * |  +---------------------------------------------------------------+  |
+ * |                                                                     |
+ * |                                                                     |
+ * +---------------------------------------------------------------------+
+ */
 static void
 emugtk_window_init(void)
 {
-       GtkWidget *main_vbox;
+       GtkWidget *vbox;
        GtkWidget *menu_bar;
        GtkWidget *buttons_bar;
-       GtkWidget *emufixed;
-       GtkWidget *fixed_frame;
+       GtkWidget *scrollwin;
+       GtkWidget *hpaned;
+       GtkWidget *vpaned;
 
        mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(mainwin), PACKAGE);
-       gtk_widget_set_usize(GTK_WIDGET(mainwin),
-                            MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT);
+       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. */
-       gtk_signal_connect(GTK_OBJECT(mainwin), "destroy",
-                          GTK_SIGNAL_FUNC(WindowDestroyEvent), NULL);
+       g_signal_connect(mainwin, "destroy",
+                        G_CALLBACK(WindowDestroyEvent), NULL);
 
-       /*
-        * Setting main window geometry based on command line options
-        * (if specified).
-       */
-       /*MainWindowSetGeometry();*/
+       g_signal_connect(G_OBJECT(mainwin), "configure-event",
+                        G_CALLBACK(mainwin_configure_event), NULL);
 
        /*
-        * main_vbox contains the menu bar and body_vbox (for all remaining
+        * vbox contains the menu bar and body_vbox (for all remaining
         * items).
         */
-       main_vbox = gtk_vbox_new(FALSE, 1);
+       vbox = gtk_vbox_new(FALSE, 1);
 
        /* Creating the menu bar. */
        menu_bar = AddMenu();
-       /* Adding menu bar to main_vbox */
-       gtk_box_pack_start(GTK_BOX(main_vbox), menu_bar, FALSE, FALSE, 1);
+       /* 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 main_vbox */
-       gtk_box_pack_start(GTK_BOX(main_vbox), buttons_bar, FALSE, FALSE, 1);
+       /* 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 window
+        */
+       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);
 
-       /* Emulator fixed window. */
-       emufixed = gtk_fixed_new();
-       gtk_widget_set_usize(GTK_WIDGET(emufixed), MAIN_WIN_WIDTH,
-                            REG_WIN_HEIGHT + MEM_WIN_HEIGHT + 10);
+       /* hpaned will contain registers and disassembly windows. */
+       hpaned = gtk_hpaned_new();
+       gtk_paned_set_position(GTK_PANED(hpaned), cfg->hpane_pos);
+       g_signal_connect(G_OBJECT(hpaned), "notify::position",
+                        G_CALLBACK(hpaned_notify_event), hpaned);
 
        /* 8051 registers frame. */
-       fixed_frame = regwin_init(REG_WIN_WIDTH, REG_WIN_HEIGHT);
-       gtk_fixed_put(GTK_FIXED(emufixed), fixed_frame, 0, 0);
+       scrollwin = regwin_init();
+       gtk_paned_pack1(GTK_PANED(hpaned), scrollwin, FALSE, FALSE);
 
        /* Program disassembly frame. */
-       fixed_frame = pgmwin_init(PGM_WIN_WIDTH, PGM_WIN_HEIGHT);
-       gtk_fixed_put(GTK_FIXED(emufixed), fixed_frame, REG_WIN_WIDTH + 10, 0);
+       scrollwin = pgmwin_init();
+       gtk_paned_pack2(GTK_PANED(hpaned), scrollwin, TRUE, FALSE);
+
+       gtk_paned_pack1(GTK_PANED(vpaned), hpaned, FALSE, FALSE);
 
        /* Memory dump frame. */
-       fixed_frame = memwin_init(MEM_WIN_WIDTH, MEM_WIN_HEIGHT);
-       gtk_fixed_put(GTK_FIXED(emufixed), fixed_frame, 0, REG_WIN_HEIGHT);
+       scrollwin = memwin_init();
+       gtk_paned_pack2(GTK_PANED(vpaned), scrollwin, TRUE, FALSE);
 
-       /* Adding fixed window to main_vbox */
-       gtk_box_pack_start(GTK_BOX(main_vbox), emufixed, FALSE, FALSE, 1);
+       /* Adding vpaned window to vbox */
+       gtk_box_pack_start(GTK_BOX(vbox), vpaned, true, true, 1);
 
-       /* Adding the main_vbox to the main window. */
-       gtk_container_add(GTK_CONTAINER(mainwin), main_vbox);
+       /* Adding the vbox to the main window. */
+       gtk_container_add(GTK_CONTAINER(mainwin), vbox);
 
        gtk_widget_show_all(mainwin);
 }
 
+static void
+emugtk_UpdateDisplay(void)
+{
+#ifdef EMU8051_DEBUG
+       g_print("emugtk_UpdateDisplay()\n");
+#endif
+
+       regwin_Show();
+       pgmwin_Disasm();
+       memwin_DumpD("00");
+}
+
+void
+AddMenuSeparator(GtkWidget *menu)
+{
+       GtkWidget *item;
+
+       item = gtk_menu_item_new();
+       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+}
+
 void
 emugtk_new_file(char *file)
 {
-       emugtk_StopRunning();
+       emugtk_stop_running();
 
        LoadHexFile(file);
 
@@ -221,9 +448,11 @@ main(int argc, char **argv)
 
        ParseCommandLineOptions(argc, argv);
 
+       app_config_load();
+
        cpu8051_init();
 
-       RunningState = 0;
+       running = 0;
 
        gtk_init(&argc, &argv);
 
@@ -236,144 +465,11 @@ main(int argc, char **argv)
 
        gtk_main();
 
-       printf("End of program.\n");
-
-       return EXIT_SUCCESS;
-}
-
-void
-AddMenuSeparator(GtkWidget *menu)
-{
-       GtkWidget *item;
-
-       item = gtk_menu_item_new();
-       gtk_menu_append(GTK_MENU(menu), item);
-}
-
-void
-emugtk_UpdateDisplay(void)
-{
-#ifdef EMU8051_DEBUG
-       g_print("emugtk_UpdateDisplay()\n");
-#endif
-
-       regwin_Show();
-       pgmwin_Disasm();
-       memwin_DumpD("0x00");
-}
-
-/* CPU reset and Gtk UI update */
-void
-emugtk_Reset(void)
-{
-       cpu8051_Reset();
-       regwin_Show();
-       pgmwin_Disasm();
-       memwin_DumpD("0x00");
-}
-
-/* CPU Step and Gtk UI update */
-void
-emugtk_Step(void)
-{
-       cpu8051_Exec();
-       regwin_Show();
-       pgmwin_Disasm();
-       memwin_DumpD("0x00");
-}
-
-/* Signal ResetEvent (ResetButton) */
-void
-emugtk_ResetEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-#ifdef EMU8051_DEBUG
-       g_print("emugtk_ResetEvent(...)\n");
-#endif
-       emugtk_StopRunning();
-       emugtk_Reset();
-}
-
-/* Signal RunEvent (RunButton) */
-void
-emugtk_RunEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
 #ifdef EMU8051_DEBUG
-       g_print("emugtk_RunEvent(...)\n");
-#endif
-       if (RunningState)
-               emugtk_StopRunning();
-       else
-               emugtk_StartRunning();
-}
-
-/* Signal StopEvent (StopButton) */
-void
-emugtk_StopEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-#ifdef EMU8051_DEBUG
-       g_print("emugtk_StopEvent(...)\n");
-#endif
-       emugtk_StopRunning();
-}
-
-/* Signal StepEvent (StepButton) */
-void
-emugtk_StepEvent(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-#ifdef EMU8051_DEBUG
-       g_print("emugtk_StepEvent(...)\n");
-#endif
-       emugtk_StopRunning();
-       emugtk_Step();
-}
-
-/* Running called by RunningFunction() */
-void
-emugtk_Running()
-{
-       cpu8051_Exec();
-       if (IsBreakpoint(cpu8051.pc)) {
-#ifdef EMU8051_DEBUG
-               g_print("Breakpoint Hit, stopping!\n");
-#endif
-               emugtk_StopRunning();
-       }
-}
-
-/* RunningFunction called when idle from gtk_main */
-gboolean
-RunningFunction(gpointer data)
-{
-       emugtk_Running();
-       return TRUE;
-}
-
-/* Get in the RunningState */
-void
-emugtk_StartRunning(void)
-{
-       if (!RunningState) {
-#ifdef EMU8051_DEBUG
-               printf("emugtk_StartRunning()\n");
+       printf("End of program.\n");
 #endif
-               RunFuncTag = gtk_idle_add(RunningFunction, 0);
 
-               RunningState = 1;
-       }
-}
+       app_config_save();
 
-/* Step out of RunningState */
-void
-emugtk_StopRunning()
-{
-       if (RunningState) {
-#ifdef EMU8051_DEBUG
-               printf("emugtk_StopRunning()\n");
-#endif
-               gtk_idle_remove(RunFuncTag);
-               RunningState = 0;
-               regwin_Show();
-               pgmwin_Disasm();
-               memwin_DumpD("0x00");
-       }
+       return EXIT_SUCCESS;
 }