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