Skip to content

Commit

Permalink
Adjust File>Exit menu item changes to do things the C++/Windows way
Browse files Browse the repository at this point in the history
Re: dictation-toolbox#184.

That is, use boolean flags and a dedicated user window message to
convey updates to the output window.  This makes it easier to update
the pop-up menu later down the line, if desired, by adding and hand-
ling new flags.
  • Loading branch information
drmfinlay committed Oct 19, 2024
1 parent fcca8b8 commit 9eee5b9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion NatlinkSource/DragonCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ BOOL CDragonCode::natConnect( IServiceProvider * pIDgnSite, BOOL bUseThreads )

if( pIDgnSite != NULL )
{
m_pSecdThrd = new MessageWindow( FALSE );
m_pSecdThrd = new MessageWindow( 0 );
}

// Connect to NatSpeak
Expand Down
63 changes: 43 additions & 20 deletions NatlinkSource/MessageWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
// thread.
#define WM_EXITTHREAD (WM_USER+363)

// Send this message to the output window to update it as specified.
#define WM_UPDATEWINDOW (WM_USER+364)

// WPARAM flags for the WM_UPDATEWINDOW message.
#define IDR_MENU_ALLOW_USER_EXIT 0x01

// The color for error messages
#define DARKRED RGB( 128, 0, 0 )

Expand All @@ -51,31 +57,36 @@ INT_PTR CALLBACK dialogProc(

return TRUE;
case WM_INITDIALOG:
HMENU hMenu, hSubMenu;
// save a pointer to the message window for later
s_pSecdThrd = (MessageWindow *)lParam;

// hide the window to start with
ShowWindow( hWnd, SW_HIDE );
return TRUE;

case WM_UPDATEWINDOW:
HINSTANCE hInstance;
HMENU hMenu, hSubMenu;
UINT uFlags; // for ModifyMenu()
LPTSTR lpszBuffer;

// Save a pointer to the message window for later.
s_pSecdThrd = (MessageWindow *)lParam;

// Load the popup menu.
// load the pop-up menu
hInstance = _Module.GetModuleInstance();
hMenu = LoadMenu( hInstance, MAKEINTRESOURCE( IDR_MENU ) );

// Enable the exit sub-menu item, if requested.
s_pSecdThrd = (MessageWindow *)lParam;
if ( s_pSecdThrd->getAllowUserExit() )
{
hSubMenu = GetSubMenu( hMenu, 0 );
lpszBuffer = new TCHAR[32];
GetMenuString( hMenu, IDD_EXIT, lpszBuffer, 32, MF_BYCOMMAND );
ModifyMenu( hSubMenu, IDD_EXIT, MF_BYCOMMAND | MF_ENABLED | MF_STRING,
IDD_EXIT, lpszBuffer );
}

// Assign IDR_MENU to the window and hide it.
// enable/disable the exit sub-menu item
uFlags = MF_BYCOMMAND | MF_STRING;
if( wParam & IDR_MENU_ALLOW_USER_EXIT )
uFlags |= MF_ENABLED;
else
uFlags |= MF_GRAYED;
hSubMenu = GetSubMenu( hMenu, 0 );
lpszBuffer = new TCHAR[32];
GetMenuString( hMenu, IDD_EXIT, lpszBuffer, 32, MF_BYCOMMAND );
ModifyMenu( hSubMenu, IDD_EXIT, uFlags, IDD_EXIT, lpszBuffer );

// assign the modified menu to the window
SetMenu( hWnd, hMenu );
ShowWindow( hWnd, SW_HIDE );
return TRUE;

case WM_EXITTHREAD:
Expand Down Expand Up @@ -200,12 +211,11 @@ DWORD CALLBACK threadMain( void * pArg )

//---------------------------------------------------------------------------

MessageWindow::MessageWindow( BOOL bAllowUserExit )
MessageWindow::MessageWindow( DWORD dwFlags )
{
// create the thread we use to display messages; we use an event to make
// sure that the thread has started before continuing

m_bAllowUserExit = bAllowUserExit;
m_hEvent = CreateEvent(
NULL, // security options
TRUE, // manual reset
Expand All @@ -228,6 +238,9 @@ MessageWindow::MessageWindow( BOOL bAllowUserExit )
CloseHandle( m_hEvent );
m_hEvent = NULL;
}

// update the window with the specified flags
updateWindow( dwFlags );
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -258,3 +271,13 @@ void MessageWindow::displayText(const char * pszText, BOOL bError )
PostMessage( m_hOutWnd, WM_SHOWTEXT, bError, (LPARAM)pszCopy );
}
}

//---------------------------------------------------------------------------

void MessageWindow::updateWindow( DWORD dwFlags )
{
if( m_hOutWnd )
{
PostMessage( m_hOutWnd, WM_UPDATEWINDOW, dwFlags, 0 );
}
}
10 changes: 4 additions & 6 deletions NatlinkSource/MessageWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MessageWindow
{
public:
// the constructor will create the thread
MessageWindow( BOOL bAllowUserExit );
MessageWindow( DWORD dwFlags );

// the destructor will terminate the thread
~MessageWindow();
Expand All @@ -23,11 +23,13 @@ class MessageWindow
// WM_COMMAND messages received by the output window should be reposted
void setMsgWnd( HWND hWnd ) { m_hMsgWnd = hWnd; }

// this will update the output window
void updateWindow( DWORD dwFlags );

// these are called from the second thread itself
void setOutWnd( HWND hWnd ) { m_hOutWnd = hWnd; }
void signalEvent() { SetEvent( m_hEvent ); }
HWND getMsgWnd() { return m_hMsgWnd; }
BOOL getAllowUserExit() { return m_bAllowUserExit; }

protected:
// This is the window handle in the primarty thread where we
Expand All @@ -44,8 +46,4 @@ class MessageWindow

// This is the handle of the output window itself
HWND m_hOutWnd;

// This is the boolean member we use to enable/disable the File>Exit menu
// item.
BOOL m_bAllowUserExit;
};

0 comments on commit 9eee5b9

Please sign in to comment.