Page 1 of 1

Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 3:42 pm
by rsb
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 1022 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 1022 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 ;
----------------------------------

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 4:34 pm
by doublemax
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.

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 4:49 pm
by rsb
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 );

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 4:50 pm
by doublemax
Is there a dc->SetBrush(brush) following? Otherwise this won't do anything.

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 4:53 pm
by rsb
Yes, there is.

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

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 5:01 pm
by doublemax
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.

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 5:23 pm
by rsb
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.

Re: Foreground color ignored using wxGraphicsContext

Posted: Thu Jun 13, 2019 5:45 pm
by doublemax
FWIW, i think nobody should use wxClientDC for drawing. Ever.

What about a "normal" wxPaintDC?