Debian Codegraph @

common/ui/message.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20
21 #include <stdio.h>
22 #include <stdarg.h>
23
24 #include "maths.h"
25 #include "pstypes.h"
26 #include "gr.h"
27 #include "event.h"
28 #include "ui.h"
29 #include "mouse.h"
30 #include "key.h"
31 #include "u_mem.h"
32
33 #include "dxxsconf.h"
34 #include "dsx-ns.h"
35 #include <array>
36 #include <memory>
37
38 namespace dcx {
39
40 // ts = total span
41 // w = width of each item
42 // n = number of items
43 // i = item number (0 based)
44 #define EVEN_DIVIDE(ts,w,n,i) ((((ts)-((w)*(n)))*((i)+1))/((n)+1))+((w)*(i))
45
46 #define BUTTON_HORZ_SPACING 20
47 #define TEXT_EXTRA_HEIGHT 5
48
49 namespace {
50
51 struct messagebox : UI_DIALOG
52 {
53 using UI_DIALOG::UI_DIALOG;
54 const ui_messagebox_tie *button = nullptr;
55 std::array<std::unique_ptr<UI_GADGET_BUTTON>, 10> button_g;
56 const char *text = nullptr;
57 int *choice = nullptr;
58 int width;
59 int text_y;
60 int line_y;
61 virtual window_event_result callback_handler(const d_event &) override;
62 };
63
64 window_event_result messagebox::callback_handler(const d_event &event)
65 {
66 if (event.type == event_type::ui_dialog_draw)
67 {
68 const grs_font * temp_font;
69
70 gr_set_current_canvas(grd_curscreen->sc_canvas);
71 auto &canvas = *grd_curcanv;
72 temp_font = grd_curscreen->sc_canvas.cv_font;
73
74 if (grd_curscreen->get_screen_width() < 640) {
75 grd_curscreen->sc_canvas.cv_font = ui_small_font.get();
76 }
77
78 ui_dialog_set_current_canvas(*this);
79 ui_string_centered(canvas, width / 2, text_y, text);
80
81 Hline(canvas, 1, width - 2, line_y + 1, CGREY);
82 Hline(canvas, 2, width - 2, line_y + 2, CBRIGHT);
83
84 grd_curscreen->sc_canvas.cv_font = temp_font;
85
86 return window_event_result::handled;
87 }
88
89 for (uint_fast32_t i=0; i < button->count(); i++ )
90 {
91 if (GADGET_PRESSED(button_g[i].get()))
92 {
93 *(choice) = i+1;
94 return window_event_result::handled;
95 }
96 }
97
98 return window_event_result::ignored;
99 }
100
101 }
102
103 int (ui_messagebox)( short xc, short yc, const char * text, const ui_messagebox_tie &Button )
104 {
105 int x, y;
106 int button_width, button_height;
107 int w, h;
108
109 int choice;
110
111 button_width = button_height = 0;
112
113 gr_set_current_canvas(grd_curscreen->sc_canvas);
114 auto &canvas = *grd_curcanv;
115 auto &cv_font = *canvas.cv_font;
116 w = grd_curscreen->get_screen_width();
117 h = grd_curscreen->get_screen_height();
118
119 for (uint_fast32_t i=0; i < Button.count(); i++ )
120 {
121 const auto &&[width, height] = ui_get_button_size(cv_font, Button.string(i));
122
123 if ( width > button_width ) button_width = width;
124 if ( height > button_height ) button_height = height;
125 }
126
127 const auto avg = cv_font.ft_w;
128 auto [text_width, text_height] = gr_get_string_size(cv_font, text);
129
130 unsigned width, height;
131 width = button_width*Button.count();
132 width += BUTTON_HORZ_SPACING*(Button.count()+1);
133 width ++;
134
135 text_width += avg*6;
136 text_width += 10;
137
138 if (text_width > width )
139 width = text_width;
140
141 height = text_height;
142 height += button_height;
143 height += 4*TEXT_EXTRA_HEIGHT;
144 height += 2; // For line in middle
145
146 {
147 const auto [mx, my, mz] = mouse_get_pos();
148 if ( xc == -1 )
149 xc = mx;
150
151 if ( yc == -1 )
152 yc = my - button_height/2;
153 }
154
155 if ( xc == -2 )
156 xc = w/2;
157
158 if ( yc == -2 )
159 yc = h/2;
160
161 x = xc - width/2;
162 y = yc - height/2;
163
164 if (x < 0 ) {
165 x = 0;
166 }
167
168 if ( (x+width-1) >= w ) {
169 x = w - width;
170 }
171
172 if (y < 0 ) {
173 y = 0;
174 }
175
176 if ( (y+height-1) >= h ) {
177 y = h - height;
178 }
179
180 const auto dlg = window_create<messagebox>(x, y, width, height, static_cast<dialog_flags>(DF_DIALOG | DF_MODAL));
181 dlg->button = &Button;
182 dlg->text = text;
183 dlg->choice = &choice;
184
185 y = TEXT_EXTRA_HEIGHT + text_height/2 - 1;
186
187 dlg->width = width;
188 dlg->text_y = y;
189
190 y = 2*TEXT_EXTRA_HEIGHT + text_height;
191
192 dlg->line_y = y;
193
194 y = height - TEXT_EXTRA_HEIGHT - button_height;
195
196 for (uint_fast32_t i=0; i < Button.count(); i++ )
197 {
198
199 x = EVEN_DIVIDE(width,button_width,Button.count(),i);
200
201 dlg->button_g[i] = ui_add_gadget_button(*dlg, x, y, button_width, button_height, Button.string(i), nullptr);
202 }
203
204 ui_gadget_calc_keys(*dlg);
205
206 //key_flush();
207
208 dlg->keyboard_focus_gadget = dlg->button_g[0].get();
209
210 choice = 0;
211
212 while(choice==0)
213 event_process();
214
215 ui_close_dialog(*dlg);
216 return choice;
217 }
218
219 }