Debian Codegraph @

common/ui/menu.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 <stdlib.h>
22
23 #include "u_mem.h"
24 #include "maths.h"
25 #include "pstypes.h"
26 #include "gr.h"
27 #include "event.h"
28 #include "mouse.h"
29 #include "ui.h"
30
31 #include "dxxsconf.h"
32 #include "dsx-ns.h"
33 #include <memory>
34
35 namespace dcx {
36
37 #define MENU_BORDER 2
38 #define MENU_VERT_SPACING 2
39
40 namespace {
41
42 struct menu : UI_DIALOG
43 {
44 explicit menu(short x, short y, short w, short h, enum dialog_flags flags, int &choice, int NumButtons) :
45 UI_DIALOG(x, y, w, h, flags),
46 button_g(std::make_unique<std::unique_ptr<UI_GADGET_BUTTON>[]>(NumButtons)),
47 choice(&choice),
48 num_buttons(NumButtons)
49 {
50 }
51 const std::unique_ptr<std::unique_ptr<UI_GADGET_BUTTON>[]> button_g;
52 int *const choice;
53 const int num_buttons;
54 virtual window_event_result callback_handler(const d_event &) override;
55 };
56
57 window_event_result menu::callback_handler(const d_event &event)
58 {
59 for (int i=0; i < num_buttons; i++ )
60 {
61 if (GADGET_PRESSED(button_g[i].get()))
62 {
63 *(choice) = i+1;
64 return window_event_result::handled;
65 }
66 }
67
68 if ((*(choice)==0) && B1_JUST_RELEASED)
69 {
70 *(choice) = -1;
71 return window_event_result::handled;
72 }
73
74 return window_event_result::ignored;
75 }
76
77 }
78
79 int MenuX( int x, int y, int NumButtons, const char *const text[] )
80 {
81 int button_width, button_height;
82 int w, h;
83 int choice;
84
85 button_width = button_height = 0;
86
87 for (int i=0; i<NumButtons; i++ )
88 {
89 const auto &&[width, height] = ui_get_button_size(*grd_curcanv->cv_font, text[i]);
90
91 if ( width > button_width ) button_width = width;
92 if ( height > button_height ) button_height = height;
93 }
94
95 unsigned width, height;
96 width = button_width + 2*(MENU_BORDER+3);
97
98 height = (button_height*NumButtons) + (MENU_VERT_SPACING*(NumButtons-1)) ;
99 height += (MENU_BORDER+3) * 2;
100
101 w = grd_curscreen->get_screen_width();
102 h = grd_curscreen->get_screen_height();
103
104 {
105 const auto [mx, my, mz] = mouse_get_pos();
106 if ( x == -1 ) x = mx - width/2;
107 if ( y == -1 ) y = my;
108 }
109
110 if (x < 0 ) {
111 x = 0;
112 }
113
114 if ( (x+width-1) >= w ) {
115 x = w - width;
116 }
117
118 if (y < 0 ) {
119 y = 0;
120 }
121
122 if ( (y+height-1) >= h ) {
123 y = h - height;
124 }
125
126 auto dlg = window_create<menu>(x, y, width, height, static_cast<dialog_flags>(DF_FILLED | DF_SAVE_BG | DF_MODAL), choice, NumButtons);
127
128 x = MENU_BORDER+3;
129 y = MENU_BORDER+3;
130
131 for (int i=0; i<NumButtons; i++ )
132 {
133 dlg->button_g[i] = ui_add_gadget_button(*dlg, x, y, button_width, button_height, text[i], nullptr);
134 y += button_height+MENU_VERT_SPACING;
135 }
136
137 choice = 0;
138
139 while(choice==0)
140 event_process();
141
142 ui_close_dialog(*dlg);
143 return choice;
144 }
145
146 }