My MyTable/MyGrid

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Hi,

The event isn't the issue, I can't figure out how to duplicate the action of rowselected by using cellselected E.G click on a cell and retrieve/store the entire rows data.

If i may suggest, it would be worth having seperate code for a table gadget and a sheet gadget. I see the table gadget as a replacement for the listicongadget as yours is far supperior imho to the listicongadget.

Also, some of the commands return a human readable row number whilst others return the computer defined id, a consistency would greatly simplify human interaction.

Please don't take the above as criticism, as i said your grid is far superrior to the listicongadget and will happily pay to use it, as i currently have to bodge around with tables in a webgadget.

cheers
Cyllceaux
Enthusiast
Enthusiast
Posts: 313
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: My MyTable/MyGrid

Post by Cyllceaux »

Hey there :)

I don't see it as a critic, more than a "missing documentation" :)

Code: Select all

Procedure CellSelected(*cell.MyTableCell)
   Protected *row.MyTableRow=*cell\GetRow()
   Protected NewList cells.MyTableCell()
   *row\GetCells(cells())
EndProcedure

*table\SetEventCellSelected(@CellSelected())
there you get a List of the cells of a row from a clicked cell. Is it what you want?
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Cyllceaux wrote: Thu Jun 10, 2021 1:20 pm Hey there :)

I don't see it as a critic, more than a "missing documentation" :)

Code: Select all

Procedure CellSelected(*cell.MyTableCell)
   Protected *row.MyTableRow=*cell\GetRow()
   Protected NewList cells.MyTableCell()
   *row\GetCells(cells())
EndProcedure

*table\SetEventCellSelected(@CellSelected())
there you get a List of the cells of a row from a clicked cell. Is it what you want?
I'm still missing something, as i understand it this returns a list of cell id's ?

How can i get the cell text from the id ?

Sorry for all the questions.
Cyllceaux
Enthusiast
Enthusiast
Posts: 313
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: My MyTable/MyGrid

Post by Cyllceaux »

You can do it this way.

Code: Select all

Procedure CellSelected(*cell.MyTableCell)
   Protected *row.MyTableRow=*cell\GetRow()
   Protected NewList cells.MyTableCell()
   *row\GetCells(cells())
   ForEach cells()
      Debug cells()\GetText()
   Next
EndProcedure

*table\SetEventCellSelected(@CellSelected())

Since I went to OO, I don't work with IDs. I work with Structures, Indices and Interfaces. :)
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Cyllceaux wrote: Thu Jun 10, 2021 9:01 pm You can do it this way.

Code: Select all

Procedure CellSelected(*cell.MyTableCell)
   Protected *row.MyTableRow=*cell\GetRow()
   Protected NewList cells.MyTableCell()
   *row\GetCells(cells())
   ForEach cells()
      Debug cells()\GetText()
   Next
EndProcedure

*table\SetEventCellSelected(@CellSelected())

Since I went to OO, I don't work with IDs. I work with Structures, Indices and Interfaces. :)
Shoot.... I was trying all sorts of variations of *cell\gettext() getcelltext() etc.

Thanks
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Hi,

So this works quite nicely for my purposes, i have selected row highlighting working - although the code isn't very pretty.

I also have alternate row background colouring working, but it's slow and I wonder if there is a better way to acheive it ?

Code: Select all

#MYTABLE_DEBUG = 0
#MYTABLE_DEBUG_LEVEL = 0

DebugLevel #MYTABLE_DEBUG_LEVEL

; XIncludeFile "default.pb"
XIncludeFile "mytable.pbi"
UseModule MyTable

Global NewList LIST_unhighlight()
Global NewList cells.MyTableCell()


Procedure CellSelected(*cell.MyTableCell)
  
  Protected *row.MyTableRow=*cell\GetRow()
   
   ; un-highlight previous highlighted row
   If ListSize(cells()) > 0
     FirstElement(cells())
     ; Set background colour according back to original colour
     If Val(cells()\GetText())%2 = 1
       LVAR_bgcolour = RGBA(255, 255, 255, 255)
     Else
       LVAR_bgcolour = RGBA(150, 150, 150, 255)
     EndIf
     ; iterate cells and change colour
     ForEach cells()
       cells()\SetBackground(LVAR_bgcolour)
     Next
     ClearList(cells())
   EndIf
   
   *row\GetCells(cells())
   FirstElement(cells())
   
   ForEach cells()
     cells()\SetBackground(RGBA(100, 100, 100, 255))
     Debug cells()\GetText()
   Next
   
EndProcedure


Global mainWindow=OpenWindow(#PB_Any,0,0,1100,600,"MyTable",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget)

Global canvasGrid=CanvasGadget(#PB_Any, 5, 5, 1000, 500,#PB_Canvas_Container|#PB_Canvas_Border|#PB_Canvas_Keyboard)
Global hscrollGrid=ScrollBarGadget(#PB_Any, 0, 0, 0, 20, 0, 0, 100)
Global vscrollGrid=ScrollBarGadget(#PB_Any, 0, 0, 20, 0, 0, 0, 100,#PB_ScrollBar_Vertical)     
CloseGadgetList()

Define *canvasGrid.MyTableTable=MyTableRegister(mainWindow, canvasGrid, hscrollGrid, vscrollGrid, #MYTABLE_TABLE_FLAGS_DEFAULT, 0, "canvasTable")
;Define *canvasGrid.MyTableTable=MyTableGridRegister(mainWindow, canvasGrid, hscrollGrid, vscrollGrid, 0, 0)

Define fontT=LoadFont(#PB_Any,"Calibri", 12, #PB_Font_HighQuality)
*canvasgrid\SetFont(FontID(fontT))

*canvasGrid\AddColumn("Column 01", 100);, #MYTABLE_COLUMN_FLAGS_RIGHT|#MYTABLE_COLUMN_FLAGS_INTEGER|#MYTABLE_COLUMN_FLAGS_RESIZEABLE)
*canvasGrid\AddColumn("Column 02", 100);
*canvasGrid\AddColumn("Column 03", 100);
*canvasGrid\AddColumn("Column 04", 100);
*canvasGrid\AddColumn("Column 05", 100);
*canvasGrid\AddColumn("Column 06", 100);
*canvasGrid\AddColumn("Column 07", 100);



For i=1 To 100
  LVAR_data.s = Str(i)
  LVAR_data + "|Column02 : "+Str(i)
  LVAR_data + "|Column03 : "+Str(i)
  LVAR_data + "|Column04 : "+Str(i)
  LVAR_data + "|Column05 : "+Str(i)
  LVAR_data + "|Column06 : "+Str(i)
  If i = 7
    LVAR_data + "|Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i) + #CRLF$ + "Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i) + #CRLF$ + "Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i)
  Else
    LVAR_data + "|Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i) + #CRLF$ + "Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i)
  EndIf
  
  *canvasGrid\AddRow(LVAR_data)
  
Next


*canvasGrid\SetRedraw(#True)

*canvasGrid\AutosizeHeader(#PB_Ignore)
*canvasGrid\AutosizeColumn(#PB_Ignore)
*canvasGrid\AutosizeRow(#PB_Ignore)

*canvasGrid\SetEventCellSelected(@CellSelected())

; colour alternate rows ( slow )
For i = 0 To 99
  
  If i % 2 = 1
    For x = 0 To 6
      Define *cell.MyTableCol=*canvasGrid\GetCell(i,x)
      *cell\SetBackground(RGBA(150, 150, 150, 255))
    Next
  EndIf
  
Next i  


Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow


*canvasGrid\UnRegister()

UnuseModule MyTable
Thanks for the help anyhoo.
Cyllceaux
Enthusiast
Enthusiast
Posts: 313
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: My MyTable/MyGrid

Post by Cyllceaux »

Oh... yes... I see.
  1. canvasGrid\SetRedraw(#False)
    this stops the table to redraw every time. So I added it after the Register.
    canvasGrid\SetRedraw(#True)
    Starts the Redraw
  2. Next I changed your Color function. You can give every row a own color
  3. I increased the rowcount to 1000.
    • Btw. If you don't use the autoSize functions, it will be extrem faster. Even with more than 100.000 Rows.
    • Oh... and I see I have a bug in AutoSizeRow. Stupid me

Code: Select all

#MYTABLE_DEBUG = 0
#MYTABLE_DEBUG_LEVEL = 0

DebugLevel #MYTABLE_DEBUG_LEVEL

; XIncludeFile "default.pb"
XIncludeFile "C:\Users\silko\OneDrive\Dokumente\PureBasic\MyTable\V1\mytable.pbi"
UseModule MyTable

Global NewList LIST_unhighlight()
Global NewList cells.MyTableCell()


Procedure CellSelected(*cell.MyTableCell)
  
  Protected *row.MyTableRow=*cell\GetRow()
   
   ; un-highlight previous highlighted row
   If ListSize(cells()) > 0
     FirstElement(cells())
     ; Set background colour according back to original colour
     If Val(cells()\GetText())%2 = 1
       LVAR_bgcolour = RGBA(255, 255, 255, 255)
     Else
       LVAR_bgcolour = RGBA(150, 150, 150, 255)
     EndIf
     ; iterate cells and change colour
     ForEach cells()
       cells()\SetBackground(LVAR_bgcolour)
     Next
     ClearList(cells())
   EndIf
   
   *row\GetCells(cells())
   FirstElement(cells())
   
   ForEach cells()
     cells()\SetBackground(RGBA(100, 100, 100, 255))
     Debug cells()\GetText()
   Next
   
EndProcedure


Global mainWindow=OpenWindow(#PB_Any,0,0,1100,600,"MyTable",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget)

Global canvasGrid=CanvasGadget(#PB_Any, 5, 5, 1000, 500,#PB_Canvas_Container|#PB_Canvas_Border|#PB_Canvas_Keyboard)
Global hscrollGrid=ScrollBarGadget(#PB_Any, 0, 0, 0, 20, 0, 0, 100)
Global vscrollGrid=ScrollBarGadget(#PB_Any, 0, 0, 20, 0, 0, 0, 100,#PB_ScrollBar_Vertical)     
CloseGadgetList()

Define *canvasGrid.MyTableTable=MyTableRegister(mainWindow, canvasGrid, hscrollGrid, vscrollGrid, #MYTABLE_TABLE_FLAGS_DEFAULT, 0, "canvasTable")
;Define *canvasGrid.MyTableTable=MyTableGridRegister(mainWindow, canvasGrid, hscrollGrid, vscrollGrid, 0, 0)

*canvasGrid\SetRedraw(#False)

Define fontT=LoadFont(#PB_Any,"Calibri", 12, #PB_Font_HighQuality)
*canvasgrid\SetFont(FontID(fontT))

*canvasGrid\AddColumn("Column 01", 100);, #MYTABLE_COLUMN_FLAGS_RIGHT|#MYTABLE_COLUMN_FLAGS_INTEGER|#MYTABLE_COLUMN_FLAGS_RESIZEABLE)
*canvasGrid\AddColumn("Column 02", 100);
*canvasGrid\AddColumn("Column 03", 100);
*canvasGrid\AddColumn("Column 04", 100);
*canvasGrid\AddColumn("Column 05", 100);
*canvasGrid\AddColumn("Column 06", 100);
*canvasGrid\AddColumn("Column 07", 100);



For i=1 To 1000
  LVAR_data.s = Str(i)
  LVAR_data + "|Column02 : "+Str(i)
  LVAR_data + "|Column03 : "+Str(i)
  LVAR_data + "|Column04 : "+Str(i)
  LVAR_data + "|Column05 : "+Str(i)
  LVAR_data + "|Column06 : "+Str(i)
  If i = 7
    LVAR_data + "|Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i) + #CRLF$ + "Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i) + #CRLF$ + "Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i)
  Else
    LVAR_data + "|Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i) + #CRLF$ + "Column07 : "+Str(i) + " Column07 : "+Str(i) + " Column07 : "+Str(i)
  EndIf
  
  *canvasGrid\AddRow(LVAR_data)
  
Next




*canvasGrid\AutosizeHeader(#PB_Ignore)
*canvasGrid\AutosizeColumn(#PB_Ignore)
*canvasGrid\AutosizeRow(#PB_Ignore)

*canvasGrid\SetEventCellSelected(@CellSelected())

; colour alternate rows ( slow )
For i = 1 To *canvasGrid\GetRowCount()
  
  If i % 2 = 1
  	Define *row.MyTableRow=*canvasGrid\GetRow(i)    
    *row\SetBackground(RGBA(150, 150, 150, 255))    
  EndIf
  
Next i  

*canvasGrid\SetRedraw(#True)

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow


*canvasGrid\UnRegister()

UnuseModule MyTable

User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Thanks very much Cyllceaux, this is perfect for my use case.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Oops may have spoke to soon, on your amended code if you scroll to the bottom it only gets to record 988.

I experimented a bit and this is consistent E.G change the number of rows to 200 only allows scrolling to 188.

I guess there is a factor for the height of the scroll bar not updating correctly ?

Sorry for all the prob's :(
Cyllceaux
Enthusiast
Enthusiast
Posts: 313
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: My MyTable/MyGrid

Post by Cyllceaux »

I know... It's a bug in AutosizeRow.
Its better to set the rowheight manually and global.

Code: Select all

*canvasGrid\SetDefaultRowHeight(40)
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Sorry - misread your first reply and realised what i should have done after i posted.

BTW i set the defaultrowheight to 200, The unused whitespace at the bottom is no problem to me.

Once again thanks for the help and this brilliant code. :P :P :P
Cyllceaux
Enthusiast
Enthusiast
Posts: 313
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: My MyTable/MyGrid

Post by Cyllceaux »

Im hard working on Version 2.
This is one of my tests:
Image
Every element is a MyTable, except the calendar and the EditorGadget in the middle.

You can find it on branch "v2".
mestnyi
Addict
Addict
Posts: 856
Joined: Mon Nov 25, 2013 6:41 am

Re: My MyTable/MyGrid

Post by mestnyi »

add these lines to the beginning of the file so that you can run it on a mac os.
since mac os has a bug in the clipoutput function.

Code: Select all

Macro PB( _pb_function_name_ ) 
  _pb_function_name_
EndMacro
Macro ClipOutput(_x_,_y_,_width_,_height_)
  CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
    PB(ClipOutput)(_x_,_y_,_width_,_height_)
  CompilerEndIf
EndMacro
Macro UnclipOutput()
  CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
    PB(UnclipOutput)()
  CompilerEndIf
EndMacro
Macro DrawingFont(_font_id_)
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    If _font_id_
      PB(DrawingFont)(_font_id_)
    EndIf
  CompilerElse
    PB(DrawingFont)(_font_id_)
  CompilerEndIf
EndMacro
mytabletable.pb
Cyllceaux
Enthusiast
Enthusiast
Posts: 313
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: My MyTable/MyGrid

Post by Cyllceaux »

mestnyi wrote: Sun Jun 13, 2021 8:41 pm add these lines to the beginning of the file so that you can run it on a mac os.
since mac os has a bug in the clipoutput function.

Code: Select all

Macro PB( _pb_function_name_ ) 
  _pb_function_name_
EndMacro
Macro ClipOutput(_x_,_y_,_width_,_height_)
  CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
    PB(ClipOutput)(_x_,_y_,_width_,_height_)
  CompilerEndIf
EndMacro
Macro UnclipOutput()
  CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
    PB(UnclipOutput)()
  CompilerEndIf
EndMacro
Macro DrawingFont(_font_id_)
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    If _font_id_
      PB(DrawingFont)(_font_id_)
    EndIf
  CompilerElse
    PB(DrawingFont)(_font_id_)
  CompilerEndIf
EndMacro
mytabletable.pb

oh... thx... good to know
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 598
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: My MyTable/MyGrid

Post by captain_skank »

Hi Cyllceaux,

Couple more q's for you.

I'm using your grid with a database and have added a navigation bar ( i don't want to diplay thousands of records in one hit ) and I clear the grid using the clearrows() statement - is this correct and does this empty all the data ?

Also is there a way to catch an event on the column heading ? I want to requery the database to sort but need to trigger it.

When selecting a cell is their a way to change/overide the default cell selected colour ( currently light blue ), I've trawled you code but can't figure where your setting it.

Finaly, the way I read the code if i define the grid with the #MYTABLE_BORDER_NONE flag no borders would be visible on either grid,row or cell - or am i missunderstanding this, becasue that's not the case.

Version 2 looks good by the way :)
Post Reply