Canvas based Grid gadget

Share your advanced PureBasic knowledge/code with the community.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Canvas based Grid gadget

Post by BorisTheOld »

RichardL wrote:When designing a grid to be used as a data entry mechanism it is a good idea to imagine yourself as a super fast typist** who would much rather enter a new line of data without taking a hand away from the keyboard to use the mouse.
Good point, and something that should be considered for any application.

Over the past 30 years we've developed various strategies for allowing our applications to be controlled entirely from the keyboard, as well as with the more normal mouse-oriented approach. Our customers find they can get from 5 to 10 times the throughput, compared to using a mouse/keyboard.

In effect, our programs are state machines that dynamically change the functions of keys to suit the situation. The idea is to maximize performance with a minimum of hand movement.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Canvas based Grid gadget

Post by fsw »

This Canvas based Grid gadget is very nicely done.

If someone needs column names to reflect more "traditional" naming convention (A, B, C.....AA, AB, AC... AAA, AAB, AAC... ZZZ) here is my version of the MyGrid_Reset() procedure:

Code: Select all

; needed for MyGrid_Reset()
#ColumnText = 65 ; "A"
#ColumnSpan = 26 ; "A" to "Z"

Procedure.i _MyGrid_Reset(*mg._MyGrid_Type, Rows, Cols)
    ; Reset everything so Grid can receive/show new data
    Protected i, factor.i, bigfactor.i
    
    If rows <= 0 : rows =1 : EndIf
    If cols <= 0 : cols =1 : EndIf
    
    *mg\Rows = rows          : Dim *mg\RowHeight(rows)
    *mg\Cols = cols          : Dim *mg\ColWidth(cols) :  Dim *mg\ColID(cols)
    
    *mg\LastIndex = (rows+1) * (cols+1) - 1
    Dim *mg\gData(*mg\LastIndex)
    If ArraySize(*mg\gData()) < 0
        Debug "failed to allocate memory for the grid data !... "
        ProcedureReturn
    EndIf
    
    ; initializations
    *mg\TopRow          = 1
    *mg\TopCol          = 1
    *mg\Row             = 1
    *mg\Col             = 1
    
    ;- set column name like a real spread sheet...
    For i=#ColumnText To #ColumnText + cols
      If i < (#ColumnText + #ColumnSpan)
        ; A to Z -> total of 26 columns... enough for small stuff.
        _MyGrid_SetCellText(*mg, 0, i - (#ColumnText -1), Chr(i))
      ElseIf i < (#ColumnText + #ColumnSpan + (#ColumnSpan * #ColumnSpan))
        ; AA to ZZ  -> 676 columns -> total of 702 columns... nice size.
        factor = Int((i - (#ColumnText + #ColumnSpan)) / #ColumnSpan) 
        Debug factor
        _MyGrid_SetCellText(*mg, 0, i - (#ColumnText -1), Chr(#ColumnText + factor) + Chr(i - #ColumnSpan * (factor + 1)))
     ElseIf i < (#ColumnText + #ColumnSpan + (#ColumnSpan * #ColumnSpan) + (#ColumnSpan * #ColumnSpan * #ColumnSpan))
        ; AAA to ZZZ  -> 17576 columns -> total of 18278 columns... that's a lot!
        factor = Int((i - (#ColumnText + #ColumnSpan)) / #ColumnSpan) 
        bigfactor = Int((i - (#ColumnText + (#ColumnSpan * #ColumnSpan) + #ColumnSpan)) / (#ColumnSpan * #ColumnSpan))
        Debug Str(factor) + " : " + Str(bigfactor)
        _MyGrid_SetCellText(*mg, 0, i - (#ColumnText -1), Chr(#ColumnText + bigfactor) + Chr(#ColumnText + factor - (#ColumnSpan * (bigfactor + 1))) + Chr(i - #ColumnSpan * (factor + 1)))
      Else
        ; AAAA to ZZZZ -> 456976 columns -> total of 475254 columns... who needs that?
        ; out of range; or bigger than 18278 columns
        _MyGrid_SetCellText(*mg, 0, i - (#ColumnText -1), "...")
      EndIf
    Next
    
    For i=1 To rows
        _MyGrid_SetCellText(*mg, i, 0, Str(i))
    Next
    
    _MyGrid_ChangeColWidth(*mg, #MyGrid_RC_Any, #MyGrid_Default_ColWidth)
    _MyGrid_ChangeRowHeight(*mg, #MyGrid_RC_Any, #MyGrid_Default_RowHeight)
    
    *mg\FrozenCol           = 0
    *mg\FrozenRow           = 0
    
    *mg\MoveStatus          = #MyGrid_MouseMove_Nothing
    *mg\PreviousX           = 0
    *mg\PreviousY           = 0
    
    *mg\NoRedraw            = #False
    
    *mg\Color_Line          = $CCCCCC
    *mg\Color_BlockBack     = $FFFFBB
    *mg\Color_Background    = $808080
    *mg\Color_FocusBack     = $DBF0E0
    *mg\Color_FocusBorder   = $009AB6
    
    *mg\WrapText            = #True
    
    ; adding the 4 default styles : data-cell/frozen-cells/col-header/row-header
    ClearList( *mg\LstStyle() )
    ClearMap(  *mg\DicStyle() )
    
    AddElement(*mg\LstStyle())      ; data area
    _MyGrid_DefineCurrentStyle(*mg, #MyGrid_Align_Center, $FFFFFF, $000000, Font_A8, 0, 1)
    
    AddElement(*mg\LstStyle())      ; frozen-data area
    _MyGrid_DefineCurrentStyle(*mg, #MyGrid_Align_Center, $D7EBFA, $000000, Font_A8, 0, 1)
    
    AddElement(*mg\LstStyle())      ; col-headers
    _MyGrid_DefineCurrentStyle(*mg, #MyGrid_Align_Center, $EB9E85, $FFFFFF, Font_A8, 0, 0)
    
    AddElement(*mg\LstStyle())      ; row-headers
    _MyGrid_DefineCurrentStyle(*mg, #MyGrid_Align_Center, $EB9E85, $FFFFFF, Font_A8, 0, 0)
    
    ; set min/max/page of scrolls
    _MyGrid_AdjustScrolls(*mg)
    
EndProcedure
For now the code covers 18278 columns...

I am to provide the public with beneficial shocks.
Alfred Hitshock
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Canvas based Grid gadget

Post by said »

@fsw: thanks for your addition (nice tip)

For those who are interested in this gadget, it has gone thru a lot of improvement since last update ... now with block selection and combo-cell ... I am just waiting for the final 5.20 to incorporate live scrolling (using BindEvent() ...) and shall soon upload inshAllah the final update!

Said
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Canvas based Grid gadget

Post by IdeasVacuum »

WinXP x86 PB5.20 Beta12

Hi Said

Few random things I have noticed while playing with your excellent gadget.

[1] MyGrid_SetColorAttribute():

#MyGrid_Color_FocusBack and #MyGrid_Color_FocusBorder are reversed in the *mg structure values

Would it make sense to include the default cell colours?

[2] .... you can put tick-box cells in Column 0 but they do not work.

[3] MyGrid_Col_Freeze()/MyGrid_Row_Freeze() does not?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Canvas based Grid gadget

Post by IdeasVacuum »

#PB_Shortcut_Prior is now #PB_Shorcut_PageUp (corrected by Bisonte)
#PB_Shortcut_Next is now #PB_Shorcut_PageDown (corrected by Bisonte)

Edit:
.... I need to work out how to change the cell edit a little bit. If the User types in some text, it shouldn't only require a Return Key hit to confirm (I know most avid keyboard Users would probably be happy, but it's confusing for those that mostly use the mouse - mouse focus on another cell should trigger acceptance of input in previous cell).

#MyGrid_Align_Right works fine with a single line of text, but if the text wraps in the cell to two or more lines, it gets aligned left.
Last edited by IdeasVacuum on Thu Aug 22, 2013 7:41 am, edited 2 times in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Canvas based Grid gadget

Post by Bisonte »

Renamed: #PB_Shorcut_Prior to #PB_Shorcut_PageUp and #PB_Shorcut_Next to #PB_Shorcut_PageDown
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Canvas based Grid gadget

Post by IdeasVacuum »

Thanks Bisonte :)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Canvas based Grid gadget

Post by IdeasVacuum »

Another question - how to define the Grid Gadget without scroll bars?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Canvas based Grid gadget (revised for PB5.20)

Post by said »

Hi All,

I have updated this gadget ... see first post. It is now compatible with PB 5.20 and has many new features ....

Hi IdeasVaccum
Few random things I have noticed while playing with your excellent gadget.

[1] MyGrid_SetColorAttribute():

#MyGrid_Color_FocusBack and #MyGrid_Color_FocusBorder are reversed in the *mg structure values

Would it make sense to include the default cell colours?

[2] .... you can put tick-box cells in Column 0 but they do not work.

[3] MyGrid_Col_Freeze()/MyGrid_Row_Freeze() does not?
1. You are right, that was in old version - fixed
2. Yes Col 0 and Row 0 are hearers, you cant access them! However you can still hide them and use Col 1/Row 1 which can be editable ...
3. What is your question ? MyGrid_Col_Freeze(gadget, col) defines the col to freeze on ...
Another question - how to define the Grid Gadget without scroll bars?
While defining the grid via MyGrid_New() you can request not to add scrolls.

See notes included with MyGrid_PB520.PBI for more details about editing cells

Best,
Said
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Canvas based Grid gadget (revised for PB5.20)

Post by IdeasVacuum »

Thanks Said!

Concerning the freezing, it was not working (XP x86, PB5.2 Beta12).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Canvas based Grid gadget (revised for PB5.20)

Post by Kiffi »

said wrote:I have updated this gadget
thanks a lot! Image

Greetings ... Kiffi
Hygge
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Canvas based Grid gadget

Post by said »

Hi All,

After 2 years! Some people are still interested in that gadget ... :D :D

Here is a slight update of the same old version (allowing to hide Focus rectangle) and moved the includes to GitHub

* MyGrid_01Aug2015.PBI
* MyGrid_PB520.PBI (the same old release of aug-2013)

Both files can be downloaded here:

https://github.com/said7/PureBasic/tree/master


First post updated as well

Said
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: Canvas based Grid gadget

Post by ozzie »

Many thanks, Said. Hiding the focus rectangle works perfectly!
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Re: Canvas based Grid gadget

Post by LuckyLuke »

Great work ! Thanks Said!

Maybe this gadget can be added to the official Purebasic distribution ?

LuckyLuke
Fred
Administrator
Administrator
Posts: 16618
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Canvas based Grid gadget

Post by Fred »

It's a nice work.
Post Reply