change function "gtk_text_buffer_select_range" to "gtk_text_buffer_apply_tag", then doesn't work!
- From: gepolv <sssssiqn gmail com>
- To: gtk-app-devel-list gnome org
- Subject: change function "gtk_text_buffer_select_range" to "gtk_text_buffer_apply_tag", then doesn't work!
- Date: Mon, 13 Mar 2006 17:49:40 +0800
when i run the ATTACHED SOURCE CODE,it works perfectly.But when i change the
function search_button_clicked as follow:
void search_button_clicked (GtkWidget *search_button, App *app)
{
const gchar *text;
GtkTextBuffer *buffer;
GtkTextIter iter;
GtkTextIter mstart, mend;
gboolean found;
/*my own*/
GtkTextTag *tag;
/*my own end*/
text = gtk_entry_get_text (GTK_ENTRY (app->search_entry));
/* Get the buffer associated with the text view widget. */
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->text_view));
/* Search from the start from buffer for text. */
gtk_text_buffer_get_start_iter (buffer, &iter);
found = gtk_text_iter_forward_search (&iter, text, 0, &mstart, &mend,
NULL);
/*my own*/
tag = gtk_text_buffer_create_tag (buffer, NULL,"foreground", "red",
NULL);
/*my own end */
if (found)
{
/* If found, hilight the text. */
/*my own*/
gtk_text_buffer_apply_tag (buffer, tag, &mstart, &mend);
/*my own end*/
}
}
After changing,when i press the button one time,it does not work at all,but
when i press again,it works!
Namely,i press two times ,then it works.But the original program requires
only time press,then it works.
I am confused,please relax me!
ANY HELP WILL BE APPRECITAED!
THE ATTACHED SOURCE CODE:
#include <gtk/gtk.h>
typedef struct _App
{
GtkWidget *text_view;
GtkWidget *search_entry;
} App;
int i=0;
/* Called when main window is destroyed. */
void
win_destroy (void)
{
gtk_main_quit();
}
/* Called when search button is clicked. */
void
search_button_clicked (GtkWidget *search_button, App *app)
{
const gchar *text;
GtkTextBuffer *buffer;
GtkTextIter iter;
GtkTextIter mstart, mend;
gboolean found;
//GtkTextTag *tag;
text = gtk_entry_get_text (GTK_ENTRY (app->search_entry));
/* Get the buffer associated with the text view widget. */
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->text_view));
/* Search from the start from buffer for text. */
gtk_text_buffer_get_start_iter (buffer, &iter);
found = gtk_text_iter_forward_search (&iter, text, 0, &mstart, &mend,
NULL);
//tag = gtk_text_buffer_create_tag (buffer, NULL,
"foreground", "red", NULL);
if (found)
{
/* If found, hilight the text. */
gtk_text_buffer_select_range (buffer, &mstart, &mend);
//gtk_text_buffer_apply_tag (buffer, tag, &mstart, &mend);
}
}
int
main (int argc, char *argv[])
{
GtkWidget *win;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *search_button;
GtkWidget *swindow;
App app;
gtk_init (&argc, &argv);
/* Create a window with a search entry, search button and a text
area. */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (win), "destroy", win_destroy, NULL);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_add (GTK_CONTAINER (win), vbox);
hbox = gtk_hbox_new (FALSE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
app.search_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (hbox), app.search_entry, TRUE, TRUE, 0);
search_button = gtk_button_new_with_label ("Search");
gtk_box_pack_start (GTK_BOX (hbox), search_button, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (search_button), "clicked",
G_CALLBACK (search_button_clicked), &app);
/* A scrolled window which automatically displays horizontal and
vertical scrollbars when the text exceeds the text view's size. */
swindow = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);
app.text_view = gtk_text_view_new ();
gtk_container_add (GTK_CONTAINER (swindow), app.text_view);
gtk_widget_show_all (win);
gtk_main();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]