Question

Here's a minimal example to illustrate the problem. When the button is clicked, 500 TextView objects should get added, each containing some text. What actually happens is that there is a short delay, 500 empty TextViews get added, there is a much longer delay and then they all get populated with text at once and the layout sizes itself properly. Code below:

import gtk.Button;
import gtk.Main;
import gtk.MainWindow;
import gtk.Notebook;
import gtk.ScrolledWindow;
import gtk.Statusbar;
import gtk.TextView;
import gtk.TextBuffer;
import gtk.UIManager;
import gtk.VBox;
import gtk.Window;

import std.stdio;

class UI : MainWindow
{
  Notebook notebook;

  this() {
    super("Test");
    setDefaultSize(200, 100);
    VBox box = new VBox(false, 2);
    notebook = new Notebook();
    Button button = new Button("add lines");
    button.addOnClicked(&addLines);
    box.packStart(notebook, true, true, 0);
    box.packStart(button, false, false, 2);
    add(box);
    showAll();
  }

  void addLines(Button b) {
    VBox box = new VBox(false, 2);
    for (int i = 0; i < 500; i++) {
      auto tv = new TextView();
      tv.getBuffer().setText("line");
      box.packStart(tv, false, false, 1);
    }
    ScrolledWindow swin = new ScrolledWindow(box);
    notebook.add(swin);
    showAll();
  }
}

void main(string[] args)
{
  Main.init(args);
  auto ui = new UI();
  Main.run();
}

Edit: this thread suggests that creating a bunch of text views is intrinsically expensive, and that I should be rewriting using a treeview.

Was it helpful?

Solution 2

After some more googling and experimenting, it turns out that GtkTextViews are intrinsically expensive to instantiate, and I should not have been trying to create so many of them. As per the advice in this thread I will be reworking my code to use a GtkTreeView instead.

OTHER TIPS

GTK is event-driven, and uses a message pump. If in a callback you do a lengthy operation, you never give a chance to the message pump to process the pending messages. You could replace the code in your callback by a sleep of 2 seconds, the effect would be the same: the UI would be frozen during that time slice.

If you can't split up your actions, use the d equivalent of what is described in gtk_events_pending documentation:

/* computation going on */
...
  while (gtk_events_pending ())
      gtk_main_iteration ();
...
/* computation continued */

Called between each of your loop iterations, it will give some time to GTK to process the events you generated by adding your widgets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top