Use of menus when programming the HP Prime

Publicado el por

Use of menus when programming the HP Prime

I have just seen a HP Prime version of the NPV, IRR and cashflow applications made by Salvomic, a user in HPmuseum.org. I found it much longer than my own limited programs, which in reality were just functions to be used out of the keyboard, without any procedural or data entry instructions. But then I saw a screen shot with menu items. That was interesting! I had heard that many users wanted to be able to use the menus in the system, but there was no direct access to them with the common HP Prime functions. That got me interested and I started to search the program code for the menu functions information. It ended up being more low-level functions that I initially expected!

Let’s take a look at them. First, you need to know that something has happened, either on the screen as screen touch (or mouse in the case of the emulator), or on the keyboard.

// —————————————————————————————————

// 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;

Then, he defines the procedure PUTMENU(mTXT), where mTXT is a list with 6 text elements. The real procedure that draws the menu is DRAWMENU() with all the text elements. Some of them may be “”.

// ———————————————————————

// 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;

He also defines the procedure GETMENU(mx,my,mTXT), where mx and my are the coordinates of the mouse click, using the convention of starting in the upper left corner of the screen, counting pixels. He uses a simple CASE function, after determining that the mouse click is in the lower part of the screen (line 4 of the procedure)

// —————————————————————————————————————————————

// 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;

So, you can copy and paste (with permission from Salvomic, of course) into your programs! This is an amazing improvement in terms of user friendliness! (I assume you copy this to the program editor in the emulator or connectivity kit, and then transfer it to your "real" calculator!)

Comentarios: 0
Más sobre: HP prime, menu, programming

Solo los usuarios registrados pueden poner comentarios.
Identificarse y añadir comentario Regístrese ahora