Find your cursor by field name.
By now, most programmers know that in an interactive program, the location of the cursor can be retrieved by accessing fields in the File Information Data Structure (INFDS) for the display file. However, as of V2R2M0, there is a better way to accomplish this task. The method I am about to explain involves using a new DDS keyword called, appropriately enough, Return Cursor Location (RTNCSRLOC). This method has some distinct advantages over the old way, as you are about to find out.
Now, you would think from the name that the RTNCSRLOC keyword would return the row and column coordinates of the cursor-which, by the way, is exactly what the INFDS gives you. That is not, however, what this keyword does. Instead, it returns the name of the field (as it was defined in the DDS) that the cursor is on.
When you stop to think about it, this has some real advantages over the row and column coordinates you get from the INFDS. It means that if you move a field in a display file from one location on the screen to another, you don't have to worry about changing the program which uses that display file. Your program makes decisions based on the name of the field where the cursor is located-not the row and column coordinates. Another advantage is that if you use the values in the INFDS, you have to perform some rather awkward binary-to-decimal conversions, whereas the RTNCSRLOC keyword does not have this problem.
The Keyword in Action
To illustrate how this keyword works, I have written a simple data entry program that prompts the user for a customer code and product code. If the user presses the F4 key within an entry field, a program is called which displays a window allowing the user to make a selection from a list of choices. The selection window differs depending on the location of the cursor when the F4 key is pressed. If the cursor is on the customer code field, the user sees a window from which he can select a customer by name (see 1). If the cursor is on the product code field, the user would be able to select a product code from the descriptions provided in the window. If the cursor is not on either field, an error message is displayed.
To illustrate how this keyword works, I have written a simple data entry program that prompts the user for a customer code and product code. If the user presses the F4 key within an entry field, a program is called which displays a window allowing the user to make a selection from a list of choices. The selection window differs depending on the location of the cursor when the F4 key is pressed. If the cursor is on the customer code field, the user sees a window from which he can select a customer by name (see Figure 1). If the cursor is on the product code field, the user would be able to select a product code from the descriptions provided in the window. If the cursor is not on either field, an error message is displayed.
How It Works
Now let's take a close look at the RTNCSRLOC keyword and see how it fits into our example. This keyword has two required parameters as well as an optional third parameter. Consider the four lines of code highlighted in 2 (the DDS for display file XRK001DF). The two required parameters, which I called RCD and FLD, must be defined as 10-character hidden fields. RCD will return the name of the record format and FLD will return the name of the field that the cursor is on. If the display file contains a single record, as in this example, the RCD field is still required, even though it is superfluous.
Now let's take a close look at the RTNCSRLOC keyword and see how it fits into our example. This keyword has two required parameters as well as an optional third parameter. Consider the four lines of code highlighted in Figure 2 (the DDS for display file XRK001DF). The two required parameters, which I called RCD and FLD, must be defined as 10-character hidden fields. RCD will return the name of the record format and FLD will return the name of the field that the cursor is on. If the display file contains a single record, as in this example, the RCD field is still required, even though it is superfluous.
The third parameter (POS) is optional and must be a hidden field defined as four positions, signed numeric. This field will return the relative position within the field where the cursor is located. For example, if we had a three- byte field on the screen, and the cursor was in the second position of the field, then the POS variable would contain the value 2. For this particular example, this optional parameter is not necessary.
The RPG program (3) starts out by displaying the main data entry screen. If the F4 key is pressed, the program determines what field the cursor is on by checking the value of FLD. If the cursor is on the customer code field, then FLD will contain the value 'CUSTNO' obtained from the display file XRK001DF. A program (PGMA) is called which presents a selection window to the user, allowing him to select a customer from a list by name. Since the code to display selection information is application-specific, I have not included PGMA or PGMB (the product description display) in this article. Each program is a simple subfile inquiry which gives the user the capability to select an item from the list.
The RPG program (Figure 3) starts out by displaying the main data entry screen. If the F4 key is pressed, the program determines what field the cursor is on by checking the value of FLD. If the cursor is on the customer code field, then FLD will contain the value 'CUSTNO' obtained from the display file XRK001DF. A program (PGMA) is called which presents a selection window to the user, allowing him to select a customer from a list by name. Since the code to display selection information is application-specific, I have not included PGMA or PGMB (the product description display) in this article. Each program is a simple subfile inquiry which gives the user the capability to select an item from the list.
If the cursor is on the product code field, then FLD will contain the value 'PRODCD,' in which case PGMB is called. This program also presents the user with a selection window; however, this one would allow him to select a product. Finally, if the cursor is not on either field, then FLD will contain blanks. In this case, indicator 70 is set on, which causes the message "Cursor not in an input field" to be displayed at the bottom of the screen.
As you can see by this example, the RTNCSRLOC keyword is not only very powerful, but also very easy to use. By eliminating the need to hard-code specific row and column values in your program, you reduce the amount of maintenance you need to perform when fields get moved around on the screen. The net result of using the keyword is the added flexibility it gives to your application programs. So, the next time you need to know where the cursor is on the screen, give this keyword a try.
Robin Klima is a senior technical editor at Midrange Computing.
REFERENCES
Data Description Specifications Reference (SC41-9620), CD ROM QBKA7401.
Return Cursor Location: Beyond Screen Coordinates
Figure 1 Selecting a Customer Code
XRK001RG Sample Entry Screen ............................... Customer code . . . . ___ : Select customer code : : : Product code . . . . ___ : 1=Select : : _ ACC ACME CHEMICAL CO : : _ JBG JOE'S BAR & GRILL : : _ OSI OMEGA SOFTWARE INC : : More... : : F12=Cancel : : : :.............................: F3=Exit F4=Prompt
Return Cursor Location: Beyond Screen Coordinates
Figure 2 Display File XRK001DF
*=============================================================== * To compile: * * CRTDSPF FILE(XXX/XRK001DF) SRCFILE(XXX/QDDSSRC) * *=============================================================== *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... A DSPSIZ(24 80 *DS3) A PRINT A ERRSFL A R MAIN A CA03(03 'Exit') A CA04(04 'Prompt') A RTNCSRLOC(&RCD &FLD &POS) A RCD 10A H A FLD 10A H A POS 4S 0H A PGMNAM 10A O 1 2 A 70 ERRMSG('Cursor not in an input fiel- A d.' 70) A 1 31'Sample Entry Screen' A DSPATR(HI) A 5 2'Customer code . . . .' A CUSTNO 3A B 5 25 A 7 2'Product code . . . .' A PRODCD 3A B 7 25 A 23 2'F3=Exit' A COLOR(BLU) A 23 12'F4=Prompt' A COLOR(BLU) *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+...
Return Cursor Location: Beyond Screen Coordinates
Figure 3 RPG Program XRK001RG
*=============================================================== * To compile: * * CRTRPGPGM PGM(XXX/XRK001RG) SRCFILE(XXX/QRPGSRC) * *=============================================================== *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 FXRK001DFCF E WORKSTN * I SDS I 1 10 PGMNAM * C *IN03 DOUEQ*ON C EXFMTMAIN C *IN04 IFEQ *ON C SELEC * If the cursor is on the customer code field then call PGMA * to present the user with the customer code selection window. C FLD WHEQ 'CUSTNO' C CALL 'PGMA' C PARM CUSTNO * If the cursor is on the product code field then call PGMB * to present the user with the product code selection window. C FLD WHEQ 'PRODCD' C CALL 'PGMB' C PARM PRODCD * If the cursor is not on either field then * issue an error message. C OTHER C MOVE *ON *IN70 C ENDSL C ENDIF C ENDDO * C MOVE *ON *INLR *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
LATEST COMMENTS
MC Press Online