1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle 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  * d-dazzle 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 d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.SuggestionEntry;
20 
21 private import dazzle.Suggestion;
22 private import dazzle.c.functions;
23 public  import dazzle.c.types;
24 private import gio.ListModelIF;
25 private import glib.ConstructionException;
26 private import glib.Str;
27 private import gobject.ObjectG;
28 private import gobject.Signals;
29 private import gtk.BuildableIF;
30 private import gtk.BuildableT;
31 private import gtk.CellEditableIF;
32 private import gtk.CellEditableT;
33 private import gtk.EditableIF;
34 private import gtk.EditableT;
35 private import gtk.Entry;
36 private import gtk.Widget;
37 private import std.algorithm;
38 
39 
40 /** */
41 public class SuggestionEntry : Entry
42 {
43 	/** the main Gtk struct */
44 	protected DzlSuggestionEntry* dzlSuggestionEntry;
45 
46 	/** Get the main Gtk struct */
47 	public DzlSuggestionEntry* getSuggestionEntryStruct(bool transferOwnership = false)
48 	{
49 		if (transferOwnership)
50 			ownedRef = false;
51 		return dzlSuggestionEntry;
52 	}
53 
54 	/** the main Gtk struct as a void* */
55 	protected override void* getStruct()
56 	{
57 		return cast(void*)dzlSuggestionEntry;
58 	}
59 
60 	/**
61 	 * Sets our main struct and passes it to the parent class.
62 	 */
63 	public this (DzlSuggestionEntry* dzlSuggestionEntry, bool ownedRef = false)
64 	{
65 		this.dzlSuggestionEntry = dzlSuggestionEntry;
66 		super(cast(GtkEntry*)dzlSuggestionEntry, ownedRef);
67 	}
68 
69 	/** */
70 	public void defaultPositionFunc(GdkRectangle* area, bool* isAbsolute, void* userData)
71 	{
72 		dzl_suggestion_entry_default_position_func(dzlSuggestionEntry, area, cast(int*)&isAbsolute, userData);
73 	}
74 
75 	/**
76 	 * This is a #DzlSuggestionPositionFunc that can be used to make the suggestion
77 	 * popover the full width of the window. It is similar to what you might find
78 	 * in a web browser.
79 	 */
80 	public void windowPositionFunc(GdkRectangle* area, bool* isAbsolute, void* userData)
81 	{
82 		dzl_suggestion_entry_window_position_func(dzlSuggestionEntry, area, cast(int*)&isAbsolute, userData);
83 	}
84 
85 
86 	/**
87 	 */
88 
89 	/** */
90 	public static GType getType()
91 	{
92 		return dzl_suggestion_entry_get_type();
93 	}
94 
95 	/** */
96 	public this()
97 	{
98 		auto p = dzl_suggestion_entry_new();
99 
100 		if(p is null)
101 		{
102 			throw new ConstructionException("null returned by new");
103 		}
104 
105 		this(cast(DzlSuggestionEntry*) p);
106 	}
107 
108 	/** */
109 	public bool getActivateOnSingleClick()
110 	{
111 		return dzl_suggestion_entry_get_activate_on_single_click(dzlSuggestionEntry) != 0;
112 	}
113 
114 	/**
115 	 * Gets the #DzlSuggestionEntry:compact property.
116 	 *
117 	 * Returns: %TRUE if compact-mode is in use.
118 	 *
119 	 * Since: 3.34
120 	 */
121 	public bool getCompact()
122 	{
123 		return dzl_suggestion_entry_get_compact(dzlSuggestionEntry) != 0;
124 	}
125 
126 	/**
127 	 * Gets the model being visualized.
128 	 *
129 	 * Returns: A #GListModel or %NULL.
130 	 */
131 	public ListModelIF getModel()
132 	{
133 		auto p = dzl_suggestion_entry_get_model(dzlSuggestionEntry);
134 
135 		if(p is null)
136 		{
137 			return null;
138 		}
139 
140 		return ObjectG.getDObject!(ListModelIF)(cast(GListModel*) p);
141 	}
142 
143 	/**
144 	 * Gets the result display widget for the entry. This is currently
145 	 * always a #DzlSuggestionPopover.
146 	 *
147 	 * Returns: a #GtkWidget
148 	 *
149 	 * Since: 3.32
150 	 */
151 	public Widget getPopover()
152 	{
153 		auto p = dzl_suggestion_entry_get_popover(dzlSuggestionEntry);
154 
155 		if(p is null)
156 		{
157 			return null;
158 		}
159 
160 		return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p);
161 	}
162 
163 	/**
164 	 * Gets the currently selected suggestion.
165 	 *
166 	 * Returns: An #DzlSuggestion or %NULL.
167 	 */
168 	public Suggestion getSuggestion()
169 	{
170 		auto p = dzl_suggestion_entry_get_suggestion(dzlSuggestionEntry);
171 
172 		if(p is null)
173 		{
174 			return null;
175 		}
176 
177 		return ObjectG.getDObject!(Suggestion)(cast(DzlSuggestion*) p);
178 	}
179 
180 	/** */
181 	public string getTypedText()
182 	{
183 		return Str.toString(dzl_suggestion_entry_get_typed_text(dzlSuggestionEntry));
184 	}
185 
186 	/** */
187 	public void hideSuggestions()
188 	{
189 		dzl_suggestion_entry_hide_suggestions(dzlSuggestionEntry);
190 	}
191 
192 	/** */
193 	public void setActivateOnSingleClick(bool activateOnSingleClick)
194 	{
195 		dzl_suggestion_entry_set_activate_on_single_click(dzlSuggestionEntry, activateOnSingleClick);
196 	}
197 
198 	/**
199 	 * Sets the #DzlSuggestionEntry:compact property.
200 	 *
201 	 * Params:
202 	 *     compact = if compact mode should be used
203 	 *
204 	 * Since: 3.34
205 	 */
206 	public void setCompact(bool compact)
207 	{
208 		dzl_suggestion_entry_set_compact(dzlSuggestionEntry, compact);
209 	}
210 
211 	/** */
212 	public void setModel(ListModelIF model)
213 	{
214 		dzl_suggestion_entry_set_model(dzlSuggestionEntry, (model is null) ? null : model.getListModelStruct());
215 	}
216 
217 	/**
218 	 * Sets a position func to position the popover.
219 	 *
220 	 * In @func, you should set the height of the rectangle to the maximum height
221 	 * that the popover should be allowed to grow.
222 	 *
223 	 * Params:
224 	 *     func = A function to call to position the popover, or %NULL to set the default.
225 	 *     funcData = closure data for @func
226 	 *     funcDataDestroy = a destroy notify for @func_data
227 	 *
228 	 * Since: 3.26
229 	 */
230 	public void setPositionFunc(DzlSuggestionPositionFunc func, void* funcData, GDestroyNotify funcDataDestroy)
231 	{
232 		dzl_suggestion_entry_set_position_func(dzlSuggestionEntry, func, funcData, funcDataDestroy);
233 	}
234 
235 	/** */
236 	public void setSuggestion(Suggestion suggestion)
237 	{
238 		dzl_suggestion_entry_set_suggestion(dzlSuggestionEntry, (suggestion is null) ? null : suggestion.getSuggestionStruct());
239 	}
240 
241 	/** */
242 	gulong addOnAction(void delegate(string, string, string, SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
243 	{
244 		return Signals.connect(this, "action", dlg, connectFlags ^ ConnectFlags.SWAPPED);
245 	}
246 
247 	/** */
248 	gulong addOnActivateSuggestion(void delegate(SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
249 	{
250 		return Signals.connect(this, "activate-suggestion", dlg, connectFlags ^ ConnectFlags.SWAPPED);
251 	}
252 
253 	/** */
254 	gulong addOnHideSuggestions(void delegate(SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
255 	{
256 		return Signals.connect(this, "hide-suggestions", dlg, connectFlags ^ ConnectFlags.SWAPPED);
257 	}
258 
259 	/**
260 	 * This moves the selected suggestion in the popover by the value
261 	 * provided. -1 moves up one row, 1, moves down a row.
262 	 *
263 	 * Params:
264 	 *     amount = The number of items to move
265 	 */
266 	gulong addOnMoveSuggestion(void delegate(int, SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
267 	{
268 		return Signals.connect(this, "move-suggestion", dlg, connectFlags ^ ConnectFlags.SWAPPED);
269 	}
270 
271 	/** */
272 	gulong addOnShowSuggestions(void delegate(SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
273 	{
274 		return Signals.connect(this, "show-suggestions", dlg, connectFlags ^ ConnectFlags.SWAPPED);
275 	}
276 
277 	/** */
278 	gulong addOnSuggestionActivated(void delegate(Suggestion, SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
279 	{
280 		return Signals.connect(this, "suggestion-activated", dlg, connectFlags ^ ConnectFlags.SWAPPED);
281 	}
282 
283 	/**
284 	 * This signal is emitted when a selection has been specifically selected
285 	 * by the user, such as by clicking on the row or moving to the row with
286 	 * keyboard, such as with #DzlSuggestionEntry::move-suggestion
287 	 *
288 	 * Params:
289 	 *     suggestion = a #DzlSuggestion
290 	 *
291 	 * Since: 3.30
292 	 */
293 	gulong addOnSuggestionSelected(void delegate(Suggestion, SuggestionEntry) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
294 	{
295 		return Signals.connect(this, "suggestion-selected", dlg, connectFlags ^ ConnectFlags.SWAPPED);
296 	}
297 }