ListIconGadget Colum Header color?

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...

Post by srod »

You either have to use 'owner drawn' methods or what is called 'custom draw'.

In the code below, I use the easier of the two (custom draw) to alternate red text, blue background and blue text, red background etc.

I've put some checks in for button states etc.

Code: Select all

;Coloured header control.
;By srod.
;Purebasic 4.
;Windows.


#LVM_GETHEADER = #LVM_FIRST + 31 

; Globals 
Global oldListIconCallback, hHeader, redbrush, bluebrush 
redbrush=CreateSolidBrush_(#Red)
bluebrush=CreateSolidBrush_(#Blue)


; Proc for subclassed ListIconGadget 
Procedure SubclassedListIcon(hwnd, msg, wparam, lparam) 
  Protected hdi.hd_item
  result = CallWindowProc_(oldListIconCallback, hwnd, msg, wparam, lparam) 
  Select msg 
    Case #WM_NOTIFY 
      *pnmh.NMHDR = lparam 
      ;--> Get handle to ListIcon header control 
      If *pnmh\code = #NM_CUSTOMDRAW
        *pnmcd.NMCUSTOMDRAW = lparam 
        ;--> Determine drawing stage 
        Select *pnmcd\dwDrawStage 
          Case #CDDS_PREPAINT 
            result = #CDRF_NOTIFYITEMDRAW 
          Case #CDDS_ITEMPREPAINT 
;Get header text.
            text$=Space(100)
            hdi\mask = #HDI_TEXT
            hdi\psztext = @text$
            hdi\cchtextmax = Len(text$)
            SendMessage_(hHeader, #HDM_GETITEM,*pnmcd\dwItemSpec,hdi)
;Check button state.
            If *pnmcd\uItemState & #CDIS_SELECTED
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH|#DFCS_PUSHED)
;Offset text because of the selected button.
              InflateRect_(*pnmcd\rc,-1,-1)
            Else
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH)
            EndIf
;Draw background.
;Here we alternate red text on blue background.
            InflateRect_(*pnmcd\rc,-1,-1)
            SetBkMode_(*pnmcd\hdc,#TRANSPARENT) 
            If *pnmcd\dwItemSpec&1
              FillRect_(*pnmcd\hdc, *pnmcd\rc, redbrush)
              SetTextColor_(*pnmcd\hdc, #Blue) 
            Else
              FillRect_(*pnmcd\hdc, *pnmcd\rc, bluebrush)
              SetTextColor_(*pnmcd\hdc, #Red) 
            EndIf
            DrawText_(*pnmcd\hdc, @text$, Len(text$), *pnmcd\rc, #DT_CENTER|#DT_VCENTER|#DT_END_ELLIPSIS)
            result = #CDRF_SKIPDEFAULT
        EndSelect 
      EndIf 
  EndSelect 
  ProcedureReturn result 
EndProcedure 


; ************************************************ 
; Main Window 
; ************************************************ 
If OpenWindow(0, 100, 100, 415, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  ListIconGadget(0, 5, 5, 405, 200, "col 0", 50, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection) 
  hHeader = SendMessage_(GadgetID(0), #LVM_GETHEADER, 0, 0) 
;Subclass ListIcon so we can customdraw the header text 
  oldListIconCallback = SetWindowLong_(GadgetID(0), #GWL_WNDPROC, @SubclassedListIcon()) 

;Add 10 more columns.
  For i = 1 To 10
    AddGadgetColumn(0, i, "col "+Str(i), 50) 
  Next  
;Add some data
  For b=0 To 99; Add 100 rows.
    AddGadgetItem(0,-1,"")
  Next
  For i = 0 To 99
    For j = 0 To 50
      SetGadgetItemText(0,i,Str(i+j),j)
    Next j
  Next i            

  Repeat 
    EventID = WaitWindowEvent() 
  Until EventID = #PB_Event_CloseWindow 

  DeleteObject_(redbrush)
  DeleteObject_(bluebrush)
  
EndIf 
End 
Hope it helps.
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Help! (grin)

Post by Fangbeast »

Predictably enough, I am in trouble again from not knowing exactly what I am doing here. I have the colour working correctly (now the same as the rest of my form!!) but am getting no heading text and I suspect that is because all my columns are different widths.

Am I right in assuming that your callback is for fixed width columns and if so, how may I fix this? My column structure is below.

Title 270
Borrowed 100
Due date 100
Returned 100
Borrowed by 200
Record 0
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The column widths will not effect the code at all.

You'll need to post some code.
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Good work srod! Image
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks. A shame the PB SetGadgetColor() command doesn't do this. Maybe one for the feature request forum?
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

srod wrote:The column widths will not effect the code at all.

You'll need to post some code.
I was afraid you'd say that (sigh). The callback below is only modified to allow all collumns to be light blue with text in black (no alternating red and blue). The blue colour works, the ellipsis effect shows but there is no text.

Bah, I should give up code whacking.

Code: Select all

Procedure SubclassedListIcon(hwnd, msg, wparam, lparam)
  Protected hdi.hd_item
  result = CallWindowProc_(form\oldListIconCallback, hwnd, msg, wparam, lparam)
  Select msg
    Case #WM_NOTIFY
      *pnmh.NMHDR = lparam    ;--> Get handle to ListIcon header control
      If *pnmh\code = #NM_CUSTOMDRAW
        *pnmcd.NMCUSTOMDRAW = lparam ;--> Determine drawing stage
        Select *pnmcd\dwDrawStage
          Case #CDDS_PREPAINT
            result = #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT ; Get header text.
            text$ = Space(100)
            hdi\mask = #HDI_TEXT
            hdi\psztext = @text$
            hdi\cchtextmax = Len(text$)
            SendMessage_(hHeader, #HDM_GETITEM, *pnmcd\dwItemSpec, hdi) ; Check button state.
            If *pnmcd\uItemState & #CDIS_SELECTED
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH | #DFCS_PUSHED)  ; Offset text because of the selected button.
              InflateRect_(*pnmcd\rc, -1, -1)
            Else
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH)
            EndIf ; Draw background. ; Here we alternate red text on blue background.
            InflateRect_(*pnmcd\rc, -1, -1)
            SetBkMode_(*pnmcd\hdc, #TRANSPARENT)
            ;If *pnmcd\dwItemSpec & 1
              FillRect_(*pnmcd\hdc, *pnmcd\rc, Colour)  ; Colour (Global Colour = CreateSolidBrush_(16629401))
              SetTextColor_(*pnmcd\hdc, $000000)
            ;Else
            ;  FillRect_(*pnmcd\hdc, *pnmcd\rc, Colour)
            ;  SetTextColor_(*pnmcd\hdc, $000000)
            ;EndIf
            DrawText_(*pnmcd\hdc, @text$, Len(text$), *pnmcd\rc, #DT_CENTER | #DT_VCENTER | #DT_END_ELLIPSIS)
            result = #CDRF_SKIPDEFAULT
        EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure

Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Without more code it's difficult to comment, but my guess from what you've described is that in the line

Code: Select all

SendMessage_(hHeader, #HDM_GETITEM, *pnmcd\dwItemSpec, hdi) ; Check button state. 
the 'hHeader' variable is not pointing to the correct header control.

You could switch this for

Code: Select all

SendMessage_(*pnmcd\hdr\hwndFrom, #HDM_GETITEM,*pnmcd\dwItemSpec,hdi) 
to see if this makes any difference.
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

Doh!!! (Bang's head solidly on desk for an hour). You were so right. (Repeats to himself a thousand times, "You will not skip lunch, dinner and then stay up till 2am every morning coding, you old fool).

"the 'hHeader' variable is not pointing to the correct header control" That fixed it.

Thank you a million times.

/me bows to the mighty useful and extremely sagacious master srod and then slinks away with his tail between his legs
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

:lol:

If you're using the alternative line given above, then you can dispense with the hHeader variable completely.
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

I'm using the above code to give all my listicongadget headings on the main form the same colour (uniformity makes the form look better) and I've had to use one callback per gadget as I don't know the windows API well enough to combine them all.

Anyhow, it looks good.

Funnily enough, I wanted to do the same thing for a ListIconGadget in another window and PB4 keeps coming up with "Syntax Error", even though I used exactly the same technique.

That will teach me to put my finger in the spinning wood router bit. When it heals, I bet I'll be able to work it out (maybe).

(For some reason, I accidentally put "not" in there before. Can't even spell now!!!)
Last edited by Fangbeast on Thu Oct 12, 2006 3:28 pm, edited 1 time in total.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Fangbeast wrote:"You will not skip lunch, dinner and then stay up till 2am every morning coding, you old fool"
of all people, you shouldn't live like that, and you know it! Take care of yourself, you old fool!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply