From bf252f63d520be07a938076518aa17c25b11f8be Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 25 Mar 2005 02:25:10 +0000 Subject: Implemented OpenFile and SaveFile. Removed SelectDirectory as it depends on IE controls and COM --- src/common.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++-------- src/test/dialog.h | 6 ++++ src/test/dialog.rc | 8 ++++- src/test/w32dtst.cpp | 61 +++++++++++++++++++++++++++++++----- src/w32dlib/common.h | 29 +++++++++++------- src/w32dlib/w32dlib.h | 1 + 6 files changed, 159 insertions(+), 31 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index ced7779..2c9d01f 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -18,6 +18,7 @@ // // ------------------------------------------------------------------------- // +#include #include "w32dlib/common.h" namespace W32DLib @@ -49,25 +50,85 @@ bool Common::Query(HWND parent, const char *msg) // ------------------------------------------------------------ // -std::string OpenFile(const char *filter) +bool Common::OpenFile(HWND parent, + const char *title, + std::string& path, + const char *filter) { - return std::string(); + char buff[1024]; + OPENFILENAME ofn={sizeof(OPENFILENAME),0}; + + if (path.length()) + { + std::strcpy(buff,path.c_str()); + } + else + { + buff[0]=0; + } + + ofn.hwndOwner=parent; + ofn.lpstrFilter=filter; + ofn.lpstrCustomFilter=NULL; + ofn.nFilterIndex=1; + ofn.lpstrFile=buff; + ofn.nMaxFile=sizeof buff; + ofn.lpstrFileTitle=NULL; + ofn.lpstrInitialDir=NULL; + ofn.lpstrTitle=title; + ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY; + + if (GetOpenFileName(&ofn)) + { + path=buff; + return true; + } + else + { + return false; + } } // ------------------------------------------------------------ // -std::string SaveFile(const char *filter) +bool Common::SaveFile(HWND parent, + const char *title, + std::string& path, + const char *filter) { - return std::string(); -} - - -// ------------------------------------------------------------ -// -std::string SelectDirectory() -{ - return std::string(); + char buff[1024]; + OPENFILENAME ofn={sizeof(OPENFILENAME),0}; + + if (path.length()) + { + std::strcpy(buff,path.c_str()); + } + else + { + buff[0]=0; + } + + ofn.hwndOwner=parent; + ofn.lpstrFilter=filter; + ofn.lpstrCustomFilter=NULL; + ofn.nFilterIndex=1; + ofn.lpstrFile=buff; + ofn.nMaxFile=sizeof buff; + ofn.lpstrFileTitle=NULL; + ofn.lpstrInitialDir=NULL; + ofn.lpstrTitle=title; + ofn.Flags=0; + + if (GetSaveFileName(&ofn)) + { + path=buff; + return true; + } + else + { + return false; + } } diff --git a/src/test/dialog.h b/src/test/dialog.h index d1dee42..ac47a4e 100644 --- a/src/test/dialog.h +++ b/src/test/dialog.h @@ -4,3 +4,9 @@ #define IDBUTTON 4 #define IDQUIT 5 #define IDCHECK 6 +#define IDLOADTXT 7 +#define IDLOADBUT 8 +#define IDSAVETXT 9 +#define IDSAVEBUT 10 +#define IDDIRTXT 11 +#define IDDIRBUT 12 diff --git a/src/test/dialog.rc b/src/test/dialog.rc index cae59bb..27f1ea6 100644 --- a/src/test/dialog.rc +++ b/src/test/dialog.rc @@ -1,7 +1,7 @@ #include #include "dialog.h" -TESTDLG DIALOG 10, 10, 240, 100 +TESTDLG DIALOG 10, 10, 240, 180 STYLE WS_POPUP | WS_BORDER CAPTION "Test Dialog" FONT 8,"MS Shell Dlg" @@ -11,4 +11,10 @@ TESTDLG DIALOG 10, 10, 240, 100 PUSHBUTTON "Try Me!", IDBUTTON, 10, 30, 150, 18 AUTOCHECKBOX "Check", IDCHECK, 10,50,150,18 PUSHBUTTON "Quit", IDQUIT, 10,70,150,18 + + EDITTEXT IDLOADTXT, 10, 100, 200, 12 + PUSHBUTTON "Load ...",IDLOADBUT, 10, 115, 100, 18 + + EDITTEXT IDSAVETXT, 10, 140, 200, 12 + PUSHBUTTON "Save ...",IDSAVEBUT, 10, 155, 100, 18 } diff --git a/src/test/w32dtst.cpp b/src/test/w32dtst.cpp index 6ce87d1..5f14a6d 100644 --- a/src/test/w32dtst.cpp +++ b/src/test/w32dtst.cpp @@ -27,16 +27,24 @@ class Test : public W32DLib::Dialog { public: - Test() : m_static(this,IDSTATIC1), - m_text(this,IDTEXT), - m_check(this,IDCHECK), - m_button(this,IDBUTTON), - m_quit(this,IDQUIT) + Test() : m_static(this,IDSTATIC1) + , m_text(this,IDTEXT) + , m_check(this,IDCHECK) + , m_button(this,IDBUTTON) + , m_quit(this,IDQUIT) + , m_load(this,IDLOADTXT) + , m_loadbut(this,IDLOADBUT) + , m_save(this,IDSAVETXT) + , m_savebut(this,IDSAVEBUT) { AddControl(&m_text); AddControl(&m_check); AddControl(&m_button); AddControl(&m_quit); + AddControl(&m_load); + AddControl(&m_loadbut); + AddControl(&m_save); + AddControl(&m_savebut); m_button.OnPress (this,static_cast(&Test::OnButton)); @@ -44,6 +52,11 @@ public: (this,static_cast(&Test::OnQuit)); m_check.OnPress (this,static_cast(&Test::OnCheck)); + + m_loadbut.OnPress + (this,static_cast(&Test::OnLoad)); + m_savebut.OnPress + (this,static_cast(&Test::OnSave)); } virtual ~Test() @@ -70,6 +83,15 @@ private: W32DLib::Button m_button; W32DLib::Button m_quit; + W32DLib::Text m_load; + W32DLib::Button m_loadbut; + + W32DLib::Text m_save; + W32DLib::Button m_savebut; + + std::string m_loadpath; + std::string m_savepath; + BOOL OnQuit(UINT msg, WPARAM wp, LPARAM lp) { std::cout << "Called OnQuit()" << std::endl; @@ -95,8 +117,33 @@ private: std::cout << "Called OnCheck() - state " << state << std::endl; - //m_quit.Enable(state==W32DLib::AutoCheck::eChecked); - Enable(state==W32DLib::AutoCheck::eChecked); + m_quit.Enable(state==W32DLib::AutoCheck::eChecked); + + return TRUE; + } + + BOOL OnLoad(UINT msg, WPARAM wp, LPARAM lp) + { + if (W32DLib::Common::OpenFile(this->GetHWND(), + "Select file to open", + m_loadpath, + "CPP Files\0*.cpp\0Any File\0*.*\0")) + { + m_load.SetText(m_loadpath.c_str()); + } + + return TRUE; + } + + BOOL OnSave(UINT msg, WPARAM wp, LPARAM lp) + { + if (W32DLib::Common::SaveFile(this->GetHWND(), + "Select file to save", + m_savepath, + NULL)) + { + m_save.SetText(m_savepath.c_str()); + } return TRUE; } diff --git a/src/w32dlib/common.h b/src/w32dlib/common.h index bb9547f..357297f 100644 --- a/src/w32dlib/common.h +++ b/src/w32dlib/common.h @@ -62,25 +62,32 @@ namespace W32DLib /// \brief Requests a file to open. /// + /// \param parent The parent window. + /// \param title Title for the selector. + /// \param path The current filename. This will hold the selected name + /// on return if a file is selected. /// \param filter The file selector filter. - /// \return The path, or an empty string if non selected. + /// \return True if a file was selected and path updated. /// - static std::string OpenFile(const char *filter); + static bool OpenFile(HWND parent, + const char *title, + std::string& path, + const char *filter); /// \brief Requests a file to save. /// + /// \param parent The parent window. + /// \param title Title for the selector. + /// \param path The current filename. This will hold the selected name + /// on return if a file is selected. /// \param filter The file selector filter. - /// \return The path, or an empty string if non selected. + /// \return True if a file was selected and path updated. /// - static std::string SaveFile(const char *filter); - - - /// \brief Selects a directory - /// - /// \return The path, or an empty string if non selected. - /// - static std::string SelectDirectory(); + static bool SaveFile(HWND parent, + const char *title, + std::string& path, + const char *filter); /// \brief Returns the last error as a readable string. diff --git a/src/w32dlib/w32dlib.h b/src/w32dlib/w32dlib.h index 914179e..b1b813c 100644 --- a/src/w32dlib/w32dlib.h +++ b/src/w32dlib/w32dlib.h @@ -29,6 +29,7 @@ #include #include #include +#include #endif // W32DLIB_H -- cgit v1.2.3