从文本控件中检索字符位置的客户区域坐标 [英] Retrieving client area coordinates of a character position from text control

查看:174
本文介绍了从文本控件中检索字符位置的客户区域坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个函数,它可以从文本编辑控件中的字符位置获取屏幕坐标。我使用 wxWidgets 框架提供的文本编辑控件。 wxTextCtrl 不公开任何可以提供此信息的API函数。在Windows上,我可以通过发送 EM_POSFROMCHAR来获取信息到文本控件。

I am in need of writing a function which can get screen coordinates from the character position in a text edit control. I am using text edit control provided by wxWidgets framework. The wxTextCtrl is not exposing any API functions which can provide this information. On Windows, I can get the information by sending EM_POSFROMCHAR to the text control.

我想知道如何在其他环境如Linux上做同样的事情。现在,只需在运行于 X Window System 之上的桌面环境(如 GNOME KDE XFCE 。有办法吗?对这些DE,谁绘制控件,如文本编辑?它是由X管理,并由特定的DE风格?

I am wondering how the same can be done on other environments like Linux. For now this needs to be done on only the desktop environments which runs on top of X Window System like GNOME, KDE and XFCE. Is there a way to do it? And on these DE, who draws the controls like text edit? Is it managed by X and styled by particular DE?

任何帮助将不胜感激。

Any help would be appreciated.

推荐答案

好吧,这是可能的,但你需要实现 wxTextCtrl 派生控件,它依赖于任何特定wxWidgets端口(wxMSW,wxGTK等)的底层平台功能。

Well, it is possible, but you need to implement a wxTextCtrl derived control that relies on underlaying platform capabilities for any particular wxWidgets port (wxMSW, wxGTK, etc).

所以,因为你问过Linux的实现。让我解释一下如何实现它为wxGTK 2.9。下面是 wxTextCtrlExt 控制定义:

So, because you have asked about Linux implementation. Let me explain how to implement it for wxGTK 2.9. Below is the wxTextCtrlExt control definition:

#include <wx/textctrl.h>
#include <wx/gdicmn.h>

//-----------------------------------------------------------------------------
// wxTextCtrlExt
//-----------------------------------------------------------------------------

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; // output location in buffer coordinates
        gtk_text_view_get_iter_location(txtView, &iter, &location);

        return wxPoint(location.x, location.y);
    }

    return wxPoint(0,0);
}

您可能还需要将缓冲区坐标转换为窗口小部件: p>

You may also want to convert your buffer coordinates to a widget's ones:

    // 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);

正如你所看到的,这个特定的实现不考虑单行文本条目,这就是为什么你在创建 wxTextCtrlExt 控件时需要使用 wxTE_MULTILINE 样式。

As you can see, this particular implementation does not account for a single line text entry, that's why you need to use a wxTE_MULTILINE style when you are creating the wxTextCtrlExt control.

好吧,下面是如何使用它。让我们 m_Text2 wxTextCtrlExt 类的子对象的指针:

Well, below is how to use it. Let's m_Text2 be the pointer to a child object of wxTextCtrlExt class:

long iCharPos = m_Text2->GetInsertionPoint();
long iCharPosMax = m_Text2->GetLastPosition();
wxPoint pos = m_Text2->GetPositionCoords(iCharPos);

现在我们在 pos

您可能感兴趣的一些链接:

Some links that maybe of any interest to you:

  • GtkTextView — Widget that displays a GtkTextBuffer
  • GtkTextBuffer — Stores attributed text for display in a GtkTextView

这些DE,谁绘制的控件,如文本编辑?是由X管理还是由特定的DE风格?

And on these DE, who draws the controls like text edit? Is it managed by X and styled by particular DE?

这取决于您使用的wxWidgets端口。 wxMSW和wxGTK端口分别使用Win32和GTK + 2本地控件。基于wxUniversal的端口(如wxX11,wxMGL)通过wxWidgets本身绘制所有控件。 X Window系统不会自行强制要求用户界面。它提供了构建GUI环境的基本框架:在屏幕上绘制和移动窗口以及与鼠标和键盘交互。

This depends on the wxWidgets port you are using. The wxMSW and wxGTK ports use Win32 and GTK+2 native controls, respectively. The wxUniversal based ports (such as wxX11, wxMGL) draw all the controls by wxWidgets itself. X Window System does not mandate the user interface by itself. It provides the basic framework for building GUI environments: drawing and moving windows on the screen and interacting with a mouse and keyboard.

这篇关于从文本控件中检索字符位置的客户区域坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆