use strict; use Win32::GUI; use Win32::GUI::Grid; # main Window my $Window = new Win32::GUI::Window ( -title => "Win32::GUI::Grid", -pos => [100, 100], -size => [400, 400], -name => "Window", ) or die "new Window"; # Grid Window my $Grid = $Window->AddGrid ( -name => "Grid", -pos => [0, 0], -rows => 50, -columns => 10, -fixedrows => 1, -fixedcolumns => 1, -editable => 1, ) or die "new Grid"; # Fill Grid for my $row (0..$Grid->GetRows()) { for my $col (0..$Grid->GetColumns()) { if ($row == 0) { $Grid->SetCellText($row, $col,"Column : $col"); } elsif ($col == 0) { $Grid->SetCellText($row, $col, "Row : $row"); } else { $Grid->SetCellText($row, $col, "Cell : ($row,$col)"); } } } # Resize Grid Cell $Grid->AutoSize(); # Event loop $Window->Show(); Win32::GUI::Dialog(); # Main window event handler sub Window_Terminate {
return -1; } sub Window_Resize { my ($width, $height) = ($Window->GetClientRect)[2..3]; $Grid->Resize ($width, $height); } # Grid event handler sub Grid_Click { my ($col, $row) = @_; print "Click on Cell ($col, $row)\n"; }
This package uses the MFC Grid control 2.25 By Chris Maunder. homepage: http://www.codeproject.com/miscctrl/gridctrl.asp
new
Create a new grid control.
Parameter : -name : Window name -parent : Parent window
-left : Left position -top : Top position -width : Width -height : Heigth
-pos : [x, y] position -size : [w, h] size
-visible : Visible -hscroll : Horizontal scroll -vscroll : Vertical scroll -enable : Enable
-style : Default style -pushstyle : Push style -popstyle : Pop style
-rows : Total rows -columns : Total columns -fixedrows : Fixed rows -fixedcolumns : Fixed columns -editable : Editable -doublebuffer : Double buffering display
Win32::GUI::Window::AddGrid
A Win32::GUI short cut for create a grid. Automaticly, create parent link.
SetRows
([nRows=10])
Sets the number of rows (including fixed rows), Returning TRUE on success.
GetRows
()
Returns the number of rows (including fixed rows).
SetColumns
([nCols=10])
Sets the number of columns (including fixed columns), Returning TRUE on success.
GetColumns
()
Returns the number of columns (including fixed columns)
SetFixedRows
([nFixedRows = 1])
Sets the number of fixed rows, returning TRUE on success.
GetFixedRows
()
Returns the number of fixed rows
SetFixedColumns
([nFixedCols = 1])
Sets the number of columns, returning TRUE on success.
GetFixedColumns
()
Returns the number of fixed columns
SetRowHeight
(nRow, height)
Sets the height of row nRow.
GetRowHeight
(nRow)
Gets the height of row nRow.
SetColumnWidth
(nCol, width)
Sets the width of column nCol.
GetColumnWidth
(nCol)
Gets the width of column nCol
GetFixedRowsHeight
()
Gets the combined height of the fixed rows.
GetFixedColumnsWidth
()
Gets the combined width of the fixed columns.
GetVirtualHeight
()
Gets the combined height of all the rows.
GetVirtualWidth
()
Gets the combined width of all the columns.
GetCellOrigin
(row, col)
Gets the topleft point for cell (nRow,nCol). Cell must be visible for success. Return an [x, y] if successful.
GetCellRect
(row, col)
Gets the bounding rectangle for the given cell. Cell must be visible for success. Return an [left, top, right, bottom] array if successful.
GetTextRect
(row, col)
Gets the bounding rectangle for the text in the given cell. Cell must be visible for success. Return an [left, top, right, bottom] array if successful..
GetTextExtent
(nRow, nCol, str)
Gets the bounding rectangle for the given text for the given cell. Return an [width, height] array if successful.
GetCellTextExtent
(nRow, nCol)
Gets the bounding rectangle for the text in the given cell. Return an [width, height] array if successful.
GetCellFromPt
(x, y)
Gets the cell position from the given point. Return an [row, col] if successful.
SetVirtualMode
([mode = TRUE])
Sets grid in virtual mode. See _GetData Event for provide data.
GetVirtualMode
()
Gets virtual mode.
SetImageList
(imagelist)
Sets the current image list for the grid. The control only takes a copy of the pointer to the image list, not a copy of the list itself.
GetImageList
()
Gets the current image list for the grid.
SetGridLines
([nWhichLines = GVL_BOTH])
Sets which (if any) gridlines are displayed.
Possible values. GVL_NONE = No grid lines GVL_HORZ = Horizontal lines only GVL_VERT = Vertical lines only GVL_BOTH = Both vertical and horizontal lines
GetGridLines
()
Gets which (if any) gridlines are displayed.
Possible values. GVL_NONE = No grid lines GVL_HORZ = Horizontal lines only GVL_VERT = Vertical lines only GVL_BOTH = Both vertical and horizontal lines
SetEditable
([bEditable = TRUE])
Sets if the grid is editable.
IsEditable
()
Gets whether or not the grid is editable.
SetListMode
([bEnableListMode = TRUE])
Sets the grid into (or out of) List mode. When the grid is in list mode, full row selection is enabled and clicking on the column header will sort the grid by rows.
GetListMode
()
Get whether or not the grid is in list mode.
SetSingleRowSelection
([bSingle = TRUE])
Sets the grid into (or out of) Single row selection mode. This mode is only effective when in ListMode. When in this mode, only a single row at a time can be selected, so the grid behaves somewhat like a multicolumn listbox.
GetSingleRowSelection
()
Get whether or not the grid is in single row selection mode.
SetSingleColSelection
([bSing = TRUE])
Sets the grid into (or out of) Single column selection mode. When in this mode, only a single column at a time can be selected.
GetSingleColSelection
()
Get whether or not the grid is in single column selection mode.
EnableSelection
([bEnable = TRUE])
Sets whether or not the grid cells can be selected.
IsSelectable
()
Get whether or not grid cells are selectable.
SetFixedRowSelection
([bSelect = TRUE])
Set whether or not clicking on a fixed row selects the cells next to it.
GetFixedRowSelection
()
Get whether or not clicking on a fixed row selects the cells next to it.
SetFixedColumnSelection
([bSelect = TRUE])
Set whether or not clicking on a fixed column selects the cells underneath.
GetFixedColumnSelection
()
Get whether or not clicking on a fixed column selects the cells underneath.
EnableDragAndDrop
([bAllow = TRUE]);
Sets whether drag and drop is enabled.
GetDragAndDrop
()
Get whether drag and drop is allowed.
SetRowResize
([bResize = TRUE])
Sets whether or not rows can be resized.
GetRowResize
()
Gets whether or not rows can be resized.
SetColumnResize
([bResize = TRUE])
Sets whether or not columns can be resized.
GetColumnResize
()
Gets whether or not columns can be resized.
SetHandleTabKey
([bHandleTab = TRUE])
Sets whether or not the TAB key is used to move the cell selection.
GetHandleTabKey
()
Gets whether or not the TAB key is used to move the cell selection.
SetDoubleBuffering
([bBuffer = TRUE])
Sets whether or not double buffering is used when painting (avoids flicker).
GetDoubleBuffering
()
Gets whether or not double buffering is used when painting.
EnableTitleTips
([bEnable = TRUE])
Sets whether or not titletips are used.
GetTitleTips
()
Gets whether or not titletips are used.
SetTrackFocusCell
([bTrack = TRUE])
Sets whether or not the fixed cells on the same row/column as the current focus cell are highlighted with a sunken border.
GetTrackFocusCell
()
Gets whether or not the fixed cells on the same row/column as the current focus cell are highlighted with a sunken border.
SetFrameFocusCell
([bFrame = TRUE])
Sets whether or not the cell with the focus is highlighted with a framed border.
GetFrameFocusCell
()
Gets whether or not the focus cell is highlighted with a framed border.
SetAutoSizeStyle
([nStyle = GVS_BOTH])
Sets how the auto-sizing should be performed.
Possible values. GVS_BOTH = use fixed and non fixed cells; GVS_HEADER = use only the fixed cells; GVS_DATA = use only non-fixed cells.
GetAutoSizeStyle
()
Gets how the auto-sizing should be performed
EnableHiddenColUnhide
([bEnable = TRUE])
Sets whether or not hidden (0-width) columns can be unhidden by the user resizing the column.
GetHiddenColUnhide
()
Gets whether or not hidden (0-width) columns can be unhidden by the user resizing the column.
void EnableHiddenRowUnhide
([bEnable = TRUE])
Sets whether or not hidden (0-height) rows can be unhidden by the user resizing the row.
GetHiddenRowUnhide
()
Gets whether or not hidden (0-height) rows can be unhidden by the user resizing the row.
EnableColumnHide
([bEnable = TRUE])
Sets whether or columns can be contracted to 0 width via mouse.
GetColumnHide
()
Gets whether or not columns can be contracted to 0 width via mouse.
void EnableRowHide
([bEnable = TRUE])
Sets whether or not rows can be contracted to 0 height via mouse.
GetRowHide
()
Sets whether or not rows can be contracted to 0 height via mouse.
SetGridBkColor
(color)
Sets the background colour of the control (the area outside fixed and non-fixed cells).
GetGridBkColor
()
Gets the background colour of the control.
SetGridLineColor
(color)
Sets the colour of the gridlines.
GetGridLineColor
()
Gets the colour of the grid lines.
SetTitleTipBackClr
(clr = CLR_DEFAULT)
Sets the background colour of the titletips.
GetTitleTipBackClr
()
Gets the background colour of the titletips.
SetTitleTipTextClr
(clr = CLR_DEFAULT)
Sets the text colour of the titletips.
GetTitleTipTextClr
()
Gets the text colour of the titletips.
Change and query the default cell implementation for the desired cell type. bFixedRow and bFixedCol specify whether the cell is fixed (in row, column or both) or unfixed. Use this to set default properties for the grid. Actual cells in the grid have their values set as default values when they are first created. They will use GetDefCell to query the grids default cell properties and use these values for drawing themselves.
SetDefCellTextColor
(bFixedRow, bFixedCol, [clr = CLR_DEFAULT])
Sets the text colour of the default cell type.
GetDefCellTextColor
(bFixedRow, bFixedCol)
Gets the text colour of default cell type.
SetDefCellBackColor
(bFixedRow, bFixedCol, [clr = CLR_DEFAULT])
Sets the background colour of the default cell type.
GetDefCellBackColor
(bFixedRow, bFixedCol)
Sets the background colour of the default cell type.
SetDefCellWidth
(bFixedRow, bFixedCol, nWidth)
Sets the width of default cell type.
GetDefCellWidth
(bFixedRow, bFixedCol)
Gets the width of default cell type.
SetDefCellHeight
(bFixedRow, bFixedCol, height)
Sets the height of default cell type.
GetDefCellHeight
(bFixedRow, bFixedCol)
Gets the height of default cell type.
SetDefCellMargin
(bFixedRow, bFixedCol, nMargin)
Sets the Margin of default cell type.
GetDefCellMargin
(bFixedRow, bFixedCol)
Gets the Margin of default cell type.
SetDefCellFormat
(bFixedRow, bFixedCol, nFormat)
Sets the format of default cell type.
GetDefCellFormat
(bFixedRow, bFixedCol)
Gets the format of default cell type.
SetDefCellStyle
(bFixedRow, bFixedCol, dwStyle)
Sets the style of default cell type.
GetDefCellStyle
(bFixedRow, bFixedCol)
Gets the style of default cell type.
SetDefCellFont
(bFixedRow, bFixedCol, dwStyle)
Sets the font of default cell type.
GetDefCellFont
(bFixedRow, bFixedCol)
Gets the font of default cell type.
SetDefCellType
(iType = GVIT_DEFAULT)
Change default cell type.
Cell type :
GVIT_NUMERIC = Numeric control edit GVIT_DATE = Date control GVIT_DATECAL = Date control with calendar control GVIT_TIME = Time control GVIT_CHECK = Check Box GVIT_COMBO = Combo Box GVIT_LIST = List Box GVIT_URL = Url control
SetCellType
(nRow, nCol, iType = GVIT_DEFAULT)
Change cell type.
Cell type :
GVIT_NUMERIC = Numeric control edit GVIT_DATE = Date control GVIT_DATECAL = Date control with calendar control GVIT_TIME = Time control GVIT_CHECK = Check Box GVIT_COMBO = Combo Box GVIT_LIST = List Box GVIT_URL = Url control
SetCellCheck
(nRow, nCol, bChecked = TRUE)
Set check box state. (GVIT_CHECK Only)
GetCellCheck
(nRow, nCol)
Get check box state. (GVIT_CHECK Only)
SetCellOptions
(nRow, nCol, ...)
Set cells options.
For GVIT_COMBO, GVIT_LIST : An array reference with list of string value (["Option1","Option2"])
For GVIT_CHECK : -checked => 0/1 : Set check.
For GVIT_URL : -autolaunch => 0/1 : Set autolauch mode (default : 1).
SetModified
([bModified = TRUE], [nRow = -1], [nCol = -1])
Sets the modified flag for a cell. If no row or columns is specified, then change affects the entire grid.
GetModified
([nRow = -1], [nCol = -1])
Gets the modified flag for a cell, or if no cell, it returns the status for the entire grid.
SetCellText
(nRow, nCol, str)
Sets the text for the given cell. Returns TRUE on success
GetCellText
(nRow, nCol)
Gets the text for the given cell.
SetCellData
(nRow, nCol, lParam)
Sets the lParam (user-defined data) field for the given cell. Returns TRUE on success.
GetCellData
(nRow, nCol)
Gets the lParam (user-defined data) field for the given cell.
SetCellImage
(nRow, nCol, iImage)
Sets the image index for the given cell. Returns TRUE on success.
GetCellImage
(nRow, nCol)
Gets the image index for the given cell.
SetCellState
(nRow, nCol, state)
Sets the state of the given cell. Returns TRUE on success.
GetCellState
(nRow, nCol)
Gets the state of the given cell.
SetCellFormat
(nRow, nCol, nFormat)
Sets the format of the given cell. Returns TRUE on success.
Default implementation of cell drawing uses CDC::DrawText, so any of the DT_* formats are available:
DT_TOP DT_LEFT DT_CENTER DT_RIGHT DT_VCENTER DT_BOTTOM DT_WORDBREAK DT_SINGLELINE DT_EXPANDTABS DT_TABSTOP DT_NOCLIP DT_EXTERNALLEADING DT_CALCRECT DT_NOPREFIX DT_INTERNAL DT_EDITCONTROL DT_PATH_ELLIPSIS DT_END_ELLIPSIS DT_MODIFYSTRING DT_RTLREADING DT_WORD_ELLIPSIS
GetCellFormat
(nRow, nCol)
Gets the format of the given cell (default returns a CDC::DrawText DT_* format).
SetCellBkColor
(nRow, nCol, [color = CLR_DEFAULT])
Sets the background colour of the given cell. Returns TRUE on success
GetCellBkColor
(nRow, nCol)
Gets the background colour of the given cell.
SetCellColor
(nRow, nCol, [color = CLR_DEFAULT])
Sets the foreground colour of the given cell. Returns TRUE on success.
GetCellColor
(nRow, nCol)
Gets the foreground colour of the given cell.
SetCellFont
(nRow, nCol, ...)
Sets the font of the given cell. Returns TRUE on success.
GetCellFont
(nRow, nCol)
Gets the font of the given cell.
EnsureCellVisible
(nRow, nCol)
Ensures that the specified cell is visible.
IsCellVisible
(nRow, nCol)
Returns TRUE if the cell is visible.
IsCellSelected
(nRow, nCol)
Returns TRUE if the cell is selected
SetCellEditable
(nRow, nCol, [bEditable = TRUE])
Sets the edtitable state of the given cell.
IsCellEditable
(nRow, nCol)
Returns TRUE if the cell is editable.
IsCellEditing
(nRow, nCol)
Returns TRUE if the cell is currently being edited.
IsCellFixed
(nRow, nCol)
Returns TRUE if the cell is a fixed cell.
GetSelectedCount
()
Gets the number of selected cells.
SetFocusCell
(nRow, nCol)
Sets the cell with the focus
GetFocusCell
()
Gets the cell with the focus.
InsertColumn
(strHeading, [nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE], [nColumn = -1])
Inserts a column at the position given by nCol, or at the end of all columns if nCol is < 0. strHeading is the column heading and nFormat the format. Returns the position of the inserted column.
InsertRow
(strHeading, [nRow = -1])
Inserts a row at the position given by nRow, or at the end of all rows if nRow is < 0. strHeading is the row heading. The format of each cell in the row will be that of the cell in the first row of the same column. Returns the position of the inserted row.
DeleteColumn
(nColumn)
Deletes column "nColumn", return TRUE on success.
DeleteRow
(nRow)
Deletes row "nRow", return TRUE on success.
DeleteAllCells
()
Deletes all rows and contents in the grid.
DeleteNonFixedRows
()
Deletes all non-fixed rows in the grid.
AutoSizeRow
(nRow, [bResetScroll = TRUE])
Auto sizes the row to the size of the largest item. If bResetScroll is TRUE then the scroll bars will be reset.
AutoSizeColumn
(nCol, [nAutoSizeStyle = GVS_DEFAULT], [bResetScroll = TRUE])
Auto sizes the column to the size of the largest item. nAutoSizeStyle sets the way the autosize will occur. If bResetScroll is TRUE then the scroll bars will be reset.
AutoSizing options :
GVS_DEFAULT = default GVS_HEADER = Size using column fixed cells data only GVS_DATA = Size using column non-fixed cells data only GVS_BOTH = Size using column fixed and non-fixed
AutoSizeRows
()
Auto sizes all rows.
AutoSizeColumns
([nAutoSizeStyle = GVS_DEFAULT])
Auto sizes all columns. nAutoSizeStyle sets the way the autosize will occur.
AutoSizing options :
GVS_DEFAULT = default GVS_HEADER = Size using column fixed cells data only GVS_DATA = Size using column non-fixed cells data only GVS_BOTH = Size using column fixed and non-fixed
AutoSize
([nAutoSizeStyle = GVS_DEFAULT])
Auto sizes all rows and columns. nAutoSizeStyle sets the way the autosize will occur.
AutoSizing options :
GVS_DEFAULT = default GVS_HEADER = Size using column fixed cells data only GVS_DATA = Size using column non-fixed cells data only GVS_BOTH = Size using column fixed and non-fixed
ExpandColumnsToFit
([bExpandFixed = TRUE])
Expands the column widths to fit the grid area. If bExpandFixed is TRUE then fixed columns will be modified, otherwise they will not be affected.
ExpandLastColumn
()
Expands the last column width to fill any remaining grid area.
ExpandRowsToFit
([bExpandFixed = TRUE])
Expands the row heights to fit the grid area. If bExpandFixed is TRUE then fixed rows will be modified, otherwise they will not be affected.
ExpandToFit
([bExpandFixed = TRUE])
Expands the rows and columns to fit the grid area. If bExpandFixed is TRUE then fixed cells will be modified, otherwise they will not be affected.
SetRedraw
(bAllowDraw, [bResetScrollBars = FALSE])
Stops/starts redraws on things like changing the number of rows and columns and autosizing, but not for user-intervention such as resizes.
RedrawCell
(nRow, nCol, [hDC=0])
Redraws the given cell. Drawing will be via the hDC if one is supplied.
RedrawRow
(row)
Redraws the given row.
RedrawColumn
(col);
Redraws the given column.
Refresh
()
Redraws the entire grid.
GetCellRange
()
Gets the range of cells for the entire grid. Return an [nMinRow, nMinCol, nMaxRow, nMaxCol] array.
SetSelectedCellRange
(nMinRow, nMinCol, nMaxRow, nMaxCol, [bForceRepaint = FALSE], [bSelectCells = TRUE])
Sets the range of selected cells.
GetSelectedCellRange
()
Gets the range of selected cells. Return an [nMinRow, nMinCol, nMaxRow, nMaxCol] array.
IsValid
(nRow, nCol)
Returns TRUE if the given row and column is valid.
GetNextCell
(nRow, nCol, nFlags)
Searches for a cell that has the specified properties and that bears the specified relationship to a given item.
Cell Searching options :
GVNI_FOCUSED = Search for focus cell GVNI_SELECTED = Search for selected cells GVNI_DROPHILITED = Search for drop highlighted cells GVNI_READONLY = Search for read-only cells GVNI_FIXED = Search for fixed cells GVNI_MODIFIED = Search for modified cells GVNI_ABOVE = Search above initial cell GVNI_BELOW = Search below initial cell GVNI_TOLEFT = Search to the left of the initial cell GVNI_TORIGHT = Search to the right of the initial cell GVNI_ALL = Search all cells in the grid starting from the given cell GVNI_AREA = Search all cells below and to the right of the given cell
ClearCells
(nMinRow, nMinCol, nMaxRow, nMaxCol)
Clear cell in range.
AutoFill
()
Auto fill witk blank cell.
SetHeaderSort
([bSortOnClick = TRUE])
Sets whether or not rows are sorted on column header clicks in ListMode.
GetHeaderSort
()
Gets whether or not rows are sorted on column header clicks in ListMode.
SetSortColumn
(nCol)
Sets the index of the currently sorted column.
GetSortColumn
()
Gets the index of the currently sorted column.
SetSortAscending
([bAscending = TRUE])
Sets whether the current sort column is sorted ascending.
GetSortAscending
()
Gets whether the current sort column is sorted ascending.
SortTextCells
(nCol, bAscending)
Sorts the grid on the given column based on cell text. Returns TRUE on success
SortNumericCells
(nCol, bAscending)
Sorts the grid on the given column based on cell numeric text. Returns TRUE on success
SortCells
(nCol, bAscending, [pfun = NULL])
Sort given method and given sort order. Optional a custom sort fonction.
Sort Function sample :
sub { my ($e1, $e2) = @_; return (int($e1) - int ($e2)); }
SetSortFunction
([pFun = NULL], [nCol = -1])
Set or Remove Perl sort function.
If nCol is -1, Set or remove a default sort method. If nCol is a valid column , Set or remove a sort method for this column only.
Sort Function sample :
sub { my ($e1, $e2) = @_; return (int($e1) - int ($e2)); }
TODO
OnEditCut
()
Copies contents of selected cells to clipboard and deletes the contents of the selected cells. (Ctrl-X)
OnEditCopy
()
Copies contents of selected cells to clipboard. (Ctrl-C)
OnEditPaste
()
Pastes the contents of the clipboard to the grid. (Ctrl-V)
OnEditSelectAll
()
Not actually a clipboard function, but handy nevertheless. This routine selects all cells in the grid. (Ctrl-A)
_Click
(nRow, nCol)
Simple left click event.
_RClick
(nRow, nCol)
Simple right click event.
_DblClick
(nRow, nCol)
Double left click event.
_Changing
(nRow, nCol)
Start changing selection event.
_Changed
(nRow, nCol)
Selection have change event.
_BeginEdit
(nRow, nCol)
Start Cell Edit event. Return non zero value to prevent editing.
_ChangedEdit
(nRow, nCol, str)
ListBox selection change event. str is current selected item. Available with GVIT_COMBO, GVIT_LIST.
_EndEdit
(nRow, nCol, [str])
End Cell Edit event. Return non zero value to prevent change.
[virtual Mode Only] str contains edited cell data.
_BeginDrag
(nRow, nCol)
Begin drag event.
_GetData
(nRow, nCol)
[virtual Mode Only] You must return cell data.
_CacheHint
(nMinRow, nMinCol, nMaxRow, nMaxCol)
[virtual Mode Only] Range before request data.
MFC Grid Control by Chris Maunder
Win32::GUI Microsoft Foundation Classes (MFC)
Laurent Rocher (lrocher@cpan.org)
Copyright 2003..2006 by Laurent Rocher (lrocher@cpan.org)
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html
Modified code from the MFC Grid control by Chris Maunder is statically linked into this module. The MFC Grid control is released with the following notice:
This code may be used in compiled form in any way you desire (including commercial use). The code may be redistributed unmodified by any means providing it is not sold for profit without the authors written consent, and providing that this notice and the authors name and all copyright notices remains intact. However, this file and the accompanying source code may not be hosted on a website or bulletin board without the authors written permission.
This software is provided "as is" without express or implied warranty. Use it at your own risk!
Whilst I have made every effort to remove any undesirable "features", I cannot be held responsible if it causes any damage or loss of time or data.