This repository has been archived on 2024-03-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
ContactBook/ComboBar.cpp
2024-03-20 09:28:18 -05:00

185 lines
4.5 KiB
C++

// combobar.cpp : implementation of the CComboToolBar and CTBComboBox class
//
#include "stdafx.h"
#include "CB32.h"
#include "ComboBar.h"
#include "MainFrm.h"
#include "CB32Doc.h"
#include "CB32View.h"
#include "FindDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CComboToolBar, CToolBarEx)
//{{AFX_MSG_MAP(CComboToolBar)
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
ON_CBN_SETFOCUS(IDW_COMBO, OnSetFocus)
END_MESSAGE_MAP()
void CComboToolBar::OnSetFocus()
{
// combobox is gaining focus, highlight text in edit control
m_toolBarCombo.SetEditSel(0,-1);
}
BOOL CComboToolBar::PreTranslateMessage(MSG* pMsg)
{
// Note: there is only one control in the toolbar that accepts keyboard input,
// this is the combobox.
// user hit ENTER
if (pMsg->wParam == VK_RETURN && GetKeyState(VK_RETURN) < 0)
{
// extract the text, update combobox lists, and do the search
CString s1;
m_toolBarCombo.GetWindowText(s1);
if(s1.GetLength())
{
CCB32View* pView = GetApplicationView();
m_toolBarCombo.SetEditSel(0, -1);
pView->m_searchHistory.AddString(s1); // update combobox history list
pView->m_pFindDialog->m_szText = s1; // update last string
pView->OnViewFindNext(s1); // find the string
return TRUE; // key processed
}
else
{
MessageBeep(MB_ICONEXCLAMATION);
return TRUE; // key processed
}
}
// user hit ESC
if (pMsg->wParam == VK_ESCAPE && GetKeyState(VK_ESCAPE) < 0)
{
GetApplicationView()->SetFocus();
return TRUE; // key processed
}
// user hit DELETE
if (pMsg->wParam == VK_DELETE && GetKeyState(VK_DELETE) < 0)
{
m_toolBarCombo.Clear();
return TRUE; // key processed
}
// user hit DOWN-ARROW
if (pMsg->wParam == VK_DOWN && GetKeyState(VK_DOWN) < 0 &&
m_toolBarCombo.GetDroppedState() == FALSE)
{
m_toolBarCombo.ShowDropDown();
return TRUE; // key processed
}
return CToolBarEx::PreTranslateMessage(pMsg);
}
BEGIN_MESSAGE_MAP(CTBComboBox, CComboBox)
//{{AFX_MSG_MAP(CTBComboBox)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
int CTBComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CComboBox::OnCreate(lpCreateStruct) == -1)
return -1;
LOGFONT logFont;
TEXTMETRIC tm;
CClientDC dc(this);
dc.GetTextMetrics(&tm);
::EnumFontFamilies(dc.m_hDC, _T("MS Sans Serif"),
CTBComboBox::FontEnumProc, (LPARAM)&logFont);
// change the weight and create the font
logFont.lfWeight = FW_BOLD;
if (m_font.CreateFontIndirect(&logFont))
SetFont(&m_font);
return 0;
}
int CALLBACK CTBComboBox::FontEnumProc(const LOGFONT *pLogFont, const TEXTMETRIC *pTextMetric, DWORD type, LPARAM pDestLogFont)
{
memcpy((BYTE*)pDestLogFont, pLogFont, sizeof(LOGFONT));
return FALSE; // return the first font in the family(i.e. smallest).
}
void CComboToolBar::OnLButtonUp(UINT nFlags, CPoint point)
{
CRect rect;
CMenu menu;
int nHit = -1;
TBBUTTON button;
CMenu* pSubMenu = NULL;
CToolBarCtrl& pBar = GetToolBarCtrl();
int nButtons = pBar.GetButtonCount();
for (int i = 0; i < nButtons; i++)
{
if (pBar.GetItemRect(i, &rect) && rect.PtInRect(point) &&
pBar.GetButton(i, &button) && !(button.fsStyle & TBSTYLE_SEP))
{
nHit = GetItemID(i);
break;
}
}
switch (nHit)
{
case ID_TBAR_DIAL:
if (menu.LoadMenu(IDR_TRAYMENU))
pSubMenu = menu.GetSubMenu(1);
if (pSubMenu)
ClientToScreen(&rect);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,
rect.left, rect.bottom, AfxGetMainWnd(), NULL);
break;
case ID_TBAR_EMAIL:
if (menu.LoadMenu(IDR_TRAYMENU))
pSubMenu = menu.GetSubMenu(2);
if (pSubMenu)
ClientToScreen(&rect);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,
rect.left, rect.bottom, AfxGetMainWnd(), NULL);
break;
case ID_TBAR_WEB:
if (menu.LoadMenu(IDR_TRAYMENU))
pSubMenu = menu.GetSubMenu(3);
if (pSubMenu)
ClientToScreen(&rect);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,
rect.left, rect.bottom, AfxGetMainWnd(), NULL);
break;
}
CToolBarEx::OnLButtonUp(nFlags, point);
}
int CComboToolBar::CheckIfHit(CPoint point)
{
CToolBarCtrl& pBar = GetToolBarCtrl();
int nButtons = pBar.GetButtonCount();
for (int i = 0; i < nButtons; i++)
{
CRect rect;
TBBUTTON button;
if (pBar.GetItemRect(i, &rect) && rect.PtInRect(point) &&
pBar.GetButton(i, &button) && !(button.fsStyle & TBSTYLE_SEP))
{
int nHit = GetItemID(i);
return nHit != 0 ? nHit : -1;
}
}
return -1;
}