/*
* rofi
*
* MIT/X11 License
* Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
* Copyright © 2013-2023 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 "helper-theme.h"
#include "helper.h"
#include "keyb.h"
#include "mode.h"
#include "view.h"
#include "widgets/textbox.h"
#include <ctype.h>
#include <glib.h>
#include <math.h>
#include <string.h>
#include <xcb/xcb.h>
#include "theme.h"
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;
/** Default tbfc */
static TBFontConfig *tbfc_default = NULL;
/** 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, const int width) {
textbox *tb = (textbox *)wid;
if ((tb->flags & TB_AUTOHEIGHT) == 0) {
return tb->widget.h;
}
if (tb->changed) {
__textbox_update_pango_text(tb);
}
int old_width = pango_layout_get_width(tb->layout);
pango_layout_set_width(
tb->layout,
PANGO_SCALE * (width - widget_padding_get_padding_width(WIDGET(tb))));
int height =
textbox_get_estimated_height(tb, pango_layout_get_line_count(tb->layout));
pango_layout_set_width(tb->layout, old_width);
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: