Toolbar trouble

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
Peloquin
Earned a small fee
Earned a small fee
Posts: 15
Joined: Thu Mar 15, 2018 3:05 pm

Toolbar trouble

Post by Peloquin »

Hi,

I'm tryin' to create a toolbar as in image below.
Clipboard01.jpg
Clipboard01.jpg (10.29 KiB) Viewed 837 times
This image was taken from DialogBlocks, this is the compiled one:
Clipboard02.jpg
Clipboard02.jpg (8.24 KiB) Viewed 837 times
I believe that was a DialogBlocks fault so I contact Dialog Block's author for answers.
He gently answer me that it's unusual to have static text controls and wxButtons in a toolbar but in theory it should be possible;
also it's unusual having toolbars in dialogs, but again sizers should in theory be able to cope.

So I'll try to create a toolbar from scratch using a panel and stardard controls but I've failed.
Clipboard03.jpg
Clipboard03.jpg (10.83 KiB) Viewed 837 times
I'm in trouble with sizer stuff (and wxWidgets in general )
As you can see there are some bugs:

- the panel do not extend to width of frame
- the wxStaticLine is not showed ( there's a small pixel on the left of X button)
- the list do not appears at all ( should be just below the panel )

How can I fix that ?
I haven't understand sizers ( and I've read about it from documentation and some tutorials)
For example, if I create an horizontal wxBoxSize and add two buttons on it,
one with wxLEFT style, the other with wxRIGHT style.
Why if I resize window, the right button do not move ?
I expect that right button should be aligned with width of window.

Thank you,

this is the code I use for toolbar stuff:

Code: Select all

wxBoxSizer* vSizer = new wxBoxSizer(wxVERTICAL);
frame->SetSizer(vSizer);

wxPanel* toolbar = new wxPanel(this, ID_TOOLBAR);
toolbar->SetFont(*g_ToolbarFont);
vSizer->Add(toolbar);

wxBoxSizer* hSizer = new wxBoxSizer(wxHORIZONTAL);
toolbar->SetSizer(hSizer);

wxBitmapButton* btnNew = new wxBitmapButton(toolbar, ID_TOOL_NEW, newBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW);
hSizer->Add(btnNew, 0, wxALIGN_CENTER_VERTICAL, 0);

wxBitmapButton* btnModify = new wxBitmapButton(toolbar, ID_TOOL_MODIFY, modifyBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW);
hSizer->Add(btnModify, 0, wxALIGN_CENTER_VERTICAL, 0);

wxBitmapButton* btnErase = new wxBitmapButton(toolbar, ID_TOOL_ERASE, eraseBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW);
hSizer->Add(btnErase, 0, wxALIGN_CENTER_VERTICAL, 0);

wxStaticLine* line1 = new wxStaticLine(toolbar, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL);
hSizer->Add(line1, 0, wxALIGN_CENTER_VERTICAL, 10);

wxStaticText* form_text = new wxStaticText(toolbar, wxID_STATIC, _("   Item:   "), wxDefaultPosition, wxDefaultSize, 0 );
hSizer->Add(form_text, 0, wxALIGN_CENTER_VERTICAL, 5);

m_comboList = new wxComboBox(toolbar, ID_COMBO_LIST, szComboListContent[g_combo_list_idx], wxDefaultPosition, wxSize(100, -1), WXSIZEOF(szComboListContent), szComboListContent, wxCB_READONLY|wxTE_PROCESS_ENTER);
hSizer->Add(m_comboList, 0, wxALIGN_CENTER_VERTICAL, 5);

wxStaticText* search_text = new wxStaticText(toolbar, wxID_STATIC, _("   Find   "), wxDefaultPosition, wxDefaultSize, 0 );
hSizer->Add(search_text, 0, wxALIGN_CENTER_VERTICAL, 5);

wxTextCtrl* search = new wxTextCtrl(toolbar, ID_SEARCH_TXT, wxEmptyString, wxDefaultPosition, wxSize(150, -1), wxTE_PROCESS_ENTER);
hSizer->Add(search, 0, wxALIGN_CENTER_VERTICAL, 5);

wxStaticText* search_in = new wxStaticText(toolbar, wxID_STATIC, _("   in   "), wxDefaultPosition, wxDefaultSize, 0 );
hSizer->Add(search_in, 0, wxALIGN_CENTER_VERTICAL, 5);

m_comboSearch = new wxComboBox(toolbar, ID_COMBO_SEARCH, szComboSearchItemContent[g_combo_search_idx], wxDefaultPosition, wxSize(200, -1), 0, NULL, wxCB_READONLY|wxTE_PROCESS_ENTER);
hSizer->Add(m_comboSearch, 0, wxALIGN_CENTER_VERTICAL, 5);

wxBitmapButton* btnSearch = new wxBitmapButton(toolbar, ID_SEARCH_BTN, wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
hSizer->Add(btnSearch, 0, wxALIGN_CENTER_VERTICAL, 5);

// list
{
	wxBitmap bmps[2];
	wxArrayString pngs;

	bmps[0] = wxBitmap(GetBitmapFromMemory(plus_png, LOGO_PNG_SIZE, wxBITMAP_TYPE_PNG));
	bmps[1] = wxBitmap(GetBitmapFromMemory(minus_png, LOGO_PNG_SIZE, wxBITMAP_TYPE_PNG));

	pngs.reserve(2);
	pngs.Add("plus.png");
	pngs.Add("minus.png");	

	m_list = new HtmlListBoxEx(frame, ID_LIST, bmps, pngs);
	m_list->SetBackgroundColour(LIST_BACK_COLOR);
	m_list->SetSelectionBackground(LIST_SELECTION_COLOR);
	vSizer->Add(m_list);
}
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Toolbar trouble

Post by doublemax »

I see 3 controls with hard-coded widths in the code. I guess the window on the "bad" screenshot is just too small to fit all controls while obeying the minimum size of these controls, so they partly overlap because the layout algorithm didn't know what to do.
the panel do not extend to width of frame

Code: Select all

vSizer->Add(toolbar);
Add tbe wxEXPAND flag when adding the toolbar.
For example, if I create an horizontal wxBoxSize and add two buttons on it,
one with wxLEFT style, the other with wxRIGHT style.
Why if I resize window, the right button do not move ?
That's a common misconception. In a horizontal sizer, the wxLEFT and wxRIGHT flags to nothing. They are used to align controls in a vertical sizer, if the controls are smaller than the widest item in the sizer.

In a horizontal sizer you would add one control, then add a wxStretchSpacer and then the second control. Then the second control would always be on the right edge.

http://neume.sourceforge.net/sizerdemo/
Use the source, Luke!
Peloquin
Earned a small fee
Earned a small fee
Posts: 15
Joined: Thu Mar 15, 2018 3:05 pm

Re: Toolbar trouble

Post by Peloquin »

Thank you for the hints and for the link!
Now the panel extend to parent width but I'm unable to see list control under panel!

I cannot find wxStretchSpacer...is a class ??
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Toolbar trouble

Post by doublemax »

I cannot find wxStretchSpacer...is a class ??
Sorry, my mistake. I meant wxSizer::AddStretchSpacer()
Use the source, Luke!
Post Reply