<commit_before>//=============================================================================
//
//	Copyright (C)1999,2000 by <NAME> <<EMAIL>>
//
// The contents of this file are copyrighted by <NAME>.  This work is
// distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE.  You may distribute this file provided that this
// copyright notice is retained.  Send comments to <<EMAIL>>.
//
//=============================================================================
//-----------------------------------------------------------------------------
// NeXTMenu.cpp
//
//	Generate a menu from an INI file definition.
//
//-----------------------------------------------------------------------------
#import "NeXTMenu.h"
extern "Objective-C" {
#import <AppKit/NSApplication.h>
#import <AppKit/NSMenu.h>
#import <AppKit/NSMenuItem.h>
}
#import "sysdef.h"
#import "csutil/inifile.h"

static NSMenu* build_menu( char const* section, csIniFile const& );

#define STR_SWITCH(X) { char const* switched_str__=(X); if (0) {}
#define STR_CASE(X) else if (strcmp(switched_str__,(#X)) == 0)
#define STR_SWITCH_END }

//-----------------------------------------------------------------------------
// OpenStep 4.2 --> MacOS/X Server compatibility
//-----------------------------------------------------------------------------
#if defined(OS_NEXT_MACOSXS)

static void menu_add_separator( NSMenu* m )
    { [m addItem:[NSMenuItem separatorItem]]; }

#else // !OS_NEXT_MACOSXS

static void menu_add_separator( NSMenu* m )
    { [[m addItemWithTitle:@"-" action:0 keyEquivalent:@""] setEnabled:NO]; }

@interface NSApplication (NeXTMenu)
- (void)setAppleMenu:(NSMenu*)m;
@end
@implementation NSApplication (NeXTMenu)
- (void)setAppleMenu:(NSMenu*)m {}
@end

#endif // OS_NEXT_MACOSXS


//-----------------------------------------------------------------------------
// menu_add_item
//	Looks up the named section in csIniFile.  Pays particular attention to
//	keys "type", "title", "shortcut", "action", & "target".  Adds a menu
//	item to the menu based upon the attributes of these keys.  "type", if
//	present, may specify "separator" in which case the menu item is a
//	"separator".  Otherwise, "title", "shortcut", & "action" are used to
//	generate the new menu item.  "target", if present, may be either
//	"application" which stands for NSApp, or "delegate" which stands for
//	[NSApp delegate].  If "target" is not specified then the item's action
//	is sent to the first-responder.
//-----------------------------------------------------------------------------
static void menu_add_item( NSMenu* menu, char const* key,
    csIniFile const& config )
    {
    char const* section = [[NSString stringWithFormat:@"Item.%s",key] cString];

    char const* type = config.GetStr( section, "type", 0 );
    if (type != 0 && strcmp( type, "separator" ) == 0)
	menu_add_separator( menu );
    else
	{
	char const* title    = config.GetStr( section, "title",    "" );
	char const* shortcut = config.GetStr( section, "shortcut", "" );
	char const* action   = config.GetStr( section, "action",   0  );

	SEL cmd = 0;
	if (action != 0)
	    cmd = NSSelectorFromString([NSString stringWithCString:action]);

	NSMenuItem* const item = [menu addItemWithTitle:
		[NSString stringWithCString:title] action:cmd
		keyEquivalent:[NSString stringWithCString:shortcut]];
	[item setKeyEquivalentModifierMask:NSCommandKeyMask];

	char const* target = config.GetStr( section, "target", 0 );
	if (target != 0)
	    {
	    STR_SWITCH (target)
		STR_CASE (application)
		    [item setTarget:NSApp];
		STR_CASE (delegate)
		    [item setTarget:[NSApp delegate]];
	    STR_SWITCH_END
	    }
	}
    }


//-----------------------------------------------------------------------------
// menu_add_submenu
//	Looks up the named section in csIniFile.  Recursively calls
//	build_menu() to generate the submenu.  Pays particular attention to
//	key "type" which, if present, may be one of "apple", "window", or
//	"services".  A TYPE qualification means that the menu should be
//	configured as the NSApplication's Apple, Window, or Services menu,
//	respectively.
//-----------------------------------------------------------------------------
static void menu_add_submenu( NSMenu* menu, char const* key,
    csIniFile const& config )
    {
    char const* section = [[NSString stringWithFormat:@"Menu.%s",key] cString];
    NSMenu* const sub = build_menu( section, config );
    if (sub != 0)
	{
	NSMenuItem* const item =
		[menu addItemWithTitle:[sub title] action:0 keyEquivalent:@""];
	[menu setSubmenu:sub forItem:item];

	char const* type = config.GetStr( section, "type", 0 );
	if (type != 0)
	    {
	    STR_SWITCH (type)
		STR_CASE (apple)
		    [NSApp setAppleMenu:sub];
		STR_CASE (window)
		    [NSApp setWindowsMenu:sub];
		STR_CASE (services)<commit_msg>Fixed bug with handling of submenu parsing.<commit_after>//=============================================================================
//
//	Copyright (C)1999,2000 by <NAME> <<EMAIL>>
//
// The contents of this file are copyrighted by <NAME>.  This work is
// distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE.  You may distribute this file provided that this
// copyright notice is retained.  Send comments to <<EMAIL>>.
//
//=============================================================================
//-----------------------------------------------------------------------------
// NeXTMenu.cpp
//
//	Generate a menu from an INI file definition.
//
//-----------------------------------------------------------------------------
#import "NeXTMenu.h"
extern "Objective-C" {
#import <AppKit/NSApplication.h>
#import <AppKit/NSMenu.h>
#import <AppKit/NSMenuItem.h>
}
#import "sysdef.h"
#import "csutil/inifile.h"

static NSMenu* build_menu( char const* section, csIniFile const& );

#define STR_SWITCH(X) { char const* switched_str__=(X); if (0) {}
#define STR_CASE(X) else if (strcmp(switched_str__,(#X)) == 0)
#define STR_SWITCH_END }

//-----------------------------------------------------------------------------
// OpenStep 4.2 --> MacOS/X Server compatibility
//-----------------------------------------------------------------------------
#if defined(OS_NEXT_MACOSXS)

static void menu_add_separator( NSMenu* m )
    { [m addItem:[NSMenuItem separatorItem]]; }

#else // !OS_NEXT_MACOSXS

static void menu_add_separator( NSMenu* m )
    { [[m addItemWithTitle:@"-" action:0 keyEquivalent:@""] setEnabled:NO]; }

@interface NSApplication (NeXTMenu)
- (void)setAppleMenu:(NSMenu*)m;
@end
@implementation NSApplication (NeXTMenu)
- (void)setAppleMenu:(NSMenu*)m {}
@end

#endif // OS_NEXT_MACOSXS


//-----------------------------------------------------------------------------
// menu_add_item
//	Looks up the named section in csIniFile.  Pays particular attention to
//	keys "type", "title", "shortcut", "action", & "target".  Adds a menu
//	item to the menu based upon the attributes of these keys.  "type", if
//	present, may specify "separator" in which case the menu item is a
//	"separator".  Otherwise, "title", "shortcut", & "action" are used to
//	generate the new menu item.  "target", if present, may be either
//	"application" which stands for NSApp, or "delegate" which stands for
//	[NSApp delegate].  If "target" is not specified then the item's action
//	is sent to the first-responder.
//-----------------------------------------------------------------------------
static void menu_add_item( NSMenu* menu, char const* key,
    csIniFile const& config )
    {
    char const* section = [[NSString stringWithFormat:@"Item.%s",key] cString];

    char const* type = config.GetStr( section, "type", 0 );
    if (type != 0 && strcmp( type, "separator" ) == 0)
	menu_add_separator( menu );
    else
	{
	char const* title    = config.GetStr( section, "title",    "" );
	char const* shortcut = config.GetStr( section, "shortcut", "" );
	char const* action   = config.GetStr( section, "action",   0  );

	SEL cmd = 0;
	if (action != 0)
	    cmd = NSSelectorFromString([NSString stringWithCString:action]);

	NSMenuItem* const item = [menu addItemWithTitle:
		[NSString stringWithCString:title] action:cmd
		keyEquivalent:[NSString stringWithCString:shortcut]];
	[item setKeyEquivalentModifierMask:NSCommandKeyMask];

	char const* target = config.GetStr( section, "target", 0 );
	if (target != 0)
	    {
	    STR_SWITCH (target)
		STR_CASE (application)
		    [item setTarget:NSApp];
		STR_CASE (delegate)
		    [item setTarget:[NSApp delegate]];
	    STR_SWITCH_END
	    }
	}
    }


//-----------------------------------------------------------------------------
// menu_add_submenu
//	Looks up the named section in csIniFile.  Recursively calls
//	build_menu() to generate the submenu.  Pays particular attention to
//	key "type" which, if present, may be one of "apple", "window", or
//	"services".  A TYPE qualification means that the menu should be
//	configured as the NSApplication's Apple, Window, or Services menu,
//	respectively.
//-----------------------------------------------------------------------------
static void menu_add_submenu( NSMenu* menu, char const* key,
    csIniFile const& config )
    {
    NSMenu* const sub = build_menu( key, config );
    if (sub != 0)
	{
	NSMenuItem* const item =
		[menu addItemWithTitle:[sub title] action:0 keyEquivalent:@""];
	[menu setSubmenu:sub forItem:item];

	char const* section =
	    [[NSString stringWithFormat:@"Menu.%s",key] cString];
	char const* type = config.GetStr( section, "type", 0 );
	if (type != 0)
	    {
	    STR_SWITCH (type)
		STR_CASE (apple)
		    [NSApp setAppleMenu:sub];
		STR_CASE (window)
		    [NSApp setWindowsMenu:sub];
		STR_CASE (services)
{'fraction_non_alphanumeric': 0.2673996368771434, 'fraction_numerical': 0.005043373007867662, 'mean_word_length': 2.3428860418071475, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2122227', 'n_tokens_mistral': 2966, 'n_tokens_neox': 2746, 'n_words': 889}