Page 1 of 1

A Problem with MenuItems (or Me!)

Posted: Sun Sep 07, 2008 12:13 pm
by xProgrammer
Hi Antonio

I am trying to Disable() / Enable() menuitems as they become valid depending upon what else has happened in a program.

The problem is that

MENUITEM <oItem> PROMPT <cPrompt> ACTION <uAction> is setting <oItem> to nil rather than the new menu item. I have modified tutor04.prg to demonstrate the problem:

Code: Select all

#include "FiveLinux.ch"

static oWnd

function Main()

   local lVal := .t., cName := "Hello", cItem, cLbx, nVal := 100, oBar

   DEFINE WINDOW oWnd TITLE "Testing controls" ;
      MENU BuildMenu()

   DEFINE BUTTONBAR oBar OF oWnd

   DEFINE BUTTON OF oBar RESOURCE "gtk-new" ACTION MsgInfo( "New" )

   DEFINE BUTTON OF oBar RESOURCE "gtk-open" ACTION MsgInfo( "Open" )

   DEFINE BUTTON OF oBar RESOURCE "gtk-save" ;
      ACTION MsgInfo( "Save" ) GROUP

   DEFINE BUTTON OF oBar RESOURCE "gtk-quit" ACTION oWnd:End()

   @ 9,  1 SAY "A Say" OF oWnd

   @ 12, 2 CHECKBOX lVal PROMPT "Click me" OF oWnd

   @ 15, 2 GET cName OF oWnd

   @ 18, 2 COMBOBOX cItem OF oWnd ITEMS { "one", "two", "three" }

   @ 9, 22 LISTBOX cLbx OF oWnd ITEMS { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve" }

   @ 21, 2 METER nVal TOTAL 200 OF oWnd

   @ 24, 2 IMAGE FILENAME "flh.gif" OF oWnd

   @ 26, 30 BUTTON "" OF oWnd ACTION MsgInfo( cLbx ) RESOURCE "gtk-ok"

   @ 26, 39 BUTTON "" OF oWnd ACTION MsgInfo( cItem ) RESOURCE "gtk-cancel"

   SET MSGBAR OF oWnd TO " FiveLinux - (c) FiveTech Software 2008"

   ACTIVATE WINDOW oWnd ;
      VALID MsgYesNo( "Are you sure ?" ) ;
      ON RIGHT CLICK MsgInfo( "right click" )

return nil

function BuildMenu()

   local oMenu
   local m2        // I added this variable

   MENU oMenu
      MENUITEM "One"
      MENU
         MENUITEM "First"  RESOURCE "gtk-new"  ACTION MsgInfo( "New" )
	 MENU
	    MENUITEM "One"
	    MENUITEM  m2 PROMPT "Two"  // and modified this line so that m2 should be this menu item
	    SEPARATOR
	    MENUITEM "Three"
	 ENDMENU
	 MENUITEM "Second" RESOURCE "gtk-quit" ACTION oWnd:End()
      ENDMENU

      MENUITEM "Two"     ACTION MsgInfo( "Two" )
      MENUITEM "Three"   ACTION MsgInfo( "Three" )
   ENDMENU
   MsgInfo( ValType( m2 ) )    // I added this MsgInfo which tells us that m2 is Undefined.  Why?
   m2:Disable()                // so this line produces an error.

return oMenu
When I look at FiveLinux.ch I see that the MENUITEM command is being preprocessed into a call to MenuAddItem()

Code: Select all

#xcommand MENUITEM [ <oMenuItem> PROMPT ] [<cPrompt>] ;
			       [ ACTION <uAction> ] ;
			       [ <resource: RESOURCE, NAME, RESNAME> <cResName> ] ;
			    => ;
			       [ <oMenuItem> := ] MenuAddItem( <cPrompt>,;
			       [ \{|o| <uAction> \} ], <cResName> )
I can't find MenuAddItem() but my guess is it must be returning nil. As I can't find it I can't attempt to fix it.

But, as always, my diagnosis could be wrong.

Regards
Doug

Posted: Sun Sep 07, 2008 4:51 pm
by Antonio Linares
Doug,

You are right. There is a bug in FiveLinux\source\classes\pdmenu.prg.

function MenuAddItem() has to be modified this way:

Code: Select all

function MenuAddItem( cPrompt, bAction, cResName )

   local oItem 

   ATail( aMenus ):Add( oItem := TMenuItem():New( cPrompt, bAction, cResName ) )

return oItem
Thanks for your feedback :-)

Posted: Sun Sep 07, 2008 9:32 pm
by xProgrammer
Hi Antonio

Thanks so much for the prompt response and bug fix. I still can't achieve what I want. I now realise that TMenu and TMenuItem aren't subclasses of TControl and hence of TWindow. As a result they don't support Disable() or Enable().

It would be nice to be able to enable or disable menu items. Failing that to add or delete them.I'm sure I did it in FiveWin. (Don't have the source handy). I did look at the FiveWin documentation and saw that MENUITEM has [ ENABLED | DISABLED ] support.

Regards
Doug