11 #include "IGraphicsMac.h" 12 #import "IGraphicsMac_view.h" 17 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 19 using namespace iplug;
20 using namespace igraphics;
22 static int GetSystemVersion()
27 if (NSAppKitVersionNumber >= 1266.0)
29 if (NSAppKitVersionNumber >= 1404.0)
37 Gestalt(gestaltSystemVersion,&a);
44 StaticStorage<CoreTextFontDescriptor> sFontDescriptorCache;
48 IGraphicsMac::IGraphicsMac(
IGEditorDelegate& dlg,
int w,
int h,
int fps,
float scale)
49 : IGRAPHICS_DRAW_CLASS(dlg, w, h, fps, scale)
52 StaticStorage<CoreTextFontDescriptor>::Accessor storage(sFontDescriptorCache);
56 IGraphicsMac::~IGraphicsMac()
58 StaticStorage<CoreTextFontDescriptor>::Accessor storage(sFontDescriptorCache);
64 PlatformFontPtr IGraphicsMac::LoadPlatformFont(
const char* fontID,
const char* fileNameOrResID)
66 return CoreTextHelpers::LoadPlatformFont(fontID, fileNameOrResID, GetBundleID(), GetSharedResourcesSubPath());
69 PlatformFontPtr IGraphicsMac::LoadPlatformFont(
const char* fontID,
const char* fontName, ETextStyle style)
71 return CoreTextHelpers::LoadPlatformFont(fontID, fontName, style);
74 PlatformFontPtr IGraphicsMac::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
76 return CoreTextHelpers::LoadPlatformFont(fontID, pData, dataSize);
79 void IGraphicsMac::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font)
81 CoreTextHelpers::CachePlatformFont(fontID, font, sFontDescriptorCache);
84 float IGraphicsMac::MeasureText(
const IText& text,
const char* str,
IRECT& bounds)
const 86 return IGRAPHICS_DRAW_CLASS::MeasureText(text, str, bounds);
89 void* IGraphicsMac::OpenWindow(
void* pParent)
93 IGRAPHICS_VIEW* pView = [[IGRAPHICS_VIEW alloc] initWithIGraphics:
this];
94 mView = (
void*) pView;
97 [[pView openGLContext] makeCurrentContext];
100 OnViewInitialized([pView layer]);
101 SetScreenScale([[NSScreen mainScreen] backingScaleFactor]);
102 GetDelegate()->LayoutUI(
this);
104 GetDelegate()->OnUIOpen();
108 [(NSView*) pParent addSubview: pView];
114 void IGraphicsMac::AttachPlatformView(
const IRECT& r,
void* pView)
116 NSView* pNewSubView = (NSView*) pView;
117 [pNewSubView setFrame:ToNSRect(
this, r)];
119 [(IGRAPHICS_VIEW*) mView addSubview:(NSView*) pNewSubView];
122 void IGraphicsMac::RemovePlatformView(
void* pView)
124 [(NSView*) pView removeFromSuperview];
127 void IGraphicsMac::CloseWindow()
131 #if defined IGRAPHICS_IMGUI 134 IGRAPHICS_IMGUIVIEW* pImGuiView = (IGRAPHICS_IMGUIVIEW*) mImGuiView;
135 [pImGuiView removeFromSuperview];
136 [pImGuiView release];
137 mImGuiView =
nullptr;
141 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
144 [[pView openGLContext] makeCurrentContext];
147 [pView removeAllToolTips];
149 [pView removeFromSuperview];
157 bool IGraphicsMac::WindowIsOpen()
162 void IGraphicsMac::PlatformResize(
bool parentHasResized)
166 NSSize size = {
static_cast<CGFloat
>(WindowWidth()), static_cast<CGFloat>(WindowHeight()) };
168 [NSAnimationContext beginGrouping];
169 [[NSAnimationContext currentContext] setDuration:0.0];
170 [(IGRAPHICS_VIEW*) mView setFrameSize: size ];
172 #if defined IGRAPHICS_IMGUI && !defined IGRAPHICS_SKIA && !defined IGRAPHICS_GL 174 [(IGRAPHICS_IMGUIVIEW*) mImGuiView setFrameSize: size ];
177 [NSAnimationContext endGrouping];
183 void IGraphicsMac::PointToScreen(
float& x,
float& y)
const 189 NSWindow* pWindow = [(IGRAPHICS_VIEW*) mView window];
190 NSPoint wndpt = [(IGRAPHICS_VIEW*) mView convertPoint:NSMakePoint(x, y) toView:nil];
191 NSPoint pt = [pWindow convertRectToScreen: NSMakeRect(wndpt.x, wndpt.y, 0.0, 0.0)].origin;
198 void IGraphicsMac::ScreenToPoint(
float& x,
float& y)
const 202 NSWindow* pWindow = [(IGRAPHICS_VIEW*) mView window];
203 NSPoint wndpt = [pWindow convertRectFromScreen: NSMakeRect(x, y, 0.0, 0.0)].origin;
204 NSPoint pt = [(IGRAPHICS_VIEW*) mView convertPoint:NSMakePoint(wndpt.x, wndpt.y) fromView:nil];
206 x = pt.x / GetDrawScale();
207 y = pt.y / GetDrawScale();
211 void IGraphicsMac::HideMouseCursor(
bool hide,
bool lock)
213 if (mCursorHidden == hide)
216 mCursorHidden = hide;
220 StoreCursorPosition();
221 CGDisplayHideCursor(kCGDirectMainDisplay);
226 DoCursorLock(mCursorX, mCursorY, mCursorX, mCursorY);
227 CGDisplayShowCursor(kCGDirectMainDisplay);
232 void IGraphicsMac::MoveMouseCursor(
float x,
float y)
238 RepositionCursor(CGPoint{x, y});
239 StoreCursorPosition();
242 void IGraphicsMac::DoCursorLock(
float x,
float y,
float& prevX,
float& prevY)
244 if (mCursorHidden && mCursorLock && !mTabletInput)
246 RepositionCursor(mCursorLockPosition);
252 mCursorX = prevX = x;
253 mCursorY = prevY = y;
257 void IGraphicsMac::RepositionCursor(CGPoint point)
259 point = CGPoint{point.x, CGDisplayPixelsHigh(CGMainDisplayID()) - point.y};
260 CGAssociateMouseAndMouseCursorPosition(
false);
261 CGDisplayMoveCursorToPoint(CGMainDisplayID(), point);
262 CGAssociateMouseAndMouseCursorPosition(
true);
265 void IGraphicsMac::StoreCursorPosition()
268 NSPoint mouse = [NSEvent mouseLocation];
269 mCursorX = mouse.x = std::round(mouse.x);
270 mCursorY = mouse.y = std::round(mouse.y);
271 mCursorLockPosition = CGPoint{mouse.x, mouse.y};
274 ScreenToPoint(mCursorX, mCursorY);
277 void IGraphicsMac::GetMouseLocation(
float& x,
float&y)
const 280 NSPoint mouse = [NSEvent mouseLocation];
288 EMsgBoxResult IGraphicsMac::ShowMessageBox(
const char* str,
const char* caption, EMsgBoxType type, IMsgBoxCompletionHanderFunc completionHandler)
290 ReleaseMouseCapture();
292 long result = (long) kCANCEL;
295 if (!caption) caption=
"";
297 NSString *msg = (NSString *) CFStringCreateWithCString(NULL,str,kCFStringEncodingUTF8);
298 NSString *cap = (NSString *) CFStringCreateWithCString(NULL,caption,kCFStringEncodingUTF8);
300 msg = msg ? msg : (NSString *) CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);
301 cap = cap ? cap : (NSString *) CFStringCreateWithCString(NULL, caption, kCFStringEncodingASCII);
306 NSRunAlertPanel(msg,
@"%@",
@"OK",
@"",
@"", cap);
310 result = NSRunAlertPanel(msg,
@"%@",
@"OK",
@"Cancel",
@"", cap);
311 result = result ? kOK : kCANCEL;
314 result = NSRunAlertPanel(msg,
@"%@",
@"Yes",
@"No",
@"", cap);
315 result = result ? kYES : kNO;
317 case kMB_RETRYCANCEL:
318 result = NSRunAlertPanel(msg,
@"%@",
@"Retry",
@"Cancel",
@"", cap);
319 result = result ? kRETRY : kCANCEL;
321 case kMB_YESNOCANCEL:
322 result = NSRunAlertPanel(msg,
@"%@",
@"Yes",
@"Cancel",
@"No", cap);
323 result = (result == 1) ? kYES : (result == -1) ? kNO : kCANCEL;
330 if(completionHandler)
331 completionHandler(static_cast<EMsgBoxResult>(result));
333 return static_cast<EMsgBoxResult
>(result);
336 void IGraphicsMac::ForceEndUserEdit()
340 [(IGRAPHICS_VIEW*) mView endUserInput];
344 void IGraphicsMac::UpdateTooltips()
346 if (!(mView && TooltipsEnabled()))
351 [(IGRAPHICS_VIEW*) mView removeAllToolTips];
353 if (GetPopupMenuControl() && GetPopupMenuControl()->GetState() > IPopupMenuControl::kCollapsed)
358 auto func = [
this](
IControl* pControl)
360 if (pControl->GetTooltip() && !pControl->IsHidden())
362 IRECT pR = pControl->GetTargetRECT();
365 [(IGRAPHICS_VIEW*) mView registerToolTip: pR];
370 ForStandardControlsFunc(func);
375 const char* IGraphicsMac::GetPlatformAPIStr()
380 bool IGraphicsMac::RevealPathInExplorerOrFinder(WDL_String& path,
bool select)
382 BOOL success = FALSE;
388 NSString* pPath = [NSString stringWithCString:path.Get() encoding:NSUTF8StringEncoding];
390 if([[NSFileManager defaultManager] fileExistsAtPath : pPath] == YES)
394 NSString* pParentDirectoryPath = [pPath stringByDeletingLastPathComponent];
396 if (pParentDirectoryPath)
398 success = [[NSWorkspace sharedWorkspace] openFile:pParentDirectoryPath];
401 success = [[NSWorkspace sharedWorkspace] selectFile: pPath inFileViewerRootedAtPath:pParentDirectoryPath];
405 success = [[NSWorkspace sharedWorkspace] openFile:pPath];
412 return (
bool) success;
415 void IGraphicsMac::PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action,
const char* ext)
423 NSString* pDefaultFileName;
424 NSString* pDefaultPath;
425 NSArray* pFileTypes = nil;
427 if (fileName.GetLength())
428 pDefaultFileName = [NSString stringWithCString:fileName.Get() encoding:NSUTF8StringEncoding];
430 pDefaultFileName = [NSString stringWithCString:
"" encoding:NSUTF8StringEncoding];
432 if(!path.GetLength())
435 pDefaultPath = [NSString stringWithCString:path.Get() encoding:NSUTF8StringEncoding];
439 if (CStringHasContents(ext))
440 pFileTypes = [[NSString stringWithUTF8String:ext] componentsSeparatedByString:
@" "];
442 if (action == EFileAction::Save)
444 NSSavePanel* pSavePanel = [NSSavePanel savePanel];
447 [pSavePanel setAllowedFileTypes: pFileTypes];
448 [pSavePanel setAllowsOtherFileTypes: NO];
450 long result = [pSavePanel runModalForDirectory:pDefaultPath file:pDefaultFileName];
452 if (result == NSOKButton)
454 NSString* pFullPath = [pSavePanel filename] ;
455 fileName.Set([pFullPath UTF8String]);
457 NSString* pTruncatedPath = [pFullPath stringByDeletingLastPathComponent];
461 path.Set([pTruncatedPath UTF8String]);
468 NSOpenPanel* pOpenPanel = [NSOpenPanel openPanel];
472 [pOpenPanel setCanChooseFiles:YES];
473 [pOpenPanel setCanChooseDirectories:NO];
474 [pOpenPanel setResolvesAliases:YES];
476 long result = [pOpenPanel runModalForDirectory:pDefaultPath file:pDefaultFileName types:pFileTypes];
478 if (result == NSOKButton)
480 NSString* pFullPath = [pOpenPanel filename] ;
481 fileName.Set([pFullPath UTF8String]);
483 NSString* pTruncatedPath = [pFullPath stringByDeletingLastPathComponent];
487 path.Set([pTruncatedPath UTF8String]);
494 void IGraphicsMac::PromptForDirectory(WDL_String& dir)
496 NSString* defaultPath;
500 defaultPath = [NSString stringWithCString:dir.Get() encoding:NSUTF8StringEncoding];
504 defaultPath = [NSString stringWithCString:DEFAULT_PATH encoding:NSUTF8StringEncoding];
505 dir.Set(DEFAULT_PATH);
508 NSOpenPanel* panelOpen = [NSOpenPanel openPanel];
510 [panelOpen setTitle:
@"Choose a Directory"];
511 [panelOpen setCanChooseFiles:NO];
512 [panelOpen setCanChooseDirectories:YES];
513 [panelOpen setResolvesAliases:YES];
514 [panelOpen setCanCreateDirectories:YES];
516 [panelOpen setDirectoryURL: [NSURL fileURLWithPath: defaultPath]];
518 if ([panelOpen runModal] == NSOKButton)
520 NSString* fullPath = [ panelOpen filename ] ;
521 dir.Set( [fullPath UTF8String] );
530 bool IGraphicsMac::PromptForColor(
IColor& color,
const char* str, IColorPickerHandlerFunc func)
533 return [(IGRAPHICS_VIEW*) mView promptForColor:color : func];
544 NSRect areaRect = ToNSRect(
this, bounds);
545 pReturnMenu = [(IGRAPHICS_VIEW*) mView createPopupMenu: menu: areaRect];
549 if(pReturnMenu && pReturnMenu->GetFunction())
550 pReturnMenu->ExecFunction();
555 void IGraphicsMac::CreatePlatformTextEntry(
int paramIdx,
const IText& text,
const IRECT& bounds,
int length,
const char* str)
559 NSRect areaRect = ToNSRect(
this, bounds);
560 [(IGRAPHICS_VIEW*) mView createTextEntry: paramIdx : text: str: length: areaRect];
564 ECursor IGraphicsMac::SetMouseCursor(ECursor cursorType)
567 [(IGRAPHICS_VIEW*) mView setMouseCursor: cursorType];
572 bool IGraphicsMac::OpenURL(
const char* url,
const char* msgWindowTitle,
const char* confirmMsg,
const char* errMsgOnFailure)
574 #pragma REMINDER("Warning and error messages for OpenURL not implemented") 575 NSURL* pNSURL =
nullptr;
576 if (strstr(url,
"http"))
577 pNSURL = [NSURL URLWithString:[NSString stringWithCString:url encoding:NSUTF8StringEncoding]];
579 pNSURL = [NSURL fileURLWithPath:[NSString stringWithCString:url encoding:NSUTF8StringEncoding]];
583 bool ok = ([[NSWorkspace sharedWorkspace] openURL:pNSURL]);
589 void* IGraphicsMac::GetWindow()
591 if (mView)
return mView;
596 int IGraphicsMac::GetUserOSVersion()
598 return (
int) GetSystemVersion();
601 bool IGraphicsMac::GetTextFromClipboard(WDL_String& str)
603 NSString* pTextOnClipboard = [[NSPasteboard generalPasteboard] stringForType: NSStringPboardType];
605 if (pTextOnClipboard == nil)
612 str.Set([pTextOnClipboard UTF8String]);
617 bool IGraphicsMac::SetTextInClipboard(
const char* str)
619 NSString* pTextForClipboard = [NSString stringWithUTF8String:str];
620 [[NSPasteboard generalPasteboard] clearContents];
621 return [[NSPasteboard generalPasteboard] setString:pTextForClipboard forType:NSStringPboardType];
624 void IGraphicsMac::CreatePlatformImGui()
626 #if defined IGRAPHICS_IMGUI 627 #if defined IGRAPHICS_SKIA && IGRAPHICS_CPU 628 #define USE_IGRAPHICS_IMGUIVIEW 1 629 #elif defined IGRAPHICS_NANOVG && IGRAPHICS_METAL 630 #define USE_IGRAPHICS_IMGUIVIEW 1 632 #define USE_IGRAPHICS_IMGUIVIEW 0 635 #if USE_IGRAPHICS_IMGUIVIEW 638 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
640 IGRAPHICS_IMGUIVIEW* pImGuiView = [[IGRAPHICS_IMGUIVIEW alloc] initWithIGraphicsView:pView];
641 [pView addSubview: pImGuiView];
642 mImGuiView = pImGuiView;
645 #endif // IGRAPHICS_IMGUI 648 #if defined IGRAPHICS_NANOVG 649 #include "IGraphicsNanoVG.cpp" 650 #elif defined IGRAPHICS_SKIA 651 #include "IGraphicsSkia.cpp" 653 #error Either NO_IGRAPHICS or one and only one choice of graphics library must be defined! The lowest level base class of an IGraphics control.
Used to manage a rectangular area, independent of draw class/platform.
virtual ECursor SetMouseCursor(ECursor cursorType=ECursor::ARROW)
Sets the mouse cursor to one of ECursor (implementations should return the result of the base impleme...
Used to manage color data, independent of draw class/platform.
This file contains the base IControl implementation, along with some base classes for specific types ...
An editor delegate base class for a SOMETHING that uses IGraphics for it's UI.
IText is used to manage font and text/text entry style for a piece of text on the UI...
void DesktopPath(WDL_String &path)