Eclipse SUMO - Simulation of Urban MObility
MFXTextFieldSearch.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-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 // TextField for search elements
19 /****************************************************************************/
20 
24 
25 #include "MFXTextFieldSearch.h"
26 
27 // =========================================================================
28 // defines
29 // =========================================================================
30 
31 #define ICON_SPACING 4 // Spacing between icon and label (2 + 2)
32 #define ICON_SIZE 16
33 
34 // ===========================================================================
35 // FOX callback mapping
36 // ===========================================================================
37 
38 FXDEFMAP(MFXTextFieldSearch) MFXTextFieldSearchMap[] = {
39  FXMAPFUNC(SEL_PAINT, 0, MFXTextFieldSearch::onPaint),
40  FXMAPFUNC(SEL_FOCUSIN, 0, MFXTextFieldSearch::onFocusIn),
41  FXMAPFUNC(SEL_FOCUSOUT, 0, MFXTextFieldSearch::onFocusOut),
42  FXMAPFUNC(SEL_FOCUS_SELF, 0, MFXTextFieldSearch::onFocusSelf),
43  FXMAPFUNC(SEL_KEYPRESS, 0, MFXTextFieldSearch::onKeyPress),
44 };
45 
46 // Object implementation
47 FXIMPLEMENT(MFXTextFieldSearch, MFXTextFieldIcon, MFXTextFieldSearchMap, ARRAYNUMBER(MFXTextFieldSearchMap))
48 
49 // ===========================================================================
50 // member method definitions
51 // ===========================================================================
52 
53 MFXTextFieldSearch::MFXTextFieldSearch(FXComposite* p, FXint ncols, FXObject* tgt, FXSelector sel, FXuint opt, FXint x, FXint y, FXint w, FXint h, FXint pl, FXint pr, FXint pt, FXint pb) :
54  MFXTextFieldIcon(p, ncols, GUIIconSubSys::getIcon(GUIIcon::SEARCH), tgt, sel, opt, x, y, w, h, pl, pr, pt, pb),
55  myTarget(tgt) {
56 }
57 
58 
59 long
60 MFXTextFieldSearch::onKeyPress(FXObject* obj, FXSelector sel, void* ptr) {
61  MFXTextFieldIcon::onKeyPress(obj, sel, ptr);
62  return myTarget->handle(this, FXSEL(SEL_COMMAND, MID_MTEXTFIELDSEARCH_UPDATED), ptr);
63 }
64 
65 
66 long
67 MFXTextFieldSearch::onPaint(FXObject*, FXSelector, void* ptr) {
68  FXEvent* ev = (FXEvent*)ptr;
69  FXDCWindow dc(this, ev);
70  // Draw frame
71  drawFrame(dc, 0, 0, width, height);
72  // Gray background if disabled
73  if (isEnabled()) {
74  dc.setForeground(backColor);
75  } else {
76  dc.setForeground(baseColor);
77  }
78  // Draw background
79  dc.fillRectangle(border, border, width - (border << 1), height - (border << 1));
80  // Draw text, clipped against frame interior
81  dc.setClipRectangle(border, border, width - (border << 1), height - (border << 1));
82  // continue depending of search string
83  if (hasFocus() || (contents.count() > 0)) {
84  drawTextRange(dc, 0, contents.length());
85  } else {
86  drawSearchTextRange(dc, 0, TL("Type to search..."));
87  }
88  // Draw caret
89  if (flags & FLAG_CARET) {
90  int xx = coord(cursor) - 1;
91  xx += ICON_SPACING + ICON_SIZE;
92  dc.setForeground(cursorColor);
93  dc.fillRectangle(xx, padtop + border, 1, height - padbottom - padtop - (border << 1));
94  dc.fillRectangle(xx - 2, padtop + border, 5, 1);
95  dc.fillRectangle(xx - 2, height - border - padbottom - 1, 5, 1);
96  }
97  // draw icon
98  dc.drawIcon(icon, 3, border + padtop + (height - padbottom - padtop - (border << 1) - ICON_SIZE) / 2);
99  return 1;
100 }
101 
102 
103 long
104 MFXTextFieldSearch::onFocusIn(FXObject* sender, FXSelector sel, void* ptr) {
105  update();
106  return MFXTextFieldIcon::onFocusIn(sender, sel, ptr);
107 }
108 
109 
110 
111 long
112 MFXTextFieldSearch::onFocusOut(FXObject* sender, FXSelector sel, void* ptr) {
113  update();
114  return MFXTextFieldIcon::onFocusOut(sender, sel, ptr);
115 }
116 
117 
118 
119 long
120 MFXTextFieldSearch::onFocusSelf(FXObject* sender, FXSelector sel, void* ptr) {
121  //onPaint(sender, sel, ptr);
122  return MFXTextFieldIcon::onFocusSelf(sender, sel, ptr);
123 }
124 
125 
127  MFXTextFieldIcon() {
128 }
129 
130 
131 void
132 MFXTextFieldSearch::drawSearchTextRange(FXDCWindow& dc, FXint fm, const FXString& searchString) {
133  FXint xx, yy, cw, hh, ww, si, ei, lx, rx, t;
134  FXint rr = width - border - padright;
135  FXint ll = border + padleft;
136  FXint mm = (ll + rr) / 2;
137  FXint to = (int)searchString.length();
138  if (to <= fm) {
139  return;
140  }
141  dc.setFont(font);
142  // Text color
143  dc.setForeground(FXRGBA(128, 128, 128, 255));
144  // Height
145  hh = font->getFontHeight();
146  // Text sticks to top of field
147  if (options & JUSTIFY_TOP) {
148  yy = padtop + border;
149  } else if (options & JUSTIFY_BOTTOM) {
150  // Text sticks to bottom of field
151  yy = height - padbottom - border - hh;
152  } else {
153  // Text centered in y
154  yy = border + padtop + (height - padbottom - padtop - (border << 1) - hh) / 2;
155  }
156  if (anchor < cursor) {
157  si = anchor;
158  ei = cursor;
159  } else {
160  si = cursor;
161  ei = anchor;
162  }
163  // Normal mode
164  ww = font->getTextWidth(searchString.text(), searchString.length());
165  // Text sticks to right of field
166  if (options & JUSTIFY_RIGHT) {
167  xx = shift + rr - ww;
168  } else if (options & JUSTIFY_LEFT) {
169  // Text sticks on left of field
170  xx = shift + ll;
171  } else {
172  // Text centered in field
173  xx = shift + mm - ww / 2;
174  }
175  // add icon spacing
176  xx += ICON_SPACING + ICON_SIZE;
177  // Reduce to avoid drawing excessive amounts of text
178  lx = xx + font->getTextWidth(&searchString[0], fm);
179  rx = lx + font->getTextWidth(&searchString[fm], to - fm);
180  while (fm < to) {
181  t = searchString.inc(fm);
182  cw = font->getTextWidth(&searchString[fm], t - fm);
183  if (lx + cw >= 0) {
184  break;
185  }
186  lx += cw;
187  fm = t;
188  }
189  while (fm < to) {
190  t = searchString.dec(to);
191  cw = font->getTextWidth(&searchString[t], to - t);
192  if (rx - cw < width) {
193  break;
194  }
195  rx -= cw;
196  to = t;
197  }
198  // Adjust selected range
199  if (si < fm) {
200  si = fm;
201  }
202  if (ei > to) {
203  ei = to;
204  }
205  // draw text
206  xx += font->getTextWidth(searchString.text(), fm);
207  yy += font->getFontAscent();
208  dc.drawText(xx, yy, &searchString[fm], to - fm);
209 }
@ MID_MTEXTFIELDSEARCH_UPDATED
callback for MFXTextFieldSearch
Definition: GUIAppEnum.h:1584
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:33
#define ICON_SPACING
FXDEFMAP(MFXTextFieldSearch) MFXTextFieldSearchMap[]
#define ICON_SIZE
#define TL(string)
Definition: MsgHandler.h:315
FXint cursor
Cursor position.
FXString contents
Edited text.
FXint coord(FXint i) const
coordinates
long onKeyPress(FXObject *, FXSelector, void *)
long onFocusIn(FXObject *, FXSelector, void *)
long onFocusOut(FXObject *, FXSelector, void *)
FXFont * font
Text font.
FXint anchor
Anchor position.
void drawTextRange(FXDCWindow &dc, FXint fm, FXint to)
draw text range
FXColor cursorColor
Color of the Cursor.
long onFocusSelf(FXObject *, FXSelector, void *)
FXint shift
Shift amount.
FXTextFieldIcon (based on FXTextFieldIcon)
long onFocusSelf(FXObject *sender, FXSelector sel, void *ptr)
focus self
void drawSearchTextRange(FXDCWindow &dc, FXint fm, const FXString &searchString)
draw search text range
long onFocusIn(FXObject *sender, FXSelector sel, void *ptr)
focus in
long onPaint(FXObject *obj, FXSelector sel, void *ptr)
paint
MFXTextFieldSearch()
FOX need this.
FXObject * myTarget
target
long onKeyPress(FXObject *obj, FXSelector sel, void *ptr)
key press
long onFocusOut(FXObject *sender, FXSelector sel, void *ptr)
focus out