[Gambas-user] Problem with code made menu event handling

Bruce bbruen at ...2308...
Sun Oct 7 06:05:39 CEST 2012


Hi Willy,

Se my comments below.

hth
Bruce

On Sun, 2012-10-07 at 03:49 +0200, Willy Raets wrote:
> Hi,
> 
> I'm working on a Add On enabled application.
> 
> Have this module called AddOns taking care of things.
> I looks in a hidden folder AddOns for the file AddOnsInfo
> This file content looks like this (without the ---):
> ---
> 2
> AddOnOne
> AddOnTwo
> ---
> 
> 2 means two AddOns are in the AddOns folder (normal gambas application
> executabes) Next are the names of these AddOns.
> The names are used for in the menu and to start the add ons.
> 
> >From the file the module makes the menu items.
> This all goes well.
> 
> With 1 AddOn in the file (lets say AddOnOne) the menu shows AddOnOne and
> when clicking runs the application AddOnOne
> 
> With 2 AddOns in the file (lets say AddOnOne and AddOnTwo) the menu
> shows AddOnOne and AddOnTwo and When clicking either one of them
> AddOnTwo is ALWAYS run.
> 
> This somehow makes sense to me looking at my code.

See my notes in your code as to why.

> But how do I determine what menu item is clicked 

This is the real problem. Actually, I don't know if there is a way. This
is also the reason that I used that addinmeta class. It essentially acts
as a wrapper for the native Menu class and handles its' own events (via
that SELF observer.  That way each addinmeta instance handles itself and
"knows" what has been clicked.

Doing this in a module as you have done maybe feasible but I found it
easier to take the above approach.


> and make the event
> start the proper add on. 
> After searching in the documentation and google searches  all day I'm
> not finding the proper way of dealing with this.
> 
> Here is the module code:
> '----------------------------------------
> ' Gambas module file
> ' 
> ' ---------------------------------------
> ' Application: AddOnTest
> ' Module: AddOns
> ' Author: W.J.L. Raets
> ' GNU General Public Licence - Version 3
> ' ---------------------------------------
> 
> Private AddOnPath As String
> Private AddOnNumber As Integer
> Private AddOnName As New String[10]
> Private hMenu As Menu
> Private hMenuItem As Menu
> 
> Public Sub SetAddOnsPath(PathToAddOns As String, ParentMenu As Menu)
>   
>   AddOnPath = PathToAddOns
>   hMenu = ParentMenu
>   ReadInfo
>   
> End
> 
> Private Sub ReadInfo()
>   
>   Dim hFile As File
>   Dim sPath, sLine As String
>   Dim iNdex As Integer
>   sPath = AddOnPath & "/AddOnsInfo"
>   '-- Read file values --
>   hFile = Open sPath For Input
>   iNdex = 0
>   Line Input #hFile, sLine
>   AddOnNumber = Val(sLine)
>   If AddOnNumber <> 0 Then
>     For iNdex = 1 To AddOnNumber
>       If Not Eof(hFile) Then
>         Line Input #hFile, sLine
>         AddOnName[iNdex] = sLine
>       Else
>         AddOnNumber = AddOnNumber - 1
>       Endif
>     Next
>     hFile.Close
>     '--
>     For iNdex = 1 To AddOnNumber
>       hMenuItem = New Menu(hMenu) As "_MenuEvent"
>       With hMenuItem
>         .Name = "mnu" & AddOnName[iNdex]
>         .Enabled = True
>         .Caption = AddOnName[iNdex]
>         .Text = AddOnName[iNdex] & "..."
>       End With
>     Next
>   Endif
>   If hMenu.Children.Count = 0 Then
>     hMenu.Visible = False
>   Endif
> 
>   
> End
> 
> Public Sub _MenuEvent_Click()
>   
>   Dim iX, iLength As Integer
>   Dim AddOnToRun As String

When this is run, hMenuItem will be the last item you added, therefore
no matter how many Add-Ins you load only the last one will be run.
So you would need to find the menu item that was clicked and set
hMenuItem to that.

>   iLength = Len(hMenuItem.Caption)
>   AddOnToRun = Left$(hMenuItem.Caption, (iLength - 3))
>   AddOnToRun = AddOnToRun & ".gambas"
>   Message(AddOnToRun)
>   AddOns.Run(AddOnToRun)
>   
> End
> 
> Public Sub Run(AddOnName As String)
>   
>   Dim sPath As String
>   Dim hProcess As Process
>   sPath = AddOnPath & "/" & AddOnName
>   hProcess = Shell sPath Wait
>   hProcess.Kill
>   
> End
> '-------------------------------
> Maybe someone can point me in the proper direction.
> 
> Thanks,
> 
> Willy
> 






More information about the User mailing list