Get X/Y Position of Caret (Input Text Cursor) Under Xorg

Retrieving client area coordinates of a character position from text control

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).

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

And the wxGTK implementation:

#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);
}

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

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.

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

Now we got our character position coordinates in pos. That's it!

Some links that maybe of any interest to you:

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

Now it is all about your second question.

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

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.

Get Current Keyboard Cursor Location

The closest you can get would be to use OS X's Accessibility Protocol. This is intended to help disabled users operate the computer, but many applications don't support it, or do not support it very well.

The procedure would be something like:

appRef = AXUIElementCreateApplication(appPID);
focusElemRef = AXUIElementCopyAttributeValue(appRef,kAXFocusedUIElementAttribute, &theValue)
AXUIElementCopyAttributeValue(focusElemRef, kAXSelectedTextRangeAttribute, &selRangeValue);
AXUIElementCopyParameterizedAttributeValue(focusElemRef, kAXBoundsForRangeParameterizedAttribute, adjSelRangeValue, &boundsValue);

Due to the spotty support for the protocol, with many applications you won't get beyond the FocusedUIElementAttribute step, but this does work with some applications.

Get The Display Name of Xorg The Correct Way?

You probably need DisplayString(dpy) - see xdpyinfo source.

Is it possible to change the position of a Gtk.MessageDialog's caption text?

I'm not familiar with the PyGTK API calls, but in C you can use

gtk_message_dialog_get_message_area ()

to get the container (box) of the messages (labels). I imagine you can then apply padding and/or margin to the container to obtain what you want to achieve.



Related Topics



Leave a reply



Submit