tags:

views:

924

answers:

3

I'm still getting used to the sizers in wxWidgets, and as such can't seem to make them do what I want...

Basicly I wantt a large panel that will contain a list of other panels/boxes, which each then contain a set of text fields

----------------------
| label    text box  |
| label2   text box2 |
----------------------
----------------------
| label    text box  |
| label2   text box2 |
----------------------
----------------------
| label    text box  |
| label2   text box2 |
----------------------

I also need to be able to add (at the end), and remove(anywhere) these boxes. If theres too many to fit in the containing panel a vertical scroll bar is also required.

This is what ive tried so far, it works for the first box thats created with the containing panel, but additional added items are just a small box in the top left of the main panel, even though the sizer code is the same for all boxes...

//itemsList is a container containg a list of *Item pointers to each box/panel/whatever the right name is
Items::Items(wxWindow *parent)
:wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_SUNKEN)
{
    //one sstarting item
    OnAdd(wxCommandEvent());
}

void Items::OnAdd(wxCommandEvent &event)
{
    unsigned id = itemsList .size();
    Item *item = new Item(this,id);
    itemsList .push_back(item);

    RebuildSizer();
}
void Items::RebuildSizer()
{
    this->SetSizer(0,true);
    wxBoxSizer *sizerV = new wxBoxSizer(wxVERTICAL);

    for(std::vector<Item*>::iterator it = itemsList .begin(); it != itemsList .end(); ++it)
        sizerV->Add(*it, 1, wxEXPAND | wxLEFT | wxRIGHT, 5);

    SetSizer(sizerV);
}
void Items::OnRemove      (wxCommandEvent &event, unsigned itemId)
{
    delete itemsList [itemId];
    itemsList .erase(items.begin()+itemId);
    for(std::vector<Item*>::iterator it = itemsList .begin()+itemId; it != itemsList .end(); ++it)
        (*it)->ChangeId(itemId++);

    RebuildSizer();
}

Also whats the best way to lay out the contents of each box? I was thinking of using a 2 by 2 grid sizer, box im not sure how to make the text boxes to expand to be as wide as possible while making the labels stay as small as possible (but also mantaing the alignment between the 2 text boxes)?

+1  A: 

"If theres too many to fit in the containing panel a vertical scroll bar is also required."

You could have a look at wxScrolledWindow.

"additional added items are just a small box in the top left of the main panel"

I am not sure, but, maybe a call to wxSizer::Layout() will help.

"Also whats the best way to lay out the contents of each box?"

Have a look at this sizerdemo. If it is not mandatory, that the labels stay as small as possible, you could give the labels a fixed width and only let the text boxes grow. If you want to adapt the size when adding or removing new boxes, you could implement the OnSize() event handler.

Ralph
A: 

May I suggest to you to post this question to one of the wxWidgets mailing list? They will be able to help you very quickly.

BugSlayer
A: 

Can I also recommend the wxForum, I have found it very useful for wxWidgets questions in the past.

More specifically for scrolling wxScrolledWindow should help, use wxScrolledWindow->SetSizer with your top level sizer (the one that contains your controls) to set up a scrolled area, also check out the samples called scroll, scrollsub and vscroll included in wxWidgets (in the samples directory of your wxwidgets install).

SteveL