优化linux 剪切板,优化 api返回非const string销毁问题
This commit is contained in:
tslediter 2024-03-13 11:51:35 +08:00
parent 76bc4e3372
commit 515f227bb2
3 changed files with 186 additions and 40 deletions

View File

@ -6429,7 +6429,7 @@ begin
end
function to_ansi_str(s);
begin
if IsTextUTF8(s) then return UTF8toansi(s);
if IsTextUTF8(s)=1 then return UTF8toansi(s);
return s;
end
function ReWriteString(fn,d);

View File

@ -1084,6 +1084,8 @@ type tsgtkapi = class(tgtkapis)
sy := rec[3]-3-ht;
end
cairo_save(hdc);
x := gtk_object_get_data(hdc,"viewport.x");
y := gtk_object_get_data(hdc,"viewport.y");
reci := array(rec[0]+x,rec[1]+y,rec[2]+x,rec[3]+y);
cairo_clip_rec(hdc,reci);
r := TextOutexA(hdc,sx,sy,txt,slen);
@ -2460,47 +2462,80 @@ type tsgtkapi = class(tgtkapis)
////////////////////////clipboard////////////////////////////////////////
function OpenClipboard(h);
begin
return true;
global g_clipbaord_ptr;
g_clipbaord_ptr := gtk_clipboard_get(69);
return (g_clipbaord_ptr>0 or g_clipbaord_ptr<0);
end
function EmptyClipboard();
begin
c := gtk_clipboard_get(69);
gtk_clipboard_clear(c);
return 0;
end
function CloseClipboard();
begin
global g_clipbaord_ptr;
g_clipbaord_ptr := nil;
return true;
end
function IsClipboardFormatAvailable(fmt);
begin
return true;
global g_clipbaord_ptr;
const CF_TEXT=0x1;
const CF_BITMAP=0x2;
c := g_clipbaord_ptr;
if not( c>0 or c<0) then return 0;
case fmt of
CF_TEXT:
begin
return gtk_clipboard_wait_is_text_available(c);
end
CF_BITMAP:
begin
gtk_clipboard_wait_is_image_available(c);
end
end
return false;
end
function getclipboardtext(clpd);
begin
c := gtk_clipboard_get(69);
r := gtk_clipboard_wait_for_text(c);
//if r then r := GtkStringToTsl(r);
//echo "\r\ncop wire:",writefile(rwraw(),"","/tmp/test12.txt",0,length(r),r);
return r;
global g_clipbaord_ptr;
c := g_clipbaord_ptr;
r := gtk_clipboard_wait_for_text(c);
return GtkStringToTsl(r);
end
function setclipboardtext(clbd,s);
begin
c := gtk_clipboard_get(69);
global g_clipbaord_ptr;
c := g_clipbaord_ptr;
if ifstring(s) and s then
begin
//gs := TslStringToGtk(s);
//return gtk_clipboard_set_text(c,gs,length(gs));
return gtk_clipboard_set_text(c,s,length(s));
if (IsTextUTF8(s)<>1) then
begin
gs := TslStringToGtk(s);
end
else gs := s;
return gtk_clipboard_set_text(c,gs,length(gs));
end else
if ifnil(s) then gtk_clipboard_set_text(c,"",0);
if ifnil(s) then
begin
gtk_clipboard_clear(c);
end
return 1;
end
function getclipboardbmp();
begin
return 0;
global g_clipbaord_ptr;
c := g_clipbaord_ptr;
return gtk_clipboard_wait_for_image(c);
end
function setclipboardbmp();
function setclipboardbmp(h);
begin
return false;
global g_clipbaord_ptr;
c := g_clipbaord_ptr;
gtk_clipboard_set_image(c,h);
return 1;
//return false;
end
//////////////////////////////////////end clip board //////////////////////////////////
@ -3652,7 +3687,12 @@ type tgtkapis = class() //gtk
//function tsl_gtk_pipread(p:pointer;var msg:string;ct:integer):integer;cdecl;external "plugin/libTSLUIL.so";
//function tsl_gtk_kill(p:pointer;sig:integer):integer;cdecl;external "plugin/libTSLUIL.so";
//function tsl_gtk_createprocessa(exe:string; cmd : array of string; ev : array of string;var pw:pointer):pointer;cdecl;external "plugin/libTSLUIL.so";
//
procedure g_free(mem:pointer);
begin
_f_ := static procedure(mem:pointer); cdecl; external getfuncptrbyname(0,functionname()); ////libglib-2.0.so
return ##_f_(g_free);
end
/////////////////////////////////timer///////////////////////////////
function g_timeout_add(interval:integer; _function:pointer; d:pointer):integer;
begin
@ -3664,24 +3704,59 @@ type tgtkapis = class() //gtk
_f_ := static procedure(timeout_handler_id:integer); cdecl; external getfuncptrbyname(0,functionname()); ////libglib-2.0.so
return ##_f_(timeout_handler_id);
end
function p_to_str(p); //读取字符串并销毁原来的ptr
begin
r := "";
if p then
begin
r := ReadStringFromPtr(p);
g_free(p);
end
return r;
end
//////////////////////////clipboard/////////////////////
procedure gtk_clipboard_set_text(c:pointer;s:string;len:integer);
begin
_f_ := static procedure(c:pointer;s:string;len:integer);cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(c,s,len);
end
procedure gtk_clipboard_set_image(c:pointer;pixbuf:pointer);
begin
_f_ := static procedure(c:pointer;pixbuf:pointer);cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(c,pixbuf);
end
function gtk_clipboard_clear(clip:pointer);
begin
_f_ := static procedure(clip:pointer);cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(clip);
end
function gtk_clipboard_wait_is_text_available(clip:pointer):integer;
begin
_f_ := static function(clip:pointer):integer;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(clip);
end
function gtk_clipboard_wait_is_image_available(clip:pointer):integer;
begin
_f_ := static function(clip:pointer):integer;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(clip);
end
function gtk_clipboard_get(atm:pointer):pointer;
begin
_f_ := static function(atm:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(atm);
end
end
function gtk_clipboard_wait_for_image(clipboard:pointer):pointer;
begin
_f_ := static function(clipboard:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(clipboard);
end
function gtk_clipboard_wait_for_text(clipboard:pointer):string;
begin
_f_ := static function(clipboard:pointer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(clipboard);
end
_f_ := static function(clipboard:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
p := ##_f_(clipboard);
return p_to_str(p);
end
////////////////////////
function gtk_events_pending():integer;
begin
@ -4153,7 +4228,8 @@ type tgtkapis = class() //gtk
function gtk_selection_data_get_text(d:pointer);
begin
_f_ := static function(d:pointer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(d);
p := ##_f_(d);
return p_to_str(p);
end
function gtk_selection_data_get_data_type(d:pointer);
begin
@ -4161,14 +4237,14 @@ type tgtkapis = class() //gtk
return ##_f_(d);
end
function gdk_atom_name(a:pointer);
function gdk_atom_name(a:pointer):string;
begin
_f_ := static function(d:pointer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(d);
_f_ := static function(d:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
p := ##_f_(d);
return p_to_str(p);
end
function gtk_drag_dest_find_target(w:pointer;ctx:pointer;tglist:pointer);
begin
begin
_f_ := static function(w:pointer;ctx:pointer;tglist:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(w,ctx,tglist);
end
@ -4438,8 +4514,9 @@ type tgtkapis = class() //gtk
end
function gtk_text_buffer_get_text(b:pointer;start:pointer;ed:pointer;ic:integer):string;
begin
_f_ := static function(b:pointer;start:pointer;ed:pointer;ic:integer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(b,start,ed,ic);
_f_ := static function(b:pointer;start:pointer;ed:pointer;ic:integer):pointer;cdecl;external getfuncptrbyname(0,functionname());
p := ##_f_(b,start,ed,ic);
return p_to_str(p);
end
function gtk_text_buffer_get_selection_bounds(b:pointer;st:pointer;ed:pointer):integer;
begin
@ -5485,8 +5562,9 @@ type tgtkapis = class() //gtk
end
function gtk_file_chooser_get_filename(p:pointer):string;
begin
_f_ := static function(p:pointer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(p);
_f_ := static function(p:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
pp := ##_f_(p);
return p_to_str(pp);
end
function gtk_file_chooser_set_filename(p:pointer;n:string):integer;
begin
@ -5515,14 +5593,15 @@ type tgtkapis = class() //gtk
end
function gtk_font_selection_dialog_get_font_name(d:pointer):string;
begin
_f_ := static function(d:pointer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(d);
_f_ := static function(d:pointer):pointer;cdecl;external getfuncptrbyname(0,functionname());
p := ##_f_(d);
return p_to_str(p);
end
function gtk_font_selection_dialog_get_preview_text(p:pointer):string;
{function gtk_font_selection_dialog_get_preview_text(p:pointer):string;
begin
_f_ := static function(p:pointer):string;cdecl;external getfuncptrbyname(0,functionname());
return ##_f_(p);
end
end}
procedure gtk_font_selection_dialog_set_preview_text(p:pointer;t:string);
begin
_f_ := static procedure(p:pointer;t:string); cdecl;external getfuncptrbyname(0,functionname());
@ -9418,7 +9497,7 @@ function tsl_gtk_get_thread();
begin
return static systhreadid();
end
type _gtkeventtype=class
type _gtkeventtype=class()
static const GDK_C_DEFAULT = "default";
static const GDK_C_help = "help";
@ -9671,6 +9750,39 @@ CAIRO_OPERATOR_HSL_SATURATION:=26;
CAIRO_OPERATOR_HSL_COLOR:=27;
CAIRO_OPERATOR_HSL_LUMINOSITY:=28;
}
{//selection 常量
static const GDK_SELECTION_PRIMARY =1;
//A GdkAtom representing the PRIMARY selection.
static const GDK_SELECTION_SECONDARY =2;
//A GdkAtom representing the SECONDARY selection.
static const GDK_SELECTION_CLIPBOARD =69;
//A GdkAtom representing the CLIPBOARD selection.
static const GDK_TARGET_BITMAP =5;
//A GdkAtom representing the BITMAP selection target.
static const GDK_TARGET_COLORMAP =7;
//A GdkAtom representing the COLORMAP selection target.
static const GDK_TARGET_DRAWABLE =17;
//A GdkAtom representing the DRAWABLE selection target.
static const GDK_TARGET_PIXMAP =20;
//A GdkAtom representing the PIXMAP selection target.
static const GDK_TARGET_STRING =31;
//A GdkAtom representing the STRING selection target.
static const GDK_SELECTION_TYPE_ATOM =4;
//A GdkAtom representing the ATOM selection type.
static const GDK_SELECTION_TYPE_BITMAP =5;
//A GdkAtom representing the BITMAP selection type.
static const GDK_SELECTION_TYPE_COLORMAP =7;
//A GdkAtom representing the COLORMAP selection type.
static const GDK_SELECTION_TYPE_DRAWABLE =17;
//A GdkAtom representing the DRAWABLE selection type.
static const GDK_SELECTION_TYPE_INTEGER =19;
//A GdkAtom representing the INTEGER selection type.
static const GDK_SELECTION_TYPE_PIXMAP =20;
//A GdkAtom representing the PIXMAP selection type.
static const GDK_SELECTION_TYPE_WINDOW =33;
//A GdkAtom representing the WINDOW selection type.
static const GDK_SELECTION_TYPE_STRING =31;
}
/////////////////////////////·ÇapiÉÏÃæµÄexternal º¯Êý///////////////////////////////////////////////////
function gtk_init_check(argc:string;argcv:string):integer;
begin

View File

@ -28,6 +28,7 @@ function intersectrect(rec1,rec2,irec);
function bitcombination(s,v,f);
function IsTextUTF8(str);
function isTextGBK(str);
function exportjsonformat(d,tbw,ct);
function get_resource_by_name(n,full);
//****************************
@ -3635,6 +3636,39 @@ function TS_ModulePath():string;cdecl;external {$ifdef linux}"libTSSVRAPI.so"{$
function TS_ExecPath():string;cdecl;external {$ifdef linux}"libTSSVRAPI.so"{$else}"TSSVRAPI.dll"{$endif} name "TS_ExecPath";
function TS_GetAppPath():string;cdecl;external {$ifdef linux}"libTSSVRAPI.so"{$else}"TSSVRAPI.dll"{$endif} name "TS_GetAppPath";
function TS_GetIniPath(t:integer;iname:string):string;cdecl;external {$ifdef linux}"libTSSVRAPI.so"{$else}"TSSVRAPI.dll"{$endif} name "TS_GetIniPath";
function isTextGBK(data) //gbk编码
begin
if not ifstring(data) then return 0;
i := 1;
len := length(data);
while (i <= len) do
begin
ordi := ord(data[i]);
if (ordi <= 0x7f) then
begin
//编码小于等于127,只有一个字节的编码兼容ASCII
i++;
continue;
end else
begin
ordi2 := ord(data[i + 1]);
//大于127的使用双字节编码
if (ordi >= 0x81 and
ordi <= 0xfe and
ordi2 >= 0x40 and
ordi2 <= 0xfe and
ordi2 <> 0xf7) then
begin
i += 2;
continue;
end else
begin
return false;
end
end
end
return true;
end
function IsTextUTF8(str)
begin
{utf8规则
@ -3643,7 +3677,7 @@ begin
三字节 1110xxxx 10xxxxxx 10xxxxxx
四字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
五字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
<EFBFBD>ヨスレ 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
六字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
}
// 0 为ansi 编码,1 为utf8编码 -1 不能确定什么编码
nBytes := 0; //UFT8可用1-6个字节编码,ASCII用一个字节