Re: NSTextView and Syntax Highlighting

2014-12-20 Thread Charles Jenkins
Martin, Ecir, and Quincey:  

Thank you all! All three answers turned out to be helpful. (Quincey, you led me 
to learn about how to get attributed substrings, should I ever need to do it.)

Martin, your chief difference from my code seems be calling the layout 
manager’s ensureGlyphsForCharacterRange: in order to synch up the layout 
manager with the current content of text storage. I took that and ran with it. 
Here is the resulting code that works for me, which I donate to the public 
domain in case anyone else finds it useful.

The methods below are found in a subclass of NSTextView:  

/// Set temporary foreground color for special characters in the given range  

-(void)setTemporaryForegroundColor:(NSColor*)color  
  forSpecialCharacters:(NSCharacterSet*)characterSet
   inRange:(NSRange)range
{
  HighlightInfo* hi = [[HighlightInfo alloc] initWithColor:color
  characterSet:characterSet
 range:range];

  // If we don't delay for a half-second, Apple's Smart Quotes stop working.  
  // I think this is a bug: why should altering temporary attributes lock out 
substitution?

  static double smartQuoteDelay = 0.5;  

  [self performSelector:@selector(setTemporaryForegroundColor:)  
 withObject:hi
 afterDelay:smartQuoteDelay];
}

/// Set temporary foreground color according to information passed in 
HighlightInfo  
/// @attention Keep this private and always call via 
performSelector:withObject:afterDelay:

-(void)setTemporaryForegroundColor:(HighlightInfo*)hi  
{
  NSLayoutManager* lm = self.layoutManager;
  NSTextStorage* ts = self.textStorage;

  NSRange searchRange = hi.range;  
  NSUInteger beyondIx = searchRange.location + searchRange.length;

  // Synchronize layoutManager with textStorage: otherwise temp attribute 
ranges don't agree  

  [lm ensureGlyphsForCharacterRange:searchRange];  
  [lm removeTemporaryAttribute:NSForegroundColorAttributeName
 forCharacterRange:searchRange];

  // Repeatedly search and apply temporary attributes to special characters in 
search range  

  NSString* str = ts.string;  

  //NSLog( @"Searching for special chars in: %@", [str 
substringWithRange:searchRange]);  

  NSCharacterSet* set = hi.characterSet;  
  NSDictionary* tempAttrs = @{ NSForegroundColorAttributeName : hi.color };

  while ( true ) {  
NSRange foundRange = [str rangeOfCharacterFromSet:set
  options:0
range:searchRange];
if ( foundRange.location == NSNotFound ) {
  break;
}
[lm setTemporaryAttributes:tempAttrs forCharacterRange:foundRange];
NSUInteger startIx = foundRange.location + foundRange.length;
if ( startIx >= beyondIx ) {
  break;
}
searchRange = NSMakeRange( startIx, beyondIx - startIx );
  }

}

The definition of HighlightInfo should be obvious, but here it is anyway:

@interface HighlightInfo : NSObject  

@property (nonatomic) NSColor* color;  
@property (nonatomic) NSCharacterSet* characterSet;
@property (nonatomic) NSRange range;

-(instancetype)initWithColor:(NSColor*)color  
characterSet:(NSCharacterSet*)characterSet
   range:(NSRange)range;

@end

Cheers and happy holidays to all!

—

Charles Jenkins

___

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: system sounds in iOS

2014-12-20 Thread Kyle Sluder
On Sat, Dec 20, 2014, at 12:12 AM, Donald Hall wrote:
> Assuming we aren’t allowed to access the system sound files, can anyone
> recommend a safe place to get free-to-use sound files?

For the sound you want, you might consider playing around with the Mac
version of GarageBand yourself. There might be a suitable effect
included in the bundled library (which is licensed to you royalty-free,
so I believe it should* be good to use those sounds in an app), or you
might be able to gin one up by time-compressing a longer sound or using
one of GB's built-in synthesizers/generators.

* I am not a lawyer, nor am I making statements on behalf of Apple
Legal.

--Kyle Sluder

___

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: system sounds in iOS

2014-12-20 Thread Carl Hoefs
On Dec 19, 2014, at 11:12 PM, Donald Hall  wrote:

> I want to play a sound indicating that the user has tried to type a 
> non-allowed character (e.g. a letter where a number is needed) in conjunction 
> with "textField:textField shouldChangeCharactersInRange:replacementString:” I 
> was thinking of “tink.caf” or “tock.caf”


#import 
#import 

AudioServicesPlaySystemSound(1057);

-Carl


___

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: What triggers -applicationDidChangeScreenParameters: delegate method?

2014-12-20 Thread Bill Cheeseman

> On Dec 11, 2014, at 4:31 AM, Bill Cheeseman  wrote:
> 
> The problem is that the delegate method is being triggered now at every 
> single launch of my application, but the screen resolution and monitor 
> arrangement have not changed. The user's ability to choose a new location for 
> the window has been overwhelmed by this inappropriate triggering of the 
> delegate method at every launch.



Further testing shows that the primary screen's visibleFrame property does in 
fact change between -awakeFromNib and the triggering of the 
-applicationDidChangeScreenParameters: delegate method a moment later, even 
though I have done nothing explicit in my code to cause a change to the screen 
size. The primary screen's visibleFrame in -awakeFromNib is {{0, 40}, {2560, 
1377}}, but by the time the delegate method is triggered it has changed to {{0, 
41}, {2560, 1376}}. In other words, the bottom of the visibleFrame has moved up 
one point, making the screen's visibleFrame height one point shorter.

This suggests that Ken was correct in proposing that the addition of my 
application's icon to the Dock at launch changed the Dock's size (by increasing 
the height of the Dock by one point), which would cause the change in 
visibleFrame and trigger the delegate method. (I am a little surprised, 
however, that the change in the Dock's size was not apparent to my application 
at the time of -awakeFromNib. Maybe I should test in 
-applicationDidFinishLaunching: instead.)

So I can avoid the problem in my application by changing the filtering code in 
the delegate method to test for a change in the screen's frame property, 
previously saved, instead of its visibleFrame property. I only care about major 
changes in the screens array or screen sizes, namely, those caused by user 
changes in System Preferences connecting or disconnecting monitors. I don't 
care about  minor changes in the Dock's size or changes in its location because 
my application design calls for the window to maintain a larger distance from 
any edge of any screen at all times.

-- 

Bill Cheeseman - b...@cheeseman.name

___

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: system sounds in iOS

2014-12-20 Thread Maxthon Chan
There is nothing preventing you from pointing a microphone and hitting record 
button. If you can find some device, preferably mechanical, that produces those 
sounds point a microphone at it and use Garageband to touch it up a little bit.

If you have you can also make your own sound effects with your preferred 
instrument. I have kept my MIDI keyboard just for this purpose, creating alert 
chords for my apps.

> On Dec 21, 2014, at 02:28, Carl Hoefs  wrote:
> 
> On Dec 19, 2014, at 11:12 PM, Donald Hall  wrote:
> 
>> I want to play a sound indicating that the user has tried to type a 
>> non-allowed character (e.g. a letter where a number is needed) in 
>> conjunction with "textField:textField 
>> shouldChangeCharactersInRange:replacementString:” I was thinking of 
>> “tink.caf” or “tock.caf”
> 
> 
> #import 
> #import 
> 
> AudioServicesPlaySystemSound(1057);
> 
> -Carl
> 
> 
> ___
> 
> 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/max%40maxchan.info
> 
> This email sent to m...@maxchan.info



smime.p7s
Description: S/MIME cryptographic signature
___

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

view problems

2014-12-20 Thread H Miersch
hi.
i have a window with a custom view in it. I add another view in code and then i 
add a couple of constraints to centre the new view in its superview. so far so 
good.

if i DON'T call [self setTranslatesAutoresizingMaskIntoConstraints:NO]; on my 
subview, it appears, but the superview (and wit it the window) is reduced in 
size to the size of the subview, and i can't make the window bigger using the 
mouse.

if I DO call [self setTranslatesAutoresizingMaskIntoConstraints:NO]; on the 
subview, the window and its view behave normally, but the subview doesn't 
appear, its drawrect method is not called, nothing.

so what am i doing wrong?


signature.asc
Description: Message signed with OpenPGP using GPGMail
___

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: view problems

2014-12-20 Thread Quincey Morris
On Dec 20, 2014, at 12:08 , H Miersch  wrote:
> 
> if I DO call [self setTranslatesAutoresizingMaskIntoConstraints:NO]; on the 
> subview, the window and its view behave normally, but the subview doesn't 
> appear, its drawrect method is not called, nothing.
> 
> so what am i doing wrong?

It sure sounds like the custom view is having its size forced to zero because 
it has no size (or other) constraints that keep this from happening.


___

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: view problems

2014-12-20 Thread H Miersch

On 20. Dec 2014, at 20:39, Quincey Morris  
wrote:

> On Dec 20, 2014, at 12:08 , H Miersch  wrote:
>> 
>> if I DO call [self setTranslatesAutoresizingMaskIntoConstraints:NO]; on the 
>> subview, the window and its view behave normally, but the subview doesn't 
>> appear, its drawrect method is not called, nothing.
>> 
>> so what am i doing wrong?
> 
> It sure sounds like the custom view is having its size forced to zero because 
> it has no size (or other) constraints that keep this from happening.

hm, i hadn't thought of that. i'll try it out and let you know.


signature.asc
Description: Message signed with OpenPGP using GPGMail
___

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: view problems

2014-12-20 Thread H Miersch

On 20. Dec 2014, at 20:39, Quincey Morris  
wrote:

> On Dec 20, 2014, at 12:08 , H Miersch  wrote:
>> 
>> if I DO call [self setTranslatesAutoresizingMaskIntoConstraints:NO]; on the 
>> subview, the window and its view behave normally, but the subview doesn't 
>> appear, its drawrect method is not called, nothing.
>> 
>> so what am i doing wrong?
> 
> It sure sounds like the custom view is having its size forced to zero because 
> it has no size (or other) constraints that keep this from happening.

it looks like that didn't help. maybe i've made a mistake with the size 
constraints, or maybe there's something else wrong.

i have a method that calculates the required height for the subview. here's the 
code i added to that method:

[self setFrameSize:NSMakeSize(width, height)];
if (heightConstraint) {
[self removeConstraint:heightConstraint];
heightConstraint = nil;
}
if (widthConstraint) {
[self removeConstraint:widthConstraint];
widthConstraint = nil;
}
heightConstraint = [NSLayoutConstraint constraintWithItem:self

attribute:NSLayoutAttributeHeight

relatedBy:NSLayoutRelationEqual

   toItem:nil

attribute:NSLayoutAttributeNotAnAttribute

   multiplier:1

 constant:height];
[self addConstraint:heightConstraint];

widthConstraint = [NSLayoutConstraint constraintWithItem:self

attribute:NSLayoutAttributeWidth

relatedBy:NSLayoutRelationEqual

   toItem:nil

attribute:NSLayoutAttributeNotAnAttribute

   multiplier:1

 constant:width];
[self addConstraint:widthConstraint];

did i screw up? wouldn't surprise me, since this is my first project where i 
use constraints in code…

come to think of it, should i add the constraints to the superview instead of 
self? gonna try that...


signature.asc
Description: Message signed with OpenPGP using GPGMail
___

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: view problems

2014-12-20 Thread Quincey Morris
On Dec 20, 2014, at 13:09 , H Miersch  wrote:
> 
>  should i add the constraints to the superview instead of self?

The height and width constraints should be on the view itself.

I’d be more concerned about timing: Did you add constraints before or after 
layout had occurred, and if after do you need to trigger a re-layout?

Also, your ‘setFrameSize’ method suggests you *might* still be setting the view 
frame somewhere. You don’t want to do that under auto-layout.

If you’re using Xcode 6, you also have debugging assistance in the view 
hierarchy display. (It’s a button down near the continue/step controls in the 
debugger pane.)

You also have -[NSWindow visualizeConstraints:] to help figure out what’s going 
on.



___

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: view problems

2014-12-20 Thread Ken Thomases
On Dec 20, 2014, at 2:08 PM, H Miersch  wrote:

> i have a window with a custom view in it. I add another view in code and then 
> i add a couple of constraints to centre the new view in its superview. so far 
> so good.
> 
> if i DON'T call [self setTranslatesAutoresizingMaskIntoConstraints:NO]; on my 
> subview, it appears, but the superview (and wit it the window) is reduced in 
> size to the size of the subview, and i can't make the window bigger using the 
> mouse.

You've shown the constraints for the size of the subview.  What are the 
constraints for centering it in its superview?  Is the superview the window's 
contentView?  If not, what are the constraints on the superview relative to 
_its_ superview?

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: view problems

2014-12-20 Thread Quincey Morris
On Dec 20, 2014, at 16:10 , Ken Thomases  wrote:
> 
> You've shown the constraints for the size of the subview.

There was an exchange in this thread that I just realized wasn’t posted to the 
list. I *think* this was accidental on the OP’s part, so I’ll copy the messages 
here, since they are (IMO) crucial to the issue:

> On Dec 20, 2014, at 13:50 , H Miersch  wrote:
> 
> On 20. Dec 2014, at 21:26, Quincey Morris 
>  > wrote:
> 
>> I’d be more concerned about timing: Did you add constraints before or after 
>> layout had occurred, and if after do you need to trigger a re-layout?
> 
> not sure when layout occurs, but as i say below, that method is called from 
> drawrect. and i added several setneedslayout calls after the creation of 
> those constraints. didn't help.
> 
>> Also, your ‘setFrameSize’ method suggests you *might* still be setting the 
>> view frame somewhere. You don’t want to do that under auto-layout.
> 
> drawrect calls the method that determines the required size of the view
> that method then finds out what size the view should be and sets that size 
> using setframesize. BTW, that is the only place in that class that i call any 
> method with set frame in its name. 
> then the method sets the size constraints to the same size. 
> 
> so are you saying i should remove that setframesize and rely on the 
> constraints?
> 
>> If you’re using Xcode 6, you also have debugging assistance in the view 
>> hierarchy display. (It’s a button down near the continue/step controls in 
>> the debugger pane.)
> 
> no, it's Xcode 5.1.1
> 
>> You also have -[NSWindow visualizeConstraints:] to help figure out what’s 
>> going on.
> 
> i'll see where that takes me...


and then:

> On Dec 20, 2014, at 14:33 , Quincey Morris 
>  wrote:
> 
> On Dec 20, 2014, at 13:50 , H Miersch  > wrote:
>> 
>> not sure when layout occurs, but as i say below, that method is called from 
>> drawrect
> 
> From -[NSView drawRect:]?? You most definitely should not be computing the 
> view size there, let alone resizing the view or setting constraints.
> 
> I suppose it would be fine to compute a *future* view size there, if that’s 
> where the calculation logic is, but it’s far too late make the view that size 
> *this* time. IAC, it would be better to factor out the calculation and do it 
> somewhere that’s no so far too late.
> 
>> drawrect calls the method that determines the required size of the view
>> that method then finds out what size the view should be and sets that size 
>> using setframesize. BTW, that is the only place in that class that i call 
>> any method with set frame in its name. 
> 
> If this is -[NSView setFrameSize:] (as opposed to a custom method that merely 
> captures the size in some custom instance variables), then it’s still setting 
> the frame, which you must not do when using auto-layout. The whole purpose of 
> auto-layout is to set the frame for you.



___

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: view problems

2014-12-20 Thread H Miersch

On 20. Dec 2014, at 22:33, Quincey Morris  
wrote:

> On Dec 20, 2014, at 13:50 , H Miersch  wrote:
>> 
>> not sure when layout occurs, but as i say below, that method is called from 
>> drawrect
> 
> From -[NSView drawRect:]?? You most definitely should not be computing the 
> view size there, let alone resizing the view or setting constraints.
> 
> I suppose it would be fine to compute a *future* view size there, if that’s 
> where the calculation logic is, but it’s far too late make the view that size 
> *this* time. IAC, it would be better to factor out the calculation and do it 
> somewhere that’s no so far too late.

ok, i've taken that method call out of drawrect: and instead i call the method 
every time one of the parameters changes. and now it works. thanks.





signature.asc
Description: Message signed with OpenPGP using GPGMail
___

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