home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / interface / StatusCtrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-01  |  10.5 KB  |  489 lines

  1. // StatusCtrl.cpp: Implementierungsdatei
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "StatusCtrl.h"
  6. #include "EnterSomething.h"
  7. #include "MainFrm.h"
  8.  
  9. #if defined(_DEBUG) && !defined(MMGR)
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. const COLORREF CStatusCtrl::m_ColTable[16] = {RGB(255, 255, 255),
  16.                                         RGB(0, 0, 0),
  17.                                         RGB(0, 0, 128),
  18.                                         RGB(0, 128, 0),
  19.                                         RGB(255, 0, 0),
  20.                                         RGB(128, 0, 0),
  21.                                         RGB(128, 0, 128),
  22.                                         RGB(128, 128, 0),
  23.                                         RGB(255, 255, 0),
  24.                                         RGB(0, 255, 0),
  25.                                         RGB(0, 128, 128),
  26.                                         RGB(0, 255, 255),
  27.                                         RGB(0, 0, 255),
  28.                                         RGB(255, 0, 255),
  29.                                         RGB(128, 128, 128),
  30.                                         RGB(192, 192, 192)
  31.                                         };
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CStatusCtrl
  35.  
  36. CStatusCtrl::CStatusCtrl()
  37. {
  38.     m_doPopupCursor = FALSE;
  39.     m_bEmpty = TRUE;
  40.     m_nMoveToBottom = 0;
  41.     m_nTimerID = 0;
  42.     m_headerPos = 0;
  43.     m_runTimer = 0;
  44. }
  45.  
  46. CStatusCtrl::~CStatusCtrl()
  47. {
  48. }
  49.  
  50.  
  51. BEGIN_MESSAGE_MAP(CStatusCtrl, CRichEditCtrl)
  52.     //{{AFX_MSG_MAP(CStatusCtrl)
  53.     ON_WM_ERASEBKGND()
  54.     ON_WM_CONTEXTMENU()
  55.     ON_WM_SETCURSOR()
  56.     ON_COMMAND(ID_OUTPUTCONTEXT_CLEARALL, OnOutputcontextClearall)
  57.     ON_COMMAND(ID_OUTPUTCONTEXT_COPYTOCLIPBOARD, OnOutputcontextCopytoclipboard)
  58.     ON_WM_CREATE()
  59.     ON_WM_TIMER()
  60.     ON_WM_RBUTTONUP()
  61.     ON_WM_MOUSEWHEEL()
  62.     //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Behandlungsroutinen fⁿr Nachrichten CStatusCtrl 
  67.  
  68. BOOL CStatusCtrl::OnEraseBkgnd(CDC* pDC) 
  69. {
  70.     return TRUE;
  71. }
  72.  
  73. void CStatusCtrl::OnContextMenu(CWnd* pWnd, CPoint point) 
  74. {
  75.     ClientToScreen(&point);
  76.  
  77.     CMenu menu;
  78.     menu.LoadMenu(IDR_OUTPUTCONTEXT);
  79.  
  80.     CMenu* pPopup = menu.GetSubMenu(0);
  81.     ASSERT(pPopup != NULL);
  82.     CWnd* pWndPopupOwner = this;
  83.     //while (pWndPopupOwner->GetStyle() & WS_CHILD)
  84.     //    pWndPopupOwner = pWndPopupOwner->GetParent();
  85.  
  86.     if (!GetLineCount())
  87.     {
  88.         pPopup->EnableMenuItem(ID_OUTPUTCONTEXT_COPYTOCLIPBOARD,MF_GRAYED);
  89.         pPopup->EnableMenuItem(ID_OUTPUTCONTEXT_CLEARALL,MF_GRAYED);
  90.     }
  91.     HCURSOR    hCursor;
  92.     hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  93.     m_doPopupCursor = TRUE;
  94.     SetCursor(hCursor);
  95.         
  96.     pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
  97.         pWndPopupOwner);
  98. }
  99.  
  100. BOOL CStatusCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  101. {
  102.     if (!m_doPopupCursor)
  103.     {
  104.         m_doPopupCursor = 0;
  105.         return CWnd::OnSetCursor(pWnd, nHitTest, message );
  106.     }
  107.     else
  108.         m_doPopupCursor = 0;
  109.     return 0;
  110. }
  111.  
  112. DWORD __stdcall CStatusCtrl::RichEditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
  113. {
  114.     CStatusCtrl *pThis = (CStatusCtrl *)dwCookie;
  115.     if (pThis->m_headerPos != -1)
  116.     {
  117.         int len = pThis->m_RTFHeader.GetLength() - pThis->m_headerPos;
  118.         if (len > cb)
  119.         {
  120.             pThis->m_headerPos = cb;
  121.             len = cb;
  122.         }
  123.         else
  124.             pThis->m_headerPos = -1;
  125.  
  126.         memcpy(pbBuff, (LPCTSTR)pThis->m_RTFHeader, len);
  127.         *pcb = len;
  128.     }
  129.     else
  130.     {
  131.         *pcb = 0;
  132.         if (pThis->m_statusBuffer.empty())
  133.             return 0;
  134.         t_buffer &buffer = pThis->m_statusBuffer.front();
  135.         if (buffer.status)
  136.         {
  137.             if (buffer.pos == -1)
  138.             {
  139.                 if (pThis->m_bEmpty)
  140.                 {
  141.                     pThis->m_bEmpty = false;
  142.                     memcpy(pbBuff, "\\cf", 3);
  143.                     pbBuff += 3;
  144.                     cb -= 3;
  145.                     *pcb += 3;
  146.                 }
  147.                 else
  148.                 {
  149.                     memcpy(pbBuff, "\\par \\cf", 8);
  150.                     pbBuff += 8;
  151.                     cb -= 8;
  152.                     *pcb += 8;
  153.                 }
  154.                 switch (buffer.type)
  155.                 {
  156.                 default:
  157.                 case 0:
  158.                     *(pbBuff++) = '2';
  159.                     break;
  160.                 case 1:
  161.                     *(pbBuff++) = '5';
  162.                     break;
  163.                 case 2:
  164.                     *(pbBuff++) = '3';
  165.                     break;
  166.                 case 3:
  167.                     *(pbBuff++) = '4';
  168.                     break;
  169.                 }
  170.                 buffer.pos = 0;
  171.             }
  172.             char *p = buffer.status + buffer.pos;
  173.             while (*p && cb > 9)
  174.             {    
  175.                 switch (*p)
  176.                 {
  177.                 case '\\':
  178.                     *(pbBuff++) = '\\';
  179.                     *(pbBuff++) = '\\';
  180.                     cb -= 2;
  181.                     *pcb += 2;
  182.                     break;
  183.                 case '{':
  184.                     *(pbBuff++) = '\\';
  185.                     *(pbBuff++) = '{';
  186.                     cb -= 2;
  187.                     *pcb += 2;
  188.                     break;
  189.                 case '}':
  190.                     *(pbBuff++) = '\\';
  191.                     *(pbBuff++) = '}';
  192.                     cb -= 2;
  193.                     *pcb += 2;
  194.                     break;
  195.                 case '\r':
  196.                     break;
  197.                 case '\n':
  198.                     *(pbBuff++) = '\\';
  199.                     *(pbBuff++) = 's';
  200.                     *(pbBuff++) = 't';
  201.                     *(pbBuff++) = 'a';
  202.                     *(pbBuff++) = 't';
  203.                     *(pbBuff++) = 'u';
  204.                     *(pbBuff++) = 's';
  205.                     cb -= 7;
  206.                     *pcb += 7;
  207.                     break;
  208.                 default:
  209.                     *(pbBuff++) = *p;
  210.                     cb--;
  211.                     (*pcb)++;
  212.                 }
  213.                 p++;
  214.  
  215.             }
  216.             if (!*p)
  217.             {
  218.                 delete [] buffer.status;
  219.                 pThis->m_statusBuffer.pop_front();
  220.                 if (pThis->m_statusBuffer.empty())
  221.                 {
  222.                     memcpy(pbBuff, "} ", 2);
  223.                     *pcb += 2;
  224.                 }
  225.                 else
  226.                 {
  227.                     *(pbBuff++) = ' ';
  228.                     (*pcb)++;
  229.                 }
  230.             }
  231.             else
  232.             {
  233.                 buffer.pos = p - buffer.status;
  234.             }
  235.         }
  236.         else
  237.         {
  238.             pThis->m_statusBuffer.pop_front();
  239.             if (pThis->m_statusBuffer.empty())
  240.             {
  241.                 memcpy(pbBuff, "} ", 2);
  242.                 *pcb += 2;
  243.             }
  244.         }
  245.     }
  246.  
  247.     return 0;
  248. }
  249.  
  250. void CStatusCtrl::OnOutputcontextClearall() 
  251. {
  252.     t_buffer buffer;
  253.     buffer.status = 0;
  254.     buffer.pos = -1;
  255.     buffer.type = 0;
  256.     m_statusBuffer.push_back(buffer);
  257.  
  258.     EDITSTREAM es;
  259.     es.dwCookie = (DWORD)this; // Pass a pointer to the CString to the callback function 
  260.     es.pfnCallback = RichEditStreamInCallback; // Specify the pointer to the callback function.
  261.     
  262.     StreamIn(SF_RTF, es); // Perform the streaming
  263.     SetSel(-1, -1);
  264.     LimitText(1000*1000);
  265.     int res = GetLimitText();
  266.     
  267.     m_bEmpty = TRUE;
  268.     m_nMoveToBottom = 0;
  269. }
  270.  
  271. void CStatusCtrl::OnOutputcontextCopytoclipboard() 
  272. {
  273.     long nStart, nEnd;
  274.     GetSel(nStart, nEnd);
  275.     if (nStart == nEnd)
  276.     {
  277.         HideSelection(TRUE, FALSE);
  278.         SetSel(0, -1);
  279.         Copy();
  280.         SetSel(nStart, nEnd);
  281.         HideSelection(FALSE, FALSE);
  282.     }
  283.     else
  284.         Copy();
  285. }
  286.  
  287.  
  288. int CStatusCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  289. {
  290.     if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1)
  291.         return -1;
  292.  
  293.     USES_CONVERSION;
  294.     
  295.     m_RTFHeader = "{\\rtf1\\ansi\\deff0";
  296.  
  297.     HFONT hSysFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  298.  
  299.     LOGFONT lf;
  300.     CFont* pFont = CFont::FromHandle( hSysFont );
  301.     pFont->GetLogFont( &lf );
  302.  
  303.     LOGFONT m_lfFont;
  304.     pFont->GetLogFont(&m_lfFont);
  305.     
  306.     m_RTFHeader += "{\\fonttbl{\\f0\\fnil "+ CString(m_lfFont.lfFaceName)+";}}";
  307.     m_RTFHeader += "{\\colortbl ;";
  308.     for (int i=0; i<16; i++)
  309.     {
  310.         CString tmp;
  311.         tmp.Format(_T("\\red%d\\green%d\\blue%d;"), GetRValue(m_ColTable[i]), GetGValue(m_ColTable[i]), GetBValue(m_ColTable[i]));
  312.         m_RTFHeader+=tmp;
  313.     }
  314.     m_RTFHeader += "}";
  315.     
  316.     int pointsize = (-m_lfFont.lfHeight*72/ GetDeviceCaps(GetDC()->GetSafeHdc(), LOGPIXELSY))*2;
  317.     CString tmp;
  318.     tmp.Format(_T("%d"), pointsize);
  319.     m_RTFHeader += "\\uc1\\pard\\fi-200\\li200\\tx200\\f0\\fs"+tmp; //180*m_nAvgCharWidth;
  320.  
  321.     t_buffer buffer;
  322.     buffer.status = 0;
  323.     buffer.pos = -1;
  324.     buffer.type = 0;
  325.     m_statusBuffer.push_back(buffer);
  326.  
  327.     m_headerPos = 0;
  328.  
  329.     EDITSTREAM es;
  330.     es.dwCookie = (DWORD)this; // Pass a pointer to the CString to the callback function 
  331.     es.pfnCallback = RichEditStreamInCallback; // Specify the pointer to the callback function.
  332.     
  333.     StreamIn(SF_RTF, es); // Perform the streaming
  334.     SetSel(-1, -1);
  335.     LimitText(1000*1000);
  336.     
  337.     return 0;
  338. }
  339.  
  340. void CStatusCtrl::ShowStatus(char *status, int nType)
  341. {
  342.     t_buffer buffer;
  343.     buffer.status = status;
  344.     buffer.type = nType;
  345.     buffer.pos = -1;
  346.  
  347.     m_statusBuffer.push_back(buffer);
  348.  
  349.     if (!m_runTimer)
  350.     {
  351.         Run();
  352.         m_runTimer = SetTimer(1339, 250, 0);
  353.     }
  354.  
  355.     return;
  356. }
  357.  
  358. void CStatusCtrl::Run()
  359. {
  360.     if (m_statusBuffer.empty())
  361.         return;
  362.  
  363.     m_headerPos = 0;
  364.  
  365.     EDITSTREAM es;
  366.  
  367.     es.dwCookie = (DWORD)this; // Pass a pointer to the CString to the callback function 
  368.     es.pfnCallback = RichEditStreamInCallback; // Specify the pointer to the callback function.
  369.  
  370.     CWnd *pFocusWnd = GetFocus();
  371.     if (pFocusWnd && pFocusWnd == this)
  372.         AfxGetMainWnd()->SetFocus();
  373.     
  374.     long nStart, nEnd;
  375.     GetSel(nStart, nEnd);
  376.     BOOL nScrollToEnd = FALSE;
  377.     
  378.     int num = 0;            //this is the number of visible lines
  379.     CRect rect;
  380.     GetRect(rect);
  381.     int height = rect.Height();
  382.     
  383.     for (int i = GetFirstVisibleLine();
  384.             i < GetLineCount() && GetCharPos(LineIndex(i)).y < height;
  385.             i++)
  386.         num++;
  387.  
  388.     if (GetFirstVisibleLine() + num+m_nMoveToBottom >= GetLineCount())
  389.         nScrollToEnd = TRUE;
  390.     HideSelection(TRUE, FALSE);
  391.     SetSel(-1, -1);
  392.     StreamIn(SF_RTF | SFF_SELECTION, es); // Perform the streaming
  393.  
  394.     int count = GetLineCount();
  395.     if (count > 1000)
  396.     {
  397.         count = count - 1000;
  398.         int index = LineIndex(count);
  399.         nStart -= index;
  400.         nEnd -= index;
  401.         if (nStart < 0)
  402.             nEnd = 0;
  403.         if (nEnd < 0)
  404.             nEnd = 0;
  405.         SetSel(0, index);
  406.         ReplaceSel(_T(""));
  407.     }
  408.  
  409.     SetSel(nStart, nEnd);
  410.     
  411.     if (pFocusWnd && pFocusWnd == this)
  412.         SetFocus();
  413.  
  414.     HideSelection(FALSE, FALSE);
  415.     if (nScrollToEnd)
  416.     {
  417.         if (nStart != nEnd && (LineFromChar(nStart) >= GetFirstVisibleLine() && LineFromChar(nStart) <= GetFirstVisibleLine() + num ||
  418.                                LineFromChar(nEnd) >= GetFirstVisibleLine() && LineFromChar(nEnd) <= GetFirstVisibleLine() + num))
  419.             LineScroll(1);
  420.         else 
  421.         {
  422.             m_nMoveToBottom++;
  423.             if (!m_nTimerID)
  424.                 m_nTimerID = SetTimer(654, 25, NULL);
  425.         }
  426.     }
  427. }
  428.  
  429. void CStatusCtrl::OnTimer(UINT_PTR nIDEvent) 
  430. {
  431.     if (nIDEvent == m_nTimerID)
  432.     {
  433.         if (m_nMoveToBottom)
  434.         {
  435.             SendMessage(WM_VSCROLL, SB_BOTTOM, 0);
  436.             m_nMoveToBottom = 0;
  437.         }
  438.         KillTimer(m_nTimerID);
  439.         m_nTimerID = 0;
  440.     }
  441.     else if (nIDEvent == static_cast<UINT>(m_runTimer))
  442.     {
  443.         KillTimer(m_runTimer);
  444.         m_runTimer = 0;
  445.         Run();
  446.     }
  447.     CRichEditCtrl::OnTimer(nIDEvent);
  448. }
  449.  
  450. void CStatusCtrl::OnRButtonUp(UINT nFlags, CPoint point) 
  451. {
  452.     ClientToScreen(&point);
  453.  
  454.     CMenu menu;
  455.     menu.LoadMenu(IDR_OUTPUTCONTEXT);
  456.     
  457.     CMenu* pPopup = menu.GetSubMenu(0);
  458.     ASSERT(pPopup != NULL);
  459.     CWnd* pWndPopupOwner = this;
  460.     //while (pWndPopupOwner->GetStyle() & WS_CHILD)
  461.     //    pWndPopupOwner = pWndPopupOwner->GetParent();
  462.     
  463.     if (!GetLineCount())
  464.     {
  465.         pPopup->EnableMenuItem(ID_OUTPUTCONTEXT_COPYTOCLIPBOARD,MF_GRAYED);
  466.         pPopup->EnableMenuItem(ID_OUTPUTCONTEXT_CLEARALL,MF_GRAYED);
  467.     }
  468.     HCURSOR    hCursor;
  469.     hCursor = AfxGetApp()->LoadStandardCursor( IDC_ARROW );
  470.     m_doPopupCursor = TRUE;
  471.     SetCursor(hCursor);
  472.     
  473.     pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
  474.         pWndPopupOwner);
  475. }
  476.  
  477. BOOL CStatusCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
  478. {
  479.     OSVERSIONINFO info = {0};
  480.     info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  481.     GetVersionEx(&info);
  482.     if (info.dwMajorVersion >= 5)
  483.         return CRichEditCtrl::OnMouseWheel(nFlags, zDelta, pt);
  484.  
  485.     LineScroll(-zDelta / 120 * 3);
  486.  
  487.     return TRUE;
  488. }
  489.