/[winpt]/trunk/Src/wptProgressDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptProgressDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 270 - (show annotations)
Sat Oct 21 18:08:57 2006 UTC (18 years, 4 months ago) by twoaday
File size: 4336 byte(s)


1 /* wptProgressDlg.cpp - Support for the GPG progress status
2 * Copyright (C) 2003, 2004, 2005 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <windows.h>
25 #include <commctrl.h>
26
27 #include "resource.h"
28 #include "wptGPG.h"
29 #include "wptTypes.h"
30 #include "wptCommonCtl.h"
31 #include "wptW32API.h"
32 #include "wptContext.h"
33 #include "wptVersion.h"
34 #include "wptErrors.h"
35
36 /* It is not possible that there are two instance of a progress
37 thread, thus we use a 'const' handle for external synchronisation. */
38 static HANDLE curr_thread = NULL;
39
40 /* Dialog procedure to show the progress of an operation. */
41 static BOOL CALLBACK
42 progress_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
43 {
44 static progress_filter_s *pfx = NULL;
45
46 switch (msg) {
47 case WM_INITDIALOG:
48 pfx = (struct progress_filter_s *)lparam;
49 if (!pfx)
50 BUG (0);
51 pfx->dlg = dlg;
52 SendDlgItemMessage (dlg, IDC_PROGRESS_BAR,
53 PBM_SETRANGE, 0, MAKELPARAM (0, 100));
54 SetDlgItemInt (pfx->dlg, IDC_PROGRESS_TOTAL, pfx->total, FALSE);
55 SetDlgItemInt (pfx->dlg, IDC_PROGRESS_CURR, pfx->curr, FALSE);
56 SetDlgItemText (pfx->dlg, IDC_PROGRESS_WHAT, pfx->what);
57 center_window2 (dlg, NULL, HWND_TOPMOST);
58 center_window (dlg, NULL);
59 SetForegroundWindow (dlg);
60 return TRUE;
61 }
62 return FALSE;
63 }
64
65
66 /* Thread to start the progress dialog.
67 @opaque contains the context for the dialog. */
68 static DWORD WINAPI
69 progress_cb_thread (void *opaque)
70 {
71 struct progress_filter_s *pfx = (struct progress_filter_s *)opaque;
72
73 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PROGRESS,
74 pfx? pfx->hwnd : GetActiveWindow (),
75 progress_dlg_proc, (LPARAM)opaque);
76 ExitThread (0);
77 return TRUE;
78 }
79
80
81 /* Close the progress dialog (if still open) and close all handles in @pfx. */
82 void
83 progress_cleanup (progress_filter_s *pfx)
84 {
85 if (!pfx)
86 return;
87 if (pfx->dlg) {
88 EndDialog (pfx->dlg, TRUE);
89 pfx->dlg = NULL;
90 }
91 if (pfx->thread_hd) {
92 CloseHandle (pfx->thread_hd);
93 pfx->thread_hd = NULL;
94 }
95 curr_thread = NULL;
96 pfx->curr = 0;
97 pfx->total = 0;
98 pfx->error = 0;
99 memset (pfx, 0, sizeof *pfx);
100 }
101
102
103 /* Wait the given time @millis for the progress thread
104 but only if it is active. Return state of the thread.
105 Return value: 1 if signaled or done. */
106 int
107 wait_for_progress_cb (DWORD millis)
108 {
109 if (!curr_thread)
110 return 1;
111 if (WaitForSingleObject (curr_thread, millis) == WAIT_OBJECT_0)
112 return 1;
113 return 0;
114 }
115
116
117 /* Generic callback for operations with a progress bar. */
118 void
119 progress_callback (void *opaque, const char *what, int type, int off, int max)
120 {
121 struct progress_filter_s *pfx = (struct progress_filter_s *)opaque;
122 DWORD tid;
123
124 /*log_debug ("progress enter %d %d\r\n", off, max);*/
125 if (!pfx || pfx->error)
126 return;
127 if (what)
128 pfx->what = what;
129 pfx->curr = off;
130 pfx->total = max;
131 if (!off && !pfx->thread_hd) {
132 pfx->thread_hd = CreateThread (NULL, 0, progress_cb_thread,
133 (void *)pfx, 0, &tid);
134 if (!pfx->thread_hd) {
135 msg_box (NULL, _("Could not create progress thread."),
136 _("WinPT"), MB_ERR);
137 pfx->error = 1;
138 return;
139 }
140 curr_thread = pfx->thread_hd;
141 log_debug ("progress_callback: started thread %08lX\r\n", tid);
142 }
143 else if (pfx->dlg != NULL) {
144 size_t n = pfx->curr * 100 / pfx->total;
145 SendDlgItemMessage (pfx->dlg, IDC_PROGRESS_BAR,
146 PBM_SETPOS, (WPARAM)n, 0);
147 SetDlgItemInt (pfx->dlg, IDC_PROGRESS_CURR, pfx->curr, FALSE);
148 }
149 if (off >= max) {
150 log_debug ("progress_callback: off %lu max %lu\r\n", off, max);
151 progress_cleanup (pfx);
152 }
153 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26