1 /*
2 * Copyright (C) 2013-2021 Graeme Gott <graeme@gottcode.org>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "command-edit.h"
19
20 #include "command.h"
21 #include "slot.h"
22
23 #include <glib/gi18n-lib.h>
24
25 using namespace WhiskerMenu;
26
27 //-----------------------------------------------------------------------------
28
CommandEdit(Command * command,GtkSizeGroup * label_size_group)29 CommandEdit::CommandEdit(Command* command, GtkSizeGroup* label_size_group) :
30 m_command(command)
31 {
32 m_widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
33
34 bool active = m_command->get_shown();
35
36 // Add toggle to hide and disable command
37 m_shown = GTK_TOGGLE_BUTTON(gtk_check_button_new_with_mnemonic(m_command->get_text()));
38 gtk_toggle_button_set_active(m_shown, active);
39 gtk_box_pack_start(GTK_BOX(m_widget), GTK_WIDGET(m_shown), false, false, 0);
40 gtk_size_group_add_widget(label_size_group, GTK_WIDGET(m_shown));
41
42 connect(m_shown, "toggled",
43 [this](GtkToggleButton* button)
44 {
45 const bool active = gtk_toggle_button_get_active(button);
46 m_command->set_shown(active);
47 gtk_widget_set_sensitive(GTK_WIDGET(m_entry), active);
48 gtk_widget_set_sensitive(GTK_WIDGET(m_browse_button), active);
49 });
50
51 // Add entry to set command
52 m_entry = GTK_ENTRY(gtk_entry_new());
53 gtk_entry_set_text(m_entry, m_command->get());
54 gtk_widget_set_sensitive(GTK_WIDGET(m_entry), active);
55 gtk_box_pack_start(GTK_BOX(m_widget), GTK_WIDGET(m_entry), true, true, 0);
56
57 connect(m_entry, "changed",
58 [this](GtkEditable* entry)
59 {
60 m_command->set(gtk_entry_get_text(GTK_ENTRY(entry)));
61 });
62
63 // Add browse button
64 m_browse_button = gtk_button_new();
65 gtk_widget_set_tooltip_text(m_browse_button, _("Browse the file system to choose a custom command."));
66 gtk_widget_set_sensitive(GTK_WIDGET(m_browse_button), active);
67 gtk_box_pack_start(GTK_BOX(m_widget), m_browse_button, false, false, 0);
68
69 GtkWidget* image = gtk_image_new_from_icon_name("document-open", GTK_ICON_SIZE_BUTTON);
70 gtk_container_add(GTK_CONTAINER(m_browse_button), image);
71
72 connect(m_browse_button, "clicked",
73 [this](GtkButton*)
74 {
75 GtkFileChooser* chooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(_("Select Command"),
76 GTK_WINDOW(gtk_widget_get_toplevel(m_widget)),
77 GTK_FILE_CHOOSER_ACTION_OPEN,
78 _("_Cancel"), GTK_RESPONSE_CANCEL,
79 _("_OK"), GTK_RESPONSE_ACCEPT,
80 nullptr));
81 gtk_file_chooser_set_local_only(chooser, true);
82 gtk_file_chooser_set_current_folder(chooser, BINDIR);
83
84 // Select current command
85 gchar* filename = g_strdup(m_command->get());
86 if (filename)
87 {
88 // Make sure command is absolute path
89 if (!g_path_is_absolute(filename))
90 {
91 gchar* absolute_path = g_find_program_in_path(filename);
92 if (absolute_path)
93 {
94 g_free(filename);
95 filename = absolute_path;
96 }
97 }
98
99 if (g_path_is_absolute(filename))
100 {
101 gtk_file_chooser_set_filename(chooser, filename);
102 }
103 g_free(filename);
104 }
105
106 // Set new command
107 if (gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_ACCEPT)
108 {
109 filename = gtk_file_chooser_get_filename(chooser);
110 gtk_entry_set_text(m_entry, filename);
111 g_free(filename);
112 }
113
114 gtk_widget_destroy(GTK_WIDGET(chooser));
115 });
116 }
117
118 //-----------------------------------------------------------------------------
119