Re: switching text field between editable and non-editable

2017-06-01 Thread J.E. Schotsman

Thanks to Ken & Charles for helping me solve this.
My code now looks like

if makeEditable
{
if myTextField.my_isFirstResponder
{
myTextField.isEditable.window?.my_commitEditing()
myTextField.isEditable.window?.selectPreviousKeyView(nil)
}
myTextField.isEditable = false
myTextField.isBezeled = false
myTextField.drawsBackground = false
}
else
{
myTextField.isEditable = true
myTextField.isBezeled = true
myTextField.drawsBackground = true
}

For a text field my_isFirstResponder (extension on NSView) returns true if it 
is the delegate of the first responder.
my_commitEditing (extension on NSWindow) finds the active text field as the 
delegate of the first responder and calls activeTextField.endEditing( 
currentEditor )

Jan E.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: switching text field between editable and non-editable

2017-05-31 Thread Jonathan Mitchell

> On 31 May 2017, at 21:13, Ken Thomases  wrote:
> 
> On May 31, 2017, at 1:51 PM, J.E. Schotsman  wrote:
>> 
>> I have a hard time achieving what the message title says.
>> Depending on user settings I want a text field to be either editable or 
>> non-editable.

The following works for me.

@implementation NSTextField (CocoaDev)

- (void)bp_styleAsEditableTextField
{
[self setSelectable:YES];
[self setEditable:YES];
[self setDrawsBackground:YES];
[self setBezeled:YES];
[self.cell setScrollable:YES]; // required so that text scrolls 
horizontally when editing
[self.cell setWraps:NO];
[self.cell setLineBreakMode:NSLineBreakByWordWrapping]; // setting to 
truncate here kills the horiz scrolling during edit

[self setContentCompressionResistancePriority:1 
forOrientation:NSLayoutConstraintOrientationHorizontal];
}

- (void)bp_styleAsEditableWrappingTextField
{
[self setSelectable:YES];
[self setEditable:YES];
[self setDrawsBackground:YES];
[self setBezeled:YES];

[self.cell setLineBreakMode:NSLineBreakByWordWrapping];
}

- (void)bp_styleAsLabel
{
[self setSelectable:NO];
[self setEditable:NO];
[self setDrawsBackground:NO];
[self setBezeled:NO];

[self.cell setLineBreakMode:NSLineBreakByTruncatingTail]; // truncate the 
tail
[self setContentCompressionResistancePriority:1 
forOrientation:NSLayoutConstraintOrientationHorizontal];
}

- (void)bp_styleAsWrappingLabel
{
[self setSelectable:NO];
[self setEditable:NO];
[self setDrawsBackground:NO];
[self setBezeled:NO];

[self.cell setLineBreakMode:NSLineBreakByWordWrapping];
}

@end
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: switching text field between editable and non-editable

2017-05-31 Thread Ken Thomases
On May 31, 2017, at 1:51 PM, J.E. Schotsman  wrote:
> 
> I have a hard time achieving what the message title says.
> Depending on user settings I want a text field to be either editable or 
> non-editable.
> 
> I’ve tried this:
> 
> if makeEditable
>   {   myTextField.isEditable = false
>   myTextField.drawsBackground = false }
> else
>   {   myTextField.isEditable = true
>   myTextField.drawsBackground = true }
> 
> Setting the editable property has the desired effect, but setting 
> drawsBackground has no effect.
> Also I need to move the focus to another control (if myTextField has focus) 
> or else the insertion point indicator keeps blinking.
> I’ve tried this:
> 
>   if  myTextField.window!.firstResponder === myTextField
>   {
>   myTextField.window!.selectPreviousKeyView(nil)
>   }
> 
> but no cigar.

Well, the window's first responder will never be the text field.  When a text 
field has focus, it's not actually the text field that is first responder.  A 
special text _view_, known as the field editor, is added to the window's view 
hierarchy in front of the text field and it is the actual first responder and 
handles the editing.  If you need to know if a given text field has the focus, 
you need to use code similar to that shown in Listing 3-8 under Determining 
First-Responder Status[1] in the Cocoa Event Handling Guide: Event Handling 
Basics.

If you want to end editing, you should use code similar to that shown in the 
docs for the endEditing(for:) method[2] of NSWindow.  You can try using 
selectPreviousKeyView() instead of the call to makeFirstResponder(_:) in those 
docs, but the problem is that it doesn't return whether it was successful or 
not.  So, you would try it and then check the first responder again.

[1] 
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/EventOverview/EventHandlingBasics/EventHandlingBasics.html#//apple_ref/doc/uid/1060i-CH5-SW23
[2] https://developer.apple.com/reference/appkit/nswindow/1419469-endediting

> firstResponder always is some button even when I see the insertion point 
> indicator blinking.

That seems unlikely.

Regards,
Ken

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: switching text field between editable and non-editable

2017-05-31 Thread Charles Srstka
> On May 31, 2017, at 2:18 PM, J.E. Schotsman  wrote:
> 
>> On 31 May 2017, at 20:59, Charles Srstka > > wrote:
>> 
>> Does setting its needsDisplay property to true have any effect?
>> 
>> Charles
> 
> No. I’ve also tried 
> 
> myTextField.superview!.setNeedsDisplay( myTextField.frame ) 
> 
> in order to get the background redrawn but again I keep seeing the white text 
> background.

Try setting isBordered and isBezeled to false as well and you should probably 
get a clear background.

Note that this won’t work if the insertion point is in the text field, because 
when that’s the case, you’re actually seeing the field editor, not the text 
field, and the field editor will still have the white background. So you 
probably want to end editding first. If you’re using bindings to populate the 
field, this is easy; just send commitEditing() to the NSObjectController or 
NSViewController that the text field is bound to. Otherwise, you’ll have to do 
it the old-fashioned way with NSWindow.makeFirstResponder(_:) (or 
NSWindow.endEditing(for:) if that won’t work for whatever reason).

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: switching text field between editable and non-editable

2017-05-31 Thread J.E. Schotsman

> On 31 May 2017, at 20:59, Charles Srstka  wrote:
> 
> Does setting its needsDisplay property to true have any effect?
> 
> Charles

No. I’ve also tried 

myTextField.superview!.setNeedsDisplay( myTextField.frame ) 

in order to get the background redrawn but again I keep seeing the white text 
background.

Jan E.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: switching text field between editable and non-editable

2017-05-31 Thread Charles Srstka
> On May 31, 2017, at 1:51 PM, J.E. Schotsman  wrote:
> 
> Hello,
> 
> I have a hard time achieving what the message title says.
> Depending on user settings I want a text field to be either editable or 
> non-editable.
> 
> I’ve tried this:
> 
> if makeEditable
>   {   myTextField.isEditable = false
>   myTextField.drawsBackground = false }
> else
>   {   myTextField.isEditable = true
>   myTextField.drawsBackground = true }
> 
> Setting the editable property has the desired effect, but setting 
> drawsBackground has no effect.

Does setting its needsDisplay property to true have any effect?

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

switching text field between editable and non-editable

2017-05-31 Thread J.E. Schotsman
Hello,

I have a hard time achieving what the message title says.
Depending on user settings I want a text field to be either editable or 
non-editable.

I’ve tried this:

if makeEditable
{   myTextField.isEditable = false
myTextField.drawsBackground = false }
else
{   myTextField.isEditable = true
myTextField.drawsBackground = true }

Setting the editable property has the desired effect, but setting 
drawsBackground has no effect.
Also I need to move the focus to another control (if myTextField has focus) or 
else the insertion point indicator keeps blinking.
I’ve tried this:

if  myTextField.window!.firstResponder === myTextField
{
myTextField.window!.selectPreviousKeyView(nil)
}

but no cigar. firstResponder always is some button even when I see the 
insertion point indicator blinking.
Can some please shed some light on this?

TIA

Jan E.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com