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