私は gtkmm を学んでおり、gnome.org で textview の例を見つけました:
https://developer.gnome.org/gtkmm-tutorial/2.22/sec-textview-examples.html.en
fill_buffers() コードを変更して、テキストビューにボタンを追加します。
void ExampleWindow::fill_buffers()
{
m_refTextBuffer1 = Gtk::TextBuffer::create();
m_refTextBuffer1->set_text("This is the text from TextBuffer #1.");
//learn
Gtk::TextIter iter = m_refTextBuffer1->get_iter_at_offset(5);
refAnchor = m_refTextBuffer1->create_child_anchor(iter);
m_Button_Text.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_quit) );
m_TextView.add_child_at_anchor(m_Button_Text, refAnchor);
//m_Button_Text.show();
m_refTextBuffer2 = Gtk::TextBuffer::create();
m_refTextBuffer2->set_text(
"This is some alternative text, from TextBuffer #2.");
}
コンストラクターは次のとおりです。
ExampleWindow::ExampleWindow()
: m_Button_Quit(Gtk::Stock::QUIT),
m_Button_Buffer1("Use buffer 1"),
m_Button_Buffer2("Use buffer 2"),
m_Button_Text("Text Button")
{
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
fill_buffers();
m_ButtonBox.pack_start(m_Button_Buffer1, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Buffer2, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
m_ButtonBox.set_border_width(5);
m_ButtonBox.set_spacing(5);
m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
//Connect signals:
m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_quit) );
m_Button_Buffer1.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_buffer1) );
m_Button_Buffer2.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_buffer2) );
//Add the TreeView, inside a ScrolledWindow, with the button underneath:
m_ScrolledWindow.add(m_TextView);
//Only show the scrollbars when they are necessary:
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
m_VBox.pack_start(m_ScrolledWindow);
//Add buttons:
m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
add(m_VBox);
on_button_buffer1();
show_all_children();
}
テキストビューに固定されたボタンを取得できるようにします。しかし、コードは機能しません。ボタンは四角形の内側に十字の形で表示され、クリックしても反応しませんでした。
次のサイトも見つけました。
http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-txtw-icw
http://www.matrix44.net/blog/?p=1033
私のコードと上記のWebサイトのコードを比較します。コードが機能しない理由がまだわかりません...
よろしくお願いします。私はこの問題に1日悩まされました...
++++++++++++++++++++
他のコードが役に立つかもしれません:
void ExampleWindow::on_button_quit()
{
hide();
}
void ExampleWindow::on_button_buffer1()
{
m_TextView.set_buffer(m_refTextBuffer1);
}
void ExampleWindow::on_button_buffer2()
{
m_TextView.set_buffer(m_refTextBuffer2);
}