static int motTextSetSpinValueAttrib(Ihandle* ih, const char* value) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int pos; if (iupStrToInt(value, &pos)) { char* value = NULL; int min, max; iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1"); XtVaGetValues(ih->handle, XmNminimumValue, &min, XmNmaximumValue, &max, NULL); if (pos < min) pos = min; if (pos > max) pos = max; if (iupAttribGet(ih, "_IUPMOT_SPIN_NOAUTO")) value = XmTextGetString(ih->handle); XtVaSetValues(ih->handle, XmNposition, pos, NULL); if (value) { XmTextSetString(ih->handle, (char*)value); XtFree(value); } iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL); return 1; } } return 0; }
static void motTextLayoutUpdateMethod(Ihandle* ih) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { /* avoid abort in X */ if (ih->currentwidth == 0) ih->currentwidth = 2; if (ih->currentheight == 0) ih->currentheight = 1; XtVaSetValues(spinbox, XmNwidth, (XtArgVal)ih->currentwidth, XmNheight, (XtArgVal)ih->currentheight, XmNarrowSize, ih->currentheight/2, NULL); XtVaSetValues(ih->handle, XmNwidth, (XtArgVal)ih->currentwidth-ih->currentheight/2, XmNheight, (XtArgVal)ih->currentheight, NULL); iupmotSetPosition(spinbox, ih->x, ih->y); } else { /* to avoid the Scrollbar warning */ XtAppSetWarningHandler(iupmot_appcontext, motDummyXtErrorHandler); iupdrvBaseLayoutUpdateMethod(ih); XtAppSetWarningHandler(iupmot_appcontext, NULL); } }
static void motTextLayoutUpdateMethod(Ihandle* ih) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { /* avoid abort in X */ if (ih->currentwidth == 0) ih->currentwidth = 1; if (ih->currentheight == 0) ih->currentheight = 1; XtVaSetValues(ih->handle, XmNwidth, (XtArgVal)ih->currentwidth-ih->currentheight/2, XmNheight, (XtArgVal)ih->currentheight, NULL); XtVaSetValues(spinbox, XmNx, (XtArgVal)ih->x, XmNy, (XtArgVal)ih->y, XmNwidth, (XtArgVal)ih->currentwidth, XmNheight, (XtArgVal)ih->currentheight, XmNarrowSize, ih->currentheight/2, NULL); } else iupdrvBaseLayoutUpdateMethod(ih); }
static char* motTextGetSpinValueAttrib(Ihandle* ih) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int pos; XtVaGetValues(ih->handle, XmNposition, &pos, NULL); return iupStrReturnInt(pos); } return NULL; }
static char* motTextGetSpinValueAttrib(Ihandle* ih) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int pos; char *str = iupStrGetMemory(50); XtVaGetValues(ih->handle, XmNposition, &pos, NULL); sprintf(str, "%d", pos); return str; } return NULL; }
static int motTextSetSpinIncAttrib(Ihandle* ih, const char* value) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int inc; if (iupStrToInt(value, &inc)) { iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1"); XtVaSetValues(ih->handle, XmNincrementValue, inc, NULL); iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL); } } return 1; }
static int motTextSetSpinMinAttrib(Ihandle* ih, const char* value) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int min; if (iupStrToInt(value, &min)) { iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1"); XtVaSetValues(ih->handle, XmNminimumValue, min, NULL); iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL); } } return 1; }
static int motTextSetSpinIncAttrib(Ihandle* ih, const char* value) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int inc; if (iupStrToInt(value, &inc)) { ih->data->disable_callbacks = 1; XtVaSetValues(ih->handle, XmNincrementValue, inc, NULL); ih->data->disable_callbacks = 0; } } return 1; }
static int motTextSetSpinMinAttrib(Ihandle* ih, const char* value) { Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { int min; if (iupStrToInt(value, &min)) { ih->data->disable_callbacks = 1; XtVaSetValues(ih->handle, XmNminimumValue, min, NULL); ih->data->disable_callbacks = 0; } } return 1; }
static void motTextKeyPressEvent(Widget w, Ihandle *ih, XKeyEvent *evt, Boolean *cont) { Widget spinbox; *cont = True; iupmotKeyPressEvent(w, ih, (XEvent*)evt, cont); if (*cont == False) return; if (evt->state & ControlMask && !(state & Mod1Mask || state & Mod5Mask)) /* Ctrl but NOT Alt */ { KeySym motcode = iupmotKeycodeToKeysym(evt); if (motcode == XK_c || motcode == XK_x || motcode == XK_v || motcode == XK_a) { ih->data->disable_callbacks = -1; /* let callbacks be processed in motTextSetClipboardAttrib */ if (motcode == XK_c) motTextSetClipboardAttrib(ih, "COPY"); else if (motcode == XK_x) motTextSetClipboardAttrib(ih, "CUT"); else if (motcode == XK_v) motTextSetClipboardAttrib(ih, "PASTE"); else if (motcode == XK_a) XmTextSetSelection(ih->handle, 0, XmTextGetLastPosition(ih->handle), CurrentTime); ih->data->disable_callbacks = 0; *cont = False; return; } } spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { KeySym motcode = iupmotKeycodeToKeysym(evt); if (motcode == XK_Left || motcode == XK_Right) { /* avoid spin increment using Left/Right arrows, but must manually handle the new cursor position */ XmTextPosition caret_pos = XmTextGetInsertionPosition(ih->handle); XmTextSetInsertionPosition(ih->handle, (motcode == XK_Left)? caret_pos-1: caret_pos+1); *cont = False; } } }
static void motTextKeyPressEvent(Widget w, Ihandle *ih, XKeyEvent *evt, Boolean *cont) { Widget spinbox; *cont = True; iupmotKeyPressEvent(w, ih, (XEvent*)evt, cont); if (*cont == False) return; if (evt->state & ControlMask) /* Ctrl */ { KeySym motcode = XKeycodeToKeysym(iupmot_display, evt->keycode, 0); if (motcode == XK_c) motTextSetClipboardAttrib(ih, "COPY"); else if (motcode == XK_x) motTextSetClipboardAttrib(ih, "CUT"); else if (motcode == XK_v) motTextSetClipboardAttrib(ih, "PASTE"); else if (motcode == XK_a) XmTextSetSelection(ih->handle, 0, XmTextGetLastPosition(ih->handle), CurrentTime); } spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox)) { KeySym motcode = XKeycodeToKeysym(iupmot_display, evt->keycode, 0); if (motcode == XK_Left || motcode == XK_Right) { /* avoid spin increment using Left/Right arrows, but must manually handle the new cursor position */ XmTextPosition caret_pos = XmTextGetInsertionPosition(ih->handle); XmTextSetInsertionPosition(ih->handle, (motcode == XK_Left)? caret_pos-1: caret_pos+1); *cont = False; } } }
static void motTextModifyVerifyCallback(Widget w, Ihandle *ih, XmTextVerifyPtr text) { int start, end, key = 0; char *value, *new_value, *insert_value; KeySym motcode = 0; IFnis cb; if (iupAttribGet(ih, "_IUPMOT_DISABLE_TEXT_CB")) return; if (iupAttribGet(ih, "_IUPMOT_SPIN_DISABLE_TEXT_CB")) { if (iupAttribGet(ih, "_IUPMOT_SPIN_NOAUTO")) text->doit = False; iupAttribSetStr(ih, "_IUPMOT_SPIN_DISABLE_TEXT_CB", NULL); return; } cb = (IFnis)IupGetCallback(ih, "ACTION"); if (!cb && !ih->data->mask) return; if (text->event && text->event->type == KeyPress) { unsigned int state = ((XKeyEvent*)text->event)->state; if (state & ControlMask || /* Ctrl */ state & Mod1Mask || state & Mod5Mask || /* Alt */ state & Mod4Mask) /* Apple/Win */ return; motcode = XKeycodeToKeysym(iupmot_display, ((XKeyEvent*)text->event)->keycode, 0); } value = XmTextGetString(ih->handle); start = text->startPos; end = text->endPos; insert_value = text->text->ptr; if (motcode == XK_Delete) { new_value = value; iupStrRemove(value, start, end, 1); } else if (motcode == XK_BackSpace) { new_value = value; iupStrRemove(value, start, end, -1); } else { if (!value) new_value = iupStrDup(insert_value); else if (insert_value) new_value = iupStrInsert(value, insert_value, start, end); else new_value = value; } if (insert_value && insert_value[0]!=0 && insert_value[1]==0) key = insert_value[0]; if (ih->data->mask && iupMaskCheck(ih->data->mask, new_value)==0) { if (new_value != value) free(new_value); XtFree(value); text->doit = False; /* abort processing */ return; } if (cb) { int cb_ret = cb(ih, key, (char*)new_value); if (cb_ret==IUP_IGNORE) text->doit = False; /* abort processing */ else if (cb_ret==IUP_CLOSE) { IupExitLoop(); text->doit = False; /* abort processing */ } else if (cb_ret!=0 && key!=0 && cb_ret != IUP_DEFAULT && cb_ret != IUP_CONTINUE) { insert_value[0] = (char)cb_ret; /* replace key */ } } if (text->doit) { /* Spin is not automatically updated when you directly edit the text */ Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox) && !iupAttribGet(ih, "_IUPMOT_SPIN_NOAUTO")) { int pos; if (iupStrToInt(new_value, &pos)) { XmTextPosition caret_pos = text->currInsert; iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1"); XtVaSetValues(ih->handle, XmNposition, pos, NULL); iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL); /* do not handle all situations, but handle the basic ones */ if (text->startPos == text->endPos) /* insert */ caret_pos++; else if (text->startPos < text->endPos && text->startPos < text->currInsert) /* backspace */ caret_pos--; XmTextSetInsertionPosition(ih->handle, caret_pos); text->doit = False; } } } if (new_value != value) free(new_value); XtFree(value); (void)w; }
static void motTextModifyVerifyCallback(Widget w, Ihandle *ih, XmTextVerifyPtr text) { int start, end, remove_dir = 0, ret; char *insert_value; KeySym motcode = 0; IFnis cb; if (ih->data->disable_callbacks) return; if (iupAttribGet(ih, "_IUPMOT_SPIN_DISABLE_TEXT_CB")) { if (iupAttribGet(ih, "_IUPMOT_SPIN_NOAUTO")) text->doit = False; iupAttribSet(ih, "_IUPMOT_SPIN_DISABLE_TEXT_CB", NULL); return; } if (text->event && text->event->type == KeyPress) { unsigned int state = ((XKeyEvent*)text->event)->state; int has_ctrl = state & ControlMask; /* Ctrl */ int has_alt = state & Mod1Mask || state & Mod5Mask; /* Alt */ int has_sys = state & Mod4Mask; /* Apple/Win */ /* only process when no modifiers are used */ /* except when Ctrl and Alt are pressed at the same time */ if (has_sys || (!has_ctrl && has_alt) || (has_ctrl && !has_alt)) { text->doit = False; /* abort processing */ return; } motcode = iupmotKeycodeToKeysym((XKeyEvent*)(text->event)); } cb = (IFnis)IupGetCallback(ih, "ACTION"); start = text->startPos; end = text->endPos; insert_value = text->text->ptr; if (motcode == XK_Delete) { insert_value = NULL; remove_dir = 1; } else if (motcode == XK_BackSpace) { insert_value = NULL; remove_dir = -1; } ret = iupEditCallActionCb(ih, cb, insert_value, start, end, ih->data->mask, ih->data->nc, remove_dir, 0); /* TODO: UTF8 support */ if (ret == 0) { text->doit = False; /* abort processing */ return; } if (ret != -1) { insert_value = text->text->ptr; insert_value[0] = (char)ret; /* replace key */ } if (text->doit) { /* Spin is not automatically updated when you directly edit the text */ Widget spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT"); if (spinbox && XmIsSpinBox(spinbox) && !iupAttribGet(ih, "_IUPMOT_SPIN_NOAUTO")) { XmTextPosition caret_pos = text->currInsert; /* do not handle all situations, but handle the basic ones */ if (text->startPos == text->endPos) /* insert */ caret_pos++; else if (text->startPos < text->endPos && text->startPos < text->currInsert) /* backspace */ caret_pos--; XmTextSetInsertionPosition(ih->handle, caret_pos); text->doit = False; iupAttribSet(ih, "_IUPMOT_UPDATESPIN", "1"); } } (void)w; }