Set the color of .xbm stipple brush

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
bluesnowball18
Earned a small fee
Earned a small fee
Posts: 11
Joined: Thu Apr 22, 2021 9:23 am

Set the color of .xbm stipple brush

Post by bluesnowball18 » Sun May 09, 2021 5:48 am

I wrote a small program which displays checker pattern using .xbm stipple image:

Code: Select all

wxBrush brush(stipple);

wxAutoBufferedPaintDC dc(this);
dc.SetBrush(brush);
dc.SetPen(*wxTRANSPARENT_PEN);
dc.Clear();
dc.DrawRectangle(8, 8, 64, 64);
Here is what it shows on MSW and GTK+3:

Image
Image

How can I change both pattern background and foreground?

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

Re: Set the color of .xbm stipple brush

Post by doublemax » Sun May 09, 2021 9:12 am

Usually the brush itself defines the colors. Try experimenting with wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE:
https://docs.wxwidgets.org/trunk/brush_ ... 12dfd367ef
Use the source, Luke!

bluesnowball18
Earned a small fee
Earned a small fee
Posts: 11
Joined: Thu Apr 22, 2021 9:23 am

Re: Set the color of .xbm stipple brush

Post by bluesnowball18 » Sun May 09, 2021 11:02 pm

Thank you for the answer.

As you suggest, I've looked at wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE and made this code snippet:

Code: Select all

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
        #include "wx/app.h"
        #include "wx/bitmap.h"
        #include "wx/frame.h"
        #include "wx/dcbuffer.h"
#endif

#include "checker.xbm"

class Frame : public wxFrame
{
public:
        Frame();

        void InitBrush();

private:
        void OnPaint(wxPaintEvent &event);

        wxBrush brush;

        wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(Frame, wxFrame)
        EVT_PAINT(Frame::OnPaint)
wxEND_EVENT_TABLE()

Frame::Frame()
        : wxFrame(nullptr, wxID_ANY, "Pattern")
{
        InitBrush();

        SetBackgroundStyle(wxBG_STYLE_PAINT);
}

void Frame::InitBrush()
{
        wxBitmap stipple((char*)checker_bits, checker_width, checker_height);
        stipple.SetMask(new wxMask(stipple));

        this->brush.SetStipple(stipple);
        this->brush.SetStyle(wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE);
}

void Frame::OnPaint(wxPaintEvent &event)
{
        wxAutoBufferedPaintDC dc(this);

        dc.SetBackground(*wxWHITE_BRUSH);
        dc.Clear();

        dc.SetBrush(this->brush);
        dc.SetPen(*wxTRANSPARENT_PEN);
        dc.SetTextBackground(wxColour(0xE6, 0x6A, 0x5C));
        dc.SetTextForeground(wxColour(0xF1, 0xAE, 0xA6));
        dc.DrawRectangle(8, 8, 64, 64);
}

class App : public wxApp
{
        bool OnInit() override;

        Frame *frame = nullptr;
};

wxIMPLEMENT_APP(App);

bool App::OnInit()
{
        this->frame = new Frame();
        this->frame->Show();

        return true;
}
Under MSW, it works and displays two-color pattern:

Image

Under GTK+3 it just shows blank image:

Image

I've tried several solutions but nothing seems to work. So I just switched to .xpm because it works on every platform and the format itself is much easier to change.

Code: Select all

wxBitmap stipple(checker_bits);

this->brush.SetStipple(stipple);

Code: Select all

dc.SetBrush(this->brush);
dc.SetPen(*wxTRANSPARENT_PEN);

dc.DrawRectangle(8, 8, 64, 64);
Image

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

Re: Set the color of .xbm stipple brush

Post by ONEEYEMAN » Mon May 10, 2021 3:46 pm

Hi,
What version of GTK+ do you use?
Does it work in the "drawing" sample?

Thank you.

Post Reply