1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-dazzle is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.ShortcutManager;
20 
21 private import dazzle.ShortcutTheme;
22 private import dazzle.ShortcutsWindow;
23 private import dazzle.c.functions;
24 public  import dazzle.c.types;
25 private import gio.Cancellable;
26 private import gio.InitableIF;
27 private import gio.InitableT;
28 private import gio.ListModelIF;
29 private import gio.ListModelT;
30 private import glib.Str;
31 private import gobject.ObjectG;
32 private import gobject.Signals;
33 private import gtk.Widget;
34 private import std.algorithm;
35 
36 
37 /** */
38 public class ShortcutManager : ObjectG, InitableIF, ListModelIF
39 {
40 	/** the main Gtk struct */
41 	protected DzlShortcutManager* dzlShortcutManager;
42 
43 	/** Get the main Gtk struct */
44 	public DzlShortcutManager* getShortcutManagerStruct(bool transferOwnership = false)
45 	{
46 		if (transferOwnership)
47 			ownedRef = false;
48 		return dzlShortcutManager;
49 	}
50 
51 	/** the main Gtk struct as a void* */
52 	protected override void* getStruct()
53 	{
54 		return cast(void*)dzlShortcutManager;
55 	}
56 
57 	/**
58 	 * Sets our main struct and passes it to the parent class.
59 	 */
60 	public this (DzlShortcutManager* dzlShortcutManager, bool ownedRef = false)
61 	{
62 		this.dzlShortcutManager = dzlShortcutManager;
63 		super(cast(GObject*)dzlShortcutManager, ownedRef);
64 	}
65 
66 	// add the Initable capabilities
67 	mixin InitableT!(DzlShortcutManager);
68 
69 	// add the ListModel capabilities
70 	mixin ListModelT!(DzlShortcutManager);
71 
72 
73 	/** */
74 	public static GType getType()
75 	{
76 		return dzl_shortcut_manager_get_type();
77 	}
78 
79 	/**
80 	 * Gets the singleton #DzlShortcutManager for the process.
81 	 *
82 	 * Returns: An #DzlShortcutManager.
83 	 */
84 	public static ShortcutManager getDefault()
85 	{
86 		auto p = dzl_shortcut_manager_get_default();
87 
88 		if(p is null)
89 		{
90 			return null;
91 		}
92 
93 		return ObjectG.getDObject!(ShortcutManager)(cast(DzlShortcutManager*) p);
94 	}
95 
96 	/** */
97 	public void addAction(string detailedActionName, string section, string group, string title, string subtitle)
98 	{
99 		dzl_shortcut_manager_add_action(dzlShortcutManager, Str.toStringz(detailedActionName), Str.toStringz(section), Str.toStringz(group), Str.toStringz(title), Str.toStringz(subtitle));
100 	}
101 
102 	/** */
103 	public void addCommand(string command, string section, string group, string title, string subtitle)
104 	{
105 		dzl_shortcut_manager_add_command(dzlShortcutManager, Str.toStringz(command), Str.toStringz(section), Str.toStringz(group), Str.toStringz(title), Str.toStringz(subtitle));
106 	}
107 
108 	/**
109 	 * This method will add @shortcuts to the #DzlShortcutManager.
110 	 *
111 	 * This provides a simple way for widgets to add their shortcuts to the manager
112 	 * so that they may be overriden by themes or the end user.
113 	 *
114 	 * Params:
115 	 *     shortcuts = shortcuts to add
116 	 *     translationDomain = the gettext domain to use for translations
117 	 */
118 	public void addShortcutEntries(DzlShortcutEntry[] shortcuts, string translationDomain)
119 	{
120 		dzl_shortcut_manager_add_shortcut_entries(dzlShortcutManager, shortcuts.ptr, cast(uint)shortcuts.length, Str.toStringz(translationDomain));
121 	}
122 
123 	/**
124 	 * Adds shortcuts registered with the #DzlShortcutManager to the
125 	 * #DzlShortcutsWindow.
126 	 *
127 	 * Params:
128 	 *     window = A #DzlShortcutsWindow
129 	 */
130 	public void addShortcutsToWindow(ShortcutsWindow window)
131 	{
132 		dzl_shortcut_manager_add_shortcuts_to_window(dzlShortcutManager, (window is null) ? null : window.getShortcutsWindowStruct());
133 	}
134 
135 	/** */
136 	public void appendSearchPath(string directory)
137 	{
138 		dzl_shortcut_manager_append_search_path(dzlShortcutManager, Str.toStringz(directory));
139 	}
140 
141 	/**
142 	 * Gets the "theme" property.
143 	 *
144 	 * Returns: An #DzlShortcutTheme.
145 	 */
146 	public ShortcutTheme getTheme()
147 	{
148 		auto p = dzl_shortcut_manager_get_theme(dzlShortcutManager);
149 
150 		if(p is null)
151 		{
152 			return null;
153 		}
154 
155 		return ObjectG.getDObject!(ShortcutTheme)(cast(DzlShortcutTheme*) p);
156 	}
157 
158 	/**
159 	 * Locates a theme by the name of the theme.
160 	 *
161 	 * If @theme_name is %NULL, then the internal theme is used. You probably dont
162 	 * need to use that as it is used by various controllers to hook up their
163 	 * default actions.
164 	 *
165 	 * Params:
166 	 *     themeName = the name of a theme or %NULL of the internal theme
167 	 *
168 	 * Returns: A #DzlShortcutTheme or %NULL.
169 	 */
170 	public ShortcutTheme getThemeByName(string themeName)
171 	{
172 		auto p = dzl_shortcut_manager_get_theme_by_name(dzlShortcutManager, Str.toStringz(themeName));
173 
174 		if(p is null)
175 		{
176 			return null;
177 		}
178 
179 		return ObjectG.getDObject!(ShortcutTheme)(cast(DzlShortcutTheme*) p);
180 	}
181 
182 	/** */
183 	public string getThemeName()
184 	{
185 		return Str.toString(dzl_shortcut_manager_get_theme_name(dzlShortcutManager));
186 	}
187 
188 	/** */
189 	public string getUserDir()
190 	{
191 		return Str.toString(dzl_shortcut_manager_get_user_dir(dzlShortcutManager));
192 	}
193 
194 	/**
195 	 * This function will try to dispatch @event to the proper widget and
196 	 * #DzlShortcutContext. If the event is handled, then %TRUE is returned.
197 	 *
198 	 * You should call this from #GtkWidget::key-press-event handler in your
199 	 * #GtkWindow toplevel.
200 	 *
201 	 * Params:
202 	 *     event = A #GdkEventKey event to handle.
203 	 *     toplevel = A #GtkWidget or %NULL.
204 	 *
205 	 * Returns: %TRUE if the event was handled.
206 	 */
207 	public bool handleEvent(GdkEventKey* event, Widget toplevel)
208 	{
209 		return dzl_shortcut_manager_handle_event(dzlShortcutManager, event, (toplevel is null) ? null : toplevel.getWidgetStruct()) != 0;
210 	}
211 
212 	/** */
213 	public void prependSearchPath(string directory)
214 	{
215 		dzl_shortcut_manager_prepend_search_path(dzlShortcutManager, Str.toStringz(directory));
216 	}
217 
218 	/** */
219 	public void queueReload()
220 	{
221 		dzl_shortcut_manager_queue_reload(dzlShortcutManager);
222 	}
223 
224 	/** */
225 	public void reload(Cancellable cancellable)
226 	{
227 		dzl_shortcut_manager_reload(dzlShortcutManager, (cancellable is null) ? null : cancellable.getCancellableStruct());
228 	}
229 
230 	/** */
231 	public void removeSearchPath(string directory)
232 	{
233 		dzl_shortcut_manager_remove_search_path(dzlShortcutManager, Str.toStringz(directory));
234 	}
235 
236 	/**
237 	 * Sets the theme for the shortcut manager.
238 	 *
239 	 * Params:
240 	 *     theme = An #DzlShortcutTheme
241 	 */
242 	public void setTheme(ShortcutTheme theme)
243 	{
244 		dzl_shortcut_manager_set_theme(dzlShortcutManager, (theme is null) ? null : theme.getShortcutThemeStruct());
245 	}
246 
247 	/** */
248 	public void setThemeName(string themeName)
249 	{
250 		dzl_shortcut_manager_set_theme_name(dzlShortcutManager, Str.toStringz(themeName));
251 	}
252 
253 	/** */
254 	public void setUserDir(string userDir)
255 	{
256 		dzl_shortcut_manager_set_user_dir(dzlShortcutManager, Str.toStringz(userDir));
257 	}
258 
259 	/** */
260 	gulong addOnChanged(void delegate(ShortcutManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
261 	{
262 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
263 	}
264 }