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

  1. // grpdlg.cpp : implementation file
  2. //
  3.  
  4. #include <afxwin.h>
  5. #include <afxext.h> 
  6. #include <ctl3d.h>
  7.  
  8. //#include "stdafx.h"
  9. //#include "morespac.h"
  10. #include "resource.h"
  11. #include "grpdlg.h"
  12. #include "appdlg.h"
  13. #include "appinfo.h"
  14. #include "app.h"
  15. #include "icondlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #define new DEBUG_NEW
  21. #endif        
  22.  
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAppGrpDlg dialog
  26.  
  27. CAppGrpDlg::CAppGrpDlg(CWnd* pParent /*=NULL*/)
  28.    : CDialog(CAppGrpDlg::IDD, pParent)
  29. {
  30.    //{{AFX_DATA_INIT(CAppGrpDlg)
  31.       // NOTE: the ClassWizard will add member initialization here
  32.    //}}AFX_DATA_INIT
  33.    
  34.    m_pCurrent = 0;
  35. }
  36.  
  37. void CAppGrpDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39.    CDialog::DoDataExchange(pDX);
  40.    //{{AFX_DATA_MAP(CAppGrpDlg)
  41.    DDX_Control(pDX, IDC_GROUPICON, m_Icon);
  42.    DDX_Control(pDX, IDC_LISTAPPS, m_lstApps);
  43.    //}}AFX_DATA_MAP
  44. }
  45.  
  46. BEGIN_MESSAGE_MAP(CAppGrpDlg, CDialog)
  47.    //{{AFX_MSG_MAP(CAppGrpDlg)
  48.    ON_BN_CLICKED(IDC_CHANGEICON, OnClickedChangeicon)
  49.    ON_BN_CLICKED(IDC_HELP1, OnClickedHelp1)
  50.    ON_BN_CLICKED(IDC_EDIT, OnClickedEdit)
  51.    ON_BN_CLICKED(IDC_ADD, OnClickedAdd)
  52.    ON_BN_CLICKED(IDC_REMOVE, OnClickedRemove)
  53.    ON_BN_CLICKED(IDC_LAUNCH, OnClickedLaunch)
  54.    ON_LBN_DBLCLK(IDC_LISTAPPS, OnDblclkListapps)
  55.    ON_BN_CLICKED(IDC_REMOVEALL, OnClickedRemoveAll)
  56.    //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CAppGrpDlg message handlers
  61.  
  62. BOOL CAppGrpDlg::OnInitDialog()
  63. {
  64.    CDialog::OnInitDialog();
  65.    
  66.    // TODO: Add extra initialization here
  67.    // CenterWindow();
  68.    
  69.    // get a pointer to the current AppList
  70.    // and insert the CApplications from this list
  71.    // to the Listbox
  72.    CMyApp * pApp = (CMyApp*)AfxGetApp();
  73.    const CApplicationList * pList = pApp->CurrentApplicationList();
  74.  
  75.    // check for the icon to use for this group
  76.    // this is either IDI_GROUPEMTY or whatever
  77.    // icon was set in other ways and stored to this 
  78.    // group
  79.    m_strIconFile = pList->IconFile();
  80.    m_nIcon = pList->IconID();
  81.            
  82.    for ( POSITION pos=pList->GetHeadPosition(); pos!=NULL; )
  83.    {
  84.       const CApplication * pApp = pList->GetNext(pos);
  85.       CApplication * pCopy = new CApplication( *pApp );
  86.       int index = m_lstApps.AddString( pCopy->Title() );
  87.       m_lstApps.SetItemDataPtr( index, (void*)pCopy );
  88.    }
  89.  
  90.    // make the first item the currently selected one
  91.    if ( m_lstApps.GetCount() )
  92.       m_lstApps.SetCurSel( 0 );
  93.  
  94.    HandleIcon();
  95.    return TRUE;  // return TRUE  unless you set the focus to a control
  96. }
  97.  
  98. void 
  99. CAppGrpDlg::HandleIcon()
  100. {  
  101.    // if an icon is specified via file and number,
  102.    // use this one
  103.    HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), m_strIconFile, m_nIcon );
  104.    if ( hIcon!=NULL && hIcon != (HICON)1 )
  105.    {  
  106.       m_Icon.SetIcon( hIcon );
  107.       return;
  108.    }
  109.  
  110.    // if there is no item in this group
  111.    // use the IDI_GROUPEMPTY icon
  112.    if ( m_lstApps.GetCount() == 0 )
  113.    {
  114.       m_Icon.SetIcon( ((CMyApp*)AfxGetApp())->LoadIcon( IDI_GROUPEMPTY ) );
  115.       return;
  116.    }  
  117.    
  118.    // if there is only one icon in this group,
  119.    // use it`s first icon
  120.    // if it has none, use the ordinary group icon
  121.    if ( m_lstApps.GetCount()==1 )
  122.    {  
  123.       CApplication * pApp = (CApplication*)m_lstApps.GetItemDataPtr( 0 );
  124.       HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pApp->Path(), 0 );
  125.       if ( hIcon )
  126.       {      
  127.          m_Icon.SetIcon( hIcon );
  128.          return;
  129.       }         
  130.    }   
  131.    
  132.    // in all other cases, use the group icon
  133.    m_Icon.SetIcon( ((CMyApp*)AfxGetApp())->LoadIcon( IDI_GROUP ));
  134. }
  135.  
  136. const CApplication * 
  137. CAppGrpDlg::CurrentApplication() const
  138. {     
  139.    ASSERT( m_pCurrent != 0 );
  140.    return m_pCurrent;
  141. }
  142.       
  143.       
  144.       
  145. void CAppGrpDlg::OnClickedChangeicon()
  146. {
  147.    // TODO: Add your control notification handler code here
  148.    CIconDlg dlg;
  149.    if ( dlg.DoModal()==IDOK )
  150.    {     
  151.       m_strIconFile = dlg.m_strIconFile;
  152.       m_nIcon = dlg.m_nIcon;
  153.       HandleIcon();
  154.    }   
  155. }
  156.  
  157. void CAppGrpDlg::OnClickedHelp1()
  158. {
  159.    AfxGetApp()->WinHelp( 0L, HELP_CONTENTS );
  160. }
  161.  
  162. void CAppGrpDlg::OnClickedEdit()
  163. {
  164.    // TODO: Add your control notification handler code here
  165.    
  166.    // Get the currently selected item
  167.    int index = m_lstApps.GetCurSel();
  168.    
  169.    if ( index == LB_ERR )
  170.    {
  171.       AfxMessageBox("You need to select an item from the listbox to edit one");      
  172.       return;
  173.    }
  174.    
  175.    // if there is a selected item,
  176.    // get its attached CApplication and
  177.    // set m_pCurrent so that the
  178.    // edit dialog will be able to
  179.    // retrieve the needed information
  180.    m_pCurrent = (CApplication*)m_lstApps.GetItemDataPtr( index );
  181.    
  182.    CAppDlg dlg;
  183.    if ( dlg.DoModal() == IDOK )
  184.    {
  185.       // seems we have some changed values here
  186.       m_pCurrent->Description(
  187.          dlg.m_strAppPath,
  188.          dlg.m_strAppTitle,
  189.          dlg.m_strAppArgs,
  190.          dlg.m_strAppDir,
  191.          dlg.m_nRunState
  192.          );  
  193.          
  194.       // change the apps title in the listbox         
  195.       m_lstApps.DeleteString( index ); 
  196.       m_lstApps.InsertString( index, m_pCurrent->Title() );
  197.       m_lstApps.SetItemDataPtr( index, (void*)m_pCurrent );
  198.       
  199.       // make it the currently selected item,
  200.       m_lstApps.SetCurSel( index );
  201.       
  202.       // handle the icon display
  203.       HandleIcon();
  204.    }
  205.  
  206.    m_pCurrent = 0;      
  207. }
  208.  
  209. void CAppGrpDlg::OnClickedAdd()
  210. {
  211.    // TODO: Add your control notification handler code here
  212.    
  213.    // add a new application
  214.    m_pCurrent = new CApplication();
  215.    CAppDlg dlg;
  216.    if ( dlg.DoModal() == IDOK )
  217.    {
  218.       // seems we have a new application
  219.       // we might want to launch
  220.       m_pCurrent->Description(
  221.          dlg.m_strAppPath,
  222.          dlg.m_strAppTitle,
  223.          dlg.m_strAppArgs,
  224.          dlg.m_strAppDir,
  225.          dlg.m_nRunState
  226.          );  
  227.          
  228.       // add the apps title to the listbox         
  229.       int index = m_lstApps.AddString( m_pCurrent->Title() );
  230.       m_lstApps.SetItemDataPtr( index, (void*)m_pCurrent );
  231.       
  232.       // make it the currently selected item,
  233.       m_lstApps.SetCurSel( index );
  234.       
  235.       // handle the icon display
  236.       HandleIcon();
  237.    }
  238.    else
  239.    {
  240.       delete m_pCurrent;
  241.    }              
  242.    
  243.    m_pCurrent = 0;
  244. }
  245.    
  246.  
  247. void CAppGrpDlg::OnClickedRemove()
  248. {
  249.    // Get the currently selected item
  250.    int index = m_lstApps.GetCurSel();
  251.    
  252.    if ( index == LB_ERR )
  253.    {
  254.       AfxMessageBox("You need to select an item from the listbox to delete one");      
  255.       return;
  256.    }
  257.                
  258.    // remove the CApplication attached to this item               
  259.    delete (CApplication*)m_lstApps.GetItemDataPtr( index );
  260.    
  261.    // remove the string
  262.    m_lstApps.DeleteString( index );   
  263.    
  264.    // handle icon display
  265.    HandleIcon();
  266. }
  267.  
  268. void CAppGrpDlg::OnClickedLaunch()
  269. {
  270.    // TODO: Add your control notification handler code here
  271.    // Get the currently selected item
  272.    int index = m_lstApps.GetCurSel();
  273.    
  274.    if ( index == LB_ERR )
  275.    {
  276.       AfxMessageBox("You need to select an item from the listbox to launch one");      
  277.       return;
  278.    }
  279.                
  280.    // run the CApplication attached to this item               
  281.    CApplication * pApp = (CApplication*)m_lstApps.GetItemDataPtr( index );
  282.    pApp->Run();     
  283.    
  284.    // fade
  285.    OnOK();
  286. }
  287.  
  288. void CAppGrpDlg::OnDblclkListapps()
  289. {
  290.    // TODO: Add your control notification handler code here
  291.    OnClickedLaunch();
  292. }
  293.  
  294. void CAppGrpDlg::OnOK()
  295. {
  296.    // TODO: Add extra validation here
  297.    // store the icon info for later usage
  298.    m_ApplicationList.Icon( m_strIconFile, m_nIcon );
  299.    
  300.    // copy the CApplist fromt eh item data ptrs to the
  301.    // m_ApplicationList so the calling
  302.    // function can retrieve the data
  303.    int nItems = m_lstApps.GetCount();
  304.    for ( int iItems=0; iItems<nItems; ++iItems )
  305.    {
  306.       m_ApplicationList.AddTail( new CApplication ( *(CApplication*)m_lstApps.GetItemDataPtr(iItems)) );
  307.    }
  308.  
  309.    // free the item data ptrs
  310.    FreeItemData();     
  311.      
  312.    CDialog::OnOK();
  313. }
  314.  
  315. void
  316. CAppGrpDlg::FreeItemData()
  317. {
  318.    int nItems = m_lstApps.GetCount();
  319.    for ( int iItems=0; iItems<nItems; ++iItems )
  320.    {
  321.       delete (CApplication*)m_lstApps.GetItemDataPtr(iItems);
  322.    }
  323. }
  324. void CAppGrpDlg::OnCancel()
  325. {
  326.    // TODO: Add extra cleanup here
  327.    FreeItemData();   
  328.    CDialog::OnCancel();
  329. }
  330.  
  331. void CAppGrpDlg::OnClickedRemoveAll()
  332. {
  333.    // TODO: Add your control notification handler code here
  334.    FreeItemData();
  335.    m_lstApps.ResetContent();
  336.    m_strIconFile="";
  337.    m_nIcon=0;
  338.    HandleIcon();   
  339.    
  340. }
  341.