mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-09-02 00:43:35 +00:00
New: selectable textfield Select All (Cmd-A, Ctrl-A) support.
Like in Cocoa, a non-editable but selectable text can now be clicked and all text can be selected with the Edit > Select All option or the associated keyboard equivalent. Just like in Cocoa this works on selectable text labels as well (text fields without a bezel).
This commit is contained in:
+52
-32
@@ -1274,17 +1274,24 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
|
||||
var wind = [self window];
|
||||
|
||||
#if PLATFORM(DOM)
|
||||
var element = [self _inputElement];
|
||||
|
||||
if ([wind firstResponder] === self)
|
||||
if ([self isEditable])
|
||||
{
|
||||
if (immediately)
|
||||
element.select();
|
||||
else
|
||||
window.setTimeout(function() { element.select(); }, 0);
|
||||
var element = [self _inputElement];
|
||||
|
||||
if ([wind firstResponder] === self)
|
||||
{
|
||||
if (immediately)
|
||||
element.select();
|
||||
else
|
||||
window.setTimeout(function() { element.select(); }, 0);
|
||||
}
|
||||
else if (wind !== nil && [wind makeFirstResponder:self])
|
||||
[self _selectText:sender immediately:immediately];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self setSelectedRange:CPMakeRange(0, _stringValue.length)];
|
||||
}
|
||||
else if (wind !== nil && [wind makeFirstResponder:self])
|
||||
[self _selectText:sender immediately:immediately];
|
||||
#else
|
||||
// Even if we can't actually select the text we need to preserve the first
|
||||
// responder side effect.
|
||||
@@ -1433,35 +1440,48 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
|
||||
|
||||
#if PLATFORM(DOM)
|
||||
|
||||
var inputElement = [self _inputElement];
|
||||
|
||||
try
|
||||
if (![self isEditable])
|
||||
{
|
||||
if ([inputElement.selectionStart isKindOfClass:CPNumber])
|
||||
{
|
||||
inputElement.selectionStart = aRange.location;
|
||||
inputElement.selectionEnd = CPMaxRange(aRange);
|
||||
}
|
||||
else
|
||||
{
|
||||
// browsers which don't support selectionStart/selectionEnd (aka IE).
|
||||
var theDocument = inputElement.ownerDocument || inputElement.document,
|
||||
existingRange = theDocument.selection.createRange(),
|
||||
range = inputElement.createTextRange();
|
||||
// No input element - selectable text field only.
|
||||
var contentView = [self layoutEphemeralSubviewNamed:@"content-view"
|
||||
positioned:CPWindowAbove
|
||||
relativeToEphemeralSubviewNamed:@"bezel-view"];
|
||||
|
||||
if (range.inRange(existingRange))
|
||||
if (contentView)
|
||||
[contentView setSelectedRange:aRange];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Input element
|
||||
var inputElement = [self _inputElement];
|
||||
|
||||
try
|
||||
{
|
||||
if ([inputElement.selectionStart isKindOfClass:CPNumber])
|
||||
{
|
||||
range.collapse(true);
|
||||
range.move('character', aRange.location);
|
||||
range.moveEnd('character', aRange.length);
|
||||
range.select();
|
||||
inputElement.selectionStart = aRange.location;
|
||||
inputElement.selectionEnd = CPMaxRange(aRange);
|
||||
}
|
||||
else
|
||||
{
|
||||
// browsers which don't support selectionStart/selectionEnd (aka IE).
|
||||
var theDocument = inputElement.ownerDocument || inputElement.document,
|
||||
existingRange = theDocument.selection.createRange(),
|
||||
range = inputElement.createTextRange();
|
||||
|
||||
if (range.inRange(existingRange))
|
||||
{
|
||||
range.collapse(true);
|
||||
range.move('character', aRange.location);
|
||||
range.moveEnd('character', aRange.length);
|
||||
range.select();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -1515,6 +1515,31 @@ var resizeTimer = nil;
|
||||
return nil;
|
||||
}
|
||||
|
||||
/*!
|
||||
Set the text selection range to the given range within the given element, which must be a child of
|
||||
this DOM window.
|
||||
*/
|
||||
- (void)setSelectedRange:(CPRange)aRange inElement:(DOMElement)anElement
|
||||
{
|
||||
if (_DOMWindow.getSelection())
|
||||
{
|
||||
var domRange = _DOMWindow.document.createRange();
|
||||
domRange.setStart(anElement.childNodes[0], aRange.location);
|
||||
domRange.setEnd(anElement.childNodes[0], CPMaxRange(aRange));
|
||||
_DOMWindow.getSelection().removeAllRanges();
|
||||
_DOMWindow.getSelection().addRange(domRange);
|
||||
}
|
||||
else if (_DOMWindow.document.selection)
|
||||
{
|
||||
var domRange = _DOMWindow.document.body.createTextRange();
|
||||
domRange.moveToElementText(anElement);
|
||||
domRange.collapse(true);
|
||||
domRange.moveStart('character', aRange.location);
|
||||
domRange.moveEnd('character', aRange.length);
|
||||
domRange.select();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
When using command (mac) or control (windows), keys are propagated to the browser by default.
|
||||
To prevent a character key from propagating (to prevent its default action, and instead use it
|
||||
|
||||
@@ -802,4 +802,9 @@ var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
|
||||
[super setFrameSize:aSize];
|
||||
}
|
||||
|
||||
- (void)setSelectedRange:(CPRange)aRange
|
||||
{
|
||||
[[[self window] platformWindow] setSelectedRange:aRange inElement:_DOMTextElement];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user