Debian Codegraph @

Descent3/newui_core.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program 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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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 program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/newui_core.cpp $
21 * $Revision: 64 $
22 * $Date: 10/21/99 5:05p $
23 * $Author: Matt $
24 *
25 * <insert description of file here>
26 *
27 * $Log: /DescentIII/Main/newui_core.cpp $
28 *
29 * 64 10/21/99 5:05p Matt
30 * Mac merge
31 *
32 * 63 5/20/99 11:08p Samir
33 * better error checking when freeing resources, may do nothing, may fix a
34 * bug.
35 *
36 * 62 5/10/99 12:31a Samir
37 * listboxes don't quit when double clicked outside selected item.
38 *
39 * 61 5/08/99 11:20p Samir
40 * hotspots have different highlight effect.
41 *
42 * 60 5/08/99 1:06a Samir
43 * scrunch messagebox buttons closer together
44 *
45 * 59 5/06/99 1:24a Samir
46 * upped editbox maxlength to 512
47 *
48 * 58 5/05/99 10:53a Samir
49 * allow loading of 'error texture' bitmaps to determine missing data (and
50 * not to error out.)
51 *
52 * 57 5/02/99 7:28p Samir
53 * fixed a bunch of option button bugs and allow music in
54 * UIFrameWithoutInput for the menu mode.
55 *
56 * 56 5/02/99 2:14a Samir
57 * fixed a few issues with option buttons.
58 *
59 * 55 5/01/99 7:46p Samir
60 * oops. really bad if statement fixed, could fix crash bug.
61 *
62 * 54 5/01/99 1:14a Samir
63 * fixed mouse sequencing probs with selections being canceled out by
64 * UISystem.
65 *
66 * 53 4/30/99 10:53p Samir
67 * only accept double clicks inside of listbox
68 *
69 * 52 4/29/99 10:02p Samir
70 * fixed percent display for slider.
71 *
72 * 51 4/29/99 3:21a Samir
73 * reorganized main menu music to work in config, multiplayer, whereever.
74 *
75 * 50 4/29/99 2:18a Samir
76 * updated art for options style menu.
77 *
78 * 49 4/28/99 1:54a Samir
79 * visual tweaks to text
80 *
81 * 48 4/27/99 9:24a Matt
82 * Added system for drawing title bars on dialogs.
83 *
84 * 47 4/26/99 7:46p Samir
85 * newuiTiledWindow flags passed to create is an int now.
86 *
87 * 46 4/21/99 2:15p Samir
88 * table file filter fixes.
89 *
90 * 45 4/21/99 12:42p Samir
91 * change some communication betwen UIEdit and child classes.
92 *
93 * 44 4/21/99 10:58a Samir
94 * added changable text.
95 *
96 * 43 4/20/99 11:46a Samir
97 * numerous ui 'feel' fixes.
98 *
99 * 42 4/18/99 7:55p Samir
100 * fixed listbox double click and added functions to load and release
101 * pertinent newui graphic data.
102 *
103 * 41 4/15/99 5:28p Samir
104 * added advanced messagebox
105 *
106 * 40 4/05/99 5:40p Samir
107 * double clicking on an empty listbox wont return anything.
108 *
109 * 39 4/05/99 5:13p Samir
110 * fixed combo box asthetics.
111 *
112 * 38 4/05/99 10:40a Samir
113 * upped count of gadgets in titled window.
114 *
115 * 37 3/23/99 9:05p Samir
116 * took out redundant ddio_KeyFlush
117 *
118 * 36 3/22/99 1:55p Jeff
119 * fixed more crashes
120 *
121 * 35 3/22/99 12:47p Jeff
122 * oops...fixed some listbox crashes
123 *
124 * 34 3/19/99 9:21p Jeff
125 * fixed some listbox sorting issues
126 *
127 * 33 3/19/99 9:15p Jeff
128 * fixed some combo box things (Samir). Fixed sorting issues with
129 * listboxes/comboboxes if an item is added/removed before the selected
130 * index (Jeff)
131 *
132 * 32 3/17/99 11:46a Jeff
133 * added callback to set focus when a sheet is realized (Samir)
134 *
135 * 31 3/10/99 3:49p Samir
136 * indent hotspots after newline.
137 *
138 * 30 3/09/99 11:58a Samir
139 * converted buddy menu to new ui.
140 *
141 * 29 3/03/99 4:16a Samir
142 * added fix for DoEditDialog, setting focus on edit gadget by default for
143 * all sheets with editboxes (1st one.)
144 *
145 * 28 3/03/99 3:38a Samir
146 * made listbox highlight a little more tolerable.
147 *
148 * 27 3/01/99 8:51p Samir
149 * upped count of ui bitmaps.
150 *
151 * 26 3/01/99 7:29p Samir
152 * fixed an issue with slaves getting freed after their master was
153 * destroyed for sliders.
154 *
155 * 25 3/01/99 6:55p Samir
156 * fixed saturation problems with old ui text.
157 *
158 * 24 3/01/99 5:52p Samir
159 * fixed slider bars and added render distance.
160 *
161 * 23 3/01/99 4:39p Samir
162 * added mono_state and dual_state menu option buttons.
163 *
164 * 22 3/01/99 4:59a Samir
165 * added hotspots, combo boxes.
166 *
167 * 21 2/28/99 3:26a Samir
168 * editbox enhancements.
169 *
170 * 20 2/28/99 2:23a Jeff
171 * fixed sorting problem when calling GetCurrentItem
172 *
173 * 19 2/28/99 1:21a Jeff
174 * fixed cut & paste bug using slider-> instead of edit->
175 *
176 * 18 2/26/99 2:10a Samir
177 * fixed newuiTiledWindow so it realizes gadgets only when first started.
178 *
179 * 17 2/24/99 9:39p Jeff
180 * fixed listbox sorting
181 *
182 * $NoKeywords: $
183 */
184
185 #include <cstdarg>
186 #include <cstdio>
187
188 #include "newui_core.h"
189 #include "bitmap.h"
190 #include "mem.h"
191 #include "pserror.h"
192 #include "game.h"
193 #include "ddio.h"
194 #include "renderer.h"
195 #include "descent.h"
196 #include "application.h"
197 #include "stringtable.h"
198 #include "gamefont.h"
199 #include "textaux.h"
200 #include "d3music.h"
201 #include "hlsoundlib.h"
202
203 #include <string.h>
204
205 extern void ui_DoCursor();
206
207 // filenames of ui bitmaps
208 /*
209 $$TABLE_GAMEFILE "LRGButton.ogf"
210 $$TABLE_GAMEFILE "LRGButtonLit.ogf"
211 $$TABLE_GAMEFILE "LongButton.ogf"
212 $$TABLE_GAMEFILE "LongButtonLit.ogf"
213 $$TABLE_GAMEFILE "SmallButton.ogf"
214 $$TABLE_GAMEFILE "SmallButtonLit.ogf"
215 $$TABLE_GAMEFILE "SmallButtonFramed.ogf"
216 $$TABLE_GAMEFILE "SmallButtonFramedLit.ogf"
217 $$TABLE_GAMEFILE "LongButtonRed.ogf"
218 $$TABLE_GAMEFILE "LongButtonLitRed.ogf"
219 $$TABLE_GAMEFILE "SmallButton.ogf"
220 $$TABLE_GAMEFILE "SmallButtonLitRed.ogf"
221 $$TABLE_GAMEFILE "Bar.ogf"
222 $$TABLE_GAMEFILE "MinusButton.ogf"
223 $$TABLE_GAMEFILE "MinusButtonLit.ogf"
224 $$TABLE_GAMEFILE "PluseButton.ogf"
225 $$TABLE_GAMEFILE "PluseButtonLit.ogf"
226 $$TABLE_GAMEFILE "UpArrow.ogf"
227 $$TABLE_GAMEFILE "UpArrowLit.ogf"
228 $$TABLE_GAMEFILE "DownArrow.ogf"
229 $$TABLE_GAMEFILE "DownArrowLit.ogf"
230 $$TABLE_GAMEFILE "TinyButton.ogf"
231 $$TABLE_GAMEFILE "TinyButtonLit.ogf"
232 $$TABLE_GAMEFILE "SmallScreen.ogf"
233 $$TABLE_GAMEFILE "MediumScreen.ogf"
234 $$TABLE_GAMEFILE "FullScreen.ogf"
235 $$TABLE_GAMEFILE "lbTop32.ogf"
236 $$TABLE_GAMEFILE "lbTopRight32.ogf"
237 $$TABLE_GAMEFILE "lbRight32.ogf"
238 $$TABLE_GAMEFILE "lbBottomRight32.ogf"
239 $$TABLE_GAMEFILE "lbBottom32.ogf"
240 $$TABLE_GAMEFILE "lbBottomLeft32.ogf"
241 $$TABLE_GAMEFILE "lbLeft32.ogf"
242 $$TABLE_GAMEFILE "lbTopLeft32.ogf"
243 $$TABLE_GAMEFILE "winTop32.ogf"
244 $$TABLE_GAMEFILE "winTopRight64.ogf"
245 $$TABLE_GAMEFILE "winRight32.ogf"
246 $$TABLE_GAMEFILE "winBottomRight64.ogf"
247 $$TABLE_GAMEFILE "winBottom32.ogf"
248 $$TABLE_GAMEFILE "winBottomLeft64.ogf"
249 $$TABLE_GAMEFILE "winLeft32.ogf"
250 $$TABLE_GAMEFILE "winTopLeft64.ogf"
251 $$TABLE_GAMEFILE "winCenter32.ogf"
252 $$TABLE_GAMEFILE "winTitleLeft32.ogf"
253 $$TABLE_GAMEFILE "winTitleCenter32.ogf"
254 $$TABLE_GAMEFILE "winTitleRight32.ogf"
255 $$TABLE_GAMEFILE "ComboBox.ogf"
256 $$TABLE_GAMEFILE "AltArrowUp.ogf"
257 $$TABLE_GAMEFILE "AltArrowUpLit.ogf"
258 $$TABLE_GAMEFILE "AltArrowDown.ogf"
259 $$TABLE_GAMEFILE "AltArrowDownLit.ogf"
260 */
261
262 #define N_NEWUI_BMPS_TOTAL 64
263
264 #define NEWUI_LRGBTN_FILE "LRGButton.ogf"
265 #define NEWUI_LRGBTNLIT_FILE "LRGButtonLit.ogf"
266 #define NEWUI_LBTN_FILE "LongButton.ogf"
267 #define NEWUI_LBTNLIT_FILE "LongButtonLit.ogf"
268 #define NEWUI_BTN_FILE "SmallButton.ogf"
269 #define NEWUI_BTNLIT_FILE "SmallButtonLit.ogf"
270 #define NEWUI_LBTNFRAMED_FILE "LongButton.ogf"
271 #define NEWUI_LBTNLITFRAMED_FILE "LongButtonLit.ogf"
272 #define NEWUI_BTNFRAMED_FILE "SmallButtonFramed.ogf"
273 #define NEWUI_BTNLITFRAMED_FILE "SmallButtonFramedLit.ogf"
274 #define NEWUI_LCHKBTN_FILE "LongButtonRed.ogf"
275 #define NEWUI_LCHKBTNLIT_FILE "LongButtonLitRed.ogf"
276 #define NEWUI_CHKBTN_FILE "SmallButton.ogf"
277 #define NEWUI_CHKBTNLIT_FILE "SmallButtonLitRed.ogf"
278 #define NEWUI_SLIDER_FILE "Bar.ogf"
279 #define NEWUI_LARR_FILE "MinusButton.ogf"
280 #define NEWUI_LARRLIT_FILE "MinusButtonLit.ogf"
281 #define NEWUI_RARR_FILE "PluseButton.ogf"
282 #define NEWUI_RARRLIT_FILE "PluseButtonLit.ogf"
283 #define NEWUI_UARR_FILE "UPArrow.ogf"
284 #define NEWUI_UARRLIT_FILE "UPArrowLit.ogf"
285 #define NEWUI_DARR_FILE "DownArrow.ogf"
286 #define NEWUI_DARRLIT_FILE "DownArrowLit.ogf"
287 #define NEWUI_UARRCB_FILE "AltArrowUp.ogf"
288 #define NEWUI_UARRLITCB_FILE "AltArrowUpLit.ogf"
289 #define NEWUI_DARRCB_FILE "AltArrowDown.ogf"
290 #define NEWUI_DARRLITCB_FILE "AltArrowDownLit.ogf"
291 #define NEWUI_TINYBTN_FILE "TinyButton.ogf"
292 #define NEWUI_TINYBTNLIT_FILE "TinyButtonLit.ogf"
293
294 #define NEWUI_MSGBOX_FILE "SmallScreen.ogf"
295 #define NEWUI_MEDWIN_FILE "MediumScreen.ogf"
296 #define NEWUI_LRGWIN_FILE "FullScreen.ogf"
297
298 #define NEWUI_LB_N_FILE "lbTop32.ogf"
299 #define NEWUI_LB_NE_FILE "lbTopRight32.ogf"
300 #define NEWUI_LB_E_FILE "lbRight32.ogf"
301 #define NEWUI_LB_SE_FILE "lbBottomRight32.ogf"
302 #define NEWUI_LB_S_FILE "lbBottom32.ogf"
303 #define NEWUI_LB_SW_FILE "lbBottomLeft32.ogf"
304 #define NEWUI_LB_W_FILE "lbLeft32.ogf"
305 #define NEWUI_LB_NW_FILE "lbTopLeft32.ogf"
306
307 #define NEWUI_CB_FILE "ComboBox.ogf"
308
309 #define NEWUI_WIN_N_FILE "winTop32.ogf"
310 #define NEWUI_WIN_NE_FILE "winTopRight64.ogf"
311 #define NEWUI_WIN_E_FILE "winRight32.ogf"
312 #define NEWUI_WIN_SE_FILE "winBottomRight64.ogf"
313 #define NEWUI_WIN_S_FILE "winBottom32.ogf"
314 #define NEWUI_WIN_SW_FILE "winBottomLeft64.ogf"
315 #define NEWUI_WIN_W_FILE "winLeft32.ogf"
316 #define NEWUI_WIN_NW_FILE "winTopLeft64.ogf"
317 #define NEWUI_WIN_C_FILE "winCenter32.ogf"
318
319 #define NEWUI_WIN_TITLE_L_FILE "winTitleLeft32.ogf"
320 #define NEWUI_WIN_TITLE_C_FILE "winTitleCenter32.ogf"
321 #define NEWUI_WIN_TITLE_R_FILE "winTitleRight32.ogf"
322
323 // used to position gadgets in a small screen
324 #define NEWUI_MSGBOX_BUTTONS_X 242
325 #define NEWUI_MSGBOX_BUTTONS_Y 24
326 #define NEWUI_MSGBOX_BUTTONS_H 22
327 #define NEWUI_MSGBOX_BUTTONS_Y2 16
328 #define NEWUI_MSGBOX_BUTTONS_H2 20
329 #define NEWUI_MSGBOX_SHEET_X 22
330 #define NEWUI_MSGBOX_SHEET_Y 30
331
332 // used to position gadgets in a medium screen
333 #define NEWUI_MEDWIN_OPTIONS_X 314
334 #define NEWUI_MEDWIN_OPTIONS_Y 60
335 #define NEWUI_MEDWIN_OPTIONS_W 140
336 #define NEWUI_MEDWIN_OPTIONS_H 27
337 #define NEWUI_MEDWIN_SHEET_X 28
338 #define NEWUI_MEDWIN_SHEET_Y 40
339 #define NEWUI_MEDWIN_TITLE_X 90
340 #define NEWUI_MEDWIN_TITLE_Y 13
341 #define NEWUI_MEDWIN_TITLE_W 125
342
343 // used to position gadgets in a large screen
344 #define NEWUI_LRGWIN_OPTIONS_X 35
345 #define NEWUI_LRGWIN_OPTIONS_Y 415
346 #define NEWUI_LRGWIN_OPTIONS_W 132
347 #define NEWUI_LRGWIN_OPTIONS_H 27
348 #define NEWUI_LRGWIN_SHEET_X 40
349 #define NEWUI_LRGWIN_SHEET_Y 50
350 #define NEWUI_LRGWIN_TITLE_X 40
351 #define NEWUI_LRGWIN_TITLE_Y 30
352
353 // used to define tiled window dims
354 #define NEWUI_TILED_SHEET_X 40
355 #define NEWUI_TILED_SHEET_Y 50
356 #define NEWUI_TILED_TITLE_X 40
357 #define NEWUI_TILED_TITLE_Y 30
358
359 // newuiListbox constants.
360 #define LB_PIECE_WIDTH 32
361 #define LB_PIECE_HEIGHT 32
362
363 // newuiTiledWindow constants.
364 #define TW_PIECE_WIDTH 32
365 #define TW_PIECE_HEIGHT 32
366 #define TW_CORNER_WIDTH 64
367 #define TW_CORNER_HEIGHT 64
368
369 // newuiEdit constants
370 #define EDIT_GADGET_WIDTH 192
371 #define EDIT_GADGET_HEIGHT 12
372 #define EDIT_BUFLEN_MAX 1025 // DAJ utb 512
373
374 // CLASS newui resource manager
375
376 class newuiResources {
377 struct t_bmp_list {
378 union {
379 int bm_handle;
380 chunked_bitmap chunk;
381 };
382 int16_t chunked; // is it a bitmap handle or chunk
383 int16_t n_count;
384 char *filename; // filename of bitmap.
385 } m_list[N_NEWUI_BMPS_TOTAL]; // list of bitmaps.
386
387 public:
388 newuiResources();
389
390 bool Init(); // initializes list
391 void Shutdown(); // frees memory
392
393 // loads bitmap and returns the object
394 UIBitmapItem *Load(const char *filename);
395
396 // frees memory for bitmap object.
397 void Free(UIBitmapItem *bmitem);
398 };
399
400 // CLASS creates an arrow button that is sensitive to touch (when down, always select)
401
402 // CLASS a new check box: note that newuiButton and UICheckBox will share the same UIButton base
403 // since bott newuiButton and UICheckBox inherit UIButton virtually.
404
405 class newuiCheckBox : public newuiButton, public UICheckBox {
406 public:
407 newuiCheckBox();
408
409 void Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y, bool is_long = false);
410
411 protected:
412 virtual void OnDraw(); // this will use the newuiButton drawing scheme
413 virtual void OnDestroy() { newuiButton::OnDestroy(); };
414 virtual void OnFormat() { newuiButton::OnFormat(); };
415 virtual void OnKeyDown(int key) { UICheckBox::OnKeyDown(key); };
416 virtual void OnKeyUp(int key) { UICheckBox::OnKeyUp(key); };
417 virtual void OnMouseBtnDown(int btn) { UICheckBox::OnMouseBtnDown(btn); };
418 virtual void OnMouseBtnUp(int btn) { UICheckBox::OnMouseBtnUp(btn); };
419 virtual void OnLostFocus() { newuiButton::OnLostFocus(); };
420 virtual void OnGainFocus() { newuiButton::OnGainFocus(); };
421 };
422
423 class newuiRadioButton : public newuiButton, public UIRadioButton {
424 public:
425 newuiRadioButton();
426
427 void Create(UIWindow *wnd, UIRadioButton *prev_rb, int16_t id, const char *name, int16_t x, int16_t y,
428 bool is_long = false);
429
430 protected:
431 virtual void OnDraw(); // this will use the newuiButton drawing scheme
432 virtual void OnDestroy() { newuiButton::OnDestroy(); };
433 virtual void OnFormat() { newuiButton::OnFormat(); };
434 virtual void OnKeyDown(int key) { UIRadioButton::OnKeyDown(key); };
435 virtual void OnKeyUp(int key) { UIRadioButton::OnKeyUp(key); };
436 virtual void OnMouseBtnDown(int btn) { UIRadioButton::OnMouseBtnDown(btn); };
437 virtual void OnMouseBtnUp(int btn) { UIRadioButton::OnMouseBtnUp(btn); };
438 virtual void OnLostFocus() { newuiButton::OnLostFocus(); };
439 virtual void OnGainFocus() { newuiButton::OnGainFocus(); };
440 };
441
442 // CLASS a new slider.
443
444 #define SUBGADGET_LEFT 0x1
445 #define SUBGADGET_RIGHT 0x2
446
447 class newuiSlider : public UIGadget {
448 int16_t m_pos;
449 int16_t m_unitrange;
450 UIBitmapItem *m_bar_bmp;
451 UISnazzyTextItem *m_title;
452 newuiArrowButton m_minus_btn;
453 newuiArrowButton m_plus_btn;
454 tSliderSettings m_unit_settings;
455
456 public:
457 newuiSlider();
458
459 void Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y, int16_t range);
460 void Offset(int16_t offs);
461 void SetPos(int16_t pos);
462 void SetRange(int16_t range);
463 int16_t GetPos() const { return m_pos; };
464 int16_t GetRange() const { return m_unitrange; };
465 void SetUnits(tSliderSettings *settings);
466
467 protected:
468 virtual void OnFormat(); // override: called when resized or before drawing.
469 virtual void OnLostFocus(); // override: behavior when gadget loses input focus.
470 virtual void OnGainFocus(); // override: behavior when gadget gains input focus.
471 virtual void OnDraw(); // behavior when gadget is being drawn.
472 virtual void OnDestroy(); // behavior when gadget is being destroyed.
473 virtual void OnNotifySelect(UIGadget *g); // this function will handle when an arrow button was pressed
474 virtual void OnKeyDown(int btn); // override: behavior when key pressed.
475 virtual void OnKeyUp(int btn); // override: behavior when key released.
476 virtual void OnAttachToWindow(); // when gadget is added to a window (AddGadget is called)
477 virtual void OnDetachFromWindow(); // when gadget is detached from window
478 };
479
480 // CLASS newuiEditBox
481
482 class newuiEditBox : public UIEdit {
483 public:
484 newuiEditBox();
485
486 void Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y, int16_t w, int16_t flags);
487 void EnableOnQuickEscapeBehavior(bool do_it);
488
489 protected:
490 virtual void OnDraw();
491 virtual void OnDestroy();
492 virtual void OnKeyDown(int key);
493
494 private:
495 UISnazzyTextItem *m_title;
496 bool m_quick_escape_enable;
497 };
498
499 static inline UISnazzyTextItem *MonitorSmallText(const char *text) {
500 return new UISnazzyTextItem(0, MONITOR9_NEWUI_FONT, text, NEWUI_MONITORFONT_COLOR);
501 }
502
503 static inline UISnazzyTextItem *MonitorLargeText(const char *text) {
504 return new UISnazzyTextItem(0, MONITOR15_NEWUI_FONT, text, NEWUI_MONITORFONT_COLOR);
505 }
506
507 static inline UISnazzyTextItem *GadgetSmallText(const char *text) {
508 return new UISnazzyTextItem(0, GADGET9_NEWUI_FONT, text, NEWUI_GADGETFONT_COLOR);
509 }
510
511 static inline UISnazzyTextItem *GadgetLargeText(const char *text) {
512 return new UISnazzyTextItem(0, GADGET15_NEWUI_FONT, text, NEWUI_GADGETFONT_COLOR);
513 }
514
515 //////////////////////////////////////////////////////////////////////////////
516 // DATA
517 static const char *Preloaded_bitmap_list[] = {
518 NEWUI_LRGBTN_FILE, // common buttons
519 NEWUI_LRGBTNLIT_FILE, NEWUI_LBTN_FILE, NEWUI_LBTNLIT_FILE, NEWUI_BTN_FILE,
520 NEWUI_BTNLIT_FILE, NEWUI_LCHKBTN_FILE, NEWUI_LCHKBTNLIT_FILE, NEWUI_CHKBTN_FILE,
521 NEWUI_CHKBTNLIT_FILE, NEWUI_TINYBTN_FILE, NEWUI_TINYBTNLIT_FILE,
522 NEWUI_MSGBOX_FILE, // window frames
523 NEWUI_MEDWIN_FILE, NEWUI_LRGWIN_FILE,
524 NEWUI_LB_N_FILE, // listbox frame
525 NEWUI_LB_NE_FILE, NEWUI_LB_E_FILE, NEWUI_LB_SE_FILE, NEWUI_LB_S_FILE,
526 NEWUI_LB_SW_FILE, NEWUI_LB_W_FILE, NEWUI_LB_NW_FILE,
527 NEWUI_WIN_N_FILE, // tiled window frames
528 NEWUI_WIN_NE_FILE, NEWUI_WIN_E_FILE, NEWUI_WIN_SE_FILE, NEWUI_WIN_S_FILE,
529 NEWUI_WIN_SW_FILE, NEWUI_WIN_W_FILE, NEWUI_WIN_NW_FILE, NEWUI_WIN_C_FILE,
530 NEWUI_WIN_TITLE_L_FILE, NEWUI_WIN_TITLE_C_FILE, NEWUI_WIN_TITLE_R_FILE,
531 };
532
533 #define N_UI_PRELOADED_BITMAPS (sizeof(Preloaded_bitmap_list) / sizeof(const char *))
534
535 int UI_frame_result = -1;
536 static newuiResources Newui_resources;
537 static void (*UI_callback)() = NULL;
538 static UIBitmapItem *Preloaded_bitmaps[N_UI_PRELOADED_BITMAPS] = {
539 NULL,
540 };
541
542 #ifndef MULTI_H
543 extern bool Multi_bail_ui_menu;
544 #endif
545
546 void SimpleUICallback();
547
548 //////////////////////////////////////////////////////////////////////////////
549 // initializes the core system for the newui
550
551 void newuiCore_Init() { Newui_resources.Init(); }
552
553 // initializes the core system for the newui
554 void newuiCore_Close() { Newui_resources.Shutdown(); }
555
556 // C interface to load and free bitmap resources
557 UIBitmapItem *newui_LoadBitmap(const char *filename) { return Newui_resources.Load(filename); }
558
559 // C interface to load and free bitmap resources
560 void newui_FreeBitmap(UIBitmapItem *bmitem) { Newui_resources.Free(bmitem); }
561
562 // touches all newui bitmaps, do before any newui menus open
563 void newuiCore_PageInBitmaps() {
564 int i;
565
566 if (Preloaded_bitmaps[0]) {
567 Int3(); // we've already paged in stuff!
568 return;
569 }
570
571 for (i = 0; i < N_UI_PRELOADED_BITMAPS; i++) {
572 Preloaded_bitmaps[i] = newui_LoadBitmap(Preloaded_bitmap_list[i]);
573 }
574 }
575
576 // call when NO newui menus open
577 void newuiCore_ReleaseBitmaps() {
578 int i;
579
580 if (!Preloaded_bitmaps[0]) {
581 Int3(); // we've already freed paged in stuff!
582 return;
583 }
584
585 for (i = 0; i < N_UI_PRELOADED_BITMAPS; i++) {
586 newui_FreeBitmap(Preloaded_bitmaps[i]);
587 Preloaded_bitmaps[i] = NULL;
588 }
589 }
590
591 // does a UI loop
592 //
593
594 int DoUI() {
595 // this should poll UI_frame_result.
596 UI_frame_result = -1;
597 ui_ShowCursor();
598 ui_Flush();
599
600 while (UI_frame_result == -1) {
601 Descent->defer();
602 DoUIFrame();
603 rend_Flip();
604 }
605
606 ui_HideCursor();
607 ui_Flush();
608
609 return UI_frame_result;
610 }
611
612 // does one frame of ui.
613 void DoUIFrame() {
614 if (Multi_bail_ui_menu) {
615 UI_frame_result = NEWUIRES_FORCEQUIT;
616 } else {
617 if (UI_callback)
618 (*UI_callback)();
619
620 if (GetFunctionMode() == MENU_MODE) {
621 tMusicSeqInfo music_info;
622
623 Sound_system.BeginSoundFrame(false);
624
625 music_info.frametime = UIFrameTime;
626 music_info.player_dead = false;
627 music_info.started_level = false;
628 D3MusicDoFrame(&music_info);
629
630 Sound_system.EndSoundFrame();
631 }
632
633 UI_frame_result = ui_DoFrame();
634 }
635
636 if (UI_input.printscreen) {
637 UI_input.printscreen = false;
638 DoScreenshot();
639 }
640 }
641
642 // does one frame of ui.
643 void DoUIFrameWithoutInput() {
644 if (Multi_bail_ui_menu) {
645 UI_frame_result = NEWUIRES_FORCEQUIT;
646 } else {
647 if (UI_callback)
648 (*UI_callback)();
649
650 if (GetFunctionMode() == MENU_MODE) {
651 tMusicSeqInfo music_info;
652
653 Sound_system.BeginSoundFrame(false);
654
655 music_info.frametime = UIFrameTime;
656 music_info.player_dead = false;
657 music_info.started_level = false;
658 D3MusicDoFrame(&music_info);
659
660 Sound_system.EndSoundFrame();
661 }
662
663 UI_frame_result = ui_DoFrame(false);
664 }
665 }
666
667 int GetUIFrameResult() { return UI_frame_result; }
668
669 // sets the callback for background rendering of desktop for UI
670 void SetUICallback(void (*fn)()) {
671 if (fn == DEFAULT_UICALLBACK)
672 fn = SimpleUICallback;
673 UI_callback = fn;
674 }
675
676 void (*GetUICallback())() { return UI_callback; }
677
678 void SimpleUICallback() {
679 StartFrame(0, 0, Max_window_w, Max_window_h);
680 rend_ClearScreen(GR_BLACK);
681 EndFrame();
682 }
683
684 //////////////////////////////////////////////////////////////////////////////
685 // CLASS newui resource manager
686
687 newuiResources::newuiResources() {}
688
689 bool newuiResources::Init() {
690 int i;
691 for (i = 0; i < N_NEWUI_BMPS_TOTAL; i++) {
692 m_list[i].n_count = 0;
693 }
694
695 return true;
696 }
697
698 void newuiResources::Shutdown() {
699 int i;
700
701 for (i = 0; i < N_NEWUI_BMPS_TOTAL; i++) {
702 if (m_list[i].n_count > 0) {
703 if (m_list[i].chunked) {
704 bm_DestroyChunkedBitmap(&m_list[i].chunk);
705 } else {
706 bm_FreeBitmap(m_list[i].bm_handle);
707 }
708 }
709 m_list[i].n_count = 0;
710 }
711 }
712
713 // loads bitmap and returns the object
714 UIBitmapItem *newuiResources::Load(const char *filename) {
715 UIBitmapItem *bmitem;
716 int i, free_slot = -1;
717
718 // do search for bitmap.
719 for (i = 0; i < N_NEWUI_BMPS_TOTAL; i++) {
720 if (m_list[i].n_count == 0) {
721 if (free_slot == -1) {
722 free_slot = i;
723 }
724 } else {
725 if (stricmp(filename, m_list[i].filename) == 0) {
726 break;
727 }
728 }
729 }
730 if (i == N_NEWUI_BMPS_TOTAL) {
731 // perform load.
732 int bm, chw, chh;
733
734 if (free_slot == -1) {
735 // reached limit.
736 Int3();
737 return NULL;
738 }
739 i = free_slot;
740
741 bm = bm_AllocLoadFileBitmap(IGNORE_TABLE(filename), 0);
742 if (bm < 0) {
743 Error("NewUI: Failed to load bitmap %s", filename);
744 }
745
746 chw = bm_w(bm, 0);
747 chh = bm_h(bm, 0);
748
749 if ((chw == 32 && chh == 32) || (chw == 64 && chh == 64) || (chw == 128 && chh == 128)) {
750 m_list[i].bm_handle = bm;
751 m_list[i].chunked = 0;
752 } else {
753 // chunk it.
754 bm_CreateChunkedBitmap(bm, &m_list[i].chunk);
755 bm_FreeBitmap(bm);
756 m_list[i].chunked = 1;
757 }
758
759 m_list[i].n_count = 0;
760 m_list[i].filename = mem_strdup(filename);
761 }
762
763 m_list[i].n_count++;
764
765 if (m_list[i].chunked) {
766 bmitem = new UIBitmapItem(&m_list[i].chunk);
767 } else {
768 bmitem = new UIBitmapItem(m_list[i].bm_handle);
769 }
770
771 return bmitem;
772 }
773
774 // frees memory for bitmap object.
775 void newuiResources::Free(UIBitmapItem *bmitem) {
776 int i;
777
778 if (!bmitem) {
779 Int3(); // Get samir.
780 return;
781 }
782
783 for (i = 0; i < N_NEWUI_BMPS_TOTAL; i++) {
784 if (m_list[i].n_count > 0) {
785 if (m_list[i].chunked && bmitem->is_chunked()) {
786 if (bmitem->get_chunked_bitmap()->bm_array == m_list[i].chunk.bm_array) {
787 break;
788 }
789 } else if (!m_list[i].chunked && !bmitem->is_chunked()) {
790 if (bmitem->get_bitmap() == m_list[i].bm_handle) {
791 break;
792 }
793 }
794 }
795 }
796
797 if (i >= N_NEWUI_BMPS_TOTAL) {
798 Int3(); // maybe we should free this, well this really should never happen but if it does
799 return; // I can't be completely sure this is a valid pointer, so rather than risking a crash I'll have a leak.
800 }
801
802 // ASSERT(i < N_NEWUI_BMPS_TOTAL);
803
804 // decrement and free.
805 delete bmitem;
806
807 m_list[i].n_count--;
808 if (m_list[i].n_count == 0) {
809 if (m_list[i].chunked) {
810 bm_DestroyChunkedBitmap(&m_list[i].chunk);
811 } else {
812 bm_FreeBitmap(m_list[i].bm_handle);
813 }
814 mem_free(m_list[i].filename);
815 }
816 }
817
818 //////////////////////////////////////////////////////////////////////////////
819 // CLASS a menu with options and sheets
820
821 newuiMenu::newuiMenu() {
822 m_nsheets = 0;
823 m_bkg = 0;
824 m_cursheet = NULL;
825 m_newoptionid = -1;
826 m_align = NEWUI_ALIGN_VERT;
827 }
828
829 // creates a menu
830 void newuiMenu::Create() {
831 m_nsheets = 0;
832
833 if (!m_bkg) {
834 m_bkg = Newui_resources.Load(NEWUI_MEDWIN_FILE);
835 }
836
837 m_sheetx = NEWUI_MEDWIN_SHEET_X;
838 m_sheety = NEWUI_MEDWIN_SHEET_Y;
839 m_optionsx = NEWUI_MEDWIN_OPTIONS_X;
840 m_optionsy = NEWUI_MEDWIN_OPTIONS_Y;
841 m_optionsw = NEWUI_MEDWIN_OPTIONS_W;
842 m_optionsh = NEWUI_MEDWIN_OPTIONS_H;
843 m_titlex = NEWUI_MEDWIN_TITLE_X;
844 m_titley = NEWUI_MEDWIN_TITLE_Y;
845 m_align = NEWUI_ALIGN_VERT;
846
847 m_uiframe_cb = GetUICallback();
848 m_activate_sheet_cb = NULL;
849 m_option_focus_cb = NULL;
850
851 UIWindow::Create((Max_window_w - m_bkg->width()) / 2, (Max_window_h - m_bkg->height()) / 2, m_bkg->width(),
852 m_bkg->height());
853 m_newoptionid = -1;
854 m_cursheetidx = -1;
855 }
856
857 // sets current option to display.
858 void newuiMenu::SetCurrentOption(int16_t id) { m_newoptionid = id; }
859
860 // returns current option
861 int16_t newuiMenu::GetCurrentOption() const {
862 if (m_cursheetidx != -1) {
863 return m_sheetbtn[m_cursheetidx].GetID();
864 }
865
866 return -1;
867 }
868
869 newuiSheet *newuiMenu::GetCurrentSheet() const { return m_cursheet; }
870
871 // when a new option is ready. at this point the system sets the focus on this option
872 void newuiMenu::SetOnOptionFocusCB(void (*fn)(newuiMenu *, int16_t, void *), void *data) {
873 m_option_focus_cb = fn;
874 m_option_focus_cb_data = data;
875 }
876
877 // processes a menu
878 int newuiMenu::DoUI() {
879 int res, i;
880
881 if (m_newoptionid != -1) {
882 m_cursheetidx = -1;
883 for (i = 0; i < m_nsheets; i++) {
884 if (m_sheetbtn[i].GetID() == (int)m_newoptionid) {
885 if (m_cursheet) {
886 m_cursheet->Unrealize();
887 m_cursheet = NULL;
888 }
889
890 if (m_hassheet[i]) {
891 if (m_activate_sheet_cb) {
892 (*m_activate_sheet_cb)(this, m_sheetbtn[i].GetID(), m_newoptionid, m_activate_sheet_cb_data);
893 }
894
895 m_cursheet = &m_sheets[i];
896 m_cursheet->Realize();
897 }
898
899 m_cursheetidx = i;
900
901 SetFocusOnGadget(&m_sheetbtn[i]);
902
903 if (m_option_focus_cb) {
904 (*m_option_focus_cb)(this, m_sheetbtn[i].GetID(), m_option_focus_cb_data);
905 }
906
907 break;
908 }
909 }
910 if (i == m_nsheets) { // error!
911 Int3();
912 if (m_cursheet) {
913 m_cursheet->Unrealize();
914 m_cursheet = NULL;
915 }
916 }
917 m_newoptionid = -1;
918 }
919
920 // refreshes gadgets on current sheet.
921 if (m_cursheet) {
922 m_cursheet->UpdateChanges();
923 }
924
925 // do ui and check if a new option was selected
926 res = ::DoUI();
927
928 for (i = 0; i < m_nsheets; i++) {
929 if (i != m_cursheetidx) {
930 if (res == m_sheetbtn[i].GetID() && m_hassheet[i]) {
931 m_newoptionid = (int16_t)res;
932 break;
933 }
934 }
935 }
936
937 // updates return values
938 if (m_cursheet) {
939 m_cursheet->UpdateReturnValues();
940 }
941
942 return res;
943 }
944
945 // when a new sheet is realized, this function will be called.
946 // passed in: the menu object, the old option and the new option respectively
947 void newuiMenu::SetOnOptionSwitchCB(void (*fn)(newuiMenu *, int16_t, int16_t, void *), void *data) {
948 m_activate_sheet_cb = fn;
949 m_activate_sheet_cb_data = data;
950 }
951
952 // equivalent of SetUICallback, called before gadgets are drawn
953 void newuiMenu::SetOnUIFrameCB(void (*fn)()) {
954 // m_uiframe_cb = GetUICallback();
955 // SetUICallback(fn);
956 }
957
958 // adds an option to a menu, returns a newuiSheet object to add user interface to.
959 void newuiMenu::AddSimpleOption(int16_t id, const char *title, int yoff) {
960 newuiMenu::AddOption(id, title, 0, false, yoff);
961 }
962
963 newuiSheet *newuiMenu::AddOption(int16_t id, const char *title, int size, bool has_sheet, int yoff) {
964 newuiMenuOptionButton *last_btn = NULL;
965
966 if (m_nsheets == N_NEWUI_SHEETS) {
967 Int3();
968 return NULL;
969 }
970
971 if (has_sheet) {
972 m_sheets[m_nsheets].Create(this, title, size, m_sheetx, m_sheety);
973 }
974
975 if (m_nsheets > 0) {
976 last_btn = &m_sheetbtn[m_nsheets - 1];
977 }
978
979 switch (m_align) {
980 case NEWUI_ALIGN_VERT:
981 m_sheetbtn[m_nsheets].Create(this, last_btn, id, title, m_optionsx, m_optionsy + m_nsheets * m_optionsh + yoff,
982 has_sheet);
983 break;
984 case NEWUI_ALIGN_HORIZ:
985 m_sheetbtn[m_nsheets].Create(this, last_btn, id, title, m_optionsx + m_nsheets * m_optionsw + yoff, m_optionsy,
986 has_sheet);
987 break;
988 default:
989 Int3();
990 }
991
992 m_hassheet[m_nsheets] = has_sheet;
993 m_newoptionid = id;
994 m_nsheets++;
995
996 if (!has_sheet) {
997 return NULL;
998 }
999
1000 return &m_sheets[m_nsheets - 1];
1001 }
1002
1003 // set master sheet,button locations
1004 void newuiMenu::SetPlacements(int x, int y, int options_x, int options_y, int options_w, int options_h, int title_x,
1005 int title_y, tAlignment align) {
1006 m_sheetx = x;
1007 m_sheety = y;
1008 m_optionsx = options_x;
1009 m_optionsy = options_y;
1010 m_optionsw = options_w;
1011 m_optionsh = options_h;
1012 m_titlex = title_x;
1013 m_titley = title_y;
1014 m_align = align;
1015 }
1016
1017 // overridable draws the window background before gadgets
1018 void newuiMenu::OnDraw() {
1019 // draw background
1020 m_bkg->draw(0, 0);
1021
1022 // draw title
1023 if (m_cursheetidx != -1 && m_hassheet[m_cursheetidx]) {
1024 UITextItem *title = m_sheetbtn[m_cursheetidx].GetTitle();
1025 UITextItem m_texttitle(MONITOR15_NEWUI_FONT, title->GetBuffer(), NEWUI_MONITORFONT_COLOR);
1026
1027 m_texttitle.draw(m_titlex - 8 + (NEWUI_MEDWIN_TITLE_W - m_texttitle.width()) / 2, m_titley);
1028 }
1029
1030 // draws gadgets.
1031 UIWindow::OnDraw();
1032 }
1033
1034 // overridable: called in Destroy
1035 void newuiMenu::OnDestroy() {
1036 int i;
1037
1038 if (m_bkg) {
1039 Newui_resources.Free(m_bkg);
1040 m_bkg = NULL;
1041 }
1042
1043 for (i = 0; i < m_nsheets; i++) {
1044 if (m_hassheet[i]) {
1045 m_sheets[i].Destroy();
1046 m_sheetbtn[i].Destroy();
1047 m_hassheet[i] = false;
1048 }
1049 }
1050
1051 m_nsheets = 0;
1052
1053 SetUICallback(m_uiframe_cb);
1054
1055 UIWindow::OnDestroy();
1056 }
1057
1058 //////////////////////////////////////////////////////////////////////////////
1059 // CLASS, large (fullscreen) menu
1060
1061 // creates a menu
1062 void newuiLargeMenu::Create() {
1063 m_bkg = Newui_resources.Load(NEWUI_LRGWIN_FILE);
1064 newuiMenu::Create();
1065 newuiMenu::SetPlacements(NEWUI_LRGWIN_SHEET_X, NEWUI_LRGWIN_SHEET_Y, NEWUI_LRGWIN_OPTIONS_X, NEWUI_LRGWIN_OPTIONS_Y,
1066 NEWUI_LRGWIN_OPTIONS_W, NEWUI_LRGWIN_OPTIONS_H, NEWUI_LRGWIN_TITLE_X, NEWUI_LRGWIN_TITLE_Y,
1067 NEWUI_ALIGN_HORIZ);
1068 }
1069
1070 // adds an option to a menu, returns a newuiSheet object to add user interface to.
1071 newuiSheet *newuiLargeMenu::AddOption(int16_t id, const char *title, int size) {
1072 return newuiMenu::AddOption(id, title, size);
1073 }
1074
1075 //////////////////////////////////////////////////////////////////////////////
1076 // CLASS, small message box
1077 #define MBTN_OK 0
1078 #define MBTN_CANCEL 1
1079
1080 newuiMessageBox::newuiMessageBox() { m_bkg = NULL; }
1081
1082 void newuiMessageBox::Create(const char *title, int type) {
1083 ASSERT(title);
1084 m_bkg = Newui_resources.Load(NEWUI_MSGBOX_FILE);
1085
1086 UIWindow::Create((Max_window_w - m_bkg->width()) / 2, (Max_window_h - m_bkg->height()) / 2, m_bkg->width(),
1087 m_bkg->height());
1088
1089 m_sheet.Create(this, title, NEWUIMENU_SMALL, NEWUI_MSGBOX_SHEET_X, NEWUI_MSGBOX_SHEET_Y);
1090
1091 if (type == MSGBOX_OK) {
1092 m_btn[MBTN_OK].Create(this, UID_OK, TXT_OK, NEWUI_MSGBOX_BUTTONS_X, NEWUI_MSGBOX_BUTTONS_Y, NEWUI_BTNF_FRAMED);
1093 AddAcceleratorKey(KEY_ESC, UID_OK);
1094 } else if (type == MSGBOX_OKCANCEL) {
1095 m_btn[MBTN_OK].Create(this, UID_OK, TXT_OK, NEWUI_MSGBOX_BUTTONS_X, NEWUI_MSGBOX_BUTTONS_Y, NEWUI_BTNF_FRAMED);
1096 m_btn[MBTN_CANCEL].Create(this, UID_CANCEL, TXT_CANCEL, NEWUI_MSGBOX_BUTTONS_X,
1097 NEWUI_MSGBOX_BUTTONS_Y + NEWUI_MSGBOX_BUTTONS_H, NEWUI_BTNF_FRAMED);
1098 } else if (type == MSGBOX_YESNO) {
1099 m_btn[MBTN_OK].Create(this, UID_OK, TXT_YES, NEWUI_MSGBOX_BUTTONS_X, NEWUI_MSGBOX_BUTTONS_Y, NEWUI_BTNF_FRAMED);
1100 m_btn[MBTN_CANCEL].Create(this, UID_CANCEL, TXT_NO, NEWUI_MSGBOX_BUTTONS_X,
1101 NEWUI_MSGBOX_BUTTONS_Y + NEWUI_MSGBOX_BUTTONS_H, NEWUI_BTNF_FRAMED);
1102 m_btn[MBTN_OK].SetHotkey(KEY_Y);
1103 m_btn[MBTN_CANCEL].SetHotkey(KEY_N);
1104 }
1105 }
1106
1107 void newuiMessageBox::AddButton(const char *title, int slot, int key) {
1108 ASSERT(slot >= 0 && slot < NEWUI_MSGBOX_MAXBTNS);
1109
1110 if (m_btn[slot].IsCreated()) {
1111 m_btn[slot].Destroy();
1112 }
1113
1114 m_btn[slot].Create(this, slot, title, NEWUI_MSGBOX_BUTTONS_X,
1115 NEWUI_MSGBOX_BUTTONS_Y2 + NEWUI_MSGBOX_BUTTONS_H2 * slot, NEWUI_BTNF_FRAMED);
1116 if (key) {
1117 m_btn[slot].SetHotkey(key);
1118 }
1119 }
1120
1121 // grab the message box's sheet.
1122 newuiSheet *newuiMessageBox::GetSheet() { return &m_sheet; }
1123
1124 // do user interface, return UID_OK or UID_CANCEL or UID_??? (UID_OK and UID_CANCEL are predefined)
1125 int newuiMessageBox::DoUI() {
1126 int res;
1127 m_sheet.Realize();
1128 res = ::DoUI();
1129 m_sheet.Unrealize();
1130
1131 return res;
1132 }
1133
1134 void newuiMessageBox::OnDestroy() {
1135 int i;
1136
1137 if (m_bkg) {
1138 Newui_resources.Free(m_bkg);
1139 m_bkg = NULL;
1140 }
1141
1142 m_sheet.Destroy();
1143
1144 for (i = 0; i < NEWUI_MSGBOX_MAXBTNS; i++) {
1145 if (m_btn[i].IsCreated()) {
1146 m_btn[i].Destroy();
1147 }
1148 }
1149
1150 UIWindow::OnDestroy();
1151 }
1152
1153 // overridable draws the window background before gadgets
1154 void newuiMessageBox::OnDraw() {
1155 // draw background
1156 m_bkg->draw(0, 0);
1157
1158 UITextItem title(MONITOR9_NEWUI_FONT, m_sheet.GetTitle(), NEWUI_MONITORFONT_COLOR);
1159 title.draw(NEWUI_MSGBOX_SHEET_X, NEWUI_MSGBOX_SHEET_Y - 16);
1160
1161 // draws gadgets.
1162 UIWindow::OnDraw();
1163 }
1164
1165 //////////////////////////////////////////////////////////////////////////////
1166 // CLASS contains gadgets
1167
1168 // constants used in newuiSheet for gadget types. hidden from user.
1169
1170 #define GADGET_GROUP 0 // repositions column, row alignment
1171 #define GADGET_HGROUP 1
1172 #define GADGET_STATIC_TXT 2 // static text
1173 #define GADGET_BUTTON 3 // small mono state button
1174 #define GADGET_CHECKBOX 4 // small two state button
1175 #define GADGET_RADIO 5 // small two state radio
1176 #define GADGET_LBUTTON 6 // large equivalents
1177 #define GADGET_LCHECKBOX 7
1178 #define GADGET_LRADIO 8
1179 #define GADGET_SLIDER 9 // slider
1180 #define GADGET_EDITBOX 10 // edit control
1181 #define GADGET_EDITBOXPASS 11 // edit control
1182 #define GADGET_EDITBOXNUM 12 // edit control
1183 #define GADGET_LISTBOX 13 // listbox
1184 #define GADGET_STATIC_BMP 14 // static bitmap
1185 #define GADGET_COMBOBOX 15 // combo box
1186 #define GADGET_HOTSPOT 16 // hotspot
1187 #define GADGET_CHANGEABLE_TXT 17 // changeable text
1188
1189 newuiSheet::newuiSheet() {
1190 m_gadgetlist = NULL;
1191 m_gadgetlimit = 0;
1192 m_ngadgets = 0;
1193 m_realized = false;
1194 }
1195
1196 void newuiSheet::Create(UIWindow *menu, const char *title, int n_items, int sx, int sy) {
1197 int i;
1198
1199 if (title) {
1200 m_title = mem_strdup(title);
1201 } else {
1202 m_title = NULL;
1203 }
1204 m_gadgetlimit = n_items;
1205 m_ngadgets = 0;
1206 if (n_items) {
1207 m_gadgetlist = (newuiSheet::t_gadget_desc *)mem_malloc(n_items * sizeof(newuiSheet::t_gadget_desc));
1208 } else {
1209 m_gadgetlist = NULL;
1210 }
1211
1212 m_parent = menu;
1213 m_sx = sx;
1214 m_sy = sy;
1215 m_realized = false;
1216 m_initial_focus_id = -1;
1217
1218 for (i = 0; i < m_gadgetlimit; i++) {
1219 m_gadgetlist[i].obj.gadget = NULL;
1220 m_gadgetlist[i].title = NULL;
1221 m_gadgetlist[i].id = -1;
1222 m_gadgetlist[i].internal = NULL;
1223 }
1224 }
1225
1226 // deallocates memory
1227 void newuiSheet::Destroy() {
1228 Unrealize();
1229
1230 newuiSheet::Reset();
1231
1232 if (m_gadgetlist)
1233 mem_free(m_gadgetlist);
1234 if (m_title)
1235 mem_free(m_title);
1236 m_title = NULL;
1237 m_gadgetlist = NULL;
1238 m_ngadgets = 0;
1239 }
1240
1241 void newuiSheet::Reset() {
1242 int i;
1243
1244 ASSERT(!m_realized);
1245
1246 for (i = 0; i < m_ngadgets; i++) {
1247 if (m_gadgetlist[i].title) {
1248 mem_free(m_gadgetlist[i].title);
1249 }
1250 if (m_gadgetlist[i].internal) {
1251 if (m_gadgetlist[i].type == GADGET_LISTBOX) {
1252 newuiListBox *lb = (newuiListBox *)m_gadgetlist[i].internal;
1253 lb->Destroy();
1254 delete lb;
1255 } else if (m_gadgetlist[i].type == GADGET_SLIDER) {
1256 mem_free(m_gadgetlist[i].internal);
1257 } else if (m_gadgetlist[i].type == GADGET_EDITBOX || m_gadgetlist[i].type == GADGET_EDITBOXNUM ||
1258 m_gadgetlist[i].type == GADGET_EDITBOXPASS) {
1259 mem_free(m_gadgetlist[i].internal);
1260 } else if (m_gadgetlist[i].type == GADGET_COMBOBOX) {
1261 newuiComboBox *cb = (newuiComboBox *)m_gadgetlist[i].internal;
1262 cb->Destroy();
1263 delete cb;
1264 } else if (m_gadgetlist[i].type == GADGET_CHANGEABLE_TXT) {
1265 mem_free(m_gadgetlist[i].parm.p);
1266 } else if (m_gadgetlist[i].type != GADGET_HOTSPOT) {
1267 Int3(); // get samir.
1268 }
1269 }
1270 }
1271
1272 m_ngadgets = 0;
1273 m_initial_focus_id = -1;
1274 }
1275
1276 // set focus on this gadget specified by id upon realization.
1277 void newuiSheet::SetInitialFocusedGadget(int16_t id) { m_initial_focus_id = id; }
1278
1279 // call this to initialize gadgets specified above in parent window
1280 void newuiSheet::Realize() {
1281 int i, first_radio_index = -1, last_toggle_index = -1, gx = m_sx, gy = m_sy;
1282 newuiRadioButton *prev_radio = NULL;
1283 bool horizontal_align = false, hotspot_group = false, toggles_group = false;
1284 newuiEditBox *focus_edit = NULL;
1285 UIGadget *focus_gadget = NULL;
1286
1287 if (m_realized)
1288 return;
1289
1290 for (i = 0; i < m_ngadgets; i++) {
1291 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1292 newuiButton *btn;
1293 newuiCheckBox *cbox;
1294 newuiRadioButton *radio;
1295 newuiSlider *slider;
1296 newuiListBox *lb;
1297 newuiEditBox *edit;
1298 newuiComboBox *cb;
1299 newuiHotspot *hot;
1300 UIText *text;
1301 UIStatic *bmp;
1302 int16_t flags;
1303 bool bval;
1304
1305 switch (desc->type) {
1306 case GADGET_HGROUP:
1307 case GADGET_GROUP:
1308 horizontal_align = (desc->type == GADGET_GROUP) ? false : true;
1309 gx = desc->parm.s[0] + m_sx;
1310 gy = desc->parm.s[1] + m_sy;
1311 if (desc->title) {
1312 text = new UIText;
1313 UITextItem item{MONITOR9_NEWUI_FONT, desc->title, NEWUI_MONITORFONT_COLOR};
1314 text->Create(m_parent, &item, gx, gy);
1315 desc->obj.gadget = text;
1316
1317 // determine pixel offset to first control if we stuffed the offset into desc->id (-1 = default)
1318 if (desc->id != -1) {
1319 gy += text->H() + desc->id;
1320 } else {
1321 gy += text->H();
1322 }
1323 } else {
1324 desc->obj.gadget = NULL;
1325 }
1326
1327 if (toggles_group && last_toggle_index != -1) {
1328 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[last_toggle_index];
1329 desc->obj.chbox->SetFlag(UIF_GROUP_END);
1330 }
1331
1332 prev_radio = NULL;
1333 hotspot_group = false;
1334 toggles_group = false;
1335 first_radio_index = -1;
1336 last_toggle_index = -1;
1337 break;
1338
1339 case GADGET_STATIC_TXT: {
1340 text = new UIText;
1341 UITextItem item{MONITOR9_NEWUI_FONT, desc->title, NEWUI_MONITORFONT_COLOR};
1342 text->Create(m_parent, &item, gx, gy);
1343 if (horizontal_align)
1344 gx += text->W() + 2;
1345 else
1346 gy += text->H();
1347 desc->obj.gadget = text;
1348 break;
1349 }
1350 case GADGET_CHANGEABLE_TXT: {
1351 text = new UIText;
1352 UITextItem item{MONITOR9_NEWUI_FONT, (const char *)desc->parm.p, NEWUI_MONITORFONT_COLOR};
1353 text->Create(m_parent, &item, gx, gy);
1354 if (horizontal_align)
1355 gx += text->W() + 2;
1356 else
1357 gy += text->H();
1358 desc->obj.text = text;
1359 break;
1360 }
1361
1362 case GADGET_STATIC_BMP: {
1363 bmp = new UIStatic;
1364 UIBitmapItem item{desc->parm.i};
1365 bmp->Create(m_parent, &item, gx, gy, 10, 10);
1366 if (horizontal_align)
1367 gx += bmp->W() + 2;
1368 else
1369 gy += bmp->H();
1370 desc->obj.gadget = bmp;
1371 break;
1372 }
1373 case GADGET_BUTTON:
1374 btn = new newuiButton;
1375 btn->Create(m_parent, desc->id, desc->title, gx, gy, desc->parm.i);
1376 if (horizontal_align)
1377 gx += btn->W() + 2;
1378 else
1379 gy += btn->H();
1380 desc->obj.button = btn;
1381 break;
1382
1383 case GADGET_CHECKBOX:
1384 cbox = new newuiCheckBox;
1385 cbox->Create(m_parent, desc->id, desc->title, gx, gy, false);
1386 if (!toggles_group) {
1387 toggles_group = true;
1388 cbox->SetFlag(UIF_GROUP_START);
1389 }
1390 last_toggle_index = i;
1391 if (desc->parm.b)
1392 cbox->SetCheck(true);
1393 if (horizontal_align)
1394 gx += cbox->W() + 2;
1395 else
1396 gy += cbox->H();
1397 desc->obj.chbox = cbox;
1398 break;
1399
1400 case GADGET_RADIO:
1401 if (first_radio_index == -1)
1402 first_radio_index = i;
1403 radio = new newuiRadioButton;
1404 radio->Create(m_parent, prev_radio, desc->id, desc->title, gx, gy, false);
1405 if (m_gadgetlist[first_radio_index].parm.i == (i - first_radio_index)) {
1406 radio->Activate();
1407 }
1408 if (horizontal_align)
1409 gx += radio->W() + 2;
1410 else
1411 gy += radio->H();
1412 desc->obj.radio = radio;
1413 prev_radio = radio;
1414 break;
1415
1416 case GADGET_LBUTTON:
1417 btn = new newuiButton;
1418 btn->Create(m_parent, desc->id, desc->title, gx, gy, desc->parm.i | NEWUI_BTNF_LONG);
1419 if (horizontal_align)
1420 gx += btn->W() + 2;
1421 else
1422 gy += btn->H();
1423 desc->obj.button = btn;
1424 break;
1425
1426 case GADGET_LCHECKBOX:
1427 cbox = new newuiCheckBox;
1428 cbox->Create(m_parent, desc->id, desc->title, gx, gy, true);
1429 if (!toggles_group) {
1430 toggles_group = true;
1431 cbox->SetFlag(UIF_GROUP_START);
1432 }
1433 last_toggle_index = i;
1434 if (desc->parm.b)
1435 cbox->SetCheck(true);
1436 if (horizontal_align)
1437 gx += cbox->W() + 2;
1438 else
1439 gy += cbox->H();
1440 desc->obj.chbox = cbox;
1441 break;
1442
1443 case GADGET_LRADIO:
1444 if (first_radio_index == -1)
1445 first_radio_index = i;
1446 radio = new newuiRadioButton;
1447 radio->Create(m_parent, prev_radio, desc->id, desc->title, gx, gy, true);
1448 if (m_gadgetlist[first_radio_index].parm.i == (i - first_radio_index)) {
1449 radio->Activate();
1450 }
1451 if (horizontal_align)
1452 gx += radio->W() + 2;
1453 else
1454 gy += radio->H();
1455 desc->obj.radio = radio;
1456 prev_radio = radio;
1457 break;
1458
1459 case GADGET_HOTSPOT:
1460 hot = new newuiHotspot;
1461 bval = desc->internal ? true : false;
1462 hot->Create(m_parent, desc->id, desc->title, gx, gy, desc->parm.s[0], desc->parm.s[1],
1463 (bval && !hotspot_group) ? UIF_GROUP_START
1464 : (bval && hotspot_group) ? UIF_GROUP_END
1465 : 0);
1466
1467 if (bval && !hotspot_group) {
1468 hotspot_group = true;
1469 } else if (bval && hotspot_group) {
1470 hotspot_group = false;
1471 }
1472
1473 if (horizontal_align)
1474 gx += hot->W() + 2;
1475 else
1476 gy += hot->H();
1477 desc->obj.hot = hot;
1478 break;
1479
1480 case GADGET_SLIDER:
1481 slider = new newuiSlider;
1482 slider->Create(m_parent, desc->id, desc->title, gx, gy, desc->parm.s[1]);
1483 slider->SetPos(desc->parm.s[0]);
1484 if (desc->internal) {
1485 slider->SetUnits((tSliderSettings *)desc->internal);
1486 }
1487 if (horizontal_align)
1488 gx += slider->W() + 4;
1489 else
1490 gy += slider->H() + 2;
1491 desc->obj.slider = slider;
1492 break;
1493
1494 case GADGET_EDITBOX:
1495 case GADGET_EDITBOXPASS:
1496 case GADGET_EDITBOXNUM:
1497 ASSERT(desc->internal);
1498 edit = new newuiEditBox;
1499 flags = (desc->type == GADGET_EDITBOXPASS) ? UIED_PASSWORD : (desc->type == GADGET_EDITBOXNUM) ? UIED_NUMBERS : 0;
1500
1501 // awful hack, but no more room to store passed in parameters. note that we should never have an edit
1502 // box with a width greater than 4096 pixels in the lifetime of this system.
1503 edit->Create(m_parent, desc->id, desc->title, gx, gy, (desc->parm.s[0] & 0xfff), flags);
1504 edit->SetBufferLen(desc->parm.s[1]);
1505 edit->SetText((char *)desc->internal);
1506
1507 if (desc->parm.s[0] & 0x1000) {
1508 edit->EnableOnQuickEscapeBehavior(true);
1509 }
1510
1511 if (!focus_edit) {
1512 focus_edit = edit;
1513 }
1514
1515 if (horizontal_align)
1516 gx += edit->W() + 4;
1517 else
1518 gy += edit->H() + 2;
1519 desc->obj.edit = edit;
1520 break;
1521
1522 case GADGET_LISTBOX:
1523 lb = (newuiListBox *)desc->internal;
1524 ASSERT(lb);
1525 lb->Move(gx, gy, lb->W(), lb->H());
1526 m_parent->AddGadget(lb);
1527 if (horizontal_align)
1528 gx += lb->W() + 4;
1529 else
1530 gy += lb->H() + 2;
1531 desc->obj.lb = lb;
1532 break;
1533
1534 case GADGET_COMBOBOX:
1535 cb = (newuiComboBox *)desc->internal;
1536 ASSERT(cb);
1537 cb->Move(gx, gy, cb->W(), cb->H());
1538 m_parent->AddGadget(cb);
1539 if (horizontal_align)
1540 gx += cb->W() + 4;
1541 else
1542 gy += cb->H() + 2;
1543 desc->obj.cb = cb;
1544 break;
1545
1546 default:
1547 Int3();
1548 }
1549 desc->changed = false;
1550
1551 if (desc->obj.gadget) {
1552 if (desc->obj.gadget->GetID() > -1 && desc->obj.gadget->GetID() == m_initial_focus_id) {
1553 focus_gadget = desc->obj.gadget;
1554 }
1555 }
1556 }
1557
1558 if (toggles_group) {
1559 if (last_toggle_index != -1) {
1560 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[last_toggle_index];
1561 desc->obj.chbox->SetFlag(UIF_GROUP_END);
1562 }
1563 toggles_group = false;
1564 }
1565
1566 if (focus_gadget) {
1567 m_parent->SetFocusOnGadget(focus_gadget);
1568 } else if (focus_edit) {
1569 m_parent->SetFocusOnGadget(focus_edit);
1570 focus_edit->Activate();
1571 }
1572
1573 m_realized = true;
1574 }
1575
1576 // call this to release gadgets specified above in parent window
1577 void newuiSheet::Unrealize() {
1578 int i, first_radio_index = -1;
1579
1580 if (!m_realized)
1581 return;
1582
1583 for (i = 0; i < m_ngadgets; i++) {
1584 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1585
1586 if (desc->obj.gadget) {
1587 switch (desc->type) {
1588 case GADGET_HGROUP:
1589 case GADGET_GROUP:
1590 first_radio_index = -1;
1591 if (desc->title && desc->obj.gadget) {
1592 desc->obj.gadget->Destroy();
1593 delete desc->obj.gadget;
1594 }
1595 break;
1596 case GADGET_LCHECKBOX:
1597 case GADGET_CHECKBOX:
1598 desc->parm.b = desc->obj.chbox->IsChecked();
1599 desc->obj.chbox->Destroy();
1600 delete desc->obj.chbox;
1601 break;
1602 case GADGET_RADIO:
1603 case GADGET_LRADIO:
1604 if (first_radio_index == -1)
1605 first_radio_index = i;
1606 if (desc->obj.radio->IsActivated()) {
1607 m_gadgetlist[first_radio_index].parm.i = i - first_radio_index;
1608 }
1609 desc->obj.radio->Destroy();
1610 delete desc->obj.radio;
1611 break;
1612 case GADGET_SLIDER:
1613 desc->parm.s[0] = desc->obj.slider->GetPos();
1614 desc->obj.slider->Destroy();
1615 delete desc->obj.slider;
1616 break;
1617
1618 case GADGET_HOTSPOT:
1619 desc->obj.hot->Destroy();
1620 delete desc->obj.hot;
1621 break;
1622
1623 case GADGET_BUTTON:
1624 case GADGET_LBUTTON:
1625 desc->obj.button->Destroy();
1626 delete desc->obj.button;
1627 break;
1628
1629 case GADGET_LISTBOX:
1630 m_parent->RemoveGadget(desc->obj.lb);
1631 break;
1632
1633 case GADGET_COMBOBOX:
1634 m_parent->RemoveGadget(desc->obj.cb);
1635 break;
1636
1637 case GADGET_EDITBOX:
1638 case GADGET_EDITBOXPASS:
1639 case GADGET_EDITBOXNUM:
1640 desc->obj.edit->GetText((char *)desc->internal, (desc->parm.s[1]) + 1);
1641 desc->obj.edit->Destroy();
1642 delete desc->obj.edit;
1643 break;
1644
1645 case GADGET_CHANGEABLE_TXT:
1646 desc->obj.text->Destroy();
1647 delete desc->obj.gadget;
1648 break;
1649
1650 default:
1651 desc->obj.gadget->Destroy();
1652 delete desc->obj.gadget;
1653 }
1654
1655 desc->obj.gadget = NULL;
1656 }
1657 }
1658
1659 m_realized = false;
1660 }
1661
1662 // refreshes gadget states with values passed to the pointers returned by the below functions.
1663 void newuiSheet::UpdateChanges() {
1664 int i, first_radio_index = 0;
1665
1666 ASSERT(m_realized);
1667
1668 for (i = 0; i < m_ngadgets; i++) {
1669 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1670 bool bval;
1671 int16_t sval;
1672 char sbuftest[EDIT_BUFLEN_MAX];
1673
1674 switch (desc->type) {
1675 case GADGET_HGROUP:
1676 case GADGET_GROUP:
1677 first_radio_index = -1;
1678 break;
1679
1680 case GADGET_LCHECKBOX:
1681 case GADGET_CHECKBOX:
1682 bval = desc->obj.chbox->IsChecked();
1683 if (desc->parm.b != bval) {
1684 desc->obj.chbox->SetCheck(desc->parm.b);
1685 }
1686 break;
1687
1688 case GADGET_LRADIO:
1689 case GADGET_RADIO:
1690 // check if new index is same as current index.
1691 if (first_radio_index == -1)
1692 first_radio_index = i;
1693 if (m_gadgetlist[first_radio_index].parm.i == (i - first_radio_index)) {
1694 // possible request for change of radio button state here.
1695 bval = desc->obj.radio->IsActivated();
1696 if (!bval) {
1697 // wasn't active before so lets activate it now.
1698 desc->obj.radio->Activate();
1699 }
1700 }
1701 break;
1702
1703 case GADGET_SLIDER:
1704 sval = desc->obj.slider->GetPos();
1705 if (desc->parm.s[0] != sval) {
1706 desc->obj.slider->SetPos(desc->parm.s[0]);
1707 desc->parm.s[0] = desc->obj.slider->GetPos();
1708 }
1709 break;
1710
1711 case GADGET_EDITBOX:
1712 case GADGET_EDITBOXNUM:
1713 case GADGET_EDITBOXPASS:
1714 desc->obj.edit->GetText(sbuftest, EDIT_BUFLEN_MAX);
1715 if (strcmp(sbuftest, (char *)desc->internal) != 0) {
1716 desc->obj.edit->SetText((const char *) desc->internal);
1717 }
1718 break;
1719
1720 case GADGET_CHANGEABLE_TXT: {
1721 UITextItem item{MONITOR9_NEWUI_FONT, (const char *)desc->parm.p, NEWUI_MONITORFONT_COLOR};
1722 desc->obj.text->SetTitle(&item);
1723 break;
1724 }
1725 }
1726 desc->changed = false;
1727 }
1728 }
1729
1730 // refreshes return values of gadgets, so they are accessible by the pointers returned by below functions
1731 void newuiSheet::UpdateReturnValues() {
1732 int i, first_radio_index = -1;
1733
1734 ASSERT(m_realized);
1735
1736 for (i = 0; i < m_ngadgets; i++) {
1737 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1738 bool bval;
1739 int16_t sval;
1740 char sbuftest[EDIT_BUFLEN_MAX];
1741
1742 if (desc->obj.gadget) {
1743 switch (desc->type) {
1744 case GADGET_HGROUP:
1745 case GADGET_GROUP:
1746 first_radio_index = -1;
1747 break;
1748 case GADGET_LCHECKBOX:
1749 case GADGET_CHECKBOX:
1750 bval = desc->obj.chbox->IsChecked();
1751 if (desc->parm.b != bval) {
1752 desc->parm.b = bval;
1753 desc->changed = true;
1754 }
1755 break;
1756 case GADGET_RADIO:
1757 case GADGET_LRADIO:
1758 if (first_radio_index == -1)
1759 first_radio_index = i;
1760 bval = desc->obj.radio->IsActivated();
1761 if (bval && m_gadgetlist[first_radio_index].parm.i != (i - first_radio_index)) {
1762 m_gadgetlist[first_radio_index].parm.i = (i - first_radio_index);
1763 m_gadgetlist[first_radio_index].changed = true;
1764 }
1765 break;
1766 case GADGET_SLIDER:
1767 sval = desc->obj.slider->GetPos();
1768 if (desc->parm.s[0] != sval) {
1769 desc->parm.s[0] = sval;
1770 desc->changed = true;
1771 }
1772 break;
1773
1774 case GADGET_EDITBOX:
1775 case GADGET_EDITBOXNUM:
1776 case GADGET_EDITBOXPASS:
1777 desc->obj.edit->GetText(sbuftest, EDIT_BUFLEN_MAX);
1778 if (strcmp(sbuftest, (char *)desc->internal) != 0) {
1779 strcpy((char *)desc->internal, sbuftest);
1780 desc->changed = true;
1781 }
1782 break;
1783
1784 default:
1785 desc->changed = false;
1786 }
1787 }
1788 }
1789 }
1790
1791 // call these functions to determine if a pointer's value has changed after call to UpdateReturnValues (internal)
1792 bool newuiSheet::HasChanged(bool *bptr) {
1793 int i;
1794
1795 ASSERT(m_realized);
1796
1797 for (i = 0; i < m_ngadgets; i++) {
1798 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1799 if (desc->obj.gadget) {
1800 switch (desc->type) {
1801 case GADGET_LCHECKBOX:
1802 case GADGET_CHECKBOX:
1803 if ((&desc->parm.b) == bptr) {
1804 return desc->changed;
1805 }
1806 break;
1807 }
1808 }
1809 }
1810
1811 return false;
1812 }
1813
1814 bool newuiSheet::HasChanged(int *iptr) {
1815 int i;
1816
1817 ASSERT(m_realized);
1818
1819 for (i = 0; i < m_ngadgets; i++) {
1820 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1821 if (desc->obj.gadget) {
1822 switch (desc->type) {
1823 case GADGET_RADIO:
1824 case GADGET_LRADIO:
1825 if ((&desc->parm.i) == iptr) {
1826 return desc->changed;
1827 }
1828 break;
1829 }
1830 }
1831 }
1832
1833 return false;
1834 }
1835
1836 bool newuiSheet::HasChanged(int16_t *sptr) {
1837 int i;
1838
1839 ASSERT(m_realized);
1840
1841 for (i = 0; i < m_ngadgets; i++) {
1842 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1843 if (desc->obj.gadget) {
1844 switch (desc->type) {
1845 case GADGET_SLIDER:
1846 if ((&desc->parm.s[0]) == sptr) {
1847 return desc->changed;
1848 }
1849 break;
1850 }
1851 }
1852 }
1853
1854 return false;
1855 }
1856
1857 bool newuiSheet::HasChanged(char *cptr) {
1858 int i;
1859
1860 ASSERT(m_realized);
1861
1862 for (i = 0; i < m_ngadgets; i++) {
1863 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1864 if (desc->obj.gadget) {
1865 switch (desc->type) {
1866 case GADGET_EDITBOX:
1867 case GADGET_EDITBOXNUM:
1868 case GADGET_EDITBOXPASS:
1869 if (desc->internal == cptr) {
1870 return desc->changed;
1871 }
1872 break;
1873 }
1874 }
1875 }
1876
1877 return false;
1878 }
1879
1880 UIGadget *newuiSheet::GetGadget(int16_t id) {
1881 int i;
1882
1883 for (i = 0; i < m_ngadgets; i++) {
1884 newuiSheet::t_gadget_desc *desc = &m_gadgetlist[i];
1885
1886 if (desc->obj.gadget && id == (int16_t)desc->obj.gadget->GetID()) {
1887 switch (desc->type) {
1888 case GADGET_STATIC_TXT:
1889 return desc->obj.gadget;
1890 case GADGET_STATIC_BMP:
1891 return desc->obj.gadget;
1892 case GADGET_BUTTON:
1893 return desc->obj.button;
1894 case GADGET_HOTSPOT:
1895 return desc->obj.hot;
1896 case GADGET_CHECKBOX:
1897 return desc->obj.chbox;
1898 case GADGET_RADIO:
1899 return desc->obj.radio;
1900 case GADGET_LBUTTON:
1901 return desc->obj.button;
1902 case GADGET_LCHECKBOX:
1903 return desc->obj.chbox;
1904 case GADGET_LRADIO:
1905 return desc->obj.radio;
1906 case GADGET_SLIDER:
1907 return desc->obj.slider;
1908 case GADGET_EDITBOX:
1909 case GADGET_EDITBOXPASS:
1910 case GADGET_EDITBOXNUM:
1911 return desc->obj.edit;
1912 case GADGET_LISTBOX:
1913 return desc->obj.lb;
1914 case GADGET_COMBOBOX:
1915 return desc->obj.cb;
1916 case GADGET_CHANGEABLE_TXT:
1917 return desc->obj.text;
1918
1919 default:
1920 return desc->obj.gadget;
1921 }
1922 }
1923 }
1924
1925 Int3();
1926 return NULL;
1927 }
1928
1929 newuiSheet::t_gadget_desc *newuiSheet::AddGadget(int16_t id, int8_t type, const char *title) {
1930 int i = m_ngadgets;
1931
1932 ASSERT(!m_realized);
1933 ASSERT(m_gadgetlimit > m_ngadgets);
1934 if (m_gadgetlimit <= m_ngadgets) {
1935 Error("Internal error in newuiSheet::AddGadget (ngadgets=%d, id=%d, type=%d, title=%s)\n", m_ngadgets, (int)id,
1936 (int)type, title ? title : "NULL");
1937 return NULL;
1938 }
1939
1940 m_gadgetlist[i].id = id;
1941 m_gadgetlist[i].type = type;
1942 m_gadgetlist[i].title = title ? mem_strdup(title) : NULL;
1943 m_gadgetlist[i].obj.gadget = NULL;
1944 m_gadgetlist[i].parm.i = 0;
1945 m_gadgetlist[i].changed = false;
1946 m_gadgetlist[i].internal = NULL;
1947
1948 m_ngadgets++;
1949
1950 return &m_gadgetlist[i];
1951 }
1952
1953 // creates gadget list.
1954 void newuiSheet::NewGroup(const char *title, int16_t x, int16_t y, tAlignment align, int16_t pix_offset) {
1955 newuiSheet::t_gadget_desc *gadget = AddGadget(-1, (align == NEWUI_ALIGN_HORIZ) ? GADGET_HGROUP : GADGET_GROUP, title);
1956 gadget->parm.s[0] = x;
1957 gadget->parm.s[1] = y;
1958 gadget->id = pix_offset;
1959 }
1960
1961 // adds standard button to current group.
1962 void newuiSheet::AddButton(const char *title, int16_t id, int16_t flags) {
1963 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_BUTTON, title);
1964 gadget->parm.i = flags;
1965 }
1966
1967 // adds standard long button to current group.
1968 void newuiSheet::AddLongButton(const char *title, int16_t id, int16_t flags) {
1969 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_LBUTTON, title);
1970 gadget->parm.i = flags;
1971 }
1972
1973 // adds checkbox to current group. initial state of checkbox can be set.
1974 bool *newuiSheet::AddCheckBox(const char *title, bool init_state, int16_t id) {
1975 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_CHECKBOX, title);
1976 gadget->parm.b = init_state;
1977 return &gadget->parm.b;
1978 }
1979
1980 // adds checkbox to current group. initial state of checkbox can be set.
1981 bool *newuiSheet::AddLongCheckBox(const char *title, bool init_state, int16_t id) {
1982 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_LCHECKBOX, title);
1983 gadget->parm.b = init_state;
1984 return &gadget->parm.b;
1985 }
1986
1987 // adds a radio button to current group. initial state of radio may be set
1988 int *newuiSheet::AddFirstRadioButton(const char *title, int16_t id) {
1989 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_RADIO, title);
1990 gadget->parm.i = 0;
1991 return &gadget->parm.i;
1992 }
1993
1994 // adds a radio button to current group. initial state of radio may be set
1995 void newuiSheet::AddRadioButton(const char *title, int16_t id) {
1996 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_RADIO, title);
1997 gadget->parm.i = -1;
1998 }
1999
2000 // adds a radio button to current group. initial state of radio may be set
2001 int *newuiSheet::AddFirstLongRadioButton(const char *title, int16_t id) {
2002 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_LRADIO, title);
2003 gadget->parm.i = 0;
2004 return &gadget->parm.i;
2005 }
2006
2007 // adds a radio button to current group. initial state of radio may be set
2008 void newuiSheet::AddLongRadioButton(const char *title, int16_t id) {
2009 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_LRADIO, title);
2010 gadget->parm.i = -1;
2011 }
2012
2013 // adds a slider, set the range for it too.
2014 int16_t *newuiSheet::AddSlider(const char *title, int16_t range, int16_t init_pos, tSliderSettings *settings, int16_t id) {
2015 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_SLIDER, title);
2016 gadget->parm.s[0] = init_pos;
2017 gadget->parm.s[1] = range;
2018
2019 if (settings) {
2020 gadget->internal = mem_malloc(sizeof(tSliderSettings));
2021 memcpy(gadget->internal, settings, sizeof(tSliderSettings));
2022 } else {
2023 gadget->internal = NULL;
2024 }
2025
2026 return &gadget->parm.s[0];
2027 }
2028
2029 // adds a static text item
2030 void newuiSheet::AddText(const char *text, ...) {
2031 std::va_list arglist;
2032 char buf[512];
2033 int len;
2034
2035 va_start(arglist, text);
2036 len = std::vsnprintf(buf, 512, text, arglist);
2037 va_end(arglist);
2038
2039 newuiSheet::t_gadget_desc *gadget = AddGadget(-1, GADGET_STATIC_TXT, buf);
2040
2041 gadget->parm.b = false; // means small text.
2042 }
2043
2044 // adds a static bitmap
2045 void newuiSheet::AddBitmap(int bm_handle) {
2046 newuiSheet::t_gadget_desc *gadget = AddGadget(-1, GADGET_STATIC_BMP, NULL);
2047 gadget->parm.i = bm_handle;
2048 }
2049
2050 // adds a static text item
2051 char *newuiSheet::AddChangeableText(int buflen) {
2052 newuiSheet::t_gadget_desc *gadget = AddGadget(-1, GADGET_CHANGEABLE_TXT, NULL);
2053 gadget->internal = (void *)(size_t)buflen;
2054 gadget->parm.p = mem_malloc(buflen);
2055 return (char *)gadget->parm.p;
2056 }
2057
2058 // adds a hotspot :(, should word wrap too.
2059 void newuiSheet::AddHotspot(const char *title, int16_t w, int16_t h, int16_t id, bool group) {
2060 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_HOTSPOT, title);
2061 gadget->parm.s[0] = w;
2062 gadget->parm.s[1] = h;
2063 gadget->internal = (void *)(group);
2064 }
2065
2066 // adds a listbox
2067 newuiListBox *newuiSheet::AddListBox(int16_t w, int16_t h, int16_t id, uint16_t flags) {
2068 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_LISTBOX, NULL);
2069 newuiListBox *lb = new newuiListBox;
2070 lb->Create(m_parent, id, 0, 0, w, h, flags);
2071 m_parent->RemoveGadget(lb);
2072 gadget->internal = lb;
2073 return lb;
2074 }
2075
2076 // adds a listbox
2077 newuiComboBox *newuiSheet::AddComboBox(int16_t id, uint16_t flags) {
2078 newuiSheet::t_gadget_desc *gadget = AddGadget(id, GADGET_COMBOBOX, NULL);
2079 newuiComboBox *cb = new newuiComboBox;
2080 cb->Create(m_parent, id, 0, 0, flags);
2081 m_parent->RemoveGadget(cb);
2082 gadget->internal = cb;
2083 return cb;
2084 }
2085
2086 // adds an edit box
2087 char *newuiSheet::AddEditBox(const char *title, int16_t maxlen, int16_t w, int16_t id, int16_t flags, bool on_escape_quit) {
2088 int8_t type = GADGET_EDITBOX;
2089 if (flags & UIED_PASSWORD) {
2090 type = GADGET_EDITBOXPASS;
2091 } else if (flags & UIED_NUMBERS) {
2092 type = GADGET_EDITBOXNUM;
2093 }
2094
2095 newuiSheet::t_gadget_desc *gadget = AddGadget(id, type, title);
2096
2097 ASSERT(maxlen < EDIT_BUFLEN_MAX);
2098 ASSERT(w <= 0xfff);
2099
2100 char *strbuf = (char *)mem_malloc(maxlen + 1);
2101 strbuf[0] = 0;
2102 gadget->internal = strbuf;
2103 gadget->parm.s[0] = (w & 0xfff);
2104 gadget->parm.s[1] = maxlen;
2105
2106 // awful hack to store extra info into gadget structure. width should never exceed 4096
2107 if (on_escape_quit) {
2108 gadget->parm.s[0] |= (0x1000);
2109 }
2110
2111 return (char *)gadget->internal;
2112 }
2113
2114 //////////////////////////////////////////////////////////////////////////////
2115 // CLASS simple buttons
2116
2117 newuiButton::newuiButton() {
2118 m_bkg = NULL;
2119 m_litbkg = NULL;
2120 m_text = NULL;
2121 }
2122
2123 void newuiButton::Create(UIWindow *menu, int16_t id, const char *name, int16_t x, int16_t y, int16_t flags) {
2124 UITextItem item{""};
2125 UIButton::Create(menu, id, &item, x, y, 10, 8, flags | UIF_FIT);
2126
2127 if (flags & NEWUI_BTNF_FRAMED) {
2128 ASSERT(!(flags & NEWUI_BTNF_LONG)); // remove this if art is added.
2129 if (!m_bkg) {
2130 m_bkg = Newui_resources.Load((flags & NEWUI_BTNF_LONG) ? NEWUI_LBTNFRAMED_FILE : NEWUI_BTNFRAMED_FILE);
2131 }
2132 if (!m_litbkg) {
2133 m_litbkg = Newui_resources.Load((flags & NEWUI_BTNF_LONG) ? NEWUI_LBTNLITFRAMED_FILE : NEWUI_BTNLITFRAMED_FILE);
2134 }
2135 }
2136
2137 newuiButton::InitStates(name, (flags & NEWUI_BTNF_LONG) != 0);
2138 }
2139
2140 void newuiTinyButton::Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y) {
2141 m_bkg = Newui_resources.Load(NEWUI_TINYBTN_FILE);
2142 m_litbkg = Newui_resources.Load(NEWUI_TINYBTNLIT_FILE);
2143 newuiButton::Create(wnd, id, name, x, y);
2144 }
2145
2146 // override: called when resized or before drawing.
2147 void newuiButton::OnFormat() {
2148 m_W = GetStateItem(m_State)->width();
2149 m_H = GetStateItem(m_State)->height();
2150
2151 UIGadget::OnFormat();
2152 }
2153
2154 void newuiButton::InitStates(const char *name, bool is_long, int16_t flags) {
2155 if (!m_bkg) {
2156 m_bkg = Newui_resources.Load(is_long ? NEWUI_LBTN_FILE : NEWUI_BTN_FILE);
2157 }
2158
2159 if (!m_litbkg) {
2160 m_litbkg = Newui_resources.Load(is_long ? NEWUI_LBTNLIT_FILE : NEWUI_BTNLIT_FILE);
2161 }
2162
2163 if (name && !m_text) {
2164 m_text = GadgetSmallText(name);
2165 }
2166
2167 SetStateItem(UI_BTS_DISABLED, m_bkg);
2168 SetStateItem(UI_BTS_INACTIVE, m_bkg);
2169 SetStateItem(UI_BTS_HILITE, m_bkg);
2170 SetStateItem(UI_BTS_ACTIVATED, m_litbkg);
2171
2172 OnFormat();
2173 }
2174
2175 void newuiButton::OnDestroy() {
2176 if (m_bkg) {
2177 Newui_resources.Free(m_bkg);
2178 m_bkg = NULL;
2179 }
2180 if (m_litbkg) {
2181 Newui_resources.Free(m_litbkg);
2182 m_litbkg = NULL;
2183 }
2184 if (m_text) {
2185 delete m_text;
2186 m_text = NULL;
2187 }
2188
2189 UIButton::OnDestroy();
2190 }
2191
2192 void newuiButton::OnDraw() {
2193 uint8_t alpha = IsDisabled() ? 128 : 255;
2194
2195 if (GetStateItem(m_State)) {
2196 GetStateItem(m_State)->set_alpha(alpha);
2197 GetStateItem(m_State)->draw(0, 0);
2198 }
2199
2200 if (m_text) {
2201 m_text->set_alpha(alpha);
2202 m_text->draw((m_W - m_text->width()) / 2, (m_H - m_text->height()) / 2);
2203 }
2204 }
2205
2206 void newuiButton::OnLostFocus() {
2207 if (m_text) {
2208 m_text->set_flags(0);
2209 m_text->set_color(NEWUI_GADGETFONT_COLOR);
2210 }
2211 UIButton::OnLostFocus();
2212 }
2213
2214 void newuiButton::OnGainFocus() {
2215 if (m_text) {
2216 m_text->set_color(NEWUI_GADGETFONT_HICOLOR);
2217 m_text->set_flags(UISNAZZYTEXTF_BLINKING);
2218 }
2219 UIButton::OnGainFocus();
2220 }
2221
2222 //////////////////////////////////////////////////////////////////////////////
2223 // CLASS a large option button used in menus.
2224
2225 void newuiMenuOptionButton::Create(newuiMenu *menu, newuiMenuOptionButton *last, int16_t id, const char *name, int16_t x,
2226 int16_t y, bool mono_press) {
2227 m_bkg = Newui_resources.Load(NEWUI_LRGBTN_FILE);
2228 m_litbkg = Newui_resources.Load(NEWUI_LRGBTNLIT_FILE);
2229 m_text = GadgetLargeText(name);
2230 m_mono_press = mono_press;
2231 m_prev_btn = last;
2232 m_next_btn = NULL;
2233 if (m_prev_btn) {
2234 m_prev_btn->m_next_btn = this;
2235 }
2236
2237 newuiButton::Create(menu, id, name, x, y);
2238 }
2239
2240 void newuiMenuOptionButton::OnMouseBtnDown(int btn) {
2241 if (btn == UILMSEBTN) {
2242 if ((m_State != UI_BTS_ACTIVATED) && PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my)) {
2243 m_State = UI_BTS_ACTIVATED;
2244 LOCK_FOCUS(this);
2245 // mprintf(0, "mouse down in button gadget (%d)\n", GetID());
2246 if (m_mono_press) {
2247 // mprintf(0, "selected button gadget (%d)\n", GetID());
2248 OnSelect();
2249 m_State = UI_BTS_HILITE;
2250 }
2251 }
2252 }
2253 }
2254
2255 void newuiMenuOptionButton::OnMouseBtnUp(int btn) {
2256 if (btn == UILMSEBTN) {
2257 if (m_State == UI_BTS_ACTIVATED) {
2258 if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
2259 m_State = UI_BTS_HILITE;
2260 // if (!m_mono_press) {
2261 OnSelect();
2262 // }
2263 } else {
2264 m_State = UI_BTS_INACTIVE;
2265 }
2266 }
2267 UNLOCK_FOCUS(this);
2268 }
2269 }
2270
2271 void newuiMenuOptionButton::OnKeyDown(int key) {
2272 if (key == KEY_DOWN || key == KEY_RIGHT) {
2273 if (m_next_btn) {
2274 if (UIGadget::IsLocked()) {
2275 UNLOCK_FOCUS(this);
2276 }
2277 m_Wnd->SetFocusOnGadget(m_next_btn, true);
2278 }
2279 } else if (key == KEY_UP || key == KEY_LEFT) {
2280 if (m_prev_btn) {
2281 if (UIGadget::IsLocked()) {
2282 UNLOCK_FOCUS(this);
2283 }
2284 m_Wnd->SetFocusOnGadget(m_prev_btn, true);
2285 }
2286 }
2287 }
2288
2289 void newuiMenuOptionButton::OnKeyUp(int key) {}
2290
2291 //////////////////////////////////////////////////////////////////////////////
2292 // CLASS creates an arrow button that is sensitive to touch (when down, always select)
2293
2294 void newuiArrowButton::Create(UIWindow *wnd, int16_t id, int16_t type, const char *name, int16_t x, int16_t y) {
2295 switch (type) {
2296 case NEWUI_ARROW_LEFT:
2297 m_bkg = Newui_resources.Load(NEWUI_LARR_FILE);
2298 m_litbkg = Newui_resources.Load(NEWUI_LARRLIT_FILE);
2299 break;
2300 case NEWUI_ARROW_RIGHT:
2301 m_bkg = Newui_resources.Load(NEWUI_RARR_FILE);
2302 m_litbkg = Newui_resources.Load(NEWUI_RARRLIT_FILE);
2303 break;
2304 case NEWUI_ARROW_UP:
2305 m_bkg = Newui_resources.Load(NEWUI_UARR_FILE);
2306 m_litbkg = Newui_resources.Load(NEWUI_UARRLIT_FILE);
2307 break;
2308 case NEWUI_ARROW_DOWN:
2309 m_bkg = Newui_resources.Load(NEWUI_DARR_FILE);
2310 m_litbkg = Newui_resources.Load(NEWUI_DARRLIT_FILE);
2311 break;
2312 case NEWUI_ARROW_CBUP:
2313 m_bkg = Newui_resources.Load(NEWUI_UARRCB_FILE);
2314 m_litbkg = Newui_resources.Load(NEWUI_UARRLITCB_FILE);
2315 break;
2316 case NEWUI_ARROW_CBDOWN:
2317 m_bkg = Newui_resources.Load(NEWUI_DARRCB_FILE);
2318 m_litbkg = Newui_resources.Load(NEWUI_DARRLITCB_FILE);
2319 break;
2320 default:
2321 Int3();
2322 }
2323
2324 newuiButton::Create(wnd, id, name, x, y);
2325 UIGadget::SetDatum(type);
2326
2327 m_timer = 0.0f;
2328 m_selecttimer = 0.0f;
2329 m_hidden = false;
2330 }
2331
2332 void newuiArrowButton::Show(bool show) { m_hidden = !show; }
2333
2334 void newuiArrowButton::OnMouseBtnDown(int btn) {
2335 bool mouse_in_gadget =
2336 PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my); // DAJ Added to keep stray clicks from changing values
2337
2338 if (btn == UILMSEBTN && mouse_in_gadget) {
2339 if (m_State == UI_BTS_HILITE) {
2340 if (!m_hidden) {
2341 m_State = UI_BTS_ACTIVATED;
2342 LOCK_FOCUS(this);
2343 m_timer = 0.0f;
2344 }
2345 }
2346 if (m_State == UI_BTS_ACTIVATED) {
2347 // this is so that select messages get set at correct times
2348 if (m_hidden) {
2349 m_State = UI_BTS_HILITE;
2350 UNLOCK_FOCUS(this);
2351 } else {
2352 if (m_timer >= m_selecttimer) {
2353 if (m_selecttimer == 0.0f) {
2354 m_selecttimer = 0.5f;
2355 } else if (m_selecttimer == 0.5f) {
2356 m_selecttimer = 0.050f;
2357 }
2358 OnSelect(); // DAJ moved from above the m_selecttimer set since this is recursive
2359 m_timer = 0.0f;
2360 }
2361 m_timer += UIFrameTime;
2362 }
2363 }
2364 }
2365 }
2366
2367 void newuiArrowButton::OnMouseBtnUp(int btn) {
2368 if (btn == UILMSEBTN) {
2369 if (m_State == UI_BTS_ACTIVATED) {
2370 if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
2371 m_State = UI_BTS_HILITE;
2372 } else {
2373 m_State = UI_BTS_INACTIVE;
2374 }
2375 }
2376 UNLOCK_FOCUS(this);
2377
2378 m_selecttimer = 0.0f;
2379 m_timer = 0.0f;
2380 }
2381 }
2382
2383 void newuiArrowButton::OnDraw() {
2384 if (m_hidden)
2385 return;
2386
2387 newuiButton::OnDraw();
2388 }
2389
2390 //////////////////////////////////////////////////////////////////////////////
2391 // CLASS a new check box: note that newuiButton and UICheckBox will share the same UIButton base
2392 // since bott newuiButton and UICheckBox inherit UIButton virtually.
2393
2394 newuiCheckBox::newuiCheckBox() {}
2395
2396 void newuiCheckBox::Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y, bool is_long) {
2397 m_bkg = Newui_resources.Load(is_long ? NEWUI_LCHKBTN_FILE : NEWUI_CHKBTN_FILE);
2398 m_litbkg = Newui_resources.Load(is_long ? NEWUI_LCHKBTNLIT_FILE : NEWUI_CHKBTNLIT_FILE);
2399
2400 UITextItem item{""};
2401 UICheckBox::Create(wnd, id, &item, x, y, 10, 8, UIF_FIT);
2402 newuiButton::InitStates(name, is_long);
2403 }
2404
2405 // this will use the newuiButton drawing scheme
2406 void newuiCheckBox::OnDraw() { newuiButton::OnDraw(); }
2407
2408 //////////////////////////////////////////////////////////////////////////////
2409 // CLASS a new radio button
2410
2411 newuiRadioButton::newuiRadioButton() {}
2412
2413 void newuiRadioButton::Create(UIWindow *wnd, UIRadioButton *prev_rb, int16_t id, const char *name, int16_t x, int16_t y,
2414 bool is_long) {
2415 m_bkg = Newui_resources.Load(is_long ? NEWUI_LBTN_FILE : NEWUI_BTN_FILE);
2416 m_litbkg = Newui_resources.Load(is_long ? NEWUI_LCHKBTNLIT_FILE : NEWUI_CHKBTNLIT_FILE);
2417
2418 UITextItem item{""};
2419 UIRadioButton::Create(wnd, prev_rb, id, &item, x, y, 10, 8, UIF_FIT);
2420 newuiButton::InitStates(name, is_long);
2421 }
2422
2423 // this will use the newuiButton drawing scheme
2424 void newuiRadioButton::OnDraw() { newuiButton::OnDraw(); }
2425
2426 //////////////////////////////////////////////////////////////////////////////
2427 // CLASS a new slider.
2428
2429 newuiSlider::newuiSlider() {
2430 m_pos = m_unitrange = 0;
2431 m_bar_bmp = NULL;
2432 m_title = NULL;
2433 }
2434
2435 void newuiSlider::Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y, int16_t range) {
2436 ASSERT(range > 0);
2437
2438 if (name) {
2439 m_title = MonitorSmallText(name);
2440 }
2441
2442 m_pos = range;
2443 m_unitrange = range;
2444 m_bar_bmp = Newui_resources.Load(NEWUI_SLIDER_FILE);
2445
2446 UIGadget::Create(wnd, id, x, y, 10, 10, 0);
2447
2448 m_unit_settings.type = -1;
2449 }
2450
2451 void newuiSlider::Offset(int16_t offs) { newuiSlider::SetPos(m_pos + offs); }
2452
2453 void newuiSlider::SetPos(int16_t pos) {
2454 if (pos < 0)
2455 pos = 0;
2456 if (pos > m_unitrange)
2457 pos = m_unitrange;
2458 m_pos = pos;
2459 }
2460
2461 void newuiSlider::SetRange(int16_t range) { m_unitrange = range; }
2462
2463 // when gadget is added to a window (AddGadget is called)
2464 void newuiSlider::OnAttachToWindow() {
2465 int16_t bx = m_X, by = m_Y;
2466
2467 if (m_title) {
2468 by += m_title->height() + 5;
2469 bx += 2;
2470 }
2471
2472 m_minus_btn.Create(m_Wnd, -1, NEWUI_ARROW_LEFT, NULL, bx, by);
2473 m_minus_btn.SetFlag(UIF_NOTIFYMASTERSEL);
2474 AttachSlaveGadget(&m_minus_btn);
2475 m_plus_btn.Create(m_Wnd, -1, NEWUI_ARROW_RIGHT, NULL, bx + m_bar_bmp->width() - 24, by);
2476 m_plus_btn.SetFlag(UIF_NOTIFYMASTERSEL);
2477 AttachSlaveGadget(&m_plus_btn);
2478 }
2479
2480 // when gadget is detached from window
2481 void newuiSlider::OnDetachFromWindow() {
2482 if (m_plus_btn.IsCreated()) {
2483 DetachSlaveGadget(&m_plus_btn);
2484 m_plus_btn.Destroy();
2485 }
2486 if (m_minus_btn.IsCreated()) {
2487 DetachSlaveGadget(&m_minus_btn);
2488 m_minus_btn.Destroy();
2489 }
2490 }
2491
2492 void newuiSlider::SetUnits(tSliderSettings *settings) {
2493 if (settings == NULL) {
2494 m_unit_settings.type = -1;
2495 } else {
2496 memcpy(&m_unit_settings, settings, sizeof(m_unit_settings));
2497 }
2498 }
2499
2500 void newuiSlider::OnFormat() {
2501 if (m_bar_bmp) {
2502 m_W = m_bar_bmp->width();
2503 m_H = m_bar_bmp->height();
2504 if (m_title) {
2505 m_H += m_title->height();
2506 }
2507 }
2508 }
2509
2510 void newuiSlider::OnLostFocus() {
2511 if (m_title) {
2512 m_title->set_flags(0);
2513 }
2514 }
2515
2516 void newuiSlider::OnGainFocus() {
2517 if (m_title) {
2518 m_title->set_flags(UISNAZZYTEXTF_BLINKING);
2519 }
2520 }
2521
2522 // override: behavior when key pressed.
2523 void newuiSlider::OnKeyDown(int key) {
2524 if (key == KEY_LEFT) {
2525 newuiSlider::Offset(-1);
2526 UIGadget::OnSelect();
2527 }
2528 if (key == KEY_RIGHT) {
2529 newuiSlider::Offset(1);
2530 UIGadget::OnSelect();
2531 }
2532 }
2533
2534 // override: behavior when key released.
2535 void newuiSlider::OnKeyUp(int key) {}
2536
2537 void newuiSlider::OnDraw() {
2538 int ox = 0, oy = 0;
2539 int x, lx;
2540 float percent_full, delta_g, green_intensity;
2541
2542 percent_full = (float)m_pos / (float)m_unitrange;
2543
2544 // draw units value if available
2545 if (m_unit_settings.type > -1) {
2546 char str[8];
2547
2548 switch (m_unit_settings.type) {
2549 case SLIDER_UNITS_INT:
2550 snprintf(str, sizeof(str), "%d",
2551 m_unit_settings.min_val.i +
2552 (int)floor((m_unit_settings.max_val.i - m_unit_settings.min_val.i) * percent_full + 0.5f));
2553 break;
2554 case SLIDER_UNITS_FLOAT:
2555 snprintf(str, sizeof(str), "%.2f",
2556 m_unit_settings.min_val.f + (m_unit_settings.max_val.f - m_unit_settings.min_val.f) * percent_full);
2557 break;
2558 case SLIDER_UNITS_PERCENT:
2559 snprintf(str, sizeof(str), "%d%%", (int)((percent_full * 100.0f) + 0.5f));
2560 break;
2561 default:
2562 Int3();
2563 str[0] = 0;
2564 }
2565
2566 UITextItem value_txt(MONITOR9_NEWUI_FONT, str, NEWUI_MONITORFONT_COLOR);
2567 value_txt.draw(ox + m_W - value_txt.width() - 1, oy + 1);
2568 }
2569
2570 // draw graphic bar first
2571 if (m_title) {
2572 m_title->draw(ox, oy + 1);
2573 oy += m_title->height() + 2;
2574 }
2575
2576 // draw bar frame
2577 if (m_bar_bmp) {
2578 m_bar_bmp->draw(ox, oy);
2579 ox += 20; // move to where bar will be filled in.
2580 oy += 2;
2581 }
2582
2583 // draw slider bar now.
2584 int width = m_bar_bmp->width() - 42;
2585 int height = m_bar_bmp->height() - 6;
2586
2587 lx = (percent_full * width) + ox;
2588 delta_g = (lx - ox) ? (percent_full * 255.0f) / (lx - ox) : 0.0f;
2589 green_intensity = 0.0f;
2590
2591 for (x = ox; x < lx; x++) {
2592 ui_DrawLine(GR_RGB(0, green_intensity, 0), x, oy + 1, x, oy + height - 1);
2593 green_intensity += delta_g;
2594 }
2595 }
2596
2597 void newuiSlider::OnDestroy() {
2598 if (m_bar_bmp) {
2599 Newui_resources.Free(m_bar_bmp);
2600 m_bar_bmp = NULL;
2601 }
2602 if (m_title) {
2603 delete m_title;
2604 m_title = NULL;
2605 }
2606 }
2607
2608 // this function will handle when an arrow button was pressed
2609 void newuiSlider::OnNotifySelect(UIGadget *g) {
2610 switch (g->GetDatum()) {
2611 case NEWUI_ARROW_LEFT:
2612 newuiSlider::Offset(-1);
2613 break;
2614 case NEWUI_ARROW_RIGHT:
2615 newuiSlider::Offset(1);
2616 break;
2617 }
2618
2619 UIGadget::OnSelect();
2620 }
2621
2622 // CLASS a new listbox. uses less memory than the old listbox hopefully.
2623
2624 newuiListBox::newuiListBox() {
2625 m_ItemList = NULL;
2626 m_Virt2Real = NULL;
2627 m_Real2Virt = NULL;
2628 selectchange_fn = NULL;
2629 selectchange_id_fn = NULL;
2630 m_callbackptr = NULL;
2631 m_NumItems = 0;
2632 m_SelectedIndex = -1;
2633 m_Index = 0;
2634 m_NumVisibleItems = 0;
2635 m_mse_clicked = false;
2636 m_nclicks = 0;
2637 m_mse_x = 0;
2638 m_mse_y = 0;
2639 m_click_time = 0.0f;
2640 }
2641
2642 void newuiListBox::Create(UIWindow *wnd, int16_t id, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t flags) {
2643 m_ItemList = NULL;
2644 m_Virt2Real = NULL;
2645 m_Real2Virt = NULL;
2646 selectchange_fn = NULL;
2647 selectchange_id_fn = NULL;
2648 m_callbackptr = NULL;
2649 m_NumItems = 0;
2650 m_SelectedIndex = -1;
2651 m_Index = 0;
2652 m_NumVisibleItems = 0;
2653 m_boffs = 12;
2654 m_nclicks = 0;
2655 m_mse_clicked = false;
2656 m_click_time = 0.0f;
2657
2658 m_pieces[N_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_N_FILE);
2659 m_pieces[NE_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_NE_FILE);
2660 m_pieces[E_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_E_FILE);
2661 m_pieces[SE_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_SE_FILE);
2662 m_pieces[S_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_S_FILE);
2663 m_pieces[SW_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_SW_FILE);
2664 m_pieces[W_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_W_FILE);
2665 m_pieces[NW_PIECE_IDX] = Newui_resources.Load(NEWUI_LB_NW_FILE);
2666
2667 // determine true width and height (snap width and height to 32 pixel boundaries
2668 int16_t p_w = LB_PIECE_WIDTH, p_h = LB_PIECE_HEIGHT;
2669 int16_t t_width = (w / p_w);
2670 int16_t t_height = (h / p_h);
2671 if (w % p_w)
2672 t_width++;
2673 if (h % p_h)
2674 t_height++;
2675 w = t_width * p_w;
2676 h = t_height * p_h;
2677
2678 UIGadget::Create(wnd, id, x, y, w, h, flags);
2679 }
2680
2681 // functions to add or remove items,
2682 void newuiListBox::AddItem(const char *name) {
2683 char **temp;
2684 int i;
2685 int *tempvirt2real, *tempreal2virt;
2686 int real_index = -1;
2687
2688 if (m_Flags & UILB_NOSORT) {
2689 // insert without sort
2690
2691 if ((m_NumItems % LISTBOX_BUFFER_SIZE) == 0) {
2692 // dynamically allocated memory for listbox, expand as needed.
2693 temp = (char **)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(char *));
2694 tempvirt2real = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
2695 tempreal2virt = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
2696
2697 if (m_ItemList) {
2698 for (i = 0; i < m_NumItems; i++) {
2699 temp[i] = m_ItemList[i];
2700 tempvirt2real[i] = m_Virt2Real[i];
2701 tempreal2virt[i] = m_Real2Virt[i];
2702 }
2703
2704 mem_free(m_ItemList);
2705 mem_free(m_Virt2Real);
2706 mem_free(m_Real2Virt);
2707 }
2708 m_ItemList = temp;
2709 m_Virt2Real = tempvirt2real;
2710 m_Real2Virt = tempreal2virt;
2711 }
2712
2713 real_index = m_NumItems;
2714 } else {
2715 // only text item listboxes!
2716
2717 // insert with sorting
2718 if ((m_NumItems % LISTBOX_BUFFER_SIZE) == 0) {
2719 // dynamically allocated memory for listbox, expand as needed.
2720 int oidx = 0, cidx = 0;
2721 int pidx = 0;
2722 int result;
2723
2724 temp = (char **)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(char *));
2725 tempvirt2real = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
2726 tempreal2virt = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
2727
2728 real_index = -1;
2729
2730 if (m_ItemList) {
2731 while (oidx <= m_NumItems) {
2732 if (oidx == m_NumItems) {
2733 if (real_index == -1) {
2734 real_index = oidx;
2735 } else {
2736 temp[cidx] = m_ItemList[pidx];
2737 tempreal2virt[cidx] = m_Real2Virt[pidx];
2738 }
2739 oidx++;
2740 continue;
2741 }
2742
2743 if (m_Flags & UILB_CASESENSITIVE)
2744 result = strcmp(m_ItemList[cidx], name);
2745 else
2746 result = stricmp(m_ItemList[cidx], name);
2747 if (result < 0) {
2748 // copy old
2749 temp[cidx] = m_ItemList[pidx];
2750 tempreal2virt[cidx] = m_Real2Virt[pidx];
2751 cidx++;
2752 pidx++;
2753 } else if (result >= 0) {
2754 // see if we need to insert, if not just keep copying
2755 if (real_index == -1) {
2756 real_index = cidx;
2757 cidx++;
2758 } else {
2759 temp[cidx] = m_ItemList[pidx];
2760 tempreal2virt[cidx] = m_Real2Virt[pidx];
2761 cidx++;
2762 pidx++;
2763 }
2764 }
2765 oidx++;
2766 }
2767 ASSERT(real_index != -1);
2768
2769 // now we need to fix m_Virt2Real to represent the new structure,
2770 // all values>=real_index must be incremented
2771 for (oidx = 0; oidx <= m_NumItems; oidx++) {
2772 if (oidx < m_NumItems) {
2773 tempvirt2real[oidx] = m_Virt2Real[oidx];
2774
2775 // if(oidx!=real_index){
2776 if (tempvirt2real[oidx] >= real_index)
2777 tempvirt2real[oidx]++;
2778 }
2779 }
2780
2781 mem_free(m_ItemList);
2782 mem_free(m_Virt2Real);
2783 mem_free(m_Real2Virt);
2784 } else {
2785 real_index = 0;
2786 }
2787
2788 m_ItemList = temp;
2789 m_Virt2Real = tempvirt2real;
2790 m_Real2Virt = tempreal2virt;
2791 } else {
2792 // find where to insert, without allocating memory
2793 int oidx = 0, cidx = 0;
2794 real_index = -1;
2795 int result;
2796
2797 int old_r2v;
2798 char *old_item = NULL;
2799
2800 while (oidx <= m_NumItems) {
2801
2802 if (oidx == m_NumItems) {
2803 // we're past the slot of the old buffer
2804 if (real_index == -1) {
2805 // we haven't set the real index yet, so it has to go here
2806 real_index = m_NumItems;
2807 } else {
2808 // the real index has been set, we're still pulling back
2809 m_Real2Virt[cidx] = old_r2v;
2810 m_ItemList[cidx] = old_item;
2811 }
2812 oidx++;
2813 continue;
2814 } else {
2815 if (m_Flags & UILB_CASESENSITIVE)
2816 result = strcmp(m_ItemList[cidx], name);
2817 else
2818 result = stricmp(m_ItemList[cidx], name);
2819 }
2820
2821 if (result < 0) {
2822 // we haven't gotten to the string yet, keep as they are
2823 cidx++;
2824 } else if (result >= 0) {
2825 // see if we need to insert, if not just keep copying
2826 if (real_index == -1) {
2827 // we need to insert here
2828 real_index = cidx;
2829 old_item = m_ItemList[cidx];
2830 old_r2v = m_Real2Virt[cidx];
2831 cidx++;
2832 } else {
2833 // old_item and old_r2v should be set!
2834 ASSERT(old_item);
2835 char *oi;
2836 int or2v;
2837
2838 or2v = m_Real2Virt[cidx];
2839 oi = m_ItemList[cidx];
2840
2841 m_Real2Virt[cidx] = old_r2v;
2842 m_ItemList[cidx] = old_item;
2843 old_r2v = or2v;
2844 old_item = oi;
2845
2846 cidx++;
2847 }
2848 }
2849
2850 // continue on
2851 oidx++;
2852 }
2853
2854 ASSERT(real_index != -1);
2855
2856 // now we need to fix m_Virt2Real to represent the new structure,
2857 // all values>=real_index must be incremented
2858 cidx = 0;
2859 for (oidx = 0; oidx <= m_NumItems; oidx++) {
2860 if (oidx != real_index) {
2861 if (m_Virt2Real[cidx] >= real_index)
2862 m_Virt2Real[cidx]++;
2863 cidx++;
2864 }
2865 }
2866 }
2867 }
2868
2869 // copy item passed to list.
2870 m_ItemList[real_index] = mem_strdup(name);
2871 m_Virt2Real[m_NumItems] = real_index;
2872 m_Real2Virt[real_index] = m_NumItems;
2873 m_NumItems++;
2874
2875 if (real_index <= m_SelectedIndex) {
2876 // adjust selected index
2877 if (m_SelectedIndex < m_NumItems - 1)
2878 m_SelectedIndex++;
2879 }
2880 }
2881
2882 void newuiListBox::RemoveItem(const char *name) {
2883 int i, j, found = -1;
2884
2885 for (i = 0; i < m_NumItems; i++) {
2886 int res;
2887 res = (m_Flags & UILB_CASESENSITIVE) ? stricmp(m_ItemList[i], name) : strcmp(m_ItemList[i], name);
2888 if (res == 0) {
2889 found = i;
2890 mem_free(m_ItemList[found]);
2891 m_ItemList[found] = NULL;
2892 break;
2893 }
2894 }
2895 if (found == -1) {
2896 mprintf(0, "newuiListBox:: Didn't find item to remove!\n");
2897 return;
2898 }
2899
2900 // found is the real index to remove
2901 // go through virt2real and subtract one from values of found or higher
2902 // go through real2virt and move everything from index found on down, and subtract
2903 // 1 from those with value of found or higher
2904 int real_index = found; // index to remove
2905 int virt_index = m_Real2Virt[found]; // index to remove
2906 int real_thresh = virt_index;
2907 int virt_thresh = real_index;
2908
2909 for (j = 0; j < (m_NumItems - 1); j++) {
2910 if (j >= real_index) {
2911 m_ItemList[j] = m_ItemList[j + 1];
2912 m_Real2Virt[j] = m_Real2Virt[j + 1];
2913 }
2914
2915 if (j >= virt_index) {
2916 m_Virt2Real[j] = m_Virt2Real[j + 1];
2917 }
2918
2919 if (m_Real2Virt[j] >= real_thresh)
2920 m_Real2Virt[j]--;
2921
2922 if (m_Virt2Real[j] >= virt_thresh)
2923 m_Virt2Real[j]--;
2924 }
2925 m_NumItems--;
2926
2927 if (real_index <= m_SelectedIndex) {
2928 // adjust selected index
2929 if (m_SelectedIndex > 0)
2930 m_SelectedIndex--;
2931 }
2932 }
2933
2934 void newuiListBox::RemoveAll() {
2935 m_SelectedIndex = -1;
2936
2937 if (m_ItemList) {
2938 int i;
2939 for (i = 0; i < m_NumItems; i++) {
2940 if (m_ItemList[i])
2941 mem_free(m_ItemList[i]);
2942 }
2943 mem_free(m_ItemList);
2944 }
2945
2946 if (m_Virt2Real)
2947 mem_free(m_Virt2Real);
2948
2949 if (m_Real2Virt)
2950 mem_free(m_Real2Virt);
2951
2952 m_ItemList = NULL;
2953 m_Virt2Real = NULL;
2954 m_Real2Virt = NULL;
2955 m_Index = m_NumItems = 0;
2956 }
2957
2958 // item information
2959 int newuiListBox::GetCurrentIndex() {
2960 if (m_NumItems == 0)
2961 return 0;
2962
2963 if (m_SelectedIndex < 0)
2964 return m_SelectedIndex;
2965
2966 if (!m_Real2Virt)
2967 return 0;
2968
2969 int virt_index = m_Real2Virt[m_SelectedIndex];
2970 ASSERT(virt_index >= 0 && virt_index < m_NumItems);
2971 return virt_index;
2972 }
2973
2974 void newuiListBox::SetCurrentIndex(int index) {
2975 if (m_NumItems == 0)
2976 return;
2977
2978 if (index >= 0 && index < m_NumItems) {
2979 int real_index = m_Virt2Real[index], old_index = m_SelectedIndex;
2980 int i = 0, y;
2981
2982 ASSERT(real_index >= 0 && real_index < m_NumItems);
2983 m_SelectedIndex = real_index;
2984
2985 // determine how many items fit in a listbox the long way.
2986 for (i = 0, y = m_boffs; i < m_NumItems; i++) {
2987 UITextItem item(m_ItemList[i]);
2988 item.set_font(MONITOR9_NEWUI_FONT);
2989
2990 y += item.height();
2991 if (y > (m_H - m_boffs - item.height())) {
2992 i++;
2993 break;
2994 }
2995 }
2996
2997 if (m_SelectedIndex < m_Index) {
2998 m_Index = m_SelectedIndex;
2999 }
3000 if (m_SelectedIndex >= (m_Index + i)) {
3001 m_Index = m_SelectedIndex - i + 1;
3002 }
3003 if (m_Index < 0) {
3004 m_Index = 0;
3005 }
3006
3007 // call callbacks if selection really changed.
3008 if (old_index != m_SelectedIndex) {
3009 if (selectchange_fn)
3010 (*selectchange_fn)(index);
3011 if (selectchange_id_fn)
3012 (*selectchange_id_fn)(GetID(), m_callbackptr);
3013 }
3014 }
3015 }
3016
3017 void newuiListBox::SetInternalCurrentIndex(int index) {}
3018
3019 bool newuiListBox::GetCurrentItem(char *buffer, int buflen) {
3020 return newuiListBox::GetItem(m_Real2Virt[m_SelectedIndex], buffer, buflen);
3021 }
3022
3023 bool newuiListBox::GetItem(int index, char *buffer, int buflen) {
3024 if (m_NumItems == 0) {
3025 return false;
3026 *buffer = 0;
3027 }
3028
3029 if (index >= 0 && index < m_NumItems) {
3030 int real_index = m_Virt2Real[index];
3031 ASSERT(real_index >= 0 && real_index < m_NumItems);
3032 strncpy(buffer, m_ItemList[real_index], buflen - 1);
3033 buffer[buflen - 1] = 0;
3034 return true;
3035 }
3036
3037 return false;
3038 }
3039
3040 bool newuiListBox::SetCurrentItem(const char *name) {
3041 int i, found = 0;
3042
3043 for (i = 0; i < m_NumItems; i++) {
3044 int res = (m_Flags & UILB_CASESENSITIVE) ? stricmp(m_ItemList[i], name) : strcmp(m_ItemList[i], name);
3045 if (res == 0) {
3046 SetCurrentIndex(m_Real2Virt[i]);
3047 found = 1;
3048 break;
3049 }
3050 }
3051
3052 if (!found) {
3053 mprintf(0, "newuiListBox::SelectItem item not found!\n");
3054 return false;
3055 }
3056
3057 return true;
3058 }
3059
3060 // set callbacks
3061 void newuiListBox::SetSelectChangeCallback(void (*fn)(int)) { selectchange_fn = fn; }
3062
3063 void newuiListBox::SetSelectChangeCallback(void (*fn)(int, void *), void *ptr) {
3064 selectchange_id_fn = fn;
3065 m_callbackptr = ptr;
3066 }
3067
3068 // override: behavior when gadget loses input focus.
3069 void newuiListBox::OnLostFocus() {}
3070
3071 // override: behavior when gadget gains input focus.
3072 void newuiListBox::OnGainFocus() {}
3073
3074 // behavior when gadget is being drawn.
3075 void newuiListBox::OnDraw() {
3076 int t_width = (m_W / 32), tx, x;
3077 int t_height = (m_H / 32), ty, y;
3078
3079 // draw tiles.
3080 tx = x = 0;
3081 ty = y = 0;
3082
3083 // top row.
3084 m_pieces[NW_PIECE_IDX]->draw(x, y);
3085 tx++;
3086 x += LB_PIECE_WIDTH;
3087 while (tx++ < (t_width - 1)) {
3088 m_pieces[N_PIECE_IDX]->draw(x, y);
3089 x += LB_PIECE_WIDTH;
3090 }
3091 m_pieces[NE_PIECE_IDX]->draw(x, y);
3092 tx++;
3093 x = 0;
3094 y += LB_PIECE_HEIGHT;
3095 ty++;
3096
3097 // mid rows.
3098 while (ty++ < (t_height - 1)) {
3099 m_pieces[W_PIECE_IDX]->draw(x, y);
3100 x += LB_PIECE_WIDTH;
3101 tx = 1;
3102 while (tx++ < (t_width - 1)) {
3103 x += LB_PIECE_WIDTH;
3104 }
3105 m_pieces[E_PIECE_IDX]->draw(x, y);
3106 tx++;
3107 x = 0;
3108 y += LB_PIECE_HEIGHT;
3109 }
3110
3111 // bottom row.
3112 m_pieces[SW_PIECE_IDX]->draw(x, y);
3113 x += LB_PIECE_WIDTH;
3114 tx = 1;
3115 while (tx++ < (t_width - 1)) {
3116 m_pieces[S_PIECE_IDX]->draw(x, y);
3117 x += LB_PIECE_WIDTH;
3118 }
3119 m_pieces[SE_PIECE_IDX]->draw(x, y);
3120 tx++;
3121 ty++;
3122
3123 // draw text?
3124 int i;
3125 uint8_t alpha = 255;
3126 bool auto_select = ((m_Flags & UILB_AUTOSELECT) > 0);
3127 bool use_scroll = true;
3128
3129 ui_SetTextClip(m_boffs, m_boffs, m_W - m_boffs - m_up_btn.W(), m_H - m_boffs);
3130
3131 for (i = m_Index, y = m_boffs; i < m_NumItems; i++) {
3132 UITextItem item(m_ItemList[i]);
3133 item.set_font(MONITOR9_NEWUI_FONT);
3134
3135 if (m_mse_clicked) {
3136 if (m_mse_y >= y && m_mse_y <= (y + item.height() - 1)) {
3137 if (m_mse_x > m_boffs && m_mse_x < (m_W - m_boffs - 12)) {
3138 newuiListBox::SetCurrentIndex((m_Real2Virt) ? m_Real2Virt[i] : 0);
3139 // item.set_font(GADGET9_NEWUI_FONT);
3140 }
3141 }
3142 }
3143
3144 y += item.height();
3145 if (y > (m_H - m_boffs - item.height())) {
3146 break;
3147 }
3148 }
3149
3150 m_mse_clicked = false;
3151
3152 if (m_SelectedIndex == -1 && m_NumItems > 0) {
3153 newuiListBox::SetCurrentIndex(m_Real2Virt[0]);
3154 }
3155
3156 for (i = m_Index, y = m_boffs; i < m_NumItems; i++) {
3157 UITextItem item(m_ItemList[i]);
3158
3159 item.set_font(MONITOR9_NEWUI_FONT);
3160 item.set_color(NEWUI_MONITORFONT_COLOR);
3161
3162 if (m_SelectedIndex == i) {
3163 // draw a bar?
3164 ddgr_color bar_color;
3165 // item.set_font(GADGET9_NEWUI_FONT);
3166 // item.set_color(NEWUI_GADGETFONT_COLOR);
3167 bar_color = m_infocus ? GR_RGB(0, 64, 0) : GR_RGB(0, 32, 0);
3168 ui_DrawRect(bar_color, m_boffs, y, m_W - m_boffs - m_up_btn.W(), y + item.height());
3169 }
3170
3171 if (!m_infocus) {
3172 item.set_alpha(192);
3173 }
3174
3175 item.draw(m_boffs, y);
3176
3177 y += item.height();
3178 if (y > (m_H - m_boffs - item.height())) {
3179 i++;
3180 break;
3181 }
3182 }
3183
3184 if (m_NumItems) {
3185 if (use_scroll && auto_select) {
3186 if (m_SelectedIndex < m_Index)
3187 newuiListBox::SetCurrentIndex(m_Real2Virt[m_Index]);
3188 if (m_SelectedIndex > i)
3189 newuiListBox::SetCurrentIndex(m_Real2Virt[i]);
3190 }
3191 }
3192
3193 m_NumVisibleItems = (i - m_Index);
3194
3195 ui_ResetTextClip();
3196 }
3197
3198 // behavior when key is pressed.
3199 void newuiListBox::OnKeyDown(int key) {
3200 if (key == KEY_SPACEBAR) {
3201
3202 } else {
3203 int new_selected_index = m_SelectedIndex;
3204
3205 if (key == KEY_UP) {
3206 if (m_SelectedIndex > 0) {
3207 new_selected_index = m_SelectedIndex - 1;
3208 }
3209 } else if (key == KEY_DOWN) {
3210 if (m_SelectedIndex < (m_NumItems - 1)) {
3211 new_selected_index = m_SelectedIndex + 1;
3212 }
3213 } else if (key == KEY_PAGEUP) {
3214 if (m_SelectedIndex != m_Index) {
3215 new_selected_index = m_Index;
3216 } else {
3217 new_selected_index = m_SelectedIndex - m_NumVisibleItems;
3218 if (new_selected_index < 0)
3219 new_selected_index = 0;
3220 }
3221 } else if (key == KEY_PAGEDOWN) {
3222 if (m_SelectedIndex != (m_Index + m_NumVisibleItems - 1)) {
3223 new_selected_index = (m_Index + m_NumVisibleItems - 1);
3224 } else {
3225 new_selected_index = m_SelectedIndex + m_NumVisibleItems;
3226 if (new_selected_index >= m_NumItems)
3227 new_selected_index = m_NumItems - 1;
3228 }
3229 }
3230 newuiListBox::SetCurrentIndex((m_Real2Virt) ? m_Real2Virt[new_selected_index] : 0);
3231 }
3232 }
3233
3234 // behavior when key is released.
3235 void newuiListBox::OnKeyUp(int key) {}
3236
3237 // behavior when mouse button is pressed.
3238 void newuiListBox::OnMouseBtnDown(int btn) {
3239 bool mouse_in_gadget = PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my);
3240
3241 if (btn == UILMSEBTN && mouse_in_gadget) {
3242 float time_delta = 0.0f, old_click_time;
3243 m_mse_x = SCREEN_TO_GAD_X(this, UI_input.mx);
3244 m_mse_y = SCREEN_TO_GAD_Y(this, UI_input.my);
3245 m_mse_clicked = true;
3246
3247 old_click_time = m_click_time;
3248 m_click_time = UI_TIME();
3249
3250 time_delta = m_click_time - old_click_time;
3251 if (time_delta > UI_DBLCLICK_DELAY) {
3252 m_nclicks = 0;
3253 }
3254
3255 if (m_nclicks == 0) {
3256 LOCK_FOCUS(this);
3257 m_nclicks = 1;
3258 // mprintf(0, "click 1\n");
3259 } else if (m_nclicks == 2) {
3260 // mprintf(0, "click 2\n");
3261 LOCK_FOCUS(this);
3262 if (((m_click_time - old_click_time) < UI_DBLCLICK_DELAY)) {
3263 if (abs(m_mse_x - m_last_mse_x) < UI_DBLCLICK_MSEDELTA && abs(m_mse_y - m_last_mse_y) < UI_DBLCLICK_MSEDELTA) {
3264 if (m_NumItems > 0 && m_SelectedIndex > -1) {
3265 int y, i;
3266 for (i = m_Index, y = m_boffs; i < m_NumItems; i++) {
3267 UITextItem item(m_ItemList[i]);
3268 item.set_font(MONITOR9_NEWUI_FONT);
3269
3270 if (m_mse_y >= y && m_mse_y <= (y + item.height() - 1)) {
3271 if (m_mse_x > m_boffs && m_mse_x < (m_W - m_boffs - 12)) {
3272 OnSelect();
3273 // mprintf(0, "select!\n");
3274 m_nclicks = 0;
3275 break;
3276 }
3277 }
3278 y += item.height();
3279 if (y > (m_H - m_boffs - item.height())) {
3280 break;
3281 }
3282 }
3283 UNLOCK_FOCUS(this);
3284 }
3285 }
3286 }
3287 if (m_nclicks == 2)
3288 m_nclicks = 3;
3289 }
3290 m_last_mse_x = m_mse_x;
3291 m_last_mse_y = m_mse_y;
3292 } else if (btn == UILMSEBTN && !mouse_in_gadget) {
3293 if (IsLocked())
3294 UNLOCK_FOCUS(this);
3295 m_nclicks = 0;
3296 }
3297 }
3298
3299 // behavior when mouse button is released.
3300 void newuiListBox::OnMouseBtnUp(int btn) {
3301 bool mouse_in_gadget = PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my);
3302
3303 if (btn == UILMSEBTN && mouse_in_gadget) {
3304 UNLOCK_FOCUS(this);
3305 if (m_nclicks == 1)
3306 m_nclicks = 2;
3307 else if (m_nclicks == 3)
3308 m_nclicks = 0;
3309 m_last_selected_index = m_SelectedIndex;
3310 }
3311 }
3312
3313 // this function will handle when an arrow button was pressed
3314 void newuiListBox::OnNotifySelect(UIGadget *g) {
3315 switch (g->GetDatum()) {
3316 case NEWUI_ARROW_UP:
3317 newuiListBox::Offset(-1);
3318 break;
3319 case NEWUI_ARROW_DOWN:
3320 newuiListBox::Offset(1);
3321 break;
3322 }
3323 }
3324
3325 // when a listbox item was double clicked (or entered)
3326 void newuiListBox::OnSelect() { UIGadget::OnSelect(); }
3327
3328 // called when destroyed.
3329 void newuiListBox::OnDestroy() {
3330 int i;
3331
3332 if (m_up_btn.IsCreated())
3333 m_up_btn.Destroy();
3334 if (m_down_btn.IsCreated())
3335 m_down_btn.Destroy();
3336
3337 for (i = 0; i < m_NumItems; i++) {
3338 mem_free(m_ItemList[i]);
3339 }
3340
3341 if (m_ItemList)
3342 mem_free(m_ItemList);
3343 if (m_Real2Virt)
3344 mem_free(m_Real2Virt);
3345 if (m_Virt2Real)
3346 mem_free(m_Virt2Real);
3347
3348 m_ItemList = NULL;
3349 m_Real2Virt = NULL;
3350 m_Virt2Real = NULL;
3351
3352 Newui_resources.Free(m_pieces[N_PIECE_IDX]);
3353 Newui_resources.Free(m_pieces[NE_PIECE_IDX]);
3354 Newui_resources.Free(m_pieces[E_PIECE_IDX]);
3355 Newui_resources.Free(m_pieces[SE_PIECE_IDX]);
3356 Newui_resources.Free(m_pieces[S_PIECE_IDX]);
3357 Newui_resources.Free(m_pieces[SW_PIECE_IDX]);
3358 Newui_resources.Free(m_pieces[W_PIECE_IDX]);
3359 Newui_resources.Free(m_pieces[NW_PIECE_IDX]);
3360
3361 UIGadget::OnDestroy();
3362 }
3363
3364 // override: behavior when gadget is processed
3365 void newuiListBox::OnUserProcess() {}
3366
3367 // when gadget is added to a window (AddGadget is called)
3368 void newuiListBox::OnAttachToWindow() {
3369 m_up_btn.Create(m_Wnd, -1, NEWUI_ARROW_UP, NULL, m_X + m_W - m_boffs - 8, m_Y + m_boffs - 1);
3370 m_up_btn.SetFlag(UIF_NOTIFYMASTERSEL);
3371 AttachSlaveGadget(&m_up_btn);
3372
3373 m_down_btn.Create(m_Wnd, -1, NEWUI_ARROW_DOWN, NULL, m_X + m_W - m_boffs - 8, m_Y + m_H - m_boffs - 19);
3374 m_down_btn.SetFlag(UIF_NOTIFYMASTERSEL);
3375 AttachSlaveGadget(&m_down_btn);
3376 }
3377
3378 void newuiListBox::OnDetachFromWindow() {
3379 DetachSlaveGadget(&m_down_btn);
3380 m_down_btn.Destroy();
3381 DetachSlaveGadget(&m_up_btn);
3382 m_up_btn.Destroy();
3383 }
3384
3385 void newuiListBox::Offset(int16_t offs) {
3386 m_Index = m_Index + offs;
3387 if (m_Index < 0) {
3388 m_Index = 0;
3389 } else if (m_Index >= (m_NumItems - m_NumVisibleItems)) {
3390 m_Index = m_NumItems - m_NumVisibleItems;
3391 }
3392 }
3393
3394 // CLASS a new listbox. uses less memory than the old listbox hopefully.
3395
3396 newuiComboBox::newuiComboBox() {
3397 m_ItemList = NULL;
3398 m_Virt2Real = NULL;
3399 m_Real2Virt = NULL;
3400 selectchange_fn = NULL;
3401 m_NumItems = 0;
3402 m_Index = 0;
3403 }
3404
3405 void newuiComboBox::Create(UIWindow *wnd, int16_t id, int16_t x, int16_t y, uint16_t flags) {
3406 m_ItemList = NULL;
3407 m_Virt2Real = NULL;
3408 m_Real2Virt = NULL;
3409 selectchange_fn = NULL;
3410 m_NumItems = 0;
3411 m_Index = 0;
3412 m_showup = m_showdown = false;
3413
3414 m_barbmp = Newui_resources.Load(NEWUI_CB_FILE);
3415 m_boffs = 12;
3416
3417 // determine true width and height (snap width and height to 32 pixel boundaries
3418 UIGadget::Create(wnd, id, x, y, m_barbmp->width(), m_barbmp->height(), flags);
3419 }
3420
3421 // functions to add or remove items,
3422 void newuiComboBox::AddItem(const char *name) {
3423 char **temp;
3424 int i;
3425 int *tempvirt2real, *tempreal2virt;
3426 int real_index = -1;
3427
3428 if (m_Flags & UILB_NOSORT) {
3429 // insert without sort
3430
3431 if ((m_NumItems % LISTBOX_BUFFER_SIZE) == 0) {
3432 // dynamically allocated memory for listbox, expand as needed.
3433 temp = (char **)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(char *));
3434 tempvirt2real = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
3435 tempreal2virt = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
3436
3437 if (m_ItemList) {
3438 for (i = 0; i < m_NumItems; i++) {
3439 temp[i] = m_ItemList[i];
3440 tempvirt2real[i] = m_Virt2Real[i];
3441 tempreal2virt[i] = m_Real2Virt[i];
3442 }
3443
3444 mem_free(m_ItemList);
3445 mem_free(m_Virt2Real);
3446 mem_free(m_Real2Virt);
3447 }
3448 m_ItemList = temp;
3449 m_Virt2Real = tempvirt2real;
3450 m_Real2Virt = tempreal2virt;
3451 }
3452
3453 real_index = m_NumItems;
3454 } else {
3455 // only text item listboxes!
3456
3457 // insert with sorting
3458 if ((m_NumItems % LISTBOX_BUFFER_SIZE) == 0) {
3459 // dynamically allocated memory for listbox, expand as needed.
3460 int oidx = 0, cidx = 0;
3461 int pidx = 0;
3462 int result;
3463
3464 temp = (char **)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(char *));
3465 tempvirt2real = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
3466 tempreal2virt = (int *)mem_malloc((m_NumItems + LISTBOX_BUFFER_SIZE) * sizeof(int));
3467
3468 real_index = -1;
3469
3470 if (m_ItemList) {
3471 while (oidx <= m_NumItems) {
3472 if (oidx == m_NumItems) {
3473 if (real_index == -1) {
3474 real_index = oidx;
3475 } else {
3476 temp[cidx] = m_ItemList[pidx];
3477 tempreal2virt[cidx] = m_Real2Virt[pidx];
3478 }
3479 oidx++;
3480 continue;
3481 }
3482
3483 if (m_Flags & UILB_CASESENSITIVE)
3484 result = strcmp(m_ItemList[cidx], name);
3485 else
3486 result = stricmp(m_ItemList[cidx], name);
3487 if (result < 0) {
3488 // copy old
3489 temp[cidx] = m_ItemList[pidx];
3490 tempreal2virt[cidx] = m_Real2Virt[pidx];
3491 cidx++;
3492 pidx++;
3493 } else if (result >= 0) {
3494 // see if we need to insert, if not just keep copying
3495 if (real_index == -1) {
3496 real_index = cidx;
3497 cidx++;
3498 } else {
3499 temp[cidx] = m_ItemList[pidx];
3500 tempreal2virt[cidx] = m_Real2Virt[pidx];
3501 cidx++;
3502 pidx++;
3503 }
3504 }
3505 oidx++;
3506 }
3507 ASSERT(real_index != -1);
3508
3509 // now we need to fix m_Virt2Real to represent the new structure,
3510 // all values>=real_index must be incremented
3511 for (oidx = 0; oidx <= m_NumItems; oidx++) {
3512 if (oidx < m_NumItems) {
3513 tempvirt2real[oidx] = m_Virt2Real[oidx];
3514
3515 // if(oidx!=real_index){
3516 if (tempvirt2real[oidx] >= real_index)
3517 tempvirt2real[oidx]++;
3518 }
3519 }
3520
3521 mem_free(m_ItemList);
3522 mem_free(m_Virt2Real);
3523 mem_free(m_Real2Virt);
3524 } else {
3525 real_index = 0;
3526 }
3527
3528 m_ItemList = temp;
3529 m_Virt2Real = tempvirt2real;
3530 m_Real2Virt = tempreal2virt;
3531 } else {
3532 // find where to insert, without allocating memory
3533 int oidx = 0, cidx = 0;
3534 real_index = -1;
3535 int result;
3536
3537 int old_r2v;
3538 char *old_item = NULL;
3539
3540 while (oidx <= m_NumItems) {
3541
3542 if (oidx == m_NumItems) {
3543 // we're past the slot of the old buffer
3544 if (real_index == -1) {
3545 // we haven't set the real index yet, so it has to go here
3546 real_index = m_NumItems;
3547 } else {
3548 // the real index has been set, we're still pulling back
3549 m_Real2Virt[cidx] = old_r2v;
3550 m_ItemList[cidx] = old_item;
3551 }
3552 oidx++;
3553 continue;
3554 } else {
3555 if (m_Flags & UILB_CASESENSITIVE)
3556 result = strcmp(m_ItemList[cidx], name);
3557 else
3558 result = stricmp(m_ItemList[cidx], name);
3559 }
3560
3561 if (result < 0) {
3562 // we haven't gotten to the string yet, keep as they are
3563 cidx++;
3564 } else if (result >= 0) {
3565 // see if we need to insert, if not just keep copying
3566 if (real_index == -1) {
3567 // we need to insert here
3568 real_index = cidx;
3569 old_item = m_ItemList[cidx];
3570 old_r2v = m_Real2Virt[cidx];
3571 cidx++;
3572 } else {
3573 // old_item and old_r2v should be set!
3574 ASSERT(old_item);
3575 char *oi;
3576 int or2v;
3577
3578 or2v = m_Real2Virt[cidx];
3579 oi = m_ItemList[cidx];
3580
3581 m_Real2Virt[cidx] = old_r2v;
3582 m_ItemList[cidx] = old_item;
3583 old_r2v = or2v;
3584 old_item = oi;
3585
3586 cidx++;
3587 }
3588 }
3589
3590 // continue on
3591 oidx++;
3592 }
3593
3594 ASSERT(real_index != -1);
3595
3596 // now we need to fix m_Virt2Real to represent the new structure,
3597 // all values>=real_index must be incremented
3598 cidx = 0;
3599 for (oidx = 0; oidx <= m_NumItems; oidx++) {
3600 if (oidx != real_index) {
3601 if (m_Virt2Real[cidx] >= real_index)
3602 m_Virt2Real[cidx]++;
3603 cidx++;
3604 }
3605 }
3606 }
3607 }
3608
3609 // copy item passed to list.
3610 m_ItemList[real_index] = mem_strdup(name);
3611 m_Virt2Real[m_NumItems] = real_index;
3612 m_Real2Virt[real_index] = m_NumItems;
3613 m_NumItems++;
3614
3615 if (real_index <= m_Index) {
3616 // adjust selected index
3617 if (m_Index < m_NumItems - 1)
3618 m_Index++;
3619 }
3620 }
3621
3622 void newuiComboBox::RemoveItem(const char *name) {
3623 int i, j, found = -1;
3624
3625 for (i = 0; i < m_NumItems; i++) {
3626 int res;
3627 res = (m_Flags & UILB_CASESENSITIVE) ? stricmp(m_ItemList[i], name) : strcmp(m_ItemList[i], name);
3628 if (res == 0) {
3629 found = i;
3630 mem_free(m_ItemList[found]);
3631 m_ItemList[found] = NULL;
3632 break;
3633 }
3634 }
3635 if (found == -1) {
3636 mprintf(0, "newuiComboBox:: Didn't find item to remove!\n");
3637 return;
3638 }
3639
3640 // found is the real index to remove
3641 // go through virt2real and subtract one from values of found or higher
3642 // go through real2virt and move everything from index found on down, and subtract
3643 // 1 from those with value of found or higher
3644 int real_index = found; // index to remove
3645 int virt_index = m_Real2Virt[found]; // index to remove
3646 int real_thresh = virt_index;
3647 int virt_thresh = real_index;
3648
3649 for (j = 0; j < (m_NumItems - 1); j++) {
3650 if (j >= real_index) {
3651 m_ItemList[j] = m_ItemList[j + 1];
3652 m_Real2Virt[j] = m_Real2Virt[j + 1];
3653 }
3654
3655 if (j >= virt_index) {
3656 m_Virt2Real[j] = m_Virt2Real[j + 1];
3657 }
3658
3659 if (m_Real2Virt[j] >= real_thresh)
3660 m_Real2Virt[j]--;
3661
3662 if (m_Virt2Real[j] >= virt_thresh)
3663 m_Virt2Real[j]--;
3664 }
3665 m_NumItems--;
3666
3667 if (real_index <= m_Index) {
3668 // adjust selected index
3669 m_Index--;
3670 }
3671 }
3672
3673 void newuiComboBox::RemoveAll() {
3674 if (m_ItemList) {
3675 int i;
3676 for (i = 0; i < m_NumItems; i++) {
3677 if (m_ItemList[i])
3678 mem_free(m_ItemList[i]);
3679 }
3680 mem_free(m_ItemList);
3681 }
3682
3683 if (m_Virt2Real)
3684 mem_free(m_Virt2Real);
3685
3686 if (m_Real2Virt)
3687 mem_free(m_Real2Virt);
3688
3689 m_ItemList = NULL;
3690 m_Virt2Real = NULL;
3691 m_Real2Virt = NULL;
3692 m_Index = m_NumItems = 0;
3693 }
3694
3695 // item information
3696 int newuiComboBox::GetCurrentIndex() {
3697 if (m_NumItems == 0)
3698 return 0;
3699
3700 if (!m_Real2Virt)
3701 return 0;
3702
3703 int virt_index = m_Real2Virt[m_Index];
3704 ASSERT(virt_index >= 0 && virt_index < m_NumItems);
3705 return virt_index;
3706 }
3707
3708 void newuiComboBox::SetCurrentIndex(int index) {
3709 if (m_NumItems == 0)
3710 return;
3711
3712 if (index >= 0 && index < m_NumItems) {
3713 int real_index = m_Virt2Real[index], old_index = m_Index;
3714
3715 ASSERT(real_index >= 0 && real_index < m_NumItems);
3716 m_Index = real_index;
3717 if (m_Index < 0) {
3718 m_Index = 0;
3719 }
3720
3721 // call callbacks if selection really changed.
3722 if (old_index != m_Index) {
3723 if (selectchange_fn)
3724 (*selectchange_fn)(index);
3725 }
3726 }
3727 }
3728
3729 bool newuiComboBox::GetCurrentItem(char *buffer, int buflen) {
3730 return newuiComboBox::GetItem(m_Real2Virt[m_Index], buffer, buflen);
3731 }
3732
3733 bool newuiComboBox::GetItem(int index, char *buffer, int buflen) {
3734 if (m_NumItems == 0) {
3735 return false;
3736 *buffer = 0;
3737 }
3738
3739 if (index >= 0 && index < m_NumItems) {
3740 int real_index = m_Virt2Real[index];
3741 ASSERT(real_index >= 0 && real_index < m_NumItems);
3742 strncpy(buffer, m_ItemList[real_index], buflen - 1);
3743 buffer[buflen - 1] = 0;
3744 return true;
3745 }
3746
3747 return false;
3748 }
3749
3750 bool newuiComboBox::SetCurrentItem(const char *name) {
3751 int i, found = 0;
3752
3753 for (i = 0; i < m_NumItems; i++) {
3754 int res = (m_Flags & UILB_CASESENSITIVE) ? stricmp(m_ItemList[i], name) : strcmp(m_ItemList[i], name);
3755 if (res == 0) {
3756 SetCurrentIndex(m_Real2Virt[i]);
3757 found = 1;
3758 break;
3759 }
3760 }
3761
3762 if (!found) {
3763 mprintf(0, "newuiComboBox::SelectItem item not found!\n");
3764 return false;
3765 }
3766
3767 return true;
3768 }
3769
3770 // set callbacks
3771 void newuiComboBox::SetSelectChangeCallback(void (*fn)(int)) { selectchange_fn = fn; }
3772
3773 // behavior when gadget is being drawn.
3774 void newuiComboBox::OnDraw() {
3775 m_barbmp->draw(0, 0);
3776 bool auto_select = ((m_Flags & UILB_AUTOSELECT) > 0);
3777 bool use_scroll = true;
3778
3779 ui_SetTextClip(m_boffs, 1, m_W - m_boffs - m_up_btn.W(), m_H - 1);
3780
3781 // decide whether to show arrows. , tricky code.
3782 if (m_NumItems > 1) {
3783 if (m_Index < (m_NumItems - 1) && !m_showdown) {
3784 m_showdown = true;
3785 m_down_btn.Show(true);
3786 }
3787 if (m_Index > 0 && !m_showup) {
3788 m_showup = true;
3789 m_up_btn.Show(true);
3790 }
3791 }
3792
3793 if (m_Index == (m_NumItems - 1) && m_showdown) {
3794 m_showdown = false;
3795 m_down_btn.Show(false);
3796 }
3797 if (m_Index == 0 && m_showup) {
3798 m_showup = false;
3799 m_up_btn.Show(false);
3800 }
3801
3802 // use gamma 192 for non focus.
3803 if (m_NumItems && m_Index < m_NumItems && m_Index >= 0) {
3804 UITextItem text(MONITOR9_NEWUI_FONT, m_ItemList[m_Index], NEWUI_MONITORFONT_COLOR);
3805 text.draw(m_boffs + 8, 4);
3806 }
3807
3808 ui_ResetTextClip();
3809 }
3810
3811 // behavior when key is pressed.
3812 void newuiComboBox::OnKeyDown(int key) {
3813 int new_selected_index = m_Index;
3814
3815 if (key == KEY_UP) {
3816 if (m_Index > 0) {
3817 new_selected_index = m_Index - 1;
3818 }
3819 } else if (key == KEY_DOWN) {
3820 if (m_Index < (m_NumItems - 1)) {
3821 new_selected_index = m_Index + 1;
3822 }
3823 }
3824 newuiComboBox::SetCurrentIndex((m_Real2Virt) ? m_Real2Virt[new_selected_index] : 0);
3825 }
3826
3827 // this function will handle when an arrow button was pressed
3828 void newuiComboBox::OnNotifySelect(UIGadget *g) {
3829 switch (g->GetDatum()) {
3830 case NEWUI_ARROW_CBUP:
3831 newuiComboBox::OnKeyDown(KEY_UP);
3832 break;
3833 case NEWUI_ARROW_CBDOWN:
3834 newuiComboBox::OnKeyDown(KEY_DOWN);
3835 break;
3836 }
3837 }
3838
3839 // when a listbox item was double clicked (or entered)
3840 void newuiComboBox::OnSelect() { UIGadget::OnSelect(); }
3841
3842 // called when destroyed.
3843 void newuiComboBox::OnDestroy() {
3844 int i;
3845
3846 if (m_up_btn.IsCreated())
3847 m_up_btn.Destroy();
3848 if (m_down_btn.IsCreated())
3849 m_down_btn.Destroy();
3850
3851 for (i = 0; i < m_NumItems; i++) {
3852 mem_free(m_ItemList[i]);
3853 }
3854
3855 if (m_ItemList)
3856 mem_free(m_ItemList);
3857 if (m_Real2Virt)
3858 mem_free(m_Real2Virt);
3859 if (m_Virt2Real)
3860 mem_free(m_Virt2Real);
3861
3862 m_ItemList = NULL;
3863 m_Real2Virt = NULL;
3864 m_Virt2Real = NULL;
3865
3866 Newui_resources.Free(m_barbmp);
3867
3868 UIGadget::OnDestroy();
3869 }
3870
3871 // when gadget is added to a window (AddGadget is called)
3872 void newuiComboBox::OnAttachToWindow() {
3873 m_boffs = 16;
3874 m_up_btn.Create(m_Wnd, -1, NEWUI_ARROW_CBUP, NULL, m_X + 4, m_Y + 3);
3875 m_up_btn.SetFlag(UIF_NOTIFYMASTERSEL);
3876 m_showup = true;
3877 AttachSlaveGadget(&m_up_btn);
3878 // m_showup = false;
3879 // m_Wnd->RemoveGadget(&m_up_btn);
3880
3881 m_down_btn.Create(m_Wnd, -1, NEWUI_ARROW_CBDOWN, NULL, m_X + m_W - m_boffs - 8, m_Y + 3);
3882 m_down_btn.SetFlag(UIF_NOTIFYMASTERSEL);
3883 AttachSlaveGadget(&m_down_btn);
3884 m_showdown = true;
3885 // m_showdown = false;
3886 // m_Wnd->RemoveGadget(&m_down_btn);
3887 }
3888
3889 void newuiComboBox::OnDetachFromWindow() {
3890 if (m_showdown) {
3891 DetachSlaveGadget(&m_down_btn);
3892 m_showdown = false;
3893 }
3894 m_down_btn.Destroy();
3895 if (m_showup) {
3896 DetachSlaveGadget(&m_up_btn);
3897 m_showup = false;
3898 }
3899 m_up_btn.Destroy();
3900 }
3901
3902 // CLASS newuiEditBox
3903
3904 newuiEditBox::newuiEditBox() { m_title = NULL; }
3905
3906 void newuiEditBox::Create(UIWindow *wnd, int16_t id, const char *name, int16_t x, int16_t y, int16_t w, int16_t flags) {
3907 // if (name) {
3908 // m_title = new UISnazzyTextItem(0, MONITOR9_NEWUI_FONT, name, NEWUI_MONITORFONT_COLOR);
3909 // }
3910 UIEdit::Create(wnd, (int)id, x, y, w ? w : EDIT_GADGET_WIDTH, EDIT_GADGET_HEIGHT, flags | UIF_BORDER);
3911
3912 // if (m_title) {
3913 // UIEdit::SetTextBounds(m_title->width()+12, m_W-1);
3914 // }
3915 // else {
3916 UIEdit::SetTextBounds(1, m_W - 1, -1);
3917 // }
3918
3919 UIEdit::SetColor(NEWUI_MONITORFONT_COLOR);
3920 UIEdit::SetFont(MONITOR9_NEWUI_FONT);
3921
3922 m_quick_escape_enable = false;
3923 }
3924
3925 void newuiEditBox::EnableOnQuickEscapeBehavior(bool do_it) { m_quick_escape_enable = do_it; }
3926
3927 void newuiEditBox::OnDestroy() {
3928 if (m_title) {
3929 delete m_title;
3930 m_title = NULL;
3931 }
3932
3933 UIEdit::OnDestroy();
3934 }
3935
3936 void newuiEditBox::OnDraw() {
3937 ui_DrawBox(GR_RGB(128, 128, 128), 0, 0, m_W, m_H);
3938 if (m_title) {
3939 if (HasFocus()) {
3940 m_title->set_flags(UISNAZZYTEXTF_BLINKING);
3941 } else {
3942 m_title->set_flags(0);
3943 }
3944 m_title->draw(0, 0);
3945 }
3946
3947 DrawText();
3948 }
3949
3950 void newuiEditBox::OnKeyDown(int key) {
3951 UIEdit::OnKeyDown(key);
3952 if (key == KEY_ESC && m_quick_escape_enable) {
3953 UIEdit::SetText(NEWUI_EDIT_CANCELED_STR);
3954 OnSelect();
3955 }
3956 }
3957
3958 // the new classic hotspot CLASS
3959
3960 newuiHotspot::newuiHotspot() { m_title = NULL; }
3961
3962 void newuiHotspot::Create(UIWindow *wnd, int16_t id, const char *title, int16_t x, int16_t y, int16_t w, int16_t h, int16_t flags) {
3963 if (title) {
3964 m_title = mem_strdup(title);
3965 } else {
3966 m_title = NULL;
3967 }
3968
3969 m_alpha = 0;
3970 m_alpha_adjust = 0;
3971 m_mse_timer = -1.0f;
3972
3973 UIGadget::Create(wnd, id, x, y, w, h, flags);
3974 }
3975
3976 void newuiHotspot::OnDraw() {
3977 static char buf[256];
3978 char *strptr;
3979 int y = 2, x = 2;
3980
3981 textaux_WordWrap(m_title, buf, m_W - 22, MONITOR9_NEWUI_FONT);
3982
3983 strptr = strtok(buf, "\n");
3984 while (strptr) {
3985 UITextItem text(MONITOR9_NEWUI_FONT, strptr, NEWUI_MONITORFONT_COLOR);
3986 ddgr_color col =
3987 (m_alpha_adjust != 0) ? GR_RGB(160 + m_alpha, 160 + m_alpha, 0 + m_alpha) : NEWUI_MONITORFONT_COLOR;
3988 text.set_color(col);
3989 text.set_alpha((m_alpha_adjust != 0) ? 255 : 128);
3990 text.draw(x, y);
3991 y += text.height();
3992 x = 20;
3993
3994 strptr = strtok(NULL, "\n");
3995 }
3996
3997 if (m_alpha_adjust != 0) {
3998 m_alpha += m_alpha_adjust;
3999 if (m_alpha < 0) {
4000 m_alpha = 0;
4001 m_alpha_adjust = 6;
4002 } else if (m_alpha > 94) {
4003 m_alpha = 94;
4004 m_alpha_adjust = -6;
4005 }
4006 }
4007 if (!m_infocus) {
4008 m_alpha_adjust = 0;
4009 m_alpha = 0;
4010 }
4011 }
4012
4013 void newuiHotspot::OnDestroy() {
4014 if (m_title) {
4015 mem_free(m_title);
4016 m_title = NULL;
4017 }
4018
4019 UIGadget::OnDestroy();
4020 }
4021
4022 void newuiHotspot::OnMouseBtnDown(int btn) {
4023 if (btn == UILMSEBTN) {
4024 LOCK_FOCUS(this);
4025 if (m_mse_timer > -1.0f) {
4026 if ((UI_TIME() - m_mse_timer) < UI_DBLCLICK_DELAY) {
4027 if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
4028 UNLOCK_FOCUS(this);
4029 OnSelect();
4030 }
4031 }
4032 m_mse_timer = -1.0f;
4033 }
4034 }
4035 }
4036
4037 void newuiHotspot::OnMouseBtnUp(int btn) {
4038 if (btn == UILMSEBTN) {
4039 UNLOCK_FOCUS(this);
4040
4041 if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
4042 if (m_mse_timer == -1.0f) {
4043 m_mse_timer = UI_TIME();
4044 }
4045 }
4046 }
4047 }
4048
4049 void newuiHotspot::OnGainFocus() {
4050 m_alpha = 0;
4051 m_alpha_adjust = 6;
4052 }
4053
4054 void newuiHotspot::OnLostFocus() {
4055 m_alpha = 0;
4056 m_alpha_adjust = 0;
4057 }
4058
4059 ///////////////////////////////////////////////////////////////////////////////
4060 // CLASS, sizable window
4061
4062 newuiTiledWindow::newuiTiledWindow() {
4063 m_startui_cb = NULL;
4064 m_endui_cb = NULL;
4065 m_draw_cb = NULL;
4066 m_onframe_cb = NULL;
4067 m_data = NULL;
4068 m_title[0] = 0;
4069 m_realized = false;
4070 }
4071
4072 void newuiTiledWindow::Create(const char *title, int16_t x, int16_t y, int16_t w, int16_t h, int flags) {
4073 if (title) {
4074 ASSERT(strlen(title) < (sizeof(m_title) - 1));
4075 }
4076
4077 // adjust w and height acoordingly with bitmap background width and height.
4078 int bk_width = (w / TW_PIECE_WIDTH) + ((w % TW_PIECE_WIDTH) ? 1 : 0);
4079 int bk_height = (h / TW_PIECE_HEIGHT) + ((h % TW_PIECE_HEIGHT) ? 1 : 0);
4080
4081 if (bk_width < 4)
4082 bk_width = 4;
4083 if (bk_height < 4)
4084 bk_height = 4;
4085
4086 w = bk_width * TW_PIECE_WIDTH;
4087 h = bk_height * TW_PIECE_HEIGHT;
4088
4089 UIWindow::Create(x, y, w, h, flags | UIF_CENTER);
4090
4091 if (title) {
4092 strcpy(m_title, title);
4093 } else {
4094 m_title[0] = 0;
4095 }
4096
4097 // initialze stuff
4098 m_startui_cb = NULL;
4099 m_endui_cb = NULL;
4100 m_draw_cb = NULL;
4101 m_onframe_cb = NULL;
4102 m_data = NULL;
4103 m_realized = false;
4104
4105 m_sheet.Create(this, NULL, 96, NEWUI_TILED_SHEET_X, title ? NEWUI_TILED_SHEET_Y : NEWUI_TILED_TITLE_Y);
4106
4107 // graphics.
4108 m_pieces[N_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_N_FILE);
4109 m_pieces[NE_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_NE_FILE);
4110 m_pieces[E_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_E_FILE);
4111 m_pieces[SE_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_SE_FILE);
4112 m_pieces[S_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_S_FILE);
4113 m_pieces[SW_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_SW_FILE);
4114 m_pieces[W_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_W_FILE);
4115 m_pieces[NW_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_NW_FILE);
4116 m_pieces[C_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_C_FILE);
4117
4118 m_pieces[TL_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_TITLE_L_FILE);
4119 m_pieces[TC_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_TITLE_C_FILE);
4120 m_pieces[TR_PIECE_IDX] = Newui_resources.Load(NEWUI_WIN_TITLE_R_FILE);
4121 }
4122
4123 // grab a newui interface from it.
4124 newuiSheet *newuiTiledWindow::GetSheet() { return &m_sheet; }
4125
4126 // do user interface on it.
4127 int newuiTiledWindow::DoUI() {
4128 // start ui.
4129 UI_frame_result = -1;
4130 ui_ShowCursor();
4131
4132 if (m_startui_cb) {
4133 (*m_startui_cb)(this, m_data);
4134 }
4135
4136 if (!m_realized) {
4137 m_sheet.Realize();
4138 m_realized = true;
4139 }
4140
4141 // refreshes gadgets on current sheet.
4142 m_sheet.UpdateChanges();
4143
4144 // this should poll UI_frame_result.
4145 while (UI_frame_result == -1) {
4146
4147 Descent->defer();
4148 DoUIFrame();
4149 if (m_onframe_cb) {
4150 (*m_onframe_cb)(this, m_data);
4151 }
4152 if (m_draw_cb) {
4153 ui_StartDraw(m_X, m_Y, m_X + m_W, m_Y + m_H);
4154 (*m_draw_cb)(this, m_data);
4155 ui_EndDraw();
4156 ui_DoCursor();
4157 }
4158 rend_Flip();
4159 }
4160
4161 // refreshes gadgets on current sheet.
4162 m_sheet.UpdateReturnValues();
4163
4164 if (m_endui_cb) {
4165 (*m_endui_cb)(this, m_data);
4166 }
4167
4168 // cleanup
4169 ui_HideCursor();
4170 ui_Flush();
4171
4172 return UI_frame_result;
4173 }
4174
4175 void newuiTiledWindow::SetData(void *data) { m_data = data; }
4176
4177 // set callbacks
4178 // first called when DoUI is called.
4179 void newuiTiledWindow::SetOnStartUICB(void (*fn)(newuiTiledWindow *, void *)) { m_startui_cb = fn; }
4180
4181 // called when DoUI returns
4182 void newuiTiledWindow::SetOnEndUICB(void (*fn)(newuiTiledWindow *, void *)) { m_endui_cb = fn; }
4183
4184 // called before rend_Flip but after ui has drawn. (SetUICallback is before ui has been rendered)
4185 void newuiTiledWindow::SetOnDrawCB(void (*fn)(newuiTiledWindow *, void *)) { m_draw_cb = fn; }
4186
4187 // called after DoUIFrame is called. for immediate results.
4188 void newuiTiledWindow::SetOnUIFrameCB(void (*fn)(newuiTiledWindow *, void *)) { m_onframe_cb = fn; }
4189
4190 #include "newui.h"
4191
4192 // Returns the index of the tile bitmap to draw along the top of the window
4193 int newuiTiledWindow::GetTopTileIndex(int tx, int window_width) {
4194 int title_width, left, right;
4195
4196 // Get size of the title
4197 int flags = GetFlags();
4198 switch (NEWUI_GETTITLEBAR(flags)) {
4199 case NUWF_TITLENONE:
4200 return N_PIECE_IDX;
4201 case NUWF_TITLESMALL:
4202 title_width = 2;
4203 break;
4204 case NUWF_TITLEMED:
4205 title_width = 4;
4206 break;
4207 case NUWF_TITLELARGE:
4208 title_width = 6;
4209 break;
4210 }
4211
4212 // If one width is odd and other even, then add an extra tile to the title
4213 if ((window_width ^ title_width) & 1)
4214 title_width++;
4215
4216 // Get positions of left & right edges
4217 left = window_width / 2 - title_width / 2 - 1;
4218 right = left + title_width + 1;
4219
4220 // Clip to edges
4221 if (left < 0)
4222 left = 0;
4223 if (right >= window_width)
4224 right = window_width - 1;
4225
4226 // Bail if no room for title
4227 if (left >= right)
4228 return N_PIECE_IDX;
4229
4230 if (tx == left)
4231 return TL_PIECE_IDX;
4232 else if (tx == right)
4233 return TR_PIECE_IDX;
4234 else if ((tx > left) && (tx < right))
4235 return TC_PIECE_IDX;
4236 else
4237 return N_PIECE_IDX;
4238 }
4239
4240 void newuiTiledWindow::OnDraw() {
4241 // draw tiles.
4242 int tx, ty;
4243 int bk_width = (m_W / TW_PIECE_WIDTH) + ((m_W % TW_PIECE_WIDTH) ? 1 : 0);
4244 int bk_height = (m_H / TW_PIECE_HEIGHT) + ((m_H % TW_PIECE_HEIGHT) ? 1 : 0);
4245 int corner_tw = TW_CORNER_WIDTH / TW_PIECE_WIDTH;
4246 int corner_th = TW_CORNER_HEIGHT / TW_PIECE_HEIGHT;
4247
4248 // fill background
4249 for (ty = 1; ty < (bk_height - 1); ty++) {
4250 for (tx = 1; tx < (bk_width - 1); tx++) {
4251 m_pieces[C_PIECE_IDX]->draw(tx * TW_PIECE_WIDTH, ty * TW_PIECE_HEIGHT);
4252 }
4253 }
4254
4255 // do sides
4256 for (ty = corner_th; ty < (bk_height - corner_th); ty++) {
4257 m_pieces[W_PIECE_IDX]->draw(0, ty * TW_PIECE_HEIGHT);
4258 m_pieces[E_PIECE_IDX]->draw((bk_width - 1) * TW_PIECE_WIDTH, ty * TW_PIECE_HEIGHT);
4259 }
4260
4261 for (tx = corner_tw; tx < (bk_width - corner_tw); tx++) {
4262
4263 m_pieces[GetTopTileIndex(tx - corner_tw, bk_width - 2 * corner_tw)]->draw(tx * TW_PIECE_WIDTH, 0);
4264 m_pieces[S_PIECE_IDX]->draw(tx * TW_PIECE_WIDTH, (bk_height - 1) * TW_PIECE_HEIGHT);
4265 }
4266
4267 // do corners.
4268 m_pieces[NW_PIECE_IDX]->draw(0, 0);
4269 m_pieces[NE_PIECE_IDX]->draw((bk_width - corner_tw) * TW_PIECE_WIDTH, 0);
4270 m_pieces[SE_PIECE_IDX]->draw((bk_width - corner_tw) * TW_PIECE_WIDTH, (bk_height - corner_th) * TW_PIECE_HEIGHT);
4271 m_pieces[SW_PIECE_IDX]->draw(0, (bk_height - corner_th) * TW_PIECE_HEIGHT);
4272
4273 // do title
4274 UITextItem text(MONITOR15_NEWUI_FONT, m_title, NEWUI_MONITORFONT_COLOR);
4275 text.draw(NEWUI_TILED_TITLE_X, NEWUI_TILED_TITLE_Y);
4276 }
4277
4278 void newuiTiledWindow::OnDestroy() {
4279 Newui_resources.Free(m_pieces[N_PIECE_IDX]);
4280 Newui_resources.Free(m_pieces[NE_PIECE_IDX]);
4281 Newui_resources.Free(m_pieces[E_PIECE_IDX]);
4282 Newui_resources.Free(m_pieces[SE_PIECE_IDX]);
4283 Newui_resources.Free(m_pieces[S_PIECE_IDX]);
4284 Newui_resources.Free(m_pieces[SW_PIECE_IDX]);
4285 Newui_resources.Free(m_pieces[W_PIECE_IDX]);
4286 Newui_resources.Free(m_pieces[NW_PIECE_IDX]);
4287 Newui_resources.Free(m_pieces[C_PIECE_IDX]);
4288
4289 Newui_resources.Free(m_pieces[TL_PIECE_IDX]);
4290 Newui_resources.Free(m_pieces[TC_PIECE_IDX]);
4291 Newui_resources.Free(m_pieces[TR_PIECE_IDX]);
4292
4293 m_startui_cb = NULL;
4294 m_endui_cb = NULL;
4295 m_draw_cb = NULL;
4296 m_onframe_cb = NULL;
4297 m_data = NULL;
4298
4299 m_sheet.Destroy();
4300
4301 UIWindow::OnDestroy();
4302 }