[gthumb] template selector: allow to use the editor dialog to set the date format



commit 594522703db48ac9b5b1dcb081acc777d1f233ec
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Wed Jun 2 12:43:31 2021 +0200

    template selector: allow to use the editor dialog to set the date format

 data/ui/code-selector.ui       |  41 ++++++++++++++--
 gthumb/gth-template-selector.c | 106 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 137 insertions(+), 10 deletions(-)
---
diff --git a/data/ui/code-selector.ui b/data/ui/code-selector.ui
index 289fca59..20ed3e19 100644
--- a/data/ui/code-selector.ui
+++ b/data/ui/code-selector.ui
@@ -172,10 +172,43 @@
               </packing>
             </child>
             <child>
-              <object class="GtkEntry" id="custom_date_format_entry">
-                <property name="can-focus">True</property>
-                <property name="invisible-char">●</property>
-                <property name="secondary-icon-name">edit-delete-symbolic</property>
+              <object class="GtkBox" id="custom_date_format_box">
+                <property name="can-focus">False</property>
+                <child>
+                  <object class="GtkEntry" id="custom_date_format_entry">
+                    <property name="visible">True</property>
+                    <property name="can-focus">True</property>
+                    <property name="invisible-char">●</property>
+                    <property name="secondary-icon-name">edit-delete-symbolic</property>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkButton" id="edit_custom_date_format_button">
+                    <property name="visible">True</property>
+                    <property name="can-focus">True</property>
+                    <property name="receives-default">True</property>
+                    <child>
+                      <object class="GtkImage">
+                        <property name="visible">True</property>
+                        <property name="can-focus">False</property>
+                        <property name="icon-name">document-edit-symbolic</property>
+                      </object>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+                <style>
+                  <class name="linked"/>
+                </style>
               </object>
               <packing>
                 <property name="expand">True</property>
diff --git a/gthumb/gth-template-selector.c b/gthumb/gth-template-selector.c
index 6eb97fb9..460a170c 100644
--- a/gthumb/gth-template-selector.c
+++ b/gthumb/gth-template-selector.c
@@ -22,6 +22,7 @@
 #include <config.h>
 #include <glib/gi18n.h>
 #include "gth-template-selector.h"
+#include "gth-template-editor-dialog.h"
 #include "glib-utils.h"
 #include "gtk-utils.h"
 #include "gth-main.h"
@@ -29,6 +30,18 @@
 
 #define GET_WIDGET(x) (_gtk_builder_get_widget (self->priv->builder, (x)))
 
+
+static GthTemplateCode Date_Format_Special_Codes[] = {
+       { GTH_TEMPLATE_CODE_TYPE_SIMPLE, N_("Year"), 'Y' },
+       { GTH_TEMPLATE_CODE_TYPE_SIMPLE, N_("Month"), 'm' },
+       { GTH_TEMPLATE_CODE_TYPE_SIMPLE, N_("Day of the month"), 'd' },
+       { GTH_TEMPLATE_CODE_TYPE_SIMPLE, N_("Hour"), 'H' },
+       { GTH_TEMPLATE_CODE_TYPE_SIMPLE, N_("Minute"), 'M' },
+       /* Translators: the time second, not the second place. */
+       { GTH_TEMPLATE_CODE_TYPE_SIMPLE, N_("Second"), 'S' },
+};
+
+
 enum {
        TYPE_DATA_COLUMN,
        TYPE_NAME_COLUMN,
@@ -74,12 +87,6 @@ static char *TypeName[] = {
        "quoted"
 };
 
-G_DEFINE_TYPE_WITH_CODE (GthTemplateSelector,
-                        gth_template_selector,
-                        GTK_TYPE_BOX,
-                        G_ADD_PRIVATE (GthTemplateSelector))
-
-
 static char *Default_Date_Formats[] = {
        "%Y-%m-%d--%H.%M.%S",
        "%x %X",
@@ -92,6 +99,14 @@ static char *Default_Date_Formats[] = {
        "%H%M%S",
        NULL
 };
+
+
+G_DEFINE_TYPE_WITH_CODE (GthTemplateSelector,
+                        gth_template_selector,
+                        GTK_TYPE_BOX,
+                        G_ADD_PRIVATE (GthTemplateSelector))
+
+
 static guint  gth_template_selector_signals[LAST_SIGNAL] = { 0 };
 
 
@@ -279,6 +294,81 @@ edit_default_value_button_clicked_cb (GtkButton           *button,
 }
 
 
+static gboolean
+date_template_eval_cb (TemplateFlags   flags,
+                      gunichar        parent_code,
+                      gunichar        code,
+                      char          **args,
+                      GString        *result,
+                      gpointer        user_data)
+{
+       gboolean   preview;
+       GDateTime *timestamp;
+       char      *format;
+       char      *text;
+
+       preview = flags & TEMPLATE_FLAGS_PREVIEW;
+
+       if (preview && (code != 0))
+               g_string_append (result, "<span foreground=\"#4696f8\">");
+
+       switch (code) {
+       case 'Y': /* Year */
+       case 'm': /* Month */
+       case 'd': /* Day of the month  */
+       case 'H': /* Hour */
+       case 'M': /* Minutes */
+       case 'S': /* Seconds */
+               timestamp = g_date_time_new_now_local ();
+               format = g_strdup_printf ("%%%c", code);
+               text = g_date_time_format (timestamp, format);
+               g_string_append (result, text);
+
+               g_free (text);
+               g_free (format);
+               g_date_time_unref (timestamp);
+               break;
+
+       default:
+               break;
+       }
+
+       if (preview && (code != 0))
+               g_string_append (result, "</span>");
+
+       return FALSE;
+}
+
+
+static void
+edit_custom_date_format_button_clicked_cb (GtkButton           *button,
+                                          GthTemplateSelector *self)
+{
+       GtkWidget *toplevel;
+       GtkWidget *dialog;
+
+       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
+       if (! GTK_IS_WINDOW (toplevel))
+               toplevel = NULL;
+
+       dialog = gth_template_editor_dialog_new (Date_Format_Special_Codes,
+                                                G_N_ELEMENTS (Date_Format_Special_Codes),
+                                                0,
+                                                _("Edit Template"),
+                                                GTK_WINDOW (toplevel));
+       gth_template_editor_dialog_set_preview_cb (GTH_TEMPLATE_EDITOR_DIALOG (dialog),
+                                                  date_template_eval_cb,
+                                                  self);
+       gth_template_editor_dialog_set_template (GTH_TEMPLATE_EDITOR_DIALOG (dialog),
+                                                gtk_entry_get_text (GTK_ENTRY (GET_WIDGET 
("custom_date_format_entry"))));
+       g_signal_connect (dialog,
+                         "response",
+                         G_CALLBACK (gth_template_editor_dialog_default_response),
+                         GET_WIDGET ("custom_date_format_entry"));
+       gtk_widget_show (dialog);
+}
+
+
 static void
 gth_template_selector_construct (GthTemplateSelector  *self,
                                 GthTemplateCode      *allowed_codes,
@@ -459,6 +549,10 @@ gth_template_selector_construct (GthTemplateSelector  *self,
                          "clicked",
                          G_CALLBACK (edit_default_value_button_clicked_cb),
                          self);
+       g_signal_connect (GET_WIDGET ("edit_custom_date_format_button"),
+                         "clicked",
+                         G_CALLBACK (edit_custom_date_format_button_clicked_cb),
+                         self);
 }
 
 


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