Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MFXComboBoxIcon.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2006-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
19//
20/****************************************************************************/
21
26
27#include "MFXComboBoxIcon.h"
28#include "MFXListIcon.h"
29#include "MFXListIconItem.h"
30#include "MFXTextFieldSearch.h"
31
32// ===========================================================================
33// FOX callback mapping
34// ===========================================================================
35
36FXDEFMAP(MFXComboBoxIcon) MFXComboBoxIconMap[] = {
37 FXMAPFUNC(SEL_FOCUS_UP, 0, MFXComboBoxIcon::onFocusUp),
38 FXMAPFUNC(SEL_FOCUS_DOWN, 0, MFXComboBoxIcon::onFocusDown),
39 FXMAPFUNC(SEL_FOCUS_SELF, 0, MFXComboBoxIcon::onFocusSelf),
43 FXMAPFUNC(SEL_LEFTBUTTONPRESS, MFXComboBoxIcon::ID_TEXT, MFXComboBoxIcon::onTextButton),
47 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETVALUE, MFXComboBoxIcon::onFwdToText),
48 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETINTVALUE, MFXComboBoxIcon::onFwdToText),
49 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETREALVALUE, MFXComboBoxIcon::onFwdToText),
50 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETSTRINGVALUE, MFXComboBoxIcon::onFwdToText),
51 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_GETINTVALUE, MFXComboBoxIcon::onFwdToText),
52 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_GETREALVALUE, MFXComboBoxIcon::onFwdToText),
53 FXMAPFUNC(SEL_COMMAND, FXWindow::ID_GETSTRINGVALUE, MFXComboBoxIcon::onFwdToText),
55};
56
57// Object implementation
58FXIMPLEMENT(MFXComboBoxIcon, FXPacker, MFXComboBoxIconMap, ARRAYNUMBER(MFXComboBoxIconMap))
59
60// ===========================================================================
61// member method definitions
62// ===========================================================================
63
64MFXComboBoxIcon::MFXComboBoxIcon(FXComposite* p, MFXStaticToolTip* staticToolTip, const bool canSearch, const int visibleItems,
65 FXObject* tgt, FXSelector sel, FXuint opts, FXint x, FXint y, FXint w, FXint h, FXint pl, FXint pr, FXint pt, FXint pb):
66 FXPacker(p, opts, x, y, w, h, 0, 0, 0, 0, 0, 0) {
67 flags |= FLAG_ENABLED;
68 target = tgt;
69 message = sel;
70 // create text field
71 myTextFieldIcon = new MFXTextFieldIcon(this, staticToolTip, GUIIcon::EMPTY, this, MFXComboBoxIcon::ID_TEXT, 0, 0, 0, 0, 0, pl, pr, pt, pb);
72 if (options & COMBOBOX_STATIC) {
73 myTextFieldIcon->setEditable(FALSE);
74 }
75 // create pane for list
76 myPane = new FXPopup(this, FRAME_LINE);
77 // check if create search button
78 if (canSearch) {
79 myTextFieldSearch = new MFXTextFieldSearch(myPane, staticToolTip, this, ID_SEARCH, FRAME_THICK | LAYOUT_FIX_WIDTH | LAYOUT_FIX_HEIGHT, 0, 0, 0, 0, 2, 2, 2, 2);
80 // create label for empty icon
81 myNoItemsLabel = new FXLabel(myPane, TL("No matches found"), nullptr, FRAME_THICK | LAYOUT_FIX_WIDTH | LAYOUT_FIX_HEIGHT, 0, 0, 0, 0, 2, 2, 2, 2);
82 myNoItemsLabel->setTextColor(GUIDesignTextColorRed);
83 myNoItemsLabel->hide();
84 }
85 // create list icon
86 myList = new MFXListIcon(myPane, this, MFXComboBoxIcon::ID_LIST, LIST_BROWSESELECT | LIST_AUTOSELECT | LAYOUT_FILL_X | LAYOUT_FILL_Y | SCROLLERS_TRACK | HSCROLLER_NEVER);
87 if (options & COMBOBOX_STATIC) {
88 myList->setScrollStyle(SCROLLERS_TRACK | HSCROLLING_OFF);
89 }
90 myList->setNumVisible(visibleItems);
91 // create button
92 myButton = new FXMenuButton(this, FXString::null, NULL, myPane, FRAME_RAISED | FRAME_THICK | MENUBUTTON_DOWN | MENUBUTTON_ATTACH_RIGHT, 0, 0, 0, 0, 0, 0, 0, 0);
93 myButton->setXOffset(border);
94 myButton->setYOffset(border);
95 flags &= ~(FXuint)FLAG_UPDATE; // Never GUI update
96
97}
98
99
101 delete myPane;
102 myPane = (FXPopup*) - 1L;
104 myButton = (FXMenuButton*) - 1L;
105 if (myTextFieldSearch) {
107 myNoItemsLabel = (FXLabel*) - 1L;
108 }
109 myList = (MFXListIcon*) - 1L;
110}
111
112
113void
115 FXPacker::create();
116 myPane->create();
117}
118
119
120void
122 FXPacker::detach();
123 myPane->detach();
124}
125
126
127void
129 myPane->destroy();
130 FXPacker::destroy();
131}
132
133
134void
136 if (!isEnabled()) {
137 FXPacker::enable();
139 myButton->enable();
140 }
141}
142
143
144void
146 if (isEnabled()) {
147 FXPacker::disable();
149 myButton->disable();
150 }
151}
152
153
154FXint
156 FXint ww = myTextFieldIcon->getDefaultWidth() + myButton->getDefaultWidth() + (border << 1);
157 FXint pw = myPane->getDefaultWidth();
158 return FXMAX(ww, pw);
159}
160
161
162FXint
164 FXint th, bh;
166 bh = myButton->getDefaultHeight();
167 return FXMAX(th, bh) + (border << 1);
168}
169
170
171void
173 const FXint itemHeight = height - (border << 1);
174 const FXint buttonWidth = myButton->getDefaultWidth();
175 const FXint textWidth = width - buttonWidth - (border << 1);
176 myTextFieldIcon->position(border, border, textWidth, itemHeight);
177 myButton->position(border + textWidth, border, buttonWidth, itemHeight);
178 if (myTextFieldSearch) {
179 myTextFieldSearch->resize(width, height);
180 myNoItemsLabel->resize(width, height);
181 }
182 myPane->resize(width, myPane->getDefaultHeight());
183 flags &= ~(FXuint)FLAG_DIRTY;
184}
185
186
187FXString
189 if (myTextFieldIcon) {
190 return myTextFieldIcon->getText();
191 } else {
192 return "";
193 }
194}
195
196
197FXIcon*
199 if (myTextFieldIcon) {
200 return myTextFieldIcon->getIcon();
201 } else {
202 return nullptr;
203 }
204}
205
206
207FXint
209 return myList->getNumItems();
210}
211
212
213void
217
218
219void
220MFXComboBoxIcon::setText(const FXString& text, FXbool notify) {
221 myTextFieldIcon->setText(text, notify);
222}
223
224
225FXbool
227 return myList->isItemCurrent(index);
228}
229
230
231long
232MFXComboBoxIcon::setCurrentItem(const FXint index, FXbool notify) {
233 if (index >= 0 && index <= myList->getNumItems()) {
234 // get item
235 MFXListIconItem* item = myList->getItem(index);
236 // set it as current item and make it visible
237 myList->setCurrentItem(item);
238 myList->makeItemVisible(index);
239 // update both text fields
242 myTextFieldIcon->setBackColor(item->getBackGroundColor());
243 // check if notify
244 if (notify && target) {
245 return target->tryHandle(this, FXSEL(SEL_COMMAND, message), (void*)item);
246 }
247 } else {
248 fxerror("%s::setItem: index out of range.\n", getClassName());
249 }
250 return 0;
251}
252
253
254long
255MFXComboBoxIcon::setCurrentItem(const FXString& text, FXbool notify) {
256 // check if item exist
257 for (int i = 0; i < myList->getNumItems(); i++) {
258 const auto itemText = myList->tolowerString(myList->getItem(i)->getText());
259 if (myList->tolowerString(text) == itemText) {
260 // use "set curent item" function
261 return setCurrentItem(i, notify);
262 }
263 }
264 return 0;
265}
266
267
268FXint
272
273
274FXint
275MFXComboBoxIcon::updateIconItem(FXint index, const FXString& text, FXIcon* icon, FXColor bgColor, void* ptr) {
276 if (index < 0 || myList->getNumItems() <= index) {
277 fxerror("%s::setItem: index out of range.\n", getClassName());
278 }
279 myList->editItem(index, text, NULL, ptr);
280 if (isItemCurrent(index)) {
282 myTextFieldIcon->setBackColor(bgColor);
284 }
285 recalc();
286 return index;
287}
288
289
290FXint
291MFXComboBoxIcon::insertIconItem(FXint index, const FXString& text, FXIcon* icon, FXColor bgColor, void* ptr) {
292 index = myList->insertItem(index, new MFXListIconItem(text, icon, bgColor, ptr));
293 if (isItemCurrent(index)) {
295 myTextFieldIcon->setBackColor(bgColor);
297 }
298 recalc();
299 return index;
300}
301
302
303FXint
304MFXComboBoxIcon::appendIconItem(const FXString& text, FXIcon* icon, FXColor bgColor, void* ptr) {
305 FXint index = myList->appendItem(new MFXListIconItem(text, icon, bgColor, ptr));
306 if (isItemCurrent(getNumItems() - 1)) {
308 myTextFieldIcon->setBackColor(bgColor);
310 }
311 recalc();
312 return index;
313}
314
315
316void
318 const auto isCurrent = myList->isItemCurrent(index);
319 if (isCurrent == TRUE) {
320 if ((index > 0) && (index < (int)myList->getNumItems())) {
321 setCurrentItem(index - 1);
322 } else if (myList->getNumItems() > 0) {
324 }
325 }
326 myList->removeItem(index);
327 recalc();
328}
329
330
331void
335 recalc();
336}
337
338
339FXint
340MFXComboBoxIcon::findItem(const FXString& text) const {
341 return myList->findItem(text);
342}
343
344
345std::string
347 return myList->getItem(index)->getText().text();
348}
349
350
351void
353 myTextFieldIcon->setBackColor(clr);
354 myList->setBackColor(clr);
355}
356
357
358void
363
364
365FXColor
369
370
371void
372MFXComboBoxIcon::setTipText(const FXString& txt) {
374}
375
376
377const FXString&
381
382
383long
384MFXComboBoxIcon::onUpdFmText(FXObject*, FXSelector, void*) {
385 return (target && !myPane->shown()) ? target->tryHandle(this, FXSEL(SEL_UPDATE, message), NULL) : 0;
386}
387
388
389long
390MFXComboBoxIcon::onCmdFilter(FXObject*, FXSelector, void* ptr) {
392 myPane->resize(width, myPane->getDefaultHeight());
393 myPane->recalc();
394 myPane->onPaint(0, 0, ptr);
395 return 1;
396}
397
398
399long
400MFXComboBoxIcon::onFwdToText(FXObject* sender, FXSelector sel, void* ptr) {
401 return myTextFieldIcon->handle(sender, sel, ptr);
402}
403
404
405long
406MFXComboBoxIcon::onListClicked(FXObject*, FXSelector sel, void* ptr) {
407 // hide pane
408 myButton->handle(this, FXSEL(SEL_COMMAND, ID_UNPOST), NULL);
409 if (FXSELTYPE(sel) == SEL_COMMAND) {
410 // cast MFXListIconItem
411 const MFXListIconItem* item = (MFXListIconItem*)ptr;
412 // set icon and background color
415 myTextFieldIcon->setBackColor(item->getBackGroundColor());
416 // Select if editable
417 if (!(options & COMBOBOX_STATIC)) {
419 }
420 if (target) {
421 target->tryHandle(this, FXSEL(SEL_COMMAND, message), (void*)getText().text());
422 }
423 }
424 return 1;
425}
426
427
428long
429MFXComboBoxIcon::onTextButton(FXObject*, FXSelector, void*) {
430 if (options & COMBOBOX_STATIC) {
431 // Post the myList
432 myButton->handle(this, FXSEL(SEL_COMMAND, ID_POST), NULL);
433 return 1;
434 }
435 return 0;
436}
437
438
439long
440MFXComboBoxIcon::onTextChanged(FXObject*, FXSelector, void* ptr) {
441 return target ? target->tryHandle(this, FXSEL(SEL_CHANGED, message), ptr) : 0;
442}
443
444
445long
446MFXComboBoxIcon::onTextCommand(FXObject*, FXSelector, void* ptr) {
447 // reset background colors
449 // check if item exist
450 for (int i = 0; i < myList->getNumItems(); i++) {
451 const auto itemText = myList->tolowerString(myList->getItem(i)->getText());
452 if (myList->tolowerString(myTextFieldIcon->getText()) == itemText) {
453 // use "set curent item" function
454 return setCurrentItem(i, TRUE);
455 }
456 }
457 // no item found, then reset icon label
459 return target ? target->tryHandle(this, FXSEL(SEL_COMMAND, message), ptr) : 0;
460}
461
462
463long
464MFXComboBoxIcon::onFocusSelf(FXObject* sender, FXSelector, void* ptr) {
465 return myTextFieldIcon->handle(sender, FXSEL(SEL_FOCUS_SELF, 0), ptr);
466}
467
468
469long
470MFXComboBoxIcon::onFocusUp(FXObject*, FXSelector, void*) {
471 if (isEnabled()) {
472 FXint index = getCurrentItem();
473 if (index < 0) {
474 index = getNumItems() - 1;
475 } else if (0 < index) {
476 index--;
477 }
478 if (0 <= index && index < getNumItems()) {
479 setCurrentItem(index, TRUE);
480 }
481 return 1;
482 }
483 return 0;
484}
485
486
487long
488MFXComboBoxIcon::onFocusDown(FXObject*, FXSelector, void*) {
489 if (isEnabled()) {
490 FXint index = getCurrentItem();
491 if (index < 0) {
492 index = 0;
493 } else if (index < getNumItems() - 1) {
494 index++;
495 }
496 if (0 <= index && index < getNumItems()) {
497 setCurrentItem(index, TRUE);
498 }
499 return 1;
500 }
501 return 0;
502}
503
504
505long MFXComboBoxIcon::onMouseWheel(FXObject*, FXSelector, void* ptr) {
506 FXEvent* event = (FXEvent*)ptr;
507 if (isEnabled()) {
508 FXint index = getCurrentItem();
509 if (event->code < 0) {
510 if (index < 0) {
511 index = 0;
512 } else if (index < getNumItems() - 1) {
513 index++;
514 }
515 } else if (event->code > 0) {
516 if (index < 0) {
517 index = getNumItems() - 1;
518 } else if (0 < index) {
519 index--;
520 }
521 }
522 if (0 <= index && index < getNumItems()) {
523 setCurrentItem(index, TRUE);
524 }
525 return 1;
526 }
527 return 0;
528}
529
530
@ MID_MTEXTFIELDSEARCH_UPDATED
callback for MFXTextFieldSearch
#define GUIDesignTextColorRed
red color (for invalid text)
Definition GUIDesigns.h:44
#define GUIDesignBackgroundColorWhite
white background color (for valid text)
Definition GUIDesigns.h:47
FXDEFMAP(MFXComboBoxIcon) MFXComboBoxIconMap[]
#define TL(string)
Definition MsgHandler.h:304
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
FXbool isItemCurrent(FXint index) const
Return true if current item.
FXint insertIconItem(FXint index, const FXString &text, FXIcon *icon=nullptr, FXColor bgColor=FXRGB(255, 255, 255), void *ptr=nullptr)
insert icon item in the given position
long setCurrentItem(const FXint index, FXbool notify=FALSE)
Set the current item (index is zero-based)
void layout()
Perform layout.
FXint getCurrentItem() const
Get the current item's index.
void destroy()
Destroy server-side resources.
FXint findItem(const FXString &text) const
find item
long onTextChanged(FXObject *, FXSelector, void *)
void removeItem(FXint index)
Remove this item from the list.
long onFwdToText(FXObject *, FXSelector, void *)
FXint getNumItems() const
Return the number of items in the list.
FXLabel * myNoItemsLabel
no items label
const FXString & getTipText() const
Get the tool tip message for this combobox.
long onMouseWheel(FXObject *, FXSelector, void *)
FXint getDefaultWidth()
Return default width.
MFXTextFieldSearch * myTextFieldSearch
text field search
void setBackColor(FXColor clr)
Set window background color.
long onListClicked(FXObject *, FXSelector, void *)
FXMenuButton * myButton
myButton
MFXListIcon * myList
list with all items
FXString getText() const
Get the text.
long onCmdFilter(FXObject *, FXSelector, void *)
void setTextColor(FXColor clr)
Change text color.
FXPopup * myPane
popup in which place search label and list
void setNumVisible(FXint nvis)
Set the number of visible items in the drop down list.
~MFXComboBoxIcon()
Destructor.
virtual void clearItems()
Remove all items from the list.
FXint updateIconItem(FXint index, const FXString &text, FXIcon *icon=nullptr, FXColor bgColor=FXRGB(255, 255, 255), void *ptr=nullptr)
Replace the item at index.
FXint getDefaultHeight()
Return default height.
long onTextButton(FXObject *, FXSelector, void *)
void setTipText(const FXString &txt)
Set the tool tip message for this combobox.
std::string getItemText(FXint index) const
Get text for specified item.
void disable()
Disable combo box.
MFXComboBoxIcon()
FOX need this.
FXColor getTextColor() const
Return text color.
FXIcon * getIcon() const
Get the icon.
long onUpdFmText(FXObject *, FXSelector, void *)
FXint appendIconItem(const FXString &text, FXIcon *icon=nullptr, FXColor bgColor=FXRGB(255, 255, 255), void *ptr=nullptr)
append icon item in the last position
void enable()
Enable combo box.
MFXTextFieldIcon * myTextFieldIcon
textField icon
long onTextCommand(FXObject *, FXSelector, void *)
void detach()
Detach server-side resources.
void create()
Create server-side resources.
long onFocusDown(FXObject *, FXSelector, void *)
long onFocusSelf(FXObject *, FXSelector, void *)
void setText(const FXString &text, FXbool notify=FALSE)
Set the text in the textField.
long onFocusUp(FXObject *, FXSelector, void *)
Commands.
A list item which allows for custom coloring.
Definition MFXListIcon.h:38
void setNumVisible(FXint nvis)
Change the number of visible items.
void makeItemVisible(MFXListIconItem *item)
Scroll to bring item into view.
FXint editItem(FXint index, const FXString &text, FXIcon *icon=NULL, void *ptr=NULL, FXbool notify=FALSE)
Replace items text, icon, and user-data pointer.
int findItem(const FXString &text) const
Search items by name (In all items)
FXString tolowerString(const FXString &str) const
tolower string
FXint getNumItems() const
Return the number of items in the list.
Definition MFXListIcon.h:88
FXint appendItem(MFXListIconItem *item, FXbool notify=FALSE)
Append a [possibly subclassed] item to the list.
FXbool isItemCurrent(FXint index) const
Return TRUE if item is current.
FXint getCurrentItemIndex() const
Return current item, if any.
void setFilter(const FXString &value, FXLabel *label)
filter items in list
MFXListIconItem * getItem(FXint index) const
Return the item at the given index.
void setTextColor(FXColor clr)
Change normal text color.
void clearItems(FXbool notify=FALSE)
Remove all items from list.
void removeItem(FXint index, FXbool notify=FALSE)
Remove node from list.
void setCurrentItem(MFXListIconItem *item, FXbool notify=FALSE)
Change current item.
FXint insertItem(FXint index, MFXListIconItem *item, FXbool notify=FALSE)
Insert a new [possibly subclassed] item at the give index.
const FXString & getText() const
Return item's text label.
FXIcon * getIcon() const
Return item's icon.
const FXColor & getBackGroundColor() const
get background color
MFXStaticToolTip (based on FXToolTip)
virtual void disable()
Disable text field.
const FXString & getTipText() const
Get the tool tip message for this text field.
virtual void enable()
Enable text field.
virtual FXint getDefaultWidth()
Return default width.
FXColor getTextColor() const
Return text color.
FXString getText() const
Get the text for this label.
virtual FXint getDefaultHeight()
Return default height.
FXIcon * getIcon() const
get icon
FXbool selectAll()
Select all text.
void setTextColor(FXColor clr)
Change text color.
void setIcon(FXIcon *ic)
Change the icon.
void setTipText(const FXString &text)
Set the tool tip message for this text field.
void setText(const FXString &text, FXbool notify=FALSE)
Change the text and move cursor to end.
FXTextFieldIcon (based on FXTextFieldIcon)