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