Multiline Listicon

Just starting out? Need help? Post your questions and find answers here.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Multiline Listicon

Post by srod »

You just have to reposition some of Sparkie's rectangle calculations.

Code: Select all

; English forum: http://www.purebasic.fr/english/viewtopic.php?t=13226&highlight=
; Author: Sparkie (updated for PB 4.00 by Andre)
; Date: 06. December 2004
; OS: Windows
; Demo: No

; --> Constants

Global gYellowBrush = CreateSolidBrush_(#Yellow)


#LVM_GETSUBITEMRECT = #LVM_FIRST + 56

#DT_END_ELLIPSIS = $8000

#CDDS_ITEM = $10000
#CDDS_PREPAINT = 1
#CDDS_ITEMPREPAINT = #CDDS_ITEM | #CDDS_PREPAINT
#CDDS_SUBITEM = $20000

#CDRF_NOTIFYITEMDRAW = $20
#CDRF_NOTIFYSUBITEMDRAW = $20
#CDRF_SKIPDEFAULT = 4

; --> Windowcallback
Procedure myWindowCallback(hWnd, msg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  Select msg
    Case #WM_NOTIFY
      *lvCD.NMLVCUSTOMDRAW = lParam
      If *lvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *lvCD\nmcd\hdr\code = #NM_CUSTOMDRAW    
        Select *lvCD\nmcd\dwDrawStage
          Case #CDDS_PREPAINT
            ;result = #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT
            result = #CDRF_NOTIFYSUBITEMDRAW;
          Case #CDDS_ITEMPREPAINT | #CDDS_SUBITEM
            thisItem = *lvCD\nmcd\dwItemSpec
            ; --> Define rect for text
            subItemRect.RECT\left = #LVIR_LABEL
            subItemRect.RECT\top = *lvCD\iSubItem
            ; --> Get the subitem rect
            SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, thisItem, @subItemRect)
            subItemText$ = GetGadgetItemText(0, *lvCD\nmcd\dwItemSpec, *lvCD\iSubItem)
            sitLen = Len(subItemText$)
            ; --> Adjust rect for text margins
            lvFlags =  #DT_END_ELLIPSIS | #DT_WORDBREAK
            ; --> Draw our text
            SetBkMode_(*lvCD\nmcd\hDC, #TRANSPARENT)
            If *lvCD\nmcd\dwItemSpec = *lvCD\nmcd\dwItemSpec>>1<<1
              FillRect_(*lvCD\nmcd\hDC, subItemRect, gYellowBrush)
              SetTextColor_(*lvCD\nmcd\hDC, #Red)
            Else
              SetTextColor_(*lvCD\nmcd\hDC, #Blue)
            EndIf
            subItemRect\left +3
            subItemRect\right -3
            subItemRect\bottom -3
            DrawText_(*lvCD\nmcd\hDC, subItemText$, sitLen, subItemRect, lvFlags)
            result = #CDRF_SKIPDEFAULT
        EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 480, 260, "Sparkies Multiline ListIconGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  SetWindowCallback(@myWindowCallback())
  CreateStatusBar(0, WindowID(0))
  ListIconGadget(0, 10, 10, 470, 225, "Column 0", 150, #PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
  AddGadgetColumn(0, 1, "Column 1", 150)
  AddGadgetColumn(0, 2, "Column 2", 150)
  ; --> Create a new imagelist (1x30) to increase row hieght
  ;imageList = ImageList_Create_(1, increase this 15 = 1 line, #ILC_COLOR32, 0, 30)
  imageList = ImageList_Create_(1, 45, #ILC_COLOR32, 0, 30)
  SendMessage_(GadgetID(0), #LVM_SETIMAGELIST, #LVSIL_SMALL, imageList)
  
  ; chr(13) new line required for wordwrap - will not automaticaly wordwrap
  
  For a=0 To 5
    addtext$ = "Locked Column 0 item # " + Str(a) + Chr(13) + "on 2 lines of text."  + Chr(13) + "on 3 lines of text."+ Chr(13) + Chr(10) + "Column 1 item #" + Str(a) + " on 1 line" + Chr(13) + "Column 1 item #" +Chr(10) + "Column 2 item #" + Str(a) + " on 1 line"
    atLen = Len(addtext$)
    AddGadgetItem(0, -1, addtext$)
    ; If varbackground$ = "white"
    ; SetGadgetItemColor(0,  a, #PB_Gadget_BackColor,  $DCFDEE, -1)
    ; varbackground$ = "green"
    ; Else
    ; SetGadgetItemColor(0,  a, #PB_Gadget_BackColor,  $FFFFFF, -1)
    ; varbackground$ = "white"
    ; EndIf
  Next
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            ; --> Display selected ListIconGadget index in statusbar
;            StatusBarText(0, 0, "Selected item index is: " + Str(GetGadgetState(0)))
        EndSelect
    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf
End

**EDIT : do not create a brush in the painting procedure without then deleting it before you exit! That will give you one horrendous memory leak.
I may look like a mule, but I'm not a complete ass.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Multiline Listicon

Post by captain_skank »

srod wrote: **EDIT : do not create a brush in the painting procedure without then deleting it before you exit! That will give you one horrendous memory leak.
Just found that out :oops:

Thanks for all your help guys and thanks to sparkie for the original code.
Post Reply