Eclipse SUMO - Simulation of Urban MObility
MFXTextFieldIcon.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2006-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 "MFXTextFieldIcon.h"
22 
23 
24 MFXTextFieldIcon::MFXTextFieldIcon(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) :
25  FXTextField(p, ncols, tgt, sel, opt, x, y, w, h, pl, pr, pt, pb),
26  myIcon(nullptr) {
27 }
28 
29 
30 void
32  myIcon = icon;
33 }
34 
35 
36 void
38  setText("");
39  setBackColor(FXRGB(255, 255, 255));
40  setIcon(nullptr);
41 }
42 
43 
44 void
45 MFXTextFieldIcon::drawIconTextRange(FXDCWindow& dc, const FXint iconWidth, FXint fm, FXint to) {
46  FXint sx, ex, xx, yy, cw, hh, ww, si, ei, lx, rx, t;
47  const FXint rr = width - border - padright;
48  const FXint ll = border + padleft;
49  const FXint mm = (ll + rr) / 2;
50  if (to <= fm) {
51  return;
52  }
53  dc.setFont(font);
54  // Text color
55  dc.setForeground(textColor);
56  // Height
57  hh = font->getFontHeight();
58  // Text sticks to top of field
59  if (options & JUSTIFY_TOP) {
60  yy = padtop + border;
61  }
62  // Text sticks to bottom of field
63  else if (options & JUSTIFY_BOTTOM) {
64  yy = height - padbottom - border - hh;
65  } else {
66  // Text centered in y
67  yy = border + padtop + (height - padbottom - padtop - (border << 1) - hh) / 2;
68  }
69  if (anchor < cursor) {
70  si = anchor;
71  ei = cursor;
72  } else {
73  si = cursor;
74  ei = anchor;
75  }
76  // Password mode
77  if (options & TEXTFIELD_PASSWD) {
78  cw = font->getTextWidth("*", 1);
79  ww = cw * contents.count();
80  // Text sticks to right of field
81  if (options & JUSTIFY_RIGHT) {
82  xx = shift + rr - ww + iconWidth;
83  } else if (options & JUSTIFY_LEFT) {
84  // Text sticks on left of field
85  xx = shift + ll + iconWidth;
86  } else {
87  // Text centered in field
88  xx = shift + mm - ww / 2 + iconWidth;
89  }
90  // Reduce to avoid drawing excessive amounts of text
91  lx = xx + cw * contents.index(fm);
92  rx = xx + cw * contents.index(to);
93  while (fm < to) {
94  if (lx + cw >= 0) {
95  break;
96  }
97  lx += cw;
98  fm = contents.inc(fm);
99  }
100  while (fm < to) {
101  if (rx - cw < width) {
102  break;
103  }
104  rx -= cw;
105  to = contents.dec(to);
106  }
107  // Adjust selected range
108  if (si < fm) {
109  si = fm;
110  }
111  if (ei > to) {
112  ei = to;
113  }
114  // Nothing selected
115  if (!hasSelection() || to <= si || ei <= fm) {
116  drawPWDTextFragment(dc, xx, yy, fm, to);
117  } else {
118  // Stuff selected
119  if (fm < si) {
120  drawPWDTextFragment(dc, xx, yy, fm, si);
121  } else {
122  si = fm;
123  }
124  if (ei < to) {
125  drawPWDTextFragment(dc, xx, yy, ei, to);
126  } else {
127  ei = to;
128  }
129  if (si < ei) {
130  sx = xx + cw * contents.index(si);
131  ex = xx + cw * contents.index(ei);
132  if (hasFocus()) {
133  dc.setForeground(selbackColor);
134  dc.fillRectangle(sx, padtop + border, ex - sx, height - padtop - padbottom - (border << 1));
135  dc.setForeground(seltextColor);
136  drawPWDTextFragment(dc, xx, yy, si, ei);
137  } else {
138  dc.setForeground(baseColor);
139  dc.fillRectangle(sx, padtop + border, ex - sx, height - padtop - padbottom - (border << 1));
140  dc.setForeground(textColor);
141  drawPWDTextFragment(dc, xx, yy, si, ei);
142  }
143  }
144  }
145  } else {
146  // Normal mode
147  ww = font->getTextWidth(contents.text(), contents.length());
148  // Text sticks to right of field
149  if (options & JUSTIFY_RIGHT) {
150  xx = shift + rr - ww + iconWidth;
151  } else if (options & JUSTIFY_LEFT) {
152  // Text sticks on left of field
153  xx = shift + ll + iconWidth;
154  } else {
155  // Text centered in field
156  xx = shift + mm - ww / 2 + iconWidth;
157  }
158  // Reduce to avoid drawing excessive amounts of text
159  lx = xx + font->getTextWidth(&contents[0], fm);
160  rx = lx + font->getTextWidth(&contents[fm], to - fm);
161  while (fm < to) {
162  t = contents.inc(fm);
163  cw = font->getTextWidth(&contents[fm], t - fm);
164  if (lx + cw >= 0) {
165  break;
166  }
167  lx += cw;
168  fm = t;
169  }
170  while (fm < to) {
171  t = contents.dec(to);
172  cw = font->getTextWidth(&contents[t], to - t);
173  if (rx - cw < width) {
174  break;
175  }
176  rx -= cw;
177  to = t;
178  }
179  // Adjust selected range
180  if (si < fm) {
181  si = fm;
182  }
183  if (ei > to) {
184  ei = to;
185  }
186  // Nothing selected
187  if (!hasSelection() || to <= si || ei <= fm) {
188  drawTextFragment(dc, xx, yy, fm, to);
189  } else {
190  // Stuff selected
191  if (fm < si) {
192  drawTextFragment(dc, xx, yy, fm, si);
193  } else {
194  si = fm;
195  }
196  if (ei < to) {
197  drawTextFragment(dc, xx, yy, ei, to);
198  } else {
199  ei = to;
200  }
201  if (si < ei) {
202  sx = xx + font->getTextWidth(contents.text(), si);
203  ex = xx + font->getTextWidth(contents.text(), ei);
204  if (hasFocus()) {
205  dc.setForeground(selbackColor);
206  dc.fillRectangle(sx, padtop + border, ex - sx, height - padtop - padbottom - (border << 1));
207  dc.setForeground(seltextColor);
208  drawTextFragment(dc, xx, yy, si, ei);
209  } else {
210  dc.setForeground(baseColor);
211  dc.fillRectangle(sx, padtop + border, ex - sx, height - padtop - padbottom - (border << 1));
212  dc.setForeground(textColor);
213  drawTextFragment(dc, xx, yy, si, ei);
214  }
215  }
216  }
217  }
218  // draw icon
219  if (myIcon) {
220  dc.drawIcon(myIcon, xx - myIcon->getWidth() - 3, yy + (hh - myIcon->getHeight()) / 2);
221  }
222 }
FXIcon * myIcon
icon
void resetTextField()
reset textField
void setIcon(FXIcon *icon)
set icon
MFXTextFieldIcon(FXComposite *p, FXint ncols, FXObject *tgt=NULL, FXSelector sel=0, FXuint opts=TEXTFIELD_NORMAL, FXint x=0, FXint y=0, FXint w=0, FXint h=0, FXint pl=DEFAULT_PAD, FXint pr=DEFAULT_PAD, FXint pt=DEFAULT_PAD, FXint pb=DEFAULT_PAD)
constructor
void drawIconTextRange(FXDCWindow &dc, const FXint iconWidth, FXint fm, FXint to)
draw icon text range