Cannot add new tabs in GtkNotebook
EdoardoLaGreca opened this issue · 3 comments
I tried in many ways to add a new tab in a GtkNotebook widget through code, without success. The closure (defined as an argument of btn_new_file.connect_clicked()
) gets called but nothing happens.
This is my code, by pressing btn_new_file
, GtkNotebook should (but doesn't) append a new tab.
Maybe I'm missing something...
//let btn_new_file: gtk::Button = ...
// My notebook, created using Glade
let ntb_tabs: Arc<Mutex<gtk::Notebook>> = Arc::new(
Mutex::new(
builder.get_object("ntb_tabs").unwrap()
)
);
let ntb_tabs_clone = ntb_tabs.clone();
btn_new_file.connect_clicked(move |_| {
let txtv_editor = gtk::TextView::new();
let ntb_tabs = ntb_tabs_clone.lock().unwrap();
// Code that SHOULD add the tab, once invoked
ntb_tabs.append_page(
&txtv_editor.upcast::<Widget>(),
Some(>k::Label::new(Some("*untitled")))
);
// I also tried this. Still, nothing happens.
//ntb_tabs.insert_page(
// &txtv_editor.upcast::<Widget>(),
// Some(>k::Label::new(Some("*untitled"))),
// Some(ntb_tabs.get_n_pages() - 1)
//);
// I added this in case redrawing the widget would solve the problem (spoiler: it doesn't)
ntb_tabs.queue_draw();
});
Can you provide a complete, runnable testcase to reproduce the problem?
Also this looks more like a GTK question than something related to the bindings, for which https://discourse.gnome.org would be a better place to ask.
I tried to make a runnable testcase to reproduce the problem but this time it shows errors (instead of just nothing, like it did before).
**
Gtk:ERROR:../gtk/gtk/gtknotebook.c:1400:gtk_notebook_buildable_add_child: assertion failed: (page != NULL)
Bail out! Gtk:ERROR:../gtk/gtk/gtknotebook.c:1400:gtk_notebook_buildable_add_child: assertion failed: (page != NULL)
zsh: abort (core dumped) cargo run
Here attached there is the full thing
runnable-testcase-notebook.zip
One thing first, in src/gui/events.rs
you don't really need the Arc
/ Mutex
. a) this is all single-threaded so Rc
and RefCell
would also do, and b) you can just clone the notebook itself (all widgets use reference counting internally).
The error above comes from Builder
when parsing your .glade
file. I think a better place for discussing this would be https://discourse.gnome.org .