Files
@ r28035:f86971abe639
Branch filter:
Location: cpp/openttd-patchpack/source/src/widgets/dropdown.cpp - annotation
r28035:f86971abe639
16.7 KiB
text/x-c
Update: Translations from eints
english (au): 3 changes by krysclarke
russian: 3 changes by Ln-Wolf
finnish: 3 changes by hpiirai
turkish: 4 changes by BeratSJ
french: 3 changes by ottdfevr
portuguese: 3 changes by azulcosta
portuguese (brazilian): 3 changes by pasantoro
english (au): 3 changes by krysclarke
russian: 3 changes by Ln-Wolf
finnish: 3 changes by hpiirai
turkish: 4 changes by BeratSJ
french: 3 changes by ottdfevr
portuguese: 3 changes by azulcosta
portuguese (brazilian): 3 changes by pasantoro
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r9111:983de9c5a848 r8284:d769847bf11d r8284:d769847bf11d r17611:e84f76d88e45 r8284:d769847bf11d r8284:d769847bf11d r26575:d5725bf82346 r27089:0c098eaacf35 r27089:0c098eaacf35 r8284:d769847bf11d r8284:d769847bf11d r18670:f122c356353c r18670:f122c356353c r21383:942c32fb8b0e r21383:942c32fb8b0e r8284:d769847bf11d r27942:f7389062d120 r8284:d769847bf11d r9859:8c15dba58099 r9859:8c15dba58099 r9859:8c15dba58099 r26575:d5725bf82346 r26575:d5725bf82346 r26575:d5725bf82346 r8284:d769847bf11d r8284:d769847bf11d r27635:9224ce9d4476 r27635:9224ce9d4476 r27635:9224ce9d4476 r27635:9224ce9d4476 r9868:7ffba1f0658a r9868:7ffba1f0658a r27082:fad11ce90187 r9868:7ffba1f0658a r9868:7ffba1f0658a r27942:f7389062d120 r8905:b08a099aa4ea r26575:d5725bf82346 r26575:d5725bf82346 r8284:d769847bf11d r8284:d769847bf11d r17537:af2bdb4ae259 r17537:af2bdb4ae259 r17537:af2bdb4ae259 r17537:af2bdb4ae259 r17537:af2bdb4ae259 r17537:af2bdb4ae259 r17537:af2bdb4ae259 r23606:675adf28a8f7 r17537:af2bdb4ae259 r27635:9224ce9d4476 r27635:9224ce9d4476 r27193:e816892ba57b r17537:af2bdb4ae259 r17537:af2bdb4ae259 r27635:9224ce9d4476 r23444:b4ced115dd3f r23444:b4ced115dd3f r25298:c9da7a666403 r23444:b4ced115dd3f r23444:b4ced115dd3f r28032:f968ebef2274 r23444:b4ced115dd3f r24597:afde5721a3b6 r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r27635:9224ce9d4476 r23444:b4ced115dd3f r23444:b4ced115dd3f r27942:f7389062d120 r23444:b4ced115dd3f r23444:b4ced115dd3f r26575:d5725bf82346 r26575:d5725bf82346 r26549:82dacf005b0a r26549:82dacf005b0a r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r23444:b4ced115dd3f r13466:b5361acecf68 r13466:b5361acecf68 r18673:e5a4490b411b r18673:e5a4490b411b r18673:e5a4490b411b r15781:98918f8b75ee r13466:b5361acecf68 r9189:27aa95601b1c r9189:27aa95601b1c r20280:ca1fc41725ff r23607:36c15679007d r13466:b5361acecf68 r21976:26aa86f2583e r27861:6269475166e0 r13466:b5361acecf68 r11832:0a9347eb1597 r13464:5625aba8a05a r9189:27aa95601b1c r19390:275f04088f5c r23601:0de2754ecd7e r13423:9a05f0d857d1 r13464:5625aba8a05a r8284:d769847bf11d r13466:b5361acecf68 r13464:5625aba8a05a r13466:b5361acecf68 r15769:4a935f419828 r8284:d769847bf11d r15610:623a23fb6560 r15610:623a23fb6560 r13465:e55b80703856 r13465:e55b80703856 r13465:e55b80703856 r13465:e55b80703856 r13466:b5361acecf68 r13466:b5361acecf68 r13465:e55b80703856 r13465:e55b80703856 r13465:e55b80703856 r13464:5625aba8a05a r23601:0de2754ecd7e r23601:0de2754ecd7e r9189:27aa95601b1c r28026:b046b2f1f539 r21512:7f9115eeac2f r13466:b5361acecf68 r13466:b5361acecf68 r20280:ca1fc41725ff r13465:e55b80703856 r18673:e5a4490b411b r15769:4a935f419828 r18178:79f9c0c80aff r18673:e5a4490b411b r26575:d5725bf82346 r13466:b5361acecf68 r13465:e55b80703856 r18673:e5a4490b411b r15781:98918f8b75ee r15781:98918f8b75ee r18673:e5a4490b411b r13465:e55b80703856 r20280:ca1fc41725ff r18667:3b72643a9c00 r13465:e55b80703856 r13466:b5361acecf68 r13466:b5361acecf68 r23601:0de2754ecd7e r28032:f968ebef2274 r13466:b5361acecf68 r13466:b5361acecf68 r13466:b5361acecf68 r27278:7b609c53d377 r27278:7b609c53d377 r13466:b5361acecf68 r27476:bb4a1b3a079d r13465:e55b80703856 r13465:e55b80703856 r13465:e55b80703856 r13465:e55b80703856 r13465:e55b80703856 r9189:27aa95601b1c r8284:d769847bf11d r27991:0d8a4a945cd0 r9189:27aa95601b1c r25564:c875d92c537a r19390:275f04088f5c r25564:c875d92c537a r19390:275f04088f5c r27476:bb4a1b3a079d r27476:bb4a1b3a079d r27476:bb4a1b3a079d r27476:bb4a1b3a079d r27549:0ef4db72c8cf r27549:0ef4db72c8cf r27549:0ef4db72c8cf r27549:0ef4db72c8cf r9189:27aa95601b1c r9189:27aa95601b1c r27570:dc26d10816f1 r27475:997d62d70d7c r27623:94a2107d67c7 r27623:94a2107d67c7 r27623:94a2107d67c7 r27623:94a2107d67c7 r27475:997d62d70d7c r27475:997d62d70d7c r27942:f7389062d120 r13466:b5361acecf68 r13466:b5361acecf68 r13466:b5361acecf68 r13466:b5361acecf68 r15610:623a23fb6560 r15610:623a23fb6560 r23023:7b8669afd1db r13466:b5361acecf68 r13466:b5361acecf68 r9209:81a7f349571d r9189:27aa95601b1c r9209:81a7f349571d r9189:27aa95601b1c r26575:d5725bf82346 r26575:d5725bf82346 r15769:4a935f419828 r9189:27aa95601b1c r23601:0de2754ecd7e r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r28032:f968ebef2274 r9189:27aa95601b1c r9189:27aa95601b1c r9859:8c15dba58099 r9209:81a7f349571d r9209:81a7f349571d r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9209:81a7f349571d r8284:d769847bf11d r8284:d769847bf11d r25564:c875d92c537a r13466:b5361acecf68 r18673:e5a4490b411b r8284:d769847bf11d r21048:d56aeab2a78a r9189:27aa95601b1c r26575:d5725bf82346 r26575:d5725bf82346 r15769:4a935f419828 r23601:0de2754ecd7e r28032:f968ebef2274 r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r8284:d769847bf11d r26575:d5725bf82346 r13466:b5361acecf68 r26575:d5725bf82346 r8284:d769847bf11d r26575:d5725bf82346 r8284:d769847bf11d r9859:8c15dba58099 r26575:d5725bf82346 r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r13466:b5361acecf68 r8284:d769847bf11d r27942:f7389062d120 r9189:27aa95601b1c r18673:e5a4490b411b r9209:81a7f349571d r9209:81a7f349571d r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r8284:d769847bf11d r27089:0c098eaacf35 r27089:0c098eaacf35 r27089:0c098eaacf35 r23139:92b491888865 r27273:9a9e1c0c58b8 r14365:ecbc00cb8995 r27089:0c098eaacf35 r27089:0c098eaacf35 r8284:d769847bf11d r25564:c875d92c537a r9189:27aa95601b1c r9189:27aa95601b1c r25564:c875d92c537a r16313:e16357d6a8ef r25564:c875d92c537a r27476:bb4a1b3a079d r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9209:81a7f349571d r8284:d769847bf11d r9189:27aa95601b1c r9189:27aa95601b1c r9869:49fc50b6a2ef r25564:c875d92c537a r9869:49fc50b6a2ef r9869:49fc50b6a2ef r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r9189:27aa95601b1c r8301:31a3e7192fcd r9209:81a7f349571d r8284:d769847bf11d r8284:d769847bf11d r14365:ecbc00cb8995 r14365:ecbc00cb8995 r14365:ecbc00cb8995 r14365:ecbc00cb8995 r9189:27aa95601b1c r8284:d769847bf11d r9189:27aa95601b1c r8284:d769847bf11d r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r23601:0de2754ecd7e r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r27083:acaaba32287a r8284:d769847bf11d r27477:0e7e07a401f2 r8284:d769847bf11d r8284:d769847bf11d r12362:52c009ca8320 r8905:b08a099aa4ea r19391:4991ef1b399a r26545:565df956f8a0 r10009:58150b3a405f r27083:acaaba32287a r10009:58150b3a405f r9868:7ffba1f0658a r23184:4b89576dcddd r23184:4b89576dcddd r8905:b08a099aa4ea r23601:0de2754ecd7e r28032:f968ebef2274 r27083:acaaba32287a r8905:b08a099aa4ea r8905:b08a099aa4ea r27083:acaaba32287a r26575:d5725bf82346 r23184:4b89576dcddd r8284:d769847bf11d r8284:d769847bf11d r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r26663:213587401e02 r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r26663:213587401e02 r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r23184:4b89576dcddd r22609:d838b25565dd r23601:0de2754ecd7e r22609:d838b25565dd r27501:dd464f57aeb0 r27501:dd464f57aeb0 r8905:b08a099aa4ea r22609:d838b25565dd r23184:4b89576dcddd r18178:79f9c0c80aff r23184:4b89576dcddd r22609:d838b25565dd r23184:4b89576dcddd r23184:4b89576dcddd r26575:d5725bf82346 r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r27083:acaaba32287a r10009:58150b3a405f r22201:5ce5b50935d0 r23184:4b89576dcddd r23601:0de2754ecd7e r23185:e82e165bf633 r23185:e82e165bf633 r23185:e82e165bf633 r23185:e82e165bf633 r23185:e82e165bf633 r8284:d769847bf11d r8284:d769847bf11d r15610:623a23fb6560 r19391:4991ef1b399a r19391:4991ef1b399a r23601:0de2754ecd7e r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r27083:acaaba32287a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r27083:acaaba32287a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r25403:b9d22f53dd41 r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r26575:d5725bf82346 r19391:4991ef1b399a r26575:d5725bf82346 r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r27083:acaaba32287a r19391:4991ef1b399a r19391:4991ef1b399a r19391:4991ef1b399a r15610:623a23fb6560 r18845:66bf168f1100 r13423:9a05f0d857d1 r13423:9a05f0d857d1 r13423:9a05f0d857d1 r18845:66bf168f1100 r20784:dabe717891cf r20784:dabe717891cf r27083:acaaba32287a r13423:9a05f0d857d1 r27737:728d55b97775 r8284:d769847bf11d r23601:0de2754ecd7e r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r28022:a8a8bac7b6cf r8284:d769847bf11d r8284:d769847bf11d r8284:d769847bf11d r23601:0de2754ecd7e r8284:d769847bf11d | /*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file dropdown.cpp Implementation of the dropdown widget. */
#include "../stdafx.h"
#include "../window_gui.h"
#include "../string_func.h"
#include "../strings_func.h"
#include "../window_func.h"
#include "../zoom_func.h"
#include "../timer/timer.h"
#include "../timer/timer_window.h"
#include "dropdown_type.h"
#include "dropdown_widget.h"
#include "../safeguards.h"
void DropDownListItem::Draw(const Rect &r, bool, Colours bg_colour) const
{
int c1 = _colour_gradient[bg_colour][3];
int c2 = _colour_gradient[bg_colour][7];
int mid = CenterBounds(r.top, r.bottom, 0);
GfxFillRect(r.left, mid - WidgetDimensions::scaled.bevel.bottom, r.right, mid - 1, c1);
GfxFillRect(r.left, mid, r.right, mid + WidgetDimensions::scaled.bevel.top - 1, c2);
}
DropDownListStringItem::DropDownListStringItem(StringID string, int result, bool masked) : DropDownListItem(result, masked), string(GetString(string))
{
}
uint DropDownListStringItem::Width() const
{
return GetStringBoundingBox(this->String()).width + WidgetDimensions::scaled.dropdowntext.Horizontal();
}
void DropDownListStringItem::Draw(const Rect &r, bool sel, Colours) const
{
Rect ir = r.Shrink(WidgetDimensions::scaled.dropdowntext);
DrawString(ir.left, ir.right, r.top, this->String(), sel ? TC_WHITE : TC_BLACK);
}
/**
* Natural sorting comparator function for DropDownList::sort().
* @param first Left side of comparison.
* @param second Right side of comparison.
* @return true if \a first precedes \a second.
* @warning All items in the list need to be derivates of DropDownListStringItem.
*/
/* static */ bool DropDownListStringItem::NatSortFunc(std::unique_ptr<const DropDownListItem> const &first, std::unique_ptr<const DropDownListItem> const &second)
{
std::string str1 = static_cast<const DropDownListStringItem*>(first.get())->String();
std::string str2 = static_cast<const DropDownListStringItem*>(second.get())->String();
return StrNaturalCompare(str1, str2) < 0;
}
DropDownListIconItem::DropDownListIconItem(SpriteID sprite, PaletteID pal, StringID string, int result, bool masked) : DropDownListStringItem(string, result, masked), sprite(sprite), pal(pal)
{
this->dim = GetSpriteSize(sprite);
this->sprite_y = dim.height;
}
uint DropDownListIconItem::Height() const
{
return std::max(this->dim.height, (uint)FONT_HEIGHT_NORMAL);
}
uint DropDownListIconItem::Width() const
{
return DropDownListStringItem::Width() + this->dim.width + WidgetDimensions::scaled.hsep_wide;
}
void DropDownListIconItem::Draw(const Rect &r, bool sel, Colours) const
{
bool rtl = _current_text_dir == TD_RTL;
Rect ir = r.Shrink(WidgetDimensions::scaled.dropdowntext);
Rect tr = ir.Indent(this->dim.width + WidgetDimensions::scaled.hsep_normal, rtl);
DrawSprite(this->sprite, this->pal, ir.WithWidth(this->dim.width, rtl).left, CenterBounds(r.top, r.bottom, this->sprite_y));
DrawString(tr.left, tr.right, CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL), this->String(), sel ? TC_WHITE : TC_BLACK);
}
void DropDownListIconItem::SetDimension(Dimension d)
{
this->dim = d;
}
static const NWidgetPart _nested_dropdown_menu_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_END, WID_DM_ITEMS), SetMinimalSize(1, 1), SetScrollbar(WID_DM_SCROLL), EndContainer(),
NWidget(NWID_SELECTION, INVALID_COLOUR, WID_DM_SHOW_SCROLL),
NWidget(NWID_VSCROLLBAR, COLOUR_END, WID_DM_SCROLL),
EndContainer(),
EndContainer(),
};
static WindowDesc _dropdown_desc(
WDP_MANUAL, nullptr, 0, 0,
WC_DROPDOWN_MENU, WC_NONE,
WDF_NO_FOCUS,
std::begin(_nested_dropdown_menu_widgets), std::end(_nested_dropdown_menu_widgets)
);
/** Drop-down menu window */
struct DropdownWindow : Window {
int parent_button; ///< Parent widget number where the window is dropped from.
const DropDownList list; ///< List with dropdown menu items.
int selected_index; ///< Index of the selected item in the list.
byte click_delay; ///< Timer to delay selection.
bool drag_mode;
bool instant_close; ///< Close the window when the mouse button is raised.
int scrolling; ///< If non-zero, auto-scroll the item list (one time).
Point position; ///< Position of the topleft corner of the window.
Scrollbar *vscroll;
/**
* Create a dropdown menu.
* @param parent Parent window.
* @param list Dropdown item list.
* @param selected Index of the selected item in the list.
* @param button Widget of the parent window doing the dropdown.
* @param instant_close Close the window when the mouse button is raised.
* @param position Topleft position of the dropdown menu window.
* @param size Size of the dropdown menu window.
* @param wi_colour Colour of the parent widget.
* @param scroll Dropdown menu has a scrollbar.
*/
DropdownWindow(Window *parent, DropDownList &&list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll)
: Window(&_dropdown_desc), list(std::move(list))
{
assert(!this->list.empty());
this->position = position;
this->CreateNestedTree();
this->vscroll = this->GetScrollbar(WID_DM_SCROLL);
uint items_width = size.width - (scroll ? NWidgetScrollbar::GetVerticalDimension().width : 0);
NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_DM_ITEMS);
nwi->SetMinimalSizeAbsolute(items_width, size.height + WidgetDimensions::scaled.fullbevel.Vertical() * 2);
nwi->colour = wi_colour;
nwi = this->GetWidget<NWidgetCore>(WID_DM_SCROLL);
nwi->colour = wi_colour;
this->GetWidget<NWidgetStacked>(WID_DM_SHOW_SCROLL)->SetDisplayedPlane(scroll ? 0 : SZSP_NONE);
this->FinishInitNested(0);
CLRBITS(this->flags, WF_WHITE_BORDER);
/* Total length of list */
int list_height = 0;
for (const auto &item : this->list) {
list_height += item->Height();
}
/* Capacity is the average number of items visible */
this->vscroll->SetCapacity(size.height * this->list.size() / list_height);
this->vscroll->SetCount(this->list.size());
this->parent = parent;
this->parent_button = button;
this->selected_index = selected;
this->click_delay = 0;
this->drag_mode = true;
this->instant_close = instant_close;
}
void Close([[maybe_unused]] int data = 0) override
{
/* Finish closing the dropdown, so it doesn't affect new window placement.
* Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
this->Window::Close();
Point pt = _cursor.pos;
pt.x -= this->parent->left;
pt.y -= this->parent->top;
this->parent->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
/* Set flag on parent widget to indicate that we have just closed. */
NWidgetCore *nwc = this->parent->GetWidget<NWidgetCore>(this->parent_button);
if (nwc != nullptr) SetBit(nwc->disp_flags, NDB_DROPDOWN_CLOSED);
}
void OnFocusLost(bool closing) override
{
if (!closing) {
this->instant_close = false;
this->Close();
}
}
Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
{
return this->position;
}
/**
* Find the dropdown item under the cursor.
* @param[out] value Selected item, if function returns \c true.
* @return Cursor points to a dropdown item.
*/
bool GetDropDownItem(int &value)
{
if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
const Rect &r = this->GetWidget<NWidgetBase>(WID_DM_ITEMS)->GetCurrentRect().Shrink(WidgetDimensions::scaled.fullbevel);
int y = _cursor.pos.y - this->top - r.top - WidgetDimensions::scaled.fullbevel.top;
int pos = this->vscroll->GetPosition();
for (const auto &item : this->list) {
/* Skip items that are scrolled up */
if (--pos >= 0) continue;
int item_height = item->Height();
if (y < item_height) {
if (item->masked || !item->Selectable()) return false;
value = item->result;
return true;
}
y -= item_height;
}
return false;
}
void DrawWidget(const Rect &r, int widget) const override
{
if (widget != WID_DM_ITEMS) return;
Colours colour = this->GetWidget<NWidgetCore>(widget)->colour;
Rect ir = r.Shrink(WidgetDimensions::scaled.fullbevel).Shrink(RectPadding::zero, WidgetDimensions::scaled.fullbevel);
int y = ir.top;
int pos = this->vscroll->GetPosition();
for (const auto &item : this->list) {
int item_height = item->Height();
/* Skip items that are scrolled up */
if (--pos >= 0) continue;
if (y + item_height - 1 <= ir.bottom) {
bool selected = (this->selected_index == item->result);
if (selected) GfxFillRect(ir.left, y, ir.right, y + item_height - 1, PC_BLACK);
item->Draw({ir.left, y, ir.right, y + item_height - 1}, selected, colour);
if (item->masked) {
GfxFillRect(ir.left, y, ir.right, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER);
}
}
y += item_height;
}
}
void OnClick([[maybe_unused]] Point pt, int widget, [[maybe_unused]] int click_count) override
{
if (widget != WID_DM_ITEMS) return;
int item;
if (this->GetDropDownItem(item)) {
this->click_delay = 4;
this->selected_index = item;
this->SetDirty();
}
}
/** Rate limit how fast scrolling happens. */
IntervalTimer<TimerWindow> scroll_interval = {std::chrono::milliseconds(30), [this](auto) {
if (this->scrolling == 0) return;
if (this->vscroll->UpdatePosition(this->scrolling)) this->SetDirty();
this->scrolling = 0;
}};
void OnMouseLoop() override
{
if (this->click_delay != 0 && --this->click_delay == 0) {
/* Close the dropdown, so it doesn't affect new window placement.
* Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
this->Close();
this->parent->OnDropdownSelect(this->parent_button, this->selected_index);
return;
}
if (this->drag_mode) {
int item;
if (!_left_button_clicked) {
this->drag_mode = false;
if (!this->GetDropDownItem(item)) {
if (this->instant_close) this->Close();
return;
}
this->click_delay = 2;
} else {
if (_cursor.pos.y <= this->top + 2) {
/* Cursor is above the list, set scroll up */
this->scrolling = -1;
return;
} else if (_cursor.pos.y >= this->top + this->height - 2) {
/* Cursor is below list, set scroll down */
this->scrolling = 1;
return;
}
if (!this->GetDropDownItem(item)) return;
}
if (this->selected_index != item) {
this->selected_index = item;
this->SetDirty();
}
}
}
};
/**
* Show a drop down list.
* @param w Parent window for the list.
* @param list Prepopulated DropDownList.
* @param selected The initially selected list item.
* @param button The widget which is passed to Window::OnDropdownSelect and OnDropdownClose.
* Unless you override those functions, this should be then widget index of the dropdown button.
* @param wi_rect Coord of the parent drop down button, used to position the dropdown menu.
* @param instant_close Set to true if releasing mouse button should close the
* list regardless of where the cursor is.
*/
void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, int button, Rect wi_rect, Colours wi_colour, bool instant_close)
{
CloseWindowByClass(WC_DROPDOWN_MENU);
/* The preferred position is just below the dropdown calling widget */
int top = w->top + wi_rect.bottom + 1;
/* The preferred width equals the calling widget */
uint width = wi_rect.Width();
/* Longest item in the list */
uint max_item_width = 0;
/* Total height of list */
uint height = 0;
for (const auto &item : list) {
height += item->Height();
max_item_width = std::max(max_item_width, item->Width());
}
max_item_width += WidgetDimensions::scaled.fullbevel.Horizontal();
/* Scrollbar needed? */
bool scroll = false;
/* Is it better to place the dropdown above the widget? */
bool above = false;
/* Available height below (or above, if the dropdown is placed above the widget). */
uint available_height = std::max(GetMainViewBottom() - top - (int)WidgetDimensions::scaled.fullbevel.Vertical() * 2, 0);
/* If the dropdown doesn't fully fit below the widget... */
if (height > available_height) {
uint available_height_above = std::max(w->top + wi_rect.top - GetMainViewTop() - (int)WidgetDimensions::scaled.fullbevel.Vertical() * 2, 0);
/* Put the dropdown above if there is more available space. */
if (available_height_above > available_height) {
above = true;
available_height = available_height_above;
}
/* If the dropdown doesn't fully fit, we need a dropdown. */
if (height > available_height) {
scroll = true;
uint avg_height = height / (uint)list.size();
/* Fit the list; create at least one row, even if there is no height available. */
uint rows = std::max<uint>(available_height / avg_height, 1);
height = rows * avg_height;
/* Add space for the scrollbar. */
max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
}
/* Set the top position if needed. */
if (above) {
top = w->top + wi_rect.top - height - WidgetDimensions::scaled.fullbevel.Vertical() * 2;
}
}
width = std::max(width, max_item_width);
Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - (int)width : wi_rect.left), top};
Dimension dw_size = {width, height};
DropdownWindow *dropdown = new DropdownWindow(w, std::move(list), selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
/* The dropdown starts scrolling downwards when opening it towards
* the top and holding down the mouse button. It can be fooled by
* opening the dropdown scrolled to the very bottom. */
if (above && scroll) dropdown->vscroll->UpdatePosition(INT_MAX);
}
/**
* Show a drop down list.
* @param w Parent window for the list.
* @param list Prepopulated DropDownList.
* @param selected The initially selected list item.
* @param button The widget within the parent window that is used to determine
* the list's location.
* @param width Override the minimum width determined by the selected widget and list contents.
* @param instant_close Set to true if releasing mouse button should close the
* list regardless of where the cursor is.
*/
void ShowDropDownList(Window *w, DropDownList &&list, int selected, int button, uint width, bool instant_close)
{
/* Our parent's button widget is used to determine where to place the drop
* down list window. */
NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
Rect wi_rect = nwi->GetCurrentRect();
Colours wi_colour = nwi->colour;
if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
} else {
w->LowerWidget(button);
}
w->SetWidgetDirty(button);
if (width != 0) {
if (_current_text_dir == TD_RTL) {
wi_rect.left = wi_rect.right + 1 - ScaleGUITrad(width);
} else {
wi_rect.right = wi_rect.left + ScaleGUITrad(width) - 1;
}
}
ShowDropDownListAt(w, std::move(list), selected, button, wi_rect, wi_colour, instant_close);
}
/**
* Show a dropdown menu window near a widget of the parent window.
* The result code of the items is their index in the \a strings list.
* @param w Parent window that wants the dropdown menu.
* @param strings Menu list, end with #INVALID_STRING_ID
* @param selected Index of initial selected item.
* @param button Button widget number of the parent window \a w that wants the dropdown menu.
* @param disabled_mask Bitmask for disabled items (items with their bit set are displayed, but not selectable in the dropdown list).
* @param hidden_mask Bitmask for hidden items (items with their bit set are not copied to the dropdown list).
* @param width Minimum width of the dropdown menu.
*/
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32_t disabled_mask, uint32_t hidden_mask, uint width)
{
DropDownList list;
for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
if (!HasBit(hidden_mask, i)) {
list.push_back(std::make_unique<DropDownListStringItem>(strings[i], i, HasBit(disabled_mask, i)));
}
}
if (!list.empty()) ShowDropDownList(w, std::move(list), selected, button, width);
}
|