Simplify paste code.

This commit is contained in:
QC 2015-02-09 20:20:51 +01:00
parent 67cbd48c8b
commit a69eb4e22c

View file

@ -743,7 +743,6 @@ static void menu_keyboard_navigation ( MenuState *state, KeySym key, unsigned in
* *
* mouse navigation through the elements. * mouse navigation through the elements.
* *
* TODO: Scroll wheel.
*/ */
static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe ) static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe )
{ {
@ -950,40 +949,22 @@ static void menu_update ( MenuState *state )
static void menu_paste ( MenuState *state, XSelectionEvent *xse ) static void menu_paste ( MenuState *state, XSelectionEvent *xse )
{ {
if ( xse->property == netatoms[UTF8_STRING] ) { if ( xse->property == netatoms[UTF8_STRING] ) {
char *pbuf = NULL; gchar *text = window_get_text_prop(display, main_window, netatoms[UTF8_STRING]);
int di; if(text != NULL && text[0] != '\0' ) {
unsigned long dl, rm; unsigned int dl = strlen(text);
Atom da; // Strip new line
while ( dl > 0 && text[dl] == '\n') {
// TODO: use window_get_prop? text[dl] = '\0';
/* we have been given the current selection, now insert it into input */ dl--;
XGetWindowProperty ( display, main_window, netatoms[UTF8_STRING],
0,
256 / 4, // max length in words.
False, // Do not delete clipboard.
netatoms[UTF8_STRING], &da, &di, &dl, &rm, (unsigned char * *) &pbuf );
// If There was remaining data left.. lets ignore this.
// Only accept it when we get bytes!
if ( di == 8 ) {
char *index;
if ( ( index = strchr ( pbuf, '\n' ) ) != NULL ) {
// Calc new length;
dl = index - pbuf;
} }
// Create a NULL terminated string. I am not sure how the data is returned.
// With or without trailing 0
char str[dl + 1];
memcpy ( str, pbuf, dl );
str[dl] = '\0';
// Insert string move cursor. // Insert string move cursor.
textbox_insert ( state->text, state->text->cursor, str ); textbox_insert ( state->text, state->text->cursor, text );
textbox_cursor ( state->text, state->text->cursor + dl ); textbox_cursor ( state->text, state->text->cursor + dl -1 );
// Force a redraw and refiltering of the text. // Force a redraw and refiltering of the text.
state->update = TRUE; state->update = TRUE;
state->refilter = TRUE; state->refilter = TRUE;
} }
XFree ( pbuf ); g_free(text);
} }
} }
@ -1233,10 +1214,9 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
} }
// Paste event. // Paste event.
else if ( ev.type == SelectionNotify ) { else if ( ev.type == SelectionNotify ) {
while ( XCheckTypedEvent ( display, SelectionNotify, &ev ) ) { do {
; menu_paste ( &state, &( ev.xselection ) );
} }while ( XCheckTypedEvent ( display, SelectionNotify, &ev ) );
menu_paste ( &state, &( ev.xselection ) );
} }
// Key press event. // Key press event.
else if ( ev.type == KeyPress ) { else if ( ev.type == KeyPress ) {