Standardize License information
[openafs.git] / src / WINNT / afsusrmgr / window.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 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "TaAfsUsrMgr.h"
16 #include "messages.h"
17 #include "window.h"
18 #include "usr_tab.h"
19 #include "grp_tab.h"
20 #include "mch_tab.h"
21 #include "command.h"
22 #include "action.h"
23 #include "creds.h"
24
25
26 /*
27  * TABS _______________________________________________________________________
28  *
29  */
30
31 static struct {
32    TABTYPE tt;
33    int idiImage;
34    int idsText;
35    int iddChild;
36    DLGPROC procChild;
37 } aTABS[] = {
38    { ttUSERS, imageUSER, IDS_TAB_USERS, IDD_TAB_USERS, (DLGPROC)Users_DlgProc },
39    { ttGROUPS, imageGROUP, IDS_TAB_GROUPS, IDD_TAB_GROUPS, (DLGPROC)Groups_DlgProc },
40    { ttMACHINES, imageSERVER, IDS_TAB_MACHINES, IDD_TAB_MACHINES, (DLGPROC)Machines_DlgProc },
41 };
42
43 static size_t cTABS = sizeof(aTABS) / sizeof(aTABS[0]);
44
45
46 /*
47  * DEFINITIONS ________________________________________________________________
48  *
49  */
50
51 #define cxMIN_TABS    150
52 #define cyMIN_TABS    100
53
54 rwWindowData awdMain[] = {
55    { IDC_TAB, raSizeX | raSizeY, MAKELONG(cxMIN_TABS,cyMIN_TABS) },
56    { IDC_CELL, raSizeX | raRepaint },
57    { IDC_CREDS, raSizeX | raRepaint },
58    { IDC_ANIM, raMoveX },
59    { IDC_BAR, raSizeX },
60    { IDC_STATIC, raRepaint },
61    { idENDLIST, 0 }
62 };
63
64 rwWindowData awdTabChild[] = {
65     { 0, raSizeX | raSizeY },
66     { idENDLIST, 0  }
67  };
68
69
70 /*
71  * PROTOTYPES _________________________________________________________________
72  *
73  */
74
75 void Main_OnInitDialog (HWND hDlg);
76 LRESULT CALLBACK Main_TabHookProc (HWND hTab, UINT msg, WPARAM wp, LPARAM lp);
77
78
79 /*
80  * ROUTINES ___________________________________________________________________
81  *
82  */
83
84 BOOL CALLBACK Main_DialogProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
85 {
86    if (msg == WM_INITDIALOG)
87       {
88       g.hMain = hDlg;
89       AfsAppLib_SetMainWindow (g.hMain);
90       }
91
92    switch (msg)
93       {
94       case WM_INITDIALOG:
95          Main_OnInitDialog (hDlg);
96          break;
97
98       case WM_SHOW_YOURSELF:
99          if (lp || g.idCell)
100             {
101             ShowWindow (g.hMain, SW_SHOW);
102             SetForegroundWindow (g.hMain);
103             Actions_WindowToTop (TRUE);
104             }
105          break;
106
107       case WM_SHOW_ACTIONS:
108          if (gr.fShowActions)
109             Actions_OpenWindow();
110          break;
111
112       case WM_ACTIVATEAPP:
113          Actions_WindowToTop ((wp) ? TRUE : FALSE);
114          if (wp)
115             StartTask (taskEXPIRED_CREDS);
116          break;
117
118       case WM_EXPIRED_CREDENTIALS:
119       case WM_REFRESHED_CREDENTIALS:
120          g.hCreds = (PVOID)lp;
121          StartTask (taskUPD_CREDS);
122          break;
123
124       case WM_SIZE:
125          // if (lp==0), we're minimizing--don't call ResizeWindow().
126          //
127          if (lp != 0)
128             ResizeWindow (hDlg, awdMain, rwaFixupGuts);
129          break;
130
131       case WM_ENDTASK:
132          LPTASKPACKET ptp;
133          if ((ptp = (LPTASKPACKET)lp) != NULL)
134             {
135             if (ptp->idTask == taskUPD_USERS)
136                Display_OnEndTask_UpdUsers (ptp);
137             else if (ptp->idTask == taskUPD_GROUPS)
138                Display_OnEndTask_UpdGroups (ptp);
139             else if (ptp->idTask == taskUPD_MACHINES)
140                Display_OnEndTask_UpdMachines (ptp);
141             FreeTaskPacket (ptp);
142             }
143          break;
144
145       case WM_ASC_NOTIFY_ACTION:
146          Actions_OnNotify (wp, lp);
147          break;
148
149       case WM_COMMAND:
150          switch (LOWORD(wp))
151             {
152             case IDCANCEL:
153                Quit (0);
154                break;
155
156             default:
157                OnContextCommand (LOWORD(wp));
158                break;
159             }
160          break;
161
162       case WM_NOTIFY: 
163          switch (((LPNMHDR)lp)->code)
164             { 
165             case TCN_SELCHANGE:
166                Main_PrepareTabChild();
167                break; 
168             }
169          break;
170
171       case WM_HELP:
172          if ((lp == 0) || (IsAncestor (g.hMain, (HWND)(((LPHELPINFO)lp)->hItemHandle))))
173             {
174             WinHelp (hDlg, cszHELPFILENAME, HELP_FINDER, 0);
175             }
176          break;
177       }
178
179    return FALSE;
180 }
181
182
183 void Main_OnInitDialog (HWND hDlg)
184 {
185    // Resize and reposition the main window
186    //
187    RECT rWindow = gr.rMain;
188    if (!rWindow.right)
189       GetWindowRect (hDlg, &rWindow);
190    ResizeWindow (hDlg, awdMain, rwaMoveToHere, &rWindow);
191
192    // Add tabs to the main window's tab control
193    //
194    HWND hTab = GetDlgItem (hDlg, IDC_TAB);
195    TabCtrl_SetImageList (hTab, AfsAppLib_CreateImageList (FALSE));
196
197    TCHAR szText[ cchRESOURCE ];
198    TC_ITEM tci;
199    tci.mask = TCIF_TEXT | TCIF_PARAM | TCIF_IMAGE;
200    tci.pszText = szText;
201
202    for (size_t iTab = 0; iTab < cTABS; ++iTab)
203       {
204       tci.iImage = aTABS[ iTab ].idiImage;
205       tci.lParam = (LPARAM)aTABS[ iTab ].tt;
206       GetString (tci.pszText, aTABS[ iTab ].idsText);
207       TabCtrl_InsertItem (hTab, iTab, &tci);
208       }
209
210    // Subclass the tab control, so that we can make it forward any WM_SIZE
211    // messages to its child window.
212    //
213    Subclass_AddHook (hTab, Main_TabHookProc);
214
215    // Since we just subclassed this control, our new wndproc wasn't around
216    // when the window was created.  Any WM_CREATE processing we'd ordinarily
217    // in that wndproc must therefore be done here.
218
219    // WM_CREATE:
220    RECT rTab;
221    GetRectInParent (hTab, &rTab);
222    ResizeWindow (hTab, awdTabChild, rwaMoveToHere, &rTab);
223
224    // Select an appropriate tab and display it.
225    //
226    Main_PrepareTabChild (gr.iTabLast);
227
228    // Tell the admin client that we're interested in any actions that occur
229    //
230    ULONG status;
231    asc_ActionListen (g.idClient, g.hMain, &status);
232 }
233
234
235 LRESULT CALLBACK Main_TabHookProc (HWND hTab, UINT msg, WPARAM wp, LPARAM lp)
236 {
237    LRESULT rc = CallWindowProc ((WNDPROC)Subclass_FindNextHook (hTab, Main_TabHookProc), hTab, msg, wp, lp);
238
239    switch (msg)
240       {
241       // Since this is a subclass proc, it's not around when the window
242       // is created.  Any WM_CREATE processing we'd ordinarily do here
243       // must therefore be done at the point where the tab control is
244       // subclassed.
245       //
246       // case WM_CREATE:
247
248       case WM_SIZE:
249          if (lp != 0)
250             ResizeWindow (hTab, awdTabChild, rwaFixupGuts);
251          break;
252
253       case WM_DESTROY:
254          Subclass_RemoveHook (hTab, Main_TabHookProc);
255          break;
256       }
257
258    return rc;
259 }
260
261
262 void Main_PrepareTabChild (int iTabNew)
263 {
264    HWND hTab = GetDlgItem (g.hMain, IDC_TAB);
265    int iTabOld = TabCtrl_GetCurSel (hTab);
266    if (iTabNew == -1)
267       iTabNew = iTabOld;
268
269    HWND hDlg;
270    if ((hDlg = GetTabChild (hTab)) != NULL)
271       DestroyWindow (hDlg);
272    if ((hDlg = ModelessDialog (aTABS[ iTabNew ].iddChild, hTab, aTABS[ iTabNew ].procChild)) != NULL)
273       {
274       if (iTabNew != iTabOld)
275          TabCtrl_SetCurSel (hTab, iTabNew);
276       ShowWindow (hDlg, SW_SHOW);
277       }
278
279    // Remember this tab as the last one the user saw, so that if the app gets
280    // closed we can re-open it to the same tab.
281    //
282    gr.iTabLast = iTabNew;
283
284    // Check/uncheck/enable/disable our dialog's menu items as appropriate.
285    // We do this whenever the current tab changes, because some of the
286    // menu items will change state depending on which tab is visible.
287    //
288    Main_SetMenus();
289 }
290
291
292 void Main_SetMenus (void)
293 {
294    HMENU hm = GetMenu (g.hMain);
295    LPASIDLIST pSel = Display_GetSelectedList();
296
297    Main_SetViewMenus (hm);
298
299    // Fix the Operations In Progress entry
300    //
301    CheckMenu (hm, M_ACTIONS, gr.fShowActions);
302
303    // Fix the CellProperties and Edit.* menu items. Many get disabled if there
304    // is no selection
305    //
306    BOOL fEnable = (pSel && pSel->cEntries) ? TRUE : FALSE;
307    EnableMenu (hm, M_DELETE, fEnable);
308    EnableMenu (hm, M_REFRESH, fEnable);
309    EnableMenu (hm, M_MEMBERSHIP, fEnable);
310    EnableMenu (hm, M_PROPERTIES, fEnable);
311
312    if ((fEnable = (pSel && (pSel->cEntries == 1))) == TRUE)
313       {
314       // Make sure it's a group
315       ASOBJTYPE Type;
316       if (!asc_ObjectTypeGet_Fast (g.idClient, g.idCell, pSel->aEntries[0].idObject, &Type))
317          fEnable = FALSE;
318       else if (Type != TYPE_GROUP)
319          fEnable = FALSE;
320       }
321
322    EnableMenu (hm, M_RENAME, fEnable);
323
324    if (pSel)
325       asc_AsidListFree (&pSel);
326 }
327
328
329 void Main_SetViewMenus (HMENU hm)
330 {
331    // Which tab is currently being displayed?
332    //
333    HWND hTab = GetDlgItem (g.hMain, IDC_TAB);
334    int iTab = TabCtrl_GetCurSel (hTab);
335    TABTYPE tt = aTABS[ iTab ].tt;
336
337    // Fix the Icons.Large/Small/Details entries
338    //
339    LPVIEWINFO pvi = (tt == ttUSERS) ? &gr.viewUsr : (tt == ttMACHINES) ? &gr.viewMch : &gr.viewGrp;
340    ICONVIEW iv = (tt == ttUSERS) ? gr.ivUsr : (tt == ttMACHINES) ? gr.ivMch : gr.ivGrp;
341
342    int idm = ((pvi->lvsView & FLS_VIEW_MASK) == FLS_VIEW_LARGE) ? M_VIEW_LARGE : ((pvi->lvsView & FLS_VIEW_MASK) == FLS_VIEW_SMALL) ? M_VIEW_SMALL : M_VIEW_DETAILS;
343    CheckMenuRadioItem (hm, M_VIEW_LARGE, M_VIEW_DETAILS, idm, MF_BYCOMMAND);
344
345    // Fix the Icons.One/Two/Status entries
346    //
347    BOOL fEnable = ((pvi->lvsView & FLS_VIEW_MASK) == FLS_VIEW_LIST) ? TRUE : FALSE;
348    EnableMenu (hm, M_VIEW_ONE, fEnable);
349    EnableMenu (hm, M_VIEW_TWO, fEnable);
350    EnableMenu (hm, M_VIEW_STATUS, fEnable);
351
352    idm = (iv == ivTWOICONS) ? M_VIEW_TWO : (iv == ivONEICON) ? M_VIEW_ONE : M_VIEW_STATUS;
353    CheckMenuRadioItem (hm, M_VIEW_ONE, M_VIEW_STATUS, idm, MF_BYCOMMAND);
354 }
355