Clearing Clipboard

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Clearing Clipboard

Postby smd » Mon Aug 10, 2015 2:37 am

Using Firemonkey, is there a way to clear the clipboard directly? I thought I saw a clear clipboard command somewhere but cannot find it. All I can find is set and get. So currently I am clearing the clipboard by setting it to a single space character. Tried setting it to an empty string but it does not work, needs at least one character.
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas

Re: Clearing Clipboard

Postby HsiaLin » Mon Aug 10, 2015 11:51 am

I dont know about FM but this is the common way.


Code: Select all
        #include <Clipbrd.hpp>

        OpenClipboard(NULL);
        EmptyClipboard();
        CloseClipboard();
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 297
Joined: Sun Jul 08, 2007 6:29 pm

Re: Clearing Clipboard

Postby rlebeau » Mon Aug 10, 2015 7:28 pm

HsiaLin wrote:I dont know about FM but this is the common way.


Those are Win32 API function calls for Windows only. Other platforms, like OSX, also support a concept of a clipboard, which FireMonkey wraps.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1508
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Clearing Clipboard

Postby rlebeau » Mon Aug 10, 2015 7:37 pm

smd wrote:Using Firemonkey, is there a way to clear the clipboard directly?


FireMonkey does not expose that functionality at this time. You would have to access platform-specific clipboard APIs directly.

smd wrote:I thought I saw a clear clipboard command somewhere but cannot find it.


There is no such command in FireMonkey.

smd wrote:So currently I am clearing the clipboard by setting it to a single space character. Tried setting it to an empty string but it does not work, needs at least one character.


On which platforms? Which version of C++Builder? You should be able to assign an empty string on all of the supported platforms. What exactly happens when you try? What does your actual code look like?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1508
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Clearing Clipboard

Postby smd » Tue Aug 11, 2015 2:02 pm

rlebeau wrote:On which platforms? Which version of C++Builder? You should be able to assign an empty string on all of the supported platforms. What exactly happens when you try? What does your actual code look like?


Rad Studio XE8 Update 1. My development is currently on Windows 8.1, but I plan to make this a cross platform program. Have not yet tested anything on OSX, ios, or Android.

I have a TMemo component named TempEdit that I use to manipulate text away from the main TMemo component. I found that if selection length is 0 then the CopyToClipboard action decides that there is nothing to do and just does nothing. So I use my TempEdit component (could also use a TEdit or other similar, but those all are using the same clipboard methods) as such:

Code: Select all
   TempEdit->Lines->Clear();
   TempEdit->Lines->Add(L" ");
   TempEdit->SelStart = 0;
   TempEdit->SelLength = 1;
   TempEdit->CopyToClipboard();
   TempEdit->Lines->Clear();
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas

Re: Clearing Clipboard

Postby rlebeau » Tue Aug 11, 2015 4:07 pm

smd wrote:I have a TMemo component named TempEdit that I use to manipulate text away from the main TMemo component. I found that if selection length is 0 then the CopyToClipboard action decides that there is nothing to do and just does nothing.


That is correct, and is by design (and has been for a long time):

XE2:
Code: Select all
procedure TMemo.CopyToClipboard;
begin
  if SelText <> '' then
    Platform.SetClipboard(SelText);
end;


XE8:
Code: Select all
function TCustomMemoModel.HasSelection: Boolean;
begin
  Result := SelLength > 0;
end;

procedure TCustomMemo.CopyToClipboard;
var
  ClipService: IFMXClipboardService;
begin
  if Model.HasSelection and TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, ClipService) then
    ClipService.SetClipboard(Model.SelectedText);
end;


However, you can just set the clipboard directly instead:

XE2:
Code: Select all
Platform->SetClipboard(TempEdit->SelText);


XE8:
Code: Select all
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXClipboardService)))
{
    _di_IFMXClipboardService Clipboard = TPlatformServices::Current->GetPlatformService(__uuidof(IFMXClipboardService));
    Clipboard->SetClipboard(TValue::From<String>(""));
}


Or:

Code: Select all
_di_IFMXClipboardService Clipboard;
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXClipboardService), (void*)&Clipboard))
{
    Clipboard->SetClipboard(TValue::From<String>(""));
}
Last edited by rlebeau on Thu Aug 13, 2015 11:06 am, edited 2 times in total.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1508
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Clearing Clipboard

Postby smd » Tue Aug 11, 2015 4:36 pm

Code: Select all
if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXClipboardService)))
  {
   _di_IFMXClipboardService Clipboard =   TPlatformServices::Current->GetPlatformService(__uuidof(IFMXClipboardService));
  Clipboard->SetClipboard(TValue::From<String>(""));
  }


That worked.

No way I would have figured that out from the well organized clearly written documentation.
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas

Re: Clearing Clipboard

Postby Marloes » Thu Aug 20, 2015 9:59 am

I tried the XE8 code but got a "Project raised exception class $C0000005" with message 'access violation 0x0060f290: read of address 0x00000000". It happens on startup when I try to run program and the call stack is:

:0060F290 System::TMonitor::GetFieldAddress(AObject=NULL)
:0060ECA1 System::__linkproc__ ClassCreate(InstanceOrVMT=????, Alloc=????)
:00669EC7 DoCreate()
:00669F39 System::Rtti::EnsurePoolToken(TokenRef=:0018FF2C)
:0066BDBD System::Rtti::TRttiContext::Create()
:00f58d29 ; __init_exit_proc
:00f58e81 ; __wstartup

It's probably very simple to fix but I do not see why so any suggestions would be greatly appreciated.
Marloes
Active Poster
Active Poster
 
Posts: 13
Joined: Mon Dec 01, 2014 5:35 am

Re: Clearing Clipboard

Postby rlebeau » Thu Aug 20, 2015 5:22 pm

Marloes wrote:I tried the XE8 code but got a "Project raised exception class $C0000005" with message 'access violation 0x0060f290: read of address 0x00000000".


A NULL pointer is being accessed, as evident by this:

Code: Select all
System::TMonitor::GetFieldAddress(AObject=NULL)


Something is calling GetFieldAddress() on a TMonitor object and specifying a NULL pointer for its AObject parameter.

Marloes wrote:It happens on startup when I try to run program


Something in your project is creating a global TRttiContext object at startup. That is not a good idea. Can you trace into the RTL source code to find out where it is being created exactly?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1508
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Clearing Clipboard

Postby Marloes » Fri Aug 21, 2015 3:37 am

First of all, it's only if I include the Clipboard code that I get this problem. When I comment it out everything is fine. After commenting out various parts I can also conclude that the line

Clipboard->SetClipboard(TValue::From<String>(""));

is causing the problem. When commented out, no errors occur. (So maybe a compiler setting, flag or include etc needs to be changed?)

I am not good at (used to) this kind of debugging but I will try to trace it down.
Marloes
Active Poster
Active Poster
 
Posts: 13
Joined: Mon Dec 01, 2014 5:35 am

Re: Clearing Clipboard

Postby smd » Fri Aug 21, 2015 4:27 am

Sounds like your application has not yet initialized the clipboard for your application since you are trying to clear it at the start of your program. Try doing a simple clipboard operation first that will force the clipboard to initialize. That way the clipboard has something to clear. Below is an example using a TMemo component. You can also use a TEdit or any other similar text type compnent that supports clipboard operations.


Code: Select all
       TempEdit->Lines->Clear();
       TempEdit->Lines->Add(L" ");
       TempEdit->SelStart = 0;
       TempEdit->SelLength = 1;
       TempEdit->CopyToClipboard();
       TempEdit->Lines->Clear();
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas

Re: Clearing Clipboard

Postby rlebeau » Fri Aug 21, 2015 2:59 pm

smd wrote:Sounds like your application has not yet initialized the clipboard for your application since you are trying to clear it at the start of your program. Try doing a simple clipboard operation first that will force the clipboard to initialize. That way the clipboard has something to clear.


That does not really make any sense. If the clipboard is not initialized yet to be cleared, it won't be initialized yet to set something else on it, either.

I would suggest simply delaying the clearing until after the app has finished initializing itself.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1508
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Clearing Clipboard

Postby rlebeau » Fri Aug 21, 2015 3:01 pm

Marloes wrote:After commenting out various parts I can also conclude that the line

Clipboard->SetClipboard(TValue::From<String>(""));

is causing the problem. When commented out, no errors occur. (So maybe a compiler setting, flag or include etc needs to be changed?)


I don't see how that line by itself can cause the startup error you are seeing. You have to be doing something to corrupt the RTTI, or maybe you are invoking this code too soon before RTTI is fully initialized, or something.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1508
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Clearing Clipboard

Postby Marloes » Fri Aug 21, 2015 3:56 pm

That is what confuses me too. That line, the only line in my program which calls the Clipboard, is only executed when a popup menu item is clicked. It is NOT invoked at all otherwise. It seems the compiler includes something in the code because that line is there and I just do not understand it :(
Last edited by Marloes on Fri Aug 21, 2015 4:13 pm, edited 1 time in total.
Marloes
Active Poster
Active Poster
 
Posts: 13
Joined: Mon Dec 01, 2014 5:35 am

Re: Clearing Clipboard

Postby Marloes » Fri Aug 21, 2015 3:57 pm

And the frame where the line is present is not even auto-created on startup
Marloes
Active Poster
Active Poster
 
Posts: 13
Joined: Mon Dec 01, 2014 5:35 am


Return to Technical

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 8 guests