/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm

http://github.com/tomahawk-player/tomahawk · Objective C++ · 294 lines · 219 code · 51 blank · 24 comment · 43 complexity · 25830b68713bc6f97f504885eb11cfcc MD5 · raw file

  1. /*
  2. Copyright (C) 2011 by Mike McQuaid
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "qsearchfield.h"
  20. #include "moc_qsearchfield.cpp"
  21. #include "qocoa_mac.h"
  22. #import "Foundation/NSAutoreleasePool.h"
  23. #import "Foundation/NSNotification.h"
  24. #import "AppKit/NSSearchField.h"
  25. #include <QApplication>
  26. #include <QKeyEvent>
  27. #include <QClipboard>
  28. class QSearchFieldPrivate : public QObject
  29. {
  30. public:
  31. QSearchFieldPrivate(QSearchField *qSearchField, NSSearchField *nsSearchField)
  32. : QObject(qSearchField), qSearchField(qSearchField), nsSearchField(nsSearchField) {}
  33. void textDidChange(const QString &text)
  34. {
  35. if (qSearchField)
  36. emit qSearchField->textChanged(text);
  37. }
  38. void textDidEndEditing()
  39. {
  40. if (qSearchField)
  41. emit qSearchField->editingFinished();
  42. }
  43. void returnPressed()
  44. {
  45. if (qSearchField) {
  46. emit qSearchField->returnPressed();
  47. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
  48. QApplication::postEvent(qSearchField, event);
  49. }
  50. }
  51. void keyDownPressed()
  52. {
  53. if (qSearchField) {
  54. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
  55. QApplication::postEvent(qSearchField, event);
  56. }
  57. }
  58. void keyUpPressed()
  59. {
  60. if (qSearchField) {
  61. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
  62. QApplication::postEvent(qSearchField, event);
  63. }
  64. }
  65. QPointer<QSearchField> qSearchField;
  66. NSSearchField *nsSearchField;
  67. };
  68. @interface QSearchFieldDelegate : NSObject<NSTextFieldDelegate>
  69. {
  70. @public
  71. QPointer<QSearchFieldPrivate> pimpl;
  72. }
  73. -(void)controlTextDidChange:(NSNotification*)notification;
  74. -(void)controlTextDidEndEditing:(NSNotification*)notification;
  75. @end
  76. @implementation QSearchFieldDelegate
  77. -(void)controlTextDidChange:(NSNotification*)notification {
  78. Q_ASSERT(pimpl);
  79. if (pimpl)
  80. pimpl->textDidChange(toQString([[notification object] stringValue]));
  81. }
  82. -(void)controlTextDidEndEditing:(NSNotification*)notification {
  83. Q_UNUSED(notification);
  84. // No Q_ASSERT here as it is called on destruction.
  85. if (pimpl)
  86. pimpl->textDidEndEditing();
  87. if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement)
  88. pimpl->returnPressed();
  89. }
  90. -(BOOL)control: (NSControl *)control textView:
  91. (NSTextView *)textView doCommandBySelector:
  92. (SEL)commandSelector {
  93. Q_ASSERT(pimpl);
  94. if (!pimpl) return NO;
  95. if (commandSelector == @selector(moveDown:)) {
  96. pimpl->keyDownPressed();
  97. return YES;
  98. } else if (commandSelector == @selector(moveUp:)) {
  99. pimpl->keyUpPressed();
  100. return YES;
  101. }
  102. return NO;
  103. }
  104. @end
  105. @interface QocoaSearchField : NSSearchField
  106. -(BOOL)performKeyEquivalent:(NSEvent*)event;
  107. @end
  108. @implementation QocoaSearchField
  109. -(BOOL)performKeyEquivalent:(NSEvent*)event {
  110. // First, check if we have the focus.
  111. // If no, it probably means this event isn't for us.
  112. NSResponder* firstResponder = [[NSApp keyWindow] firstResponder];
  113. if ([firstResponder isKindOfClass:[NSText class]] &&
  114. [(NSText*)firstResponder delegate] == self) {
  115. if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask)
  116. {
  117. QString keyString = toQString([event characters]);
  118. if (keyString == "a") // Cmd+a
  119. {
  120. [self performSelector:@selector(selectText:)];
  121. return YES;
  122. }
  123. else if (keyString == "c") // Cmd+c
  124. {
  125. [[self currentEditor] copy: nil];
  126. return YES;
  127. }
  128. else if (keyString == "v") // Cmd+v
  129. {
  130. [[self currentEditor] paste: nil];
  131. return YES;
  132. }
  133. else if (keyString == "x") // Cmd+x
  134. {
  135. [[self currentEditor] cut: nil];
  136. return YES;
  137. }
  138. }
  139. }
  140. return NO;
  141. }
  142. @end
  143. QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
  144. {
  145. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  146. NSSearchField *search = [[QocoaSearchField alloc] init];
  147. QSearchFieldDelegate *delegate = [[QSearchFieldDelegate alloc] init];
  148. pimpl = delegate->pimpl = new QSearchFieldPrivate(this, search);
  149. [search setDelegate:delegate];
  150. setupLayout(search, this);
  151. setFixedHeight(24);
  152. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  153. [search release];
  154. [pool drain];
  155. }
  156. void QSearchField::setMenu(QMenu *menu)
  157. {
  158. Q_ASSERT(pimpl);
  159. if (!pimpl)
  160. return;
  161. NSMenu *nsMenu = menu->toNSMenu();
  162. [[pimpl->nsSearchField cell] setSearchMenuTemplate:nsMenu];
  163. }
  164. void QSearchField::popupMenu()
  165. {
  166. }
  167. void QSearchField::setText(const QString &text)
  168. {
  169. Q_ASSERT(pimpl);
  170. if (!pimpl)
  171. return;
  172. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  173. [pimpl->nsSearchField setStringValue:fromQString(text)];
  174. [pool drain];
  175. }
  176. void QSearchField::setPlaceholderText(const QString &text)
  177. {
  178. Q_ASSERT(pimpl);
  179. if (!pimpl)
  180. return;
  181. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  182. [[pimpl->nsSearchField cell] setPlaceholderString:fromQString(text)];
  183. [pool drain];
  184. }
  185. void QSearchField::clear()
  186. {
  187. Q_ASSERT(pimpl);
  188. if (!pimpl)
  189. return;
  190. [pimpl->nsSearchField setStringValue:@""];
  191. emit textChanged(QString());
  192. }
  193. void QSearchField::selectAll()
  194. {
  195. Q_ASSERT(pimpl);
  196. if (!pimpl)
  197. return;
  198. [pimpl->nsSearchField performSelector:@selector(selectText:)];
  199. }
  200. QString QSearchField::text() const
  201. {
  202. Q_ASSERT(pimpl);
  203. if (!pimpl)
  204. return QString();
  205. return toQString([pimpl->nsSearchField stringValue]);
  206. }
  207. QString QSearchField::placeholderText() const
  208. {
  209. Q_ASSERT(pimpl);
  210. if (!pimpl)
  211. return QString();
  212. return toQString([[pimpl->nsSearchField cell] placeholderString]);
  213. }
  214. void QSearchField::setFocus(Qt::FocusReason)
  215. {
  216. Q_ASSERT(pimpl);
  217. if (!pimpl)
  218. return;
  219. if ([pimpl->nsSearchField acceptsFirstResponder])
  220. [[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField];
  221. }
  222. void QSearchField::setFocus()
  223. {
  224. setFocus(Qt::OtherFocusReason);
  225. }
  226. void QSearchField::changeEvent(QEvent* event)
  227. {
  228. if (event->type() == QEvent::EnabledChange) {
  229. Q_ASSERT(pimpl);
  230. if (!pimpl)
  231. return;
  232. const bool enabled = isEnabled();
  233. [pimpl->nsSearchField setEnabled: enabled];
  234. }
  235. QWidget::changeEvent(event);
  236. }
  237. void QSearchField::resizeEvent(QResizeEvent *resizeEvent)
  238. {
  239. QWidget::resizeEvent(resizeEvent);
  240. }