rofi  1.7.0
textbox.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
6  * Copyright © 2013-2021 Qball Cow <qball@gmpclient.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28 
29 #include "widgets/textbox.h"
30 #include "helper-theme.h"
31 #include "helper.h"
32 #include "keyb.h"
33 #include "mode.h"
34 #include "view.h"
35 #include <ctype.h>
36 #include <glib.h>
37 #include <math.h>
38 #include <string.h>
39 #include <xcb/xcb.h>
40 
41 #include "theme.h"
42 
44 #define DOT_OFFSET 15
45 
46 static void textbox_draw(widget *, cairo_t *);
47 static void textbox_free(widget *);
48 static int textbox_get_width(widget *);
49 static int _textbox_get_height(widget *);
50 static void __textbox_update_pango_text(textbox *tb);
51 
53 static PangoContext *p_context = NULL;
55 static PangoFontMetrics *p_metrics = NULL;
56 
58 static TBFontConfig *tbfc_default = NULL;
59 
61 static GHashTable *tbfc_cache = NULL;
62 
63 static gboolean textbox_blink(gpointer data) {
64  textbox *tb = (textbox *)data;
65  if (tb->blink < 2) {
66  tb->blink = !tb->blink;
69  } else {
70  tb->blink--;
71  }
72  return TRUE;
73 }
74 
75 static void textbox_resize(widget *wid, short w, short h) {
76  textbox *tb = (textbox *)wid;
77  textbox_moveresize(tb, tb->widget.x, tb->widget.y, w, h);
78 }
80  textbox *tb = (textbox *)wid;
81  if ((tb->flags & TB_AUTOHEIGHT) == 0) {
82  return tb->widget.h;
83  }
84  if (tb->changed) {
86  }
87  int height =
88  textbox_get_estimated_height(tb, pango_layout_get_line_count(tb->layout));
89  return height;
90 }
91 
94  MouseBindingMouseDefaultAction action, gint x,
95  gint y, G_GNUC_UNUSED void *user_data) {
96  textbox *tb = (textbox *)wid;
97  switch (action) {
98  case MOUSE_CLICK_DOWN: {
99  gint i;
100  // subtract padding on left.
101  x -= widget_padding_get_left(wid);
102  gint max = textbox_get_font_width(tb);
103  // Right of text, move to end.
104  if (x >= max) {
105  textbox_cursor_end(tb);
106  } else if (x > 0) {
107  // If in range, get index.
108  pango_layout_xy_to_index(tb->layout, x * PANGO_SCALE, y * PANGO_SCALE, &i,
109  NULL);
110  textbox_cursor(tb, i);
111  }
113  }
114  case MOUSE_CLICK_UP:
115  case MOUSE_DCLICK_DOWN:
116  case MOUSE_DCLICK_UP:
117  break;
118  }
120 }
121 
123  tb->tbfc = tbfc_default;
124  const char *font = rofi_theme_get_string(WIDGET(tb), "font", NULL);
125  if (font) {
126  TBFontConfig *tbfc = g_hash_table_lookup(tbfc_cache, font);
127  if (tbfc == NULL) {
128  tbfc = g_malloc0(sizeof(TBFontConfig));
129  tbfc->pfd = pango_font_description_from_string(font);
130  if (helper_validate_font(tbfc->pfd, font)) {
131  tbfc->metrics = pango_context_get_metrics(p_context, tbfc->pfd, NULL);
132 
133  PangoLayout *layout = pango_layout_new(p_context);
134  pango_layout_set_font_description(layout, tbfc->pfd);
135  pango_layout_set_text(layout, "aAjb", -1);
136  PangoRectangle rect;
137  pango_layout_get_pixel_extents(layout, NULL, &rect);
138  tbfc->height = rect.y + rect.height;
139  g_object_unref(layout);
140 
141  // Cast away consts. (*yuck*) because table_insert does not know it is
142  // const.
143  g_hash_table_insert(tbfc_cache, (char *)font, tbfc);
144  } else {
145  pango_font_description_free(tbfc->pfd);
146  g_free(tbfc);
147  tbfc = NULL;
148  }
149  }
150  if (tbfc) {
151  // Update for used font.
152  pango_layout_set_font_description(tb->layout, tbfc->pfd);
153  tb->tbfc = tbfc;
154  }
155  }
156 }
157 
158 textbox *textbox_create(widget *parent, WidgetType type, const char *name,
160  const char *text, double xalign, double yalign) {
161  textbox *tb = g_slice_new0(textbox);
162 
163  widget_init(WIDGET(tb), parent, type, name);
164 
165  tb->widget.draw = textbox_draw;
166  tb->widget.free = textbox_free;
172  tb->flags = flags;
173  tb->emode = PANGO_ELLIPSIZE_END;
174 
175  tb->changed = FALSE;
176 
177  tb->layout = pango_layout_new(p_context);
178  textbox_font(tb, tbft);
179 
181 
182  if ((tb->flags & TB_WRAP) == TB_WRAP) {
183  pango_layout_set_wrap(tb->layout, PANGO_WRAP_WORD_CHAR);
184  }
185 
186  // Allow overriding of markup.
187  if (rofi_theme_get_boolean(WIDGET(tb), "markup",
188  (tb->flags & TB_MARKUP) == TB_MARKUP)) {
189  tb->flags |= TB_MARKUP;
190  } else {
191  tb->flags &= (~TB_MARKUP);
192  }
193 
194  const char *txt = rofi_theme_get_string(WIDGET(tb), "str", text);
195  if (txt == NULL || (*txt) == '\0') {
196  txt = rofi_theme_get_string(WIDGET(tb), "content", text);
197  }
198  const char *placeholder =
199  rofi_theme_get_string(WIDGET(tb), "placeholder", NULL);
200  if (placeholder) {
201  tb->placeholder = placeholder;
202  }
203  textbox_text(tb, txt ? txt : "");
204  textbox_cursor_end(tb);
205 
206  // auto height/width modes get handled here
207  textbox_moveresize(tb, tb->widget.x, tb->widget.y, tb->widget.w,
208  tb->widget.h);
209 
210  tb->blink_timeout = 0;
211  tb->blink = 1;
212  if ((tb->flags & TB_EDITABLE) == TB_EDITABLE) {
213  if (rofi_theme_get_boolean(WIDGET(tb), "blink", TRUE)) {
214  tb->blink_timeout = g_timeout_add(1200, textbox_blink, tb);
215  }
217  }
218 
219  tb->yalign = rofi_theme_get_double(WIDGET(tb), "vertical-align", yalign);
220  tb->yalign = MAX(0, MIN(1.0, tb->yalign));
221  tb->xalign = rofi_theme_get_double(WIDGET(tb), "horizontal-align", xalign);
222  tb->xalign = MAX(0, MIN(1.0, tb->xalign));
223 
224  return tb;
225 }
226 
230 const char *const theme_prop_names[][3] = {
232  {"normal.normal", "selected.normal", "alternate.normal"},
234  {"normal.urgent", "selected.urgent", "alternate.urgent"},
236  {"normal.active", "selected.active", "alternate.active"},
237 };
238 
240  TextBoxFontType t = tbft & STATE_MASK;
241  if (tb == NULL) {
242  return;
243  }
244  // ACTIVE has priority over URGENT if both set.
245  if (t == (URGENT | ACTIVE)) {
246  t = ACTIVE;
247  }
248  switch ((tbft & FMOD_MASK)) {
249  case HIGHLIGHT:
251  break;
252  case ALT:
254  break;
255  default:
257  break;
258  }
259  if (tb->tbft != tbft || tb->widget.state == NULL) {
261  }
262  tb->tbft = tbft;
263 }
264 
272  pango_layout_set_attributes(tb->layout, NULL);
273  if (tb->placeholder && (tb->text == NULL || tb->text[0] == 0)) {
274  tb->show_placeholder = TRUE;
275  pango_layout_set_text(tb->layout, tb->placeholder, -1);
276  return;
277  }
278  tb->show_placeholder = FALSE;
279  if ((tb->flags & TB_PASSWORD) == TB_PASSWORD) {
280  size_t l = g_utf8_strlen(tb->text, -1);
281  char string[l + 1];
282  memset(string, '*', l);
283  string[l] = '\0';
284  pango_layout_set_text(tb->layout, string, l);
285  } else if (tb->flags & TB_MARKUP || tb->tbft & MARKUP) {
286  pango_layout_set_markup(tb->layout, tb->text, -1);
287  } else {
288  pango_layout_set_text(tb->layout, tb->text, -1);
289  }
290 }
291 const char *textbox_get_visible_text(const textbox *tb) {
292  if (tb == NULL) {
293  return NULL;
294  }
295  return pango_layout_get_text(tb->layout);
296 }
298  if (tb == NULL) {
299  return NULL;
300  }
301  return pango_layout_get_attributes(tb->layout);
302 }
303 void textbox_set_pango_attributes(textbox *tb, PangoAttrList *list) {
304  if (tb == NULL) {
305  return;
306  }
307  pango_layout_set_attributes(tb->layout, list);
308 }
309 
310 // set the default text to display
311 void textbox_text(textbox *tb, const char *text) {
312  if (tb == NULL) {
313  return;
314  }
315  g_free(tb->text);
316  const gchar *last_pointer = NULL;
317 
318  if (g_utf8_validate(text, -1, &last_pointer)) {
319  tb->text = g_strdup(text);
320  } else {
321  if (last_pointer != NULL) {
322  // Copy string up to invalid character.
323  tb->text = g_strndup(text, (last_pointer - text));
324  } else {
325  tb->text = g_strdup("Invalid UTF-8 string.");
326  }
327  }
329  if (tb->flags & TB_AUTOWIDTH) {
330  textbox_moveresize(tb, tb->widget.x, tb->widget.y, tb->widget.w,
331  tb->widget.h);
332  if (WIDGET(tb)->parent) {
333  widget_update(WIDGET(tb)->parent);
334  }
335  }
336 
337  tb->cursor = MAX(0, MIN((int)g_utf8_strlen(tb->text, -1), tb->cursor));
339 }
340 
341 // within the parent handled auto width/height modes
342 void textbox_moveresize(textbox *tb, int x, int y, int w, int h) {
343  unsigned int offset = ((tb->flags & TB_INDICATOR) ? DOT_OFFSET : 0);
344  if (tb->flags & TB_AUTOWIDTH) {
345  pango_layout_set_width(tb->layout, -1);
346  w = textbox_get_font_width(tb) +
348  } else {
349  // set ellipsize
350  if ((tb->flags & TB_EDITABLE) == TB_EDITABLE) {
351  pango_layout_set_ellipsize(tb->layout, PANGO_ELLIPSIZE_MIDDLE);
352  } else if ((tb->flags & TB_WRAP) != TB_WRAP) {
353  pango_layout_set_ellipsize(tb->layout, tb->emode);
354  } else {
355  pango_layout_set_ellipsize(tb->layout, PANGO_ELLIPSIZE_NONE);
356  }
357  }
358 
359  if (tb->flags & TB_AUTOHEIGHT) {
360  // Width determines height!
361  int tw = MAX(1, w);
362  pango_layout_set_width(
363  tb->layout,
364  PANGO_SCALE *
365  (tw - widget_padding_get_padding_width(WIDGET(tb)) - offset));
366  int hd = textbox_get_height(tb);
367  h = MAX(hd, h);
368  }
369 
370  if (x != tb->widget.x || y != tb->widget.y || w != tb->widget.w ||
371  h != tb->widget.h) {
372  tb->widget.x = x;
373  tb->widget.y = y;
374  tb->widget.h = MAX(1, h);
375  tb->widget.w = MAX(1, w);
376  }
377 
378  // We always want to update this
379  pango_layout_set_width(
380  tb->layout,
381  PANGO_SCALE * (tb->widget.w -
384 }
385 
386 // will also unmap the window if still displayed
387 static void textbox_free(widget *wid) {
388  if (wid == NULL) {
389  return;
390  }
391  textbox *tb = (textbox *)wid;
392  if (tb->blink_timeout > 0) {
393  g_source_remove(tb->blink_timeout);
394  tb->blink_timeout = 0;
395  }
396  g_free(tb->text);
397 
398  if (tb->layout != NULL) {
399  g_object_unref(tb->layout);
400  }
401 
402  g_slice_free(textbox, tb);
403 }
404 
405 static void textbox_draw(widget *wid, cairo_t *draw) {
406  if (wid == NULL) {
407  return;
408  }
409  textbox *tb = (textbox *)wid;
410  unsigned int dot_offset = ((tb->flags & TB_INDICATOR) ? DOT_OFFSET : 0);
411 
412  if (tb->changed) {
414  }
415 
416  // Skip the side MARGIN on the X axis.
417  int x = widget_padding_get_left(WIDGET(tb));
418  int top = widget_padding_get_top(WIDGET(tb));
419  int y = (pango_font_metrics_get_ascent(tb->tbfc->metrics) -
420  pango_layout_get_baseline(tb->layout)) /
421  PANGO_SCALE;
422  int line_width = 0, line_height = 0;
423  // Get actual width.
424  pango_layout_get_pixel_size(tb->layout, &line_width, &line_height);
425 
426  if (tb->yalign > 0.001) {
427  int bottom = widget_padding_get_bottom(WIDGET(tb));
428  top = (tb->widget.h - bottom - line_height - top) * tb->yalign + top;
429  }
430  y += top;
431 
432  x += dot_offset;
433 
434  if (tb->xalign > 0.001) {
435  int rem =
436  MAX(0, tb->widget.w - widget_padding_get_padding_width(WIDGET(tb)) -
437  line_width);
438  x = tb->xalign * rem + widget_padding_get_left(WIDGET(tb));
439  }
440  // TODO check if this is still needed after flatning.
441  cairo_set_operator(draw, CAIRO_OPERATOR_OVER);
442  cairo_set_source_rgb(draw, 0.0, 0.0, 0.0);
443  rofi_theme_get_color(WIDGET(tb), "text-color", draw);
444 
445  if (tb->show_placeholder) {
446  rofi_theme_get_color(WIDGET(tb), "placeholder-color", draw);
447  }
448  // Set ARGB
449  // We need to set over, otherwise subpixel hinting wont work.
450  cairo_move_to(draw, x, top);
451  cairo_save(draw);
452  cairo_reset_clip(draw);
453  pango_cairo_show_layout(draw, tb->layout);
454  cairo_restore(draw);
455 
456  // draw the cursor
457  rofi_theme_get_color(WIDGET(tb), "text-color", draw);
458  if (tb->flags & TB_EDITABLE && tb->blink) {
459  // We want to place the cursor based on the text shown.
460  const char *text = pango_layout_get_text(tb->layout);
461  // Clamp the position, should not be needed, but we are paranoid.
462  int cursor_offset = MIN(tb->cursor, g_utf8_strlen(text, -1));
463  PangoRectangle pos;
464  // convert to byte location.
465  char *offset = g_utf8_offset_to_pointer(text, cursor_offset);
466  pango_layout_get_cursor_pos(tb->layout, offset - text, &pos, NULL);
467  int cursor_x = pos.x / PANGO_SCALE;
468  int cursor_y = pos.y / PANGO_SCALE;
469  int cursor_height = pos.height / PANGO_SCALE;
470  int cursor_width = 2;
471  cairo_rectangle(draw, x + cursor_x, y + cursor_y, cursor_width,
472  cursor_height);
473  cairo_fill(draw);
474  }
475 
476  if ((tb->flags & TB_INDICATOR) == TB_INDICATOR && (tb->tbft & (SELECTED))) {
477  cairo_arc(draw, DOT_OFFSET / 2.0, tb->widget.h / 2.0, 2.0, 0, 2.0 * M_PI);
478  cairo_fill(draw);
479  }
480 }
481 
482 // cursor handling for edit mode
483 void textbox_cursor(textbox *tb, int pos) {
484  if (tb == NULL) {
485  return;
486  }
487  int length = (tb->text == NULL) ? 0 : g_utf8_strlen(tb->text, -1);
488  tb->cursor = MAX(0, MIN(length, pos));
489  // Stop blink!
490  tb->blink = 3;
492 }
493 
501 static int textbox_cursor_inc(textbox *tb) {
502  int old = tb->cursor;
503  textbox_cursor(tb, tb->cursor + 1);
504  return old != tb->cursor;
505 }
506 
514 static int textbox_cursor_dec(textbox *tb) {
515  int old = tb->cursor;
516  textbox_cursor(tb, tb->cursor - 1);
517  return old != tb->cursor;
518 }
519 
520 // Move word right
522  if (tb->text == NULL) {
523  return;
524  }
525  // Find word boundaries, with pango_Break?
526  gchar *c = g_utf8_offset_to_pointer(tb->text, tb->cursor);
527  while ((c = g_utf8_next_char(c))) {
528  gunichar uc = g_utf8_get_char(c);
529  GUnicodeBreakType bt = g_unichar_break_type(uc);
530  if ((bt == G_UNICODE_BREAK_ALPHABETIC ||
531  bt == G_UNICODE_BREAK_HEBREW_LETTER || bt == G_UNICODE_BREAK_NUMERIC ||
532  bt == G_UNICODE_BREAK_QUOTATION)) {
533  break;
534  }
535  }
536  if (c == NULL || *c == '\0') {
537  return;
538  }
539  while ((c = g_utf8_next_char(c))) {
540  gunichar uc = g_utf8_get_char(c);
541  GUnicodeBreakType bt = g_unichar_break_type(uc);
542  if (!(bt == G_UNICODE_BREAK_ALPHABETIC ||
543  bt == G_UNICODE_BREAK_HEBREW_LETTER ||
544  bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION)) {
545  break;
546  }
547  }
548  int index = g_utf8_pointer_to_offset(tb->text, c);
549  textbox_cursor(tb, index);
550 }
551 // move word left
553  // Find word boundaries, with pango_Break?
554  gchar *n;
555  gchar *c = g_utf8_offset_to_pointer(tb->text, tb->cursor);
556  while ((c = g_utf8_prev_char(c)) && c != tb->text) {
557  gunichar uc = g_utf8_get_char(c);
558  GUnicodeBreakType bt = g_unichar_break_type(uc);
559  if ((bt == G_UNICODE_BREAK_ALPHABETIC ||
560  bt == G_UNICODE_BREAK_HEBREW_LETTER || bt == G_UNICODE_BREAK_NUMERIC ||
561  bt == G_UNICODE_BREAK_QUOTATION)) {
562  break;
563  }
564  }
565  if (c != tb->text) {
566  while ((n = g_utf8_prev_char(c))) {
567  gunichar uc = g_utf8_get_char(n);
568  GUnicodeBreakType bt = g_unichar_break_type(uc);
569  if (!(bt == G_UNICODE_BREAK_ALPHABETIC ||
570  bt == G_UNICODE_BREAK_HEBREW_LETTER ||
571  bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION)) {
572  break;
573  }
574  c = n;
575  if (n == tb->text) {
576  break;
577  }
578  }
579  }
580  int index = g_utf8_pointer_to_offset(tb->text, c);
581  textbox_cursor(tb, index);
582 }
583 
584 // end of line
586  if (tb->text == NULL) {
587  tb->cursor = 0;
589  return;
590  }
591  tb->cursor = (int)g_utf8_strlen(tb->text, -1);
593  // Stop blink!
594  tb->blink = 2;
595 }
596 
597 // insert text
598 void textbox_insert(textbox *tb, const int char_pos, const char *str,
599  const int slen) {
600  if (tb == NULL) {
601  return;
602  }
603  char *c = g_utf8_offset_to_pointer(tb->text, char_pos);
604  int pos = c - tb->text;
605  int len = (int)strlen(tb->text);
606  pos = MAX(0, MIN(len, pos));
607  // expand buffer
608  tb->text = g_realloc(tb->text, len + slen + 1);
609  // move everything after cursor upward
610  char *at = tb->text + pos;
611  memmove(at + slen, at, len - pos + 1);
612  // insert new str
613  memmove(at, str, slen);
614 
615  // Set modified, lay out need te be redrawn
616  // Stop blink!
617  tb->blink = 2;
618  tb->changed = TRUE;
619 }
620 
621 // remove text
622 void textbox_delete(textbox *tb, int pos, int dlen) {
623  if (tb == NULL) {
624  return;
625  }
626  int len = g_utf8_strlen(tb->text, -1);
627  if (len == pos) {
628  return;
629  }
630  pos = MAX(0, MIN(len, pos));
631  if ((pos + dlen) > len) {
632  dlen = len - dlen;
633  }
634  // move everything after pos+dlen down
635  char *start = g_utf8_offset_to_pointer(tb->text, pos);
636  char *end = g_utf8_offset_to_pointer(tb->text, pos + dlen);
637  // Move remainder + closing \0
638  memmove(start, end, (tb->text + strlen(tb->text)) - end + 1);
639  if (tb->cursor >= pos && tb->cursor < (pos + dlen)) {
640  tb->cursor = pos;
641  } else if (tb->cursor >= (pos + dlen)) {
642  tb->cursor -= dlen;
643  }
644  // Set modified, lay out need te be redrawn
645  // Stop blink!
646  tb->blink = 2;
647  tb->changed = TRUE;
648 }
649 
655 static void textbox_cursor_del(textbox *tb) {
656  if (tb == NULL || tb->text == NULL) {
657  return;
658  }
659  textbox_delete(tb, tb->cursor, 1);
660 }
661 
667 static void textbox_cursor_bkspc(textbox *tb) {
668  if (tb && tb->cursor > 0) {
669  textbox_cursor_dec(tb);
670  textbox_cursor_del(tb);
671  }
672 }
674  if (tb && tb->cursor > 0) {
675  int cursor = tb->cursor;
677  if (cursor > tb->cursor) {
678  textbox_delete(tb, tb->cursor, cursor - tb->cursor);
679  }
680  }
681 }
682 static void textbox_cursor_del_eol(textbox *tb) {
683  if (tb && tb->cursor >= 0) {
684  int length = g_utf8_strlen(tb->text, -1) - tb->cursor;
685  if (length >= 0) {
686  textbox_delete(tb, tb->cursor, length);
687  }
688  }
689 }
690 static void textbox_cursor_del_sol(textbox *tb) {
691  if (tb && tb->cursor >= 0) {
692  int length = tb->cursor;
693  if (length >= 0) {
694  textbox_delete(tb, 0, length);
695  }
696  }
697 }
699  if (tb && tb->cursor >= 0) {
700  int cursor = tb->cursor;
702  if (cursor < tb->cursor) {
703  textbox_delete(tb, cursor, tb->cursor - cursor);
704  }
705  }
706 }
707 
708 // handle a keypress in edit mode
709 // 2 = nav
710 // 0 = unhandled
711 // 1 = handled
712 // -1 = handled and return pressed (finished)
714  if (tb == NULL) {
715  return 0;
716  }
717  if (!(tb->flags & TB_EDITABLE)) {
718  return 0;
719  }
720 
721  switch (action) {
722  // Left or Ctrl-b
723  case MOVE_CHAR_BACK:
724  return (textbox_cursor_dec(tb) == TRUE) ? 2 : 0;
725  // Right or Ctrl-F
726  case MOVE_CHAR_FORWARD:
727  return (textbox_cursor_inc(tb) == TRUE) ? 2 : 0;
728  // Ctrl-U: Kill from the beginning to the end of the line.
729  case CLEAR_LINE:
730  textbox_text(tb, "");
731  return 1;
732  // Ctrl-A
733  case MOVE_FRONT:
734  textbox_cursor(tb, 0);
735  return 2;
736  // Ctrl-E
737  case MOVE_END:
738  textbox_cursor_end(tb);
739  return 2;
740  // Ctrl-Alt-h
741  case REMOVE_WORD_BACK:
743  return 1;
744  // Ctrl-Alt-d
745  case REMOVE_WORD_FORWARD:
747  return 1;
748  case REMOVE_TO_EOL:
750  return 1;
751  case REMOVE_TO_SOL:
753  return 1;
754  // Delete or Ctrl-D
755  case REMOVE_CHAR_FORWARD:
756  textbox_cursor_del(tb);
757  return 1;
758  // Alt-B, Ctrl-Left
759  case MOVE_WORD_BACK:
761  return 2;
762  // Alt-F, Ctrl-Right
763  case MOVE_WORD_FORWARD:
765  return 2;
766  // BackSpace, Shift-BackSpace, Ctrl-h
767  case REMOVE_CHAR_BACK:
769  return 1;
770  default:
771  g_return_val_if_reached(0);
772  }
773 }
774 
775 gboolean textbox_append_text(textbox *tb, const char *pad, const int pad_len) {
776  if (tb == NULL) {
777  return FALSE;
778  }
779  if (!(tb->flags & TB_EDITABLE)) {
780  return FALSE;
781  }
782 
783  // Filter When alt/ctrl is pressed do not accept the character.
784 
785  gboolean used_something = FALSE;
786  const gchar *w, *n, *e;
787  for (w = pad, n = g_utf8_next_char(w), e = w + pad_len; w < e;
788  w = n, n = g_utf8_next_char(n)) {
789  if (g_unichar_iscntrl(g_utf8_get_char(w))) {
790  continue;
791  }
792  textbox_insert(tb, tb->cursor, w, n - w);
793  textbox_cursor(tb, tb->cursor + 1);
794  used_something = TRUE;
795  }
796  return used_something;
797 }
798 
799 static void tbfc_entry_free(TBFontConfig *tbfc) {
800  pango_font_metrics_unref(tbfc->metrics);
801  if (tbfc->pfd) {
802  pango_font_description_free(tbfc->pfd);
803  }
804  g_free(tbfc);
805 }
806 void textbox_setup(void) {
807  tbfc_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
808  (GDestroyNotify)tbfc_entry_free);
809 }
810 
812 const char *default_font_name = "default";
813 void textbox_set_pango_context(const char *font, PangoContext *p) {
814  g_assert(p_metrics == NULL);
815  p_context = g_object_ref(p);
816  p_metrics = pango_context_get_metrics(p_context, NULL, NULL);
817  TBFontConfig *tbfc = g_malloc0(sizeof(TBFontConfig));
818  tbfc->metrics = p_metrics;
819 
820  PangoLayout *layout = pango_layout_new(p_context);
821  pango_layout_set_text(layout, "aAjb", -1);
822  PangoRectangle rect;
823  pango_layout_get_pixel_extents(layout, NULL, &rect);
824  tbfc->height = rect.y + rect.height;
825  g_object_unref(layout);
826  tbfc_default = tbfc;
827 
828  g_hash_table_insert(tbfc_cache, (gpointer *)(font ? font : default_font_name),
829  tbfc);
830 }
831 
832 void textbox_cleanup(void) {
833  g_hash_table_destroy(tbfc_cache);
834  if (p_context) {
835  g_object_unref(p_context);
836  p_context = NULL;
837  }
838 }
839 
841  textbox *tb = (textbox *)wid;
842  if (tb->flags & TB_AUTOWIDTH) {
843  unsigned int offset = (tb->flags & TB_INDICATOR) ? DOT_OFFSET : 0;
845  offset;
846  }
847  return tb->widget.w;
848 }
849 
851  textbox *tb = (textbox *)wid;
852  if (tb->flags & TB_AUTOHEIGHT) {
854  tb, pango_layout_get_line_count(tb->layout));
855  }
856  return tb->widget.h;
857 }
858 int textbox_get_height(const textbox *tb) {
859  return textbox_get_font_height(tb) +
861 }
862 
864  PangoRectangle rect;
865  pango_layout_get_pixel_extents(tb->layout, NULL, &rect);
866  return rect.height + rect.y;
867 }
868 
870  PangoRectangle rect;
871  pango_layout_get_pixel_extents(tb->layout, NULL, &rect);
872  return rect.width + rect.x;
873 }
874 
877 
879 static double char_width = -1;
881  if (char_width < 0) {
882  int width = pango_font_metrics_get_approximate_char_width(p_metrics);
883  char_width = (width) / (double)PANGO_SCALE;
884  }
885  return char_width;
886 }
887 
889 static double ch_width = -1;
891  if (ch_width < 0) {
892  int width = pango_font_metrics_get_approximate_digit_width(p_metrics);
893  ch_width = (width) / (double)PANGO_SCALE;
894  }
895  return ch_width;
896 }
897 
898 int textbox_get_estimated_height(const textbox *tb, int eh) {
899  int height = tb->tbfc->height;
900  return (eh * height) + widget_padding_get_padding_height(WIDGET(tb));
901 }
903  if (wid == NULL) {
904  return 0;
905  }
906  textbox *tb = (textbox *)wid;
907  unsigned int offset = ((tb->flags & TB_INDICATOR) ? DOT_OFFSET : 0);
908  if (wid->expand && tb->flags & TB_AUTOWIDTH) {
910  offset;
911  }
912  RofiDistance w = rofi_theme_get_distance(WIDGET(tb), "width", 0);
914  if (wi > 0) {
915  return wi;
916  }
917  int padding = widget_padding_get_left(WIDGET(tb));
918  padding += widget_padding_get_right(WIDGET(tb));
919  int old_width = pango_layout_get_width(tb->layout);
920  pango_layout_set_width(tb->layout, -1);
921  int width = textbox_get_font_width(tb);
922  // Restore.
923  pango_layout_set_width(tb->layout, old_width);
924  return width + padding + offset;
925 }
926 
927 void textbox_set_ellipsize(textbox *tb, PangoEllipsizeMode mode) {
928  if (tb) {
929  tb->emode = mode;
930  if ((tb->flags & TB_WRAP) != TB_WRAP) {
931  // Store the mode.
932  pango_layout_set_ellipsize(tb->layout, tb->emode);
934  }
935  }
936 }
gboolean helper_validate_font(PangoFontDescription *pfd, const char *font)
Definition: helper.c:584
KeyBindingAction
Definition: keyb.h:58
MouseBindingMouseDefaultAction
Definition: keyb.h:166
@ REMOVE_TO_SOL
Definition: keyb.h:88
@ MOVE_FRONT
Definition: keyb.h:66
@ REMOVE_WORD_FORWARD
Definition: keyb.h:80
@ REMOVE_WORD_BACK
Definition: keyb.h:78
@ MOVE_CHAR_FORWARD
Definition: keyb.h:76
@ MOVE_WORD_FORWARD
Definition: keyb.h:72
@ REMOVE_TO_EOL
Definition: keyb.h:86
@ MOVE_WORD_BACK
Definition: keyb.h:70
@ MOVE_END
Definition: keyb.h:68
@ REMOVE_CHAR_BACK
Definition: keyb.h:84
@ CLEAR_LINE
Definition: keyb.h:64
@ MOVE_CHAR_BACK
Definition: keyb.h:74
@ REMOVE_CHAR_FORWARD
Definition: keyb.h:82
@ MOUSE_CLICK_DOWN
Definition: keyb.h:167
@ MOUSE_DCLICK_UP
Definition: keyb.h:170
@ MOUSE_CLICK_UP
Definition: keyb.h:168
@ MOUSE_DCLICK_DOWN
Definition: keyb.h:169
int textbox_get_height(const textbox *tb)
Definition: textbox.c:858
void textbox_insert(textbox *tb, const int char_pos, const char *str, const int slen)
Definition: textbox.c:598
void textbox_font(textbox *tb, TextBoxFontType tbft)
Definition: textbox.c:239
TextboxFlags
Definition: textbox.h:89
void textbox_delete(textbox *tb, int pos, int dlen)
Definition: textbox.c:622
int textbox_keybinding(textbox *tb, KeyBindingAction action)
Definition: textbox.c:713
TextBoxFontType
Definition: textbox.h:101
void textbox_cleanup(void)
Definition: textbox.c:832
double textbox_get_estimated_char_width(void)
Definition: textbox.c:880
int textbox_get_font_height(const textbox *tb)
Definition: textbox.c:863
void textbox_set_pango_attributes(textbox *tb, PangoAttrList *list)
Definition: textbox.c:303
void textbox_set_ellipsize(textbox *tb, PangoEllipsizeMode mode)
Definition: textbox.c:927
void textbox_setup(void)
Definition: textbox.c:806
const char * textbox_get_visible_text(const textbox *tb)
Definition: textbox.c:291
double textbox_get_estimated_char_height(void)
Definition: textbox.c:876
int textbox_get_desired_width(widget *wid)
Definition: textbox.c:902
PangoAttrList * textbox_get_pango_attributes(textbox *tb)
Definition: textbox.c:297
textbox * textbox_create(widget *parent, WidgetType type, const char *name, TextboxFlags flags, TextBoxFontType tbft, const char *text, double xalign, double yalign)
Definition: textbox.c:158
int textbox_get_estimated_height(const textbox *tb, int eh)
Definition: textbox.c:898
void textbox_cursor(textbox *tb, int pos)
Definition: textbox.c:483
void textbox_set_pango_context(const char *font, PangoContext *p)
Definition: textbox.c:813
int textbox_get_font_width(const textbox *tb)
Definition: textbox.c:869
void textbox_cursor_end(textbox *tb)
Definition: textbox.c:585
gboolean textbox_append_text(textbox *tb, const char *pad, const int pad_len)
Definition: textbox.c:775
void textbox_moveresize(textbox *tb, int x, int y, int w, int h)
Definition: textbox.c:342
void textbox_text(textbox *tb, const char *text)
Definition: textbox.c:311
double textbox_get_estimated_ch(void)
Definition: textbox.c:890
@ TB_INDICATOR
Definition: textbox.h:96
@ TB_AUTOHEIGHT
Definition: textbox.h:90
@ TB_PASSWORD
Definition: textbox.h:95
@ TB_MARKUP
Definition: textbox.h:93
@ TB_WRAP
Definition: textbox.h:94
@ TB_EDITABLE
Definition: textbox.h:92
@ TB_AUTOWIDTH
Definition: textbox.h:91
@ SELECTED
Definition: textbox.h:109
@ URGENT
Definition: textbox.h:105
@ ACTIVE
Definition: textbox.h:107
@ HIGHLIGHT
Definition: textbox.h:116
@ STATE_MASK
Definition: textbox.h:120
@ ALT
Definition: textbox.h:114
@ FMOD_MASK
Definition: textbox.h:118
@ MARKUP
Definition: textbox.h:111
void rofi_view_queue_redraw(void)
Definition: view.c:504
void widget_queue_redraw(widget *wid)
Definition: widget.c:509
WidgetType
Definition: widget.h:56
void widget_update(widget *widget)
Definition: widget.c:499
#define WIDGET(a)
Definition: widget.h:119
WidgetTriggerActionResult
Definition: widget.h:76
@ WIDGET_TRIGGER_ACTION_RESULT_HANDLED
Definition: widget.h:80
@ WIDGET_TRIGGER_ACTION_RESULT_IGNORED
Definition: widget.h:78
@ ROFI_ORIENTATION_HORIZONTAL
Definition: rofi-types.h:136
double height
Definition: textbox.h:55
PangoFontMetrics * metrics
Definition: textbox.h:53
PangoFontDescription * pfd
Definition: textbox.h:51
void(* free)(struct _widget *widget)
const char * state
widget_trigger_action_cb trigger_action
int(* get_desired_height)(struct _widget *)
int(* get_width)(struct _widget *)
int(* get_height)(struct _widget *)
gboolean expand
void(* draw)(struct _widget *widget, cairo_t *draw)
void(* resize)(struct _widget *, short, short)
int(* get_desired_width)(struct _widget *)
int blink
Definition: textbox.h:73
const char * placeholder
Definition: textbox.h:66
char * text
Definition: textbox.h:65
short cursor
Definition: textbox.h:64
PangoEllipsizeMode emode
Definition: textbox.h:81
double yalign
Definition: textbox.h:76
widget widget
Definition: textbox.h:62
int tbft
Definition: textbox.h:69
double xalign
Definition: textbox.h:77
guint blink_timeout
Definition: textbox.h:74
int show_placeholder
Definition: textbox.h:67
PangoLayout * layout
Definition: textbox.h:68
TBFontConfig * tbfc
Definition: textbox.h:79
unsigned long flags
Definition: textbox.h:63
int changed
Definition: textbox.h:71
static TBFontConfig * tbfc_default
Definition: textbox.c:58
static PangoContext * p_context
Definition: textbox.c:53
static int textbox_get_width(widget *)
Definition: textbox.c:840
static int textbox_get_desired_height(widget *wid)
Definition: textbox.c:79
static void textbox_cursor_dec_word(textbox *tb)
Definition: textbox.c:552
static void textbox_cursor_inc_word(textbox *tb)
Definition: textbox.c:521
static gboolean textbox_blink(gpointer data)
Definition: textbox.c:63
static WidgetTriggerActionResult textbox_editable_trigger_action(widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data)
Definition: textbox.c:93
const char *const theme_prop_names[][3]
Definition: textbox.c:230
const char * default_font_name
Definition: textbox.c:812
#define DOT_OFFSET
Definition: textbox.c:44
static double ch_width
Definition: textbox.c:889
static int textbox_cursor_inc(textbox *tb)
Definition: textbox.c:501
static void textbox_free(widget *)
Definition: textbox.c:387
static void textbox_initialize_font(textbox *tb)
Definition: textbox.c:122
static void textbox_resize(widget *wid, short w, short h)
Definition: textbox.c:75
static void textbox_cursor_del_sol(textbox *tb)
Definition: textbox.c:690
static void textbox_cursor_bkspc(textbox *tb)
Definition: textbox.c:667
static void textbox_cursor_bkspc_word(textbox *tb)
Definition: textbox.c:673
static void textbox_draw(widget *, cairo_t *)
Definition: textbox.c:405
static void textbox_cursor_del_word(textbox *tb)
Definition: textbox.c:698
static PangoFontMetrics * p_metrics
Definition: textbox.c:55
static void textbox_cursor_del(textbox *tb)
Definition: textbox.c:655
static double char_width
Definition: textbox.c:879
static void __textbox_update_pango_text(textbox *tb)
Definition: textbox.c:271
static int _textbox_get_height(widget *)
Definition: textbox.c:850
static void textbox_cursor_del_eol(textbox *tb)
Definition: textbox.c:682
static int textbox_cursor_dec(textbox *tb)
Definition: textbox.c:514
static void tbfc_entry_free(TBFontConfig *tbfc)
Definition: textbox.c:799
static GHashTable * tbfc_cache
Definition: textbox.c:61
RofiDistance rofi_theme_get_distance(const widget *widget, const char *property, int def)
Definition: theme.c:830
int rofi_theme_get_boolean(const widget *widget, const char *property, int def)
Definition: theme.c:856
int distance_get_pixel(RofiDistance d, RofiOrientation ori)
Definition: theme.c:1282
void rofi_theme_get_color(const widget *widget, const char *property, cairo_t *d)
Definition: theme.c:996
double rofi_theme_get_double(const widget *widget, const char *property, double def)
Definition: theme.c:969
const char * rofi_theme_get_string(const widget *widget, const char *property, const char *def)
Definition: theme.c:941
MenuFlags flags
Definition: view.c:107
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition: widget.c:44
void widget_set_state(widget *widget, const char *state)
Definition: widget.c:72
int widget_padding_get_padding_width(const widget *wid)
Definition: widget.c:642
int widget_padding_get_left(const widget *wid)
Definition: widget.c:581
int widget_padding_get_right(const widget *wid)
Definition: widget.c:591
int widget_padding_get_padding_height(const widget *wid)
Definition: widget.c:636
int widget_padding_get_top(const widget *wid)
Definition: widget.c:603
int widget_padding_get_bottom(const widget *wid)
Definition: widget.c:613