Debian Codegraph @

common/ui/checkbox.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 #include <string.h>
23
24 #include "u_mem.h"
25 #include "maths.h"
26 #include "pstypes.h"
27 #include "event.h"
28 #include "gr.h"
29 #include "ui.h"
30 #include "mouse.h"
31 #include "key.h"
32
33 namespace dcx {
34
35 #define Middle(x) ((2*(x)+1)/4)
36
37 namespace {
38
39 void ui_draw_checkbox(UI_DIALOG &dlg, UI_GADGET_CHECKBOX &checkbox)
40 {
41 #if 0 //ndef OGL
42 if ((checkbox.status==1) || (checkbox.position != checkbox.oldposition))
43 #endif
44 {
45 gr_set_current_canvas(checkbox.canvas);
46 auto &canvas = *grd_curcanv;
47 gr_set_fontcolor(canvas, dlg.keyboard_focus_gadget == &checkbox
48 ? CRED
49 : CBLACK, -1);
50
51 unsigned offset;
52 if (checkbox.position == 0)
53 {
54 ui_draw_box_out(canvas, 0, 0, checkbox.width - 1, checkbox.height - 1);
55 offset = 0;
56 } else {
57 ui_draw_box_in(canvas, 0, 0, checkbox.width - 1, checkbox.height - 1);
58 offset = 1;
59 }
60 ui_string_centered(canvas, Middle(checkbox.width) + offset, Middle(checkbox.height) + offset, checkbox.flag ? "X" : " ");
61 gr_ustring(canvas, *canvas.cv_font, checkbox.width + 4, 2, checkbox.text.get());
62 }
63 }
64
65 }
66
67 std::unique_ptr<UI_GADGET_CHECKBOX> ui_add_gadget_checkbox(UI_DIALOG &dlg, short x, short y, short w, short h, short group, const char *const text)
68 {
69 auto checkbox = ui_gadget_add<UI_GADGET_CHECKBOX>(dlg, x, y, x + w - 1, y + h - 1);
70 auto ltext = strlen(text) + 1;
71 MALLOC(checkbox->text, char[], ltext + 4);
72 memcpy(checkbox->text.get(), text, ltext);
73 checkbox->width = w;
74 checkbox->height = h;
75 checkbox->position = 0;
76 checkbox->oldposition = 0;
77 checkbox->pressed = 0;
78 checkbox->flag = 0;
79 checkbox->group = group;
80 return checkbox;
81 }
82
83 window_event_result UI_GADGET_CHECKBOX::event_handler(UI_DIALOG &dlg, const d_event &event)
84 {
85 oldposition = position;
86 pressed = 0;
87
88 if (event.type == event_type::mouse_button_down || event.type == event_type::mouse_button_up)
89 {
90 const auto OnMe = ui_mouse_on_gadget(*this);
91
92 if (B1_JUST_PRESSED && OnMe)
93 {
94 position = 1;
95 return window_event_result::handled;
96 }
97 else if (B1_JUST_RELEASED)
98 {
99 if ((position==1) && OnMe)
100 pressed = 1;
101
102 position = 0;
103 }
104 }
105
106
107 if (event.type == event_type::key_command)
108 {
109 const auto key = event_key_get(event);
110 if (dlg.keyboard_focus_gadget == this && (key == KEY_SPACEBAR || key == KEY_ENTER))
111 {
112 position = 2;
113 return window_event_result::handled;
114 }
115 }
116 else if (event.type == event_type::key_release)
117 {
118 const auto key = event_key_get(event);
119 position = 0;
120
121 if (dlg.keyboard_focus_gadget == this && (key == KEY_SPACEBAR || key == KEY_ENTER))
122 pressed = 1;
123 }
124 if (pressed == 1)
125 {
126 flag ^= 1;
127 auto rval = ui_gadget_send_event(dlg, event_type::ui_gadget_pressed, *this);
128 if (rval == window_event_result::ignored)
129 rval = window_event_result::handled;
130 return rval;
131 }
132
133 if (event.type == event_type::window_draw)
134 ui_draw_checkbox(dlg, *this);
135
136 return window_event_result::ignored;
137 }
138
139 void ui_checkbox_check(UI_GADGET_CHECKBOX * checkbox, int check)
140 {
141 check = check != 0;
142 if (checkbox->flag == check)
143 return;
144
145 checkbox->flag = check;
146 }
147
148 }