rofi  1.6.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-2020 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 <config.h>
30 #include <xcb/xcb.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <math.h>
35 #include "widgets/textbox.h"
36 #include "keyb.h"
37 #include "helper.h"
38 #include "helper-theme.h"
39 #include "mode.h"
40 #include "view.h"
41 
42 #include "theme.h"
43 
45 #define DOT_OFFSET 15
46 
47 static void textbox_draw ( widget *, cairo_t * );
48 static void textbox_free ( widget * );
49 static int textbox_get_width ( widget * );
50 static int _textbox_get_height ( widget * );
51 static void __textbox_update_pango_text ( textbox *tb );
52 
54 static PangoContext *p_context = NULL;
56 static PangoFontMetrics *p_metrics = NULL;
57 
59 typedef struct TBFontConfig
60 {
62  PangoFontDescription *pfd;
64  PangoFontMetrics *metrics;
66  double height;
68 
70 static GHashTable *tbfc_cache = NULL;
71 
72 static gboolean textbox_blink ( gpointer data )
73 {
74  textbox *tb = (textbox *) data;
75  if ( tb->blink < 2 ) {
76  tb->blink = !tb->blink;
77  widget_queue_redraw ( WIDGET ( tb ) );
79  }
80  else {
81  tb->blink--;
82  }
83  return TRUE;
84 }
85 
86 static void textbox_resize ( widget *wid, short w, short h )
87 {
88  textbox *tb = (textbox *) wid;
89  textbox_moveresize ( tb, tb->widget.x, tb->widget.y, w, h );
90 }
92 {
93  textbox *tb = (textbox *) wid;
94  if ( ( tb->flags & TB_AUTOHEIGHT ) == 0 ) {
95  return tb->widget.h;
96  }
97  if ( tb->changed ) {
99  }
100  int height = textbox_get_estimated_height ( tb, pango_layout_get_line_count ( tb->layout ) );
101  return height;
102 }
103 
104 static WidgetTriggerActionResult textbox_editable_trigger_action ( widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data )
105 {
106  textbox *tb = (textbox *) wid;
107  switch ( action )
108  {
109  case MOUSE_CLICK_DOWN:
110  {
111  gint i;
112  // subtract padding on left.
113  x -= widget_padding_get_left ( wid );
114  gint max = textbox_get_font_width ( tb );
115  // Right of text, move to end.
116  if ( x >= max ) {
117  textbox_cursor_end ( tb );
118  }
119  else if ( x > 0 ) {
120  // If in range, get index.
121  pango_layout_xy_to_index ( tb->layout, x * PANGO_SCALE, y * PANGO_SCALE, &i, NULL );
122  textbox_cursor ( tb, i );
123  }
125  }
126  case MOUSE_CLICK_UP:
127  case MOUSE_DCLICK_DOWN:
128  case MOUSE_DCLICK_UP:
129  break;
130  }
132 }
133 
134 static void textbox_initialize_font ( textbox *tb )
135 {
136  tb->metrics = p_metrics;
137  const char * font = rofi_theme_get_string ( WIDGET ( tb ), "font", NULL );
138  if ( font ) {
139  TBFontConfig *tbfc = g_hash_table_lookup ( tbfc_cache, font );
140  if ( tbfc == NULL ) {
141  tbfc = g_malloc0 ( sizeof ( TBFontConfig ) );
142  tbfc->pfd = pango_font_description_from_string ( font );
143  if ( helper_validate_font ( tbfc->pfd, font ) ) {
144  tbfc->metrics = pango_context_get_metrics ( p_context, tbfc->pfd, NULL );
145  tbfc->height = pango_font_metrics_get_ascent ( tbfc->metrics ) + pango_font_metrics_get_descent ( tbfc->metrics );
146 
147  // Cast away consts. (*yuck*) because table_insert does not know it is const.
148  g_hash_table_insert ( tbfc_cache, (char *) font, tbfc );
149  }
150  else {
151  pango_font_description_free ( tbfc->pfd );
152  g_free ( tbfc );
153  tbfc = NULL;
154  }
155  }
156  if ( tbfc ) {
157  // Update for used font.
158  pango_layout_set_font_description ( tb->layout, tbfc->pfd );
159  tb->metrics = tbfc->metrics;
160  }
161  }
162 }
163 
164 textbox* textbox_create ( widget *parent, WidgetType type, const char *name, TextboxFlags flags, TextBoxFontType tbft, const char *text, double xalign, double yalign )
165 {
166  textbox *tb = g_slice_new0 ( textbox );
167 
168  widget_init ( WIDGET ( tb ), parent, type, name );
169 
170  tb->widget.draw = textbox_draw;
171  tb->widget.free = textbox_free;
177  tb->flags = flags;
178  tb->emode = PANGO_ELLIPSIZE_END;
179 
180  tb->changed = FALSE;
181 
182  tb->layout = pango_layout_new ( p_context );
183  textbox_font ( tb, tbft );
184 
186 
187  if ( ( flags & TB_WRAP ) == TB_WRAP ) {
188  pango_layout_set_wrap ( tb->layout, PANGO_WRAP_WORD_CHAR );
189  }
190 
191  const char *txt = rofi_theme_get_string ( WIDGET ( tb ), "str", text );
192  if ( txt == NULL || ( *txt ) == '\0' ) {
193  txt = rofi_theme_get_string ( WIDGET ( tb ), "content", text );
194  }
195  const char *placeholder = rofi_theme_get_string ( WIDGET ( tb ), "placeholder", NULL );
196  if ( placeholder ) {
197  tb->placeholder = placeholder;
198  }
199  textbox_text ( tb, txt ? txt : "" );
200  textbox_cursor_end ( tb );
201 
202  // auto height/width modes get handled here
203  textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
204 
205  tb->blink_timeout = 0;
206  tb->blink = 1;
207  if ( ( flags & TB_EDITABLE ) == TB_EDITABLE ) {
208  if ( rofi_theme_get_boolean ( WIDGET ( tb ), "blink", TRUE ) ) {
209  tb->blink_timeout = g_timeout_add ( 1200, textbox_blink, tb );
210  }
212  }
213 
214  tb->yalign = rofi_theme_get_double ( WIDGET ( tb ), "vertical-align", yalign );
215  tb->yalign = MAX ( 0, MIN ( 1.0, tb->yalign ) );
216  tb->xalign = rofi_theme_get_double ( WIDGET ( tb ), "horizontal-align", xalign );
217  tb->xalign = MAX ( 0, MIN ( 1.0, tb->xalign ) );
218 
219  return tb;
220 }
221 
225 const char *const theme_prop_names[][3] = {
227  { "normal.normal", "selected.normal", "alternate.normal" },
229  { "normal.urgent", "selected.urgent", "alternate.urgent" },
231  { "normal.active", "selected.active", "alternate.active" },
232 };
233 
235 {
236  TextBoxFontType t = tbft & STATE_MASK;
237  if ( tb == NULL ) {
238  return;
239  }
240  // ACTIVE has priority over URGENT if both set.
241  if ( t == ( URGENT | ACTIVE ) ) {
242  t = ACTIVE;
243  }
244  switch ( ( tbft & FMOD_MASK ) )
245  {
246  case HIGHLIGHT:
247  widget_set_state ( WIDGET ( tb ), theme_prop_names[t][1] );
248  break;
249  case ALT:
250  widget_set_state ( WIDGET ( tb ), theme_prop_names[t][2] );
251  break;
252  default:
253  widget_set_state ( WIDGET ( tb ), theme_prop_names[t][0] );
254  break;
255  }
256  if ( tb->tbft != tbft || tb->widget.state == NULL ) {
257  widget_queue_redraw ( WIDGET ( tb ) );
258  }
259  tb->tbft = tbft;
260 }
261 
269 {
270  pango_layout_set_attributes ( tb->layout, NULL );
271  if ( tb->placeholder && ( tb->text == NULL || tb->text[0] == 0 ) ) {
272  tb->show_placeholder = TRUE;
273  pango_layout_set_text ( tb->layout, tb->placeholder, -1 );
274  return;
275  }
276  tb->show_placeholder = FALSE;
277  if ( ( tb->flags & TB_PASSWORD ) == TB_PASSWORD ) {
278  size_t l = g_utf8_strlen ( tb->text, -1 );
279  char string [l + 1];
280  memset ( string, '*', l );
281  string[l] = '\0';
282  pango_layout_set_text ( tb->layout, string, l );
283  }
284  else if ( tb->flags & TB_MARKUP || tb->tbft & MARKUP ) {
285  pango_layout_set_markup ( tb->layout, tb->text, -1 );
286  }
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 {
293  if ( tb == NULL ) {
294  return NULL;
295  }
296  return pango_layout_get_text ( tb->layout );
297 }
298 PangoAttrList *textbox_get_pango_attributes ( textbox *tb )
299 {
300  if ( tb == NULL ) {
301  return NULL;
302  }
303  return pango_layout_get_attributes ( tb->layout );
304 }
305 void textbox_set_pango_attributes ( textbox *tb, PangoAttrList *list )
306 {
307  if ( tb == NULL ) {
308  return;
309  }
310  pango_layout_set_attributes ( tb->layout, list );
311 }
312 
313 // set the default text to display
314 void textbox_text ( textbox *tb, const char *text )
315 {
316  if ( tb == NULL ) {
317  return;
318  }
319  g_free ( tb->text );
320  const gchar *last_pointer = NULL;
321 
322  if ( g_utf8_validate ( text, -1, &last_pointer ) ) {
323  tb->text = g_strdup ( text );
324  }
325  else {
326  if ( last_pointer != NULL ) {
327  // Copy string up to invalid character.
328  tb->text = g_strndup ( text, ( last_pointer - text ) );
329  }
330  else {
331  tb->text = g_strdup ( "Invalid UTF-8 string." );
332  }
333  }
335  if ( tb->flags & TB_AUTOWIDTH ) {
336  textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
337  if ( WIDGET ( tb )->parent ) {
338  widget_update ( WIDGET ( tb )->parent );
339  }
340  }
341 
342  tb->cursor = MAX ( 0, MIN ( ( int ) g_utf8_strlen ( tb->text, -1 ), tb->cursor ) );
343  widget_queue_redraw ( WIDGET ( tb ) );
344 }
345 
346 // within the parent handled auto width/height modes
347 void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
348 {
349  unsigned int offset = ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
350  if ( tb->flags & TB_AUTOWIDTH ) {
351  pango_layout_set_width ( tb->layout, -1 );
352  w = textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( WIDGET ( tb ) ) + offset;
353  }
354  else {
355  // set ellipsize
356  if ( ( tb->flags & TB_EDITABLE ) == TB_EDITABLE ) {
357  pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_MIDDLE );
358  }
359  else if ( ( tb->flags & TB_WRAP ) != TB_WRAP ) {
360  pango_layout_set_ellipsize ( tb->layout, tb->emode );
361  }
362  else {
363  pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_NONE );
364  }
365  }
366 
367  if ( tb->flags & TB_AUTOHEIGHT ) {
368  // Width determines height!
369  int tw = MAX ( 1, w );
370  pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tw - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
371  int hd = textbox_get_height ( tb );
372  h = MAX ( hd, h );
373  }
374 
375  if ( x != tb->widget.x || y != tb->widget.y || w != tb->widget.w || h != tb->widget.h ) {
376  tb->widget.x = x;
377  tb->widget.y = y;
378  tb->widget.h = MAX ( 1, h );
379  tb->widget.w = MAX ( 1, w );
380  }
381 
382  // We always want to update this
383  pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
384  widget_queue_redraw ( WIDGET ( tb ) );
385 }
386 
387 // will also unmap the window if still displayed
388 static void textbox_free ( widget *wid )
389 {
390  if ( wid == NULL ) {
391  return;
392  }
393  textbox *tb = (textbox *) wid;
394  if ( tb->blink_timeout > 0 ) {
395  g_source_remove ( tb->blink_timeout );
396  tb->blink_timeout = 0;
397  }
398  g_free ( tb->text );
399 
400  if ( tb->layout != NULL ) {
401  g_object_unref ( tb->layout );
402  }
403 
404  g_slice_free ( textbox, tb );
405 }
406 
407 static void textbox_draw ( widget *wid, cairo_t *draw )
408 {
409  if ( wid == NULL ) {
410  return;
411  }
412  textbox *tb = (textbox *) wid;
413  unsigned int offset = ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
414 
415  if ( tb->changed ) {
417  }
418 
419  // Skip the side MARGIN on the X axis.
420  int x = widget_padding_get_left ( WIDGET ( tb ) );
421  int top = widget_padding_get_top ( WIDGET ( tb ) );
422  int y = ( pango_font_metrics_get_ascent ( tb->metrics ) - pango_layout_get_baseline ( tb->layout ) ) / PANGO_SCALE;
423  int line_width = 0, line_height = 0;
424  // Get actual width.
425  pango_layout_get_pixel_size ( tb->layout, &line_width, &line_height );
426 
427  if ( tb->yalign > 0.001 ) {
428  int bottom = widget_padding_get_bottom ( WIDGET ( tb ) );
429  top = ( tb->widget.h - bottom - line_height - top ) * tb->yalign + top;
430  }
431  y += top;
432 
433  x += offset;
434 
435  if ( tb->xalign > 0.001 ) {
436  int rem = MAX ( 0, tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - line_width );
437  x = tb->xalign * rem + widget_padding_get_left ( WIDGET ( tb ) );
438  }
439  // TODO check if this is still needed after flatning.
440  cairo_set_operator ( draw, CAIRO_OPERATOR_OVER );
441  cairo_set_source_rgb ( draw, 0.0, 0.0, 0.0 );
442  rofi_theme_get_color ( WIDGET ( tb ), "text-color", draw );
443 
444  if ( tb->show_placeholder ) {
445  rofi_theme_get_color ( WIDGET ( tb ), "placeholder-color", draw );
446  }
447  // Set ARGB
448  // We need to set over, otherwise subpixel hinting wont work.
449  cairo_move_to ( draw, x, top );
450  cairo_save ( draw );
451  cairo_reset_clip ( draw );
452  pango_cairo_show_layout ( draw, tb->layout );
453  cairo_restore ( draw );
454 
455  // draw the cursor
456  rofi_theme_get_color ( WIDGET ( tb ), "text-color", draw );
457  if ( tb->flags & TB_EDITABLE && tb->blink ) {
458  // We want to place the cursor based on the text shown.
459  const char *text = pango_layout_get_text ( tb->layout );
460  // Clamp the position, should not be needed, but we are paranoid.
461  int cursor_offset = MIN ( tb->cursor, g_utf8_strlen ( text, -1 ) );
462  PangoRectangle pos;
463  // convert to byte location.
464  char *offset = g_utf8_offset_to_pointer ( text, cursor_offset );
465  pango_layout_get_cursor_pos ( tb->layout, offset - text, &pos, NULL );
466  int cursor_x = pos.x / PANGO_SCALE;
467  int cursor_y = pos.y / PANGO_SCALE;
468  int cursor_height = pos.height / PANGO_SCALE;
469  int cursor_width = 2;
470  cairo_rectangle ( draw, x + cursor_x, y + cursor_y, cursor_width, cursor_height );
471  cairo_fill ( draw );
472  }
473 
474  if ( ( tb->flags & TB_INDICATOR ) == TB_INDICATOR && ( tb->tbft & ( SELECTED ) ) ) {
475  cairo_arc ( draw, DOT_OFFSET / 2.0, tb->widget.h / 2.0, 2.0, 0, 2.0 * M_PI );
476  cairo_fill ( draw );
477  }
478 }
479 
480 // cursor handling for edit mode
481 void textbox_cursor ( textbox *tb, int pos )
482 {
483  if ( tb == NULL ) {
484  return;
485  }
486  int length = ( tb->text == NULL ) ? 0 : g_utf8_strlen ( tb->text, -1 );
487  tb->cursor = MAX ( 0, MIN ( length, pos ) );
488  // Stop blink!
489  tb->blink = 3;
490  widget_queue_redraw ( WIDGET ( tb ) );
491 }
492 
500 static int textbox_cursor_inc ( textbox *tb )
501 {
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 {
516  int old = tb->cursor;
517  textbox_cursor ( tb, tb->cursor - 1 );
518  return old != tb->cursor;
519 }
520 
521 // Move word right
522 static void textbox_cursor_inc_word ( textbox *tb )
523 {
524  if ( tb->text == NULL ) {
525  return;
526  }
527  // Find word boundaries, with pango_Break?
528  gchar *c = g_utf8_offset_to_pointer ( tb->text, tb->cursor );
529  while ( ( c = g_utf8_next_char ( c ) ) ) {
530  gunichar uc = g_utf8_get_char ( c );
531  GUnicodeBreakType bt = g_unichar_break_type ( uc );
532  if ( ( bt == G_UNICODE_BREAK_ALPHABETIC || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
533  bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION ) ) {
534  break;
535  }
536  }
537  if ( c == NULL || *c == '\0' ) {
538  return;
539  }
540  while ( ( c = g_utf8_next_char ( c ) ) ) {
541  gunichar uc = g_utf8_get_char ( c );
542  GUnicodeBreakType bt = g_unichar_break_type ( uc );
543  if ( !( bt == G_UNICODE_BREAK_ALPHABETIC || 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
552 static void textbox_cursor_dec_word ( textbox *tb )
553 {
554  // Find word boundaries, with pango_Break?
555  gchar *n;
556  gchar *c = g_utf8_offset_to_pointer ( tb->text, tb->cursor );
557  while ( ( c = g_utf8_prev_char ( c ) ) && c != tb->text ) {
558  gunichar uc = g_utf8_get_char ( c );
559  GUnicodeBreakType bt = g_unichar_break_type ( uc );
560  if ( ( bt == G_UNICODE_BREAK_ALPHABETIC || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
561  bt == G_UNICODE_BREAK_NUMERIC || 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 || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
570  bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION ) ) {
571  break;
572  }
573  c = n;
574  if ( n == tb->text ) {
575  break;
576  }
577  }
578  }
579  int index = g_utf8_pointer_to_offset ( tb->text, c );
580  textbox_cursor ( tb, index );
581 }
582 
583 // end of line
585 {
586  if ( tb->text == NULL ) {
587  tb->cursor = 0;
588  widget_queue_redraw ( WIDGET ( tb ) );
589  return;
590  }
591  tb->cursor = ( int ) g_utf8_strlen ( tb->text, -1 );
592  widget_queue_redraw ( WIDGET ( tb ) );
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, const int slen )
599 {
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 {
624  if ( tb == NULL ) {
625  return;
626  }
627  int len = g_utf8_strlen ( tb->text, -1 );
628  if ( len == pos ) {
629  return;
630  }
631  pos = MAX ( 0, MIN ( len, pos ) );
632  if ( ( pos + dlen ) > len ) {
633  dlen = len - dlen;
634  }
635  // move everything after pos+dlen down
636  char *start = g_utf8_offset_to_pointer ( tb->text, pos );
637  char *end = g_utf8_offset_to_pointer ( tb->text, pos + dlen );
638  // Move remainder + closing \0
639  memmove ( start, end, ( tb->text + strlen ( tb->text ) ) - end + 1 );
640  if ( tb->cursor >= pos && tb->cursor < ( pos + dlen ) ) {
641  tb->cursor = pos;
642  }
643  else if ( tb->cursor >= ( pos + dlen ) ) {
644  tb->cursor -= dlen;
645  }
646  // Set modified, lay out need te be redrawn
647  // Stop blink!
648  tb->blink = 2;
649  tb->changed = TRUE;
650 }
651 
657 static void textbox_cursor_del ( textbox *tb )
658 {
659  if ( tb == NULL || tb->text == NULL ) {
660  return;
661  }
662  textbox_delete ( tb, tb->cursor, 1 );
663 }
664 
670 static void textbox_cursor_bkspc ( textbox *tb )
671 {
672  if ( tb && tb->cursor > 0 ) {
673  textbox_cursor_dec ( tb );
674  textbox_cursor_del ( tb );
675  }
676 }
678 {
679  if ( tb && tb->cursor > 0 ) {
680  int cursor = tb->cursor;
682  if ( cursor > tb->cursor ) {
683  textbox_delete ( tb, tb->cursor, cursor - tb->cursor );
684  }
685  }
686 }
687 static void textbox_cursor_del_eol ( textbox *tb )
688 {
689  if ( tb && tb->cursor >= 0 ) {
690  int length = g_utf8_strlen ( tb->text, -1 ) - tb->cursor;
691  if ( length >= 0 ) {
692  textbox_delete ( tb, tb->cursor, length );
693  }
694  }
695 }
696 static void textbox_cursor_del_sol ( textbox *tb )
697 {
698  if ( tb && tb->cursor >= 0 ) {
699  int length = tb->cursor;
700  if ( length >= 0 ) {
701  textbox_delete ( tb, 0, length );
702  }
703  }
704 }
705 static void textbox_cursor_del_word ( textbox *tb )
706 {
707  if ( tb && tb->cursor >= 0 ) {
708  int cursor = tb->cursor;
710  if ( cursor < tb->cursor ) {
711  textbox_delete ( tb, cursor, tb->cursor - cursor );
712  }
713  }
714 }
715 
716 // handle a keypress in edit mode
717 // 2 = nav
718 // 0 = unhandled
719 // 1 = handled
720 // -1 = handled and return pressed (finished)
722 {
723  if ( tb == NULL ) {
724  return 0;
725  }
726  if ( !( tb->flags & TB_EDITABLE ) ) {
727  return 0;
728  }
729 
730  switch ( action )
731  {
732  // Left or Ctrl-b
733  case MOVE_CHAR_BACK:
734  return ( textbox_cursor_dec ( tb ) == TRUE ) ? 2 : 0;
735  // Right or Ctrl-F
736  case MOVE_CHAR_FORWARD:
737  return ( textbox_cursor_inc ( tb ) == TRUE ) ? 2 : 0;
738  // Ctrl-U: Kill from the beginning to the end of the line.
739  case CLEAR_LINE:
740  textbox_text ( tb, "" );
741  return 1;
742  // Ctrl-A
743  case MOVE_FRONT:
744  textbox_cursor ( tb, 0 );
745  return 2;
746  // Ctrl-E
747  case MOVE_END:
748  textbox_cursor_end ( tb );
749  return 2;
750  // Ctrl-Alt-h
751  case REMOVE_WORD_BACK:
753  return 1;
754  // Ctrl-Alt-d
755  case REMOVE_WORD_FORWARD:
757  return 1;
758  case REMOVE_TO_EOL:
759  textbox_cursor_del_eol ( tb );
760  return 1;
761  case REMOVE_TO_SOL:
762  textbox_cursor_del_sol ( tb );
763  return 1;
764  // Delete or Ctrl-D
765  case REMOVE_CHAR_FORWARD:
766  textbox_cursor_del ( tb );
767  return 1;
768  // Alt-B, Ctrl-Left
769  case MOVE_WORD_BACK:
771  return 2;
772  // Alt-F, Ctrl-Right
773  case MOVE_WORD_FORWARD:
775  return 2;
776  // BackSpace, Shift-BackSpace, Ctrl-h
777  case REMOVE_CHAR_BACK:
778  textbox_cursor_bkspc ( tb );
779  return 1;
780  default:
781  g_return_val_if_reached ( 0 );
782  }
783 }
784 
785 gboolean textbox_append_text ( textbox *tb, const char *pad, const int pad_len )
786 {
787  if ( tb == NULL ) {
788  return FALSE;
789  }
790  if ( !( tb->flags & TB_EDITABLE ) ) {
791  return FALSE;
792  }
793 
794  // Filter When alt/ctrl is pressed do not accept the character.
795 
796  gboolean used_something = FALSE;
797  const gchar *w, *n, *e;
798  for ( w = pad, n = g_utf8_next_char ( w ), e = w + pad_len; w < e; w = n, n = g_utf8_next_char ( n ) ) {
799  if ( g_unichar_iscntrl ( g_utf8_get_char ( w ) ) ) {
800  continue;
801  }
802  textbox_insert ( tb, tb->cursor, w, n - w );
803  textbox_cursor ( tb, tb->cursor + 1 );
804  used_something = TRUE;
805  }
806  return used_something;
807 }
808 
809 static void tbfc_entry_free ( TBFontConfig *tbfc )
810 {
811  pango_font_metrics_unref ( tbfc->metrics );
812  if ( tbfc->pfd ) {
813  pango_font_description_free ( tbfc->pfd );
814  }
815  g_free ( tbfc );
816 }
817 void textbox_setup ( void )
818 {
819  tbfc_cache = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, (GDestroyNotify) tbfc_entry_free );
820 }
821 
823 const char *default_font_name = "default";
824 void textbox_set_pango_context ( const char *font, PangoContext *p )
825 {
826  g_assert ( p_metrics == NULL );
827  p_context = g_object_ref ( p );
828  p_metrics = pango_context_get_metrics ( p_context, NULL, NULL );
829  TBFontConfig *tbfc = g_malloc0 ( sizeof ( TBFontConfig ) );
830  tbfc->metrics = p_metrics;
831  tbfc->height = pango_font_metrics_get_ascent ( tbfc->metrics ) + pango_font_metrics_get_descent ( tbfc->metrics );
832  g_hash_table_insert ( tbfc_cache, (gpointer *) ( font ? font : default_font_name ), tbfc );
833 }
834 
835 void textbox_cleanup ( void )
836 {
837  g_hash_table_destroy ( tbfc_cache );
838  if ( p_context ) {
839  g_object_unref ( p_context );
840  p_context = NULL;
841  }
842 }
843 
845 {
846  textbox *tb = (textbox *) wid;
847  if ( tb->flags & TB_AUTOWIDTH ) {
848  unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
849  return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset;
850  }
851  return tb->widget.w;
852 }
853 
855 {
856  textbox *tb = (textbox *) wid;
857  if ( tb->flags & TB_AUTOHEIGHT ) {
858  return textbox_get_estimated_height ( tb, pango_layout_get_line_count ( tb->layout ) );
859  }
860  return tb->widget.h;
861 }
862 int textbox_get_height ( const textbox *tb )
863 {
865 }
866 
868 {
869  int height;
870  pango_layout_get_pixel_size ( tb->layout, NULL, &height );
871  return height;
872 }
873 
875 {
876  PangoRectangle rect;
877  pango_layout_get_pixel_extents ( tb->layout, NULL, &rect );
878  return rect.width + rect.x;
879 }
880 
882 static double char_height = -1;
884 {
885  if ( char_height < 0 ) {
886  int height = pango_font_metrics_get_ascent ( p_metrics ) + pango_font_metrics_get_descent ( p_metrics );
887  char_height = ( height ) / (double) PANGO_SCALE;
888  }
889  return char_height;
890 }
891 
893 static double char_width = -1;
895 {
896  if ( char_width < 0 ) {
897  int width = pango_font_metrics_get_approximate_char_width ( p_metrics );
898  char_width = ( width ) / (double) PANGO_SCALE;
899  }
900  return char_width;
901 }
902 
904 static double ch_width = -1;
906 {
907  if ( ch_width < 0 ) {
908  int width = pango_font_metrics_get_approximate_digit_width ( p_metrics );
909  ch_width = ( width ) / (double) PANGO_SCALE;
910  }
911  return ch_width;
912 }
913 
914 int textbox_get_estimated_height ( const textbox *tb, int eh )
915 {
916  int height = pango_font_metrics_get_ascent ( tb->metrics ) + pango_font_metrics_get_descent ( tb->metrics );
917  return ceil ( ( eh * height ) / (double) PANGO_SCALE ) + widget_padding_get_padding_height ( WIDGET ( tb ) );
918 }
920 {
921  if ( wid == NULL ) {
922  return 0;
923  }
924  textbox *tb = (textbox *) wid;
925  unsigned int offset = ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
926  if ( wid->expand && tb->flags & TB_AUTOWIDTH ) {
927  return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset;
928  }
929  RofiDistance w = rofi_theme_get_distance ( WIDGET ( tb ), "width", 0 );
931  if ( wi > 0 ) {
932  return wi;
933  }
934  int padding = widget_padding_get_left ( WIDGET ( tb ) );
935  padding += widget_padding_get_right ( WIDGET ( tb ) );
936  int old_width = pango_layout_get_width ( tb->layout );
937  pango_layout_set_width ( tb->layout, -1 );
938  int width = textbox_get_font_width ( tb );
939  // Restore.
940  pango_layout_set_width ( tb->layout, old_width );
941  return width + padding + offset;
942 }
943 
944 void textbox_set_ellipsize ( textbox *tb, PangoEllipsizeMode mode )
945 {
946  if ( tb ) {
947  tb->emode = mode;
948  if ( ( tb->flags & TB_WRAP ) != TB_WRAP ) {
949  // Store the mode.
950  pango_layout_set_ellipsize ( tb->layout, tb->emode );
951  widget_queue_redraw ( WIDGET ( tb ) );
952  }
953  }
954 }
WIDGET
#define WIDGET(a)
Definition: widget.h:115
REMOVE_WORD_BACK
@ REMOVE_WORD_BACK
Definition: keyb.h:79
rofi_theme_get_color
void rofi_theme_get_color(const widget *widget, const char *property, cairo_t *d)
Definition: theme.c:843
textbox_font
void textbox_font(textbox *tb, TextBoxFontType tbft)
Definition: textbox.c:234
textbox_cursor_bkspc_word
static void textbox_cursor_bkspc_word(textbox *tb)
Definition: textbox.c:677
textbox::flags
unsigned long flags
Definition: textbox.h:53
MOVE_CHAR_BACK
@ MOVE_CHAR_BACK
Definition: keyb.h:75
WidgetType
WidgetType
Definition: widget.h:57
REMOVE_WORD_FORWARD
@ REMOVE_WORD_FORWARD
Definition: keyb.h:81
textbox_cursor_del_eol
static void textbox_cursor_del_eol(textbox *tb)
Definition: textbox.c:687
_textbox_get_height
static int _textbox_get_height(widget *)
Definition: textbox.c:854
ACTIVE
@ ACTIVE
Definition: textbox.h:98
TBFontConfig
struct TBFontConfig TBFontConfig
textbox_get_desired_width
int textbox_get_desired_width(widget *wid)
Definition: textbox.c:919
_widget::expand
gboolean expand
Definition: widget-internal.h:60
tbfc_cache
static GHashTable * tbfc_cache
Definition: textbox.c:70
_widget::get_height
int(* get_height)(struct _widget *)
Definition: widget-internal.h:70
textbox_get_estimated_ch
double textbox_get_estimated_ch(void)
Definition: textbox.c:905
MOUSE_CLICK_UP
@ MOUSE_CLICK_UP
Definition: keyb.h:170
textbox::emode
PangoEllipsizeMode emode
Definition: textbox.h:70
MARKUP
@ MARKUP
Definition: textbox.h:102
KeyBindingAction
KeyBindingAction
Definition: keyb.h:59
textbox::widget
widget widget
Definition: textbox.h:52
rofi_theme_get_double
double rofi_theme_get_double(const widget *widget, const char *property, double def)
Definition: theme.c:816
REMOVE_TO_EOL
@ REMOVE_TO_EOL
Definition: keyb.h:87
textbox_text
void textbox_text(textbox *tb, const char *text)
Definition: textbox.c:314
_widget::get_desired_width
int(* get_desired_width)(struct _widget *)
Definition: widget-internal.h:82
textbox_cursor_end
void textbox_cursor_end(textbox *tb)
Definition: textbox.c:584
distance_get_pixel
int distance_get_pixel(RofiDistance d, RofiOrientation ori)
Definition: theme.c:996
DOT_OFFSET
#define DOT_OFFSET
Definition: textbox.c:45
_widget::get_desired_height
int(* get_desired_height)(struct _widget *)
Definition: widget-internal.h:81
_widget::get_width
int(* get_width)(struct _widget *)
Definition: widget-internal.h:68
rofi_theme_get_distance
RofiDistance rofi_theme_get_distance(const widget *widget, const char *property, int def)
Definition: theme.c:745
TB_PASSWORD
@ TB_PASSWORD
Definition: textbox.h:85
textbox_cursor_del
static void textbox_cursor_del(textbox *tb)
Definition: textbox.c:657
MOUSE_DCLICK_UP
@ MOUSE_DCLICK_UP
Definition: keyb.h:172
rofi_theme_get_boolean
int rofi_theme_get_boolean(const widget *widget, const char *property, int def)
Definition: theme.c:767
textbox_append_text
gboolean textbox_append_text(textbox *tb, const char *pad, const int pad_len)
Definition: textbox.c:785
mode.h
textbox_setup
void textbox_setup(void)
Definition: textbox.c:817
TBFontConfig::pfd
PangoFontDescription * pfd
Definition: textbox.c:62
HIGHLIGHT
@ HIGHLIGHT
Definition: textbox.h:107
TB_AUTOHEIGHT
@ TB_AUTOHEIGHT
Definition: textbox.h:80
textbox_cursor_bkspc
static void textbox_cursor_bkspc(textbox *tb)
Definition: textbox.c:670
TB_AUTOWIDTH
@ TB_AUTOWIDTH
Definition: textbox.h:81
_widget::state
const char * state
Definition: widget-internal.h:98
MOUSE_CLICK_DOWN
@ MOUSE_CLICK_DOWN
Definition: keyb.h:169
widget_padding_get_bottom
int widget_padding_get_bottom(const widget *wid)
Definition: widget.c:512
textbox::show_placeholder
int show_placeholder
Definition: textbox.h:57
rofi_view_queue_redraw
void rofi_view_queue_redraw(void)
Definition: view.c:473
textbox::layout
PangoLayout * layout
Definition: textbox.h:58
textbox_get_font_height
int textbox_get_font_height(const textbox *tb)
Definition: textbox.c:867
widget_set_state
void widget_set_state(widget *widget, const char *state)
Definition: widget.c:93
MOVE_WORD_FORWARD
@ MOVE_WORD_FORWARD
Definition: keyb.h:73
widget_padding_get_right
int widget_padding_get_right(const widget *wid)
Definition: widget.c:492
textbox_cursor_del_word
static void textbox_cursor_del_word(textbox *tb)
Definition: textbox.c:705
widget_update
void widget_update(widget *widget)
Definition: widget.c:397
CLEAR_LINE
@ CLEAR_LINE
Definition: keyb.h:65
char_width
static double char_width
Definition: textbox.c:893
textbox_cursor_del_sol
static void textbox_cursor_del_sol(textbox *tb)
Definition: textbox.c:696
char_height
static double char_height
Definition: textbox.c:882
ch_width
static double ch_width
Definition: textbox.c:904
_widget::draw
void(* draw)(struct _widget *widget, cairo_t *draw)
Definition: widget-internal.h:72
helper_validate_font
gboolean helper_validate_font(PangoFontDescription *pfd, const char *font)
Definition: helper.c:534
textbox_get_estimated_char_width
double textbox_get_estimated_char_width(void)
Definition: textbox.c:894
SELECTED
@ SELECTED
Definition: textbox.h:100
MouseBindingMouseDefaultAction
MouseBindingMouseDefaultAction
Definition: keyb.h:168
textbox_get_width
static int textbox_get_width(widget *)
Definition: textbox.c:844
textbox::xalign
double xalign
Definition: textbox.h:67
REMOVE_TO_SOL
@ REMOVE_TO_SOL
Definition: keyb.h:89
textbox_keybinding
int textbox_keybinding(textbox *tb, KeyBindingAction action)
Definition: textbox.c:721
theme.h
flags
MenuFlags flags
Definition: view.c:108
textbox_get_height
int textbox_get_height(const textbox *tb)
Definition: textbox.c:862
RofiDistance
Definition: rofi-types.h:125
_widget::free
void(* free)(struct _widget *widget)
Definition: widget-internal.h:94
_widget::trigger_action
widget_trigger_action_cb trigger_action
Definition: widget-internal.h:89
_widget::y
short y
Definition: widget-internal.h:42
textbox_get_visible_text
const char * textbox_get_visible_text(const textbox *tb)
Definition: textbox.c:291
textbox::cursor
short cursor
Definition: textbox.h:54
MOVE_END
@ MOVE_END
Definition: keyb.h:69
tbfc_entry_free
static void tbfc_entry_free(TBFontConfig *tbfc)
Definition: textbox.c:809
textbox_draw
static void textbox_draw(widget *, cairo_t *)
Definition: textbox.c:407
widget_queue_redraw
void widget_queue_redraw(widget *wid)
Definition: widget.c:408
textbox::text
char * text
Definition: textbox.h:55
MOUSE_DCLICK_DOWN
@ MOUSE_DCLICK_DOWN
Definition: keyb.h:171
textbox_insert
void textbox_insert(textbox *tb, const int char_pos, const char *str, const int slen)
Definition: textbox.c:598
keyb.h
textbox_resize
static void textbox_resize(widget *wid, short w, short h)
Definition: textbox.c:86
textbox_cursor
void textbox_cursor(textbox *tb, int pos)
Definition: textbox.c:481
_widget::x
short x
Definition: widget-internal.h:40
textbox::blink_timeout
guint blink_timeout
Definition: textbox.h:64
widget_padding_get_top
int widget_padding_get_top(const widget *wid)
Definition: widget.c:502
STATE_MASK
@ STATE_MASK
Definition: textbox.h:111
rofi_theme_get_string
const char * rofi_theme_get_string(const widget *widget, const char *property, const char *def)
Definition: theme.c:800
textbox_get_desired_height
static int textbox_get_desired_height(widget *wid)
Definition: textbox.c:91
URGENT
@ URGENT
Definition: textbox.h:96
TB_MARKUP
@ TB_MARKUP
Definition: textbox.h:83
p_context
static PangoContext * p_context
Definition: textbox.c:54
FMOD_MASK
@ FMOD_MASK
Definition: textbox.h:109
TextboxFlags
TextboxFlags
Definition: textbox.h:79
TBFontConfig
Definition: textbox.c:60
TBFontConfig::height
double height
Definition: textbox.c:66
textbox_cleanup
void textbox_cleanup(void)
Definition: textbox.c:835
textbox_set_ellipsize
void textbox_set_ellipsize(textbox *tb, PangoEllipsizeMode mode)
Definition: textbox.c:944
textbox_cursor_inc_word
static void textbox_cursor_inc_word(textbox *tb)
Definition: textbox.c:522
MOVE_FRONT
@ MOVE_FRONT
Definition: keyb.h:67
textbox_get_pango_attributes
PangoAttrList * textbox_get_pango_attributes(textbox *tb)
Definition: textbox.c:298
textbox_get_font_width
int textbox_get_font_width(const textbox *tb)
Definition: textbox.c:874
_widget::h
short h
Definition: widget-internal.h:46
widget_init
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition: widget.c:74
_widget::w
short w
Definition: widget-internal.h:44
TB_WRAP
@ TB_WRAP
Definition: textbox.h:84
theme_prop_names
const char *const theme_prop_names[][3]
Definition: textbox.c:225
textbox_delete
void textbox_delete(textbox *tb, int pos, int dlen)
Definition: textbox.c:622
TB_INDICATOR
@ TB_INDICATOR
Definition: textbox.h:86
textbox_free
static void textbox_free(widget *)
Definition: textbox.c:388
textbox_get_estimated_height
int textbox_get_estimated_height(const textbox *tb, int eh)
Definition: textbox.c:914
TextBoxFontType
TextBoxFontType
Definition: textbox.h:92
textbox_set_pango_attributes
void textbox_set_pango_attributes(textbox *tb, PangoAttrList *list)
Definition: textbox.c:305
__textbox_update_pango_text
static void __textbox_update_pango_text(textbox *tb)
Definition: textbox.c:268
textbox::blink
int blink
Definition: textbox.h:63
view.h
textbox_cursor_inc
static int textbox_cursor_inc(textbox *tb)
Definition: textbox.c:500
ALT
@ ALT
Definition: textbox.h:105
textbox_editable_trigger_action
static WidgetTriggerActionResult textbox_editable_trigger_action(widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data)
Definition: textbox.c:104
textbox
Definition: textbox.h:51
textbox.h
textbox::tbft
int tbft
Definition: textbox.h:59
helper-theme.h
textbox_initialize_font
static void textbox_initialize_font(textbox *tb)
Definition: textbox.c:134
widget_padding_get_left
int widget_padding_get_left(const widget *wid)
Definition: widget.c:482
textbox_set_pango_context
void textbox_set_pango_context(const char *font, PangoContext *p)
Definition: textbox.c:824
_widget::resize
void(* resize)(struct _widget *, short, short)
Definition: widget-internal.h:74
p_metrics
static PangoFontMetrics * p_metrics
Definition: textbox.c:56
MOVE_WORD_BACK
@ MOVE_WORD_BACK
Definition: keyb.h:71
textbox::changed
int changed
Definition: textbox.h:61
helper.h
textbox_moveresize
void textbox_moveresize(textbox *tb, int x, int y, int w, int h)
Definition: textbox.c:347
MOVE_CHAR_FORWARD
@ MOVE_CHAR_FORWARD
Definition: keyb.h:77
REMOVE_CHAR_FORWARD
@ REMOVE_CHAR_FORWARD
Definition: keyb.h:83
textbox::metrics
PangoFontMetrics * metrics
Definition: textbox.h:69
WIDGET_TRIGGER_ACTION_RESULT_HANDLED
@ WIDGET_TRIGGER_ACTION_RESULT_HANDLED
Definition: widget.h:82
WIDGET_TRIGGER_ACTION_RESULT_IGNORED
@ WIDGET_TRIGGER_ACTION_RESULT_IGNORED
Definition: widget.h:80
textbox_get_estimated_char_height
double textbox_get_estimated_char_height(void)
Definition: textbox.c:883
textbox::placeholder
const char * placeholder
Definition: textbox.h:56
textbox::yalign
double yalign
Definition: textbox.h:66
ROFI_ORIENTATION_HORIZONTAL
@ ROFI_ORIENTATION_HORIZONTAL
Definition: rofi-types.h:138
TB_EDITABLE
@ TB_EDITABLE
Definition: textbox.h:82
widget_padding_get_padding_width
int widget_padding_get_padding_width(const widget *wid)
Definition: widget.c:544
widget_padding_get_padding_height
int widget_padding_get_padding_height(const widget *wid)
Definition: widget.c:537
_widget
Definition: widget-internal.h:36
WidgetTriggerActionResult
WidgetTriggerActionResult
Definition: widget.h:78
textbox_cursor_dec_word
static void textbox_cursor_dec_word(textbox *tb)
Definition: textbox.c:552
default_font_name
const char * default_font_name
Definition: textbox.c:823
TBFontConfig::metrics
PangoFontMetrics * metrics
Definition: textbox.c:64
REMOVE_CHAR_BACK
@ REMOVE_CHAR_BACK
Definition: keyb.h:85
textbox_blink
static gboolean textbox_blink(gpointer data)
Definition: textbox.c:72
textbox_cursor_dec
static int textbox_cursor_dec(textbox *tb)
Definition: textbox.c:514
textbox_create
textbox * textbox_create(widget *parent, WidgetType type, const char *name, TextboxFlags flags, TextBoxFontType tbft, const char *text, double xalign, double yalign)
Definition: textbox.c:164