Reputation: 161
Here is a modification to the standard "minimal" app that nearly does what I want. The idea is to have a series of boxes running down the page that each contain a chunk of wrapped text. These text boxes will alternate with boxes containing wxListView objects.
I can see two potential sources of error:
Is wxStaticText the correct widget to use?
Is there a parameter that I haven't spotted that needs tweaking?
Here is the code, my mods to minimal.cpp are mainly in the "RightPanel" and "InnerPanel" classes and in the "extra" routine at the end.
// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp : public wxApp {
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame {
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void extra();
};
enum { ID_Hello = 1 };
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit() {
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "Hello World") {
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
extra();
}
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) {
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& WXUNUSED(event)) {
wxLogMessage("Hello world from wxWidgets!");
}
//--------------------------------------------
#include "wx/splitter.h"
//--------------------------------------------
struct LeftPanel : public wxPanel {
LeftPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
SetBackgroundColour(0xcbc0ff);
}
};
struct InnerPanel : public wxPanel {
InnerPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
char *msg = "It is a truth universally acknowledged, that a long piece of "
"text really should be wrapped"
" ... but ... "
"It is a truth universally acknowledged that great words will "
"be misquoted";
auto st = new wxStaticText( this, wxID_ANY, msg );
st->SetBackgroundColour(0xf5f5f5);
auto sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->AddSpacer(10);
sizer->Add(st, wxSizerFlags(0).Expand().Border(wxALL, 5));
sizer->AddSpacer(10);
st->Wrap(-1);
SetSizer(sizer);
}
};
struct RightPanel : public wxPanel {
RightPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
SetBackgroundColour(0xeeccaa);
wxFont fnt(wxFontInfo(12).FaceName("Arial"));
SetFont(fnt);
auto sizer = new wxBoxSizer(wxVERTICAL);
for (int i=0; i<5; i++) {
auto p = new InnerPanel(this);
sizer->Add(p, wxSizerFlags(0).Expand());
sizer->AddSpacer(10);
}
SetSizer(sizer);
}
};
void MyFrame::extra() {
SetClientSize(wxSize(500, 600));
auto vSplitter = new wxSplitterWindow(this, wxID_ANY);
vSplitter->SetSashGravity(0.5);
vSplitter->SetMinimumPaneSize(50);
auto rPanel = new RightPanel(vSplitter);
auto lPanel = new LeftPanel(vSplitter);
vSplitter->SplitVertically(lPanel, rPanel, -300);
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(vSplitter, wxSizerFlags(1).Expand() );
SetSizer(sizer);
}
There's a wxSplitterWindow in there so that I can see whether the text rewraps as I change the width of an outer panel.
Upvotes: 1
Views: 317
Reputation: 22753
wxStaticText
doesn't wrap the text automatically, but you can wrap it manually using its Wrap()
method. So if you add a call to e.g.
st->Wrap(GetTextExtent("It is a truth universall acknowledged, that").x);
to your code after creating st
, it will wrap at this condition. You can, of course, also define a wxEVT_SIZE
event handler rewrapping it dynamically.
Upvotes: 1