00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 using System;
00011 using System.Runtime.InteropServices;
00012 using System.Diagnostics;
00013 using System.Drawing;
00014 using EventTime = System.Double;
00015
00016
00017 namespace OpenTK.Platform.MacOS.Carbon
00018 {
00019
00020 #region --- Types defined in MacTypes.h ---
00021
00022 [StructLayout(LayoutKind.Sequential)]
00023 internal struct CarbonPoint
00024 {
00025 internal short V;
00026 internal short H;
00027
00028 public CarbonPoint(int x, int y)
00029 {
00030 V = (short)x;
00031 H = (short)y;
00032 }
00033 }
00034
00035 [StructLayout(LayoutKind.Sequential)]
00036 internal struct Rect
00037 {
00038 short top;
00039 short left;
00040 short bottom;
00041 short right;
00042
00043 internal Rect(short _left, short _top, short _width, short _height)
00044 {
00045 top = _top;
00046 left = _left;
00047 bottom = (short)(_top + _height);
00048 right = (short)(_left + _width);
00049 }
00050
00051 internal short X
00052 {
00053 get { return left; }
00054 set
00055 {
00056 short width = Width;
00057 left = value;
00058 right = (short)(left + width);
00059 }
00060 }
00061 internal short Y
00062 {
00063 get { return top; }
00064 set
00065 {
00066 short height = Height;
00067 top = value;
00068 bottom = (short)(top + height);
00069 }
00070 }
00071 internal short Width
00072 {
00073 get { return (short)(right - left); }
00074 set { right = (short)(left + value); }
00075 }
00076 internal short Height
00077 {
00078 get { return (short)(bottom - top); }
00079 set { bottom = (short)(top + value); }
00080 }
00081
00082 public override string ToString()
00083 {
00084 return string.Format(
00085 "Rect: [{0}, {1}, {2}, {3}]", X, Y, Width, Height);
00086 }
00087
00088 public Rectangle ToRectangle()
00089 {
00090 return new Rectangle(X, Y, Width, Height);
00091 }
00092 }
00093
00094 #endregion
00095 #region --- Types defined in HIGeometry.h ---
00096
00097 [StructLayout(LayoutKind.Sequential)]
00098 internal struct HIPoint
00099 {
00100 public float X;
00101 public float Y;
00102 }
00103 [StructLayout(LayoutKind.Sequential)]
00104 internal struct HISize
00105 {
00106 public float Width;
00107 public float Height;
00108 }
00109 [StructLayout(LayoutKind.Sequential)]
00110 internal struct HIRect
00111 {
00112 public HIPoint Origin;
00113 public HISize Size;
00114
00115 public override string ToString()
00116 {
00117 return string.Format(
00118 "Rect: [{0}, {1}, {2}, {3}]", Origin.X, Origin.Y, Size.Width, Size.Height);
00119 }
00120 }
00121
00122 #endregion
00123
00124 #region --- Types defined in CarbonEvents.h ---
00125
00126 enum EventAttributes : uint
00127 {
00128 kEventAttributeNone = 0,
00129 kEventAttributeUserEvent = (1 << 0),
00130 kEventAttributeMonitored = 1 << 3,
00131 }
00132
00133 [StructLayout(LayoutKind.Sequential)]
00134 internal struct EventTypeSpec
00135 {
00136 internal EventTypeSpec(EventClass evtClass, AppEventKind evtKind)
00137 {
00138 this.EventClass = evtClass;
00139 this.EventKind = (uint)evtKind;
00140 }
00141 internal EventTypeSpec(EventClass evtClass, AppleEventKind appleKind)
00142 {
00143 this.EventClass = evtClass;
00144 this.EventKind = (uint)appleKind;
00145 }
00146 internal EventTypeSpec(EventClass evtClass, MouseEventKind evtKind)
00147 {
00148 this.EventClass = evtClass;
00149 this.EventKind = (uint)evtKind;
00150 }
00151 internal EventTypeSpec(EventClass evtClass, KeyboardEventKind evtKind)
00152 {
00153 this.EventClass = evtClass;
00154 this.EventKind = (uint)evtKind;
00155 }
00156 internal EventTypeSpec(EventClass evtClass, WindowEventKind evtKind)
00157 {
00158 this.EventClass = evtClass;
00159 this.EventKind = (uint)evtKind;
00160 }
00161
00162 internal EventClass EventClass;
00163 internal uint EventKind;
00164 }
00165
00166 internal enum EventClass : int
00167 {
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 Mouse = 0x6d6f7573,
00180 Keyboard = 0x6b657962,
00181 Application = 0x6170706c,
00182 AppleEvent = 0x65707063,
00183 Menu = 0x6d656e75,
00184 Window = 0x77696e64,
00185 }
00186 internal enum WindowEventKind : int
00187 {
00188
00189 WindowUpdate = 1,
00190 WindowDrawContent = 2,
00191 WindowDrawStructure = 3,
00192 WindowEraseContent = 4,
00193 WindowActivate = 5,
00194 WindowDeactivate = 6,
00195 WindowSizeChanged = 23,
00196 WindowBoundsChanging = 26,
00197 WindowBoundsChanged = 27,
00198 WindowClickDragRgn = 32,
00199 WindowClickResizeRgn = 33,
00200 WindowClickCollapseRgn = 34,
00201 WindowClickCloseRgn = 35,
00202 WindowClickZoomRgn = 36,
00203 WindowClickContentRgn = 37,
00204 WindowClickProxyIconRgn = 38,
00205 WindowClose = 72,
00206 WindowClosed = 73,
00207 }
00208 internal enum MouseEventKind : int
00209 {
00210 MouseDown = 1,
00211 MouseUp = 2,
00212 MouseMoved = 5,
00213 MouseDragged = 6,
00214 MouseEntered = 8,
00215 MouseExited = 9,
00216 WheelMoved = 10,
00217 }
00218 internal enum MouseButton : short
00219 {
00220 Primary = 1,
00221 Secondary = 2,
00222 Tertiary = 3,
00223 }
00224
00225 internal enum KeyboardEventKind : int
00226 {
00227
00228 RawKeyDown = 1,
00229 RawKeyRepeat = 2,
00230 RawKeyUp = 3,
00231 RawKeyModifiersChanged = 4,
00232 }
00233
00234 internal enum AppEventKind : int
00235 {
00236
00237 AppActivated = 1,
00238 AppDeactivated = 2,
00239 AppQuit = 3,
00240 AppLaunchNotification = 4,
00241 }
00242
00243 enum AppleEventKind : int
00244 {
00245 AppleEvent = 1,
00246 }
00247
00248 internal enum EventParamName : int
00249 {
00250 WindowRef = 0x77696e64,
00251
00252
00253 MouseLocation = 0x6d6c6f63,
00254 WindowMouseLocation = 0x776d6f75,
00255 MouseButton = 0x6d62746e,
00256 ClickCount = 0x63636e74,
00257 MouseWheelAxis = 0x6d776178,
00258 MouseWheelDelta = 0x6d77646c,
00259 MouseDelta = 0x6d647461,
00260
00261
00262 KeyCode = 0x6b636f64,
00263 KeyMacCharCode = 0x6b636872,
00264 KeyModifiers = 0x6b6d6f64,
00265
00266 }
00267 internal enum EventParamType : int
00268 {
00269 typeWindowRef = 0x77696e64,
00270
00271 typeMouseButton = 0x6d62746e,
00272 typeMouseWheelAxis = 0x6d776178,
00273 typeHIPoint = 0x68697074,
00274 typeHISize = 0x6869737a,
00275 typeHIRect = 0x68697263,
00276
00277 typeChar = 0x54455854,
00278
00279 typeUInt32 = 0x6d61676e,
00280 typeSInt32 = 0x6c6f6e67,
00281 typeSInt16 = 0x73686f72,
00282 typeSInt64 = 0x636f6d70,
00283 typeIEEE32BitFloatingPoint = 0x73696e67,
00284 typeIEEE64BitFloatingPoint = 0x646f7562,
00285 }
00286
00287 internal enum EventMouseButton : int
00288 {
00289 Primary = 0,
00290 Secondary = 1,
00291 Tertiary = 2,
00292 }
00293
00294 internal enum WindowRegionCode : int
00295 {
00296 TitleBarRegion = 0,
00297 TitleTextRegion = 1,
00298 CloseBoxRegion = 2,
00299 ZoomBoxRegion = 3,
00300 DragRegion = 5,
00301 GrowRegion = 6,
00302 CollapseBoxRegion = 7,
00303 TitleProxyIconRegion = 8,
00304 StructureRegion = 32,
00305 ContentRegion = 33,
00306 UpdateRegion = 34,
00307 OpaqueRegion = 35,
00308 GlobalPortRegion = 40,
00309 ToolbarButtonRegion = 41
00310 };
00311
00312 #endregion
00313
00314 #region --- MacWindows.h ---
00315
00316 internal enum WindowClass : uint
00317 {
00318 Alert = 1,
00319 MovableAlert = 2,
00320 Modal = 3,
00321 MovableModal = 4,
00322 Floating = 5,
00323 Document = 6,
00324 Desktop = 7,
00325 Utility = 8,
00326 Help = 10,
00327 Sheet = 11,
00328 Toolbar = 12,
00329 Plain = 13,
00330 Overlay = 14,
00331 SheetAlert = 15,
00332 AltPlain = 16,
00333 Drawer = 20,
00334 All = 0xFFFFFFFFu
00335 }
00336
00337 [Flags]
00338 internal enum WindowAttributes : uint
00339 {
00340 NoAttributes = 0u,
00341 CloseBox = (1u << 0),
00342 HorizontalZoom = (1u << 1),
00343 VerticalZoom = (1u << 2),
00344 FullZoom = (VerticalZoom | HorizontalZoom),
00345 CollapseBox = (1u << 3),
00346 Resizable = (1u << 4),
00347 SideTitlebar = (1u << 5),
00348 NoUpdates = (1u << 16),
00349 NoActivates = (1u << 17),
00350 NoBuffering = (1u << 20),
00351 StandardHandler = (1u << 25),
00352 InWindowMenu = (1u << 27),
00353 LiveResize = (1u << 28),
00354 StandardDocument = (CloseBox | FullZoom | CollapseBox | Resizable),
00355 StandardFloating = (CloseBox | CollapseBox)
00356 }
00357
00358 internal enum WindowPositionMethod : uint
00359 {
00360 CenterOnMainScreen = 1,
00361 CenterOnParentWindow = 2,
00362 CenterOnParentWindowScreen = 3,
00363 CascadeOnMainScreen = 4,
00364 CascadeOnParentWindow = 5,
00365 CascadeOnParentWindowScreen = 6,
00366 CascadeStartAtParentWindowScreen = 10,
00367 AlertPositionOnMainScreen = 7,
00368 AlertPositionOnParentWindow = 8,
00369 AlertPositionOnParentWindowScreen = 9
00370 }
00371
00372 internal delegate OSStatus MacOSEventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData);
00373
00374 internal enum WindowPartCode : short
00375 {
00376 inDesk = 0,
00377 inNoWindow = 0,
00378 inMenuBar = 1,
00379 inSysWindow = 2,
00380 inContent = 3,
00381 inDrag = 4,
00382 inGrow = 5,
00383 inGoAway = 6,
00384 inZoomIn = 7,
00385 inZoomOut = 8,
00386 inCollapseBox = 11,
00387 inProxyIcon = 12,
00388 inToolbarButton = 13,
00389 inStructure = 15,
00390 }
00391
00392 #endregion
00393 #region --- Enums from gestalt.h ---
00394
00395 internal enum GestaltSelector
00396 {
00397 SystemVersion = 0x73797376,
00398 SystemVersionMajor = 0x73797331,
00399 SystemVersionMinor = 0x73797332,
00400 SystemVersionBugFix = 0x73797333,
00401 };
00402
00403 #endregion
00404 #region --- Process Manager ---
00405
00406 enum ProcessApplicationTransformState : int
00407 {
00408 kProcessTransformToForegroundApplication = 1,
00409 }
00410
00411 struct ProcessSerialNumber
00412 {
00413 public ulong high;
00414 public ulong low;
00415 }
00416
00417 #endregion
00418
00419
00420 enum HICoordinateSpace
00421 {
00422 _72DPIGlobal = 1,
00423 ScreenPixel = 2,
00424 Window = 3,
00425 View = 4
00426 };
00427
00428 #region --- Carbon API Methods ---
00429
00430 internal partial class API
00431 {
00432 const string carbon = "/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon";
00433
00434 [DllImport(carbon)]
00435 internal static extern EventClass GetEventClass(IntPtr inEvent);
00436 [DllImport(carbon)]
00437 internal static extern uint GetEventKind(IntPtr inEvent);
00438
00439 #region --- Window Construction ---
00440
00441 [DllImport(carbon,EntryPoint="CreateNewWindow")]
00442 private static extern OSStatus _CreateNewWindow(WindowClass @class, WindowAttributes attributes, ref Rect r, out IntPtr window);
00443
00444 internal static IntPtr CreateNewWindow(WindowClass @class, WindowAttributes attributes, Rect r)
00445 {
00446 IntPtr retval;
00447 OSStatus stat = _CreateNewWindow(@class, attributes, ref r, out retval);
00448
00449 Debug.Print("Created Window: {0}", retval);
00450
00451 if (stat != OSStatus.NoError)
00452 {
00453 throw new MacOSException(stat);
00454 }
00455
00456 return retval;
00457 }
00458
00459 [DllImport(carbon)]
00460 internal static extern void DisposeWindow(IntPtr window);
00461
00462 #endregion
00463 #region --- Showing / Hiding Windows ---
00464
00465 [DllImport(carbon)]
00466 internal static extern void ShowWindow(IntPtr window);
00467 [DllImport(carbon)]
00468 internal static extern void HideWindow(IntPtr window);
00469 [DllImport(carbon)]
00470 internal static extern bool IsWindowVisible(IntPtr window);
00471 [DllImport(carbon)]
00472 internal static extern void SelectWindow(IntPtr window);
00473
00474 #endregion
00475 #region --- Window Boundaries ---
00476
00477 [DllImport(carbon)]
00478 internal static extern OSStatus RepositionWindow(IntPtr window, IntPtr parentWindow, WindowPositionMethod method);
00479 [DllImport(carbon)]
00480 internal static extern void SizeWindow(IntPtr window, short w, short h, bool fUpdate);
00481 [DllImport(carbon)]
00482 internal static extern void MoveWindow(IntPtr window, short x, short y, bool fUpdate);
00483
00484 [DllImport(carbon)]
00485 static extern OSStatus GetWindowBounds(IntPtr window, WindowRegionCode regionCode, out Rect globalBounds);
00486 internal static Rect GetWindowBounds(IntPtr window, WindowRegionCode regionCode)
00487 {
00488 Rect retval;
00489 OSStatus result = GetWindowBounds(window, regionCode, out retval);
00490
00491 if (result != OSStatus.NoError)
00492 throw new MacOSException(result);
00493
00494 return retval;
00495 }
00496
00497
00498
00499
00500 #endregion
00501 #region --- Processing Events ---
00502
00503 [DllImport(carbon)]
00504 static extern IntPtr GetEventDispatcherTarget();
00505
00506 [DllImport(carbon,EntryPoint="ReceiveNextEvent")]
00507 static extern OSStatus ReceiveNextEvent(uint inNumTypes,
00508 IntPtr inList,
00509 double inTimeout,
00510 bool inPullEvent,
00511 out IntPtr outEvent);
00512
00513 [DllImport(carbon)]
00514 static extern void SendEventToEventTarget(IntPtr theEvent, IntPtr theTarget);
00515
00516 [DllImport(carbon)]
00517 static extern void ReleaseEvent(IntPtr theEvent);
00518
00519 internal static void SendEvent(IntPtr theEvent)
00520 {
00521 IntPtr theTarget = GetEventDispatcherTarget();
00522 SendEventToEventTarget(theEvent, theTarget);
00523 }
00524
00525
00526 internal static void ProcessEvents()
00527 {
00528 IntPtr theEvent;
00529 IntPtr theTarget = GetEventDispatcherTarget();
00530
00531 for (;;)
00532 {
00533 OSStatus status = ReceiveNextEvent(0, IntPtr.Zero, 0.0, true, out theEvent);
00534
00535 if (status == OSStatus.EventLoopTimedOut)
00536 break;
00537
00538 if (status != OSStatus.NoError)
00539 {
00540 Debug.Print("Message Loop status: {0}", status);
00541 break;
00542 }
00543 if (theEvent == IntPtr.Zero)
00544 break;
00545
00546 try
00547 {
00548 SendEventToEventTarget(theEvent, theTarget);
00549 }
00550 catch (System.ExecutionEngineException e)
00551 {
00552 Console.Error.WriteLine("ExecutionEngineException caught.");
00553 Console.Error.WriteLine("theEvent: " + new EventInfo(theEvent).ToString());
00554 Console.Error.WriteLine(e.Message);
00555 Console.Error.WriteLine(e.StackTrace);
00556 }
00557
00558 ReleaseEvent(theEvent);
00559 }
00560
00561 }
00562
00563 #region --- Processing apple event ---
00564
00565 [StructLayout(LayoutKind.Sequential)]
00566
00567 struct EventRecord
00568 {
00569 public ushort what;
00570 public uint message;
00571 public uint when;
00572 public CarbonPoint where;
00573 public uint modifiers;
00574 }
00575
00576 [DllImport(carbon)]
00577 static extern bool ConvertEventRefToEventRecord(IntPtr inEvent, out EventRecord outEvent);
00578
00579 [DllImport(carbon)]
00580 static extern OSStatus AEProcessAppleEvent(ref EventRecord theEventRecord);
00581
00582 static internal void ProcessAppleEvent(IntPtr inEvent)
00583 {
00584 EventRecord record;
00585
00586 ConvertEventRefToEventRecord(inEvent, out record);
00587 AEProcessAppleEvent(ref record);
00588 }
00589
00590 #endregion
00591
00592 #endregion
00593 #region --- Getting Event Parameters ---
00594
00595 [DllImport(carbon,EntryPoint="CreateEvent")]
00596 static extern OSStatus _CreateEvent( IntPtr inAllocator,
00597 EventClass inClassID, UInt32 kind, EventTime when,
00598 EventAttributes flags,out IntPtr outEvent);
00599
00600 internal static IntPtr CreateWindowEvent(WindowEventKind kind)
00601 {
00602 IntPtr retval;
00603
00604 OSStatus stat = _CreateEvent(IntPtr.Zero, EventClass.Window, (uint)kind,
00605 0, EventAttributes.kEventAttributeNone, out retval);
00606
00607 if (stat != OSStatus.NoError)
00608 {
00609 throw new MacOSException(stat);
00610 }
00611
00612 return retval;
00613 }
00614
00615 [DllImport(carbon)]
00616 static extern OSStatus GetEventParameter(
00617 IntPtr inEvent, EventParamName inName, EventParamType inDesiredType,
00618 IntPtr outActualType, uint inBufferSize, IntPtr outActualSize, IntPtr outData);
00619
00620 static internal MacOSKeyCode GetEventKeyboardKeyCode(IntPtr inEvent)
00621 {
00622 int code;
00623
00624 unsafe
00625 {
00626 int* codeAddr = &code;
00627
00628 OSStatus result = API.GetEventParameter(inEvent,
00629 EventParamName.KeyCode, EventParamType.typeUInt32, IntPtr.Zero,
00630 (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(UInt32)), IntPtr.Zero,
00631 (IntPtr) codeAddr);
00632
00633 if (result != OSStatus.NoError)
00634 {
00635 throw new MacOSException(result);
00636 }
00637 }
00638
00639 return (MacOSKeyCode)code;
00640 }
00641
00642 internal static char GetEventKeyboardChar(IntPtr inEvent)
00643 {
00644 char code;
00645
00646 unsafe
00647 {
00648 char* codeAddr = &code;
00649
00650 OSStatus result = API.GetEventParameter(inEvent,
00651 EventParamName.KeyMacCharCode, EventParamType.typeChar, IntPtr.Zero,
00652 (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(char)), IntPtr.Zero,
00653 (IntPtr)codeAddr);
00654
00655 if (result != OSStatus.NoError)
00656 {
00657 throw new MacOSException(result);
00658 }
00659 }
00660
00661 return code;
00662 }
00663
00664 static internal MouseButton GetEventMouseButton(IntPtr inEvent)
00665 {
00666 int button;
00667
00668 unsafe
00669 {
00670 int* btn = &button;
00671
00672 OSStatus result = API.GetEventParameter(inEvent,
00673 EventParamName.MouseButton, EventParamType.typeMouseButton, IntPtr.Zero,
00674 (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(short)), IntPtr.Zero,
00675 (IntPtr)btn);
00676
00677 if (result != OSStatus.NoError)
00678 throw new MacOSException(result);
00679 }
00680
00681 return (MouseButton)button;
00682 }
00683 static internal int GetEventMouseWheelDelta(IntPtr inEvent)
00684 {
00685 int delta;
00686
00687 unsafe
00688 {
00689 int* d = δ
00690
00691 OSStatus result = API.GetEventParameter(inEvent,
00692 EventParamName.MouseWheelDelta, EventParamType.typeSInt32,
00693 IntPtr.Zero, (uint)sizeof(int), IntPtr.Zero, (IntPtr)d);
00694
00695 if (result != OSStatus.NoError)
00696 throw new MacOSException(result);
00697 }
00698
00699 return delta;
00700 }
00701
00702 static internal OSStatus GetEventWindowMouseLocation(IntPtr inEvent, out HIPoint pt)
00703 {
00704 HIPoint point;
00705
00706 unsafe
00707 {
00708 HIPoint* parm = &point;
00709
00710 OSStatus result = API.GetEventParameter(inEvent,
00711 EventParamName.WindowMouseLocation, EventParamType.typeHIPoint, IntPtr.Zero,
00712 (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(HIPoint)), IntPtr.Zero,
00713 (IntPtr)parm);
00714
00715 pt = point;
00716
00717 return result;
00718 }
00719
00720 }
00721
00722 static internal OSStatus GetEventWindowRef(IntPtr inEvent, out IntPtr windowRef)
00723 {
00724 IntPtr retval;
00725
00726 unsafe
00727 {
00728 IntPtr* parm = &retval;
00729 OSStatus result = API.GetEventParameter(inEvent,
00730 EventParamName.WindowRef, EventParamType.typeWindowRef, IntPtr.Zero,
00731 (uint)sizeof(IntPtr), IntPtr.Zero, (IntPtr)parm);
00732
00733 windowRef = retval;
00734
00735 return result;
00736 }
00737 }
00738
00739 static internal OSStatus GetEventMouseLocation(IntPtr inEvent, out HIPoint pt)
00740 {
00741 HIPoint point;
00742
00743 unsafe
00744 {
00745 HIPoint* parm = &point;
00746
00747 OSStatus result = API.GetEventParameter(inEvent,
00748 EventParamName.MouseLocation, EventParamType.typeHIPoint, IntPtr.Zero,
00749 (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(HIPoint)), IntPtr.Zero,
00750 (IntPtr)parm);
00751
00752 pt = point;
00753
00754 return result;
00755 }
00756
00757 }
00758 static internal MacOSKeyModifiers GetEventKeyModifiers(IntPtr inEvent)
00759 {
00760 uint code;
00761
00762 unsafe
00763 {
00764 uint* codeAddr = &code;
00765
00766 OSStatus result = API.GetEventParameter(inEvent,
00767 EventParamName.KeyModifiers, EventParamType.typeUInt32, IntPtr.Zero,
00768 (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(uint)), IntPtr.Zero,
00769 (IntPtr)codeAddr);
00770
00771 if (result != OSStatus.NoError)
00772 {
00773 throw new MacOSException(result);
00774 }
00775 }
00776
00777 return (MacOSKeyModifiers)code;
00778 }
00779
00780 #endregion
00781 #region --- Event Handlers ---
00782
00783 [DllImport(carbon,EntryPoint="InstallEventHandler")]
00784 static extern OSStatus _InstallEventHandler(
00785 IntPtr eventTargetRef, IntPtr handlerProc,
00786 int numtypes, EventTypeSpec[] typeList,
00787 IntPtr userData, IntPtr handlerRef);
00788
00789 internal static void InstallWindowEventHandler(IntPtr windowRef, IntPtr uppHandlerProc,
00790 EventTypeSpec[] eventTypes, IntPtr userData, IntPtr handlerRef)
00791 {
00792 IntPtr windowTarget = GetWindowEventTarget(windowRef);
00793
00794
00795
00796
00797
00798
00799
00800
00801 OSStatus error = _InstallEventHandler(windowTarget, uppHandlerProc,
00802 eventTypes.Length, eventTypes,
00803 userData, handlerRef);
00804
00805
00806
00807 if (error != OSStatus.NoError)
00808 {
00809 throw new MacOSException(error);
00810 }
00811 }
00812
00813 internal static void InstallApplicationEventHandler(IntPtr uppHandlerProc,
00814 EventTypeSpec[] eventTypes, IntPtr userData, IntPtr handlerRef)
00815 {
00816
00817 OSStatus error = _InstallEventHandler(GetApplicationEventTarget(), uppHandlerProc,
00818 eventTypes.Length, eventTypes,
00819 userData, handlerRef);
00820
00821 if (error != OSStatus.NoError)
00822 {
00823 throw new MacOSException(error);
00824 }
00825
00826 }
00827
00828 [DllImport(carbon)]
00829 internal static extern OSStatus RemoveEventHandler(IntPtr inHandlerRef);
00830
00831 #endregion
00832 #region --- GetWindowEventTarget ---
00833
00834 [DllImport(carbon)]
00835 internal static extern IntPtr GetWindowEventTarget(IntPtr window);
00836
00837 [DllImport(carbon)]
00838 internal static extern IntPtr GetApplicationEventTarget();
00839
00840 #endregion
00841 #region --- UPP Event Handlers ---
00842
00843 [DllImport(carbon)]
00844 internal static extern IntPtr NewEventHandlerUPP(MacOSEventHandler handler);
00845
00846 [DllImport(carbon)]
00847 internal static extern void DisposeEventHandlerUPP(IntPtr userUPP);
00848
00849 #endregion
00850 #region --- Process Manager ---
00851
00852 [DllImport(carbon)]
00853 internal static extern int TransformProcessType(ref Carbon.ProcessSerialNumber psn, ProcessApplicationTransformState type);
00854 [DllImport(carbon)]
00855 internal static extern int GetCurrentProcess(ref Carbon.ProcessSerialNumber psn);
00856 [DllImport(carbon)]
00857 internal static extern int SetFrontProcess(ref Carbon.ProcessSerialNumber psn);
00858
00859 #endregion
00860 #region --- Setting Dock Tile ---
00861
00862 [DllImport(carbon)]
00863 internal extern static IntPtr CGColorSpaceCreateDeviceRGB();
00864 [DllImport(carbon)]
00865 internal extern static IntPtr CGDataProviderCreateWithData(IntPtr info, IntPtr[] data, int size, IntPtr releasefunc);
00866 [DllImport(carbon)]
00867 internal extern static IntPtr CGImageCreate(int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow, IntPtr colorspace, uint bitmapInfo, IntPtr provider, IntPtr decode, int shouldInterpolate, int intent);
00868 [DllImport(carbon)]
00869 internal extern static void SetApplicationDockTileImage(IntPtr imageRef);
00870 [DllImport(carbon)]
00871 internal extern static void RestoreApplicationDockTileImage();
00872
00873 #endregion
00874
00875 [DllImport(carbon)]
00876 static extern IntPtr GetControlBounds(IntPtr control, out Rect bounds);
00877
00878 internal static Rect GetControlBounds(IntPtr control)
00879 {
00880 Rect retval;
00881 GetControlBounds(control, out retval);
00882
00883 return retval;
00884 }
00885
00886 [DllImport(carbon)]
00887 internal static extern OSStatus ActivateWindow (IntPtr inWindow, bool inActivate);
00888
00889 [DllImport(carbon)]
00890 internal static extern void RunApplicationEventLoop();
00891
00892 [DllImport(carbon)]
00893 internal static extern void QuitApplicationEventLoop();
00894
00895 [DllImport(carbon)]
00896 internal static extern IntPtr GetControlOwner(IntPtr control);
00897
00898 [DllImport(carbon)]
00899 internal static extern IntPtr HIViewGetWindow(IntPtr inView);
00900
00901 [DllImport(carbon)]
00902 static extern OSStatus HIViewGetFrame(IntPtr inView, out HIRect outRect);
00903 internal static HIRect HIViewGetFrame(IntPtr inView)
00904 {
00905 HIRect retval;
00906 OSStatus result = HIViewGetFrame(inView, out retval);
00907
00908 if (result != OSStatus.NoError)
00909 throw new MacOSException(result);
00910
00911 return retval;
00912 }
00913 #region --- SetWindowTitle ---
00914
00915 [DllImport(carbon)]
00916 static extern void SetWindowTitleWithCFString(IntPtr windowRef, IntPtr title);
00917
00918 internal static void SetWindowTitle(IntPtr windowRef, string title)
00919 {
00920 IntPtr str = __CFStringMakeConstantString(title);
00921
00922 Debug.Print("Setting window title: {0}, CFstring : {1}, Text : {2}", windowRef, str, title);
00923
00924 SetWindowTitleWithCFString(windowRef, str);
00925
00926
00927
00928
00929
00930
00931 }
00932
00933 #endregion
00934
00935 [DllImport(carbon,EntryPoint="ChangeWindowAttributes")]
00936 static extern OSStatus _ChangeWindowAttributes(IntPtr windowRef, WindowAttributes setTheseAttributes, WindowAttributes clearTheseAttributes);
00937 internal static void ChangeWindowAttributes(IntPtr windowRef, WindowAttributes setTheseAttributes, WindowAttributes clearTheseAttributes)
00938 {
00939 OSStatus error = _ChangeWindowAttributes(windowRef, setTheseAttributes, clearTheseAttributes);
00940
00941 if (error != OSStatus.NoError)
00942 {
00943 throw new MacOSException(error);
00944 }
00945 }
00946
00947 [DllImport(carbon)]
00948 static extern IntPtr __CFStringMakeConstantString(string cStr);
00949
00950 [DllImport(carbon)]
00951 static extern void CFRelease(IntPtr cfStr);
00952
00953 [DllImport(carbon)]
00954 internal static extern OSStatus CallNextEventHandler(IntPtr nextHandler, IntPtr theEvent);
00955
00956 [DllImport(carbon)]
00957 internal static extern IntPtr GetWindowPort(IntPtr windowRef);
00958
00959 #region --- Menus ---
00960
00961 [DllImport(carbon)]
00962 internal static extern IntPtr AcquireRootMenu();
00963
00964
00965 #endregion
00966
00967 [DllImport(carbon)]
00968 internal static extern bool IsWindowCollapsed(IntPtr windowRef);
00969
00970 [DllImport(carbon, EntryPoint = "CollapseWindow")]
00971 static extern OSStatus _CollapseWindow(IntPtr windowRef, bool collapse);
00972
00973 internal static void CollapseWindow(IntPtr windowRef, bool collapse)
00974 {
00975 OSStatus error = _CollapseWindow(windowRef, collapse);
00976
00977 if (error != OSStatus.NoError)
00978 {
00979 throw new MacOSException(error);
00980 }
00981 }
00982
00983 [DllImport(carbon, EntryPoint="IsWindowInStandardState")]
00984 static extern bool _IsWindowInStandardState(IntPtr windowRef, IntPtr inIdealSize, IntPtr outIdealStandardState);
00985
00986 internal static bool IsWindowInStandardState(IntPtr windowRef)
00987 {
00988 return _IsWindowInStandardState(windowRef, IntPtr.Zero, IntPtr.Zero);
00989 }
00990
00991 [DllImport(carbon, EntryPoint = "ZoomWindowIdeal")]
00992 unsafe static extern OSStatus _ZoomWindowIdeal(IntPtr windowRef, short inPartCode, IntPtr toIdealSize);
00993
00994 internal static void ZoomWindowIdeal(IntPtr windowRef, WindowPartCode inPartCode, ref CarbonPoint toIdealSize)
00995 {
00996 CarbonPoint pt = toIdealSize;
00997 OSStatus error ;
00998 IntPtr handle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CarbonPoint)));
00999 Marshal.StructureToPtr(toIdealSize, handle, false);
01000
01001 error = _ZoomWindowIdeal(windowRef, (short)inPartCode, handle);
01002
01003 toIdealSize = (CarbonPoint)Marshal.PtrToStructure(handle,typeof(CarbonPoint));
01004
01005 Marshal.FreeHGlobal(handle);
01006
01007 if (error != OSStatus.NoError)
01008 {
01009 throw new MacOSException(error);
01010 }
01011 }
01012
01013 [DllImport(carbon)]
01014 internal unsafe static extern OSStatus DMGetGDeviceByDisplayID(
01015 IntPtr displayID, out IntPtr displayDevice, Boolean failToMain);
01016
01017 #region Nonworking HIPointConvert routines
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062 #endregion
01063
01064 const string gestaltlib = "/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon";
01065
01066
01067 [DllImport(gestaltlib)]
01068 internal static extern OSStatus Gestalt(GestaltSelector selector, out int response);
01069 }
01070
01071 #endregion
01072
01073 }
01074
01075