MicroGrid ~ Rev 0.97 now available.

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by Kwai chang caine »

Works good now 8)
Just a question is it possible a day to insert different jpg image directly, in each case in your splendid tools :D
ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by RichardL »

Hi KCC,

In short... YES! :D

When each ImageCell is created two Images are created and assigned to the cell. One is red and the other green.
The simplest way of using your own picture is to draw it on these.

Use Image = uGridGetFlipImageNum(#GadNum,X,Y,n) with n=0 and n=1 to get the two Image numbers, then
draw your own content on them. Keep the sizes unchanged.

This is how the column of Option buttons are made in my example.

Have a look at the link in line 24 of the code... a good place to get some nice button designs.

RichardL
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by Kwai chang caine »

Thanks a lot RICHARDL for your precious explanation 8)
Again thanks for your great job and obviously the sharing 8)
With your grid...EXCEL be a day affraid :wink: :D
ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by RichardL »

Hi,
Version 0.92 is almost complete. After KCC's comments about using images I thought that a more elegant way of setting the height of an image cell would be a good idea. In the new version ugrid(n)\ImageCellH determines the cell heights instead of ugrid(n)\GridFontHeight. To improve presentation the text is vertically centralised.

While making these changes I realised that the uGridService() routine could be tidied up and simplified, so that was done as well.

I'm testing now... the new version should be released in a day or two.

Cheers,
RichardL
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by Kwai chang caine »

Thanks a lot to have considered my request 8)
But i have not really understand how enter a picture in the grid :oops:
Image
I have reduce your code, for just have the grid with picture....but how i must enter my "c:\...\image.jpg" :oops:
Have you a very simple donkey example ?? :mrgreen:

Code: Select all

XIncludeFile "MicroGrid03.pbi"

CompilerIf #PB_Compiler_IsMainFile
  ; *************************************
  ;            Test code
  ;{ *************************************

  Enumeration 4242
    #Win_Test
    #Gad_Grid3
  EndEnumeration
  
  OpenWindow(#Win_Test,0,0,920,500,"MicroGrid Test 0.91",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)

  uGridEnableCallback(#True)
  
  ;- Demo two state Image cells
  With uGridDefaults
    \Font           = "Arial"
    \FontH          = 8
  EndWith
  
  k$ = "Col1[80]|Col2[25]|"
  uGridGadget(#Gad_Grid3,0,0,920,500,k$,10,#uGrid_AutoSizeX|#uGrid_AutoSizeY) ; Make grid,
  uGridSetCellType(#Gad_Grid3,0,0,-1,-1,#uGrid_CellImageFlip)                ; all cells to be images,
  
  ; Make right column into Option buttons
  For Y = 0 To 7
    C = #White
    For n = 0 To 1
      T = uGridGetFlipImageNum(#Gad_Grid3,1,Y,n)  ; Get Image# for state 'n'
      If T 
        W = ImageWidth(T) : H = ImageHeight(T)    ; Size of image
        If StartDrawing(ImageOutput(T))
            Box(0,0,W,H,#White)                   ; Backdrop for cell
            Circle(W/2,H/2,4,#Black)              ; Draw the 'Option Button'
            Circle(W/2,H/2,3,C)
          StopDrawing()
        EndIf
      EndIf
      C = #Red
     
    Next
  Next
  
  ; Set state of column 1 'Option button'
  For Y = 0 To 7
    uGridSetFlipImageState(#Gad_Grid3,1,Y,Y&1)
  Next
   
  uGridRefreshGrid(#Gad_Grid3)                                               ; show initial states.
   

Repeat
  Select WaitWindowEvent(10)
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
          ; 'uGrids' grouped in order simplifies coding 
        Case #Gad_Grid3, MyGrid3 
          uGridService(EventGadget())
          
      EndSelect
  EndSelect
  
ForEver

Procedure uGridCallback(uGridNum,uGridEvent,IParam.i,*uGridCell)
 
EndProcedure
;}
CompilerEndIf


ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by RichardL »

Hi KCC,

I will be posting a new version of uGrid later today. It includes the ability to specify the height of a cell in pixels.
When I have done that I will make a demo to show how to use uGrid to display user's pictures.

RichardL
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Now with 'Image Cells' (Rev 0.91)

Post by Kwai chang caine »

Thanks a lot RICHARDL 8)
ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by RichardL »

Good morning all,

For anyone wishing to draw an image file (bmp/jpg/png.....), and in particular KCC, the following procedure should do the necessary :D
Thanks to feedback from KCC I have stamped on a silly bug and revised the code to be a bit safer.

Also, There is a new function ; uGridDrawTextImage() to draw text in an image cell with the option to clear the cell before, or leave an existing picture in place.

I have not blended the new procedures into the main code because the main code is already about 60,000 characters in size and adding they would pop the 60,000 character upload limit.

Don't forget to install the appropriate decoders...

RichardL

Code: Select all

Procedure uGridDrawFileImage(*uGridCell,state,FileName$,NoScale=#False)   ; Load and scale a file image into cell
  ; Load an image, scale it to fit a specified flip cell and draw
  ; it onto the specified cell. A grid refresh will required
  ; after all images have been drawn.
  
  ; Filename$  : Full path, name and extension.
  ; *uGridCell : Pointer to sructure defining the cell
  ; State      : 0 or 1 to specify the cell's state.
  ; Scaling    : Default is to keeping image aspect ratio correct and providing 
  ;              a border top & bottom or left & right.
  ;              NoScale = #true the image is scaled to fill the cell and may
  ;              appear distorted
  
  Protected IID,IW,Ih,AR_Image.f,cw,ch,AR_Cell.f,X,Y,*CellDat.CELLOPTIONS,T,F.f
  
  
  ; Check for null pointer
  If *uGridCell = 0 : ProcedureReturn #False : EndIf 
  
  ; Load the specified picture image
  If FileSize(FileName$) = 0 : ProcedureReturn #False : EndIf ; No file...
  Debug FileName$
  IID =  LoadImage(#PB_Any,FileName$)
  If IID = 0 :  ProcedureReturn #False :  EndIf               ; Will not load...
  Debug "here................"
  
  ; Get size of picture and calculate the aspect ratio.
  IW = ImageWidth(IID) : Ih = ImageHeight(IID)
  AR_Image = IW/Ih 
  
  *CellDat.CELLOPTIONS = *uGridCell
  With *CellDat
    
    ; Get size of cell and calculate the aspect ratio.
    T = \CellImage[state & 1]
    cw = ImageWidth(T) : ch = ImageHeight(T)
    AR_Cell = cw / ch
    
    ; Calculate scaling factor so major image dimension
    ; fits the cell, then scale the image.
    If NoScale
      ResizeImage(IID,cw,ch)
      X=0 : Y = 0
    Else
      If AR_Image > AR_Cell
        F = IW / cw
        ResizeImage(IID, cw, Ih / F) 
        X = 0 : Y = (ch-ImageHeight(IID))/2
      Else
        F = Ih / ch
        ResizeImage(IID, IW / F, ch)
        X = (cw-ImageWidth(IID))/2 : Y = 0
      EndIf
    EndIf
    
    ; Draw the image on the cell 
    If StartDrawing(ImageOutput(\CellImage[state & 1]))
        Box(0,0,cw,ch,\CellBackColour)
        DrawImage(ImageID(IID),X,Y)
      StopDrawing()
    EndIf
    
  EndWith
  
  FreeImage(IID)
  ProcedureReturn #True
  
EndProcedure

Procedure uGridDrawTextImage(GadNum,*uGridCell,state,Text$,NoClear=#True) ; Render a text string into an image cell. 
  
  ; Gadnum     : Ugrid gadget number
  ; *uGridCell : Pointer to sructure defining the cell
  ; State      : 0 or 1 to specify the cell's state.
  ; Text$      ; String to be rendered. Use '|' to force a new line.
  
  Protected IID,IW,Ih,cw,ch,Y,*CellDat.CELLOPTIONS,T,GridIndex,n,k$
  
  GridIndex    = uGridGetGridIndex(GadNum)           ; Look in table for wanted grid
  If GridIndex = -1 : ProcedureReturn #False : EndIf ; Grid not found
  If *uGridCell = 0 : ProcedureReturn #False : EndIf ; Check for NULL pointer 
  
  *CellDat.CELLOPTIONS = *uGridCell
  With *CellDat
    
    ; Get cell image dimensions
    T = \CellImage[state & 1]
    cw = ImageWidth(T) : ch = ImageHeight(T)
    
    If StartDrawing(ImageOutput(\CellImage[state & 1]))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(uGrid(GridIndex)\GridFontID)
        If NoClear = #False                  ; Optionally....
          Box(0,0,cw,ch,\CellBackColour)     ; clear the cell.
        EndIf
        Y = 0                                ; Initial text position.
        For n = 1 To  CountString(Text$,"|") + 1
          k$ = StringField(Text$,n,"|")      ; Isolate the field,
          DrawText(1,Y,k$,\CellTextColour)   ; show it,
          Y + TextHeight("Xy")               ; move text position down one row.
          If Y+TextHeight("Xy") > \CellH     ; Check there is room for another row...
            Break                            ; NO...!
          EndIf
        Next
      StopDrawing()
    EndIf
  EndWith
  
  ProcedureReturn #True
  
EndProcedure
Here is a code snip that I use to test the above procedures:

Code: Select all

  UseJPEGImageDecoder() ; Define libraries
  
  FileName$ =  "c:\temp\FredIsHere.jpg" 
  T = uGridGetCellPtr(#Gad_Grid3,2,0)
  uGridDrawFileImage(T,0,FileName$)
  
  FileName$ =  "c:\temp\temppic.jpg" 
  T = uGridGetCellPtr(#Gad_Grid3,2,1)
  uGridDrawFileImage(T,0,FileName$,0)
  
  FileName$ =  "c:\temp\tall.jpg" 
  T = uGridGetCellPtr(#Gad_Grid3,2,2)
  uGridDrawFileImage(T,0,FileName$)
  
  FileName$ =  "c:\temp\wide.jpg" 
  T = uGridGetCellPtr(#Gad_Grid3,2,2)
  uGridDrawFileImage(T,1,FileName$)
   
  uGridDrawTextImage(#Gad_Grid3,uGridGetCellPtr(#Gad_Grid3,2,0),0," Hello| World",1)
  uGridDrawTextImage(#Gad_Grid3,uGridGetCellPtr(#Gad_Grid3,2,0),1," Hello| you...",1)
Last edited by RichardL on Fri Jan 24, 2014 12:30 pm, edited 6 times in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by Kwai chang caine »

Thanks i wait justely your code :D
Because i need it for show two pictures side by side in two columns
I believe, i have found a little bug

Code: Select all

  FreeImage(1)
i havze replace by

Code: Select all

 FreeImage(IID)
And that works great. 8)
So i have a question..can i choose the height of the case, because i want enter picture more big

Thanks a lot for your work and sharing, for me and the others 8) 8)
ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by RichardL »

Hi KCC,

Does this snip help...?

Code: Select all

; Demo two state Image cells
With uGridDefaults
  \EditStart     = #PB_EventType_LeftButtonDown  ; Single click selection
  \GridFont     = "Arial"
  \GridFontH   = 12
  \TitleFont     = "Courier"
  \TitleFontH   = 12
  \ImageCellH  = 99  ; Set cell height... used for all cells.
EndWith
RichardL
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by Kwai chang caine »

I have replace the 99 by a number like 200 and not see really the difference :oops:
ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by RichardL »

Good afternoon,

@KCC - Thanks for the PM feedback.

I have updated the image drawing procedure in the earlier posting and also included a new facility to draw text in the image cells.

RichardL
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by Kwai chang caine »

Thanks RICHARDL 8)
ImageThe happiness is a road...
Not a destination
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: MicroGrid ~ Bigger 'Image Cells' (Rev 0.93)

Post by RichardL »

Hi,
I have had too many other jobs to complete to give much time to uGrid, but have now added some new features and fixed some nasty creepy-crawly bugs!

The new features are mainly concerned with SQLite database interactivity. I make no claims to providing anything other than the most basic functions, SQL is not my cup-of-tea and I have added just the things I needed for my amateur radio project plus a few minor extras.

The source file now breaks the 60000 character limit, by about 50%, so I could split it in two or maybe ZIP it with the documentation and post it on DropBox... with a link here.

Please let me know if you are interested.. otherwise I won't bother!

Here are the upgrades since the last version (0.93)

Best regards,
RichardL

Code: Select all

; Rev 0.94
; ======== 
; Mod    - Reworked RowSwap to ONLY move 'variable' data.
; Mod    - Faster caret flash
; Fixed  - Selection Cell border did not clear properly.

; Rev 0.95 - Linking to a SQL data base
;========
; Added - Callback  #uGridEvent_TitleRightClick
; Added = Callback for CNTRL+ A=>Z (Excludes C,V and X, already used.)
; Added - First attempt to make a simple SQL data base that ties in with a uGrid.
;         - uGridCreateSQLiteDB()       Creates a SQLite DB based on uGrid
;         - uGridWriteRowToDatabase()   Writes a line grid => to DB
;         - uGridGetDataBaseEntries()   Reads DB =>grid                                    ***
;         - uGridDbSearch()             Search for an exact pattern match in a specific column.
;         - uGridGetTimeExtent()        provides the 'TimeStamp' values for the first and last DB records.

; Mod   - Callbacks #uGridEvent_CellSelect
;                   #uGridEvent_CellXY          both report PREVIOUS cell pointer in IParam.
; Mod   - Cell's structure now included the Grid's GadgetNumber.

; Rev 0.96 - More SQL stuff.
; ========
; Mod    - uGridCreateSQLiteDB()           Now has optional options string to define the SQLite variable types.
; Added  -  uGridDeleteDatabaseRow()       Delete row from database identified by TimeStamp ***
;        -  uGridUpdateDatabaseRow()       Re-write database row identified by TimeStamp    ***
; NOTE: *** uGridGetDatabaseEntries()      Modified to write the TimeStamp to the Data field of the first cell on each row.
;           The Delete and Update functions REQUIRE the Data field of the first cell to have the record's TimeStamp.
; Mod    -  uGridDbSearch() Now uses 'LIKE' instead of '=' to allow partial match options.


; Added  - Mouse wheel moves selection box up / down. (Great for scrolling SQL database etc!)
;          Exception: If selected cell is list then mouse scrolls the list

; Added  - Callback #uGridEvent_UpLimit    User trying to move selection above top line. It's scrolly time :-)
;        - Callback #uGridEvent_DownLimit  User trying to move selection under bottom line. It's scrolly time :-)

; Added  - uGridSetListCellState(GadNum,X,Y,CellState) Set ListCell text to match 'State'
; Added  - uGridGetListCellState(GadNum,X,Y)           Return offset of text in refernce string.

; Mod    - CNTRLC/V/X now use the ClipBoard() functions instead of local storage. Allows pasting outside uGrid.
;          Needs to have text length limited.

; STILL TO DO... Picture storage is not tested and probably does not work!
Last edited by RichardL on Thu Mar 13, 2014 9:13 pm, edited 1 time in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MicroGrid ~ Does anyone want Rev0.96?

Post by Kwai chang caine »

Me,... i'm always interesting by insert more big picture than the ICO of the native GRID, in each cells of your nice GRID 8)
But i don't know if you had the time to implement this function :oops:
ImageThe happiness is a road...
Not a destination
Post Reply