📄 gui.cpp
字号:
/* * Copyright (c) 2001 Intel Corporation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. Neither the name of the Intel Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. *//* * gui: Win32 GUI Trutella interface. */#include "gui.h"CTrutellaApp g_app;IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() //}}AFX_MSG_MAPEND_MESSAGE_MAP()static UINT indicators[] ={ ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,};CMainFrame::CMainFrame(){ // TODO: add member initialization code here}CMainFrame::~CMainFrame(){}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){ if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators( indicators, sizeof(indicators) / sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; } return 0;}BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs){ return CFrameWnd::PreCreateWindow(cs);}void CMainFrame::SetStatusText(CString s){ m_wndStatusBar.SetPaneText(0, s);}IMPLEMENT_DYNCREATE(CPrefConnectionPage, CPropertyPage)CPrefConnectionPage::CPrefConnectionPage() : CPropertyPage(CPrefConnectionPage::IDD){ //{{AFX_DATA_INIT(CPrefConnectionPage) m_Host = _T(""); //}}AFX_DATA_INIT}CPrefConnectionPage::~CPrefConnectionPage(){}void CPrefConnectionPage::DoDataExchange(CDataExchange* pDX){ CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPrefConnectionPage) DDX_Control(pDX, IDC_HOSTS_LIST, m_ctlHostList); DDX_Text(pDX, IDC_HOST, m_Host); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CPrefConnectionPage, CPropertyPage) //{{AFX_MSG_MAP(CPrefConnectionPage) ON_BN_CLICKED(IDC_ADDHOST, OnAddhost) ON_BN_CLICKED(IDC_REMOVEHOST, OnRemovehost) //}}AFX_MSG_MAPEND_MESSAGE_MAP()BOOL CPrefConnectionPage::OnInitDialog() { CPropertyPage::OnInitDialog(); DWORD style = m_ctlHostList.GetExtendedStyle() | LVS_EX_FULLROWSELECT; m_ctlHostList.SetExtendedStyle(style); m_ctlHostList.InsertColumn(0, "Host", LVCFMT_LEFT, 167); m_ctlHostList.InsertColumn(1, "Address", LVCFMT_LEFT, 100); m_ctlHostList.InsertColumn(2, "Port", LVCFMT_LEFT, 50); return TRUE;}void CPrefConnectionPage::OnAddhost() { UpdateData(TRUE); if(m_Host == "") return; CWaitCursor c; Trut::Host *host = g_app.trut->AddHost(m_Host); if(!host) { MessageBox("Cannot connect to " + m_Host + "."); return; } AddListItem(host);}void CPrefConnectionPage::OnRemovehost() { POSITION pos = m_ctlHostList.GetFirstSelectedItemPosition(); if(!pos) return; UINT nIndex = m_ctlHostList.GetNextSelectedItem(pos); Trut::Host *host = (Trut::Host *)m_ctlHostList.GetItemData(nIndex); if (!host) return; g_app.trut->RemoveHost(host); m_ctlHostList.DeleteItem(nIndex);}void CPrefConnectionPage::AddListItem(Trut::Host *host){ if(!host) return; CString ip; g_app.FormatIPAddr(host->GetIp(), ip); CString port; port.Format("%d", host->GetPort()); UINT item = m_ctlHostList.InsertItem( LVIF_TEXT | LVIF_PARAM, m_ctlHostList.GetItemCount(), host->GetUrl(), 0, 0, 0, (LPARAM)host); m_ctlHostList.SetItem(item, 1, LVIF_TEXT, ip, 0, 0, 0, 0); m_ctlHostList.SetItem(item, 2, LVIF_TEXT, port, 0, 0, 0, 0);}BOOL CPrefConnectionPage::OnSetActive(){ m_ctlHostList.DeleteAllItems(); CTrutellaApp::HostList::iterator i = g_app.hosts.begin(); for (i = g_app.hosts.begin(); i != g_app.hosts.end(); i++) { Trut::Host *host = *i; AddListItem(host); } return CPropertyPage::OnSetActive();}IMPLEMENT_DYNCREATE(CPrefGroups, CPropertyPage)CPrefGroups::CPrefGroups() : CPropertyPage(CPrefGroups::IDD){ //{{AFX_DATA_INIT(CPrefGroups) m_GroupName = _T(""); //}}AFX_DATA_INIT}CPrefGroups::~CPrefGroups(){}void CPrefGroups::DoDataExchange(CDataExchange* pDX){ CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPrefGroups) DDX_Control(pDX, IDC_GROUPLIST, m_ctlGroupList); DDX_Text(pDX, IDC_GROUPNAME, m_GroupName); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CPrefGroups, CPropertyPage) //{{AFX_MSG_MAP(CPrefGroups) ON_BN_CLICKED(IDC_LEAVEGROUP, OnLeavegroup) ON_BN_CLICKED(IDC_JOINGROUP, OnJoingroup) //}}AFX_MSG_MAPEND_MESSAGE_MAP()BOOL CPrefGroups::OnInitDialog() { CPropertyPage::OnInitDialog(); DWORD style = m_ctlGroupList.GetExtendedStyle() | LVS_EX_FULLROWSELECT; m_ctlGroupList.SetExtendedStyle(style); m_ctlGroupList.InsertColumn(0, "Name", LVCFMT_LEFT, 317); return TRUE;}struct JoinContext{ CPrefGroups *pref; CEvent event;};void CPrefGroups::OnJoingroup() { UpdateData(TRUE); if(m_GroupName == "") return; CWaitCursor c; JoinContext context; context.pref = this; g_app.trut->JoinGroup(m_GroupName, 2, JoinCallback, AcceptCallback, &context); CSingleLock lock(&context.event); lock.Lock();}void CPrefGroups::JoinCallback(Trut::Group *group, Trut::JoinStatus status, void *context){ JoinContext *joinctx = (JoinContext*) context; if (!joinctx) return; joinctx->event.SetEvent(); CPrefGroups *pref = joinctx->pref; switch(status) { case Trut::JOIN_CREATED: pref->MessageBox("No active group members could be found, " "so a new group has been created."); g_app.groups.push_back(group); pref->m_ctlGroupList.InsertItem( LVIF_TEXT | LVIF_PARAM, pref->m_ctlGroupList.GetItemCount(), group->GetName(), 0, 0, 0, (LPARAM) group); break; case Trut::JOIN_OK: g_app.groups.push_back(group); pref->m_ctlGroupList.InsertItem( LVIF_TEXT | LVIF_PARAM, pref->m_ctlGroupList.GetItemCount(), group->GetName(), 0, 0, 0, (LPARAM) group); break; default: pref->MessageBox("You were denied access to the group."); break; }}int CPrefGroups::AcceptCallback(Trut::Group *group, const char *id, void *context){ HWND hwnd = g_app.m_pMainWnd ? g_app.m_pMainWnd->m_hWnd:NULL; return (::MessageBox(hwnd, (CString) id + " is trying to join the group " + (CString)group->GetName() + ". Will you allow this person to " "join your group?", "Trutella", MB_YESNO) == IDYES);}void CPrefGroups::OnLeavegroup() { POSITION pos = m_ctlGroupList.GetFirstSelectedItemPosition(); if(!pos) return; UINT item = m_ctlGroupList.GetNextSelectedItem(pos); Trut::Group *group = (Trut::Group*) m_ctlGroupList.GetItemData(item); if (!group) return; m_ctlGroupList.DeleteItem(item); CTrutellaApp::ShareList::iterator i = g_app.shares.begin(); while (i != g_app.shares.end()) { Trut::Shared *shared = *i; i++; if (shared && shared->GetGroup() == group) g_app.shares.remove(shared); } g_app.groups.remove(group); g_app.trut->LeaveGroup(group);}BOOL CPrefGroups::OnSetActive() { m_ctlGroupList.DeleteAllItems(); CTrutellaApp::GroupList::iterator i; for (i = g_app.groups.begin(); i != g_app.groups.end(); i++) { Trut::Group *group = *i; if (group) m_ctlGroupList.InsertItem( LVIF_TEXT | LVIF_PARAM, m_ctlGroupList.GetItemCount(), group->GetName(), 0, 0, 0, (LPARAM)group); } return CPropertyPage::OnSetActive();}IMPLEMENT_DYNCREATE(CPrefSharing, CPropertyPage)CPrefSharing::CPrefSharing() : CPropertyPage(CPrefSharing::IDD){ //{{AFX_DATA_INIT(CPrefSharing) m_ext = _T(""); m_groupItem = -1; //}}AFX_DATA_INIT m_current = NULL;}CPrefSharing::~CPrefSharing(){}void CPrefSharing::DoDataExchange(CDataExchange* pDX){ CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPrefSharing) DDX_Control(pDX, IDC_RESCANSHARE, m_ctlRescanBtn); DDX_Control(pDX, IDC_REMOVESHARE, m_ctlRemoveBtn); DDX_Control(pDX, IDC_ADDSHARE, m_ctlAddBtn); DDX_Control(pDX, IDC_SHAREEXT, m_ctlExtensions); DDX_Control(pDX, IDC_SHAREGROUP, m_ctlGroup); DDX_Control(pDX, IDC_SHARELIST, m_ctlShareList); DDX_Text(pDX, IDC_SHAREEXT, m_ext); DDX_CBIndex(pDX, IDC_SHAREGROUP, m_groupItem); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CPrefSharing, CPropertyPage) //{{AFX_MSG_MAP(CPrefSharing) ON_BN_CLICKED(IDC_ADDSHARE, OnAddshare) ON_BN_CLICKED(IDC_REMOVESHARE, OnRemoveshare) ON_WM_DESTROY() ON_NOTIFY(NM_CLICK, IDC_SHARELIST, OnClickSharelist) ON_BN_CLICKED(IDC_RESCANSHARE, OnRescanshare) //}}AFX_MSG_MAPEND_MESSAGE_MAP()BOOL CPrefSharing::OnInitDialog() { CPropertyPage::OnInitDialog(); DWORD style = m_ctlShareList.GetExtendedStyle() | LVS_EX_FULLROWSELECT; m_ctlShareList.SetExtendedStyle(style); m_ctlShareList.InsertColumn(0, "Path", LVCFMT_LEFT, 317); return TRUE;}void CPrefSharing::OnClickSharelist(NMHDR* pNMHDR, LRESULT* pResult){ SaveCurrent(); POSITION pos = m_ctlShareList.GetFirstSelectedItemPosition(); if (!pos) return; UINT selected = m_ctlShareList.GetNextSelectedItem(pos); Trut::Shared *shared = (Trut::Shared*) m_ctlShareList.GetItemData(selected); if (!shared) return; m_current = shared; m_ext = shared->GetExt(); m_groupItem = m_ctlGroup.GetCount() - 1; UpdateData(FALSE); if (shared->GetGroup()) m_ctlGroup.SelectString(-1, shared->GetGroup()->GetName()); m_ctlGroup.EnableWindow(); m_ctlExtensions.EnableWindow(); m_ctlAddBtn.EnableWindow(); m_ctlRescanBtn.EnableWindow(); m_ctlRemoveBtn.EnableWindow(); *pResult = 0;}void CPrefSharing::OnAddshare() { SaveCurrent(); char *path = GetPath(); if (!path) return; CString ext; ext.LoadString(IDS_SHAREEXT_DEFAULTS); Trut::Shared *shared = g_app.trut->AddShared(path, ext, NULL); if (!shared) return; g_app.shares.push_back(shared); m_ctlShareList.InsertItem(LVIF_TEXT | LVIF_PARAM | LVIF_STATE, m_ctlShareList.GetItemCount(), shared->GetPath(), LVIS_SELECTED, LVIS_SELECTED, 0, (LPARAM) shared); m_current = shared; m_ext = shared->GetExt(); m_groupItem = m_ctlGroup.GetCount() - 1; UpdateData(FALSE); m_ctlGroup.EnableWindow(); m_ctlExtensions.EnableWindow(); m_ctlRemoveBtn.EnableWindow();}void CPrefSharing::OnRemoveshare(){ SaveCurrent(); POSITION pos = m_ctlShareList.GetFirstSelectedItemPosition(); if (!pos) return; UINT item = m_ctlShareList.GetNextSelectedItem(pos); Trut::Shared *shared = (Trut::Shared*) m_ctlShareList.GetItemData(item); if (!shared) return; m_ctlShareList.DeleteItem(item); g_app.shares.remove(shared); g_app.trut->RemoveShared(shared); if (m_ctlShareList.GetItemCount() <= 0) { m_ctlGroup.EnableWindow(FALSE); m_ctlExtensions.EnableWindow(FALSE); m_ctlRemoveBtn.EnableWindow(FALSE); }}void CPrefSharing::OnDestroy() { g_app.trut->RescanAllShared(); CPropertyPage::OnDestroy();}void CPrefSharing::OnRescanshare() { SaveCurrent(); g_app.trut->RescanAllShared();}BOOL CPrefSharing::OnSetActive() { m_ctlShareList.DeleteAllItems(); CTrutellaApp::ShareList::iterator i; for(i = g_app.shares.begin(); i != g_app.shares.end(); i++) { Trut::Shared *shared = *i; if (shared) m_ctlShareList.InsertItem(LVIF_TEXT | LVIF_PARAM, m_ctlShareList.GetItemCount(), shared->GetPath(), 0, 0, 0, (LPARAM) shared); } m_ctlGroup.ResetContent(); m_ctlGroup.AddString("All of GnutellaNet"); m_ctlGroup.SetItemDataPtr(0, NULL); CTrutellaApp::GroupList::iterator j; for(j = g_app.groups.begin(); j != g_app.groups.end(); j++) { Trut::Group *group = *j; int item = m_ctlGroup.AddString(group->GetName()); m_ctlGroup.SetItemDataPtr(item, group); } m_ext = _T(""); m_groupItem = -1; UpdateData(FALSE); m_ctlGroup.EnableWindow(FALSE); m_ctlExtensions.EnableWindow(FALSE); m_ctlRemoveBtn.EnableWindow(FALSE); return CPropertyPage::OnSetActive();}BOOL CPrefSharing::OnKillActive() { SaveCurrent(); return CPropertyPage::OnKillActive();}char *CPrefSharing::GetPath(){ BROWSEINFO bi; bi.hwndOwner = m_hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = NULL; bi.lpszTitle = _T("Select a Directory to Share"); bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; LPITEMIDLIST pidl = ::SHBrowseForFolder(&bi); char path[MAX_PATH]; if (!pidl || !::SHGetPathFromIDList(pidl, path)) return NULL; int size = strlen(path); if(path[size - 1] == '\\') path[size - 1] = '\0'; return strdup(path);}void CPrefSharing::SaveCurrent(){ Trut::Shared *shared = m_current; if (!shared) return; m_current = NULL; char ext[128]; ext[0] = '\0'; int len = m_ctlExtensions.GetLine(0, ext, sizeof(ext)); if (len >= 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -