]> git.tdb.fi Git - ext/subsurface.git/blobdiff - gtk-gui.c
Might as well free current_file
[ext/subsurface.git] / gtk-gui.c
index 18c19c79f1cd377d39ad3f846346ed49ef82b9d0..895ef449365ec220bd833c28f1bcf0745b508a21 100644 (file)
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -21,6 +21,8 @@ GtkWidget *main_vbox;
 GtkWidget *error_info_bar;
 GtkWidget *error_label;
 GtkWidget *vpane, *hpane;
+GtkWidget *notebook;
+
 int        error_count;
 
 const char *divelist_font;
@@ -96,7 +98,7 @@ void report_error(GError* error)
        {
                return;
        }
-       
+
        if (error_info_bar == NULL)
        {
                error_count = 1;
@@ -106,11 +108,11 @@ void report_error(GError* error)
                g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
                gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
                                              GTK_MESSAGE_ERROR);
-               
+
                error_label = gtk_label_new(error->message);
                GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
                gtk_container_add(GTK_CONTAINER(container), error_label);
-               
+
                gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
                gtk_widget_show_all(main_vbox);
        }
@@ -149,7 +151,7 @@ static void file_open(GtkWidget *w, gpointer data)
                GSList *filenames, *fn_glist;
                char *filename;
                filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
-               
+
                GError *error = NULL;
                while(filenames != NULL) {
                        filename = filenames->data;
@@ -160,7 +162,7 @@ static void file_open(GtkWidget *w, gpointer data)
                                g_error_free(error);
                                error = NULL;
                        }
-                       
+
                        g_free(filename);
                        filenames = g_slist_next(filenames);
                }
@@ -170,10 +172,43 @@ static void file_open(GtkWidget *w, gpointer data)
        gtk_widget_destroy(dialog);
 }
 
+/* return the path and the file component contained in the full path */
+static char *path_and_file(char *pathin, char **fileout) {
+       char *slash = pathin, *next;
+       char *result;
+       size_t len, n;
+
+       if (! pathin) {
+               *fileout = strdup("");
+               return strdup("");
+       }
+       while ((next = strpbrk(slash + 1, "\\/")))
+               slash = next;
+       if (pathin != slash)
+               slash++;
+       *fileout = strdup(slash);
+
+       /* strndup(pathin, slash - pathin) */
+       n = slash - pathin;
+       len = strlen(pathin);
+       if (n < len)
+               len = n;
+
+       result = (char *)malloc(len + 1);
+       if (!result)
+               return 0;
+
+       result[len] = '\0';
+       return (char *)memcpy(result, pathin, len);
+}
+
 static void file_save_as(GtkWidget *w, gpointer data)
 {
        GtkWidget *dialog;
        char *filename = NULL;
+       char *current_file;
+       char *current_dir;
+
        dialog = gtk_file_chooser_dialog_new("Save File As",
                GTK_WINDOW(main_window),
                GTK_FILE_CHOOSER_ACTION_SAVE,
@@ -182,7 +217,13 @@ static void file_save_as(GtkWidget *w, gpointer data)
                NULL);
        gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
 
-       gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), existing_filename);
+       current_dir = path_and_file(existing_filename, &current_file);
+       gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), current_dir);
+       gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), current_file);
+
+       free(current_dir);
+       free(current_file);
+
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
                filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        }
@@ -662,10 +703,14 @@ static void view_info(GtkWidget *w, gpointer data)
 static void view_three(GtkWidget *w, gpointer data)
 {
        GtkAllocation alloc;
+       GtkRequisition requisition;
+
        gtk_widget_get_allocation(hpane, &alloc);
        gtk_paned_set_position(GTK_PANED(hpane), alloc.width/2);
        gtk_widget_get_allocation(vpane, &alloc);
-       gtk_paned_set_position(GTK_PANED(vpane), alloc.height/2);
+       gtk_widget_size_request(notebook, &requisition);
+       /* pick the requested size for the notebook plus 6 pixels for frame */
+       gtk_paned_set_position(GTK_PANED(vpane), requisition.height + 6);
 }
 
 static GtkActionEntry menu_items[] = {
@@ -750,11 +795,11 @@ static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
 void init_ui(int *argcp, char ***argvp)
 {
        GtkWidget *win;
-       GtkWidget *notebook;
        GtkWidget *nb_page;
        GtkWidget *dive_list;
        GtkWidget *menubar;
        GtkWidget *vbox;
+       GtkWidget *scrolled;
        GdkScreen *screen;
        GtkIconTheme *icon_theme=NULL;
        GtkSettings *settings;
@@ -826,13 +871,16 @@ void init_ui(int *argcp, char ***argvp)
 
        vpane = gtk_vpaned_new();
        gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
-
        hpane = gtk_hpaned_new();
        gtk_paned_add1(GTK_PANED(vpane), hpane);
+       g_signal_connect_after(G_OBJECT(vbox), "realize", G_CALLBACK(view_three), NULL);
 
        /* Notebook for dive info vs profile vs .. */
        notebook = gtk_notebook_new();
-       gtk_paned_add1(GTK_PANED(hpane), notebook);
+       scrolled = gtk_scrolled_window_new(NULL, NULL);
+       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+       gtk_paned_add1(GTK_PANED(hpane), scrolled);
+       gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), notebook);
        g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
 
        /* Create the actual divelist */