| 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/editor/GrListBox.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:57:38 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * New graphical listbox |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 8 6/10/98 1:57p Samir |
| 30 | * fixed some listbox problems. |
| 31 | * |
| 32 | * 7 6/10/98 12:18p Matt |
| 33 | * Added hack to fix bug until Samir can fix it for real. |
| 34 | * |
| 35 | * 6 2/10/98 3:16p Craig |
| 36 | * Samir |
| 37 | * |
| 38 | * 5 9/30/97 5:35p Samir |
| 39 | * Fixed hangup bug when masking out textures. |
| 40 | * |
| 41 | * 4 8/26/97 11:39a Samir |
| 42 | * Check editor_active to see whether editor graphics are active or not. |
| 43 | * |
| 44 | * 3 8/26/97 10:49a Samir |
| 45 | * Fixed some small bugs with the scroll bar. |
| 46 | * |
| 47 | * $NoKeywords: $ |
| 48 | */ |
| 49 | |
| 50 | // GrListBox.cpp : implementation file |
| 51 | // |
| 52 | |
| 53 | #include "stdafx.h" |
| 54 | #include "editor.h" |
| 55 | #include "GrListBox.h" |
| 56 | |
| 57 | #ifdef _DEBUG |
| 58 | #define new DEBUG_NEW |
| 59 | #undef THIS_FILE |
| 60 | static char THIS_FILE[] = __FILE__; |
| 61 | #endif |
| 62 | |
| 63 | ///////////////////////////////////////////////////////////////////////////// |
| 64 | // CGrListBox |
| 65 | |
| 66 | CGrListBox::CGrListBox() { |
| 67 | items_max_row = 0; |
| 68 | items_per_row = 0; |
| 69 | m_ListLen = 0; |
| 70 | m_ListItem = 0; |
| 71 | m_SurfDimension = 0; |
| 72 | m_ItemSel = -1; // -1 because item number can be 0 and a valid item. |
| 73 | m_HitX = 0; |
| 74 | m_HitY = 0; |
| 75 | m_CheckHit = false; |
| 76 | m_IsItemSelInBox = false; |
| 77 | } |
| 78 | |
| 79 | CGrListBox::~CGrListBox() {} |
| 80 | |
| 81 | BOOL CGrListBox::Create(CWnd *parent_wnd, int id, const RECT &rect, int surf_dim, bool selbox) { |
| 82 | m_SurfDimension = surf_dim; |
| 83 | m_SelectionBox = selbox; |
| 84 | |
| 85 | return CWnd::Create(NULL, "GrListBox", WS_CHILD | WS_VISIBLE | WS_VSCROLL, rect, parent_wnd, id); |
| 86 | } |
| 87 | |
| 88 | void CGrListBox::SelectItem(int item) { |
| 89 | // don't do a damn thing if the item selected equals the requested selected item |
| 90 | if (item != m_ItemSel) { |
| 91 | m_ItemSel = item; |
| 92 | OnItemSelected(item); |
| 93 | ListBoxProc(false); // update list box data but don't draw yet. |
| 94 | |
| 95 | // scroll window until item wanted is in view. |
| 96 | if (m_IsItemSelInBox == false) { |
| 97 | // SAMIR: fix this!! |
| 98 | int count = 0; |
| 99 | |
| 100 | while (m_ListItem < m_ItemSel && (++count < 100)) |
| 101 | if (RowDown(1, false)) |
| 102 | break; |
| 103 | |
| 104 | while (m_ListItem > m_ItemSel && (++count < 100)) |
| 105 | if (RowUp(1, false)) |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | CWnd::Invalidate(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void CGrListBox::SetItemFirst(int item) { m_ListItem = item; } |
| 114 | |
| 115 | int CGrListBox::GetItemFirst() const { return m_ListItem; } |
| 116 | |
| 117 | BEGIN_MESSAGE_MAP(CGrListBox, CWnd) |
| 118 | //{{AFX_MSG_MAP(CGrListBox) |
| 119 | ON_WM_LBUTTONDOWN() |
| 120 | ON_WM_PAINT() |
| 121 | ON_WM_VSCROLL() |
| 122 | //}}AFX_MSG_MAP |
| 123 | END_MESSAGE_MAP() |
| 124 | |
| 125 | ///////////////////////////////////////////////////////////////////////////// |
| 126 | // CGrListBox message handlers |
| 127 | |
| 128 | void CGrListBox::OnPaint() { |
| 129 | CPaintDC dc(this); // device context for painting |
| 130 | |
| 131 | ListBoxProc(true); |
| 132 | } |
| 133 | |
| 134 | void CGrListBox::OnLButtonDown(UINT nFlags, CPoint point) { |
| 135 | int old_sel; |
| 136 | |
| 137 | m_CheckHit = true; |
| 138 | m_HitX = point.x; |
| 139 | m_HitY = point.y; |
| 140 | |
| 141 | // should we have to redraw box if we haven't changed a selection? |
| 142 | old_sel = m_ItemSel; |
| 143 | ListBoxProc(false); |
| 144 | if (old_sel != m_ItemSel) |
| 145 | CWnd::Invalidate(FALSE); |
| 146 | |
| 147 | CWnd::OnLButtonDown(nFlags, point); |
| 148 | } |
| 149 | |
| 150 | void CGrListBox::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar) { |
| 151 | |
| 152 | SCROLLINFO si; |
| 153 | int cur_pos, new_pos; |
| 154 | |
| 155 | GetScrollInfo(SB_VERT, &si, SIF_ALL); |
| 156 | cur_pos = m_ScrollPos; |
| 157 | |
| 158 | switch (nSBCode) { |
| 159 | case SB_LINEDOWN: |
| 160 | new_pos = cur_pos + 1; |
| 161 | break; |
| 162 | case SB_LINEUP: |
| 163 | new_pos = cur_pos - 1; |
| 164 | break; |
| 165 | case SB_THUMBPOSITION: |
| 166 | new_pos = nPos; |
| 167 | break; |
| 168 | case SB_THUMBTRACK: |
| 169 | new_pos = nPos; |
| 170 | break; |
| 171 | case SB_PAGEDOWN: |
| 172 | new_pos = cur_pos + si.nPage; |
| 173 | break; |
| 174 | case SB_PAGEUP: |
| 175 | new_pos = cur_pos - si.nPage; |
| 176 | break; |
| 177 | default: |
| 178 | new_pos = cur_pos; |
| 179 | } |
| 180 | |
| 181 | if (new_pos < 0) |
| 182 | new_pos = 0; |
| 183 | if (new_pos > (int)(si.nMax - si.nPage + 1)) |
| 184 | new_pos = (int)(si.nMax - si.nPage + 1); |
| 185 | // if (new_pos == 0) RowUp((cur_pos-new_pos)+1); // list up until start of list. prevents missing items |
| 186 | if (new_pos != cur_pos) { |
| 187 | // ok, new_pos will be an index into the listbox, NOT the texture list. |
| 188 | // so we will get the number of texture rows we went up or down and change |
| 189 | // tex_start accordingly. |
| 190 | SCROLLINFO si; |
| 191 | |
| 192 | // mprintf(0, "scroll new_pos = %d. original_pos = %d\n", new_pos, cur_pos); |
| 193 | if (new_pos < cur_pos) |
| 194 | RowUp(cur_pos - new_pos); |
| 195 | if (new_pos > cur_pos) |
| 196 | RowDown(new_pos - cur_pos); |
| 197 | |
| 198 | cur_pos = new_pos; |
| 199 | |
| 200 | si.cbSize = sizeof(si); |
| 201 | si.fMask = SIF_POS; |
| 202 | si.nPos = m_ScrollPos = cur_pos; |
| 203 | CWnd::SetScrollInfo(SB_VERT, &si); |
| 204 | } |
| 205 | |
| 206 | CWnd::OnVScroll(nSBCode, nPos, pScrollBar); |
| 207 | } |
| 208 | |
| 209 | ///////////////////////////////////////////////////////////////////////////// |
| 210 | // internal functions |
| 211 | |
| 212 | // these functions move the list pointer (m_ListItem) up or down a few rows. |
| 213 | bool CGrListBox::RowUp(int rows, bool draw) { |
| 214 | int old = m_ListItem; |
| 215 | int done = 0, count = 0; |
| 216 | bool retval = false; |
| 217 | |
| 218 | while ((count < (items_per_row * rows)) && !done) { |
| 219 | int olditem; |
| 220 | |
| 221 | if (m_ListItem == m_ItemSel) |
| 222 | retval = true; |
| 223 | |
| 224 | olditem = m_ListItem; |
| 225 | m_ListItem = ListPrevItem(m_ListItem); |
| 226 | // mprintf(0, "m_ListItem = %d\n", m_ListItem); |
| 227 | if (m_ListItem > olditem) { |
| 228 | m_ListItem = olditem; |
| 229 | done = 1; |
| 230 | // mprintf(0, "m_ListItem = %d\n", m_ListItem); |
| 231 | } else if (old <= m_ListItem) { |
| 232 | m_ListItem = old; |
| 233 | done = 1; |
| 234 | } else |
| 235 | count++; |
| 236 | } |
| 237 | |
| 238 | if (draw) |
| 239 | CWnd::Invalidate(); |
| 240 | |
| 241 | return retval; |
| 242 | } |
| 243 | |
| 244 | bool CGrListBox::RowDown(int rows, bool draw) { |
| 245 | int old = m_ListItem; |
| 246 | int done = 0, count = 0; |
| 247 | bool retval = false; |
| 248 | |
| 249 | while ((count < (items_per_row * rows)) && !done) { |
| 250 | if (m_ListItem == m_ItemSel) |
| 251 | retval = true; |
| 252 | |
| 253 | m_ListItem = ListNextItem(m_ListItem); |
| 254 | |
| 255 | if (old >= m_ListItem) { |
| 256 | m_ListItem = old; |
| 257 | done = 1; |
| 258 | } else |
| 259 | count++; |
| 260 | } |
| 261 | |
| 262 | if (draw) |
| 263 | CWnd::Invalidate(); |
| 264 | |
| 265 | return retval; |
| 266 | } |
| 267 | |
| 268 | int CGrListBox::CalcRowOfItem(int item) { |
| 269 | int curitem = 0, oldcuritem = 0; |
| 270 | int column = 0; |
| 271 | int row = 0; |
| 272 | |
| 273 | do { |
| 274 | curitem = ListNextItem(curitem); |
| 275 | column++; |
| 276 | if (column == items_per_row) { |
| 277 | row++; |
| 278 | column = 0; |
| 279 | } |
| 280 | if (oldcuritem >= curitem) |
| 281 | return 0; |
| 282 | oldcuritem = curitem; |
| 283 | } while (curitem != item); |
| 284 | |
| 285 | return row; |
| 286 | } |
| 287 | |
| 288 | // draws all the items in the list, or don't draw. invoking DrawItem and uses the list management functions |
| 289 | void CGrListBox::ListBoxProc(bool do_draw) { |
| 290 | static bool in_listboxproc = false; |
| 291 | grHardwareSurface m_ItemSurf; |
| 292 | grHardwareSurface m_ItemSurfSel; |
| 293 | RECT box_rect; |
| 294 | int left, top, draw_x, draw_y; |
| 295 | int box_width, box_height; |
| 296 | int num_items, item; |
| 297 | |
| 298 | // don't draw if editor is in background. |
| 299 | if (in_listboxproc) |
| 300 | return; |
| 301 | in_listboxproc = true; |
| 302 | if (!Editor_active) |
| 303 | do_draw = false; |
| 304 | |
| 305 | // initialize graphic objects |
| 306 | CWnd::GetClientRect(&box_rect); |
| 307 | box_height = box_rect.bottom - box_rect.top; |
| 308 | box_width = box_rect.right - box_rect.left; |
| 309 | |
| 310 | if (do_draw) { |
| 311 | Desktop_surf->attach_to_window((unsigned)CWnd::m_hWnd); |
| 312 | Desktop_surf->clear(0, 0, box_width, box_height); |
| 313 | m_ItemSurf.create(m_SurfDimension, m_SurfDimension, BPP_16); |
| 314 | m_ItemSurfSel.create(m_SurfDimension + 8, m_SurfDimension + 8, BPP_16); |
| 315 | } |
| 316 | |
| 317 | // initialize list manager. |
| 318 | items_per_row = box_width / (m_SurfDimension + 8); |
| 319 | items_max_row = box_height / (m_SurfDimension + 8); |
| 320 | m_ListLen = this->ListEnumerate(); |
| 321 | m_ListItem = (m_ListItem / items_per_row) * items_per_row; // align rows correctly |
| 322 | m_ListItem = this->ListFirstItem(m_ListItem); |
| 323 | |
| 324 | // start drawing items in list box. |
| 325 | left = (box_width / 2) - (items_per_row * (m_SurfDimension + 8)) / 2; |
| 326 | top = (box_height / 2) - (items_max_row * (m_SurfDimension + 8)) / 2; |
| 327 | num_items = 0; |
| 328 | item = m_ListItem; |
| 329 | m_IsItemSelInBox = false; // this will turn true during the drawing loop |
| 330 | |
| 331 | // drawing and item selection check all purpose loop |
| 332 | // if our start list function returned a -1, this means that there are no list items to draw! |
| 333 | // also stop if we've exceeded the number of items that can exist in the limited box of visible |
| 334 | // space. |
| 335 | while ((m_ListItem > -1) && (num_items < (items_per_row * items_max_row))) { |
| 336 | int previtem; |
| 337 | |
| 338 | // draw at draw_x, draw_y. go to next item in list and check if we are at end of list. |
| 339 | draw_x = left + ((num_items % items_per_row) * (m_SurfDimension + 8)); |
| 340 | draw_y = top + ((num_items / items_per_row) * (m_SurfDimension + 8)); |
| 341 | |
| 342 | previtem = item; |
| 343 | |
| 344 | if (m_CheckHit) |
| 345 | if (m_HitX >= draw_x && m_HitX <= (draw_x + m_SurfDimension + 8) && m_HitY >= draw_y && |
| 346 | m_HitY <= (draw_y + m_SurfDimension + 8)) { |
| 347 | m_ItemSel = item; |
| 348 | OnItemSelected(m_ItemSel); |
| 349 | m_CheckHit = false; |
| 350 | } |
| 351 | |
| 352 | if (m_ItemSel == item) |
| 353 | m_IsItemSelInBox = true; |
| 354 | |
| 355 | if (do_draw) { |
| 356 | if (m_ItemSel == item) { |
| 357 | if (m_SelectionBox) { |
| 358 | grViewport vp(&m_ItemSurfSel); |
| 359 | vp.clear(); |
| 360 | vp.lock(); |
| 361 | vp.rect(GR_RGB(255, 0, 255), 0, 0, vp.width() - 1, vp.height() - 1); |
| 362 | vp.rect(GR_RGB(255, 0, 255), 1, 1, vp.width() - 2, vp.height() - 2); |
| 363 | vp.rect(GR_RGB(255, 0, 255), 2, 2, vp.width() - 3, vp.height() - 3); |
| 364 | vp.unlock(); |
| 365 | Desktop_surf->blt(draw_x - 4, draw_y - 4, vp.get_parent()); |
| 366 | this->DrawItem(draw_x, draw_y, &m_ItemSurf, item); |
| 367 | } else { |
| 368 | this->DrawItem(draw_x - 4, draw_y - 4, &m_ItemSurfSel, item); |
| 369 | } |
| 370 | } else |
| 371 | this->DrawItem(draw_x, draw_y, &m_ItemSurf, item); |
| 372 | } |
| 373 | |
| 374 | item = this->ListNextItem(item); |
| 375 | |
| 376 | // break if we hit end of list. |
| 377 | if (item <= previtem) |
| 378 | break; |
| 379 | |
| 380 | num_items++; |
| 381 | } |
| 382 | |
| 383 | // update the scroll bar |
| 384 | // free up any graphic resources initialized here. |
| 385 | if (do_draw) { |
| 386 | SCROLLINFO si; |
| 387 | |
| 388 | si.cbSize = sizeof(si); |
| 389 | si.fMask = SIF_ALL; |
| 390 | si.nMin = 0; |
| 391 | si.nMax = (m_ListLen / items_per_row) + ((m_ListLen % items_per_row) ? 0 : -1); |
| 392 | si.nPos = m_ScrollPos = CalcRowOfItem(m_ListItem); |
| 393 | si.nPage = (si.nMax >= items_max_row) ? items_max_row : (si.nMax + 1); |
| 394 | CWnd::SetScrollInfo(SB_VERT, &si); |
| 395 | CWnd::ShowScrollBar(SB_VERT); |
| 396 | |
| 397 | m_ItemSurfSel.free(); |
| 398 | m_ItemSurf.free(); |
| 399 | |
| 400 | Desktop_surf->attach_to_window(NULL); |
| 401 | } |
| 402 | |
| 403 | in_listboxproc = false; |
| 404 | } |