2 * Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #import "KWQLineEdit.h"
28 #import "KWQExceptions.h"
29 #import "KWQLogging.h"
30 #import "KWQTextField.h"
31 #import "WebCoreTextRendererFactory.h"
33 // This replicates constants from [NSTextFieldCell drawingRectForBounds].
34 #define VERTICAL_FUDGE_FACTOR 3
36 QLineEdit::QLineEdit()
37 : m_returnPressed(this, SIGNAL(returnPressed()))
38 , m_textChanged(this, SIGNAL(textChanged(const QString &)))
39 , m_clicked(this, SIGNAL(clicked()))
41 KWQTextField *view = nil;
44 view = [[KWQTextField alloc] initWithQLineEdit:this];
47 [view setSelectable:YES]; // must do this explicitly so setEditable:NO does not make it NO
48 KWQ_UNBLOCK_EXCEPTIONS;
51 QLineEdit::~QLineEdit()
53 KWQTextField* textField = (KWQTextField*)getView();
55 [textField invalidate];
56 KWQ_UNBLOCK_EXCEPTIONS;
59 void QLineEdit::setEchoMode(EchoMode mode)
61 KWQTextField *textField = (KWQTextField *)getView();
63 [textField setPasswordMode:mode == Password];
64 KWQ_UNBLOCK_EXCEPTIONS;
67 void QLineEdit::setCursorPosition(int)
69 // Don't do anything here.
72 int QLineEdit::cursorPosition() const
74 // Not needed. We ignore setCursorPosition().
78 void QLineEdit::setFont(const QFont &font)
80 QWidget::setFont(font);
81 KWQTextField *textField = (KWQTextField *)getView();
83 [textField setFont:font.getNSFont()];
84 KWQ_UNBLOCK_EXCEPTIONS;
87 void QLineEdit::setText(const QString &s)
89 KWQTextField *textField = (KWQTextField *)getView();
91 [textField setStringValue:s.getNSString()];
92 KWQ_UNBLOCK_EXCEPTIONS;
95 QString QLineEdit::text()
97 KWQTextField *textField = (KWQTextField *)getView();
100 NSMutableString *text = [[[textField stringValue] mutableCopy] autorelease];
101 [text replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, [text length])];
102 [text replaceOccurrencesOfString:@"\r" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, [text length])];
103 return QString::fromNSString(text);
104 KWQ_UNBLOCK_EXCEPTIONS;
109 void QLineEdit::setMaxLength(int len)
111 KWQTextField *textField = (KWQTextField *)getView();
112 [textField setMaximumLength:len];
115 bool QLineEdit::isReadOnly() const
117 KWQTextField *textField = (KWQTextField *)getView();
119 KWQ_BLOCK_EXCEPTIONS;
120 return ![textField isEditable];
121 KWQ_UNBLOCK_EXCEPTIONS;
126 void QLineEdit::setReadOnly(bool flag)
128 KWQTextField *textField = (KWQTextField *)getView();
129 KWQ_BLOCK_EXCEPTIONS;
130 [textField setEditable:!flag];
131 KWQ_UNBLOCK_EXCEPTIONS;
134 int QLineEdit::maxLength() const
136 KWQTextField *textField = (KWQTextField *)getView();
138 KWQ_BLOCK_EXCEPTIONS;
139 return [textField maximumLength];
140 KWQ_UNBLOCK_EXCEPTIONS;
145 void QLineEdit::selectAll()
147 KWQTextField *textField = (KWQTextField *)getView();
148 KWQ_BLOCK_EXCEPTIONS;
149 [textField selectText:nil];
150 KWQ_UNBLOCK_EXCEPTIONS;
153 bool QLineEdit::edited() const
155 KWQTextField *textField = (KWQTextField *)getView();
157 KWQ_BLOCK_EXCEPTIONS;
158 return [textField edited];
159 KWQ_UNBLOCK_EXCEPTIONS;
164 void QLineEdit::setEdited(bool flag)
166 KWQTextField *textField = (KWQTextField *)getView();
167 KWQ_BLOCK_EXCEPTIONS;
168 [textField setEdited:flag];
169 KWQ_UNBLOCK_EXCEPTIONS;
172 QSize QLineEdit::sizeForCharacterWidth(int numCharacters) const
174 // Figure out how big a text field needs to be for a given number of characters
175 // by installing a string with that number of characters (using "0" as the nominal
176 // character) and then asking the field's cell what the size should be.
178 KWQTextField *textField = (KWQTextField *)getView();
180 ASSERT(numCharacters > 0);
184 KWQ_BLOCK_EXCEPTIONS;
185 NSMutableString *nominalWidthString = [NSMutableString stringWithCapacity:numCharacters];
186 for (int i = 0; i < numCharacters; ++i) {
187 [nominalWidthString appendString:@"0"];
190 NSString *value = [textField stringValue];
191 int maximumLength = [textField maximumLength];
192 [textField setMaximumLength:numCharacters];
193 [textField setStringValue:nominalWidthString];
194 size = [[textField cell] cellSize];
195 [textField setMaximumLength:maximumLength];
196 [textField setStringValue:value];
197 KWQ_UNBLOCK_EXCEPTIONS;
202 int QLineEdit::baselinePosition(int height) const
204 KWQTextField *textField = (KWQTextField *)getView();
206 KWQ_BLOCK_EXCEPTIONS;
207 NSRect bounds = [textField bounds];
208 NSFont *font = [textField font];
209 return (int)ceil([[textField cell] drawingRectForBounds:bounds].origin.y - bounds.origin.y
210 + [font defaultLineHeightForFont] + [font descender]);
211 KWQ_UNBLOCK_EXCEPTIONS;
216 void QLineEdit::clicked()
221 bool QLineEdit::hasMarkedText()
223 KWQ_BLOCK_EXCEPTIONS;
224 return [[NSInputManager currentInputManager] hasMarkedText];
225 KWQ_UNBLOCK_EXCEPTIONS;
229 void QLineEdit::setAlignment(AlignmentFlags alignment)
231 KWQ_BLOCK_EXCEPTIONS;
233 KWQTextField *textField = getView();
234 [textField setAlignment:KWQNSTextAlignmentForAlignmentFlags(alignment)];
236 KWQ_UNBLOCK_EXCEPTIONS;
239 void QLineEdit::setWritingDirection(QPainter::TextDirection direction)
241 KWQ_BLOCK_EXCEPTIONS;
243 KWQTextField *textField = getView();
244 [textField setBaseWritingDirection:(direction == QPainter::RTL ? NSWritingDirectionRightToLeft : NSWritingDirectionLeftToRight)];
246 KWQ_UNBLOCK_EXCEPTIONS;
249 bool QLineEdit::checksDescendantsForFocus() const
254 NSTextAlignment KWQNSTextAlignmentForAlignmentFlags(Qt::AlignmentFlags a)
258 ERROR("unsupported alignment");
260 return NSLeftTextAlignment;
262 return NSRightTextAlignment;
263 case Qt::AlignHCenter:
264 return NSCenterTextAlignment;