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