Hi, ALL,
I have kind of weird problem.
I have a wxNotebook. One of the panels there contains wxGrid with 4 columns. When I do a right click I want a pop-up menu to show up. Everything up to this point works fine.
Now when I choose one of the items in that menu I want a wxPanel to be displayed. The bottom of that wxPanel should be where the clicked row is. So it should work just like a wxComboBox except that I don't want the arrow to be displayed and the panel should work like a popup window where I can get a selection and the string I selected will show up in the appropriate cell
My problem is - how do I position the wxPanel?
I can probably figure out the position of the first {visible} row in the wxGrid and set the position of the wxPanel, but what I do with the second+ row?
And am I thinking correctly even for the first row?
Thank you.
P.S.: I hope I explained my problem properly. Let me know if it is not clear enough.
Properly position a panel
- doublemax@work
- Super wx Problem Solver
- Posts: 474
- Joined: Wed Jul 29, 2020 6:06 pm
- Location: NRW, Germany
Re: Properly position a panel
In general i'd recommend to use wxPopupWindow instead of adding a wxPanel to the grid.
The wxGridEvent should contain mouse coordinates relative to the client window. Convert them with CalcGridWindowUnscrolledPosition or CalcGridWindowScrolledPosition (i can never tell which one does what).
https://docs.wxwidgets.org/trunk/classw ... 3cffee65fa
The wxGridEvent should contain mouse coordinates relative to the client window. Convert them with CalcGridWindowUnscrolledPosition or CalcGridWindowScrolledPosition (i can never tell which one does what).
https://docs.wxwidgets.org/trunk/classw ... 3cffee65fa
Re: Properly position a panel
If I understood it correctly, you want to show a panel below the selected grid cell. You will have to do something like the following to determine the screen position of the panel:
PopupMenu is a method of a wxWindow. Therefore the screen position has to be transformed into a client position using ScreenToClient. However, in your case you will probably have to use the screen position directly (without using ScreenToClient), because your panel will be positioned using screen coordinates.
Code: Select all
void GridFrame::OnSelectCell( wxGridEvent& ev )
{
// Get grid object
wxGrid* grid = ((wxGrid*) ev.GetEventObject());
// Determine grid position on screen (position of cell (0,0))
wxPoint gridPosOnScreen = grid->GetScreenPosition();
// Determine relative position of selected cell
wxGridCellCoords selectedCellCoords(ev.GetRow(), ev.GetCol());
wxRect relCellPos = grid->BlockToDeviceRect(selectedCellCoords, selectedCellCoords);
// Determine size of grid corner label window
wxWindow* cornerWin = grid->GetGridCornerLabelWindow();
wxSize cornerSize = cornerWin->GetSize();
// Calculate position of popup menu
wxPoint menuPosition = gridPosOnScreen;
menuPosition.x += (relCellPos.x + cornerSize.GetWidth());
menuPosition.y += (relCellPos.y + relCellPos.height + cornerSize.GetHeight());
ScreenToClient(&menuPosition.x, &menuPosition.y);
// Show popup menu
wxMenu actionMenu;
actionMenu.Append(10101, "Action 1");
actionMenu.Append(10102, "Action 2");
actionMenu.Append(10103, "Action 3");
PopupMenu(&actionMenu, menuPosition);
ev.Skip();
}
Re: Properly position a panel
Ulrich,
No, I want to show it above.
The notebook with grid will be at the bottom of the window (at least initially).
Hence the question of calculating its position.
Thank you.
No, I want to show it above.
The notebook with grid will be at the bottom of the window (at least initially).
Hence the question of calculating its position.
Thank you.
Re: Properly position a panel
doublemax,
Even if I use wxPopu{Transient}Window, I will still need to calculate its position, right?
Or the library will do that for me?
Thank you.
Even if I use wxPopu{Transient}Window, I will still need to calculate its position, right?
Or the library will do that for me?
Thank you.
Re: Properly position a panel
Yes, but it takes screen coordinates, which might be easier to calculate using wxWindow::ClientToScreenEven if I use wxPopu{Transient}Window, I will still need to calculate its position, right?
Use the source, Luke!
Re: Properly position a panel
Well, in that case you need to know the size (especially the height) of the panel, you intend to show above the selected grid cell. Thereafter the approach would be very similar to the one I showed in my previous post.
Code: Select all
// Create panel
wxPanel* panel = new wxPanel(...);
// ...
wxSize panelSize = panel->GetSize();
wxPoint panelPosition = gridPosOnScreen;
panelPosition.x += (relCellPos.x + cornerSize.GetWidth());
panelPosition.y += (relCellPos.y + cornerSize.GetHeight() - panelSize.GetHeight);
panel->SetPosition();
Maybe I haven't understood what you really intend to accomplish, but if you want to show some sort of popup panel above a selected grid cell, calculating the position of the popup panel should be rather straight forward as in the above code snippet.