Debian Codegraph @

common/ui/icon.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 namespace {
36
37 #define Middle(x) ((2*(x)+1)/4)
38
39 static void ui_draw_box_in1(grs_canvas &canvas, const unsigned x1, const unsigned y1, const unsigned x2, const unsigned y2)
40 {
41 const auto color{CWHITE};
42 gr_urect(canvas, x1 + 1, y1 + 1, x2 - 1, y2 - 1, color);
43 ui_draw_shad(canvas, x1, y1, x2, y2, CGREY, CBRIGHT);
44 }
45
46 void ui_draw_icon(UI_GADGET_ICON &icon)
47 {
48 int x, y;
49
50 #if 0 //ndef OGL
51 if ((icon->status==1) || (icon->position != icon->oldposition))
52 #endif
53 {
54 gr_set_current_canvas(icon.canvas);
55 auto &canvas = *grd_curcanv;
56 const auto &&[width, height] = gr_get_string_size(*canvas.cv_font, icon.text.get());
57
58 x = ((icon.width - 1) / 2) - ((width - 1) / 2);
59 y = ((icon.height - 1) / 2) - ((height - 1) / 2);
60
61 if (icon.position==1)
62 {
63 // Draw pressed
64 ui_draw_box_in(canvas, 0, 0, icon.width, icon.height);
65 x += 2; y += 2;
66 }
67 else if (icon.flag)
68 {
69 // Draw part out
70 ui_draw_box_in1(canvas, 0, 0, icon.width, icon.height);
71 x += 1; y += 1;
72 }
73 else
74 {
75 // Draw released!
76 ui_draw_box_out(canvas, 0, 0, icon.width, icon.height);
77 }
78
79 gr_set_fontcolor(canvas, CBLACK, -1);
80 gr_ustring(canvas, *canvas.cv_font, x, y, icon.text.get());
81 }
82 }
83
84 }
85
86 std::unique_ptr<UI_GADGET_ICON> ui_add_gadget_icon(UI_DIALOG &dlg, const char *const text, short x, short y, short w, short h, int k,int (*const f)())
87 {
88 auto icon = ui_gadget_add<UI_GADGET_ICON>(dlg, x, y, x + w - 1, y + h - 1);
89
90 icon->width = w;
91 icon->height = h;
92 auto ltext = strlen(text) + 1;
93 MALLOC( icon->text, char[], ltext + 1);//Hack by KRB
94 memcpy(icon->text.get(), text, ltext);
95 icon->trap_key = k;
96 icon->user_function = f;
97 icon->oldposition = 0;
98 icon->position = 0;
99 icon->pressed = 0;
100 icon->canvas->cv_font = ui_small_font.get();
101 // Call twice to get original;
102 if (f)
103 {
104 icon->flag = static_cast<int8_t>(f());
105 icon->flag = static_cast<int8_t>(f());
106 } else {
107 icon->flag = 0;
108 }
109 return icon;
110 }
111
112 window_event_result UI_GADGET_ICON::event_handler(UI_DIALOG &dlg, const d_event &event)
113 {
114 oldposition = position;
115 pressed = 0;
116
117 window_event_result rval = window_event_result::ignored;
118 if (event.type == event_type::mouse_button_down || event.type == event_type::mouse_button_up)
119 {
120 const auto OnMe = ui_mouse_on_gadget(*this);
121
122 if (B1_JUST_PRESSED && OnMe)
123 {
124 position = 1;
125 rval = window_event_result::handled;
126 }
127 else if (B1_JUST_RELEASED)
128 {
129 if ((position == 1) && OnMe)
130 pressed = 1;
131
132 position = 0;
133 }
134 }
135
136
137 if (event.type == event_type::key_command)
138 {
139 const auto key = event_key_get(event);
140 if (key == trap_key)
141 {
142 position = 1;
143 rval = window_event_result::handled;
144 }
145 }
146 else if (event.type == event_type::key_release)
147 {
148 const auto key = event_key_get(event);
149 position = 0;
150 if (key == trap_key)
151 pressed = 1;
152 }
153 if (pressed == 1)
154 {
155 flag = static_cast<int8_t>(user_function());
156 rval = ui_gadget_send_event(dlg, event_type::ui_gadget_pressed, *this);
157 if (rval == window_event_result::ignored)
158 rval = window_event_result::handled;
159 }
160
161 if (event.type == event_type::window_draw)
162 ui_draw_icon(*this);
163
164 return rval;
165 }
166
167 }