| 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/ui/UICombo.cpp $ |
| 21 | * $Revision: 3 $ |
| 22 | * $Date: 4/14/99 1:52a $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/ui/UICombo.cpp $ |
| 28 | * |
| 29 | * 3 4/14/99 1:52a Jeff |
| 30 | * fixed case mismatched #includes |
| 31 | * |
| 32 | * 2 3/03/99 2:45p Samir |
| 33 | * use arrows up and down for combo boxes. |
| 34 | * |
| 35 | * 1 9/02/98 6:49p Samir |
| 36 | * New combo box |
| 37 | * |
| 38 | * $NoKeywords: $ |
| 39 | */ |
| 40 | |
| 41 | #include "UIlib.h" |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | // UIComboBox |
| 45 | // A listbox may contain text items, BUT only one will be visible |
| 46 | // Allows the user to scroll through them too. |
| 47 | |
| 48 | UIComboBox::UIComboBox() { |
| 49 | m_ItemList = NULL; |
| 50 | m_nItems = 0; |
| 51 | m_Index = 0; |
| 52 | m_ShowUp = false; |
| 53 | m_ShowDown = false; |
| 54 | selectchange_fn = NULL; |
| 55 | } |
| 56 | |
| 57 | UIComboBox::~UIComboBox() {} |
| 58 | |
| 59 | // creates a combo box |
| 60 | void UIComboBox::Create(UIWindow *parent, int id, int x, int y, int w, int h, int flags) { |
| 61 | m_ItemList = NULL; |
| 62 | m_nItems = 0; |
| 63 | m_Index = 0; |
| 64 | m_ShowUp = false; |
| 65 | m_ShowDown = false; |
| 66 | selectchange_fn = NULL; |
| 67 | m_scrolltimer = 0.0f; |
| 68 | m_scrollstage = 0; |
| 69 | |
| 70 | UIGadget::Create(parent, id, x, y, w, h, flags); |
| 71 | |
| 72 | m_CX = 1; |
| 73 | m_CY = 1; |
| 74 | m_CX2 = m_W - 1; |
| 75 | m_CY2 = m_H - 1; |
| 76 | } |
| 77 | |
| 78 | void UIComboBox::AddItem(const UIItem *item) { |
| 79 | int i; |
| 80 | |
| 81 | if ((m_nItems % LISTBOX_BUFFER_SIZE) == 0) { |
| 82 | // dynamically allocated memory for listbox, expand as needed. |
| 83 | UIItem **temp = new UIItem *[m_nItems + LISTBOX_BUFFER_SIZE]; |
| 84 | if (m_ItemList) { |
| 85 | for (i = 0; i < m_nItems; i++) |
| 86 | temp[i] = m_ItemList[i]; |
| 87 | delete[] m_ItemList; |
| 88 | } |
| 89 | m_ItemList = temp; |
| 90 | } |
| 91 | |
| 92 | // copy item passed to list. |
| 93 | m_ItemList[m_nItems++] = (UIItem *)item; |
| 94 | } |
| 95 | |
| 96 | // removes an item from the list. |
| 97 | void UIComboBox::RemoveItem(const UIItem *item) { |
| 98 | int i, j; |
| 99 | |
| 100 | for (i = 0; i < m_nItems; i++) { |
| 101 | if (item == m_ItemList[i]) { |
| 102 | for (j = i; j < (m_nItems - 1); j++) |
| 103 | m_ItemList[j] = m_ItemList[j + 1]; |
| 104 | m_nItems--; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // empties combobox |
| 111 | void UIComboBox::RemoveAll() { m_Index = m_nItems = 0; } |
| 112 | |
| 113 | // selects the given item in the list. |
| 114 | void UIComboBox::SelectItem(const UIItem *item) { |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < m_nItems; i++) { |
| 118 | if (item == m_ItemList[i]) { |
| 119 | SetSelectedIndex(i); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // returns the item at listbox index given. |
| 126 | UIItem *UIComboBox::GetItem(int index) const { return (index < m_nItems) ? m_ItemList[index] : NULL; } |
| 127 | |
| 128 | // sets the selected index. |
| 129 | void UIComboBox::SetSelectedIndex(int index) { |
| 130 | if (index < m_nItems) { |
| 131 | m_Index = index; |
| 132 | if (selectchange_fn) |
| 133 | (*selectchange_fn)(index); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void UIComboBox::SetSelectChangeCallback(void (*fn)(int)) { selectchange_fn = fn; } |
| 138 | |
| 139 | // called from outside gadget hierarchy. |
| 140 | |
| 141 | void UIComboBox::OnDestroy() { |
| 142 | UIGadget::OnDestroy(); |
| 143 | |
| 144 | if (m_ItemList) |
| 145 | delete[] m_ItemList; |
| 146 | |
| 147 | m_ItemList = NULL; |
| 148 | } |
| 149 | |
| 150 | // behavior when gadget is being drawn. |
| 151 | void UIComboBox::OnDraw() { |
| 152 | UITextItem uarrow; |
| 153 | UITextItem darrow; |
| 154 | char arrowbuf[2]; |
| 155 | |
| 156 | if (m_Flags & UIF_BORDER) |
| 157 | ui_DrawBox(GR_WHITE, 0, 0, m_W, m_H); |
| 158 | |
| 159 | // draw all items in box that can be visible. |
| 160 | int x = m_CX; |
| 161 | int y = m_CY; |
| 162 | |
| 163 | if (!m_nItems) |
| 164 | return; |
| 165 | |
| 166 | snprintf(arrowbuf, sizeof(arrowbuf), "%c", UI_UP_ARROW_CHAR); |
| 167 | uarrow = UITextItem(arrowbuf, m_ItemList[m_Index]->get_color(), 255); |
| 168 | snprintf(arrowbuf, sizeof(arrowbuf), "%c", UI_DOWN_ARROW_CHAR); |
| 169 | darrow = UITextItem(arrowbuf, m_ItemList[m_Index]->get_color(), 255); |
| 170 | |
| 171 | ui_SetTextClip(m_CX, m_CY, m_CX2, m_CY2); |
| 172 | |
| 173 | // reset these |
| 174 | m_ShowUp = m_ShowDown = false; |
| 175 | m_ArrY = y; |
| 176 | m_ArrH = uarrow.height(); |
| 177 | |
| 178 | if (m_Index > 0) { |
| 179 | m_UpArrX = m_CX; |
| 180 | m_UpArrW = uarrow.width(); |
| 181 | uarrow.draw(m_UpArrX, m_ArrY); |
| 182 | m_ShowUp = true; |
| 183 | } |
| 184 | if (m_Index < (m_nItems - 1)) { |
| 185 | m_DownArrX = m_CX2 - darrow.width(); |
| 186 | m_DownArrW = darrow.width(); |
| 187 | darrow.draw(m_DownArrX, m_ArrY); |
| 188 | m_ShowDown = true; |
| 189 | } |
| 190 | |
| 191 | // draw current item. |
| 192 | x = m_CX + uarrow.width() + 2; |
| 193 | ui_SetTextClip(x, m_CY, m_CX2 - darrow.width() - 2, m_CY2); |
| 194 | m_ItemList[m_Index]->draw(x, y); |
| 195 | |
| 196 | ui_ResetTextClip(); |
| 197 | } |
| 198 | |
| 199 | // behavior when key is pressed. |
| 200 | void UIComboBox::OnKeyDown(int key) { |
| 201 | if (key == KEY_UP) { |
| 202 | if (m_Index > 0) { |
| 203 | UIComboBox::SetSelectedIndex(m_Index - 1); |
| 204 | } |
| 205 | } else if (key == KEY_DOWN) { |
| 206 | if (m_Index < (m_nItems - 1)) { |
| 207 | UIComboBox::SetSelectedIndex(m_Index + 1); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // behavior when mouse button is pressed. |
| 213 | void UIComboBox::OnMouseBtnDown(int btn) { |
| 214 | if (btn == UILMSEBTN) { |
| 215 | int mx = SCREEN_TO_GAD_X(this, UI_input.mx); |
| 216 | int my = SCREEN_TO_GAD_Y(this, UI_input.my); |
| 217 | |
| 218 | // determine whether to accept mouse input at this time. |
| 219 | if (m_scrollstage == 0) { |
| 220 | m_scrollstage = 1; |
| 221 | } else if (m_scrollstage == 1) { |
| 222 | if ((m_scrolltimer - UI_TIME()) < KEYDOWN_FIRST_DELAY) |
| 223 | return; |
| 224 | m_scrollstage = 2; |
| 225 | } else if (m_scrollstage == 2) { |
| 226 | if ((m_scrolltimer - UI_TIME()) < KEYDOWN_REPEAT_DELAY) |
| 227 | return; |
| 228 | } |
| 229 | m_scrolltimer = UI_TIME(); |
| 230 | |
| 231 | // do mouse processing. |
| 232 | if (m_ShowUp && PT_IN_RECT(mx, my, m_UpArrX, m_ArrY, m_UpArrW, m_ArrH)) { |
| 233 | if (m_Index > 0) |
| 234 | UIComboBox::SetSelectedIndex(m_Index - 1); |
| 235 | } else if (m_ShowDown && PT_IN_RECT(mx, my, m_DownArrX, m_ArrY, m_DownArrW, m_ArrH)) { |
| 236 | if (m_Index < (m_nItems - 1)) |
| 237 | UIComboBox::SetSelectedIndex(m_Index + 1); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // behavior when mouse button is released. |
| 243 | void UIComboBox::OnMouseBtnUp(int btn) { |
| 244 | if (btn == UILMSEBTN) { |
| 245 | m_scrollstage = 0; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void UIComboBox::OnSelect() { UIGadget::OnSelect(); } |