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

#include <gtk/gtk.h>
#include "common.h"
#include "search.h"

GtkWidget *search_text_view = NULL;
GtkWidget *search_find_dialog_entry = 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_source_iter_forward_search(&start, text, 0, &m_start, &m_end, NULL);

  if (!found) {
    gtk_text_buffer_get_start_iter(text_buffer, &start);
    found = gtk_source_iter_forward_search(&start, text, 0, &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(GtkDialog *dialog,
                   gint response_id,
                   gpointer user_data) {
  if (response_id != GTK_RESPONSE_ACCEPT)
    return;

  if (!GTK_IS_TEXT_VIEW(search_text_view))
    return;

  if (GTK_IS_ENTRY(search_find_dialog_entry))
    search_find_text(g_strdup(gtk_entry_get_text(GTK_ENTRY(search_find_dialog_entry))));
}

void search_find_dialog_show(GtkWindow *parent,
                             GtkWidget *text_view) {
  GtkWidget *dialog;
  GtkWidget *vbox;
  GtkWidget *entry;
  GtkWidget *content_area;
  GtkWidget *label;

  dialog = gtk_dialog_new_with_buttons(_("Find"),
                                       parent,
                                       GTK_DIALOG_DESTROY_WITH_PARENT,
                                       GTK_STOCK_FIND,
                                       GTK_RESPONSE_ACCEPT,
                                       NULL);
  gtk_widget_set_size_request(dialog, 240, 80);
  gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
  content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

  entry = gtk_entry_new();
  gtk_container_add(GTK_CONTAINER(content_area), entry);

  g_signal_connect(dialog,
                   "response",
                   G_CALLBACK(search_find_dialog_response_callback),
                   NULL);

  search_text_view = text_view;
  search_find_dialog_entry = entry;

  gtk_dialog_run(dialog);
}