Author Topic: tree->GetSelection not working anymore on rev7548  (Read 8033 times)

Offline daniloz

  • Regular
  • ***
  • Posts: 268
tree->GetSelection not working anymore on rev7548
« on: November 03, 2011, 02:18:26 pm »
Hi,

I was developing a plugin and since one week, the part that gets the selected item on the "Project Manager" is not working anymore.
I have a pretty much standard code, as in other places in C::B and other plugins, i.e.:
Code
void cbTree::OnContextMenu( wxCommandEvent& /*event*/ )
{
    wxTreeCtrl* tree = Manager::Get()->GetProjectManager()->GetTree();
    if ( !tree )
        return;

    wxTreeItemId selItem = tree->GetSelection();
    if ( !selItem.IsOk() )
        return;

    const FileTreeData* ftData = static_cast<FileTreeData*>( tree->GetItemData( selItem ) );
    if ( !ftData )
        return;

    if (ftData->GetKind() != FileTreeData::ftdkFile)
        return;

    ProjectFile* pFile = ftData->GetProjectFile();
    if ( !pFile )
        return;

    // Do something with the pFile here //
    return;
}

After adding some verbosity to the code and debugging, I figured out that
Code
wxTreeItemId selItem = tree->GetSelection();
is returning a NULL pointer.

Any idea what has changed in the last week or so that could break this...

Btw, I checked the code completion plugin, and the part where is does a "Reparse this Project" (void NativeParser::ReparseSelectedProject()) is also not working for the same reason...

Attached is a simple project to reproduce the problem.

[attachment deleted by admin]
« Last Edit: November 03, 2011, 02:36:05 pm by daniloz »

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: tree->GetSelection not working anymore on rev7548
« Reply #1 on: November 03, 2011, 02:57:31 pm »
Sorry to reply to my own post, but I have some information about this issue:

If instead of
Code
    wxTreeItemId selItem = tree->GetSelection();
I use
Code
    wxTreeItemId selItem = Manager::Get()->GetProjectManager()->GetTreeSelection();

Then everything is working...

It seems something was broken in tree->GetSelection()
« Last Edit: November 03, 2011, 10:06:50 pm by daniloz »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: tree->GetSelection not working anymore on rev7548
« Reply #2 on: November 03, 2011, 03:05:20 pm »
Same problem here reparse project or reparse file does not woks any more, all the recent nightly builds has this issue.

Edit: It should be commit before rev 6546.
« Last Edit: November 03, 2011, 03:16:00 pm by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: tree->GetSelection not working anymore on rev7548
« Reply #3 on: November 03, 2011, 09:50:39 pm »
The reason is quite simple:
Quote from: wxWidgets documentation
wxTreeCtrl::GetSelection

wxTreeItemId GetSelection() const

Returns the selection, or an invalid item if there is no selection. This function only works with the controls without wxTR_MULTIPLE style, use GetSelections for the controls which do have this style.

wxTR_MULTIPLE was added to the projects tree in revision 7531.

Quote from: projectmanager.h
        /** Get the selection of the project manager's tree (GUI).
          * This must be used instead of tree->GetSelection() because the tree
          * has the wxTR_MULTIPLE style.
          * This usually returns the first item in the selection list, but
          * if there is a right-click popup menu then the user may have
          * selected several items and right-clicked on one, so return the
          * right-click item instead.
          * of the first
          * @return A wxTreeItemId of the selected tree item.
          */
        wxTreeItemId GetTreeSelection();

So all code that uses
Code
wxTreeCtrl::GetSelection()
has to be fixed to use
Code
ProjectManager::GetTreeSelection()
instead.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: tree->GetSelection not working anymore on rev7548
« Reply #4 on: November 04, 2011, 01:52:07 am »
Thank you jens!!!
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: tree->GetSelection not working anymore on rev7548
« Reply #5 on: November 04, 2011, 09:52:50 am »
So all code that uses
Code
wxTreeCtrl::GetSelection()
has to be fixed to use
Code
ProjectManager::GetTreeSelection()
instead.
That is certainly true. It seems I missed one position, where this applies, too. I'll correct this once I find the time...

If instead of
Code
    wxTreeItemId selItem = tree->GetSelection();
I use
Code
    wxTreeItemId selItem = Manager::Get()->GetProjectManager()->GetTreeSelection();
That would be the right thing to do...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: tree->GetSelection not working anymore on rev7548
« Reply #6 on: November 04, 2011, 10:10:55 am »
@jens and @MortenMacFly:
Thanks very much for your answers!!!

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: tree->GetSelection not working anymore on rev7548
« Reply #7 on: November 04, 2011, 10:27:57 am »
@jens and @MortenMacFly:
Thanks very much for your answers!!!
BTW: It's committed meanwhile. I've missed at least three positions, actually... :?
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ