0023243: Adapt OpenGL viewer for using in Cocoa applications on Mac OS X
[occt.git] / src / ViewerTest / ViewerTest_ViewerCommands_1.mm
CommitLineData
4fe56619 1// Copyright (c) 2012 OPEN CASCADE SAS
2//
3// The content of this file is subject to the Open CASCADE Technology Public
4// License Version 6.5 (the "License"). You may not use the content of this file
5// except in compliance with the License. Please obtain a copy of the License
6// at http://www.opencascade.org and read it completely before using this file.
7//
8// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10//
11// The Original Code and all software distributed under the License is
12// distributed on an "AS IS" basis, without warranty of any kind, and the
13// Initial Developer hereby disclaims all such warranties, including without
14// limitation, any warranties of merchantability, fitness for a particular
15// purpose or non-infringement. Please see the License for the specific terms
16// and conditions governing the rights and limitations under the License.
17
18#if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
19
20#import <Cocoa/Cocoa.h>
21
22#include <Cocoa_Window.hxx>
23#include <ViewerTest.hxx>
24#include <V3d_View.hxx>
25#include <V3d_Viewer.hxx>
26#include <AIS_InteractiveContext.hxx>
27#include <NIS_View.hxx>
28
29//! Custom Cocoa view to handle events
30@interface ViewerTest_CocoaEventManagerView : NSView
31@end
32
33extern void VT_ProcessExpose();
34extern void VT_ProcessConfigure();
35extern void VT_ProcessKeyPress (const char* theBuffer);
36extern void VT_ProcessMotion();
37extern void VT_ProcessButton3Press();
38extern void VT_ProcessButton3Release();
39extern void VT_ProcessControlButton2Motion();
40extern void VT_ProcessControlButton3Motion();
41extern Standard_Boolean VT_ProcessButton1Press (Standard_Integer theArgsNb,
42 const char** theArgsVec,
43 Standard_Boolean theToPick,
44 Standard_Boolean theIsShift);
45extern void VT_ProcessButton1Release(Standard_Boolean theIsShift);
46
47extern int X_Motion; // Current cursor position
48extern int Y_Motion;
49extern int X_ButtonPress; // Last ButtonPress position
50extern int Y_ButtonPress;
51extern Standard_Boolean IsDragged;
52
53// =======================================================================
54// function : ViewerMainLoop
55// purpose :
56// =======================================================================
57int ViewerMainLoop (Standard_Integer, const char** )
58{
59 // unused
60 return 0;
61}
62
63// =======================================================================
64// function : ViewerTest_SetCocoaEventManagerView
65// purpose :
66// =======================================================================
67void ViewerTest_SetCocoaEventManagerView (const Handle(Cocoa_Window)& theWindow)
68{
69 if (theWindow.IsNull())
70 {
71 return;
72 }
73
74 NSWindow* aWin = [theWindow->HView() window];
75 NSRect aBounds = [[aWin contentView] bounds];
76
77 ViewerTest_CocoaEventManagerView* aView = [[ViewerTest_CocoaEventManagerView alloc] initWithFrame: aBounds];
78
79 // replace content view in the window
80 theWindow->SetHView (aView);
81
82 // make view as first responder in winow to capture all useful events
83 [aWin makeFirstResponder: aView];
84 [aWin setAcceptsMouseMovedEvents: YES];
85
86 // should be retained by parent NSWindow
87 [aView release];
88}
89
90// =======================================================================
91// function : getMouseCoords
92// purpose : Retrieve cursor position
93// =======================================================================
94static void getMouseCoords (NSView* theView,
95 NSEvent* theEvent,
96 Standard_Integer& theX,
97 Standard_Integer& theY)
98{
99 NSPoint aMouseLoc = [theView convertPoint: [theEvent locationInWindow] fromView: nil];
100 NSRect aBounds = [theView bounds];
101
102 theX = Standard_Integer(aMouseLoc.x);
103 theY = Standard_Integer(aBounds.size.height - aMouseLoc.y);
104}
105
106@implementation ViewerTest_CocoaEventManagerView
107
108// =======================================================================
109// function : setFrameSize
110// purpose :
111// =======================================================================
112- (void )setFrameSize: (NSSize )theNewSize
113{
114 [super setFrameSize: theNewSize];
115 VT_ProcessConfigure();
116}
117
118// =======================================================================
119// function : drawRect
120// purpose :
121// =======================================================================
122- (void )drawRect: (NSRect )theDirtyRect
123{
124 VT_ProcessExpose();
125}
126
127// =======================================================================
128// function : mouseMoved
129// purpose :
130// =======================================================================
131- (void )mouseMoved: (NSEvent* )theEvent
132{
133 getMouseCoords (self, theEvent, X_Motion, Y_Motion);
134 VT_ProcessMotion();
135}
136
137// =======================================================================
138// function : acceptsFirstResponder
139// purpose :
140// =======================================================================
141- (BOOL )acceptsFirstResponder
142{
143 return YES;
144}
145
146// =======================================================================
147// function : mouseDown
148// purpose :
149// =======================================================================
150- (void )mouseDown: (NSEvent* )theEvent
151{
152 getMouseCoords (self, theEvent, X_ButtonPress, Y_ButtonPress);
153 VT_ProcessButton1Press (0, NULL, Standard_False, [theEvent modifierFlags] & NSShiftKeyMask);
154}
155
156// =======================================================================
157// function : mouseUp
158// purpose :
159// =======================================================================
160- (void )mouseUp: (NSEvent* )theEvent
161{
162 getMouseCoords (self, theEvent, X_Motion, Y_Motion);
163 VT_ProcessButton1Release([theEvent modifierFlags] & NSShiftKeyMask);
164}
165
166
167// =======================================================================
168// function : mouseDragged
169// purpose :
170// =======================================================================
171- (void )mouseDragged: (NSEvent* )theEvent
172{
173 IsDragged = Standard_True;
174 if ([theEvent modifierFlags] & NSControlKeyMask)
175 {
176 getMouseCoords (self, theEvent, X_Motion, Y_Motion);
177 VT_ProcessControlButton2Motion();
178 }
179}
180
181// =======================================================================
182// function : rightMouseDown
183// purpose :
184// =======================================================================
185- (void )rightMouseDown: (NSEvent* )theEvent
186{
187 getMouseCoords (self, theEvent, X_ButtonPress, Y_ButtonPress);
188 VT_ProcessButton3Press(); // Start rotation
189}
190
191// =======================================================================
192// function : rightMouseUp
193// purpose :
194// =======================================================================
195- (void )rightMouseUp: (NSEvent* )theEvent
196{
197 VT_ProcessButton3Release();
198}
199
200// =======================================================================
201// function : rightMouseDragged
202// purpose :
203// =======================================================================
204- (void )rightMouseDragged: (NSEvent* )theEvent
205{
206 if ([theEvent modifierFlags] & NSControlKeyMask)
207 {
208 getMouseCoords (self, theEvent, X_Motion, Y_Motion);
209 VT_ProcessControlButton3Motion();
210 }
211}
212
213// =======================================================================
214// function : scrollWheel
215// purpose :
216// =======================================================================
217- (void )scrollWheel: (NSEvent* )theEvent
218{
219 float aDelta = [theEvent deltaY];
220 if (Abs (aDelta) < 0.001)
221 {
222 // a lot of values near zero can be generated by touchpad
223 return;
224 }
225
226 ViewerTest::CurrentView()->Zoom (0, 0, aDelta, aDelta);
227}
228
229// =======================================================================
230// function : keyDown
231// purpose :
232// =======================================================================
233- (void )keyDown: (NSEvent* )theEvent
234{
235 NSString* aStringNs = [theEvent characters];
236 if (aStringNs == NULL || [aStringNs length] == 0)
237 {
238 return;
239 }
240
241 const Standard_CString aString = [aStringNs UTF8String];
242 VT_ProcessKeyPress (aString);
243}
244
245@end
246
247#endif