summaryrefslogtreecommitdiff
path: root/saedit/search.c
blob: e8d4097967806d9569c149926d1d0811995c9d19 (plain) (blame)
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
/*=======================================*\
|  ____                         ____      |
| /    \         /\            |          |
| \____         /  \           |____      |
|      \       /____\          |          |
| \____/prite /      \nimation |____ditor |
|                                         |
|      Copyleft Vasily_Makarov 2011       |
|                                         |
\*=======================================*/

#include "search.h"

GtkWidget *search_text_view = NULL;
gchar *search_last_text = NULL;

gboolean search_find_text(gchar *text) {
	search_last_text = text;

	GtkTextBuffer *text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(search_text_view));
	if (!GTK_IS_TEXT_BUFFER(text_buffer))
		return FALSE;

	gboolean found;

	GtkTextIter m_start, m_end, start;
	gtk_text_buffer_get_selection_bounds(text_buffer, NULL, &start);
	found = gtk_text_iter_forward_search(&start, text, 4, &m_start, &m_end, NULL);

	if (!found) {
		gtk_text_buffer_get_start_iter(text_buffer, &start);
		found = gtk_text_iter_forward_search(&start, text, 4, &m_start, &m_end, NULL);
	}

	if (found) {
		gtk_text_buffer_place_cursor(text_buffer, &m_start);
		gtk_text_buffer_move_mark_by_name(text_buffer, "selection_bound", &m_end);
		gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW(search_text_view),
		                              gtk_text_buffer_get_insert(text_buffer),
		                              0.25,
		                              FALSE,
		                              0.0,
		                              0.0);
		return TRUE;
	} else
		return FALSE;
}

gboolean search_find_next() {
	if (search_last_text != NULL)
		search_find_text(search_last_text);
}

void search_find_dialog_response_callback(GtkWidget *dialog,
        gint response_id,
        gpointer entry) {

	if (response_id == GTK_RESPONSE_CANCEL || response_id == GTK_RESPONSE_DELETE_EVENT)
		gtk_widget_hide(dialog);
	else {

		g_return_if_fail(response_id == GTK_RESPONSE_ACCEPT);
		g_return_if_fail(GTK_IS_TEXT_VIEW(search_text_view));
		g_return_if_fail(GTK_IS_ENTRY(GTK_ENTRY(entry)));

		search_find_text(g_strdup(gtk_entry_get_text(GTK_ENTRY(entry))));

	}
}

void search_init(GtkWidget *text_view) {
	search_text_view = text_view;
}