home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / WILDASS / SOURCE / MAINWND.CPP < prev    next >
C/C++ Source or Header  |  1993-08-14  |  12KB  |  418 lines

  1. // mainwnd.cpp
  2. //
  3. // class behaviour of the main window class
  4. //
  5. // update: 1.00 28-Jul-93 tw  
  6. //         1.01 01-Aug-93 tw put in FakeCptn functionality
  7.  
  8. #include <afxwin.h>
  9. #include <afxext.h> 
  10. #include <ctl3d.h>
  11.                     
  12. #include "resource.h"                    
  13. #include "mainwnd.h"
  14. #include "iconbtn.h"  
  15. #include "app.h"
  16.     
  17. static UINT BASED_CODE arrTileButtons[] = 
  18.    { 
  19.       IDB_TILE1, IDB_TILE2, IDB_TILE3, IDB_TILE4
  20.    };    
  21.    
  22. static UINT BASED_CODE arrWorkspaceButtons[] =
  23.    {
  24.       IDB_WSP1, IDB_WSP2, IDB_WSP3, IDB_WSP4
  25.    };
  26.    
  27. static UINT BASED_CODE arrOptionsButtons[] =
  28.    {
  29.       IDB_OPTS1, IDB_OPTS2, IDB_OPTS3, IDB_OPTS4
  30.    };            
  31.             
  32. BEGIN_MESSAGE_MAP( CMainWnd, CFrameWnd )
  33.    ON_WM_CREATE()
  34.    ON_WM_NCHITTEST()
  35.    ON_WM_PAINT()
  36.    ON_WM_ERASEBKGND()
  37.    ON_WM_ACTIVATEAPP() 
  38.    ON_WM_SYSCOMMAND()
  39.    
  40.    ON_COMMAND( nBeginIdIconButtons,   OnLaunchGrp1 )
  41.    ON_COMMAND( nBeginIdIconButtons+1, OnLaunchGrp2 )
  42.    ON_COMMAND( nBeginIdIconButtons+2, OnLaunchGrp3 )
  43.    ON_COMMAND( nBeginIdIconButtons+3, OnLaunchGrp4 )
  44.    ON_COMMAND( nBeginIdIconButtons+4, OnLaunchGrp5 )
  45.    ON_COMMAND( nBeginIdIconButtons+5, OnLaunchGrp6 )
  46.    ON_COMMAND( nBeginIdIconButtons+6, OnLaunchGrp7 )
  47.    ON_COMMAND( nBeginIdIconButtons+7, OnLaunchGrp8 )
  48.    ON_COMMAND( nBeginIdEditButtons,   OnEditGrp1 )
  49.    ON_COMMAND( nBeginIdEditButtons+1, OnEditGrp2 )
  50.    ON_COMMAND( nBeginIdEditButtons+2, OnEditGrp3 )
  51.    ON_COMMAND( nBeginIdEditButtons+3, OnEditGrp4 )
  52.    ON_COMMAND( nBeginIdEditButtons+4, OnEditGrp5 )
  53.    ON_COMMAND( nBeginIdEditButtons+5, OnEditGrp6 )
  54.    ON_COMMAND( nBeginIdEditButtons+6, OnEditGrp7 )
  55.    ON_COMMAND( nBeginIdEditButtons+7, OnEditGrp8 )
  56.    
  57. END_MESSAGE_MAP()            
  58.           
  59. CMainWnd::CMainWnd()
  60. {          
  61.    // calculate the fake captions size
  62.    int cyCaption = GetSystemMetrics( SM_CYCAPTION ) / 3; 
  63.    
  64.    // calculate the number of toolbar buttons
  65.    m_nToolBarButtons = sizeof( arrTileButtons ) / sizeof(UINT);
  66.    
  67.    // calculate the 3 toolbars rects                  
  68.    int cxBar = m_nToolBarButtons * CMainWnd::cxBtnToolBar + 2 * CMainWnd::cxFrameToolBar;
  69.    m_rectTileBar = CRect( 0, cyCaption, 
  70.                           cxBar, cyCaption + CMainWnd::cyBtnToolBar + CMainWnd::cyFrameToolBar );
  71.                           
  72.    m_rectTileBar.OffsetRect( 0, 2 );                          
  73.    m_rectWorkspaceBar = m_rectTileBar;
  74.    m_rectWorkspaceBar.OffsetRect( CSize( 0, CMainWnd::cyBtnToolBar + CMainWnd::cyFrameToolBar ) );
  75.    m_rectOptionsBar = m_rectWorkspaceBar;
  76.    m_rectOptionsBar.OffsetRect( CSize( 0, CMainWnd::cyBtnToolBar + CMainWnd::cyFrameToolBar ) );
  77.  
  78.    // calculate the windows size
  79.    m_rectWnd = CRect( 0, 0, 
  80.             m_rectTileBar.Width()+                 // tileBar width
  81.             nIconButtons*CIconButton::Width()+     // plus icon buttons width
  82.             CMainWnd::cxFrameToolBar-              // frame toolbar width
  83.             nIconButtons,                          // icon buttons overlap by one pixel
  84.             m_rectOptionsBar.BottomRight().y+
  85.             CMainWnd::cyFrameToolBar );
  86.                       
  87.    // calculate the fake caption bar size
  88.    m_pRectCaption = new CRect( 0, 0, m_rectWnd.Width(), cyCaption);                      
  89.    ASSERT( m_pRectCaption );
  90.    
  91.    // create it, using the base classes create fxn
  92.    VERIFY( CreateEx( WS_EX_TOPMOST, NULL, AfxGetAppName(), 
  93.       WS_POPUP | WS_BORDER, m_rectWnd.TopLeft().x, 
  94.       m_rectWnd.TopLeft().y, m_rectWnd.Width(),
  95.       m_rectWnd.Height(), NULL, NULL ));
  96.       
  97. }  
  98.  
  99. CMainWnd::~CMainWnd()
  100. {
  101.    // take care of the alloted objects
  102.    delete m_pRectCaption;
  103. }
  104.  
  105. void 
  106. CMainWnd::DroppedFiles( HDROP hDrop, UINT nID )
  107. {
  108.    // get number of files
  109.    UINT nFiles = DragQueryFile( hDrop, -1, NULL, 0 );   
  110.                                 
  111.    // calculate, which list is responsible for these files
  112.    if ( nID >= nBeginIdEditButtons ) nID-=100;
  113.    
  114.    nID-=200;   // id is now an index into the applist array
  115.                       
  116.    // for each file, query the file information
  117.    for ( UINT iFiles=0; iFiles<nFiles; iFiles++ )
  118.    {
  119.       UINT cbFileBuffer = DragQueryFile( hDrop, iFiles, NULL, 0 );
  120.    
  121.       CString strFileName;            
  122.       char * pFileName = strFileName.GetBuffer( cbFileBuffer+2 );
  123.     
  124.       DragQueryFile( hDrop, iFiles, pFileName, cbFileBuffer+1 );
  125.       strFileName.ReleaseBuffer();
  126.       
  127.       // add the filename to the appropriate Application list
  128.       // and take care of possible icon redraws
  129.       ((CMyApp*)AfxGetApp())->AddToGroup( nID, strFileName );
  130.       m_arrIconButtons[nID].SetIcon( ((CMyApp*)AfxGetApp())->Icon( nID ));
  131.    }   
  132.  
  133.    // free the allocated dropfile memory      
  134.    ::DragFinish( hDrop );
  135. }
  136.  
  137. void 
  138. CMainWnd::OnSysCommand( UINT nID, LPARAM lParam )
  139. {
  140.    if ( nID == SC_CLOSE )
  141.    {
  142.       AfxMessageBox("You cannot close this application with that command."
  143.           "If you close down MoreSpace!, Windows will be terminated. "
  144.           "If you want to end your Windows session, use the Exit Windows button "
  145.           "from the task dialog." );
  146.              
  147.    } 
  148.    else
  149.    {
  150.       CWnd::OnSysCommand( nID, lParam ); 
  151.    }      
  152. }
  153.           
  154. int          
  155. CMainWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )
  156. {       
  157.    // make sure base classes OnCreate works
  158.    if ( CFrameWnd::OnCreate( lpCreateStruct ) == -1 )
  159.       return -1;
  160.     
  161.    // make the window to be of appropriate size
  162.    // but don`t show (repaint) it
  163.    MoveWindow( m_rectWnd, FALSE );
  164.       
  165.    // create the toolbars
  166.    CreateToolBar( &m_wndTileBar,IDR_TILEBAR, arrTileButtons, &m_rectTileBar );
  167.    CreateToolBar( &m_wndWorkspaceBar,IDR_WORKSPACEBAR, arrWorkspaceButtons, &m_rectWorkspaceBar );
  168.    CreateToolBar( &m_wndOptionsBar,IDR_OPTIONSBAR, arrOptionsButtons, &m_rectOptionsBar );                                       
  169.  
  170.    // create the IconButtons and the editButtons
  171.    CPoint posIconButton( m_rectTileBar.Width()-1, m_rectTileBar.TopLeft().y+CMainWnd::cyFrameToolBar );
  172.    CRect rectWnd;
  173.    for ( UINT iIconButtons=0; iIconButtons < nIconButtons; ++iIconButtons )
  174.    {
  175.       m_arrIconButtons[iIconButtons].Create( this, posIconButton, nBeginIdIconButtons+iIconButtons );
  176.       
  177.       // set the icon for this button by asking the app what icon
  178.       // the attached list is using
  179.       m_arrIconButtons[iIconButtons].SetIcon( ((CMyApp*)AfxGetApp())->Icon( iIconButtons ));
  180.       
  181.       // tell windows this button will accept files
  182.       m_arrIconButtons[iIconButtons].DragAcceptFiles( TRUE );      
  183.       
  184.       // create the edit buttons
  185.       CRect rectWnd;
  186.       m_arrIconButtons[iIconButtons].GetWindowRect( &rectWnd );
  187.       ScreenToClient( &rectWnd );
  188.       CRect posEditButton( rectWnd.TopLeft().x, 
  189.                            rectWnd.BottomRight().y-1, 
  190.                            rectWnd.TopLeft().x+rectWnd.Width(), 
  191.                            m_rectOptionsBar.BottomRight().y );
  192.       m_arrEditButtons[iIconButtons].Create( "",
  193.                                              WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  194.                                              posEditButton,
  195.                                              this,
  196.                                              nBeginIdEditButtons+iIconButtons );
  197.                                              
  198.       // move pos so the next button is located someplace else
  199.       posIconButton.Offset( CIconButton::Width()-1, 0 );
  200.       m_arrEditButtons[iIconButtons].DragAcceptFiles( TRUE );
  201.    }                                     
  202.        
  203.    // retrun OK, in this case: 0
  204.    return 0;       
  205. }
  206.  
  207. void 
  208. CMainWnd::CreateToolBar( CToolBar * pBar, UINT nIdResource, const UINT * pArrBtns, CRect * pRect )
  209. {
  210.    // create the toolbar
  211.    VERIFY( pBar->Create( this, WS_CHILD | WS_VISIBLE | CBRS_NOALIGN ));
  212.    VERIFY( pBar->LoadBitmap( nIdResource ));
  213.    VERIFY( pBar->SetButtons( pArrBtns, m_nToolBarButtons ));
  214.    pBar->SetSizes( CSize( CMainWnd::cxBtnToolBar, CMainWnd::cyBtnToolBar ),
  215.                           CSize( CMainWnd::cxImageToolBar, CMainWnd::cyImageToolBar ));
  216.     
  217.    // Move the bar were we want it to be
  218.    ScreenToClient( pRect );    
  219.    pBar->MoveWindow( *pRect );
  220.    
  221. }
  222.  
  223.    
  224. UINT
  225. CMainWnd::OnNcHitTest( CPoint pnt )
  226. {  
  227.    // get the client coordinates of the point we`re testing     
  228.    ScreenToClient( &pnt );
  229.    
  230.    // if we`re in the fake caption bar, tell windows we`re in
  231.    // the actual caption bar
  232.    if ( m_pRectCaption->PtInRect( pnt ))
  233.    {
  234.       return HTCAPTION;    
  235.    }
  236.    
  237.    // base class will do the rest
  238.    return CFrameWnd::OnNcHitTest( pnt );      
  239. }
  240.      
  241.       
  242. void 
  243. CMainWnd::OnPaint()
  244. {         
  245.    // select a color according to focus
  246.    COLORREF col = ( GetFocus() == this ) ? GetSysColor( COLOR_ACTIVECAPTION )
  247.                                          : GetSysColor( COLOR_INACTIVECAPTION );
  248.                
  249.    // paint the caption
  250.    CBrush brushCaption;
  251.    VERIFY( brushCaption.CreateSolidBrush( col ));
  252.    
  253.    CPaintDC dc( this );
  254.    dc.FillRect( *m_pRectCaption, &brushCaption );   
  255.           
  256.    CPen penBlack;
  257.    VERIFY( penBlack.CreatePen( PS_SOLID, 0, RGB( 0, 0, 0) ));
  258.    
  259.    // paint the black separator line
  260.    CPen * pPenOrig;
  261.    VERIFY( pPenOrig = dc.SelectObject( &penBlack ));
  262.    
  263.    dc.MoveTo( 0, m_pRectCaption->bottom );
  264.    VERIFY( dc.LineTo( m_pRectCaption->right, m_pRectCaption->bottom ));
  265.    
  266.    dc.SelectObject( pPenOrig ); 
  267. }
  268.  
  269. BOOL 
  270. CMainWnd::OnEraseBkgnd( CDC* pDC )
  271. {       
  272.    CRect rect;
  273.    GetClientRect( &rect );
  274.    CBrush brushGray( GetSysColor( COLOR_BTNFACE ) );
  275.    pDC->FillRect( &rect, &brushGray );
  276.    return FALSE;
  277. }
  278.  
  279. void
  280. CMainWnd::OnActivateApp( BOOL bActive, HTASK /*hTask*/ )
  281. {         
  282.    // if we are supposed to stay visible, we need to paint 
  283.    // some stuff - in the other case, we only need to hide
  284.    // ourselves away
  285.    CMyApp * pApp = (CMyApp*)AfxGetApp();
  286.    
  287.    // explicitly set focus so we are
  288.    // painted, even when activated via taskmanager
  289.    SetFocus();
  290.    
  291.    if( pApp->Settings_StayVisible() )
  292.    {                                  
  293.       // we only need to paint the fake caption bar
  294.       // in this case
  295.       InvalidateRect( *m_pRectCaption );
  296.    }
  297.    else
  298.    {  
  299.       // we only need to do something
  300.       // about the hiding: in case we are
  301.       // _activated_ the mousehook will show
  302.       // us...
  303.       if ( ! bActive )
  304.          ShowWindow( SW_HIDE );
  305.    }
  306. }
  307.    
  308. void 
  309. CMainWnd::OnLaunch( int nGrp )
  310. {
  311.    ((CMyApp*)AfxGetApp())->OnLaunchGroup( nGrp );
  312.     m_arrIconButtons[nGrp].SetIcon( ((CMyApp*)AfxGetApp())->Icon( nGrp ));
  313. }
  314.  
  315. void 
  316. CMainWnd::OnEdit( int nGrp )
  317. {
  318.    ((CMyApp*)AfxGetApp())->OnEditGroup( nGrp );
  319.     m_arrIconButtons[nGrp].SetIcon( ((CMyApp*)AfxGetApp())->Icon( nGrp ));
  320. }
  321.       
  322. void 
  323. CMainWnd::OnLaunchGrp1()
  324. {  
  325.    OnLaunch( 0 );
  326. }
  327.  
  328. void 
  329. CMainWnd::OnLaunchGrp2()
  330. {
  331.    OnLaunch( 1 );
  332. }
  333.  
  334. void 
  335. CMainWnd::OnLaunchGrp3()
  336. {
  337.    OnLaunch( 2 );
  338. }
  339.  
  340. void 
  341. CMainWnd::OnLaunchGrp4()
  342. {
  343.    OnLaunch( 3 );
  344. }
  345.  
  346. void 
  347. CMainWnd::OnLaunchGrp5()
  348. {  
  349.    OnLaunch( 4 );
  350. }
  351.  
  352. void 
  353. CMainWnd::OnLaunchGrp6()
  354. {
  355.    OnLaunch( 5 );
  356. }
  357.  
  358. void 
  359. CMainWnd::OnLaunchGrp7()
  360. {
  361.    OnLaunch( 6 );
  362. }
  363.  
  364. void 
  365. CMainWnd::OnLaunchGrp8()
  366. {  
  367.    OnLaunch( 7 );
  368. }
  369.  
  370. void 
  371. CMainWnd::OnEditGrp1()
  372. {  
  373.    OnEdit( 0 );
  374. }
  375.  
  376. void 
  377. CMainWnd::OnEditGrp2()
  378. {
  379.    OnEdit( 1 );
  380. }
  381.  
  382. void 
  383. CMainWnd::OnEditGrp3()
  384. {
  385.    OnEdit( 2 );
  386. }
  387.  
  388. void 
  389. CMainWnd::OnEditGrp4()
  390. {
  391.    OnEdit( 3 );
  392. }
  393.  
  394. void 
  395. CMainWnd::OnEditGrp5()
  396. {  
  397.    OnEdit( 4 );
  398. }
  399.  
  400. void 
  401. CMainWnd::OnEditGrp6()
  402. {
  403.    OnEdit( 5 );
  404. }
  405.  
  406. void 
  407. CMainWnd::OnEditGrp7()
  408. {
  409.    OnEdit( 6 );
  410. }
  411.  
  412. void 
  413. CMainWnd::OnEditGrp8()
  414. {  
  415.    OnEdit( 7 );
  416. }
  417.       
  418.