Background image flickering of wxWidgets Panel - c++

What am trying to accomplish is having two Panels , one to draw on , and the other one will hold the tools , so I'm using the default Panel with size of the whole screen for drawing shapes on drawPanel , and a custom Panel on top of it for the tools , so I can add a background to it toolsPanel:
#ifndef WXIMAGEPANEL_H
#define WXIMAGEPANEL_H
#include <wx/wx.h>
#include <wx/custombgwin.h>
#include <wx/dcbuffer.h>
class wxImagePanel : public wxCustomBackgroundWindow<wxPanel>
{
public:
wxImagePanel();
wxImagePanel (wxWindow *parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& sizee = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxNO_BORDER,
const wxString& name = wxPanelNameStr);
void SetBackgroundImage(const wxBitmap & background);
virtual ~wxImagePanel();
protected:
private:
const wxBitmap * ptr_backgorund;
void paintEvent(wxPaintEvent & evt);
void OnEraseBackground(wxEraseEvent& event);
DECLARE_EVENT_TABLE()
};
#endif // WXIMAGEPANEL_H
----------------------------------------------------
#include "wxImagePanel.h"
wxImagePanel::wxImagePanel()
{
//ctor
//SetBackgroundStyle(wxBG_STYLE_PAINT);
}
wxImagePanel::wxImagePanel (wxWindow *parent,
wxWindowID winid ,
const wxPoint& pos ,
const wxSize& sizee ,
long style ,
const wxString& name)
{
Create(parent,winid,pos,sizee,style,name);
//SetBackgroundStyle(wxBG_STYLE_PAINT);
}
void wxImagePanel::SetBackgroundImage(const wxBitmap & background)
{
this->ptr_backgorund = &background;
SetBackgroundBitmap(background);
}
wxImagePanel::~wxImagePanel()
{
//dtor
if(ptr_backgorund)
delete ptr_backgorund;
}
BEGIN_EVENT_TABLE(wxImagePanel, wxPanel)
//EVT_PAINT(wxImagePanel::paintEvent)
EVT_ERASE_BACKGROUND(wxImagePanel::OnEraseBackground)
END_EVENT_TABLE()
void wxImagePanel::OnEraseBackground(wxEraseEvent& event)
{
}
void wxImagePanel::paintEvent(wxPaintEvent & evt)
{
wxAutoBufferedPaintDC dc(this);
PrepareDC(dc);
if(ptr_backgorund)
dc.DrawBitmap( *ptr_backgorund, 0, 0);
}
I have tried both ways (drawing the background myself, and using SetBackgroundBitmap method) , both ways are flickering when am calling drawPanel->Refresh() on MouseMove event , so what am missing here , that causing the toolsPanel to flicker?

The flicker is unavoidable if you call Refresh() on the entire window on each mouse move, this simply shouldn't be done. At the very least, you need to refresh just the small area which really needs to be repainted and not the entire window, which would reduce the flicker significantly but might still be not enough. The best solution is to use the (unfortunately still undocumented) wxOverlay class to overlay whatever you're drawing when the mouse moves on top of the window.

try refreshing like this:
window->Refresh(false)
Here's a similar thread containing a very useful piece of advice: on Refresh, "the option eraseBackground = FALSE is very important to avoid flickering"
how to animate picture without making it flicker using wxWidgets in erlang?
The documentation says Refresh repaints "window, and all of its children recursively (except under wxGTK1 where this is not implemented)". Although I don't understand the behaviour, I'm suspecting the recursive nature of Refresh causing the flicker, since I'm usually experiencing it under Windows 7 but not under Linux GTK (with the exact same code).
However, under Windows 10 I don't see any flicker even with eraseBackground = TRUE.

Related

wxPopupTransientWindow not showing content correctly

I'm creating a C++ wxWidgets calculator application. I'm implementing trigonometric functions, and to save on space I've reunited all of them in a single button. If you right click on the button then, a popup is created, which contains buttons for all the functions. I'm using a derived wxPopupTransientWindow class for this job, the problem is, the buttons aren't displaying correctly.
This is my code:
expandMenu.h
#include "wx/wx.h"
#include "wx/popupwin.h"
struct expandMenuInfo
{
const wxString& label;
wxWindowID id;
expandMenuInfo(const wxString& l, wxWindowID i)
: label{l}
, id{i}
{}
};
class expandMenu : public wxPopupTransientWindow
{
wxWindow* panel;
wxBoxSizer* sizer;
public:
expandMenu(wxWindow* parent, wxPoint pos, std::vector<expandMenuInfo> buttons);
~expandMenu();
};
expandMenu.cpp
#include "expandMenu.h"
// PANNELLO ESTENSIONE
expandMenu::expandMenu(wxWindow* parent, wxPoint pos, std::vector<expandMenuInfo> buttons)
: wxPopupTransientWindow(parent, wxBORDER_NONE | wxPU_CONTAINS_CONTROLS)
{
this->SetPosition(pos);
this->SetSize(50 * buttons.size(), 50);
this->SetBackgroundColour(wxColour(90, 93, 121));
panel = new wxWindow(this, wxID_ANY);
sizer = new wxBoxSizer(wxHORIZONTAL);
// costruisci struttura
for (unsigned int i = 0; i < buttons.size(); i++)
{
wxButton* btn = new wxButton(this, buttons.at(i).id, buttons.at(i).label);
sizer->Add(btn, 1, wxEXPAND);
}
panel->SetSizer(sizer);
}
expandMenu::~expandMenu()
{
}
And this is the code I use for my custom wxButton's to actually create the popup (it's temporary):
void ikeButton::rightClick(wxMouseEvent& evt) // CREA PANNELLO ESTENSIONE
{
if (flags & EXPANDABLE)
{
std::vector<expandMenuInfo> buttons;
buttons.push_back(expandMenuInfo(L"sin", 3001));
buttons.push_back(expandMenuInfo(L"cos", 3002));
buttons.push_back(expandMenuInfo(L"tan", 3003));
expandMenu* menu = new expandMenu(this, wxGetMousePosition(), buttons);
menu->Popup();
}
}
Thank you in advance for any help, as I'm pretty new to this framework.
To get the buttons to layout in the popup window, in the constuctor for expandMenu I think you just need to change panel->SetSizer(sizer); to
SetSizerAndFit(sizer);
Layout();
From a UI perspective, I think a split button might be a better way to implement the functionality you are describing. wxWidgets doesn't currently have such a control, but this post shows 2 different ways you can create one.
I believe the issue is that you're specifying this for the parent of the buttons, i.e. the popup window. I think you wanted their parent to be the panel instead.

Making an application bundle with Xcode, C++ and wxWidgets

I'm new to macos, I'm trying to figure out how to make an application bundle so my code won't be just an executable file.
I'm working with xcode version 12.5 and writing a test gui application using c++ language and the wxWidgets library.
Now I tried to make a simple gui with just a button and a menu bar (The bar on top of the screen with different menus like File or About ). I encountered 2 issues :
The first and the major one is that the menubar doesn't respond when the executable starts, it only works when focused on another program and then focuses back to the executable, then the menu is clickable and working. I read that this happens because I build the program as an executable and not as an application bundle. Although, couldn't figure out how to do so in xcode, and the internet doesn't quite answers how to do so.
The button doesn't have a click effect when you only touch the mousepad ( you have to click it to make a blue click effect ), you have to click and hold the button to see the button change color.
CODE :
cpp file of the app ( inherits from wxApp )
bool instaStalkApp::OnInit(){
instaFrame = new instaStalkFrame("InstaStalk", wxPoint(50,50), wxSize(P_WIDTH,P_HEIGHT));
instaFrame->Show(true);
SetTopWindow(instaFrame);
mainPanel = new instaStalkPanel(instaFrame, ID_PANEL_MAIN, wxPoint(-1,-1), wxSize(P_WIDTH, P_HEIGHT), wxTAB_TRAVERSAL, "panel");
testButton = new instaButton(mainPanel, ID_TEST_BUTTON_1, "test1" , wxPoint(P_WIDTH / 2 ,P_HEIGHT / 2), wxSize(100,20), "test1but");
return true;
}
Header file of the button class :
#ifndef instaButton_hpp
#define instaButton_hpp
#include <stdio.h>
#include <wx/wx.h>
class instaButton : public wxButton {
private:
public:
instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name);
void On_Button1Click(wxCommandEvent &event);
wxDECLARE_EVENT_TABLE();
};
#endif /* instaButton_hpp */
event table of the button :
wxBEGIN_EVENT_TABLE(instaButton, wxButton)
EVT_BUTTON(ID_TEST_BUTTON_1, instaButton::On_Button1Click)
wxEND_EVENT_TABLE()
cpp file of custom button
#include <wx/wx.h>
#include "instaButton.hpp"
instaButton::instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name) : wxButton(parent, id, label, pos, size, 0, wxDefaultValidator, name){
}
void instaButton::On_Button1Click(wxCommandEvent &event){
wxLogMessage("hi");
}
cpp file of the frame with the menuBar :
#include <wx/wx.h>
#include "instaStalkFrame.hpp"
#include "instaStalkPanel.hpp"
#include "consts.hpp"
instaStalkFrame::instaStalkFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size){
menuFile = new wxMenu;
menuFile->Append(ID_TEST, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar(menuBar);
}
void instaStalkFrame::OnExit(wxCommandEvent& event){
Close(true);
}
void instaStalkFrame::OnAbout(wxCommandEvent& event){
wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION );
}
void instaStalkFrame::OnTest(wxCommandEvent& event){
wxLogMessage("Test!");
}
In order to make an Application Bundle:
You should have been creating an OSX Application project inside Xcode. Check wxWiki (I know it is for older version of Xcode, but it still applies and you can try to match it in the newer version).
If you want to do that later - you can try to build minimal sample from thr wxWidgets distribution, see what it uses in order to create a bundle and replicate that in you test program. But you absolutely should make an Application Bundle project in Xcode to simplify your life.
The menu will start working when you create a Bundle. As you should.
I don't think hovering effect is implemented. You can ask about it on wx-users ML.
Let us know if you need any more guidance.

Why does turning on double buffering break my list control and resizing the window then fixes it?

I am using a wxListCtrl in report style in a programming project on Windows 10, using Visual Studio 2017. I noticed that when the program first starts, there are some strange artifacts: the vertical lines between the columns don't move with the headers when I resize them with the mouse. Also, the scrollbar does not move when I drag it but the list jumps to the position when I release the mouse button. Once I resize the window however, everything is fine.
After reducing the program to a minimal example, I found out that turning off double buffering for the frame that contains the list control fixes everything. But I don't really understand why. Can anybody explain this to me?
#include <wx/wx.h>
#include <wx/listctrl.h>
class MainWindow : public wxFrame
{
public:
MainWindow();
void updateWidgets();
private:
wxListCtrl *listCtrl;
void initWidgets();
};
MainWindow::MainWindow()
{
// ****************************************************************
// Set window properties and init the widgets.
// ****************************************************************
wxFrame::Create(NULL, wxID_ANY, wxT("wxListCtrl Issue"), wxDefaultPosition,
wxDefaultSize, wxCLOSE_BOX | wxMINIMIZE_BOX | wxMAXIMIZE_BOX |
wxSYSTEM_MENU | wxCAPTION | wxRAISED_BORDER | wxRESIZE_BORDER);
// Comment the following line to fix the list control, but why?
this->SetDoubleBuffered(true);
initWidgets();
}
void MainWindow::initWidgets()
{
listCtrl = new wxListCtrl(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxLC_REPORT);
listCtrl->InsertColumn(0, wxT("Col 0"), wxLIST_FORMAT_LEFT, 50);
for (int i = 0; i < 60; i++)
{
long index = listCtrl->InsertItem(0, wxT("Item"));
}
}
class wxListCtrl_Issue : public wxApp
{
public:
virtual bool OnInit();
};
bool wxListCtrl_Issue::OnInit()
{
MainWindow *mainWindow = new MainWindow();
mainWindow->Show(true);
return true;
}
wxIMPLEMENT_APP(wxListCtrl_Issue);
Generally speaking, you shouldn't interfere with the drawing of native controls and wxListCtrl is native under MSW. Moreover, it already is double-buffered using its own specific mechanism for this (LVS_EX_DOUBLEBUFFER) and so it's not surprising at all that setting WS_EX_COMPOSITED for it unexpectedly (from the controls point of view) breaks it.
I.e. the answer is simple: just don't call SetDoubleBuffered() for it, nor any other native control.

wxWidgets Transparent wxTextCtrl

I'm trying to create a custom transparent wxTextCtrl by driving from it and drawing the text myself as follow:
BEGIN_EVENT_TABLE(TextLayer, wxTextCtrl)
EVT_PAINT(TextLayer::OnPaint)
EVT_ERASE_BACKGROUND(TextLayer::OnEraseBackground)
END_EVENT_TABLE()
void TextLayer::OnEraseBackground(wxEraseEvent& event) {}
void TextLayer::OnPaint(wxPaintEvent & evt)
{
wxAutoBufferedPaintDC dc(this);
PrepareDC(dc);
wxGraphicsContext *gc = wxGraphicsContext::Create( dc );
if(gc)
{
dc.SetFont(GetFont());
dc.SetTextForeground(GetForegroundColour());
auto a = GetValue();
dc.DrawText(GetValue(), 0, 0);
delete gc;
}
}
but it's not working , it's not transparent , how I'm supposed to do it correctly?
Sorry, but you won't be able to make a native control transparent. You can have custom transparent controls with wxGTK (see the "erase" sample), but not the native ones.

wxWidget wxScrolledWindow cancel previous drawing

I'm trying to get a scrolling panel where I can draw picture like on MS Paint
The problem is that, when I draw something to try the application:
I draw one rectangle (or more than one)
I scroll the panel to the right, the rectangles remain to the left
I scroll the panel to the left, the rectangles are disappeared
I'm a newbie in wx and I think I'm missing something important
Using wxWidget in Codelite (C++), Windows 7 32 bit
That's the code:
editorshp.h
class EditorShp : public wxFrame {
protected:
wxClientDC *dcPannelloDisegno;
wxScrolledWindow *scrollwin;
wxPanel *pannello;
public:
EditorShp( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Editor SHP"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 800,500 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
void draw_temp(wxCommandEvent& WXUNUSED(event));
};
editorshp.cpp
PS: bDrawTemp is the button I use for drawing some rectangles (the first is at point 100,100; the second at 200,100 and so on)
EditorShp::EditorShp( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style ) {
scrollwin = new wxScrolledWindow(this, -1, wxPoint(0, 0), wxSize(700, 400));
scrollwin->SetScrollbars(1, 1, 1600, 1000, 0, 0);
pannello = new wxPanel(scrollwin, -1, wxPoint(0, 0), wxSize(700, 400), wxFULL_REPAINT_ON_RESIZE);
dcPannelloDisegno = new wxClientDC(pannello);
scrollwin->DoPrepareDC(*dcPannelloDisegno);
wxButton *bDrawTemp= new wxButton( this, wxID_ANY, _("Indietro"), wxPoint(0, 400), wxSize( 100, 24 ), 0 );
bDrawTemp->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(EditorShp::disegnaTemp), NULL, this);
scrollwin->Show(true);
this->Show(true);
}
void EditorShp::draw_temp(wxCommandEvent& WXUNUSED(event)){
static int x = 100, y = 100;
dcPannelloDisegno->DrawRectangle(wxPoint(x, y), wxSize(10, 10));
//pannello->Refresh(); it cancels everything
pannello->Update();
x += 100;
x = x % 1600;
}
EDIT 1: Sorry it's not wxScrollingPanel but wxScrolledWindow
First, catch wxEVT_PAINT and do the drawing in its hadler, using a wxPaintDC (or one of its variants - wx[Auto]BufferedPaintDC).
Second, you are now attempting to paint on pannello, and for that you do not need to prepare the wxDC. However, from your description you want to paint on scrollwin, and in that case you might not even need pannello.
So, for drawing on a scrolled window, see scroll sample that comes with wxW. What you probably need is the implementation of MySimpleCanvas (described as a scrolled window which draws a simple rectangle) - to see it press F1 when running the sample.
As a general rule, the samples should be the first place to search for a functionality; there are a lot of them exemplified there.