File: command-edit.cpp

package info (click to toggle)
xfce4-whiskermenu-plugin 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,692 kB
  • sloc: cpp: 8,401; ansic: 26; makefile: 16
file content (118 lines) | stat: -rw-r--r-- 3,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2013-2021 Graeme Gott <graeme@gottcode.org>
 *
 * This library 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 library 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 library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "command-edit.h"

#include "command.h"
#include "slot.h"

#include <glib/gi18n-lib.h>

using namespace WhiskerMenu;

//-----------------------------------------------------------------------------

CommandEdit::CommandEdit(Command* command, GtkSizeGroup* label_size_group) :
	m_command(command)
{
	m_widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);

	const bool is_active = m_command->get_shown();

	// Add toggle to hide and disable command
	m_shown = GTK_TOGGLE_BUTTON(gtk_check_button_new_with_mnemonic(m_command->get_text()));
	gtk_toggle_button_set_active(m_shown, is_active);
	gtk_box_pack_start(GTK_BOX(m_widget), GTK_WIDGET(m_shown), false, false, 0);
	gtk_size_group_add_widget(label_size_group, GTK_WIDGET(m_shown));

	connect(m_shown, "toggled",
		[this](GtkToggleButton* button)
		{
			const bool active = gtk_toggle_button_get_active(button);
			m_command->set_shown(active);
			gtk_widget_set_sensitive(GTK_WIDGET(m_entry), active);
			gtk_widget_set_sensitive(GTK_WIDGET(m_browse_button), active);
		});

	// Add entry to set command
	m_entry = GTK_ENTRY(gtk_entry_new());
	gtk_entry_set_text(m_entry, m_command->get());
	gtk_widget_set_sensitive(GTK_WIDGET(m_entry), is_active);
	gtk_box_pack_start(GTK_BOX(m_widget), GTK_WIDGET(m_entry), true, true, 0);

	connect(m_entry, "changed",
		[this](GtkEditable* entry)
		{
			m_command->set(gtk_entry_get_text(GTK_ENTRY(entry)));
		});

	// Add browse button
	m_browse_button = gtk_button_new();
	gtk_widget_set_tooltip_text(m_browse_button, _("Browse the file system to choose a custom command."));
	gtk_widget_set_sensitive(GTK_WIDGET(m_browse_button), is_active);
	gtk_box_pack_start(GTK_BOX(m_widget), m_browse_button, false, false, 0);

	GtkWidget* image = gtk_image_new_from_icon_name("document-open", GTK_ICON_SIZE_BUTTON);
	gtk_container_add(GTK_CONTAINER(m_browse_button), image);

	connect(m_browse_button, "clicked",
		[this](GtkButton*)
		{
			GtkFileChooser* chooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(_("Select Command"),
					GTK_WINDOW(gtk_widget_get_toplevel(m_widget)),
					GTK_FILE_CHOOSER_ACTION_OPEN,
					_("_Cancel"), GTK_RESPONSE_CANCEL,
					_("_OK"), GTK_RESPONSE_ACCEPT,
					nullptr));
			gtk_file_chooser_set_local_only(chooser, true);
			gtk_file_chooser_set_current_folder(chooser, BINDIR);

			// Select current command
			gchar* filename = g_strdup(m_command->get());
			if (filename)
			{
				// Make sure command is absolute path
				if (!g_path_is_absolute(filename))
				{
					gchar* absolute_path = g_find_program_in_path(filename);
					if (absolute_path)
					{
						g_free(filename);
						filename = absolute_path;
					}
				}

				if (g_path_is_absolute(filename))
				{
					gtk_file_chooser_set_filename(chooser, filename);
				}
				g_free(filename);
			}

			// Set new command
			if (gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_ACCEPT)
			{
				filename = gtk_file_chooser_get_filename(chooser);
				gtk_entry_set_text(m_entry, filename);
				g_free(filename);
			}

			gtk_widget_destroy(GTK_WIDGET(chooser));
		});
}

//-----------------------------------------------------------------------------