Project

General

Profile

signature_demo.p

Sergey Ivanovskiy, 11/05/2026 12:42 PM

Download (16.2 KB)

 
1
/*------------------------------------------------------------------------
2
    File        : signature_demo.p
3
    Purpose     : FWD User Meeting demo (#12087, topic 15): drive a signature
4
                  pad with an LCD screen, attached to the user's PC, from 4GL
5
                  code running in the FWD web client.
6

    
7
    Notes       : The pad is reached through the browser: 4GL code -> FWD
8
                  server -> web client (JavaScript) -> local SigWeb service
9
                  -> USB pad.  With webClient/signaturePadInSimMode = TRUE
10
                  the FWD server simulates the whole pad (r16820): the LCD,
11
                  its hot spots and the ink; the mouse is the pen.
12

    
13
                  The pad screens are drawn by the 4GL code with
14
                  LCDWriteString (text and pseudo-graphic boxes) and react
15
                  through KeyPadAddHotSpot / KeyPadQueryHotSpot:
16
                  1. Review: the delivery details, Cancel and OK.
17
                  2. Sign:   a framed signing box, Clear, Cancel and OK.
18
                  3. Result: "Thank you" (the signature is saved as a BMP
19
                     and shown in the window) or "Cancelled".
20
                  The simulated LCD is shown 1:1 in the widget, so the LCD
21
                  coordinates below are widget pixels (400 x 300).
22
  ----------------------------------------------------------------------*/
23

    
24
/* hot spot key codes */
25
&SCOPED-DEFINE HS-OK     1
26
&SCOPED-DEFINE HS-CANCEL 2
27
&SCOPED-DEFINE HS-CLEAR  3
28

    
29
/* pad screens */
30
&SCOPED-DEFINE SCR-IDLE   0
31
&SCOPED-DEFINE SCR-REVIEW 1
32
&SCOPED-DEFINE SCR-SIGN   2
33
&SCOPED-DEFINE SCR-DONE   3
34

    
35
DEFINE VARIABLE hSig      AS HANDLE    NO-UNDO.
36
DEFINE VARIABLE hImg      AS HANDLE    NO-UNDO.
37
DEFINE VARIABLE cTitle    AS CHARACTER NO-UNDO INITIAL "Arial, size=14, bold".
38
DEFINE VARIABLE cBody     AS CHARACTER NO-UNDO INITIAL "Arial, size=11".
39
DEFINE VARIABLE cBold     AS CHARACTER NO-UNDO INITIAL "Arial, size=11, bold".
40
DEFINE VARIABLE cBox      AS CHARACTER NO-UNDO INITIAL "Courier New, size=12, bold".
41
DEFINE VARIABLE dCharW    AS DECIMAL   NO-UNDO INITIAL 9.6.  /* Courier New 12 pt cell width */
42
DEFINE VARIABLE iLineH    AS INTEGER   NO-UNDO INITIAL 15.   /* pseudo-graphic line pitch */
43
DEFINE VARIABLE cSigFile  AS CHARACTER NO-UNDO.
44
DEFINE VARIABLE lLcd      AS LOGICAL   NO-UNDO.
45
DEFINE VARIABLE iScreen   AS INTEGER   NO-UNDO.
46
DEFINE VARIABLE iPoints   AS INTEGER   NO-UNDO.
47

    
48
DEFINE VARIABLE cOrder    AS CHARACTER NO-UNDO FORMAT "x(12)"  LABEL "Order"
49
   INITIAL "SO-104522".
50
DEFINE VARIABLE cCustomer AS CHARACTER NO-UNDO FORMAT "x(30)"  LABEL "Customer"
51
   INITIAL "Northwind Garden Supplies".
52
DEFINE VARIABLE cItems    AS CHARACTER NO-UNDO FORMAT "x(30)"  LABEL "Items"
53
   INITIAL "3 pallets, 42 cartons".
54
DEFINE VARIABLE cStatus   AS CHARACTER NO-UNDO FORMAT "x(70)"  LABEL "Status"
55
   INITIAL "Press Start Signing to send the delivery to the pad".
56
DEFINE VARIABLE cLog      AS CHARACTER NO-UNDO
57
   VIEW-AS EDITOR SCROLLBAR-VERTICAL SIZE-PIXELS 336 BY 152.
58

    
59
DEFINE BUTTON btnStart  LABEL "Start Signing" SIZE-PIXELS 100 BY 26.
60
DEFINE BUTTON btnClear  LABEL "Clear"         SIZE-PIXELS 80 BY 26.
61
DEFINE BUTTON btnAccept LABEL "Accept"        SIZE-PIXELS 80 BY 26.
62
DEFINE BUTTON btnCancel LABEL "Cancel"        SIZE-PIXELS 80 BY 26.
63
DEFINE BUTTON btnExit   LABEL "Exit"          SIZE-PIXELS 80 BY 26.
64

    
65
DEFINE FRAME fMain
66
   cOrder    AT X 70  Y 6
67
   cCustomer AT X 70  Y 26
68
   cItems    AT X 70  Y 46
69
   "Signature pad screen:" AT X 12  Y 76
70
   "Saved image:"          AT X 430 Y 76
71
   "Pad events:"           AT X 430 Y 220
72
   cLog      AT X 430 Y 240 NO-LABEL
73
   btnStart  AT X 12  Y 410
74
   btnClear  AT X 120 Y 410
75
   btnAccept AT X 208 Y 410
76
   btnCancel AT X 296 Y 410
77
   btnExit   AT X 686 Y 410
78
   cStatus   AT X 60  Y 446
79
   WITH SIDE-LABELS SIZE-PIXELS 780 BY 476 THREE-D NO-BOX.
80

    
81
ASSIGN
82
   CURRENT-WINDOW:TITLE         = "Delivery Confirmation"
83
   CURRENT-WINDOW:WIDTH-PIXELS  = 780
84
   CURRENT-WINDOW:HEIGHT-PIXELS = 476.
85

    
86
CREATE SIGNATURE hSig
87
   ASSIGN NAME          = "sigPad"
88
          FRAME         = FRAME fMain:HANDLE
89
          X             = 12
90
          Y             = 96
91
          WIDTH-PIXELS  = 400
92
          HEIGHT-PIXELS = 300
93
          BGCOLOR-RGB   = 0xFFFFFF
94
          FGCOLOR-RGB   = 0x000000
95
          SENSITIVE     = TRUE
96
          VISIBLE       = TRUE.
97

    
98
CREATE IMAGE hImg
99
   ASSIGN FRAME          = FRAME fMain:HANDLE
100
          X              = 430
101
          Y              = 96
102
          WIDTH-PIXELS   = 336
103
          HEIGHT-PIXELS  = 118
104
          STRETCH-TO-FIT = TRUE
105
          RETAIN-SHAPE   = TRUE
106
          VISIBLE        = TRUE.
107

    
108
/* every pen-up is the moment to look at the pad: hot spot taps and ink */
109
ON "PEN-UP" OF hSig DO:
110
   RUN checkPad.
111
END.
112

    
113
ON CHOOSE OF btnStart IN FRAME fMain DO:
114
   RUN startSigning.
115
END.
116

    
117
/* the window buttons do what the pad buttons do; setButtons enables each one only when it applies */
118
ON CHOOSE OF btnClear IN FRAME fMain DO:
119
   RUN logEvent("Window: Clear").
120
   RUN clearInk.
121
END.
122

    
123
ON CHOOSE OF btnAccept IN FRAME fMain DO:
124
   RUN logEvent("Window: Accept").
125
   RUN acceptSignature.
126
END.
127

    
128
ON CHOOSE OF btnCancel IN FRAME fMain DO:
129
   RUN logEvent("Window: Cancel").
130
   RUN cancelSigning.
131
END.
132

    
133
ON CHOOSE OF btnExit IN FRAME fMain
134
   APPLY "CLOSE" TO THIS-PROCEDURE.
135

    
136
ON CLOSE OF THIS-PROCEDURE DO:
137
   IF iScreen <> {&SCR-IDLE} THEN
138
      hSig:SetTabletState(0).
139
   APPLY "CLOSE" TO CURRENT-WINDOW.
140
END.
141

    
142
DISPLAY cOrder cCustomer cItems cStatus WITH FRAME fMain.
143
ENABLE cLog btnStart btnClear btnAccept btnCancel btnExit WITH FRAME fMain.
144
cLog:READ-ONLY IN FRAME fMain = TRUE.
145
RUN setButtons.
146

    
147
WAIT-FOR CLOSE OF THIS-PROCEDURE OR CLOSE OF CURRENT-WINDOW.
148

    
149
/* ----------------------------------------------------------------------
150
   window helpers
151
   ---------------------------------------------------------------------- */
152
PROCEDURE setStatus:
153
   DEFINE INPUT PARAMETER pcText AS CHARACTER NO-UNDO.
154

    
155
   cStatus = pcText.
156
   DISPLAY cStatus WITH FRAME fMain.
157
END PROCEDURE.
158

    
159
/* Enable the window buttons that apply to the current pad screen. */
160
PROCEDURE setButtons:
161
   DO WITH FRAME fMain:
162
      ASSIGN
163
         /* a new signing starts when none is in progress */
164
         btnStart:SENSITIVE  = (iScreen = {&SCR-IDLE} OR iScreen = {&SCR-DONE})
165
         /* Clear and Accept act on the ink, so only while the signing box is shown */
166
         btnClear:SENSITIVE  = (iScreen = {&SCR-SIGN})
167
         btnAccept:SENSITIVE = (iScreen = {&SCR-SIGN})
168
         btnCancel:SENSITIVE = (iScreen = {&SCR-REVIEW} OR iScreen = {&SCR-SIGN}).
169
   END.
170
END PROCEDURE.
171

    
172
PROCEDURE logEvent:
173
   DEFINE INPUT PARAMETER pcText AS CHARACTER NO-UNDO.
174

    
175
   DO WITH FRAME fMain:
176
      cLog:SCREEN-VALUE = cLog:SCREEN-VALUE
177
                          + (IF cLog:SCREEN-VALUE = "" THEN "" ELSE CHR(10))
178
                          + STRING(TIME, "HH:MM:SS") + "  " + pcText.
179
      cLog:CURSOR-OFFSET = LENGTH(cLog:SCREEN-VALUE) + 1.
180
   END.
181
END PROCEDURE.
182

    
183
/* ----------------------------------------------------------------------
184
   pad drawing helpers (LCD coordinates)
185
   ---------------------------------------------------------------------- */
186
PROCEDURE lcdText:
187
   DEFINE INPUT PARAMETER piDest AS INTEGER   NO-UNDO.
188
   DEFINE INPUT PARAMETER piX    AS INTEGER   NO-UNDO.
189
   DEFINE INPUT PARAMETER piY    AS INTEGER   NO-UNDO.
190
   DEFINE INPUT PARAMETER pcFont AS CHARACTER NO-UNDO.
191
   DEFINE INPUT PARAMETER pcText AS CHARACTER NO-UNDO.
192

    
193
   /* mode 2: write opaque */
194
   hSig:LCDWriteString(piDest, 2, piX, piY, pcFont, pcText).
195
END PROCEDURE.
196

    
197
/* A pseudo-graphic button: +------+ / |  OK  | / +------+, and its hot spot. */
198
PROCEDURE lcdButton:
199
   DEFINE INPUT PARAMETER piDest  AS INTEGER   NO-UNDO.
200
   DEFINE INPUT PARAMETER piX     AS INTEGER   NO-UNDO.
201
   DEFINE INPUT PARAMETER piY     AS INTEGER   NO-UNDO.
202
   DEFINE INPUT PARAMETER piCells AS INTEGER   NO-UNDO.
203
   DEFINE INPUT PARAMETER pcLabel AS CHARACTER NO-UNDO.
204
   DEFINE INPUT PARAMETER piKey   AS INTEGER   NO-UNDO.
205

    
206
   DEFINE VARIABLE cEdge AS CHARACTER NO-UNDO.
207
   DEFINE VARIABLE iPad  AS INTEGER   NO-UNDO.
208
   DEFINE VARIABLE iW    AS INTEGER   NO-UNDO.
209
   DEFINE VARIABLE iH    AS INTEGER   NO-UNDO.
210

    
211
   cEdge = "+" + FILL("-", piCells - 2) + "+".
212
   iPad  = TRUNCATE((piCells - 2 - LENGTH(pcLabel)) / 2, 0).
213

    
214
   RUN lcdText(piDest, piX, piY, cBox, cEdge).
215
   RUN lcdText(piDest, piX, piY + iLineH, cBox,
216
               "|" + FILL(" ", iPad) + pcLabel
217
                   + FILL(" ", piCells - 2 - iPad - LENGTH(pcLabel)) + "|").
218
   RUN lcdText(piDest, piX, piY + 2 * iLineH, cBox, cEdge).
219

    
220
   /* coordinate system 1: LCD pixels; sizes in variables, the method takes INTEGER arguments */
221
   ASSIGN
222
      iW = INTEGER(piCells * dCharW)
223
      iH = 3 * iLineH + 2.
224
   hSig:KeyPadAddHotSpot(piKey, 1, piX, piY, iW, iH).
225
END PROCEDURE.
226

    
227
/* Invert a button on the LCD for a moment, so the user sees the tap. */
228
PROCEDURE flashButton:
229
   DEFINE INPUT PARAMETER piX     AS INTEGER NO-UNDO.
230
   DEFINE INPUT PARAMETER piY     AS INTEGER NO-UNDO.
231
   DEFINE INPUT PARAMETER piCells AS INTEGER NO-UNDO.
232

    
233
   DEFINE VARIABLE iW AS INTEGER NO-UNDO.
234
   DEFINE VARIABLE iH AS INTEGER NO-UNDO.
235

    
236
   ASSIGN
237
      iW = INTEGER(piCells * dCharW)
238
      iH = 3 * iLineH + 2.
239
   /* mode 1: complement */
240
   hSig:LCDRefresh(1, piX, piY, iW, iH).
241
   PAUSE 1 NO-MESSAGE.
242
END PROCEDURE.
243

    
244
PROCEDURE noInk:
245
   /* a 1 x 1 signature window: taps on the buttons leave no ink */
246
   hSig:LCDSetWindow(0, 0, 1, 1).
247
   hSig:SetSigWindow(1, 0, 0, 1, 1).
248
END PROCEDURE.
249

    
250
/* ----------------------------------------------------------------------
251
   pad screens
252
   ---------------------------------------------------------------------- */
253
PROCEDURE startSigning:
254
   hSig:SetTabletState(1).
255

    
256
   /* 11/12: T-LBK462 (240 x 64 LCD); 58: the FWD pad simulator, which emulates an LCD */
257
   lLcd = LOOKUP(STRING(hSig:TabletModelNumber()), "11,12,58") > 0.
258
   RUN logEvent("Pad connected, model " + STRING(hSig:TabletModelNumber())
259
                + (IF lLcd THEN " (LCD)" ELSE "")).
260

    
261
   hSig:ClearTablet().
262
   hImg:LOAD-IMAGE("").
263

    
264
   IF lLcd THEN
265
      RUN showReview.
266
   ELSE
267
   DO:
268
      iScreen = {&SCR-SIGN}.
269
      RUN setButtons.
270
      RUN setStatus("Waiting for the signature").
271
   END.
272
END PROCEDURE.
273

    
274
/* Screen 1: the customer checks the delivery and taps OK or Cancel. */
275
PROCEDURE showReview:
276
   hSig:KeyPadClearHotSpotList().
277
   hSig:LCDRefresh(0, 0, 0, 400, 300).
278
   RUN noInk.
279

    
280
   RUN lcdText(0, 16, 12,  cTitle, "Delivery " + cOrder).
281
   RUN lcdText(0, 16, 38,  cBox,   FILL("=", 38)).
282
   RUN lcdText(0, 16, 62,  cBold,  "Customer").
283
   RUN lcdText(0, 110, 62, cBody,  cCustomer).
284
   RUN lcdText(0, 16, 86,  cBold,  "Items").
285
   RUN lcdText(0, 110, 86, cBody,  cItems).
286
   RUN lcdText(0, 16, 124, cBody,  "Please check the delivery, then tap OK.").
287

    
288
   RUN lcdButton(0, 30,  222, 12, "Cancel", {&HS-CANCEL}).
289
   RUN lcdButton(0, 254, 222, 12, "OK",     {&HS-OK}).
290
   hSig:SetLCDCaptureMode(2).
291

    
292
   iScreen = {&SCR-REVIEW}.
293
   RUN setButtons.
294
   RUN logEvent("Pad screen 1: review the delivery").
295
   RUN setStatus("The customer reviews the delivery on the pad").
296
END PROCEDURE.
297

    
298
/* Screen 2: a framed signing box; built in the pad's background memory, so Clear can restore it. */
299
PROCEDURE showSign:
300
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
301

    
302
   hSig:KeyPadClearHotSpotList().
303
   hSig:ClearTablet().
304
   hSig:LCDRefresh(0, 0, 0, 400, 300).
305

    
306
   RUN lcdText(1, 16, 8, cBold, "Please sign inside the box").
307
   RUN lcdText(1, 16, 34, cBox, "+" + FILL("-", 36) + "+").
308
   DO iRow = 0 TO 8:
309
      IF iRow = 7 THEN
310
         RUN lcdText(1, 16, 49 + iRow * iLineH, cBox, "| X " + FILL("_", 32) + " |").
311
      ELSE
312
         RUN lcdText(1, 16, 49 + iRow * iLineH, cBox, "|" + FILL(" ", 36) + "|").
313
   END.
314
   RUN lcdText(1, 16, 49 + 9 * iLineH, cBox, "+" + FILL("-", 36) + "+").
315

    
316
   RUN lcdButton(1, 16,  222, 11, "Clear",  {&HS-CLEAR}).
317
   RUN lcdButton(1, 139, 222, 11, "Cancel", {&HS-CANCEL}).
318
   RUN lcdButton(1, 262, 222, 11, "OK",     {&HS-OK}).
319

    
320
   /* mode 2: copy the background memory to the display */
321
   hSig:LCDRefresh(2, 0, 0, 400, 300).
322

    
323
   /* ink only inside the box, and it stays on the LCD */
324
   hSig:LCDSetWindow(28, 52, 342, 128).
325
   hSig:SetSigWindow(1, 28, 52, 342, 128).
326
   hSig:SetLCDCaptureMode(2).
327

    
328
   iScreen = {&SCR-SIGN}.
329
   RUN setButtons.
330
   RUN logEvent("Pad screen 2: sign").
331
   RUN setStatus("Waiting for the signature").
332
END PROCEDURE.
333

    
334
/* Screen 3: a closing message, the pad is done. */
335
PROCEDURE showResult:
336
   DEFINE INPUT PARAMETER pcLine1 AS CHARACTER NO-UNDO.
337
   DEFINE INPUT PARAMETER pcLine2 AS CHARACTER NO-UNDO.
338

    
339
   hSig:KeyPadClearHotSpotList().
340
   RUN noInk.
341
   hSig:LCDRefresh(0, 0, 0, 400, 300).
342
   RUN lcdText(0, 16,  90,  cBox,   FILL("=", 38)).
343
   RUN lcdText(0, 120, 116, cTitle, pcLine1).
344
   RUN lcdText(0, 60,  150, cBody,  pcLine2).
345
   RUN lcdText(0, 16,  180, cBox,   FILL("=", 38)).
346
   iScreen = {&SCR-DONE}.
347
   RUN setButtons.
348
END PROCEDURE.
349

    
350
/* ----------------------------------------------------------------------
351
   pad events
352
   ---------------------------------------------------------------------- */
353
PROCEDURE checkPad:
354
   CASE iScreen:
355
      WHEN {&SCR-REVIEW} THEN
356
      DO:
357
         IF hSig:KeyPadQueryHotSpot({&HS-OK}) > 0 THEN
358
         DO:
359
            RUN logEvent("Pad: OK tapped").
360
            RUN flashButton(254, 222, 12).
361
            RUN showSign.
362
         END.
363
         ELSE IF hSig:KeyPadQueryHotSpot({&HS-CANCEL}) > 0 THEN
364
         DO:
365
            RUN logEvent("Pad: Cancel tapped").
366
            RUN flashButton(30, 222, 12).
367
            RUN cancelSigning.
368
         END.
369
      END.
370

    
371
      WHEN {&SCR-SIGN} THEN
372
      DO:
373
         IF lLcd AND hSig:KeyPadQueryHotSpot({&HS-OK}) > 0 THEN
374
         DO:
375
            RUN logEvent("Pad: OK tapped").
376
            RUN flashButton(262, 222, 11).
377
            RUN acceptSignature.
378
         END.
379
         ELSE IF lLcd AND hSig:KeyPadQueryHotSpot({&HS-CANCEL}) > 0 THEN
380
         DO:
381
            RUN logEvent("Pad: Cancel tapped").
382
            RUN flashButton(139, 222, 11).
383
            RUN cancelSigning.
384
         END.
385
         ELSE IF lLcd AND hSig:KeyPadQueryHotSpot({&HS-CLEAR}) > 0 THEN
386
         DO:
387
            RUN logEvent("Pad: Clear tapped").
388
            RUN flashButton(16, 222, 11).
389
            RUN clearInk.
390
         END.
391
         ELSE
392
         DO:
393
            iPoints = hSig:NumberOfTabletPoints().
394
            RUN logEvent("Pen up: " + STRING(iPoints) + " points").
395
            RUN setStatus(STRING(iPoints) + " points captured, tap OK on the pad").
396
         END.
397
      END.
398
   END CASE.
399
END PROCEDURE.
400

    
401
PROCEDURE clearInk:
402
   hSig:ClearSigWindow(1).
403
   hSig:ClearTablet().
404
   IF lLcd THEN
405
   DO:
406
      /* restore the screen without ink; re-adding the hot spots resets their tap counters */
407
      RUN showSign.
408
   END.
409
   RUN setStatus("Signature cleared, please sign again").
410
END PROCEDURE.
411

    
412
PROCEDURE cancelSigning:
413
   hSig:ClearTablet().
414
   RUN showResult("Cancelled", "The delivery was not signed.").
415
   RUN logEvent("Signing cancelled").
416
   RUN setStatus("Signing cancelled by the customer").
417
END PROCEDURE.
418

    
419
/* Save the signature as a monochrome BMP and show it in the window. */
420
PROCEDURE acceptSignature:
421
   DEFINE VARIABLE mImg AS MEMPTR NO-UNDO.
422

    
423
   iPoints = hSig:NumberOfTabletPoints().
424
   IF iPoints = 0 THEN
425
   DO:
426
      IF lLcd THEN
427
      DO:
428
         /* re-adding the hot spots resets their tap counters; the hint goes over the title */
429
         RUN showSign.
430
         RUN lcdText(0, 16, 8, cBold, "Please sign first, then tap OK").
431
      END.
432
      RUN logEvent("No signature yet").
433
      RUN setStatus("No signature yet: the customer must sign first").
434
      RETURN.
435
   END.
436

    
437
   hSig:ClearSigWindow(1).
438

    
439
   /* the image settings of the production screen: 571 x 200, centered and zoomed */
440
   hSig:SetImageFileFormat(2).
441
   hSig:SetImageXSize(571).
442
   hSig:SetImageYSize(200).
443
   hSig:SetImagePenWidth(11).
444
   hSig:SetJustifyMode(5).
445
   hSig:SetJustifyX(10).
446
   hSig:SetJustifyY(10).
447

    
448
   mImg = hSig:getSignatureImage("bmp", "Format1bppIndexed").
449
   cSigFile = SESSION:TEMP-DIRECTORY + "signature_" + cOrder + ".bmp".
450
   COPY-LOB FROM mImg TO FILE cSigFile NO-CONVERT.
451
   SET-SIZE(mImg) = 0.
452

    
453
   hImg:LOAD-IMAGE(cSigFile).
454

    
455
   /* the pad keeps showing the result; it is switched off when the program closes */
456
   IF lLcd THEN
457
      RUN showResult("Thank you!", "Your signature was received.").
458
   ELSE
459
   DO:
460
      iScreen = {&SCR-DONE}.
461
      RUN setButtons.
462
   END.
463

    
464
   RUN logEvent("Signature saved: " + STRING(iPoints) + " points").
465
   RUN setStatus("Signature saved as signature_" + cOrder + ".bmp ("
466
                 + STRING(iPoints) + " points)").
467
END PROCEDURE.