1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD 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  * gtkD 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 gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.RecentChooserT;
26 
27 public  import glib.ErrorG;
28 public  import glib.GException;
29 public  import glib.ListG;
30 public  import glib.ListSG;
31 public  import glib.Str;
32 public  import gobject.ObjectG;
33 public  import gobject.Signals;
34 public  import gtk.RecentFilter;
35 public  import gtk.RecentInfo;
36 public  import gtkc.gdktypes;
37 public  import gtkc.gtk;
38 public  import gtkc.gtktypes;
39 public  import std.algorithm;
40 
41 
42 /**
43  * #GtkRecentChooser is an interface that can be implemented by widgets
44  * displaying the list of recently used files.  In GTK+, the main objects
45  * that implement this interface are #GtkRecentChooserWidget,
46  * #GtkRecentChooserDialog and #GtkRecentChooserMenu.
47  * 
48  * Recently used files are supported since GTK+ 2.10.
49  */
50 public template RecentChooserT(TStruct)
51 {
52 	/** Get the main Gtk struct */
53 	public GtkRecentChooser* getRecentChooserStruct()
54 	{
55 		return cast(GtkRecentChooser*)getStruct();
56 	}
57 
58 
59 	/**
60 	 * Adds @filter to the list of #GtkRecentFilter objects held by @chooser.
61 	 *
62 	 * If no previous filter objects were defined, this function will call
63 	 * gtk_recent_chooser_set_filter().
64 	 *
65 	 * Params:
66 	 *     filter = a #GtkRecentFilter
67 	 *
68 	 * Since: 2.10
69 	 */
70 	public void addFilter(RecentFilter filter)
71 	{
72 		gtk_recent_chooser_add_filter(getRecentChooserStruct(), (filter is null) ? null : filter.getRecentFilterStruct());
73 	}
74 
75 	/**
76 	 * Gets the #GtkRecentInfo currently selected by @chooser.
77 	 *
78 	 * Return: a #GtkRecentInfo.  Use gtk_recent_info_unref() when
79 	 *     when you have finished using it.
80 	 *
81 	 * Since: 2.10
82 	 */
83 	public RecentInfo getCurrentItem()
84 	{
85 		auto p = gtk_recent_chooser_get_current_item(getRecentChooserStruct());
86 		
87 		if(p is null)
88 		{
89 			return null;
90 		}
91 		
92 		return ObjectG.getDObject!(RecentInfo)(cast(GtkRecentInfo*) p, true);
93 	}
94 
95 	/**
96 	 * Gets the URI currently selected by @chooser.
97 	 *
98 	 * Return: a newly allocated string holding a URI.
99 	 *
100 	 * Since: 2.10
101 	 */
102 	public string getCurrentUri()
103 	{
104 		auto retStr = gtk_recent_chooser_get_current_uri(getRecentChooserStruct());
105 		
106 		scope(exit) Str.freeString(retStr);
107 		return Str.toString(retStr);
108 	}
109 
110 	/**
111 	 * Gets the #GtkRecentFilter object currently used by @chooser to affect
112 	 * the display of the recently used resources.
113 	 *
114 	 * Return: a #GtkRecentFilter object.
115 	 *
116 	 * Since: 2.10
117 	 */
118 	public RecentFilter getFilter()
119 	{
120 		auto p = gtk_recent_chooser_get_filter(getRecentChooserStruct());
121 		
122 		if(p is null)
123 		{
124 			return null;
125 		}
126 		
127 		return ObjectG.getDObject!(RecentFilter)(cast(GtkRecentFilter*) p);
128 	}
129 
130 	/**
131 	 * Gets the list of recently used resources in form of #GtkRecentInfo objects.
132 	 *
133 	 * The return value of this function is affected by the “sort-type” and
134 	 * “limit” properties of @chooser.
135 	 *
136 	 * Return: A newly allocated
137 	 *     list of #GtkRecentInfo objects.  You should
138 	 *     use gtk_recent_info_unref() on every item of the list, and then free
139 	 *     the list itself using g_list_free().
140 	 *
141 	 * Since: 2.10
142 	 */
143 	public ListG getItems()
144 	{
145 		auto p = gtk_recent_chooser_get_items(getRecentChooserStruct());
146 		
147 		if(p is null)
148 		{
149 			return null;
150 		}
151 		
152 		return new ListG(cast(GList*) p, true);
153 	}
154 
155 	/**
156 	 * Gets the number of items returned by gtk_recent_chooser_get_items()
157 	 * and gtk_recent_chooser_get_uris().
158 	 *
159 	 * Return: A positive integer, or -1 meaning that all items are
160 	 *     returned.
161 	 *
162 	 * Since: 2.10
163 	 */
164 	public int getLimit()
165 	{
166 		return gtk_recent_chooser_get_limit(getRecentChooserStruct());
167 	}
168 
169 	/**
170 	 * Gets whether only local resources should be shown in the recently used
171 	 * resources selector.  See gtk_recent_chooser_set_local_only()
172 	 *
173 	 * Return: %TRUE if only local resources should be shown.
174 	 *
175 	 * Since: 2.10
176 	 */
177 	public bool getLocalOnly()
178 	{
179 		return gtk_recent_chooser_get_local_only(getRecentChooserStruct()) != 0;
180 	}
181 
182 	/**
183 	 * Gets whether @chooser can select multiple items.
184 	 *
185 	 * Return: %TRUE if @chooser can select more than one item.
186 	 *
187 	 * Since: 2.10
188 	 */
189 	public bool getSelectMultiple()
190 	{
191 		return gtk_recent_chooser_get_select_multiple(getRecentChooserStruct()) != 0;
192 	}
193 
194 	/**
195 	 * Retrieves whether @chooser should show an icon near the resource.
196 	 *
197 	 * Return: %TRUE if the icons should be displayed, %FALSE otherwise.
198 	 *
199 	 * Since: 2.10
200 	 */
201 	public bool getShowIcons()
202 	{
203 		return gtk_recent_chooser_get_show_icons(getRecentChooserStruct()) != 0;
204 	}
205 
206 	/**
207 	 * Retrieves whether @chooser should show the recently used resources that
208 	 * were not found.
209 	 *
210 	 * Return: %TRUE if the resources not found should be displayed, and
211 	 *     %FALSE otheriwse.
212 	 *
213 	 * Since: 2.10
214 	 */
215 	public bool getShowNotFound()
216 	{
217 		return gtk_recent_chooser_get_show_not_found(getRecentChooserStruct()) != 0;
218 	}
219 
220 	/**
221 	 * Returns whether @chooser should display recently used resources
222 	 * registered as private.
223 	 *
224 	 * Return: %TRUE if the recent chooser should show private items,
225 	 *     %FALSE otherwise.
226 	 *
227 	 * Since: 2.10
228 	 */
229 	public bool getShowPrivate()
230 	{
231 		return gtk_recent_chooser_get_show_private(getRecentChooserStruct()) != 0;
232 	}
233 
234 	/**
235 	 * Gets whether @chooser should display tooltips containing the full path
236 	 * of a recently user resource.
237 	 *
238 	 * Return: %TRUE if the recent chooser should show tooltips,
239 	 *     %FALSE otherwise.
240 	 *
241 	 * Since: 2.10
242 	 */
243 	public bool getShowTips()
244 	{
245 		return gtk_recent_chooser_get_show_tips(getRecentChooserStruct()) != 0;
246 	}
247 
248 	/**
249 	 * Gets the value set by gtk_recent_chooser_set_sort_type().
250 	 *
251 	 * Return: the sorting order of the @chooser.
252 	 *
253 	 * Since: 2.10
254 	 */
255 	public GtkRecentSortType getSortType()
256 	{
257 		return gtk_recent_chooser_get_sort_type(getRecentChooserStruct());
258 	}
259 
260 	/**
261 	 * Gets the URI of the recently used resources.
262 	 *
263 	 * The return value of this function is affected by the “sort-type” and “limit”
264 	 * properties of @chooser.
265 	 *
266 	 * Since the returned array is %NULL terminated, @length may be %NULL.
267 	 *
268 	 * Return: A newly allocated, %NULL-terminated array of strings. Use
269 	 *     g_strfreev() to free it.
270 	 *
271 	 * Since: 2.10
272 	 */
273 	public string[] getUris()
274 	{
275 		size_t length;
276 		
277 		auto retStr = gtk_recent_chooser_get_uris(getRecentChooserStruct(), &length);
278 		
279 		scope(exit) Str.freeStringArray(retStr);
280 		return Str.toStringArray(retStr, length);
281 	}
282 
283 	/**
284 	 * Gets the #GtkRecentFilter objects held by @chooser.
285 	 *
286 	 * Return: A singly linked list
287 	 *     of #GtkRecentFilter objects.  You
288 	 *     should just free the returned list using g_slist_free().
289 	 *
290 	 * Since: 2.10
291 	 */
292 	public ListSG listFilters()
293 	{
294 		auto p = gtk_recent_chooser_list_filters(getRecentChooserStruct());
295 		
296 		if(p is null)
297 		{
298 			return null;
299 		}
300 		
301 		return new ListSG(cast(GSList*) p);
302 	}
303 
304 	/**
305 	 * Removes @filter from the list of #GtkRecentFilter objects held by @chooser.
306 	 *
307 	 * Params:
308 	 *     filter = a #GtkRecentFilter
309 	 *
310 	 * Since: 2.10
311 	 */
312 	public void removeFilter(RecentFilter filter)
313 	{
314 		gtk_recent_chooser_remove_filter(getRecentChooserStruct(), (filter is null) ? null : filter.getRecentFilterStruct());
315 	}
316 
317 	/**
318 	 * Selects all the items inside @chooser, if the @chooser supports
319 	 * multiple selection.
320 	 *
321 	 * Since: 2.10
322 	 */
323 	public void selectAll()
324 	{
325 		gtk_recent_chooser_select_all(getRecentChooserStruct());
326 	}
327 
328 	/**
329 	 * Selects @uri inside @chooser.
330 	 *
331 	 * Params:
332 	 *     uri = a URI
333 	 *
334 	 * Return: %TRUE if @uri was found.
335 	 *
336 	 * Since: 2.10
337 	 *
338 	 * Throws: GException on failure.
339 	 */
340 	public bool selectUri(string uri)
341 	{
342 		GError* err = null;
343 		
344 		auto p = gtk_recent_chooser_select_uri(getRecentChooserStruct(), Str.toStringz(uri), &err) != 0;
345 		
346 		if (err !is null)
347 		{
348 			throw new GException( new ErrorG(err) );
349 		}
350 		
351 		return p;
352 	}
353 
354 	/**
355 	 * Sets @uri as the current URI for @chooser.
356 	 *
357 	 * Params:
358 	 *     uri = a URI
359 	 *
360 	 * Return: %TRUE if the URI was found.
361 	 *
362 	 * Since: 2.10
363 	 *
364 	 * Throws: GException on failure.
365 	 */
366 	public bool setCurrentUri(string uri)
367 	{
368 		GError* err = null;
369 		
370 		auto p = gtk_recent_chooser_set_current_uri(getRecentChooserStruct(), Str.toStringz(uri), &err) != 0;
371 		
372 		if (err !is null)
373 		{
374 			throw new GException( new ErrorG(err) );
375 		}
376 		
377 		return p;
378 	}
379 
380 	/**
381 	 * Sets @filter as the current #GtkRecentFilter object used by @chooser
382 	 * to affect the displayed recently used resources.
383 	 *
384 	 * Params:
385 	 *     filter = a #GtkRecentFilter
386 	 *
387 	 * Since: 2.10
388 	 */
389 	public void setFilter(RecentFilter filter)
390 	{
391 		gtk_recent_chooser_set_filter(getRecentChooserStruct(), (filter is null) ? null : filter.getRecentFilterStruct());
392 	}
393 
394 	/**
395 	 * Sets the number of items that should be returned by
396 	 * gtk_recent_chooser_get_items() and gtk_recent_chooser_get_uris().
397 	 *
398 	 * Params:
399 	 *     limit = a positive integer, or -1 for all items
400 	 *
401 	 * Since: 2.10
402 	 */
403 	public void setLimit(int limit)
404 	{
405 		gtk_recent_chooser_set_limit(getRecentChooserStruct(), limit);
406 	}
407 
408 	/**
409 	 * Sets whether only local resources, that is resources using the file:// URI
410 	 * scheme, should be shown in the recently used resources selector.  If
411 	 * @local_only is %TRUE (the default) then the shown resources are guaranteed
412 	 * to be accessible through the operating system native file system.
413 	 *
414 	 * Params:
415 	 *     localOnly = %TRUE if only local files can be shown
416 	 *
417 	 * Since: 2.10
418 	 */
419 	public void setLocalOnly(bool localOnly)
420 	{
421 		gtk_recent_chooser_set_local_only(getRecentChooserStruct(), localOnly);
422 	}
423 
424 	/**
425 	 * Sets whether @chooser can select multiple items.
426 	 *
427 	 * Params:
428 	 *     selectMultiple = %TRUE if @chooser can select more than one item
429 	 *
430 	 * Since: 2.10
431 	 */
432 	public void setSelectMultiple(bool selectMultiple)
433 	{
434 		gtk_recent_chooser_set_select_multiple(getRecentChooserStruct(), selectMultiple);
435 	}
436 
437 	/**
438 	 * Sets whether @chooser should show an icon near the resource when
439 	 * displaying it.
440 	 *
441 	 * Params:
442 	 *     showIcons = whether to show an icon near the resource
443 	 *
444 	 * Since: 2.10
445 	 */
446 	public void setShowIcons(bool showIcons)
447 	{
448 		gtk_recent_chooser_set_show_icons(getRecentChooserStruct(), showIcons);
449 	}
450 
451 	/**
452 	 * Sets whether @chooser should display the recently used resources that
453 	 * it didn’t find.  This only applies to local resources.
454 	 *
455 	 * Params:
456 	 *     showNotFound = whether to show the local items we didn’t find
457 	 *
458 	 * Since: 2.10
459 	 */
460 	public void setShowNotFound(bool showNotFound)
461 	{
462 		gtk_recent_chooser_set_show_not_found(getRecentChooserStruct(), showNotFound);
463 	}
464 
465 	/**
466 	 * Whether to show recently used resources marked registered as private.
467 	 *
468 	 * Params:
469 	 *     showPrivate = %TRUE to show private items, %FALSE otherwise
470 	 *
471 	 * Since: 2.10
472 	 */
473 	public void setShowPrivate(bool showPrivate)
474 	{
475 		gtk_recent_chooser_set_show_private(getRecentChooserStruct(), showPrivate);
476 	}
477 
478 	/**
479 	 * Sets whether to show a tooltips containing the full path of each
480 	 * recently used resource in a #GtkRecentChooser widget.
481 	 *
482 	 * Params:
483 	 *     showTips = %TRUE if tooltips should be shown
484 	 *
485 	 * Since: 2.10
486 	 */
487 	public void setShowTips(bool showTips)
488 	{
489 		gtk_recent_chooser_set_show_tips(getRecentChooserStruct(), showTips);
490 	}
491 
492 	/**
493 	 * Sets the comparison function used when sorting to be @sort_func.  If
494 	 * the @chooser has the sort type set to #GTK_RECENT_SORT_CUSTOM then
495 	 * the chooser will sort using this function.
496 	 *
497 	 * To the comparison function will be passed two #GtkRecentInfo structs and
498 	 * @sort_data;  @sort_func should return a positive integer if the first
499 	 * item comes before the second, zero if the two items are equal and
500 	 * a negative integer if the first item comes after the second.
501 	 *
502 	 * Params:
503 	 *     sortFunc = the comparison function
504 	 *     sortData = user data to pass to @sort_func, or %NULL
505 	 *     dataDestroy = destroy notifier for @sort_data, or %NULL
506 	 *
507 	 * Since: 2.10
508 	 */
509 	public void setSortFunc(GtkRecentSortFunc sortFunc, void* sortData, GDestroyNotify dataDestroy)
510 	{
511 		gtk_recent_chooser_set_sort_func(getRecentChooserStruct(), sortFunc, sortData, dataDestroy);
512 	}
513 
514 	/**
515 	 * Changes the sorting order of the recently used resources list displayed by
516 	 * @chooser.
517 	 *
518 	 * Params:
519 	 *     sortType = sort order that the chooser should use
520 	 *
521 	 * Since: 2.10
522 	 */
523 	public void setSortType(GtkRecentSortType sortType)
524 	{
525 		gtk_recent_chooser_set_sort_type(getRecentChooserStruct(), sortType);
526 	}
527 
528 	/**
529 	 * Unselects all the items inside @chooser.
530 	 *
531 	 * Since: 2.10
532 	 */
533 	public void unselectAll()
534 	{
535 		gtk_recent_chooser_unselect_all(getRecentChooserStruct());
536 	}
537 
538 	/**
539 	 * Unselects @uri inside @chooser.
540 	 *
541 	 * Params:
542 	 *     uri = a URI
543 	 *
544 	 * Since: 2.10
545 	 */
546 	public void unselectUri(string uri)
547 	{
548 		gtk_recent_chooser_unselect_uri(getRecentChooserStruct(), Str.toStringz(uri));
549 	}
550 
551 	protected class OnItemActivatedDelegateWrapper
552 	{
553 		void delegate(RecentChooserIF) dlg;
554 		gulong handlerId;
555 		ConnectFlags flags;
556 		this(void delegate(RecentChooserIF) dlg, gulong handlerId, ConnectFlags flags)
557 		{
558 			this.dlg = dlg;
559 			this.handlerId = handlerId;
560 			this.flags = flags;
561 		}
562 	}
563 	protected OnItemActivatedDelegateWrapper[] onItemActivatedListeners;
564 
565 	/**
566 	 * This signal is emitted when the user "activates" a recent item
567 	 * in the recent chooser.  This can happen by double-clicking on an item
568 	 * in the recently used resources list, or by pressing
569 	 * `Enter`.
570 	 *
571 	 * Since: 2.10
572 	 */
573 	gulong addOnItemActivated(void delegate(RecentChooserIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
574 	{
575 		onItemActivatedListeners ~= new OnItemActivatedDelegateWrapper(dlg, 0, connectFlags);
576 		onItemActivatedListeners[onItemActivatedListeners.length - 1].handlerId = Signals.connectData(
577 			this,
578 			"item-activated",
579 			cast(GCallback)&callBackItemActivated,
580 			cast(void*)onItemActivatedListeners[onItemActivatedListeners.length - 1],
581 			cast(GClosureNotify)&callBackItemActivatedDestroy,
582 			connectFlags);
583 		return onItemActivatedListeners[onItemActivatedListeners.length - 1].handlerId;
584 	}
585 	
586 	extern(C) static void callBackItemActivated(GtkRecentChooser* recentchooserStruct,OnItemActivatedDelegateWrapper wrapper)
587 	{
588 		wrapper.dlg(wrapper.outer);
589 	}
590 	
591 	extern(C) static void callBackItemActivatedDestroy(OnItemActivatedDelegateWrapper wrapper, GClosure* closure)
592 	{
593 		wrapper.outer.internalRemoveOnItemActivated(wrapper);
594 	}
595 
596 	protected void internalRemoveOnItemActivated(OnItemActivatedDelegateWrapper source)
597 	{
598 		foreach(index, wrapper; onItemActivatedListeners)
599 		{
600 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
601 			{
602 				onItemActivatedListeners[index] = null;
603 				onItemActivatedListeners = std.algorithm.remove(onItemActivatedListeners, index);
604 				break;
605 			}
606 		}
607 	}
608 	
609 
610 	protected class OnSelectionChangedDelegateWrapper
611 	{
612 		void delegate(RecentChooserIF) dlg;
613 		gulong handlerId;
614 		ConnectFlags flags;
615 		this(void delegate(RecentChooserIF) dlg, gulong handlerId, ConnectFlags flags)
616 		{
617 			this.dlg = dlg;
618 			this.handlerId = handlerId;
619 			this.flags = flags;
620 		}
621 	}
622 	protected OnSelectionChangedDelegateWrapper[] onSelectionChangedListeners;
623 
624 	/**
625 	 * This signal is emitted when there is a change in the set of
626 	 * selected recently used resources.  This can happen when a user
627 	 * modifies the selection with the mouse or the keyboard, or when
628 	 * explicitly calling functions to change the selection.
629 	 *
630 	 * Since: 2.10
631 	 */
632 	gulong addOnSelectionChanged(void delegate(RecentChooserIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
633 	{
634 		onSelectionChangedListeners ~= new OnSelectionChangedDelegateWrapper(dlg, 0, connectFlags);
635 		onSelectionChangedListeners[onSelectionChangedListeners.length - 1].handlerId = Signals.connectData(
636 			this,
637 			"selection-changed",
638 			cast(GCallback)&callBackSelectionChanged,
639 			cast(void*)onSelectionChangedListeners[onSelectionChangedListeners.length - 1],
640 			cast(GClosureNotify)&callBackSelectionChangedDestroy,
641 			connectFlags);
642 		return onSelectionChangedListeners[onSelectionChangedListeners.length - 1].handlerId;
643 	}
644 	
645 	extern(C) static void callBackSelectionChanged(GtkRecentChooser* recentchooserStruct,OnSelectionChangedDelegateWrapper wrapper)
646 	{
647 		wrapper.dlg(wrapper.outer);
648 	}
649 	
650 	extern(C) static void callBackSelectionChangedDestroy(OnSelectionChangedDelegateWrapper wrapper, GClosure* closure)
651 	{
652 		wrapper.outer.internalRemoveOnSelectionChanged(wrapper);
653 	}
654 
655 	protected void internalRemoveOnSelectionChanged(OnSelectionChangedDelegateWrapper source)
656 	{
657 		foreach(index, wrapper; onSelectionChangedListeners)
658 		{
659 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
660 			{
661 				onSelectionChangedListeners[index] = null;
662 				onSelectionChangedListeners = std.algorithm.remove(onSelectionChangedListeners, index);
663 				break;
664 			}
665 		}
666 	}
667 	
668 }