/src/libtomahawk/thirdparty/Qocoa/qocoa_mac.h

http://github.com/tomahawk-player/tomahawk · C Header · 63 lines · 33 code · 9 blank · 21 comment · 1 complexity · dc13e8a7e4fee3d441ac500455ad5775 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. #ifndef QCOCOA_MAC_H
  20. #define QCOCOA_MAC_H
  21. #import <Cocoa/Cocoa.h>
  22. #include <AppKit/NSImage.h>
  23. #include <QString>
  24. #include <QVBoxLayout>
  25. #include <QMacCocoaViewContainer>
  26. #include <qmacfunctions.h>
  27. static inline NSString* fromQString(const QString &string)
  28. {
  29. const QByteArray utf8 = string.toUtf8();
  30. const char* cString = utf8.constData();
  31. return [[[NSString alloc] initWithUTF8String:cString] autorelease];
  32. }
  33. static inline QString toQString(NSString *string)
  34. {
  35. if (!string)
  36. return QString();
  37. return QString::fromUtf8([string UTF8String]);
  38. }
  39. static inline NSImage* fromQPixmap(const QPixmap &pixmap)
  40. {
  41. CGImageRef cgImage = QtMac::toCGImageRef(pixmap);
  42. return [[[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize] autorelease];
  43. }
  44. static inline void setupLayout(NSView *cocoaView, QWidget *parent)
  45. {
  46. parent->setAttribute(Qt::WA_NativeWindow);
  47. QVBoxLayout *layout = new QVBoxLayout(parent);
  48. layout->setMargin(0);
  49. layout->addWidget(new QMacCocoaViewContainer(cocoaView, parent));
  50. }
  51. #endif