📄 textbox.cc
字号:
//**************************************************************//* filename: textbox.cc *//* *//**************************************************************//* programmed by: Thomas Wagner *//* last change: 13-05-95 *//**************************************************************#include <stdio.h>#include <stdlib.h>#include <string.h>#include <values.h>#include <X11/keysym.h>#include "textbox.h"extern unsigned long buttonpix, whitepix, blackpix, selectpix;TextBox::TextBox (Display * initdisplay, GC initgc, BigWindow * initParent, XFontStruct * initfontstruct, int initx, int inity, int initwidth, int initheight, int Givenxpos, char *inittext, int borderwidth, unsigned short initstatus, int initactionnumber, int Clickactionnumber):Window_Info (initdisplay, initgc, initParent->GetWindow (), initfontstruct, initx, inity, initwidth, initheight, inittext, borderwidth){ givenxpos = Givenxpos; textpos = 0; status = initstatus; Parent = initParent; actionnumber = initactionnumber; clickactionnumber = Clickactionnumber; ChangeText (text); if (status & TEXTOPTION_GIVENXPOS) textx = givenxpos; long eventmask = ExposureMask | ButtonPressMask; if (status & TEXTOPTION_SELECTABLE) eventmask |= ButtonReleaseMask | KeyPressMask | KeyReleaseMask; if ((status & TEXTOPTION_TEXTINPUT) || (status & TEXTOPTION_DOUBLEINPUT) || (status & TEXTOPTION_INTINPUT)) eventmask |= KeyPressMask | KeyReleaseMask | FocusChangeMask; SetSelectedInput (eventmask); MapMe ();}void TextBox::SelectMe (){ char changed; if (status & TEXTOPTION_SELECTED) changed = 0; else changed = 1; if (changed) { status |= TEXTOPTION_SELECTED; Redraw (); }}void TextBox::UnselectMe (){ char changed; if (status & TEXTOPTION_SELECTED) changed = 1; else changed = 0; if (changed) { status &= ~TEXTOPTION_SELECTED; Redraw (); }}int TextBox::IsSelected (){ return (status & TEXTOPTION_SELECTED);}void TextBox::SetStatus (unsigned short newstatus){ status = newstatus; if ((status & TEXTOPTION_TEXTINPUT) || (status & TEXTOPTION_DOUBLEINPUT) || (status & TEXTOPTION_INTINPUT)) AddSelectedInput (KeyPressMask | KeyReleaseMask | FocusChangeMask); else DelSelectedInput (KeyPressMask | KeyReleaseMask | FocusChangeMask);}void TextBox::MakeMeSelectable (){ char changed; if (status & TEXTOPTION_SELECTABLE) changed = 0; else changed = 1; if (changed) { status |= TEXTOPTION_SELECTABLE; Redraw (); }}void TextBox::MakeMeUnselectable (){ char changed; if (status & TEXTOPTION_SELECTABLE) changed = 1; else changed = 0; if (changed) { status &= ~TEXTOPTION_SELECTABLE; Redraw (); }}void TextBox::Redraw (){ XSetForeground (display, gc, whitepix); XFillRectangle (display, window, gc, 0, 0, width, height); if (!(status & TEXTOPTION_GIVENXPOS)) textx = (int) ((width - XTextWidth (fontstruct, text, strlen (text))) * .5) + 1; else textx = givenxpos; if (status & TEXTOPTION_SELECTED) { XSetForeground (display, gc, selectpix); XFillRectangle (display, window, gc, 0, 0, width, height); XSetForeground (display, gc, whitepix); if (text != NULL) XDrawString (display, window, gc, textx, texty, text, strlen (text)); } else { XSetForeground (display, gc, whitepix); XFillRectangle (display, window, gc, 0, 0, width, height); if ((status & TEXTOPTION_SELECTABLE) || (status & TEXTOPTION_DARK)) XSetForeground (display, gc, blackpix); else XSetForeground (display, gc, buttonpix); if (text != NULL) XDrawString (display, window, gc, textx, texty, text, strlen (text)); } if ((status & TEXTOPTION_TEXTINPUT) || (status & TEXTOPTION_DOUBLEINPUT) || (status & TEXTOPTION_INTINPUT)) { char *mark = "|"; int dummy; Window focus; XGetInputFocus (display, &focus, &dummy); if (focus == window) // mark acute position { short halfwidth = XTextWidth (fontstruct, "a", strlen ("a")) / 2; short markxpos = XTextWidth (fontstruct, text, textpos) - halfwidth; XDrawString (display, window, gc, textx + markxpos, texty, mark, strlen (mark)); } }}void TextBox::ChangeText (char *newtext){ text = newtext; if (!(status & TEXTOPTION_GIVENXPOS)) textx = (int) ((width - XTextWidth (fontstruct, text, strlen (text))) * .5) + 1; texty = (int) ((height + fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent) * .5 - fontstruct->max_bounds.descent); Redraw ();}void TextBox::ChangeText (int intvalue){ unsigned short textlength = 20; char *newtext; newtext = (char *) malloc (sizeof (char) * textlength); sprintf (newtext, "%d", intvalue); ChangeText (newtext);}void TextBox::ChangeText (double doublevalue){ unsigned short textlength = 20; char *newtext; newtext = (char *) malloc (sizeof (char) * textlength); sprintf (newtext, "%f", doublevalue); ChangeText (newtext);}void TextBox::HandleEvent (XEvent * Event){ static Time lastpresstime = 0; static TextBox *lastbox = NULL; switch (Event->type) { case FocusIn: Redraw (); break; case FocusOut: Redraw (); break; case Expose: if (Event->xexpose.count == 0) Redraw (); break; case LeaveNotify: XSetInputFocus (display, DefaultRootWindow (display), RevertToNone, CurrentTime); break; case KeyPress: if ((status & TEXTOPTION_TEXTINPUT) || (status & TEXTOPTION_DOUBLEINPUT) || (status & TEXTOPTION_INTINPUT)) { int dummy; char newchar = FALSE, textchanged = FALSE; Window focus; XGetInputFocus (display, &focus, &dummy); if (focus == window) { if (text == NULL) { text = (char *) malloc (sizeof (char) * 2); text[0] = 0; } int charnumber; char string[4]; int stringlength = 4; KeySym ks; XComposeStatus cs; charnumber = XLookupString (&(Event->xkey), string, stringlength, &ks, &cs); switch (ks) { case XK_Delete: if (textpos != 0) { char *newtext = (char *) malloc (sizeof (char) * strlen (text) + 2); strcpy (newtext, ""); strncat (newtext, text, textpos - 1); strcat (newtext, &text[textpos]); text = newtext; textpos--; } textchanged = TRUE; break; case XK_BackSpace: textchanged = TRUE; printf ("XK_BackSpace\n"); break; case XK_Return: Parent->Action (clickactionnumber, (int) this); break; case XK_Left: if (textpos > 0) textpos--; break; case XK_Right: textpos = MIN (strlen (text), (unsigned) textpos + 1 ); break; case XK_Begin: textpos = 0; break; case XK_End: textpos = strlen (text); break; } if (status & TEXTOPTION_TEXTINPUT) if ((ks >= XK_space) && (ks <= XK_asciitilde)) newchar = TRUE; if (status & TEXTOPTION_DOUBLEINPUT) { if ((ks >= XK_0) && (ks <= XK_9)) newchar = TRUE; if (ks == XK_period) { char *pos = strchr (text, '.'); if (pos == NULL) newchar = TRUE; } } if (status & TEXTOPTION_INTINPUT) if ((ks >= XK_0) && (ks <= XK_9)) newchar = TRUE; if (newchar == TRUE) { char *newtext = (char *) malloc (sizeof (char) * strlen (text) + 2); strcpy (newtext, ""); strncat (newtext, text, textpos); strncat (newtext, &string[0], 1); strcat (newtext, &text[textpos]); text = newtext; ++textpos; } if ((newchar == TRUE) || (textchanged == TRUE)) { if (status & TEXTOPTION_INTINPUT) { int temp; char tempchar[20]; sscanf (text, "%d", &temp); sprintf (tempchar, "%d", temp); if ((strcmp (text, tempchar)) && (strlen (text) != 0)) // integer > MAXINT strcpy (text, tempchar); Parent->Action (actionnumber, (int) &temp); if (strlen (text) < textpos) textpos = strlen (text); } if (status & TEXTOPTION_DOUBLEINPUT) { double temp; if (strlen (text) > DOUBLELENGTH) { char *newtext = (char *) malloc (sizeof (char) * (DOUBLELENGTH + 1)); strcpy (newtext, ""); strncat (newtext, text, textpos - 1); strcat (newtext, &text[textpos]); text = newtext; textpos--; } sscanf (text, "%lf", &temp); Parent->Action (actionnumber, (int) &temp); if (strlen (text) < textpos) textpos = strlen (text); } } Redraw (); } // if(focus==window) } break; case ButtonPress: if ((status & TEXTOPTION_TEXTINPUT) || (status & TEXTOPTION_DOUBLEINPUT) || (status & TEXTOPTION_INTINPUT)) XSetInputFocus (display, window, RevertToNone, CurrentTime); if (Event->xbutton.button == Button1 && TEXTOPTION_SELECTABLE & status) { Parent->Action (actionnumber, (int) this); if (status & TEXTOPTION_DOUBLECLICK) if (((Event->xbutton.time - lastpresstime) < DOUBLECLICKTIME) && (this == lastbox)) Parent->Action (clickactionnumber, (int) this); lastpresstime = Event->xbutton.time; lastbox = this; } }}TextBox::~TextBox (){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -