[ghex/gtk4-port: 48/91] Implement Revert and some prefs tweaks




commit 289042239850dbc2191333473e7785b2dc6fb6db
Author: Logan Rathbone <poprocks gmail com>
Date:   Fri Jan 22 12:19:04 2021 -0500

    Implement Revert and some prefs tweaks

 data/org.gnome.GHex.gschema.xml.in |  2 +-
 src/common-ui.c                    |  2 +-
 src/ghex-application-window.c      | 95 ++++++++++++++++++++++++++++++++++----
 src/gtkhex.c                       | 14 ++++++
 src/preferences.ui                 |  2 +-
 5 files changed, 104 insertions(+), 11 deletions(-)
---
diff --git a/data/org.gnome.GHex.gschema.xml.in b/data/org.gnome.GHex.gschema.xml.in
index 79dc2b87..d6250c9a 100644
--- a/data/org.gnome.GHex.gschema.xml.in
+++ b/data/org.gnome.GHex.gschema.xml.in
@@ -34,7 +34,7 @@
                        <default>0</default>
                </key>
                <key name="show-offsets" type="b">
-                       <default>false</default>
+                       <default>true</default>
                </key>
                <key name="dark-mode" enum="org.gnome.GHex.DarkMode">
                        <default>'system'</default>
diff --git a/src/common-ui.c b/src/common-ui.c
index 40ccaa41..00ad5ca5 100644
--- a/src/common-ui.c
+++ b/src/common-ui.c
@@ -3,7 +3,7 @@
 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 /* common-ui.c - Common UI utility functions
 
-   Copyright (C) 1998 - 2004 Free Software Foundation
+   Copyright © 1998 - 2004 Free Software Foundation
    Copyright © 2021 Logan Rathbone
 
    GHex is free software; you can redistribute it and/or
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index bab0bbee..4a4a7c94 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -113,6 +113,7 @@ static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  */
 static const char *main_actions[] = {
        "ghex.save-as",
+       "ghex.revert",
        "ghex.print",
        "ghex.print-preview",
        "win.group-data-by",
@@ -646,13 +647,22 @@ static void
 close_doc_confirmation_dialog (GHexApplicationWindow *self)
 {
        GtkWidget *dialog;
+       HexDocument *doc;
+
+       g_return_if_fail (GTK_IS_HEX (self->gh));
+
+       doc = gtk_hex_get_document (self->gh);
+       g_return_if_fail (HEX_IS_DOCUMENT (doc));
 
        dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW(self),
                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                        GTK_MESSAGE_QUESTION,
                        GTK_BUTTONS_NONE,
-                       (_("<big><b>You have unsaved changes.</b></big>\n\n"
-                          "Would you like to save your changes?")));
+                       /* Translators: %s is the filename that is currently being
+                        * edited. */
+                       _("<big><b>%s has been edited since opening.</b></big>\n\n"
+                          "Would you like to save your changes?"),
+                       doc->path_end);
 
        gtk_dialog_add_buttons (GTK_DIALOG(dialog),
                        _("_Save Changes"),             GTK_RESPONSE_ACCEPT,
@@ -1006,6 +1016,8 @@ ghex_application_window_set_can_save (GHexApplicationWindow *self,
 
        gtk_widget_action_set_enabled (GTK_WIDGET(self),
                        "ghex.save", can_save);
+       gtk_widget_action_set_enabled (GTK_WIDGET(self),
+                       "ghex.revert", can_save);
 
        g_object_notify_by_pspec (G_OBJECT(self), properties[PROP_CAN_SAVE]);
 }
@@ -1130,6 +1142,73 @@ save_as (GtkWidget *widget,
        g_clear_object (&existing_file);
 }
 
+
+static void
+revert_response_cb (GtkDialog *dialog,
+               int response_id,
+               gpointer user_data)
+{
+       GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(user_data);
+       HexDocument *doc;
+       char *gtk_file_name;
+
+       if (response_id != GTK_RESPONSE_ACCEPT)
+               goto end;
+
+       doc = gtk_hex_get_document (self->gh);
+       gtk_file_name = g_filename_to_utf8 (doc->file_name,
+                       -1, NULL, NULL, NULL);
+
+       hex_document_read (doc);
+
+       g_free(gtk_file_name);
+
+end:
+       gtk_window_destroy (GTK_WINDOW(dialog));
+}
+
+static void
+revert (GtkWidget *widget,
+               const char *action_name,
+               GVariant *parameter)
+{
+       GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(widget);
+       HexDocument *doc;
+       GtkWidget *dialog;
+       gint reply;
+       
+       g_return_if_fail (GTK_IS_HEX (self->gh));
+
+       doc = gtk_hex_get_document (self->gh);
+       g_return_if_fail (HEX_IS_DOCUMENT (doc));
+
+       /* Yes, this *is* a programmer error. The Revert menu should not be shown
+        * to the user at all if there is nothing to revert. */
+       g_return_if_fail (hex_document_has_changed (doc));
+
+       dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW(self),
+                       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+                       GTK_MESSAGE_QUESTION,
+                       GTK_BUTTONS_NONE,
+                       /* Translators: %s here is the filename the user is being asked to
+                        * confirm whether they want to revert. */
+                       _("<big><b>Are you sure you want to revert %s?</b></big>\n\n"
+                       "Your changes will be lost.\n\n"
+                       "This action cannot be undone."),
+                       doc->path_end);
+
+       gtk_dialog_add_buttons (GTK_DIALOG(dialog),
+                       _("_Revert"), GTK_RESPONSE_ACCEPT,
+                       _("_Go Back"), GTK_RESPONSE_REJECT,
+                       NULL);
+       gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_REJECT);
+
+       g_signal_connect (dialog, "response",
+                       G_CALLBACK (revert_response_cb), self);
+
+       gtk_widget_show (dialog);
+}
+                       
 static void
 print_preview (GtkWidget *widget,
                const char *action_name,
@@ -1618,15 +1697,11 @@ static void
 ghex_notebook_tab_refresh_file_name (GHexNotebookTab *self)
 {
        HexDocument *doc;
-       char *basename;
 
        doc = gtk_hex_get_document (self->gh);
-       basename = g_path_get_basename (doc->file_name);
 
-       gtk_label_set_markup (GTK_LABEL(self->label), basename);
+       gtk_label_set_markup (GTK_LABEL(self->label), doc->path_end);
        tab_bold_label (self, hex_document_has_changed (doc));
-
-       g_free (basename);
 }
 
 static GtkWidget *
@@ -1894,7 +1969,7 @@ ghex_application_window_class_init(GHexApplicationWindowClass *klass)
        properties[PROP_CAN_SAVE] =
                g_param_spec_boolean ("can-save",
                        "Can save",
-                       "Whether the Save button should currently be clickable",
+                       "Whether the Save (or Revert) button should currently be clickable",
                        FALSE,  // gboolean default_value
                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
@@ -1915,6 +1990,10 @@ ghex_application_window_class_init(GHexApplicationWindowClass *klass)
                        NULL,   // GVariant string param_type
                        save_as);
 
+       gtk_widget_class_install_action (widget_class, "ghex.revert",
+                       NULL,   // GVariant string param_type
+                       revert);
+
        gtk_widget_class_install_action (widget_class, "ghex.print",
                        NULL,   // GVariant string param_type
                        do_print);
diff --git a/src/gtkhex.c b/src/gtkhex.c
index 42255a2e..3659f4be 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -2988,6 +2988,20 @@ gtk_hex_class_init (GtkHexClass *klass)
                        "gtkhex.copy",
                        NULL);  // no args.
 
+       /* Ctrl+z - undo */
+       gtk_widget_class_add_binding_action (widget_class,
+                       GDK_KEY_z,
+                       GDK_CONTROL_MASK,
+                       "gtkhex.undo",
+                       NULL);  // no args.
+
+       /* Ctrl+y - redo */
+       gtk_widget_class_add_binding_action (widget_class,
+                       GDK_KEY_y,
+                       GDK_CONTROL_MASK,
+                       "gtkhex.redo",
+                       NULL);  // no args.
+
        // API CHANGES
 //     klass->primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
 //     klass->clipboard = gtk_clipboard_get(GDK_NONE);
diff --git a/src/preferences.ui b/src/preferences.ui
index 99f64331..2751f678 100644
--- a/src/preferences.ui
+++ b/src/preferences.ui
@@ -192,7 +192,7 @@
                                                                                                <property 
name="spacing">12</property>
                                                                                                <child>
                                                                                                        
<object class="GtkCheckButton" id="shaded_box_chkbtn">
-                                                                                                             
  <property name="label" translatable="yes">Print shaded rows:</property>
+                                                                                                             
  <property name="label" translatable="yes">Print alternating shaded rows</property>
                                                                                                        
</object>
                                                                                                </child>
                                                                                        </object> <!-- /print 
shaded hbox -->


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]