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