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.MenuManager;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.Menu;
24 private import gio.MenuModel;
25 private import glib.ConstructionException;
26 private import glib.ErrorG;
27 private import glib.GException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 
31 
32 /**
33  * The goal of #DzlMenuManager is to simplify the process of merging multiple
34  * GtkBuilder .ui files containing menus into a single representation of the
35  * application menus. Additionally, it provides the ability to "unmerge"
36  * previously merged menus.
37  * 
38  * This allows for an application to have plugins which seemlessly extends
39  * the core application menus.
40  * 
41  * Implementation notes:
42  * 
43  * To make this work, we don't use the GMenu instances created by a GtkBuilder
44  * instance. Instead, we create the menus ourself and recreate section and
45  * submenu links. This allows the #DzlMenuManager to be in full control of
46  * the generated menus.
47  * 
48  * dzl_menu_manager_get_menu_by_id() will always return a #GMenu, however
49  * that menu may contain no children until something has extended it later
50  * on during the application process.
51  *
52  * Since: 3.26
53  */
54 public class MenuManager : ObjectG
55 {
56 	/** the main Gtk struct */
57 	protected DzlMenuManager* dzlMenuManager;
58 
59 	/** Get the main Gtk struct */
60 	public DzlMenuManager* getMenuManagerStruct(bool transferOwnership = false)
61 	{
62 		if (transferOwnership)
63 			ownedRef = false;
64 		return dzlMenuManager;
65 	}
66 
67 	/** the main Gtk struct as a void* */
68 	protected override void* getStruct()
69 	{
70 		return cast(void*)dzlMenuManager;
71 	}
72 
73 	/**
74 	 * Sets our main struct and passes it to the parent class.
75 	 */
76 	public this (DzlMenuManager* dzlMenuManager, bool ownedRef = false)
77 	{
78 		this.dzlMenuManager = dzlMenuManager;
79 		super(cast(GObject*)dzlMenuManager, ownedRef);
80 	}
81 
82 
83 	/** */
84 	public static GType getType()
85 	{
86 		return dzl_menu_manager_get_type();
87 	}
88 
89 	/** */
90 	public this()
91 	{
92 		auto p = dzl_menu_manager_new();
93 
94 		if(p is null)
95 		{
96 			throw new ConstructionException("null returned by new");
97 		}
98 
99 		this(cast(DzlMenuManager*) p, true);
100 	}
101 
102 	/** */
103 	public uint addFilename(string filename)
104 	{
105 		GError* err = null;
106 
107 		auto p = dzl_menu_manager_add_filename(dzlMenuManager, Str.toStringz(filename), &err);
108 
109 		if (err !is null)
110 		{
111 			throw new GException( new ErrorG(err) );
112 		}
113 
114 		return p;
115 	}
116 
117 	/** */
118 	public uint addResource(string resource)
119 	{
120 		GError* err = null;
121 
122 		auto p = dzl_menu_manager_add_resource(dzlMenuManager, Str.toStringz(resource), &err);
123 
124 		if (err !is null)
125 		{
126 			throw new GException( new ErrorG(err) );
127 		}
128 
129 		return p;
130 	}
131 
132 	/**
133 	 * Returns: A #GMenu.
134 	 */
135 	public Menu getMenuById(string menuId)
136 	{
137 		auto p = dzl_menu_manager_get_menu_by_id(dzlMenuManager, Str.toStringz(menuId));
138 
139 		if(p is null)
140 		{
141 			return null;
142 		}
143 
144 		return ObjectG.getDObject!(Menu)(cast(GMenu*) p);
145 	}
146 
147 	/** */
148 	public uint merge(string menuId, MenuModel model)
149 	{
150 		return dzl_menu_manager_merge(dzlMenuManager, Str.toStringz(menuId), (model is null) ? null : model.getMenuModelStruct());
151 	}
152 
153 	/**
154 	 * This removes items from menus that were added as part of a previous
155 	 * menu merge. Use the value returned from dzl_menu_manager_merge() as
156 	 * the @merge_id.
157 	 *
158 	 * Params:
159 	 *     mergeId = A previously registered merge id
160 	 *
161 	 * Since: 3.26
162 	 */
163 	public void remove(uint mergeId)
164 	{
165 		dzl_menu_manager_remove(dzlMenuManager, mergeId);
166 	}
167 }