A little many more code, but ready for testing.
Code: Select all
;-TOP
EnableExplicit
; ***************************************************************************************
; ShowDatabaseItems by mk-soft from 12.02.2017
Procedure GetTextWidth(Text.s, FontID.i = 0)
Static image
Protected result
If Not image
image = CreateImage(#PB_Any, 1, 1)
EndIf
If image And StartDrawing(ImageOutput(image))
If FontID
DrawingFont(FontID)
EndIf
result = TextWidth(Text)
StopDrawing()
EndIf
ProcedureReturn result
EndProcedure
; ---------------------------------------------------------------------------------------
Procedure ClearGadgetColumns(Gadget)
CompilerIf #PB_Compiler_Version <= 551
ClearGadgetItems(Gadget)
While GetGadgetItemText(Gadget, -1, 0)
RemoveGadgetColumn(Gadget, 0)
Wend
CompilerElse
RemoveGadgetColumn(Gadget, #PB_All)
CompilerEndIf
EndProcedure
; ---------------------------------------------------------------------------------------
Procedure CountGadgetColumns(Gadget)
Protected result
CompilerIf #PB_Compiler_Version <= 551
While GetGadgetItemText(Gadget, -1, result)
result + 1
Wend
CompilerElse
result = GetGadgetAttribute(Gadget, #PB_ListIcon_ColumnCount)
CompilerEndIf
ProcedureReturn result
EndProcedure
; ---------------------------------------------------------------------------------------
Procedure FitColumnWidth(Gadget, Column = #PB_All)
Protected columns, rows, col, row, width, len, max, dx
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
If Column = #PB_All
columns = CountGadgetColumns(Gadget) - 1
For col = 0 To columns
SendMessage_(GadgetID(Gadget), #LVM_SETCOLUMNWIDTH, col, #LVSCW_AUTOSIZE_USEHEADER)
Next
Else
SendMessage_(GadgetID(Gadget), #LVM_SETCOLUMNWIDTH, Column, #LVSCW_AUTOSIZE_USEHEADER)
EndIf
CompilerElse
rows = CountGadgetItems(Gadget) - 1
dx = GetTextWidth("X", GetGadgetFont(Gadget))
If Column = #PB_All
columns = CountGadgetColumns(Gadget) - 1
For col = 0 To columns
max = 0
For row = -1 To rows
len = Len(GetGadgetItemText(Gadget, row, col))
If len > max
max = len
EndIf
Next
width = max * dx + 4
SetGadgetItemAttribute(Gadget, 0, #PB_ListIcon_ColumnWidth, width, col)
Next
Else
For row = -1 To rows
len = Len(GetGadgetItemText(Gadget, row, Column))
If len > max
max = len
EndIf
Next
width = max * dx + 4
SetGadgetItemAttribute(Gadget, 0, #PB_ListIcon_ColumnWidth, width, Column)
EndIf
CompilerEndIf
EndProcedure
Procedure ShowDatabaseRows(Gadget, DBase, Hide = #False)
Protected result.i, columns.i, index.i, size.i, text.s
Repeat
If GadgetType(Gadget) <> #PB_GadgetType_ListIcon
Break
EndIf
If Not IsDatabase(DBase)
Break
EndIf
HideGadget(Gadget, Hide)
ClearGadgetColumns(Gadget)
columns = DatabaseColumns(DBase)
For index = 0 To columns - 1
text = DatabaseColumnName(DBase, index)
size = GetTextWidth(text) + 12
AddGadgetColumn(Gadget, index, text, size)
Next
While NextDatabaseRow(DBase)
text = ""
For index = 0 To columns - 1
text + GetDatabaseString(DBase, index) + #LF$
Next
AddGadgetItem(Gadget, -1, text)
Wend
FinishDatabaseQuery(DBase)
HideGadget(Gadget, #False)
result = CountGadgetItems(Gadget)
Until #True
ProcedureReturn result
EndProcedure
; ***************************************************************************************
CompilerIf #PB_Compiler_IsMainFile
; Constant
Enumeration ;Window
#Main
EndEnumeration
Enumeration ; Menu
#Menu
EndEnumeration
Enumeration ; MenuItems
#MenuExitApplication
EndEnumeration
Enumeration ; Gadgets
#List
#Edit
EndEnumeration
Enumeration ; Statusbar
#Status
EndEnumeration
; Global Variable
Global ExitApplication
; Functions
UseSQLiteDatabase()
Procedure CheckDatabaseUpdate(Database, Query$)
Protected Result = DatabaseUpdate(Database, Query$)
If Result = 0
Debug DatabaseError()
EndIf
ProcedureReturn Result
EndProcedure
Procedure CreateDummyDatabase(DBase)
If OpenDatabase(DBase, ":memory:", "", "")
CheckDatabaseUpdate(DBase, "CREATE TABLE food (recid INTEGER PRIMARY KEY ASC, name CHAR(50), weight FLOAT)")
CheckDatabaseUpdate(DBase, "INSERT INTO food (name, weight) VALUES ('apple', '10.005')")
CheckDatabaseUpdate(DBase, "INSERT INTO food (name, weight) VALUES ('pear', '5.9')")
CheckDatabaseUpdate(DBase, "INSERT INTO food (name, weight) VALUES ('banana', '20.35')")
Else
Debug "Can't open database !"
EndIf
EndProcedure
Procedure UpdateWindow()
Protected x, y, dx, dy, menu, status
menu = MenuHeight()
If IsStatusBar(#Status)
status = StatusBarHeight(#Status)
Else
status = 0
EndIf
x = 0
y = 0
dx = WindowWidth(#Main)
dy = WindowHeight(#Main) - menu - status
ResizeGadget(#List, x, y, dx, dy)
EndProcedure
; Main
Procedure Main()
Protected event, style, dx, dy, count
Protected row, recid.s
style = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Main", style)
; Menu
CreateMenu(#Menu, WindowID(#Main))
MenuTitle("Ablage")
MenuItem(#MenuExitApplication, "Be&enden")
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
; Mac default menu
If Not IsMenu(#Menu)
CreateMenu(#Menu, WindowID(#Main))
EndIf
MenuItem(#PB_Menu_About, "")
MenuItem(#PB_Menu_Preferences, "")
CompilerEndIf
; Statusbar
CreateStatusBar(#Status, WindowID(#Main))
AddStatusBarField(#PB_Ignore)
; Gadgets
dx = WindowWidth(#Main)
dy = WindowHeight(#Main) - StatusBarHeight(#Status) - MenuHeight()
ListIconGadget(#List, 0, 0, dx, dy, "recid", 100)
;-Test database
CreateDummyDatabase(0)
If DatabaseQuery(0, "SELECT * FROM food")
;If DatabaseQuery(0, "SELECT * FROM sqlite_master")
count = ShowDatabaseRows(#List, 0, #True)
StatusBarText(#Status, 0, "Items: " + count)
EndIf
FitColumnWidth(#List)
; Bind Evnts
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
; Main Loop
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_Menu
Select EventMenu()
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Case #PB_Menu_About
Case #PB_Menu_Preferences
Case #PB_Menu_Quit
ExitApplication = #True
CompilerEndIf
Case #MenuExitApplication
ExitApplication = #True
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #List
Select EventType()
Case #PB_EventType_LeftDoubleClick
row = GetGadgetState(#List)
recid.s = GetGadgetItemText(#List, row, 0)
If MessageRequester("Stop", "Delete recid " + recid + " from food?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
CheckDatabaseUpdate(0, "DELETE FROM food WHERE recid = " + recid)
If AffectedDatabaseRows(0)
If DatabaseQuery(0, "SELECT * FROM food")
count = ShowDatabaseRows(#List, 0, #True)
StatusBarText(#Status, 0, "Items: " + count)
EndIf
EndIf
EndIf
EndSelect
EndSelect
Case #PB_Event_CloseWindow
Select EventWindow()
Case #Main
ExitApplication = #True
EndSelect
EndSelect
Until ExitApplication
EndIf
EndProcedure : Main()
End
CompilerEndIf