Add view menu option to enable/disable IRAM/XRAM windows
[emu8051.git] / src / filemenu.c
index ded4a5c..d2dae9c 100644 (file)
 
 #define FILENAME_DESCRIPTION "Open Intel Hex file"
 
+static char previous_folder[MAX_FILENAME_LENGTH + 1];
+
 static void
-FileOpenDialog_OK(GtkWidget *widget, gpointer file_dialog)
+remember_current_folder(GtkFileChooser *chooser)
 {
-       char *selected_file;
-
-#if defined(DEBUG)
-       g_print("FileOpenDialog_OK()\n");
-#endif
-
-       /*
-        * The cast to (char *) is to remove a warning in GTK2, because the
-        * return value of the gtk_file_selection_get_filename() function is
-        * 'G_CONST_RETURN gchar *'.
-        */
-       selected_file = (char *) gtk_file_selection_get_filename(
-               GTK_FILE_SELECTION(file_dialog));
+       char *folder;
 
-       g_print("emugtk_File = %s\n", selected_file);
+       folder = gtk_file_chooser_get_current_folder(chooser);
 
-       emugtk_new_file(selected_file);
+       if (folder != NULL) {
+               if (strlen(folder) >= MAX_FILENAME_LENGTH) {
+                       /* Non-critical error */
+                       log_warn("current folder name too long for buffer");
+               } else {
+                       log_info("current folder = %s", folder);
+                       strncpy(previous_folder, folder, MAX_FILENAME_LENGTH);
+               }
 
-       gtk_widget_destroy(GTK_WIDGET(file_dialog));
+               g_free(folder);
+       }
 }
 
 void
 FileOpenEvent(GtkObject *object, gpointer data)
 {
        GtkWidget *file_dialog;
+       char *dir;
+       char *cwd = NULL;
 
-#if defined(DEBUG)
-       g_print("FileOpenEvent()\n");
-#endif
+       log_info("FileOpenEvent()");
 
        /* Create a new file selection widget. */
-       file_dialog = gtk_file_selection_new(FILENAME_DESCRIPTION);
-
-       /* Connect the file dialog's OK button up to a handler. */
-       gtk_signal_connect(
-               GTK_OBJECT(GTK_FILE_SELECTION(file_dialog)->ok_button),
-               "clicked", GTK_SIGNAL_FUNC(FileOpenDialog_OK), file_dialog);
-
-       /*
-        * Ensure that the file selection dialog box is destroyed when the user
-        * clicks CANCEL.
-        */
-       gtk_signal_connect_object(
-               GTK_OBJECT(GTK_FILE_SELECTION(file_dialog)->cancel_button),
-               "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
-               (gpointer) file_dialog);
-
-       /* Show the dialog. */
-       gtk_widget_show(GTK_WIDGET(file_dialog));
-
-       /*
-        * To have the main window of our application being unusable while
-        * using the dialog.
-        */
-       gtk_window_set_modal(GTK_WINDOW(file_dialog), TRUE);
+       file_dialog = gtk_file_chooser_dialog_new(
+               FILENAME_DESCRIPTION, NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
+               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+               GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
+
+       if (strlen(previous_folder) == 0) {
+               /* Opening file chooser to current working directory. */
+               cwd = g_get_current_dir();
+               dir = cwd;
+       } else {
+               /* Opening file chooser to previous opened directory. */
+               dir = previous_folder;
+       }
+
+       gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dialog), dir);
+
+       if (cwd)
+               g_free(cwd);
+
+       if (gtk_dialog_run(GTK_DIALOG(file_dialog)) == GTK_RESPONSE_ACCEPT) {
+               char *selected_file;
+
+               selected_file = gtk_file_chooser_get_filename(
+                       GTK_FILE_CHOOSER(file_dialog));
+
+               if (selected_file != NULL) {
+                       log_info("emugtk_File = %s", selected_file);
+
+                       remember_current_folder(GTK_FILE_CHOOSER(file_dialog));
+
+                       emugtk_new_file(selected_file);
+                       g_free(selected_file);
+               }
+       }
+
+       gtk_widget_destroy(file_dialog);
 }
 
 static void
 FileQuitEvent(gchar *string)
 {
-#if defined(DEBUG)
-       g_print("%s\n", string);
-#endif
-
-       gtk_main_quit();
+       emugtk_quit_gui();
 }
 
 void
@@ -114,22 +120,20 @@ FileAddMenu(GtkWidget *menu_bar)
 
        /* Create the 'open' item. */
        item = gtk_menu_item_new_with_label(FILENAME_DESCRIPTION);
-       gtk_menu_append(GTK_MENU(menu), item);
+       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
        /* Attach the callback functions to the activate signal. */
-       gtk_signal_connect_object(GTK_OBJECT(item), "activate",
-                                 GTK_SIGNAL_FUNC(FileOpenEvent), NULL);
+       g_signal_connect(item, "activate", G_CALLBACK(FileOpenEvent), NULL);
 
        AddMenuSeparator(menu);
 
        item = gtk_menu_item_new_with_label("Exit");
-       gtk_menu_append(GTK_MENU(menu), item);
+       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
        /* We can attach the Quit menu item to our exit function */
-       gtk_signal_connect_object(GTK_OBJECT(item), "activate",
-                                 GTK_SIGNAL_FUNC(FileQuitEvent),
-                                 (gpointer) "file.quit");
+       g_signal_connect(item, "activate", G_CALLBACK(FileQuitEvent),
+                        (gpointer) "file.quit");
 
        /* Adding submenu title. */
        item = gtk_menu_item_new_with_label("File");
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
-       gtk_menu_bar_append(GTK_MENU_BAR(menu_bar), item);
+       gtk_menu_shell_append((GtkMenuShell *) menu_bar, item);
 }