Neither one nor Many

 
November 26 2010

There are a few things I figured out how to do, and I seem to use them on a regular basis. For these snippets I have to search through my projects every now and then, to find them, and it would be a lot easier if I kept them somewhere listed. Hopefully you will find something useful as well.

Clearing a sizer

I usually make master/detail screens where the detail list is a vertical sizer with stuff on it. Usually a panel with more stuff on it. Every now and then you may want to clear it. I use:

while (theSizer->GetChildren().GetCount() > 0) {
    theSizer->GetItem(static_cast<size_t>(0))->DeleteWindows();
    theSizer->Remove(0);
}

// populate the sizer again maybe

sizerChangelogItems->Layout();

You might want to surround the code with Freeze() and Thaw() for performance.

Hiding and showing a panel on an AUI frame

This is more of a note I keep forgetting about: Instead of calling the Hide() and Show() on the window (panel) itself (which compiles). Use the following:

someWindow->GetAuiManager().GetPane(someWindow->splitterwindow1).Show(false);
someWindow->GetAuiManager().GetPane(someWindow->panel1).Show(true);

someWindow->GetAuiManager().Update();

Using dropdowns with object data.

I use wxChoice a lot, I especially like that the items themselves can refer to anything. Typically how I initialize a choicebox:

wxChoice *choice = ...;
Object *someObject = ...;
for (...) {
    choice->Append("object 1", (void *)someObject);
}
choice->SetStringSelection("object 1");

Then usually I have an event that triggers on the selection of an item in the choicebox. In the event I first get the wxChoice, then cast the client data for the selected item to the object pointed to:

void SomeWindow::OnChoiceSelected( wxCommandEvent& event )
{
    wxChoice *choice = static_cast<wxChoice *>(event.GetEventObject());
    Object *someObject = static_cast<Object *>(choice->GetClientData(choice->GetSelection()));
    if (someObject != NULL) {
        //...
    }
}

Directory functions

wxString resdir(RESOURCE_DIR);
if (!::wxDirExists(resdir)) {
    throw std::runtime_error("resources dir not found");      
}
wxDir dir(resdir);
if ( !dir.IsOpened() ) {
    throw std::runtime_error("resources directory could not be opened");
}
wxString filename;
bool cont = dir.GetFirst(&filename);
while ( cont ) {
    int lastdot = filename.Find('.', true);
    if (lastdot != wxNOT_FOUND) {
        wxString name(filename.SubString(0, lastdot - 1));
        wxString ext(filename.SubString(lastdot + 1, filename.Length()));
        if (!ext.compare(_T("cpp"))) {
            ...
        } else {
            ...
        }
    }
    cont = dir.GetNext(&filename);
}

Write to file function

wxFile article(markdownfilename, wxFile::write);
if (!article.IsOpened()) {
    throw std::runtime_error("main.md could not be opened for writing");
}
article.Write(textctrlArticleBody->GetValue());
article.Close();
C++ Comments (0)


Leave a Reply

Comment may not be visible immediately, because I process everything manually.**

**) I plan to automate this.., but it's on my ToDo since for ever..


Author:
Ray Burgemeestre
february 23th, 1984

Topics:
C++, Linux, Webdev

Other interests:
Music, Art, Zen