Project

General

Profile

spreadsheet_demo.p

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

Download (14.7 KB)

 
1
/*------------------------------------------------------------------------
2
    File        : spreadsheet_demo.p
3
    Purpose     : FWD User Meeting demo (#12087, topic 15): the SPREADSHEET
4
                  widget, the FWD replacement of the Formula One spreadsheet
5
                  OCX, running in the web client.
6

    
7
    Notes       : The 4GL code uses the OCX API unchanged (chCtrlFrame:F1Book,
8
                  mapped to the FWD widget by spreadsheet_demo.p.ext-hints):
9
                  1. Builds a quarterly sales workbook: title, a colored
10
                     header, number formats, SUM formulas for the totals and a
11
                     share column, a second sheet with notes.
12
                  2. Reacts to the user: the EndEdit event validates every
13
                     edit (a quarter cell takes numbers only) and SelChange
14
                     reads the recalculated totals back into 4GL.
15
                  3. Changes the sheet from 4GL: "+10% for Q4" updates the
16
                     numbers and the formulas follow; "Highlight Best" finds
17
                     the best product and marks its row.
18
  ----------------------------------------------------------------------*/
19

    
20
&SCOPED-DEFINE F1 chCtrlFrame:F1Book
21

    
22
/* workbook layout */
23
&SCOPED-DEFINE ROW-TITLE  1
24
&SCOPED-DEFINE ROW-HEAD   3
25
&SCOPED-DEFINE ROW-FIRST  4
26
&SCOPED-DEFINE ROW-LAST   9
27
&SCOPED-DEFINE ROW-TOTAL  10
28
&SCOPED-DEFINE COL-TOTAL  6
29
&SCOPED-DEFINE COL-SHARE  7
30

    
31
DEFINE VARIABLE CtrlFrame   AS WIDGET-HANDLE NO-UNDO.
32
DEFINE VARIABLE chCtrlFrame AS COM-HANDLE    NO-UNDO.
33
DEFINE VARIABLE iBestRow    AS INTEGER       NO-UNDO.
34
DEFINE VARIABLE cOldEntry   AS CHARACTER     NO-UNDO.  /* the active cell's content before an edit */
35
DEFINE VARIABLE iFixRow     AS INTEGER       NO-UNDO.  /* a rejected edit to undo, see SelChange */
36
DEFINE VARIABLE iFixCol     AS INTEGER       NO-UNDO.
37
DEFINE VARIABLE cFixEntry   AS CHARACTER     NO-UNDO.
38

    
39
DEFINE VARIABLE dGrand  AS DECIMAL   NO-UNDO FORMAT ">>>,>>>,>>9"  LABEL "Grand total".
40
DEFINE VARIABLE cBest   AS CHARACTER NO-UNDO FORMAT "x(20)"        LABEL "Best product".
41
DEFINE VARIABLE dBest   AS DECIMAL   NO-UNDO FORMAT ">>>,>>>,>>9"  LABEL "Its total".
42
DEFINE VARIABLE cStatus AS CHARACTER NO-UNDO FORMAT "x(90)"        LABEL "Status"
43
   INITIAL "Edit a quarter cell, or use the buttons".
44
DEFINE VARIABLE cLog    AS CHARACTER NO-UNDO
45
   VIEW-AS EDITOR SCROLLBAR-VERTICAL SIZE-PIXELS 418 BY 150.
46

    
47
DEFINE BUTTON btnReload LABEL "Reload"         SIZE-PIXELS 100 BY 26.
48
DEFINE BUTTON btnRaise  LABEL "+10% for Q4"    SIZE-PIXELS 100 BY 26.
49
DEFINE BUTTON btnBest   LABEL "Highlight Best" SIZE-PIXELS 110 BY 26.
50
DEFINE BUTTON btnExit   LABEL "Exit"           SIZE-PIXELS 80  BY 26.
51

    
52
DEFINE FRAME fMain
53
   btnReload AT X 12  Y 340
54
   btnRaise  AT X 120 Y 340
55
   btnBest   AT X 228 Y 340
56
   btnExit   AT X 808 Y 340
57
   "Read back by 4GL code:" AT X 12  Y 380
58
   dGrand    AT X 110 Y 404
59
   cBest     AT X 110 Y 428
60
   dBest     AT X 110 Y 452
61
   "Sheet events:"          AT X 470 Y 380
62
   cLog      AT X 470 Y 398 NO-LABEL
63
   cStatus   AT X 60  Y 560
64
   WITH SIDE-LABELS SIZE-PIXELS 900 BY 590 THREE-D NO-BOX.
65

    
66
ASSIGN
67
   CURRENT-WINDOW:TITLE         = "Quarterly Sales"
68
   CURRENT-WINDOW:WIDTH-PIXELS  = 900
69
   CURRENT-WINDOW:HEIGHT-PIXELS = 590.
70

    
71
ON CHOOSE OF btnReload IN FRAME fMain DO:
72
   RUN logEvent("Window: Reload").
73
   RUN buildSheet.
74
END.
75

    
76
ON CHOOSE OF btnRaise IN FRAME fMain DO:
77
   RUN logEvent("Window: +10% for Q4").
78
   RUN raiseQ4.
79
END.
80

    
81
ON CHOOSE OF btnBest IN FRAME fMain DO:
82
   RUN logEvent("Window: Highlight Best").
83
   RUN highlightBest.
84
END.
85

    
86
ON CHOOSE OF btnExit IN FRAME fMain
87
   APPLY "CLOSE" TO THIS-PROCEDURE.
88

    
89
CREATE CONTROL-FRAME CtrlFrame
90
   ASSIGN FRAME         = FRAME fMain:HANDLE
91
          X             = 12
92
          Y             = 12
93
          WIDTH-PIXELS  = 876
94
          HEIGHT-PIXELS = 316
95
          TAB-STOP      = TRUE
96
          HIDDEN        = TRUE
97
          SENSITIVE     = TRUE.
98
/* FWD takes the OCX name of the event procedures (sheet.F1Book.EndEdit) from the NAME */
99
CtrlFrame:NAME = "sheet.F1Book".
100
chCtrlFrame = CtrlFrame:COM-HANDLE.
101
{&F1}:LoadControls("", "spreadsheet").
102

    
103
VIEW FRAME fMain.
104
DISPLAY cStatus WITH FRAME fMain.
105
ENABLE btnReload btnRaise btnBest btnExit cLog WITH FRAME fMain.
106
cLog:READ-ONLY IN FRAME fMain = TRUE.
107
CtrlFrame:HIDDEN = FALSE.
108

    
109
RUN buildSheet.
110

    
111
WAIT-FOR CLOSE OF THIS-PROCEDURE.
112

    
113
IF VALID-HANDLE(CtrlFrame) THEN
114
   DELETE WIDGET CtrlFrame.
115

    
116
/* ----------------------------------------------------------------------
117
   window helpers
118
   ---------------------------------------------------------------------- */
119
PROCEDURE setStatus:
120
   DEFINE INPUT PARAMETER pcText AS CHARACTER NO-UNDO.
121

    
122
   cStatus = pcText.
123
   DISPLAY cStatus WITH FRAME fMain.
124
END PROCEDURE.
125

    
126
PROCEDURE logEvent:
127
   DEFINE INPUT PARAMETER pcText AS CHARACTER NO-UNDO.
128

    
129
   cLog = cLog + (IF cLog = "" THEN "" ELSE CHR(10)) + STRING(TIME, "HH:MM:SS") + "  " + pcText.
130
   DISPLAY cLog WITH FRAME fMain.
131
   cLog:CURSOR-OFFSET IN FRAME fMain = LENGTH(cLog) + 1.
132
END PROCEDURE.
133

    
134
FUNCTION cellName RETURNS CHARACTER (INPUT piRow AS INTEGER, INPUT piCol AS INTEGER):
135
   RETURN CHR(64 + piCol) + STRING(piRow).
136
END FUNCTION.
137

    
138
/* ----------------------------------------------------------------------
139
   the workbook
140
   ---------------------------------------------------------------------- */
141
PROCEDURE buildSheet:
142
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
143
   DEFINE VARIABLE iCol AS INTEGER NO-UNDO.
144

    
145
   /* product, Q1, Q2, Q3, Q4 */
146
   DEFINE VARIABLE cData AS CHARACTER NO-UNDO EXTENT 6 INITIAL [
147
      "Garden Tools,42500,51200,63800,38900",
148
      "Seeds and Bulbs,61800,48300,22100,30400",
149
      "Planters,18400,24600,29900,21700",
150
      "Irrigation,12900,33500,47200,15800",
151
      "Outdoor Lighting,15600,14200,19800,36500",
152
      "Garden Furniture,22300,41700,52600,17900" ].
153

    
154
   {&F1}:StartBatch().
155
   {&F1}:ClearRange(1, 1, 30, 10, 3).
156
   iBestRow = 0.
157

    
158
   /* title */
159
   {&F1}:TextRC({&ROW-TITLE}, 1) = "Quarterly Sales 2026 - Northwind Garden Supplies".
160
   RUN styleRange({&ROW-TITLE}, 1, {&ROW-TITLE}, 1, 14, RGB-VALUE(10, 129, 209), -1, "").
161

    
162
   /* header */
163
   {&F1}:TextRC({&ROW-HEAD}, 1) = "Product".
164
   DO iCol = 2 TO 5:
165
      {&F1}:TextRC({&ROW-HEAD}, iCol) = "Q" + STRING(iCol - 1).
166
   END.
167
   {&F1}:TextRC({&ROW-HEAD}, {&COL-TOTAL}) = "Total".
168
   {&F1}:TextRC({&ROW-HEAD}, {&COL-SHARE}) = "Share".
169
   RUN styleRange({&ROW-HEAD}, 1, {&ROW-HEAD}, {&COL-SHARE}, 10, RGB-VALUE(255, 255, 255),
170
                  RGB-VALUE(51, 49, 49), "").
171

    
172
   /* data rows: numbers from 4GL, totals and shares as formulas */
173
   DO iRow = {&ROW-FIRST} TO {&ROW-LAST}:
174
      {&F1}:TextRC(iRow, 1) = ENTRY(1, cData[iRow - {&ROW-FIRST} + 1]).
175
      DO iCol = 2 TO 5:
176
         {&F1}:NumberRC(iRow, iCol) = DECIMAL(ENTRY(iCol, cData[iRow - {&ROW-FIRST} + 1])).
177
      END.
178
      {&F1}:FormulaRC(iRow, {&COL-TOTAL}) = "SUM(B" + STRING(iRow) + ":E" + STRING(iRow) + ")".
179
      {&F1}:FormulaRC(iRow, {&COL-SHARE}) = "F" + STRING(iRow) + "/$F$" + STRING({&ROW-TOTAL}).
180
   END.
181

    
182
   /* total row */
183
   {&F1}:TextRC({&ROW-TOTAL}, 1) = "Total".
184
   DO iCol = 2 TO {&COL-SHARE}:
185
      {&F1}:FormulaRC({&ROW-TOTAL}, iCol) = "SUM(" + cellName({&ROW-FIRST}, iCol) + ":"
186
                                            + cellName({&ROW-LAST}, iCol) + ")".
187
   END.
188

    
189
   /* data and total rows, one style per range (see styleRange) */
190
   DO iRow = {&ROW-FIRST} TO {&ROW-LAST}:
191
      RUN styleDataRow(iRow, -1).
192
   END.
193
   RUN styleTotalRow.
194

    
195
   {&F1}:SetColWidthAuto({&ROW-HEAD}, 2, {&ROW-TOTAL}, {&COL-SHARE}, FALSE).
196
   /* the product names: 18 characters, in 1/256 of a character as in Formula One */
197
   {&F1}:ColWidth(1) = 18 * 256.
198
   {&F1}:SheetName(1) = "Sales 2026".
199
   /* every other cell is locked: totals and shares are formulas */
200
   {&F1}:EnableProtection = TRUE.
201
   /* A1 active, so the sheet shows from the title on */
202
   {&F1}:SetActiveCell(1, 1).
203
   {&F1}:EndBatch().
204

    
205
   RUN readBack.
206
   RUN logEvent("Workbook built by 4GL: " + STRING({&ROW-LAST} - {&ROW-FIRST} + 1) + " products, "
207
                + "formulas for totals and shares").
208
   RUN setStatus("Edit a quarter cell, or use the buttons").
209
END PROCEDURE.
210

    
211
/* Style a range of cells in one go: bold font of the given size and color, an optional solid fill and
212
   an optional number format.  FWD applies each call with the style of the range's top-left cell, so a
213
   range must not mix cells of different styles. */
214
PROCEDURE styleRange:
215
   DEFINE INPUT PARAMETER piRow1   AS INTEGER   NO-UNDO.
216
   DEFINE INPUT PARAMETER piCol1   AS INTEGER   NO-UNDO.
217
   DEFINE INPUT PARAMETER piRow2   AS INTEGER   NO-UNDO.
218
   DEFINE INPUT PARAMETER piCol2   AS INTEGER   NO-UNDO.
219
   DEFINE INPUT PARAMETER piSize   AS INTEGER   NO-UNDO.  /* 0: keep the font */
220
   DEFINE INPUT PARAMETER piColor  AS INTEGER   NO-UNDO.
221
   DEFINE INPUT PARAMETER piFill   AS INTEGER   NO-UNDO.  /* -1: no fill */
222
   DEFINE INPUT PARAMETER pcFormat AS CHARACTER NO-UNDO.  /* "": keep the format */
223

    
224
   {&F1}:SetSelection(piRow1, piCol1, piRow2, piCol2).
225
   IF piSize > 0 THEN
226
      {&F1}:SetFont("Arial", piSize, TRUE, FALSE, FALSE, FALSE, piColor, FALSE, FALSE).
227
   IF piFill >= 0 THEN
228
      {&F1}:SetPattern(1, piFill, piFill).
229
   ELSE
230
      {&F1}:SetPattern(0, RGB-VALUE(255, 255, 255), RGB-VALUE(255, 255, 255)).
231
   IF pcFormat <> "" THEN
232
      {&F1}:NumberFormat = pcFormat.
233
END PROCEDURE.
234

    
235
/* A data row: plain product and quarters, bold blue total and share; piFill -1 for no highlight. */
236
PROCEDURE styleDataRow:
237
   DEFINE INPUT PARAMETER piRow  AS INTEGER NO-UNDO.
238
   DEFINE INPUT PARAMETER piFill AS INTEGER NO-UNDO.
239

    
240
   RUN styleRange(piRow, 1, piRow, 1, 0, 0, piFill, "").
241
   RUN styleRange(piRow, 2, piRow, 5, 0, 0, piFill, "#,##0").
242
   /* the quarter values are the only cells the user can edit */
243
   {&F1}:SetProtection(FALSE, FALSE).
244
   RUN styleRange(piRow, {&COL-TOTAL}, piRow, {&COL-TOTAL}, 10, RGB-VALUE(10, 129, 209), piFill, "#,##0").
245
   RUN styleRange(piRow, {&COL-SHARE}, piRow, {&COL-SHARE}, 10, RGB-VALUE(10, 129, 209), piFill, "0.0%").
246
END PROCEDURE.
247

    
248
PROCEDURE styleTotalRow:
249
   DEFINE VARIABLE iGrey AS INTEGER NO-UNDO.
250

    
251
   iGrey = RGB-VALUE(219, 221, 220).
252
   RUN styleRange({&ROW-TOTAL}, 1, {&ROW-TOTAL}, 1, 10, RGB-VALUE(0, 0, 0), iGrey, "").
253
   RUN styleRange({&ROW-TOTAL}, 2, {&ROW-TOTAL}, 5, 10, RGB-VALUE(0, 0, 0), iGrey, "#,##0").
254
   RUN styleRange({&ROW-TOTAL}, {&COL-TOTAL}, {&ROW-TOTAL}, {&COL-TOTAL}, 10, RGB-VALUE(10, 129, 209),
255
                  iGrey, "#,##0").
256
   RUN styleRange({&ROW-TOTAL}, {&COL-SHARE}, {&ROW-TOTAL}, {&COL-SHARE}, 10, RGB-VALUE(10, 129, 209),
257
                  iGrey, "0.0%").
258
END PROCEDURE.
259

    
260
/* Read the recalculated results back from the sheet into 4GL. */
261
PROCEDURE readBack:
262
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
263
   DEFINE VARIABLE dVal AS DECIMAL NO-UNDO.
264

    
265
   ASSIGN
266
      dGrand = {&F1}:NumberRC({&ROW-TOTAL}, {&COL-TOTAL})
267
      dBest  = 0
268
      cBest  = "".
269
   DO iRow = {&ROW-FIRST} TO {&ROW-LAST}:
270
      dVal = {&F1}:NumberRC(iRow, {&COL-TOTAL}).
271
      IF dVal > dBest THEN
272
         ASSIGN
273
            dBest    = dVal
274
            cBest    = {&F1}:TextRC(iRow, 1)
275
            iBestRow = iRow.
276
   END.
277
   DISPLAY dGrand cBest dBest WITH FRAME fMain.
278
END PROCEDURE.
279

    
280
/* 4GL changes the data: Q4 up by 10 percent; the totals and shares follow by themselves. */
281
PROCEDURE raiseQ4:
282
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
283

    
284
   {&F1}:StartBatch().
285
   DO iRow = {&ROW-FIRST} TO {&ROW-LAST}:
286
      {&F1}:NumberRC(iRow, 5) = ROUND({&F1}:NumberRC(iRow, 5) * 1.1, 0).
287
   END.
288
   {&F1}:EndBatch().
289
   RUN readBack.
290
   RUN logEvent("Q4 raised by 10%, grand total now " + TRIM(STRING(dGrand, ">>>,>>>,>>9"))).
291
   RUN setStatus("Q4 raised by 10% in 4GL; the formulas updated the totals").
292
END PROCEDURE.
293

    
294
/* 4GL formats the sheet: the best product's row gets a highlight. */
295
PROCEDURE highlightBest:
296
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
297

    
298
   RUN readBack.
299
   {&F1}:StartBatch().
300
   /* clear an earlier highlight, then mark the best row */
301
   DO iRow = {&ROW-FIRST} TO {&ROW-LAST}:
302
      RUN styleDataRow(iRow, IF iRow = iBestRow THEN RGB-VALUE(0, 242, 255) ELSE -1).
303
   END.
304
   /* styling leaves the last styled cell selected: select the best product's name instead */
305
   {&F1}:SetSelection(iBestRow, 1, iBestRow, 1).
306
   {&F1}:EndBatch().
307
   RUN logEvent("Best product: " + cBest + ", row " + STRING(iBestRow) + " highlighted").
308
   RUN setStatus("Best product " + cBest + " (" + TRIM(STRING(dBest, ">>>,>>>,>>9")) + ") highlighted by 4GL").
309
END PROCEDURE.
310

    
311
/* ----------------------------------------------------------------------
312
   sheet events (OCX event procedures: NAME of the control frame + event)
313
   ---------------------------------------------------------------------- */
314

    
315
/* Every edit of a quarter cell: amounts only.  An invalid entry is undone: SelChange, which follows
316
   when the edit is committed, writes the cell's previous content back. */
317
PROCEDURE sheet.F1Book.EndEdit:
318
   DEFINE INPUT-OUTPUT PARAMETER p-EditString AS CHARACTER NO-UNDO.
319
   DEFINE INPUT-OUTPUT PARAMETER p-Cancel     AS INTEGER   NO-UNDO.
320

    
321
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
322
   DEFINE VARIABLE iCol AS INTEGER NO-UNDO.
323
   DEFINE VARIABLE dVal AS DECIMAL NO-UNDO.
324

    
325
   {&F1}:GetActiveCell(OUTPUT iRow, OUTPUT iCol).
326

    
327
   dVal = DECIMAL(REPLACE(p-EditString, ",", "")) NO-ERROR.
328
   IF ERROR-STATUS:ERROR OR dVal < 0 OR TRIM(p-EditString) = "" THEN
329
   DO:
330
      RUN logEvent("EndEdit " + cellName(iRow, iCol) + " [" + p-EditString + "] rejected, kept "
331
                   + cOldEntry).
332
      ASSIGN
333
         iFixRow   = iRow
334
         iFixCol   = iCol
335
         cFixEntry = cOldEntry.
336
      RETURN.
337
   END.
338

    
339
   RUN logEvent("EndEdit " + cellName(iRow, iCol) + " [" + p-EditString + "] accepted").
340
END PROCEDURE.
341

    
342
/* A new active cell (also after an edit, when Enter moves down): remember its content and read the
343
   recalculated totals back. */
344
PROCEDURE sheet.F1Book.SelChange:
345
   DEFINE VARIABLE dOld AS DECIMAL NO-UNDO.
346
   DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
347
   DEFINE VARIABLE iCol AS INTEGER NO-UNDO.
348

    
349
   /* undo a rejected edit, now that it is committed */
350
   IF iFixRow > 0 THEN
351
   DO:
352
      {&F1}:EntryRC(iFixRow, iFixCol) = cFixEntry.
353
      RUN logEvent("4GL restored " + cellName(iFixRow, iFixCol) + " to " + cFixEntry).
354
      RUN setStatus("Not an amount: " + cellName(iFixRow, iFixCol) + " keeps " + cFixEntry).
355
      iFixRow = 0.
356
      /* the totals are those from before the rejected edit: nothing to read back */
357
      {&F1}:GetActiveCell(OUTPUT iRow, OUTPUT iCol).
358
      cOldEntry = {&F1}:EntryRC(iRow, iCol).
359
      RETURN.
360
   END.
361

    
362
   /* remember the content of the new active cell, for EndEdit */
363
   {&F1}:GetActiveCell(OUTPUT iRow, OUTPUT iCol).
364
   cOldEntry = {&F1}:EntryRC(iRow, iCol).
365

    
366
   dOld = dGrand.
367
   RUN readBack.
368
   IF dGrand <> dOld THEN
369
   DO:
370
      RUN logEvent("SelChange: grand total " + TRIM(STRING(dOld, ">>>,>>>,>>9")) + " -> "
371
                   + TRIM(STRING(dGrand, ">>>,>>>,>>9"))).
372
      RUN setStatus("The sheet recalculated; 4GL read the new totals").
373
   END.
374
END PROCEDURE.