How do I show a text cursor on wxTextCtrl with inverted colors? Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 2:58 pm

wxTextCtrl's allow changing the text color and the background color with SetForegroundColour and SetBackgroundColour.
However, (at least on wxGTK 3.0.4) the text cursor stays the same color.
So, if I have code like:

Code: Select all

   m_text->SetForegroundColour(*wxWHITE);
   m_text->SetBackgroundColour(*wxBLACK);
what else do I need to call to get the text cursor to show up? (Basically, I need a white text cursor instead of a black text cursor.) This problem can be seen when running the samples/text sample in the wxWidgets 3.0.4 source code.

If this is a bug, I think possibly wxGTK needs to call gtk_widget_override_cursor when the foreground colour is set on a text control.

Full application that shows this problem:

Code: Select all

#include <wx/app.h>
#include <wx/frame.h>
#include <wx/textctrl.h>

class MyApp: public wxApp
{
public:
  bool OnInit();
};

bool MyApp::OnInit()
{
  if ( !wxApp::OnInit() )
    return false;

  wxFrame *frame = new wxFrame(NULL, wxID_ANY, wxS("Test"), wxPoint(50, 50));

  wxTextCtrl * m_text = new wxTextCtrl(frame, wxID_ANY);
  m_text->SetForegroundColour(*wxWHITE);
  m_text->SetBackgroundColour(*wxBLACK);
  frame->Show(true);
  return true;
}

IMPLEMENT_APP(MyApp)

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by doublemax » Fri Apr 10, 2020 3:56 pm

Which Linux distro / window manager?

I tested under Q4OS (Debian based), wxWidgets 3.1.3, and the caret always has the same color as the text (= it works correctly)
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4264
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by ONEEYEMAN » Fri Apr 10, 2020 4:00 pm

Hi,
I don't think under Windows you can change the color of the cursor in the (native) wxTextCtrl.

And under GTK/OSX it mijt be theme-specific.

Maybe you can try to use wxRicTextCtrl?

Thank you.

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 5:28 pm

doublemax wrote:
Fri Apr 10, 2020 3:56 pm
Which Linux distro / window manager?
Fedora 31, xfwm4, wxWidgets 3.0.4

I will try with wxWidgets 3.1.3 and see if it is fixed in that version.

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 5:59 pm

jrincayc wrote:
Fri Apr 10, 2020 5:28 pm
[I will try with wxWidgets 3.1.3 and see if it is fixed in that version.
wxWidgets 3.1.3

Code: Select all

mkdir build_gtk3
cd build_gtk3
../configure --with-gtk=3
make -j3

g++ `~/software/wxWidgets-3.1.3/build_gtk3/wx-config --cxxflags` -c text_test.cpp
g++ -o text  `~/software/wxWidgets-3.1.3/build_gtk3/wx-config --libs` text_test.o
This is with the wxTextCtrl code I posted above.
And still I do not get a visible cursor.

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 6:01 pm

ONEEYEMAN wrote:
Fri Apr 10, 2020 4:00 pm
Hi,
I don't think under Windows you can change the color of the cursor in the (native) wxTextCtrl.

And under GTK/OSX it mijt be theme-specific.

Maybe you can try to use wxRicTextCtrl?

Thank you.
I don't get a visible cursor with the following wxRichTextCtrl code:

Code: Select all

#include <wx/app.h>
#include <wx/frame.h>
#include <wx/richtext/richtextctrl.h>

class MyApp: public wxApp
{
public:
  bool OnInit();
};

bool MyApp::OnInit()
{
  if ( !wxApp::OnInit() )
    return false;

  wxFrame *frame = new wxFrame(NULL, wxID_ANY, wxS("Test"), wxPoint(50, 50));

  wxRichTextCtrl * text = new wxRichTextCtrl(frame, wxID_ANY);
  text->SetForegroundColour(*wxWHITE);
  text->SetBackgroundColour(*wxBLACK);
  wxRichTextAttr backgroundColourAttr;
  backgroundColourAttr.SetBackgroundColour(*wxBLACK);
  backgroundColourAttr.SetTextColour(*wxWHITE);
  text->BeginStyle(backgroundColourAttr);
  text->WriteText(wxT("White on black"));
  text->EndStyle();
  frame->Show(true);
  return true;
}

IMPLEMENT_APP(MyApp)


New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by New Pagodi » Fri Apr 10, 2020 6:15 pm

wxStyledTextCtrl supports setting caret colors. It's a little more complicated than wxTextCtrl though.

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 6:25 pm

Hm, this seems to be determined by the gtk theme. It does work (both 3.1.3 and 3.0.4) with the Arc theme, but not with the Xfce theme. So I think this is a bug, but with the gtk theme I had been using, not with gtk or with wxWidgets. If I can figure out what in the theme is causing this, I'll report back here, and also file a bug with xfce.

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by doublemax » Fri Apr 10, 2020 6:26 pm

My wx version was built with GTK2. Can you try again with GTK2 and if it works there, open a bug report at http://trac.wxwidgets.org ? Thanks.
Use the source, Luke!

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 7:22 pm

Hm, MULTILINE seems to be broken for me, even tho' single line does work. This is broken for all the combinations I have tried:

Code: Select all


#include <wx/app.h>
#include <wx/frame.h>
#include <wx/textctrl.h>

class MyApp: public wxApp
{
public:
  bool OnInit();
};

bool MyApp::OnInit()
{
  if ( !wxApp::OnInit() )
    return false;

  wxFrame *frame = new wxFrame(NULL, wxID_ANY, wxS("Test"), wxPoint(50, 50));

  wxTextCtrl * m_text = new wxTextCtrl(frame, wxID_ANY, wxEmptyString,
				       wxDefaultPosition, wxDefaultSize,
				       wxTE_MULTILINE | wxTE_RICH);
  m_text->AppendText("This is a test\n");
  m_text->SetForegroundColour(*wxWHITE);
  m_text->SetBackgroundColour(*wxBLACK);
  //m_text->SetDefaultStyle(wxTextAttr(*wxWHITE,*wxBLACK,*wxITALIC_FONT));
  //m_text->SetStyle(0, m_text->GetLastPosition(),
  //		   wxTextAttr(*wxWHITE,*wxBLACK));
  m_text->AppendText("This is another test");
  frame->Show(true);
  return true;
}

IMPLEMENT_APP(MyApp)


User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by doublemax » Fri Apr 10, 2020 7:54 pm

MULTILINE seems to be broken for me, even tho' single line does work
Broken in what way? You might have to set an explicit height to see multiple lines.
Use the source, Luke!

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Fri Apr 10, 2020 8:32 pm

doublemax wrote:
Fri Apr 10, 2020 7:54 pm
MULTILINE seems to be broken for me, even tho' single line does work
Broken in what way? You might have to set an explicit height to see multiple lines.
As soon as I add wxTE_MULTILINE the cursor no longer changes to the foreground color. So with multiline I can't see the text cursor with wxTextCtrl. I put the multiline example in my previous post.

(I tried setting an explicit size, but that didn't solve the problem.)

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by doublemax » Sat Apr 11, 2020 1:16 am

I rebuild wx 3.1.3 with GTK3 and see the same issue as you. Please open a bug report at http://trac.wxwidgets.org
Use the source, Luke!

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Sat Apr 11, 2020 2:54 am

doublemax wrote:
Sat Apr 11, 2020 1:16 am
I rebuild wx 3.1.3 with GTK3 and see the same issue as you. Please open a bug report at http://trac.wxwidgets.org
Ticket added: https://trac.wxwidgets.org/ticket/18722

jrincayc
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Sep 02, 2019 12:20 am

Re: How do I show a text cursor on wxTextCtrl with inverted colors?

Post by jrincayc » Sun Apr 19, 2020 8:12 pm

jrincayc wrote:
Sat Apr 11, 2020 2:54 am
doublemax wrote:
Sat Apr 11, 2020 1:16 am
I rebuild wx 3.1.3 with GTK3 and see the same issue as you. Please open a bug report at http://trac.wxwidgets.org
Ticket added: https://trac.wxwidgets.org/ticket/18722
A patch is now in git, and it now works correctly with GTK.

Post Reply