Re: [gtkmm] No refresh until mouse moves?



On Friday 25 April 2003 8:41 pm, Eugene Koontz wrote:
> Hi,
>     I've been using gtkmm for a week and I love it so far. I've been
> able to basically create a GUI app in about a week even though I don't
> have much background in GUI programming. However, as a newbie, I've hit
> a problem that probably has a simple solution that I've overlooked
> (though I have search the docs and google). Here is the problem :
>
>     After I insert some text into a TextBuffer, I would like the text to
> immediately appear in the TextView associated with the TextBuffer.
> However, it seems that I don't see the text until I mouse over the
> window or, if the window is already focused, until I move the mouse over
> a button or click inside the TextView.
>    My question is: how do I force the TextView to refresh?
>
> Here's some of the code :
>
> MyApp::MyApp() {
>
>    Gtk::TextView dialog; // the TextView that shows the text.
>    dialog(Gtk::TextBuffer::create()); // associate a TextBuffer with the
> TextView.
> dialog.get_buffer()->signal_insert().connect(slot(*this,&MyApp::refresh_tex
>t_view));
>
>    // now, start a thread to send text to the TextView's TextBuffer.
>    pthread_t hello_thr;
>    pthread_create(&hello_thr, NULL, EndlessLoopWrapper,(void*)this);
>
>    show_all();
> }

[snip]

You are writing to the TextBuffer in the thread you have created, and then 
refreshing in the main program thread via a signal.

You can't do this. Gtk+/Gtkmm is not thread safe.  In Gtk+ you have to use the 
gdk_threads_enter() and gtk_threads_leave() global locks before doing 
anything in Gdk/Gtk+ through multiple threads (and flush with gtk_flush() 
before calling gdk_threads_leave()).  I imagine (I do not know) you can do 
the same with Gtkmm, although it is better only to tamper with Gtk+ in a 
single GUI thread (usually the main program thread), and to communicate with 
that thread through pipes or some other mechanism.  Look at the documentation 
on Glib::Dispatcher to see how this can be done.

Of course, in your simple demonstration example you can do automatic periodic 
writes to the TextView buffer through a timer, and avoid threads entirely.  
(But presumably your program was just a case demonstration).

Chris.




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