rofi/source/textbox.c

482 lines
13 KiB
C
Raw Normal View History

2012-11-07 04:16:59 +00:00
/*
2012-08-24 01:28:39 +00:00
2014-03-22 20:04:19 +00:00
MIT/X11 License
Copyright (c) 2012 Sean Pringle <sean.pringle@gmail.com>
Modified (c) 2013-2014 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.
*/
2012-08-24 01:28:39 +00:00
#define _GNU_SOURCE
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include <ctype.h>
2014-08-02 18:02:37 +00:00
2014-03-12 07:41:38 +00:00
#include "rofi.h"
#include "textbox.h"
#include <glib.h>
#define SIDE_MARGIN 2
2012-08-24 01:28:39 +00:00
2014-08-02 18:02:37 +00:00
extern Display *display;
2012-08-24 01:28:39 +00:00
2014-05-26 07:00:14 +00:00
/**
* Font + font color cache.
* Avoid re-loading font on every change on every textbox.
*/
2014-08-02 18:02:37 +00:00
XftColor color_fg;
XftColor color_bg;
XftColor color_hlfg;
XftColor color_hlbg;
PangoContext *p_context = NULL;
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
// Xft text box, optionally editable
2014-03-22 20:04:19 +00:00
textbox* textbox_create ( Window parent,
TextboxFlags flags,
short x, short y, short w, short h,
TextBoxFontType tbft,
char *text )
2012-08-24 01:28:39 +00:00
{
2014-03-22 20:04:19 +00:00
textbox *tb = calloc ( 1, sizeof ( textbox ) );
2012-08-24 01:28:39 +00:00
2014-03-22 20:04:19 +00:00
tb->flags = flags;
2014-01-10 09:35:38 +00:00
tb->parent = parent;
2012-08-24 01:28:39 +00:00
2014-01-10 09:35:38 +00:00
tb->x = x;
tb->y = y;
2014-03-22 20:04:19 +00:00
tb->w = MAX ( 1, w );
tb->h = MAX ( 1, h );
2012-11-07 04:16:59 +00:00
2014-08-02 18:02:37 +00:00
tb->layout = pango_layout_new ( p_context );
PangoFontDescription *pfd = pango_font_description_from_string ( config.menu_font );
pango_layout_set_font_description ( tb->layout, pfd );
pango_font_description_free ( pfd );
2014-05-26 08:32:26 +00:00
unsigned int cp = ( tbft == NORMAL ) ? color_bg.pixel : color_hlbg.pixel;
2012-11-07 04:16:59 +00:00
2014-03-22 20:04:19 +00:00
tb->window = XCreateSimpleWindow ( display, tb->parent, tb->x, tb->y, tb->w, tb->h, 0, None, cp );
2012-08-24 01:28:39 +00:00
2014-01-10 09:35:38 +00:00
// need to preload the font to calc line height
textbox_font ( tb, tbft );
2012-08-24 01:28:39 +00:00
2014-03-22 20:04:19 +00:00
textbox_text ( tb, text ? text : "" );
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
2014-03-22 20:04:19 +00:00
textbox_moveresize ( tb, tb->x, tb->y, tb->w, tb->h );
2012-08-24 01:28:39 +00:00
2014-01-10 09:35:38 +00:00
// edit mode controls
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_EDITABLE ) {
2014-03-22 20:04:19 +00:00
tb->xim = XOpenIM ( display, NULL, NULL, NULL );
tb->xic = XCreateIC ( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL );
2014-01-10 09:35:38 +00:00
}
2012-08-24 01:28:39 +00:00
2014-01-10 09:35:38 +00:00
return tb;
2012-08-24 01:28:39 +00:00
}
// set an Xft font by name
void textbox_font ( textbox *tb, TextBoxFontType tbft )
2012-08-24 01:28:39 +00:00
{
switch ( tbft )
2014-03-22 20:04:19 +00:00
{
case HIGHLIGHT:
tb->color_bg = color_hlbg;
tb->color_fg = color_hlfg;
break;
case NORMAL:
default:
tb->color_bg = color_bg;
tb->color_fg = color_fg;
break;
}
2012-08-24 01:28:39 +00:00
}
// outer code may need line height, width, etc
2014-03-22 20:04:19 +00:00
void textbox_extents ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
}
// set the default text to display
2014-03-22 20:04:19 +00:00
void textbox_text ( textbox *tb, char *text )
2012-08-24 01:28:39 +00:00
{
2014-06-04 19:29:23 +00:00
if ( tb->text ) {
2014-03-22 20:04:19 +00:00
free ( tb->text );
}
if ( g_utf8_validate ( text, -1, NULL ) ) {
tb->text = strdup ( text );
}
else {
tb->text = strdup ( "Invalid UTF-8 string." );
}
2014-08-02 18:02:37 +00:00
pango_layout_set_text ( tb->layout, tb->text, strlen ( tb->text ) );
2014-03-22 20:04:19 +00:00
tb->cursor = MAX ( 0, MIN ( ( int ) strlen ( text ), tb->cursor ) );
textbox_extents ( tb );
2012-08-24 01:28:39 +00:00
}
2014-05-22 07:33:32 +00:00
void textbox_move ( textbox *tb, int x, int y )
{
2014-06-04 19:29:23 +00:00
if ( x != tb->x || y != tb->y ) {
tb->x = x;
tb->y = y;
XMoveResizeWindow ( display, tb->window, tb->x, tb->y, tb->w, tb->h );
}
}
2012-08-24 01:28:39 +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
{
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_AUTOHEIGHT ) {
2014-08-02 18:57:05 +00:00
h = textbox_get_height ( tb );
2014-03-22 20:04:19 +00:00
}
2014-01-10 09:35:38 +00:00
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_AUTOWIDTH ) {
if ( w > 1 ) {
2014-08-02 20:27:04 +00:00
w = MIN ( w, textbox_get_width ( tb ) );
}
2014-06-04 19:29:23 +00:00
else{
2014-08-02 20:27:04 +00:00
w = textbox_get_width ( tb );
}
2014-03-22 20:04:19 +00:00
}
2014-08-02 18:02:37 +00:00
else {
// set ellipsize
if ( ( tb->flags & TB_EDITABLE ) == TB_EDITABLE ) {
pango_layout_set_ellipsize ( tb->layout, PANGO_ELLIPSIZE_MIDDLE );
}
else{
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
2014-06-04 19:29:23 +00:00
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) {
2014-01-10 09:35:38 +00:00
tb->x = x;
tb->y = y;
2014-03-22 20:04:19 +00:00
tb->w = MAX ( 1, w );
tb->h = MAX ( 1, h );
XMoveResizeWindow ( display, tb->window, tb->x, tb->y, tb->w, tb->h );
2014-01-10 09:35:38 +00:00
}
2012-08-24 01:28:39 +00:00
}
2014-03-22 20:04:19 +00:00
void textbox_show ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
2014-03-22 20:04:19 +00:00
XMapWindow ( display, tb->window );
2012-08-24 01:28:39 +00:00
}
2014-05-14 18:56:38 +00:00
void textbox_hide ( textbox *tb )
{
XUnmapWindow ( display, tb->window );
}
2012-08-24 01:28:39 +00:00
// will also unmap the window if still displayed
2014-03-22 20:04:19 +00:00
void textbox_free ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
2014-06-06 17:31:33 +00:00
if ( tb == NULL ) {
return;
}
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_EDITABLE ) {
2014-03-22 20:04:19 +00:00
XDestroyIC ( tb->xic );
XCloseIM ( tb->xim );
2014-01-10 09:35:38 +00:00
}
2014-06-04 19:29:23 +00:00
if ( tb->text ) {
2014-03-22 20:04:19 +00:00
free ( tb->text );
}
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
2014-03-22 20:04:19 +00:00
XDestroyWindow ( display, tb->window );
free ( tb );
2012-08-24 01:28:39 +00:00
}
2014-03-22 20:04:19 +00:00
void textbox_draw ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
2014-08-02 18:02:37 +00:00
GC context = XCreateGC ( display, tb->window, 0, 0 );
Pixmap canvas = XCreatePixmap ( display, tb->window, tb->w, tb->h, DefaultDepth ( display, DefaultScreen ( display ) ) );
XftDraw *draw = XftDrawCreate ( display, canvas, DefaultVisual ( display, DefaultScreen ( display ) ), DefaultColormap ( display, DefaultScreen ( display ) ) );
2014-01-10 09:35:38 +00:00
// clear canvas
2014-03-22 20:04:19 +00:00
XftDrawRect ( draw, &tb->color_bg, 0, 0, tb->w, tb->h );
2012-08-24 01:28:39 +00:00
2014-05-22 07:33:32 +00:00
char *text = tb->text ? tb->text : "";
int text_len = strlen ( text );
int length = text_len;
2014-08-03 09:48:48 +00:00
int font_height = textbox_get_font_height ( tb );
2014-05-22 07:33:32 +00:00
int line_width = 0;
2012-08-24 01:28:39 +00:00
2014-05-22 07:33:32 +00:00
int cursor_x = 0;
2014-08-03 09:48:48 +00:00
int cursor_width = MAX ( 2, font_height / 10 );
2012-08-24 01:28:39 +00:00
2014-08-02 18:02:37 +00:00
pango_layout_set_text ( tb->layout, tb->text, strlen ( tb->text ) );
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_EDITABLE ) {
2014-08-02 18:02:37 +00:00
PangoRectangle pos;
int cursor_offset = 0;
cursor_offset = MIN ( tb->cursor, text_len );
2014-08-02 18:02:37 +00:00
pango_layout_get_cursor_pos ( tb->layout, cursor_offset, &pos, NULL );
// Add a small 4px offset between cursor and last glyph.
2014-08-02 18:02:37 +00:00
cursor_x = pos.x / PANGO_SCALE;
}
2012-08-24 01:28:39 +00:00
2014-08-02 18:02:37 +00:00
pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->w - 2 * SIDE_MARGIN ) );
2012-08-24 01:28:39 +00:00
int x = PANGO_SCALE * SIDE_MARGIN, y = 0;
2012-08-24 01:28:39 +00:00
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_RIGHT ) {
x = ( tb->w - line_width ) * PANGO_SCALE;
2014-03-22 20:04:19 +00:00
}
else if ( tb->flags & TB_CENTER ) {
x = ( PANGO_SCALE * ( tb->w - line_width ) ) / 2;
2014-03-22 20:04:19 +00:00
}
y = ( PANGO_SCALE * ( textbox_get_width ( tb ) - textbox_get_font_width ( tb ) ) ) / 2;
// Render the layout.
pango_xft_render_layout ( draw, &( tb->color_fg ), tb->layout,
x, y );
2012-08-24 01:28:39 +00:00
2014-01-10 09:35:38 +00:00
// draw the cursor
2014-06-04 19:29:23 +00:00
if ( tb->flags & TB_EDITABLE ) {
2014-08-03 09:48:48 +00:00
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, SIDE_MARGIN, cursor_width, font_height );
2014-03-22 20:04:19 +00:00
}
2012-08-24 01:28:39 +00:00
2014-05-22 07:33:32 +00:00
XftDrawRect ( draw, &tb->color_bg, tb->w, 0, 0, tb->h );
2014-01-10 09:35:38 +00:00
// flip canvas to window
2014-03-22 20:04:19 +00:00
XCopyArea ( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 );
2012-08-24 01:28:39 +00:00
2014-03-22 20:04:19 +00:00
XFreeGC ( display, context );
XftDrawDestroy ( draw );
XFreePixmap ( display, canvas );
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
{
2014-03-22 20:04:19 +00:00
tb->cursor = MAX ( 0, MIN ( ( int ) strlen ( tb->text ), pos ) );
2012-08-24 01:28:39 +00:00
}
// move right
2014-03-22 20:04:19 +00:00
void textbox_cursor_inc ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
int index = g_utf8_next_char ( &( tb->text[tb->cursor] ) ) - tb->text;
textbox_cursor ( tb, index );
2012-08-24 01:28:39 +00:00
}
// move left
2014-03-22 20:04:19 +00:00
void textbox_cursor_dec ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
int index = g_utf8_prev_char ( &( tb->text[tb->cursor] ) ) - tb->text;
textbox_cursor ( tb, index );
2012-08-24 01:28:39 +00:00
}
// end of line
2014-03-22 20:04:19 +00:00
void textbox_cursor_end ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
2014-03-22 20:04:19 +00:00
tb->cursor = ( int ) strlen ( tb->text );
2012-08-24 01:28:39 +00:00
}
// insert text
2014-03-22 20:04:19 +00:00
void textbox_insert ( textbox *tb, int pos, char *str )
2012-08-24 01:28:39 +00:00
{
2014-03-22 20:04:19 +00:00
int len = ( int ) strlen ( tb->text ), slen = ( int ) strlen ( str );
pos = MAX ( 0, MIN ( len, pos ) );
2014-01-10 09:35:38 +00:00
// expand buffer
2014-03-22 20:04:19 +00:00
tb->text = 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 );
textbox_extents ( tb );
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
{
2014-03-22 20:04:19 +00:00
int len = strlen ( tb->text );
pos = MAX ( 0, MIN ( len, pos ) );
2014-01-10 09:35:38 +00:00
// move everything after pos+dlen down
char *at = tb->text + pos;
2014-03-22 20:04:19 +00:00
memmove ( at, at + dlen, len - pos );
textbox_extents ( tb );
2012-08-24 01:28:39 +00:00
}
// delete on character
2014-03-22 20:04:19 +00:00
void textbox_cursor_del ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
int index = g_utf8_next_char ( &( tb->text[tb->cursor] ) ) - tb->text;
textbox_delete ( tb, tb->cursor, index - tb->cursor );
2012-08-24 01:28:39 +00:00
}
// back up and delete one character
2014-03-22 20:04:19 +00:00
void textbox_cursor_bkspc ( textbox *tb )
2012-08-24 01:28:39 +00:00
{
2014-06-04 19:29:23 +00:00
if ( 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
}
// handle a keypress in edit mode
// 0 = unhandled
// 1 = handled
// -1 = handled and return pressed (finished)
2014-03-22 20:04:19 +00:00
int textbox_keypress ( textbox *tb, XEvent *ev )
2012-08-24 01:28:39 +00:00
{
2014-01-10 09:35:38 +00:00
KeySym key;
Status stat;
2014-03-22 20:04:19 +00:00
char pad[32];
int len;
2014-01-10 09:35:38 +00:00
2014-06-04 19:29:23 +00:00
if ( !( tb->flags & TB_EDITABLE ) ) {
2014-03-22 20:04:19 +00:00
return 0;
}
2014-01-10 09:35:38 +00:00
2014-03-22 20:04:19 +00:00
len = Xutf8LookupString ( tb->xic, &ev->xkey, pad, sizeof ( pad ), &key, &stat );
2014-01-10 09:35:38 +00:00
pad[len] = 0;
2014-08-03 19:51:31 +00:00
// Left or Ctrl-b
if ( key == XK_Left ||
( ( ev->xkey.state & ControlMask ) && key == XK_b ) ) {
textbox_cursor_dec ( tb );
2014-01-10 09:35:38 +00:00
return 1;
2014-03-22 20:04:19 +00:00
}
2014-08-03 19:51:31 +00:00
// Right or Ctrl-F
else if ( key == XK_Right ||
( ( ev->xkey.state & ControlMask ) && key == XK_f ) ) {
2014-03-22 20:04:19 +00:00
textbox_cursor_inc ( tb );
2014-01-10 09:35:38 +00:00
return 1;
2014-08-03 19:51:31 +00:00
}
// Delete or Ctrl-D
else if ( key == XK_Delete ||
( ( ev->xkey.state & ControlMask ) && key == XK_d ) ) {
2014-03-22 20:04:19 +00:00
textbox_cursor_del ( tb );
2014-01-10 09:35:38 +00:00
return 1;
2014-03-22 20:04:19 +00:00
}
2014-08-03 19:51:31 +00:00
// Ctrl-U: Kill from the beginning to the end of the line.
else if ( ( ev->xkey.state & ControlMask ) && key == XK_u ) {
textbox_text ( tb, "" );
2014-08-03 19:51:31 +00:00
return 1;
}
// Ctrl-A
else if ( ( ev->xkey.state & ControlMask ) && key == XK_a ) {
2014-08-03 19:51:31 +00:00
textbox_cursor ( tb, 0 );
return 1;
}
// Ctrl-E
else if ( ( ev->xkey.state & ControlMask ) && key == XK_e ) {
2014-08-03 19:51:31 +00:00
textbox_cursor_end ( tb );
return 1;
}
// BackSpace, Ctrl-h
else if ( key == XK_BackSpace ||
( ( ev->xkey.state & ControlMask ) && key == XK_h ) ) {
2014-03-22 20:04:19 +00:00
textbox_cursor_bkspc ( tb );
2014-01-10 09:35:38 +00:00
return 1;
2014-03-22 20:04:19 +00:00
}
2014-08-03 19:51:31 +00:00
else if ( key == XK_Return || key == XK_KP_Enter ||
( ( ev->xkey.state & ControlMask ) && key == XK_j ) ||
( ( ev->xkey.state & ControlMask ) && key == XK_m ) ) {
2014-01-10 09:35:38 +00:00
return -1;
2014-03-22 20:04:19 +00:00
}
2014-06-04 19:29:23 +00:00
else if ( !iscntrl ( *pad ) ) {
2014-03-22 20:04:19 +00:00
textbox_insert ( tb, tb->cursor, pad );
textbox_cursor_inc ( tb );
2014-01-10 09:35:38 +00:00
return 1;
}
return 0;
2012-08-24 01:28:39 +00:00
}
/***
* Font setup.
*/
void textbox_setup (
const char *bg, const char *fg,
const char *hlbg, const char *hlfg
)
{
2014-05-26 07:00:14 +00:00
Visual *visual = DefaultVisual ( display, DefaultScreen ( display ) );
Colormap colormap = DefaultColormap ( display, DefaultScreen ( display ) );
2014-05-26 07:00:14 +00:00
XftColorAllocName ( display, visual, colormap, fg, &color_fg );
XftColorAllocName ( display, visual, colormap, bg, &color_bg );
XftColorAllocName ( display, visual, colormap, hlfg, &color_hlfg );
XftColorAllocName ( display, visual, colormap, hlbg, &color_hlbg );
2014-08-02 18:02:37 +00:00
PangoFontMap *font_map = pango_xft_get_font_map ( display, DefaultScreen ( display ) );
p_context = pango_font_map_create_context ( font_map );
}
void textbox_cleanup ()
{
2014-08-02 18:02:37 +00:00
if ( p_context ) {
2014-05-26 07:00:14 +00:00
Visual *visual = DefaultVisual ( display, DefaultScreen ( display ) );
Colormap colormap = DefaultColormap ( display, DefaultScreen ( display ) );
2014-05-26 07:00:14 +00:00
XftColorFree ( display, visual, colormap, &color_fg );
XftColorFree ( display, visual, colormap, &color_bg );
XftColorFree ( display, visual, colormap, &color_hlfg );
XftColorFree ( display, visual, colormap, &color_hlbg );
2014-08-02 18:02:37 +00:00
g_object_unref ( p_context );
p_context = NULL;
}
}
2014-08-02 18:02:37 +00:00
int textbox_get_width ( textbox *tb )
{
return textbox_get_font_width ( tb ) + 2 * SIDE_MARGIN;
}
int textbox_get_height ( textbox *tb )
{
return textbox_get_font_height ( tb ) + 2 * SIDE_MARGIN;
2014-08-02 18:02:37 +00: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;
}