help with GtkEntry()



Hi, 

I'm a newbie, and I have a question about GtkEntry().
I'm trying to understand how to select text in the entry box, so that
I can add some text to what the user has typed in, and then force
it to look as though the user selected it (and it will appear on the
screen highlighted).  For example, In the following program, if the user types in
"ABC", I add in "DEFGHIJKL" and then want the DEFGHIJKL to be selected
(and highlighted, so that if the user then types P, the P will replace the
DEFGHIJKL).

The problem is, no matter how I try to select the region, or for that matter, set the
position, the cursor is always before the "D" and nothing is selected. HELP!
What am i doing wrong? 

I am running this on windows XP, using gtk 2.4.7, from Tor Lillqvist's site.

Thanks

Sharon

-----------

#include <stdio.h>
#include <gtk/gtk.h>

FILE *errlog;

void dstry()
{
    gtk_main_quit();
}

void changed_text_handler(GtkEditable *editable, gpointer data)
{
    char *etext;
    int l, x = 0, *p = &x;
    
    etext = gtk_editable_get_chars(editable, 0, -1);
 fprintf(errlog, "changed_text() - etext = \"%s\"\n", etext);
 
    if (strcmp(etext, "ABC") == 0)
    {
      const char *instext = "DEFGHIJKL";
 fprintf(errlog, "about to change text...\n");
 
      g_signal_handlers_block_by_func (editable,(gpointer) changed_text_handler, data);
      *p = 3;
      gtk_editable_insert_text (editable, instext, strlen(instext), p);
      g_signal_handlers_unblock_by_func (editable,(gpointer) changed_text_handler, data);
  
      etext = gtk_editable_get_chars(editable, 0, -1);
 fprintf(errlog,"\t from gtk_editable_get_chars(), etext = \"%s\"\n", etext);
      g_free(etext);
// here i try to see if i can change the position; it tells me that i have, but the cursor
// doesnt move
      gtk_editable_set_position(editable, 6);
    x = gtk_editable_get_position(editable);
     fprintf(errlog,"\t from gtk_editable_get_position(), x = \"%d\"\n", x);
     
//this doesnt do anything either. nothing gets highlighted...
         gtk_editable_select_region(editable, 3, 8);
  }  
  fflush(errlog);

}
    
int main(int argc, char **argv)
{
    GtkWidget *frm_main, *entry;
  
    if ((errlog  = fopen("errlog.txt", "w")) == NULL)
        exit (1);
         
    gtk_init(&argc, &argv);
  
    frm_main = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(frm_main),"ENTRY test");
    gtk_window_set_position(GTK_WINDOW(frm_main),GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(frm_main), 700, 700);
  
    gtk_signal_connect(GTK_OBJECT(frm_main), "destroy",
        GTK_SIGNAL_FUNC(dstry), NULL);
  
    entry = gtk_entry_new();
    g_signal_connect(G_OBJECT(entry), "changed",
         (gpointer) changed_text_handler, NULL);
   
    gtk_container_add(GTK_CONTAINER(frm_main), entry);
    
    gtk_widget_show_all(frm_main);
    
    gtk_main();
    
    return 0;
}

-------------------------
the output file "errlog.txt" is :

C_T_H() - text = A, data = "(null)"
C_T_H() - text = AB, data = "(null)"
C_T_H() - text = ABC, data = "(null)"
about to change text...
  from gtk_editable_get_chars(), etext = "ABCDEFGHIJKL"
  from gtk_editable_get_position(), x = "6"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]