这是可能的,但你需要实现一个wxTextCtrl
派生控件,依赖于底层平台能力来支持任何特定的wxWidgets端口(如wxMSW,wxGTK等)。
因此,因为您询问了Linux实现。让我解释一下如何为wxGTK 2.9实现它。以下是wxTextCtrlExt
控件定义:
#include <wx/textctrl.h>
#include <wx/gdicmn.h>
class wxTextCtrlExt : public wxTextCtrl
{
public:
wxTextCtrlExt() : wxTextCtrl() { }
wxTextCtrlExt(wxWindow* parent,
wxWindowID id,
const wxString& value = wxEmptyString,
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
long style = wxTE_MULTILINE,
const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTextCtrlNameStr)
: wxTextCtrl(parent, id, value, pos, size, style, validator, name ) { }
wxPoint GetPositionCoords(long pos);
};
还有wxGTK实现:
#include "wxTextCtrlExt.h"
#include <wx/defs.h>
#include <gtk/gtk.h>
wxPoint wxTextCtrlExt::GetPositionCoords(long pos)
{
if ( IsMultiLine() ) {
GtkTextView* txtView = GTK_TEXT_VIEW(m_focusWidget);
GtkTextBuffer* txtBuffer = gtk_text_view_get_buffer(txtView);
GtkTextIter iter;
gtk_text_buffer_get_iter_at_offset(txtBuffer, &iter, pos);
GdkRectangle location;
gtk_text_view_get_iter_location(txtView, &iter, &location);
return wxPoint(location.x, location.y);
}
return wxPoint(0,0);
}
您可能还希望将缓冲区坐标转换为小部件的坐标:
// If we need to convert our coordinates to
GdkPoint out;
gtk_text_view_buffer_to_window_coords(txtView, GTK_TEXT_WINDOW_WIDGET, location.x, location.y, &out.x, &out.y );
return wxPoint(out.x, out.y);
正如您所看到的,这个实现并没有考虑单行文本输入,因此在创建控件时需要使用样式。
好的,下面是如何使用它。假设是类的一个子对象指针:
long iCharPos = m_Text2->GetInsertionPoint();
long iCharPosMax = m_Text2->GetLastPosition();
wxPoint pos = m_Text2->GetPositionCoords(iCharPos);
现在我们已经获得了角色位置坐标在
pos
中。就是这样!
以下是一些可能对您感兴趣的链接:
-
GtkTextView — 显示GtkTextBuffer的小部件
-
GtkTextBuffer — 存储带属性的文本以在GtkTextView中显示
现在关键是你的第二个问题。
引用:
“在这些DE上,谁绘制控件(如文本编辑)?它是由X管理并由特定的DE进行样式设置吗?”
这取决于您使用的wxWidgets端口。 wxMSW和wxGTK端口分别使用Win32和GTK+2本地控件。基于wxUniversal的端口(例如wxX11,wxMGL)通过wxWidgets自身绘制所有控件。
X Window System本身不规定用户界面。它提供了构建GUI环境的基本框架:在屏幕上绘制和移动窗口以及与鼠标和键盘交互。