Adding TextCtrl to Every New Page WxNotebook

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Adding TextCtrl to Every New Page WxNotebook

Post by gillp28 » Tue Jul 23, 2019 5:59 pm

I am trying to add the text ctrl to every new page that gets added. When the button is pressed.

Code: Select all

const long wxTreeCtrlFrame::ID_ADDNODE = wxNewId();
const long wxTreeCtrlFrame::ID_DeleteNode = wxNewId();
const long wxTreeCtrlFrame::ID_EditNode = wxNewId();
const long wxTreeCtrlFrame::ID_ChoiceBox = wxNewId(); 
const long wxTreeCtrlFrame::ID_Notes = wxNewId(); 
const long wxTreeCtrlFrame::ID_AddPage = wxNewId();
const long wxTreeCtrlFrame::ID_DeletePage = wxNewId();
const long wxTreeCtrlFrame::ID_BOLD = wxNewId(); 
const long wxTreeCtrlFrame::ID_ITALIC = wxNewId(); 
const long wxTreeCtrlFrame::ID_FORMAT_ALIGN_LEFT = wxNewId();
const long wxTreeCtrlFrame::ID_FORMAT_ALIGN_RIGHT = wxNewId();
const long wxTreeCtrlFrame::ID_FORMAT_ALIGN_CENTRE = wxNewId();
const long wxTreeCtrlFrame::ID_UNDERLINE = wxNewId();
const long wxTreeCtrlFrame::ID_FONT = wxNewId();

wxBoxSizer* sizer;

wxRichTextCtrl* textCtrl;

//*)

typedef std::vector<std::pair<int, std::string>> data_structure;
std::vector<data_structure> list;
wxNotebook* notes; 

BEGIN_EVENT_TABLE(wxTreeCtrlFrame, wxFrame)
//(*EventTable(wxTreeCtrlFrame)
//*)
END_EVENT_TABLE()

wxTreeCtrlFrame::wxTreeCtrlFrame(wxWindow* parent, wxWindowID id)
{
	Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
	SetClientSize(wxSize(500, 500));

	wxPanel* notePanel = new wxPanel(this, wxID_ANY);
	//wxPanel* toolPanel = new wxPanel(this, wxID_ANY);
	wxBoxSizer* mainSizer = new wxBoxSizer(wxHORIZONTAL);
	wxBoxSizer* verticalSizer = new wxBoxSizer(wxVERTICAL); 
	wxBoxSizer* verticalSizer2 = new wxBoxSizer(wxVERTICAL); 
	wxBoxSizer* horSize = new wxBoxSizer(wxHORIZONTAL);
	sizer = new wxBoxSizer(wxVERTICAL);

	notes = new wxNotebook(notePanel, ID_Notes, wxDefaultPosition, wxDefaultSize, wxNB_TOP);

	//wxColour col = notes->GetThemeBackgroundColour();
	wxButton* addPage = new wxButton(notePanel, ID_AddPage, ("Add Note"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_AddPage"));
	wxButton* deletePage = new wxButton(notePanel, ID_DeletePage, ("Delete Note"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_DeletePage"));
	wxNotebookPage* page = new wxNotebookPage(notes, -1); 
	page->SetBackgroundColour(wxColour(*wxWHITE));

	textCtrl = new wxRichTextCtrl(page,wxID_ANY, ("Add Note"), wxDefaultPosition, wxDefaultSize, wxVSCROLL | wxHSCROLL | wxNO_BORDER | wxWANTS_CHARS, wxDefaultValidator);


	//bitmap 
	wxBitmap bitmap;
	bitmap.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/bold.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* bold = new wxBitmapButton(notePanel,ID_BOLD, bitmap, wxDefaultPosition,wxDefaultSize, 0);
	
	wxBitmap bitmap1;
	bitmap1.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/italic.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* italic = new wxBitmapButton(notePanel, ID_ITALIC, bitmap1, wxDefaultPosition, wxDefaultSize, 0);

	wxBitmap bitmap2;
	bitmap2.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/underline.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* underline = new wxBitmapButton(notePanel, ID_UNDERLINE, bitmap2, wxDefaultPosition, wxDefaultSize, 0);
	
	wxBitmap bitmap3;
	bitmap3.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/centre.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* alignCentre = new wxBitmapButton(notePanel, ID_FORMAT_ALIGN_CENTRE, bitmap3, wxDefaultPosition, wxDefaultSize, 0);

	wxBitmap bitmap4;
	bitmap4.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/alignleft.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* alignLeft = new wxBitmapButton(notePanel, ID_FORMAT_ALIGN_LEFT, bitmap4, wxDefaultPosition, wxDefaultSize, 0);
	
	wxBitmap bitmap5;
	bitmap5.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/alignright.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* alignRight = new wxBitmapButton(notePanel, ID_FORMAT_ALIGN_RIGHT, bitmap5, wxDefaultPosition, wxDefaultSize, 0);

	wxBitmap bitmap6;
	bitmap6.LoadFile("C:/Users/pavne/Downloads/wxWidgets/samples/richtext/bitmaps/font.xpm", wxBITMAP_TYPE_XPM);
	wxBitmapButton* font = new wxBitmapButton(notePanel, ID_FONT, bitmap6, wxDefaultPosition, wxDefaultSize, 0);
	
	//sizer->Add(toolBar,1,wxEXPAND,2);
	sizer->Add(textCtrl, 1, wxEXPAND | wxALL, 2);
	page->SetSizer(sizer);
	notes->AddPage(page, L"Note 1"); 
	//notes->AddPage(page, L"Note 2");

	wxRichTextAttr attr;
	
	mainSizer->Add(verticalSizer, 10, wxEXPAND | wxALL, 4);

	mainSizer->Add(verticalSizer2,1, wxTOP,26);
	verticalSizer2->Add(bold, 1, wxTOP, 5);
	verticalSizer2->Add(italic, 1, wxTOP, 5);
	verticalSizer2->Add(underline, 1, wxTOP, 5);
	verticalSizer2->Add(alignLeft, 1, wxTOP, 5);
	verticalSizer2->Add(alignCentre, 1, wxTOP, 5);
	verticalSizer2->Add(alignRight, 1, wxTOP, 5);
	verticalSizer2->Add(font, 1, wxTOP, 5);



	verticalSizer->Add(notes, 1, wxEXPAND | wxALL, 4);
	horSize->Add(addPage, 2, wxEXPAND | wxALL, 2); 
	horSize->Add(deletePage, 2, wxEXPAND | wxALL, 2); 
	verticalSizer->Add(horSize, 0, wxALIGN_TOP | wxALIGN_RIGHT | wxTOP | wxRIGHT | wxBOTTOM, 4);
	notePanel->SetSizer(mainSizer);

	Connect(ID_AddPage, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)& wxTreeCtrlFrame::CreatePage);
	Connect(ID_DeletePage, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)& wxTreeCtrlFrame::DeletePage); 
	Connect(ID_BOLD, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::bold);
	Connect(ID_ITALIC, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::italic);
	Connect(ID_UNDERLINE, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::underline);
	Connect(ID_FORMAT_ALIGN_LEFT, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::OnUpdateAlignLeft);
	Connect(ID_FORMAT_ALIGN_CENTRE, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::OnUpdateAlignCentre);
	Connect(ID_FORMAT_ALIGN_RIGHT, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::OnUpdateAlignRight);
	Connect(ID_FONT, wxEVT_BUTTON, (wxObjectEventFunction)& wxTreeCtrlFrame::OnFont); 
}


wxTreeCtrlFrame::~wxTreeCtrlFrame()
{
	//(*Destroy(wxTreeCtrlFrame)
	//*)
}

void wxTreeCtrlFrame::CreatePage(wxCommandEvent& event)
{
	size_t count = notes->GetPageCount() + 1;
	wxNotebookPage* page = new wxNotebookPage(notes, -1); 
	page->SetSizer(sizer);
	notes->AddPage(page, L"Note " + std::to_string(count));
	notes->ChangeSelection(count-1); 
}

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

Re: Adding TextCtrl to Every New Page WxNotebook

Post by doublemax » Tue Jul 23, 2019 6:45 pm

Code: Select all

wxBoxSizer* sizer;
You set it as sizer for the first notebook page and add a text control to it. This is ok.

Code: Select all

page->SetSizer(sizer);
But then you use the same sizer for other notebook pages. This doesn't work.

It seems like you want that one text control to appear on all notebook pages without duplicating it. This is not possible.
You can either create a new text control for each page or you move the text control outside the notebook.
Use the source, Luke!

gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Adding TextCtrl to Every New Page WxNotebook

Post by gillp28 » Tue Jul 23, 2019 8:38 pm

I have added this recently and it works to create a new text ctrl. But i am unable to access the textctrl of the new page created. So I tried to add a vector list of RichTextCtrl but it gives me errors every time I try to append to it.


Code: Select all

std::vector<wxRichTextCtrl> textCtrlList; 

void wxTreeCtrlFrame::CreatePage(wxCommandEvent& event)
{
	size_t count = notes->GetPageCount() + 1;
	wxNotebookPage* page = new wxNotebookPage(notes, -1); 
	wxRichTextCtrl* newTextCtrl = new wxRichTextCtrl(page, wxID_ANY, ("Add Note"), wxDefaultPosition, wxDefaultSize, wxVSCROLL | wxHSCROLL | wxNO_BORDER | wxWANTS_CHARS, wxDefaultValidator);
	textCtrlList.push_back(newTextCtrl); // error line
	wxBoxSizer* newSizer = new wxBoxSizer(wxHORIZONTAL);
	newSizer->Add(newTextCtrl, 1, wxEXPAND | wxALL, 2);
	page->SetSizer(newSizer); 
	notes->AddPage(page, L"Note " + std::to_string(count));
	notes->ChangeSelection(count-1); 
}


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

Re: Adding TextCtrl to Every New Page WxNotebook

Post by doublemax » Tue Jul 23, 2019 8:49 pm

Code: Select all

textCtrlList.push_back(newTextCtrl); // error line
What kind of error, at compile time or run time. How is textCtrlList declared?
Use the source, Luke!

gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Adding TextCtrl to Every New Page WxNotebook

Post by gillp28 » Tue Jul 23, 2019 8:58 pm

attempting to reference a deleted function
'wxRichTextCtrl::wxRichTextCtrl(const wxRichTextCtrl &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'wxControl::wxControl(const wxControl &)

that's just by adding the one line other than that it works fine. I declared it at the top of the program textCtrlList.

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

Re: Adding TextCtrl to Every New Page WxNotebook

Post by doublemax » Tue Jul 23, 2019 9:07 pm

What type is textCtrlList?

If should work if it's defined like this:

Code: Select all

std::vector<wxRichTextCtrl *> textCtrlList;
Use the source, Luke!

Post Reply