Drawing RoundedRectangles

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
chrism238
Earned a small fee
Earned a small fee
Posts: 20
Joined: Sun Feb 17, 2013 6:45 pm

Drawing RoundedRectangles

Post by chrism238 »

Hello, I wish to draw a rounded rectangle that is filled with a colour with an alpha value (its brush).

While this works as hoped on OS-X, drawing directly using the wxDC, I understand that for this to also work on Linux that I need to use a wxGraphicsContext when drawing.

However, when testing on either OS-X or Linux, I find that RoundedRectangles do not have their rounded corners when using a wxGraphicsContext.

Here is my coded attempt, and a screenshot of what I'm seeing.
Any suggestions much appreciated!

Code: Select all

    void DrawMap(wxWindowDC &dc)
    {
#define MY_ALPHA                100
        wxColour myColour(60, 60, 60, MY_ALPHA);
        wxDouble x = 20.0;

        dc.Clear();
        dc.SetTextForeground(*wxBLUE);
        dc.DrawText( "just a very long text string under rectangles",  5, 100);

#define ROUND_RECT_DETAILS      (x, 20.0, 60.0, 200.0, -0.25)

        wxGraphicsContext *gc = wxGraphicsContext::Create( dc );
        if(gc) {
            gc->SetPen(myColour);
            gc->SetBrush(myColour);

            gc->DrawRoundedRectangle ROUND_RECT_DETAILS;
            x += 80.0;

            wxGraphicsPath path = gc->CreatePath();
            path.AddRoundedRectangle ROUND_RECT_DETAILS;
            x += 80.0;
            gc->FillPath(path);
            gc->StrokePath(path);

            delete gc;
        }

        dc.SetPen(myColour);
        dc.SetBrush(myColour);
        dc.DrawRoundedRectangle ROUND_RECT_DETAILS;

//  JUST LABEL THE 3 ROUNDED RECTANGLES
        dc.SetTextForeground(*wxBLACK);
        x       = 25.0;
        dc.DrawText( "gc->DRR",  x, 230);
        x += 80.0;
        dc.DrawText( "path.ARR", x, 230);
        x += 80.0;
        dc.DrawText( "dc.DRR",   x, 230);
    }
Attachments
try.png
try.png (12.28 KiB) Viewed 2705 times
mycode.cpp
(988 Bytes) Downloaded 147 times
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Drawing RoundedRectangles

Post by doublemax »

Code: Select all

#define ROUND_RECT_DETAILS      (x, 20.0, 60.0, 200.0, -0.25)
What's the negative value for "radius" supposed to mean? Use a value like 10 or 20 any you get the expected result.
Use the source, Luke!
chrism238
Earned a small fee
Earned a small fee
Posts: 20
Joined: Sun Feb 17, 2013 6:45 pm

Re: Drawing RoundedRectangles

Post by chrism238 »

From http://docs.wxwidgets.org/3.1.0/classwx_d_c.html :

If radius is positive, the value is assumed to be the radius of the rounded corner. If radius is negative, the absolute value is assumed to be the proportion of the smallest dimension of the rectangle. This means that the corner can be a sensible size relative to the size of the rectangle, and also avoids the strange effects X produces when the corners are too big for the rectangle.

which works, as expected, when using dc.DrawRoundedRectangle()
I'd assumed that a negative radius would similarly work when using a wxGraphicsContext - but the details of the radius parameter are not documented.

While using a positive radius does work for a gc - THANKS - I've wasted too much time assuming the negative value would also work. I wonder why they don't.
Post Reply