wxDC drawing in modal dialog (works on Mac, not PC) Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
BenKissBox
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Jan 30, 2020 2:22 pm
Location: France / Netherlands

wxDC drawing in modal dialog (works on Mac, not PC)

Post by BenKissBox » Sat Jul 11, 2020 8:02 pm

Hello all,

I am facing a very strange problem with my wxWidgets application.

The application uses a modal dialog to display monitoring data, and a timer is used to refresh the view.

The code works perfectly on Mac OS, but nothing is draw on Windows.

In the constructor, I make the following call

Code: Select all

	wxClientDC (this);
	DrawBars(dc);
Here is an excerpt of the drawing code (simplified in order to test). The OnPaint method is called by the event handler, but nothing is displayed (however, I see the wxButton and wxStaticText, they are displayed as expected and the wxStaticText data is refreshed by the monitoring)

Code: Select all

void CDMXMonitorDlg::OnPaint (wxPaintEvent& event)
{
	wxPaintDC dc(this);
	DrawBars (dc);
}  // CDMXMonitorDlg::OnPaint
//---------------------------------------------------------------------------

void CDMXMonitorDlg::DrawBars (wxDC& dc)
{
	int Channel;

	for (Channel=0; Channel<16; Channel++)
	{
		// Draw bargraph border
       	 	dc.SetBrush (*wxTRANSPARENT_BRUSH);
        	dc.SetPen(wxPen(*wxBLACK, 1));
        	dc.DrawRectangle (19+(40*Channel), BARGRAPH_TOP, 30, BARGRAPH_BORDER_H);
	}
}  // CDMXMonitorDlg::DrawBars
//---------------------------------------------------------------------------
I also tried to make this->Refresh() and this->Update() in the method called by the timer but it does not change anything.

Is there a special way to handle DC graphics in a modal dialog ?

I am pretty sure I miss a stupid thing, but I can't get it for now

Benoit

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

Re: wxDC drawing in modal dialog (works on Mac, not PC)

Post by doublemax » Sat Jul 11, 2020 8:25 pm

In the constructor, I make the following call

Code: Select all

 wxClientDC (this);
  DrawBars(dc);
This is useless and should be removed.

How do you connect the paint event andler and does the dialog really have no children? (If it has you need to draw onto the panel or whatever is the "visible" element).
Use the source, Luke!

BenKissBox
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Jan 30, 2020 2:22 pm
Location: France / Netherlands

Re: wxDC drawing in modal dialog (works on Mac, not PC)

Post by BenKissBox » Sat Jul 11, 2020 9:24 pm

doublemax wrote:
Sat Jul 11, 2020 8:25 pm
How do you connect the paint event andler and does the dialog really have no children? (If it has you need to draw onto the panel or whatever is the "visible" element).
Hi Max,

the Paint event is connected like this

Code: Select all

BEGIN_EVENT_TABLE(CDMXMonitorDlg, wxDialog)
    EVT_BUTTON (ID_PREV_DMX_PAGE, CDMXMonitorDlg::OnPrevPage)
    EVT_BUTTON (ID_NEXT_DMX_PAGE, CDMXMonitorDlg::OnNextPage)
    EVT_PAINT (CDMXMonitorDlg::OnPaint)
END_EVENT_TABLE()
Here are the elements which are visible on the dialog

Code: Select all

	
	wxRadioButton* DMXOutputSelect;
	wxRadioButton* DMXInputSelect;
	wxStaticBox* GaugeBox;
	wxButton* CancelBtn;
        wxButton* NextPageBtn;
        wxButton* PrevPageBtn;
	wxStaticText* ChannelLabel[16];
	wxStaticText* ValueLabel[16];
The bargraphs are supposed to be displayed on a wxStaticBox (GaugeBox). Should I consider it as a child ? (I did not for now)
In that case, is there a way to get a DC from a wxStaticBox (since I did not find any way to get one, I supposed it does not act as a panel or something like that)

Benoit

BenKissBox
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Jan 30, 2020 2:22 pm
Location: France / Netherlands

Re: wxDC drawing in modal dialog (works on Mac, not PC)

Post by BenKissBox » Sat Jul 11, 2020 9:29 pm

Arrrrggg !!! :(

Just made a test right now : I removed the wxStaticBox, and my bargraphs appeared.

So apparently, the wxStatic is the child which "hides" the drawings. I will look again tomorrow in the wxStaticBox documentation, as I did not find how to "draw" on it (that's why I thought it should not considered as a child)

Benoit

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

Re: wxDC drawing in modal dialog (works on Mac, not PC)

Post by doublemax » Sat Jul 11, 2020 9:32 pm

You need to catch the paint event of the wxStaticBox.

Code: Select all

GaugeBox->Bind(wxEVT_PAINT, &CDMXMonitorDlg::OnPaint, this);
Then inside the paint event handler make sure to create a wxPaintDC for the wxStaticBox, not the dialog. For that, GaugeBox needs to be a member variable of CDMXMonitorDlg

Code: Select all

void CDMXMonitorDlg::OnPaint (wxPaintEvent& event)
{
  wxPaintDC dc(GaugeBox);
  DrawBars (dc);
} 
The coordinates you use in DrawBars() should then be relative to the wxStaticBox, not the dialog.
Use the source, Luke!

BenKissBox
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Jan 30, 2020 2:22 pm
Location: France / Netherlands

Re: wxDC drawing in modal dialog (works on Mac, not PC)

Post by BenKissBox » Sun Jul 12, 2020 6:11 am

Hi Doublemax,

thank you for your help (if one day you come in France, make me a sign, you *really* deserve a beer 8)

I realized just after closing the computer that everything was related to a Connect/Bind issue... :lol:

I dig a little in the source code, and apparently, there are minor differences between implementation of wxStaticBox on Windows and Mac OS platforms. On Mac, they do not act as a child which explains that the bargraphs are displayed correctly on this platform and not on Windows (until EVT_PAINT is processed correctly)
I will check that deeper when I have time, because I remember that these objects are children in XCode Interface Builder.

Benoit

Post Reply