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 gio.FilenameCompleter;
26 
27 private import gio.c.functions;
28 public  import gio.c.types;
29 private import glib.ConstructionException;
30 private import glib.Str;
31 private import gobject.ObjectG;
32 private import gobject.Signals;
33 public  import gtkc.giotypes;
34 private import std.algorithm;
35 
36 
37 /**
38  * Completes partial file and directory names given a partial string by
39  * looking in the file system for clues. Can return a list of possible
40  * completion strings for widget implementations.
41  */
42 public class FilenameCompleter : ObjectG
43 {
44 	/** the main Gtk struct */
45 	protected GFilenameCompleter* gFilenameCompleter;
46 
47 	/** Get the main Gtk struct */
48 	public GFilenameCompleter* getFilenameCompleterStruct(bool transferOwnership = false)
49 	{
50 		if (transferOwnership)
51 			ownedRef = false;
52 		return gFilenameCompleter;
53 	}
54 
55 	/** the main Gtk struct as a void* */
56 	protected override void* getStruct()
57 	{
58 		return cast(void*)gFilenameCompleter;
59 	}
60 
61 	protected override void setStruct(GObject* obj)
62 	{
63 		gFilenameCompleter = cast(GFilenameCompleter*)obj;
64 		super.setStruct(obj);
65 	}
66 
67 	/**
68 	 * Sets our main struct and passes it to the parent class.
69 	 */
70 	public this (GFilenameCompleter* gFilenameCompleter, bool ownedRef = false)
71 	{
72 		this.gFilenameCompleter = gFilenameCompleter;
73 		super(cast(GObject*)gFilenameCompleter, ownedRef);
74 	}
75 
76 
77 	/** */
78 	public static GType getType()
79 	{
80 		return g_filename_completer_get_type();
81 	}
82 
83 	/**
84 	 * Creates a new filename completer.
85 	 *
86 	 * Returns: a #GFilenameCompleter.
87 	 *
88 	 * Throws: ConstructionException GTK+ fails to create the object.
89 	 */
90 	public this()
91 	{
92 		auto p = g_filename_completer_new();
93 
94 		if(p is null)
95 		{
96 			throw new ConstructionException("null returned by new");
97 		}
98 
99 		this(cast(GFilenameCompleter*) p, true);
100 	}
101 
102 	/**
103 	 * Obtains a completion for @initial_text from @completer.
104 	 *
105 	 * Params:
106 	 *     initialText = text to be completed.
107 	 *
108 	 * Returns: a completed string, or %NULL if no completion exists.
109 	 *     This string is not owned by GIO, so remember to g_free() it
110 	 *     when finished.
111 	 */
112 	public string getCompletionSuffix(string initialText)
113 	{
114 		auto retStr = g_filename_completer_get_completion_suffix(gFilenameCompleter, Str.toStringz(initialText));
115 
116 		scope(exit) Str.freeString(retStr);
117 		return Str.toString(retStr);
118 	}
119 
120 	/**
121 	 * Gets an array of completion strings for a given initial text.
122 	 *
123 	 * Params:
124 	 *     initialText = text to be completed.
125 	 *
126 	 * Returns: array of strings with possible completions for @initial_text.
127 	 *     This array must be freed by g_strfreev() when finished.
128 	 */
129 	public string[] getCompletions(string initialText)
130 	{
131 		auto retStr = g_filename_completer_get_completions(gFilenameCompleter, Str.toStringz(initialText));
132 
133 		scope(exit) Str.freeStringArray(retStr);
134 		return Str.toStringArray(retStr);
135 	}
136 
137 	/**
138 	 * If @dirs_only is %TRUE, @completer will only
139 	 * complete directory names, and not file names.
140 	 *
141 	 * Params:
142 	 *     dirsOnly = a #gboolean.
143 	 */
144 	public void setDirsOnly(bool dirsOnly)
145 	{
146 		g_filename_completer_set_dirs_only(gFilenameCompleter, dirsOnly);
147 	}
148 
149 	protected class OnGotCompletionDataDelegateWrapper
150 	{
151 		void delegate(FilenameCompleter) dlg;
152 		gulong handlerId;
153 
154 		this(void delegate(FilenameCompleter) dlg)
155 		{
156 			this.dlg = dlg;
157 			onGotCompletionDataListeners ~= this;
158 		}
159 
160 		void remove(OnGotCompletionDataDelegateWrapper source)
161 		{
162 			foreach(index, wrapper; onGotCompletionDataListeners)
163 			{
164 				if (wrapper.handlerId == source.handlerId)
165 				{
166 					onGotCompletionDataListeners[index] = null;
167 					onGotCompletionDataListeners = std.algorithm.remove(onGotCompletionDataListeners, index);
168 					break;
169 				}
170 			}
171 		}
172 	}
173 	OnGotCompletionDataDelegateWrapper[] onGotCompletionDataListeners;
174 
175 	/**
176 	 * Emitted when the file name completion information comes available.
177 	 */
178 	gulong addOnGotCompletionData(void delegate(FilenameCompleter) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
179 	{
180 		auto wrapper = new OnGotCompletionDataDelegateWrapper(dlg);
181 		wrapper.handlerId = Signals.connectData(
182 			this,
183 			"got-completion-data",
184 			cast(GCallback)&callBackGotCompletionData,
185 			cast(void*)wrapper,
186 			cast(GClosureNotify)&callBackGotCompletionDataDestroy,
187 			connectFlags);
188 		return wrapper.handlerId;
189 	}
190 
191 	extern(C) static void callBackGotCompletionData(GFilenameCompleter* filenamecompleterStruct, OnGotCompletionDataDelegateWrapper wrapper)
192 	{
193 		wrapper.dlg(wrapper.outer);
194 	}
195 
196 	extern(C) static void callBackGotCompletionDataDestroy(OnGotCompletionDataDelegateWrapper wrapper, GClosure* closure)
197 	{
198 		wrapper.remove(wrapper);
199 	}
200 }