static void Listener_help(Widget w, XEvent *event, char **str, Cardinal *num) { char *source = NULL; int end = 0, beg, len; end = XmTextGetLastPosition(w); /* cursor perhaps? */ beg = end - 1024; if (beg < 0) beg = 0; if (beg >= end) return; len = end - beg + 1; source = (char *)CALLOC(len + 1, sizeof(char)); XmTextGetSubstring(w, beg, len, len + 1, source); provide_listener_help(source); FREE(source); }
static void Word_upper(Widget w, XEvent *event, char **str, Cardinal *num) { bool up, cap; XmTextPosition curpos, endpos; up = (str[0][0] == 'u'); cap = (str[0][0] == 'c'); curpos = XmTextGetCursorPosition(w); endpos = XmTextGetLastPosition(w); if (curpos < endpos) { int i, length, wstart, wend; char *buf = NULL; length = endpos - curpos; buf = (char *)CALLOC(length + 1, sizeof(char)); XmTextGetSubstring(w, curpos, length, length + 1, buf); wstart = 0; wend = length; for (i = 0; i < length; i++) if (!isspace((int)(buf[i]))) { wstart = i; break; } for (i = wstart + 1; i < length; i++) if (isspace((int)(buf[i]))) { wend = i; break; } if (cap) { buf[0] = toupper(buf[wstart]); buf[1] = '\0'; XmTextReplace(w, curpos + wstart, curpos + wstart + 1, buf); } else { int j; for (i = wstart, j = 0; i < wend; i++, j++) if (up) buf[j] = toupper(buf[i]); else buf[j] = tolower(buf[i]); buf[j] = '\0'; XmTextReplace(w, curpos + wstart, curpos + wend, buf); } XmTextSetCursorPosition(w, curpos + wend); if (buf) FREE(buf); } }
static void Text_transpose(Widget w, XEvent *event, char **str, Cardinal *num) { XmTextPosition curpos; curpos = XmTextGetCursorPosition(w); if (curpos > 1) { char buf[3]; /* needs room for null */ char tmp; XmTextGetSubstring(w, (XmTextPosition)(curpos - 1), 2, 3, buf); tmp = buf[0]; buf[0] = buf[1]; buf[1] = tmp; XmTextReplace(w, curpos - 1, curpos + 1, buf); XmTextSetCursorPosition(w, curpos + 1); } }
static char* motTextGetLineValueAttrib(Ihandle* ih) { if (ih->data->is_multiline) { int lin, col, start, end; char* str = iupStrGetMemory(200); char *value = XmTextGetString(ih->handle); XmTextPosition pos = XmTextGetInsertionPosition(ih->handle); motTextGetLinColFromPosition(value, pos, &lin, &col); start = motTextSetLinColToPosition(value, lin, 1); end = motTextSetLinColToPosition(value, lin, 20000); XtFree(value); XmTextGetSubstring(ih->handle, start, end-start, 200, str); /* do not include the EOL */ return str; } else return motTextGetValueAttrib(ih); }
static void Begin_of_line(Widget w, XEvent *ev, char **ustr, Cardinal *num) { /* don't back up before listener prompt */ XmTextPosition curpos, loc; Boolean found; curpos = XmTextGetCursorPosition(w) - 1; found = XmTextFindString(w, curpos, "\n", XmTEXT_BACKWARD, &loc); if (found) { char *str = NULL; str = (char *)CALLOC(ss->listener_prompt_length + 3, sizeof(char)); XmTextGetSubstring(w, loc + 1, ss->listener_prompt_length, ss->listener_prompt_length + 2, str); if (strncmp(listener_prompt(ss), str, ss->listener_prompt_length) == 0) XmTextSetCursorPosition(w, loc + ss->listener_prompt_length + 1); else XmTextSetCursorPosition(w, loc + 1); FREE(str); } else XmTextSetCursorPosition(w, 1); }
static void Listener_completion(Widget w, XEvent *event, char **str, Cardinal *num) { /* used only by the listener widget -- needs to be smart about text since overall string can be enormous * and we don't want to back up past the last prompt * also if at start of line (or all white-space to previous \n, indent */ int beg, end, len, matches = 0; char *old_text; ss->sgx->completion_requestor = listener_text; ss->sgx->completion_requestor_dialog = NULL; beg = printout_end + 1; end = XmTextGetLastPosition(w); if (end <= beg) return; len = end - beg + 1; old_text = (char *)CALLOC(len + 1, sizeof(char)); XmTextGetSubstring(w, beg, len, len + 1, old_text); /* now old_text is the stuff typed since the last prompt */ if (old_text) { char *new_text = NULL, *file_text = NULL; bool try_completion = true; new_text = complete_listener_text(old_text, end, &try_completion, &file_text); if (!try_completion) { FREE(old_text); return; } if (strcmp(old_text, new_text) == 0) matches = get_completion_matches(); XmTextReplace(w, beg, end, new_text); XmTextSetCursorPosition(w, XmTextGetLastPosition(w)); if (new_text) { FREE(new_text); new_text = NULL; } if (matches > 1) { bool need_position; clear_possible_completions(); set_save_completions(true); if (file_text) new_text = filename_completer(file_text, NULL); else new_text = command_completer(old_text, NULL); if (new_text) { FREE(new_text); new_text = NULL; } need_position = (completions_dialog == NULL); display_completions(); set_save_completions(false); if (need_position) { Position wx, wy; int xoff, yoff; Window wn; /* try to position the newly popped up help window below the text field */ XtVaGetValues(w, XmNx, &wx, XmNy, &wy, NULL); XTranslateCoordinates(XtDisplay(w), XtWindow(w), DefaultRootWindow(XtDisplay(w)), 0, 0, &xoff, &yoff, &wn); wx += xoff; wy += yoff; XtVaSetValues(completions_dialog, XmNx, wx, XmNy, wy + 140, NULL); } } if (file_text) FREE(file_text); if (old_text) FREE(old_text); } }