Eclipse SUMO - Simulation of Urban MObility
MFXTreeListDynamic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2004-2024 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 /****************************************************************************/
18 //
19 /****************************************************************************/
20 
21 #include "MFXTreeListDynamic.h"
22 
24 
25 #define SIDE_SPACING 4 // Spacing between side and item
26 #define ICON_SPACING 4 // Spacing between parent and child in x direction
27 
28 // ===========================================================================
29 // FOX callback mapping
30 // ===========================================================================
31 
32 FXDEFMAP(MFXTreeListDynamic) MFXTreeListDynamicMap[] = {
33  FXMAPFUNC(SEL_LEFTBUTTONPRESS, 0, MFXTreeListDynamic::onLeftBtnPress),
34 };
35 
36 // Object implementation
37 FXIMPLEMENT(MFXTreeListDynamic, FXTreeList, MFXTreeListDynamicMap, ARRAYNUMBER(MFXTreeListDynamicMap))
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
42 
43 // FXTreeItemDynamic
44 
45 FXTreeItemDynamic::FXTreeItemDynamic(const FXString& text, FXIcon* oi, FXIcon* ci, void* ptr) :
46  FXTreeItem(text, oi, ci, ptr) {
47 }
48 
49 
50 void
52  myTextColor = clr;
53 }
54 
55 
56 void
57 FXTreeItemDynamic::draw(const FXTreeList* list, FXDC& dc, FXint xx, FXint yy, FXint, FXint hh) const {
58  FXIcon* icon = (state & OPENED) ? openIcon : closedIcon;
59  FXFont* font = list->getFont();
60  FXint th = 0, tw = 0, ih = 0, iw = 0;
61  xx += SIDE_SPACING / 2;
62  if (icon) {
63  iw = icon->getWidth();
64  ih = icon->getHeight();
65  dc.drawIcon(icon, xx, yy + (hh - ih) / 2);
66  xx += ICON_SPACING + iw;
67  }
68  if (!label.empty()) {
69  tw = 4 + font->getTextWidth(label.text(), label.length());
70  th = 4 + font->getFontHeight();
71  yy += (hh - th) / 2;
72  if (isSelected()) {
73  dc.setForeground(list->getSelBackColor());
74  dc.fillRectangle(xx, yy, tw, th);
75  }
76  if (hasFocus()) {
77  dc.drawFocusRectangle(xx + 1, yy + 1, tw - 2, th - 2);
78  }
79  if (!isEnabled()) {
80  dc.setForeground(makeShadowColor(list->getBackColor()));
81  } else if (isSelected()) {
82  dc.setForeground(list->getSelTextColor());
83  } else {
84  dc.setForeground(myTextColor);
85  }
86  dc.drawText(xx + 2, yy + font->getFontAscent() + 2, label);
87  }
88 }
89 
90 // MFXTreeListDynamic
91 
92 MFXTreeListDynamic::MFXTreeListDynamic(FXComposite* p, FXObject* tgt, FXSelector sel, FXuint opts) :
93  FXTreeList(p, tgt, sel, opts, 0, 0, 0, 200) {
94 }
95 
96 
97 void
99  // update height
100  setHeight(getContentHeight() + 20);
101  // show
102  FXTreeList::show();
103 }
104 
105 
106 void
108  // hide
109  FXTreeList::hide();
110 }
111 
112 
113 void
115  // update
116  FXTreeList::update();
117 }
118 
119 
120 void
122  myFXTreeItemDynamicItems.clear();
123  return FXTreeList::clearItems();
124 }
125 
126 
127 FXint
129  return FXTreeList::getNumItems();
130 }
131 
132 
133 FXint
135  return mySelectedItem;
136 }
137 
138 
139 FXTreeItem*
140 MFXTreeListDynamic::prependItem(FXTreeItem* father, const FXString& text, FXIcon* oi, FXColor tColor) {
141  FXTreeItemDynamic* newItem = dynamic_cast<FXTreeItemDynamic*>(FXTreeList::prependItem(father, new FXTreeItemDynamic(text, oi, oi, nullptr), false));
142  if (newItem != nullptr) {
143  myFXTreeItemDynamicItems.insert(myFXTreeItemDynamicItems.begin(), newItem);
144  newItem->setTextColor(tColor);
145  return newItem;
146  }
147  throw ProcessError("New item cannot be NULL");
148 }
149 
150 
151 FXTreeItem*
152 MFXTreeListDynamic::appendItem(FXTreeItem* father, const FXString& text, FXIcon* oi, FXColor tColor) {
153  FXTreeItemDynamic* newItem = dynamic_cast<FXTreeItemDynamic*>(FXTreeList::appendItem(father, new FXTreeItemDynamic(text, oi, oi, nullptr), false));
154  if (newItem != nullptr) {
155  myFXTreeItemDynamicItems.push_back(newItem);
156  newItem->setTextColor(tColor);
157  return newItem;
158  }
159  throw ProcessError("New item cannot be NULL");
160 }
161 
162 
163 FXWindow*
165  return this;
166 }
167 
168 
169 FXTreeItem*
170 MFXTreeListDynamic::getItemAt(FXint x, FXint y) const {
171  return FXTreeList::getItemAt(x, y);
172 }
173 
174 
176 MFXTreeListDynamic::getItem(FXint index) const {
177  return myFXTreeItemDynamicItems.at(index);
178 }
179 
180 
181 void
183  if (mySelectedItem != -1) {
184  myFXTreeItemDynamicItems.at(mySelectedItem)->setSelected(false);
185  mySelectedItem = -1;
186  }
187 }
188 
189 
190 long
191 MFXTreeListDynamic::onLeftBtnPress(FXObject* obj, FXSelector sel, void* ptr) {
192  FXTreeList::onLeftBtnPress(obj, sel, ptr);
193  // update selected item
194  mySelectedItem = -1;
195  for (int i = 0; i < (int)myFXTreeItemDynamicItems.size(); i++) {
196  if (myFXTreeItemDynamicItems.at(i)->isSelected()) {
197  mySelectedItem = i;
198  }
199  }
200  // update height
201  setHeight(getContentHeight() + 20);
202  return 1;
203 }
204 
205 
207  FXTreeList::FXTreeList() {
208 }
FXDEFMAP(MFXTreeListDynamic) MFXTreeListDynamicMap[]
#define ICON_SPACING
#define SIDE_SPACING
FXColor myTextColor
set text color
void setTextColor(FXColor clr)
Change text color.
void draw(const FXTreeList *list, FXDC &dc, FXint xx, FXint yy, FXint, FXint hh) const
draw tree item
MFXTreeListDynamic.
void show()
Show MFXTreeListDynamic.
std::vector< FXTreeItemDynamic * > myFXTreeItemDynamicItems
list with current FXTreeItemDynamic elements
void clearItems()
clear items
FXint getNumItems()
get num items
FXint mySelectedItem
selected item
long onLeftBtnPress(FXObject *, FXSelector, void *)
void resetSelectedItem()
reset selected item
MFXTreeListDynamic()
default constructor
void hide()
Hide MFXTreeListDynamic.
FXWindow * getFXWindow()
get FXWindows associated with this MFXTreeListDynamic
FXTreeItem * getItemAt(FXint x, FXint y) const
Get item at x,y, if any.
FXTreeItem * appendItem(FXTreeItem *father, const FXString &text, FXIcon *oi, FXColor tColor=FXRGB(0, 0, 0))
append item with given text and icon
FXint getSelectedIndex()
getSelected item index
FXTreeItemDynamic * getItem(FXint index) const
Get item.
FXTreeItem * prependItem(FXTreeItem *father, const FXString &text, FXIcon *oi, FXColor tColor=FXRGB(0, 0, 0))
prepend item with given text and icon