more-rx-updates-20060504
[openafs.git] / src / WINNT / afssvrcfg / toolbox.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * INCLUDES ___________________________________________________________________
12  *
13  */
14 #include <winsock2.h>
15 #include <ws2tcpip.h>
16
17 extern "C" {
18 #include <afs/param.h>
19 #include <afs/stds.h>
20 }
21
22 #include "afscfg.h"
23 #include <WINNT\afsapplib.h>
24 #include <stdio.h>
25 #include "toolbox.h"
26
27
28 /*
29  * EXPORTED FUNCTIONS _________________________________________________________
30  *
31  */
32 void SetEnable(HWND hDlg, UINT nControl, ENABLE_STATE eState)
33 {
34         HWND hControl = GetDlgItem(hDlg, nControl);
35
36         BOOL bEnable;
37
38         switch (eState) {
39                 case ES_ENABLE:         bEnable = TRUE;
40                                                         break;
41
42                 case ES_DISABLE:        bEnable = FALSE;
43                                                         break;
44
45                 case ES_TOGGLE:         bEnable = !IsWindowEnabled(hControl);
46                                                         break;
47         }
48         
49         EnableWindow(hControl, bEnable);
50 }
51
52 void SetElapsedTime(HWND hwnd, DWORD nCtrlID, ULONG ulMin, ULONG ulMax, ULONG ulTime)
53 {
54         SYSTEMTIME stMin;
55         SET_ELAPSED_TIME_FROM_SECONDS (&stMin, ulMin);
56
57         SYSTEMTIME stMax;
58         SET_ELAPSED_TIME_FROM_SECONDS (&stMax, ulMax);
59
60         SYSTEMTIME st;
61         SET_ELAPSED_TIME_FROM_SECONDS (&st, ulTime);
62
63         HWND hElapsed = ::GetDlgItem(hwnd, nCtrlID);
64         EL_SetRange (hElapsed, &stMin, &stMax);
65         EL_SetTime (hElapsed, &st);
66 }
67
68 void GetElapsedTime(HWND hwnd, DWORD nCtrlID, DWORD& dwTime)
69 {
70         SYSTEMTIME stFinal;
71         
72         HWND hElapsed = ::GetDlgItem(hwnd, nCtrlID);
73         EL_GetTime (hElapsed, &stFinal);
74         
75         dwTime = GET_SECONDS_FROM_ELAPSED_TIME(&stFinal);
76 }
77
78 LPCTSTR SecondsToElapsedTime(UINT uiNumSeconds)
79 {
80         static TCHAR szTime[64], sz[32];
81
82         *szTime = 0;
83         *sz = 0;
84
85         int nHours, nMinutes, nSeconds;
86
87         nHours = uiNumSeconds / 3600;
88         nMinutes = (uiNumSeconds % 3600) / 60;
89         nSeconds = (uiNumSeconds % 3600) % 60;
90
91         if (nHours)
92                 _stprintf(szTime, TEXT("%d hours"), nHours);
93
94         if (nMinutes) {
95                 if (nHours)
96                         lstrcat(szTime, TEXT(", "));
97                 _stprintf(sz, TEXT("%d minutes"), nMinutes); 
98                 lstrcat(szTime, sz);
99         }
100
101         if (nSeconds) {
102                 if (nHours || nMinutes)
103                         lstrcat(szTime, TEXT(", "));
104                 _stprintf(sz, TEXT("%d seconds"), nSeconds);
105                 lstrcat(szTime, sz);
106         }
107
108         return szTime;
109 }
110
111 BOOL IsButtonChecked(HWND hDlg, UINT uiCtrlID)
112 {
113         return SendMessage(GetDlgItem(hDlg, uiCtrlID), BM_GETCHECK, 0, 0) == BST_CHECKED;
114 }
115
116 int GetButtonState(HWND hDlg, UINT uiCtrlID)
117 {
118         return SendMessage(GetDlgItem(hDlg, uiCtrlID), BM_GETCHECK, 0, 0);
119 }
120
121 void SetCheck(HWND hDlg, UINT uiCtrlID, int nChecked)
122 {
123         SendMessage(GetDlgItem(hDlg, uiCtrlID), BM_SETCHECK, nChecked, 0);
124 }
125
126 TCHAR *GetResString(UINT nMsgID, TCHAR *pszMsg, UINT nLen)
127 {
128         GetString(pszMsg, nMsgID, nLen);
129
130         return pszMsg;
131 }
132
133 void ShowWnd(HWND hDlg, UINT uiCtrlID, BOOL bShow)
134 {
135         ShowWindow(GetDlgItem(hDlg, uiCtrlID), bShow ? SW_SHOW : SW_HIDE);
136 }
137
138 void EnableWnd(HWND hDlg, UINT uiCtrlID, BOOL bEnable)
139 {
140         EnableWindow(GetDlgItem(hDlg, uiCtrlID), bEnable);
141 }
142
143 void SetWndText(HWND hDlg, UINT uiCtrlID, LPCTSTR pszMsg)
144 {
145         SetWindowText(GetDlgItem(hDlg, uiCtrlID), pszMsg);
146 }
147
148 void SetWndText(HWND hDlg, UINT uiCtrlID, UINT nMsgID)
149 {
150         TCHAR szMsg[cchRESOURCE];
151
152         GetString(szMsg, nMsgID);
153         
154         SetWndText(hDlg, uiCtrlID, szMsg);
155 }
156
157 TCHAR *GetWndText(HWND hDlg, UINT uiCtrlID, TCHAR *pszTextBuffer, int nTextLen)
158 {
159         GetWindowText(GetDlgItem(hDlg, uiCtrlID), pszTextBuffer, nTextLen);
160
161         return pszTextBuffer;
162 }
163
164 void ForceUpdateWindow(HWND hWnd)
165 {
166         InvalidateRect(hWnd, 0, TRUE);
167         UpdateWindow(hWnd);
168 }
169
170 void ForceUpdateWindow(HWND hDlg, UINT uiCtrlID)
171 {
172         ForceUpdateWindow(GetDlgItem(hDlg, uiCtrlID));
173 }
174
175 int AddLBString(HWND hDlg, UINT uiCtrlID, LPCTSTR pszString)
176 {
177         return SendMessage(GetDlgItem(hDlg, uiCtrlID), LB_ADDSTRING, 0, (LONG)pszString);
178 }
179
180 int ClearListBox(HWND hDlg, UINT uiCtrlID)
181 {
182         return SendMessage(GetDlgItem(hDlg, uiCtrlID), LB_RESETCONTENT, 0, 0);
183 }
184
185 void SetUpDownRange(HWND hDlg, UINT uiCtrlID, int nMinVal, int nMaxVal)
186 {
187         SendMessage(GetDlgItem(hDlg, uiCtrlID), UDM_SETRANGE, 0, (LPARAM)MAKELONG((short)nMaxVal, (short)nMinVal));
188 }
189
190 void MakeBold(HWND hWnd)
191 {
192         HFONT hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0);
193
194         LOGFONT logFont;
195         
196         GetObject(hFont, sizeof(LOGFONT), &logFont);
197
198         logFont.lfWeight = FW_BOLD;
199
200         HFONT hNewFont = CreateFontIndirect(&logFont);
201         ASSERT(hNewFont);
202
203         SendMessage(hWnd, WM_SETFONT, (WPARAM)hNewFont, MAKELPARAM(TRUE, 0));
204 }
205
206 int MsgBox(HWND hParent, UINT uiTextID, UINT uiCaptionID, UINT uType)
207 {
208         TCHAR szText[cchRESOURCE];
209         TCHAR szCaption[cchRESOURCE];
210
211         GetString(szText, uiTextID);
212         GetString(szCaption, uiCaptionID);
213
214         return MessageBox(hParent, szText, szCaption, uType);
215 }
216
217 void HideAndDisable(HWND hDlg, UINT uiCtrlID)
218 {
219         ShowWnd(hDlg, uiCtrlID, SW_HIDE);
220         SetEnable(hDlg, uiCtrlID, ES_DISABLE);
221 }
222
223 void ShowAndEnable(HWND hDlg, UINT uiCtrlID, BOOL bShowAndEnable)
224 {
225         int nShow = SW_SHOW;
226         ENABLE_STATE es = ES_ENABLE;
227
228         if (!bShowAndEnable) {
229                 nShow = SW_HIDE;
230                 es = ES_DISABLE;
231         }
232
233         ShowWnd(hDlg, uiCtrlID, nShow);
234         SetEnable(hDlg, uiCtrlID, es);
235 }
236
237 void MoveWnd(HWND hDlg, UINT nCtrlID, int xOffset, int yOffset)
238 {
239         HWND hCtrl = GetDlgItem(hDlg, nCtrlID);
240         
241         RECT rect;
242         GetWindowRect(hCtrl, &rect);
243         
244         POINT p1, p2;
245         p1.x = rect.left;
246         p1.y = rect.top;
247         p2.x = rect.right;
248         p2.y = rect.bottom;
249         
250         ScreenToClient(hDlg, &p1);
251         ScreenToClient(hDlg, &p2);
252
253         rect.left = p1.x;
254         rect.top = p1.y;
255         rect.right = p2.x;
256         rect.bottom = p2.y;
257
258         MoveWindow(hCtrl, rect.left + xOffset, rect.top + yOffset, rect.right - rect.left, rect.bottom - rect.top, TRUE);
259 }
260