bonjour
dans les codes sources de gtksourceview il dans le dossier test un widget customisé pour rechercher du texte. Je n'arrive pas à le faire fonctionner test-search.c


je suis sous
version Gtksource : 3.20.4
version gtk 3.20.9
je compile avec
gcc -std=c11 -Wall -fmax-errors=10 -Wextra test-search.c -o test-search `pkg-config --cflags --libs gtk+-3.0 gtksourceview-3.0`

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*-
 * test-search.c
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2013 - Sébastien Wilmet <swilmet@gnome.org>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>
//test-search.c
/*  Compile with:
 *  gcc -std=c11 -Wall -fmax-errors=10 -Wextra test-search.c -o test-search `pkg-config --cflags --libs gtk+-3.0 gtksourceview-3.0`
 */
 
 
#define TEST_TYPE_SEARCH             (test_search_get_type ())
#define TEST_SEARCH(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_SEARCH, TestSearch))
#define TEST_SEARCH_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_SEARCH, TestSearchClass))
#define TEST_IS_SEARCH(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_SEARCH))
#define TEST_IS_SEARCH_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_SEARCH))
#define TEST_SEARCH_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_SEARCH, TestSearchClass))
 
typedef struct _TestSearch        TestSearch;
typedef struct _TestSearchClass   TestSearchClass;
typedef struct _TestSearchPrivate TestSearchPrivate;
 
struct _TestSearch
{
	GtkGrid parent;
	TestSearchPrivate *priv;
};
 
struct _TestSearchClass
{
	GtkGridClass parent_class;
};
 
struct _TestSearchPrivate
{
	GtkSourceView *source_view;
	GtkSourceBuffer *source_buffer;
	GtkSourceSearchContext *search_context;
	GtkSourceSearchSettings *search_settings;
	GtkEntry *replace_entry;
	GtkLabel *label_occurrences;
	GtkLabel *label_regex_error;
 
	guint idle_update_label_id;
};
 
GType test_search_get_type (void);
 
G_DEFINE_TYPE_WITH_PRIVATE (TestSearch, test_search, GTK_TYPE_GRID)
 
static void open_file (TestSearch  *search,    const gchar *filename)
{
	gchar *contents;
	GError *error = NULL;
	GtkSourceLanguageManager *language_manager;
	GtkSourceLanguage *language;
	GtkTextIter iter;
 
	/* In a realistic application you would use GtkSourceFile of course. */
	if (!g_file_get_contents (filename, &contents, NULL, &error))
	{
		g_error (" bad choice :Impossible to load file: %s", error->message);
	}
 
	gtk_text_buffer_set_text (GTK_TEXT_BUFFER (search->priv->source_buffer),
				  contents,
				  -1);
 
	language_manager = gtk_source_language_manager_get_default ();
	language = gtk_source_language_manager_get_language (language_manager, "c");
	gtk_source_buffer_set_language (search->priv->source_buffer, language);
 
	gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (search->priv->source_buffer),
					&iter);
 
	gtk_text_buffer_select_range (GTK_TEXT_BUFFER (search->priv->source_buffer),
				      &iter,
				      &iter);
 
	g_free (contents);
}
 
static void
update_label_occurrences (TestSearch *search)
{
	gint occurrences_count;
	GtkTextIter select_start;
	GtkTextIter select_end;
	gint occurrence_pos;
	gchar *text;
 
	occurrences_count = gtk_source_search_context_get_occurrences_count (search->priv->search_context);
 
	gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
					      &select_start,
					      &select_end);
 
	occurrence_pos = gtk_source_search_context_get_occurrence_position (search->priv->search_context,
									    &select_start,
									    &select_end);
 
	if (occurrences_count == -1)
	{
		text = g_strdup ("");
	}
	else if (occurrence_pos == -1)
	{
		text = g_strdup_printf ("%u occurrences", occurrences_count);
	}
	else
	{
		text = g_strdup_printf ("%d of %u", occurrence_pos, occurrences_count);
	}
 
	gtk_label_set_text (search->priv->label_occurrences, text);
	g_free (text);
}
 
static void
update_label_regex_error (TestSearch *search)
{
	GError *error;
 
	error = gtk_source_search_context_get_regex_error (search->priv->search_context);
 
	if (error == NULL)
	{
		gtk_label_set_text (search->priv->label_regex_error, "");
		gtk_widget_hide (GTK_WIDGET (search->priv->label_regex_error));
	}
	else
	{
		gtk_label_set_text (search->priv->label_regex_error, error->message);
		gtk_widget_show (GTK_WIDGET (search->priv->label_regex_error));
		g_clear_error (&error);
	}
}
 
static void
search_entry_changed_cb (TestSearch *search,
			 GtkEntry   *entry)
{
	const gchar *text = gtk_entry_get_text (entry);
	gchar *unescaped_text = gtk_source_utils_unescape_search_text (text);
 
	gtk_source_search_settings_set_search_text (search->priv->search_settings, unescaped_text);
	g_free (unescaped_text);
}
 
static void
select_search_occurrence (TestSearch        *search,
			  const GtkTextIter *match_start,
			  const GtkTextIter *match_end)
{
	GtkTextMark *insert;
 
	gtk_text_buffer_select_range (GTK_TEXT_BUFFER (search->priv->source_buffer),
				      match_start,
				      match_end);
 
	insert = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (search->priv->source_buffer));
 
	gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (search->priv->source_view),
					    insert);
}
 
static void
backward_search_finished (GtkSourceSearchContext *search_context,
			  GAsyncResult           *result,
			  TestSearch             *search)
{
	GtkTextIter match_start;
	GtkTextIter match_end;
 
	///if (gtk_source_search_context_backward_finish2 (search_context, result,      &match_start,&match_end,NULL,NULL))
	if (gtk_source_search_context_backward_finish (search_context, result,	&match_start,&match_end,NULL))
	{
		select_search_occurrence (search, &match_start, &match_end);
	}
}
 
static void
button_previous_clicked_cb (TestSearch *search,
			    GtkButton  *button)
{
	GtkTextIter start_at;
 
	gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
					      &start_at,
					      NULL);
 
	gtk_source_search_context_backward_async (search->priv->search_context,
						  &start_at,
						  NULL,
						  (GAsyncReadyCallback)backward_search_finished,
						  search);
		(void)button;
}
 
static void forward_search_finished (GtkSourceSearchContext *search_context,  GAsyncResult   *result, TestSearch             *search)
{
	GtkTextIter match_start;
	GtkTextIter match_end;
 
	///if (gtk_source_search_context_forward_finish2(search_context, result, &match_start, &match_end, NULL, NULL))
	if (gtk_source_search_context_forward_finish(search_context, result, &match_start, &match_end, NULL))
	{
		select_search_occurrence (search, &match_start, &match_end);
	}
}
 
static void button_next_clicked_cb (TestSearch *search, GtkButton  *button)
{
	GtkTextIter start_at;
 
	gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
					      NULL,
					      &start_at);
 
	gtk_source_search_context_forward_async (search->priv->search_context,
						 &start_at,
						 NULL,
						 (GAsyncReadyCallback)forward_search_finished,
						 search);
		(void)button;
}
 
static void button_replace_clicked_cb (TestSearch *search, GtkButton  *button)
{
	GtkTextIter match_start;
	GtkTextIter match_end;
	GtkTextIter iter;
	GtkEntryBuffer *entry_buffer;
	gint replace_length;
 
	gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
					      &match_start,
					      &match_end);
 
	entry_buffer = gtk_entry_get_buffer (search->priv->replace_entry);
	replace_length = gtk_entry_buffer_get_bytes (entry_buffer);
 
	///gtk_source_search_context_replace2 (search->priv->search_context,
	gtk_source_search_context_replace (search->priv->search_context,
					    &match_start,
					    &match_end,
					    gtk_entry_get_text (search->priv->replace_entry),
					    replace_length,
					    NULL);
 
	gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
					      NULL,
					      &iter);
 
	gtk_source_search_context_forward_async (search->priv->search_context,
						 &iter,
						 NULL,
						 (GAsyncReadyCallback)forward_search_finished,
						 search);
		(void)button;
}
 
static void
button_replace_all_clicked_cb (TestSearch *search, GtkButton  *button)
{
	GtkEntryBuffer *entry_buffer = gtk_entry_get_buffer (search->priv->replace_entry);
	gint replace_length = gtk_entry_buffer_get_bytes (entry_buffer);
 
	gtk_source_search_context_replace_all (search->priv->search_context,
					       gtk_entry_get_text (search->priv->replace_entry),
					       replace_length,
					       NULL);
		(void)button;
}
 
static gboolean update_label_idle_cb (TestSearch *search)
{
	search->priv->idle_update_label_id = 0;
 
	update_label_occurrences (search);
 
	return G_SOURCE_REMOVE;
}
 
static void mark_set_cb (GtkTextBuffer *buffer, GtkTextIter   *location, GtkTextMark   *mark,  TestSearch    *search)
{
	GtkTextMark *insert;
	GtkTextMark *selection_bound;
 
	insert = gtk_text_buffer_get_insert (buffer);
	selection_bound = gtk_text_buffer_get_selection_bound (buffer);
 
	if ((mark == insert || mark == selection_bound) &&
	    search->priv->idle_update_label_id == 0)
	{
		search->priv->idle_update_label_id = g_idle_add ((GSourceFunc)update_label_idle_cb,
								 search);
	}
(void)location;
}
 
static void highlight_toggled_cb (TestSearch      *search, GtkToggleButton *button)
{
	gtk_source_search_context_set_highlight (search->priv->search_context,
						 gtk_toggle_button_get_active (button));
		(void)button;
}
 
static void match_case_toggled_cb (TestSearch      *search, GtkToggleButton *button)
{
	gtk_source_search_settings_set_case_sensitive (search->priv->search_settings,
						       gtk_toggle_button_get_active (button));
}
 
static void
at_word_boundaries_toggled_cb (TestSearch      *search,
			       GtkToggleButton *button)
{
	gtk_source_search_settings_set_at_word_boundaries (search->priv->search_settings,
							   gtk_toggle_button_get_active (button));
		(void)button;
}
 
static void wrap_around_toggled_cb (TestSearch      *search, GtkToggleButton *button)
{
	gtk_source_search_settings_set_wrap_around (search->priv->search_settings,
						    gtk_toggle_button_get_active (button));
		(void)button;
}
 
static void
regex_toggled_cb (TestSearch      *search,
		  GtkToggleButton *button)
{
	gtk_source_search_settings_set_regex_enabled (search->priv->search_settings,
						      gtk_toggle_button_get_active (button));
}
 
static void test_search_dispose (GObject *object)
{
	TestSearch *search = TEST_SEARCH (object);
 
	g_clear_object (&search->priv->source_buffer);
	g_clear_object (&search->priv->search_context);
	g_clear_object (&search->priv->search_settings);
 
	G_OBJECT_CLASS (test_search_parent_class)->dispose (object);
}
 
static void test_search_class_init (TestSearchClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
	object_class->dispose = test_search_dispose;
 
	gtk_widget_class_set_template_from_resource (widget_class,
						     "/home/francis/programme_example/test/test-search.ui");
 
	gtk_widget_class_bind_template_child_private (widget_class, TestSearch, source_view);
	gtk_widget_class_bind_template_child_private (widget_class, TestSearch, replace_entry);
	gtk_widget_class_bind_template_child_private (widget_class, TestSearch, label_occurrences);
	gtk_widget_class_bind_template_child_private (widget_class, TestSearch, label_regex_error);
 
	gtk_widget_class_bind_template_callback (widget_class, search_entry_changed_cb);
	gtk_widget_class_bind_template_callback (widget_class, button_previous_clicked_cb);
	gtk_widget_class_bind_template_callback (widget_class, button_next_clicked_cb);
	gtk_widget_class_bind_template_callback (widget_class, button_replace_clicked_cb);
	gtk_widget_class_bind_template_callback (widget_class, button_replace_all_clicked_cb);
 
	/* It is also possible to bind the properties with
	 * g_object_bind_property(), between the check buttons and the source
	 * buffer. But GtkBuilder and Glade don't support that yet.
	 */
	gtk_widget_class_bind_template_callback (widget_class, highlight_toggled_cb);
	gtk_widget_class_bind_template_callback (widget_class, match_case_toggled_cb);
	gtk_widget_class_bind_template_callback (widget_class, at_word_boundaries_toggled_cb);
	gtk_widget_class_bind_template_callback (widget_class, wrap_around_toggled_cb);
	gtk_widget_class_bind_template_callback (widget_class, regex_toggled_cb);
}
 
static void test_search_init (TestSearch *search)
{
	search->priv = test_search_get_instance_private (search);
 
	gtk_widget_init_template (GTK_WIDGET (search));
 
	search->priv->source_buffer = GTK_SOURCE_BUFFER (
		gtk_text_view_get_buffer (GTK_TEXT_VIEW (search->priv->source_view)));
 
	g_object_ref (search->priv->source_buffer);
 
	open_file (search, "/home/francis/programme_example/chapitre_gtksourcevew_pasApas/gtksourceview_08.c");
 
	search->priv->search_settings = gtk_source_search_settings_new ();
 
	search->priv->search_context = gtk_source_search_context_new (search->priv->source_buffer,
								      search->priv->search_settings);
 
	g_signal_connect_swapped (search->priv->search_context,
				  "notify::occurrences-count",
				  G_CALLBACK (update_label_occurrences),
				  search);
 
	g_signal_connect (search->priv->source_buffer,
			  "mark-set",
			  G_CALLBACK (mark_set_cb),
			  search);
 
	g_signal_connect_swapped (search->priv->search_context,
				  "notify::regex-error",
				  G_CALLBACK (update_label_regex_error),
				  search);
 
	update_label_regex_error (search);
}
 
static TestSearch *
test_search_new (void)
{
	return g_object_new (test_search_get_type (), NULL);
}
 
gint
main (gint argc, gchar *argv[])
{
	GtkWidget *window;
	TestSearch *search;
 
	gtk_init (&argc, &argv);
 
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
		g_print("version Gtksource : %d.%d.%d\n", 	gtk_source_get_major_version (),
													gtk_source_get_minor_version (),
													gtk_source_get_micro_version ());
 
		g_printf("version gtk %d.%d.%d\n", gtk_major_version, gtk_minor_version, gtk_micro_version);
 
	gtk_window_set_default_size (GTK_WINDOW (window), 700, 500);
 
	g_signal_connect (window,
			  "destroy",
			  G_CALLBACK (gtk_main_quit),
			  NULL);
 
	search = test_search_new ();
	gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (search));
 
	gtk_widget_show (window);
 
	gtk_main ();
 
	return 0;
}

premiere bizarrerie
il y a plusieurs instruction dépréciée
gtk_source_search_context_backward_finish
à passer en
gtk_source_search_context_backward_finish2

gtk_source_search_context_forward_finish
à passer en
gtk_source_search_context_forward_finish2

gtk_source_search_context_replace
à passer en
gtk_source_search_context_replace2


si je le fais les versions ne sont pas reconnus à l’exécution, pourquoi cela ?

ensuite avec la version non modifiée le programme plante avec les erreurs suivantes
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(test-search:28603): Gtk-CRITICAL **: Unable to load resource for composite template for type 'TestSearch': La ressource dans «*test-search.ui*» n'existe pas
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_callback_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_callback_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_callback_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_callback_full: assertion 'widget_class->priv->template != NULL' failed
 
(test-search:28603): Gtk-CRITICAL **: gtk_widget_class_bind_template_callback_full: assertion 'widget_
je ne suis pas à l'aise avec les widget customisé si quelqu'un à une piste je suis preneur