From 98164e7547f7ea21204e69885158cd0494e08c5d Mon Sep 17 00:00:00 2001 From: Ian C Date: Sat, 2 Apr 2005 01:53:30 +0000 Subject: Added simple support for dialog menus --- src/dialog.cpp | 24 +++++++++++++++++++++++- src/test/dialog.h | 8 ++++++++ src/test/dialog.rc | 19 +++++++++++++++++++ src/test/w32dtst.cpp | 26 ++++++++++++++++++++++++++ src/w32dlib/dialog.h | 23 ++++++++++++++++++++++- src/w32dlib/window.h | 7 +++++++ src/window.cpp | 8 ++++++++ 7 files changed, 113 insertions(+), 2 deletions(-) diff --git a/src/dialog.cpp b/src/dialog.cpp index 512dbdd..7a26a16 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -28,6 +28,7 @@ namespace W32DLib // ------------------------------------------------------------ // Dialog::Dialog() : Window() + , m_menuowner(0) { } @@ -92,6 +93,15 @@ void Dialog::AddControl(Control *control) } +// ------------------------------------------------------------ +// +void Dialog::SetMenuProc(Window *owner, W32DLibCallback callback) +{ + m_menuowner=owner; + m_menuproc=callback; +} + + // ------------------------------------------------------------ // @@ -99,6 +109,7 @@ BOOL Dialog::InstanceProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) { BOOL ret=FALSE; WORD lo=LOWORD(wp); + bool handled=false; switch(msg) { @@ -127,7 +138,7 @@ BOOL Dialog::InstanceProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) ret=Window::InstanceProc(wnd,msg,wp,lp); break; - // Messages passed onto controls + // Messages passed onto menus and controls // case WM_COMMAND: // Check for windows and resources in the wp @@ -141,9 +152,20 @@ BOOL Dialog::InstanceProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) " for resource " << (*i)->ResourceID()); ret=(*i)->ProcessMessage(msg,wp,lp); + handled=true; break; } } + + if (!handled && m_menuowner) + { + W32DEBUGOUT(MsgName(msg) << " (" << msg << + ", " << wp << ", " << lp << ")" << + " passed onto MenuProc"); + + ret=(m_menuowner->*m_menuproc)(msg,wp,lp); + } + break; default: diff --git a/src/test/dialog.h b/src/test/dialog.h index 5b08673..7123816 100644 --- a/src/test/dialog.h +++ b/src/test/dialog.h @@ -11,3 +11,11 @@ #define IDDIRTXT 11 #define IDDIRBUT 12 #define IDCOMBO 13 +#define IDMENU 14 +#define IDMENUFILE 15 +#define IDMENULOAD 16 +#define IDMENUSAVE 17 +#define IDMENUQUIT 18 +#define IDMENUTEST 19 +#define IDMENUTESTITEM1 20 +#define IDMENUTESTITEM2 21 diff --git a/src/test/dialog.rc b/src/test/dialog.rc index d280453..ed3508e 100644 --- a/src/test/dialog.rc +++ b/src/test/dialog.rc @@ -5,6 +5,7 @@ TESTDLG DIALOG 10, 10, 500, 300 STYLE WS_POPUP | WS_BORDER CAPTION "Test Dialog" FONT 8,"MS Shell Dlg" + MENU IDMENU { PUSHBUTTON "Try Me!", IDBUTTON, 10, 10, 150, 18 AUTOCHECKBOX "Check", IDCHECK, 10,30,150,18 @@ -21,3 +22,21 @@ TESTDLG DIALOG 10, 10, 500, 300 LTEXT "Text:", IDSTATIC1, 210, 10, 35, 12 EDITTEXT IDTEXT, 250, 10, 100, 100, ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_MULTILINE | ES_WANTRETURN } + +IDMENU MENU +{ + POPUP "File" + { + MENUITEM "Load", IDMENULOAD + MENUITEM "Save", IDMENUSAVE + MENUITEM "Quit", IDMENUQUIT + } + POPUP "Test" + { + MENUITEM "Test Item 1", IDMENUTESTITEM1 + POPUP "Sub" + { + MENUITEM "Test Item 2", IDMENUTESTITEM2 + } + } +} diff --git a/src/test/w32dtst.cpp b/src/test/w32dtst.cpp index 0e09ea2..75b5b44 100644 --- a/src/test/w32dtst.cpp +++ b/src/test/w32dtst.cpp @@ -59,6 +59,9 @@ public: (this,static_cast(&Test::OnComboDbl)); m_combo.OnTextChanged (this,static_cast(&Test::OnComboTxt)); + + SetMenuProc + (this,static_cast(&Test::OnMenu)); } virtual ~Test() @@ -201,6 +204,25 @@ private: std::cout << ":GetText=" << m_text.GetText() << std::endl; return TRUE; } + + BOOL OnMenu(UINT msg, WPARAM wp, LPARAM lp) + { + int cmd=LOWORD(wp); + + std::cout << "Menu command " << cmd << std::endl; + + switch(cmd) + { + case IDMENUQUIT: + Dialog::Close(IDCANCEL); + break; + + default: + break; + } + + return TRUE; + } }; int WINAPI WinMain (HINSTANCE hInstance, @@ -217,6 +239,10 @@ int WINAPI WinMain (HINSTANCE hInstance, { std::cout << "res=IDOK" << std::endl; } + else if (i==IDCANCEL) + { + std::cout << "res=IDCANCEL" << std::endl; + } else { std::cout << "res=" << i << std::endl; diff --git a/src/w32dlib/dialog.h b/src/w32dlib/dialog.h index f4ba5c6..34c22f4 100644 --- a/src/w32dlib/dialog.h +++ b/src/w32dlib/dialog.h @@ -70,7 +70,7 @@ namespace W32DLib /// Call this to display the dialog. /// /// \param instance The HINSTANCE. Call Common::GetInstance() - /// to get the applications instance (from a DLL, for example). + /// to get the applications instance (from a DLL, for example). /// \param parent The parent window of the dialog (NULL for none). /// \return The result from Close(). /// \sa Close() @@ -92,8 +92,26 @@ namespace W32DLib /// Note that the pointer to the control is stored, so the control /// must exist as long as the dialog is in use. /// + /// \param control The control the add. + /// void AddControl(Control *control); + /// \brief Sets a callback for handling menu commands. + /// + /// This call simply passes on all WM_COMMAND messages that were not + /// for any registered control's resource ID. + /// + /// Due to this, the callback should take care to ensure it only + /// actions recognised menu commands. + /// + /// \param owner The class (generally a Dialog derived one) in which + /// the callback resides. The class must be derived from + /// Window. + /// \param callback The callback. + /// + void SetMenuProc(Window *owner, W32DLibCallback callback); + + protected: /// \brief Handles windows messages. @@ -113,6 +131,9 @@ namespace W32DLib typedef std::vector ControlSet; + Window *m_menuowner; + W32DLibCallback m_menuproc; + bool m_open; ControlSet m_cset; diff --git a/src/w32dlib/window.h b/src/w32dlib/window.h index 4dee89b..bd17ede 100644 --- a/src/w32dlib/window.h +++ b/src/w32dlib/window.h @@ -47,6 +47,13 @@ namespace W32DLib /// HWND GetHWND(); + /// \brief Returns the HMENU associated with this window. + /// + /// This call will only work while the window is on display. + /// The behaviour is undefined if it is called at any other time. + /// + HMENU GetHMENU(); + /// \brief Send a message to the window. /// /// This call will only work while the window is on display. diff --git a/src/window.cpp b/src/window.cpp index af662b9..5ab893e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -49,6 +49,14 @@ HWND Window::GetHWND() } +// ------------------------------------------------------------ +// +HMENU Window::GetHMENU() +{ + return GetMenu(m_wnd); +} + + // ------------------------------------------------------------ // LRESULT Window::SendMsg(UINT msg, WPARAM wp, LPARAM lp) -- cgit v1.2.3