2015-03-08 10:43:31 -04:00
|
|
|
/**
|
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright (c) 2012 Sean Pringle <sean.pringle@gmail.com>
|
2015-12-31 18:27:00 -05:00
|
|
|
* Modified (c) 2013-2016 Qball Cow <qball@gmpclient.org>
|
2015-03-08 10:43:31 -04: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.
|
2014-03-22 16:04:19 -04:00
|
|
|
*/
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2014-08-22 03:26:46 -04:00
|
|
|
#include <config.h>
|
2016-02-19 16:43:53 -05:00
|
|
|
#include <xcb/xcb.h>
|
2014-01-19 07:57:54 -05:00
|
|
|
#include <ctype.h>
|
2016-01-07 10:01:56 -05:00
|
|
|
#include <string.h>
|
2015-09-27 06:57:54 -04:00
|
|
|
#include <glib.h>
|
2016-01-07 10:01:56 -05:00
|
|
|
#include "settings.h"
|
2014-01-19 07:57:54 -05:00
|
|
|
#include "textbox.h"
|
2015-04-30 15:47:32 -04:00
|
|
|
#include "keyb.h"
|
2015-09-27 06:57:54 -04:00
|
|
|
#include "x11-helper.h"
|
2016-02-10 16:12:49 -05:00
|
|
|
#include "mode.h"
|
|
|
|
#include "view.h"
|
2015-09-27 06:57:54 -04:00
|
|
|
|
2015-03-22 07:56:26 -04:00
|
|
|
#define SIDE_MARGIN 1
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2014-05-26 03:00:14 -04:00
|
|
|
/**
|
|
|
|
* Font + font color cache.
|
|
|
|
* Avoid re-loading font on every change on every textbox.
|
|
|
|
*/
|
2016-03-24 17:13:19 -04:00
|
|
|
typedef struct
|
2015-04-06 11:13:26 -04:00
|
|
|
{
|
2015-09-26 14:34:34 -04:00
|
|
|
Color fg;
|
|
|
|
Color bg;
|
|
|
|
Color bgalt;
|
|
|
|
Color hlfg;
|
|
|
|
Color hlbg;
|
2015-04-06 11:13:26 -04:00
|
|
|
} RowColor;
|
|
|
|
|
|
|
|
#define num_states 3
|
2015-09-19 06:57:48 -04:00
|
|
|
RowColor colors[num_states];
|
2014-08-02 14:02:37 -04:00
|
|
|
|
|
|
|
PangoContext *p_context = NULL;
|
2016-02-06 07:06:58 -05:00
|
|
|
static gboolean textbox_blink ( gpointer data )
|
2016-01-20 14:57:26 -05:00
|
|
|
{
|
2016-02-06 07:06:58 -05:00
|
|
|
textbox *tb = (textbox *) data;
|
2016-01-22 12:18:26 -05:00
|
|
|
if ( tb->blink < 2 ) {
|
2016-01-20 14:57:26 -05:00
|
|
|
tb->blink = !tb->blink;
|
|
|
|
tb->update = TRUE;
|
2016-02-06 07:06:58 -05:00
|
|
|
rofi_view_queue_redraw ( );
|
2016-01-22 12:18:26 -05:00
|
|
|
}
|
|
|
|
else {
|
2016-01-20 14:57:26 -05:00
|
|
|
tb->blink--;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2014-08-02 14:02:37 -04:00
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
textbox* textbox_create ( TextboxFlags flags, short x, short y, short w, short h,
|
2015-09-19 06:57:48 -04:00
|
|
|
TextBoxFontType tbft, const char *text )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2016-02-19 13:29:06 -05:00
|
|
|
textbox *tb = g_slice_new0 ( textbox );
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->flags = flags;
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2016-01-09 10:22:09 -05:00
|
|
|
tb->widget.x = x;
|
|
|
|
tb->widget.y = y;
|
|
|
|
tb->widget.w = MAX ( 1, w );
|
|
|
|
tb->widget.h = MAX ( 1, h );
|
2012-11-06 23:16:59 -05:00
|
|
|
|
2015-05-23 14:06:06 -04:00
|
|
|
tb->changed = FALSE;
|
|
|
|
|
2016-02-21 13:18:58 -05:00
|
|
|
tb->main_surface = cairo_image_surface_create ( CAIRO_FORMAT_ARGB32, tb->widget.w, tb->widget.h );
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->main_draw = cairo_create ( tb->main_surface );
|
2015-12-28 05:24:25 -05:00
|
|
|
tb->layout = pango_layout_new ( p_context );
|
2014-05-25 17:32:06 -04:00
|
|
|
textbox_font ( tb, tbft );
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2015-09-23 14:44:24 -04:00
|
|
|
if ( ( flags & TB_WRAP ) == TB_WRAP ) {
|
2015-06-03 03:04:29 -04:00
|
|
|
pango_layout_set_wrap ( tb->layout, PANGO_WRAP_WORD_CHAR );
|
2015-05-23 14:06:06 -04:00
|
|
|
}
|
2015-09-22 16:23:52 -04:00
|
|
|
textbox_text ( tb, text ? text : "" );
|
2014-03-22 16:04:19 -04:00
|
|
|
textbox_cursor_end ( tb );
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2014-01-10 04:35:38 -05:00
|
|
|
// auto height/width modes get handled here
|
2016-01-09 10:22:09 -05:00
|
|
|
textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2016-01-20 14:57:26 -05:00
|
|
|
tb->blink_timeout = 0;
|
2016-01-22 12:18:26 -05:00
|
|
|
tb->blink = 1;
|
2016-01-20 14:57:26 -05:00
|
|
|
if ( ( flags & TB_EDITABLE ) == TB_EDITABLE ) {
|
2016-02-06 07:06:58 -05:00
|
|
|
tb->blink_timeout = g_timeout_add ( 1200, textbox_blink, tb );
|
2016-01-20 14:57:26 -05:00
|
|
|
}
|
|
|
|
|
2014-01-10 04:35:38 -05:00
|
|
|
return tb;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
2014-05-25 17:32:06 -04:00
|
|
|
void textbox_font ( textbox *tb, TextBoxFontType tbft )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-10-26 09:22:58 -04:00
|
|
|
TextBoxFontType t = tbft & STATE_MASK;
|
|
|
|
// ACTIVE has priority over URGENT if both set.
|
|
|
|
if ( t == ( URGENT | ACTIVE ) ) {
|
|
|
|
t = ACTIVE;
|
|
|
|
}
|
|
|
|
RowColor *color = &( colors[t] );
|
2015-04-06 11:13:26 -04:00
|
|
|
switch ( ( tbft & FMOD_MASK ) )
|
2014-03-22 16:04:19 -04:00
|
|
|
{
|
2014-05-25 17:32:06 -04:00
|
|
|
case HIGHLIGHT:
|
2015-04-06 11:13:26 -04:00
|
|
|
tb->color_bg = color->hlbg;
|
|
|
|
tb->color_fg = color->hlfg;
|
2014-05-25 17:32:06 -04:00
|
|
|
break;
|
2015-01-12 13:14:46 -05:00
|
|
|
case ALT:
|
2015-04-06 11:13:26 -04:00
|
|
|
tb->color_bg = color->bgalt;
|
|
|
|
tb->color_fg = color->fg;
|
2015-01-12 13:14:46 -05:00
|
|
|
break;
|
2014-05-25 17:32:06 -04:00
|
|
|
default:
|
2015-04-06 11:13:26 -04:00
|
|
|
tb->color_bg = color->bg;
|
|
|
|
tb->color_fg = color->fg;
|
2014-05-25 17:32:06 -04:00
|
|
|
break;
|
2014-01-13 15:35:43 -05:00
|
|
|
}
|
2015-12-04 17:57:08 -05:00
|
|
|
if ( ( tbft & SELECTED ) == SELECTED ) {
|
|
|
|
tb->color_bg = color->hlbg;
|
|
|
|
tb->color_fg = color->hlfg;
|
|
|
|
}
|
2015-09-26 14:34:34 -04:00
|
|
|
if ( tb->tbft != tbft ) {
|
|
|
|
tb->update = TRUE;
|
|
|
|
}
|
2015-03-31 16:45:02 -04:00
|
|
|
tb->tbft = tbft;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:36:20 -05: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 )
|
|
|
|
{
|
|
|
|
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_attributes ( tb->layout, NULL );
|
|
|
|
pango_layout_set_text ( tb->layout, string, l );
|
|
|
|
}
|
2016-02-09 15:25:29 -05:00
|
|
|
else if ( tb->flags & TB_MARKUP || tb->tbft & MARKUP ) {
|
2016-01-11 15:25:38 -05:00
|
|
|
pango_layout_set_markup ( tb->layout, tb->text, -1 );
|
2016-01-11 02:36:20 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pango_layout_set_attributes ( tb->layout, NULL );
|
2016-01-14 11:42:07 -05:00
|
|
|
pango_layout_set_text ( tb->layout, tb->text, -1 );
|
2016-01-11 02:36:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 21:28:39 -04:00
|
|
|
// set the default text to display
|
2015-02-03 02:21:59 -05:00
|
|
|
void textbox_text ( textbox *tb, const char *text )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->update = TRUE;
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( tb->text );
|
2015-02-02 10:44:08 -05:00
|
|
|
const gchar *last_pointer = NULL;
|
2015-09-26 14:34:34 -04:00
|
|
|
|
2015-02-02 10:44:08 -05:00
|
|
|
if ( g_utf8_validate ( text, -1, &last_pointer ) ) {
|
2014-08-09 05:40:42 -04:00
|
|
|
tb->text = g_strdup ( text );
|
2014-08-07 15:42:16 -04:00
|
|
|
}
|
|
|
|
else {
|
2015-02-02 10:44:08 -05: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 15:42:16 -04:00
|
|
|
}
|
2016-01-11 02:36:20 -05:00
|
|
|
__textbox_update_pango_text ( tb );
|
2015-05-23 14:06:06 -04:00
|
|
|
if ( tb->flags & TB_AUTOWIDTH ) {
|
2016-01-09 10:22:09 -05:00
|
|
|
textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
|
2015-05-23 14:06:06 -04:00
|
|
|
}
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
tb->cursor = MAX ( 0, MIN ( ( int ) strlen ( text ), tb->cursor ) );
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
2015-03-05 14:26:52 -05:00
|
|
|
// within the parent handled auto width/height modes
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( tb->flags & TB_AUTOWIDTH ) {
|
2015-03-23 03:56:55 -04:00
|
|
|
pango_layout_set_width ( tb->layout, -1 );
|
2015-09-26 14:34:34 -04:00
|
|
|
w = textbox_get_width ( tb );
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2014-08-02 14:02:37 -04:00
|
|
|
else {
|
|
|
|
// set ellipsize
|
2014-08-02 17:08:14 -04:00
|
|
|
if ( ( tb->flags & TB_EDITABLE ) == TB_EDITABLE ) {
|
2014-08-02 14:25:43 -04:00
|
|
|
pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_MIDDLE );
|
2014-08-02 17:08:14 -04:00
|
|
|
}
|
2015-09-23 14:44:24 -04:00
|
|
|
else if ( ( tb->flags & TB_WRAP ) != TB_WRAP ) {
|
2014-08-02 14:25:43 -04:00
|
|
|
pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_END );
|
|
|
|
}
|
2014-08-02 14:02:37 -04:00
|
|
|
}
|
2014-01-10 04:35:38 -05:00
|
|
|
|
2015-06-03 03:04:29 -04:00
|
|
|
if ( tb->flags & TB_AUTOHEIGHT ) {
|
|
|
|
// Width determines height!
|
|
|
|
int tw = MAX ( 1, w );
|
|
|
|
pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tw - 2 * SIDE_MARGIN ) );
|
|
|
|
h = textbox_get_height ( tb );
|
|
|
|
}
|
|
|
|
|
2016-01-09 10:22:09 -05: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 04:35:38 -05:00
|
|
|
}
|
2015-09-26 14:34:34 -04:00
|
|
|
|
2015-06-03 03:04:29 -04:00
|
|
|
// We always want to update this
|
2016-01-09 10:22:09 -05:00
|
|
|
pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->widget.w - 2 * SIDE_MARGIN ) );
|
2015-10-16 14:51:37 -04:00
|
|
|
tb->update = TRUE;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// will also unmap the window if still displayed
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_free ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-06-06 13:31:33 -04:00
|
|
|
if ( tb == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-20 14:57:26 -05:00
|
|
|
if ( tb->blink_timeout > 0 ) {
|
|
|
|
g_source_remove ( tb->blink_timeout );
|
|
|
|
tb->blink_timeout = 0;
|
|
|
|
}
|
2014-01-10 04:35:38 -05:00
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( tb->text );
|
|
|
|
|
2014-08-07 15:42:16 -04:00
|
|
|
if ( tb->layout != NULL ) {
|
2014-08-02 14:02:37 -04:00
|
|
|
g_object_unref ( tb->layout );
|
|
|
|
}
|
2015-09-26 14:34:34 -04:00
|
|
|
if ( tb->main_draw ) {
|
|
|
|
cairo_destroy ( tb->main_draw );
|
|
|
|
tb->main_draw = NULL;
|
|
|
|
}
|
|
|
|
if ( tb->main_surface ) {
|
|
|
|
cairo_surface_destroy ( tb->main_surface );
|
|
|
|
tb->main_surface = NULL;
|
|
|
|
}
|
2014-01-10 04:35:38 -05:00
|
|
|
|
2016-02-19 13:29:06 -05:00
|
|
|
g_slice_free ( textbox, tb );
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
static void texbox_update ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-09-26 14:34:34 -04:00
|
|
|
if ( tb->update ) {
|
|
|
|
if ( tb->main_surface ) {
|
|
|
|
cairo_destroy ( tb->main_draw );
|
|
|
|
cairo_surface_destroy ( tb->main_surface );
|
|
|
|
tb->main_draw = NULL;
|
|
|
|
tb->main_surface = NULL;
|
2015-05-23 14:06:06 -04:00
|
|
|
}
|
2016-02-21 13:18:58 -05:00
|
|
|
tb->main_surface = cairo_image_surface_create ( CAIRO_FORMAT_ARGB32, tb->widget.w, tb->widget.h );
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->main_draw = cairo_create ( tb->main_surface );
|
|
|
|
cairo_set_operator ( tb->main_draw, CAIRO_OPERATOR_SOURCE );
|
|
|
|
|
|
|
|
pango_cairo_update_layout ( tb->main_draw, tb->layout );
|
|
|
|
char *text = tb->text ? tb->text : "";
|
|
|
|
int text_len = strlen ( text );
|
|
|
|
int font_height = textbox_get_font_height ( tb );
|
|
|
|
|
|
|
|
int cursor_x = 0;
|
|
|
|
int cursor_width = MAX ( 2, font_height / 10 );
|
|
|
|
|
|
|
|
if ( tb->changed ) {
|
2016-01-11 02:36:20 -05:00
|
|
|
__textbox_update_pango_text ( tb );
|
2015-05-23 14:06:06 -04:00
|
|
|
}
|
2014-08-02 14:02:37 -04:00
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
if ( tb->flags & TB_EDITABLE ) {
|
|
|
|
PangoRectangle pos;
|
|
|
|
int cursor_offset = 0;
|
|
|
|
cursor_offset = MIN ( tb->cursor, text_len );
|
|
|
|
pango_layout_get_cursor_pos ( tb->layout, cursor_offset, &pos, NULL );
|
|
|
|
// Add a small 4px offset between cursor and last glyph.
|
|
|
|
cursor_x = pos.x / PANGO_SCALE;
|
|
|
|
}
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
// Skip the side MARGIN on the X axis.
|
|
|
|
int x = SIDE_MARGIN;
|
|
|
|
int y = 0;
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
if ( tb->flags & TB_RIGHT ) {
|
|
|
|
int line_width = 0;
|
|
|
|
// Get actual width.
|
|
|
|
pango_layout_get_pixel_size ( tb->layout, &line_width, NULL );
|
2016-01-09 10:22:09 -05:00
|
|
|
x = ( tb->widget.w - line_width - SIDE_MARGIN );
|
2015-09-26 14:34:34 -04:00
|
|
|
}
|
|
|
|
else if ( tb->flags & TB_CENTER ) {
|
|
|
|
int tw = textbox_get_font_width ( tb );
|
2016-01-09 10:22:09 -05:00
|
|
|
x = ( ( tb->widget.w - tw - 2 * SIDE_MARGIN ) ) / 2;
|
2015-09-26 14:34:34 -04:00
|
|
|
}
|
2015-10-05 12:22:01 -04:00
|
|
|
short fh = textbox_get_font_height ( tb );
|
2016-01-09 10:22:09 -05:00
|
|
|
if ( fh > tb->widget.h ) {
|
2015-10-10 07:03:11 -04:00
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
else {
|
2016-01-09 10:22:09 -05:00
|
|
|
y = ( ( tb->widget.h - fh ) ) / 2;
|
2015-10-05 12:22:01 -04:00
|
|
|
}
|
2015-09-26 14:34:34 -04:00
|
|
|
|
|
|
|
// Set ARGB
|
2015-12-04 17:57:08 -05:00
|
|
|
Color col = tb->color_bg;
|
|
|
|
double scale = 1.0;
|
|
|
|
if ( ( tb->tbft & SELECTED ) == SELECTED && ( tb->tbft & FMOD_MASK ) != HIGHLIGHT ) {
|
|
|
|
scale = 0.4;
|
|
|
|
}
|
|
|
|
cairo_set_source_rgba ( tb->main_draw, col.red, col.green, col.blue, col.alpha * scale );
|
2015-09-26 14:34:34 -04:00
|
|
|
cairo_paint ( tb->main_draw );
|
|
|
|
|
|
|
|
col = tb->color_fg;
|
2015-11-30 08:05:39 -05:00
|
|
|
cairo_set_source_rgba ( tb->main_draw, col.red, col.green, col.blue, col.alpha * scale );
|
2015-09-26 14:34:34 -04:00
|
|
|
// draw the cursor
|
2016-01-20 14:57:26 -05:00
|
|
|
if ( tb->flags & TB_EDITABLE && tb->blink ) {
|
2015-09-26 14:34:34 -04:00
|
|
|
cairo_rectangle ( tb->main_draw, x + cursor_x, y, cursor_width, font_height );
|
|
|
|
cairo_fill ( tb->main_draw );
|
|
|
|
}
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2015-12-28 05:24:25 -05:00
|
|
|
// Set ARGB
|
|
|
|
// We need to set over, otherwise subpixel hinting wont work.
|
|
|
|
cairo_set_operator ( tb->main_draw, CAIRO_OPERATOR_OVER );
|
|
|
|
cairo_move_to ( tb->main_draw, x, y );
|
|
|
|
pango_cairo_show_layout ( tb->main_draw, tb->layout );
|
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->update = FALSE;
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2015-09-26 14:34:34 -04:00
|
|
|
}
|
|
|
|
void textbox_draw ( textbox *tb, cairo_t *draw )
|
|
|
|
{
|
|
|
|
texbox_update ( tb );
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2015-09-26 14:34:34 -04:00
|
|
|
/* Write buffer */
|
2012-08-23 21:28:39 -04:00
|
|
|
|
2016-01-09 10:22:09 -05:00
|
|
|
cairo_set_source_surface ( draw, tb->main_surface, tb->widget.x, tb->widget.y );
|
|
|
|
cairo_rectangle ( draw, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
|
2015-09-26 14:34:34 -04:00
|
|
|
cairo_fill ( draw );
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cursor handling for edit mode
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_cursor ( textbox *tb, int pos )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-02-15 15:15:16 -05:00
|
|
|
int length = ( tb->text == NULL ) ? 0 : strlen ( tb->text );
|
2015-02-13 09:37:55 -05:00
|
|
|
tb->cursor = MAX ( 0, MIN ( length, pos ) );
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->update = TRUE;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// move right
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_cursor_inc ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-08-07 15:42:16 -04:00
|
|
|
int index = g_utf8_next_char ( &( tb->text[tb->cursor] ) ) - tb->text;
|
|
|
|
textbox_cursor ( tb, index );
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// move left
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_cursor_dec ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-08-07 15:42:16 -04:00
|
|
|
int index = g_utf8_prev_char ( &( tb->text[tb->cursor] ) ) - tb->text;
|
|
|
|
textbox_cursor ( tb, index );
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
2015-02-07 10:42:42 -05:00
|
|
|
// Move word right
|
2015-02-18 02:53:38 -05:00
|
|
|
static void textbox_cursor_inc_word ( textbox *tb )
|
2015-02-07 10:42:42 -05:00
|
|
|
{
|
2015-02-15 15:15:16 -05:00
|
|
|
if ( tb->text == NULL ) {
|
2015-02-13 09:37:55 -05:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 10:42:42 -05:00
|
|
|
// Find word boundaries, with pango_Break?
|
|
|
|
gchar *c = &( tb->text[tb->cursor] );
|
|
|
|
while ( ( c = g_utf8_next_char ( c ) ) ) {
|
|
|
|
gunichar uc = g_utf8_get_char ( c );
|
|
|
|
GUnicodeBreakType bt = g_unichar_break_type ( uc );
|
2015-09-19 14:59:50 -04: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 10:42:42 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-15 15:15:16 -05:00
|
|
|
if ( c == NULL ) {
|
2015-02-13 09:37:55 -05:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 10:42:42 -05: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 14:59:50 -04: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 10:42:42 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int index = c - tb->text;
|
|
|
|
textbox_cursor ( tb, index );
|
|
|
|
}
|
|
|
|
// move word left
|
2015-02-18 02:53:38 -05:00
|
|
|
static void textbox_cursor_dec_word ( textbox *tb )
|
2015-02-07 10:42:42 -05:00
|
|
|
{
|
|
|
|
// Find word boundaries, with pango_Break?
|
|
|
|
gchar *n;
|
|
|
|
gchar *c = &( tb->text[tb->cursor] );
|
|
|
|
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 14:59:50 -04: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 10:42:42 -05: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 14:59:50 -04: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 10:42:42 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = n;
|
|
|
|
if ( n == tb->text ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int index = c - tb->text;
|
|
|
|
textbox_cursor ( tb, index );
|
|
|
|
}
|
|
|
|
|
2012-08-23 21:28:39 -04:00
|
|
|
// end of line
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_cursor_end ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-12-23 11:59:03 -05:00
|
|
|
if ( tb->text == NULL ) {
|
|
|
|
tb->cursor = 0;
|
|
|
|
tb->update = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 16:04:19 -04:00
|
|
|
tb->cursor = ( int ) strlen ( tb->text );
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->update = TRUE;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert text
|
2015-11-22 14:41:45 -05:00
|
|
|
void textbox_insert ( textbox *tb, int pos, char *str, int slen )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-11-22 14:41:45 -05:00
|
|
|
int len = ( int ) strlen ( tb->text );
|
2014-03-22 16:04:19 -04:00
|
|
|
pos = MAX ( 0, MIN ( len, pos ) );
|
2014-01-10 04:35:38 -05:00
|
|
|
// expand buffer
|
2014-08-09 05:40:42 -04:00
|
|
|
tb->text = g_realloc ( tb->text, len + slen + 1 );
|
2014-01-10 04:35:38 -05:00
|
|
|
// move everything after cursor upward
|
|
|
|
char *at = tb->text + pos;
|
2014-03-22 16:04:19 -04:00
|
|
|
memmove ( at + slen, at, len - pos + 1 );
|
2014-01-10 04:35:38 -05:00
|
|
|
// insert new str
|
2014-03-22 16:04:19 -04:00
|
|
|
memmove ( at, str, slen );
|
2015-05-23 14:06:06 -04:00
|
|
|
|
|
|
|
// Set modified, lay out need te be redrawn
|
|
|
|
tb->changed = TRUE;
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->update = TRUE;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove text
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_delete ( textbox *tb, int pos, int dlen )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-03-22 16:04:19 -04:00
|
|
|
int len = strlen ( tb->text );
|
|
|
|
pos = MAX ( 0, MIN ( len, pos ) );
|
2014-01-10 04:35:38 -05:00
|
|
|
// move everything after pos+dlen down
|
|
|
|
char *at = tb->text + pos;
|
2014-08-30 14:45:08 -04:00
|
|
|
// Move remainder + closing \0
|
2014-09-03 07:07:26 -04:00
|
|
|
memmove ( at, at + dlen, len - pos - dlen + 1 );
|
|
|
|
if ( tb->cursor >= pos && tb->cursor < ( pos + dlen ) ) {
|
|
|
|
tb->cursor = pos;
|
|
|
|
}
|
|
|
|
else if ( tb->cursor >= ( pos + dlen ) ) {
|
2014-08-30 14:45:08 -04:00
|
|
|
tb->cursor -= dlen;
|
|
|
|
}
|
2015-05-23 14:06:06 -04:00
|
|
|
// Set modified, lay out need te be redrawn
|
|
|
|
tb->changed = TRUE;
|
2015-09-26 14:34:34 -04:00
|
|
|
tb->update = TRUE;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete on character
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_cursor_del ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2015-02-15 15:15:16 -05:00
|
|
|
if ( tb->text == NULL ) {
|
2015-02-13 09:37:55 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-08-07 15:42:16 -04:00
|
|
|
int index = g_utf8_next_char ( &( tb->text[tb->cursor] ) ) - tb->text;
|
|
|
|
textbox_delete ( tb, tb->cursor, index - tb->cursor );
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// back up and delete one character
|
2014-03-22 16:04:19 -04:00
|
|
|
void textbox_cursor_bkspc ( textbox *tb )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( tb->cursor > 0 ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
textbox_cursor_dec ( tb );
|
|
|
|
textbox_cursor_del ( tb );
|
2014-01-10 04:35:38 -05:00
|
|
|
}
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
2015-02-18 02:53:38 -05:00
|
|
|
static void textbox_cursor_bkspc_word ( textbox *tb )
|
2015-02-07 10:42:42 -05:00
|
|
|
{
|
|
|
|
if ( tb->cursor > 0 ) {
|
|
|
|
int cursor = tb->cursor;
|
|
|
|
textbox_cursor_dec_word ( tb );
|
|
|
|
if ( cursor > tb->cursor ) {
|
|
|
|
textbox_delete ( tb, tb->cursor, cursor - tb->cursor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 02:53:38 -05:00
|
|
|
static void textbox_cursor_del_word ( textbox *tb )
|
2015-02-07 10:42:42 -05:00
|
|
|
{
|
|
|
|
if ( tb->cursor >= 0 ) {
|
|
|
|
int cursor = tb->cursor;
|
|
|
|
textbox_cursor_inc_word ( tb );
|
|
|
|
if ( cursor < tb->cursor ) {
|
|
|
|
textbox_delete ( tb, cursor, tb->cursor - cursor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 21:28:39 -04:00
|
|
|
|
|
|
|
// handle a keypress in edit mode
|
2015-08-29 17:02:30 -04:00
|
|
|
// 2 = nav
|
2012-08-23 21:28:39 -04:00
|
|
|
// 0 = unhandled
|
|
|
|
// 1 = handled
|
|
|
|
// -1 = handled and return pressed (finished)
|
2016-04-07 09:32:22 -04:00
|
|
|
int textbox_keybinding ( textbox *tb, KeyBindingAction action )
|
2012-08-23 21:28:39 -04:00
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( !( tb->flags & TB_EDITABLE ) ) {
|
2016-05-08 05:13:11 -04:00
|
|
|
g_return_val_if_reached ( 0 );
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2016-04-07 09:01:14 -04:00
|
|
|
|
2016-04-07 09:32:22 -04:00
|
|
|
switch ( action )
|
|
|
|
{
|
2016-04-07 09:07:02 -04:00
|
|
|
// Left or Ctrl-b
|
2016-04-07 09:32:22 -04:00
|
|
|
case MOVE_CHAR_BACK:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_dec ( tb );
|
|
|
|
return 2;
|
|
|
|
// Right or Ctrl-F
|
2016-04-07 09:32:22 -04:00
|
|
|
case MOVE_CHAR_FORWARD:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_inc ( tb );
|
|
|
|
return 2;
|
|
|
|
// Ctrl-U: Kill from the beginning to the end of the line.
|
2016-04-07 09:32:22 -04:00
|
|
|
case CLEAR_LINE:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_text ( tb, "" );
|
|
|
|
return 1;
|
|
|
|
// Ctrl-A
|
2016-04-07 09:32:22 -04:00
|
|
|
case MOVE_FRONT:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor ( tb, 0 );
|
|
|
|
return 2;
|
|
|
|
// Ctrl-E
|
2016-04-07 09:32:22 -04:00
|
|
|
case MOVE_END:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_end ( tb );
|
|
|
|
return 2;
|
|
|
|
// Ctrl-Alt-h
|
2016-04-07 09:32:22 -04:00
|
|
|
case REMOVE_WORD_BACK:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_bkspc_word ( tb );
|
|
|
|
return 1;
|
|
|
|
// Ctrl-Alt-d
|
2016-04-07 09:32:22 -04:00
|
|
|
case REMOVE_WORD_FORWARD:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_del_word ( tb );
|
|
|
|
return 1;
|
2016-04-07 09:32:22 -04:00
|
|
|
// Delete or Ctrl-D
|
|
|
|
case REMOVE_CHAR_FORWARD:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_del ( tb );
|
|
|
|
return 1;
|
|
|
|
// Alt-B
|
2016-04-07 09:32:22 -04:00
|
|
|
case MOVE_WORD_BACK:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_dec_word ( tb );
|
|
|
|
return 2;
|
|
|
|
// Alt-F
|
2016-04-07 09:32:22 -04:00
|
|
|
case MOVE_WORD_FORWARD:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_inc_word ( tb );
|
|
|
|
return 2;
|
|
|
|
// BackSpace, Ctrl-h
|
2016-04-07 09:32:22 -04:00
|
|
|
case REMOVE_CHAR_BACK:
|
2016-04-07 09:07:02 -04:00
|
|
|
textbox_cursor_bkspc ( tb );
|
|
|
|
return 1;
|
2016-04-07 09:32:22 -04:00
|
|
|
case ACCEPT_CUSTOM:
|
2016-04-07 09:07:02 -04:00
|
|
|
return -2;
|
2016-04-07 09:32:22 -04:00
|
|
|
case ACCEPT_ENTRY:
|
2016-04-07 09:07:02 -04:00
|
|
|
return -1;
|
2016-04-07 09:32:22 -04:00
|
|
|
default:
|
2016-05-08 05:13:11 -04:00
|
|
|
g_return_val_if_reached ( 0 );
|
2016-04-07 09:07:02 -04:00
|
|
|
}
|
2016-04-07 09:01:14 -04:00
|
|
|
|
2016-05-08 05:13:11 -04:00
|
|
|
g_return_val_if_reached ( 0 );
|
2016-04-07 09:01:14 -04:00
|
|
|
}
|
|
|
|
|
2016-04-07 09:11:42 -04:00
|
|
|
gboolean textbox_append ( textbox *tb, char *pad, int pad_len )
|
2016-04-07 09:01:14 -04:00
|
|
|
{
|
|
|
|
if ( !( tb->flags & TB_EDITABLE ) ) {
|
2016-04-07 09:11:42 -04:00
|
|
|
return FALSE;
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2016-04-07 09:01:14 -04:00
|
|
|
int old_blink = tb->blink;
|
|
|
|
tb->blink = 2;
|
2016-04-07 09:11:42 -04:00
|
|
|
|
|
|
|
// Filter When alt/ctrl is pressed do not accept the character.
|
|
|
|
if ( !g_ascii_iscntrl ( *pad ) ) {
|
|
|
|
textbox_insert ( tb, tb->cursor, pad, pad_len );
|
|
|
|
textbox_cursor ( tb, tb->cursor + pad_len );
|
|
|
|
return TRUE;
|
2014-01-10 04:35:38 -05:00
|
|
|
}
|
2016-04-07 09:11:42 -04:00
|
|
|
|
2016-01-20 14:57:26 -05:00
|
|
|
tb->blink = old_blink;
|
2016-04-07 09:11:42 -04:00
|
|
|
return FALSE;
|
2012-08-23 21:28:39 -04:00
|
|
|
}
|
2014-05-25 17:32:06 -04:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Font setup.
|
|
|
|
*/
|
2016-02-21 13:42:32 -05:00
|
|
|
static void textbox_parse_string ( const char *str, RowColor *color )
|
2015-04-06 11:13:26 -04:00
|
|
|
{
|
|
|
|
if ( str == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-20 05:16:55 -04:00
|
|
|
char *cstr = g_strdup ( str );
|
|
|
|
char *endp = NULL;
|
|
|
|
char *token;
|
|
|
|
int index = 0;
|
|
|
|
const char *const sep = ",";
|
2016-03-19 08:29:04 -04:00
|
|
|
for ( token = strtok_r ( cstr, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
|
2015-04-06 11:13:26 -04:00
|
|
|
switch ( index )
|
|
|
|
{
|
|
|
|
case 0:
|
2016-02-21 13:42:32 -05:00
|
|
|
color->bg = color_get ( g_strstrip ( token ) );
|
2015-04-06 11:13:26 -04:00
|
|
|
break;
|
|
|
|
case 1:
|
2016-02-21 13:42:32 -05:00
|
|
|
color->fg = color_get ( g_strstrip ( token ) );
|
2015-04-06 11:13:26 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
2016-02-21 13:42:32 -05:00
|
|
|
color->bgalt = color_get ( g_strstrip ( token ) );
|
2015-04-06 11:13:26 -04:00
|
|
|
break;
|
|
|
|
case 3:
|
2016-02-21 13:42:32 -05:00
|
|
|
color->hlbg = color_get ( g_strstrip ( token ) );
|
2015-04-06 11:13:26 -04:00
|
|
|
break;
|
|
|
|
case 4:
|
2016-02-21 13:42:32 -05:00
|
|
|
color->hlfg = color_get ( g_strstrip ( token ) );
|
2015-04-06 11:13:26 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
g_free ( cstr );
|
|
|
|
}
|
2016-02-21 13:42:32 -05:00
|
|
|
void textbox_setup ( void )
|
2014-05-25 17:32:06 -04:00
|
|
|
{
|
2016-03-05 12:28:39 -05:00
|
|
|
textbox_parse_string ( config.color_normal, &( colors[NORMAL] ) );
|
|
|
|
textbox_parse_string ( config.color_urgent, &( colors[URGENT] ) );
|
|
|
|
textbox_parse_string ( config.color_active, &( colors[ACTIVE] ) );
|
2015-12-28 05:24:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void textbox_set_pango_context ( PangoContext *p )
|
|
|
|
{
|
|
|
|
textbox_cleanup ();
|
|
|
|
p_context = g_object_ref ( p );
|
2015-04-06 11:13:26 -04:00
|
|
|
}
|
2014-05-25 17:32:06 -04:00
|
|
|
|
2015-02-18 02:53:38 -05:00
|
|
|
void textbox_cleanup ( void )
|
2014-05-25 17:32:06 -04:00
|
|
|
{
|
2014-08-02 14:02:37 -04:00
|
|
|
if ( p_context ) {
|
|
|
|
g_object_unref ( p_context );
|
2015-09-26 14:34:34 -04:00
|
|
|
p_context = NULL;
|
2014-05-25 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-02 14:02:37 -04:00
|
|
|
|
|
|
|
int textbox_get_width ( textbox *tb )
|
|
|
|
{
|
|
|
|
return textbox_get_font_width ( tb ) + 2 * SIDE_MARGIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
int textbox_get_height ( textbox *tb )
|
|
|
|
{
|
2014-08-02 15:09:20 -04:00
|
|
|
return textbox_get_font_height ( tb ) + 2 * SIDE_MARGIN;
|
2014-08-02 14:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int textbox_get_font_height ( textbox *tb )
|
|
|
|
{
|
|
|
|
int height;
|
|
|
|
pango_layout_get_pixel_size ( tb->layout, NULL, &height );
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
int textbox_get_font_width ( textbox *tb )
|
|
|
|
{
|
|
|
|
int width;
|
|
|
|
pango_layout_get_pixel_size ( tb->layout, &width, NULL );
|
|
|
|
return width;
|
|
|
|
}
|
2014-08-11 14:21:29 -04:00
|
|
|
|
2015-02-18 02:53:38 -05:00
|
|
|
double textbox_get_estimated_char_width ( void )
|
2014-08-11 14:21:29 -04:00
|
|
|
{
|
2014-08-26 14:25:00 -04:00
|
|
|
// Get width
|
2015-12-28 05:24:25 -05:00
|
|
|
PangoFontMetrics *metric = pango_context_get_metrics ( p_context, NULL, NULL );
|
|
|
|
int width = pango_font_metrics_get_approximate_char_width ( metric );
|
2014-08-11 14:21:29 -04:00
|
|
|
pango_font_metrics_unref ( metric );
|
|
|
|
return ( width ) / (double) PANGO_SCALE;
|
|
|
|
}
|
2015-03-19 14:58:05 -04:00
|
|
|
|
|
|
|
int textbox_get_estimated_char_height ( void )
|
|
|
|
{
|
|
|
|
// Get width
|
2015-12-28 05:24:25 -05:00
|
|
|
PangoFontMetrics *metric = pango_context_get_metrics ( p_context, NULL, NULL );
|
2015-09-19 14:59:50 -04:00
|
|
|
int height = pango_font_metrics_get_ascent ( metric ) + pango_font_metrics_get_descent ( metric );
|
2015-03-19 14:58:05 -04:00
|
|
|
pango_font_metrics_unref ( metric );
|
|
|
|
return ( height ) / PANGO_SCALE + 2 * SIDE_MARGIN;
|
|
|
|
}
|