2012-11-07 04:16:59 +00:00
|
|
|
/*
|
2012-08-24 01:28:39 +00:00
|
|
|
|
|
|
|
MIT/X11 License
|
|
|
|
Copyright (c) 2012 Sean Pringle <sean.pringle@gmail.com>
|
2014-01-13 08:42:52 +00:00
|
|
|
Modified (c) 2013-2014 Qball Cow <qball@gmpclient.org>
|
2012-08-24 01:28:39 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-01-19 12:57:54 +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-01-20 22:36:20 +00:00
|
|
|
#include "simpleswitcher.h"
|
2014-01-19 12:57:54 +00:00
|
|
|
#include "textbox.h"
|
2014-01-13 08:42:52 +00:00
|
|
|
#define SIDE_MARGIN 3
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-19 12:57:54 +00:00
|
|
|
extern Display *display;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +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-01-19 12:57:54 +00:00
|
|
|
textbox* textbox_create( Window parent,
|
2014-01-19 13:18:42 +00:00
|
|
|
TextboxFlags flags,
|
|
|
|
short x, short y, short w, short h,
|
|
|
|
char *font, char *fg, char *bg,
|
|
|
|
char *text, char *prompt )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
textbox *tb = calloc( 1, sizeof( textbox ) );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
tb->flags = flags;
|
|
|
|
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;
|
|
|
|
tb->w = MAX( 1, w );
|
|
|
|
tb->h = MAX( 1, h );
|
2012-11-07 04:16:59 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
XColor color;
|
|
|
|
Colormap map = DefaultColormap( display, DefaultScreen( display ) );
|
|
|
|
unsigned int cp = XAllocNamedColor( display, map, bg, &color, &color ) ? color.pixel: None;
|
2012-11-07 04:16:59 +00:00
|
|
|
|
2014-01-10 09:35:38 +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, font, fg, bg );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
tb->prompt = strdup( prompt ? prompt: "" );
|
|
|
|
textbox_text( tb, text ? text: "" );
|
2014-01-16 17:47: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
|
|
|
|
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
|
|
|
|
if ( tb->flags & TB_EDITABLE ) {
|
|
|
|
tb->xim = XOpenIM( display, NULL, NULL, NULL );
|
|
|
|
tb->xic = XCreateIC( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL );
|
|
|
|
}
|
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
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_font( textbox *tb, char *font, char *fg, char *bg )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-13 20:35:43 +00:00
|
|
|
if ( tb->font ) {
|
|
|
|
XftColorFree ( display,
|
|
|
|
DefaultVisual( display, DefaultScreen( display ) ),
|
|
|
|
DefaultColormap( display, DefaultScreen( display ) ),
|
|
|
|
&tb->color_fg );
|
|
|
|
XftColorFree ( display,
|
|
|
|
DefaultVisual( display, DefaultScreen( display ) ),
|
|
|
|
DefaultColormap( display, DefaultScreen( display ) ),
|
|
|
|
&tb->color_bg );
|
|
|
|
|
|
|
|
XftFontClose( display, tb->font );
|
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
tb->font = XftFontOpenName( display, DefaultScreen( display ), font );
|
|
|
|
|
|
|
|
XftColorAllocName( display, DefaultVisual( display, DefaultScreen( display ) ), DefaultColormap( display, DefaultScreen( display ) ), fg, &tb->color_fg );
|
|
|
|
XftColorAllocName( display, DefaultVisual( display, DefaultScreen( display ) ), DefaultColormap( display, DefaultScreen( display ) ), bg, &tb->color_bg );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// outer code may need line height, width, etc
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_extents( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-30 20:32:36 +00:00
|
|
|
int length = strlen( tb->text ) + strlen( tb->prompt ) +1;
|
2014-01-10 09:35:38 +00:00
|
|
|
char *line = alloca( length + 1 );
|
2014-01-30 17:23:52 +00:00
|
|
|
sprintf( line, "%s %s", tb->prompt, tb->text );
|
2014-01-10 09:35:38 +00:00
|
|
|
XftTextExtents8( display, tb->font, ( unsigned char* )line, length, &tb->extents );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the default text to display
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_text( textbox *tb, char *text )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->text ) free( tb->text );
|
|
|
|
|
|
|
|
tb->text = strdup( text );
|
|
|
|
tb->cursor = MAX( 0,MIN( ( int )strlen( text ), tb->cursor ) );
|
|
|
|
textbox_extents( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set an input prompt for edit mode
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_prompt( textbox *tb, char *text )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->prompt ) free( tb->prompt );
|
|
|
|
|
|
|
|
tb->prompt = strdup( text );
|
|
|
|
textbox_extents( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// within the parent. handled auto width/height modes
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_moveresize( textbox *tb, int x, int y, int w, int h )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->flags & TB_AUTOHEIGHT )
|
|
|
|
h = tb->font->ascent + tb->font->descent;
|
|
|
|
|
|
|
|
if ( tb->flags & TB_AUTOWIDTH )
|
|
|
|
w = tb->extents.width;
|
|
|
|
|
|
|
|
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) {
|
|
|
|
tb->x = x;
|
|
|
|
tb->y = y;
|
|
|
|
tb->w = MAX( 1, w );
|
|
|
|
tb->h = MAX( 1, h );
|
|
|
|
XMoveResizeWindow( display, tb->window, tb->x, tb->y, tb->w, tb->h );
|
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_show( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
XMapWindow( display, tb->window );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_hide( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
XUnmapWindow( display, tb->window );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// will also unmap the window if still displayed
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_free( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->flags & TB_EDITABLE ) {
|
|
|
|
XDestroyIC( tb->xic );
|
|
|
|
XCloseIM( tb->xim );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( tb->text ) free( tb->text );
|
|
|
|
|
|
|
|
if ( tb->prompt ) free( tb->prompt );
|
|
|
|
|
2014-01-22 12:07:52 +00:00
|
|
|
if ( tb->font ) {
|
|
|
|
XftColorFree ( display,
|
|
|
|
DefaultVisual( display, DefaultScreen( display ) ),
|
|
|
|
DefaultColormap( display, DefaultScreen( display ) ),
|
|
|
|
&tb->color_fg );
|
|
|
|
XftColorFree ( display,
|
|
|
|
DefaultVisual( display, DefaultScreen( display ) ),
|
|
|
|
DefaultColormap( display, DefaultScreen( display ) ),
|
|
|
|
&tb->color_bg );
|
|
|
|
|
|
|
|
XftFontClose( display, tb->font );
|
|
|
|
}
|
2014-01-10 09:35:38 +00:00
|
|
|
|
|
|
|
XDestroyWindow( display, tb->window );
|
|
|
|
free( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_draw( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
int i;
|
|
|
|
XGlyphInfo extents;
|
|
|
|
|
|
|
|
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 ) ) );
|
|
|
|
|
|
|
|
// clear canvas
|
|
|
|
XftDrawRect( draw, &tb->color_bg, 0, 0, tb->w, tb->h );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
char *line = tb->text,
|
|
|
|
*text = tb->text ? tb->text: "",
|
|
|
|
*prompt = tb->prompt ? tb->prompt: "";
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
int text_len = strlen( text );
|
|
|
|
int length = text_len;
|
|
|
|
int line_height = tb->font->ascent + tb->font->descent;
|
|
|
|
int line_width = 0;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
int cursor_x = 0;
|
|
|
|
int cursor_offset = 0;
|
|
|
|
int cursor_width = MAX( 2, line_height/10 );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->flags & TB_EDITABLE ) {
|
2014-01-30 17:23:52 +00:00
|
|
|
int prompt_len = strlen( prompt ) +1;
|
2014-01-10 09:35:38 +00:00
|
|
|
length = text_len + prompt_len;
|
|
|
|
cursor_offset = MIN( tb->cursor + prompt_len, length );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
line = alloca( length + 10 );
|
2014-01-30 17:23:52 +00:00
|
|
|
sprintf( line, "%s %s", prompt, text );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// replace spaces so XftTextExtents8 includes their width
|
|
|
|
for ( i = 0; i < length; i++ ) if ( isspace( line[i] ) ) line[i] = '_';
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// calc cursor position
|
|
|
|
XftTextExtents8( display, tb->font, ( unsigned char* )line, cursor_offset, &extents );
|
|
|
|
cursor_x = extents.width;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// restore correct text string with spaces
|
2014-01-30 17:23:52 +00:00
|
|
|
sprintf( line, "%s %s", prompt, text );
|
2014-01-10 09:35:38 +00:00
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// calc full input text width
|
2014-01-13 08:42:52 +00:00
|
|
|
// Calculate the right size, so no characters are cut off.
|
|
|
|
// TODO: Check performance of this.
|
2014-01-13 20:35:43 +00:00
|
|
|
while ( 1 ) {
|
2014-01-13 08:42:52 +00:00
|
|
|
XftTextExtents8( display, tb->font, ( unsigned char* )line, length, &extents );
|
|
|
|
line_width = extents.width;
|
2014-01-13 20:35:43 +00:00
|
|
|
|
|
|
|
if ( line_width < ( tb->w-2*SIDE_MARGIN ) ) break;
|
|
|
|
|
2014-01-13 08:42:52 +00:00
|
|
|
length--;
|
|
|
|
}
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-13 08:42:52 +00:00
|
|
|
int x = SIDE_MARGIN, y = tb->font->ascent;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->flags & TB_RIGHT ) x = tb->w - line_width;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->flags & TB_CENTER ) x = ( tb->w - line_width ) / 2;
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// draw the text, including any prompt in edit mode
|
|
|
|
XftDrawString8( draw, &tb->color_fg, tb->font, x, y, ( unsigned char* )line, length );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
// draw the cursor
|
|
|
|
if ( tb->flags & TB_EDITABLE )
|
2014-01-13 08:42:52 +00:00
|
|
|
XftDrawRect( draw, &tb->color_fg, cursor_x+SIDE_MARGIN, 2, cursor_width, line_height-4 );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-13 08:42:52 +00:00
|
|
|
XftDrawRect( draw, &tb->color_bg, tb->w-SIDE_MARGIN, 0, SIDE_MARGIN, tb->h );
|
2014-01-10 09:35:38 +00:00
|
|
|
// flip canvas to window
|
|
|
|
XCopyArea( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 );
|
2012-08-24 01:28:39 +00:00
|
|
|
|
2014-01-10 09:35:38 +00:00
|
|
|
XFreeGC( display, context );
|
|
|
|
XftDrawDestroy( draw );
|
|
|
|
XFreePixmap( display, canvas );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cursor handling for edit mode
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor( textbox *tb, int pos )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
tb->cursor = MAX( 0, MIN( ( int )strlen( tb->text ), pos ) );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// move right
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_inc( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
textbox_cursor( tb, tb->cursor+1 );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// move left
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_dec( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
textbox_cursor( tb, tb->cursor-1 );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// beginning of line
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_home( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
tb->cursor = 0;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// end of line
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_end( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
tb->cursor = ( int )strlen( tb->text );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert text
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_insert( textbox *tb, int pos, char *str )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
int len = ( int )strlen( tb->text ), slen = ( int )strlen( str );
|
|
|
|
pos = MAX( 0, MIN( len, pos ) );
|
|
|
|
// expand buffer
|
|
|
|
tb->text = realloc( tb->text, len + slen + 1 );
|
|
|
|
// move everything after cursor upward
|
|
|
|
char *at = tb->text + pos;
|
|
|
|
memmove( at + slen, at, len - pos + 1 );
|
|
|
|
// insert new str
|
|
|
|
memmove( at, str, slen );
|
|
|
|
textbox_extents( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove text
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_delete( textbox *tb, int pos, int dlen )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
int len = strlen( tb->text );
|
|
|
|
pos = MAX( 0, MIN( len, pos ) );
|
|
|
|
// move everything after pos+dlen down
|
|
|
|
char *at = tb->text + pos;
|
|
|
|
memmove( at, at + dlen, len - pos );
|
|
|
|
textbox_extents( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert one character
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_ins( textbox *tb, char c )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
char tmp[2] = { c, 0 };
|
|
|
|
textbox_insert( tb, tb->cursor, tmp );
|
|
|
|
textbox_cursor_inc( tb );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete on character
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_del( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
textbox_delete( tb, tb->cursor, 1 );
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// back up and delete one character
|
2014-01-10 09:35:38 +00:00
|
|
|
void textbox_cursor_bkspc( textbox *tb )
|
2012-08-24 01:28:39 +00:00
|
|
|
{
|
2014-01-10 09:35:38 +00:00
|
|
|
if ( tb->cursor > 0 ) {
|
|
|
|
textbox_cursor_dec( tb );
|
|
|
|
textbox_cursor_del( tb );
|
|
|
|
}
|
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-01-10 09:35:38 +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;
|
|
|
|
char pad[32];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if ( !( tb->flags & TB_EDITABLE ) ) return 0;
|
|
|
|
|
|
|
|
len = XmbLookupString( tb->xic, &ev->xkey, pad, sizeof( pad ), &key, &stat );
|
|
|
|
pad[len] = 0;
|
|
|
|
|
|
|
|
if ( key == XK_Left ) {
|
|
|
|
textbox_cursor_dec( tb );
|
|
|
|
return 1;
|
|
|
|
} else if ( key == XK_Right ) {
|
|
|
|
textbox_cursor_inc( tb );
|
|
|
|
return 1;
|
|
|
|
} else if ( key == XK_Home ) {
|
|
|
|
textbox_cursor_home( tb );
|
|
|
|
return 1;
|
|
|
|
} else if ( key == XK_End ) {
|
|
|
|
textbox_cursor_end( tb );
|
|
|
|
return 1;
|
|
|
|
} else if ( key == XK_Delete ) {
|
|
|
|
textbox_cursor_del( tb );
|
|
|
|
return 1;
|
|
|
|
} else if ( key == XK_BackSpace ) {
|
|
|
|
textbox_cursor_bkspc( tb );
|
|
|
|
return 1;
|
|
|
|
} else if ( key == XK_Return ) {
|
|
|
|
return -1;
|
|
|
|
} else if ( !iscntrl( *pad ) ) {
|
|
|
|
textbox_insert( tb, tb->cursor, pad );
|
|
|
|
textbox_cursor_inc( tb );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-08-24 01:28:39 +00:00
|
|
|
}
|