// w32dlib - Win32 Window 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 "w32dlib/window.h" namespace W32DLib { // ------------------------------------------------------------ // Window::ProcSet Window::m_procset; // ------------------------------------------------------------ // Window::Window() : m_wnd(0) { std::ostringstream name; name << "W32DLIB_MUTEX_" << GetCurrentProcessId(); m_mutex=CreateMutex(NULL,FALSE,name.str().c_str()); } // ------------------------------------------------------------ // Window::~Window() { if (m_mutex!=NULL) { CloseHandle(m_mutex); } } // ------------------------------------------------------------ // bool Window::HasMutex() { return m_mutex!=NULL; } // ------------------------------------------------------------ // HWND Window::GetHWND() { return m_wnd; } // ------------------------------------------------------------ // HMENU Window::GetHMENU() { return GetMenu(m_wnd); } // ------------------------------------------------------------ // bool Window::SetHMENU(HMENU menu) { return SetMenu(m_wnd,menu)!=0; } // ------------------------------------------------------------ // bool Window::Move(int x, int y, int width, int height, bool repaint) { return MoveWindow(m_wnd,x,y,width,height,repaint ? TRUE:FALSE)!=0; } // ------------------------------------------------------------ // bool Window::MoveBase(Window *w, int x, int y, int width, int height, bool repaint) { RECT r; r.left=x; r.top=y; r.right=width; r.bottom=height; if (MapDialogRect(w->m_wnd,&r)) { return MoveWindow(m_wnd,r.left,r.top, r.right,r.bottom,repaint ? TRUE:FALSE)!=0; } else { return false; } } // ------------------------------------------------------------ // LRESULT Window::SendMsg(UINT msg, WPARAM wp, LPARAM lp) { return SendMessage(m_wnd,msg,wp,lp); } // ------------------------------------------------------------ // void Window::SetText(const char *text) { SendMsg(WM_SETTEXT,0,reinterpret_cast(text)); } // ------------------------------------------------------------ // void Window::SetText(const std::string& text) { W32DEBUGOUT("WND=" << m_wnd << " SetText(" << EncodeCString(text.c_str()) << ")"); SetText(text.c_str()); } // ------------------------------------------------------------ // std::string Window::GetText() { int len=GetWindowTextLength(m_wnd); char *buff=new char[len+1]; std::string res; buff[0]=0; SendMsg(WM_GETTEXT,len+1,reinterpret_cast(buff)); W32DEBUGOUT("WND=" << m_wnd << " GetText(" << EncodeCString(buff) << ")"); res=buff; delete[] buff; return res; } // ------------------------------------------------------------ // void Window::Enable(bool enable) { EnableWindow(m_wnd,enable); } // ------------------------------------------------------------ // BOOL Window::InstanceProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) { DWORD res=WAIT_OBJECT_0; bool ret=FALSE; switch(msg) { case WM_INITDIALOG: case WM_CREATE: m_wnd=wnd; if (m_mutex) { res=WaitForSingleObject(m_mutex,INFINITE); } if (res==WAIT_OBJECT_0) { try { m_procset[wnd]=this; } catch (...) { } if (m_mutex) { ReleaseMutex(m_mutex); } } ret=TRUE; break; case WM_DESTROY: m_procset.erase(wnd); break; default: break; } return ret; } // ------------------------------------------------------------ // BOOL CALLBACK Window::WindowProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) { if (msg==WM_INITDIALOG || msg==WM_CREATE) { Window *window=reinterpret_cast(lp); return window->InstanceProc(wnd,msg,wp,lp); } else { if (m_procset.count(wnd)) { return m_procset[wnd]->InstanceProc(wnd,msg,wp,lp); } else { return FALSE; } } } }; // namespace w32dlib // END OF FILE