886efe6d188c282e7d5baf815115882c72806686
[platform/upstream/krb5.git] / src / windows / leash / MainFrm.cpp
1 //      **************************************************************************************
2 //      File:                   MainFrm.cpp
3 //      By:                             Arthur David Leather
4 //      Created:                12/02/98
5 //      Copyright               @1998 Massachusetts Institute of Technology - All rights reserved.
6 //      Description:    CPP file for MainFrm.h. Contains variables and functions
7 //                                      for Leash
8 //
9 //      History:
10 //
11 //      MM/DD/YY        Inits   Description of Change
12 //      12/02/98        ADL             Original
13 //      **************************************************************************************
14
15
16 #include "stdafx.h"
17 #include "LeashUIApplication.h"
18 #include "Leash.h"
19 #include "MainFrm.h"
20 #include "lglobals.h"
21 //#include "KrbRealmHostMaintenance.h"
22
23 #ifdef _DEBUG
24 #define new DEBUG_NEW
25 #undef THIS_FILE
26 static char THIS_FILE[] = __FILE__;
27 #endif
28
29 /////////////////////////////////////////////////////////////////////////////
30 // CMainFrame
31
32 #define MIN_LEFT      179
33 #define MIN_TOP           61
34 #define MIN_RIGHT         530
35 #define MIN_BOTTOM        280
36
37 #ifndef NO_STATUS_BAR
38 CMFCStatusBar CMainFrame::m_wndStatusBar;
39 #endif
40 CMFCToolBar   CMainFrame::m_wndToolBar;
41 CImageList CMainFrame::m_imageList;
42 CImageList CMainFrame::m_disabledImageList;
43 BOOL       CMainFrame::m_isMinimum;
44 BOOL       CMainFrame::m_isBeingResized;
45 int        CMainFrame::m_whatSide;
46
47 IMPLEMENT_DYNCREATE(CMainFrame, CLeashFrame)
48
49 BEGIN_MESSAGE_MAP(CMainFrame, CLeashFrame)
50         //{{AFX_MSG_MAP(CMainFrame)
51         ON_WM_CREATE()
52         ON_WM_SIZING()
53     ON_WM_CLOSE()
54         ON_WM_GETMINMAXINFO()
55     ON_COMMAND(ID_APP_EXIT, OnClose)
56         //}}AFX_MSG_MAP
57         // Global help commands
58         ON_COMMAND(ID_HELP_LEASH_, CMainFrame::OnHelpFinder)
59         ON_COMMAND(ID_HELP, CMainFrame::OnHelp)
60         ON_COMMAND(ID_CONTEXT_HELP, CMainFrame::OnContextHelp)
61         ON_MESSAGE_VOID(WM_RIBBON_RESIZE, OnRibbonResize)
62 END_MESSAGE_MAP()
63
64 static UINT indicators[] =
65 {
66         ID_SEPARATOR,           // status line indicator
67         ID_SEPARATOR,
68     ID_SEPARATOR,
69     ID_SEPARATOR
70 };
71
72
73 /////////////////////////////////////////////////////////////////////////////
74 // CMainFrame construction/destruction
75
76 CMainFrame::CMainFrame()
77 {
78         m_winRectLeft = 0;
79         m_winRectTop = 0;
80         m_winRectRight = 0;
81         m_winRectBottom = 0;
82         m_whatSide = RESET_MINSIZE;
83         m_isMinimum = FALSE;
84     m_isBeingResized = FALSE;
85     m_bOwnerCreated = FALSE;
86     pApplication = NULL;
87 }
88
89 CMainFrame::~CMainFrame()
90 {
91 }
92
93 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
94 {
95     if (CLeashApp::m_useRibbon) {
96         HWND hwnd;
97         HRESULT hr;
98         // Fixup tooltips (cribbed from http://social.msdn.microsoft.com/Forums/en/vcmfcatl/thread/5c5b4879-d278-4d79-8894-99e7f9b322df)
99
100         CMFCToolTipInfo ttParams;
101         ttParams.m_bVislManagerTheme = TRUE;
102         ttParams.m_bVislManagerTheme = FALSE;
103         ttParams.m_bDrawSeparator = FALSE;
104         ttParams.m_clrFillGradient = afxGlobalData.clrBarFace;
105         ttParams.m_clrFill = RGB(255, 255, 255);
106         ttParams.m_clrBorder = afxGlobalData.clrBarShadow;
107         ttParams.m_clrText = afxGlobalData.clrBarText;
108
109         theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
110                 RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
111
112         CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows7));
113         CDockingManager::SetDockingMode(DT_SMART);
114         m_wndRibbonBar.SetWindows7Look(TRUE);
115
116         // Initialize the ribbon, keeping a handle to the IUIApplication
117         // so that we can query the ribbon height and save space for it
118         // when calculating our layout.
119         hwnd = this->GetSafeHwnd();
120         if (hwnd == NULL)
121             printf("Failed to get HWND\n");
122         hr = LeashUIApplication::CreateInstance(&pApplication, hwnd);
123         if (FAILED(hr)) {
124             MessageBox("LeashUIApplication::CreateInstance!", "Error", MB_OK);
125             return -1;
126         }
127     }
128
129         if (CLeashFrame::OnCreate(lpCreateStruct) == -1)
130                 return -1;
131
132     ShowWindow(SW_HIDE);
133
134 /* NT4 and NT5 aren't shipped with a version of MFC that supports
135 // 'CreateEx()' as of 2/1/99
136 #if _MFC_VER > 0x0421
137
138         if (!m_wndToolBar.CreateEx(this) ||
139                 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
140         {
141                 TRACE0("Failed to create toolbar\n");
142                 return -1;      // fail to create
143         }
144
145 #else
146
147         if (!m_wndToolBar.Create(this) ||
148                 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
149         {
150                 TRACE0("Failed to create toolbar\n");
151                 return -1;      // fail to create
152         }
153
154 #endif
155 */
156         if ((!CLeashApp::m_useRibbon) &&
157                 (!m_wndToolBar.Create(this) ||
158                  !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)))
159         {
160                 MessageBox("There is problem creating the Leash Toolbar!",
161                    "Error", MB_OK);
162         TRACE0("Failed to create toolbar\n");
163                 return -1;      // fail to create
164         }
165
166 #ifndef NO_STATUS_BAR
167         if (!m_wndStatusBar.Create(this) ||
168                 !m_wndStatusBar.SetIndicators(indicators,
169                   (CLeashApp::m_hAfsDLL ? 4 : 3)))
170         {
171                 MessageBox("There is problem creating the Leash Status Bar!",
172                    "Error", MB_OK);
173         TRACE0("Failed to create status bar\n");
174                 return -1;      // fail to create
175         }
176 #endif
177
178
179         // TODO: Remove this if you don't want tool tips or a resizeable toolbar
180         //m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() |
181         //                           CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
182
183     if (!CLeashApp::m_useRibbon) {
184         // TODO: Delete these three lines if you don't want the toolbar to
185         //  be dockable
186         m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
187         EnableDocking(CBRS_ALIGN_ANY);
188         DockPane(&m_wndToolBar);
189     }
190
191         return 0;
192 }
193
194 BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
195 {
196     return CLeashFrame::PreTranslateMessage(pMsg);
197 }
198
199 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
200 {
201         // Use the specific class name we established earlier
202     // Remove the Minimize and Maximize buttons
203     cs.style &= ~WS_MINIMIZEBOX;
204     cs.style &= ~WS_MAXIMIZEBOX;
205     // Initialize the extended window style to display a TaskBar entry with WS_EX_APPWINDOW
206     cs.dwExStyle |= WS_EX_APPWINDOW;
207 //    cs.dwExStyle |= WS_EX_OVERLAPPEDWINDOW ;
208         cs.lpszClass = _T("LEASH.0WNDCLASS");
209     cs.lpszName = _T("MIT Kerberos");
210
211     CString strText = AfxGetApp()->GetProfileString(CLeashFrame::s_profileHeading,
212                                                     CLeashFrame::s_profileRect);
213     if (!strText.IsEmpty())
214     {
215         CRect rect;
216
217         rect.left = atoi((const char*) strText);
218         rect.top = atoi((const char*) strText + 5);
219         rect.right = atoi((const char*) strText + 10);
220         rect.bottom = atoi((const char*) strText + 15);
221
222         cs.x = rect.left;
223         cs.y = rect.top;
224         cs.cx = rect.right - rect.left;
225         cs.cy = rect.bottom - rect.top;
226
227         if ( cs.x < 0 )
228             cs.x = CW_USEDEFAULT;
229         if ( cs.y < 0 )
230             cs.y = CW_USEDEFAULT;
231         if ( cs.cx <= 0 )
232             cs.cx = CLeashFrame::s_rectDefault.right;
233         if ( cs.cy <= 0 )
234             cs.cy = CLeashFrame::s_rectDefault.bottom;
235     }
236     else
237     {
238         cs.cx = CLeashFrame::s_rectDefault.right;
239         cs.cy = CLeashFrame::s_rectDefault.bottom;
240         cs.y = CW_USEDEFAULT;
241         cs.x = CW_USEDEFAULT;
242     }
243
244     // Change the following line to call
245         // CLeashFrame::PreCreateWindow(cs) if this is an SDI application.
246         if (!CLeashFrame::PreCreateWindow(cs))
247         return FALSE;
248
249     // We create a parent window for our application to ensure that
250     // it has an owner.  This way we can disable the TaskBar entry
251     // by removing the WS_EX_APPWINDOW style later on.
252     if ( !m_bOwnerCreated )
253     {
254         m_bOwnerCreated = m_MainFrameOwner.Create(IDD_FRAMEOWNER);
255         if ( m_bOwnerCreated )
256             m_MainFrameOwner.ShowWindow(SW_HIDE);
257     }
258     if ( m_bOwnerCreated )
259         cs.hwndParent = m_MainFrameOwner.GetSafeHwnd();
260
261     return TRUE;
262 }
263
264
265 BOOL CMainFrame::ShowTaskBarButton(BOOL bVisible)
266 {
267     if (!m_bOwnerCreated)
268         return FALSE;
269
270     if (bVisible) {
271         ShowWindow(SW_HIDE);
272         ModifyStyleEx(0, WS_EX_APPWINDOW);
273         ShowWindow(SW_SHOW);
274     } else {
275         ShowWindow(SW_HIDE);
276         ModifyStyleEx(WS_EX_APPWINDOW, 0);
277         ShowWindow(SW_SHOW);
278     }
279     return TRUE;
280 }
281
282 /////////////////////////////////////////////////////////////////////////////
283 // CMainFrame diagnostics
284
285 #ifdef _DEBUG
286 void CMainFrame::AssertValid() const
287 {
288         CLeashFrame::AssertValid();
289 }
290
291 void CMainFrame::Dump(CDumpContext& dc) const
292 {
293         CLeashFrame::Dump(dc);
294 }
295
296 #endif //_DEBUG
297
298 /////////////////////////////////////////////////////////////////////////////
299 // CMainFrame message handlers
300
301 void CMainFrame::OnResetWindowSize()
302 {
303     WINDOWPLACEMENT wndpl;
304         wndpl.length = sizeof(WINDOWPLACEMENT);
305
306     if (!GetWindowPlacement(&wndpl))
307     {
308         MessageBox("There is a problem getting Leash Window size!",
309                    "Error", MB_OK);
310         return;
311     }
312
313     wndpl.rcNormalPosition = CLeashFrame::s_rectDefault;
314
315         m_whatSide = SKIP_MINSIZE;
316
317     if (!SetWindowPlacement(&wndpl))
318     {
319         MessageBox("There is a problem setting Leash Window size!",
320                    "Error", MB_OK);
321     }
322
323         m_whatSide = RESET_MINSIZE;
324 }
325
326 void CMainFrame::OnSizing(UINT fwSide, LPRECT pRect)
327 { // Keeps track of Leash window size for function CMainFrame::RecalcLayout
328         m_winRectLeft = pRect->left;
329         m_winRectTop = pRect->top;
330         m_winRectRight = pRect->right;
331         m_winRectBottom = pRect->bottom;
332
333         if (m_whatSide)
334           m_whatSide = fwSide;
335
336         CLeashFrame::OnSizing(fwSide, pRect);
337 }
338
339 void CMainFrame::RecalcLayout(BOOL bNotify)
340 { // MINSIZE - Insurance that we have a minimum Leash window size
341         int width = MIN_RIGHT - MIN_LEFT;
342         int height = MIN_BOTTOM - MIN_TOP;
343         LeashUIApplication *leashUI;
344         RECT border;
345         border.left = border.right = border.bottom = 0;
346         // Leave room for the ribbon.
347         leashUI = static_cast<LeashUIApplication*>(pApplication);
348         border.top = (leashUI != NULL) ? leashUI->GetRibbonHeight() : 0;
349         NegotiateBorderSpace(CFrameWnd::borderSet, &border);
350
351     BOOL change = FALSE;
352         WINDOWPLACEMENT wndpl;
353         wndpl.length = sizeof(WINDOWPLACEMENT);
354
355     if (!GetWindowPlacement(&wndpl))
356     {
357         MessageBox("There is a problem getting Leash Window size!",
358                    "Error", MB_OK);
359         return;
360     }
361
362         if (m_whatSide)
363         {
364                 if ((m_winRectRight - m_winRectLeft) < width)
365                 {
366                         if (m_whatSide == LEFT_SIDE) {
367                 wndpl.rcNormalPosition.left = wndpl.rcNormalPosition.right - width;
368                 change = TRUE;
369                         } else if (m_whatSide == RIGHT_SIDE) {
370                 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + width;
371                 change = TRUE;
372             }
373                 }
374                 else if ((m_winRectBottom - m_winRectTop) < height)
375                 {
376                         if (m_whatSide == TOP_SIDE) {
377                 wndpl.rcNormalPosition.top = wndpl.rcNormalPosition.bottom - height;
378                 change = TRUE;
379                         } else if (m_whatSide == BOTTOM_SIDE) {
380                 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + height;
381                 change = TRUE;
382             }
383                 }
384         }
385
386     if ( change ) {
387         if (!SetWindowPlacement(&wndpl))
388         {
389             MessageBox("There is a problem setting Leash Window size!",
390                         "Error", MB_OK);
391         }
392     }
393     m_isBeingResized = TRUE;
394
395     CLeashFrame::RecalcLayout(bNotify);
396 }
397
398
399 void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
400 {
401     lpMMI->ptMinTrackSize.x = 650;
402     lpMMI->ptMinTrackSize.y = 240;
403         CLeashFrame::OnGetMinMaxInfo(lpMMI);
404 }
405
406 void CMainFrame::OnClose(void)
407 {
408     CLeashFrame::OnClose();
409 }
410
411 LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
412 {
413     BOOL oldMin = m_isMinimum;
414     //printf("CMainFrame::WindowProc() Msg: %x, WPARAM: %x, LPARAM: %x\n", message, wParam, lParam);
415         switch(message)
416         {
417     case WM_CLOSE:
418         printf("received WM_CLOSE!");
419         break;
420     case WM_SIZE:
421         switch ( wParam ) {
422         case SIZE_MINIMIZED:
423             m_isMinimum = TRUE;
424             break;
425         case SIZE_MAXIMIZED:
426         case SIZE_RESTORED:
427             m_isMinimum = FALSE;
428             break;
429         }
430         break;
431     case ID_OBTAIN_TGT_WITH_LPARAM:
432         GetActiveView()->SendMessage(ID_OBTAIN_TGT_WITH_LPARAM, wParam,
433                                      lParam);
434         break;
435         }
436
437     if ( oldMin != m_isMinimum ) {
438         if ( m_isMinimum ) {
439             ShowTaskBarButton(FALSE);
440             ShowWindow(SW_HIDE);
441         }
442     }
443     return CLeashFrame::WindowProc(message, wParam, lParam);
444 }
445
446 // Signalled by LeashUIApplication::OnViewChanged when the ribbon height
447 // changes.
448 void CMainFrame::OnRibbonResize()
449 {
450     RecalcLayout(TRUE);
451 }
452
453 /*
454 void CMainFrame::OnHelp()
455 {
456
457 }
458 */
459
460 /*
461 void CMainFrame::OnContextHelp()
462 {
463
464 }
465 */