 | Using a macro to create an accessible pop-up calendar in WordPerfect® By David Kosorok
In your professional or personal life, you may find that you're constantly referring to calendars searching for a particular date. Just think how convenient it would be if you could easily display a calendar within WordPerfect®. You wouldn't have to flip through page after page of your daily planner or wall calendar; you wouldn't strain your eyes reading the miniscule calendar on your watch, and you'd save time not waiting for your PIM (personal information manager) to load.
In this article, we'll show you how you can create a pop-up calendar with a WordPerfect macro. This macro creates a calendar dialog box that lets you quickly search for dates in different months and years, as shown below in Figure A. The macro can also be used to insert a chosen date into your document in one of two formats: Normal or Fancy. The Normal format prints the month, day and year. The Fancy format prints the date in longhand, as if you were speaking it.
 Figure A: You can use a macro to create a pop-up calendar that displays in your WordPerfect® document.
Using the DialogAddDate control
To display the pop-up calendar, we'll use the WordPerfect DialogAddDate dialog box control. The command is defined as:
DialogAddDate (Dialog: string; Control: string; [Left:
numeric]; [Top: numeric]; [Width: numeric]; [Height:
numeric]; Style: enumeration; MacroVar: variable;
[Year: numeric]; [Month: enumeration]; [Day: numeric])
There are quite a few parameters for this command, so let's take a look at each one. Dialog is the name or number of the dialog box that the date control will be added to. Control can be a name or number, and is used to identify the date control.
The next set of parameters--Left, Top, Width and Height--control the position and size of the date control, in dialog box units. These four parameters are optional.
Style, another optional parameter, determines if the date is valid. The valid arguments are NoValidate! and Validate!. The initial value of the control, and the value of the control when the dialog box is closed, is stored in the MacroVar variable.
The remaining parameters are all optional. Year and Day receive numeric values representing the year and day of the control. The Month parameter receives enumeration in the format January!.
Creating the macro
To create this macro, open a blank document and choose Tools, Macro, Macro, Toolbar. Next, enter the code shown in Listing A. When you enter the code, make sure you don't type the line numbers. These are for reference only.
Note: To save time and accuracy, instead of typing in the code, you can download the macro from the Element K Journals' FTP site.
When you've finished, click the Save & Compile button to display the Macro Save As dialog box. Choose a folder from the Save In dropdown list in which to save the macro and then enter Popcal.wcm in the File Name text box. Now click Save. If you made any mistakes while typing the macro, you'll get the Perfect Script Compiler – Syntax Error dialog box. If your macro has errors, click Cancel Compilation. Find and fix any mistakes, and then choose Save & Compile again. When you can save without getting any error messages, choose File Close to return to your document.
Listing A: The Popcal.wcm macro
1 Application (WordPerfect; "WordPerfect"; Default; "EN")
2 Call (Declare)
3 Call (Show)
4 Return
5
6 Label (Declare)
7 vDay := ?DateDay
8 vMonth :=?DateMonth
9 vYear := ?DateYear
10 vC := 1
11 Global (vDName; vMName; vD; vEx; vY)
12 Return
13
14 Label (Show)
15 DialogDefine (100; 50; 50; 172; 90; 16 + 512; "Popup Calendar")
16 DialogAddText (100; 100; 5; 12; 50; 11; ; "Choose &Date:")
17 DialogAddDate (100; 200; 55; 10; 62; 15; Validate!; var; vYear; vMonth; vDay)
18 DialogAddRadioButton (100; 225; 5; 30; 165; 11; "&Normal (" + vMonth + "/"
19 + vDay + "/" + vYear + ")"; vN)
20 vDate := RegionGetWindowText ("100.200")
21 Fancy (vDate)
22 DialogAddRadioButton (100; 250; 5; 45; 165; 11; "&Fancy Date (" + vDName +
23 ", " + vMName + " " + vD + vEx + ", " + vY+ ")"; vC)
24 DialogAddPushbutton (100; 300; 60; 70; 50; 15; ; "&Insert")
25 DialogAddPushbutton (100; 400; 115; 70; 50; 15; DefaultBttn!; "&Close")
26 DialogShow (100; "WordPerfect"; CB; 200)
27 CallbackWait
28 Return
29
30 Label (CB)
31 If (CB[5] = 274)
32 CallbackResume
33 EndIf
34 Switch (CB[3])
35 CaseOf 300:
36 vDate := RegionGetWindowText ("100.200")
37 vC := RegionGetCheck ("100.250")
38 If (vC)
39 Fancy (vDate)
40 Type (vDName + ", " + vMName + " " + vD + vEx + ", " + vY)
41 Else
42 Type (vDate)
43 EndIf
44 CallbackResume
45 CaseOf 400: CallbackResume
46 EndSwitch
47 Return
48
49 Procedure Fancy (vDate)
50 D[] := StrParseList (vDate; "/")
51 vM := D[1]
52 vD := D[2]
53 vY := D[3]
54 vDT := DateAndTime (vD; vM; vY)
55 vMName := DateMonthName (vDT)
56 vDName := DateWeekdayName (vDT)
57 vLD := SubStr (vD; -1; 1)
58 If ((vD > 0 & vD < 4) | vD > 19)
59 Switch (vLD)
60 CaseOf 1: vEx := "st"
61 CaseOf 2: vEx := "nd"
62 CaseOf 3: vEx := "rd"
63 Default: vEx := "th"
64 EndSwitch
65 EndIf
66 If (vD > 3 & vD < 20)
67 vEx := "th"
68 EndIf
69 EndProc
Using the macro
To use the macro as a calendar, choose Tools, Macro, Play. In the Play Macro dialog box, navigate to the folder in which you saved the Popcal.wcm macro. Select the macro file and then click Play. The Popup Calendar dialog box appears with the current date, as shown in the figure below. Next, click the Calendar button to display a monthly calendar. To find the date you're looking for, use the single arrows to move forward or backward one month at a time and the double arrows to move forward or backward one year at a time. To close the calendar, click Cancel and then click Close.
To use the macro to insert a date in your document, place the insertion point where you want the date to appear and then repeat the steps in the previous paragraph to display the month of the date you're looking for. When you have the appropriate month displayed, click on the calendar day you want to insert, such as June 6, 2001, as shown below. When you do, the new date automatically appears in the Choose Date text box. To insert the date in your document, select the format you want to use, either Normal (M/D/YYYY) or Fancy (Day Name, Month Name, D, YYYY), and then click Insert. The date is inserted at the location of the insertion point and the Popup Calendar dialog box closes.
 Figure B: When you play the Popcal.wcm macro, it displays the current date in the Choose Date text box.
 Figure C: You can use the pop-up calendar to select and insert a date into your document.
Examining the macro
Let's take a look at the commands in this macro. Lines 1-4 contain the main program. Line 2 calls the Declare subroutine on lines 6-12. The Declare subroutine sets the macro defaults by defining the current date with system variables, sets the default date type to Fancy by assigning 1 to vC, and globalizes a series of date variables to be used in a later procedure.
Line 3 calls the subroutine Show on lines 14-28. The basic dialog box shown in Figure B is defined in these lines. The DialogAddDate control on line 17 is a great control for dates. All of the calendar math is done without the macro programmer ever having to worry about leap years or any of the other date concerns commonly associated with calendars. The current date is displayed when the macro is first played. Line 20 uses a Region command to assign the current date to vDate. Line 21 calls the Fancy procedure, and sends it the parameter vDate, which is the standard Month/Day/Year format.
Breaking down the date
The Fancy procedure, found in lines 49-69, breaks the date back down into usable components. The variable on line 50 uses the StrParseList command to break out the month, day and year variables and store them in the D[] array. The StrParseList command is one of the handiest functions in WordPerfect's macro language. It parses a string into substrings. The first parameter is the string that you want split and saved into an array, in this case, vDate. The string should have a consistent character or set of characters that separate each item. The second parameter is this separator character. The vDate variable, for example, uses the forward slash (/) to separate each date element. Lines 51-53 redefine the elements of the array just created into different variable names.
Creating the Fancy format
Line 54 assigns a numeric value representing the date and time, similar to a Julian number. This date can't be earlier than January 1, 1601. This value is then used in lines 55 and 56 to get the name of the month (such as May or June) and the name of the weekday (such as Monday or Tuesday).
Lines 57-68 determine what follows the number of the day, such as the 1st, 2nd, 3rd or 4th. Line 69 ends the Fancy procedure, and returns to the location immediately following the last line calling the procedure, in this case line 22.
Controlling flow with callbacks
The rest of the pop-up calendar is then displayed. Line 27 uses the CallbackWait command to call the last subroutine of the macro (CB), starting with lines 30-47. Lines 31-33 detect when the X in the upper-right corner of the dialog box is selected, and quits the macro. The Switch statement captures the remaining possible keystrokes in lines 34-46.
The first case on line 35 detects the Insert button. Line 36 grabs the date from the Choose Date text box and assigns it to the vDate variable. If the Fancy Date option button is selected, lines 39-40 perform a few simple actions. First, the Fancy procedure is called while passing vDate as the parameter. At this point, vDate contains the selected date in a Month/Day/Year format. As previously explained, the Fancy procedure breaks down the date and formats it as shown after the Fancy Date option in Figure B.
A procedure was only one way to create this subroutine. The procedure, used in conjunction with the global variables, allows the information to be used anywhere in the macro. The procedure could just as easily have been a function that passed the vDate variable and returned the completed string. Basically, it's a matter of preference. There are many macro programmers who may want to avoid using global variables, and, therefore, may choose the function method as the preferred alternative.
If the Normal option button is selected, line 42 simply types the date as shown next to the Normal option in Figure B. Line 44 has the CallbackResume command, which effectively quits the macro. Finally, line 45 closes the macro with the CallbackResume command without typing anything to the document.
Keeping the date at your fingertips
In this article, we've shown you how to write a macro using the DialogAddDate control that creates a pop-up calendar in WordPerfect. You probably won't go through your office or home and clean out all the calendars hanging on your walls, but this macro can be a convenient addition to your WordPerfect environment. You'll always have a calendar close at hand and an easy way to insert formatted dates in your documents.
Copyright® 2000, Element K Content LLC. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Element K Content LLC is prohibited. Element K is a service mark of Element K LLC.

|
| |
|