ListIcon highlight portion of Cell

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

ListIcon highlight portion of Cell

Post by IdeasVacuum »

So, I am displaying the results of a string search in a ListIcon. I am showing each whole line that contains the string searched for. Without disturbing existing niceties such as grid-lines and alternate row colours (alternate per file), it would be good to highlight the search string thus:

Image

Relatively easy to do in Editor/Scintilla gadgets, but has anyone achieved the same for a ListIcon? Does it require kilometers of code?
Edit: Or changing the text colour would be good, à la Android.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: ListIcon highlight portion of Cell

Post by netmaestro »

See what you can use from this:

Code: Select all

; Owner draw listview for the purpose of selective styling
; Lloyd Gallant (netmaestro) December 2016

Global search$ = "TIM"

Procedure MainWindowCallBack(hwnd, msg, wparam, lparam) 
  result = #PB_ProcessPureBasicEvents 
  
  Select msg 
    Case #WM_DRAWITEM
      *lpdis.DRAWITEMSTRUCT = lparam 
      Dim itemrect.RECT(3) 
      For i = 1 To 3 
        RtlZeroMemory_(@itemrect(i), SizeOf(RECT)) 
        With itemrect(i)
          \top = i 
          \left = #LVIR_BOUNDS
        EndWith
        SendMessage_(*lpdis\hwndItem, #LVM_GETSUBITEMRECT, *lpdis\itemid, @itemrect(i)) 
        text$ = GetGadgetItemText(GetDlgCtrlID_(*lpdis\hwndItem), *lpdis\itemid, i) 
        loc = FindString(text$, search$, 1, #PB_String_NoCase)
        If loc > 0
          found$ = Mid(text$, loc, Len(search$))
          If loc > 1
            leftside$ = Left(text$, loc-1)
          Else
            leftside$ = ""
          EndIf
          GetTextExtentPoint32_(*lpdis\hDC, leftside$, Len(leftside$), @sz.SIZE)
          start = sz\cx + 2
          GetTextExtentPoint32_(*lpdis\hDC, found$, Len(found$), @sz.SIZE)
          searchwidth = sz\cx 
          height = sz\cy
          SetRect_(highlight.RECT, start+itemrect(i)\left, itemrect(i)\top+3, start+itemrect(i)\left+searchwidth, itemrect(i)\top+height)
        Else
          start=0
        EndIf
        If *lpdis\itemState & #ODS_SELECTED
          hbrBackground = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))
          FillRect_(*lpdis\hdc, itemrect(i), hbrBackground)
          DrawFocusRect_(*lpdis\hdc, itemrect(i))
          SetTextColor_(*lpdis\hdc, GetSysColor_(#COLOR_HIGHLIGHTTEXT))
        Else
          hbrBackground = CreateSolidBrush_(GetSysColor_(#COLOR_WINDOW))
          FillRect_(*lpdis\hdc, itemrect(i), hbrBackground)
          If start
            hbrHighlight = CreateSolidBrush_(RGB(255,255,0))
            FillRect_(*lpdis\hdc, highlight, hbrHighlight)
            DeleteObject_(hbrHighlight)
          EndIf
          SetTextColor_(*lpdis\hdc, GetSysColor_(#COLOR_WINDOWTEXT))
        EndIf
        DeleteObject_(hbrBackground)
        SetBkMode_(*lpdis\hDC, #TRANSPARENT)
        TextOut_(*lpdis\hDC, itemrect(i)\left+2, itemrect(i)\top+2, text$, Len(text$)) 
      Next 
      
    Case #WM_MEASUREITEM 
      *lpmis.MEASUREITEMSTRUCT = lparam 
      *lpmis\itemheight = 20 
      
  EndSelect 
  
  ProcedureReturn result 
  
EndProcedure 

OpenWindow(0,0,0,320,240,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
SetWindowCallback(@MainWindowCallBack()) 

ListIconGadget(0,0,0,320,240,"",0,#PB_ListIcon_GridLines|#LVS_OWNERDRAWFIXED) 
AddGadgetColumn(0,1,"FirstName",100) 
AddGadgetColumn(0,2,"LastName",100) 
AddGadgetColumn(0,3,"City",115) 
AddGadgetItem(0, -1, Chr(10) + "Lloyd" + Chr(10) + "Gallant" + Chr(10) + "Barrie" ) 
AddGadgetItem(0, -1, Chr(10) + "Mortimer" + Chr(10) + "Penrose" + Chr(10)  + "Kitchener" ) 
AddGadgetItem(0, -1, Chr(10) + "Mark" + Chr(10) + "Dutton" + Chr(10)   + "Guelph" ) 
AddGadgetItem(0, -1, Chr(10) + "Timothy" + Chr(10) + "Knechtel" + Chr(10)  + "East Timmins") 

Repeat 
  EventID = WaitWindowEvent() 
Until EventID = #PB_Event_CloseWindow 
I didn't handle selected lines and I didn't look for more than one occurrence of the search string in a cell. But you can easily add those functionalities yourself, I'm trying to keep the demo as simple as possible.
BERESHEIT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ListIcon highlight portion of Cell

Post by IdeasVacuum »

That is excellent netmaestro thank you!
I have crudely fluffed-up the gui, most things are retained except alternate row colouring, I can work on that.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply