1 module webkit2webextension.ContextMenu;
2 
3 private import glib.ConstructionException;
4 private import glib.ListG;
5 private import glib.Variant;
6 private import gobject.ObjectG;
7 private import webkit2webextension.ContextMenuItem;
8 private import webkit2webextension.c.functions;
9 public  import webkit2webextension.c.types;
10 
11 
12 /**
13  * #WebKitContextMenu represents a context menu containing
14  * #WebKitContextMenuItem<!-- -->s in a #WebKitWebView.
15  * 
16  * When a #WebKitWebView is about to display the context menu, it
17  * emits the #WebKitWebView::context-menu signal, which has the
18  * #WebKitContextMenu as an argument. You can modify it, adding new
19  * submenus that you can create with webkit_context_menu_new(), adding
20  * new #WebKitContextMenuItem<!-- -->s with
21  * webkit_context_menu_prepend(), webkit_context_menu_append() or
22  * webkit_context_menu_insert(), maybe after having removed the
23  * existing ones with webkit_context_menu_remove_all().
24  */
25 public class ContextMenu : ObjectG
26 {
27 	/** the main Gtk struct */
28 	protected WebKitContextMenu* webKitContextMenu;
29 
30 	/** Get the main Gtk struct */
31 	public WebKitContextMenu* getContextMenuStruct(bool transferOwnership = false)
32 	{
33 		if (transferOwnership)
34 			ownedRef = false;
35 		return webKitContextMenu;
36 	}
37 
38 	/** the main Gtk struct as a void* */
39 	protected override void* getStruct()
40 	{
41 		return cast(void*)webKitContextMenu;
42 	}
43 
44 	/**
45 	 * Sets our main struct and passes it to the parent class.
46 	 */
47 	public this (WebKitContextMenu* webKitContextMenu, bool ownedRef = false)
48 	{
49 		this.webKitContextMenu = webKitContextMenu;
50 		super(cast(GObject*)webKitContextMenu, ownedRef);
51 	}
52 
53 
54 	/** */
55 	public static GType getType()
56 	{
57 		return webkit_context_menu_get_type();
58 	}
59 
60 	/**
61 	 * Creates a new #WebKitContextMenu object to be used as a submenu of an existing
62 	 * #WebKitContextMenu. The context menu of a #WebKitWebView is created by the view
63 	 * and passed as an argument of #WebKitWebView::context-menu signal.
64 	 * To add items to the menu use webkit_context_menu_prepend(),
65 	 * webkit_context_menu_append() or webkit_context_menu_insert().
66 	 * See also webkit_context_menu_new_with_items() to create a #WebKitContextMenu with
67 	 * a list of initial items.
68 	 *
69 	 * Returns: The newly created #WebKitContextMenu object
70 	 *
71 	 * Throws: ConstructionException GTK+ fails to create the object.
72 	 */
73 	public this()
74 	{
75 		auto __p = webkit_context_menu_new();
76 
77 		if(__p is null)
78 		{
79 			throw new ConstructionException("null returned by new");
80 		}
81 
82 		this(cast(WebKitContextMenu*) __p, true);
83 	}
84 
85 	/**
86 	 * Creates a new #WebKitContextMenu object to be used as a submenu of an existing
87 	 * #WebKitContextMenu with the given initial items.
88 	 * See also webkit_context_menu_new()
89 	 *
90 	 * Params:
91 	 *     items = a #GList of #WebKitContextMenuItem
92 	 *
93 	 * Returns: The newly created #WebKitContextMenu object
94 	 *
95 	 * Throws: ConstructionException GTK+ fails to create the object.
96 	 */
97 	public this(ListG items)
98 	{
99 		auto __p = webkit_context_menu_new_with_items((items is null) ? null : items.getListGStruct());
100 
101 		if(__p is null)
102 		{
103 			throw new ConstructionException("null returned by new_with_items");
104 		}
105 
106 		this(cast(WebKitContextMenu*) __p, true);
107 	}
108 
109 	/**
110 	 * Adds @item at the end of the @menu.
111 	 *
112 	 * Params:
113 	 *     item = the #WebKitContextMenuItem to add
114 	 */
115 	public void append(ContextMenuItem item)
116 	{
117 		webkit_context_menu_append(webKitContextMenu, (item is null) ? null : item.getContextMenuItemStruct());
118 	}
119 
120 	/**
121 	 * Gets the first item in the @menu.
122 	 *
123 	 * Returns: the first #WebKitContextMenuItem of @menu,
124 	 *     or %NULL if the #WebKitContextMenu is empty.
125 	 */
126 	public ContextMenuItem first()
127 	{
128 		auto __p = webkit_context_menu_first(webKitContextMenu);
129 
130 		if(__p is null)
131 		{
132 			return null;
133 		}
134 
135 		return ObjectG.getDObject!(ContextMenuItem)(cast(WebKitContextMenuItem*) __p);
136 	}
137 
138 	/**
139 	 * Gets the item at the given position in the @menu.
140 	 *
141 	 * Params:
142 	 *     position = the position of the item, counting from 0
143 	 *
144 	 * Returns: the #WebKitContextMenuItem at position @position in @menu,
145 	 *     or %NULL if the position is off the end of the @menu.
146 	 */
147 	public ContextMenuItem getItemAtPosition(uint position)
148 	{
149 		auto __p = webkit_context_menu_get_item_at_position(webKitContextMenu, position);
150 
151 		if(__p is null)
152 		{
153 			return null;
154 		}
155 
156 		return ObjectG.getDObject!(ContextMenuItem)(cast(WebKitContextMenuItem*) __p);
157 	}
158 
159 	/**
160 	 * Returns the item list of @menu.
161 	 *
162 	 * Returns: a #GList of
163 	 *     #WebKitContextMenuItem<!-- -->s
164 	 */
165 	public ListG getItems()
166 	{
167 		auto __p = webkit_context_menu_get_items(webKitContextMenu);
168 
169 		if(__p is null)
170 		{
171 			return null;
172 		}
173 
174 		return new ListG(cast(GList*) __p);
175 	}
176 
177 	/**
178 	 * Gets the length of the @menu.
179 	 *
180 	 * Returns: the number of #WebKitContextMenuItem<!-- -->s in @menu
181 	 */
182 	public uint getNItems()
183 	{
184 		return webkit_context_menu_get_n_items(webKitContextMenu);
185 	}
186 
187 	/**
188 	 * Gets the user data of @menu.
189 	 * This function can be used from the UI Process to get user data previously set
190 	 * from the Web Process with webkit_context_menu_set_user_data().
191 	 *
192 	 * Returns: the user data of @menu, or %NULL if @menu doesn't have user data
193 	 *
194 	 * Since: 2.8
195 	 */
196 	public Variant getUserData()
197 	{
198 		auto __p = webkit_context_menu_get_user_data(webKitContextMenu);
199 
200 		if(__p is null)
201 		{
202 			return null;
203 		}
204 
205 		return new Variant(cast(GVariant*) __p);
206 	}
207 
208 	/**
209 	 * Inserts @item into the @menu at the given position.
210 	 * If @position is negative, or is larger than the number of items
211 	 * in the #WebKitContextMenu, the item is added on to the end of
212 	 * the @menu. The first position is 0.
213 	 *
214 	 * Params:
215 	 *     item = the #WebKitContextMenuItem to add
216 	 *     position = the position to insert the item
217 	 */
218 	public void insert(ContextMenuItem item, int position)
219 	{
220 		webkit_context_menu_insert(webKitContextMenu, (item is null) ? null : item.getContextMenuItemStruct(), position);
221 	}
222 
223 	/**
224 	 * Gets the last item in the @menu.
225 	 *
226 	 * Returns: the last #WebKitContextMenuItem of @menu,
227 	 *     or %NULL if the #WebKitContextMenu is empty.
228 	 */
229 	public ContextMenuItem last()
230 	{
231 		auto __p = webkit_context_menu_last(webKitContextMenu);
232 
233 		if(__p is null)
234 		{
235 			return null;
236 		}
237 
238 		return ObjectG.getDObject!(ContextMenuItem)(cast(WebKitContextMenuItem*) __p);
239 	}
240 
241 	/**
242 	 * Moves @item to the given position in the @menu.
243 	 * If @position is negative, or is larger than the number of items
244 	 * in the #WebKitContextMenu, the item is added on to the end of
245 	 * the @menu.
246 	 * The first position is 0.
247 	 *
248 	 * Params:
249 	 *     item = the #WebKitContextMenuItem to add
250 	 *     position = the new position to move the item
251 	 */
252 	public void moveItem(ContextMenuItem item, int position)
253 	{
254 		webkit_context_menu_move_item(webKitContextMenu, (item is null) ? null : item.getContextMenuItemStruct(), position);
255 	}
256 
257 	/**
258 	 * Adds @item at the beginning of the @menu.
259 	 *
260 	 * Params:
261 	 *     item = the #WebKitContextMenuItem to add
262 	 */
263 	public void prepend(ContextMenuItem item)
264 	{
265 		webkit_context_menu_prepend(webKitContextMenu, (item is null) ? null : item.getContextMenuItemStruct());
266 	}
267 
268 	/**
269 	 * Removes @item from the @menu.
270 	 * See also webkit_context_menu_remove_all() to remove all items.
271 	 *
272 	 * Params:
273 	 *     item = the #WebKitContextMenuItem to remove
274 	 */
275 	public void remove(ContextMenuItem item)
276 	{
277 		webkit_context_menu_remove(webKitContextMenu, (item is null) ? null : item.getContextMenuItemStruct());
278 	}
279 
280 	/**
281 	 * Removes all items of the @menu.
282 	 */
283 	public void removeAll()
284 	{
285 		webkit_context_menu_remove_all(webKitContextMenu);
286 	}
287 
288 	/**
289 	 * Sets user data to @menu.
290 	 * This function can be used from a Web Process extension to set user data
291 	 * that can be retrieved from the UI Process using webkit_context_menu_get_user_data().
292 	 * If the @user_data #GVariant is floating, it is consumed.
293 	 *
294 	 * Params:
295 	 *     userData = a #GVariant
296 	 *
297 	 * Since: 2.8
298 	 */
299 	public void setUserData(Variant userData)
300 	{
301 		webkit_context_menu_set_user_data(webKitContextMenu, (userData is null) ? null : userData.getVariantStruct());
302 	}
303 }