home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_16 / EMFScope / TOOLBAR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-10  |  12.8 KB  |  533 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   : toolbar.cpp                                                         //
  10. //  Description: EMFScope toolbar                                                    //
  11. //  Version    : 1.00.001, July 10, 2000                                             //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #include <windows.h>
  16. #include <commctrl.h>
  17. #include <assert.h>
  18.  
  19. #include "Winpp.h"
  20.  
  21. #include "spehon32\\spehon32.h"
  22. #include "resource.h"
  23.  
  24. #include "canvas.h"
  25. #include "toolbar.h"
  26. #include "Spoolfil.h"
  27. #include "emfscope.h"    // for EmfScope.hMainMenu
  28.  
  29.  
  30. KSpoolFile SpoolFile;
  31.  
  32.  
  33. BOOL KToolBar::DlgProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  34. {
  35.     switch (uMessage)
  36.     {
  37.         case WM_INITDIALOG:
  38.             m_hWnd = hWnd;
  39.             FileList.Create(hCurInst, hWnd, GetSubMenu(EmfScope.hMainMenu, 2));
  40.             return TRUE;
  41.         
  42.         case WM_COMMAND:
  43.             switch (LOWORD(wParam))
  44.             {
  45.                 case IDC_SCL_11:
  46.                 case IDC_SCL_12:
  47.                 case IDC_SCL_13:
  48.                 case IDC_SCL_14:
  49.                 case IDC_SCL_15:
  50.                 case IDC_SCL_16:
  51.                     SetScaleColor(LOWORD(wParam) - IDC_SCL_11 + 1, -1);
  52.                         
  53.                     CheckRadioButton(hWnd, IDC_SCL_11, IDC_SCL_16, LOWORD(wParam));
  54.                     return TRUE;
  55.                     
  56.                 case IDC_WHITE:
  57.                 case IDC_GRAY:
  58.                 case IDC_YELLOW:
  59.                     SetScaleColor(0, LOWORD(wParam)-IDC_WHITE);
  60.                     CheckRadioButton(hWnd, IDC_WHITE, IDC_YELLOW, LOWORD(wParam));
  61.                     break;
  62.  
  63.                 case IDC_EMFLIST:
  64.                     if (HIWORD(wParam)==LBN_DBLCLK)
  65.                     {
  66.                         FileList.Open();
  67.                         return TRUE;
  68.                     }
  69.                     return FALSE;
  70.                     break;
  71.  
  72.                 case IDC_PRINT:
  73.                     if (w_canvas)
  74.                         w_canvas->Print();
  75.                     return TRUE;
  76.  
  77.                 case IDC_OPEN:
  78.                     if (w_canvas)
  79.                     {
  80.                         const char *filename;
  81.                         
  82.                         filename = w_canvas->OpenFile();
  83.  
  84.                         if (filename)
  85.                             FileList.AddtoFileList(filename);
  86.                     }
  87.                     break;
  88.  
  89.                 case IDC_SETUP:            
  90.                     Setup();                
  91.                     break;
  92.  
  93.                 default:
  94.                     return FALSE;
  95.             }
  96.  
  97.         case WM_USER:
  98.             if (!PrivateMessage(wParam, lParam))
  99.                 return FALSE;
  100.  
  101.             if (b_showemf)
  102.             {
  103.                 b_showemf = FALSE;
  104.  
  105.                 if (w_canvas)
  106.                     w_canvas->LoadEmfFile(s_filename.string());
  107.             }                
  108.             return TRUE;
  109.             break;
  110.     
  111.         case WM_COPYDATA:
  112.             return SpoolFile.ReceiveData((COPYDATASTRUCT *)lParam);
  113.             break;
  114.  
  115.         default:
  116.             return FALSE;
  117.     }
  118. }
  119.  
  120.  
  121. const COLORREF TinyPalette[] =
  122.     {
  123.         RGB(0xFF, 0xFF, 0xFF),    // white
  124.         RGB(0x80, 0x80, 0x80),      // gray
  125.         RGB(0xFF, 0xFF, 0xC0)     // yellow+gray
  126.     };
  127.  
  128.  
  129. void KToolBar::SetScaleColor(int scale, int color)
  130. {
  131.     // if incoming scale==0, don't change, but communicate to Canvas window
  132.     if (scale)
  133.         n_scale = scale;
  134.  
  135.     if ( (color>=0) && (color<=2) )
  136.         n_color = color;
  137.     
  138.     if (w_canvas)
  139.         w_canvas->SetScaleColor(n_scale, TinyPalette[n_color]);
  140. }
  141.  
  142.  
  143. void KToolBar::SetCanvas(KCanvasWindow *canvas)
  144. {
  145.     w_canvas = canvas;
  146.  
  147.     FileList.SetCanvas(canvas);
  148.     w_canvas->SetScaleColor(n_scale, TinyPalette[n_color]);
  149.     w_canvas->SetDelay(n_delay);
  150. }
  151.  
  152. const char nam_profile    [] = "emfscope.ini";
  153.  
  154. const char sec_emfscope   [] = "EmfScope";
  155. const char key_scale      [] = "Scale";
  156. const char key_saveemf    [] = "SaveEmf";
  157. const char key_midview    [] = "MidView";
  158. const char key_backcolor  [] = "BackColor";
  159. const char key_destdir    [] = "DestDir";
  160. const char key_delay      [] = "Delay";
  161.  
  162. const char sec_recentfiles[] = "RecentFiles";
  163.  
  164. KToolBar::~KToolBar()
  165. {
  166.     DeleteObject(hi_print);
  167.     DeleteObject(hi_open);
  168.     DeleteObject(hi_setup);
  169. }
  170.  
  171. const int max_recentfiles = 64;
  172. const int max_delay       = 16;
  173.  
  174. KToolBar::KToolBar(HINSTANCE hInst, HWND hWnd)
  175. {
  176.     RECT    rect;
  177.  
  178.     w_canvas    = NULL;
  179.  
  180.     hCurInst    = hInst;
  181.  
  182.     b_showemf   = FALSE;
  183.  
  184.     n_pageno    = 0;
  185.     n_scale     = 1;
  186.     n_color     = 0;
  187.  
  188.     hi_print    = LoadIcon(hInst, MAKEINTRESOURCE(IDI_PRINT));
  189.     hi_open     = LoadIcon(hInst, MAKEINTRESOURCE(IDI_OPEN));
  190.     hi_setup    = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SETUP));
  191.  
  192.     hwnd_dialog   = Createdialog(hInst, IDD_TOOLBAR, hWnd);
  193.  
  194.     hwnd_activity = GetDlgItem(hwnd_dialog, IDC_ACTION);
  195.     hwnd_title    = GetDlgItem(hwnd_dialog, IDC_TITLE);
  196.     hwnd_time     = GetDlgItem(hwnd_dialog, IDC_TIME);
  197.  
  198.     SendDlgItemMessage(hwnd_dialog, IDC_OPEN,  BM_SETIMAGE, IMAGE_ICON, (LPARAM)hi_open);
  199.     SendDlgItemMessage(hwnd_dialog, IDC_PRINT, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hi_print);
  200.     SendDlgItemMessage(hwnd_dialog, IDC_SETUP, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hi_setup);
  201.  
  202.     GetWindowRect(hwnd_dialog, &rect);
  203.     n_height = unmapy(rect.bottom - rect.top);
  204.     
  205.     n_width  = rect.left;
  206.     GetWindowRect(GetDlgItem(hwnd_dialog, IDC_SCALE), &rect);
  207.     // report width up to the left boundary of scale group box to hide them first
  208.     n_width  = unmapx(rect.left - n_width)- 6;
  209.     
  210.     n_scale = GetPrivateProfileInt(sec_emfscope, key_scale, -1, nam_profile);
  211.     // check for first usage, or invalid value
  212.     if ( (n_scale<1) || (n_scale>6) )
  213.     {
  214.         n_scale = 3;     // read default scale is 3
  215.     }
  216.     
  217.     CheckRadioButton(hwnd_dialog, IDC_SCL_11, IDC_SCL_16, IDC_SCL_11 + n_scale - 1);
  218.  
  219.     b_SaveEmf = GetPrivateProfileInt(sec_emfscope, key_saveemf, 1, nam_profile)==1;
  220.     b_MidView = GetPrivateProfileInt(sec_emfscope, key_midview, 1, nam_profile)==1;
  221.  
  222.     n_color = GetPrivateProfileInt(sec_emfscope, key_backcolor, 2, nam_profile);
  223.     n_color %= 3;        // 0, 1, 2
  224.     CheckRadioButton(hwnd_dialog, IDC_WHITE, IDC_YELLOW, IDC_WHITE + n_color);
  225.  
  226.     n_delay = GetPrivateProfileInt(sec_emfscope, key_delay, 0, nam_profile);
  227.     if (n_delay>max_delay)
  228.         n_delay = max_delay;
  229.  
  230.     if (!GetPrivateProfileString(sec_emfscope, key_destdir, NULL,
  231.                                  DestDir, sizeof(DestDir), nam_profile))
  232.         GetTempPath(sizeof(DestDir), DestDir);
  233.  
  234.     for (int i=1; i<=max_recentfiles; i++)
  235.     {
  236.         char     filename[256];
  237.         OFSTRUCT ofs;
  238.         int      len;
  239.         char     key_no[5];
  240.  
  241.         wsprintf(key_no, "%d", i);
  242.  
  243.         len = GetPrivateProfileString(sec_recentfiles, key_no, NULL, 
  244.                                         filename, sizeof(filename), nam_profile);
  245.  
  246.         if (len==0) break;
  247.         
  248.         if (OpenFile(filename, &ofs, OF_EXIST)!=HFILE_ERROR)
  249.             FileList.AddtoFileList(filename);
  250.     }
  251. }
  252.  
  253.  
  254. BOOL IsValidDir(const char *path)
  255. {
  256.     char CurDir[MAX_PATH];
  257.     BOOL rslt;
  258.  
  259.     GetCurrentDirectory(sizeof(CurDir), CurDir);
  260.  
  261.     rslt = SetCurrentDirectory(path);
  262.  
  263.     SetCurrentDirectory(CurDir);
  264.  
  265.     return rslt;
  266. }
  267.  
  268.  
  269. class KSetupDialog : public KModalDialog
  270. {
  271. public:
  272.     KToolBar * toolbar;
  273.     
  274.     BOOL DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  275. };
  276.  
  277.  
  278. BOOL KSetupDialog::DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  279. {
  280.     switch (uMsg)
  281.     {
  282.         case WM_INITDIALOG:
  283.             m_hWnd = hWnd;
  284.             
  285.             CheckDlgButton(IDC_SAVEEMF, toolbar->b_SaveEmf);
  286.             CheckDlgButton(IDC_MIDVIEW, toolbar->b_MidView);
  287.             SetDlgItemText(hWnd, IDC_DESTDIR, toolbar->DestDir);
  288.         
  289.             SendDlgItemMessage(hWnd, IDC_SPEED, TBM_SETRANGE, TRUE, MAKELPARAM(0, max_delay));
  290.             SendDlgItemMessage(hWnd, IDC_SPEED, TBM_SETPOS, TRUE, toolbar->n_delay);
  291.             break;
  292.  
  293.         case WM_COMMAND:
  294.             switch (LOWORD(wParam))
  295.             {
  296.                 case IDOK:
  297.                     {
  298.                         char NewDir[MAX_PATH];
  299.  
  300.                         GetDlgItemText(hWnd, IDC_DESTDIR, NewDir, sizeof(NewDir));
  301.  
  302.                         // if different
  303.                         if (strcmp(NewDir, toolbar->DestDir))
  304.                             if (IsValidDir(NewDir))
  305.                             {
  306.                                 if (NewDir[strlen(NewDir)-1]!='\\')
  307.                                     strcat(NewDir, "\\");
  308.                                 strcpy(toolbar->DestDir, NewDir);
  309.                             }
  310.                             else
  311.                                 MessageBox(0, "Invalid directory", LoadStringTemp(IDS_APPTITLE), MB_OK);
  312.                     }
  313.                         
  314.                     toolbar->b_SaveEmf = IsDlgButtonChecked(hWnd, IDC_SAVEEMF);
  315.                     toolbar->b_MidView = IsDlgButtonChecked(hWnd, IDC_MIDVIEW);
  316.  
  317.                     EndDialog(hWnd, IDOK);
  318.                     break;
  319.  
  320.                 default:
  321.                     return FALSE;
  322.             }
  323.             break;
  324.  
  325.         case WM_HSCROLL:
  326.         {
  327.             int delay = toolbar->n_delay;
  328.  
  329.             switch (LOWORD(wParam))
  330.             {
  331.                 case TB_TOP          : delay =  0;              break;
  332.                 case TB_BOTTOM       : delay =  max_delay;      break;
  333.                 case TB_PAGEUP       : delay -= max_delay/4;    break;
  334.                 case TB_PAGEDOWN     : delay += max_delay/4;    break;
  335.                 case TB_LINEUP       : delay --;                break;
  336.                 case TB_LINEDOWN     : delay ++;                break;
  337.                 case TB_THUMBPOSITION: delay =  HIWORD(wParam);    break;
  338.             }
  339.  
  340.             if (delay<0)
  341.                 delay = 0;
  342.             else if (delay>max_delay)
  343.                 delay = max_delay;
  344.  
  345.             if (toolbar->n_delay!=delay)
  346.             {
  347.                 toolbar->n_delay = delay;
  348.                 SendDlgItemMessage(hWnd, IDC_SPEED, TBM_SETPOS, TRUE, toolbar->n_delay);
  349.             }
  350.         }
  351.         break;
  352.  
  353.         default:
  354.             return FALSE;
  355.     }
  356.  
  357.     return TRUE;
  358. }
  359.  
  360.  
  361. void KToolBar::Setup(void)
  362. {
  363.     KSetupDialog dlg;
  364.  
  365.     dlg.toolbar = this;
  366.     dlg.Dialogbox(hCurInst, IDD_SETUP, m_hWnd);
  367.  
  368.     if (w_canvas)
  369.         w_canvas->SetDelay(n_delay);
  370. }
  371.  
  372.  
  373. void KToolBar::SaveSettings(void)
  374. {
  375.     char number[12];
  376.     int  count;
  377.  
  378.     wsprintf(number, "%d", n_scale);
  379.     WritePrivateProfileString(sec_emfscope, key_scale, number, nam_profile);
  380.  
  381.     wsprintf(number, "%d", b_SaveEmf);
  382.     WritePrivateProfileString(sec_emfscope, key_saveemf, number, nam_profile);
  383.  
  384.     wsprintf(number, "%d", b_MidView);
  385.     WritePrivateProfileString(sec_emfscope, key_midview, number, nam_profile);
  386.  
  387.     wsprintf(number, "%d", n_color);
  388.     WritePrivateProfileString(sec_emfscope, key_backcolor, number, nam_profile);
  389.  
  390.     WritePrivateProfileString(sec_emfscope, key_destdir, DestDir, nam_profile);
  391.  
  392.     wsprintf(number, "%d", n_delay);
  393.     WritePrivateProfileString(sec_emfscope, key_delay, number, nam_profile);
  394.     
  395.     count = FileList.GetCount();
  396.     
  397.     for (int i = max(0, count-max_recentfiles); i<=count; i++)
  398.     {
  399.         wsprintf(number, "%d", i+1);
  400.  
  401.         WritePrivateProfileString(sec_recentfiles, number, 
  402.             i==count ? NULL :    FileList.GetEmfFileName(i), 
  403.             nam_profile);
  404.     }
  405.  
  406. }
  407.  
  408.  
  409. BOOL KToolBar::SaveEmfFile(const char *filename)
  410. {
  411.     FileList.AddtoFileList(filename);
  412.  
  413.     if (b_SaveEmf || b_MidView)
  414.     {
  415.         char *p;
  416.         char newfilename[MAX_PATH];
  417.         char name[5];
  418.         BOOL rslt;
  419.  
  420.         strcpy(newfilename, filename);
  421.  
  422.         // find the actual file name within the full path
  423.         p = newfilename;
  424.         while (strchr(p, '\\')!=NULL)
  425.             p = strchr(p, '\\') + 1;
  426.         
  427.         // p = & ~EMFxxxx.TMP
  428.         if (strncmp(p, "~EMF", 4))
  429.             return FALSE;
  430.  
  431.         *p = 0;       // newfilename will contain the path without the filename
  432.         strncpy(name, p+4, 4); // xxxx
  433.         name[4] = 0;
  434.  
  435. #ifndef KEEPOLDDIR
  436.         strcpy(newfilename, DestDir);
  437. #endif
  438.         strcat(newfilename, name);
  439.         strcat(newfilename, ".emf");  // path xxx.emf
  440.  
  441.         PrivateMessage(id_activity, act_copy);
  442.  
  443.         rslt = CopyFile(filename, newfilename, TRUE);
  444.  
  445.         if ( !rslt && (GetLastError()==ERROR_FILE_EXISTS) )
  446.         if (MessageBox(NULL, 
  447.                        "Fill already exists.\nDo you want to overwrite?",
  448.                        "Save Emf File",
  449.                        MB_YESNO | MB_ICONQUESTION) == IDYES)
  450.             rslt = CopyFile(filename, newfilename, FALSE);
  451.         
  452.         if (rslt)
  453.         {
  454.             SpoolFile.GenDevFile(newfilename);
  455.  
  456.             strcpy(s_filename.string(), newfilename);
  457.             FileList.ReplaceLastName(s_filename.string());
  458.  
  459.             b_showemf = b_MidView;
  460.         }
  461.         
  462.         PrivateMessage(id_activity, act_done);
  463.  
  464.         return rslt;
  465.     }
  466.     else
  467.         return FALSE;
  468. }
  469.  
  470.  
  471. void KToolBar::ReportTime(DWORD tick)
  472. {
  473.     char temp[256];
  474.  
  475.     n_pageno ++;
  476.  
  477.     wsprintf(temp, "%d. %s (%d.%02d s)", n_pageno, s_filename.string(), tick/1000, tick%1000/10);
  478.     
  479.     FileList.ReplaceLastName(temp);
  480. }
  481.  
  482.  
  483. BOOL KToolBar::PrivateMessage(int id, LPARAM lParam)
  484. {
  485.     switch (id)
  486.     {
  487.         case id_emffile:
  488.             if (s_filename.Append(lParam))
  489.                 SaveEmfFile(s_filename.string());
  490.             break;
  491.  
  492.         case id_title:
  493.             if (s_title.Append(lParam))
  494.                 SetWindowText(hwnd_title, s_title.string());
  495.             break;
  496.  
  497.         case id_device:
  498.             if (s_device.Append(lParam))
  499.             {
  500.                 char WinText[128];
  501.  
  502.                 wsprintf(WinText, LoadStringTemp(IDS_APPTITLE), s_device.string());
  503.                 SetWindowText(GetParent(hwnd_dialog), WinText);
  504.             }
  505.             break;
  506.  
  507.         case id_activity:
  508.             {
  509.                 const char *p = NULL;
  510.  
  511.                 switch (lParam)
  512.                 {
  513.                     case act_done   : p = NULL;                        break;
  514.                     case act_despool: p = LoadStringTemp(IDS_DESPOOL); break;
  515.                     case act_delete : p = LoadStringTemp(IDS_DELETE);  break;
  516.                     case act_copy   : p = LoadStringTemp(IDS_COPY);    break;                             
  517.                 }
  518.                 SetWindowText(hwnd_activity, p);
  519.             } 
  520.  
  521.             break;
  522.  
  523.         case id_time:
  524.             ReportTime(lParam);
  525.             break;
  526.  
  527.         default:
  528.             return FALSE;
  529.     }
  530.     
  531.     return TRUE;
  532. }
  533.