home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_16 / EMFScope / FILELIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-10  |  6.4 KB  |  280 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : filelist.cpp                                                         //
  10. //  Description: EMF file list box                                                   //
  11. //  Version    : 1.00.001, July 10, 2000                                             //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #include <windows.h>
  16. #include <assert.h>
  17. #include <commctrl.h>
  18.  
  19. #include "Winpp.h"
  20.  
  21. #include "resource.h"
  22. #include "canvas.h"
  23. #include "spoolfil.h"
  24.  
  25. #include "filelist.h"
  26.  
  27.  
  28. // Subclassing Emflist list box to provide context menu
  29.  
  30. LRESULT KFileList::WndProc(HWND hWnd, UINT uMsg,
  31.                             WPARAM wParam, LPARAM lParam)
  32. {
  33.     switch (uMsg)
  34.     {
  35.         case WM_RBUTTONDOWN:
  36.             {
  37.                 DWORD rslt = SendMessage(hWnd, LB_ITEMFROMPOINT, 0, lParam);
  38.  
  39.                 if ( HIWORD(rslt) == 0 ) // inside client area
  40.                 {
  41.                     POINT pt;
  42.  
  43.                     pt.x = LOWORD(lParam);
  44.                     pt.y = HIWORD(lParam);
  45.                     ClientToScreen(hWnd, & pt);
  46.  
  47.                     TrackMenu(LOWORD(rslt), pt.x, pt.y);
  48.                 }
  49.             }
  50.             break;
  51.  
  52.         case WM_COMMAND:
  53.             switch (LOWORD(wParam))
  54.             {
  55.                 // message from emflist context menu
  56.                 case IDM_OPEN:            Open();            break;
  57.                 case IDM_RENAME:        Rename();        break;
  58.                 case IDM_DELETE:        Delete();        break;
  59.                 case IDM_REMOVE:        Remove();        break;
  60.             }
  61.     }
  62.                 
  63.     return CallWindowProc(OldWndProc, hWnd, uMsg, wParam, lParam);
  64. }
  65.  
  66.  
  67. void KFileList::Create(HINSTANCE hInstance, HWND hDlg, HMENU hMenu)
  68. {
  69.     hCurInst     = hInstance;
  70.     hCurMenu     = hMenu;
  71.     hwnd_emflist = GetDlgItem(hDlg, IDC_EMFLIST);
  72.     
  73.     n_lastid   = -1;
  74.  
  75.     // subclass emflist listbox
  76.     if (hwnd_emflist)
  77.         SubClass(hwnd_emflist);
  78. }
  79.  
  80.  
  81. void KFileList::AddtoFileList(const char *filename)
  82. {
  83.     n_lastid = SendMessage(hwnd_emflist, LB_ADDSTRING, 0, (LPARAM)filename); 
  84.         
  85.     if (n_lastid>=n_maxvisible)
  86.         SendMessage(hwnd_emflist, LB_SETTOPINDEX, n_lastid-n_maxvisible, 0);
  87. }
  88.  
  89.  
  90. void KFileList::ReplaceLastName(const char *name)
  91. {
  92.     SendMessage(hwnd_emflist, LB_DELETESTRING, n_lastid, 0);
  93.     SendMessage(hwnd_emflist, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) name);
  94.     
  95.     if (n_lastid>=n_maxvisible)
  96.         SendMessage(hwnd_emflist, LB_SETTOPINDEX, n_lastid-n_maxvisible, 0);
  97. }
  98.  
  99.  
  100. int KFileList::GetCount(void)
  101. {
  102.     return SendMessage(hwnd_emflist, LB_GETCOUNT, 0, 0);
  103. }
  104.  
  105.  
  106. // fetch emf file name from list box
  107. const char *KFileList::GetEmfFileName(int id)
  108. {
  109.     char *pname;
  110.     char *pend;
  111.     static char desp[256];
  112.  
  113.     SendMessage(hwnd_emflist, LB_GETTEXT, id, (LPARAM)(LPCTSTR)desp);
  114.     // the string is in the format: page. filename (time)
  115.     // or filename
  116.             
  117.     pname = strchr(desp, ' ');
  118.     if (pname)
  119.     {
  120.         pname++;
  121.                 
  122.         pend = strchr(pname, ' ');
  123.  
  124.         if (pend)
  125.             *pend = 0;
  126.  
  127.         return pname;
  128.     }
  129.     else
  130.         return desp;
  131. }
  132.  
  133.  
  134. void KFileList::Open(void)
  135. {
  136.     int  i = SendMessage(hwnd_emflist, LB_GETCURSEL, 0, 0);
  137.  
  138.     if (i != LB_ERR)
  139.         w_canvas->LoadEmfFile(GetEmfFileName(i));
  140. }
  141.  
  142. //    HMENU hMenu = GetSubMenu(EmfScope.hMainMenu, 2);
  143.  
  144. void KFileList::TrackMenu(int id, int x, int y)
  145. {
  146.     TrackPopupMenu(hCurMenu, TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN,
  147.         x, y, 0, hwnd_emflist, NULL);
  148. }
  149.  
  150.  
  151. void KFileList::Remove(void)
  152. {
  153.     int i = SendMessage(hwnd_emflist, LB_GETCURSEL, 0, 0);
  154.  
  155.     if (i != LB_ERR)
  156.         SendMessage(hwnd_emflist, LB_DELETESTRING, i, 0);
  157. }
  158.  
  159.  
  160. // emflist rename dialog box
  161. class KRenameDialog : public KModalDialog
  162. {
  163. public:
  164.     char OldName[128];
  165.     char NewName[128];
  166.     
  167.     BOOL DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  168. };
  169.  
  170.  
  171. BOOL KRenameDialog::DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  172. {
  173.     switch (uMsg)
  174.     {
  175.         case WM_INITDIALOG:
  176.             m_hWnd = hWnd;
  177.             SetDlgItemText(hWnd, IDC_OLDNAME, OldName);
  178.             SetDlgItemText(hWnd, IDC_NEWNAME, OldName);
  179.             break;
  180.  
  181.         case WM_COMMAND:
  182.             switch (LOWORD(wParam))
  183.             {
  184.                 case IDOK:
  185.                     GetDlgItemText(hWnd, IDC_NEWNAME, NewName, sizeof(NewName) - 1);
  186.                     NewName[sizeof(NewName)-1] = 0;
  187.                     EndDialog(hWnd, IDOK);
  188.                     break;
  189.  
  190.                 case IDCANCEL:
  191.                     EndDialog(hWnd, IDCANCEL);
  192.                     break;
  193.  
  194.                 default:
  195.                     return FALSE;
  196.             }
  197.             break;
  198.  
  199.  
  200.         default:
  201.             return FALSE;
  202.     }
  203.  
  204.     return TRUE;
  205. }
  206.  
  207.  
  208. void KFileList::Rename(void)
  209. {
  210.     int i = SendMessage(hwnd_emflist, LB_GETCURSEL, 0, 0);
  211.  
  212.     if (i != LB_ERR)
  213.     {
  214.         KRenameDialog dlg;
  215.  
  216.         strcpy(dlg.OldName, GetEmfFileName(i));
  217.         
  218.         if (dlg.Dialogbox(hCurInst, IDD_RENAME, GetParent(hwnd_emflist)) == IDOK)
  219.         {
  220.             // remove file from canvas window
  221.             w_canvas->UnloadEmfFile(dlg.OldName);
  222.  
  223.             if (MoveFile(dlg.OldName, dlg.NewName))
  224.             {
  225.                 // delete old string
  226.                 SendMessage(hwnd_emflist, LB_DELETESTRING, i, 0);
  227.                 // insert new string
  228.                 SendMessage(hwnd_emflist, LB_INSERTSTRING, i, (LPARAM) (LPCSTR) dlg.NewName);
  229.  
  230.                 const char *name = GetDevFileName(dlg.OldName);
  231.  
  232.                 // rename associated .dev file
  233.                 if (name != NULL)
  234.                 {
  235.                     strcpy(dlg.OldName, name);
  236.  
  237.                     name = GetDevFileName(dlg.NewName);
  238.                     if (name != NULL)
  239.                         MoveFile(dlg.OldName, name);
  240.                 }
  241.             }
  242.             else
  243.                 MessageBeep(MB_ICONEXCLAMATION);
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249. void KFileList::Delete(void)
  250. {
  251.     int i = SendMessage(hwnd_emflist, LB_GETCURSEL, 0, 0);
  252.  
  253.     if (i != LB_ERR)
  254.     {
  255.         char   temp[128];
  256.         LPCSTR name = GetEmfFileName(i);
  257.  
  258.         wsprintf(temp, LoadStringTemp(IDS_CONFIRMDELETE), (LPCSTR) name);
  259.         
  260.         if (MessageBox(GetParent(hwnd_emflist), temp, LoadStringTemp(IDS_APPTITLE), MB_OKCANCEL) == IDOK)
  261.         {
  262.             // remove file from canvas window
  263.             w_canvas->UnloadEmfFile(name);
  264.  
  265.             if (DeleteFile(name))
  266.             {
  267.                 name = GetDevFileName(name);
  268.  
  269.                 if (name)
  270.                     DeleteFile(name);
  271.  
  272.                 // remove string from listbox
  273.                 SendMessage(hwnd_emflist, LB_DELETESTRING, i, 0);
  274.             }
  275.             else
  276.                 MessageBeep(MB_ICONEXCLAMATION);
  277.         }
  278.     }
  279. }
  280.