1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu installer newt page)
21 #:use-module (gnu installer utils)
22 #:use-module (gnu installer newt utils)
23 #:use-module (guix i18n)
24 #:use-module (ice-9 i18n)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 receive)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
30 #:export (draw-info-page
35 run-listbox-selection-page
37 run-checkbox-tree-page
38 run-file-textbox-page))
42 ;;; Some helpers around guile-newt to draw or run generic pages. The
43 ;;; difference between 'draw' and 'run' terms comes from newt library. A page
44 ;;; is drawn when the form it contains does not expect any user
45 ;;; interaction. In that case, it is necessary to call (newt-refresh) to force
46 ;;; the page to be displayed. When a form is 'run', it is blocked waiting for
47 ;;; any action from the user (press a button, input some text, ...).
51 (define (draw-info-page text title)
52 "Draw an informative page with the given TEXT as content. Set the title of
55 (make-reflowed-textbox -1 -1 text 40
57 (grid (make-grid 1 1))
59 (set-grid-field grid 0 0 GRID-ELEMENT-COMPONENT text-box)
60 (add-component-to-form form text-box)
61 (make-wrapped-grid-window grid title)
63 ;; This call is imperative, otherwise the form won't be displayed. See the
64 ;; explanation in the above commentary.
68 (define (draw-connecting-page service-name)
69 "Draw a page to indicate a connection in in progress."
71 (format #f (G_ "Connecting to ~a, please wait.") service-name)
72 (G_ "Connection in progress")))
74 (define* (run-input-page text title
76 (allow-empty-input? #f)
78 (input-field-width 40)
80 "Run a page to prompt user for an input. The given TEXT will be displayed
81 above the input field. The page title is set to TITLE. Unless
82 allow-empty-input? is set to #t, an error page will be displayed if the user
83 enters an empty input. INPUT-FLAGS is a bitwise-or'd set of flags for the
84 input box, such as FLAG-PASSWORD."
86 (make-reflowed-textbox -1 -1 text
89 (grid (make-grid 1 3))
90 (input-entry (make-entry -1 -1 20
92 (ok-button (make-button -1 -1 (G_ "OK")))
96 (set-entry-text input-entry default-text))
98 (set-grid-field grid 0 0 GRID-ELEMENT-COMPONENT text-box)
99 (set-grid-field grid 0 1 GRID-ELEMENT-COMPONENT input-entry
101 (set-grid-field grid 0 2 GRID-ELEMENT-COMPONENT ok-button
104 (add-components-to-form form text-box input-entry ok-button)
105 (make-wrapped-grid-window grid title)
106 (let ((error-page (lambda ()
107 (run-error-page (G_ "Please enter a non empty input.")
108 (G_ "Empty input")))))
110 (receive (exit-reason argument)
112 (let ((input (entry-value input-entry)))
113 (if (and (not allow-empty-input?)
114 (eq? exit-reason 'exit-component)
117 ;; Display the error page.
119 ;; Set the focus back to the input input field.
120 (set-current-component form input-entry)
123 (destroy-form-and-pop form)
126 (define (run-error-page text title)
127 "Run a page to inform the user of an error. The page contains the given TEXT
128 to explain the error and an \"OK\" button to acknowledge the error. The title
129 of the page is set to TITLE."
131 (make-reflowed-textbox -1 -1 text 40
132 #:flags FLAG-BORDER))
133 (grid (make-grid 1 2))
134 (ok-button (make-button -1 -1 "OK"))
137 (set-grid-field grid 0 0 GRID-ELEMENT-COMPONENT text-box)
138 (set-grid-field grid 0 1 GRID-ELEMENT-COMPONENT ok-button
141 ;; Set the background color to red to indicate something went wrong.
142 (newt-set-color COLORSET-ROOT "white" "red")
143 (add-components-to-form form text-box ok-button)
144 (make-wrapped-grid-window grid title)
146 ;; Restore the background to its original color.
147 (newt-set-color COLORSET-ROOT "white" "blue")
148 (destroy-form-and-pop form)))
150 (define* (run-confirmation-page text title
151 #:key (exit-button-procedure (const #f)))
152 "Run a page to inform the user of an error. The page contains the given TEXT
153 to explain the error and an \"OK\" button to acknowledge the error. The title
154 of the page is set to TITLE."
156 (make-reflowed-textbox -1 -1 text 40
157 #:flags FLAG-BORDER))
158 (ok-button (make-button -1 -1 (G_ "Continue")))
159 (exit-button (make-button -1 -1 (G_ "Exit")))
160 (grid (vertically-stacked-grid
161 GRID-ELEMENT-COMPONENT text-box
163 (horizontal-stacked-grid
164 GRID-ELEMENT-COMPONENT ok-button
165 GRID-ELEMENT-COMPONENT exit-button)))
168 (add-form-to-grid grid form #t)
169 (make-wrapped-grid-window grid title)
171 (receive (exit-reason argument)
179 ((components=? argument ok-button)
181 ((components=? argument exit-button)
182 (exit-button-procedure))))))
184 (destroy-form-and-pop form))))))
186 (define* (run-listbox-selection-page #:key
189 (info-textbox-width 50)
193 (listbox-default-item #f)
194 (listbox-allow-multiple? #f)
195 (sort-listbox-items? #t)
197 (skip-item-procedure?
200 (button-callback-procedure
203 (button2-callback-procedure
205 (listbox-callback-procedure
207 (hotkey-callback-procedure
209 "Run a page asking the user to select an item in a listbox. The page
210 contains, stacked vertically from the top to the bottom, an informative text
211 set to INFO-TEXT, a listbox and a button. The listbox will be filled with
212 LISTBOX-ITEMS converted to text by applying the procedure LISTBOX-ITEM->TEXT
213 on every item. The selected item from LISTBOX-ITEMS is returned. The button
214 text is set to BUTTON-TEXT and the procedure BUTTON-CALLBACK-PROCEDURE called
215 when it is pressed. The procedure LISTBOX-CALLBACK-PROCEDURE is called when an
216 item from the listbox is selected (by pressing the <ENTER> key).
218 INFO-TEXTBOX-WIDTH is the width of the textbox where INFO-TEXT will be
219 displayed. LISTBOX-HEIGHT is the height of the listbox.
221 If LISTBOX-DEFAULT-ITEM is set to the value of one of the items in
222 LISTBOX-ITEMS, it will be selected by default. Otherwise, the first element of
223 the listbox is selected.
225 If LISTBOX-ALLOW-MULTIPLE? is set to #t, multiple items from the listbox can
226 be selected (using the <SPACE> key). It that case, a list containing the
227 selected items will be returned.
229 If SORT-LISTBOX-ITEMS? is set to #t, the listbox items are sorted using
230 'string-locale<?' procedure (after being converted to text).
232 If ALLOW-DELETE? is #t, the form will return if the <DELETE> key is pressed,
233 otherwise nothing will happen.
235 Each time the listbox current item changes, call SKIP-ITEM-PROCEDURE? with the
236 current listbox item as argument. If it returns #t, skip the element and jump
237 to the next/previous one depending on the previous item, otherwise do
240 (define (fill-listbox listbox items)
241 "Append the given ITEMS to LISTBOX, once they have been converted to text
242 with LISTBOX-ITEM->TEXT. Each item appended to the LISTBOX is given a key by
243 newt. Save this key by returning an association list under the form:
245 ((NEWT-LISTBOX-KEY . ITEM) ...)
247 where NEWT-LISTBOX-KEY is the key returned by APPEND-ENTRY-TO-LISTBOX, when
248 ITEM was inserted into LISTBOX."
250 (let* ((text (listbox-item->text item))
251 (key (append-entry-to-listbox listbox text)))
255 (define (sort-listbox-items listbox-items)
256 "Return LISTBOX-ITEMS sorted using the 'string-locale<?' procedure on the text
257 corresponding to each item in the list."
258 (let* ((items (map (lambda (item)
259 (cons item (listbox-item->text item)))
262 (sort items (lambda (a b)
263 (let ((text-a (cdr a))
265 (string-locale<? text-a text-b))))))
266 (map car sorted-items)))
268 ;; Store the last selected listbox item's key.
269 (define last-listbox-key (make-parameter #f))
271 (define (previous-key keys key)
272 (let ((index (list-index (cut eq? key <>) keys)))
275 (list-ref keys (- index 1)))))
277 (define (next-key keys key)
278 (let ((index (list-index (cut eq? key <>) keys)))
280 (< index (- (length keys) 1))
281 (list-ref keys (+ index 1)))))
283 (define (set-default-item listbox listbox-keys default-item)
284 "Set the default item of LISTBOX to DEFAULT-ITEM. LISTBOX-KEYS is the
285 association list returned by the FILL-LISTBOX procedure. It is used because
286 the current listbox item has to be selected by key."
287 (for-each (match-lambda
289 (when (equal? item default-item)
290 (set-current-listbox-entry-by-key listbox key))))
293 (let* ((listbox (make-listbox
296 (logior FLAG-SCROLL FLAG-BORDER FLAG-RETURNEXIT
297 (if listbox-allow-multiple?
302 (make-reflowed-textbox -1 -1 info-text
304 #:flags FLAG-BORDER))
305 (button (make-button -1 -1 button-text))
306 (button2 (and button2-text
307 (make-button -1 -1 button2-text)))
308 (grid (vertically-stacked-grid
309 GRID-ELEMENT-COMPONENT info-textbox
310 GRID-ELEMENT-COMPONENT listbox
313 horizontal-stacked-grid
314 GRID-ELEMENT-COMPONENT button
316 (list GRID-ELEMENT-COMPONENT button2)
318 (sorted-items (if sort-listbox-items?
319 (sort-listbox-items listbox-items)
321 (keys (fill-listbox listbox sorted-items)))
323 ;; On every listbox element change, check if we need to skip it. If yes,
324 ;; depending on the 'last-listbox-key', jump forward or backward. If no,
326 (add-component-callback
329 (let* ((current-key (current-listbox-entry listbox))
330 (listbox-keys (map car keys))
331 (last-key (last-listbox-key))
332 (item (assoc-ref keys current-key))
333 (prev-key (previous-key listbox-keys current-key))
334 (next-key (next-key listbox-keys current-key)))
335 ;; Update last-listbox-key before a potential call to
336 ;; set-current-listbox-entry-by-key, because it will immediately
337 ;; cause this callback to be called for the new entry.
338 (last-listbox-key current-key)
339 (when (skip-item-procedure? item)
340 (when (eq? prev-key last-key)
342 (set-current-listbox-entry-by-key listbox next-key)
343 (set-current-listbox-entry-by-key listbox prev-key)))
344 (when (eq? next-key last-key)
346 (set-current-listbox-entry-by-key listbox prev-key)
347 (set-current-listbox-entry-by-key listbox next-key)))))))
349 (when listbox-default-item
350 (set-default-item listbox keys listbox-default-item))
353 (form-add-hotkey form KEY-DELETE))
355 (add-form-to-grid grid form #t)
356 (make-wrapped-grid-window grid title)
358 (receive (exit-reason argument)
366 ((components=? argument button)
367 (button-callback-procedure))
369 (components=? argument button2))
370 (button2-callback-procedure))
371 ((components=? argument listbox)
372 (if listbox-allow-multiple?
373 (let* ((entries (listbox-selection listbox))
374 (items (map (lambda (entry)
375 (assoc-ref keys entry))
377 (listbox-callback-procedure items))
378 (let* ((entry (current-listbox-entry listbox))
379 (item (assoc-ref keys entry)))
380 (listbox-callback-procedure item))))))
382 (let* ((entry (current-listbox-entry listbox))
383 (item (assoc-ref keys entry)))
384 (hotkey-callback-procedure argument item)))))
386 (destroy-form-and-pop form))))))
388 (define* (run-scale-page #:key
391 (info-textbox-width 50)
393 (scale-full-value 100)
395 (max-scale-update 5))
396 "Run a page with a progress bar (called 'scale' in newt). The given
397 INFO-TEXT is displayed in a textbox above the scale. The width of the textbox
398 is set to INFO-TEXTBOX-WIDTH. The width of the scale is set to
399 SCALE-WIDTH. SCALE-FULL-VALUE indicates the value that correspond to 100% of
402 The procedure SCALE-UPDATE-PROC shall return a new scale
403 value. SCALE-UPDATE-PROC will be called until the returned value is superior
404 or equal to SCALE-FULL-VALUE, but no more than MAX-SCALE-UPDATE times. An
405 error is raised if the MAX-SCALE-UPDATE limit is reached."
407 (make-reflowed-textbox -1 -1 info-text
409 #:flags FLAG-BORDER))
410 (scale (make-scale -1 -1 scale-width scale-full-value))
411 (grid (vertically-stacked-grid
412 GRID-ELEMENT-COMPONENT info-textbox
413 GRID-ELEMENT-COMPONENT scale))
416 (add-form-to-grid grid form #t)
417 (make-wrapped-grid-window grid title)
420 ;; This call is imperative, otherwise the form won't be displayed. See the
421 ;; explanation in the above commentary.
427 (let loop ((i max-scale-update)
429 (let ((value (scale-update-proc last-value)))
430 (set-scale-value scale value)
433 (unless (>= value scale-full-value)
436 (error "Max scale updates reached."))))))
438 (destroy-form-and-pop form)))))
440 (define %none-selected
443 (define* (run-checkbox-tree-page #:key
447 (selection %none-selected)
449 (info-textbox-width 50)
450 (checkbox-tree-height 10)
451 (ok-button-callback-procedure
453 (exit-button-callback-procedure
455 "Run a page allowing the user to select one or multiple items among ITEMS in
456 a checkbox list. The page contains vertically stacked from the top to the
457 bottom, an informative text set to INFO-TEXT, the checkbox list and two
458 buttons, 'Ok' and 'Exit'. The page title's is set to TITLE. ITEMS are
459 converted to text using ITEM->TEXT before being displayed in the checkbox
460 list. SELECTION is a list of Booleans of the same length as ITEMS that
461 specifies which items are initially checked.
463 INFO-TEXTBOX-WIDTH is the width of the textbox where INFO-TEXT will be
464 displayed. CHECKBOX-TREE-HEIGHT is the height of the checkbox list.
466 OK-BUTTON-CALLBACK-PROCEDURE is called when the 'Ok' button is pressed.
467 EXIT-BUTTON-CALLBACK-PROCEDURE is called when the 'Exit' button is
470 This procedure returns the list of checked items in the checkbox list among
471 ITEMS when 'Ok' is pressed."
472 (define (fill-checkbox-tree checkbox-tree items)
473 (map (lambda (item selected?)
474 (let* ((item-text (item->text item))
475 (key (add-entry-to-checkboxtree checkbox-tree item-text
483 (let* ((checkbox-tree
484 (make-checkboxtree -1 -1
488 (make-reflowed-textbox -1 -1 info-text
490 #:flags FLAG-BORDER))
491 (ok-button (make-button -1 -1 (G_ "OK")))
492 (exit-button (make-button -1 -1 (G_ "Exit")))
493 (grid (vertically-stacked-grid
494 GRID-ELEMENT-COMPONENT info-textbox
495 GRID-ELEMENT-COMPONENT checkbox-tree
497 (horizontal-stacked-grid
498 GRID-ELEMENT-COMPONENT ok-button
499 GRID-ELEMENT-COMPONENT exit-button)))
500 (keys (fill-checkbox-tree checkbox-tree items))
503 (add-form-to-grid grid form #t)
504 (make-wrapped-grid-window grid title)
506 (receive (exit-reason argument)
514 ((components=? argument ok-button)
515 (let* ((entries (current-checkbox-selection checkbox-tree))
516 (current-items (map (lambda (entry)
517 (assoc-ref keys entry))
519 (ok-button-callback-procedure)
521 ((components=? argument exit-button)
522 (exit-button-callback-procedure))))))
524 (destroy-form-and-pop form))))))
526 (define* (run-file-textbox-page #:key
530 (info-textbox-width 50)
531 (file-textbox-width 50)
532 (file-textbox-height 30)
534 (ok-button-callback-procedure
536 (exit-button-callback-procedure
539 (make-reflowed-textbox -1 -1 info-text
541 #:flags FLAG-BORDER))
542 (file-text (read-all file))
547 (logior FLAG-SCROLL FLAG-BORDER)))
548 (ok-button (make-button -1 -1 (G_ "OK")))
549 (exit-button (make-button -1 -1 (G_ "Exit")))
550 (grid (vertically-stacked-grid
551 GRID-ELEMENT-COMPONENT info-textbox
552 GRID-ELEMENT-COMPONENT file-textbox
555 horizontal-stacked-grid
556 GRID-ELEMENT-COMPONENT ok-button
558 (list GRID-ELEMENT-COMPONENT exit-button)
562 (set-textbox-text file-textbox file-text)
563 (add-form-to-grid grid form #t)
564 (make-wrapped-grid-window grid title)
566 (receive (exit-reason argument)
574 ((components=? argument ok-button)
575 (ok-button-callback-procedure))
577 (components=? argument exit-button))
578 (exit-button-callback-procedure))))))
580 (destroy-form-and-pop form))))))