home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 November / CMCD1104.ISO / Software / Shareware / Programare / skincraft / SkinCrafter_v1.4.12_Demo.msi / _49E29CB9A65AABBF653C1037E1AA74B6 / _CDE00E59040D4A20BA32A694B570DB3A < prev    next >
Encoding:
Text File  |  2003-10-04  |  9.6 KB  |  438 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MDIDemo.h"
  6.  
  7. #include "MainFrm.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMainFrame
  17.  
  18. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  19.  
  20. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  21.     //{{AFX_MSG_MAP(CMainFrame)
  22.     ON_WM_CREATE()
  23.     ON_COMMAND(IDR_FILE_OPEN, OnFileOpen)
  24.     ON_COMMAND(ID_TOOLS_REMOVESKIN, OnToolsRemoveskin)
  25.     ON_COMMAND(ID_TOOLS_APPLYSKIN, OnToolsApplyskin)
  26.     ON_COMMAND(ID_SKIN_SETSKINDIR, OnSkinSetskindir)
  27.     //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29.  
  30. static UINT indicators[] =
  31. {
  32.     ID_SEPARATOR,           // status line indicator
  33.     ID_INDICATOR_CAPS,
  34.     ID_INDICATOR_NUM,
  35.     ID_INDICATOR_SCRL,
  36. };
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CMainFrame construction/destruction
  40.  
  41. CMainFrame::CMainFrame()
  42. {
  43.     // TODO: add member initialization code here
  44.     
  45. }
  46.  
  47. CMainFrame::~CMainFrame()
  48. {
  49. }
  50.  
  51. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  52. {
  53.     if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  54.         return -1;
  55.     
  56.     char str1[256]="";
  57.     GetCurrentDirectory(256,str1);
  58.     theApp.m_ExePath.Format("%s",str1);
  59.  
  60.     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  61.         | CBRS_TOOLTIPS | CBRS_SIZE_FIXED) ||
  62.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  63.     {
  64.         TRACE0("Failed to create toolbar\n");
  65.         return -1;      // fail to create
  66.     }
  67.  
  68.     if (!m_wndStatusBar.Create(this) ||
  69.         !m_wndStatusBar.SetIndicators(indicators,
  70.           sizeof(indicators)/sizeof(UINT)))
  71.     {
  72.         TRACE0("Failed to create status bar\n");
  73.         return -1;      // fail to create
  74.     }
  75.  
  76.     // TODO: Delete these three lines if you don't want the toolbar to
  77.     //  be dockable
  78.     m_wndToolBar.EnableDocking(CBRS_ALIGN_TOP);
  79.     EnableDocking(CBRS_ALIGN_ANY);
  80.     DockControlBar(&m_wndToolBar);
  81.  
  82.     CString str = theApp.m_ExePath;
  83.     
  84.     HKEY hk; 
  85.     DWORD dwData; 
  86.  
  87.    /* if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
  88.             "Software\\SkinCrafter\\SkinCrafter Demo", &hk)) 
  89.         AfxMessageBox("Error opening reg key");*/
  90.     if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  91.                "Software\\SkinCrafter\\SkinCrafter Demo",
  92.                0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS)
  93.     {
  94.         //AfxMessageBox("Error open key");;
  95.     }
  96.  
  97.     char skinPath[_MAX_PATH] = "";
  98.     
  99.     if(RegQueryValueEx(hk,"SkinPath",NULL,NULL,(LPBYTE)skinPath,&dwData) == ERROR_SUCCESS)
  100.     {
  101.         str.Format("%s",skinPath);
  102.         //AfxMessageBox(str+" - from registry");
  103.     }
  104.  
  105.     
  106.     RegCloseKey(hk);
  107.  
  108.     SHFILEINFO shinfo;
  109.     DWORD dw = SHGetFileInfo( str, NULL,
  110.                    &shinfo,
  111.                    sizeof(shinfo),
  112.                    SHGFI_DISPLAYNAME );
  113.     if(dw == 0)
  114.         str = theApp.m_ExePath;
  115.  
  116.     theApp.m_strSkinDir = str;
  117.  
  118.     ((CMainFrame*)AfxGetMainWnd())->FillMenu();
  119.  
  120.     return 0;
  121. }
  122.  
  123. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  124. {
  125.     if( !CMDIFrameWnd::PreCreateWindow(cs) )
  126.         return FALSE;
  127.         
  128.     return TRUE;
  129. }
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CMainFrame diagnostics
  133.  
  134. #ifdef _DEBUG
  135. void CMainFrame::AssertValid() const
  136. {
  137.     CMDIFrameWnd::AssertValid();
  138. }
  139.  
  140. void CMainFrame::Dump(CDumpContext& dc) const
  141. {
  142.     CMDIFrameWnd::Dump(dc);
  143. }
  144.  
  145. #endif //_DEBUG
  146.  
  147. /////////////////////////////////////////////////////////////////////////////
  148. // CMainFrame message handlers
  149.  
  150.  
  151. void CMainFrame::OnFileOpen() 
  152. {
  153.     // TODO: Add your command handler code here
  154.     
  155.     /////////////////////////////////////
  156.     
  157.     CString Filter("SkinCrafter files (*.skf)|*.skf||");
  158.  
  159.     CFileDialog FileDlg(TRUE, "skf", NULL, NULL, Filter);
  160.     FileDlg.m_ofn.lpstrTitle = "Select file";
  161.  
  162.     HKEY hk; 
  163.     DWORD dwData; 
  164.     
  165.     FileDlg.m_ofn.lpstrInitialDir = theApp.m_strSkinDir;
  166.  
  167.     if(IDOK == FileDlg.DoModal())
  168.     {
  169.         CString SelectedFile = FileDlg.GetPathName();
  170.  
  171.         theApp.m_pSkin.LoadSkinFromFile(SelectedFile);
  172.         theApp.m_pSkin.ApplySkin();
  173.  
  174.         theApp.m_pSkin.UpdateControl(0);
  175.     }
  176. }
  177.  
  178. void CMainFrame::OnToolsRemoveskin() 
  179. {
  180.     // TODO: Add your command handler code here
  181.     theApp.m_pSkin.RemoveSkin();
  182. }
  183.  
  184. void CMainFrame::OnToolsApplyskin() 
  185. {
  186.     // TODO: Add your command handler code here
  187.     theApp.m_pSkin.ApplySkin();
  188. }
  189. static int __stdcall BrowseProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
  190. {
  191.     switch(uMsg)
  192.     {
  193.     case BFFM_INITIALIZED:
  194.         {
  195.             HKEY hk; 
  196.             DWORD dwData; 
  197.             
  198.             CString str = theApp.m_ExePath;
  199.  
  200.             if(theApp.m_strSkinDir.IsEmpty())
  201.             {
  202.                 
  203.  
  204.                 if (RegCreateKey(HKEY_LOCAL_MACHINE, 
  205.                         "Software\\SkinCrafter\\SkinCrafter Demo", &hk)) 
  206.                     int err = 1; 
  207.  
  208.                 char skinPath[_MAX_PATH] = "";
  209.  
  210.                 if(RegQueryValueEx(hk,"SkinPath",NULL,NULL,(LPBYTE)skinPath,&dwData) == ERROR_SUCCESS)
  211.                 {
  212.                     
  213.                     str.Format("%s",skinPath);;
  214.                 }
  215.                 else
  216.                     str = theApp.m_strSkinDir;
  217.  
  218.                 RegCloseKey(hk);
  219.  
  220.                 SHFILEINFO shinfo;
  221.                 DWORD dw = SHGetFileInfo( str, NULL,
  222.                                &shinfo,
  223.                                sizeof(shinfo),
  224.                                SHGFI_DISPLAYNAME );
  225.                 if(dw == 0)
  226.                     str = theApp.m_ExePath;
  227.             }
  228.             else
  229.             {
  230.                 str = theApp.m_strSkinDir;
  231.             }
  232.  
  233.             SendMessage(hWnd,BFFM_SETSELECTION,TRUE,(LPARAM)str.GetBuffer(str.GetLength()));
  234.             str.ReleaseBuffer();
  235.             break;
  236.         }
  237.     default:
  238.         break;
  239.     }
  240.     return 0;
  241. }
  242.  
  243.  
  244. void CMainFrame::FillMenu()
  245. {
  246.     CFileFind findFile;
  247.     CString filemask = theApp.m_strSkinDir+"\\*.skf";
  248.     BOOL bWorking(findFile.FindFile(filemask));
  249.  
  250.     int count = 0;
  251.     m_MenuMap.RemoveAll();
  252.  
  253.     CMenu* pSkinMenu=NULL,*pSkinMenu2=NULL;
  254.  
  255.     if(theApp.m_hMDIMenu)
  256.     {
  257.         for(int i = CMenu::FromHandle(theApp.m_hMDIMenu)->GetMenuItemCount()-1;i>=0;i--)
  258.         {
  259.             CString str;
  260.             CMenu::FromHandle(theApp.m_hMDIMenu)->GetMenuString(i,str,MF_BYPOSITION);
  261.             if(!str.Compare("Skin"))
  262.             {
  263.                 pSkinMenu = CMenu::FromHandle(theApp.m_hMDIMenu)->GetSubMenu(i);
  264.                 break;
  265.             }
  266.         }
  267.             for(i=pSkinMenu->GetMenuItemCount()-1;i>1;i--)
  268.         {
  269.             pSkinMenu->DeleteMenu(i,MF_BYPOSITION);
  270.         }
  271.     }
  272.  
  273.  
  274.     for(int i = GetMenu()->GetMenuItemCount()-1;i>=0;i--)
  275.     {
  276.         CString str;
  277.         GetMenu()->GetMenuString(i,str,MF_BYPOSITION);
  278.         if(!str.Compare("Skin"))
  279.         {
  280.             pSkinMenu2 = GetMenu()->GetSubMenu(i);
  281.             break;
  282.         }
  283.     }
  284.     //Remove all skin items from previous skin folder if they were
  285.  
  286.     if(pSkinMenu2)
  287.         for(i=pSkinMenu2->GetMenuItemCount()-1;i>1;i--)
  288.         {
  289.             pSkinMenu2->DeleteMenu(i,MF_BYPOSITION);
  290.         }
  291.  
  292.     while (bWorking)
  293.     {
  294.         bWorking = findFile.FindNextFile();
  295.         if (findFile.IsDots() || findFile.IsDirectory())// skip . and .. files; otherwise, we'd recur infinitely!
  296.             continue;
  297.         
  298.         CString filepath= findFile.GetFilePath();
  299.         CString filename = findFile.GetFileName();
  300.         BSTR a, n, e, d;
  301.         theApp.m_pSkin.GetSkinCopyRight(filepath,&n,&a,&d,&e);
  302.  
  303.         CString key;
  304.         key.Format("%d",count+2222);
  305.         m_MenuMap.SetAt(key,filepath);
  306.  
  307.         if(a)
  308.             SysFreeString(a);
  309.         if(d)
  310.             SysFreeString(d);
  311.         if(e)
  312.             SysFreeString(e);
  313.  
  314.         if(n)
  315.         {
  316.             CString name(n);
  317.             SysFreeString(n);
  318.             if(pSkinMenu)
  319.                 pSkinMenu->AppendMenu(MF_STRING|MF_ENABLED,count+2222,name);
  320.             if(pSkinMenu!=pSkinMenu2 && pSkinMenu2)
  321.                 pSkinMenu2->AppendMenu(MF_STRING|MF_ENABLED,count+2222,name);
  322.         }
  323.         else
  324.         {
  325.             if(pSkinMenu)
  326.                 pSkinMenu->AppendMenu(MF_STRING|MF_ENABLED,count+2222,filename);
  327.             if(pSkinMenu!=pSkinMenu2 && pSkinMenu2)
  328.                 pSkinMenu2->AppendMenu(MF_STRING|MF_ENABLED,count+2222,filename);
  329.         }
  330.         count++;
  331.  
  332.     }
  333.     findFile.Close();
  334.  
  335. }
  336.  
  337. void CMainFrame::OnSkinSetskindir() 
  338. {
  339.     BROWSEINFO bi;
  340.     char foldername[MAX_PATH];
  341.     ZeroMemory(&bi,sizeof(bi));
  342.     bi.pszDisplayName=foldername;
  343.     bi.hwndOwner=m_hWnd;
  344.     bi.lpfn=(BFFCALLBACK)BrowseProc;
  345.  
  346.     CString title("Select Skin Directory");
  347.     bi.lpszTitle=title;
  348.     
  349.     bi.ulFlags=BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
  350.     LPITEMIDLIST lpid= SHBrowseForFolder(&bi);
  351.     if(lpid)
  352.     {    IMalloc* pMalloc;
  353.         SHGetPathFromIDList(lpid,foldername);
  354.         SHGetMalloc(&pMalloc);
  355.         pMalloc->Free((void*) lpid);
  356.         pMalloc->Release();
  357.         theApp.m_strSkinDir.Format("%s",foldername);
  358.     }
  359.     
  360.     FillMenu();
  361. }
  362.  
  363. LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  364. {
  365.     switch(message)    
  366.     {
  367.     case WM_COMMAND:
  368.         {
  369.             if(HIWORD(wParam)==0)
  370.             {
  371.                 WORD a = LOWORD(wParam);
  372.                 CString key,path;
  373.                 key.Format("%d",a);
  374.                 if(m_MenuMap.Lookup(key,path))
  375.                 {
  376.                     theApp.m_pSkin.LoadSkinFromFile(path);
  377.                     theApp.m_pSkin.ApplySkin();
  378.                     theApp.m_pSkin.UpdateControl(0);
  379.  
  380.                 }
  381.                 
  382.             }
  383.         }
  384.     }
  385.     return CMDIFrameWnd::WindowProc(message, wParam, lParam);
  386. }
  387.  
  388. BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
  389. {
  390.     switch(nCode)
  391.     {
  392.     case CN_UPDATE_COMMAND_UI:
  393.         {
  394.             CCmdUI* pCui = (CCmdUI*)pExtra;
  395.             if(pCui->m_pMenu)
  396.             {
  397.                 CMenu* pM = pCui->m_pMenu;
  398.                 CMenu* pSkinMenu, *pSkinMenu2;
  399.                 for(int i = CMenu::FromHandle(theApp.m_hMDIMenu)->GetMenuItemCount()-1;i>=0;i--)
  400.                 {
  401.                     CString str;
  402.                     CMenu::FromHandle(theApp.m_hMDIMenu)->GetMenuString(i,str,MF_BYPOSITION);
  403.                     if(!str.Compare("Skin"))
  404.                     {
  405.                         pSkinMenu = CMenu::FromHandle(theApp.m_hMDIMenu)->GetSubMenu(i);
  406.                         break;
  407.                     }
  408.                 }
  409.  
  410.                 for(i = GetMenu()->GetMenuItemCount()-1;i>=0;i--)
  411.                 {
  412.                     CString str;
  413.                     GetMenu()->GetMenuString(i,str,MF_BYPOSITION);
  414.                     if(!str.Compare("Skin"))
  415.                     {
  416.                         pSkinMenu2 = GetMenu()->GetSubMenu(i);
  417.                         break;
  418.                     }
  419.                 }
  420.  
  421.                 if(pM && (pM == pSkinMenu || pM == pSkinMenu2))
  422.                 {
  423.                     pCui->Enable(TRUE);
  424.                     for(int i=pM->GetMenuItemCount()-1;i>1;i--)
  425.                     {
  426.                         pM->EnableMenuItem(i,TRUE);
  427.                     }
  428.                 }
  429.                 
  430.                 return TRUE;
  431.             }
  432.  
  433.         }
  434.     }
  435.  
  436.     return CMDIFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  437. }
  438.