Indent the bunch with my astyle line.

This commit is contained in:
Qball Cow 2014-01-10 10:35:38 +01:00
parent bad311adab
commit b47f22ca51
3 changed files with 961 additions and 886 deletions

View File

@ -24,12 +24,16 @@ normal:
debug: debug:
$(CC) -o simpleswitcher-debug simpleswitcher.c -std=c99 $(CFLAGS) -Wunused-parameter -g -DDEBUG $(LDADD) $(CC) -o simpleswitcher-debug simpleswitcher.c -std=c99 $(CFLAGS) -Wunused-parameter -g -DDEBUG $(LDADD)
install: install-man install: normal install-man
install -Dm 755 simpleswitcher $(BINDIR)/simpleswitcher install -Dm 755 simpleswitcher $(BINDIR)/simpleswitcher
install-man: install-man:
install -Dm 644 simpleswitcher.1 $(MANDIR) install -Dm 644 simpleswitcher.1 $(MANDIR)
gzip $(MANDIR)/simpleswitcher.1 gzip -f $(MANDIR)/simpleswitcher.1
clean: clean:
rm -f simpleswitcher simpleswitcher-debug rm -f simpleswitcher simpleswitcher-debug
indent:
@astyle --style=linux -S -C -D -N -H -L -W3 -f simpleswitcher.c textbox.c

File diff suppressed because it is too large Load Diff

432
textbox.c
View File

@ -32,343 +32,333 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define TB_EDITABLE 1<<19 #define TB_EDITABLE 1<<19
typedef struct { typedef struct {
unsigned long flags; unsigned long flags;
Window window, parent; Window window, parent;
short x, y, w, h; short x, y, w, h;
short cursor; short cursor;
XftFont *font; XftFont *font;
XftColor color_fg, color_bg; XftColor color_fg, color_bg;
char *text, *prompt; char *text, *prompt;
XIM xim; XIM xim;
XIC xic; XIC xic;
XGlyphInfo extents; XGlyphInfo extents;
} textbox; } textbox;
void textbox_font(textbox *tb, char *font, char *fg, char *bg); void textbox_font( textbox *tb, char *font, char *fg, char *bg );
void textbox_text(textbox *tb, char *text); void textbox_text( textbox *tb, char *text );
void textbox_moveresize(textbox *tb, int x, int y, int w, int h); void textbox_moveresize( textbox *tb, int x, int y, int w, int h );
// Xft text box, optionally editable // Xft text box, optionally editable
textbox* textbox_create(Window parent, unsigned long flags, short x, short y, short w, short h, char *font, char *fg, char *bg, char *text, char *prompt) textbox* textbox_create( Window parent, unsigned long flags, short x, short y, short w, short h, char *font, char *fg, char *bg, char *text, char *prompt )
{ {
textbox *tb = calloc(1, sizeof(textbox)); textbox *tb = calloc( 1, sizeof( textbox ) );
tb->flags = flags; tb->flags = flags;
tb->parent = parent; tb->parent = parent;
tb->x = x; tb->y = y; tb->w = MAX(1, w); tb->h = MAX(1, h); tb->x = x;
tb->y = y;
tb->w = MAX( 1, w );
tb->h = MAX( 1, h );
XColor color; Colormap map = DefaultColormap(display, DefaultScreen(display)); XColor color;
unsigned int cp = XAllocNamedColor(display, map, bg, &color, &color) ? color.pixel: None; Colormap map = DefaultColormap( display, DefaultScreen( display ) );
unsigned int cp = XAllocNamedColor( display, map, bg, &color, &color ) ? color.pixel: None;
tb->window = XCreateSimpleWindow(display, tb->parent, tb->x, tb->y, tb->w, tb->h, 0, None, cp); tb->window = XCreateSimpleWindow( display, tb->parent, tb->x, tb->y, tb->w, tb->h, 0, None, cp );
// need to preload the font to calc line height // need to preload the font to calc line height
textbox_font(tb, font, fg, bg); textbox_font( tb, font, fg, bg );
tb->prompt = strdup(prompt ? prompt: ""); tb->prompt = strdup( prompt ? prompt: "" );
textbox_text(tb, text ? text: ""); textbox_text( tb, text ? text: "" );
// auto height/width modes get handled here // auto height/width modes get handled here
textbox_moveresize(tb, tb->x, tb->y, tb->w, tb->h); textbox_moveresize( tb, tb->x, tb->y, tb->w, tb->h );
// edit mode controls // edit mode controls
if (tb->flags & TB_EDITABLE) if ( tb->flags & TB_EDITABLE ) {
{ tb->xim = XOpenIM( display, NULL, NULL, NULL );
tb->xim = XOpenIM(display, NULL, NULL, NULL); tb->xic = XCreateIC( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL );
tb->xic = XCreateIC(tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL); }
}
return tb; return tb;
} }
// set an Xft font by name // set an Xft font by name
void textbox_font(textbox *tb, char *font, char *fg, char *bg) void textbox_font( textbox *tb, char *font, char *fg, char *bg )
{ {
if (tb->font) XftFontClose(display, tb->font); if ( tb->font ) XftFontClose( display, tb->font );
tb->font = XftFontOpenName(display, DefaultScreen(display), font);
XftColorAllocName(display, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)), fg, &tb->color_fg); tb->font = XftFontOpenName( display, DefaultScreen( display ), font );
XftColorAllocName(display, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)), bg, &tb->color_bg);
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 );
} }
// outer code may need line height, width, etc // outer code may need line height, width, etc
void textbox_extents(textbox *tb) void textbox_extents( textbox *tb )
{ {
int length = strlen(tb->text) + strlen(tb->prompt); int length = strlen( tb->text ) + strlen( tb->prompt );
char *line = alloca(length + 1); char *line = alloca( length + 1 );
sprintf(line, "%s%s", tb->prompt, tb->text); sprintf( line, "%s%s", tb->prompt, tb->text );
XftTextExtents8(display, tb->font, (unsigned char*)line, length, &tb->extents); XftTextExtents8( display, tb->font, ( unsigned char* )line, length, &tb->extents );
} }
// set the default text to display // set the default text to display
void textbox_text(textbox *tb, char *text) void textbox_text( textbox *tb, char *text )
{ {
if (tb->text) free(tb->text); if ( tb->text ) free( tb->text );
tb->text = strdup(text);
tb->cursor = MAX(0,MIN((int)strlen(text), tb->cursor)); tb->text = strdup( text );
textbox_extents(tb); tb->cursor = MAX( 0,MIN( ( int )strlen( text ), tb->cursor ) );
textbox_extents( tb );
} }
// set an input prompt for edit mode // set an input prompt for edit mode
void textbox_prompt(textbox *tb, char *text) void textbox_prompt( textbox *tb, char *text )
{ {
if (tb->prompt) free(tb->prompt); if ( tb->prompt ) free( tb->prompt );
tb->prompt = strdup(text);
textbox_extents(tb); tb->prompt = strdup( text );
textbox_extents( tb );
} }
// within the parent. handled auto width/height modes // within the parent. handled auto width/height modes
void textbox_moveresize(textbox *tb, int x, int y, int w, int h) void textbox_moveresize( textbox *tb, int x, int y, int w, int h )
{ {
if (tb->flags & TB_AUTOHEIGHT) if ( tb->flags & TB_AUTOHEIGHT )
h = tb->font->ascent + tb->font->descent; h = tb->font->ascent + tb->font->descent;
if (tb->flags & TB_AUTOWIDTH) if ( tb->flags & TB_AUTOWIDTH )
w = tb->extents.width; w = tb->extents.width;
if (x != tb->x || y != tb->y || w != tb->w || h != tb->h) if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) {
{ tb->x = x;
tb->x = x; tb->y = y; tb->w = MAX(1, w); tb->h = MAX(1, h); tb->y = y;
XMoveResizeWindow(display, tb->window, tb->x, tb->y, tb->w, tb->h); tb->w = MAX( 1, w );
} tb->h = MAX( 1, h );
XMoveResizeWindow( display, tb->window, tb->x, tb->y, tb->w, tb->h );
}
} }
void textbox_show(textbox *tb) void textbox_show( textbox *tb )
{ {
XMapWindow(display, tb->window); XMapWindow( display, tb->window );
} }
void textbox_hide(textbox *tb) void textbox_hide( textbox *tb )
{ {
XUnmapWindow(display, tb->window); XUnmapWindow( display, tb->window );
} }
// will also unmap the window if still displayed // will also unmap the window if still displayed
void textbox_free(textbox *tb) void textbox_free( textbox *tb )
{ {
if (tb->flags & TB_EDITABLE) if ( tb->flags & TB_EDITABLE ) {
{ XDestroyIC( tb->xic );
XDestroyIC(tb->xic); XCloseIM( tb->xim );
XCloseIM(tb->xim); }
}
if (tb->text) free(tb->text); if ( tb->text ) free( tb->text );
if (tb->prompt) free(tb->prompt);
if (tb->font) XftFontClose(display, tb->font);
XDestroyWindow(display, tb->window); if ( tb->prompt ) free( tb->prompt );
free(tb);
if ( tb->font ) XftFontClose( display, tb->font );
XDestroyWindow( display, tb->window );
free( tb );
} }
void textbox_draw(textbox *tb) void textbox_draw( textbox *tb )
{ {
int i; int i;
XGlyphInfo extents; XGlyphInfo extents;
GC context = XCreateGC(display, tb->window, 0, 0); GC context = XCreateGC( display, tb->window, 0, 0 );
Pixmap canvas = XCreatePixmap(display, tb->window, tb->w, tb->h, DefaultDepth(display, DefaultScreen(display))); 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))); XftDraw *draw = XftDrawCreate( display, canvas, DefaultVisual( display, DefaultScreen( display ) ), DefaultColormap( display, DefaultScreen( display ) ) );
// clear canvas // clear canvas
XftDrawRect(draw, &tb->color_bg, 0, 0, tb->w, tb->h); XftDrawRect( draw, &tb->color_bg, 0, 0, tb->w, tb->h );
char *line = tb->text, char *line = tb->text,
*text = tb->text ? tb->text: "", *text = tb->text ? tb->text: "",
*prompt = tb->prompt ? tb->prompt: ""; *prompt = tb->prompt ? tb->prompt: "";
int text_len = strlen(text); int text_len = strlen( text );
int length = text_len; int length = text_len;
int line_height = tb->font->ascent + tb->font->descent; int line_height = tb->font->ascent + tb->font->descent;
int line_width = 0; int line_width = 0;
int cursor_x = 0; int cursor_x = 0;
int cursor_offset = 0; int cursor_offset = 0;
int cursor_width = MAX(2, line_height/10); int cursor_width = MAX( 2, line_height/10 );
if (tb->flags & TB_EDITABLE) if ( tb->flags & TB_EDITABLE ) {
{ int prompt_len = strlen( prompt );
int prompt_len = strlen(prompt); length = text_len + prompt_len;
length = text_len + prompt_len; cursor_offset = MIN( tb->cursor + prompt_len, length );
cursor_offset = MIN(tb->cursor + prompt_len, length);
line = alloca(length + 10); line = alloca( length + 10 );
sprintf(line, "%s%s", prompt, text); sprintf( line, "%s%s", prompt, text );
// replace spaces so XftTextExtents8 includes their width // replace spaces so XftTextExtents8 includes their width
for (i = 0; i < length; i++) if (isspace(line[i])) line[i] = '_'; for ( i = 0; i < length; i++ ) if ( isspace( line[i] ) ) line[i] = '_';
// calc cursor position // calc cursor position
XftTextExtents8(display, tb->font, (unsigned char*)line, cursor_offset, &extents); XftTextExtents8( display, tb->font, ( unsigned char* )line, cursor_offset, &extents );
cursor_x = extents.width; cursor_x = extents.width;
// restore correct text string with spaces // restore correct text string with spaces
sprintf(line, "%s%s", prompt, text); sprintf( line, "%s%s", prompt, text );
} }
// calc full input text width // calc full input text width
XftTextExtents8(display, tb->font, (unsigned char*)line, length, &extents); XftTextExtents8( display, tb->font, ( unsigned char* )line, length, &extents );
line_width = extents.width; line_width = extents.width;
int x = 2, y = tb->font->ascent; int x = 2, y = tb->font->ascent;
if (tb->flags & TB_RIGHT) x = tb->w - line_width;
if (tb->flags & TB_CENTER) x = (tb->w - line_width) / 2;
// draw the text, including any prompt in edit mode if ( tb->flags & TB_RIGHT ) x = tb->w - line_width;
XftDrawString8(draw, &tb->color_fg, tb->font, x, y, (unsigned char*)line, length);
// draw the cursor if ( tb->flags & TB_CENTER ) x = ( tb->w - line_width ) / 2;
if (tb->flags & TB_EDITABLE)
XftDrawRect(draw, &tb->color_fg, cursor_x, 2, cursor_width, line_height-4);
// flip canvas to window // draw the text, including any prompt in edit mode
XCopyArea(display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0); XftDrawString8( draw, &tb->color_fg, tb->font, x, y, ( unsigned char* )line, length );
XFreeGC(display, context); // draw the cursor
XftDrawDestroy(draw); if ( tb->flags & TB_EDITABLE )
XFreePixmap(display, canvas); XftDrawRect( draw, &tb->color_fg, cursor_x, 2, cursor_width, line_height-4 );
// flip canvas to window
XCopyArea( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 );
XFreeGC( display, context );
XftDrawDestroy( draw );
XFreePixmap( display, canvas );
} }
// cursor handling for edit mode // cursor handling for edit mode
void textbox_cursor(textbox *tb, int pos) void textbox_cursor( textbox *tb, int pos )
{ {
tb->cursor = MAX(0, MIN((int)strlen(tb->text), pos)); tb->cursor = MAX( 0, MIN( ( int )strlen( tb->text ), pos ) );
} }
// move right // move right
void textbox_cursor_inc(textbox *tb) void textbox_cursor_inc( textbox *tb )
{ {
textbox_cursor(tb, tb->cursor+1); textbox_cursor( tb, tb->cursor+1 );
} }
// move left // move left
void textbox_cursor_dec(textbox *tb) void textbox_cursor_dec( textbox *tb )
{ {
textbox_cursor(tb, tb->cursor-1); textbox_cursor( tb, tb->cursor-1 );
} }
// beginning of line // beginning of line
void textbox_cursor_home(textbox *tb) void textbox_cursor_home( textbox *tb )
{ {
tb->cursor = 0; tb->cursor = 0;
} }
// end of line // end of line
void textbox_cursor_end(textbox *tb) void textbox_cursor_end( textbox *tb )
{ {
tb->cursor = (int)strlen(tb->text); tb->cursor = ( int )strlen( tb->text );
} }
// insert text // insert text
void textbox_insert(textbox *tb, int pos, char *str) void textbox_insert( textbox *tb, int pos, char *str )
{ {
int len = (int)strlen(tb->text), slen = (int)strlen(str); int len = ( int )strlen( tb->text ), slen = ( int )strlen( str );
pos = MAX(0, MIN(len, pos)); pos = MAX( 0, MIN( len, pos ) );
// expand buffer // expand buffer
tb->text = realloc(tb->text, len + slen + 1); tb->text = realloc( tb->text, len + slen + 1 );
// move everything after cursor upward // move everything after cursor upward
char *at = tb->text + pos; char *at = tb->text + pos;
memmove(at + slen, at, len - pos + 1); memmove( at + slen, at, len - pos + 1 );
// insert new str // insert new str
memmove(at, str, slen); memmove( at, str, slen );
textbox_extents(tb); textbox_extents( tb );
} }
// remove text // remove text
void textbox_delete(textbox *tb, int pos, int dlen) void textbox_delete( textbox *tb, int pos, int dlen )
{ {
int len = strlen(tb->text); int len = strlen( tb->text );
pos = MAX(0, MIN(len, pos)); pos = MAX( 0, MIN( len, pos ) );
// move everything after pos+dlen down // move everything after pos+dlen down
char *at = tb->text + pos; char *at = tb->text + pos;
memmove(at, at + dlen, len - pos); memmove( at, at + dlen, len - pos );
textbox_extents(tb); textbox_extents( tb );
} }
// insert one character // insert one character
void textbox_cursor_ins(textbox *tb, char c) void textbox_cursor_ins( textbox *tb, char c )
{ {
char tmp[2] = { c, 0 }; char tmp[2] = { c, 0 };
textbox_insert(tb, tb->cursor, tmp); textbox_insert( tb, tb->cursor, tmp );
textbox_cursor_inc(tb); textbox_cursor_inc( tb );
} }
// delete on character // delete on character
void textbox_cursor_del(textbox *tb) void textbox_cursor_del( textbox *tb )
{ {
textbox_delete(tb, tb->cursor, 1); textbox_delete( tb, tb->cursor, 1 );
} }
// back up and delete one character // back up and delete one character
void textbox_cursor_bkspc(textbox *tb) void textbox_cursor_bkspc( textbox *tb )
{ {
if (tb->cursor > 0) if ( tb->cursor > 0 ) {
{ textbox_cursor_dec( tb );
textbox_cursor_dec(tb); textbox_cursor_del( tb );
textbox_cursor_del(tb); }
}
} }
// handle a keypress in edit mode // handle a keypress in edit mode
// 0 = unhandled // 0 = unhandled
// 1 = handled // 1 = handled
// -1 = handled and return pressed (finished) // -1 = handled and return pressed (finished)
int textbox_keypress(textbox *tb, XEvent *ev) int textbox_keypress( textbox *tb, XEvent *ev )
{ {
KeySym key; Status stat; KeySym key;
char pad[32]; int len; Status stat;
char pad[32];
int len;
if (!(tb->flags & TB_EDITABLE)) return 0; if ( !( tb->flags & TB_EDITABLE ) ) return 0;
len = XmbLookupString(tb->xic, &ev->xkey, pad, sizeof(pad), &key, &stat); len = XmbLookupString( tb->xic, &ev->xkey, pad, sizeof( pad ), &key, &stat );
pad[len] = 0; pad[len] = 0;
if (key == XK_Left) if ( key == XK_Left ) {
{ textbox_cursor_dec( tb );
textbox_cursor_dec(tb); return 1;
return 1; } else if ( key == XK_Right ) {
} textbox_cursor_inc( tb );
else return 1;
if (key == XK_Right) } else if ( key == XK_Home ) {
{ textbox_cursor_home( tb );
textbox_cursor_inc(tb); return 1;
return 1; } else if ( key == XK_End ) {
} textbox_cursor_end( tb );
else return 1;
if (key == XK_Home) } else if ( key == XK_Delete ) {
{ textbox_cursor_del( tb );
textbox_cursor_home(tb); return 1;
return 1; } else if ( key == XK_BackSpace ) {
} textbox_cursor_bkspc( tb );
else return 1;
if (key == XK_End) } else if ( key == XK_Return ) {
{ return -1;
textbox_cursor_end(tb); } else if ( !iscntrl( *pad ) ) {
return 1; textbox_insert( tb, tb->cursor, pad );
} textbox_cursor_inc( tb );
else return 1;
if (key == XK_Delete) }
{
textbox_cursor_del(tb); return 0;
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;
} }