| 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/PictListBox.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:57:38 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Picture list box |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 2 8/12/97 10:48p Matt |
| 30 | * Force redraw when SelectItem() is called, though this causes a double |
| 31 | * redraw when a picture of an item is clicked on. |
| 32 | * |
| 33 | * 8 5/28/97 6:16p Samir |
| 34 | * When NextListItem returns the same itemnum as before, we are done also. |
| 35 | * |
| 36 | * 7 4/25/97 11:52a Samir |
| 37 | * Took out annoying mprintfs |
| 38 | * |
| 39 | * 6 4/01/97 3:26p Samir |
| 40 | * Now when scrolling up, always refresh if at zero the get any |
| 41 | * undisplayed objects. |
| 42 | * |
| 43 | * 5 3/25/97 6:30p Samir |
| 44 | * Fixed problem when no items in listbox |
| 45 | * |
| 46 | * 4 3/21/97 11:21a Samir |
| 47 | * Fixed scroll up problems when adding new poweups. |
| 48 | * |
| 49 | * 3 2/28/97 3:05p Samir |
| 50 | * Moved StartIterateItems to before checking size of list. |
| 51 | * |
| 52 | * 2 2/21/97 6:24p Samir |
| 53 | * Fixed scrolling probs. |
| 54 | * |
| 55 | * $NoKeywords: $ |
| 56 | */ |
| 57 | |
| 58 | #include "stdafx.h" |
| 59 | #include "PictListBox.h" |
| 60 | |
| 61 | #include "d3edit.h" |
| 62 | #include "pserror.h" |
| 63 | #include "gr.h" |
| 64 | |
| 65 | // Construction and destruction |
| 66 | |
| 67 | editorPictListBox::editorPictListBox() { |
| 68 | m_ItemStart = 0; |
| 69 | m_ListPos = 0; |
| 70 | m_ItemsPerRow = 0; |
| 71 | m_ItemsMaxRow = 0; |
| 72 | m_ItemsMax = 0; |
| 73 | m_PageLen = 0; |
| 74 | m_Redraw = 0; |
| 75 | |
| 76 | m_CheckForSelect = 0; |
| 77 | m_CheckX = m_CheckY = 0; |
| 78 | |
| 79 | m_ItemSelected = -1; |
| 80 | m_OldItemCount = -1; |
| 81 | m_ParentWnd = NULL; |
| 82 | } |
| 83 | |
| 84 | // Initialization |
| 85 | |
| 86 | void editorPictListBox::Create(CWnd *parent, int static_id, int scroll_id, int item_dim, int init_item) { |
| 87 | m_SurfDim = item_dim; |
| 88 | m_ParentWnd = parent; |
| 89 | |
| 90 | m_StaticID = static_id; |
| 91 | m_ScrollbarID = scroll_id; |
| 92 | m_ItemSelected = init_item; |
| 93 | } |
| 94 | |
| 95 | // Operations |
| 96 | |
| 97 | void editorPictListBox::Invalidate() { |
| 98 | if (!m_ParentWnd) |
| 99 | return; |
| 100 | m_Redraw = 1; // force a complete redraw of the listbox |
| 101 | Update(); |
| 102 | } |
| 103 | |
| 104 | // Listbox handlers |
| 105 | // updates the display of the listbox if needed. |
| 106 | |
| 107 | void editorPictListBox::SelectItem(int item) { |
| 108 | if (!m_ParentWnd) |
| 109 | return; |
| 110 | m_ItemSelected = item; |
| 111 | OnItemSelected(item); |
| 112 | |
| 113 | // Need to redraw to show new selected item |
| 114 | Invalidate(); |
| 115 | } |
| 116 | |
| 117 | void editorPictListBox::Update() { |
| 118 | RECT rect; |
| 119 | int do_update; |
| 120 | |
| 121 | if (!m_ParentWnd) |
| 122 | return; |
| 123 | |
| 124 | // create necessary graphics objects |
| 125 | Desktop_surf->attach_to_window((unsigned)m_ParentWnd->GetDlgItem(m_StaticID)->m_hWnd); |
| 126 | m_ParentWnd->GetDlgItem(m_StaticID)->GetClientRect(&m_ListRect); |
| 127 | ::CopyRect(&rect, &m_ListRect); |
| 128 | |
| 129 | // determine if we need to update the list box. |
| 130 | StartIterateListItems(); |
| 131 | |
| 132 | if (m_OldItemCount == m_ItemsMax && !m_Redraw) { |
| 133 | do_update = 0; |
| 134 | } else { |
| 135 | do_update = 1; |
| 136 | m_Redraw = 0; |
| 137 | m_OldItemCount = m_ItemsMax; |
| 138 | } |
| 139 | |
| 140 | // draw listbox |
| 141 | if (do_update) { |
| 142 | int itemnum = m_ItemStart, count = 0, q, r; |
| 143 | int left, top; |
| 144 | bool done = 0; |
| 145 | |
| 146 | Desktop_surf->clear(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); |
| 147 | m_ItemSurf.create(m_SurfDim, m_SurfDim, BPP_16); |
| 148 | m_ItemSurfSel.create(m_SurfDim + 8, m_SurfDim + 8, BPP_16); |
| 149 | |
| 150 | // determine number of contents that can fit in the listbox. |
| 151 | q = rect.right - rect.left; |
| 152 | r = rect.bottom - rect.top; |
| 153 | m_ItemsPerRow = q / (m_SurfDim + 8); |
| 154 | m_ItemsMaxRow = r / (m_SurfDim + 8); |
| 155 | ASSERT(m_ItemsPerRow && m_ItemsMaxRow); // No no no no... |
| 156 | |
| 157 | left = (q / 2) - ((m_ItemsPerRow * (m_SurfDim + 8)) / 2); |
| 158 | top = (r / 2) - ((m_ItemsMaxRow * (m_SurfDim + 8)) / 2); |
| 159 | |
| 160 | if (m_ItemsMax == 0) |
| 161 | done = 1; // if no items, then we just wont draw. |
| 162 | |
| 163 | // okay draw each item. |
| 164 | while (!done) { |
| 165 | int x, y; |
| 166 | int olditemnum; |
| 167 | |
| 168 | x = left + rect.left + ((count % m_ItemsPerRow) * (m_SurfDim + 8)); |
| 169 | y = top + rect.top + ((count / m_ItemsPerRow) * (m_SurfDim + 8)); |
| 170 | |
| 171 | if (m_CheckForSelect) { |
| 172 | // note that if we've detected a switch in textures, we need to redraw the box |
| 173 | // yet again to prevent multiple selected textures. This should be okay, since |
| 174 | // m_CheckForTextureList is cleared. |
| 175 | if (m_CheckX >= x && m_CheckX <= (x + 48) && m_CheckY >= y && m_CheckY <= (y + 48)) { |
| 176 | m_CheckForSelect = 0; |
| 177 | SelectItem(itemnum); |
| 178 | m_Redraw = 1; |
| 179 | Update(); |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | olditemnum = itemnum; |
| 185 | DrawItem(x, y, itemnum); |
| 186 | itemnum = NextListItem(itemnum); |
| 187 | |
| 188 | // repeated itemnum, means there is no more. |
| 189 | if (itemnum == olditemnum) |
| 190 | done = 1; |
| 191 | else |
| 192 | count++; |
| 193 | |
| 194 | if (count == m_ItemsPerRow * m_ItemsMaxRow) |
| 195 | done = 1; |
| 196 | if (itemnum <= m_ItemStart) |
| 197 | done = 1; |
| 198 | } |
| 199 | |
| 200 | m_ItemSurfSel.free(); |
| 201 | m_ItemSurf.free(); |
| 202 | |
| 203 | // must define the vertical scroll bar dimensions |
| 204 | CScrollBar *bar = (CScrollBar *)m_ParentWnd->GetDlgItem(m_ScrollbarID); |
| 205 | SCROLLINFO si; |
| 206 | |
| 207 | m_PageLen = m_ItemsMaxRow; //(count/m_ItemsPerRow)+ ((count%m_ItemsPerRow) ? 1 : 0); |
| 208 | m_ListPos = bar->GetScrollPos(); |
| 209 | si.cbSize = sizeof(si); |
| 210 | si.fMask = SIF_ALL; |
| 211 | si.nMin = 0; |
| 212 | si.nMax = (m_ItemsMax / m_ItemsPerRow) + ((m_ItemsMax % m_ItemsPerRow) ? 1 : 0) - 1; |
| 213 | si.nPos = m_ListPos; |
| 214 | si.nPage = m_PageLen; |
| 215 | bar->SetScrollInfo(&si); |
| 216 | bar->ShowScrollBar(TRUE); |
| 217 | } |
| 218 | |
| 219 | // restore graphics state |
| 220 | Desktop_surf->attach_to_window((unsigned)NULL); |
| 221 | } |
| 222 | |
| 223 | // Handlers called by external source |
| 224 | |
| 225 | void editorPictListBox::OnVScroll(UINT nSBCode, int nPos, CScrollBar *pScrollBar) { |
| 226 | int id = -1, min, max; |
| 227 | int new_pos = 0; |
| 228 | |
| 229 | if (!m_ParentWnd) |
| 230 | return; |
| 231 | |
| 232 | ASSERT(pScrollBar == (CScrollBar *)m_ParentWnd->GetDlgItem(m_ScrollbarID)); |
| 233 | |
| 234 | if (pScrollBar) { |
| 235 | id = pScrollBar->GetDlgCtrlID(); |
| 236 | pScrollBar->GetScrollRange(&min, &max); |
| 237 | |
| 238 | switch (nSBCode) { |
| 239 | case SB_LINEDOWN: |
| 240 | new_pos = m_ListPos + 1; |
| 241 | break; |
| 242 | case SB_LINEUP: |
| 243 | new_pos = m_ListPos - 1; |
| 244 | break; |
| 245 | case SB_THUMBPOSITION: |
| 246 | new_pos = nPos; |
| 247 | break; |
| 248 | case SB_THUMBTRACK: |
| 249 | new_pos = nPos; |
| 250 | break; |
| 251 | case SB_PAGEDOWN: |
| 252 | new_pos = m_ListPos + m_PageLen; |
| 253 | break; |
| 254 | case SB_PAGEUP: |
| 255 | new_pos = m_ListPos - m_PageLen; |
| 256 | break; |
| 257 | default: |
| 258 | new_pos = m_ListPos; |
| 259 | } |
| 260 | |
| 261 | if (new_pos < 0) |
| 262 | new_pos = 0; |
| 263 | if (new_pos > (max - m_PageLen)) |
| 264 | new_pos = (max - m_PageLen) + 1; |
| 265 | if (new_pos == 0) |
| 266 | ListUp((m_ListPos - new_pos) + 1); // list up until start of list. prevents missing items |
| 267 | if (new_pos != m_ListPos) { |
| 268 | // ok, new_pos will be an index into the listbox, NOT the texture list. |
| 269 | // so we will get the number of texture rows we went up or down and change |
| 270 | // tex_start accordingly. |
| 271 | SCROLLINFO si; |
| 272 | |
| 273 | // mprintf(0, "scroll new_pos = %d. original_pos = %d\n", new_pos, m_ListPos); |
| 274 | if (new_pos < m_ListPos) |
| 275 | ListUp(m_ListPos - new_pos); |
| 276 | if (new_pos > m_ListPos) |
| 277 | ListDown(new_pos - m_ListPos); |
| 278 | |
| 279 | m_ListPos = new_pos; |
| 280 | |
| 281 | si.cbSize = sizeof(si); |
| 282 | si.fMask = SIF_POS; |
| 283 | si.nPos = m_ListPos; |
| 284 | pScrollBar->SetScrollInfo(&si); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void editorPictListBox::OnLButtonDown(CPoint point) { |
| 290 | if (!m_ParentWnd) |
| 291 | return; |
| 292 | |
| 293 | m_CheckForSelect = 1; |
| 294 | m_CheckX = point.x; |
| 295 | m_CheckY = point.y; |
| 296 | |
| 297 | Invalidate(); |
| 298 | } |
| 299 | |
| 300 | void editorPictListBox::ListUp(int rows) { |
| 301 | int old = m_ItemStart; |
| 302 | int done = 0, count = 0; |
| 303 | |
| 304 | while ((count < (m_ItemsPerRow * rows)) && !done) { |
| 305 | int olditem; |
| 306 | |
| 307 | olditem = m_ItemStart; |
| 308 | m_ItemStart = PrevListItem(m_ItemStart); |
| 309 | // mprintf(0, "m_ItemStart = %d\n", m_ItemStart); |
| 310 | if (m_ItemStart > olditem) { |
| 311 | m_ItemStart = olditem; |
| 312 | done = 1; |
| 313 | // mprintf(0, "m_ItemStart = %d\n", m_ItemStart); |
| 314 | } else if (old <= m_ItemStart) { |
| 315 | m_ItemStart = old; |
| 316 | done = 1; |
| 317 | } else |
| 318 | count++; |
| 319 | } |
| 320 | |
| 321 | Invalidate(); |
| 322 | } |
| 323 | |
| 324 | void editorPictListBox::ListDown(int rows) { |
| 325 | int old = m_ItemStart; |
| 326 | int done = 0, count = 0; |
| 327 | |
| 328 | while ((count < (m_ItemsPerRow * rows)) && !done) { |
| 329 | m_ItemStart = NextListItem(m_ItemStart); |
| 330 | |
| 331 | if (old >= m_ItemStart) { |
| 332 | m_ItemStart = old; |
| 333 | done = 1; |
| 334 | } else |
| 335 | count++; |
| 336 | } |
| 337 | |
| 338 | Invalidate(); |
| 339 | } |