From 1a568e3535a19b75647a7b9e4ac2192b5e9be536 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 25 Jun 2022 13:00:06 +0100 Subject: [PATCH] New function dlg_editbox_select_range. This manipulates the selection inside an edit box, to select a specific range of characters in the contained text. The idea is that you can use it as a means of error highlighting, if the user has entered something invalid in that edit box and you want to draw their attention to the specific location of the part you're unhappy with. --- dialog.h | 2 ++ unix/dialog.c | 21 +++++++++++++++++++++ windows/controls.c | 8 ++++++++ 3 files changed, 31 insertions(+) diff --git a/dialog.h b/dialog.h index 90b995c1..fad78d16 100644 --- a/dialog.h +++ b/dialog.h @@ -573,6 +573,8 @@ void dlg_checkbox_set(dlgcontrol *ctrl, dlgparam *dp, bool checked); bool dlg_checkbox_get(dlgcontrol *ctrl, dlgparam *dp); void dlg_editbox_set(dlgcontrol *ctrl, dlgparam *dp, char const *text); char *dlg_editbox_get(dlgcontrol *ctrl, dlgparam *dp); /* result must be freed by caller */ +void dlg_editbox_select_range(dlgcontrol *ctrl, dlgparam *dp, + size_t start, size_t len); /* The `listbox' functions can also apply to combo boxes. */ void dlg_listbox_clear(dlgcontrol *ctrl, dlgparam *dp); void dlg_listbox_del(dlgcontrol *ctrl, dlgparam *dp, int index); diff --git a/unix/dialog.c b/unix/dialog.c index eda622f1..161f393d 100644 --- a/unix/dialog.c +++ b/unix/dialog.c @@ -357,6 +357,27 @@ char *dlg_editbox_get(dlgcontrol *ctrl, dlgparam *dp) unreachable("bad control type in editbox_get"); } +void dlg_editbox_select_range(dlgcontrol *ctrl, dlgparam *dp, + size_t start, size_t len) +{ + struct uctrl *uc = dlg_find_byctrl(dp, ctrl); + assert(uc->ctrl->type == CTRL_EDITBOX); + + GtkWidget *entry = NULL; + +#if GTK_CHECK_VERSION(2,4,0) + if (uc->combo) + entry = gtk_bin_get_child(GTK_BIN(uc->combo)); +#endif + + if (uc->entry) + entry = uc->entry; + + assert(entry && "we should have a GtkEntry one way or another"); + + gtk_editable_select_region(GTK_EDITABLE(entry), start, start + len); +} + #if !GTK_CHECK_VERSION(2,4,0) static void container_remove_and_destroy(GtkWidget *w, gpointer data) { diff --git a/windows/controls.c b/windows/controls.c index c0159510..7065b7be 100644 --- a/windows/controls.c +++ b/windows/controls.c @@ -2217,6 +2217,14 @@ char *dlg_editbox_get(dlgcontrol *ctrl, dlgparam *dp) return GetDlgItemText_alloc(dp->hwnd, c->base_id+1); } +void dlg_editbox_select_range(dlgcontrol *ctrl, dlgparam *dp, + size_t start, size_t len) +{ + struct winctrl *c = dlg_findbyctrl(dp, ctrl); + assert(c && c->ctrl->type == CTRL_EDITBOX); + SendDlgItemMessage(dp->hwnd, c->base_id+1, EM_SETSEL, start, start+len); +} + /* The `listbox' functions can also apply to combo boxes. */ void dlg_listbox_clear(dlgcontrol *ctrl, dlgparam *dp) {