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