0

我不明白如何计算gtk_source_search_context_get_occurrences_countGtkSourceBuffer 中出现的次数。正确找到并突出显示所有事件,但函数始终返回 -1,根据文档https://developer-old.gnome.org/gtksourceview/3.24/GtkSourceSearchContext.html#gtk-source-search-context-get-occurrences -count表示缓冲区没有被完全扫描。你能告诉我如何完全扫描缓冲区吗?这就是我目前所拥有的:

void find (GtkButton *button, gpointer userData) { (void)button;

struct lit *myData = (struct lit*)userData;

const gchar *searchString = NULL;

GtkSourceSearchContext *search_context;
GtkSourceSearchSettings *settings;

GtkSourceBuffer *buffer = GTK_SOURCE_BUFFER(get_current_buffer(myData));

GtkTextIter start_sel, end_sel;

searchString = gtk_entry_get_text(GTK_ENTRY(search_entry));

if (searchString != NULL)
{
    settings = gtk_source_search_settings_new ();

    search_context = gtk_source_search_context_new(buffer, settings);

    if (check_case)
        gtk_source_search_settings_set_case_sensitive (settings, TRUE);

    else
        gtk_source_search_settings_set_case_sensitive (settings, FALSE);

    gtk_source_search_settings_set_search_text (settings, searchString);

    gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(buffer), &start_sel, &end_sel);

    GtkTextIter current_loc;

    gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER(buffer), &current_loc);

    gtk_source_search_context_forward2 (search_context, &current_loc, &start_sel, &end_sel, NULL);

    gtk_source_search_settings_set_wrap_around
                           (settings,
                            TRUE);

    gint numberOccurences = gtk_source_search_context_get_occurrences_count (search_context);

    g_print("%d\n", numberOccurences);

    if (numberOccurences != -1)
    {
        const gchar *occurences = g_strdup_printf("%i occurences", numberOccurences);

        gtk_label_set_text (GTK_LABEL(label_occurences), occurences);

        gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW(currentTabSourceView(myData)),
                             &start_sel,
                             0,
                             TRUE,
                             0.0,
                             0.0);

        g_signal_connect (buffer, "notify::text", G_CALLBACK (clearSearchHighlight), search_context);
    }
}

}

4

0 回答 0