📄 fl_input_.cxx
字号:
} if (Fl::event_clicks()>1 || type()==FL_SECRET_INPUT) { while (!lineboundary(newpos)) newpos++; while (!lineboundary(newmark)) newmark--; } else { while (!wordboundary(newpos)) newpos++; while (!wordboundary(newmark)) newmark--; } } else { if (Fl::event_clicks()>1 || type()==FL_SECRET_INPUT) { while (!lineboundary(newpos)) newpos--; } else { while (!wordboundary(newpos)) newpos--; } } // if the multiple click does not increase the selection, revert // to single-click behavior: if (!drag && (mark() > position() ? (newmark >= position() && newpos <= mark()) : (newmark >= mark() && newpos <= position()))) { Fl::event_clicks(0); newmark = newpos = l-value(); } } position(newpos, newmark);}int Fl_Input_::position(int p, int m) { was_up_down = 0; if (p<0) p = 0; if (p>size()) p = size(); if (m<0) m = 0; if (m>size()) m = size(); if (p == position_ && m == mark_) return 0; //if (Fl::selection_owner() == this) Fl::selection_owner(0); if (p != m) { if (p != position_) minimal_update(position_, p); if (m != mark_) minimal_update(mark_, m); } else { // new position is a cursor if (position_ == mark_) { // old position was just a cursor if (Fl::focus() == this && !(damage()&FL_DAMAGE_EXPOSE)) { minimal_update(position_); erase_cursor_only = 1; } } else { // old position was a selection minimal_update(position_, mark_); } } position_ = p; mark_ = m; return 1;}int Fl_Input_::up_down_position(int i, int keepmark) { while (i > 0 && index(i-1) != '\n') i--; // go to start of line double oldwid = 0.0; setfont(); while (index(i) && index(i)!='\n') { double tt = oldwid + fl_width(index(i)); if ((oldwid+tt)/2 >= up_down_pos) break; oldwid = tt; i++; } int j = position(i, keepmark ? mark_ : i); was_up_down = 1; return j;}int Fl_Input_::copy() { if (mark() != position()) { int b, e; if (position() < mark()) { b = position(); e = mark(); } else { e = position(); b = mark(); } Fl::selection(*this, value()+b, (type()!=FL_SECRET_INPUT) ? e-b : 0); return 1; } return 0;}#define MAXFLOATSIZE 40static char* undobuffer;static int undobufferlength;static Fl_Input_* undowidget;static int undoat; // points after insertionstatic int undocut; // number of characters deleted therestatic int undoinsert; // number of characters insertedstatic int yankcut; // length of valid contents of buffer, even if undocut=0static void undobuffersize(int n) { if (n > undobufferlength) { if (undobuffer) { do {undobufferlength *= 2;} while (undobufferlength < n); undobuffer = (char*)realloc(undobuffer, undobufferlength); } else { undobufferlength = n+9; undobuffer = (char*)malloc(undobufferlength); } }}// all changes go through here, delete characters b-e and insert text:int Fl_Input_::replace(int b, int e, const char* text, int ilen) { was_up_down = 0; if (b<0) b = 0; if (e<0) e = 0; if (b>size_) b = size_; if (e>size_) e = size_; if (e<b) {int t=b; b=e; e=t;} if (text && !ilen) ilen = strlen(text); if (e<=b && !ilen) return 0; // don't clobber undo for a null operation if (size_+ilen-(e-b) > maximum_size_) { ilen = maximum_size_-size_+(e-b); if (ilen < 0) ilen = 0; } put_in_buffer(size_+ilen); if (e>b) { if (undowidget == this && b == undoat) { undobuffersize(undocut+(e-b)); memcpy(undobuffer+undocut, value_+b, e-b); undocut += e-b; } else if (undowidget == this && e == undoat && !undoinsert) { undobuffersize(undocut+(e-b)); memmove(undobuffer+(e-b), undobuffer, undocut); memcpy(undobuffer, value_+b, e-b); undocut += e-b; } else if (undowidget == this && e == undoat && (e-b)<undoinsert) { undoinsert -= e-b; } else { undobuffersize(e-b); memcpy(undobuffer, value_+b, e-b); undocut = e-b; undoinsert = 0; } memmove(buffer+b, buffer+e, size_-e+1); size_ -= e-b; undowidget = this; undoat = b; if (type() == FL_SECRET_INPUT) yankcut = 0; else yankcut = undocut; } if (ilen) { if (undowidget == this && b == undoat) undoinsert += ilen; else { undocut = 0; undoinsert = ilen; } memmove(buffer+b+ilen, buffer+b, size_-b+1); memcpy(buffer+b, text, ilen); size_ += ilen; } undowidget = this; mark_ = position_ = undoat = b+ilen; minimal_update(b); if (when()&FL_WHEN_CHANGED) do_callback(); else set_changed(); return 1;}int Fl_Input_::undo() { was_up_down = 0; if (undowidget != this || !undocut && !undoinsert) return 0; int ilen = undocut; int xlen = undoinsert; int b = undoat-xlen; int b1 = b; put_in_buffer(size_+ilen); if (ilen) { memmove(buffer+b+ilen, buffer+b, size_-b+1); memcpy(buffer+b, undobuffer, ilen); size_ += ilen; b += ilen; } if (xlen) { undobuffersize(xlen); memcpy(undobuffer, buffer+b, xlen); memmove(buffer+b, buffer+b+xlen, size_-xlen-b+1); size_ -= xlen; } undocut = xlen; if (xlen) yankcut = xlen; undoinsert = ilen; undoat = b; mark_ = b /* -ilen */; position_ = b; minimal_update(b1); if (when()&FL_WHEN_CHANGED) do_callback(); else set_changed(); return 1;}#if 0int Fl_Input_::yank() { // fake yank by trying to get it out of undobuffer if (!yankcut) return 0; return change(position(), position(), undobuffer, yankcut);}#endifint Fl_Input_::copy_cuts() { // put the yank buffer into the X clipboard if (!yankcut) return 0; Fl::selection(*this, undobuffer, yankcut); return 1;}void Fl_Input_::maybe_do_callback() { if (changed() || (when()&FL_WHEN_NOT_CHANGED)) { clear_changed(); do_callback();}}int Fl_Input_::handletext(int event, int X, int Y, int W, int H) { switch (event) { case FL_FOCUS: if ( (when() & FL_WHEN_NOT_CHANGED) || (when() & FL_WHEN_RELEASE_ALWAYS) ) do_callback(); if (mark_ == position_) { minimal_update(size()+1); } else //if (Fl::selection_owner() != this) minimal_update(mark_, position_); return 1; case FL_UNFOCUS: if (mark_ == position_) { if (!(damage()&FL_DAMAGE_EXPOSE)) { minimal_update(position_); erase_cursor_only = 1; } } else {//if (Fl::selection_owner() != this) minimal_update(mark_, position_); } if (when() & FL_WHEN_RELEASE) { maybe_do_callback(); } return 1; case FL_PUSH: handle_mouse(X, Y, W, H, Fl::event_state(FL_SHIFT)); return 1; case FL_DRAG: handle_mouse(X, Y, W, H, 1); return 1; case FL_RELEASE: // handle_mouse(X, Y, W, H, 1); copy(); return 1;// case FL_SELECTIONCLEAR:// minimal_update(mark_, position_);// mark_ = position_;// return 1; case FL_PASTE: { // strip trailing control characters and spaces before pasting: const char* t = Fl::event_text(); const char* e = t+Fl::event_length(); if (type()!=FL_MULTILINE_INPUT) while (e > t && *(uchar*)(e-1) <= ' ') e--; return replace(position(), mark(), t, e-t);} default: return 0; }}/*------------------------------*/Fl_Input_::Fl_Input_(int x, int y, int w, int h, const char* l): Fl_Widget(x, y, w, h, l) { box(FL_DOWN_BOX); color(FL_WHITE, FL_SELECTION_COLOR); align(FL_ALIGN_LEFT); textsize_ = FL_NORMAL_SIZE; textfont_ = FL_HELVETICA; textcolor_ = FL_BLACK; cursor_color_ = FL_BLACK; // was FL_BLUE mark_ = position_ = size_ = 0; bufsize = 0; buffer = 0; value_ = ""; xscroll_ = yscroll_ = 0; maximum_size_ = 32767; ww_ = 0;}void Fl_Input_::put_in_buffer(int len) { if (value_ == buffer && bufsize > len) { buffer[size_] = 0; return; } if (!bufsize) { if (len > size_) len += 9; // let a few characters insert before realloc bufsize = len+1; buffer = (char*)malloc(bufsize); } else if (bufsize <= len) { // we may need to move old value in case it points into buffer: int moveit = (value_ >= buffer && value_ < buffer+bufsize); // enlarge current buffer if (len > size_) { do {bufsize *= 2;} while (bufsize <= len); } else { bufsize = len+1; } char* nbuffer = (char*)realloc(buffer, bufsize); if (moveit) value_ += (nbuffer-buffer); buffer = nbuffer; } memmove(buffer, value_, size_); buffer[size_] = 0; value_ = buffer;}int Fl_Input_::static_value(const char* str, int len) { clear_changed(); if (undowidget == this) undowidget = 0; if (str == value_ && len == size_) return 0; if (len) { // non-empty new value: if (xscroll_ || yscroll_) { xscroll_ = yscroll_ = 0; minimal_update(0); } else { int i = 0; // find first different character: if (value_) { for (; i<size_ && i<len && str[i]==value_[i]; i++); if (i==size_ && i==len) return 0; } minimal_update(i); } value_ = str; size_ = len; } else { // empty new value: if (!size_) return 0; // both old and new are empty. size_ = 0; value_ = ""; xscroll_ = yscroll_ = 0; minimal_update(0); } position(size(), 0); return 1;}int Fl_Input_::static_value(const char* str) { return static_value(str, str ? strlen(str) : 0);}int Fl_Input_::value(const char* str, int len) { int r = static_value(str, len); if (len) put_in_buffer(len); return r;}int Fl_Input_::value(const char* str) { return value(str, str ? strlen(str) : 0);}void Fl_Input_::resize(int X, int Y, int W, int H) { if (W != w()) xscroll_ = 0; if (H != h()) yscroll_ = 0; Fl_Widget::resize(X, Y, W, H);}Fl_Input_::~Fl_Input_() { if (undowidget == this) undowidget = 0; if (bufsize) free((void*)buffer);}//// End of "$Id: Fl_Input_.cxx,v 1.1.1.1 2003/08/07 21:18:40 jasonk Exp $".//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -