Просмотр исходного кода

Fix ngui Ctrl+Delete and add Ctrl+Backspace

Johannes Zellner 2 недель назад
Родитель
Сommit
8b33d495f9
6 измененных файлов с 60 добавлено и 6 удалено
  1. 4 2
      AGENTS.md
  2. 1 1
      gallery.c
  3. 11 0
      io.c
  4. 1 0
      io.h
  5. 42 3
      ngui.c
  6. 1 0
      ngui.h

+ 4 - 2
AGENTS.md

@@ -255,8 +255,10 @@ run area), so adding code no longer moves any address.
   the toolkit exports.  Buttons fire on the release edge and keep `active`
   until `ui->released`: inferring release from the sampled `ui->buttons`
   races the WM event queue and drops clicks.  Text fields support mouse
-  drag-selection, Ctrl+Left/Right word jumps, Ctrl+Delete word deletion and
-  Ctrl+A select-all; typing or Backspace/Delete replaces a selection.
+  drag-selection, Ctrl+Left/Right word jumps, Ctrl+Delete/Ctrl+Backspace word
+  deletion and Ctrl+A select-all; typing or Backspace/Delete replaces a
+  selection.  At the end of the line Ctrl+Delete removes the word before the
+  caret instead of doing nothing.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `blit.c/h`, `gpu.c`, `ati.c`, `nvidia.c`,
   `nv50.c`, `pci.h` — VESA LFB + shadow buffer.  Drawing goes into a
   `struct surface` render target: `vga_target()` is the screen's shadow buffer

+ 1 - 1
gallery.c

@@ -833,7 +833,7 @@ static void draw_inputs(struct ngui *g, struct ngui_rect card)
 			  g->theme.text_dim);
 		ngui_text(g, hx + 10, hy + 90, "Ctrl+Left/Right - words",
 			  g->theme.text_dim);
-		ngui_text(g, hx + 10, hy + 108, "Ctrl+Del - delete word",
+		ngui_text(g, hx + 10, hy + 108, "Ctrl+Del/Bksp - words",
 			  g->theme.text_dim);
 		ngui_text(g, hx + 10, hy + 126, "Drag - select, Ctrl+A all",
 			  g->theme.text_dim);

+ 11 - 0
io.c

@@ -349,8 +349,14 @@ static int kbd_process_byte(unsigned char sc)
 		kbd_extended = 0;
 		if (sc & 0x80) {
 			kbd_repeat_stop(1, sc & 0x7F);
+			if (sc == 0x9D)   /* right Ctrl release */
+				ctrl_pressed = 0;
 			return 0;  /* key release */
 		}
+		if (sc == 0x1D) {         /* right Ctrl */
+			ctrl_pressed = 1;
+			return 0;
+		}
 		if (sc == 0x4B)
 			key = ctrl_pressed ? IO_KEY_CTRL_LEFT : IO_KEY_LEFT;
 		else if (sc == 0x4D)
@@ -388,6 +394,11 @@ static int kbd_process_byte(unsigned char sc)
 		ctrl_pressed = 1;
 		return 0;
 	}
+	if (sc == 0x0E && ctrl_pressed) {
+		/* Ctrl+Backspace: delete the previous word. */
+		kbd_repeat_start(IO_KEY_CTRL_BACKSPACE, 0, sc);
+		return IO_KEY_CTRL_BACKSPACE;
+	}
 	{
 		char c = (char)scancode_to_ascii[sc];
 		if (c) {

+ 1 - 0
io.h

@@ -42,6 +42,7 @@ void io_drain_keyboard(void);
 #define IO_KEY_CTRL_LEFT  0x109
 #define IO_KEY_CTRL_RIGHT 0x10A
 #define IO_KEY_CTRL_DELETE 0x10B
+#define IO_KEY_CTRL_BACKSPACE 0x10C
 int io_poll_key(void);
 /* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
 int io_poll_key_with_hw(void);

+ 42 - 3
ngui.c

@@ -790,6 +790,26 @@ static unsigned int input_word_prev(const struct ngui_input *in,
 	return pos;
 }
 
+/* End of the word to the right of `pos`.  A caret inside a word takes the
+ * rest of that word and its trailing separators; a caret on separators takes
+ * them plus the next word. */
+static unsigned int input_word_delete_end(const struct ngui_input *in,
+					  unsigned int pos)
+{
+	if (pos < in->len && input_word_char(in->text[pos])) {
+		while (pos < in->len && input_word_char(in->text[pos]))
+			pos++;
+		while (pos < in->len && !input_word_char(in->text[pos]))
+			pos++;
+		return pos;
+	}
+	while (pos < in->len && !input_word_char(in->text[pos]))
+		pos++;
+	while (pos < in->len && input_word_char(in->text[pos]))
+		pos++;
+	return pos;
+}
+
 static void input_sel_bounds(const struct ngui_input *in,
 			     unsigned int *a, unsigned int *b)
 {
@@ -975,11 +995,30 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 				if (!input_delete_selection(in))
 					input_delete(in, 0);
 				changed = 1;
+			} else if (k == NGUI_KEY_CTRL_BACKSPACE) {
+				if (input_delete_selection(in))
+					changed = 1;
+				else
+					changed = input_delete_range(in,
+						input_word_prev(in, in->cursor),
+						in->cursor);
 			} else if (k == NGUI_KEY_CTRL_DELETE) {
-				if (input_delete_selection(in) ||
-				    input_delete_range(in, in->cursor,
-						input_word_next(in, in->cursor)))
+				if (input_delete_selection(in)) {
 					changed = 1;
+				} else {
+					unsigned int a = in->cursor;
+					unsigned int b = input_word_delete_end(in,
+									      a);
+
+					/* At the end of the line, delete the word
+					 * before the caret instead of doing
+					 * nothing. */
+					if (b == a) {
+						a = input_word_prev(in, in->cursor);
+						b = in->cursor;
+					}
+					changed = input_delete_range(in, a, b);
+				}
 			} else if (k == NGUI_KEY_LEFT) {
 				input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
 			} else if (k == NGUI_KEY_RIGHT) {

+ 1 - 0
ngui.h

@@ -32,6 +32,7 @@
 #define NGUI_KEY_CTRL_LEFT  0x109
 #define NGUI_KEY_CTRL_RIGHT 0x10A
 #define NGUI_KEY_CTRL_DELETE 0x10B
+#define NGUI_KEY_CTRL_BACKSPACE 0x10C
 
 #define NGUI_RGB(r, g, b) \
 	((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))