Multi-Line TreeGadget [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Multi-Line TreeGadget [Windows]

Post by RASHAD »

Happy new year guys :)

Code: Select all

#TVM_SETITEMHEIGHT  = $111B
#TVM_GETITEMHEIGHT  = $111C
#TVM_SETBKCOLOR     = $111D

Global hBrush,hBrush2,TVH

hBrush = CreateSolidBrush_($D7FEFE)
hBrush2 = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))

Procedure WCallback(hWnd,uMsg,wParam,lParam) 
  result=#PB_ProcessPureBasicEvents 
  Select uMsg
    Case  #WM_NOTIFY 
      *tvCD.NMTVCUSTOMDRAW=lParam 
      If *tvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *tvCD\nmcd\hdr\code = #NM_CUSTOMDRAW    
        Select *tvCD\nmcd\dwDrawStage 
          Case #CDDS_PREPAINT:ProcedureReturn #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT: ProcedureReturn #CDRF_NOTIFYPOSTPAINT
          Case #CDDS_ITEMPOSTPAINT 
            Item=*tvCD\nmcd\dwItemSpec
            tvr.RECT\left=Item
            SendMessage_(GadgetID(0),#TVM_GETITEMRECT,#True,@tvr)            
            tvi.TV_ITEM 
            tvi$ = Space(#MAX_PATH) 
            tvi\mask = #TVIF_HANDLE | #TVIF_PARAM | #TVIF_TEXT 
            tvi\pszText = @tvi$ 
            tvi\cchTextMax = #MAX_PATH 
            tvi\hItem=Item
            SendMessage_(GadgetID(0),#TVM_GETITEM,0,tvi)
            SetBkMode_(*tvCD\nmcd\hdc,#TRANSPARENT)
            If *tvCD\nmcd\uItemState = #CDIS_FOCUS | #CDIS_SELECTED
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush2)
              SetTextColor_(*tvCD\nmcd\hdc,$FFFFFF)
            Else
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush)
              SetTextColor_(*tvCD\nmcd\hdc,0)
            EndIf
            If FindString(tvi$,#CRLF$) > 0
               tvr\left + 2
               DrawText_(*tvCD\nmcd\hdc,tvi$,Len(tvi$),tvr,#DT_WORDBREAK ) 
            Else
               TextOut_(*tvCD\nmcd\hdc,tvr\left+2,tvr\top+14,tvi$,Len(tvi$))
            EndIf            
            ProcedureReturn #CDRF_NEWFONT            
        EndSelect 
      EndIf 
  EndSelect 
  ProcedureReturn result 
EndProcedure 

LoadFont(0,"Tahoma",14)

If OpenWindow(0,0,0,600,400,"TreeGadget",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  TreeGadget(0, 10,10,580,380)
  SendMessage_(GadgetID(0), #TVM_SETBKCOLOR, 0, $D7FEFE)
  SetGadgetFont( 0,FontID(0))
  TVH = SendMessage_(GadgetID(0),#TVM_GETITEMHEIGHT,0,0)
  SendMessage_(GadgetID(0),#TVM_SETITEMHEIGHT,TVH*2,0)
  AddGadgetItem (0,-1,"First Main Node"+#CRLF$+"Happ New Year",0,0)
  AddGadgetItem (0,-1,"Sub Item!"+#CRLF$+"RASHAD",0,1) 
  AddGadgetItem (0,-1,"Second Main Level Node"+#CRLF$+"Happy New Year",0,0)
  AddGadgetItem(0,-1,"Sub text",0,1) 
  AddGadgetItem(0,-1,"Third Node")
  
  SetWindowCallback(@WCallback()) 
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
          Quit = 1
    EndSelect
  Until Quit = 1 
  DeleteObject_(hBrush)
  DeleteObject_(hBrush2)
EndIf
Egypt my love
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Multi-Line TreeGadget [Windows]

Post by RSBasic »

Nice code. Thank you for sharing. :)
Image
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Multi-Line TreeGadget [Windows]

Post by IdeasVacuum »

Hi Rashad! Now that is clever code 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Multi-Line TreeGadget [Windows]

Post by RASHAD »

Happy New Year gentlemen
And thank you very much
Egypt my love
Learner
User
User
Posts: 15
Joined: Tue Dec 14, 2004 1:32 am
Location: Australia

Re: Multi-Line TreeGadget [Windows]

Post by Learner »

Very Nice thanks... Just what I needed...

All the best.. :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Multi-Line TreeGadget [Windows]

Post by RASHAD »

Hi Learner
Next is a more advanced and precise version hope you like it

Code: Select all

#TVM_SETITEMHEIGHT = #TV_FIRST + 27 
#TVM_GETITEMHEIGHT = #TV_FIRST + 28
#TVM_SETBKCOLOR = #TV_FIRST + 29
#TVM_MAPACCIDTOHTREEITEM = #TV_FIRST + 42
#TVM_MAPHTREEITEMTOACCID = #TV_FIRST + 43 

Global hBrush,hBrush2,TVH,fSize

hBrush = CreateSolidBrush_($D7FEFE)
hBrush2 = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))

Procedure WCallback(hWnd,uMsg,wParam,lParam) 
  result=#PB_ProcessPureBasicEvents 
  Select uMsg
    Case  #WM_NOTIFY 
      *tvCD.NMTVCUSTOMDRAW=lParam 
      If *tvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *tvCD\nmcd\hdr\code = #NM_CUSTOMDRAW    
        Select *tvCD\nmcd\dwDrawStage 
          Case #CDDS_PREPAINT:ProcedureReturn #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT: ProcedureReturn #CDRF_NOTIFYPOSTPAINT
          Case #CDDS_ITEMPOSTPAINT 
            Item=*tvCD\nmcd\dwItemSpec
            ItemID = SendMessage_(GadgetID(0),#TVM_MAPHTREEITEMTOACCID,Item,0)                     
            tvi.TVITEM
            tvi$ = Space(#MAX_PATH) 
            tvi\mask = #TVIF_HANDLE | #TVIF_PARAM | #TVIF_TEXT  
            tvi\pszText = @tvi$ 
            tvi\cchTextMax = #MAX_PATH 
            tvi\hItem = Item
            SendMessage_(GadgetID(0),#TVM_GETITEM,0,tvi)
            If ItemID = 2 Or ItemID = 4
              tvi\mask = #TVIF_HANDLE | #TVIF_STATE  
              tvi\hItem = Item
              tvi\state = 0  
              tvi\stateMask = #TVIS_STATEIMAGEMASK
              SendMessage_(GadgetID(0),#TVM_SETITEM,0,@tvi)
            EndIf  
            SetBkMode_(*tvCD\nmcd\hdc,#TRANSPARENT)
            
            tvr.RECT\left=Item
            SendMessage_(GadgetID(0),#TVM_GETITEMRECT,#True,@tvr)
            If *tvCD\nmcd\uItemState = #CDIS_FOCUS | #CDIS_SELECTED
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush2)
              SetTextColor_(*tvCD\nmcd\hdc,$FFFFFF)
            Else
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush)
              SetTextColor_(*tvCD\nmcd\hdc,0)
            EndIf
            If FindString(tvi$,#CRLF$) > 0
               tvr\left + 2
               tvr\top + 2
               DrawText_(*tvCD\nmcd\hdc,tvi$,Len(tvi$),tvr,#DT_WORDBREAK ) 
            Else
               TextOut_(*tvCD\nmcd\hdc,tvr\left+1,tvr\top+fSize,tvi$,Len(tvi$))
            EndIf            
            ProcedureReturn #CDRF_NEWFONT            
        EndSelect 
      EndIf 
  EndSelect 
  ProcedureReturn result 
EndProcedure 

fSize = 14
ftn = LoadFont(#PB_Any,"Georgia",fSize)

If OpenWindow(0,0,0,600,400,"TreeGadget",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  TreeGadget(0, 10,10,580,380,#TVS_CHECKBOXES)
  SendMessage_(GadgetID(0), #TVM_SETBKCOLOR, 0, $D7FEFE)
  SetGadgetFont( 0,FontID(ftn))
  If GetGadgetFont(0) <> FontID(ftn)
     fSize = 9
  EndIf
  TVH = SendMessage_(GadgetID(0),#TVM_GETITEMHEIGHT,0,0)
  SendMessage_(GadgetID(0),#TVM_SETITEMHEIGHT,TVH*2,0)
  AddGadgetItem (0,-1,"First Main Node"+#CRLF$+"Happ New Year",0,0)
  AddGadgetItem (0,-1,"Sub Item!"+#CRLF$+"RASHAD",0,1) 
  AddGadgetItem (0,-1,"Second Main Level Node"+#CRLF$+"Happy New Year",0,0)
  AddGadgetItem(0,-1,"Sub text",0,1) 
  AddGadgetItem(0,-1,"Third Node")
  
  ;Debug GetWindowLongPtr_(GadgetID(0),#GWL_STYLE) & #TVS_CHECKBOXES
  
  SetWindowCallback(@WCallback()) 
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
          Quit = 1
          
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            ;Debug GetGadgetItemText(0,GetGadgetState(0))
        EndSelect    
    EndSelect
  Until Quit = 1 
  DeleteObject_(hBrush)
  DeleteObject_(hBrush2)
EndIf

Egypt my love
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: Multi-Line TreeGadget [Windows]

Post by ElementE »

Here is RASHAD's first program with comments added.
I hope some might find it useful in understanding Windows programming.

Code: Select all

#TVM_SETITEMHEIGHT  = $111B
#TVM_GETITEMHEIGHT  = $111C
#TVM_SETBKCOLOR     = $111D

Global hBrush,hBrush2,TVH

; The CreateSolidBrush function creates a logical brush
; that has the specified solid color.
; When you no longer need the HBRUSH object, call the
; DeleteObject function To delete it.
hBrush = CreateSolidBrush_($D7FEFE)
; GetSysColor
; Retrieves the current color of the specified display element.
; Display elements are the parts of a window And the display
; that appear on the system display screen.
; COLOR_HIGHLIGHT - Item(s) selected in a control.
hBrush2 = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))

Procedure WCallback(hWnd,uMsg,wParam,lParam)
  result=#PB_ProcessPureBasicEvents
  Select uMsg
    Case  #WM_NOTIFY
;         Structure NMTVCUSTOMDRAW -Contains information specific to an
;                                   NM_CUSTOMDRAW (tree view) notification code
;                                   sent by a tree-view control.
;             nmcd.NMCUSTOMDRAW  - Structure that contains general custom draw
;                                  information
;                                  |  Structure NMCUSTOMDRAW
;                                  |    hdr.NMHDR - Contains information about
;                                  |                a notification message.
;                                  |                |  Structure NMHDR
;                                  |                |    hwndFrom.i - A window handle to
;                                  |                |                 the control sending
;                                  |                |                 the message.   
;                                  |                |    idFrom.i   - An identifier of the
;                                  |                |                 control sending the message.
;                                  |                |    code.l     - A notification code.
;                                  |                |  EndStructure
;                                  |    dwDrawStage.l - The current drawing stage.
;                                  |    hdc.i         - A handle to the control's device context.
;                                  |                    Use this HDC (Handle to Device Context)
;                                  |                    to perform any GDI functions.
;                                  |    rc.RECT       - The RECT structure that describes the bounding
;                                  |                    rectangle of the area being drawn.
;                                  |                      Structure RECT
;                                  |                      | left.l
;                                  |                      | top.l
;                                  |                      | right.l
;                                  |                      | bottom.l
;                                  |                      EndStructur
;                                  |    dwItemSpec.i  - The item number.
;                                  |    uItemState.l  - The current item state.
;                                  |    lItemlParam.i - Application-defined item data.
;                                  |  EndStructure
;            clrText.l         - COLORREF value representing the color that will
;                                be used to display text foreground in the
;                                tree-view control.
;            clrTextBk.l       - COLORREF value representing the color that will
;                                be used To display text background in the
;                                tree-view control.
;            iLevel.l          - (int) Zero-based level of the item being drawn.
;                                The root item is at level zero, a child of the
;                                root item is at level one, And so on.
;         EndStructure     
      *tvCD.NMTVCUSTOMDRAW=lParam
;       #NM_CUSTOMDRAW - Notifies a control's parent window about custom drawing operations.
;                        This notification code is sent in the form of a WM_NOTIFY message.
      If *tvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *tvCD\nmcd\hdr\code = #NM_CUSTOMDRAW   
        Select *tvCD\nmcd\dwDrawStage
;           #CDDS_PREPAINT - Before the paint cycle begins.
;           #CDRF_NOTIFYITEMDRAW - The control will notify the parent of any item-related drawing operations.
;                                  It will send NM_CUSTOMDRAW notification codes before And after drawing items.
          Case #CDDS_PREPAINT:ProcedureReturn #CDRF_NOTIFYITEMDRAW
;           #CDDS_ITEMPREPAINT - Before an item is drawn.
;           #CDRF_NOTIFYPOSTPAINT - The control will notify the parent after painting an item.           
          Case #CDDS_ITEMPREPAINT: ProcedureReturn #CDRF_NOTIFYPOSTPAINT
;           #CDDS_ITEMPOSTPAINT -  After an item has been drawn.           
          Case #CDDS_ITEMPOSTPAINT
            ; The dwItemSpec member of the nmcd structure contains the handle of the item being drawn. 
            Item=*tvCD\nmcd\dwItemSpec
            tvr.RECT\left=Item
;             TVM_GETITEMRECT message - Retrieves the bounding rectangle for a tree-view
;                                       item and indicates whether the item is visible.
;                  Parameters: wParam - Value specifying the portion of the item
;                                       for which to retrieve the bounding rectangle.
;                                       If this parameter is TRUE, the bounding rectangle
;                                       includes only the text of the item. Otherwise,
;                                       it includes the entire line that the item occupies
;                                       in the tree-view control.
;                              lParam - Pointer to a RECT Structure that, when sending the
;                                       message, contains the handle of the item to retrieve
;                                       the rectangle for. After returning from the message,
;                                       this parameter contains the bounding rectangle.
;                                       The coordinates are relative To the upper-left corner
;                                       of the tree-view control. 
            SendMessage_(GadgetID(0),#TVM_GETITEMRECT,#True,@tvr)           
            tvi.TV_ITEM  ; Specifies or receives attributes of a tree-view item.
;                            Structure TV_ITEM
;                            | mask.l - Array of flags that indicate which of the other structure
;                            |          members contain valid Data.
;                            | hItem.i - Handle to the item.
;                            | state.l - Set of bit flags and image list indexes that indicate the
;                            |           item's state.
;                            | stateMask.l - Bits of the state member that are valid.
;                            | *pszText - Pointer to a null-terminated string that contains the
;                            |            item text If the Structure specifies item attributes.
;                            | cchTextMax.l - Size of the buffer pointed to by the pszText member,
;                            |                in characters.
;                            | iImage.l - Index in the tree-view control's image list of the icon
;                            |            image To use when the item is in the nonselected state.
;                            | iSelectedImage.l - Index in the tree-view control's image list of
;                            |                    the icon image To use when the item is in the
;                            |                    selected state
;                            | cChildren.l - lag that indicates whether the item has associated
;                            |               child items.
;                            | lParam.i - A value to associate with the item.
;                            EndStructure
            tvi$ = Space(#MAX_PATH) ; #MAX_PATH = 260
;             TVIF_HANDLE - The hItem member is valid.
;             TVIF_PARAM  - The lParam member is valid.
;             TVIF_TEXT   - The pszText And cchTextMax members are valid.           
            tvi\mask = #TVIF_HANDLE | #TVIF_PARAM | #TVIF_TEXT
            tvi\pszText = @tvi$
            tvi\cchTextMax = #MAX_PATH
            tvi\hItem=Item
            ; TVM_GETITEM message - Retrieves some or all of a tree-view item's attributes.
            SendMessage_(GadgetID(0),#TVM_GETITEM,0,tvi)
            ; SetBkMode - Sets the background mix mode of the specified device context.
            ; #TRANSPARENT - Background remains untouched.
            SetBkMode_(*tvCD\nmcd\hdc,#TRANSPARENT)
            ; #CDIS_FOCUS - The item is in focus.
            ; #CDIS_SELECTED - The item is selected.
            If *tvCD\nmcd\uItemState = #CDIS_FOCUS | #CDIS_SELECTED
              ; FillRect - Fills a rectangle by using the specified brush.
              ;            This function includes the left And top borders, but excludes
              ;            the right And bottom borders of the rectangle.
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush2)
              ; SetTextColor - Sets the text color for the specified device context
              ;                to the specified color.
              SetTextColor_(*tvCD\nmcd\hdc,$FFFFFF)
            Else
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush)
              SetTextColor_(*tvCD\nmcd\hdc,0)
            EndIf
            If FindString(tvi$,#CRLF$) > 0
               tvr\left + 2
               ; DrawText - Draws formatted text in the specified rectangle.
               ; #DT_WORDBREAK - Breaks words. Lines are automatically broken between
               ;                 words If a word would extend past the edge of the rectangle
               ;                 specified by the lpRect parameter. A carriage Return-line
               ;                 feed sequence also breaks the line.
               ;                 If this is not specified, output is on one line.
               DrawText_(*tvCD\nmcd\hdc,tvi$,Len(tvi$),tvr,#DT_WORDBREAK )
            Else
               ; TextOut - Writes a character string at the specified location,
               ;           using the currently selected font, background color, And text color.
               TextOut_(*tvCD\nmcd\hdc,tvr\left+2,tvr\top+14,tvi$,Len(tvi$))
            EndIf 
            ; #CDRF_NEWFONT - The application specified a new font for the item         
            ProcedureReturn #CDRF_NEWFONT           
        EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure

LoadFont(0,"Tahoma",14)

If OpenWindow(0,0,0,600,400,"TreeGadget",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  TreeGadget(0, 10,10,580,380)
  ; TVM_SETBKCOLOR message - Sets the background color of the control.
  SendMessage_(GadgetID(0), #TVM_SETBKCOLOR, 0, $D7FEFE)
  SetGadgetFont( 0,FontID(0))
  ; TVM_GETITEMHEIGHT message - Retrieves the current height of the each tree-view item.
  TVH = SendMessage_(GadgetID(0),#TVM_GETITEMHEIGHT,0,0)
  ; TVM_SETITEMHEIGHT message - Sets the height of the tree-view items.
  SendMessage_(GadgetID(0),#TVM_SETITEMHEIGHT,TVH*2,0)
  AddGadgetItem (0,-1,"First Main Node"+#CRLF$+"Happ New Year",0,0)
  AddGadgetItem (0,-1,"Sub Item!"+#CRLF$+"RASHAD",0,1)
  AddGadgetItem (0,-1,"Second Main Level Node"+#CRLF$+"Happy New Year",0,0)
  AddGadgetItem(0,-1,"Sub text",0,1)
  AddGadgetItem(0,-1,"Third Node")
 
  SetWindowCallback(@WCallback())
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
          Quit = 1
    EndSelect
  Until Quit = 1
  DeleteObject_(hBrush)
  DeleteObject_(hBrush2)
EndIf
Think Unicode!
Learner
User
User
Posts: 15
Joined: Tue Dec 14, 2004 1:32 am
Location: Australia

Re: Multi-Line TreeGadget [Windows]

Post by Learner »

I've added some code to accommodate what I'm trying to do... Almost there but not quite...:)

Thanks again Rashad and Elemente.

Code: Select all

; MultiLine Treegadget
; Rashad 19/12/15
;
#TVM_SETITEMHEIGHT  = $111B
#TVM_GETITEMHEIGHT  = $111C
#TVM_SETBKCOLOR     = $111D
#MULTILINEWIDTH = 40
;#TOTLINES= 4
Global hBrush,hBrush2,TVH

hBrush = CreateSolidBrush_($D7FEFE)
hBrush2 = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))

Procedure WCallback(hWnd,uMsg,wParam,lParam)
  result=#PB_ProcessPureBasicEvents
  Select uMsg
    Case  #WM_NOTIFY
      *tvCD.NMTVCUSTOMDRAW=lParam
      If *tvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *tvCD\nmcd\hdr\code = #NM_CUSTOMDRAW   
        Select *tvCD\nmcd\dwDrawStage
          Case #CDDS_PREPAINT:ProcedureReturn #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT: ProcedureReturn #CDRF_NOTIFYPOSTPAINT
          Case #CDDS_ITEMPOSTPAINT
            Item=*tvCD\nmcd\dwItemSpec
            tvr.RECT\left=Item
            SendMessage_(GadgetID(0),#TVM_GETITEMRECT,#True,@tvr)           
            tvi.TV_ITEM
            tvi$ = Space(#MAX_PATH)
            tvi\mask = #TVIF_HANDLE | #TVIF_PARAM | #TVIF_TEXT
            tvi\pszText = @tvi$
            tvi\cchTextMax = #MAX_PATH
            tvi\hItem=Item
            SendMessage_(GadgetID(0),#TVM_GETITEM,0,tvi)
            SetBkMode_(*tvCD\nmcd\hdc,#TRANSPARENT)
            If *tvCD\nmcd\uItemState = #CDIS_FOCUS | #CDIS_SELECTED
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush2)
              SetTextColor_(*tvCD\nmcd\hdc,$FFFFFF)
            Else
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush)
              SetTextColor_(*tvCD\nmcd\hdc,0)
            EndIf
            If FindString(tvi$,#CRLF$) > 0
              tvr\left + 2
              DrawText_(*tvCD\nmcd\hdc,tvi$,Len(tvi$),tvr,#DT_WORDBREAK )
            Else
              TextOut_(*tvCD\nmcd\hdc,tvr\left+2,tvr\top+14,tvi$,Len(tvi$))
            EndIf           
            ProcedureReturn #CDRF_NEWFONT           
        EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure

LoadFont(0,"Tahoma",14)

If OpenWindow(0,0,0,600,400,"TreeGadget",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  TreeGadget(0, 10,10,580,380)
  SendMessage_(GadgetID(0), #TVM_SETBKCOLOR, 0, $D7FEFE)
  SetGadgetFont( 0,FontID(0))
  TVH = SendMessage_(GadgetID(0),#TVM_GETITEMHEIGHT,0,0)
  
  ; determine how many lines needed for text based on width wanted and insert carriage returns
  ; this code needs to be compiled in ascii mode to work 
  ; added by Learner 21/12/15
  Text$="1234567890123456789012345678901234567890"+"12345678901234567890123456789012345678901234567890"+
        "12345678901234567890123456789012345678901234567890"
  TextLen=Len(Text$)
  TotalLines=Round(TextLen/#MULTILINEWIDTH,#PB_Round_Up)     
  buffSize=TextLen+(TotalLines*2)
  *buff=AllocateMemory(buffSize)
  PokeS(*buff,Text$)
  LinesCount=1
  For count = 1 To buffSize
    If count=#MULTILINEWIDTH*LinesCount
      InsertPos+#MULTILINEWIDTH
      ; copy buffer 1 pos to right to allow for insert
      PokeS(*buff+InsertPos+2,PeekS(*buff+InsertPos,buffSize-InsertPos))
      ; insert CRLF
      PokeS(*buff+InsertPos, #CRLF$,2,#PB_String_NoZero)
      InsertPos+2
      LinesCount+1
    EndIf
  Next
  Text$=PeekS(*buff)
  ;
  SendMessage_(GadgetID(0),#TVM_SETITEMHEIGHT,TVH*TotalLines,0) ;  number of lines needed Learner 21/12/15
  ; *** there may be a way to change line height to match LinesCount on the fly
  AddGadgetItem (0,-1,Text$,0,0) ; sample multiline with crlf's inserted
  AddGadgetItem (0,-1,"Sub Node",0,1)  
  AddGadgetItem (0,-1,"Second Main Level Node"+#CRLF$+"Happy New Year",0,0)
  AddGadgetItem(0,-1,"Sub text",0,1)
  AddGadgetItem(0,-1,"Third Node")
  
  SetWindowCallback(@WCallback())
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
  Until Quit = 1
  DeleteObject_(hBrush)
  DeleteObject_(hBrush2)
EndIf

RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Multi-Line TreeGadget [Windows]

Post by RASHAD »

Thanks ElementE

Hi Learner
You are on the right track

Code modified to :

1- No tool tips
2- No horizontal scroll
3- Accommodate font size and no.of lines needed

Code: Select all

; MultiLine Treegadget
; Rashad 19/12/15
;
#TVM_SETITEMHEIGHT  = $111B
#TVM_GETITEMHEIGHT  = $111C
#TVM_SETBKCOLOR     = $111D
#MULTILINEWIDTH = 40
;#TOTLINES= 4
Global hBrush,hBrush2,TVH,TotalLines

hBrush = CreateSolidBrush_($D7FEFE)
hBrush2 = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))

Procedure WCallback(hWnd,uMsg,wParam,lParam)
  result=#PB_ProcessPureBasicEvents
  Select uMsg
    Case  #WM_NOTIFY
      *tvCD.NMTVCUSTOMDRAW=lParam
      If *tvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *tvCD\nmcd\hdr\code = #NM_CUSTOMDRAW   
        Select *tvCD\nmcd\dwDrawStage
          Case #CDDS_PREPAINT:ProcedureReturn #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT: ProcedureReturn #CDRF_NOTIFYPOSTPAINT
          Case #CDDS_ITEMPOSTPAINT
            Item=*tvCD\nmcd\dwItemSpec
            tvr.RECT\left=Item
            SendMessage_(GadgetID(0),#TVM_GETITEMRECT,#True,@tvr)           
            tvi.TV_ITEM
            tvi$ = Space(#MAX_PATH)
            tvi\mask = #TVIF_HANDLE | #TVIF_PARAM | #TVIF_TEXT
            tvi\pszText = @tvi$
            tvi\cchTextMax = #MAX_PATH
            tvi\hItem=Item
            SendMessage_(GadgetID(0),#TVM_GETITEM,0,tvi)
            SetBkMode_(*tvCD\nmcd\hdc,#TRANSPARENT)
            tvr\right + 4
            If *tvCD\nmcd\uItemState = #CDIS_FOCUS | #CDIS_SELECTED
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush2)
              SetTextColor_(*tvCD\nmcd\hdc,$FFFFFF)
            Else
              FillRect_(*tvCD\nmcd\hdc, tvr, hBrush)
              SetTextColor_(*tvCD\nmcd\hdc,0)
            EndIf
            tvr\left + 4
            nCRLF = CountString(tvi$,#CRLF$)
            trim =(TotalLines - nCRLF - 1)
            tvr\top + trim*tvh/2+2
            DrawText_(*tvCD\nmcd\hdc,tvi$,Len(tvi$),tvr,#DT_WORDBREAK )           
            ProcedureReturn #CDRF_NEWFONT           
        EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure

LoadFont(0,"Tahoma",14)

If OpenWindow(0,0,0,600,400,"TreeGadget",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  TreeGadget(0, 10,10,580,380)
  SetWindowLongPtr_(GadgetID(0),#GWL_STYLE,GetWindowLongPtr_(GadgetID(0),#GWL_STYLE)|#TVS_NOTOOLTIPS|#TVS_NOHSCROLL)
  SendMessage_(GadgetID(0), #TVM_SETBKCOLOR, 0, $D7FEFE)
  SetGadgetFont( 0,FontID(0))
  TVH = SendMessage_(GadgetID(0),#TVM_GETITEMHEIGHT,0,0)
 
  ; determine how many lines needed for text based on width wanted and insert carriage returns
  ; this code needs to be compiled in ascii mode to work
  ; added by Learner 21/12/15
  
  Text$="1234567890123456789012345678901234567890"+"12345678901234567890123456789012345678901234567890"+
        "12345678901234567890123456789012345678901234567890"
  TextLen=Len(Text$)
  TotalLines=Round(TextLen/#MULTILINEWIDTH,#PB_Round_Up)
  
  For n = #MULTILINEWIDTH+1 To textlen Step #MULTILINEWIDTH+2
    Text$ = InsertString(Text$,#CRLF$,n)
  Next  
        
  SendMessage_(GadgetID(0),#TVM_SETITEMHEIGHT,TVH*TotalLines,0) ;  number of lines needed Learner 21/12/15
  ; *** there may be a way to change line height to match LinesCount on the fly
  AddGadgetItem (0,-1,Text$,0,0) ; sample multiline with crlf's inserted
  AddGadgetItem (0,-1,"Sub Node",0,1)
  AddGadgetItem (0,-1,"Second Main Level Node"+#CRLF$+"Happy New Year",0,0)
  AddGadgetItem(0,-1,"Sub text",0,1)
  AddGadgetItem(0,-1,"Third Node")
 
  SetWindowCallback(@WCallback())
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
  Until Quit = 1
  DeleteObject_(hBrush)
  DeleteObject_(hBrush2)
EndIf
Edit :Modified to support Ascii & Unicode
Egypt my love
Post Reply