Skip to content
Permalink
Browse files

SDL2 clipboard support

git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@8521 8aca7d54-2c30-db11-9de9-000461428c89
  • Loading branch information
Markus Pristovsek
Markus Pristovsek committed Jun 26, 2018
1 parent 8593f5b commit 2a110cfcac47672b79e10ecb45218dca2d204dbc
Showing with 59 additions and 0 deletions.
  1. +59 −0 clipboard_s2.cc
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2010 Knightly
*
* Clipboard functions for copy and paste of text
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/

#include <SDL2/SDL.h>

#include <string.h>

#include "simsys.h"
#include "display/simgraph.h"
#include "simdebug.h"
#include "dataobj/translator.h"

/**
* Copy text to the clipboard
* @param source : pointer to the start of the source text
* @param length : number of character bytes to copy
* @author Knightly
*/
void dr_copy(const char *source, size_t length)
{
char *text = (char *)malloc( length+1 );
strncpy( text, source, length );
text[length] = 0;
SDL_SetClipboardText( text );
free( text );
}


/**
* Paste text from the clipboard
* @param target : pointer to the insertion position in the target text
* @param max_length : maximum number of character bytes which could be inserted
* @return number of character bytes actually inserted -> for cursor advancing
* @author Knightly
*/
size_t dr_paste(char *target, size_t max_length)
{
size_t insert_len = 0;

if( SDL_HasClipboardText() ) {
char *str = SDL_GetClipboardText();

// Insert string.
size_t const str_len = strlen(str);
insert_len = str_len <= max_length ? str_len : (size_t)utf8_get_prev_char((utf8 const *)str, max_length);
memmove(target + insert_len, target, strlen(target) + 1);
memcpy(target, str, insert_len);

SDL_free( str );
}

return insert_len;
}

0 comments on commit 2a110cf

Please sign in to comment.
You can’t perform that action at this time.