1 /******************************************************************************** 2 * * 3 * T e x t F i e l d W i d g e t * 4 * * 5 ********************************************************************************* 6 * Copyright (C) 1997,2020 by Jeroen van der Zijp. All Rights Reserved. * 7 ********************************************************************************* 8 * This library is free software; you can redistribute it and/or modify * 9 * it under the terms of the GNU Lesser General Public License as published by * 10 * the Free Software Foundation; either version 3 of the License, or * 11 * (at your option) any later version. * 12 * * 13 * This library is distributed in the hope that it will be useful, * 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 16 * GNU Lesser General Public License for more details. * 17 * * 18 * You should have received a copy of the GNU Lesser General Public License * 19 * along with this program. If not, see <http://www.gnu.org/licenses/> * 20 ********************************************************************************/ 21 #ifndef FXTEXTFIELD_H 22 #define FXTEXTFIELD_H 23 24 #ifndef FXFRAME_H 25 #include "FXFrame.h" 26 #endif 27 28 namespace FX { 29 30 31 /// Textfield styles 32 enum { 33 TEXTFIELD_PASSWD = 0x00080000, /// Password mode 34 TEXTFIELD_INTEGER = 0x00100000, /// Integer mode 35 TEXTFIELD_REAL = 0x00200000, /// Real mode 36 TEXTFIELD_READONLY = 0x00400000, /// NOT editable 37 TEXTFIELD_ENTER_ONLY = 0x00800000, /// Only callback when enter hit 38 TEXTFIELD_LIMITED = 0x01000000, /// Limit entry to given number of columns 39 TEXTFIELD_OVERSTRIKE = 0x02000000, /// Overstrike mode 40 TEXTFIELD_AUTOGRAY = 0x04000000, /// Automatically gray out text field when not updated 41 TEXTFIELD_AUTOHIDE = 0x08000000, /// Automatically hide text field when not updated 42 TEXTFIELD_NORMAL = FRAME_SUNKEN|FRAME_THICK 43 }; 44 45 46 /** 47 * A text field is a single-line text entry widget. 48 * The text field widget supports clipboard for cut-and-paste 49 * operations. 50 * Text input may be constrained to a certain format; the built-in 51 * capabilities support integer and real number entry constraints; 52 * additional constraints on the input may be implemented by intercepting 53 * the SEL_VERIFY message; a custom handler should examine the tentative 54 * input string passed as type const FXchar* in the message data, and return 55 * a value of "0" if the new input is accepted. 56 * During text entry, the text field sends a SEL_CHANGED message to its target, 57 * with the message data set to the current text value of type const FXchar*. 58 * When the text is accepted by hitting ENTER, the SEL_COMMAND message is sent. 59 * The text field also sends SEL_COMMAND when the focus moves to another control. 60 * TEXTFIELD_ENTER_ONLY can be passed to suppress this feature. Typically, this 61 * flag is used in dialogs that close when ENTER is hit in a text field. 62 */ 63 class FXAPI FXTextField : public FXFrame { 64 FXDECLARE(FXTextField) 65 protected: 66 FXString contents; // Edited text 67 FXFont *font; // Text font 68 const FXchar *delimiters; // Set of delimiters 69 FXString clipped; // Clipped text 70 FXString help; // Help string 71 FXString tip; // Tooltip 72 FXColor textColor; // Text color 73 FXColor selbackColor; // Selected background color 74 FXColor seltextColor; // Selected text color 75 FXColor cursorColor; // Color of the Cursor 76 FXuint blink; // Next cursor blink state 77 FXint cursor; // Cursor position 78 FXint anchor; // Anchor position 79 FXint columns; // Number of columns visible 80 FXint shift; // Shift amount 81 protected: 82 FXTextField(); 83 FXint index(FXint x) const; 84 FXint coord(FXint i) const; 85 FXint leftWord(FXint pos) const; 86 FXint rightWord(FXint pos) const; 87 FXint wordStart(FXint pos) const; 88 FXint wordEnd(FXint pos) const; 89 void drawCursor(FXuint state); 90 void paintCursor(FXDCWindow& dc) const; 91 void eraseCursor(FXDCWindow& dc) const; 92 void drawTextRange(FXDCWindow& dc,FXint fm,FXint to) const; 93 void drawTextFragment(FXDCWindow& dc,FXint x,FXint y,FXint fm,FXint to) const; 94 void drawPWDTextFragment(FXDCWindow& dc,FXint x,FXint y,FXint fm,FXint to) const; 95 void enterText(const FXString& text,FXbool notify); 96 FXbool deletePendingSelection(FXbool notify); 97 private: 98 FXTextField(const FXTextField&); 99 FXTextField& operator=(const FXTextField&); 100 public: 101 long onPaint(FXObject*,FXSelector,void*); 102 long onUpdate(FXObject*,FXSelector,void*); 103 long onBlink(FXObject*,FXSelector,void*); 104 long onFocusIn(FXObject*,FXSelector,void*); 105 long onFocusOut(FXObject*,FXSelector,void*); 106 long onFocusSelf(FXObject*,FXSelector,void*); 107 long onMotion(FXObject*,FXSelector,void*); 108 long onAutoScroll(FXObject*,FXSelector,void*); 109 long onLeftBtnPress(FXObject*,FXSelector,void*); 110 long onLeftBtnRelease(FXObject*,FXSelector,void*); 111 long onMiddleBtnPress(FXObject*,FXSelector,void*); 112 long onMiddleBtnRelease(FXObject*,FXSelector,void*); 113 long onKeyPress(FXObject*,FXSelector,void*); 114 long onKeyRelease(FXObject*,FXSelector,void*); 115 long onVerify(FXObject*,FXSelector,void*); 116 long onSelectionLost(FXObject*,FXSelector,void*); 117 long onSelectionGained(FXObject*,FXSelector,void*); 118 long onSelectionRequest(FXObject*,FXSelector,void* ptr); 119 long onClipboardLost(FXObject*,FXSelector,void*); 120 long onClipboardGained(FXObject*,FXSelector,void*); 121 long onClipboardRequest(FXObject*,FXSelector,void*); 122 long onCmdSetTip(FXObject*,FXSelector,void*); 123 long onCmdGetTip(FXObject*,FXSelector,void*); 124 long onCmdSetHelp(FXObject*,FXSelector,void*); 125 long onCmdGetHelp(FXObject*,FXSelector,void*); 126 long onQueryTip(FXObject*,FXSelector,void*); 127 long onQueryHelp(FXObject*,FXSelector,void*); 128 long onUpdIsEditable(FXObject*,FXSelector,void*); 129 long onUpdHaveSelection(FXObject*,FXSelector,void*); 130 long onUpdHaveEditableSelection(FXObject*,FXSelector,void*); 131 132 // Value access 133 long onCmdSetValue(FXObject*,FXSelector,void*); 134 long onCmdSetIntValue(FXObject*,FXSelector,void*); 135 long onCmdSetLongValue(FXObject*,FXSelector,void*); 136 long onCmdSetRealValue(FXObject*,FXSelector,void*); 137 long onCmdSetStringValue(FXObject*,FXSelector,void*); 138 long onCmdGetIntValue(FXObject*,FXSelector,void*); 139 long onCmdGetLongValue(FXObject*,FXSelector,void*); 140 long onCmdGetRealValue(FXObject*,FXSelector,void*); 141 long onCmdGetStringValue(FXObject*,FXSelector,void*); 142 143 // Cursor movement 144 long onCmdCursorHome(FXObject*,FXSelector,void*); 145 long onCmdCursorEnd(FXObject*,FXSelector,void*); 146 long onCmdCursorRight(FXObject*,FXSelector,void*); 147 long onCmdCursorLeft(FXObject*,FXSelector,void*); 148 long onCmdCursorWordLeft(FXObject*,FXSelector,void*); 149 long onCmdCursorWordRight(FXObject*,FXSelector,void*); 150 151 // Cursor drag movement 152 long onCmdCursorShiftHome(FXObject*,FXSelector,void*); 153 long onCmdCursorShiftEnd(FXObject*,FXSelector,void*); 154 long onCmdCursorShiftRight(FXObject*,FXSelector,void*); 155 long onCmdCursorShiftLeft(FXObject*,FXSelector,void*); 156 long onCmdCursorShiftWordLeft(FXObject*,FXSelector,void*); 157 long onCmdCursorShiftWordRight(FXObject*,FXSelector,void*); 158 159 // Inserting 160 long onCmdInsertString(FXObject*,FXSelector,void*); 161 long onIMEStart(FXObject*,FXSelector,void*); 162 163 // Manipulation Selection 164 long onCmdCutSel(FXObject*,FXSelector,void*); 165 long onCmdCopySel(FXObject*,FXSelector,void*); 166 long onCmdPasteSel(FXObject*,FXSelector,void*); 167 long onCmdDeleteSel(FXObject*,FXSelector,void*); 168 long onCmdReplaceSel(FXObject*,FXSelector,void*); 169 long onCmdPasteMiddle(FXObject*,FXSelector,void*); 170 long onCmdSelectAll(FXObject*,FXSelector,void*); 171 long onCmdDeselectAll(FXObject*,FXSelector,void*); 172 173 // Deletion 174 long onCmdBackspace(FXObject*,FXSelector,void*); 175 long onCmdBackspaceWord(FXObject*,FXSelector,void*); 176 long onCmdBackspaceBol(FXObject*,FXSelector,void*); 177 long onCmdDeleteChar(FXObject*,FXSelector,void*); 178 long onCmdDeleteWord(FXObject*,FXSelector,void*); 179 long onCmdDeleteEol(FXObject*,FXSelector,void*); 180 long onCmdDeleteAll(FXObject*,FXSelector,void*); 181 182 // Control commands 183 long onCmdToggleEditable(FXObject*,FXSelector,void*); 184 long onUpdToggleEditable(FXObject*,FXSelector,void*); 185 long onCmdToggleOverstrike(FXObject*,FXSelector,void*); 186 long onUpdToggleOverstrike(FXObject*,FXSelector,void*); 187 public: 188 189 /// Default text delimiters 190 static const FXchar textDelimiters[]; 191 192 public: 193 194 enum{ 195 ID_CURSOR_HOME=FXFrame::ID_LAST, 196 ID_CURSOR_END, 197 ID_CURSOR_RIGHT, 198 ID_CURSOR_LEFT, 199 ID_CURSOR_WORD_LEFT, 200 ID_CURSOR_WORD_RIGHT, 201 ID_CURSOR_SHIFT_HOME, 202 ID_CURSOR_SHIFT_END, 203 ID_CURSOR_SHIFT_LEFT, 204 ID_CURSOR_SHIFT_RIGHT, 205 ID_CURSOR_SHIFT_WORD_LEFT, 206 ID_CURSOR_SHIFT_WORD_RIGHT, 207 ID_SELECT_ALL, 208 ID_DESELECT_ALL, 209 ID_CUT_SEL, 210 ID_COPY_SEL, 211 ID_DELETE_SEL, 212 ID_REPLACE_SEL, 213 ID_PASTE_SEL, 214 ID_PASTE_MIDDLE, 215 ID_INSERT_STRING, 216 ID_BACKSPACE, 217 ID_BACKSPACE_WORD, 218 ID_BACKSPACE_BOL, 219 ID_DELETE_CHAR, 220 ID_DELETE_WORD, 221 ID_DELETE_EOL, 222 ID_DELETE_ALL, 223 ID_TOGGLE_EDITABLE, 224 ID_TOGGLE_OVERSTRIKE, 225 ID_BLINK, 226 ID_AUTOSCROLL, 227 ID_LAST 228 }; 229 230 public: 231 232 /// Construct text field wide enough to display ncols columns 233 FXTextField(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); 234 235 /// Create server-side resources 236 virtual void create(); 237 238 /// Perform layout 239 virtual void layout(); 240 241 /// Enable text field 242 virtual void enable(); 243 244 /// Disable text field 245 virtual void disable(); 246 247 /// Return default width 248 virtual FXint getDefaultWidth(); 249 250 /// Return default height 251 virtual FXint getDefaultHeight(); 252 253 /// Yes, text field may receive focus 254 virtual FXbool canFocus() const; 255 256 /// Move the focus to this window 257 virtual void setFocus(); 258 259 /// Remove the focus from this window 260 virtual void killFocus(); 261 262 /// Set editable mode 263 void setEditable(FXbool edit=true); 264 265 /// Return true if text field may be edited 266 FXbool isEditable() const; 267 268 /// Set overstrike mode 269 void setOverstrike(FXbool over=true); 270 271 /// Return true if overstrike mode in effect 272 FXbool isOverstrike() const; 273 274 /// Change the text and move cursor to end 275 void setText(const FXString& text,FXbool notify=false); 276 277 /// Replace range of bytes with text 278 void replaceText(FXint pos,FXint m,const FXString& text,FXbool notify=false); 279 280 /// Append text at the end 281 void appendText(const FXString& text,FXbool notify=false); 282 283 /// Insert text at position 284 void insertText(FXint pos,const FXString& text,FXbool notify=false); 285 286 /// Remove range of text 287 void removeText(FXint pos,FXint m,FXbool notify=false); 288 289 /// Remove all text 290 void clearText(FXbool notify=false); 291 292 /// Get entire text getText()293 FXString getText() const { return contents; } 294 295 /// Select all text 296 FXbool selectAll(); 297 298 /// Select len characters starting at given position pos 299 FXbool setSelection(FXint pos,FXint len); 300 301 /// Extend the selection from the anchor to the given position 302 FXbool extendSelection(FXint pos); 303 304 /// Unselect the text 305 FXbool killSelection(); 306 307 /// Get selected text 308 FXString getSelectedText() const; 309 310 /// Copy primary selection to clipboard 311 FXbool copySelection(); 312 313 /// Cut primary selection to clipboard 314 FXbool cutSelection(FXbool notify=false); 315 316 /// Delete primary selection 317 FXbool deleteSelection(FXbool notify=false); 318 319 /// Replace primary selection by other text 320 FXbool replaceSelection(const FXString& text,FXbool notify=false); 321 322 /// Paste primary selection 323 FXbool pasteSelection(FXbool notify=false); 324 325 /// Paste clipboard 326 FXbool pasteClipboard(FXbool notify=false); 327 328 /// Return true if position pos is selected 329 FXbool isPosSelected(FXint pos) const; 330 331 /// Return true if position is fully visible 332 FXbool isPosVisible(FXint pos) const; 333 334 /// Scroll text to make the given position visible 335 void makePositionVisible(FXint pos); 336 337 /// Set cursor position 338 void setCursorPos(FXint pos); 339 340 /// Return cursor position getCursorPos()341 FXint getCursorPos() const { return cursor; } 342 343 /// Move cursor to position, and scroll into view 344 void moveCursor(FXint pos); 345 346 /// Move cursor to position, and extent the selection to this point 347 void moveCursorAndSelect(FXint pos); 348 349 /// Change anchor position 350 void setAnchorPos(FXint pos); 351 352 /// Return anchor position getAnchorPos()353 FXint getAnchorPos() const { return anchor; } 354 355 /// Change text style 356 void setTextStyle(FXuint style); 357 358 /// Return text style 359 FXuint getTextStyle() const; 360 361 /// Set the text font 362 void setFont(FXFont* fnt); 363 364 /// Get the text font getFont()365 FXFont* getFont() const { return font; } 366 367 /// Change number of visible columns 368 void setNumColumns(FXint cols); 369 370 /// Return number of visible columns getNumColumns()371 FXint getNumColumns() const { return columns; } 372 373 /// Change word delimiters 374 void setDelimiters(const FXchar* delims=textDelimiters){ delimiters=delims; } 375 376 /// Return word delimiters getDelimiters()377 const FXchar* getDelimiters() const { return delimiters; } 378 379 /** 380 * Change text justification mode. The justify mode is a combination of 381 * horizontal justification (JUSTIFY_LEFT, JUSTIFY_RIGHT, or JUSTIFY_CENTER_X), 382 * and vertical justification (JUSTIFY_TOP, JUSTIFY_BOTTOM, JUSTIFY_CENTER_Y). 383 * Note that JUSTIFY_CENTER_X can not be set from the constructor since by 384 * default text fields are left-justified. 385 */ 386 void setJustify(FXuint mode); 387 388 /// Return text justification mode 389 FXuint getJustify() const; 390 391 /// Change text color 392 void setTextColor(FXColor clr); 393 394 /// Return text color getTextColor()395 FXColor getTextColor() const { return textColor; } 396 397 /// Change selected background color 398 void setSelBackColor(FXColor clr); 399 400 /// Return selected background color getSelBackColor()401 FXColor getSelBackColor() const { return selbackColor; } 402 403 /// Change selected text color 404 void setSelTextColor(FXColor clr); 405 406 /// Return selected text color getSelTextColor()407 FXColor getSelTextColor() const { return seltextColor; } 408 409 /// Changes the cursor color 410 void setCursorColor(FXColor clr); 411 412 /// Return the cursor color getCursorColor()413 FXColor getCursorColor() const { return cursorColor; } 414 415 /// Set the status line help text for this label setHelpText(const FXString & text)416 void setHelpText(const FXString& text){ help=text; } 417 418 /// Get the status line help text for this label getHelpText()419 const FXString& getHelpText() const { return help; } 420 421 /// Set the tool tip message for this text field setTipText(const FXString & text)422 void setTipText(const FXString& text){ tip=text; } 423 424 /// Get the tool tip message for this text field getTipText()425 const FXString& getTipText() const { return tip; } 426 427 /// Save text field to a stream 428 virtual void save(FXStream& store) const; 429 430 /// Load text field from a stream 431 virtual void load(FXStream& store); 432 433 /// Destructor 434 virtual ~FXTextField(); 435 }; 436 437 } 438 439 #endif 440