/*
* rofi
*
* MIT/X11 License
* Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
* Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
*
* 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.
*
*/
#include <config.h>
#include <xcb/xcb.h>
#include <ctype.h>
#include <string.h>
#include <glib.h>
#include <math.h>
#include "widgets/textbox.h"
#include "keyb.h"
#include "helper.h"
#include "helper-theme.h"
#include "mode.h"
#include "view.h"
#include "theme.h"
/** The space reserved for the DOT when enabling multi-select. */
#define DOT_OFFSET 15
static void textbox_draw ( widget *, cairo_t * );
static void textbox_free ( widget * );
static int textbox_get_width ( widget * );
static int _textbox_get_height ( widget * );
static void __textbox_update_pango_text ( textbox *tb );
/** Default pango context */
static PangoContext *p_context = NULL;
/** The pango font metrics */
static PangoFontMetrics *p_metrics = NULL;
/** Cache to hold font descriptions. This it to avoid having to lookup each time. */
typedef struct TBFontConfig
{
/** Font description */
PangoFontDescription *pfd;
/** Font metrics */
PangoFontMetrics *metrics;
/** height */
double height;
}TBFontConfig;
/** HashMap of previously parsed font descriptions. */
static GHashTable *tbfc_cache = NULL;
static gboolean textbox_blink ( gpointer data )
{
textbox *tb = (textbox *) data;
if ( tb->blink < 2 ) {
tb->blink = !tb->blink;
widget_queue_redraw ( WIDGET ( tb ) );
rofi_view_queue_redraw ( );
}
else {
tb->blink--;
}
return TRUE;
}
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 );
}
static int textbox_get_desired_height ( widget *wid )
{
textbox *tb = (textbox *) wid;
if ( ( tb->flags & TB_AUTOHEIGHT ) == 0 ) {
return tb->widget.h;
}
if ( tb->changed ) {
__textbox_update_pango_text ( tb );
}
int height = textbox_get_height ( tb );
return height;
}
static WidgetTriggerActionResult textbox_editable_trigger_action ( widget *wid, MouseBindingMouseDefaultAction action, gint x, gint y, G_GNUC_UNUSED void *user_data )
{
textbox *tb = (textbox *) wid;
switch ( action )
{
case MOUSE_CLICK_DOWN:
{
gint i;
// subtract padding on left.
x -= widget_padding_get_left ( wid );
gint max = textbox_get_font_width ( tb );
// Right of text, move to end.
if ( x >= max ) {
textbox_cursor_end ( tb );
}
else if ( x > 0 ) {
// If in range, get index.
pango_layout_xy_to_index ( tb->layout, x * PANGO_SCALE, y * PANGO_SCALE, &i, NULL );
textbox_cursor ( tb, i );
}
return WIDGET_TRIGGER_ACTION_RESULT_HANDLED;
}
case MOUSE_CLICK_UP:
case MOUSE_DCLICK_DOWN:
case MOUSE_DCLICK_UP:
break;
}
return WIDGET_TRIGGER_ACTION_RESULT_IGNORED;
}
static void textbox_initialize_font ( textbox *tb )
{
tb->metrics = p_metrics;
const char