iPlug2 - C++ Audio Plug-in Framework
IGraphicsMac_view.h
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #import <Cocoa/Cocoa.h>
12 
13 #if defined IGRAPHICS_GL
14 #import <QuartzCore/QuartzCore.h>
15 #endif
16 
17 #include "IGraphicsMac.h"
18 #include "IGraphicsStructs.h"
19 
20 BEGIN_IPLUG_NAMESPACE
21 BEGIN_IGRAPHICS_NAMESPACE
22 
23 inline NSRect ToNSRect(IGraphics* pGraphics, const IRECT& bounds)
24 {
25  const float scale = pGraphics->GetDrawScale();
26  const float x = floor(bounds.L * scale);
27  const float y = floor(bounds.T * scale);
28  const float x2 = ceil(bounds.R * scale);
29  const float y2 = ceil(bounds.B * scale);
30 
31  return NSMakeRect(x, y, x2 - x, y2 - y);
32 }
33 
34 inline IRECT ToIRECT(IGraphics* pGraphics, const NSRect* pNSRect)
35 {
36  const float scale = 1.f/pGraphics->GetDrawScale();
37  const float x = pNSRect->origin.x;
38  const float y = pNSRect->origin.y;
39  const float w = pNSRect->size.width;
40  const float h = pNSRect->size.height;
41 
42  return IRECT(x * scale, y * scale, (x + w) * scale, (y + h) * scale);
43 }
44 
45 inline NSColor* ToNSColor(const IColor& c)
46 {
47  return [NSColor colorWithDeviceRed:(double) c.R / 255.0 green:(double) c.G / 255.0 blue:(double) c.B / 255.0 alpha:(double) c.A / 255.0];
48 }
49 
50 inline IColor FromNSColor(const NSColor* c)
51 {
52  return IColor(c.alphaComponent * 255., c.redComponent* 255., c.greenComponent * 255., c.blueComponent * 255.);
53 }
54 
55 inline int GetMouseOver(IGraphicsMac* pGraphics)
56 {
57  return pGraphics->GetMouseOver();
58 }
59 
60 END_IGRAPHICS_NAMESPACE
61 END_IPLUG_NAMESPACE
62 
63 // based on code by Scott Gruby http://blog.gruby.com/2008/03/30/filtering-nstextfield-take-2/
64 @interface IGRAPHICS_FORMATTER : NSFormatter
65 {
66  NSCharacterSet* filterCharacterSet;
67  int maxLength;
68  int maxValue;
69 }
70 
71 - (void) setAcceptableCharacterSet: (NSCharacterSet*) pCharacterSet;
72 - (void) setMaximumLength:(int) inLength;
73 - (void) setMaximumValue:(int) inValue;
74 
75 @end
76 
77 @interface IGRAPHICS_TEXTFIELDCELL : NSTextFieldCell
78 {
79  BOOL mIsEditingOrSelecting;
80 }
81 
82 @end
83 
84 using namespace iplug;
85 using namespace igraphics;
86 
87 @interface IGRAPHICS_MENU : NSMenu
88 {
89  IPopupMenu* mIPopupMenu;
90 }
91 - (id) initWithIPopupMenuAndReceiver: (IPopupMenu*) pMenu : (NSView*) pView;
92 - (IPopupMenu*) iPopupMenu;
93 @end
94 
95 // Dummy view class used to receive Menu Events inline
96 @interface IGRAPHICS_MENU_RCVR : NSView
97 {
98  NSMenuItem* nsMenuItem;
99 }
100 - (void) onMenuSelection:(id)sender;
101 - (NSMenuItem*) menuItem;
102 @end
103 
104 @interface IGRAPHICS_TEXTFIELD : NSTextField
105 {
106 }
107 - (bool) becomeFirstResponder;
108 @end
109 
110 #ifdef IGRAPHICS_GL
111 #define VIEW_BASE NSOpenGLView
112 #else
113 #define VIEW_BASE NSView
114 #endif
115 
116 @interface IGRAPHICS_VIEW : VIEW_BASE <NSTextFieldDelegate/*, WKScriptMessageHandler*/>
117 {
118  CVDisplayLinkRef mDisplayLink;
119  dispatch_source_t mDisplaySource;
120  NSTimer* mTimer;
121 
122  NSTrackingArea* mTrackingArea;
123  IGRAPHICS_TEXTFIELD* mTextFieldView;
124  NSCursor* mMoveCursor;
125  float mPrevX, mPrevY;
126  bool mMouseOutDuringDrag;
127  IRECTList mDirtyRects;
128  IColorPickerHandlerFunc mColorPickerFunc;
129 @public
130  IGraphicsMac* mGraphics; // OBJC instance variables have to be pointers
131 }
132 - (id) initWithIGraphics: (IGraphicsMac*) pGraphics;
133 - (BOOL) isOpaque;
134 - (BOOL) acceptsFirstResponder;
135 - (BOOL) acceptsFirstMouse: (NSEvent*) pEvent;
136 - (void) viewDidMoveToWindow;
137 - (void) viewDidChangeBackingProperties: (NSNotification*) pNotification;
138 - (void) drawRect: (NSRect) bounds;
139 - (void) render;
140 - (void) killTimer;
141 - (void) onTimer: (NSTimer*) pTimer;
142 //mouse
143 - (void) getMouseXY: (NSEvent*) pEvent : (float&) x : (float&) y;
144 - (IMouseInfo) getMouseLeft: (NSEvent*) pEvent;
145 - (IMouseInfo) getMouseRight: (NSEvent*) pEvent;
146 - (void) updateTrackingAreas;
147 - (void) mouseEntered:(NSEvent*) pEvent;
148 - (void) mouseExited:(NSEvent*) pEvent;
149 - (void) mouseDown: (NSEvent*) pEvent;
150 - (void) mouseUp: (NSEvent*) pEvent;
151 - (void) mouseDragged: (NSEvent*) pEvent;
152 - (void) rightMouseDown: (NSEvent*) pEvent;
153 - (void) rightMouseUp: (NSEvent*) pEvent;
154 - (void) rightMouseDragged: (NSEvent*) pEvent;
155 - (void) mouseMoved: (NSEvent*) pEvent;
156 - (void) scrollWheel: (NSEvent*) pEvent;
157 - (void) keyDown: (NSEvent*) pEvent;
158 - (void) keyUp: (NSEvent*) pEvent;
159 //text entry
160 - (void) removeFromSuperview;
161 - (void) controlTextDidEndEditing: (NSNotification*) pNotification;
162 - (void) createTextEntry: (int) paramIdx : (const IText&) text : (const char*) str : (int) length : (NSRect) areaRect;
163 - (void) endUserInput;
164 //pop-up menu
165 - (IPopupMenu*) createPopupMenu: (IPopupMenu&) menu : (NSRect) bounds;
166 //color picker
167 - (BOOL) promptForColor: (IColor&) color : (IColorPickerHandlerFunc) func;
168 - (void) onColorPicked: (NSColorPanel*) pColorPanel;
169 
170 //tooltip
171 - (NSString*) view: (NSView*) pView stringForToolTip: (NSToolTipTag) tag point: (NSPoint) point userData: (void*) pData;
172 - (void) registerToolTip: (IRECT&) bounds;
173 //drag-and-drop
174 - (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender;
175 - (BOOL) performDragOperation: (id<NSDraggingInfo>) sender;
176 //
177 - (void) setMouseCursor: (ECursor) cursorType;
178 @end
179 
180 #if defined IGRAPHICS_IMGUI
181 #import <MetalKit/MetalKit.h>
182 
183 @interface IGRAPHICS_IMGUIVIEW : MTKView
184 {
185  IGRAPHICS_VIEW* mView;
186 }
187 @property (nonatomic, strong) id <MTLCommandQueue> commandQueue;
188 - (id) initWithIGraphicsView: (IGRAPHICS_VIEW*) pView;
189 @end
190 #endif
191 
Used to manage a list of rectangular areas and optimize them for drawing to the screen.
Used to manage a rectangular area, independent of draw class/platform.
Used to manage color data, independent of draw class/platform.
float R
Right side of the rectangle (X + W)
IGraphics platform class for macOS.
Definition: IGraphicsMac.h:23
A class for setting the contents of a pop up menu.
IText is used to manage font and text/text entry style for a piece of text on the UI...
float GetDrawScale() const
Gets the graphics context scaling factor.
Definition: IGraphics.h:1064
Used to group mouse coordinates with mouse modifier information.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
float L
Left side of the rectangle (X)
float T
Top of the rectangle (Y)
float B
Bottom of the rectangle (Y + H)