2017-04-15 10:32:05 +00:00
|
|
|
/*
|
|
|
|
* rofi
|
2015-03-08 14:43:31 +00:00
|
|
|
*
|
2017-04-15 10:32:05 +00:00
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
|
|
|
|
* Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
|
2015-03-08 14:43:31 +00:00
|
|
|
*
|
2017-04-15 10:32:05 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2015-03-08 14:43:31 +00:00
|
|
|
*
|
2014-03-22 20:04:19 +00:00
|
|
|
*/
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-08-22 07:26:46 +00:00
|
|
|
#include <config.h>
|
2016-02-19 21:43:53 +00:00
|
|
|
#include <xcb/xcb.h>
|
2014-01-19 12:57:54 +00:00
|
|
|
#include <ctype.h>
|
2016-01-07 15:01:56 +00:00
|
|
|
#include <string.h>
|
2015-09-27 10:57:54 +00:00
|
|
|
#include <glib.h>
|
2016-07-18 21:49:26 +00:00
|
|
|
#include <math.h>
|
2016-10-08 16:57:59 +00:00
|
|
|
#include "widgets/textbox.h"
|
2015-04-30 19:47:32 +00:00
|
|
|
#include "keyb.h"
|
2017-02-03 08:52:56 +00:00
|
|
|
#include "helper.h"
|
2017-02-17 13:06:31 +00:00
|
|
|
#include "helper-theme.h"
|
2016-02-10 21:12:49 +00:00
|
|
|
#include "mode.h"
|
|
|
|
#include "view.h"
|
2015-09-27 10:57:54 +00:00
|
|
|
|
2016-12-11 16:06:31 +00:00
|
|
|
#include "theme.h"
|
|
|
|
|
2018-06-12 10:16:33 +00:00
|
|
|
/** The space reserved for the DOT when enabling multi-select. */
|
2016-08-30 16:07:34 +00:00
|
|
|
#define DOT_OFFSET 15
|
2016-07-18 21:49:26 +00:00
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
static void textbox_draw ( widget *, cairo_t * );
|
|
|
|
static void textbox_free ( widget * );
|
|
|
|
static int textbox_get_width ( widget * );
|
|
|
|
static int _textbox_get_height ( widget * );
|
2017-01-03 11:17:17 +00:00
|
|
|
static void __textbox_update_pango_text ( textbox *tb );
|
2016-09-27 06:51:17 +00:00
|
|
|
|
2016-10-14 16:56:09 +00:00
|
|
|
/** Default pango context */
|
2016-07-31 21:28:31 +00:00
|
|
|
static PangoContext *p_context = NULL;
|
2016-10-14 16:56:09 +00:00
|
|
|
/** The pango font metrics */
|
2016-07-30 18:39:45 +00:00
|
|
|
static PangoFontMetrics *p_metrics = NULL;
|
2016-10-14 16:56:09 +00:00
|
|
|
|
2017-01-24 07:40:37 +00:00
|
|
|
/** Cache to hold font descriptions. This it to avoid having to lookup each time. */
|
2017-02-03 19:49:16 +00:00
|
|
|
typedef struct TBFontConfig
|
|
|
|
{
|
2017-01-24 07:40:37 +00:00
|
|
|
/** Font description */
|
|
|
|
PangoFontDescription *pfd;
|
|
|
|
/** Font metrics */
|
|
|
|
PangoFontMetrics *metrics;
|
2017-06-03 13:36:21 +00:00
|
|
|
/** height */
|
2017-06-03 18:35:50 +00:00
|
|
|
double height;
|
2017-01-24 07:40:37 +00:00
|
|
|
}TBFontConfig;
|
|
|
|
|
2017-03-11 12:20:46 +00:00
|
|
|
/** HashMap of previously parsed font descriptions. */
|
2017-01-24 07:40:37 +00:00
|
|
|
static GHashTable *tbfc_cache = NULL;
|
|
|
|
|
2016-02-06 12:06:58 +00:00
|
|
|
static gboolean textbox_blink ( gpointer data )
|
2016-01-20 19:57:26 +00:00
|
|
|
{
|
2016-02-06 12:06:58 +00:00
|
|
|
textbox *tb = (textbox *) data;
|
2016-01-22 17:18:26 +00:00
|
|
|
if ( tb->blink < 2 ) {
|
2017-02-03 19:49:16 +00:00
|
|
|
tb->blink = !tb->blink;
|
2016-10-08 16:57:59 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2016-02-06 12:06:58 +00:00
|
|
|
rofi_view_queue_redraw ( );
|
2016-01-22 17:18:26 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-01-20 19:57:26 +00:00
|
|
|
tb->blink--;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2014-08-02 18:02:37 +00:00
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
static void textbox_resize ( widget *wid, short w, short h )
|
|
|
|
{
|
|
|
|
textbox *tb = (textbox *) wid;
|
|
|
|
textbox_moveresize ( tb, tb->widget.x, tb->widget.y, w, h );
|
|
|
|
}
|
2016-12-30 18:59:45 +00:00
|
|
|
static int textbox_get_desired_height ( widget *wid )
|
|
|
|
{
|
2017-01-08 20:36:06 +00:00
|
|
|
textbox *tb = (textbox *) wid;
|
|
|
|
if ( ( tb->flags & TB_AUTOHEIGHT ) == 0 ) {
|
2016-12-30 18:59:45 +00:00
|
|
|
return tb->widget.h;
|
|
|
|
}
|
2017-01-03 11:17:17 +00:00
|
|
|
if ( tb->changed ) {
|
|
|
|
__textbox_update_pango_text ( tb );
|
|
|
|
}
|
2017-01-08 20:36:06 +00:00
|
|
|
int height = textbox_get_height ( tb );
|
2017-01-03 11:17:17 +00:00
|
|
|
return height;
|
2016-12-30 18:59:45 +00:00
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
|
2017-05-30 10:45:48 +00:00
|
|
|
static WidgetTriggerActionResult textbox_editable_trigger_action ( widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data )
|
2017-05-28 00:18:09 +00:00
|
|
|
{
|
|
|
|
textbox *tb = (textbox *) wid;
|
|
|
|
switch ( action )
|
|
|
|
{
|
|
|
|
case MOUSE_CLICK_DOWN:
|
|
|
|
{
|
|
|
|
gint i;
|
2017-11-23 17:41:52 +00:00
|
|
|
// subtract padding on left.
|
2017-06-03 16:39:37 +00:00
|
|
|
x -= widget_padding_get_left ( wid );
|
|
|
|
gint max = textbox_get_font_width ( tb );
|
|
|
|
// Right of text, move to end.
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( x >= max ) {
|
2017-06-03 16:39:37 +00:00
|
|
|
textbox_cursor_end ( tb );
|
2017-06-03 18:35:50 +00:00
|
|
|
}
|
|
|
|
else if ( x > 0 ) {
|
2017-06-03 16:39:37 +00:00
|
|
|
// If in range, get index.
|
|
|
|
pango_layout_xy_to_index ( tb->layout, x * PANGO_SCALE, y * PANGO_SCALE, &i, NULL );
|
|
|
|
textbox_cursor ( tb, i );
|
|
|
|
}
|
2017-05-30 10:45:48 +00:00
|
|
|
return WIDGET_TRIGGER_ACTION_RESULT_HANDLED;
|
2017-05-28 00:18:09 +00:00
|
|
|
}
|
|
|
|
case MOUSE_CLICK_UP:
|
|
|
|
case MOUSE_DCLICK_DOWN:
|
|
|
|
case MOUSE_DCLICK_UP:
|
|
|
|
break;
|
|
|
|
}
|
2017-05-30 10:45:48 +00:00
|
|
|
return WIDGET_TRIGGER_ACTION_RESULT_IGNORED;
|
2017-05-28 00:18:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 06:52:30 +00:00
|
|
|
static void textbox_initialize_font ( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-01-24 07:40:37 +00:00
|
|
|
tb->metrics = p_metrics;
|
2017-04-28 07:15:01 +00:00
|
|
|
const char * font = rofi_theme_get_string ( WIDGET ( tb ), "font", NULL );
|
2017-06-03 18:35:50 +00:00
|
|
|
tb->left_offset = textbox_get_estimated_char_height ();
|
2017-02-03 19:49:16 +00:00
|
|
|
if ( font ) {
|
2017-01-24 07:40:37 +00:00
|
|
|
TBFontConfig *tbfc = g_hash_table_lookup ( tbfc_cache, font );
|
2017-02-03 19:49:16 +00:00
|
|
|
if ( tbfc == NULL ) {
|
|
|
|
tbfc = g_malloc0 ( sizeof ( TBFontConfig ) );
|
2017-01-24 07:40:37 +00:00
|
|
|
tbfc->pfd = pango_font_description_from_string ( font );
|
2017-02-03 19:49:16 +00:00
|
|
|
if ( helper_validate_font ( tbfc->pfd, font ) ) {
|
2017-02-03 08:52:56 +00:00
|
|
|
tbfc->metrics = pango_context_get_metrics ( p_context, tbfc->pfd, NULL );
|
2017-06-03 18:35:50 +00:00
|
|
|
tbfc->height = pango_font_metrics_get_ascent ( tbfc->metrics ) + pango_font_metrics_get_descent ( tbfc->metrics );
|
2017-06-03 13:36:21 +00:00
|
|
|
|
2017-05-04 05:56:40 +00:00
|
|
|
// Cast away consts. (*yuck*) because table_insert does not know it is const.
|
2017-05-15 20:46:33 +00:00
|
|
|
g_hash_table_insert ( tbfc_cache, (char *) font, tbfc );
|
2017-02-03 19:49:16 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-02-03 08:52:56 +00:00
|
|
|
pango_font_description_free ( tbfc->pfd );
|
2017-02-03 19:49:16 +00:00
|
|
|
g_free ( tbfc );
|
2017-02-03 08:52:56 +00:00
|
|
|
tbfc = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( tbfc ) {
|
|
|
|
// Update for used font.
|
|
|
|
pango_layout_set_font_description ( tb->layout, tbfc->pfd );
|
2017-06-03 18:35:50 +00:00
|
|
|
tb->metrics = tbfc->metrics;
|
|
|
|
tb->left_offset = ( tbfc->height ) / (double) PANGO_SCALE;
|
2017-01-24 07:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-07 06:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 11:46:09 +00:00
|
|
|
textbox* textbox_create ( widget *parent, WidgetType type, const char *name, TextboxFlags flags, TextBoxFontType tbft, const char *text, double xalign, double yalign )
|
2017-09-07 06:52:30 +00:00
|
|
|
{
|
|
|
|
textbox *tb = g_slice_new0 ( textbox );
|
|
|
|
|
2017-09-07 11:46:09 +00:00
|
|
|
widget_init ( WIDGET ( tb ), parent, type, name );
|
2017-09-07 06:52:30 +00:00
|
|
|
|
|
|
|
tb->widget.draw = textbox_draw;
|
|
|
|
tb->widget.free = textbox_free;
|
|
|
|
tb->widget.resize = textbox_resize;
|
|
|
|
tb->widget.get_width = textbox_get_width;
|
|
|
|
tb->widget.get_height = _textbox_get_height;
|
|
|
|
tb->widget.get_desired_height = textbox_get_desired_height;
|
|
|
|
tb->widget.get_desired_width = textbox_get_desired_width;
|
|
|
|
tb->flags = flags;
|
|
|
|
|
|
|
|
tb->changed = FALSE;
|
|
|
|
|
|
|
|
tb->layout = pango_layout_new ( p_context );
|
|
|
|
textbox_font ( tb, tbft );
|
|
|
|
|
|
|
|
textbox_initialize_font ( tb );
|
|
|
|
|
2017-06-03 13:36:21 +00:00
|
|
|
if ( ( tb->flags & TB_ICON ) != TB_ICON ) {
|
|
|
|
tb->left_offset = 0;
|
|
|
|
}
|
2017-01-24 07:40:37 +00:00
|
|
|
|
2015-09-23 18:44:24 +00:00
|
|
|
if ( ( flags & TB_WRAP ) == TB_WRAP ) {
|
2015-06-03 07:04:29 +00:00
|
|
|
pango_layout_set_wrap ( tb->layout, PANGO_WRAP_WORD_CHAR );
|
2015-05-23 18:06:06 +00:00
|
|
|
}
|
2017-05-28 16:05:02 +00:00
|
|
|
|
2017-06-03 18:35:50 +00:00
|
|
|
const char *txt = rofi_theme_get_string ( WIDGET ( tb ), "str", text );
|
|
|
|
textbox_text ( tb, txt ? txt : "" );
|
2014-03-22 20:04:19 +00:00
|
|
|
textbox_cursor_end ( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// auto height/width modes get handled here
|
2016-01-09 15:22:09 +00:00
|
|
|
textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2016-01-20 19:57:26 +00:00
|
|
|
tb->blink_timeout = 0;
|
2016-01-22 17:18:26 +00:00
|
|
|
tb->blink = 1;
|
2016-01-20 19:57:26 +00:00
|
|
|
if ( ( flags & TB_EDITABLE ) == TB_EDITABLE ) {
|
2017-05-28 00:18:09 +00:00
|
|
|
tb->blink_timeout = g_timeout_add ( 1200, textbox_blink, tb );
|
|
|
|
tb->widget.trigger_action = textbox_editable_trigger_action;
|
2016-01-20 19:57:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 18:14:53 +00:00
|
|
|
tb->yalign = rofi_theme_get_double ( WIDGET ( tb ), "vertical-align", xalign );
|
2017-06-03 18:35:50 +00:00
|
|
|
tb->yalign = MAX ( 0, MIN ( 1.0, tb->yalign ) );
|
2017-06-07 18:14:53 +00:00
|
|
|
tb->xalign = rofi_theme_get_double ( WIDGET ( tb ), "horizontal-align", yalign );
|
|
|
|
tb->xalign = MAX ( 0, MIN ( 1.0, tb->xalign ) );
|
2017-05-26 18:23:46 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
return tb;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 17:08:49 +00:00
|
|
|
/**
|
|
|
|
* State names used for theming.
|
|
|
|
*/
|
2017-05-11 16:30:44 +00:00
|
|
|
const char *const theme_prop_names[][3] = {
|
2017-01-01 17:08:49 +00:00
|
|
|
/** Normal row */
|
2017-01-08 20:36:06 +00:00
|
|
|
{ "normal.normal", "selected.normal", "alternate.normal" },
|
2017-01-01 17:08:49 +00:00
|
|
|
/** Urgent row */
|
2017-01-08 20:36:06 +00:00
|
|
|
{ "normal.urgent", "selected.urgent", "alternate.urgent" },
|
2017-01-01 17:08:49 +00:00
|
|
|
/** Active row */
|
2017-01-08 20:36:06 +00:00
|
|
|
{ "normal.active", "selected.active", "alternate.active" },
|
2016-12-12 20:14:57 +00:00
|
|
|
};
|
|
|
|
|
2014-05-25 21:32:06 +00:00
|
|
|
void textbox_font ( textbox *tb, TextBoxFontType tbft )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2015-10-26 13:22:58 +00:00
|
|
|
TextBoxFontType t = tbft & STATE_MASK;
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-26 13:22:58 +00:00
|
|
|
// ACTIVE has priority over URGENT if both set.
|
|
|
|
if ( t == ( URGENT | ACTIVE ) ) {
|
|
|
|
t = ACTIVE;
|
|
|
|
}
|
2015-04-06 15:13:26 +00:00
|
|
|
switch ( ( tbft & FMOD_MASK ) )
|
2014-03-22 20:04:19 +00:00
|
|
|
{
|
2014-05-25 21:32:06 +00:00
|
|
|
case HIGHLIGHT:
|
2017-01-08 20:36:06 +00:00
|
|
|
widget_set_state ( WIDGET ( tb ), theme_prop_names[t][1] );
|
2014-05-25 21:32:06 +00:00
|
|
|
break;
|
2015-01-12 18:14:46 +00:00
|
|
|
case ALT:
|
2017-01-08 20:36:06 +00:00
|
|
|
widget_set_state ( WIDGET ( tb ), theme_prop_names[t][2] );
|
2015-01-12 18:14:46 +00:00
|
|
|
break;
|
2014-05-25 21:32:06 +00:00
|
|
|
default:
|
2017-01-08 20:36:06 +00:00
|
|
|
widget_set_state ( WIDGET ( tb ), theme_prop_names[t][0] );
|
2014-05-25 21:32:06 +00:00
|
|
|
break;
|
2014-01-13 20:35:43 +00:00
|
|
|
}
|
2016-12-28 11:21:42 +00:00
|
|
|
if ( tb->tbft != tbft || tb->widget.state == NULL ) {
|
2016-10-17 18:54:02 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2015-09-26 18:34:34 +00:00
|
|
|
}
|
2015-03-31 20:45:02 +00:00
|
|
|
tb->tbft = tbft;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 07:36:20 +00:00
|
|
|
/**
|
|
|
|
* @param tb The textbox object.
|
|
|
|
*
|
|
|
|
* Update the pango layout's text. It does this depending on the
|
|
|
|
* textbox flags.
|
|
|
|
*/
|
|
|
|
static void __textbox_update_pango_text ( textbox *tb )
|
|
|
|
{
|
2017-03-10 22:39:29 +00:00
|
|
|
pango_layout_set_attributes ( tb->layout, NULL );
|
2016-01-11 07:36:20 +00:00
|
|
|
if ( ( tb->flags & TB_PASSWORD ) == TB_PASSWORD ) {
|
|
|
|
size_t l = g_utf8_strlen ( tb->text, -1 );
|
|
|
|
char string [l + 1];
|
|
|
|
memset ( string, '*', l );
|
|
|
|
string[l] = '\0';
|
|
|
|
pango_layout_set_text ( tb->layout, string, l );
|
|
|
|
}
|
2016-02-09 20:25:29 +00:00
|
|
|
else if ( tb->flags & TB_MARKUP || tb->tbft & MARKUP ) {
|
2016-01-11 20:25:38 +00:00
|
|
|
pango_layout_set_markup ( tb->layout, tb->text, -1 );
|
2016-01-11 07:36:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-01-14 16:42:07 +00:00
|
|
|
pango_layout_set_text ( tb->layout, tb->text, -1 );
|
2016-01-11 07:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-23 22:39:56 +00:00
|
|
|
const char *textbox_get_visible_text ( const textbox *tb )
|
2016-05-10 16:02:23 +00:00
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( tb == NULL ) {
|
2017-05-28 16:19:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-05-10 16:02:23 +00:00
|
|
|
return pango_layout_get_text ( tb->layout );
|
|
|
|
}
|
|
|
|
PangoAttrList *textbox_get_pango_attributes ( textbox *tb )
|
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( tb == NULL ) {
|
2017-05-28 16:19:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-05-10 16:02:23 +00:00
|
|
|
return pango_layout_get_attributes ( tb->layout );
|
|
|
|
}
|
|
|
|
void textbox_set_pango_attributes ( textbox *tb, PangoAttrList *list )
|
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( tb == NULL ) {
|
2017-05-28 16:19:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-10 16:02:23 +00:00
|
|
|
pango_layout_set_attributes ( tb->layout, list );
|
|
|
|
}
|
2016-01-11 07:36:20 +00:00
|
|
|
|
2012-08-24 01:28:39 +00:00
|
|
|
// set the default text to display
|
2015-02-03 07:21:59 +00:00
|
|
|
void textbox_text ( textbox *tb, const char *text )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( tb == NULL ) {
|
2017-05-28 16:19:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( tb->text );
|
2015-02-02 15:44:08 +00:00
|
|
|
const gchar *last_pointer = NULL;
|
2015-09-26 18:34:34 +00:00
|
|
|
|
2015-02-02 15:44:08 +00:00
|
|
|
if ( g_utf8_validate ( text, -1, &last_pointer ) ) {
|
2014-08-09 09:40:42 +00:00
|
|
|
tb->text = g_strdup ( text );
|
2014-08-07 19:42:16 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-02-02 15:44:08 +00:00
|
|
|
if ( last_pointer != NULL ) {
|
|
|
|
// Copy string up to invalid character.
|
|
|
|
tb->text = g_strndup ( text, ( last_pointer - text ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tb->text = g_strdup ( "Invalid UTF-8 string." );
|
|
|
|
}
|
2014-08-07 19:42:16 +00:00
|
|
|
}
|
2016-01-11 07:36:20 +00:00
|
|
|
__textbox_update_pango_text ( tb );
|
2015-05-23 18:06:06 +00:00
|
|
|
if ( tb->flags & TB_AUTOWIDTH ) {
|
2016-01-09 15:22:09 +00:00
|
|
|
textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( WIDGET ( tb )->parent ) {
|
2017-01-01 01:00:54 +00:00
|
|
|
widget_update ( WIDGET ( tb )->parent );
|
|
|
|
}
|
2015-05-23 18:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 06:58:48 +00:00
|
|
|
tb->cursor = MAX ( 0, MIN ( ( int ) g_utf8_strlen ( tb->text, -1 ), tb->cursor ) );
|
2016-10-17 18:54:02 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 15:58:32 +00:00
|
|
|
void textbox_icon ( textbox *tb, cairo_surface_t *icon )
|
|
|
|
{
|
2018-05-12 08:40:15 +00:00
|
|
|
// Add our reference to the surface.
|
|
|
|
if ( icon != NULL ) {
|
|
|
|
cairo_surface_reference ( icon );
|
|
|
|
}
|
|
|
|
if ( tb->icon ) {
|
|
|
|
// If we overwrite an old one, destroy the reference we hold.
|
|
|
|
cairo_surface_destroy ( tb->icon );
|
|
|
|
}
|
2017-04-12 15:58:32 +00:00
|
|
|
tb->icon = icon;
|
|
|
|
|
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:26:52 +00:00
|
|
|
// within the parent handled auto width/height modes
|
2014-03-22 20:04:19 +00:00
|
|
|
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
unsigned int offset = tb->left_offset * 1.2 + ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( tb->flags & TB_AUTOWIDTH ) {
|
2015-03-23 07:56:55 +00:00
|
|
|
pango_layout_set_width ( tb->layout, -1 );
|
2017-01-08 20:36:06 +00:00
|
|
|
w = textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( WIDGET ( tb ) ) + offset;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2014-08-02 18:02:37 +00:00
|
|
|
else {
|
|
|
|
// set ellipsize
|
2014-08-02 21:08:14 +00:00
|
|
|
if ( ( tb->flags & TB_EDITABLE ) == TB_EDITABLE ) {
|
2014-08-02 18:25:43 +00:00
|
|
|
pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_MIDDLE );
|
2014-08-02 21:08:14 +00:00
|
|
|
}
|
2015-09-23 18:44:24 +00:00
|
|
|
else if ( ( tb->flags & TB_WRAP ) != TB_WRAP ) {
|
2014-08-02 18:25:43 +00:00
|
|
|
pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_END );
|
|
|
|
}
|
2014-08-02 18:02:37 +00:00
|
|
|
}
|
2014-01-10 09:35:38 +00:00
|
|
|
|
2015-06-03 07:04:29 +00:00
|
|
|
if ( tb->flags & TB_AUTOHEIGHT ) {
|
|
|
|
// Width determines height!
|
|
|
|
int tw = MAX ( 1, w );
|
2017-01-08 20:36:06 +00:00
|
|
|
pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tw - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
|
2017-01-05 21:56:32 +00:00
|
|
|
int hd = textbox_get_height ( tb );
|
2017-01-08 20:36:06 +00:00
|
|
|
h = MAX ( hd, h );
|
2015-06-03 07:04:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 15:22:09 +00:00
|
|
|
if ( x != tb->widget.x || y != tb->widget.y || w != tb->widget.w || h != tb->widget.h ) {
|
|
|
|
tb->widget.x = x;
|
|
|
|
tb->widget.y = y;
|
|
|
|
tb->widget.h = MAX ( 1, h );
|
|
|
|
tb->widget.w = MAX ( 1, w );
|
2014-01-10 09:35:38 +00:00
|
|
|
}
|
2015-09-26 18:34:34 +00:00
|
|
|
|
2015-06-03 07:04:29 +00:00
|
|
|
// We always want to update this
|
2017-01-08 20:36:06 +00:00
|
|
|
pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
|
2016-10-17 18:54:02 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// will also unmap the window if still displayed
|
2016-10-08 16:57:59 +00:00
|
|
|
static void textbox_free ( widget *wid )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( wid == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
textbox *tb = (textbox *) wid;
|
2016-01-20 19:57:26 +00:00
|
|
|
if ( tb->blink_timeout > 0 ) {
|
|
|
|
g_source_remove ( tb->blink_timeout );
|
|
|
|
tb->blink_timeout = 0;
|
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( tb->text );
|
|
|
|
|
2018-05-12 08:40:15 +00:00
|
|
|
if ( tb->icon ) {
|
|
|
|
cairo_surface_destroy ( tb->icon );
|
|
|
|
tb->icon = NULL;
|
|
|
|
}
|
2014-08-07 19:42:16 +00:00
|
|
|
if ( tb->layout != NULL ) {
|
2014-08-02 18:02:37 +00:00
|
|
|
g_object_unref ( tb->layout );
|
|
|
|
}
|
2014-01-10 09:35:38 +00:00
|
|
|
|
2016-02-19 18:29:06 +00:00
|
|
|
g_slice_free ( textbox, tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 19:31:47 +00:00
|
|
|
static void textbox_draw ( widget *wid, cairo_t *draw )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( wid == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-17 13:07:11 +00:00
|
|
|
textbox *tb = (textbox *) wid;
|
2017-06-03 18:35:50 +00:00
|
|
|
unsigned int offset = tb->left_offset * 1.2 + ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
|
2015-09-26 18:34:34 +00:00
|
|
|
|
2017-02-02 19:31:47 +00:00
|
|
|
if ( tb->changed ) {
|
|
|
|
__textbox_update_pango_text ( tb );
|
|
|
|
}
|
2014-08-02 18:02:37 +00:00
|
|
|
|
2017-02-02 19:31:47 +00:00
|
|
|
// Skip the side MARGIN on the X axis.
|
2017-06-08 06:46:16 +00:00
|
|
|
int x = widget_padding_get_left ( WIDGET ( tb ) );
|
2017-04-12 15:58:32 +00:00
|
|
|
int top = widget_padding_get_top ( WIDGET ( tb ) );
|
2017-06-08 06:46:16 +00:00
|
|
|
int y = ( pango_font_metrics_get_ascent ( tb->metrics ) - pango_layout_get_baseline ( tb->layout ) ) / PANGO_SCALE;
|
|
|
|
int line_width = 0, line_height = 0;
|
|
|
|
// Get actual width.
|
|
|
|
pango_layout_get_pixel_size ( tb->layout, &line_width, &line_height );
|
2017-06-06 07:09:57 +00:00
|
|
|
|
|
|
|
if ( tb->yalign > 0.001 ) {
|
|
|
|
int bottom = widget_padding_get_bottom ( WIDGET ( tb ) );
|
2017-06-08 06:46:16 +00:00
|
|
|
top = ( tb->widget.h - bottom - line_height - top ) * tb->yalign + top;
|
2017-06-06 07:09:57 +00:00
|
|
|
}
|
2017-06-07 05:53:33 +00:00
|
|
|
y += top;
|
2017-04-12 15:58:32 +00:00
|
|
|
|
|
|
|
// draw Icon
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( ( tb->flags & TB_ICON ) == TB_ICON && tb->icon != NULL ) {
|
2017-06-03 13:36:21 +00:00
|
|
|
int iconheight = tb->left_offset;
|
2017-06-03 18:35:50 +00:00
|
|
|
cairo_save ( draw );
|
2017-04-12 15:58:32 +00:00
|
|
|
|
2017-06-03 18:35:50 +00:00
|
|
|
int iconh = cairo_image_surface_get_height ( tb->icon );
|
2017-09-21 17:26:54 +00:00
|
|
|
int iconw = cairo_image_surface_get_width ( tb->icon );
|
2017-10-05 15:45:50 +00:00
|
|
|
int icons = MAX ( iconh, iconw );
|
2017-09-21 17:26:54 +00:00
|
|
|
double scale = (double) iconheight / icons;
|
2017-10-05 15:45:50 +00:00
|
|
|
cairo_translate ( draw, x + ( iconheight - iconw * scale ) / 2.0, y + ( iconheight - iconh * scale ) / 2.0 );
|
2017-06-03 18:35:50 +00:00
|
|
|
cairo_scale ( draw, scale, scale );
|
2017-10-05 15:45:50 +00:00
|
|
|
cairo_set_source_surface ( draw, tb->icon, 0, 0 );
|
2017-06-03 18:35:50 +00:00
|
|
|
cairo_paint ( draw );
|
|
|
|
cairo_restore ( draw );
|
2017-04-12 15:58:32 +00:00
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
x += offset;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2017-06-07 18:14:53 +00:00
|
|
|
if ( tb->xalign > 0.001 ) {
|
|
|
|
int rem = MAX ( 0, tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - line_width );
|
|
|
|
x = tb->xalign * rem + widget_padding_get_left ( WIDGET ( tb ) );
|
2017-02-02 19:31:47 +00:00
|
|
|
}
|
2017-06-04 14:08:34 +00:00
|
|
|
// TODO check if this is still needed after flatning.
|
|
|
|
cairo_set_operator ( draw, CAIRO_OPERATOR_OVER );
|
2017-06-22 06:54:29 +00:00
|
|
|
cairo_set_source_rgb ( draw, 0.0, 0.0, 0.0 );
|
2017-09-07 06:35:09 +00:00
|
|
|
rofi_theme_get_color ( WIDGET ( tb ), "text-color", draw );
|
2017-02-02 19:31:47 +00:00
|
|
|
// draw the cursor
|
|
|
|
if ( tb->flags & TB_EDITABLE && tb->blink ) {
|
2017-03-14 16:02:03 +00:00
|
|
|
// We want to place the cursor based on the text shown.
|
|
|
|
const char *text = pango_layout_get_text ( tb->layout );
|
|
|
|
// Clamp the position, should not be needed, but we are paranoid.
|
|
|
|
int cursor_offset = MIN ( tb->cursor, g_utf8_strlen ( text, -1 ) );
|
|
|
|
PangoRectangle pos;
|
|
|
|
// convert to byte location.
|
|
|
|
char *offset = g_utf8_offset_to_pointer ( text, cursor_offset );
|
|
|
|
pango_layout_get_cursor_pos ( tb->layout, offset - text, &pos, NULL );
|
2017-03-17 13:07:11 +00:00
|
|
|
int cursor_x = pos.x / PANGO_SCALE;
|
|
|
|
int cursor_y = pos.y / PANGO_SCALE;
|
|
|
|
int cursor_height = pos.height / PANGO_SCALE;
|
|
|
|
int cursor_width = 2;
|
2017-02-02 19:31:47 +00:00
|
|
|
cairo_rectangle ( draw, x + cursor_x, y + cursor_y, cursor_width, cursor_height );
|
|
|
|
cairo_fill ( draw );
|
|
|
|
}
|
2015-12-28 10:24:25 +00:00
|
|
|
|
2017-02-02 19:31:47 +00:00
|
|
|
// Set ARGB
|
|
|
|
// We need to set over, otherwise subpixel hinting wont work.
|
2017-02-24 08:54:11 +00:00
|
|
|
cairo_move_to ( draw, x, top );
|
2017-02-02 19:31:47 +00:00
|
|
|
pango_cairo_show_layout ( draw, tb->layout );
|
2016-07-18 21:49:26 +00:00
|
|
|
|
2017-02-02 19:31:47 +00:00
|
|
|
if ( ( tb->flags & TB_INDICATOR ) == TB_INDICATOR && ( tb->tbft & ( SELECTED ) ) ) {
|
2017-06-03 18:35:50 +00:00
|
|
|
cairo_arc ( draw, tb->left_offset * 1.2 + DOT_OFFSET / 2.0, tb->widget.h / 2.0, 2.0, 0, 2.0 * M_PI );
|
2017-02-02 19:31:47 +00:00
|
|
|
cairo_fill ( draw );
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2015-09-26 18:34:34 +00:00
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
|
|
|
|
// cursor handling for edit mode
|
2014-03-22 20:04:19 +00:00
|
|
|
void textbox_cursor ( textbox *tb, int pos )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
int length = ( tb->text == NULL ) ? 0 : g_utf8_strlen ( tb->text, -1 );
|
2015-02-13 14:37:55 +00:00
|
|
|
tb->cursor = MAX ( 0, MIN ( length, pos ) );
|
2016-09-16 18:35:46 +00:00
|
|
|
// Stop blink!
|
2016-10-17 18:54:02 +00:00
|
|
|
tb->blink = 3;
|
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 07:41:32 +00:00
|
|
|
/**
|
|
|
|
* @param tb Handle to the textbox
|
|
|
|
*
|
|
|
|
* Move cursor one position forward.
|
2017-05-12 14:08:49 +00:00
|
|
|
*
|
|
|
|
* @returns if cursor was moved.
|
2016-10-20 07:41:32 +00:00
|
|
|
*/
|
2017-05-12 14:08:49 +00:00
|
|
|
static int textbox_cursor_inc ( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-12 14:08:49 +00:00
|
|
|
int old = tb->cursor;
|
2016-08-15 06:58:48 +00:00
|
|
|
textbox_cursor ( tb, tb->cursor + 1 );
|
2017-05-15 20:46:33 +00:00
|
|
|
return old != tb->cursor;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 07:41:32 +00:00
|
|
|
/**
|
|
|
|
* @param tb Handle to the textbox
|
|
|
|
*
|
|
|
|
* Move cursor one position backward.
|
2017-05-12 14:08:49 +00:00
|
|
|
*
|
|
|
|
* @returns if cursor was moved.
|
2016-10-20 07:41:32 +00:00
|
|
|
*/
|
2017-05-12 14:08:49 +00:00
|
|
|
static int textbox_cursor_dec ( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-12 14:08:49 +00:00
|
|
|
int old = tb->cursor;
|
2016-08-15 06:58:48 +00:00
|
|
|
textbox_cursor ( tb, tb->cursor - 1 );
|
2017-05-15 20:46:33 +00:00
|
|
|
return old != tb->cursor;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 15:42:42 +00:00
|
|
|
// Move word right
|
2015-02-18 07:53:38 +00:00
|
|
|
static void textbox_cursor_inc_word ( textbox *tb )
|
2015-02-07 15:42:42 +00:00
|
|
|
{
|
2015-02-15 20:15:16 +00:00
|
|
|
if ( tb->text == NULL ) {
|
2015-02-13 14:37:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 15:42:42 +00:00
|
|
|
// Find word boundaries, with pango_Break?
|
2016-08-15 06:58:48 +00:00
|
|
|
gchar *c = g_utf8_offset_to_pointer ( tb->text, tb->cursor );
|
2015-02-07 15:42:42 +00:00
|
|
|
while ( ( c = g_utf8_next_char ( c ) ) ) {
|
|
|
|
gunichar uc = g_utf8_get_char ( c );
|
|
|
|
GUnicodeBreakType bt = g_unichar_break_type ( uc );
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( ( bt == G_UNICODE_BREAK_ALPHABETIC || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
|
|
|
|
bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION ) ) {
|
2015-02-07 15:42:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 19:34:19 +00:00
|
|
|
if ( c == NULL || *c == '\0' ) {
|
2015-02-13 14:37:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 15:42:42 +00:00
|
|
|
while ( ( c = g_utf8_next_char ( c ) ) ) {
|
|
|
|
gunichar uc = g_utf8_get_char ( c );
|
|
|
|
GUnicodeBreakType bt = g_unichar_break_type ( uc );
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( !( bt == G_UNICODE_BREAK_ALPHABETIC || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
|
|
|
|
bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION ) ) {
|
2015-02-07 15:42:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
int index = g_utf8_pointer_to_offset ( tb->text, c );
|
2015-02-07 15:42:42 +00:00
|
|
|
textbox_cursor ( tb, index );
|
|
|
|
}
|
|
|
|
// move word left
|
2015-02-18 07:53:38 +00:00
|
|
|
static void textbox_cursor_dec_word ( textbox *tb )
|
2015-02-07 15:42:42 +00:00
|
|
|
{
|
|
|
|
// Find word boundaries, with pango_Break?
|
|
|
|
gchar *n;
|
2016-08-15 06:58:48 +00:00
|
|
|
gchar *c = g_utf8_offset_to_pointer ( tb->text, tb->cursor );
|
2015-02-07 15:42:42 +00:00
|
|
|
while ( ( c = g_utf8_prev_char ( c ) ) && c != tb->text ) {
|
|
|
|
gunichar uc = g_utf8_get_char ( c );
|
|
|
|
GUnicodeBreakType bt = g_unichar_break_type ( uc );
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( ( bt == G_UNICODE_BREAK_ALPHABETIC || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
|
|
|
|
bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION ) ) {
|
2015-02-07 15:42:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( c != tb->text ) {
|
|
|
|
while ( ( n = g_utf8_prev_char ( c ) ) ) {
|
|
|
|
gunichar uc = g_utf8_get_char ( n );
|
|
|
|
GUnicodeBreakType bt = g_unichar_break_type ( uc );
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( !( bt == G_UNICODE_BREAK_ALPHABETIC || bt == G_UNICODE_BREAK_HEBREW_LETTER ||
|
|
|
|
bt == G_UNICODE_BREAK_NUMERIC || bt == G_UNICODE_BREAK_QUOTATION ) ) {
|
2015-02-07 15:42:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = n;
|
|
|
|
if ( n == tb->text ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
int index = g_utf8_pointer_to_offset ( tb->text, c );
|
2015-02-07 15:42:42 +00:00
|
|
|
textbox_cursor ( tb, index );
|
|
|
|
}
|
|
|
|
|
2012-08-24 01:28:39 +00:00
|
|
|
// end of line
|
2017-08-29 08:40:24 +00:00
|
|
|
void textbox_cursor_end ( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2015-12-23 16:59:03 +00:00
|
|
|
if ( tb->text == NULL ) {
|
|
|
|
tb->cursor = 0;
|
2016-10-17 18:54:02 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2015-12-23 16:59:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
tb->cursor = ( int ) g_utf8_strlen ( tb->text, -1 );
|
2016-10-17 18:54:02 +00:00
|
|
|
widget_queue_redraw ( WIDGET ( tb ) );
|
2016-09-16 18:35:46 +00:00
|
|
|
// Stop blink!
|
|
|
|
tb->blink = 2;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert text
|
2016-08-23 22:39:56 +00:00
|
|
|
void textbox_insert ( textbox *tb, const int char_pos, const char *str, const int slen )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
char *c = g_utf8_offset_to_pointer ( tb->text, char_pos );
|
|
|
|
int pos = c - tb->text;
|
|
|
|
int len = ( int ) strlen ( tb->text );
|
2014-03-22 20:04:19 +00:00
|
|
|
pos = MAX ( 0, MIN ( len, pos ) );
|
2014-01-10 09:35:38 +00:00
|
|
|
// expand buffer
|
2014-08-09 09:40:42 +00:00
|
|
|
tb->text = g_realloc ( tb->text, len + slen + 1 );
|
2014-01-10 09:35:38 +00:00
|
|
|
// move everything after cursor upward
|
|
|
|
char *at = tb->text + pos;
|
2014-03-22 20:04:19 +00:00
|
|
|
memmove ( at + slen, at, len - pos + 1 );
|
2014-01-10 09:35:38 +00:00
|
|
|
// insert new str
|
2014-03-22 20:04:19 +00:00
|
|
|
memmove ( at, str, slen );
|
2015-05-23 18:06:06 +00:00
|
|
|
|
|
|
|
// Set modified, lay out need te be redrawn
|
2016-09-16 18:35:46 +00:00
|
|
|
// Stop blink!
|
2016-09-17 16:34:04 +00:00
|
|
|
tb->blink = 2;
|
2015-05-23 18:06:06 +00:00
|
|
|
tb->changed = TRUE;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove text
|
2014-03-22 20:04:19 +00:00
|
|
|
void textbox_delete ( textbox *tb, int pos, int dlen )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
int len = g_utf8_strlen ( tb->text, -1 );
|
|
|
|
if ( len == pos ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 20:04:19 +00:00
|
|
|
pos = MAX ( 0, MIN ( len, pos ) );
|
2016-08-15 06:58:48 +00:00
|
|
|
if ( ( pos + dlen ) > len ) {
|
|
|
|
dlen = len - dlen;
|
|
|
|
}
|
2014-01-10 09:35:38 +00:00
|
|
|
// move everything after pos+dlen down
|
2016-08-15 06:58:48 +00:00
|
|
|
char *start = g_utf8_offset_to_pointer ( tb->text, pos );
|
|
|
|
char *end = g_utf8_offset_to_pointer ( tb->text, pos + dlen );
|
2014-08-30 18:45:08 +00:00
|
|
|
// Move remainder + closing \0
|
2016-08-15 06:58:48 +00:00
|
|
|
memmove ( start, end, ( tb->text + strlen ( tb->text ) ) - end + 1 );
|
2014-09-03 11:07:26 +00:00
|
|
|
if ( tb->cursor >= pos && tb->cursor < ( pos + dlen ) ) {
|
|
|
|
tb->cursor = pos;
|
|
|
|
}
|
|
|
|
else if ( tb->cursor >= ( pos + dlen ) ) {
|
2014-08-30 18:45:08 +00:00
|
|
|
tb->cursor -= dlen;
|
|
|
|
}
|
2015-05-23 18:06:06 +00:00
|
|
|
// Set modified, lay out need te be redrawn
|
2016-09-16 18:35:46 +00:00
|
|
|
// Stop blink!
|
2016-09-17 16:34:04 +00:00
|
|
|
tb->blink = 2;
|
2015-05-23 18:06:06 +00:00
|
|
|
tb->changed = TRUE;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 07:41:32 +00:00
|
|
|
/**
|
|
|
|
* @param tb Handle to the textbox
|
|
|
|
*
|
|
|
|
* Delete character after cursor.
|
|
|
|
*/
|
|
|
|
static void textbox_cursor_del ( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb == NULL || tb->text == NULL ) {
|
2015-02-13 14:37:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-15 06:58:48 +00:00
|
|
|
textbox_delete ( tb, tb->cursor, 1 );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 07:41:32 +00:00
|
|
|
/**
|
|
|
|
* @param tb Handle to the textbox
|
|
|
|
*
|
|
|
|
* Delete character before cursor.
|
|
|
|
*/
|
|
|
|
static void textbox_cursor_bkspc ( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb && tb->cursor > 0 ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
textbox_cursor_dec ( tb );
|
|
|
|
textbox_cursor_del ( tb );
|
2014-01-10 09:35:38 +00:00
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
2015-02-18 07:53:38 +00:00
|
|
|
static void textbox_cursor_bkspc_word ( textbox *tb )
|
2015-02-07 15:42:42 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb && tb->cursor > 0 ) {
|
2015-02-07 15:42:42 +00:00
|
|
|
int cursor = tb->cursor;
|
|
|
|
textbox_cursor_dec_word ( tb );
|
|
|
|
if ( cursor > tb->cursor ) {
|
|
|
|
textbox_delete ( tb, tb->cursor, cursor - tb->cursor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 09:32:30 +00:00
|
|
|
static void textbox_cursor_del_eol ( textbox *tb )
|
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb && tb->cursor >= 0 ) {
|
2016-08-15 06:58:48 +00:00
|
|
|
int length = g_utf8_strlen ( tb->text, -1 ) - tb->cursor;
|
2016-07-25 09:32:30 +00:00
|
|
|
if ( length >= 0 ) {
|
|
|
|
textbox_delete ( tb, tb->cursor, length );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 06:10:55 +00:00
|
|
|
static void textbox_cursor_del_sol ( textbox *tb )
|
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb && tb->cursor >= 0 ) {
|
2016-07-27 06:10:55 +00:00
|
|
|
int length = tb->cursor;
|
|
|
|
if ( length >= 0 ) {
|
|
|
|
textbox_delete ( tb, 0, length );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 07:53:38 +00:00
|
|
|
static void textbox_cursor_del_word ( textbox *tb )
|
2015-02-07 15:42:42 +00:00
|
|
|
{
|
2017-06-03 18:35:50 +00:00
|
|
|
if ( tb && tb->cursor >= 0 ) {
|
2015-02-07 15:42:42 +00:00
|
|
|
int cursor = tb->cursor;
|
|
|
|
textbox_cursor_inc_word ( tb );
|
|
|
|
if ( cursor < tb->cursor ) {
|
|
|
|
textbox_delete ( tb, cursor, tb->cursor - cursor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
|
|
|
|
// handle a keypress in edit mode
|
2015-08-29 21:02:30 +00:00
|
|
|
// 2 = nav
|
2012-08-24 01:28:39 +00:00
|
|
|
// 0 = unhandled
|
|
|
|
// 1 = handled
|
|
|
|
// -1 = handled and return pressed (finished)
|
2016-04-07 13:32:22 +00:00
|
|
|
int textbox_keybinding ( textbox *tb, KeyBindingAction action )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( !( tb->flags & TB_EDITABLE ) ) {
|
2016-09-17 16:34:04 +00:00
|
|
|
return 0;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2016-04-07 13:01:14 +00:00
|
|
|
|
2016-04-07 13:32:22 +00:00
|
|
|
switch ( action )
|
|
|
|
{
|
2016-04-07 13:07:02 +00:00
|
|
|
// Left or Ctrl-b
|
2016-04-07 13:32:22 +00:00
|
|
|
case MOVE_CHAR_BACK:
|
2017-05-15 20:46:33 +00:00
|
|
|
return ( textbox_cursor_dec ( tb ) == TRUE ) ? 2 : 0;
|
2016-04-07 13:07:02 +00:00
|
|
|
// Right or Ctrl-F
|
2016-04-07 13:32:22 +00:00
|
|
|
case MOVE_CHAR_FORWARD:
|
2017-05-15 20:46:33 +00:00
|
|
|
return ( textbox_cursor_inc ( tb ) == TRUE ) ? 2 : 0;
|
2016-04-07 13:07:02 +00:00
|
|
|
// Ctrl-U: Kill from the beginning to the end of the line.
|
2016-04-07 13:32:22 +00:00
|
|
|
case CLEAR_LINE:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_text ( tb, "" );
|
|
|
|
return 1;
|
|
|
|
// Ctrl-A
|
2016-04-07 13:32:22 +00:00
|
|
|
case MOVE_FRONT:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor ( tb, 0 );
|
|
|
|
return 2;
|
|
|
|
// Ctrl-E
|
2016-04-07 13:32:22 +00:00
|
|
|
case MOVE_END:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_end ( tb );
|
|
|
|
return 2;
|
|
|
|
// Ctrl-Alt-h
|
2016-04-07 13:32:22 +00:00
|
|
|
case REMOVE_WORD_BACK:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_bkspc_word ( tb );
|
|
|
|
return 1;
|
|
|
|
// Ctrl-Alt-d
|
2016-04-07 13:32:22 +00:00
|
|
|
case REMOVE_WORD_FORWARD:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_del_word ( tb );
|
|
|
|
return 1;
|
2016-07-25 09:32:30 +00:00
|
|
|
case REMOVE_TO_EOL:
|
|
|
|
textbox_cursor_del_eol ( tb );
|
|
|
|
return 1;
|
2016-07-27 06:10:55 +00:00
|
|
|
case REMOVE_TO_SOL:
|
|
|
|
textbox_cursor_del_sol ( tb );
|
|
|
|
return 1;
|
2016-04-07 13:32:22 +00:00
|
|
|
// Delete or Ctrl-D
|
|
|
|
case REMOVE_CHAR_FORWARD:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_del ( tb );
|
|
|
|
return 1;
|
2018-06-13 12:15:31 +00:00
|
|
|
// Alt-B, Ctrl-Left
|
2016-04-07 13:32:22 +00:00
|
|
|
case MOVE_WORD_BACK:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_dec_word ( tb );
|
|
|
|
return 2;
|
2018-06-13 12:15:31 +00:00
|
|
|
// Alt-F, Ctrl-Right
|
2016-04-07 13:32:22 +00:00
|
|
|
case MOVE_WORD_FORWARD:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_inc_word ( tb );
|
|
|
|
return 2;
|
2018-06-13 12:15:31 +00:00
|
|
|
// BackSpace, Shift-BackSpace, Ctrl-h
|
2016-04-07 13:32:22 +00:00
|
|
|
case REMOVE_CHAR_BACK:
|
2016-04-07 13:07:02 +00:00
|
|
|
textbox_cursor_bkspc ( tb );
|
|
|
|
return 1;
|
2016-04-07 13:32:22 +00:00
|
|
|
default:
|
2016-05-08 09:13:11 +00:00
|
|
|
g_return_val_if_reached ( 0 );
|
2016-04-07 13:07:02 +00:00
|
|
|
}
|
2016-04-07 13:01:14 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 11:24:44 +00:00
|
|
|
gboolean textbox_append_text ( textbox *tb, const char *pad, const int pad_len )
|
2016-04-07 13:01:14 +00:00
|
|
|
{
|
2017-05-28 16:19:59 +00:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-04-07 13:01:14 +00:00
|
|
|
if ( !( tb->flags & TB_EDITABLE ) ) {
|
2016-04-07 13:11:42 +00:00
|
|
|
return FALSE;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2016-04-07 13:11:42 +00:00
|
|
|
|
|
|
|
// Filter When alt/ctrl is pressed do not accept the character.
|
2017-06-01 11:24:44 +00:00
|
|
|
|
2017-06-03 18:35:50 +00:00
|
|
|
gboolean used_something = FALSE;
|
2017-06-01 11:24:44 +00:00
|
|
|
const gchar *w, *n, *e;
|
2017-06-03 18:35:50 +00:00
|
|
|
for ( w = pad, n = g_utf8_next_char ( w ), e = w + pad_len; w < e; w = n, n = g_utf8_next_char ( n ) ) {
|
2017-06-01 11:24:44 +00:00
|
|
|
if ( g_unichar_iscntrl ( g_utf8_get_char ( w ) ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
textbox_insert ( tb, tb->cursor, w, n - w );
|
2016-08-15 06:58:48 +00:00
|
|
|
textbox_cursor ( tb, tb->cursor + 1 );
|
2017-06-01 11:24:44 +00:00
|
|
|
used_something = TRUE;
|
2014-01-10 09:35:38 +00:00
|
|
|
}
|
2017-06-01 11:24:44 +00:00
|
|
|
return used_something;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
2014-05-25 21:32:06 +00:00
|
|
|
|
2017-01-24 07:40:37 +00:00
|
|
|
static void tbfc_entry_free ( TBFontConfig *tbfc )
|
|
|
|
{
|
2017-02-03 19:49:16 +00:00
|
|
|
pango_font_metrics_unref ( tbfc->metrics );
|
|
|
|
if ( tbfc->pfd ) {
|
2017-01-24 07:40:37 +00:00
|
|
|
pango_font_description_free ( tbfc->pfd );
|
|
|
|
}
|
2017-02-02 07:34:42 +00:00
|
|
|
g_free ( tbfc );
|
2017-01-24 07:40:37 +00:00
|
|
|
}
|
2016-02-21 18:42:32 +00:00
|
|
|
void textbox_setup ( void )
|
2014-05-25 21:32:06 +00:00
|
|
|
{
|
2017-02-03 19:49:16 +00:00
|
|
|
tbfc_cache = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, (GDestroyNotify) tbfc_entry_free );
|
2015-12-28 10:24:25 +00:00
|
|
|
}
|
2017-03-11 12:20:46 +00:00
|
|
|
|
|
|
|
/** Name of the default font (if none is given) */
|
2017-01-24 07:40:37 +00:00
|
|
|
const char *default_font_name = "default";
|
|
|
|
void textbox_set_pango_context ( const char *font, PangoContext *p )
|
2015-12-28 10:24:25 +00:00
|
|
|
{
|
2017-02-03 19:49:16 +00:00
|
|
|
g_assert ( p_metrics == NULL );
|
2015-12-28 10:24:25 +00:00
|
|
|
p_context = g_object_ref ( p );
|
2016-07-30 18:39:45 +00:00
|
|
|
p_metrics = pango_context_get_metrics ( p_context, NULL, NULL );
|
2017-02-03 19:49:16 +00:00
|
|
|
TBFontConfig *tbfc = g_malloc0 ( sizeof ( TBFontConfig ) );
|
2017-01-24 07:40:37 +00:00
|
|
|
tbfc->metrics = p_metrics;
|
2017-06-04 14:08:34 +00:00
|
|
|
tbfc->height = pango_font_metrics_get_ascent ( tbfc->metrics ) + pango_font_metrics_get_descent ( tbfc->metrics );
|
2017-02-03 19:49:16 +00:00
|
|
|
g_hash_table_insert ( tbfc_cache, (gpointer *) ( font ? font : default_font_name ), tbfc );
|
2015-04-06 15:13:26 +00:00
|
|
|
}
|
2014-05-25 21:32:06 +00:00
|
|
|
|
2015-02-18 07:53:38 +00:00
|
|
|
void textbox_cleanup ( void )
|
2014-05-25 21:32:06 +00:00
|
|
|
{
|
2017-01-24 07:40:37 +00:00
|
|
|
g_hash_table_destroy ( tbfc_cache );
|
2014-08-02 18:02:37 +00:00
|
|
|
if ( p_context ) {
|
|
|
|
g_object_unref ( p_context );
|
2015-09-26 18:34:34 +00:00
|
|
|
p_context = NULL;
|
2014-05-25 21:32:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-02 18:02:37 +00:00
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
int textbox_get_width ( widget *wid )
|
2014-08-02 18:02:37 +00:00
|
|
|
{
|
2016-10-08 16:57:59 +00:00
|
|
|
textbox *tb = (textbox *) wid;
|
2016-12-27 23:37:32 +00:00
|
|
|
if ( tb->flags & TB_AUTOWIDTH ) {
|
|
|
|
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
|
2016-12-29 16:31:37 +00:00
|
|
|
return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset;
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
return tb->widget.w;
|
2014-08-02 18:02:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
int _textbox_get_height ( widget *wid )
|
|
|
|
{
|
|
|
|
textbox *tb = (textbox *) wid;
|
2016-12-27 23:37:32 +00:00
|
|
|
if ( tb->flags & TB_AUTOHEIGHT ) {
|
|
|
|
return textbox_get_height ( tb );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
return tb->widget.h;
|
|
|
|
}
|
2016-08-23 22:39:56 +00:00
|
|
|
int textbox_get_height ( const textbox *tb )
|
2014-08-02 18:02:37 +00:00
|
|
|
{
|
2017-01-08 20:36:06 +00:00
|
|
|
return textbox_get_font_height ( tb ) + widget_padding_get_padding_height ( WIDGET ( tb ) );
|
2014-08-02 18:02:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 22:39:56 +00:00
|
|
|
int textbox_get_font_height ( const textbox *tb )
|
2014-08-02 18:02:37 +00:00
|
|
|
{
|
|
|
|
int height;
|
|
|
|
pango_layout_get_pixel_size ( tb->layout, NULL, &height );
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2016-08-23 22:39:56 +00:00
|
|
|
int textbox_get_font_width ( const textbox *tb )
|
2014-08-02 18:02:37 +00:00
|
|
|
{
|
2017-06-03 12:10:07 +00:00
|
|
|
PangoRectangle rect;
|
|
|
|
pango_layout_get_pixel_extents ( tb->layout, NULL, &rect );
|
2017-06-03 18:35:50 +00:00
|
|
|
return rect.width + rect.x;
|
2014-08-02 18:02:37 +00:00
|
|
|
}
|
2014-08-11 18:21:29 +00:00
|
|
|
|
2018-06-12 10:16:33 +00:00
|
|
|
/** Caching for the estimated character height. (em) */
|
2016-12-31 22:00:06 +00:00
|
|
|
static double char_height = -1;
|
|
|
|
double textbox_get_estimated_char_height ( void )
|
|
|
|
{
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( char_height < 0 ) {
|
2016-12-31 22:00:06 +00:00
|
|
|
int height = pango_font_metrics_get_ascent ( p_metrics ) + pango_font_metrics_get_descent ( p_metrics );
|
|
|
|
char_height = ( height ) / (double) PANGO_SCALE;
|
|
|
|
}
|
|
|
|
return char_height;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:08:49 +00:00
|
|
|
/** Caching for the expected character width. */
|
2016-12-31 22:00:06 +00:00
|
|
|
static double char_width = -1;
|
2015-02-18 07:53:38 +00:00
|
|
|
double textbox_get_estimated_char_width ( void )
|
2014-08-11 18:21:29 +00:00
|
|
|
{
|
2017-01-08 20:36:06 +00:00
|
|
|
if ( char_width < 0 ) {
|
2016-12-31 22:00:06 +00:00
|
|
|
int width = pango_font_metrics_get_approximate_char_width ( p_metrics );
|
|
|
|
char_width = ( width ) / (double) PANGO_SCALE;
|
|
|
|
}
|
|
|
|
return char_width;
|
2014-08-11 18:21:29 +00:00
|
|
|
}
|
2015-03-19 18:58:05 +00:00
|
|
|
|
2018-06-12 10:16:33 +00:00
|
|
|
/** Cache storing the estimated width of a digit (ch). */
|
2017-09-05 11:52:44 +00:00
|
|
|
static double ch_width = -1;
|
2017-09-05 11:52:21 +00:00
|
|
|
double textbox_get_estimated_ch ( void )
|
|
|
|
{
|
|
|
|
if ( ch_width < 0 ) {
|
|
|
|
int width = pango_font_metrics_get_approximate_digit_width ( p_metrics );
|
|
|
|
ch_width = ( width ) / (double) PANGO_SCALE;
|
|
|
|
}
|
|
|
|
return ch_width;
|
|
|
|
}
|
|
|
|
|
2016-12-31 22:00:06 +00:00
|
|
|
int textbox_get_estimated_height ( const textbox *tb, int eh )
|
2015-03-19 18:58:05 +00:00
|
|
|
{
|
2017-01-24 07:40:37 +00:00
|
|
|
int height = pango_font_metrics_get_ascent ( tb->metrics ) + pango_font_metrics_get_descent ( tb->metrics );
|
2017-01-08 20:36:06 +00:00
|
|
|
return ( eh * height ) / PANGO_SCALE + widget_padding_get_padding_height ( WIDGET ( tb ) );
|
2015-03-19 18:58:05 +00:00
|
|
|
}
|
2017-05-23 07:12:04 +00:00
|
|
|
int textbox_get_desired_width ( widget *wid )
|
|
|
|
{
|
2017-05-28 00:18:09 +00:00
|
|
|
textbox *tb = (textbox *) wid;
|
2017-06-03 18:35:50 +00:00
|
|
|
unsigned int offset = tb->left_offset * 1.2 + ( ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0 );
|
2017-05-28 14:10:11 +00:00
|
|
|
if ( wid->expand && tb->flags & TB_AUTOWIDTH ) {
|
2017-05-23 07:12:04 +00:00
|
|
|
return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset;
|
|
|
|
}
|
2017-06-03 18:35:50 +00:00
|
|
|
RofiDistance w = rofi_theme_get_distance ( WIDGET ( tb ), "width", 0 );
|
|
|
|
int wi = distance_get_pixel ( w, ROFI_ORIENTATION_HORIZONTAL );
|
|
|
|
if ( wi > 0 ) {
|
2017-05-28 14:10:11 +00:00
|
|
|
return wi;
|
|
|
|
}
|
2017-06-03 12:10:07 +00:00
|
|
|
int padding = widget_padding_get_left ( WIDGET ( tb ) );
|
|
|
|
padding += widget_padding_get_right ( WIDGET ( tb ) );
|
|
|
|
int old_width = pango_layout_get_width ( tb->layout );
|
2017-05-28 00:18:09 +00:00
|
|
|
pango_layout_set_width ( tb->layout, -1 );
|
2017-06-03 12:10:07 +00:00
|
|
|
int width = textbox_get_font_width ( tb );
|
2017-05-23 07:12:04 +00:00
|
|
|
// Restore.
|
2017-06-03 12:10:07 +00:00
|
|
|
pango_layout_set_width ( tb->layout, old_width );
|
|
|
|
return width + padding + offset;
|
2017-05-23 07:12:04 +00:00
|
|
|
}
|