// w32dlib - Win32 Dialog Helpers // // Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk) // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // ------------------------------------------------------------------------- // #include #include #include "w32dlib/common.h" namespace W32DLib { static const char *def_msg="Message"; static const char *def_err="Error"; static const char *def_qry="Error"; // ------------------------------------------------------------ // void Common::Initialise() { InitCommonControls(); CoInitialize(NULL); } // ------------------------------------------------------------ // void Common::MessageTitle(const char *title) { def_msg=title; def_err=title; def_qry=title; } // ------------------------------------------------------------ // void Common::Message(HWND parent, const char *title, const char *msg) { ::MessageBox(parent,msg,title ? title:def_msg,MB_ICONINFORMATION|MB_OK); } // ------------------------------------------------------------ // void Common::Message(HWND parent, const char *title, const std::string& msg) { ::MessageBox(parent,msg.c_str(),title ? title:def_msg, MB_ICONINFORMATION|MB_OK); } // ------------------------------------------------------------ // void Common::Error(HWND parent, const char *title, const char *msg) { ::MessageBox(parent,msg,title ? title:def_err,MB_ICONSTOP|MB_OK); } // ------------------------------------------------------------ // void Common::Error(HWND parent, const char *title, const std::string& msg) { ::MessageBox(parent,msg.c_str(),title ? title:def_err,MB_ICONSTOP|MB_OK); } // ------------------------------------------------------------ // bool Common::Query(HWND parent, const char *title, const char *msg) { return ::MessageBox(parent,msg,title ? title:def_qry, MB_ICONQUESTION|MB_YESNO)==IDOK; } // ------------------------------------------------------------ // bool Common::Query(HWND parent, const char *title, const std::string& msg) { return ::MessageBox(parent,msg.c_str(),title ? title:def_qry, MB_ICONQUESTION|MB_YESNO)==IDOK; } // ------------------------------------------------------------ // bool Common::OpenFile(HWND parent, const char *title, std::string& path, const char *filter) { 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; } } // ------------------------------------------------------------ // bool Common::SaveFile(HWND parent, const char *title, std::string& path, const char *filter) { 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; } } // ------------------------------------------------------------ // bool Common::SelectDir(HWND parent, const char *title, std::string& path) { char name[1024]; LPITEMIDLIST sel=NULL; BROWSEINFO bi={0}; strcpy(name,path.c_str()); bi.hwndOwner=parent; bi.pidlRoot=NULL; bi.pszDisplayName=name; bi.lpszTitle=title; bi.ulFlags=0; bi.lpfn=BrowseCallback; bi.lParam=(LPARAM)name; sel=SHBrowseForFolder(&bi); if (SHGetPathFromIDList(sel,name)) { path=name; return true; } else { return false; } } // ------------------------------------------------------------ // int CALLBACK Common::BrowseCallback(HWND w, UINT u, LPARAM p1, LPARAM p2) { if (u==BFFM_INITIALIZED) { PostMessage(w,BFFM_SETSELECTION,TRUE,p2); } return 0; } // ------------------------------------------------------------ // std::string Common::GetOSError() { std::string res; LPVOID msg; DWORD dw=GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &msg, 0, NULL ); res=static_cast(msg); LocalFree(msg); return res; } // ------------------------------------------------------------ // HINSTANCE Common::GetInstance() { MEMORY_BASIC_INFORMATION mbi; VirtualQuery((void *)GetInstance, &mbi, sizeof(mbi)); return (HINSTANCE)mbi.AllocationBase; } }; // namespace W32DLib // END OF FILE