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.Suggestion;
20 
21 private import cairo.Surface;
22 private import dazzle.c.functions;
23 public  import dazzle.c.types;
24 private import gio.IconIF;
25 private import glib.ConstructionException;
26 private import glib.Str;
27 private import gobject.ObjectG;
28 private import gobject.Signals;
29 private import gtk.Widget;
30 private import std.algorithm;
31 
32 
33 /** */
34 public class Suggestion : ObjectG
35 {
36 	/** the main Gtk struct */
37 	protected DzlSuggestion* dzlSuggestion;
38 
39 	/** Get the main Gtk struct */
40 	public DzlSuggestion* getSuggestionStruct(bool transferOwnership = false)
41 	{
42 		if (transferOwnership)
43 			ownedRef = false;
44 		return dzlSuggestion;
45 	}
46 
47 	/** the main Gtk struct as a void* */
48 	protected override void* getStruct()
49 	{
50 		return cast(void*)dzlSuggestion;
51 	}
52 
53 	/**
54 	 * Sets our main struct and passes it to the parent class.
55 	 */
56 	public this (DzlSuggestion* dzlSuggestion, bool ownedRef = false)
57 	{
58 		this.dzlSuggestion = dzlSuggestion;
59 		super(cast(GObject*)dzlSuggestion, ownedRef);
60 	}
61 
62 
63 	/** */
64 	public static GType getType()
65 	{
66 		return dzl_suggestion_get_type();
67 	}
68 
69 	/** */
70 	public this()
71 	{
72 		auto p = dzl_suggestion_new();
73 
74 		if(p is null)
75 		{
76 			throw new ConstructionException("null returned by new");
77 		}
78 
79 		this(cast(DzlSuggestion*) p, true);
80 	}
81 
82 	/**
83 	 * Gets the icon for the suggestion, if any.
84 	 *
85 	 * Returns: a #GIcon or %NULL
86 	 *
87 	 * Since: 3.30
88 	 */
89 	public IconIF getIcon()
90 	{
91 		auto p = dzl_suggestion_get_icon(dzlSuggestion);
92 
93 		if(p is null)
94 		{
95 			return null;
96 		}
97 
98 		return ObjectG.getDObject!(IconIF)(cast(GIcon*) p, true);
99 	}
100 
101 	/** */
102 	public string getIconName()
103 	{
104 		return Str.toString(dzl_suggestion_get_icon_name(dzlSuggestion));
105 	}
106 
107 	/**
108 	 * This function allows subclasses to dynamicly generate content for the
109 	 * suggestion such as may be required when integrating with favicons or
110 	 * similar.
111 	 *
112 	 * @widget is provided so that the implementation may determine scale or
113 	 * any other style-specific settings from the style context.
114 	 *
115 	 * Params:
116 	 *     widget = a widget that may contain the surface
117 	 *
118 	 * Returns: a #cairo_surface_t or %NULL
119 	 *
120 	 * Since: 3.30
121 	 */
122 	public Surface getIconSurface(Widget widget)
123 	{
124 		auto p = dzl_suggestion_get_icon_surface(dzlSuggestion, (widget is null) ? null : widget.getWidgetStruct());
125 
126 		if(p is null)
127 		{
128 			return null;
129 		}
130 
131 		return new Surface(cast(cairo_surface_t*) p);
132 	}
133 
134 	/** */
135 	public string getId()
136 	{
137 		return Str.toString(dzl_suggestion_get_id(dzlSuggestion));
138 	}
139 
140 	/** */
141 	public string getSubtitle()
142 	{
143 		return Str.toString(dzl_suggestion_get_subtitle(dzlSuggestion));
144 	}
145 
146 	/** */
147 	public string getTitle()
148 	{
149 		return Str.toString(dzl_suggestion_get_title(dzlSuggestion));
150 	}
151 
152 	/**
153 	 * This function is meant to be used to replace the text in the entry with text
154 	 * that represents the suggestion most accurately. This happens when the user
155 	 * presses tab while typing a suggestion. For example, if typing "gno" in the
156 	 * entry, you might have a suggest_suffix of "me.org" so that the user sees
157 	 * "gnome.org". But the replace_typed_text might include more data such as
158 	 * "https://gnome.org" as it more closely represents the suggestion.
159 	 *
160 	 * Params:
161 	 *     typedText = the text that was typed into the entry
162 	 *
163 	 * Returns: The replacement text to insert into
164 	 *     the entry when "tab" is pressed to complete the insertion.
165 	 */
166 	public string replaceTypedText(string typedText)
167 	{
168 		auto retStr = dzl_suggestion_replace_typed_text(dzlSuggestion, Str.toStringz(typedText));
169 
170 		scope(exit) Str.freeString(retStr);
171 		return Str.toString(retStr);
172 	}
173 
174 	/** */
175 	public void setIconName(string iconName)
176 	{
177 		dzl_suggestion_set_icon_name(dzlSuggestion, Str.toStringz(iconName));
178 	}
179 
180 	/** */
181 	public void setId(string id)
182 	{
183 		dzl_suggestion_set_id(dzlSuggestion, Str.toStringz(id));
184 	}
185 
186 	/** */
187 	public void setSubtitle(string subtitle)
188 	{
189 		dzl_suggestion_set_subtitle(dzlSuggestion, Str.toStringz(subtitle));
190 	}
191 
192 	/** */
193 	public void setTitle(string title)
194 	{
195 		dzl_suggestion_set_title(dzlSuggestion, Str.toStringz(title));
196 	}
197 
198 	/**
199 	 * This function requests potential text to append to @typed_text to make it
200 	 * more clear to the user what they will be activating by selecting this
201 	 * suggestion. For example, if they start typing "gno", a potential suggested
202 	 * suffix might be "me.org" to create "gnome.org".
203 	 *
204 	 * Params:
205 	 *     typedText = The user entered text
206 	 *
207 	 * Returns: Suffix to append to @typed_text
208 	 *     or %NULL to leave it unchanged.
209 	 */
210 	public string suggestSuffix(string typedText)
211 	{
212 		auto retStr = dzl_suggestion_suggest_suffix(dzlSuggestion, Str.toStringz(typedText));
213 
214 		scope(exit) Str.freeString(retStr);
215 		return Str.toString(retStr);
216 	}
217 
218 	/** */
219 	gulong addOnReplaceTypedText(string delegate(string, Suggestion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
220 	{
221 		return Signals.connect(this, "replace-typed-text", dlg, connectFlags ^ ConnectFlags.SWAPPED);
222 	}
223 
224 	/** */
225 	gulong addOnSuggestSuffix(string delegate(string, Suggestion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
226 	{
227 		return Signals.connect(this, "suggest-suffix", dlg, connectFlags ^ ConnectFlags.SWAPPED);
228 	}
229 }