rofi  1.5.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-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 
44 #define DOT_OFFSET 15
45 
46 static void textbox_draw ( widget *, cairo_t * );
47 static void textbox_free ( widget * );
48 static int textbox_get_width ( widget * );
49 static int _textbox_get_height ( widget * );
50 static void __textbox_update_pango_text ( textbox *tb );
51 
53 static PangoContext *p_context = NULL;
55 static PangoFontMetrics *p_metrics = NULL;
56 
58 typedef struct TBFontConfig
59 {
61  PangoFontDescription *pfd;
63  PangoFontMetrics *metrics;
65  double height;
67 
69 static GHashTable *tbfc_cache = NULL;
70 
71 static gboolean textbox_blink ( gpointer data )
72 {
73  textbox *tb = (textbox *) data;
74  if ( tb->blink < 2 ) {
75  tb->blink = !tb->blink;
76  widget_queue_redraw ( WIDGET ( tb ) );
78  }
79  else {
80  tb->blink--;
81  }
82  return TRUE;
83 }
84 
85 static void textbox_resize ( widget *wid, short w, short h )
86 {
87  textbox *tb = (textbox *) wid;
88  textbox_moveresize ( tb, tb->widget.x, tb->widget.y, w, h );
89 }
91 {
92  textbox *tb = (textbox *) wid;
93  if ( ( tb->flags & TB_AUTOHEIGHT ) == 0 ) {
94  return tb->widget.h;
95  }
96  if ( tb->changed ) {
98  }
99  int height = textbox_get_height ( tb );
100  return height;
101 }
102 
103 static WidgetTriggerActionResult textbox_editable_trigger_action ( widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data )
104 {
105  textbox *tb = (textbox *) wid;
106  switch ( action )
107  {
108  case MOUSE_CLICK_DOWN:
109  {
110  gint i;
111  // subtract padding on left.
112  x -= widget_padding_get_left ( wid );
113  gint max = textbox_get_font_width ( tb );
114  // Right of text, move to end.
115  if ( x >= max ) {
116  textbox_cursor_end ( tb );
117  }
118  else if ( x > 0 ) {
119  // If in range, get index.
120  pango_layout_xy_to_index ( tb->layout, x * PANGO_SCALE, y * PANGO_SCALE, &i, NULL );
121  textbox_cursor ( tb, i );
122  }
124  }
125  case MOUSE_CLICK_UP:
126  case MOUSE_DCLICK_DOWN:
127  case MOUSE_DCLICK_UP:
128  break;
129  }
131 }
132 
133 static void textbox_initialize_font ( textbox *tb )
134 {
135  tb->metrics = p_metrics;
136  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  tb->left_offset = ( tbfc->height ) / (double) PANGO_SCALE;
161  }
162  }
163 }
164 
165 textbox* textbox_create ( widget *parent, WidgetType type, const char *name, TextboxFlags flags, TextBoxFontType tbft, const char *text, double xalign, double yalign )
166 {
167  textbox *tb = g_slice_new0 ( textbox );
168 
169  widget_init ( WIDGET ( tb ), parent, type, name );
170 
171  tb->widget.draw = textbox_draw;
172  tb->widget.free = textbox_free;
178  tb->flags = flags;
179 
180  tb->changed = FALSE;
181 
182  tb->layout = pango_layout_new ( p_context );
183  textbox_font ( tb, tbft );
184 
186 
187  if ( ( tb->flags & TB_ICON ) != TB_ICON ) {
188  tb->left_offset = 0;
189  }
190 
191  if ( ( flags & TB_WRAP ) == TB_WRAP ) {
192  pango_layout_set_wrap ( tb->layout, PANGO_WRAP_WORD_CHAR );
193  }
194 
195  const char *txt = rofi_theme_get_string ( WIDGET ( tb ), "str", text );
196  textbox_text ( tb, txt ? txt : "" );
197  textbox_cursor_end ( tb );
198 
199  // auto height/width modes get handled here
200  textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
201 
202  tb->blink_timeout = 0;
203  tb->blink = 1;
204  if ( ( flags & TB_EDITABLE ) == TB_EDITABLE ) {
205  tb->blink_timeout = g_timeout_add ( 1200, textbox_blink, tb );
207  }
208 
209  tb->yalign = rofi_theme_get_double ( WIDGET ( tb ), "vertical-align", xalign );
210  tb->yalign = MAX ( 0, MIN ( 1.0, tb->yalign ) );
211  tb->xalign = rofi_theme_get_double ( WIDGET ( tb ), "horizontal-align", yalign );
212  tb->xalign = MAX ( 0, MIN ( 1.0, tb->xalign ) );
213 
214  return tb;
215 }
216 
220 const char *const theme_prop_names[][3] = {
222  { "normal.normal", "selected.normal", "alternate.normal" },
224  { "normal.urgent", "selected.urgent", "alternate.urgent" },
226  { "normal.active", "selected.active", "alternate.active" },
227 };
228 
230 {
231  TextBoxFontType t = tbft & STATE_MASK;
232  if ( tb == NULL ) {
233  return;
234  }
235  // ACTIVE has priority over URGENT if both set.
236  if ( t == ( URGENT | ACTIVE ) ) {
237  t = ACTIVE;
238  }
239  switch ( ( tbft & FMOD_MASK ) )
240  {
241  case HIGHLIGHT:
242  widget_set_state ( WIDGET ( tb ), theme_prop_names[t][1] );
243  break;
244  case ALT:
245  widget_set_state ( WIDGET ( tb ), theme_prop_names[t][2] );
246  break;
247  default:
248  widget_set_state ( WIDGET ( tb ), theme_prop_names[t][0] );
249  break;
250  }
251  if ( tb->tbft != tbft || tb->widget.state == NULL ) {
252  widget_queue_redraw ( WIDGET ( tb ) );
253  }
254  tb->tbft = tbft;
255 }
256 
264 {
265  pango_layout_set_attributes ( tb->layout, NULL );
266  if ( ( tb->flags & TB_PASSWORD ) == TB_PASSWORD ) {
267  size_t l = g_utf8_strlen ( tb->text, -1 );
268  char string [l + 1];
269  memset ( string, '*', l );
270  string[l] = '\0';
271  pango_layout_set_text ( tb->layout, string, l );
272  }
273  else if ( tb->flags & TB_MARKUP || tb->tbft & MARKUP ) {
274  pango_layout_set_markup ( tb->layout, tb->text, -1 );
275  }
276  else {
277  pango_layout_set_text ( tb->layout, tb->text, -1 );
278  }
279 }
280 const char *textbox_get_visible_text ( const textbox *tb )
281 {
282  if ( tb == NULL ) {
283  return NULL;
284  }
285  return pango_layout_get_text ( tb->layout );
286 }
287 PangoAttrList *textbox_get_pango_attributes ( textbox *tb )
288 {
289  if ( tb == NULL ) {
290  return NULL;
291  }
292  return pango_layout_get_attributes ( tb->layout );
293 }
294 void textbox_set_pango_attributes ( textbox *tb, PangoAttrList *list )
295 {
296  if ( tb == NULL ) {
297  return;
298  }
299  pango_layout_set_attributes ( tb->layout, list );
300 }
301 
302 // set the default text to display
303 void textbox_text ( textbox *tb, const char *text )
304 {
305  if ( tb == NULL ) {
306  return;
307  }
308  g_free ( tb->text );
309  const gchar *last_pointer = NULL;
310 
311  if ( g_utf8_validate ( text, -1, &last_pointer ) ) {
312  tb->text = g_strdup ( text );
313  }
314  else {
315  if ( last_pointer != NULL ) {
316  // Copy string up to invalid character.
317  tb->text = g_strndup ( text, ( last_pointer - text ) );
318  }
319  else {
320  tb->text = g_strdup ( "Invalid UTF-8 string." );
321  }
322  }
324  if ( tb->flags & TB_AUTOWIDTH ) {
325  textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
326  if ( WIDGET ( tb )->parent ) {
327  widget_update ( WIDGET ( tb )->parent );
328  }
329  }
330 
331  tb->cursor = MAX ( 0, MIN ( ( int ) g_utf8_strlen ( tb->text, -1 ), tb->cursor ) );
332  widget_queue_redraw ( WIDGET ( tb ) );
333 }
334 
335 void textbox_icon ( textbox *tb, cairo_surface_t *icon )
336 {
337  tb->icon = icon;
338 
339  widget_queue_redraw ( WIDGET ( tb ) );
340 }
341 
342 // within the parent handled auto width/height modes
343 void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
344 {
345  unsigned int offset = tb->left_offset * 1.2 + ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
346  if ( tb->flags & TB_AUTOWIDTH ) {
347  pango_layout_set_width ( tb->layout, -1 );
348  w = textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( WIDGET ( tb ) ) + offset;
349  }
350  else {
351  // set ellipsize
352  if ( ( tb->flags & TB_EDITABLE ) == TB_EDITABLE ) {
353  pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_MIDDLE );
354  }
355  else if ( ( tb->flags & TB_WRAP ) != TB_WRAP ) {
356  pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_END );
357  }
358  }
359 
360  if ( tb->flags & TB_AUTOHEIGHT ) {
361  // Width determines height!
362  int tw = MAX ( 1, w );
363  pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tw - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
364  int hd = textbox_get_height ( tb );
365  h = MAX ( hd, h );
366  }
367 
368  if ( x != tb->widget.x || y != tb->widget.y || w != tb->widget.w || h != tb->widget.h ) {
369  tb->widget.x = x;
370  tb->widget.y = y;
371  tb->widget.h = MAX ( 1, h );
372  tb->widget.w = MAX ( 1, w );
373  }
374 
375  // We always want to update this
376  pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
377  widget_queue_redraw ( WIDGET ( tb ) );
378 }
379 
380 // will also unmap the window if still displayed
381 static void textbox_free ( widget *wid )
382 {
383  if ( wid == NULL ) {
384  return;
385  }
386  textbox *tb = (textbox *) wid;
387  if ( tb->blink_timeout > 0 ) {
388  g_source_remove ( tb->blink_timeout );
389  tb->blink_timeout = 0;
390  }
391  g_free ( tb->text );
392 
393  if ( tb->layout != NULL ) {
394  g_object_unref ( tb->layout );
395  }
396 
397  g_slice_free ( textbox, tb );
398 }
399 
400 static void textbox_draw ( widget *wid, cairo_t *draw )
401 {
402  if ( wid == NULL ) {
403  return;
404  }
405  textbox *tb = (textbox *) wid;
406  unsigned int offset = tb->left_offset * 1.2 + ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
407 
408  if ( tb->changed ) {
410  }
411 
412  // Skip the side MARGIN on the X axis.
413  int x = widget_padding_get_left ( WIDGET ( tb ) );
414  int top = widget_padding_get_top ( WIDGET ( tb ) );
415  int y = ( pango_font_metrics_get_ascent ( tb->metrics ) - pango_layout_get_baseline ( tb->layout ) ) / PANGO_SCALE;
416  int line_width = 0, line_height = 0;
417  // Get actual width.
418  pango_layout_get_pixel_size ( tb->layout, &line_width, &line_height );
419 
420  if ( tb->yalign > 0.001 ) {
421  int bottom = widget_padding_get_bottom ( WIDGET ( tb ) );
422  top = ( tb->widget.h - bottom - line_height - top ) * tb->yalign + top;
423  }
424  y += top;
425 
426  // draw Icon
427  if ( ( tb->flags & TB_ICON ) == TB_ICON && tb->icon != NULL ) {
428  int iconheight = tb->left_offset;
429  cairo_save ( draw );
430 
431  int iconh = cairo_image_surface_get_height ( tb->icon );
432  int iconw = cairo_image_surface_get_width ( tb->icon );
433  int icons = MAX ( iconh, iconw );
434  double scale = (double) iconheight / icons;
435  cairo_translate ( draw, x + ( iconheight - iconw * scale ) / 2.0, y + ( iconheight - iconh * scale ) / 2.0 );
436  cairo_scale ( draw, scale, scale );
437  cairo_set_source_surface ( draw, tb->icon, 0, 0 );
438  cairo_paint ( draw );
439  cairo_restore ( draw );
440  }
441  x += offset;
442 
443  if ( tb->xalign > 0.001 ) {
444  int rem = MAX ( 0, tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - line_width );
445  x = tb->xalign * rem + widget_padding_get_left ( WIDGET ( tb ) );
446  }
447  // TODO check if this is still needed after flatning.
448  cairo_set_operator ( draw, CAIRO_OPERATOR_OVER );
449  cairo_set_source_rgb ( draw, 0.0, 0.0, 0.0 );
450  rofi_theme_get_color ( WIDGET ( tb ), "text-color", draw );
451  // draw the cursor
452  if ( tb->flags & TB_EDITABLE && tb->blink ) {
453  // We want to place the cursor based on the text shown.
454  const char *text = pango_layout_get_text ( tb->layout );
455  // Clamp the position, should not be needed, but we are paranoid.
456  int cursor_offset = MIN ( tb->cursor, g_utf8_strlen ( text, -1 ) );
457  PangoRectangle pos;
458  // convert to byte location.
459  char *offset = g_utf8_offset_to_pointer ( text, cursor_offset );
460  pango_layout_get_cursor_pos ( tb->layout, offset - text, &pos, NULL );
461  int cursor_x = pos.x / PANGO_SCALE;
462  int cursor_y = pos.y / PANGO_SCALE;
463  int cursor_height = pos.height / PANGO_SCALE;
464  int cursor_width = 2;
465  cairo_rectangle ( draw, x + cursor_x, y + cursor_y, cursor_width, cursor_height );
466  cairo_fill ( draw );
467  }
468 
469  // Set ARGB
470  // We need to set over, otherwise subpixel hinting wont work.
471  cairo_move_to ( draw, x, top );
472  pango_cairo_show_layout ( draw, tb->layout );
473 
474  if ( ( tb->flags & TB_INDICATOR ) == TB_INDICATOR && ( tb->tbft & ( SELECTED ) ) ) {
475  cairo_arc ( draw, tb->left_offset * 1.2 + 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
769  case MOVE_WORD_BACK:
771  return 2;
772  // Alt-F
773  case MOVE_WORD_FORWARD:
775  return 2;
776  // 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_height ( tb );
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 
903 static double ch_width = -1;
905 {
906  if ( ch_width < 0 ) {
907  int width = pango_font_metrics_get_approximate_digit_width ( p_metrics );
908  ch_width = ( width ) / (double) PANGO_SCALE;
909  }
910  return ch_width;
911 }
912 
913 int textbox_get_estimated_height ( const textbox *tb, int eh )
914 {
915  int height = pango_font_metrics_get_ascent ( tb->metrics ) + pango_font_metrics_get_descent ( tb->metrics );
916  return ( eh * height ) / PANGO_SCALE + widget_padding_get_padding_height ( WIDGET ( tb ) );
917 }
919 {
920  textbox *tb = (textbox *) wid;
921  unsigned int offset = tb->left_offset * 1.2 + ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
922  if ( wid->expand && tb->flags & TB_AUTOWIDTH ) {
923  return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset;
924  }
925  RofiDistance w = rofi_theme_get_distance ( WIDGET ( tb ), "width", 0 );
927  if ( wi > 0 ) {
928  return wi;
929  }
930  int padding = widget_padding_get_left ( WIDGET ( tb ) );
931  padding += widget_padding_get_right ( WIDGET ( tb ) );
932  int old_width = pango_layout_get_width ( tb->layout );
933  pango_layout_set_width ( tb->layout, -1 );
934  int width = textbox_get_font_width ( tb );
935  // Restore.
936  pango_layout_set_width ( tb->layout, old_width );
937  return width + padding + offset;
938 }
static void textbox_cursor_del_eol(textbox *tb)
Definition: textbox.c:687
void textbox_cursor_end(textbox *tb)
Definition: textbox.c:584
static PangoFontMetrics * p_metrics
Definition: textbox.c:55
static void textbox_cursor_dec_word(textbox *tb)
Definition: textbox.c:552
int left_offset
Definition: textbox.h:71
PangoFontMetrics * metrics
Definition: textbox.c:63
int blink
Definition: textbox.h:64
static gboolean textbox_blink(gpointer data)
Definition: textbox.c:71
MouseBindingMouseDefaultAction
Definition: keyb.h:154
#define DOT_OFFSET
Definition: textbox.c:44
WidgetType
Definition: widget.h:56
void textbox_cleanup(void)
Definition: textbox.c:835
guint blink_timeout
Definition: textbox.h:65
int widget_padding_get_bottom(const widget *wid)
Definition: widget.c:516
void rofi_theme_get_color(const widget *widget, const char *property, cairo_t *d)
Definition: theme.c:647
void textbox_setup(void)
Definition: textbox.c:817
unsigned long flags
Definition: textbox.h:53
void textbox_text(textbox *tb, const char *text)
Definition: textbox.c:303
int(* get_width)(struct _widget *)
void widget_update(widget *widget)
Definition: widget.c:411
const char * default_font_name
Definition: textbox.c:823
static void textbox_cursor_del(textbox *tb)
Definition: textbox.c:657
static void textbox_cursor_del_sol(textbox *tb)
Definition: textbox.c:696
void textbox_icon(textbox *tb, cairo_surface_t *icon)
Definition: textbox.c:335
void rofi_view_queue_redraw(void)
Definition: view.c:431
static double char_height
Definition: textbox.c:882
double textbox_get_estimated_char_width(void)
Definition: textbox.c:894
int widget_padding_get_padding_width(const widget *wid)
Definition: widget.c:548
void(* resize)(struct _widget *, short, short)
static int textbox_get_width(widget *)
Definition: textbox.c:844
int widget_padding_get_top(const widget *wid)
Definition: widget.c:506
const char * rofi_theme_get_string(const widget *widget, const char *property, const char *def)
Definition: theme.c:604
void widget_set_state(widget *widget, const char *state)
Definition: widget.c:56
static int _textbox_get_height(widget *)
Definition: textbox.c:854
static double char_width
Definition: textbox.c:893
static GHashTable * tbfc_cache
Definition: textbox.c:69
double yalign
Definition: textbox.h:67
double rofi_theme_get_double(const widget *widget, const char *property, double def)
Definition: theme.c:620
int(* get_height)(struct _widget *)
static double ch_width
Definition: textbox.c:903
widget widget
Definition: textbox.h:52
static void textbox_cursor_bkspc(textbox *tb)
Definition: textbox.c:670
void textbox_font(textbox *tb, TextBoxFontType tbft)
Definition: textbox.c:229
char * text
Definition: textbox.h:55
RofiDistance rofi_theme_get_distance(const widget *widget, const char *property, int def)
Definition: theme.c:549
void(* draw)(struct _widget *widget, cairo_t *draw)
int(* get_desired_width)(struct _widget *)
textbox * textbox_create(widget *parent, WidgetType type, const char *name, TextboxFlags flags, TextBoxFontType tbft, const char *text, double xalign, double yalign)
Definition: textbox.c:165
void textbox_cursor(textbox *tb, int pos)
Definition: textbox.c:481
double height
Definition: textbox.c:65
Definition: textbox.h:107
int textbox_get_font_width(const textbox *tb)
Definition: textbox.c:874
Definition: keyb.h:66
int changed
Definition: textbox.h:59
PangoFontMetrics * metrics
Definition: textbox.h:70
short cursor
Definition: textbox.h:54
TextboxFlags
Definition: textbox.h:79
widget_trigger_action_cb trigger_action
static void textbox_cursor_del_word(textbox *tb)
Definition: textbox.c:705
double textbox_get_estimated_ch(void)
Definition: textbox.c:904
void widget_queue_redraw(widget *wid)
Definition: widget.c:421
int textbox_keybinding(textbox *tb, KeyBindingAction action)
Definition: textbox.c:721
const char * textbox_get_visible_text(const textbox *tb)
Definition: textbox.c:280
static int textbox_get_desired_height(widget *wid)
Definition: textbox.c:90
static void textbox_draw(widget *, cairo_t *)
Definition: textbox.c:400
TextBoxFontType
Definition: textbox.h:93
static void __textbox_update_pango_text(textbox *tb)
Definition: textbox.c:263
static void tbfc_entry_free(TBFontConfig *tbfc)
Definition: textbox.c:809
Definition: textbox.h:98
void textbox_moveresize(textbox *tb, int x, int y, int w, int h)
Definition: textbox.c:343
int tbft
Definition: textbox.h:57
static int textbox_cursor_inc(textbox *tb)
Definition: textbox.c:500
KeyBindingAction
Definition: keyb.h:55
static PangoContext * p_context
Definition: textbox.c:53
void textbox_insert(textbox *tb, const int char_pos, const char *str, const int slen)
Definition: textbox.c:598
int textbox_get_height(const textbox *tb)
Definition: textbox.c:862
MenuFlags flags
Definition: view.c:107
void(* free)(struct _widget *widget)
static void textbox_resize(widget *wid, short w, short h)
Definition: textbox.c:85
static void textbox_free(widget *)
Definition: textbox.c:381
int distance_get_pixel(RofiDistance d, RofiOrientation ori)
Definition: theme.c:735
static void textbox_initialize_font(textbox *tb)
Definition: textbox.c:133
double xalign
Definition: textbox.h:68
WidgetTriggerActionResult
Definition: widget.h:77
PangoFontDescription * pfd
Definition: textbox.c:61
PangoAttrList * textbox_get_pango_attributes(textbox *tb)
Definition: textbox.c:287
#define WIDGET(a)
Definition: widget.h:115
int widget_padding_get_right(const widget *wid)
Definition: widget.c:496
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition: widget.c:37
const char * state
cairo_surface_t * icon
Definition: textbox.h:61
int textbox_get_estimated_height(const textbox *tb, int eh)
Definition: textbox.c:913
static void textbox_cursor_inc_word(textbox *tb)
Definition: textbox.c:522
int(* get_desired_height)(struct _widget *)
int textbox_get_font_height(const textbox *tb)
Definition: textbox.c:867
int widget_padding_get_padding_height(const widget *wid)
Definition: widget.c:541
const char *const theme_prop_names[][3]
Definition: textbox.c:220
int textbox_get_desired_width(widget *wid)
Definition: textbox.c:918
double textbox_get_estimated_char_height(void)
Definition: textbox.c:883
PangoLayout * layout
Definition: textbox.h:56
static int textbox_cursor_dec(textbox *tb)
Definition: textbox.c:514
void textbox_set_pango_attributes(textbox *tb, PangoAttrList *list)
Definition: textbox.c:294
struct TBFontConfig TBFontConfig
static WidgetTriggerActionResult textbox_editable_trigger_action(widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data)
Definition: textbox.c:103
void textbox_delete(textbox *tb, int pos, int dlen)
Definition: textbox.c:622
int widget_padding_get_left(const widget *wid)
Definition: widget.c:486
static void textbox_cursor_bkspc_word(textbox *tb)
Definition: textbox.c:677
gboolean textbox_append_text(textbox *tb, const char *pad, const int pad_len)
Definition: textbox.c:785
gboolean expand
gboolean helper_validate_font(PangoFontDescription *pfd, const char *font)
Definition: helper.c:556
void textbox_set_pango_context(const char *font, PangoContext *p)
Definition: textbox.c:824