I am having trouble with getting a wxStaticText label to wrap dynamically in a dialog, using wxWidgets 3.0.2. I followed ideas for other questions like this one, bit I still have strange effects.
I am using the Wrap(int) function on the text, in a callback on the wxEVT_SIZE event, but it seems to have an unexpected effect on the text, and also seems to only "ratchet" down the size, and won't wrap again as the window expands.
The main part of the binding is:
CTOR(...) {
....
m_text->Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}
void CLASS::onResize( wxSizeEvent& event )
{
m_text->Wrap( event.GetSize().GetWidth() );
event.Skip();
}
The result looks OK when the dialog is first shown, but when you resize narrower and back up, you get this result:
A minimum reproducible example is:
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class DIALOG_WRAPTEXT: public wxDialog
{
public:
DIALOG_WRAPTEXT( wxWindow* parent,
const wxString& aTitle, const wxSize aSize );
private:
void onResize( wxSizeEvent& evt );
wxBoxSizer* m_itemBoxSizer;
wxStaticText* m_text;
};
DIALOG_WRAPTEXT::DIALOG_WRAPTEXT(
wxWindow* parent, const wxString& aTitle, const wxSize aSize ):
wxDialog( parent, wxID_ANY, aTitle,
wxPoint( -1, -1 ), aSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
m_itemBoxSizer = new wxBoxSizer( wxVERTICAL );
SetSizer( m_itemBoxSizer );
wxString msg("Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
);
m_text = new wxStaticText( this, wxID_ANY, msg );
// wxEXPAND makes no difference
m_itemBoxSizer->Add( m_text, 1, wxALIGN_TOP | wxALL | wxEXPAND, 5 );
// Bind to m_text or this, same effect
m_text->Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}
void DIALOG_WRAPTEXT::onResize( wxSizeEvent& event )
{
//m_text->Freeze(); // makes no difference
const auto w = event.GetSize().GetWidth();
wxLogDebug( "Wrap to width: %d",w ); // produces sensible values
m_text->Wrap( w );
//m_text->Thaw();
event.Skip();
}
class MyApp: public wxApp
{
public:
bool OnInit() override
{
auto d = new DIALOG_WRAPTEXT(NULL, "Dialog title", wxSize(200, 200));
d->ShowModal();
d->Destroy();
}
};
wxIMPLEMENT_APP(MyApp);
What is the right way to dynamically wrap static text in a dialog?
Without any wrap(), the wxStaticText displays the text correctly (wrapping at word boundaries) using the follwing minimal code on windows with wx 3.0.2. I can resize the dialog (shrink, grow) and the wxStaticText will update itself correctly. This is not enough for your use case? Are you sure you need to use the wrap function?
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class DIALOG_WRAPTEXT : public wxDialog
{
public:
DIALOG_WRAPTEXT(wxWindow* parent,
const wxString& aTitle, const wxSize aSize);
private:
void onResize(wxSizeEvent& evt);
wxBoxSizer* m_itemBoxSizer;
wxStaticText* m_text;
};
DIALOG_WRAPTEXT::DIALOG_WRAPTEXT(
wxWindow* parent, const wxString& aTitle, const wxSize aSize) :
wxDialog(parent, wxID_ANY, aTitle,
wxPoint(-1, -1), aSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
m_itemBoxSizer = new wxBoxSizer(wxVERTICAL);
SetSizer(m_itemBoxSizer);
wxString msg("Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
);
m_text = new wxStaticText(this, wxID_ANY, msg);
// wxEXPAND makes no difference
m_itemBoxSizer->Add(m_text, 1, wxALIGN_TOP | wxALL | wxEXPAND, 5);
// Act on dialog resize
Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}
void DIALOG_WRAPTEXT::onResize(wxSizeEvent& event)
{
// layout everything in the dialog
Layout();
event.Skip();
}
class MyApp : public wxApp
{
public:
bool OnInit() override
{
auto d = new DIALOG_WRAPTEXT(NULL, "Dialog title", wxSize(200, 200));
d->ShowModal();
d->Destroy();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
Encountered similar problem. I have to store the unwrapped message somewhere, when wxStaticText is resized, set the message and call wrap. otherwise the line may not be wrapped nicely.
void MyFrame::onResize(wxSizeEvent& evt)
{
const auto w = evt.GetSize().GetWidth();
m_text->SetLabel(m_msg); // unwrapped message
m_text->Wrap(w);
evt.Skip();
}
Related
I'm writing a C++ wxWidgets application. I want my custom wxButton's that have a EXPANDABLE flag to open up a panel on top of them when I right click. Said panel is also supposed to automatically close when the cursor leaves it, or when the user clicks on one of the buttons it contains.
I created a custom wxPanel class for this job, but I'm having some trouble. The panel is successfully created when I right click on the button, but:
The panel is not removed when the cursor leaves it
There's an awkward graphical glitch when I move the cursor that cause the other interface elements to get on top of it (?)
Here's my code:
expandPanel.h
#pragma once
#include "wx/wx.h"
#include "ikeButton.h"
class expandPanel : public wxPanel
{
wxBoxSizer* sizer;
int type;
public:
expandPanel(wxWindow* parent, wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL,
const wxString & name = wxPanelNameStr);
~expandPanel();
void paintEvent(wxPaintEvent& evt);
void paintNow();
void Render(wxDC& dc);
void mouseLeftWindow(wxMouseEvent& evt);
DECLARE_EVENT_TABLE()
};
expandPanel.cpp
#include "expandPanel.h"
BEGIN_EVENT_TABLE(expandPanel, wxPanel)
EVT_LEAVE_WINDOW(expandPanel::mouseLeftWindow)
EVT_PAINT(expandPanel::paintEvent)
END_EVENT_TABLE()
expandPanel::expandPanel(wxWindow* parent, wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString& name)
{
wxPanel::Create(parent, id, pos, size, style, name);
sizer = new wxBoxSizer(wxHORIZONTAL);
SetSizer(sizer);
}
expandPanel::~expandPanel()
{
}
void expandPanel::paintEvent(wxPaintEvent& evt)
{
wxPaintDC dc(this);
Render(dc);
}
void expandPanel::paintNow()
{
wxClientDC dc(this);
Render(dc);
}
void expandPanel::Render(wxDC& dc)
{
dc.SetBrush(wxColour(80, 83, 111));
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle(0, 0, this->GetSize().GetWidth(), this->GetSize().GetHeight());
}
void expandPanel::mouseLeftWindow(wxMouseEvent& evt)
{
this->Close();
}
(I'm fairly new to this framework)
Ok, I tried switching to wxPopupTransientWindow, but the buttons appear on a different location on screen and sometimes disappear when I move the cursor, but the popup placed in the correct position do not disappear in any way. I feel like I'm doing this completely wrong.
expandMenu.h
#pragma once
#include "wx/wx.h"
#include "wx/popupwin.h"
#include "ikeButton.h"
class expandMenu : public wxPopupTransientWindow
{
wxBoxSizer* sizer;
public:
expandMenu(wxWindow* parent, wxPoint pos, wxSize size, int flags = wxBORDER_NONE);
~expandMenu();
void AddButton(ikeButton* btn);
};
expandMenu.cpp
#include "expandMenu.h"
expandMenu::expandMenu(wxWindow* parent, wxPoint pos, wxSize size, int flags)
: wxPopupTransientWindow(parent, flags)
{
this->SetPosition(pos);
this->SetSize(size);
this->SetBackgroundColour(wxColour(90, 93, 121));
sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Layout();
SetSizer(sizer);
}
expandMenu::~expandMenu()
{
}
void expandMenu::AddButton(ikeButton* btn)
{
unsigned int w = this->GetSize().GetWidth(), h = this->GetSize().GetHeight();
if (sizer->GetItemCount() > 0)
this->SetSize(wxSize(w, h + h / sizer->GetItemCount() ));
sizer->Add(btn, 0, wxEXPAND);
sizer->Layout();
}
Then I generate them like this:
void Main::OnExpandableRightClick(wxMouseEvent& evt)
{
expandMenu* menu = new expandMenu(this, wxGetMousePosition(), wxSize(50, 50));
for (int i = 0; i < 3; i++) menu->AddButton(new ikeButton(this, wxID_ANY, "TEST " + std::to_string(i), OP));
menu->Popup();
evt.Skip();
}
(This is a test scenario to see if everything is working)
Hi i am trying to create a wxApp that will have not have a titlebar(including the minimize, maximize and close) icons that are by default provided. The code that i have is as follows:
main.h
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
bool MyApp::OnInit()
{
MyFrame *prop = new MyFrame(wxT("MyFrame"));
prop->Show(true);
return true;
}
myframe.h
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
void OnClick(wxMouseEvent& event);
};
myframe.cpp
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 130))
{
MyPanel *panel = new MyPanel(this, wxID_ANY);
panel->SetBackgroundColour(wxColour(255,255,255));
MyButton *button = new MyButton(panel, wxID_ANY, wxT("Ok"));
Connect( wxEVT_LEFT_UP,
wxMouseEventHandler(MyFrame::OnClick));
panel->Centre();
}
void MyFrame::OnClick(wxMouseEvent& event)
{
std::cout << "event reached frame class" << std::endl;
}
mypanel.h
class MyPanel : public wxPanel
{
public:
MyPanel(wxFrame *frame, int id);
void OnClick(wxMouseEvent& event);
};
mypanel.cpp
MyPanel::MyPanel(wxFrame *frame, int id)
: wxPanel(frame, id)
{
Connect( wxEVT_LEFT_UP,
wxMouseEventHandler(MyPanel::OnClick));
}
void MyPanel::OnClick(wxMouseEvent& event)
{
std::cout << "event reached panel class" << std::endl;
}
mybutton.h
class MyButton : public wxPanel
{
public:
MyButton(wxPanel *panel, int id, const wxString &label);
void OnClick(wxMouseEvent& event);
};
mybutton.cpp
void MyButton::onClick(wxMouseEvent &event)
{
}
What i want is:
There should be no title bar(including the 3 maximize, minimize & close buttons) at the top.
Now since there is no titlebar at the top of the frame, there is no way to drag or close or maximize or minimize the window. For that i want to create a custom titlebar at the top which should have the three customized maximized,minimized and close button and also i should be able to drag the frame by double clicking and holding and dragging the topmost part of the newly created frame.
Is this possible in wxWidgets? If yes, how can i achieve this?
I am not proposing any new way of dragging. The new frame/window that we will have should also be dragged only by its own customized title bar. It's exactly like dragging the old frame by double clicking and dragging the frame in the native case. I just want to customize the native title bar. Like increase its height, change it's colour, change how to three buttons(minimize, maximize and close) look.
Here's the simplest example I can think of how to create a frame with a pseudo titlebar that can be clicked to drag the frame around. This example shows which mouse events need to be handled to drag the window around and how to do the calculations needed in those event handlers.
Note that moving the frame needs to be done in screen coordinates, but the coordinates received in the event handlers will be in client coordinates for the title bar. This example also shows how to do those coordinate conversions.
#include "wx/wx.h"
class CustomTitleBar:public wxWindow
{
public:
CustomTitleBar(wxWindow* p) : wxWindow(p,wxID_ANY)
{
m_dragging = false;
SetBackgroundColour(*wxGREEN);
Bind(wxEVT_LEFT_DOWN,&CustomTitleBar::OnMouseLeftDown,this);
Bind(wxEVT_MOUSE_CAPTURE_LOST, &CustomTitleBar::OnMouseCaptureLost,
this);
}
wxSize DoGetBestClientSize() const override
{
return wxSize(-1,20);
}
private:
void OnMouseLeftDown(wxMouseEvent& event)
{
if ( !m_dragging )
{
Bind(wxEVT_LEFT_UP,&CustomTitleBar::OnMouseLeftUp,this);
Bind(wxEVT_MOTION,&CustomTitleBar::OnMouseMotion,this);
m_dragging = true;
wxPoint clientStart = event.GetPosition();
m_dragStartMouse = ClientToScreen(clientStart);
m_dragStartWindow = GetParent()->GetPosition();
CaptureMouse();
}
}
void OnMouseLeftUp(wxMouseEvent&)
{
FinishDrag();
}
void OnMouseMotion(wxMouseEvent& event)
{
wxPoint curClientPsn = event.GetPosition();
wxPoint curScreenPsn = ClientToScreen(curClientPsn);
wxPoint movementVector = curScreenPsn - m_dragStartMouse;
GetParent()->SetPosition(m_dragStartWindow + movementVector);
}
void OnMouseCaptureLost(wxMouseCaptureLostEvent&)
{
FinishDrag();
}
void FinishDrag()
{
if ( m_dragging )
{
Unbind(wxEVT_LEFT_UP,&CustomTitleBar::OnMouseLeftUp,this);
Unbind(wxEVT_MOTION,&CustomTitleBar::OnMouseMotion,this);
m_dragging = false;
}
if ( HasCapture() )
{
ReleaseMouse();
}
}
wxPoint m_dragStartMouse;
wxPoint m_dragStartWindow;
bool m_dragging;
};
class Customframe : public wxFrame
{
public:
Customframe(wxWindow* p)
:wxFrame(p, wxID_ANY, wxString(), wxDefaultPosition, wxSize(150,100),
wxBORDER_NONE)
{
CustomTitleBar* t = new CustomTitleBar(this);
SetBackgroundColour(*wxBLUE);
wxBoxSizer* szr = new wxBoxSizer(wxVERTICAL);
szr->Add(t,wxSizerFlags(0).Expand());
SetSizer(szr);
Layout();
}
};
class MyFrame: public wxFrame
{
public:
MyFrame():wxFrame(NULL, wxID_ANY, "Custom frame Demo", wxDefaultPosition,
wxSize(400, 300))
{
wxPanel* bg = new wxPanel(this, wxID_ANY);
wxButton* btn = new wxButton(bg, wxID_ANY, "Custom frame");
wxBoxSizer* szr = new wxBoxSizer(wxVERTICAL);
szr->Add(btn,wxSizerFlags(0).Border(wxALL));
bg->SetSizer(szr);
Layout();
btn->Bind(wxEVT_BUTTON, &MyFrame::OnButton, this);
m_customFrame = NULL;
}
private:
void OnButton(wxCommandEvent&)
{
if ( m_customFrame )
{
m_customFrame->Close();
m_customFrame = NULL;
}
else
{
m_customFrame = new Customframe(this);
m_customFrame->CenterOnParent();
m_customFrame->Show();
}
}
wxFrame* m_customFrame;
};
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
MyFrame* frame = new MyFrame();
frame->Show();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
On windows, it looks like this.
You should be able to add whatever buttons you want to the custom title bar exactly as you would add buttons to any other window.
"Is this possible in wxWidgets?"
Yes. You need to use a wxWindow instead of a wxFrame and set some style for it. like wxBORDER_NONE. But you will have to implement many things that wxFrame already provides.
And your proposed way of dragging seems wrong/confusing to me. 99.99% of users prefer a UI they are used to it, avoiding to learn a new way of doing simple things they already know.
If you just want to avoid resizing then you have two ways:
a) Catch the size-event and do nothing. Calling event.Skip(false)
(to prevent handling in parent) is not neccessary.
b) Once you have created your window and correctly sized, get its size and set it as max and min.
In both cases the user will see a "resize" mouse pointer when hovering the mouse at any border, but nothing else, no resizing, will be done.
#JasonLiam,
Don't forget to place the application icon in the top left corner of you title bar and handle the right/left mouse clicks appropriately (as in native application) (if you want to go this route and get rid of the native frame window).
Thank you.
how to show another frame after button click event?
like this
My code here show window OnInit. but what to do next?
I did not find how to do this. little experience with this.
I comment the window that should be.
enum
{
wxID_First_Load = 5000,
wxID_First_Frame,
wxID_First_Panel
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void fileLoad(wxCommandEvent& event);
private:
int file_count = 0;
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(wxID_First_Load, MyFrame::fileLoad)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame("Hello World", wxDefaultPosition, wxSize(450, 250));
frame->SetWindowStyle(wxCAPTION | wxSYSTEM_MENU );
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_First_Frame, title, pos, size)
{
wxBoxSizer *first_sizer = new wxBoxSizer(wxVERTICAL);
wxPanel *firstPanel = new wxPanel(this, wxID_First_Panel);
wxButton *firstButton_Load = new wxButton(firstPanel, wxID_First_Load, "Choose file");
firstPanel->SetSizer(first_sizer);
first_sizer->Add(firstButton_Load, 1, wxEXPAND | wxALL, 10);
firstPanel->SetSizer(first_sizer);
}
void MyFrame::fileLoad(wxCommandEvent& WXUNUSED(event))
{
file_count = 2;
}
Second Frame or window:
wxPanel *firstPanel = new wxPanel(this, wxID_First_Panel);
wxBoxSizer *second_sizer = new wxBoxSizer(wxVERTICAL);
for (int i = 0; i < file_count; i++)
{
second_sizer->Add(new wxTextCtrl(firstPanel, wxWindowID(i), "Hello", wxDefaultPosition, wxSize(235, 60)), wxSizerFlags(0).Border(wxALL, 5));
}
firstPanel->SetSizer(second_sizer);
To create a new frame you need to create a new object of wxFrame class or a class deriving from it. Typically, you want to put some data and logic into your new frame, so you would create some class, e.g. MySecondaryFrame (but hopefully with a better name) , inheriting from wxFrame in a similar way to your existing MyFrame class.
Then to show it you would do the same thing that you do in MyApp::OnInit(), i.e. create a new object of this class and call Show() to actually show it.
P.S. Note that your SetWindowStyle(wxCAPTION | wxSYSTEM_MENU ) call is unnecessary, these styles are already on by default. Also, hard coding the frame size in pixels is a bad idea, consider using sizers to determine the best size suitable for the frame contents or just leave it unspecified if it really doesn't matter.
I want to view an image in one of wxSplitterWindow using wxScrolledWindow.
I derived class from wxPanel, which I add into wxScrolledWindow. But the result is not what i want.(
class ImagePanel : public wxPanel
{
public:
//ImagePanel(wxFrame* parent);
ImagePanel(wxWindow* parent);
void paintEvent(wxPaintEvent& event);
void paintNow();
void setBitmap(wxBitmap& imBitmap);
void setBitmap(wxImage& img);
int GetHeight();
int GetWidth();
void render(wxDC& dc);
DECLARE_EVENT_TABLE()
private:
wxBitmap curBitmap;
};
Paint event:
void ImagePanel::paintEvent(wxPaintEvent& event)
{
wxBufferedPaintDC dc(this);
render(dc);
}
This is how I have written the interface into main frame constructor:
topSizer = new wxBoxSizer(wxVERTICAL);
topSplitter = new wxSplitterWindow(this,ID_TOPSPLITTER,wxDefaultPosition,wxDefaultSize,wxSP_LIVE_UPDATE|wxSP_3D);
topSizer->Add(topSplitter,1,wxALL|wxEXPAND,0);
imagePanel = new wxPanel(topSplitter,ID_IMAGEPANEL);
imageSizer = new wxBoxSizer(wxVERTICAL);
imagePanel->SetSizer(imageSizer);
optionPanel = new wxPanel(topSplitter,ID_OPTIONPANEL);
optionSizer = new wxBoxSizer(wxVERTICAL);
optionPanel->SetSizer(optionSizer);
topSplitter->SplitVertically(imagePanel, optionPanel);
topSplitter->SetMinimumPaneSize(200);
//===========================================
//image panel interface
imageScWindow=new wxScrolledWindow(imagePanel,ID_IMAGESCWINDOW,wxDefaultPosition,wxDefaultSize,
wxVSCROLL|wxHSCROLL|wxALL|wxALWAYS_SHOW_SB|wxFULL_REPAINT_ON_RESIZE);
imgArea = new ImagePanel(imageScWindow);
imageSizer->Add(imageScWindow,1,wxEXPAND,0);
scrollSizer = new wxBoxSizer(wxVERTICAL);
imageScWindow->SetSizer(scrollSizer);
scrollSizer->Add(imgArea,1,wxEXPAND,0);
imageScWindow->SetTargetWindow(imgArea);
And this is the function that loads image:
void MainFrame::Setpic(wxCommandEvent& event)
{
wxString path = GetCurrentWorkingDir()+wxT("\\")+tmptxt1->GetValue();
wxImage img(path);
wxBitmap imBitmap(img,-1);
imgArea->setBitmap(imBitmap);
imageScWindow->SetScrollbars(1,1,imgArea->GetWidth(),imgArea->GetHeight());
imageScWindow->Refresh();
}
Thats what i get:
https://i.imgur.com/x1GIlrl.png
I have also tried to do like this in the constructor, without SetTargetArea:
//===========================================
//image panel interface
imageScWindow=new wxScrolledWindow(imagePanel,ID_IMAGESCWINDOW,wxDefaultPosition,wxDefaultSize,
wxVSCROLL|wxHSCROLL|wxALL|wxALWAYS_SHOW_SB|wxFULL_REPAINT_ON_RESIZE);
imgArea = new ImagePanel(imageScWindow);
imageSizer->Add(imageScWindow,1,wxEXPAND,0);
scrollSizer = new wxBoxSizer(wxVERTICAL);
imageScWindow->SetSizer(scrollSizer);
scrollSizer->Add(imgArea,1,wxEXPAND,0);
Then image shows properly, but if I change the frame size or move splitter, scrollbars disappear, and I have to set them again somwhere.
Then i tried to do like this:
int prvx,prvy;
void ImagePanel::paintEvent(wxPaintEvent& event)
{
reinterpret_cast<wxScrolledWindow*>(this->GetParent())->GetViewStart(&prvx,&prvy);
reinterpret_cast<wxScrolledWindow*>(this->GetParent())->SetScrollbars(1,1,curBitmap.GetWidth(),curBitmap.GetHeight());
reinterpret_cast<wxScrolledWindow*>(this->GetParent())->Scroll(wxPoint(prvx,prvy));
wxBufferedPaintDC dc(this);
render(dc);
}
It shows properly, but lags while scrolling, even without wxFULL_REPAINT_ON_RESIZE in wxScrolledWindow. The reason why it is in paint event: if I add this into OnSize event, program crashes. What to do?
I'm not sure where to begin answering your question. Here's a minimal sample of a frame that has a splitter with a bitmap on the left and a panel on the right. I hope you can adapt this sample to do what you need.
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <wx/scrolwin.h>
#include <wx/splitter.h>
#include <wx/dcbuffer.h>
class MyFrame : public wxFrame
{
public:
MyFrame( wxWindow* parent, int id = wxID_ANY, wxString title = "Demo",
wxPoint pos = wxDefaultPosition, wxSize size = wxDefaultSize,
int style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
private:
void OnScrollPaint( wxPaintEvent& event );
wxScrolledCanvas* m_canvas;
wxBitmap m_bitmap;
};
MyFrame::MyFrame( wxWindow* parent, int id, wxString title, wxPoint pos,
wxSize size, int style )
:wxFrame( parent, id, title, pos, size, style )
{
m_bitmap=wxBitmap ("test.png", wxBITMAP_TYPE_PNG );
wxSplitterWindow* m_splitter1
= new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSP_LIVE_UPDATE );
m_canvas = new wxScrolledCanvas( m_splitter1, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSTATIC_BORDER|wxHSCROLL|wxVSCROLL );
m_canvas->SetScrollRate( 5, 5 );
m_canvas->SetVirtualSize(m_bitmap.GetWidth(), m_bitmap.GetHeight());
m_canvas->SetBackgroundStyle(wxBG_STYLE_PAINT);
m_canvas->Bind( wxEVT_PAINT, &MyFrame::OnScrollPaint , this );
wxPanel* m_panel2 = new wxPanel( m_splitter1, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSTATIC_BORDER|wxTAB_TRAVERSAL );
m_splitter1->SplitVertically( m_canvas, m_panel2, GetSize().x/2 );
}
void MyFrame::OnScrollPaint( wxPaintEvent& event )
{
wxAutoBufferedPaintDC dc(m_canvas);
m_canvas->DoPrepareDC(dc);
dc.Clear();
dc.DrawBitmap(m_bitmap,0,0);
}
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
wxInitAllImageHandlers();
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
To run this, be sure to change "test.png" in line 35 to the name of an actual image file on your computer (and change wxBITMAP_TYPE_PNG if the image file isn't a png).
The important parts are:
Set the background style of the canvas to wxBG_STYLE_PAINT as shown in line 46.
In the paint handler, call the DoPrepareDC method on the paintdc.
By the way, the paint handler shown in my sample isn't very good. It draws the entire bitmap every time. A better approach would be to get the update region and only redraw the parts that are needed, but I wanted to keep this simple.
I've trying to get a pointer to a widget (in the code it's named text). But at least I've got only this
error: no matching function for call to 'std::basic_ostream::basic_ostream(wxWindow*)'
my code:
gui.h
#include <wx/wx.h>
class wxWCK : public wxFrame
{
public:
wxWCK(const wxString& title);
void OnClickCon(wxCommandEvent& event);
void OnClickSta(wxCommandEvent& event);
private:
wxButton *connect;
wxButton *start;
wxTextCtrl *text;
};
const int ID_CON = 100;
const int ID_STA = 101;
const int ID_MF0 = 102;
const int ID_TEX = 103;
void Connect();
void Start();
and gui.cpp
#include "gui.h"
wxWCK::wxWCK(const wxString& title)
: wxFrame(NULL, ID_MF0, title, wxDefaultPosition, wxSize(400,300))
{
wxPanel *panel = new wxPanel(this, -1);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
//Outputwidget
text = new wxTextCtrl(panel, ID_TEX, _T(""),
wxPoint(-1, -1), wxSize(1000, 1000), wxTE_MULTILINE);
//redirecting stream to the outputwidget
//std::ostream stream(text);
stream << "Hello" << std::endl;
connect = new wxButton(panel, ID_CON, _T("Connect"));
start = new wxButton(panel, ID_STA, _T("Start"));
hbox1->Add(text);
hbox2->Add(connect);
hbox2->Add(start);
vbox->Add(hbox1, 1, wxEXPAND);
vbox->Add(hbox2, 0, wxALIGN_LEFT | wxRIGHT | wxBOTTOM, 10);
panel->SetSizer(vbox);
Connect(ID_CON, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(wxWCK::OnClickCon));
//Connect(ID_STA, wxEVT_COMMAND_BUTTON_CLICKED,
// wxCommandEventHandler(wxWCK::OnClickSta));
}
void wxWCK::OnClickCon(wxCommandEvent& WXUNUSED(event))
{ void Connect(); }
void wxWCK::OnClickSta(wxCommandEvent& WXUNUSED(event))
{ void Start(); }
void Connect()
{
//Try to get a pointer to 'text'
std::ostream stream(wxWindow::FindWindowById(ID_TEX));
stream << "Connected" << std::endl;
}
/*
void Start()
{
//Try to get a pointer to 'text'
std::ostream stream(wxWindow::FindWindowById(ID_TEX));
stream << "Started" << std::endl;
}
*/
I hope somebody can help me. A other solution can be to get the stream as global. But when I try to get the stream-declaration in the header, he says he dont know any text so I move the text-declaration outside the class and I got a muliple declaration error... I think, because I include the gui.h in gui.cpp and main.h .
-Casisto
edit:
I changed the the function to:
void Connect()
{
//Try to get a pointer to 'text'
std::ostream stream((wxTextCtrl*) wxWindow::FindWindowById(ID_TEX));
stream << "Connected" << std::endl;
}
Now I don't get a error or a warning, but when I click on "Connect"-Button, the wxTextCtrl get no "Input" (I mean, there is only "Hello" in there)
The initial error must have been from the old code of Connect() because wxWindow::FindWindowById() returns a wxWindow*.
First thing to try: change wxWCK::OnClickCon() to { (*text) << "Connected\n"; }. This should just work.
Next, if you really need that void Connect(), try again without creating the std::ostream (you don't really need it); something like { (*(wxTextCtrl*)wxWindow::FindWindowById(ID_TEX)) << "Connected\n"; }.
However, this might still not work, so make ID_CON = wxID_HIGHEST + 1, ID_STA = wxID_HIGHEST + 2 etc and try again. IIRC I've seen cases where ids with low values caused curious behaviour.
The error message doesn't fit the code you show, in it text is a wxTextCtrl, which derives from std::streambuf and so can be used to construct an associated std::ostream, and not a wxWindow.
IOW your code should work as shown, you must be not showing us everything.