home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 July / VPR0207A.ISO / OLS / UNRAR32007 / unrar32007.lzh / src.lzh / src / dialog.cxx < prev    next >
C/C++ Source or Header  |  2001-01-11  |  8KB  |  294 lines

  1. /*
  2.  *   Copyright (c) 1998-2001 T. Kamei (kamei@jsdlab.co.jp)
  3.  *
  4.  *   Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for any purpose is hereby granted provided
  6.  * that the above copyright notice and this permission notice appear
  7.  * in all copies of the software and related documentation.
  8.  *
  9.  *                          NO WARRANTY
  10.  *
  11.  *   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY WARRANTIES;
  12.  * WITHOUT EVEN THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS
  13.  * FOR A PARTICULAR PURPOSE.
  14.  */
  15.  
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <commctrl.h>
  19. #include <mbstring.h>
  20. #include "comm-arc.h"
  21. #include "util.h"
  22. #include "dialog.h"
  23.  
  24. static void
  25. center_window (HWND hwnd)
  26. {
  27.   HWND owner = GetWindow (hwnd, GW_OWNER);
  28.   if (!owner)
  29.     owner = GetParent (hwnd);
  30.   if (!owner)
  31.     owner = GetDesktopWindow ();
  32.  
  33.   RECT dr, or;
  34.   GetWindowRect (hwnd, &dr);
  35.   GetWindowRect (owner, &or);
  36.  
  37.   LONG left = (or.left + (or.right - or.left) / 3
  38.                - (dr.right - dr.left) / 3);
  39.   LONG top = (or.top + (or.bottom - or.top) / 3
  40.               - (dr.bottom - dr.top) / 3);
  41.  
  42.   RECT work;
  43.   SystemParametersInfo (SPI_GETWORKAREA, 0, &work, 0);
  44.  
  45.   left = min (max (left, work.left), work.right - (dr.right - dr.left));
  46.   top = min (max (top, work.top), work.bottom - (dr.bottom - dr.top));
  47.  
  48.   SetWindowPos (hwnd, 0, left, top, 0, 0,
  49.                 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  50. }
  51.  
  52. static void
  53. set_size (HWND hwnd, int id, long size)
  54. {
  55.   char buf[32];
  56.   sprintf (buf, "%d bytes", size);
  57.   SetDlgItemText (hwnd, id, buf);
  58. }
  59.  
  60. static void
  61. set_date (HWND hwnd, int id, DWORD date)
  62. {
  63.   char buf[32];
  64.   sprintf (buf, "%d/%02d/%02d %02d:%02d:%02d",
  65.            (date >> 25) + 1980,
  66.            (date >> 21) & 15,
  67.            (date >> 16) & 31,
  68.            (date >> 11) & 31,
  69.            (date >> 5) & 63,
  70.            (date & 31) * 2);
  71.   SetDlgItemText (hwnd, id, buf);
  72. }
  73.  
  74. static BOOL CALLBACK
  75. replace_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  76. {
  77.   switch (msg)
  78.     {
  79.     case WM_INITDIALOG:
  80.       {
  81.         center_window (hwnd);
  82.         replace_param *rp = (replace_param *)lparam;
  83.         SetDlgItemText (hwnd, IDC_OLDNAME, rp->old_name);
  84.         set_size (hwnd, IDC_OLDSIZE, rp->old_size);
  85.         set_date (hwnd, IDC_OLDDATE, rp->old_date);
  86.         SetDlgItemText (hwnd, IDC_NEWNAME, rp->new_name);
  87.         set_size (hwnd, IDC_NEWSIZE, rp->new_size);
  88.         set_date (hwnd, IDC_NEWDATE, rp->new_date);
  89.         SendMessage (GetDlgItem (hwnd, IDC_Q), STM_SETICON,
  90.                      WPARAM (LoadIcon (0, IDI_QUESTION)), 0);
  91.         return 1;
  92.       }
  93.     case WM_COMMAND:
  94.       switch (LOWORD (wparam))
  95.         {
  96.         case IDYES:
  97.         case IDNO:
  98.         case IDC_ALL:
  99.         case IDCANCEL:
  100.           EndDialog (hwnd, LOWORD (wparam));
  101.           return 1;
  102.         }
  103.       break;
  104.     }
  105.   return 0;
  106. }
  107.  
  108. int
  109. replace_dialog (HWND hwnd, const replace_param &rp)
  110. {
  111.   return DialogBoxParam (lstate.hinst, MAKEINTRESOURCE (IDD_REPLACE),
  112.                          hwnd, replace_dlgproc, LPARAM (&rp));
  113. }
  114.  
  115. void
  116. doevents ()
  117. {
  118.   MSG msg;
  119.   while (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
  120.     {
  121.       TranslateMessage (&msg);
  122.       DispatchMessage (&msg);
  123.     }
  124. }
  125.  
  126. static BOOL CALLBACK
  127. progress_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  128. {
  129.   progress_dlg *p;
  130.   if (msg == WM_INITDIALOG)
  131.     {
  132.       SetWindowLong (hwnd, DWL_USER, lparam);
  133.       p = (progress_dlg *)lparam;
  134.       p->hwnd = hwnd;
  135.     }
  136.   else
  137.     p = (progress_dlg *)GetWindowLong (hwnd, DWL_USER);
  138.   return p ? p->wndproc (msg, wparam, lparam) : 0;
  139. }
  140.  
  141. BOOL
  142. progress_dlg::wndproc (UINT msg, WPARAM wparam, LPARAM lparam)
  143. {
  144.   switch (msg)
  145.     {
  146.     case WM_INITDIALOG:
  147.       center_window (hwnd);
  148.       return 1;
  149.  
  150.     case WM_COMMAND:
  151.       if (LOWORD (wparam) == IDCANCEL)
  152.         {
  153.           DestroyWindow (hwnd);
  154.           hwnd = 0;
  155.           return 1;
  156.         }
  157.       break;
  158.     }
  159.   return 0;
  160. }
  161.  
  162. int
  163. progress_dlg::create (HWND parent)
  164. {
  165.   if (!CreateDialogParam (lstate.hinst, MAKEINTRESOURCE (IDD_INPROG),
  166.                           parent, progress_dlgproc, LPARAM (this)))
  167.     return 0;
  168.   ShowWindow (hwnd, SW_SHOW);
  169.   doevents ();
  170.   return 1;
  171. }
  172.  
  173. int
  174. progress_dlg::init (const char *path, int nmax)
  175. {
  176.   doevents ();
  177.   if (!hwnd)
  178.     return 0;
  179.   max_bytes = nmax;
  180.   SendDlgItemMessage (hwnd, IDC_PROG, PBM_SETRANGE, 0, MAKELPARAM (0, PROGRESS_MAX));
  181.   SendDlgItemMessage (hwnd, IDC_PROG, PBM_SETPOS, 0, 0);
  182.   SetDlgItemText (hwnd, IDC_NAME, path);
  183.   return 1;
  184. }
  185.  
  186. int
  187. progress_dlg::update (int n) const
  188. {
  189.   doevents ();
  190.   if (!hwnd)
  191.     return 0;
  192.   SendDlgItemMessage (hwnd, IDC_PROG, PBM_SETPOS,
  193.                       max_bytes ? int (PROGRESS_MAX * double (n) / max_bytes) : PROGRESS_MAX,
  194.                       0);
  195.   return 1;
  196. }
  197.  
  198. static char passwd[128];
  199.  
  200. static BOOL CALLBACK
  201. askpass_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  202. {
  203.   switch (msg)
  204.     {
  205.     case WM_INITDIALOG:
  206.       center_window (hwnd);
  207.       return 1;
  208.  
  209.     case WM_COMMAND:
  210.       switch (LOWORD (wparam))
  211.         {
  212.         case IDOK:
  213.           GetDlgItemText (hwnd, IDC_PASSWD, passwd, sizeof passwd - 1);
  214.           /* fall thru... */
  215.         case IDCANCEL:
  216.           EndDialog (hwnd, LOWORD (wparam));
  217.           return 1;
  218.         }
  219.       break;
  220.     }
  221.   return 0;
  222. }
  223.  
  224. char *
  225. askpass_dialog (HWND hwnd)
  226. {
  227.   return DialogBox (lstate.hinst, MAKEINTRESOURCE (IDD_ASKPASSWD),
  228.                     hwnd, askpass_dlgproc) == IDOK ? passwd : 0;
  229. }
  230.  
  231. static char *vol_name;
  232.  
  233. static BOOL CALLBACK
  234. changevol_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  235. {
  236.   switch (msg)
  237.     {
  238.     case WM_INITDIALOG:
  239.       center_window (hwnd);
  240.       SetDlgItemText (hwnd, IDC_PATH, vol_name);
  241.       return 1;
  242.  
  243.     case WM_COMMAND:
  244.       switch (LOWORD (wparam))
  245.         {
  246.         case IDC_REF:
  247.           {
  248.             char buf[MAX_PATH];
  249.             char filter[256];
  250.             LoadString (lstate.hinst, IDS_FILTER, filter, sizeof filter);
  251.             for (char *p = filter; (p = (char *)_mbschr ((u_char *)p, '|')); p++)
  252.               *p = 0;
  253.             GetDlgItemText (hwnd, IDC_PATH, buf, sizeof buf);
  254.             OPENFILENAME of;
  255.             memset (&of, 0, sizeof of);
  256.             of.lStructSize = sizeof of;
  257.             of.hwndOwner = hwnd;
  258.             of.lpstrFilter = filter;
  259.             of.nFilterIndex = 1;
  260.             of.lpstrFile = buf;
  261.             of.nMaxFile    = sizeof buf;
  262.             of.Flags = (OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR
  263.                         | OFN_PATHMUSTEXIST | OFN_EXPLORER);
  264.             if (GetOpenFileName (&of))
  265.               SetDlgItemText (hwnd, IDC_PATH, buf);
  266.             return 1;
  267.           }
  268.  
  269.         case IDOK:
  270.           {
  271.             char buf[MAX_PATH];
  272.             GetDlgItemText (hwnd, IDC_PATH, buf, sizeof buf);
  273.             if (!*buf)
  274.               return 1;
  275.             strcpy (vol_name, buf);
  276.           }
  277.           /* fall thru... */
  278.         case IDCANCEL:
  279.           EndDialog (hwnd, LOWORD (wparam));
  280.           return 1;
  281.         }
  282.       break;
  283.     }
  284.   return 0;
  285. }
  286.  
  287. int
  288. change_vol_dialog (HWND hwnd, char *path)
  289. {
  290.   vol_name = path;
  291.   return DialogBox (lstate.hinst, MAKEINTRESOURCE (IDD_CHANGEVOL),
  292.                     hwnd, changevol_dlgproc) == IDOK;
  293. }
  294.