// CommandBarsSampleView.cpp // // (c)1998-2025 Codejock Software, All Rights Reserved. // // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN // CONSENT OF CODEJOCK SOFTWARE. // // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A // SINGLE COMPUTER. // // CONTACT INFORMATION: // support@codejock.com // http://www.codejock.com // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CommandBarsSample.h" #include "MainFrm.h" #include "CommandBarsSampleDoc.h" #include "CntrItem.h" #include "RTF\ScrollRichEditViewEx.h" #include "CommandBarsSampleView.h" #include "WorkspaceView.h" #include "ControlSelector.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // ///////////////////////////////////////////////////////////////////////////// // CCommandBarsSampleView IMPLEMENT_DYNCREATE(CCommandBarsSampleView, CScrollRichEditViewEx) BEGIN_MESSAGE_MAP(CCommandBarsSampleView, CScrollRichEditViewEx) //{{AFX_MSG_MAP(CCommandBarsSampleView) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! ON_WM_DESTROY() ON_WM_RBUTTONUP() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview) ON_COMMAND(ID_VIEW_GRID, OnEmptyCommand) ON_COMMAND(ID_VIEW_RULER, OnEmptyCommand) ON_COMMAND(ID_VIEW_LINENUMBER, OnEmptyCommand) ON_COMMAND(ID_VIEW_WORDWRAP, OnEmptyCommand) ON_XTP_EXECUTE(ID_EDIT_UNDO, OnEditUndo) ON_NOTIFY(XTP_LBN_SELCHANGE, ID_EDIT_UNDO, OnListBoxControlSelChange) ON_NOTIFY(XTP_LBN_POPUP, ID_EDIT_UNDO, OnListBoxControlPoup) ON_NOTIFY(XTP_SLN_SELCHANGE, ID_INSERT_TABLE, OnTableSelChange) ON_XTP_EXECUTE(ID_INSERT_TABLE, OnInsertTable) ON_COMMAND(ID_FONT_CLEAR, OnEmptyCommand) ON_UPDATE_COMMAND_UI(ID_INSERT_TABLE, OnEnableCommand) ON_NOTIFY(XTP_SBN_SCROLL, ID_INDICATOR_ZOOMSLIDER, OnZoomSliderScroll) ON_COMMAND(ID_VIEW_ZOOM_IN, OnZoomIn) ON_COMMAND(ID_VIEW_ZOOM_OUT, OnZoomOut) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCommandBarsSampleView construction/destruction CCommandBarsSampleView::CCommandBarsSampleView() { m_nTextHeight = 0; m_nTotalHeight = 0; m_nScrollPos = 0; m_bInScrollChanged = FALSE; m_clrText = COLORREF_NULL; m_clrBack = COLORREF_NULL; m_nZoom = 100; } CCommandBarsSampleView::~CCommandBarsSampleView() { } BOOL CCommandBarsSampleView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.dwExStyle &= ~WS_EX_CLIENTEDGE; return CScrollRichEditViewEx::PreCreateWindow(cs); } void CCommandBarsSampleView::OnInitialUpdate() { CScrollRichEditViewEx::OnInitialUpdate(); USES_CONVERSION; // Set the printing margins (720 twips = 1/2 inch). SetMargins(CRect(720, 720, 720, 720)); if (GetDocument()->GetPathName().IsEmpty()) SetSampleDescriptionText(GetRichEditCtrl(), _T("")); ModifyStyleEx(WS_EX_CLIENTEDGE, 0); ModifyStyle(0, WS_BORDER); static BOOL bLoadOnce = FALSE; if (!bLoadOnce && GetDocument()->GetPathName().IsEmpty()) { bLoadOnce = TRUE; HINSTANCE hInstance = AfxGetInstanceHandle(); LPCTSTR lpszResourceName = _T("FEEDBACK.RTF"); LPCTSTR lpszResourceType = _T("RTF"); HRSRC hRsrc = ::FindResource(hInstance, lpszResourceName, lpszResourceType); if (hRsrc == NULL) return; HGLOBAL hGlobal = LoadResource(hInstance, hRsrc); if (hGlobal == NULL) return; LPCSTR pData = (LPCSTR)LockResource(hGlobal); if (pData == NULL) return; DWORD dwSize = (DWORD)SizeofResource(hInstance, hRsrc); if (dwSize == 0) return; CMemFile memRTF((BYTE*)pData, dwSize, 0); CArchive ar(&memRTF, CArchive::load | CArchive::bNoFlushOnDelete | CArchive::bNoByteSwap); Serialize(ar); GetDocument()->SetModifiedFlag(FALSE); } } ///////////////////////////////////////////////////////////////////////////// // CCommandBarsSampleView printing BOOL CCommandBarsSampleView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CCommandBarsSampleView::OnDestroy() { // Deactivate the item on destruction; this is important // when a splitter view is being used. CScrollRichEditViewEx::OnDestroy(); } ///////////////////////////////////////////////////////////////////////////// // CCommandBarsSampleView diagnostics #ifdef _DEBUG void CCommandBarsSampleView::AssertValid() const { CScrollRichEditViewEx::AssertValid(); } void CCommandBarsSampleView::Dump(CDumpContext& dc) const { CScrollRichEditViewEx::Dump(dc); } CCommandBarsSampleDoc* CCommandBarsSampleView::GetDocument() // non-debug version is inline { _ASSERTE(m_pDocument->IsKindOf(RUNTIME_CLASS(CCommandBarsSampleDoc))); return (CCommandBarsSampleDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CCommandBarsSampleView message handlers void CCommandBarsSampleView::OnFilePrintPreview() { // In derived classes, implement special window handling here // Be sure to Unhook Frame Window close if hooked. // must not create this on the frame. Must outlive this function CPrintPreviewState* pState = new CPrintPreviewState; // DoPrintPreview's return value does not necessarily indicate that // Print preview succeeded or failed, but rather what actions are necessary // at this point. If DoPrintPreview returns TRUE, it means that // OnEndPrintPreview will be (or has already been) called and the // pState structure will be/has been deleted. // If DoPrintPreview returns FALSE, it means that OnEndPrintPreview // WILL NOT be called and that cleanup, including deleting pState // must be done here. if (!DoPrintPreview(XTP_IDD_PREVIEW_DIALOGBAR, this, RUNTIME_CLASS(CXTPPreviewView), pState)) { // In derived classes, reverse special window handling here for // Preview failure case TRACE0("Error: DoPrintPreview failed.\n"); AfxMessageBox(AFX_IDP_COMMAND_FAILURE); delete pState; // preview failed to initialize, delete State now } } void CCommandBarsSampleView::OnEditUndo(NMHDR* pNMHDR, LRESULT* pResult) { if (((NMXTPCONTROL*)pNMHDR)->pControl->GetType() == xtpControlSplitButtonPopup) { AfxMessageBox(_T("Undo last command")); *pResult = 1; // Handled; } CXTPControlListBox* pControlListBox = DYNAMIC_DOWNCAST(CXTPControlListBox, ((NMXTPCONTROL*)pNMHDR)->pControl); if (pControlListBox) { CString str; str.Format(_T("Undo last %i actions"), pControlListBox->GetListCtrl()->GetSelCount()); AfxMessageBox(str); *pResult = 1; // Handled; } } void CCommandBarsSampleView::OnListBoxControlSelChange(NMHDR* pNMHDR, LRESULT* pRes) { _ASSERTE(pNMHDR != NULL); _ASSERTE(pRes != NULL); CXTPControlListBox* pControlListBox = DYNAMIC_DOWNCAST(CXTPControlListBox, ((NMXTPCONTROL*)pNMHDR)->pControl); if (pControlListBox) { CXTPControlStatic* pInfo = FindInfoControl(pControlListBox); if (pInfo) { CString str; str.Format(_T("Undo %i Actions"), pControlListBox->GetListCtrl()->GetSelCount()); pInfo->SetCaption(str); pInfo->DelayRedrawParent(); } *pRes = 1; } } CXTPControlStatic* CCommandBarsSampleView::FindInfoControl(CXTPControl* pControl) { CXTPCommandBar* pCommandBar = pControl->GetParent(); for (int i = 0; i < pCommandBar->GetControls()->GetCount(); i++) { CXTPControlStatic* pControlStatic = DYNAMIC_DOWNCAST(CXTPControlStatic, pCommandBar->GetControl(i)); if (pControlStatic && pControlStatic->GetID() == pControl->GetID()) { return pControlStatic; } } return NULL; } void CCommandBarsSampleView::OnListBoxControlPoup(NMHDR* pNMHDR, LRESULT* pRes) { _ASSERTE(pNMHDR != NULL); _ASSERTE(pRes != NULL); CXTPControlListBox* pControlListBox = DYNAMIC_DOWNCAST(CXTPControlListBox, ((NMXTPCONTROL*)pNMHDR)->pControl); if (pControlListBox) { CListBox* pListBox = pControlListBox->GetListCtrl(); pListBox->ResetContent(); int nCount = RAND_S() % 20 + 3; for (int i = 0; i < nCount; i++) { CString str; str.Format(_T("Undo String %i"), i + 1); pListBox->AddString(str); } CXTPControlStatic* pInfo = FindInfoControl(pControlListBox); if (pInfo) { CString str; pInfo->SetCaption(_T("Undo 0 Actions")); pInfo->DelayRedrawParent(); } *pRes = 1; } } CRect CCommandBarsSampleView::GetParentViewRect() { return ((CWorkspaceView*)GetParent())->GetViewRect(CXTPClientRect(GetParent())); } CScrollBar* CCommandBarsSampleView::GetParentScrollbar(int nBar) const { if (nBar != SB_VERT) return NULL; if (!AfxGetMainWnd()) return NULL; return &((CWorkspaceView*)GetParent())->m_wndScrollBar[nBar]; } COLORREF CCommandBarsSampleView::GetFrameColor() { #ifdef _XTP_INCLUDE_DOCKINGPANE return theApp.m_csPane.m_clrBorderDark; #else return GetXtremeColor(COLOR_ACTIVEBORDER); #endif } void CCommandBarsSampleView::OnRButtonUp(UINT /*nFlags*/, CPoint point) { ClientToScreen(&point); CXTPCommandBars* pCommandBars = ((CMainFrame*)AfxGetMainWnd())->GetCommandBars(); CXTPMiniToolBar* pMiniToolBar = DYNAMIC_DOWNCAST( CXTPMiniToolBar, pCommandBars->GetContextMenus()->FindCommandBar(IDR_MENU_MINITOOLBAR)); if (!pMiniToolBar) return; CXTPPopupBar* pPopupBar = (CXTPPopupBar*)pCommandBars->GetContextMenus()->FindCommandBar( IDR_MENU_CONTEXT); pMiniToolBar->TrackPopupMenu(pPopupBar, 0, point.x, point.y); } void CCommandBarsSampleView::OnTableSelChange(NMHDR* pNMHDR, LRESULT* pRes) { _ASSERTE(pNMHDR != NULL); _ASSERTE(pRes != NULL); CControlTable* pControlTable = DYNAMIC_DOWNCAST(CControlTable, ((NMXTPCONTROL*)pNMHDR)->pControl); if (pControlTable) { CXTPControlStatic* pInfo = FindInfoControl((CXTPControl*)pControlTable); if (pInfo) { CSize szActive = pControlTable->m_szItemsActive; CString str = _T("Cancel"); if (szActive != CSize(0)) { str.Format(_T("%i x %i Table"), szActive.cx, szActive.cy); } pInfo->SetWidth(pControlTable->m_szItem.cx * pControlTable->m_szItemsVisible.cx); pInfo->SetCaption(str); pInfo->DelayRedrawParent(); } *pRes = 1; } } void CCommandBarsSampleView::OnEnableCommand(CCmdUI* pCmdUI) { pCmdUI->Enable(); } void CCommandBarsSampleView::OnInsertTable(NMHDR* pNMHDR, LRESULT* pResult) { CControlTable* pControlTable = DYNAMIC_DOWNCAST(CControlTable, ((NMXTPCONTROL*)pNMHDR)->pControl); if (pControlTable) { CSize szActive = pControlTable->m_szResult; CString str; str.Format(_T("<%i x %i Table>"), szActive.cx, szActive.cy); MessageBox(str); *pResult = 1; // Handled; } } // Convert 0 -> 10%, 500 -> 100 %, 1000 -> 500 % int PosToZoom(int nPos) { return nPos <= 500 ? 10 + MulDiv(nPos, 90, 500) : 100 + MulDiv(nPos - 500, 400, 500); } int ZoomToPos(int nZoom) { return nZoom < 100 ? MulDiv(nZoom - 10, 500, 90) : 500 + MulDiv(nZoom - 100, 500, 400); } #ifndef EM_SETZOOM # define EM_SETZOOM (WM_USER + 225) #endif void CCommandBarsSampleView::OnZoomSliderScroll(NMHDR* pNMHDR, LRESULT* pResult) { NMXTPSCROLL* pNMScroll = (NMXTPSCROLL*)pNMHDR; CXTPStatusBarSliderPane* pPane = DYNAMIC_DOWNCAST(CXTPStatusBarSliderPane, pNMScroll->pSender); if (!pPane) return; int nZoom = m_nZoom; switch (pNMScroll->nSBCode) { case SB_TOP: nZoom = 10; break; case SB_BOTTOM: nZoom = 500; break; case SB_LINEUP: nZoom = max(((nZoom / 10) - 1) * 10, 10); break; case SB_LINEDOWN: nZoom = min(((nZoom / 10) + 1) * 10, 500); break; case SB_THUMBTRACK: nZoom = PosToZoom(pNMScroll->nPos); if (nZoom > 90 && nZoom < 150) nZoom = 100; break; case SB_PAGEUP: nZoom = max(nZoom - 10, 10); break; case SB_PAGEDOWN: nZoom = min(nZoom + 10, 500); break; } if (nZoom == m_nZoom) return; m_nZoom = nZoom; pPane->SetPos(ZoomToPos(nZoom)); CXTPStatusBar* pStatusBar = pPane->GetStatusBar(); CXTPStatusBarPane* pPaneZoomIndicator = pStatusBar->FindPane(ID_INDICATOR_ZOOM); CString strZoom; strZoom.Format(_T("%i%%"), m_nZoom); pPaneZoomIndicator->SetText(strZoom); SendMessage(EM_SETZOOM, m_nZoom, 100); } void CCommandBarsSampleView::OnZoomIn() { int nZoom = m_nZoom; nZoom = min(((nZoom / 10) + 1) * 10, 500); if (nZoom == m_nZoom) return; m_nZoom = nZoom; SendMessage(EM_SETZOOM, m_nZoom, 100); } void CCommandBarsSampleView::OnZoomOut() { int nZoom = m_nZoom; nZoom = max(((nZoom / 10) - 1) * 10, 10); if (nZoom == m_nZoom) return; m_nZoom = nZoom; SendMessage(EM_SETZOOM, m_nZoom, 100); }