带有常量字符串的Gtk :: TextView [英] Gtk::TextView with constant string
问题描述
我正在使用Gtkmm 3+,并且我试图做的是使文本缓冲区具有常量字符串>",即使用户尝试将其删除.另外,当用户按下回车键时,它将自动再次出现.基本上像终端一样具有常量字符串.
I am using Gtkmm 3+ and What I am trying to do is have the text buffer have the constant string "> " even if the user tries to delete it. In addition when the user pressed return it will automatically be there again. Basically have a constant string like a terminal does.
我可以考虑完成此操作的唯一方法是连接到delete和Backspace信号,以便用户无法删除字符串.但是,还有更好的方法吗?
The only way I can think about about accomplishing this would be to connect to the delete and backspace signals so the user cannot delete the string. But, is there a better way?
到目前为止,这是我唯一想到的方法:
so far this is the only way I can think of:
//in constructor
txt_view_i_.signal_event().connect(sigc::mem_fun(*this, &MainWindow::inputEvent));
//function
bool MainWindow::inputEvent(GdkEvent* event)
{
if((event->key.keyval == GDK_KEY_BackSpace || event->key.keyval == GDK_KEY_Delete) && buffer_input_->get_char_count() < 3)
return true;
return false;
}
但是效果并不理想,因为如果您输入的字符数超过3个,然后转到该行的开头,则可以删除常量字符串.
But doesn't work perfectly, because if you type in more then 3 characters then go to the beginning of the line you can delete the constant string.
我刚才想到的另一种方法是在TextView小部件中添加标签.我这样做了,但是用户仍然可以删除它.这是该代码:
Another way I just thought about was to add a label to the TextView widget. I did that but, the user could still delete it. Here is the code for that:
Gtk::TextBuffer::iterator it = buffer_input_->get_iter_at_line(1);
Glib::RefPtr<Gtk::TextChildAnchor> refAnchor = buffer_input_->create_child_anchor(it);
Gtk::Label* lbl = Gtk::manage(new Gtk::Label("> "));
txt_view_i_.add_child_at_anchor(*lbl, refAnchor);
推荐答案
这与我回答的问题非常相似,但并不完全相同提示.
This is very similar, but not quite identical, to the question I answered here: You can create a GtkTextTag
that makes its contents uneditable, and apply it from the beginning of the buffer up to and including the "> "
prompt.
然后,当您收到输入时,将您的输出添加到缓冲区,然后在下一行添加新的提示,然后重新应用标签以使整个内容不可编辑.
Then when you receive input, append your output to the buffer and then append a new prompt on the next line, and re-apply the tag to make the whole thing uneditable.
链接的答案中的链接显示了完成此操作的一些C代码,甚至包括提示.它不是Gtkmm或C ++,但应作为示例.
The links in the linked answer show some C code where this is done, even including a prompt. It's not Gtkmm or C++, but it should serve as an illustration.
这篇关于带有常量字符串的Gtk :: TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!