home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1999 October / VPR9910A.BIN / OLS / unrar32005 / unrar32005.lzh / src / dialog.cxx < prev    next >
C/C++ Source or Header  |  1998-04-03  |  6KB  |  226 lines

  1. /*
  2.  *   Copyright (c) 1998 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 "comm-arc.h"
  20. #include "util.h"
  21. #include "dialog.h"
  22.  
  23. static void
  24. center_window (HWND hwnd)
  25. {
  26.   HWND owner = GetWindow (hwnd, GW_OWNER);
  27.   if (!owner)
  28.     owner = GetParent (hwnd);
  29.   if (!owner)
  30.     owner = GetDesktopWindow ();
  31.  
  32.   RECT dr, or;
  33.   GetWindowRect (hwnd, &dr);
  34.   GetWindowRect (owner, &or);
  35.  
  36.   LONG left = (or.left + (or.right - or.left) / 3
  37.                - (dr.right - dr.left) / 3);
  38.   LONG top = (or.top + (or.bottom - or.top) / 3
  39.               - (dr.bottom - dr.top) / 3);
  40.  
  41.   RECT work;
  42.   SystemParametersInfo (SPI_GETWORKAREA, 0, &work, 0);
  43.  
  44.   left = min (max (left, work.left), work.right - (dr.right - dr.left));
  45.   top = min (max (top, work.top), work.bottom - (dr.bottom - dr.top));
  46.  
  47.   SetWindowPos (hwnd, 0, left, top, 0, 0,
  48.                 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  49. }
  50.  
  51. static void
  52. set_size (HWND hwnd, int id, long size)
  53. {
  54.   char buf[32];
  55.   sprintf (buf, "%d bytes", size);
  56.   SetDlgItemText (hwnd, id, buf);
  57. }
  58.  
  59. static void
  60. set_date (HWND hwnd, int id, DWORD date)
  61. {
  62.   char buf[32];
  63.   sprintf (buf, "%d/%02d/%02d %02d:%02d:%02d",
  64.            (date >> 25) + 1980,
  65.            (date >> 21) & 15,
  66.            (date >> 16) & 31,
  67.            (date >> 11) & 31,
  68.            (date >> 5) & 63,
  69.            (date & 31) * 2);
  70.   SetDlgItemText (hwnd, id, buf);
  71. }
  72.  
  73. static BOOL CALLBACK
  74. replace_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  75. {
  76.   switch (msg)
  77.     {
  78.     case WM_INITDIALOG:
  79.       {
  80.         center_window (hwnd);
  81.         replace_param *rp = (replace_param *)lparam;
  82.         SetDlgItemText (hwnd, IDC_OLDNAME, rp->old_name);
  83.         set_size (hwnd, IDC_OLDSIZE, rp->old_size);
  84.         set_date (hwnd, IDC_OLDDATE, rp->old_date);
  85.         SetDlgItemText (hwnd, IDC_NEWNAME, rp->new_name);
  86.         set_size (hwnd, IDC_NEWSIZE, rp->new_size);
  87.         set_date (hwnd, IDC_NEWDATE, rp->new_date);
  88.         SendMessage (GetDlgItem (hwnd, IDC_Q), STM_SETICON,
  89.                      WPARAM (LoadIcon (0, IDI_QUESTION)), 0);
  90.         return 1;
  91.       }
  92.     case WM_COMMAND:
  93.       switch (LOWORD (wparam))
  94.         {
  95.         case IDYES:
  96.         case IDNO:
  97.         case IDC_ALL:
  98.         case IDCANCEL:
  99.           EndDialog (hwnd, LOWORD (wparam));
  100.           return 1;
  101.         }
  102.       break;
  103.     }
  104.   return 0;
  105. }
  106.  
  107. int
  108. replace_dialog (HWND hwnd, const replace_param &rp)
  109. {
  110.   return DialogBoxParam (lstate.hinst, MAKEINTRESOURCE (IDD_REPLACE),
  111.                          hwnd, replace_dlgproc, LPARAM (&rp));
  112. }
  113.  
  114. void
  115. doevents ()
  116. {
  117.   MSG msg;
  118.   while (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
  119.     {
  120.       TranslateMessage (&msg);
  121.       DispatchMessage (&msg);
  122.     }
  123. }
  124.  
  125. static BOOL CALLBACK
  126. progress_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  127. {
  128.   progress_dlg *p;
  129.   if (msg == WM_INITDIALOG)
  130.     {
  131.       SetWindowLong (hwnd, DWL_USER, lparam);
  132.       p = (progress_dlg *)lparam;
  133.       p->hwnd = hwnd;
  134.     }
  135.   else
  136.     p = (progress_dlg *)GetWindowLong (hwnd, DWL_USER);
  137.   return p ? p->wndproc (msg, wparam, lparam) : 0;
  138. }
  139.  
  140. BOOL
  141. progress_dlg::wndproc (UINT msg, WPARAM wparam, LPARAM lparam)
  142. {
  143.   switch (msg)
  144.     {
  145.     case WM_INITDIALOG:
  146.       center_window (hwnd);
  147.       return 1;
  148.  
  149.     case WM_COMMAND:
  150.       if (LOWORD (wparam) == IDCANCEL)
  151.         {
  152.           DestroyWindow (hwnd);
  153.           hwnd = 0;
  154.           return 1;
  155.         }
  156.       break;
  157.     }
  158.   return 0;
  159. }
  160.  
  161. int
  162. progress_dlg::create (HWND parent)
  163. {
  164.   if (!CreateDialogParam (lstate.hinst, MAKEINTRESOURCE (IDD_INPROG),
  165.                           parent, progress_dlgproc, LPARAM (this)))
  166.     return 0;
  167.   ShowWindow (hwnd, SW_SHOW);
  168.   doevents ();
  169.   return 1;
  170. }
  171.  
  172. int
  173. progress_dlg::init (const char *path, int nmax) const
  174. {
  175.   doevents ();
  176.   if (!hwnd)
  177.     return 0;
  178.   SendDlgItemMessage (hwnd, IDC_PROG, PBM_SETRANGE, 0, MAKELPARAM (0, nmax));
  179.   SendDlgItemMessage (hwnd, IDC_PROG, PBM_SETPOS, 0, 0);
  180.   SetDlgItemText (hwnd, IDC_NAME, path);
  181.   return 1;
  182. }
  183.  
  184. int
  185. progress_dlg::update (int n) const
  186. {
  187.   doevents ();
  188.   if (!hwnd)
  189.     return 0;
  190.   SendDlgItemMessage (hwnd, IDC_PROG, PBM_SETPOS, n, 0);
  191.   return 1;
  192. }
  193.  
  194. static char passwd[128];
  195.  
  196. static BOOL CALLBACK
  197. askpass_dlgproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  198. {
  199.   switch (msg)
  200.     {
  201.     case WM_INITDIALOG:
  202.       center_window (hwnd);
  203.       return 1;
  204.  
  205.     case WM_COMMAND:
  206.       switch (LOWORD (wparam))
  207.         {
  208.         case IDOK:
  209.           GetDlgItemText (hwnd, IDC_PASSWD, passwd, sizeof passwd - 1);
  210.           /* fall thru... */
  211.         case IDCANCEL:
  212.           EndDialog (hwnd, LOWORD (wparam));
  213.           return 1;
  214.         }
  215.       break;
  216.     }
  217.   return 0;
  218. }
  219.  
  220. char *
  221. askpass_dialog (HWND hwnd)
  222. {
  223.   return DialogBox (lstate.hinst, MAKEINTRESOURCE (IDD_ASKPASSWD),
  224.                     hwnd, askpass_dlgproc) == IDOK ? passwd : 0;
  225. }
  226.