inspired by NetMaestro code:
http://www.purebasic.fr/english/viewtopic.php?t=22209
Code: Select all
#LIdate = "%dd-%mm-%yyyy"
#intDate = "%yyyy-%mm-%dd"
Global LIhwnd.l, LIgadID.l, LIdatlin.l, LIallcols.s, LItcol.s, LIcolold.l, LIupdown.l=1
macro LIcoltype(fld)
fld.s = PeekS(LIDatlin)
select LItcol
case "N": fld = Rset(fld,15,"0") ; numeric col
case "D": fld = FormatDate(#intDate,ParseDate(#LIdate, fld)) ; date col
default : fld = ucase(fld) ; string col
endselect
endmacro
Procedure CompareColClick(item1, item2, lParamSort)
result = 0
lvi.LV_ITEM
lvi\pszText = LIdatlin
lvi\cchTextMax = 512
lvi\Mask = #LVIF_TEXT
lvi\iSubItem = lParamSort
SendMessage_(LIhwnd, #LVM_GETITEMTEXT, item1, @lvi)
LIcoltype(first)
SendMessage_(LIhwnd, #LVM_GETITEMTEXT, item2, @lvi)
LIcoltype(second)
if first > second: result = 1: endif
if first < second: result = -1: endif
if lParamSort = LIcolant: result = result * LIupdown: endif
ProcedureReturn result
EndProcedure
Procedure ColumnClick(hwnd, uMsg, wParam, lParam)
result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_NOTIFY
*msg.NMHDR = lParam
LIhwnd = *msg\hwndFrom
LIgadID = GetDlgCtrlID_(LIhwnd)
if GadgetType(LIgadID) = #PB_GadgetType_ListIcon AND *msg\code = #LVN_COLUMNCLICK
if MessageRequester("Sort", "Do you want to sort by this column?", #PB_MessageRequester_YesNoCancel) = #PB_MessageRequester_Yes
SetCursor_(LoadCursor_(0, #IDC_WAIT))
*pnmv.NM_LISTVIEW = lParam
LIdatlin = AllocateMemory(512)
LItcol = "A"
; here you can check for a lot of ListIcons in your window
if val(StringField(LIallcols,1,":")) = LIgadID
LItcol = mid(StringField(LIallcols,2,":"),*pnmv\iSubItem+1,1)
endif
SendMessage_(LIhwnd, #LVM_SORTITEMSEX, *pnmv\iSubItem, @CompareColClick())
UpdateWindow_(LIhwnd)
FreeMemory(LIdatlin)
if LIcolant = *pnmv\iSubItem
LIupdown = -LIupdown
else: LIupdown = -1: endif
LIcolant = *pnmv\iSubItem
SetCursor_(LoadCursor_(0, #IDC_ARROW))
endif
endIf
EndSelect
ProcedureReturn result
EndProcedure
;-----------------------------------
; MAIN
;-----------------------------------
SetWindowCallback(@ColumnClick())
(some stuff... openwindow ... listicongadget...)
LIallcols = str(#Listicon)+":"+"AANAD"+":" ; String of column types (see macro LIcoltype)
Repeat
WaitWindowEvent
(some stuff)
...
until #PB_Event_CloseWindow
END