Post Reply 
Sample program for creating and using the DRAWMENU command
04-09-2015, 05:00 PM (This post was last modified: 04-09-2015 05:12 PM by kharpster.)
Post: #1
Sample program for creating and using the DRAWMENU command
Here is a program that I put together to test the DRAWMENU command, it allows you to control the items on the menu using a list. Any menu item left blank will return a value of zero. The program is only to demonstrate the functionality of the DRAWMENU command and how to read the selected item number. Feel free to use the code as you see fit, as long as you do not charge for it or any program that contains my code.

Many thanks to Wes Loewer for the starting point of my code (http://www.hpmuseum.org/cgi-sys/cgiwrap/...ead=253307) !!!


The main program name is: MENUTEST


// Initialize procedures used in project
PUTMENU();
GETMENU();

// Initialize variables used globally in project
mSEL;

EXPORT MENUTEST()
BEGIN
// Initial local variables used within this procedure
LOCAL m,m1,mx,my,mTXT;

// Flush the mouse buffer
WHILE MOUSE(1)≥0 DO END;

// Set the menu text and draw the menu, blank items will return a selected value of zero
mTXT := {"ITEM1","ITEM2","","","","ITEM6"};
PUTMENU(mTXT);

// Wait for mouse input (screen touch)
REPEAT
m := MOUSE;
m1 := m(1);
UNTIL SIZE(m1)>0;

// Convert mouse coordinates to decimal (you can find the arrow using shift 9 on the Prime keyboard)
mx := B→R(m1(1));
my := B→R(m1(2));

// Determine if mouse coordinates are inside of a valid menu item otherwise return a zero
GETMENU(mx,my,mTXT);

// If a non-blank menu item was selected, do something, in this case we just issue a message
IF mSEL > 0 THEN
MSGBOX("Menu item " + mSEL + " was selected");
END;
END;

// ----------------------------------------------
// Draw the menu using the list passed in
// ----------------------------------------------
PUTMENU(mTXT)
BEGIN
DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
END;

// ------------------------------------------------------------------------------------------
// Get the number of the menu item selected (1-6) by checking mouse position
// Menu items with blank text will return a zero
// ------------------------------------------------------------------------------------------
GETMENU(mx,my,mTXT)
BEGIN
mSEL := 0;
IF my≥220 AND my≤239 THEN
CASE
IF mx≥0 AND mx≤51 AND mTXT(1)>"" THEN
mSEL := 1;
END;
IF mx≥53 AND mx≤104 AND mTXT(2)>"" THEN
mSEL := 2;
END;
IF mx≥106 AND mx≤157 AND mTXT(3)>"" THEN
mSEL := 3;
END;
IF mx≥159 AND mx≤210AND mTXT(4)>"" THEN
mSEL := 4;
END;
IF mx≥212 AND mx≤263 AND mTXT(5)>"" THEN
mSEL := 5;
END;
IF mx≥265 AND mx≤319 AND mTXT(6)>"" THEN
mSEL := 6;
END;
END;
END;
END;
Find all posts by this user
Quote this message in a reply
04-10-2015, 11:52 AM
Post: #2
RE: Sample program for creating and using the DRAWMENU command
excellent, could you please add a loop to the program, this constantly running.

target

Pressing an empty menu, remain in loop
Pressing the ESC key out

another request

create a popup menu selection

{
{ "ITEM menu1", "option1", "option2", ..., "option"}
{ "ITEM menu2", "option1", "option2", ..., "optionn"},{"
{}
{}
...
{ "ITEM menun", "option1", "option2", ..., "optionn"},{"
};

Thanks and sorry for my bad English
Find all posts by this user
Quote this message in a reply
04-10-2015, 07:10 PM
Post: #3
RE: Sample program for creating and using the DRAWMENU command
Here's a little test program that uses a loop:

//softmenu-DRAWMENU demo

softmenu(mx, my, n);
keyup();
keydown();

EXPORT softmen()
BEGIN
LOCAL menu, tmp, m, m1, mx, my;
RECT();
DRAWMENU("1","2","3","4","5","Exit");
TEXTOUT_P("PRESS A MENU KEY...",0, 20);
WAIT(2);

keydown();
m:= MOUSE;
m1:= m(1);
my:= m1(2);
mx:= m1(1);
menu:= softmenu(mx, my, 6);
WHILE menu < 6 DO
IF menu > -1 THEN
RECT_P(G0, 0, 40, 319, 219);
DRAWMENU("1","2","3","4","5","Exit");
TEXTOUT_P("Pressed: "+menu, 0, 100);
END;
keyup();
mx:= keydown();
IF mx == -1 THEN
my:= 0;
ELSE my:= 240;
END;
menu:= softmenu(mx, my, 6);
END;
END;

softmenu(x, y, n)
BEGIN
LOCAL m;
m:= -1;
IF y >= 220 THEN
CASE
IF 0 <= x <= 51 THEN m:= 1; END;
IF 53 <= x <= 104 THEN m:= 2; END;
IF 106 <= x <= 157 THEN m:= 3; END;
IF 159 <= x <= 210 THEN m:= 4; END;
IF 212 <= x <= 263 THEN m:= 5; END;
IF 265 <= x <= 319 THEN m:= 6; END;
DEFAULT m:=0;
END;
IF m > n THEN m:=0; END;
END;
RETURN m;
END;

keyup()
BEGIN
LOCAL m, m1;
m:= MOUSE;
m1:= m(1);
WHILE SIZE(m1) > 0 DO
m:= MOUSE;
m1:= m(1);
END;
END;

keydown()
BEGIN
LOCAL m, m1, mx, k;
m:= MOUSE;
m1:= m(1);
WHILE SIZE(m1) == 0 DO
m:= MOUSE;
m1:= m(1);
END;
k:= m1(1);
IF m1(2) < 220 THEN
k:= -1; END;
RETURN k;
END;
Find all posts by this user
Quote this message in a reply
04-11-2015, 04:42 AM
Post: #4
RE: Sample program for creating and using the DRAWMENU command
In response to a request for a program that loops by (compsystems), here is an update of my prior code. You now have to press the ESC key or by default (no coding required) the ON key to exit the program. Additionally, I have utilized the TEXTOUT_P command to write information to the screen in lieu of using MSGBOX. The program also provides the functionality of returning and displaying the key number from the GETKEY command....

// Initialize procedures used in project
EVENTHANDLER();
PUTMENU();
GETMENU();

// Initialize variables used globally in project
mSEL;
eTYP;
kP;
m;
m1;

EXPORT MENUTEST()
BEGIN
// Initialize local variables used within this procedure
LOCAL mx,my,mTXT,EXITPGM;

// Set the menu text (this is softcoded and determines if a menu item is valid)
mTXT := {"ITEM1","ITEM2","","","","ITEM6"};

// MAIN CODE - Loop until ESC is pressed, then exit the program
EXITPGM := 0;
RECT();
WHILE EXITPGM == 0 DO

// Draw the menu
PUTMENU(mTXT);

// Flush the mouse buffer
WHILE MOUSE(1) ≥ 0 DO END;

// Loop until we have a keyboard or mouse event
REPEAT
EVENTHANDLER();
UNTIL eTYP <> "";

CASE

// If the event type was a keyboard action then process it
IF eTYP == "K" THEN
// If the ESC key was pressed set the program to end
IF kP == 4 THEN
EXITPGM := 1;
ELSE
// ESC was not pressed do what you want with the keypress, we are just going to do a TEXTOUT
RECT_P(0,0,319,219);
TEXTOUT_P("Key "+kP+" was pressed",95,100);
END;
END;

// If the event type was mouse action then process it
IF eTYP == "M" THEN
// Convert mouse coordinates to decimal
mx := B→R(m1(1));
my := B→R(m1(2));
// Determine if mouse coordinates are inside of a valid menu item otherwise return a zero
GETMENU(mx,my,mTXT);
// If an empty/blank menu item was selected, do something, we are just going to do a TEXTOUT
IF mSEL > 0 THEN
RECT_P(0,0,319,219);
TEXTOUT_P("Menu item "+mSEL+" was selected",75,100);
ELSE
// Mouse not in menu, do something if needed
END;
END;

END;

END;
END;

// -------------------------------------------------------------------
// Detect keyboard or mouse input (keyboard has priority)
// -------------------------------------------------------------------
EVENTHANDLER()
BEGIN
eTYP := "";
kP := GETKEY;
IF kP <> -1 THEN
eTYP := "K";
ELSE
m := MOUSE;
m1 := m(1);
IF SIZE(m1) > 0 THEN
eTYP := "M";
END;
END;
END;

// ----------------------------------------------
// Draw the menu using the list passed in
// ----------------------------------------------
PUTMENU(mTXT)
BEGIN
DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
END;

// ------------------------------------------------------------------------------------------
// Get the number of the menu item selected (1-6) by checking mouse position
// Menu items with empty/blank text will return a zero
// ------------------------------------------------------------------------------------------
GETMENU(mx,my,mTXT)
BEGIN
mSEL := 0;
IF my≥220 AND my≤239 THEN
CASE
IF mx≥0 AND mx≤51 AND mTXT(1)>"" THEN
mSEL := 1;
END;
IF mx≥53 AND mx≤104 AND mTXT(2)>"" THEN
mSEL := 2;
END;
IF mx≥106 AND mx≤157 AND mTXT(3)>"" THEN
mSEL := 3;
END;
IF mx≥159 AND mx≤210AND mTXT(4)>"" THEN
mSEL := 4;
END;
IF mx≥212 AND mx≤263 AND mTXT(5)>"" THEN
mSEL := 5;
END;
IF mx≥265 AND mx≤319 AND mTXT(6)>"" THEN
mSEL := 6;
END;
END;
END;
END;
Find all posts by this user
Quote this message in a reply
04-11-2015, 09:25 AM (This post was last modified: 04-11-2015 08:45 PM by Thomas_Sch.)
Post: #5
RE: Sample program for creating and using the DRAWMENU command
Many thanks for your framework!
I added just a little bit of formatting (indent).
Code:

// Initialize procedures used in project
EVENTHANDLER();
PUTMENU();
GETMENU();

// Initialize variables used globally in project
mSEL;
eTYP;
kP;
m;
m1;

EXPORT MENUTEST()
BEGIN
  // Initialize local variables used within this procedure
  LOCAL mx,my,mTXT,EXITPGM;
  
  // Set the menu text (this is softcoded and determines if a menu item is valid)
  mTXT := {"ITEM1","ITEM2","","","","ITEM6"};
  
  // MAIN CODE - Loop until ESC is pressed, then exit the program
  EXITPGM := 0;
  RECT();
  WHILE EXITPGM == 0 DO
    
    // Draw the menu
    PUTMENU(mTXT);
    
    // Flush the mouse buffer
    WHILE MOUSE(1) ≥ 0 DO END;
    
    // Loop until we have a keyboard or mouse event
    REPEAT
      EVENTHANDLER();
    UNTIL eTYP <> "";
    
    CASE    
      // If the event type was a keyboard action then process it
      IF eTYP == "K" THEN
        // If the ESC key was pressed set the program to end
        IF kP == 4 THEN
          EXITPGM := 1;
        ELSE
          // ESC was not pressed do what you want with the keypress, 
          //  we are just going to do a TEXTOUT
          RECT_P(0,0,319,219);
          TEXTOUT_P("Key "+kP+" was pressed",95,100);
        END;
      END;  // IF eTYP == "K"
      
      // If the event type was mouse action then process it
      IF eTYP == "M" THEN
        // Convert mouse coordinates to decimal
        mx := B→R(m1(1));
        my := B→R(m1(2));
         // Determine if mouse coordinates are inside of a valid menu 
         // item otherwise return a zero
        GETMENU(mx,my,mTXT);
         // If a valid menu item was selected, do something, we are just 
         // going to do a TEXTOUT
        IF mSEL > 0 THEN
          RECT_P(0,0,319,219);
          TEXTOUT_P("Menu item "+mSEL+" was selected",75,100);
        ELSE
          // Mouse not in menu, do something if needed
        END;
      END;  // IF eTYP == "M"
      
    END;  // CASE
    
  END;  // WHILE EXITPGM
END;

// -------------------------------------------------------------------
// Detect keyboard or mouse input (keyboard has priority)
// -------------------------------------------------------------------
EVENTHANDLER()
BEGIN
  eTYP := "";
  kP := GETKEY;
  IF kP <> -1 THEN
    eTYP := "K";
  ELSE
    m := MOUSE;
    m1 := m(1);
    IF SIZE(m1) > 0 THEN
      eTYP := "M";
    END;
  END;
END;

// ----------------------------------------------
// Draw the menu using the list passed in
// ----------------------------------------------
PUTMENU(mTXT)
BEGIN
    DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
END;

// ----------------------------------------------------------------------------
// Get the number of the menu item selected (1-6) by checking mouse position
// Menu items with empty/blank text will return a zero
// ----------------------------------------------------------------------------
GETMENU(mx,my,mTXT)
BEGIN
  mSEL := 0;
  IF my≥220 AND my≤239 THEN
    CASE
      IF mx≥0 AND mx≤51 AND mTXT(1)>"" THEN
        mSEL := 1;
      END;
      IF mx≥53 AND mx≤104 AND mTXT(2)>"" THEN
        mSEL := 2;
      END;
      IF mx≥106 AND mx≤157 AND mTXT(3)>"" THEN
        mSEL := 3;
      END;
      IF mx≥159 AND mx≤210AND mTXT(4)>"" THEN
        mSEL := 4;
      END;
      IF mx≥212 AND mx≤263 AND mTXT(5)>"" THEN
        mSEL := 5;
      END;
      IF mx≥265 AND mx≤319 AND mTXT(6)>"" THEN
        mSEL := 6;
      END;  // IF mx 
    END;  // CASE
  END;  // IF my
END;

edit: changed comment line
// If an empty/blank menu item was selected, do something, we are just going to do a TEXTOUT
to
// If a valid menu item was selected, do something, we are just going to do a TEXTOUT
Find all posts by this user
Quote this message in a reply
04-11-2015, 02:41 PM
Post: #6
RE: Sample program for creating and using the DRAWMENU command
Thomas,

The program is actually indented on my Prime, but lost it's formatting when I cut and pasted it from the Connectivity Kit window into the forum. Thanks for adding it back in!

Am I missing a trick to retain the formatting during the copy and paste operation from the Connectivity Kit?
Find all posts by this user
Quote this message in a reply
04-11-2015, 03:07 PM
Post: #7
RE: Sample program for creating and using the DRAWMENU command
(04-11-2015 02:41 PM)kharpster Wrote:  Thomas,

The program is actually indented on my Prime, but lost it's formatting when I cut and pasted it from the Connectivity Kit window into the forum. Thanks for adding it back in!

Am I missing a trick to retain the formatting during the copy and paste operation from the Connectivity Kit?
There are two possible 'tools' or steps to save the formatting:
- using the
Code:
 [code]   [ /code ]
tags
(hash sign in the top line of the posting editor, labeled "insert formatted code")
- using an text editor like notepad or notepad++ as a intermediate step
for copying the program from CK to editor, then to the form
But I'm not sure whether the second step is really necessary.
Find all posts by this user
Quote this message in a reply
04-11-2015, 07:26 PM
Post: #8
RE: Sample program for creating and using the DRAWMENU command
Thank you Thomas!, I totally missed the hash mark (#) to insert formatted code into the message window.

Incidentally this comment line:

// If an empty/blank menu item was selected, do something, we are just going to do a TEXTOUT


Should read:

// If a valid menu item was selected, do something, we are just going to do a TEXTOUT


Just too old to be updating and posting code after midnight...
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)