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