1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2001, Sven2
6 * Copyright (c) 2017-2023, The LegacyClonk Team and contributors
7 *
8 * Distributed under the terms of the ISC license; see accompanying file
9 * "COPYING" for details.
10 *
11 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12 * See accompanying file "TRADEMARK" for details.
13 *
14 * To redistribute this file separately, substitute the full license texts
15 * for the above references.
16 */
17
18#pragma once
19
20#include "C4Constants.h"
21#include "C4Gui.h"
22
23namespace C4GUI
24{
25// dropdown box to select elements from a list
26class ComboBox : public Control
27{
28public:
29 struct ComboMenuCBStruct // struct used as menu callback parameter for dropdown menu
30 {
31 StdStrBuf sText;
32 int32_t id;
33
34 ComboMenuCBStruct() : sText(), id(0) {}
35 ComboMenuCBStruct(const char *szText, int32_t id) : sText(szText), id(id) {}
36 };
37
38private:
39 class C4KeyBinding *pKeyOpenCombo, *pKeyCloseCombo;
40
41private:
42 int32_t iOpenMenu; // associated menu (used to flag dropped down)
43 ComboBox_FillCB *pFillCallback; // callback used to display the dropdown
44 char Text[C4MaxTitle + 1]; // currently selected item
45 bool fReadOnly; // merely a label in ReadOnly-mode
46 bool fSimple; // combo without border and stuff
47 bool fMouseOver; // mouse hovering over this?
48 CStdFont *pUseFont; // font used to draw this control
49 uint32_t dwFontClr, dwBGClr, dwBorderClr; // colors used to draw this control
50 C4Facet *pFctSideArrow; // arrow gfx used to start combo-dropdown
51
52private:
53 bool DoDropdown(); // open dropdown menu (context menu)
54 bool KeyDropDown() { return DoDropdown(); }
55 bool KeyAbortDropDown() { return AbortDropdown(fByUser: true); }
56 bool AbortDropdown(bool fByUser); // abort dropdown menu, if it's open
57
58protected:
59 virtual void DrawElement(C4FacetEx &cgo) override; // draw combo box
60
61 virtual void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, uint32_t dwKeyParam) override; // input: mouse. left-click opens menu
62 virtual void MouseEnter(CMouse &rMouse) override; // called when mouse cursor enters element region
63 virtual void MouseLeave(CMouse &rMouse) override; // called when mouse cursor leaves element region
64
65 virtual bool IsFocusOnClick() override { return false; } // don't select control on click
66 virtual Control *IsFocusElement() override { return fReadOnly ? nullptr : this; } // this control can gain focus if not readonly
67
68 void OnCtxComboSelect(C4GUI::Element *pListItem, const ComboMenuCBStruct &rNewSel);
69
70public:
71 ComboBox(const C4Rect &rtBounds);
72 ~ComboBox();
73
74 void SetComboCB(ComboBox_FillCB *pNewFillCallback);
75 static int32_t GetDefaultHeight();
76 void SetText(const char *szToText);
77 void SetReadOnly(bool fToVal) { if (fReadOnly = fToVal) AbortDropdown(fByUser: false); }
78 void SetSimple(bool fToVal) { fSimple = fToVal; }
79 const StdStrBuf GetText() { return StdStrBuf(Text, false); }
80 void SetFont(CStdFont *pToFont) { pUseFont = pToFont; }
81
82 void SetColors(uint32_t dwFontClr, uint32_t dwBGClr, uint32_t dwBorderClr)
83 {
84 this->dwFontClr = dwFontClr; this->dwBGClr = dwBGClr; this->dwBorderClr = dwBorderClr;
85 }
86
87 void SetDecoration(C4Facet *pFctSideArrow)
88 {
89 this->pFctSideArrow = pFctSideArrow;
90 }
91
92 friend class ComboBox_FillCB;
93};
94}
95