2
Copyright (C) 2011 by Mike McQuaid
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
#include "qsearchfield.h"
24
#include "moc_qsearchfield.cpp"
26
#include "qocoa_mac.h"
27
#include "utils/Logger.h"
29
#import "Foundation/NSAutoreleasePool.h"
30
#import "Foundation/NSNotification.h"
31
#import "AppKit/NSSearchField.h"
33
#include <QApplication>
36
class QSearchFieldPrivate : public QObject
39
QSearchFieldPrivate(QSearchField *qSearchField, NSSearchField *nsSearchField)
40
: QObject(qSearchField), qSearchField(qSearchField), nsSearchField(nsSearchField) {}
42
void textDidChange(const QString &text)
45
emit qSearchField->textChanged(text);
48
void textDidEndEditing()
51
emit qSearchField->editingFinished();
57
emit qSearchField->returnPressed();
60
QPointer<QSearchField> qSearchField;
61
NSSearchField *nsSearchField;
64
@interface QSearchFieldDelegate : NSObject<NSTextFieldDelegate>
67
QPointer<QSearchFieldPrivate> pimpl;
69
-(void)controlTextDidChange:(NSNotification*)notification;
70
-(void)controlTextDidEndEditing:(NSNotification*)notification;
73
@implementation QSearchFieldDelegate
74
-(void)controlTextDidChange:(NSNotification*)notification {
77
pimpl->textDidChange(toQString([[notification object] stringValue]));
80
-(void)controlTextDidEndEditing:(NSNotification*)notification {
81
// No Q_ASSERT here as it is called on destruction.
83
pimpl->textDidEndEditing();
85
if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement)
86
pimpl->returnPressed();
92
static const unsigned short kKeycodeA = 0;
93
static const unsigned short kKeycodeX = 7;
94
static const unsigned short kKeycodeC = 8;
95
static const unsigned short kKeycodeV = 9;
99
@interface QocoaSearchField : NSSearchField
100
-(BOOL)performKeyEquivalent:(NSEvent*)event;
103
@implementation QocoaSearchField
104
-(BOOL)performKeyEquivalent:(NSEvent*)event {
105
if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask)
107
const unsigned short keyCode = [event keyCode];
108
/* if (keyCode == kKeycodeA) // Cmd+a
110
[self performSelector:@selector(selectText:)];
113
else*/ if (keyCode == kKeycodeC) // Cmd+c
115
QClipboard* clipboard = QApplication::clipboard();
116
clipboard->setText(toQString([self stringValue]));
119
else if (keyCode == kKeycodeV) // Cmd+v
121
QClipboard* clipboard = QApplication::clipboard();
122
[self setStringValue:fromQString(clipboard->text())];
125
else if (keyCode == kKeycodeX) // Cmd+x
127
QClipboard* clipboard = QApplication::clipboard();
128
clipboard->setText(toQString([self stringValue]));
129
[self setStringValue:@""];
138
QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
140
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
142
NSSearchField *search = [[QocoaSearchField alloc] init];
144
QSearchFieldDelegate *delegate = [[QSearchFieldDelegate alloc] init];
145
pimpl = delegate->pimpl = new QSearchFieldPrivate(this, search);
146
[search setDelegate:delegate];
148
setupLayout(search, this);
151
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
158
void QSearchField::setText(const QString &text)
164
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
165
[pimpl->nsSearchField setStringValue:fromQString(text)];
169
void QSearchField::setPlaceholderText(const QString &text)
175
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
176
[[pimpl->nsSearchField cell] setPlaceholderString:fromQString(text)];
180
QString QSearchField::placeholderText() const {
182
NSString* placeholder = [[pimpl->nsSearchField cell] placeholderString];
183
return toQString(placeholder);
186
void QSearchField::setFocus(Qt::FocusReason reason)
192
if ([pimpl->nsSearchField acceptsFirstResponder])
193
[[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField];
196
void QSearchField::setFocus()
198
setFocus(Qt::OtherFocusReason);
201
void QSearchField::clear()
207
[pimpl->nsSearchField setStringValue:@""];
208
emit textChanged(QString());
211
void QSearchField::selectAll()
217
[pimpl->nsSearchField performSelector:@selector(selectText:)];
220
QString QSearchField::text() const
226
return toQString([pimpl->nsSearchField stringValue]);
229
void QSearchField::resizeEvent(QResizeEvent *resizeEvent)
231
QWidget::resizeEvent(resizeEvent);
234
bool QSearchField::eventFilter(QObject *o, QEvent *e)
236
return QWidget::eventFilter(o, e);