thiton
thiton

Reputation: 36049

How to change the background colour of a wxTextCtrl without breaking the inactive selection colour?

I want to signal an input error in a wxGTK application by setting the background of a text field to red on error and to white on successful input. However, when the background color is set via SetBackgroundColor, the background colour of an inactive selection is set to the same color. This leads to a very undesirable situation when setting the background colour to white: Since the foreground color for selected text is white, and the background colour for selected text is now also white, the text is unreadable.

How can I reset the colours on a wxTextCtrl so that inactive selected text has a grey background (the default setting before SetBackgroundColour)? SetBackgroundStyle( wxBG_STYLE_SYSTEM) was my first guess, but has no effect on wxGTK.

Code example:

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

class App : public wxApp {
    bool OnInit() {
        wxFrame* frame = new wxFrame(NULL, wxID_ANY, wxT("Frame"));
        wxTextCtrl* text = new wxTextCtrl( frame, wxID_ANY, wxT("foo bar") );
        text->SetBackgroundStyle( wxBG_STYLE_COLOUR );
        text->SetBackgroundColour( *wxWHITE );
        frame->Show();
        return true;
    }
};

IMPLEMENT_APP( App );

Upvotes: 3

Views: 5633

Answers (2)

Guest
Guest

Reputation: 1

Tried this and it works:

  TextCtrl1->SetBackgroundColour(wxColour(0xFF,0xA0,0xA0));
  TextCtrl1->SetStyle(0, -1, TextCtrl1->GetDefaultStyle());

Upvotes: 0

Roin
Roin

Reputation: 53

You could try working with SetDefaultStyle, I didn't try this myself but here is some excerpt from the wxwidgets documentation:

text->SetDefaultStyle(wxTextAttr(*wxRED));
text->AppendText("Red text\n");
text->SetDefaultStyle(wxTextAttr(wxNullColour, *wxLIGHT_GREY));
text->AppendText("Red on grey text\n");
text->SetDefaultStyle(wxTextAttr(*wxBLUE);
text->AppendText("Blue on grey text\n");

This will most likely allow you to change the colour independantly of the text and/or change the colour of the text itself as well. Here is the link to the wxTextCtrl Documentation where I found this code snippet: http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html

Regards, Roin

Upvotes: 2

Related Questions