/ include / AppKit / NSMenuItem.hpp
NSMenuItem.hpp
  1  /*
  2   *
  3   * Copyright 2020-2021 Apple Inc.
  4   *
  5   * Licensed under the Apache License, Version 2.0 (the "License");
  6   * you may not use this file except in compliance with the License.
  7   * You may obtain a copy of the License at
  8   *
  9   *     http://www.apache.org/licenses/LICENSE-2.0
 10   *
 11   * Unless required by applicable law or agreed to in writing, software
 12   * distributed under the License is distributed on an "AS IS" BASIS,
 13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14   * See the License for the specific language governing permissions and
 15   * limitations under the License.
 16   */
 17  
 18  //-------------------------------------------------------------------------------------------------------------------------------------------------------------
 19  //
 20  // AppKit/NSMenuItem.hpp
 21  //
 22  //-------------------------------------------------------------------------------------------------------------------------------------------------------------
 23  
 24  #pragma once
 25  
 26  //-------------------------------------------------------------------------------------------------------------------------------------------------------------
 27  
 28  #include <Foundation/NSPrivate.hpp>
 29  #include "AppKitPrivate.hpp"
 30  #include <string>
 31  
 32  namespace NS
 33  {
 34  	_NS_OPTIONS( NS::UInteger, KeyEquivalentModifierMask )
 35  	{
 36  		EventModifierFlagCapsLock			= 1 << 16, // Set if Caps Lock key is pressed.
 37  		EventModifierFlagShift				= 1 << 17, // Set if Shift key is pressed.
 38  		EventModifierFlagControl			= 1 << 18, // Set if Control key is pressed.
 39  		EventModifierFlagOption				= 1 << 19, // Set if Option or Alternate key is pressed.
 40  		EventModifierFlagCommand			= 1 << 20, // Set if Command key is pressed.
 41  		EventModifierFlagNumericPad			= 1 << 21, // Set if any key in the numeric keypad is pressed.
 42  		EventModifierFlagHelp				= 1 << 22, // Set if the Help key is pressed.
 43  		EventModifierFlagFunction			= 1 << 23, // Set if any function key is pressed.
 44  		
 45  		// Used to retrieve only the device-independent modifier flags, allowing applications to mask off the device-dependent modifier flags, including event coalescing information.
 46  		EventModifierFlagDeviceIndependentFlagsMask	= 0xffff0000UL
 47  	};
 48  
 49  	typedef void (*MenuItemCallback)( void* unused, SEL name, const NS::Object* pSender );
 50  
 51  	class MenuItem : public NS::Referencing< MenuItem >
 52  	{
 53  		public:
 54  			static SEL						registerActionCallback( const char* name, MenuItemCallback callback );
 55  			static MenuItem*				alloc();
 56  			MenuItem*						init();
 57  
 58  			void							setKeyEquivalentModifierMask( NS::KeyEquivalentModifierMask modifierMask );
 59  			NS::KeyEquivalentModifierMask	KeyEquivalentModifierMask() const;
 60  			void							setSubmenu( const class Menu* pSubmenu );
 61  	};
 62  }
 63  
 64  
 65  _NS_INLINE SEL NS::MenuItem::registerActionCallback( const char* name, NS::MenuItemCallback callback )
 66  {
 67  	auto siz = strlen( name );
 68  	SEL sel;
 69  	if ( ( siz > 0 ) && ( name[ siz - 1 ] != ':' ) )
 70  	{
 71  		char* colName = (char *)alloca( siz + 2 );
 72  		memcpy( colName, name, siz );
 73  		colName[ siz ] = ':';
 74  		colName[ siz + 1 ] = '\0';
 75  		sel = sel_registerName( colName );
 76  	}
 77  	else
 78  	{
 79  		sel = sel_registerName( name );
 80  	}
 81  
 82  	if ( callback )
 83  	{
 84  		class_addMethod( (Class)_NS_PRIVATE_CLS( NSObject ), sel, (IMP)callback, "v@:@" );
 85  	}
 86  	return sel;
 87  }
 88  
 89  _NS_INLINE NS::MenuItem* NS::MenuItem::alloc()
 90  {
 91  	return Object::alloc< NS::MenuItem >( _NS_PRIVATE_CLS( NSMenuItem ) );
 92  }
 93  
 94  _NS_INLINE NS::MenuItem* NS::MenuItem::init()
 95  {
 96  	return Object::sendMessage< NS::MenuItem* >( this, _NS_PRIVATE_SEL( init ) );
 97  }
 98  
 99  _NS_INLINE void NS::MenuItem::setKeyEquivalentModifierMask( NS::KeyEquivalentModifierMask modifierMask )
100  {
101  	return Object::sendMessage< void >( this, _NS_PRIVATE_SEL( setKeyEquivalentModifierMask_ ), modifierMask );
102  }
103  
104  _NS_INLINE NS::KeyEquivalentModifierMask NS::MenuItem::KeyEquivalentModifierMask() const
105  {
106  	return Object::sendMessage< NS::KeyEquivalentModifierMask >( this, _NS_PRIVATE_SEL( keyEquivalentModifierMask ) );
107  }
108  
109  _NS_INLINE void NS::MenuItem::setSubmenu( const class NS::Menu* pSubmenu )
110  {
111  	Object::sendMessage< void >( this, _NS_PRIVATE_SEL( setSubmenu_ ), pSubmenu );
112  }