Eclipse SUMO - Simulation of Urban MObility
FXTreeListDinamic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2022 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 "FXTreeListDinamic.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(FXTreeListDinamic) FXTreeListDinamicMap[] = {
33  FXMAPFUNC(SEL_LEFTBUTTONPRESS, 0, FXTreeListDinamic::onLeftBtnPress),
34 };
35 
36 // Object implementation
37 FXIMPLEMENT(FXTreeListDinamic, FXTreeList, FXTreeListDinamicMap, ARRAYNUMBER(FXTreeListDinamicMap))
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  register FXIcon* icon = (state & OPENED) ? openIcon : closedIcon;
59  register FXFont* font = list->getFont();
60  register 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 // FXTreeListDinamic
91 
92 FXTreeListDinamic::FXTreeListDinamic(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 FXTreeListDinamic::insertItem(FXTreeItem* father, const FXString& text, FXIcon* oi) {
141  FXTreeItemDynamic* newItem = dynamic_cast<FXTreeItemDynamic*>(FXTreeList::insertItem(nullptr, father, new FXTreeItemDynamic(text, oi, oi, nullptr), false));
142  if (newItem != nullptr) {
143  myFXTreeItemDynamicItems.push_back(newItem);
144  return newItem;
145  } else {
146  throw ProcessError("New item cannot be NULL");
147  }
148 }
149 
150 
151 FXWindow*
153  return this;
154 }
155 
156 
157 FXTreeItem*
158 FXTreeListDinamic::getItemAt(FXint x,FXint y) const {
159  return FXTreeList::getItemAt(x, y);
160 }
161 
162 
164 FXTreeListDinamic::getItem(FXint index) const {
165  return myFXTreeItemDynamicItems.at(index);
166 }
167 
168 
169 void
171  if (mySelectedItem != -1) {
172  myFXTreeItemDynamicItems.at(mySelectedItem)->setSelected(false);
173  mySelectedItem = -1;
174  }
175 }
176 
177 
178 long
179 FXTreeListDinamic::onLeftBtnPress(FXObject* obj, FXSelector sel, void* ptr) {
180  FXTreeList::onLeftBtnPress(obj, sel, ptr);
181  // update selected item
182  mySelectedItem = -1;
183  for (int i = 0; i < (int)myFXTreeItemDynamicItems.size(); i++) {
184  if (myFXTreeItemDynamicItems.at(i)->isSelected()) {
185  mySelectedItem = i;
186  }
187  }
188  // update height
189  setHeight(getContentHeight() + 20);
190  return 1;
191 }
192 
193 
195  FXTreeList::FXTreeList() {
196 }
FXTreeListDinamic::myFXTreeItemDynamicItems
std::vector< FXTreeItemDynamic * > myFXTreeItemDynamicItems
list with current FXTreeItemDynamic elements
Definition: FXTreeListDinamic.h:99
FXTreeListDinamic::getItem
FXTreeItemDynamic * getItem(FXint index) const
Get item.
Definition: FXTreeListDinamic.cpp:164
FXTreeItemDynamic::setTextColor
void setTextColor(FXColor clr)
Change text color.
Definition: FXTreeListDinamic.cpp:51
FXTreeListDinamic::getFXWindow
FXWindow * getFXWindow()
get FXWindows associated with this FXTreeListDinamic
Definition: FXTreeListDinamic.cpp:152
FXTreeListDinamic::resetSelectedItem
void resetSelectedItem()
reset selected item
Definition: FXTreeListDinamic.cpp:170
FXTreeListDinamic::FXTreeListDinamic
FXTreeListDinamic()
default constructor
Definition: FXTreeListDinamic.cpp:194
FXTreeListDinamic::show
void show()
Show FXTreeListDinamic.
Definition: FXTreeListDinamic.cpp:98
FXTreeListDinamic::getItemAt
FXTreeItem * getItemAt(FXint x, FXint y) const
Get item at x,y, if any.
Definition: FXTreeListDinamic.cpp:158
FXTreeListDinamic::update
void update()
update
Definition: FXTreeListDinamic.cpp:114
FXTreeListDinamic::getNumItems
FXint getNumItems()
get num items
Definition: FXTreeListDinamic.cpp:128
ICON_SPACING
#define ICON_SPACING
Definition: FXTreeListDinamic.cpp:26
ProcessError
Definition: UtilExceptions.h:37
UtilExceptions.h
FXTreeListDinamic
FXTreeListDinamic.
Definition: FXTreeListDinamic.h:48
SIDE_SPACING
#define SIDE_SPACING
Definition: FXTreeListDinamic.cpp:25
FXTreeListDinamic::clearItems
void clearItems()
clear items
Definition: FXTreeListDinamic.cpp:121
FXDEFMAP
FXDEFMAP(FXTreeListDinamic) FXTreeListDinamicMap[]
FXTreeListDinamic::insertItem
FXTreeItem * insertItem(FXTreeItem *father, const FXString &text, FXIcon *oi)
Insert item with given text and icon.
Definition: FXTreeListDinamic.cpp:140
FXTreeItemDynamic::myTextColor
FXColor myTextColor
set text color
Definition: FXTreeListDinamic.h:44
FXTreeListDinamic.h
FXTreeListDinamic::onLeftBtnPress
long onLeftBtnPress(FXObject *, FXSelector, void *)
Definition: FXTreeListDinamic.cpp:179
FXTreeListDinamic::getSelectedIndex
FXint getSelectedIndex()
getSelected item index
Definition: FXTreeListDinamic.cpp:134
FXTreeItemDynamic::draw
void draw(const FXTreeList *list, FXDC &dc, FXint xx, FXint yy, FXint, FXint hh) const
draw tree item
Definition: FXTreeListDinamic.cpp:57
FXTreeListDinamic::hide
void hide()
Hide FXTreeListDinamic.
Definition: FXTreeListDinamic.cpp:107
FXTreeItemDynamic
Definition: FXTreeListDinamic.h:29
FXTreeListDinamic::mySelectedItem
FXint mySelectedItem
selected item
Definition: FXTreeListDinamic.h:102