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

Annotation of /trunk/Src/wptProgressDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 244 - (hide annotations)
Mon Jul 3 14:10:21 2006 UTC (18 years, 8 months ago) by twoaday
File size: 4255 byte(s)


1 werner 36 /* 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 werner 47 #include "resource.h"
28 werner 36 #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 twoaday 229 /* 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 werner 36
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 twoaday 229 static progress_filter_s *pfx = NULL;
45 werner 36
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 twoaday 229 curr_thread = NULL;
96 werner 36 pfx->curr = 0;
97     pfx->total = 0;
98     pfx->error = 0;
99 twoaday 229 memset (pfx, 0, sizeof *pfx);
100 werner 36 }
101    
102    
103 twoaday 244 /* 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 twoaday 229 {
109     if (!curr_thread)
110 twoaday 244 return 1;
111     if (WaitForSingleObject (curr_thread, millis) == WAIT_OBJECT_0)
112     return 1;
113     return 0;
114 twoaday 229 }
115    
116    
117 werner 36 /* 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 twoaday 244 /*log_debug ("progress enter %d %d\r\n", off, max);*/
125 werner 36 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 twoaday 229 pfx->thread_hd = CreateThread (NULL, 0, progress_cb_thread,
133     (void *)pfx, 0, &tid);
134 werner 36 if (!pfx->thread_hd) {
135     msg_box (NULL, _("Could not create progress thread."), _("WinPT"), MB_ERR);
136     pfx->error = 1;
137     return;
138     }
139 twoaday 229 curr_thread = pfx->thread_hd;
140 werner 36 log_debug ("progress_callback: started thread %08lX\r\n", tid);
141     }
142     else if (pfx->dlg != NULL) {
143     size_t n = pfx->curr * 100 / pfx->total;
144 twoaday 229 SendDlgItemMessage (pfx->dlg, IDC_PROGRESS_BAR,
145     PBM_SETPOS, (WPARAM)n, 0);
146 werner 36 SetDlgItemInt (pfx->dlg, IDC_PROGRESS_CURR, pfx->curr, FALSE);
147     }
148 twoaday 229 if (off >= max)
149 werner 36 progress_cleanup (pfx);
150     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26