X-Plane Menu SDK Sample

A basic demonstration for adding submenus to the in-sim Plugin menu.

#include "XPLMMenus.h"
#include <string.h>
#if IBM
	#include <windows.h>
#endif

#ifndef XPLM300
	#error This is made to be compiled against the XPLM300 SDK
#endif

int g_menu_container_idx; // The index of our menu item in the Plugins menu
XPLMMenuID g_menu_id; // The menu container we'll append all our menu items to
void menu_handler(void *, void *);

PLUGIN_API int XPluginStart(
						char *		outName,
						char *		outSig,
						char *		outDesc)
{
	strcpy(outName, "MenuPlugin");
	strcpy(outSig, "xpsdk.examples.menuplugin");
	strcpy(outDesc, "A sample plug-in that demonstrates and exercises the X-Plane menu API.");

	g_menu_container_idx = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "Sample Menu", 0, 0);
	g_menu_id = XPLMCreateMenu("Sample Menu", XPLMFindPluginsMenu(), g_menu_container_idx, menu_handler, NULL);
	XPLMAppendMenuItem(g_menu_id, "Toggle Settings", (void *)"Menu Item 1", 1);
	XPLMAppendMenuSeparator(g_menu_id);
	XPLMAppendMenuItem(g_menu_id, "Toggle Shortcuts", (void *)"Menu Item 2", 1);
	XPLMAppendMenuItemWithCommand(g_menu_id, "Toggle Flight Configuration (Command-Based)", XPLMFindCommand("sim/operation/toggle_flight_config"));
	
	// Changed your mind? You can destroy the submenu you created with XPLMDestroyMenu(),
	// then remove the "Sample Menu" item from the "Plugins" menu with XPLMRemoveMenuItem().
	//XPLMDestroyMenu(g_menu_id);
	//XPLMRemoveMenuItem(XPLMFindPluginsMenu(), g_menu_container_idx);

	XPLMMenuID aircraft_menu = XPLMFindAircraftMenu();
	if(aircraft_menu) // This will be NULL unless this plugin was loaded with an aircraft (i.e., it was located in the current aircraft's "plugins" subdirectory)
	{
		XPLMAppendMenuItemWithCommand(aircraft_menu, "Toggle Settings (Command-Based)", XPLMFindCommand("sim/operation/toggle_settings_window"));
	}
	
	return 1;
}

PLUGIN_API void	XPluginStop(void)
{
	// Since we created this menu, we'll be good citizens and clean it up as well
	XPLMDestroyMenu(g_menu_id);
	// If we were able to add a command to the aircraft menu, it will be automatically removed for us when we're unloaded
}

PLUGIN_API void XPluginDisable(void) { }
PLUGIN_API int XPluginEnable(void) { return 1; }
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void * inParam) { }

void menu_handler(void * in_menu_ref, void * in_item_ref)
{
	if(!strcmp((const char *)in_item_ref, "Menu Item 1"))
	{
		XPLMCommandOnce(XPLMFindCommand("sim/operation/toggle_settings_window"));
	}
	else if(!strcmp((const char *)in_item_ref, "Menu Item 2"))
	{
		XPLMCommandOnce(XPLMFindCommand("sim/operation/toggle_key_shortcuts_window"));
	}
}
  • Facebook
  • Reddit
  • Twitter
  • LinkedIn

About Tyler Young

Tyler is a software developer for X-Plane. Among other projects, he was in charge of the new X-Plane 11 user interface.
Bookmark the permalink.

6 Responses to X-Plane Menu SDK Sample

  1. Diego Altgenug says:

    One Question,
    how canĀ“t I find the method from the lib XPLM300 , XPLMFindAircraftMenu??
    Is ther a trick?

  2. Filippo says:

    Hi Tyler,
    amazing job with XP11! One other question: is there / will be there any way to "listen" to user clicks on menus other than "Plugins". For example "Location", "Settings", etc... ? thx for your help.

    • Tyler Young says:

      No, we have no plans for something like that. I take it you're looking to replace the native UI for certain windows?

      • Filippo says:

        I'm working with an overlayed OS window which i need to hide when the user enters an XPlane "sim blocking" menu... a (working) workaround is to keep note of when the last FlightLoop occured (by using XPLMRegisterFlightLoopCallback), and hide my overlayed win (separate thread) when curtime is after X millisecs. Of course this brings some visible delay...

        • Tyler Young says:

          Just to confirm: you have a popped-out, first-class X-Plane window... and you want it to disappear when there's a modal window (like Settings or Flight Configuration) onscreen?

          The behavior that all the built-in windows exhibit in this situation is to simply darken and stop responding to input. (And this behavior comes free to SDK-created windows.)

Leave a Reply

Your email address will not be published. Required fields are marked *

Please do not report bugs in the blog comments.
Only bugs reported via the X-Plane Bug Reporter are tracked.