Foreground color ignored using wxGraphicsContext

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
rsb
Earned some good credits
Earned some good credits
Posts: 135
Joined: Fri May 29, 2015 7:26 pm

Foreground color ignored using wxGraphicsContext

Post by rsb » Thu Jun 13, 2019 3:42 pm

Hello,

We're trying to improve our drawing code using wxGraphicsContext.

The problem is that when drawing filled objects using stippled patterns, the foreground color is ignored
and comes out white. Using a wxClientDC object, the color is correct.

Any idea what would cause this?

Thanks very much.

RSB

OS: Windows 7 Pro
wxWidgets: wxWidgets 3.1.0
Compiler: Visual Studio 15

Here's code snippets and pictures of the two methods used to draw circles:

wxClientDC:
DeviceContext3.PNG
DeviceContext3.PNG (9.57 KiB) Viewed 96 times
----------------------------------
CREATE_DC;

graphics__set_foreground_color(window_attribute_p,dc,window_attribute_p->active_color_index);
graphics__set_line_width(window_attribute_p,dc,1) ;

graphics__set_clip_region(window_attribute_p,iti_dc,window_attribute_p->clip_region);

graphics__set_alpha(window_attribute_p, dc, alpha) ;

dc->DrawCircle(center_position.x_coord,center_position.y_coord,radius);
----------------------------------

wxGraphicsContext:
GraphicsContext3.PNG
GraphicsContext3.PNG (13.41 KiB) Viewed 96 times
----------------------------------
CREATE_DC;

graphics__set_foreground_color(window_attribute_p,dc,window_attribute_p->active_color_index);
graphics__set_line_width(window_attribute_p,dc,1) ;

graphics__set_clip_region(window_attribute_p,iti_dc,window_attribute_p->clip_region);

graphics__set_alpha(window_attribute_p, dc, alpha) ;

wxBitmap bitmap(dc->GetSize()) ;

wxMemoryDC memDC ;
memDC.SelectObject(bitmap) ;

memDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(), dc, 0, 0) ;

wxGraphicsContext *pGraphicsContext = wxGraphicsContext::Create(memDC) ;

pGraphicsContext->SetPen(dc->GetPen()) ;
pGraphicsContext->SetBrush(dc->GetBrush()) ;

wxGraphicsPath graphicsPath = pGraphicsContext->CreatePath() ;

graphicsPath.AddCircle((double) center_position.x_coord, (double) center_position.y_coord, (double) radius) ;

wxPolygonFillMode mode = wxODDEVEN_RULE ;

pGraphicsContext->DrawPath(graphicsPath, mode) ;

dc->Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(), &memDC, 0, 0) ;

memDC.SelectObject(wxNullBitmap) ;

delete pGraphicsContext ;
----------------------------------

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

Re: Foreground color ignored using wxGraphicsContext

Post by doublemax » Thu Jun 13, 2019 4:34 pm

Which method do you use to set the foreground color? I only see wxDC::SetTextForeground which is only relevant for text.

For filled polygons the brush is what determines the color.
Use the source, Luke!

rsb
Earned some good credits
Earned some good credits
Posts: 135
Joined: Fri May 29, 2015 7:26 pm

Re: Foreground color ignored using wxGraphicsContext

Post by rsb » Thu Jun 13, 2019 4:49 pm

Inside this routine,
graphics__set_foreground_color(window_attribute_p,dc,window_attribute_p->active_color_index);

The brush is set as follows:

wxBrush brush = dc->GetBrush();

brush.SetColour( foregroundColour ); // Green
brush.SetStyle( wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE );
brush.SetStipple( *pWX_bitmap );

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

Re: Foreground color ignored using wxGraphicsContext

Post by doublemax » Thu Jun 13, 2019 4:50 pm

Is there a dc->SetBrush(brush) following? Otherwise this won't do anything.
Use the source, Luke!

rsb
Earned some good credits
Earned some good credits
Posts: 135
Joined: Fri May 29, 2015 7:26 pm

Re: Foreground color ignored using wxGraphicsContext

Post by rsb » Thu Jun 13, 2019 4:53 pm

Yes, there is.

dc->SetPen(pen);
dc->SetBrush(brush);

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

Re: Foreground color ignored using wxGraphicsContext

Post by doublemax » Thu Jun 13, 2019 5:01 pm

What happens if you comment out the two stipple-related calls? Does it draw with the correct color? I think the bitmap does override the color if it doesn't have a mask. In that case just the bitmap is used.

If that's not it, try to create a minimal, compilable sample that shows the issue.
Use the source, Luke!

rsb
Earned some good credits
Earned some good credits
Posts: 135
Joined: Fri May 29, 2015 7:26 pm

Re: Foreground color ignored using wxGraphicsContext

Post by rsb » Thu Jun 13, 2019 5:23 pm

Yes, if I change the brush style to filled and skip the call to SetStipple, it does draw with the correct color.

If the bitmap overrides the color, why does it work using the wxClientDC.

I'll try to come up with a small program.

Thanks very much.

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

Re: Foreground color ignored using wxGraphicsContext

Post by doublemax » Thu Jun 13, 2019 5:45 pm

FWIW, i think nobody should use wxClientDC for drawing. Ever.

What about a "normal" wxPaintDC?
Use the source, Luke!

Post Reply