3

Like question in title says. I would like a text field that should be possible to select just like text you would in a browser, but not editable.

I tried setting the Enable property to off and inactive. I also tried using both text fields and edit texts and static text boxes. Edit text boxes with Enable set to on allows me to select it, but not copy it. I don't know if there could be OS specific issues (Ubuntu 16.04) since matlab seems to like to use other key shortcuts for copy paste in Linux. Funny enough, when I right-click the selectable text, nothing happens.

It should work in GUIDE, so I imagine there is just some setting in properties manager I missed.

user1661303
  • 539
  • 1
  • 7
  • 20
  • For the right-click behaviour, you have to code it yourself (e.g. add a [`uicontextmenu`](https://mathworks.com/help/matlab/ref/uicontextmenu.html) to your text box). For your other question, if your final goal is to just copy the full text to the clipboard, there is a nice trick described [here](https://mathworks.com/help/matlab/ref/uicontrol.html#f52-578590) (in the "tip" section). Otherwise no, there is no simple way to react/prevent text modification on the fly, matlab will only fire an event (letting you interact) once the focus leaves the text box (or enter is pressed). – Hoki Oct 21 '16 at 16:35
  • An inactive editbox is the way to go imho. You can change the shortcut keys to be windows-like in `Settings >> MATLAB >> Keyboard >> Shortcuts >> Active settings: Windows Default Set` (it's probably on "Emact Default Set" at the moment). – Dev-iL Oct 21 '16 at 16:43

1 Answers1

2

There is a way to achieve that but (i) not directly in GUIDE, and (ii) not even in pure MATLAB. This solution relies on undocumented underlying java properties of the MATLAB edit box.

To be able to access these properties, you first need to download the finjobj utility from the file exchange.

Armed with that, the following code works fine on MATLAB R2015a, you may have to adjust or fiddle for other MATLAB versions.

Code for demo_editbox_interception.m:

function h = demo_editbox_interception

% create minimalistic text box
h.fig = figure('Toolbar','none','Menubar','none') ;
h.ht = uicontrol('style','edit','Position',[20 20 300 30],...
    'String','This is a multi-word test string');

% find the handle of the underlying java object
h.jt = findjobj(h.ht) ;
% disable edition of the java text box
h.jt.Editable = 0 ;
% => This leaves you with a text box where you can select all or part of
% the text present, but you cannot modify the text.

%% Now add copy functionality:
% choose which functionality you want from the options below: 
% set(h.ht,'ButtonDownFcn',@copy_full_string)
set(h.ht,'ButtonDownFcn',{@copy_selection,h.jt} )


function str = copy_full_string(hobj,~)
    % this function will copy the ENTIRE content of the edit box into the
    % clipboard. It does NOT rely on the undocumented java properties
    str = get(hobj,'String') ;
    clipboard('copy',str)
    disp(['String: "' str '" copied into the clipboard.'] )


function str = copy_selection(hobj,evt,jt)
    % this function will copy the SELECTED content of the edit box into the
    % clipboard. It DOES rely on the undocumented java properties
    str = jt.SelectedText ;
    clipboard('copy',str)
    disp(['String: "' str '" copied into the clipboard.'] )

You can see it in effect. Select text with the left mouse button, and fire the callback (the copy) with the right button:

demo gif

Hoki
  • 11,637
  • 1
  • 24
  • 43