How do I space out MenuTitles above each ListView.?

Just starting out? Need help? Post your questions and find answers here.
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

How do I space out MenuTitles above each ListView.?

Post by dcr3 »

Hi.Is there a neater and better way of spacing MenuTitles, so it would be above in each
ListView or ListIcon. An example is provided to illustrate what I mean. Thanks in advance.

Code: Select all

Enumeration FormWindow
  #Win1
EndEnumeration

Enumeration FormGadget
  #ListIcon_1
  #ListIcon_2
  #ListIcon_3
  #Main_MTB
  #Mnu_1
  #Mnu_2
  #Mnu_3
  #Mnu_4
  #Mnu_5
  #Mnu_6
  #Mnu_7
  #Mnu_8
  #Mnu_9
  #Mnu_10
  #Mnu_11
  #Mnu_12
  #Mnu_13
  #Mnu_14
  #Mnu_15
  #Mnu_16
  #Mnu_17
  #Mnu_18
  #Mnu_19
   
EndEnumeration

OpenWindow(#Win1, 0, 0, 460,330, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Minimize)
 
CreateMenu(#Main_MTB, WindowID(#Win1))
MenuTitle("List1")
MenuItem(#Mnu_1,"Open")
MenuItem(#Mnu_2, "OPT1")
MenuItem(#Mnu_3, "OPT2")
MenuItem(#Mnu_4, "OPT3")
MenuItem(#Mnu_5, "OPT4")
MenuItem(#Mnu_6, "Save")

MenuTitle(Space(37))
MenuTitle("List2")
MenuItem(#Mnu_7, "Open")
MenuItem(#Mnu_8, "OPT1")
MenuItem(#Mnu_9, "OPT2")
MenuItem(#Mnu_10, "OPT3")
MenuItem(#Mnu_11, "OPT4")
MenuItem(#Mnu_12, "Save")
MenuTitle(Space(33))

MenuTitle("List3")
MenuItem(#Mnu_13,"Open")
MenuItem(#Mnu_14,"OPT1")
MenuItem(#Mnu_15,"OPT2")
MenuItem(#Mnu_16,"OPT3")
MenuItem(#Mnu_17,"OPT4")
MenuItem(#Mnu_18, "Save")

  ListIconGadget(#ListIcon_1, 10, 10, 140, 290,"List1",85)
  AddGadgetColumn(#ListIcon_1, 1, "A", 50)
  
  ListIconGadget(#ListIcon_2, 160, 10, 140, 290, "List2",85)
  AddGadgetColumn(#ListIcon_2, 1, "B", 50)
  
  ListIconGadget(#ListIcon_3, 310, 10, 140, 290, "List3",85)
  AddGadgetColumn(#ListIcon_3, 1, "C", 50)
  
  Repeat 
    Select WaitWindowEvent()
     
    Case #PB_Event_CloseWindow
     
      appQuit = 1
      Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
  
    Case #PB_Event_Gadget
      Select EventGadget() 
      EndSelect 
  
EndSelect
Until appQuit

User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How do I space out MenuTitles above each ListView.?

Post by Thunder93 »

If you doing Windows-specific code.. With use of Windows APIs, you can use what was demonstrated by the user Sparkie way back in 2004. Here's the manipulated version. :wink:

Code: Select all

#ListIcon_1 = 100
#ListIcon_2 = 101

If OpenWindow(0, 0, 0, 500, 150, "Menu in fake containers",  #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
  
  ListIconGadget(#ListIcon_1, 10, 20, 205, 105,"List1",85)
  AddGadgetColumn(#ListIcon_1, 1, "A", 50)
  
  *mycontainer1 = CreateWindowEx_(#WS_EX_CLIENTEDGE, "Button", "", #WS_VISIBLE | #WS_POPUP | #BS_OWNERDRAW, 10, 0, 200, 100, WindowID(0), 0, GetModuleHandle_(0),0)
  *newmenu1 = CreateMenu(0, *mycontainer1) 
  MenuTitle("&Project #1") 
  MenuItem(1         ,"&New   Ctrl+N") 
  MenuItem(2        ,"&Open...   Ctrl+O")
  
  MenuTitle("&Edit")
  MenuTitle("&Help")        
  
  
  ListIconGadget(#ListIcon_2, 250, 20, 205, 105,"List2",85)
  AddGadgetColumn(#ListIcon_2, 1, "A", 50)
  
  *mycontainer2 = CreateWindowEx_(#WS_EX_CLIENTEDGE, "Button", "", #WS_VISIBLE | #WS_POPUP | #BS_OWNERDRAW, 250, 0, 200, 100, WindowID(0), 0, GetModuleHandle_(0),0)
  *newmenu2 = CreateMenu(1, *mycontainer2) 
  MenuTitle("&Project #2") 
  MenuItem(1         ,"&New   Ctrl+N") 
  MenuItem(2        ,"&Open...   Ctrl+O")
  MenuTitle("&Preferences")
  MenuTitle("&About")
  
  SetMenu_(*mycontainer2, *newmenu2)
  SetParent_(*mycontainer2, WindowID(0))  
  
  SetMenu_(*mycontainer1, *newmenu1)
  SetParent_(*mycontainer1, WindowID(0))
EndIf


Repeat 
  
  Event = WaitWindowEvent() 
  
Until Event = #PB_Event_CloseWindow 

End
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How do I space out MenuTitles above each ListView.?

Post by dcr3 »

Great. Thanks. Thunder93. :D
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: How do I space out MenuTitles above each ListView.?

Post by Bisonte »

And here for your special case ... without "API-Magic" ;) so it should be crossplattform

Code: Select all

Enumeration FormWindow
  #Win1
EndEnumeration

Enumeration FormGadget
  #ListIcon_1
  #ListIcon_2
  #ListIcon_3
  #Main_MTB
  #MCLI_1
  #MCLI_2
  #MCLI_3
  #Mnu_1
  #Mnu_2
  #Mnu_3
  #Mnu_4
  #Mnu_5
  #Mnu_6   
EndEnumeration

Global MenuCanvas = -1

Procedure CanvasMenuButton()
  
  Protected Gadget = EventGadget()
  Protected Window = EventWindow()
  Protected y, ET = EventType()
  
  If ET = #PB_EventType_MouseEnter
    MenuCanvas = Gadget
  EndIf
  
  If StartDrawing(CanvasOutput(Gadget))
    Box(0, 0, OutputWidth(), OutputHeight(), GetWindowColor(Window))
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      ; Set your font here if you are on MacOS
    CompilerElse
      DrawingFont(GetGadgetFont(#PB_Default))
    CompilerEndIf
    DrawingMode(#PB_2DDrawing_Transparent)
    y = ((OutputHeight()/2)-(TextHeight("|")/2))
    DrawText(7, y, PeekS(GetGadgetData(Gadget), -1, #PB_UTF8), #Black)
    StopDrawing()
  EndIf
  If ET = #PB_EventType_LeftButtonDown Or ET = #PB_EventType_RightButtonDown
    DisplayPopupMenu(#Main_MTB, WindowID(Window), GadgetX(Gadget, #PB_Gadget_ScreenCoordinate), GadgetY(Gadget, #PB_Gadget_ScreenCoordinate) + GadgetHeight(Gadget))
  EndIf
   
EndProcedure

OpenWindow(#Win1, 0, 0, 460,330, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered); | #PB_Window_Minimize)
SetWindowColor(#Win1, $EFEFEF) ; Set the WindowColor, to have a backgroundcolor for the canvas

;: One single PopupMenu
CreatePopupMenu(#Main_MTB)
MenuItem(#Mnu_1,"Open")
MenuItem(#Mnu_2, "OPT1")
MenuItem(#Mnu_3, "OPT2")
MenuItem(#Mnu_4, "OPT3")
MenuItem(#Mnu_5, "OPT4")
MenuItem(#Mnu_6, "Save")

;: And here comes the "menu"
CanvasGadget(#MCLI_1, 10, 0, 140, 20) : SetGadgetData(#MCLI_1, UTF8("List1"))

ListIconGadget(#ListIcon_1, 10, 20, 140, 290,"List1",85)
AddGadgetColumn(#ListIcon_1, 1, "A", 50)

CanvasGadget(#MCLI_2, 160, 0, 140, 20) : SetGadgetData(#MCLI_2, UTF8("List2"))
ListIconGadget(#ListIcon_2, 160, 20, 140, 290, "List2",85)
AddGadgetColumn(#ListIcon_2, 1, "B", 50)

CanvasGadget(#MCLI_3, 310, 0, 140, 20) : SetGadgetData(#MCLI_3, UTF8("List3"))
ListIconGadget(#ListIcon_3, 310, 20, 140, 290, "List3",85)

AddGadgetColumn(#ListIcon_3, 1, "C", 50)

For i = #MCLI_1 To #MCLI_3
  BindGadgetEvent(i, @CanvasMenuButton())
  PostEvent(#PB_Event_Gadget, #Win1, i, #PB_EventType_Change)
Next i

Repeat
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow
      
      appQuit = 1
    Case #PB_Event_Menu
      Select MenuCanvas
        Case #MCLI_1
          Select EventMenu()
            Case #Mnu_1
              Debug "MenuItem 1 on ListIcon 1"
          EndSelect
        Case #MCLI_2
          Select EventMenu()
            Case #Mnu_3
              Debug "MenuItem 3 on ListIcon 2"
          EndSelect 
        Case #MCLI_3
          Select EventMenu()
            Case #Mnu_5
              Debug "MenuItem 5 on ListIcon 3"
          EndSelect
      EndSelect
      
      
    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
      
  EndSelect
Until appQuit
Last edited by Bisonte on Wed Sep 27, 2017 1:06 pm, edited 2 times in total.
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How do I space out MenuTitles above each ListView.?

Post by Shardik »

Bisonte wrote:And here for your special case ... without "API-Magic" ;) so it has to be crossplattform
MacOS 10.6.8 (Snow Leopard) with PB 5.61 x86:
PB Debugger wrote:[ERROR] Line: 35
[ERROR] DrawingFont(): The specified FontID is NULL (0 value).
Please don't take cross-platform usage for granted when you didn't use any API functions. There still remain subtle differences between the PureBasic implementations on the different operating systems, so an actual test on the different operating systems remains always necessary... :wink:
PB help for GetGadgetFont() wrote:Note: On Mac OSX, this function returns 0 if the gadget does not have a specific font associated with it.
In order to implement the missing function GetGadgetFont(#PB_Default) on MacOS it's necessary to utilize API functions:

Code: Select all

Enumeration FormWindow
  #Win1
EndEnumeration

Enumeration FormGadget
  #ListIcon_1
  #ListIcon_2
  #ListIcon_3
  #Main_MTB
  #MCLI_1
  #MCLI_2
  #MCLI_3
  #Mnu_1
  #Mnu_2
  #Mnu_3
  #Mnu_4
  #Mnu_5
  #Mnu_6   
EndEnumeration

Global MenuCanvas = -1

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  Procedure.S GetGadgetFontName(GadgetID.I)
    Protected FontID.I
    Protected FontName.S
    
    FontID = GetGadgetFont(GadgetID)
    
    If FontID
      FontName = PeekS(CocoaMessage(0, CocoaMessage(0, FontID, "displayName"),
        "UTF8String"), -1, #PB_UTF8)
    EndIf
    
    ProcedureReturn FontName
  EndProcedure
  
  Procedure.I GetGadgetFontSize(GadgetID.I)
    Protected FontID.I
    Protected FontSize.CGFloat
    
    FontID = GetGadgetFont(GadgetID)
    
    If FontID
      CocoaMessage(@FontSize, FontID, "pointSize")
    EndIf
    
    ProcedureReturn Int(FontSize)
  EndProcedure
CompilerEndIf

Procedure CanvasMenuButton()
 
  Protected Gadget = EventGadget()
  Protected Window = EventWindow()
  Protected y, ET = EventType()
 
  If ET = #PB_EventType_MouseEnter
    MenuCanvas = Gadget
  EndIf

  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    LoadFont(0, GetGadgetFontName(#MCLI_1), GetGadgetFontSize(#MCLI_1))
  CompilerEndIf
 
  If StartDrawing(CanvasOutput(Gadget))
    Box(0, 0, OutputWidth(), OutputHeight(), GetWindowColor(Window))

    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      DrawingFont(FontID(0))
    CompilerElse
      DrawingFont(GetGadgetFont(#PB_Default))
    CompilerEndIf
 
    DrawingMode(#PB_2DDrawing_Transparent)
    y = ((OutputHeight()/2)-(TextHeight("|")/2))
    DrawText(7, y, PeekS(GetGadgetData(Gadget), -1, #PB_UTF8), #Black)
    StopDrawing()
  EndIf
  If ET = #PB_EventType_LeftButtonDown Or ET = #PB_EventType_RightButtonDown
    DisplayPopupMenu(#Main_MTB, WindowID(Window), GadgetX(Gadget, #PB_Gadget_ScreenCoordinate), GadgetY(Gadget, #PB_Gadget_ScreenCoordinate) + GadgetHeight(Gadget))
  EndIf
   
EndProcedure

OpenWindow(#Win1, 0, 0, 460,330, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered); | #PB_Window_Minimize)
SetWindowColor(#Win1, $EFEFEF) ; Set the WindowColor, to have a backgroundcolor for the canvas

;: One single PopupMenu
CreatePopupMenu(#Main_MTB)
MenuItem(#Mnu_1,"Open")
MenuItem(#Mnu_2, "OPT1")
MenuItem(#Mnu_3, "OPT2")
MenuItem(#Mnu_4, "OPT3")
MenuItem(#Mnu_5, "OPT4")
MenuItem(#Mnu_6, "Save")

;: And here comes the "menu"
CanvasGadget(#MCLI_1, 10, 0, 140, 20) : SetGadgetData(#MCLI_1, UTF8("List1"))

ListIconGadget(#ListIcon_1, 10, 20, 140, 290,"List1",85)
AddGadgetColumn(#ListIcon_1, 1, "A", 50)

CanvasGadget(#MCLI_2, 160, 0, 140, 20) : SetGadgetData(#MCLI_2, UTF8("List2"))
ListIconGadget(#ListIcon_2, 160, 20, 140, 290, "List2",85)
AddGadgetColumn(#ListIcon_2, 1, "B", 50)

CanvasGadget(#MCLI_3, 310, 0, 140, 20) : SetGadgetData(#MCLI_3, UTF8("List3"))
ListIconGadget(#ListIcon_3, 310, 20, 140, 290, "List3",85)

AddGadgetColumn(#ListIcon_3, 1, "C", 50)

For i = #MCLI_1 To #MCLI_3
  BindGadgetEvent(i, @CanvasMenuButton())
  PostEvent(#PB_Event_Gadget, #Win1, i, #PB_EventType_Change)
Next i

Repeat
  Select WaitWindowEvent()
     
    Case #PB_Event_CloseWindow
     
      appQuit = 1
    Case #PB_Event_Menu
      Select MenuCanvas
        Case #MCLI_1
          Select EventMenu()
            Case #Mnu_1
              Debug "MenuItem 1 on ListIcon 1"
          EndSelect
        Case #MCLI_2
          Select EventMenu()
            Case #Mnu_3
              Debug "MenuItem 3 on ListIcon 2"
          EndSelect
        Case #MCLI_3
          Select EventMenu()
            Case #Mnu_5
              Debug "MenuItem 5 on ListIcon 3"
          EndSelect
      EndSelect
     
     
    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
     
  EndSelect
Until appQuit
Last edited by Shardik on Wed Sep 27, 2017 1:09 pm, edited 1 time in total.
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: How do I space out MenuTitles above each ListView.?

Post by Bisonte »

Sorry my fault, I trusted my knowledge about english ..... :oops:

I mean : Should have to be crossplatform...

I think that is the right text... Because i never "granted" cossplatform in my mind...

I edit the code above...
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How do I space out MenuTitles above each ListView.?

Post by dcr3 »

Very nice Bisonte. And a lovely touch by Shardik to make it crossplatform. Thanks. :D :D
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How do I space out MenuTitles above each ListView.?

Post by Thunder93 »

Just tweaked Bisonte's initially posted version to be little more natural in the way we normally see menus works. It does need little more enhancement to share exact characteristics, however I think you'll like the changes made so far. And the same goes for the visual aspect, but It's improved.

Also each menu is now with its own popup menu. When time permits, I'll try to do little more with it. Hopefully someone else will race this to the finish line first.

Code: Select all

Enumeration FormWindow
  #Win1
EndEnumeration

Enumeration FormGadget
  #ListIcon_1
  #ListIcon_2
  #ListIcon_3
  
  #CustomMenu1
  #MCLI_1
  #MCLI_2
  #MCLI_3
  #Mnu_1
  #Mnu_2
  #Mnu_3
  #Mnu_4
  #Mnu_5
  #Mnu_6
  
  #CustomMenu2
  #Mnu2_1
  #Mnu2_2
  #Mnu2_3
  #Mnu2_4
  #Mnu2_5
  #Mnu2_6
  
  #CustomMenu3
  #Mnu3_1
  #Mnu3_2
  #Mnu3_3
  #Mnu3_4
  #Mnu3_5
  #Mnu3_6
EndEnumeration

Structure Custom_Menu
  Title.s
  TextHeight.l
  TextWidth.l
  MenuID.l
EndStructure : Global NewMap Menu.Custom_Menu()


Procedure.l GetText_Width(Text$, WindowID = 0)
  Protected TextWidth.l
  
  If StartDrawing(WindowOutput(WindowID))   
    TextWidth = TextWidth(Text$)
    StopDrawing()
  EndIf
  
  ProcedureReturn TextWidth
EndProcedure

Procedure.l GetText_Height(Text$ = "|", WindowID = 0)
  Protected TextHeight.l
  
  If StartDrawing(WindowOutput(WindowID))   
    TextHeight = TextHeight(Text$)
    StopDrawing()
  EndIf
  
  ProcedureReturn TextHeight
EndProcedure

Procedure.l AddMenu(Gadget.l, Title.s, MenuID.l)
  GadgetID.s = Str(Gadget)
  
  Menu(GadgetID)\Title = Title
  Menu(GadgetID)\TextWidth = GetText_Width(Title)
  Menu(GadgetID)\TextHeight = GetText_Height()
  Menu(GadgetID)\MenuID = MenuID
  
  If IsGadget(Gadget)
    ResizeGadget(Gadget, #PB_Ignore, #PB_Ignore, Menu(GadgetID)\TextWidth+3, #PB_Ignore)
  EndIf 
  
EndProcedure


Procedure.l Draw_Custom_Menu(Gadget)
  If StartDrawing(CanvasOutput(Gadget))
    Box(0, 0, OutputWidth(), OutputHeight(), GetWindowColor(#Win1))
    DrawingFont(GetGadgetFont(#PB_Default))
    DrawingMode(#PB_2DDrawing_Transparent)
    FindMapElement(Menu(), Str(Gadget))   
    y = ((OutputHeight()/2)-(Menu()\TextHeight/2))
    String$ = Menu()\Title
    DrawText(7, y, String$, #Black)
    StopDrawing()
  EndIf
EndProcedure


Global MenuCanvas = -1

Procedure CanvasMenuButton()
  Static.l PrevGadget, State.b
  Protected Gadget = EventGadget()
  Protected Window = EventWindow()
  Protected y, ET = EventType()
  
  
  If ET = #PB_EventType_MouseEnter
    MenuCanvas = Gadget
    
  ElseIf ET = #PB_EventType_MouseLeave    
    If StartDrawing(CanvasOutput(Gadget))
      FillArea(0, 0, -1, GetWindowColor(Window))
      StopDrawing()
    EndIf
    
    
  ElseIf ET = #PB_EventType_MouseMove    
    If StartDrawing(CanvasOutput(Gadget))
      FillArea(0, 0, -1, $FFF8F0)    
      StopDrawing()
    EndIf
    
    
  ElseIf ET = #PB_EventType_Change
    Draw_Custom_Menu(Gadget)    
    
  ElseIf ET = #PB_EventType_LeftButtonDown Or ET = #PB_EventType_RightButtonDown
    If Gadget <> PrevGadget : State = 0 : Else : State ! 1 : EndIf
    
    If State And Gadget = PrevGadget
      ProcedureReturn 0
    EndIf
    
    PrevGadget = Gadget
    
    FindMapElement(Menu(), Str(Gadget))
    
    DisplayPopupMenu(Menu()\MenuID, WindowID(Window), GadgetX(Gadget, #PB_Gadget_ScreenCoordinate), GadgetY(Gadget, #PB_Gadget_ScreenCoordinate) + GadgetHeight(Gadget))
  EndIf
  
EndProcedure

OpenWindow(#Win1, 0, 0, 460,330, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered); | #PB_Window_Minimize)
SetWindowColor(#Win1, $EFEFEF)                                                         ; Set the WindowColor, to have a backgroundcolor for the canvas

CreatePopupMenu(#CustomMenu1)
MenuItem(#Mnu_1,"Open")
MenuItem(#Mnu_2, "OPT1")
MenuItem(#Mnu_3, "OPT2")
MenuItem(#Mnu_4, "OPT3")
MenuItem(#Mnu_5, "OPT4")
MenuItem(#Mnu_6, "Save")

CreatePopupMenu(#CustomMenu2)
MenuItem(#Mnu2_1,"Edit")
MenuItem(#Mnu2_2, "NO1")
MenuItem(#Mnu2_3, "NO2")
MenuItem(#Mnu2_4, "NO3")
MenuItem(#Mnu2_5, "NO4")
MenuItem(#Mnu2_6, "Exit")

CreatePopupMenu(#CustomMenu3)
MenuItem(#Mnu3_1,"View")
MenuItem(#Mnu3_2, "Yes1")
MenuItem(#Mnu3_3, "Yes2")
MenuItem(#Mnu3_4, "Yes3")
MenuItem(#Mnu3_5, "Yes4")
MenuItem(#Mnu3_6, "Close")

;: And here comes the "menu"
CanvasGadget(#MCLI_1, 10, 0, 0, 20) ;: SetGadgetData(#MCLI_1, UTF8("List1"))
AddMenu(#MCLI_1, "List1", #CustomMenu1)

ListIconGadget(#ListIcon_1, 10, 20, 140, 290,"List1",85)
AddGadgetColumn(#ListIcon_1, 1, "A", 50)

CanvasGadget(#MCLI_2, 160, 0, 0, 20) ;: SetGadgetData(#MCLI_2, UTF8("List 2"))
AddMenu(#MCLI_2, "List 2", #CustomMenu2)

ListIconGadget(#ListIcon_2, 160, 20, 140, 290, "List2",85)
AddGadgetColumn(#ListIcon_2, 1, "B", 50)

CanvasGadget(#MCLI_3, 310, 0, 0, 20) ;: SetGadgetData(#MCLI_3, UTF8("List  3"))
AddMenu(#MCLI_3, "List   3", #CustomMenu3)

ListIconGadget(#ListIcon_3, 310, 20, 140, 290, "List3",85)

AddGadgetColumn(#ListIcon_3, 1, "C", 50)

For i = #MCLI_1 To #MCLI_3
  BindGadgetEvent(i, @CanvasMenuButton())
  PostEvent(#PB_Event_Gadget, #Win1, i, #PB_EventType_Change)
Next i

Repeat
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow      
      appQuit = 1
      
    Case #PB_Event_Menu
      Select MenuCanvas
        Case #MCLI_1
          Select EventMenu()
            Case #Mnu_1
              Debug "MenuItem 1 on ListIcon 1"
          EndSelect
        Case #MCLI_2
          Select EventMenu()
            Case #Mnu2_3
              Debug "MenuItem 3 on ListIcon 2"
          EndSelect 
        Case #MCLI_3
          Select EventMenu()
            Case #Mnu3_5
              Debug "MenuItem 5 on ListIcon 3"
          EndSelect
      EndSelect
      
      
    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
      
  EndSelect
Until appQuit
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How do I space out MenuTitles above each ListView.?

Post by dcr3 »

Excellent, with its own individual popup menu even better.Thank you Thunder93. :D :D
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How do I space out MenuTitles above each ListView.?

Post by Thunder93 »

You might like this little better. :wink:

Image

Code: Select all


;-
;- Constants - PRIVATE

#_Menu_None = 0
#_Menu_Item = 1
#_Menu_Drop = 2

#_Menu_Dropping = $010000

;-
;- Constants - PUBLIC

Enumeration Windows
  #Win1
EndEnumeration

Enumeration Gadgets
  #ListIcon_1
  #ListIcon_2
  #ListIcon_3
EndEnumeration

Enumeration MenuItems
  #MenuItem1
  #MenuItem2
  #MenuItem3
  #MenuItem4
  #MenuItem5
  #MenuItem6
  #MenuItem7
  #MenuItem8
  #MenuItem9
  #MenuItem10
  #MenuItem11
  #MenuItem12
EndEnumeration

;-
;- Structures - PRIVATE

Structure _Custom_Menu_Item
  Text.s
  MenuID.l
  ImageID.i
EndStructure

Structure _Custom_Menu
  Title.s
  
  Flags.l
  Canvas.i
  Window.i
  Popup.i
  
  Width.l
  Height.l
  
  FontID.l
  ColorFont.l
  
  ColorIdleBorder.l
  ColorIdleTop.l
  ColorIdleBottom.l
  
  ColorHoverBorder.l
  ColorHoverTop.l
  ColorHoverBottom.l
  
  ColorPressBorder.l
  ColorPressTop.l
  ColorPressBottom.l
  
  HoverItem.l
  PressItem.l
  
  List Item._Custom_Menu_Item()
EndStructure

;-
;- Globals - PRIVATE

Global _MenuLastPopup.i = #Null

;-
;- Macros - PUBLIC

Macro AddMenuGadgetBar(Mg)
  AddMenuGadgetItem((Mg), "-")
EndMacro

;-
;- Procedures - PRIVATE
Procedure _MenuGadgetRedraw(*_Mg._Custom_Menu)
  If (*_Mg And *_Mg\Canvas And IsGadget(*_Mg\Canvas))
    With *_Mg
      If (Not (*_Mg\Flags & #_Menu_Dropping))
        Protected DrawingID.i = StartDrawing(CanvasOutput(\Canvas))
        If (DrawingID)
          Protected TopColor.i, BottomColor.i, BorderColor.i
          
          If ((\PressItem = \HoverItem) And (\PressItem = #_Menu_Item))
            TopColor    = \ColorPressTop
            BottomColor = \ColorPressBottom
            BorderColor = \ColorPressBorder
          ElseIf (\HoverItem)
            TopColor    = \ColorHoverTop
            BottomColor = \ColorHoverBottom
            BorderColor = \ColorHoverBorder
          Else
            TopColor    = \ColorIdleTop
            BottomColor = \ColorIdleBottom
            BorderColor = \ColorIdleBorder
          EndIf
          DrawingMode(#PB_2DDrawing_Gradient)
          BackColor(TopColor)
          FrontColor(BottomColor)
          LinearGradient(0, 0, 0, \Height)
          Box(0, 0, \Width, \Height)
          DrawingMode(#PB_2DDrawing_Default)
          Box(0, 0, 1, \Height, BorderColor)
          Box(0, 0, \Width, 1, BorderColor)
          Box(0, \Height - 1, \Width, 1, BorderColor)
          Box(\Width - 1, 0, 1, \Height, BorderColor)
          
          If ((\PressItem = \HoverItem) And (\PressItem))
            TopColor    = \ColorPressTop
            BottomColor = \ColorPressBottom
            BorderColor = \ColorPressBorder
          ElseIf (\HoverItem)
            TopColor    = \ColorHoverTop
            BottomColor = \ColorHoverBottom
            BorderColor = \ColorHoverBorder
          Else
            TopColor    = \ColorIdleTop
            BottomColor = \ColorIdleBottom
            BorderColor = \ColorIdleBorder
          EndIf
          
          DrawingFont(\FontID)
          DrawingMode(#PB_2DDrawing_Transparent)
          ClipOutput(1, 1, \Width - 2, \Height - 2)
          CompilerIf #PB_Compiler_OS = #PB_OS_Windows
            SelectObject_(DrawingID, \FontID)
            SetTextColor_(DrawingID, \ColorFont)
            SetBkMode_(DrawingID, #TRANSPARENT)
            Protected Rect.RECT
            Rect\left   = 1
            Rect\top    = 1
            Rect\right  = \Width ;- \DropWidthReal - 1
            Rect\bottom = \Height - 1
            DrawText_(DrawingID, @\Title, -1, @Rect,
                      #DT_SINGLELINE | #DT_NOPREFIX | #DT_CENTER | #DT_VCENTER)
          CompilerElse
            DrawText((\Width/2 + 1) - TextWidth(\Title)/2, \Height/2 - TextHeight(\Title)/2, \Title, \ColorFont)
          CompilerEndIf
          UnclipOutput()
          
          StopDrawing()
        EndIf
      EndIf
    EndWith
  EndIf
EndProcedure

Procedure _MenuGadgetUpdate(*_Mg._Custom_Menu, Redraw.i = #False)
  If (*_Mg And *_Mg\Canvas And IsGadget(*_Mg\Canvas))
    With *_Mg
      \Width  = GadgetWidth(\Canvas)
      \Height = GadgetHeight(\Canvas)
      If (Redraw)
        _MenuGadgetRedraw(*_Mg)
      EndIf
    EndWith
  EndIf
EndProcedure

Procedure _MenuCallback()
  Protected *_Mg._Custom_Menu
  *_Mg = GetGadgetData(EventGadget())
  If (*_Mg And *_Mg\Canvas And IsGadget(*_Mg\Canvas))
    With *_Mg
      Protected PreHover.i = \HoverItem
      Protected PrePress.i = \PressItem
      
      Select EventType()          
        Case #PB_EventType_MouseEnter, #PB_EventType_MouseMove, #PB_EventType_LeftButtonDown         
          Protected x.i = GetGadgetAttribute(\Canvas, #PB_Canvas_MouseX)
          Protected y.i = GetGadgetAttribute(\Canvas, #PB_Canvas_MouseY)
          If ((x >= 0) And (x < \Width) And (y >= 0) And (y < \Height))
            \HoverItem = #_Menu_Drop            
          Else
            \HoverItem = #_Menu_None
          EndIf
          If (EventType() = #PB_EventType_LeftButtonDown)
            \PressItem = \HoverItem
          EndIf
        Case #PB_EventType_MouseLeave
          \HoverItem = #_Menu_None
          
        Case #PB_EventType_LeftButtonUp
          If (\PressItem = \HoverItem)
            Select (\PressItem)
              Case #_Menu_Item
                If (ListSize(\Item()))
                  _MenuLastPopup = *_Mg
                  FirstElement(\Item())
                  PostEvent(#PB_Event_Menu, \Window, \Item()\MenuID)
                EndIf
                \PressItem = #_Menu_None
              Case #_Menu_Drop
                If (ListSize(\Item()))
                  If (Not \Popup)
                    \Popup = CreatePopupMenu(#PB_Any)
                    If (\Popup)
                      ForEach (\Item())
                        If (\Item()\Text = "-")
                          MenuBar()
                        Else
                          MenuItem(\Item()\MenuID, \Item()\Text, \Item()\ImageID)
                        EndIf
                      Next
                    EndIf
                  EndIf
                  If (\Popup)
                    \PressItem = #_Menu_None
                    _MenuGadgetRedraw(*_Mg)
                    *_Mg\Flags | #_Menu_Dropping
                    ; "Pre-Drop"
                    _MenuLastPopup = *_Mg
                    DisplayPopupMenu(\Popup, WindowID(\Window), GadgetX(\Canvas, #PB_Gadget_ScreenCoordinate), GadgetY(\Canvas, #PB_Gadget_ScreenCoordinate) + \Height)
                    ; "Post-Drop"
                    *_Mg\Flags & ~#_Menu_Dropping
                  Else
                    _MenuLastPopup = #Null
                  EndIf
                EndIf
            EndSelect
          Else
            \PressItem = #_Menu_None
          EndIf          
          
      EndSelect
      If ((\HoverItem <> PreHover) Or (\PressItem <> PrePress))
        _MenuGadgetRedraw(*_Mg)
      EndIf
    EndWith
  EndIf
EndProcedure

;-
;- Procedures - PUBLIC

Procedure.i EventMenuButton()
  ProcedureReturn (_MenuLastPopup)
EndProcedure

Procedure.i FreeMenuGadget(*_Mg._Custom_Menu)
  If (*_Mg)
    If (*_Mg\Canvas And IsGadget(*_Mg\Canvas))
      SetGadgetData(*_Mg\Canvas, #Null)
      UnbindGadgetEvent(*_Mg\Canvas, @_MenuCallback())
      FreeGadget(*_Mg\Canvas)
    EndIf
    If (*_Mg\Popup)
      FreeMenu(*_Mg\Popup)
    EndIf
    ClearList(*_Mg\Item())
    ClearStructure(*_Mg, _Custom_Menu)
    FreeMemory(*_Mg)
  EndIf
  ProcedureReturn (#Null)
EndProcedure

Procedure.i AddMenuGadget(x.l, y.l, Width.l = 85, Height.l = 25, Title.s = "", Window.l = 0)
  Protected *_Mg._Custom_Menu = #Null
  If (IsWindow(Window))
    Protected Canvas.i = CanvasGadget(#PB_Any, x, y, Width, Height)
    If (Canvas)
      *_Mg = AllocateMemory(SizeOf(_Custom_Menu))
      If (*_Mg)
        InitializeStructure(*_Mg, _Custom_Menu)
        With *_Mg
          \Title = Title
          \Canvas    = Canvas
          \ColorFont = RGB(0, 0, 0)
          \Window    = Window
          
          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Windows
              \FontID = GetGadgetFont(#PB_Default)
            CompilerCase #PB_OS_MacOS
              Protected Temp.i = TextGadget(#PB_Any, 0, 0, 25, 25, " ")
              If Temp
                \FontID = GetGadgetFont(Temp)
                FreeGadget(Temp)
              EndIf
          CompilerEndSelect       
          
          \ColorIdleBorder  = RGB(239, 239, 239) ; RGB(172, 172, 172)
          \ColorIdleTop     = RGB(240, 240, 240)
          \ColorIdleBottom  = RGB(229, 229, 229)
          \ColorHoverBorder = RGB(126, 180, 234)
          \ColorHoverTop    = RGB(236, 244, 252)
          \ColorHoverBottom = RGB(220, 236, 252)
          \ColorPressBorder = RGB(86, 157, 229)
          \ColorPressTop    = RGB(218, 236, 252)
          \ColorPressBottom = RGB(196, 224, 252)
        EndWith
        
        SetGadgetData(Canvas, *_Mg)
        _MenuGadgetUpdate(*_Mg, #True)
        BindGadgetEvent(Canvas, @_MenuCallback())
      Else
        FreeGadget(Canvas)
      EndIf
    EndIf
  EndIf
  ProcedureReturn (*_Mg)
EndProcedure

Procedure.i AddMenuGadgetItem(*_Mg._Custom_Menu, Text.s, ID.i = 0, ImageID.i = #Null)
  Protected Result.i = -1
  If (*_Mg And *_Mg\Canvas And IsGadget(*_Mg\Canvas))
    With *_Mg
      LastElement(\Item())
      AddElement(\Item())
      \Item()\MenuID  = ID
      \Item()\Text    = Text
      \Item()\ImageID = ImageID
      _MenuGadgetUpdate(*_Mg, #True)
      Result = ListSize(\Item()) - 1
    EndWith
  EndIf
  ProcedureReturn (Result)
EndProcedure

Procedure.i ClearMenuGadgetItems(*_Mg._Custom_Menu)
  Protected Result.i = -1
  If (*_Mg And *_Mg\Canvas And IsGadget(*_Mg\Canvas))
    With *_Mg
      ClearList(\Item())
      _MenuGadgetUpdate(*_Mg, #True)
      Result = 0
    EndWith
  EndIf
  ProcedureReturn (Result)
EndProcedure


OpenWindow(#Win1, 0, 0, 460,330, "Window Multiple Canvas Menus", #PB_Window_SystemMenu | #PB_Window_ScreenCentered); | #PB_Window_Minimize)
SetWindowColor(#Win1, $EFEFEF)

;- Menu

Dim _Mg.i(3)

*_Mg = AddMenuGadget(10, 0, 35, 20, "File")
AddMenuGadgetItem(*_Mg, "New", 1)
AddMenuGadgetItem(*_Mg, "Open...", 2)
AddMenuGadgetItem(*_Mg, "Save As..", 3)
AddMenuGadgetBar(*_Mg)
AddMenuGadgetItem(*_Mg, "Exit", 4)
_Mg(0) = *_Mg

*_Mg = AddMenuGadget(160, 0, 85, 20, "File")
AddMenuGadgetItem(*_Mg, "New", 6)
AddMenuGadgetItem(*_Mg, "Import File", 7)
AddMenuGadgetBar(*_Mg)
AddMenuGadgetItem(*_Mg, "Clear", 8)
_Mg(1) = *_Mg

*_Mg = AddMenuGadget(245, 0, 85, 20, "Tools")
AddMenuGadgetItem(*_Mg, "Archiver", 9)
AddMenuGadgetItem(*_Mg, "Notepad", 10)
AddMenuGadgetItem(*_Mg, "Phone E.T.", 11)
_Mg(2) = *_Mg


; ...moving on
ListIconGadget(#ListIcon_1, 10, 20, 140, 290,"List1",85)
AddGadgetColumn(#ListIcon_1, 1, "A", 50)

ListIconGadget(#ListIcon_2, 160, 20, 280, 290, "List2",85)
AddGadgetColumn(#ListIcon_2, 1, "B", 50)


Repeat
  Select WaitWindowEvent()
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 1
          Debug "Yes 1"
        Case 2
          Debug "Yes 2"
        Case 3
          Debug "Yes 3"
        Case 4
          Debug "Application Closing"
          appQuit = 1 
          
        Default
          Debug "Not programmed yet.."
      EndSelect
      
    Case #PB_Event_CloseWindow      
      appQuit = 1    
      
      FreeMenuGadget(_Mg(0))
      FreeMenuGadget(_Mg(1))
      FreeMenuGadget(_Mg(2))  
  EndSelect
  
Until appQuit
Last edited by Thunder93 on Fri Sep 29, 2017 10:19 pm, edited 1 time in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How do I space out MenuTitles above each ListView.?

Post by dcr3 »

Wow. This is nice, I mean extremely nice. You skill's and effort, surpassed all my expectations.
Once again, I appreciate and thank you very much. Thunder93. :D :D :D :D :D
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How do I space out MenuTitles above each ListView.?

Post by Thunder93 »

The visual effect I would have liked to offer isn't achievable in a cross-platform supported way, currently with PureBasic language.

When click to bring up the popup menu, and then simply moving from this menu to another, and It'll automatically close the currently opened popup menu and bring up the other ones without needing any extra leg work.

However with the use of PB native command DisplayPopupMenu, it prevents this standardized menu effect.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: How do I space out MenuTitles above each ListView.?

Post by davido »

@Thunder93,
Very nice. Thank you.

I noticed in the code snippet, Flags is defined as an Integer, is this intended?

Code: Select all

Structure _Custom_Menu
  Title.s
  
  Canvas.l
  Flags.i
  Window.l
  Popup.l
DE AA EB
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How do I space out MenuTitles above each ListView.?

Post by Thunder93 »

It was but isn't necessary, a Long would be more appropriate here.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: How do I space out MenuTitles above each ListView.?

Post by Bisonte »

Thunder93 wrote:It was but isn't necessary, a Long would be more appropriate here.
If you store OS handles in this vars, integer is the type. (or if you use #PB_Any).

That will prevent you from bug-searching.
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Post Reply