1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
2 /* gtksourcecompletionproposal.c
3 * This file is part of GtkSourceView
4 *
5 * Copyright (C) 2007 - 2009 Jesús Barbero Rodríguez <chuchiperriman@gmail.com>
6 * Copyright (C) 2009 - Jesse van den Kieboom <jessevdk@gnome.org>
7 *
8 * GtkSourceView is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * GtkSourceView is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "gtksourcecompletionproposal.h"
28
29 /**
30 * SECTION:completionproposal
31 * @title: GtkSourceCompletionProposal
32 * @short_description: Completion proposal interface
33 *
34 * The proposal interface represents a completion item in the completion window.
35 * It provides information on how to display the completion item and what action
36 * should be taken when the completion item is activated.
37 *
38 * The proposal is displayed in the completion window with a label and
39 * optionally an icon.
40 * The label may be specified using plain text or markup by implementing
41 * the corresponding get function. Only one of those get functions
42 * should return a value different from %NULL.
43 * The icon may be specified as a #GdkPixbuf, as an icon name or as a #GIcon by
44 * implementing the corresponding get function. At most one of those get functions
45 * should return a value different from %NULL, if they all return %NULL no icon
46 * will be used.
47 */
48
49 enum
50 {
51 CHANGED,
52 N_SIGNALS
53 };
54
55 static guint signals[N_SIGNALS];
56
57 typedef GtkSourceCompletionProposalIface GtkSourceCompletionProposalInterface;
58
G_DEFINE_INTERFACE(GtkSourceCompletionProposal,gtk_source_completion_proposal,G_TYPE_OBJECT)59 G_DEFINE_INTERFACE (GtkSourceCompletionProposal, gtk_source_completion_proposal, G_TYPE_OBJECT)
60
61 static gchar *
62 gtk_source_completion_proposal_get_label_default (GtkSourceCompletionProposal *proposal)
63 {
64 return NULL;
65 }
66
67 static gchar *
gtk_source_completion_proposal_get_markup_default(GtkSourceCompletionProposal * proposal)68 gtk_source_completion_proposal_get_markup_default (GtkSourceCompletionProposal *proposal)
69 {
70 return NULL;
71 }
72
73 static gchar *
gtk_source_completion_proposal_get_text_default(GtkSourceCompletionProposal * proposal)74 gtk_source_completion_proposal_get_text_default (GtkSourceCompletionProposal *proposal)
75 {
76 return NULL;
77 }
78
79 static GdkPixbuf *
gtk_source_completion_proposal_get_icon_default(GtkSourceCompletionProposal * proposal)80 gtk_source_completion_proposal_get_icon_default (GtkSourceCompletionProposal *proposal)
81 {
82 return NULL;
83 }
84
85 static const gchar *
gtk_source_completion_proposal_get_icon_name_default(GtkSourceCompletionProposal * proposal)86 gtk_source_completion_proposal_get_icon_name_default (GtkSourceCompletionProposal *proposal)
87 {
88 return NULL;
89 }
90
91 static GIcon *
gtk_source_completion_proposal_get_gicon_default(GtkSourceCompletionProposal * proposal)92 gtk_source_completion_proposal_get_gicon_default (GtkSourceCompletionProposal *proposal)
93 {
94 return NULL;
95 }
96
97 static gchar *
gtk_source_completion_proposal_get_info_default(GtkSourceCompletionProposal * proposal)98 gtk_source_completion_proposal_get_info_default (GtkSourceCompletionProposal *proposal)
99 {
100 return NULL;
101 }
102
103 static guint
gtk_source_completion_proposal_hash_default(GtkSourceCompletionProposal * proposal)104 gtk_source_completion_proposal_hash_default (GtkSourceCompletionProposal *proposal)
105 {
106 return g_direct_hash (proposal);
107 }
108
109 static gboolean
gtk_source_completion_proposal_equal_default(GtkSourceCompletionProposal * proposal,GtkSourceCompletionProposal * other)110 gtk_source_completion_proposal_equal_default (GtkSourceCompletionProposal *proposal,
111 GtkSourceCompletionProposal *other)
112 {
113 return g_direct_equal (proposal, other);
114 }
115
116 static void
gtk_source_completion_proposal_default_init(GtkSourceCompletionProposalIface * iface)117 gtk_source_completion_proposal_default_init (GtkSourceCompletionProposalIface *iface)
118 {
119 static gboolean initialized = FALSE;
120
121 iface->get_label = gtk_source_completion_proposal_get_label_default;
122 iface->get_markup = gtk_source_completion_proposal_get_markup_default;
123 iface->get_text = gtk_source_completion_proposal_get_text_default;
124 iface->get_icon = gtk_source_completion_proposal_get_icon_default;
125 iface->get_icon_name = gtk_source_completion_proposal_get_icon_name_default;
126 iface->get_gicon = gtk_source_completion_proposal_get_gicon_default;
127 iface->get_info = gtk_source_completion_proposal_get_info_default;
128 iface->hash = gtk_source_completion_proposal_hash_default;
129 iface->equal = gtk_source_completion_proposal_equal_default;
130
131 if (!initialized)
132 {
133 /**
134 * GtkSourceCompletionProposal::changed:
135 * @proposal: The #GtkSourceCompletionProposal
136 *
137 * Emitted when the proposal has changed. The completion popup
138 * will react to this by updating the shown information.
139 *
140 */
141 signals[CHANGED] =
142 g_signal_new ("changed",
143 G_TYPE_FROM_INTERFACE (iface),
144 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
145 G_STRUCT_OFFSET (GtkSourceCompletionProposalIface, changed),
146 NULL, NULL, NULL,
147 G_TYPE_NONE, 0);
148
149 initialized = TRUE;
150 }
151 }
152
153 /**
154 * gtk_source_completion_proposal_get_label:
155 * @proposal: a #GtkSourceCompletionProposal.
156 *
157 * Gets the label of @proposal. The label is shown in the list of proposals as
158 * plain text. If you need any markup (such as bold or italic text), you have
159 * to implement gtk_source_completion_proposal_get_markup(). The returned string
160 * must be freed with g_free().
161 *
162 * Returns: a new string containing the label of @proposal.
163 */
164 gchar *
gtk_source_completion_proposal_get_label(GtkSourceCompletionProposal * proposal)165 gtk_source_completion_proposal_get_label (GtkSourceCompletionProposal *proposal)
166 {
167 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
168
169 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_label (proposal);
170 }
171
172 /**
173 * gtk_source_completion_proposal_get_markup:
174 * @proposal: a #GtkSourceCompletionProposal.
175 *
176 * Gets the label of @proposal with markup. The label is shown in the list of
177 * proposals and may contain markup. This will be used instead of
178 * gtk_source_completion_proposal_get_label() if implemented. The returned string
179 * must be freed with g_free().
180 *
181 * Returns: a new string containing the label of @proposal with markup.
182 */
183 gchar *
gtk_source_completion_proposal_get_markup(GtkSourceCompletionProposal * proposal)184 gtk_source_completion_proposal_get_markup (GtkSourceCompletionProposal *proposal)
185 {
186 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
187
188 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_markup (proposal);
189 }
190
191 /**
192 * gtk_source_completion_proposal_get_text:
193 * @proposal: a #GtkSourceCompletionProposal.
194 *
195 * Gets the text of @proposal. The text that is inserted into
196 * the text buffer when the proposal is activated by the default activation.
197 * You are free to implement a custom activation handler in the provider and
198 * not implement this function. For more information, see
199 * gtk_source_completion_provider_activate_proposal(). The returned string must
200 * be freed with g_free().
201 *
202 * Returns: a new string containing the text of @proposal.
203 */
204 gchar *
gtk_source_completion_proposal_get_text(GtkSourceCompletionProposal * proposal)205 gtk_source_completion_proposal_get_text (GtkSourceCompletionProposal *proposal)
206 {
207 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
208
209 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_text (proposal);
210 }
211
212 /**
213 * gtk_source_completion_proposal_get_icon:
214 * @proposal: a #GtkSourceCompletionProposal.
215 *
216 * Gets the #GdkPixbuf for the icon of @proposal.
217 *
218 * Returns: (nullable) (transfer none): A #GdkPixbuf with the icon of @proposal.
219 */
220 GdkPixbuf *
gtk_source_completion_proposal_get_icon(GtkSourceCompletionProposal * proposal)221 gtk_source_completion_proposal_get_icon (GtkSourceCompletionProposal *proposal)
222 {
223 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
224
225 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_icon (proposal);
226 }
227
228 /**
229 * gtk_source_completion_proposal_get_icon_name:
230 * @proposal: a #GtkSourceCompletionProposal.
231 *
232 * Gets the icon name of @proposal.
233 *
234 * Returns: (nullable) (transfer none): The icon name of @proposal.
235 *
236 * Since: 3.18
237 */
238 const gchar *
gtk_source_completion_proposal_get_icon_name(GtkSourceCompletionProposal * proposal)239 gtk_source_completion_proposal_get_icon_name (GtkSourceCompletionProposal *proposal)
240 {
241 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
242
243 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_icon_name (proposal);
244 }
245
246 /**
247 * gtk_source_completion_proposal_get_gicon:
248 * @proposal: a #GtkSourceCompletionProposal.
249 *
250 * Gets the #GIcon for the icon of @proposal.
251 *
252 * Returns: (nullable) (transfer none): A #GIcon with the icon of @proposal.
253 *
254 * Since: 3.18
255 */
256 GIcon *
gtk_source_completion_proposal_get_gicon(GtkSourceCompletionProposal * proposal)257 gtk_source_completion_proposal_get_gicon (GtkSourceCompletionProposal *proposal)
258 {
259 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
260
261 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_gicon (proposal);
262 }
263
264 /**
265 * gtk_source_completion_proposal_get_info:
266 * @proposal: a #GtkSourceCompletionProposal.
267 *
268 * Gets extra information associated to the proposal. This information will be
269 * used to present the user with extra, detailed information about the
270 * selected proposal. The returned string must be freed with g_free().
271 *
272 * Returns: (nullable) (transfer full): a newly-allocated string containing
273 * extra information of @proposal or %NULL if no extra information is associated
274 * to @proposal.
275 */
276 gchar *
gtk_source_completion_proposal_get_info(GtkSourceCompletionProposal * proposal)277 gtk_source_completion_proposal_get_info (GtkSourceCompletionProposal *proposal)
278 {
279 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), NULL);
280
281 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->get_info (proposal);
282 }
283
284 /**
285 * gtk_source_completion_proposal_hash:
286 * @proposal: a #GtkSourceCompletionProposal.
287 *
288 * Get the hash value of @proposal. This is used to (together with
289 * gtk_source_completion_proposal_equal()) to match proposals in the completion
290 * model. By default, it uses a direct hash (g_direct_hash()).
291 *
292 * Returns: The hash value of @proposal.
293 */
294 guint
gtk_source_completion_proposal_hash(GtkSourceCompletionProposal * proposal)295 gtk_source_completion_proposal_hash (GtkSourceCompletionProposal *proposal)
296 {
297 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), 0);
298
299 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->hash (proposal);
300 }
301
302 /**
303 * gtk_source_completion_proposal_equal:
304 * @proposal: a #GtkSourceCompletionProposal.
305 * @other: a #GtkSourceCompletionProposal.
306 *
307 * Get whether two proposal objects are the same. This is used to (together
308 * with gtk_source_completion_proposal_hash()) to match proposals in the
309 * completion model. By default, it uses direct equality (g_direct_equal()).
310 *
311 * Returns: %TRUE if @proposal and @object are the same proposal
312 */
313 gboolean
gtk_source_completion_proposal_equal(GtkSourceCompletionProposal * proposal,GtkSourceCompletionProposal * other)314 gtk_source_completion_proposal_equal (GtkSourceCompletionProposal *proposal,
315 GtkSourceCompletionProposal *other)
316 {
317 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal), FALSE);
318 g_return_val_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (other), FALSE);
319
320 return GTK_SOURCE_COMPLETION_PROPOSAL_GET_INTERFACE (proposal)->equal (proposal, other);
321 }
322
323 /**
324 * gtk_source_completion_proposal_changed:
325 * @proposal: a #GtkSourceCompletionProposal.
326 *
327 * Emits the "changed" signal on @proposal. This should be called by
328 * implementations whenever the name, icon or info of the proposal has
329 * changed.
330 */
331 void
gtk_source_completion_proposal_changed(GtkSourceCompletionProposal * proposal)332 gtk_source_completion_proposal_changed (GtkSourceCompletionProposal *proposal)
333 {
334 g_return_if_fail (GTK_SOURCE_IS_COMPLETION_PROPOSAL (proposal));
335 g_signal_emit (proposal, signals[CHANGED], 0);
336 }
337