Convert search-and-run window to GtkBuilder .ui file
authorColin Clark <colin.clark@cclark.uk>
Tue, 27 Jun 2023 14:11:49 +0000 (15:11 +0100)
committerColin Clark <colin.clark@cclark.uk>
Tue, 27 Jun 2023 14:11:49 +0000 (15:11 +0100)
Conversion done only for future reference.

meson.build
src/main.h
src/meson.build
src/search-and-run.cc
src/ui/meson.build [new file with mode: 0644]
src/ui/search-and-run.ui [new file with mode: 0644]
src/ui/ui.gresource.xml [new file with mode: 0644]

index 412399f..444305b 100644 (file)
@@ -591,6 +591,9 @@ configure_file(input : 'config.h.in',
 script_sources = []
 subdir('scripts')
 
+# For gtk builder checks on .ui files
+ui_sources = []
+
 # Process subdirs before the sources
 subdir('po')
 subdir('plugins')
@@ -662,3 +665,11 @@ if shellcheck_exe.found()
 else
     summary({'shellcheck' : ['Test runs:', false]}, section : 'Testing', bool_yn : true)
 endif
+
+gtk_builder_tool = find_program('gtk-builder-tool', required : false)
+if gtk_builder_tool.found()
+    test('UI Build', gtk_builder_tool, args: ['validate', ui_sources], timeout: 100)
+    summary({'gtk-builder-tool' : ['Test runs:', true]}, section : 'Testing', bool_yn : true)
+else
+    summary({'gtk-builder-tool' : ['Test runs:', false]}, section : 'Testing', bool_yn : true)
+endif
index 5d1f4dd..6f5ef69 100644 (file)
@@ -82,6 +82,7 @@
 #define GQ_ARCHIVE_DIR "geeqie-archive"
 #define GQ_RESOURCE_PATH_ICONS "/org/geeqie/icons"
 #define GQ_RESOURCE_PATH_CREDITS "/org/geeqie/credits"
+#define GQ_RESOURCE_PATH_UI "/org/geeqie/ui"
 
 #define GQ_SYSTEM_WIDE_DIR    "/etc/" GQ_APPNAME_LC
 
index e9133ce..c430d6e 100644 (file)
@@ -239,6 +239,7 @@ project_sources += main_sources
 subdir('authors')
 subdir('icons')
 subdir('pan-view')
+subdir('ui')
 subdir('view-file')
 
 gqmarshal = gnome.genmarshal('gq-marshal', prefix : 'gq_marshal', sources : 'gq-marshal.list')
index 613443d..31949b3 100644 (file)
@@ -33,13 +33,11 @@ enum {
 struct SarData
 {
        GtkWidget *window;
-       GtkWidget *vbox;
-       GtkWidget *entry_box;
-       GtkEntryCompletion *completion;
        GtkListStore *command_store;
        GtkAction *action;
        LayoutWindow *lw;
        gboolean match_found;
+       GtkBuilder *builder;
 };
 
 static gint sort_iter_compare_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer UNUSED(data))
@@ -88,8 +86,7 @@ static void command_store_populate(SarData* sar)
        gboolean duplicate_command;
        gchar *accel;
 
-       sar->command_store = gtk_list_store_new(SAR_COUNT, G_TYPE_STRING, G_TYPE_OBJECT);
-
+       sar->command_store = GTK_LIST_STORE(gtk_builder_get_object(sar->builder, "command_store"));
        sortable = GTK_TREE_SORTABLE(sar->command_store);
        gtk_tree_sortable_set_sort_func(sortable, SAR_LABEL, sort_iter_compare_func, nullptr, nullptr);
 
@@ -176,9 +173,12 @@ static gboolean search_and_run_destroy(gpointer data)
        auto sar = static_cast<SarData *>(data);
 
        sar->lw->sar_window = nullptr;
+       g_object_unref(gtk_builder_get_object(sar->builder, "completion"));
+       g_object_unref(gtk_builder_get_object(sar->builder, "command_store"));
        gtk_widget_destroy(sar->window);
+       g_free(sar);
 
-       return G_SOURCE_CONTINUE;
+       return G_SOURCE_REMOVE;
 }
 
 static gboolean entry_box_activate_cb(GtkWidget *UNUSED(widget), gpointer data)
@@ -280,47 +280,29 @@ static gboolean match_func(GtkEntryCompletion *completion, const gchar *key, Gtk
 GtkWidget *search_and_run_new(LayoutWindow *lw)
 {
        SarData *sar;
-       GdkGeometry geometry;
+       gchar *ui_path;
 
        sar = g_new0(SarData, 1);
        sar->lw = lw;
-       sar->window = window_new(GTK_WINDOW_TOPLEVEL, "sar_window", nullptr, nullptr, _("Search and Run command"));
-       DEBUG_NAME(sar->window);
 
-       geometry.min_width = 500;
-       geometry.max_width = 1500;
-       geometry.min_height = 10;
-       geometry.max_height = 10;
-       gtk_window_set_geometry_hints(GTK_WINDOW(sar->window), nullptr, &geometry, static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));
+       ui_path = g_build_filename(GQ_RESOURCE_PATH_UI, "search-and-run.ui", nullptr);
 
-       gtk_window_set_resizable(GTK_WINDOW(sar->window), TRUE);
-
-       sar->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
-       gtk_container_add(GTK_CONTAINER(sar->window), sar->vbox);
-       gtk_widget_show(sar->vbox);
+       sar->builder = gtk_builder_new_from_resource(ui_path);
+       command_store_populate(sar);
 
-       sar->entry_box = gtk_entry_new();
-       gtk_box_pack_start(GTK_BOX(sar->vbox), sar->entry_box, FALSE, FALSE, 0);
-       gtk_widget_show(sar->entry_box);
-       gtk_entry_set_icon_from_stock(GTK_ENTRY(sar->entry_box), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
-       gtk_widget_show(sar->vbox);
-       gtk_widget_set_tooltip_text(sar->entry_box, "Search for commands and run them");
-       g_signal_connect(G_OBJECT(sar->entry_box), "key_press_event", G_CALLBACK(keypress_cb), sar);
-       g_signal_connect(G_OBJECT(sar->entry_box), "activate", G_CALLBACK(entry_box_activate_cb), sar);
+       sar->window = GTK_WIDGET(gtk_builder_get_object(sar->builder, "search_and_run"));
+       DEBUG_NAME(sar->window);
 
-       command_store_populate(sar);
+       gtk_entry_completion_set_match_func(GTK_ENTRY_COMPLETION(gtk_builder_get_object(sar->builder, "completion")), match_func, sar, nullptr);
 
-       sar->completion = gtk_entry_completion_new();
-       gtk_entry_set_completion(GTK_ENTRY(sar->entry_box), sar->completion);
-       gtk_entry_completion_set_inline_completion(sar->completion, FALSE);
-       gtk_entry_completion_set_inline_selection(sar->completion, FALSE);
-       gtk_entry_completion_set_minimum_key_length(sar->completion, 1);
-       gtk_entry_completion_set_match_func(sar->completion, match_func, sar, nullptr);
-       g_signal_connect(G_OBJECT(sar->completion), "match-selected", G_CALLBACK(match_selected_cb), sar);
-       gtk_entry_completion_set_model(sar->completion, GTK_TREE_MODEL(sar->command_store));
-       gtk_entry_completion_set_text_column(sar->completion, SAR_LABEL);
+       g_signal_connect(G_OBJECT(gtk_builder_get_object(sar->builder, "completion")), "match-selected", G_CALLBACK(match_selected_cb), sar);
+       g_signal_connect(G_OBJECT(gtk_builder_get_object(sar->builder, "entry")), "key_press_event", G_CALLBACK(keypress_cb), sar);
+       g_signal_connect(G_OBJECT(gtk_builder_get_object(sar->builder, "entry")), "activate", G_CALLBACK(entry_box_activate_cb), sar);
 
        gtk_widget_show(sar->window);
+       g_free(ui_path);
+
        return sar->window;
 }
+
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
diff --git a/src/ui/meson.build b/src/ui/meson.build
new file mode 100644 (file)
index 0000000..a51da49
--- /dev/null
@@ -0,0 +1,29 @@
+# This file is a part of Geeqie project (https://www.geeqie.org/).
+# Copyright (C) 2008 - 2023 The Geeqie Team
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#
+
+target_string = '--target=' + meson.current_build_dir() + '/ui.h'
+ui_resources_h = custom_target('ui_resources_h',
+                input : 'ui.gresource.xml',
+                output : 'ui.h',
+                command : [glib_compile_resources, '--generate-header', target_string, '--sourcedir=@CURRENT_SOURCE_DIR@', '@INPUT@'])
+
+target_string = '--target=' + meson.current_build_dir() + '/ui.c'
+ui_resources_c = custom_target('ui_resources_c',
+                input : 'ui.gresource.xml',
+                output : 'ui.c',
+                command : [glib_compile_resources, '--generate-source', target_string, '--sourcedir=@CURRENT_SOURCE_DIR@', '@INPUT@'])
+
+project_sources += ui_resources_h
+project_sources += ui_resources_c
+
+ui_sources += files('search-and-run.ui')
diff --git a/src/ui/search-and-run.ui b/src/ui/search-and-run.ui
new file mode 100644 (file)
index 0000000..d3d6cc3
--- /dev/null
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.40.0 -->
+<interface>
+  <requires lib="gtk+" version="3.24"/>
+  <object class="GtkListStore" id="command_store">
+    <columns>
+      <!-- column-name gchararray1 -->
+      <column type="gchararray"/>
+      <!-- column-name GObject1 -->
+      <column type="GObject"/>
+    </columns>
+  </object>
+  <object class="GtkEntryCompletion" id="completion">
+    <property name="model">command_store</property>
+    <property name="text-column">0</property>
+    <child>
+      <object class="GtkCellRendererText" id="cellrenderertext1"/>
+      <attributes>
+        <attribute name="text">0</attribute>
+      </attributes>
+    </child>
+  </object>
+  <object class="GtkWindow" id="search_and_run">
+    <property name="name">sar_window</property>
+    <property name="width-request">500</property>
+    <property name="height-request">10</property>
+    <property name="can-focus">False</property>
+    <property name="valign">start</property>
+    <property name="hexpand">False</property>
+    <property name="title" translatable="yes">Search and Run command - Geeqie</property>
+    <property name="role">sar_window</property>
+    <property name="resizable">False</property>
+    <property name="modal">True</property>
+    <property name="icon-name">geeqie.png</property>
+    <child>
+      <object class="GtkEntry" id="entry">
+        <property name="visible">True</property>
+        <property name="can-focus">True</property>
+        <property name="tooltip-text" translatable="yes">Search for commands and run them</property>
+        <property name="primary-icon-name">edit-find</property>
+        <property name="secondary-icon-tooltip-text" translatable="yes">Search for commands and run them</property>
+        <property name="completion">completion</property>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/src/ui/ui.gresource.xml b/src/ui/ui.gresource.xml
new file mode 100644 (file)
index 0000000..a0613de
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ /*
+ * Copyright (C) 2023 The Geeqie Team
+ *
+ * Author: Colin Clark
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+-->
+
+<gresources>
+  <gresource prefix="/org/geeqie/ui">
+    <file>search-and-run.ui</file>
+  </gresource>
+</gresources>