ComboBoxGadget in Editable ListIconGadget [Windows]

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

ComboBoxGadget in Editable ListIconGadget [Windows]

Post by RASHAD »

Very simple very optimized

- Double click for edit
- Click Column 4 for ComboBoxGadget()

Code: Select all

Global oldCallback,ListIcon,LIColor,Oldr,Oldc,editflag

Procedure LIcallback(hwnd, msg, wparam, lparam) 
  result = CallWindowProc_(oldCallback, hwnd, msg, wparam, lparam)
  Select msg
    Case #WM_NOTIFY 
      *NMHDR.NMHDR = lParam
      If *nmhdr\code = #HDN_ITEMCHANGING And *NMHDR\code = #HDN_FIRST
        *phdn.NMHEADER = lParam                 
        If  editflag = 1
          r.RECT\top = Oldc
          r.RECT\left = #LVIR_BOUNDS
          SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, Oldr, r)
          If Oldc = 0
            r\right = r\left+SendMessage_(GadgetID(0), #LVM_GETCOLUMNWIDTH, 0, 0)
          EndIf
          MoveWindow_(GadgetID(2),r\left+GadgetX(0)+2,r\top+GadgetY(0)+2,r\right-r\left,r\bottom-r\top,1)  
          SetFocus_(GadgetID(2))
        EndIf
      EndIf
      
    Case #WM_LBUTTONDOWN
      SetGadgetState(0,-1)
      MoveWindow_(GadgetID(2),0,0,0,0,1)
      If editflag = 1 And GetGadgetText(2) <> ""
        SetGadgetItemText(0, Oldr,GetGadgetText(2),Oldc)
      EndIf
      pInfo.LVHITTESTINFO
      pInfo\pt\x = (lParam & $FFFF) 
      pInfo\pt\y = (lParam>> 16 & $FFFF)                    
      SendMessage_(ListIcon,#LVM_SUBITEMHITTEST,0,@pInfo)
      If  pInfo\iSubItem = 4 And Run =0              
        SendMessage_(GadgetID(0),#WM_HSCROLL,#SB_PAGERIGHT,0)
        Run = 1
        r.RECT 
        r\top  = pInfo\iSubItem 
        r\left = #LVIR_BOUNDS 
        SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, pInfo\iItem, r)
        MoveWindow_(GadgetID(5),r\left+GadgetX(0)+2,r\top+GadgetY(0)+2,r\right-r\left,r\bottom-r\top,1)
      Else
        Run =0  
        MoveWindow_(GadgetID(5),0,0,0,0,1)
        SetGadgetItemColor(0,OLdr,#PB_Gadget_FrontColor,#Black ,Oldc)
        SetGadgetItemColor(0,Oldr, #PB_Gadget_BackColor, LIColor,Oldc)
        SetGadgetItemColor(0,pInfo\iItem,#PB_Gadget_FrontColor,#White ,pInfo\iSubItem)
        SetGadgetItemColor(0,pInfo\iItem, #PB_Gadget_BackColor,GetSysColor_(#COLOR_HIGHLIGHT),pInfo\iSubItem)
      EndIf          
      editflag = 0
      Oldr = pInfo\iItem
      Oldc = pInfo\iSubItem
      
      
    Case #WM_LBUTTONDBLCLK
      editflag = 1
      r.RECT\top = Oldc
      r.RECT\left = #LVIR_BOUNDS
      SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, Oldr, r)
      SendMessage_(GadgetID(0), #LVM_GETHOTITEM, 0,0)
      If Oldc = 0
        r\right = r\left+SendMessage_(GadgetID(0), #LVM_GETCOLUMNWIDTH, 0, 0)
      EndIf
      SetGadgetText(2,"")
      MoveWindow_(GadgetID(2),r\left+GadgetX(0)+2,r\top+GadgetY(0)+2,r\right-r\left,r\bottom-r\top,1)
      SetFocus_(GadgetID(2))
      
    Case #WM_VSCROLL,#WM_HSCROLL
      MoveWindow_(GadgetID(2),0,0,0,0,1)
      MoveWindow_(GadgetID(5),0,0,0,0,1)
      SetGadgetItemColor(0,OLdr,#PB_Gadget_FrontColor,#Black ,Oldc)
      SetGadgetItemColor(0,Oldr, #PB_Gadget_BackColor, LIColor,Oldc)
      
      
  EndSelect
  ProcedureReturn result
EndProcedure

LoadFont(0,"Tahoma",10) 

OpenWindow(0,0,0,640,480,"Test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered| #PB_Window_SizeGadget)
ListIcon = ListIconGadget(0,10,10,620,450,"Column 0",200,#PB_ListIcon_GridLines|#WS_CLIPSIBLINGS );|#LVS_EDITLABELS) 
AddGadgetColumn(0,1,"Column 1",210) 
AddGadgetColumn(0,2,"Column 2",200) 
AddGadgetColumn(0,3,"Column 3",200)
AddGadgetColumn(0,4,"Column 4",200)
AddGadgetColumn(0,5,"Column 5",100) 

For i = 0 To 16 
  linestr.s = LSet(Str(i),3," ") 
  AddGadgetItem(0, -1, "Text on Line "+linestr+" in Column 1"+Chr(10)+"Text on Line "+linestr+" in Column 2" + Chr(10)+"Text on Line "+linestr+" in Column 3" + Chr(10)+"Text on Line "+linestr+" in Column 3"+ Chr(10)+"") 
Next
SetGadgetColor(0, #PB_Gadget_BackColor,$DFFEFD)
ButtonGadget(1,10,450,150,24,"Add new ROW")
StringGadget(2,0,0,0,0,"",#ES_MULTILINE|#WS_CLIPSIBLINGS);|#PB_String_BorderLess)
SendMessage_(GadgetID(2), #EM_SETMARGINS, #EC_LEFTMARGIN, 3)
BringWindowToTop_(GadgetID(2))
oldCallback = SetWindowLongPtr_(ListIcon, #GWL_WNDPROC, @LIcallback())

SetGadgetFont(0,FontID(0))
SetGadgetFont(2,FontID(0))
If GetGadgetColor(0, #PB_Gadget_BackColor) < 0 
  LIColor = #White
Else
  LIColor =  GetGadgetColor(0, #PB_Gadget_BackColor)
EndIf

ComboBoxGadget(5,0,0,0,0,#WS_CLIPSIBLINGS)
For a = 1 To 5
  AddGadgetItem(5, -1,"ComboBox item " + Str(a))
Next
BringWindowToTop_(GadgetID(5))

Repeat
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #WM_KEYDOWN
      If GetActiveGadget() = 2 And  EventwParam() = 27
        MoveWindow_(GadgetID(2),0,0,0,0,1)
        editflag = 0
      ElseIf GetActiveGadget() = 2 And  EventwParam() = 13
        SetGadgetItemText(0, Oldr,GetGadgetText(2),Oldc)
        MoveWindow_(GadgetID(2),0,0,0,0,1)
        editflag = 0
      EndIf
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 1            
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          linestr.s = LSet(Str(CountGadgetItems(0)),3," ")
          AddGadgetItem(0, -1, "Text on Line "+linestr+" in Column 1"+Chr(10)+"Text on Line "+linestr+" in Column 2" + Chr(10)+"Text on Line "+linestr+" in Column 3" + Chr(10)+"Text on Line "+linestr+" in Column 3"+ Chr(10)+"")
        Case 5
          MoveWindow_(GadgetID(5),0,0,0,0,1)
          SetGadgetItemText(0, Oldr,GetGadgetText(5),Oldc)             
      EndSelect
      
      
    Case #WM_SIZE
      ResizeGadget(0,10,10,WindowWidth(0)-20,WindowHeight(0)-60)
      
  EndSelect 
  
Until Quit = 1
End


Edit : Added support for Return and Esc
Edit 2 : Some bugs fixed
Edit 3 : More bugs fixed
Edit 4 : More bugs fixed
Last edited by RASHAD on Sun Mar 09, 2025 3:44 pm, edited 5 times in total.
Egypt my love
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by jassing »

Nice; but it should leave the text in there when you edit and response as 'cancel' to escape.

Otherwise, slick editable grid....
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by RASHAD »

Hi jassing
Previous post updated
Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by davido »

Hi RASHAD,

Very nice. Thank you for sharing.
DE AA EB
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by Kwai chang caine »

Great job RASHAD like usual 8)
And KCC found bugs..like usual too... :oops:

1/ If you modify the first line of the first column, after when you select this case all the column are selected
2/ If you modify a cell, you do return for valid and it's all good
But if you press enter another time, without move the selection, the text disappears

Otherwise.... thanks a lot for sharing and always try to give splendid code to us 8)
ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by RASHAD »

Hi guys
First post updated again
Egypt my love
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by JHPJHP »

Hi RASHAD,

First off, great work - thank you!

I just posted a link to this post for a question asked in another Board (Coding Questions):
viewtopic.php?f=13&t=55754&sid=30e56e82 ... 4f744e6895

I also noticed a few small bugs, if inside one of the editable options, and a column is resized... or if the last column is only half showing due to the scrollbar position and the combobox is activated...

Thanks again,

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by RASHAD »

Hi JHPJHP
First post updated
Egypt my love
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by USCode »

Very nice, thanks for sharing.
Would it be possible to create a cross-platform version?
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by said »

Very simple and very nice RASHAD :o thanks for sharing
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by RASHAD »

@USCode thanks mate
Maybe Shardik can create a cross-platform version he is the three phase magician

@said thanks mate

First post updated again
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by Kwai chang caine »

RASHAD wrote:Hi guys
First post updated again
Thanks for the fix 8)
But only one of the two bug are fixed

I don't know if it's the normal behaviour of the listicongadget but

If you run the code, you can select the first left cell and the second independamently...etc..
Now.... modify the first or the second cell of the first column "Column 0"
Since this moment it's impossible to select only the one or second cell, always ALL the first column "Column 0" are selected :shock:

Again thanks to your present 8)
ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by RASHAD »

Hi KCC
I can not reproduce your case
So test the next code using another tech.
It has some advantages over the previous
But remember column 0 now is not in use

Code: Select all

Global oldCallback,ListIcon,LIColor,Oldr,Oldc,editflag

Procedure LIcallback(hwnd, msg, wparam, lparam) 
  result = CallWindowProc_(oldCallback, hwnd, msg, wparam, lparam)
  Select msg
    Case #WM_NOTIFY
       *NMHDR.NMHDR = lParam
       If *nmhdr\code = #HDN_ITEMCHANGING And *NMHDR\code = #HDN_FIRST
          *phdn.NMHEADER = lParam
          If  *phdn\iItem = 0 
                ProcedureReturn 1
          EndIf                 
          r.RECT\top = Oldc
          r.RECT\left = #LVIR_BOUNDS
          SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, Oldr, r)
          If editflag = 1
              MoveWindow_(GadgetID(2),r\left,r\top,r\right-r\left,r\bottom-r\top,1)  
              SetFocus_(GadgetID(2))
          ElseIf Oldc = 4 And Run =0
              Run = 1
              MoveWindow_(GadgetID(5),r\left,r\top,r\right-r\left,r\bottom-r\top,1)
          EndIf   
       EndIf
                    
    Case #WM_LBUTTONDOWN
          SetGadgetState(0,-1)
          MoveWindow_(GadgetID(2),0,0,0,0,1)
          If editflag = 1 And GetGadgetText(2) <> ""
              SetGadgetItemText(0, Oldr,GetGadgetText(2),Oldc)
          EndIf
          pInfo.LVHITTESTINFO
          pInfo\pt\x = (lParam & $FFFF) 
          pInfo\pt\y = (lParam>> 16 & $FFFF)                    
          SendMessage_(ListIcon,#LVM_SUBITEMHITTEST,0,@pInfo)
          If  pInfo\iSubItem = 4 And Run =0
              Run = 1
              r.RECT 
              r\top  = pInfo\iSubItem 
              r\left = #LVIR_BOUNDS 
              SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, pInfo\iItem, r)
              MoveWindow_(GadgetID(5),r\left,r\top,r\right-r\left,r\bottom-r\top,1)
          Else
              Run =0  
              MoveWindow_(GadgetID(5),0,0,0,0,1)
              SetGadgetItemColor(0,OLdr,#PB_Gadget_FrontColor,#Black ,Oldc)
              SetGadgetItemColor(0,Oldr, #PB_Gadget_BackColor, LIColor,Oldc)
              SetGadgetItemColor(0,pInfo\iItem,#PB_Gadget_FrontColor,#White ,pInfo\iSubItem)
              SetGadgetItemColor(0,pInfo\iItem, #PB_Gadget_BackColor,GetSysColor_(#COLOR_HIGHLIGHT),pInfo\iSubItem)
          EndIf          
          editflag = 0
          Oldr = pInfo\iItem
          Oldc = pInfo\iSubItem
        
    
    Case #WM_LBUTTONDBLCLK
          editflag = 1
          r.RECT\top = Oldc
          r.RECT\left = #LVIR_BOUNDS
          SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, Oldr, r)
          SetGadgetText(2,"")
          MoveWindow_(GadgetID(2),r\left,r\top,r\right-r\left,r\bottom-r\top,1)
          SetFocus_(GadgetID(2))
          
    Case #WM_VSCROLL,#WM_HSCROLL
          r.RECT\top = Oldc
          r.RECT\left = #LVIR_BOUNDS
          SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, Oldr, r)
          If editflag = 1
              MoveWindow_(GadgetID(2),r\left,r\top,r\right-r\left,r\bottom-r\top,1)  
              SetFocus_(GadgetID(2))
          ElseIf Oldc = 4 And Run =0
              Run = 1
              MoveWindow_(GadgetID(5),r\left,r\top,r\right-r\left,r\bottom-r\top,1)
          EndIf
          InvalidateRect_(GadgetID(2),0,1)   

  EndSelect
  ProcedureReturn result
EndProcedure

LoadFont(0,"Tahoma",10) 

OpenWindow(0,0,0,640,480,"Test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered| #PB_Window_SizeGadget)
ListIcon = ListIconGadget(0,10,10,620,460,"Column 0",0,#PB_ListIcon_GridLines)
AddGadgetColumn(0,1,"Column 1",200) 
AddGadgetColumn(0,2,"Column 2",200) 
AddGadgetColumn(0,3,"Column 3",200)
AddGadgetColumn(0,4,"Column 4",200)
AddGadgetColumn(0,5,"Column 5",100) 
 
For i = 0 To 16 
  linestr.s = LSet(Str(i),3," ") 
  AddGadgetItem(0, -1, Chr(10)+"Text on Line "+linestr+" in Column 1"+Chr(10)+"Text on Line "+linestr+" in Column 2" + Chr(10)+"Text on Line "+linestr+" in Column 3" + Chr(10)+"") 
Next
SetGadgetColor(0, #PB_Gadget_BackColor,$DFFEFD)
StringGadget(2,0,0,0,0,"",#ES_MULTILINE)
SendMessage_(GadgetID(2), #EM_SETMARGINS, #EC_LEFTMARGIN, 3)
SetParent_(GadgetID(2),GadgetID(0))
oldCallback = SetWindowLongPtr_(ListIcon, #GWL_WNDPROC, @LIcallback())

SetGadgetFont(0,FontID(0))
SetGadgetFont(2,FontID(0))
If GetGadgetColor(0, #PB_Gadget_BackColor) < 0 
   LIColor = #White
Else
   LIColor =  GetGadgetColor(0, #PB_Gadget_BackColor)
EndIf

ComboBoxGadget(5,0,0,0,0)
For a = 1 To 5
  AddGadgetItem(5, -1,"ComboBox item " + Str(a))
Next
SetParent_(GadgetID(5),GadgetID(0))

Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1
            
      Case #WM_KEYDOWN
           If GetActiveGadget() = 2 And  EventwParam() = 27
               MoveWindow_(GadgetID(2),0,0,0,0,1)
               editflag = 0
           ElseIf GetActiveGadget() = 2 And  EventwParam() = 13
               SetGadgetItemText(0, Oldr,GetGadgetText(2),Oldc)
               MoveWindow_(GadgetID(2),0,0,0,0,1)
               editflag = 0
           EndIf
      
      Case #PB_Event_Menu
          Select EventMenu()
           Case 1            
          EndSelect
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 5
               MoveWindow_(GadgetID(5),0,0,0,0,1)
               SetGadgetItemText(0, Oldr,GetGadgetText(5),Oldc)             
          EndSelect
          
           
      Case #WM_SIZE
            ResizeGadget(0,10,10,WindowWidth(0)-20,WindowHeight(0)-60)
            
  EndSelect 

Until Quit = 1
End
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by Kwai chang caine »

Waouuuuhhhh !!!! RASHAD too strong like usually !! :shock:
That's works perfectly now

Thanks a lot for spend your time to fix a problem that apparently just KCC have :oops:

It's that the dream team...me i have all the problems...and you all the solutions :mrgreen:
It's my vision of the sharing :lol:

Have a good day and again thanks for your great code :wink:
ImageThe happiness is a road...
Not a destination
morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: ComboBoxGadget in Editable ListIconGadget [Windows]

Post by morosh »

Hi back:
I was playing with this snippet, which is wonderful, thank you RASHAD:
I noticed that after editing a cell and pressing Enter, there is a sound notification which may be comes from Window, is-it possible to avoid it?
Also a small remark,the paragraph:

Code: Select all

    Case #WM_NOTIFY 
       *NMHDR.NMHDR = lParam
       If *nmhdr\code = #HDN_ITEMCHANGING And *NMHDR\code = #HDN_FIRST
          *phdn.NMHEADER = lParam                 
          If  editflag = 1
              r.RECT\top = Oldc
              r.RECT\left = #LVIR_BOUNDS
              SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, Oldr, r)
              If Oldc = 0
                 r\right = r\left+SendMessage_(GadgetID(0), #LVM_GETCOLUMNWIDTH, 0, 0)
              EndIf
              MoveWindow_(GadgetID(2),r\left+GadgetX(0)+2,r\top+GadgetY(0)+2,r\right-r\left,r\bottom-r\top,1)  
              SetFocus_(GadgetID(2))
          EndIf
       EndIf
is never executed because of :

Code: Select all

       If *nmhdr\code = #HDN_ITEMCHANGING And *NMHDR\code = #HDN_FIRST
which is never true, correct?
what's its purpose?

Regards
PureBasic: Surprisingly simple, diabolically powerful
Post Reply