From 893bdb59283de16f01e188e26f8cacd2d8c6cd2d Mon Sep 17 00:00:00 2001
From: Mike Small <smallm@sdf.org>
Date: Sun, 31 Jan 2021 12:39:10 -0500
Subject: [PATCH] Convert menus.c to C++ menus.cc

---
 ChangeLog                 |  22 ++
 Makefile.in               |   1 +
 man/Makefile.in           |   1 +
 src/Makefile.am           |   8 +-
 src/Makefile.in           |   7 +-
 src/add_window.c          |  10 +-
 src/add_window.h          |  15 +-
 src/events.c              |   2 +-
 src/events.h              |  74 ++---
 src/{menus.c => menus.cc} | 566 ++++++++++++++++++--------------------
 src/menus.h               |  55 ++--
 src/resize.h              |  32 ++-
 src/screen.h              |   3 +-
 src/twm.c                 |   5 +-
 src/twm.h                 |  15 +-
 src/util.c                |   8 +-
 src/util.h                |   3 +-
 17 files changed, 419 insertions(+), 408 deletions(-)
 rename src/{menus.c => menus.cc} (85%)

diff --git a/ChangeLog b/ChangeLog
index dda6e15..2523927 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2021-01-31  Mike Small  <smallm@sdf.org>
+
+	* src/util.h: move CreateMenuIcon prototype here
+
+	* src/add_window.h: C++ compatibility (extern "C")
+
+	* src/events.h: ditto
+
+	* src/resize.h: ditto
+
+	* src/twm.h: ditto and move or remove some functions
+
+	* src/twm.c (InitVariables): remove use of CaseSensitive variable
+
+	* src/screen.h (ScreenInfo): remove CaseSensitive variable
+
+	* src/menus.h: C++ compatibility (extern "C"), move GetWMState here
+
+	* src/menus.cc: convert to C++ (beginning, enough to build)
+
+	* src/Makefile.am (AM_CXXFLAGS): remove -Wno-logical-op-parenthesis
+
 2019-10-21  Mike Small  <smallm@sdf.org>
 
 	* src/icons.h: Remove IconUp and IconDown from icons interface
diff --git a/Makefile.in b/Makefile.in
index 57c2adb..ea60bf3 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -346,6 +346,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@
diff --git a/man/Makefile.in b/man/Makefile.in
index 0e1263b..266fe48 100644
--- a/man/Makefile.in
+++ b/man/Makefile.in
@@ -295,6 +295,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@
diff --git a/src/Makefile.am b/src/Makefile.am
index 6ddecf2..28a9747 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -32,10 +32,10 @@ AM_CPPFLAGS =							\
 
 AM_CFLAGS = -Werror $(TWM_CFLAGS)
 # TWM_CFLAGS has results of pkg-config --cflags x11
-AM_CXXFLAGS = -Werror -Wno-logical-op-parenthesis $(TWM_CFLAGS) -std=c++11
+AM_CXXFLAGS = -Werror $(TWM_CFLAGS) -std=c++11
 twmruined_LDADD = $(TWM_LIBS)
 
-twmruined_SOURCES =		\
+twmruined_SOURCES =	\
         add_window.c	\
         add_window.h	\
         cursor.c	\
@@ -44,11 +44,11 @@ twmruined_SOURCES =		\
         events.h	\
         gc.c		\
         gc.h		\
-        icons.cc		\
+        icons.cc	\
         icons.h		\
         list.c		\
         list.h		\
-        menus.c		\
+        menus.cc	\
         menus.h		\
         parse.c		\
         parse.h		\
diff --git a/src/Makefile.in b/src/Makefile.in
index 701e9bb..e8f093a 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -368,6 +368,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@
@@ -384,7 +385,7 @@ AM_CPPFLAGS = \
 
 AM_CFLAGS = -Werror $(TWM_CFLAGS)
 # TWM_CFLAGS has results of pkg-config --cflags x11
-AM_CXXFLAGS = -Werror -Wno-logical-op-parenthesis $(TWM_CFLAGS) -std=c++11
+AM_CXXFLAGS = -Werror $(TWM_CFLAGS) -std=c++11
 twmruined_LDADD = $(TWM_LIBS)
 twmruined_SOURCES = \
         add_window.c	\
@@ -395,11 +396,11 @@ twmruined_SOURCES = \
         events.h	\
         gc.c		\
         gc.h		\
-        icons.cc		\
+        icons.cc	\
         icons.h		\
         list.c		\
         list.h		\
-        menus.c		\
+        menus.cc	\
         menus.h		\
         parse.c		\
         parse.h		\
diff --git a/src/add_window.c b/src/add_window.c
index cddde08..15af156 100644
--- a/src/add_window.c
+++ b/src/add_window.c
@@ -183,7 +183,7 @@ AddWindow(Window w)
     tmp_win->classh = NoClass;
     XGetClassHint(dpy, tmp_win->w, &tmp_win->classh);
     FetchWmProtocols (tmp_win);
-    FetchWmColormapWindows (tmp_win);
+    FetchWmColormapWindows(tmp_win);
 
     if (name == NULL)
 	tmp_win->name = strdup(NoName);
@@ -638,7 +638,7 @@ static void do_add_binding (int button, int context, int modifier, int func)
 }
 
 void
-AddDefaultBindings (void)
+AddDefaultBindings(void)
 {
     /*
      * The bindings are stored in Scr->Mouse, indexed by
@@ -760,7 +760,7 @@ static Window CreateHighlightWindow (TwmWindow *tmp_win)
 }
 
 
-void ComputeCommonTitleOffsets (void)
+void ComputeCommonTitleOffsets(void)
 {
     int buttonwidth = (Scr->TBInfo.width + Scr->TBInfo.pad);
 
@@ -991,7 +991,7 @@ CreateColormapWindow(Window w, Bool creating_parent, Bool property_window)
 }
 
 void
-FetchWmColormapWindows (TwmWindow *tmp)
+FetchWmColormapWindows(TwmWindow *tmp)
 {
     register int i, j;
     Window *cmap_windows = NULL;
@@ -1120,7 +1120,7 @@ FetchWmColormapWindows (TwmWindow *tmp)
 }
 
 
-void GetWindowSizeHints (TwmWindow *tmp)
+void GetWindowSizeHints(TwmWindow *tmp)
 {
     long supplied = 0;
 
diff --git a/src/add_window.h b/src/add_window.h
index bb46d0e..c3d1f64 100644
--- a/src/add_window.h
+++ b/src/add_window.h
@@ -61,13 +61,20 @@ in this Software without prior written authorization from The Open Group.
 #ifndef _ADD_WINDOW_
 #define _ADD_WINDOW_
 
+#ifdef __cplusplus
+#define EXTERN extern "C"
+#else
+#define EXTERN extern
+#endif
+
 extern char NoName[];
 
-extern void AddDefaultBindings ( void );
-extern TwmWindow * AddWindow ( Window w );
+EXTERN void AddDefaultBindings( void );
+extern TwmWindow* AddWindow( Window w );
+EXTERN void ComputeCommonTitleOffsets( void );
 extern ColormapWindow * CreateColormapWindow ( Window w, Bool creating_parent, Bool property_window );
-extern TwmColormap * CreateTwmColormap ( Colormap c );
-extern void FetchWmColormapWindows ( TwmWindow *tmp );
+extern TwmColormap* CreateTwmColormap( Colormap c );
+EXTERN void FetchWmColormapWindows( TwmWindow *tmp );
 extern void FetchWmProtocols ( TwmWindow *tmp );
 extern void GetGravityOffsets ( TwmWindow *tmp, int *xp, int *yp );
 extern void GetWindowSizeHints ( TwmWindow *tmp );
diff --git a/src/events.c b/src/events.c
index 82d0168..4ac05c8 100644
--- a/src/events.c
+++ b/src/events.c
@@ -922,7 +922,7 @@ HandlePropertyNotify(void)
 
       default:
 	if (Event.xproperty.atom == _XA_WM_COLORMAP_WINDOWS) {
-	    FetchWmColormapWindows (Tmp_win);	/* frees old data */
+	    FetchWmColormapWindows(Tmp_win);	/* frees old data */
 	    break;
 	} else if (Event.xproperty.atom == _XA_WM_PROTOCOLS) {
 	    FetchWmProtocols (Tmp_win);
diff --git a/src/events.h b/src/events.h
index e6bcfc9..12eb92b 100644
--- a/src/events.h
+++ b/src/events.h
@@ -64,45 +64,51 @@ in this Software without prior written authorization from The Open Group.
 #include "screen.h"
 #include "twm.h"
 
+#ifdef __cplusplus
+#define EXTERN extern "C"
+#else
+#define EXTERN extern
+#endif
+
 typedef void (*event_proc)(void);
 
 extern Time lastTimestamp;
 #define LastTimestamp() lastTimestamp
 
-extern void AutoRaiseWindow ( TwmWindow *tmp );
-extern void SetRaiseWindow ( TwmWindow *tmp );
-extern void InitEvents ( void );
-extern Bool StashEventTime ( XEvent *ev );
-extern Window WindowOfEvent ( XEvent *e );
-extern Bool DispatchEvent2 ( void );
-extern Bool DispatchEvent ( void );
-extern void HandleEvents ( void );
-extern void HandleColormapNotify ( void );
-extern void HandleVisibilityNotify ( void );
-extern void HandleKeyPress ( void );
-extern void free_cwins ( TwmWindow *tmp );
-extern void HandlePropertyNotify ( void );
-extern void HandleClientMessage ( void );
-extern void HandleExpose ( void );
-extern void HandleDestroyNotify ( void );
-extern void HandleCreateNotify ( void );
-extern void HandleMapRequest ( void );
-extern void SimulateMapRequest ( Window w );
-extern void HandleMapNotify ( void );
-extern void HandleUnmapNotify ( void );
-extern void HandleMotionNotify ( void );
-extern void HandleButtonRelease ( void );
-extern void HandleButtonPress ( void );
-extern void HandleEnterNotify ( void );
-extern void HandleLeaveNotify ( void );
-extern void HandleConfigureRequest ( void );
-extern void HandleShapeNotify ( void );
-extern void HandleUnknown ( void );
-extern int Transient ( Window w, Window *propw );
-extern ScreenInfo * FindScreenInfo ( Window w );
-extern void InstallWindowColormaps ( int type, TwmWindow *tmp );
-extern void InstallRootColormap ( void );
-extern void UninstallRootColormap ( void );
+EXTERN void AutoRaiseWindow( TwmWindow *tmp );
+extern void SetRaiseWindow( TwmWindow *tmp );
+extern void InitEvents( void );
+extern Bool StashEventTime( XEvent *ev );
+extern Window WindowOfEvent( XEvent *e );
+EXTERN Bool DispatchEvent2( void );
+EXTERN Bool DispatchEvent( void );
+extern void HandleEvents( void );
+extern void HandleColormapNotify( void );
+extern void HandleVisibilityNotify( void );
+extern void HandleKeyPress( void );
+extern void free_cwins( TwmWindow *tmp );
+extern void HandlePropertyNotify( void );
+extern void HandleClientMessage( void );
+extern void HandleExpose( void );
+extern void HandleDestroyNotify( void );
+extern void HandleCreateNotify( void );
+extern void HandleMapRequest( void );
+extern void SimulateMapRequest( Window w );
+extern void HandleMapNotify( void );
+extern void HandleUnmapNotify( void );
+extern void HandleMotionNotify( void );
+extern void HandleButtonRelease( void );
+extern void HandleButtonPress( void );
+extern void HandleEnterNotify( void );
+extern void HandleLeaveNotify( void );
+extern void HandleConfigureRequest( void );
+extern void HandleShapeNotify( void );
+EXTERN void HandleUnknown( void );
+extern int Transient( Window w, Window *propw );
+extern ScreenInfo* FindScreenInfo( Window w );
+EXTERN void InstallWindowColormaps( int type, TwmWindow *tmp );
+EXTERN void InstallRootColormap( void );
+EXTERN void UninstallRootColormap( void );
 
 extern event_proc EventHandler[];
 extern Window DragWindow;
diff --git a/src/menus.c b/src/menus.cc
similarity index 85%
rename from src/menus.c
rename to src/menus.cc
index a60f2be..54a2082 100644
--- a/src/menus.c
+++ b/src/menus.cc
@@ -56,13 +56,16 @@ in this Software without prior written authorization from The Open Group.
  *
  * 17-Nov-87 Thomas E. LaStrange		File created
  *
+ * 29-Nov-20 Mike Small				Switch to C++
  ***********************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+#include <algorithm>
 #include <stdio.h>
+#include <vector>
 #include <X11/Xos.h>
 #include "twm.h"
 #include "gc.h"
@@ -82,14 +85,19 @@ in this Software without prior written authorization from The Open Group.
 #include <X11/extensions/sync.h>
 #include <X11/SM/SMlib.h>
 
-int RootFunction = 0;
-MenuRoot *ActiveMenu = NULL;		/**< the active menu */
-MenuItem *ActiveItem = NULL;		/**< the active menu item */
-int MoveFunction;			/**< either F_MOVE or F_FORCEMOVE */
-int WindowMoved = FALSE;
-int menuFromFrameOrWindowOrTitlebar = FALSE;
+constexpr Pixel UNUSED_PIXEL{~0UL};
+const int SHADOWWIDTH{5};	/* in pixels */
+
+
+int RootFunction{0};
+MenuRoot *ActiveMenu{nullptr};		/**< the active menu */
+MenuItem *ActiveItem{nullptr};		/**< the active menu item */
+int MoveFunction;			/**< either F_MOVE or F_FORCEMOVE */
+// change these two to bool when use in events.c changes to C++.
+int WindowMoved{false};
+int menuFromFrameOrWindowOrTitlebar{false};
+int ConstMove{false};		/**< constrained move variables */
 
-int ConstMove = FALSE;		/**< constrained move variables */
 int ConstMoveDir;
 int ConstMoveX;
 int ConstMoveY;
@@ -103,28 +111,26 @@ int ConstMoveYB;
 int ResizeOrigX;
 int ResizeOrigY;
 
-int MenuDepth = 0;		/**< number of menus up */
+int MenuDepth{0};		/**< number of menus up */
 static struct {
     int x;
     int y;
 } MenuOrigins[MAXMENUDEPTH];
 static Cursor LastCursor;
 
-static Bool belongs_to_twm_window ( TwmWindow *t, Window w );
-static void Identify ( TwmWindow *t );
-static void send_clientmessage ( Window w, Atom a, Time timestamp );
-static void BumpWindowColormap ( TwmWindow *tmp, int inc );
-static int  DeferExecution ( int context, int func, Cursor cursor );
-static Bool NeedToDefer ( MenuRoot *root );
-static void DestroyMenu ( MenuRoot *menu );
+static bool belongs_to_twm_window(const TwmWindow& t, Window w);
+static void Identify(const TwmWindow* t);
+static void send_clientmessage(Window w, Atom a, Time timestamp);
+static void BumpWindowColormap(TwmWindow &tmp, int inc);
+static bool DeferExecution(int context, int func, Cursor cursor);
+static bool NeedToDefer(MenuRoot *root);
+static void DestroyMenu(MenuRoot *menu);
 static void MakeMenu ( MenuRoot *mr );
 static void Execute ( const char *s );
 static void WarpAlongRing ( XButtonEvent *ev, Bool forward );
 static int  WarpThere ( TwmWindow * t );
 static void WarpToWindow ( TwmWindow *t );
 
-#define SHADOWWIDTH 5			/* in pixels */
-
 
 
 
@@ -177,7 +183,7 @@ InitMenus(void)
 Bool AddFuncKey (char *name, int cont, int mods, int func, char *win_name,
                  char *action)
 {
-    FuncKey *tmp;
+    FuncKey *fkey;
     KeySym keysym;
     KeyCode keycode;
 
@@ -192,61 +198,49 @@ Bool AddFuncKey (char *name, int cont, int mods, int func, char *win_name,
     }
 
     /* see if there already is a key defined for this context */
-    for (tmp = Scr->FuncKeyRoot.next; tmp != NULL; tmp = tmp->next)
+    for (fkey = Scr->FuncKeyRoot.next; fkey != NULL; fkey = fkey->next)
     {
-	if (tmp->keysym == keysym &&
-	    tmp->cont == cont &&
-	    tmp->mods == mods)
+	if (fkey->keysym == keysym &&
+	    fkey->cont == cont &&
+	    fkey->mods == mods)
 	    break;
     }
 
-    if (tmp == NULL)
+    if (fkey == NULL)
     {
-	tmp = malloc(sizeof(FuncKey));
-	tmp->next = Scr->FuncKeyRoot.next;
-	Scr->FuncKeyRoot.next = tmp;
+	fkey = new FuncKey;
+	fkey->next = Scr->FuncKeyRoot.next;
+	Scr->FuncKeyRoot.next = fkey;
     }
 
-    tmp->name = name;
-    tmp->keysym = keysym;
-    tmp->keycode = keycode;
-    tmp->cont = cont;
-    tmp->mods = mods;
-    tmp->func = func;
-    tmp->win_name = win_name;
-    tmp->action = action;
+    fkey->name = name;
+    fkey->keysym = keysym;
+    fkey->keycode = keycode;
+    fkey->cont = cont;
+    fkey->mods = mods;
+    fkey->func = func;
+    fkey->win_name = win_name;
+    fkey->action = action;
 
     return True;
 }
 
 
 
-int CreateTitleButton (const char *name, int func, const char *action,
-                       MenuRoot *menuroot, Bool rightside, Bool append)
+int CreateTitleButton(const char *name, int func, const char *action,
+                      MenuRoot *menuroot, Bool rightside, Bool append)
 {
-    TitleButton *tb = malloc (sizeof(TitleButton));
-
-    if (!tb) {
-	fprintf (stderr,
-		 "%s:  unable to allocate %ld bytes for title button\n",
-		 ProgramName, (unsigned long)sizeof(TitleButton));
-	return 0;
-    }
-
-    tb->next = NULL;
-    tb->name = name;			/* note that we are not copying */
-    tb->bitmap = None;			/* WARNING, values not set yet */
-    tb->width = 0;			/* see InitTitlebarButtons */
-    tb->height = 0;			/* ditto */
-    tb->func = func;
-    tb->action = action;
-    tb->menuroot = menuroot;
-    tb->rightside = rightside;
-    if (rightside) {
+    /* 1. Note that we are not copying name.
+     * 2. bitmap, width, and height are set later by InitTitlebarButtons */
+    TitleButton* tb = new TitleButton {
+	// next, name, bitmap, srcx, srcy, width, height, dstx, dsty
+	nullptr, name, None, 0, 0, 0, 0, 0, 0,
+	func, action, menuroot, rightside
+    };
+    if (rightside)
 	Scr->TBInfo.nright++;
-    } else {
+    else
 	Scr->TBInfo.nleft++;
-    }
 
     /*
      * Cases for list:
@@ -262,13 +256,13 @@ int CreateTitleButton (const char *name, int func, const char *action,
 	tb->next = Scr->TBInfo.head;
 	Scr->TBInfo.head = tb;
     } else if (append && rightside) {	/* 3 */
-	register TitleButton *t;
+	TitleButton *t;
 	for /* SUPPRESS 530 */
 	  (t = Scr->TBInfo.head; t->next; t = t->next);
 	t->next = tb;
 	tb->next = NULL;
     } else {				/* 2 */
-	register TitleButton *t, *prev = NULL;
+	TitleButton *t, *prev = NULL;
 	for (t = Scr->TBInfo.head; t && !t->rightside; t = t->next) {
 	    prev = t;
 	}
@@ -291,7 +285,7 @@ int CreateTitleButton (const char *name, int func, const char *action,
  * the button, then put in a question; if we can't find the question mark,
  * something is wrong and we are probably going to be in trouble later on.
  */
-void InitTitlebarButtons (void)
+void InitTitlebarButtons(void)
 {
     TitleButton *tb;
     int h;
@@ -320,9 +314,9 @@ void InitTitlebarButtons (void)
 	    fprintf (stderr, "%s:  unable to add resize button\n",
 		     ProgramName);
 	}
-	AddDefaultBindings ();
+	AddDefaultBindings();
     }
-    ComputeCommonTitleOffsets ();
+    ComputeCommonTitleOffsets();
 
     /*
      * load in images and do appropriate centering
@@ -511,7 +505,7 @@ UpdateMenu(void)
 	    continue;
 
 	if (Event.type == ButtonRelease || Cancel) {
-	  menuFromFrameOrWindowOrTitlebar = FALSE;
+	  menuFromFrameOrWindowOrTitlebar = false;
 	  fromMenu = FALSE;
 	  return;
 	}
@@ -626,47 +620,39 @@ UpdateMenu(void)
 MenuRoot *
 NewMenuRoot(const char *name)
 {
-    MenuRoot *tmp;
+    MenuRoot *mroot;
 
-#define UNUSED_PIXEL ((unsigned long) (~0))	/* more than 24 bits */
+    mroot = new MenuRoot {
+	nullptr, nullptr, nullptr, nullptr,
+	name, None, None, UNUSED_PIXEL, UNUSED_PIXEL, NEVER_MAPPED,
+	0, 0, 0, false, false, false };
 
-    tmp = malloc(sizeof(MenuRoot));
-    tmp->hi_fore = UNUSED_PIXEL;
-    tmp->hi_back = UNUSED_PIXEL;
-    tmp->name = name;
-    tmp->prev = NULL;
-    tmp->first = NULL;
-    tmp->last = NULL;
-    tmp->items = 0;
-    tmp->width = 0;
-    tmp->mapped = NEVER_MAPPED;
-    tmp->pull = FALSE;
-    tmp->w = None;
-    tmp->shadow = None;
-    tmp->real_menu = FALSE;
-
-    if (Scr->MenuList == NULL)
+    if (Scr->MenuList == nullptr)
     {
-	Scr->MenuList = tmp;
-	Scr->MenuList->next = NULL;
+	Scr->MenuList = mroot;
+	Scr->MenuList->next = nullptr;
     }
 
-    if (Scr->LastMenu == NULL)
+    // This is the only use of LastMenu. Careful. Though the variable
+    // is only referenced here it builds out the tail of the list
+    // referenced by MenuList. Might as well keep til the list can
+    // be put in a std::vector.
+    if (Scr->LastMenu == nullptr)
     {
-	Scr->LastMenu = tmp;
-	Scr->LastMenu->next = NULL;
+	Scr->LastMenu = mroot;
+	Scr->LastMenu->next = nullptr;
     }
     else
     {
-	Scr->LastMenu->next = tmp;
-	Scr->LastMenu = tmp;
-	Scr->LastMenu->next = NULL;
+	Scr->LastMenu->next = mroot;
+	Scr->LastMenu = mroot;
+	Scr->LastMenu->next = nullptr;
     }
 
     if (strcmp(name, TWM_WINDOWS) == 0)
-	Scr->Windows = tmp;
+	Scr->Windows = mroot;
 
-    return (tmp);
+    return mroot;
 }
 
 
@@ -686,64 +672,64 @@ MenuItem *
 AddToMenu(MenuRoot *menu, const char *item, const char *action,
           MenuRoot *sub, int func, const char *fore, const char *back)
 {
-    MenuItem *tmp;
-    int width;
-
 #ifdef DEBUG_MENUS
     fprintf(stderr, "adding menu item=\"%s\", action=%s, sub=%d, f=%d\n",
 	item, action, sub, func);
 #endif
 
-    tmp = malloc(sizeof(MenuItem));
-    tmp->root = menu;
+    MenuItem *m_item = new MenuItem{
+	    nullptr,	// next
+	    nullptr,    // prev
+	    sub,	// sub
+	    menu,	// root
+	    item,
+	    action,
+	    0,		// fore
+	    0, 		// back
+	    0,		// hi_fore
+	    0,		// hi_back
+	    menu->items++,
+	    0,		// x coord for text
+	    static_cast<short>(func),
+	    0,		// state
+	    static_cast<short>(strlen(item)),
+	    false	// user_colors
+    };
 
-    if (menu->first == NULL)
+    if (menu->first == nullptr)
     {
-	menu->first = tmp;
-	tmp->prev = NULL;
+	menu->first = m_item;
+	m_item->prev = nullptr;
     }
     else
     {
-	menu->last->next = tmp;
-	tmp->prev = menu->last;
+	menu->last->next = m_item;
+	m_item->prev = menu->last;
     }
-    menu->last = tmp;
+    menu->last = m_item;
 
-    tmp->item = item;
-    tmp->strlen = strlen(item);
-    tmp->action = action;
-    tmp->next = NULL;
-    tmp->sub = NULL;
-    tmp->state = 0;
-    tmp->func = func;
+    if (!Scr->HaveFonts)
+	CreateFonts();
 
-    if (!Scr->HaveFonts) CreateFonts();
-    width = MyFont_TextWidth(&Scr->MenuFont, item, tmp->strlen);
+    int width{MyFont_TextWidth(&Scr->MenuFont, item, m_item->strlen)};
     if (width <= 0)
 	width = 1;
     if (width > menu->width)
 	menu->width = width;
 
-    tmp->user_colors = FALSE;
-    if (Scr->Monochrome == COLOR && fore != NULL)
+    if (Scr->Monochrome == COLOR && fore != nullptr)
     {
-	int save;
-
-	save = Scr->FirstTime;
-	Scr->FirstTime = TRUE;
-	GetColor(COLOR, &tmp->fore, fore);
-	GetColor(COLOR, &tmp->back, back);
+	auto save{Scr->FirstTime};
+	Scr->FirstTime = true;
+	GetColor(COLOR, &m_item->fore, fore);
+	GetColor(COLOR, &m_item->back, back);
 	Scr->FirstTime = save;
-	tmp->user_colors = TRUE;
+	m_item->user_colors = true;
     }
-    if (sub != NULL)
-    {
-	tmp->sub = sub;
-	menu->pull = TRUE;
-    }
-    tmp->item_num = menu->items++;
+    if (sub != nullptr)
+	menu->pull = true;
 
-    return (tmp);
+    return m_item;
 }
 
 
@@ -962,73 +948,52 @@ MakeMenu(MenuRoot *mr)
  *  \param center whether or not to center horizontally over position
  */
 Bool
-PopUpMenu (MenuRoot *menu, int x, int y, Bool center)
+PopUpMenu(MenuRoot *menu, int x, int y, Bool center)
 {
-    int WindowNameCount;
-    TwmWindow **WindowNames;
-    TwmWindow *tmp_win2,*tmp_win3;
-    int i;
-    int (*compar)(const char *, const char *) =
-      (Scr->CaseSensitive ? strcmp : XmuCompareISOLatin1);
-
-    if (!menu) return False;
+    if (!menu)
+	return False;
 
     InstallRootColormap();
 
     if (menu == Scr->Windows)
     {
-	TwmWindow *tmp_win;
+	/* this is the windows list menu, rebuild it */
 
-	/* this is the twm windows menu,  let's go ahead and build it */
+	DestroyMenu(menu);
 
-	DestroyMenu (menu);
-
-	menu->first = NULL;
-	menu->last = NULL;
+	menu->first = nullptr;
+	menu->last = nullptr;
 	menu->items = 0;
 	menu->width = 0;
 	menu->mapped = NEVER_MAPPED;
-  	AddToMenu(menu, "TWM Windows", NULLSTR, NULL, F_TITLE,NULLSTR,NULLSTR);
+  	AddToMenu(menu, "Windows", NULLSTR, nullptr,
+		  F_TITLE, NULLSTR, NULLSTR);
 
-        for(tmp_win = Scr->TwmRoot.next , WindowNameCount=0;
-            tmp_win != NULL;
-            tmp_win = tmp_win->next)
-          WindowNameCount++;
-        if (WindowNameCount != 0)
-        {
-            WindowNames = malloc(sizeof(TwmWindow *) * WindowNameCount);
-            WindowNames[0] = Scr->TwmRoot.next;
-            for(tmp_win = Scr->TwmRoot.next->next , WindowNameCount=1;
-                tmp_win != NULL;
-                tmp_win = tmp_win->next,WindowNameCount++)
-            {
-                tmp_win2 = tmp_win;
-                for (i=0;i<WindowNameCount;i++)
-                {
-                    if ((*compar)(tmp_win2->name,WindowNames[i]->name) < 0)
-                    {
-                        tmp_win3 = tmp_win2;
-                        tmp_win2 = WindowNames[i];
-                        WindowNames[i] = tmp_win3;
-                    }
-                }
-                WindowNames[WindowNameCount] = tmp_win2;
-            }
-            for (i=0; i<WindowNameCount; i++)
-            {
-                AddToMenu(menu, WindowNames[i]->name, (char *)WindowNames[i],
-                          NULL, F_POPUP,NULL,NULL);
-            }
-            free(WindowNames);
-        }
+	std::vector<const TwmWindow*> windows;
+	for(auto w = Scr->TwmRoot.next; w; w = w->next)
+	    windows.push_back(w);
+
+	std::sort(windows.begin(), windows.end(),
+		  [](const TwmWindow* w1, const TwmWindow* w2) {
+		      return 0 > XmuCompareISOLatin1(w1->name, w2->name);
+		  });
+
+	for (auto w : windows)
+	{
+	    // 3rd argument char* (action)? How does that work?
+	    AddToMenu(menu, w->name, (char *)w, nullptr, F_POPUP,
+		      nullptr, nullptr);
+	}
 
 	MakeMenu(menu);
     }
 
-    if (menu->w == None || menu->items == 0) return False;
+    if (menu->w == None || menu->items == 0)
+	return False;
 
     /* Prevent recursively bringing up menus. */
-    if (menu->mapped == MAPPED) return False;
+    if (menu->mapped == MAPPED)
+	return False;
 
     /*
      * Dynamically set the parent;  this allows pull-ups to also be main
@@ -1116,7 +1081,7 @@ PopDownMenu(void)
     ActiveItem = NULL;
     MenuDepth = 0;
     if (Context == C_WINDOW || Context == C_FRAME || Context == C_TITLE)
-      menuFromFrameOrWindowOrTitlebar = TRUE;
+      menuFromFrameOrWindowOrTitlebar = true;
 }
 
 
@@ -1143,22 +1108,20 @@ FindMenuRoot(const char *name)
 
 
 
-static Bool
-belongs_to_twm_window (TwmWindow *t, Window w)
+static bool
+belongs_to_twm_window(const TwmWindow& t, Window w)
 {
-    if (!t) return False;
+    if (w == t.frame || w == t.title_w || w == t.hilite_w ||
+	w == t.icon_w || w == t.icon_bm_w) return true;
 
-    if (w == t->frame || w == t->title_w || w == t->hilite_w ||
-	w == t->icon_w || w == t->icon_bm_w) return True;
-
-    if (t && t->titlebuttons) {
-	register TBWindow *tbw;
-	register int nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;
-	for (tbw = t->titlebuttons; nb > 0; tbw++, nb--) {
-	    if (tbw->window == w) return True;
+    if (t.titlebuttons) {
+	int nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;
+	for (const TBWindow* tbw = t.titlebuttons; nb > 0; tbw++, nb--) {
+	    if (tbw->window == w)
+		return true;
 	}
     }
-    return False;
+    return false;
 }
 
 
@@ -1348,18 +1311,18 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_IDENTIFY:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	Identify(tmp_win);
 	break;
 
     case F_VERSION:
-	Identify ((TwmWindow *) NULL);
+	Identify(nullptr);
 	break;
 
     case F_AUTORAISE:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	tmp_win->auto_raise = !tmp_win->auto_raise;
 	if (tmp_win->auto_raise) ++(Scr->NumAutoRaises);
@@ -1389,7 +1352,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	EventHandler[EnterNotify] = HandleUnknown;
 	EventHandler[LeaveNotify] = HandleUnknown;
 	if (DeferExecution(context, func, Scr->MoveCursor))
-	    return TRUE;
+	    return true;
 
 	PopDownMenu();
 
@@ -1407,7 +1370,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	     * see if this is being done from the titlebar
 	     */
 	    fromtitlebar =
-	      belongs_to_twm_window (tmp_win, eventp->xbutton.window);
+	      belongs_to_twm_window(*tmp_win, eventp->xbutton.window);
 
 	    /* Save pointer position so we can tell if it was moved or
 	       not during the resize. */
@@ -1453,14 +1416,14 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
     case F_TOPZOOM:
     case F_BOTTOMZOOM:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 	fullzoom(tmp_win, func);
 	break;
 
     case F_MOVE:
     case F_FORCEMOVE:
 	if (DeferExecution(context, func, Scr->MoveCursor))
-	    return TRUE;
+	    return true;
 
 	PopDownMenu();
 	rootw = eventp->xbutton.root;
@@ -1487,7 +1450,6 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	    DragY = eventp->xbutton.y;
 	    moving_icon = TRUE;
 	}
-
 	else if (w != tmp_win->icon_w)
 	{
 	    XTranslateCoordinates(dpy, w, tmp_win->frame,
@@ -1518,7 +1480,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	{
 	    int width, height;
 
-	    ConstMove = TRUE;
+	    ConstMove = true;
 	    ConstMoveDir = MOVE_NONE;
 	    ConstMoveX = eventp->xbutton.x_root - DragX - JunkBW;
 	    ConstMoveY = eventp->xbutton.y_root - DragY - JunkBW;
@@ -1565,7 +1527,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	/*
 	 * see if this is being done from the titlebar
 	 */
-	fromtitlebar = belongs_to_twm_window (tmp_win, eventp->xbutton.window);
+	fromtitlebar = belongs_to_twm_window(*tmp_win, eventp->xbutton.window);
 
 	if (menuFromFrameOrWindowOrTitlebar) {
 	  /* warp the pointer to the middle of the window */
@@ -1575,7 +1537,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	  XFlush(dpy);
 	}
 
-	while (TRUE)
+	while (true)
 	{
 	    long releaseEvent = menuFromFrameOrWindowOrTitlebar ?
 	                          ButtonPress : ButtonRelease;
@@ -1621,7 +1583,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
 	    if (Cancel)
 	    {
-		WindowMoved = FALSE;
+		WindowMoved = false;
 		UninstallRootColormap();
 		return TRUE;	/* XXX should this be FALSE? */
 	    }
@@ -1652,7 +1614,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	        abs(eventp->xmotion.y_root - origY) < Scr->MoveDelta)
 		continue;
 
-	    WindowMoved = TRUE;
+	    WindowMoved = true;
 	    DragWindow = w;
 
 	    if (ConstMove)
@@ -1771,7 +1733,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	    }
 
 	    if (NeedToDefer(mroot) && DeferExecution(context, func, Scr->SelectCursor))
-		return TRUE;
+		return true;
 	    else
 	    {
 		for (mitem = mroot->first; mitem != NULL; mitem = mitem->next)
@@ -1787,7 +1749,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
     case F_DEICONIFY:
     case F_ICONIFY:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	if (tmp_win->icon)
 	{
@@ -1802,7 +1764,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_RAISELOWER:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	if (!WindowMoved) {
 	    XWindowChanges xwc;
@@ -1816,7 +1778,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_RAISE:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	/* check to make sure raise is not from the WindowFunction */
 	if (w == tmp_win->icon_w && Context != C_ROOT)
@@ -1828,7 +1790,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_LOWER:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	if (w == tmp_win->icon_w)
 	    XLowerWindow(dpy, tmp_win->icon_w);
@@ -1839,7 +1801,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_FOCUS:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	if (tmp_win->icon == FALSE)
 	{
@@ -1850,16 +1812,16 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	    else
 	    {
 		if (Scr->Focus != NULL) {
-		    SetBorder (Scr->Focus, False);
+		    SetBorder(Scr->Focus, False);
 		    if (Scr->Focus->hilite_w)
 		      XUnmapWindow (dpy, Scr->Focus->hilite_w);
 		}
 
 		InstallWindowColormaps (0, tmp_win);
 		if (tmp_win->hilite_w) XMapWindow (dpy, tmp_win->hilite_w);
-		SetBorder (tmp_win, True);
+		SetBorder(tmp_win, True);
 		if (!tmp_win->wmhints || tmp_win->wmhints->input)
-		    SetFocus (tmp_win, eventp->xbutton.time);
+		    SetFocus(tmp_win, eventp->xbutton.time);
 		Scr->FocusRoot = FALSE;
 		Scr->Focus = tmp_win;
 	    }
@@ -1868,13 +1830,13 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_DESTROY:
 	if (DeferExecution(context, func, Scr->DestroyCursor))
-	    return TRUE;
+	    return true;
 	XKillClient(dpy, tmp_win->w);
 	break;
 
     case F_DELETE:
 	if (DeferExecution(context, func, Scr->DestroyCursor))
-	    return TRUE;
+	    return true;
 	else if (tmp_win->protocols & DoesWmDeleteWindow)
 	  SendDeleteWindowMessage (tmp_win, LastTimestamp());
 	else
@@ -1882,8 +1844,8 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 	break;
 
     case F_SAVEYOURSELF:
-	if (DeferExecution (context, func, Scr->SelectCursor))
-	  return TRUE;
+	if (DeferExecution(context, func, Scr->SelectCursor))
+	  return true;
 
 	if (tmp_win->protocols & DoesWmSaveYourself)
 	  SendSaveYourselfMessage (tmp_win, LastTimestamp());
@@ -1961,12 +1923,14 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_COLORMAP:
 	{
-	    if (strcmp (action, COLORMAP_NEXT) == 0) {
-		BumpWindowColormap (tmp_win, 1);
-	    } else if (strcmp (action, COLORMAP_PREV) == 0) {
-		BumpWindowColormap (tmp_win, -1);
-	    } else {
-		BumpWindowColormap (tmp_win, 0);
+	    if (tmp_win != nullptr) {
+		if (strcmp(action, COLORMAP_NEXT) == 0) {
+		    BumpWindowColormap(*tmp_win, 1);
+		} else if(strcmp (action, COLORMAP_PREV) == 0) {
+		    BumpWindowColormap(*tmp_win, -1);
+		} else {
+		    BumpWindowColormap(*tmp_win, 0);
+		}
 	    }
 	}
 	break;
@@ -1974,10 +1938,10 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
     case F_WARPPREV:
     case F_WARPNEXT:
 	{
-		register TwmWindow *t;
-		static TwmWindow *savedwarp = NULL;
+		TwmWindow* t;
+		static TwmWindow* savedwarp{nullptr};
 		TwmWindow *of, *l, *n;
-		int c=0;
+		int c{0};
 
 #define wseq(w) (func == F_WARPNEXT ? (w)->next : (w)->prev)
 #define nwin(w) ((w) && (n=wseq(w)) != NULL && n != &Scr->TwmRoot ? n : l)
@@ -2007,22 +1971,20 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_WARPTO:
 	{
-	    register TwmWindow *t;
-	    int len;
+	    const size_t len{strlen(action)};
 
-	    len = strlen(action);
-
-	    for (t = Scr->TwmRoot.next; t != NULL; t = t->next) {
+	    TwmWindow* t;
+	    for (t = Scr->TwmRoot.next; t != nullptr; t = t->next) {
 		if (!strncmp(action, t->name, len))
                     if (WarpThere(t)) break;
 	    }
 	    if (!t) {
-		for (t = Scr->TwmRoot.next; t != NULL; t = t->next) {
+		for (t = Scr->TwmRoot.next; t != nullptr; t = t->next) {
 		    if (!strncmp(action, t->classh.res_name, len))
                         if (WarpThere(t)) break;
 		}
 		if (!t) {
-		    for (t = Scr->TwmRoot.next; t != NULL; t = t->next) {
+		    for (t = Scr->TwmRoot.next; t != nullptr; t = t->next) {
 			if (!strncmp(action, t->classh.res_class, len))
                             if (WarpThere(t)) break;
 		    }
@@ -2091,7 +2053,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
 
     case F_WINREFRESH:
 	if (DeferExecution(context, func, Scr->SelectCursor))
-	    return TRUE;
+	    return true;
 
 	if (context == C_ICON && tmp_win->icon_w)
 	    w = XCreateSimpleWindow(dpy, tmp_win->icon_w,
@@ -2112,8 +2074,8 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
     case F_PRIORITY:
 	if (HasSync)
 	{
-	    if (DeferExecution (context, func, Scr->SelectCursor))
-		return TRUE;
+	    if (DeferExecution(context, func, Scr->SelectCursor))
+		return true;
 	    (void)XSyncSetPriority(dpy, tmp_win->w, atoi(action));
         }
 	break;
@@ -2135,26 +2097,24 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win,
  *  is C_ROOT
  *
  *  \param context the context in which the mouse button was pressed
- *	\param func    the function to defer
- *  \param cursor  cursor the cursor to display while waiting
+ *  \param func    the function to defer
+ *  \param cursor  the cursor to display while waiting
  */
-static int
+static bool
 DeferExecution(int context, int func, Cursor cursor)
 {
-  if (context == C_ROOT)
-    {
+    if (context == C_ROOT) {
 	LastCursor = cursor;
 	XGrabPointer(dpy, Scr->Root, True,
-	    ButtonPressMask | ButtonReleaseMask,
-	    GrabModeAsync, GrabModeAsync,
-	    Scr->Root, cursor, CurrentTime);
+		     ButtonPressMask | ButtonReleaseMask,
+		     GrabModeAsync, GrabModeAsync,
+		     Scr->Root, cursor, CurrentTime);
 
 	RootFunction = func;
 
-	return (TRUE);
+	return true;
     }
-
-    return (FALSE);
+    return false;
 }
 
 
@@ -2179,12 +2139,10 @@ ReGrab(void)
  *
  *  \param root the menu root to check
  */
-static Bool
+static bool
 NeedToDefer(MenuRoot *root)
 {
-    MenuItem *mitem;
-
-    for (mitem = root->first; mitem != NULL; mitem = mitem->next)
+    for (MenuItem* mitem = root->first; mitem != nullptr; mitem = mitem->next)
     {
 	switch (mitem->func)
 	{
@@ -2208,10 +2166,10 @@ NeedToDefer(MenuRoot *root)
         case F_TOPZOOM:
         case F_BOTTOMZOOM:
 	case F_AUTORAISE:
-	    return TRUE;
+	    return true;
 	}
     }
-    return FALSE;
+    return false;
 }
 
 
@@ -2269,7 +2227,7 @@ FocusOnRoot(void)
     SetFocus ((TwmWindow *) NULL, LastTimestamp());
     if (Scr->Focus != NULL)
     {
-	SetBorder (Scr->Focus, False);
+	SetBorder(Scr->Focus, False);
 	if (Scr->Focus->hilite_w) XUnmapWindow (dpy, Scr->Focus->hilite_w);
     }
     InstallWindowColormaps(0, &Scr->TwmRoot);
@@ -2386,7 +2344,7 @@ Iconify(TwmWindow *tmp_win, int def_x, int def_y)
 	    if (t->icon_w)
 	      XUnmapWindow(dpy, t->icon_w);
 	    SetMapStateProp(t, IconicState);
-	    SetBorder (t, False);
+	    SetBorder(t, False);
 	    if (t == Scr->Focus)
 	      {
 		SetFocus ((TwmWindow *) NULL, LastTimestamp());
@@ -2411,7 +2369,7 @@ Iconify(TwmWindow *tmp_win, int def_x, int def_y)
     XUnmapWindow(dpy, tmp_win->frame);
     SetMapStateProp(tmp_win, IconicState);
 
-    SetBorder (tmp_win, False);
+    SetBorder(tmp_win, False);
     if (tmp_win == Scr->Focus)
     {
 	SetFocus ((TwmWindow *) NULL, LastTimestamp());
@@ -2426,7 +2384,7 @@ Iconify(TwmWindow *tmp_win, int def_x, int def_y)
 
 
 static void
-Identify (TwmWindow *t)
+Identify(const TwmWindow* t)
 {
     int i, n, twidth, width, height;
     int x, y;
@@ -2513,7 +2471,7 @@ SetMapStateProp(TwmWindow *tmp_win, int state)
 
 
 Bool
-GetWMState (Window w, int *statep, Window *iwp)
+GetWMState(Window w, int *statep, Window *iwp)
 {
     Atom actual_type;
     int actual_format;
@@ -2583,50 +2541,51 @@ WarpToScreen (int n, int inc)
  * rotate our internal copy of WM_COLORMAP_WINDOWS
  */
 static void
-BumpWindowColormap (TwmWindow *tmp, int inc)
+BumpWindowColormap(TwmWindow& cmw, int inc)
 {
-    int i, j, previously_installed;
-    ColormapWindow **cwins;
 
-    if (!tmp) return;
 
-    if (inc && tmp->cmaps.number_cwins > 0) {
-	cwins = malloc(sizeof(ColormapWindow *) * tmp->cmaps.number_cwins);
+    if (inc && cmw.cmaps.number_cwins > 0) {
+	// must use malloc until uses in C files convert to C++ and new.
+	ColormapWindow **cwins{
+	    static_cast<ColormapWindow**>(malloc(sizeof(ColormapWindow*)
+						 * cmw.cmaps.number_cwins)) };
 	if (cwins) {
+	    int previously_installed;
 	    if ((previously_installed =
-		/* SUPPRESS 560 */(Scr->cmapInfo.cmaps == &tmp->cmaps &&
-	        tmp->cmaps.number_cwins))) {
-		for (i = tmp->cmaps.number_cwins; i-- > 0; )
-		    tmp->cmaps.cwins[i]->colormap->state = 0;
+		/* SUPPRESS 560 */(Scr->cmapInfo.cmaps == &cmw.cmaps &&
+	        cmw.cmaps.number_cwins))) {
+		for (int i = cmw.cmaps.number_cwins; i-- > 0; )
+		    cmw.cmaps.cwins[i]->colormap->state = 0;
 	    }
 
-	    for (i = 0; i < tmp->cmaps.number_cwins; i++) {
-		j = i - inc;
-		if (j >= tmp->cmaps.number_cwins)
-		    j -= tmp->cmaps.number_cwins;
+	    for (int i = 0; i < cmw.cmaps.number_cwins; ++i) {
+		int j{i-inc};
+		if (j >= cmw.cmaps.number_cwins)
+		    j -= cmw.cmaps.number_cwins;
 		else if (j < 0)
-		    j += tmp->cmaps.number_cwins;
-		cwins[j] = tmp->cmaps.cwins[i];
+		    j += cmw.cmaps.number_cwins;
+		cwins[j] = cmw.cmaps.cwins[i];
 	    }
 
-	    free(tmp->cmaps.cwins);
+	    free(cmw.cmaps.cwins);
 
-	    tmp->cmaps.cwins = cwins;
+	    cmw.cmaps.cwins = cwins;
 
-	    if (tmp->cmaps.number_cwins > 1)
-		bzero (tmp->cmaps.scoreboard,
-		       ColormapsScoreboardLength(&tmp->cmaps));
+	    if (cmw.cmaps.number_cwins > 1)
+		bzero (cmw.cmaps.scoreboard,
+		       ColormapsScoreboardLength(&cmw.cmaps));
 
 	    if (previously_installed)
 		InstallWindowColormaps(PropertyNotify, (TwmWindow *) NULL);
 	}
     } else
-	FetchWmColormapWindows (tmp);
+	FetchWmColormapWindows(&cmw);
 }
 
 
 void
-SetBorder (TwmWindow *tmp, Bool onoroff)
+SetBorder(TwmWindow *tmp, Bool onoroff)
 {
     if (onoroff) {
 	XSetWindowBorder (dpy, tmp->frame, tmp->border);
@@ -2641,21 +2600,20 @@ SetBorder (TwmWindow *tmp, Bool onoroff)
 
 
 static void
-DestroyMenu (MenuRoot *menu)
+DestroyMenu(MenuRoot *menu)
 {
-    MenuItem *item;
-
     if (menu->w) {
-	XDeleteContext (dpy, menu->w, MenuContext);
-	XDeleteContext (dpy, menu->w, ScreenContext);
-	if (Scr->Shadow) XDestroyWindow (dpy, menu->shadow);
+	XDeleteContext(dpy, menu->w, MenuContext);
+	XDeleteContext(dpy, menu->w, ScreenContext);
+	if (Scr->Shadow)
+	    XDestroyWindow(dpy, menu->shadow);
 	XDestroyWindow(dpy, menu->w);
     }
 
-    for (item = menu->first; item; ) {
-	MenuItem *tmp = item;
+    for (MenuItem* item = menu->first; item; ) {
+	MenuItem* tmp{item};
 	item = item->next;
-	free (tmp);
+	delete tmp;
     }
 }
 
@@ -2716,7 +2674,7 @@ WarpAlongRing (XButtonEvent *ev, Bool forward)
 
 
 static void
-WarpToWindow (TwmWindow *t)
+WarpToWindow(TwmWindow *t)
 {
     int x, y;
 
@@ -2746,33 +2704,33 @@ WarpToWindow (TwmWindow *t)
  *     data[1]		time stamp
  */
 static void
-send_clientmessage (Window w, Atom a, Time timestamp)
+send_clientmessage(Window w, Atom a, Time timestamp)
 {
-    XClientMessageEvent ev;
+    XEvent ev;
 
-    ev.type = ClientMessage;
-    ev.window = w;
-    ev.message_type = _XA_WM_PROTOCOLS;
-    ev.format = 32;
-    ev.data.l[0] = a;
-    ev.data.l[1] = timestamp;
-    XSendEvent (dpy, w, False, 0L, (XEvent *) &ev);
+    ev.xclient.type = ClientMessage;
+    ev.xclient.window = w;
+    ev.xclient.message_type = _XA_WM_PROTOCOLS;
+    ev.xclient.format = 32;
+    ev.xclient.data.l[0] = a;
+    ev.xclient.data.l[1] = timestamp;
+    XSendEvent(dpy, w, False, 0L, &ev);
 }
 
 void
-SendDeleteWindowMessage (TwmWindow *tmp, Time timestamp)
+SendDeleteWindowMessage(TwmWindow *tmp, Time timestamp)
 {
-    send_clientmessage (tmp->w, _XA_WM_DELETE_WINDOW, timestamp);
+    send_clientmessage(tmp->w, _XA_WM_DELETE_WINDOW, timestamp);
 }
 
 void
-SendSaveYourselfMessage (TwmWindow *tmp, Time timestamp)
+SendSaveYourselfMessage(TwmWindow *tmp, Time timestamp)
 {
-    send_clientmessage (tmp->w, _XA_WM_SAVE_YOURSELF, timestamp);
+    send_clientmessage(tmp->w, _XA_WM_SAVE_YOURSELF, timestamp);
 }
 
 void
-SendTakeFocusMessage (TwmWindow *tmp, Time timestamp)
+SendTakeFocusMessage(TwmWindow *tmp, Time timestamp)
 {
-    send_clientmessage (tmp->w, _XA_WM_TAKE_FOCUS, timestamp);
+    send_clientmessage(tmp->w, _XA_WM_TAKE_FOCUS, timestamp);
 }
diff --git a/src/menus.h b/src/menus.h
index cadffaf..0c4631f 100644
--- a/src/menus.h
+++ b/src/menus.h
@@ -164,29 +164,36 @@ extern int MenuDepth;
 #define COLORMAP_PREV "prev"
 #define COLORMAP_DEFAULT "default"
 
-extern void InitMenus ( void );
-extern Bool AddFuncKey ( char *name, int cont, int mods, int func, char *win_name, char *action );
-extern int CreateTitleButton ( const char *name, int func, const char *action, MenuRoot *menuroot, Bool rightside, Bool append );
-extern void InitTitlebarButtons ( void );
-extern void PaintEntry ( MenuRoot *mr, MenuItem *mi, int exposure );
-extern void PaintMenu ( MenuRoot *mr, XEvent *e );
-extern void UpdateMenu ( void );
-extern MenuRoot * NewMenuRoot ( const char *name );
-extern MenuItem * AddToMenu ( MenuRoot *menu, const char *item, const char *action, MenuRoot *sub, int func, const char *fore, const char *back );
-extern void MakeMenus ( void );
-extern Bool PopUpMenu ( MenuRoot *menu, int x, int y, Bool center );
-extern void PopDownMenu ( void );
-extern MenuRoot * FindMenuRoot ( const char *name );
-extern int ExecuteFunction ( int func, const char *action, Window w, TwmWindow *tmp_win, XEvent *eventp, int context, int pulldown );
-extern void ReGrab ( void );
-extern void FocusOnRoot ( void );
-extern void DeIconify ( TwmWindow *tmp_win );
-extern void Iconify ( TwmWindow *tmp_win, int def_x, int def_y );
-extern void SetMapStateProp ( TwmWindow *tmp_win, int state );
-extern void WarpToScreen ( int n, int inc );
-extern void SetBorder ( TwmWindow *tmp, Bool onoroff );
-extern void SendDeleteWindowMessage ( TwmWindow *tmp, Time timestamp );
-extern void SendSaveYourselfMessage ( TwmWindow *tmp, Time timestamp );
-extern void SendTakeFocusMessage ( TwmWindow *tmp, Time timestamp );
+#ifdef __cplusplus
+#define EXTERN extern "C"
+#else
+#define EXTERN extern
+#endif
+
+EXTERN void InitMenus( void );
+EXTERN Bool AddFuncKey( char *name, int cont, int mods, int func, char *win_name, char *action );
+EXTERN int CreateTitleButton( const char *name, int func, const char *action, MenuRoot *menuroot, Bool rightside, Bool append );
+EXTERN void InitTitlebarButtons( void );
+EXTERN void PaintEntry( MenuRoot *mr, MenuItem *mi, int exposure );
+EXTERN void PaintMenu( MenuRoot *mr, XEvent *e );
+EXTERN void UpdateMenu( void );
+EXTERN MenuRoot * NewMenuRoot( const char *name );
+EXTERN MenuItem * AddToMenu( MenuRoot *menu, const char *item, const char *action, MenuRoot *sub, int func, const char *fore, const char *back );
+EXTERN void MakeMenus( void );
+EXTERN Bool PopUpMenu( MenuRoot *menu, int x, int y, Bool center );
+EXTERN void PopDownMenu( void );
+EXTERN MenuRoot * FindMenuRoot( const char *name );
+EXTERN int ExecuteFunction( int func, const char *action, Window w, TwmWindow *tmp_win, XEvent *eventp, int context, int pulldown );
+EXTERN void ReGrab( void );
+EXTERN void FocusOnRoot( void );
+EXTERN void DeIconify( TwmWindow *tmp_win );
+EXTERN void Iconify( TwmWindow *tmp_win, int def_x, int def_y );
+EXTERN void SetMapStateProp( TwmWindow *tmp_win, int state );
+EXTERN void WarpToScreen( int n, int inc );
+EXTERN void SetBorder( TwmWindow *tmp, Bool onoroff );
+EXTERN void SendDeleteWindowMessage( TwmWindow *tmp, Time timestamp );
+EXTERN void SendSaveYourselfMessage( TwmWindow *tmp, Time timestamp );
+EXTERN void SendTakeFocusMessage( TwmWindow *tmp, Time timestamp );
+EXTERN Bool GetWMState( Window w, int *statep, Window *iwp );
 
 #endif /* _MENUS_ */
diff --git a/src/resize.h b/src/resize.h
index e227ed6..a1f9d22 100644
--- a/src/resize.h
+++ b/src/resize.h
@@ -61,18 +61,24 @@ in this Software without prior written authorization from The Open Group.
 #ifndef _RESIZE_
 #define _RESIZE_
 
-extern void AddEndResize ( TwmWindow *tmp_win );
-extern void AddStartResize ( TwmWindow *tmp_win, int x, int y, int w, int h );
-extern void ConstrainSize ( TwmWindow *tmp_win, int *widthp, int *heightp );
-extern void DoResize ( int x_root, int y_root, TwmWindow *tmp_win );
-extern void EndResize ( void );
-extern void fullzoom ( TwmWindow *tmp_win, int flag );
-extern void MenuDoResize ( int x_root, int y_root, TwmWindow *tmp_win );
-extern void MenuEndResize ( TwmWindow *tmp_win );
-extern void MenuStartResize ( TwmWindow *tmp_win, int x, int y, int w, int h );
-extern void SetFrameShape ( TwmWindow *tmp );
-extern void SetupFrame ( TwmWindow *tmp_win, int x, int y, int w, int h, int bw, Bool sendEvent );
-extern void SetupWindow ( TwmWindow *tmp_win, int x, int y, int w, int h, int bw );
-extern void StartResize ( XEvent *evp, TwmWindow *tmp_win, Bool fromtitlebar );
+#ifdef __cplusplus
+#define EXTERN extern "C"
+#else
+#define EXTERN extern
+#endif
+
+extern void AddEndResize( TwmWindow *tmp_win );
+extern void AddStartResize( TwmWindow *tmp_win, int x, int y, int w, int h );
+extern void ConstrainSize( TwmWindow *tmp_win, int *widthp, int *heightp );
+extern void DoResize( int x_root, int y_root, TwmWindow *tmp_win );
+extern void EndResize( void );
+EXTERN void fullzoom( TwmWindow *tmp_win, int flag );
+EXTERN void MenuDoResize( int x_root, int y_root, TwmWindow *tmp_win );
+EXTERN void MenuEndResize( TwmWindow *tmp_win );
+EXTERN void MenuStartResize( TwmWindow *tmp_win, int x, int y, int w, int h );
+extern void SetFrameShape( TwmWindow *tmp );
+extern void SetupFrame( TwmWindow *tmp_win, int x, int y, int w, int h, int bw, Bool sendEvent );
+extern void SetupWindow( TwmWindow *tmp_win, int x, int y, int w, int h, int bw );
+EXTERN void StartResize( XEvent *evp, TwmWindow *tmp_win, Bool fromtitlebar );
 
 #endif /* _RESIZE_ */
diff --git a/src/screen.h b/src/screen.h
index 7f0e261..7aaeb57 100644
--- a/src/screen.h
+++ b/src/screen.h
@@ -87,7 +87,7 @@ typedef struct ScreenInfo
     int hilite_pm_width, hilite_pm_height;  /* cache the size */
 
     MenuRoot *MenuList;		/* head of the menu list */
-    MenuRoot *LastMenu;		/* the last menu (mostly unused?) */
+    MenuRoot *LastMenu;		/* the last menu (for adding to menu list) */
     MenuRoot *Windows;		/* the TwmWindows menu */
 
     TwmWindow *Ring;		/* one of the windows in window ring */
@@ -212,7 +212,6 @@ typedef struct ScreenInfo
     short InterpolateMenuColors;/* make pretty menus */
     short HaveFonts;		/* set if fonts have been loaded */
     short FirstTime;		/* first time we've read .twmrc */
-    short CaseSensitive;	/* be case-sensitive when sorting names */
     short WarpUnmapped;		/* allow warping to unmapped windows */
 
     FuncKey FuncKeyRoot;
diff --git a/src/twm.c b/src/twm.c
index 0350b0d..d315886 100644
--- a/src/twm.c
+++ b/src/twm.c
@@ -722,7 +722,6 @@ InitVariables(void)
     Scr->InterpolateMenuColors = FALSE;
     Scr->FirstTime = TRUE;
     Scr->HaveFonts = FALSE;		/* i.e. not loaded yet */
-    Scr->CaseSensitive = TRUE;
     Scr->WarpUnmapped = FALSE;
 
     /* setup default fonts; overridden by defaults from system.twmrc */
@@ -748,7 +747,7 @@ InitVariables(void)
 }
 
 void
-CreateFonts (void)
+CreateFonts(void)
 {
     GetFont(&Scr->TitleBarFont);
     GetFont(&Scr->MenuFont);
@@ -795,7 +794,7 @@ RestoreWithdrawnLocation (TwmWindow *tmp)
 
 
 void
-Reborder (Time time)
+Reborder(Time time)
 {
     TwmWindow *tmp;			/* temp twm window structure */
     int scrnum;
diff --git a/src/twm.h b/src/twm.h
index f92d66f..6cc8880 100644
--- a/src/twm.h
+++ b/src/twm.h
@@ -74,6 +74,12 @@ from The Open Group.
 #include <X11/StringDefs.h>
 #include <X11/Intrinsic.h>
 
+#ifdef __cplusplus
+#define EXTERN extern "C"
+#else
+#define EXTERN extern
+#endif
+
 #ifndef WithdrawnState
 #define WithdrawnState 0
 #endif
@@ -327,11 +333,10 @@ typedef struct TWMWinConfigEntry
 
 #include <X11/Xosdefs.h>
 #include <stdlib.h>
-extern void CreateFonts ( void );
+EXTERN void CreateFonts( void );
 extern void RestoreWithdrawnLocation ( TwmWindow *tmp );
-extern void Reborder( Time time);
-extern void Done( XtPointer, XtSignalId * ) _X_NORETURN;
-extern void ComputeCommonTitleOffsets ( void );
+EXTERN void Reborder( Time time);
+EXTERN void Done( XtPointer, XtSignalId * ) _X_NORETURN;
 extern void ComputeTitleLocation ( TwmWindow *tmp );
 extern void ComputeWindowTitleOffsets ( TwmWindow *tmp_win, int width, Bool squeeze );
 extern char *ProgramName;
@@ -373,7 +378,6 @@ extern int Argc;
 extern char **Argv;
 extern void NewFontCursor ( Cursor *cp, const char *str );
 extern void NewBitmapCursor ( Cursor *cp, char *source, char *mask );
-extern Pixmap CreateMenuIcon ( int height, unsigned int *widthp, unsigned int *heightp );
 
 extern Bool ErrorOccurred;
 extern XErrorEvent LastErrorEvent;
@@ -381,7 +385,6 @@ extern XErrorEvent LastErrorEvent;
 #define ResetError() (ErrorOccurred = False)
 
 extern Bool RestartPreviousState;
-extern Bool GetWMState ( Window w, int *statep, Window *iwp );
 
 extern void twmrc_error_prefix ( void );
 
diff --git a/src/util.c b/src/util.c
index 4f282bc..fd17771 100644
--- a/src/util.c
+++ b/src/util.c
@@ -919,14 +919,14 @@ CreateQuestionPixmap (unsigned *widthp, unsigned *heightp)
 
 
 static Pixmap
-CreateMenuPixmap (unsigned *widthp, unsigned *heightp)
+CreateMenuPixmap(unsigned *widthp, unsigned *heightp)
 {
-    return CreateMenuIcon (Scr->TBInfo.width - Scr->TBInfo.border * 2,
-			   widthp,heightp);
+    return CreateMenuIcon(Scr->TBInfo.width - Scr->TBInfo.border * 2,
+			  widthp, heightp);
 }
 
 Pixmap
-CreateMenuIcon (int height, unsigned *widthp, unsigned *heightp)
+CreateMenuIcon(int height, unsigned *widthp, unsigned *heightp)
 {
     int h, w;
     int ih, iw;
diff --git a/src/util.h b/src/util.h
index d4f6ce6..6de63c7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -71,7 +71,8 @@ EXTERN void MoveOutline ( Window root, int x, int y, int width, int height,
 			  int bw, int th );
 EXTERN void Zoom ( Window wf, Window wt );
 EXTERN const char * ExpandFilename ( const char *name );
-EXTERN void GetUnknownIcon ( const char *name );
+EXTERN Pixmap CreateMenuIcon( int height, unsigned int *widthp, unsigned int *heightp );
+EXTERN void GetUnknownIcon( const char *name );
 EXTERN Pixmap FindBitmap ( const char *name, unsigned int *widthp,
 			   unsigned int *heightp );
 EXTERN Pixmap GetBitmap ( const char *name );