WxGrid highlighting cells on hover not working as expected

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
james28909
Knows some wx things
Knows some wx things
Posts: 39
Joined: Mon Jul 01, 2019 8:27 pm

WxGrid highlighting cells on hover not working as expected

Post by james28909 »

First off, i am not sure if this is better off in general development, or if it needs to be in platform related issues so apologies in advance if i am in the wrong sub forum.

the problem i am facing is, i have wrote a small function that highlights cells on hover... it works GREAT unless the cell spans multiple columns, then the spanned cells are not highlighted if hovering in the spanned cell area (past the first cell in the span).

the way i am doing this is as follows:

Code: Select all

my $last_col;
my $last_row;

my $id = $grid->GetGridWindow();

EVT_MOTION( $id, sub { onmove( $_[1], $grid, $grid2 ) } ); 

sub onmove {

    my ( $event, $grid, $grid2 ) = @_;

    my ($coords) = $event->GetPosition();

    my ( $x, $y ) = $grid->CalcUnscrolledPosition( $coords->x, $coords->y );

    my $col_pos = $grid->XToCol($x);
    my $row_pos = $grid->YToRow($y);

    #highlight cell on hover!
    if ( $col_pos != $last_col or $row_pos != $last_row ) {

        my $last_label = $grid2->GetCellValue( 0, $last_col ) if defined($last_col);
        $last_label =~ /(\d{2})\/(\d{2})\/(\d{4})\s(\d{2}):(\d{2})/;
        $grid->SetCellBackgroundColour( $last_row, $col_label_lookup{"$3$1$2$4$5"}, Wx::Colour->newRGB( 50, 50, 50 )) if defined($last_row);

        $last_col = $col_pos;
        $last_row = $row_pos;

        my $label = $grid2->GetCellValue( 0, $col_pos );

        $label =~ /(\d{2})\/(\d{2})\/(\d{4})\s(\d{2}):(\d{2})/;
        $grid->SetCellBackgroundColour( $row_pos, $col_label_lookup{"$3$1$2$4$5"}, Wx::Colour->newRGB( 220, 220, 220 ));

        $grid->Refresh;
    }
}
Like i said, if i hover the first cell that is spanned multiple columns/cells, the whole cell span is "highlighted". if i hover in the part of the cell thats spanned, it does not highlight the cell at all. so if a cell has a span of 3 columns, if i hover in the first column of the spanned cell, the whole cell is "highlighted". if i hover over to the second or third column in the same cell span, nothing gets highlighted.

https://imgur.com/a/Ny8myaw - hovering first cell in span

https://imgur.com/a/yJAXPGK - hovering past the first cell in the cell span

i did notice that if the cell is spanned, the built in functions of WxGrid are able to tell if it is spanned... if i select a cell thats spanned it outlines the whole spanned cell. so i need to figure out, hopefully with a little help, how wxgrid is managing to know if that cell is spanned and clone that behaviour.

the lib i am working with does not have GetCellSize built in. when i try to use this function i get an error "GetCellSize not found in Wx::Grid", but somehow the built in functions of WxGrid are able to tell because when i click a cell (in original cell or spanned from original cell), it outlines the whole spanned cell.

EDITED: updated description of the problem and added pictures which hopefully help describe my problem.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: WxGrid highlighting cells on hover not working as expected

Post by doublemax »

Which wxWidgets version does wxPerl use? GetCellSize() is not really new.

This is how the method looks like, you should get the desired information through GetCellAttr()

Code: Select all

wxGrid::CellSpan wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
{
    wxGridCellAttr *attr = GetCellAttr(row, col);
    attr->GetSize( num_rows, num_cols );
    attr->DecRef();

    if ( *num_rows == 1 && *num_cols == 1 )
        return CellSpan_None; // just a normal cell

    if ( *num_rows < 0 || *num_cols < 0 )
        return CellSpan_Inside; // covered by a multi-span cell

    // this cell spans multiple cells to its right/bottom
    return CellSpan_Main;
}
If it's a cell inside a spanned block, (row + *num_rows) will be the row of the "root" cell of the block. Accordingly for the col.
Use the source, Luke!
james28909
Knows some wx things
Knows some wx things
Posts: 39
Joined: Mon Jul 01, 2019 8:27 pm

Re: WxGrid highlighting cells on hover not working as expected

Post by james28909 »

i am pretty positive that the version i am using is Wx-perl 0.69 and the actual wxwidgets version is 2.9.4 so its not to old. is there a resource somewhere that has a list of changes between each revision?

i wish i could figure out how to compile them for activestate perl. there is a quick and short guide but it leaves to many questions to compile them successfully (for me anyways)

ill check out what you posted and will let you know something soon but until then i have one more question about cell attributes. if i am able to get the cells attributes, am i right in thinking i can call 'GetSize();' on the attributes to get the actual size of the cell?

what i really need is like a get cell origin function. that way when i hover over column 3 in a row it will tell me the origin of the spanned cell is column 1.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: WxGrid highlighting cells on hover not working as expected

Post by doublemax »

what i really need is like a get cell origin function. that way when i hover over column 3 in a row it will tell me the origin of the spanned cell is column 1.
Yes, i explained that above. The "num_rows" and "num_cols" values give you all information about spanned cells.

If you call this for the upper left cell of a span block, it will return the size (in cells) of the spanned area. E.g (1,3) for a block than spans 3 cells horizonally.

But if this is called for any other cell of a span block, these values will return the negative distance (in cells) to the "root" cell, the upper left cell of the block.

Suppose you have a block of 3 cells spanned horizontally. The returned "num_rows" and "num_cols" values for the 3 cells will be:
First cell: 1,3 - because it spans over 1 row and 3 cells
Second cell: 0, -1 - That's the negative distance to the first cell
Third cell: 0, -2 - That's the negative distance to the first cell
Use the source, Luke!
james28909
Knows some wx things
Knows some wx things
Posts: 39
Joined: Mon Jul 01, 2019 8:27 pm

Re: WxGrid highlighting cells on hover not working as expected

Post by james28909 »

well i tried to implement this but it seems the function GetCellSize, GetCellAttr and $attr->GetSize are not supported by my version of wxperl. i do know citrus perl has a updated (on latest stable release 3.0.4). i think im going to download that and try again because something seems to be off with this lib i am currently using. there is literally no documentation on wxperl, like supported functions ect.

one function that i can call is '$grid->GetOrCreateCellAttr($row_pos, $col_pos);' but alas, when i try to call $attr->GetSize() it failed with error 'cannot find getsize via package Wx::GridCellAttr' so its like it just isnt going to let me get the cell size lol.

i can keep track of the cell spans myself when i am building the data structure for the grid, but it sure would be cool if i could just call a built in function.

EDIT: ive installed citrus perl and it has wxwidgets 3.0.2 built in. i exported the functions list of WxGrid and WxGridTableBase and i do not see GetCellAttr or GetCellSize or GetSize listed.

here is exported functions for Wx::GridCellAttr:

Code: Select all

SetRenderer
DESTROY
SetDefAttr
IsReadOnly
HasFont
SetEditor
GetBackgroundColour
GetEditor
SetTextColour
SetFont
GetOverflow
HasRenderer
DecRef
SetOverflow
GetFont
GetRenderer
GetTextColour
HasEditor
HasAlignment
IncRef
SetReadOnly
SetAlignment
new
HasBackgroundColour
CLONE
GetAlignment
HasTextColour
SetBackgroundColour
here is exported functions for Wx::Grid:

Code: Select all

GetSelectedCols
ProcessTableMessage
HideCellEditControl
GetViewWidth
SetColAttr
SetCellEditor
SetCellRenderer
GetDefaultCellTextColour
MakeCellVisible
SetColFormatBool
SetSelectionBackground
DisableDragColMove
SetDefaultCellFont
GridLinesEnabled
GetColLabelAlignment
GetGridRowLabelWindow
ShowCellEditControl
EnableDragRowSize
GetCellAlignment
GetSelectionBlockBottomRight
AutoSizeColumn
GetBatchCount
GetSelectionBlockTopLeft
GetSelectionBackground
GetRowLabelAlignment
AutoSizeRowLabelSize
SetCellFont
SetCellValueXY
ClearGrid
GetCellRenderer
GetDefaultRenderer
GetRowGridLinePen
RegisterDataType
CanDragGridSize
GetGridCursorRow
GetCellTextColour
SetTable
MakeCellVisibleXY
newDefault
GetColGridLinePen
SetRowMinimalHeight
CanDragColMove
GetOrCreateCellAttr
MoveCursorDownBlock
CellToRect
GetCellValueCo
MoveCursorRightBlock
Create
IsVisibleCo
GetDefaultEditorForType
IsInSelectionCo
EnableCellEditControl
SelectBlockXYWH
SetScrollLineY
SetDefaultCellBackgroundColour
SelectBlockPP
GetDefaultCellBackgroundColour
AutoSize
SetRowLabelSize
GetDefaultGridLinePen
DisableDragGridSize
IsVisibleXY
SetCellTextColour
GetCellValue
HideRowLabels
GetDefaultEditorForCellCo
GetDefaultCellOverflow
IsReadOnly
YToEdgeOfRow
IsSelection
HideColLabels
AutoSizeColLabelSize
SetRowAttr
SetColMinimalWidth
SetCellValue
SetCellAlignment
AppendCols
EndBatch
IsVisible
GetDefaultCellAlignment
GetDefaultColLabelSize
GetColLabelSize
DeleteCols
MoveCursorRight
SetEvents
GetTable
GetScrollLineY
SetCellOverflow
GetGridColLabelWindow
GetRowSize
SetSelectionForeground
SetDefaultCellTextColour
EnableDragColMove
GetDefaultRendererForCell
GetLabelFont
BeginBatch
GetSelectionMode
DisableDragColSize
AutoSizeRow
GetDefaultEditor
AutoSizeRows
SetRowLabelAlignment
AutoSizeColumns
SetColLabelSize
DisableCellEditControl
YToRow
SetSelectionMode
CreateGrid
SelectRow
ForceRefresh
EnableEditing
BlockToDeviceRect
SetGridCursor
GetLabelBackgroundColour
InsertCols
IsCurrentCellReadOnly
SetDefaultRowSize
MakeCellVisibleCo
GetColLabelValue
SetRowLabelValue
SetLabelFont
SetColLabelValue
MoveCursorDown
SetLabelTextColour
GetSelectionForeground
SetScrollLineX
CanDragRowSize
SetCellBackgroundColour
GetDefaultRowLabelSize
GetCellEditor
SetGridLineColour
IsInSelectionXY
EnableDragGridSize
GetDefaultEditorForCell
SetLabelBackgroundColour
SetCellHighlightColour
SetColFormatCustom
SetRowMinimalAcceptableHeight
IsEditable
GetCellFont
SetDefaultEditor
SetReadOnly
GetLabelTextColour
new
SetColMinimalAcceptableWidth
GetNumberCols
EnableDragColSize
GetColSize
SaveEditControlValue
GetColAt
DeleteRows
DisableDragRowSize
SetCellHighlightPenWidth
MovePageUp
GetGridCursorCol
SelectBlock
CellToRectCo
SetUseNativeColLabels
EnableGridLines
GetGridLineColour
XToEdgeOfCol
GetDefaultColSize
GetScrollLineX
GetGridWindow
newFull
GetCellValueXY
SetCellValueCo
InsertRows
SetDefaultRenderer
CanEnableCellControl
GetDefaultRendererForType
ClearSelection
GetDefaultCellFont
SelectAll
GetCellBackgroundColour
GetRowMinimalAcceptableHeight
GetDefaultEditorForCellXY
SetDefaultCellOverflow
GetNumberRows
IsInSelection
AppendRows
SetRowSize
SetColSize
IsCellEditControlEnabled
GetSelectedCells
SetColFormatFloat
SelectCol
GetRowLabelValue
CanDragColSize
GetDefaultRowSize
SetColFormatNumber
SetColLabelAlignment
SetCellHighlightROPenWidth
GetSelectedRows
XToCol
GetColMinimalAcceptableWidth
MoveCursorUp
SetDefaultCellAlignment
SetColPos
MoveCursorLeft
CellToRectXY
MoveCursorUpBlock
SetCellSize
SetDefaultColSize
bootstrap
SetMargins
MoveCursorLeftBlock
GetColPos
MovePageDown
GetCellOverflow
GetRowLabelSize
here is a list of exported functions via Wx::GridTableBase:

Code: Select all

GetAttr
GetValue
GetNumberRows
GetRowsCount
SetValue
SetRowAttr
SetValueAsBool
Clear
AppendRows
SetView
IsEmptyCell
GetAttrProvider
DeleteRows
GetColLabelValue
DeleteCols
AppendCols
SetRowLabelValue
GetRowLabelValue
InsertCols
SetAttr
GetValueAsDouble
CanSetValueAs
GetNumberCols
GetColsCount
SetValueAsDouble
SetAttrProvider
GetTypeName
GetValueAsLong
SetColAttr
GetValueAsBool
InsertRows
Destroy
SetValueAsLong
SetColLabelValue
IsEmpty
GetView
CanGetValueAs
CanHaveAttributes
i also have contacted the maintainer, multiple times spanning back about 10 months with no response at all. so maybe i should just abandon this wxperl hit and miss crap and just use the C++ source. :l
james28909
Knows some wx things
Knows some wx things
Posts: 39
Joined: Mon Jul 01, 2019 8:27 pm

Re: WxGrid highlighting cells on hover not working as expected

Post by james28909 »

i was able to fix this by keeping track of the col_spans myself. i created a hash lookup for col_spans for each row, and seems to have worked lovely. thanks
Post Reply