Debian Codegraph @

sys/GuiTrust.cpp

raw
1 /* GuiTrust.cpp
2 *
3 * Copyright (C) 2024 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "GuiTrust.h"
20 #include "praatP.h"
21 #include "Gui.h"
22
23 GuiDialog GuiTrust_createDialog (GuiWindow optionalParent,
24 conststring32 message1, conststring32 message2, conststring32 message3, conststring32 message4, conststring32 message5,
25 conststring32 option1, conststring32 option2, conststring32 option3, conststring32 option4, conststring32 option5
26 ) {
27 constexpr int DIALOG_WIDTH = 700;
28 constexpr int BUTTON_HEIGHT = 60;
29 /*
30 Compute the height of the trust form.
31 */
32 constexpr int SHORT_LABEL_HEIGHT = 25;
33 constexpr int STEP_LABEL_HEIGHT = 15;
34 int dialogHeight = Gui_TOP_DIALOG_SPACING;
35 for (int i = 1; i <= 5; i ++) {
36 conststring32 message = ( i == 1 ? message1 : i == 2 ? message2 : i == 3 ? message3 : i == 4 ? message4 : message5 );
37 if (message) {
38 const int numberOfLines = 1 + !! str32chr (message, U'\n') + !! str32chr (message, U'“');
39 const int labelHeight = SHORT_LABEL_HEIGHT + (numberOfLines - 1) * STEP_LABEL_HEIGHT;
40 dialogHeight += labelHeight + Gui_VERTICAL_DIALOG_SPACING_SAME;
41 }
42 }
43 dialogHeight += Gui_VERTICAL_DIALOG_SPACING_DIFFERENT;
44 for (int i = 1; i <= 5; i ++) {
45 conststring32 option = ( i == 1 ? option1 : i == 2 ? option2 : i == 3 ? option3 : i == 4 ? option4 : option5 );
46 if (option)
47 dialogHeight += BUTTON_HEIGHT + Gui_VERTICAL_DIALOG_SPACING_SAME;
48 }
49 dialogHeight += 60; // the Help button
50 /*
51 Create the dialog.
52 */
53 int x = Gui_LEFT_DIALOG_SPACING, buttonWidth = DIALOG_WIDTH - x - Gui_RIGHT_DIALOG_SPACING;
54 GuiDialog me = GuiDialog_create (optionalParent, 150, 70, DIALOG_WIDTH, dialogHeight,
55 U"Praat Trust window: checking for security and safety", nullptr, nullptr, GuiDialog_Modality::BLOCKING);
56 /*
57 Add the labels.
58 */
59 int y = Gui_TOP_DIALOG_SPACING;
60 for (int i = 1; i <= 5; i ++) {
61 conststring32 message = ( i == 1 ? message1 : i == 2 ? message2 : i == 3 ? message3 : i == 4 ? message4 : message5 );
62 if (message) {
63 const int numberOfLines = 1 + !! str32chr (message, U'\n') + !! str32chr (message, U'“');
64 const int labelHeight = SHORT_LABEL_HEIGHT + (numberOfLines - 1) * STEP_LABEL_HEIGHT;
65 uint32 labelFlags = GuiLabel_CENTRE | GuiLabel_MULTILINE;
66 if (str32chr (message, U'“'))
67 labelFlags |= GuiLabel_BOLD;
68 GuiLabel_createShown (me, x, x + buttonWidth, y, y + labelHeight, message, labelFlags);
69 y += labelHeight + Gui_VERTICAL_DIALOG_SPACING_SAME;
70 }
71 }
72 /*
73 Add the buttons.
74 */
75 y += Gui_VERTICAL_DIALOG_SPACING_DIFFERENT;
76 for (int i = 1; i <= 5; i ++) {
77 conststring32 option = ( i == 1 ? option1 : i == 2 ? option2 : i == 3 ? option3 : i == 4 ? option4 : option5 );
78 if (option) {
79 GuiButton_createShown (me, x, x + buttonWidth, y, y + BUTTON_HEIGHT,
80 option, nullptr, nullptr, GuiButton_MULTILINE + ( i == 1 ? GuiButton_DEFAULT : 0 ));
81 y += BUTTON_HEIGHT + Gui_VERTICAL_DIALOG_SPACING_SAME;
82 }
83 }
84 return me;
85 }
86
87 integer GuiTrust_get (GuiWindow optionalParent, Editor optionalTrustWindowOwningEditor,
88 conststring32 message1, conststring32 message2, conststring32 message3, conststring32 message4, conststring32 message5,
89 conststring32 option1, conststring32 option2, conststring32 option3, conststring32 option4, conststring32 option5,
90 Interpreter interpreter
91 ) {
92 Melder_pre (interpreter);
93
94 GuiDialog me = GuiTrust_createDialog (optionalParent,
95 message1, message2, message3, message4, message5, option1, option2, option3, option4, option5);
96 GuiThing_show (me);
97 const integer clickedButtonId = GuiDialog_run (me);
98 GuiThing_hide (me);
99 GuiObject_destroy (my d_widget);
100 const bool sheClickedOnClose = ( clickedButtonId == 0 );
101 const bool sheClickedOnCancel = ( clickedButtonId == 1 );
102 if (sheClickedOnClose || sheClickedOnCancel) {
103 Interpreter_stop (interpreter);
104 Melder_throw (U"You interrupted the script.");
105 }
106 return clickedButtonId;
107 }
108
109 /* End of file GuiTrust.cpp */