File: gtktextsearch.c

package info (click to toggle)
vdk2 2.4.0-5.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,524 kB
  • ctags: 4,975
  • sloc: cpp: 26,950; sh: 10,854; ansic: 9,220; makefile: 603; perl: 113
file content (244 lines) | stat: -rw-r--r-- 7,395 bytes parent folder | download | duplicates (7)
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
/*
*    
*
*  Copyright (C) 2002 Mikael Hermansson <tyan@linux.se>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program 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 General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


#include <gtk/gtk.h>
#include <vdk/gtktextsearch.h>

static GObjectClass *parent_class = NULL;

void gtk_text_search_class_init (GtkTextSearchClass *klass);
void gtk_text_search_init (GtkTextSearch *object);
void gtk_text_search_finalize (GObject *object);


void
gtk_text_search_init(GtkTextSearch *text_search)
{
  text_search->search_for = g_strdup("");
  text_search->sflags = 0;
  text_search->mark_current = NULL;
  text_search->mark_stop = NULL;
  text_search->buffer = NULL;
}

void
gtk_text_search_class_init(GtkTextSearchClass *klass)
{
  GObjectClass *object_class;
  object_class = (GObjectClass*) klass;
  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gtk_text_search_finalize;
}

void
gtk_text_search_finalize (GObject *object)
{
  GtkTextSearch *text_search = GTK_TEXT_SEARCH (object);
  g_free (text_search->search_for);
  g_object_unref (G_OBJECT(text_search->mark_current));
  g_object_unref (G_OBJECT(text_search->mark_stop));
  g_object_unref (G_OBJECT(text_search->buffer));

   (* G_OBJECT_CLASS (parent_class)->finalize) (G_OBJECT(text_search));
}

GtkTextSearch *
gtk_text_search_new (GtkTextBuffer *buffer,
                              const GtkTextIter *start, 
                              const char *searchfor, 
                                  GtkETextSearchFlags sflags, 
                              const GtkTextIter *limit)
{
  GtkTextSearch *text_search;

  text_search = GTK_TEXT_SEARCH(g_object_new (GTK_TYPE_TEXT_SEARCH, NULL));
  gtk_text_search_set(text_search, buffer, start, searchfor, sflags, limit);
  
  return text_search;
}

void
gtk_text_search_set (GtkTextSearch *text_search,
                                GtkTextBuffer *buffer, 
                              const GtkTextIter *startp, 
                              const char *searchfor, 
                                  GtkETextSearchFlags sflags, 
                              const GtkTextIter *limit)
{
  GtkTextIter end, start;

  text_search = GTK_TEXT_SEARCH(g_object_new (GTK_TYPE_TEXT_SEARCH, NULL));

  if (text_search->buffer != buffer)  {
    g_object_ref (G_OBJECT(text_search->buffer));
  }

  if (startp)
    start = *startp;
  else
    gtk_text_buffer_get_start_iter(text_search->buffer, &start);

  if (limit)
    end = *limit;
  else
    gtk_text_buffer_get_end_iter(text_search->buffer, &end);

  if (sflags)  
    text_search->sflags = sflags;  
  
  if(searchfor)  {
    g_free(text_search->search_for);
    text_search->search_for = g_strdup (searchfor);
 }

  if (text_search->mark_current)
    g_object_unref(G_OBJECT(text_search->mark_current));
  if (text_search->mark_stop)
    g_object_unref(G_OBJECT(text_search->mark_stop));

  text_search->mark_current = gtk_text_buffer_create_mark(text_search->buffer, "search_mark_current", &start, FALSE);   
  text_search->mark_stop = gtk_text_buffer_create_mark(text_search->buffer, "search_mark_stop", &end, FALSE);   
}

void
gtk_text_search_set_interval (GtkTextSearch *text_search,
                                GtkTextBuffer *buffer, 
                              const GtkTextIter *start, 
                             const GtkTextIter *end)
{
  gtk_text_search_set (text_search, buffer, start, NULL, 0, end);
}

static gboolean
gtk_source_buffer_compare_unichar (gunichar ch,  gpointer data)
{
  GtkTextSearch *si;
  gunichar chsearch; 
  si = GTK_TEXT_SEARCH(data);

  /* there are no more characters match this means we succed scaning  :-) */
  if (!*si->offset)  {  
    si->is_matched = TRUE; 
    return TRUE;
  }


  chsearch = g_utf8_get_char(si->offset);  
  if ( ch == 0xFFFC && si->sflags & GTK_ETEXT_SEARCH_TEXT_ONLY)   /* we don't care about pixbufs */
    return FALSE;

  if (si->sflags & GTK_ETEXT_SEARCH_CASE_INSENSITIVE)  {
    chsearch = g_unichar_tolower(chsearch);
    ch = g_unichar_tolower(ch);
  }

  if (ch == chsearch) 
    si->offset = g_utf8_next_char(si->offset);
  else if (si->offset != si->search_for) /* the scan has already matched first character */
    return TRUE;  /* return TRUE stop scan because we failed on this character */

  return FALSE;
}

gboolean
gtk_text_search_forward (GtkTextSearch *search, GtkTextIter *match_start, GtkTextIter *match_end)
{
  GtkTextIter iter1, iter2;
/*  g_return_val_if_fail(search =! NULL, FALSE);*/

/*  g_return_val_if_fail(GTK_IS_TEXT_SEARCH(search), FALSE);*/

  /* always initialize */   
  search->is_matched = FALSE;
  search->offset = search->search_for;


  gtk_text_buffer_get_iter_at_mark (search->buffer, &iter1, search->mark_current);
  gtk_text_buffer_get_iter_at_mark (search->buffer, &iter2, search->mark_stop);

  gtk_source_buffer_compare_unichar (gtk_text_iter_get_char(&iter1), search);
  gtk_text_iter_forward_find_char (&iter1, gtk_source_buffer_compare_unichar, search , &iter2);
  gtk_text_buffer_move_mark (search->buffer, search->mark_current, &iter1);   
  if ( search->is_matched)
  {
    *match_start = iter1;
    *match_end = iter1;
    gtk_text_iter_backward_chars(match_start, g_utf8_strlen(search->search_for, -1));

    return TRUE;
  }

  return FALSE;  
}

gint
gtk_text_search_forward_foreach (GtkTextSearch *search, GtkTextSearchForeachFunc sfunc, gpointer data)
{
  GtkTextIter iter1, iter2, start_match, end_match;
  gint count = 0;
  
  gtk_text_buffer_get_iter_at_mark (search->buffer, &iter1, search->mark_current);
  gtk_text_buffer_get_iter_at_mark (search->buffer, &iter2, search->mark_stop);
  while (gtk_text_iter_compare (&iter1, &iter2) < 0)  {
    if (gtk_text_search_forward (search, &start_match, &end_match))  {
      count++;
      if (sfunc (&start_match, &end_match, data))
        return count;
    }

    gtk_text_buffer_get_iter_at_mark (search->buffer, &iter1, search->mark_current);
    gtk_text_buffer_get_iter_at_mark (search->buffer, &iter2, search->mark_stop);
  }
  
  return count;
}


GType gtk_text_search_get_type ()
{
  static GType our_type = 0;

  if (our_type == 0)
    {
      static const GTypeInfo our_info =
      {
        sizeof (GtkTextSearchClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_text_search_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkTextSearch),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_text_search_init
      };

      our_type = g_type_register_static (G_TYPE_OBJECT,
                                         "GtkTextSearch",
                                         &our_info,
                                         0);
    }

  return our_type;
  
}