2 * PROJECT: ReactOS Device Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/devmgr/devmgmt/MainWindow.cpp
5 * PURPOSE: Implements the main container window for the device view
6 * COPYRIGHT: Copyright 2014 - 2015 Ged Murphy <gedmurphy@reactos.org>
12 #include "MainWindow.h"
15 /* DATA *****************************************************/
17 #define BTN_PROPERTIES 0
18 #define BTN_SCAN_HARDWARE 1
19 #define BTN_ENABLE_DRV 2
20 #define BTN_DISABLE_DRV 3
21 #define BTN_UPDATE_DRV 4
22 #define BTN_UNINSTALL_DRV 5
24 HINSTANCE g_hThisInstance
= NULL
;
25 HINSTANCE g_hParentInstance
= NULL
;
28 static const MENU_HINT MainMenuHintTable
[] =
31 { IDC_EXIT
, IDS_HINT_EXIT
},
34 { IDC_PROPERTIES
, IDS_HINT_PROPERTIES
},
35 { IDC_SCAN_HARDWARE
, IDS_HINT_SCAN
},
36 { IDC_ENABLE_DRV
, IDS_HINT_ENABLE
},
37 { IDC_DISABLE_DRV
, IDS_HINT_DISABLE
},
38 { IDC_UPDATE_DRV
, IDS_HINT_UPDATE
},
39 { IDC_UNINSTALL_DRV
, IDS_HINT_UNINSTALL
},
40 { IDC_ADD_HARDWARE
, IDS_HINT_ADD
},
44 { IDC_DEVBYTYPE
, IDS_HINT_DEV_BY_TYPE
},
45 { IDC_DEVBYCONN
, IDS_HINT_DEV_BY_CONN
},
46 { IDC_RESBYTYPE
, IDS_HINT_RES_BY_TYPE
},
47 { IDC_RESBYCONN
, IDS_HINT_RES_BY_TYPE
},
48 { IDC_SHOWHIDDEN
, IDS_HINT_SHOW_HIDDEN
},
50 { IDC_ABOUT
, IDS_HINT_ABOUT
}
56 static const MENU_HINT SystemMenuHintTable
[] =
58 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
59 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
60 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
61 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
62 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
63 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
}
66 static TBBUTTON TbButtons
[] =
68 { BTN_PROPERTIES
, IDC_PROPERTIES
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
69 { BTN_SCAN_HARDWARE
, IDC_SCAN_HARDWARE
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
70 { 2, IDC_STATIC
, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0 },
71 { BTN_ENABLE_DRV
, IDC_ENABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
72 { BTN_DISABLE_DRV
, IDC_DISABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
73 { BTN_UPDATE_DRV
, IDC_UPDATE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
74 { BTN_UNINSTALL_DRV
, IDC_UNINSTALL_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }
78 /* PUBLIC METHODS **********************************************/
80 CDeviceManager::CDeviceManager(void) :
86 m_szMainWndClass
= L
"DevMgmtWndClass";
89 CDeviceManager::~CDeviceManager(void)
95 CDeviceManager::Create(_In_ HWND
/*hWndParent*/,
97 _In_opt_z_ LPCWSTR
/*lpMachineName*/,
100 CDeviceManager MainWindow
;
101 INITCOMMONCONTROLSEX icex
;
102 CAtlStringW szAppName
;
105 // Store the instances
106 g_hParentInstance
= hInst
;
107 g_hThisInstance
= GetModuleHandleW(L
"devmgr.dll");
109 // Initialize common controls
110 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
111 icex
.dwICC
= ICC_BAR_CLASSES
| ICC_COOL_CLASSES
;
112 InitCommonControlsEx(&icex
);
114 // Load the application name
115 if (szAppName
.LoadStringW(g_hThisInstance
, IDS_APPNAME
))
117 // Initialize the main window
118 if (MainWindow
.Initialize(szAppName
, nCmdShow
))
120 // Run the application
121 Ret
= MainWindow
.Run();
123 // Uninitialize the main window
124 MainWindow
.Uninitialize();
133 /* PRIVATE METHODS **********************************************/
136 CDeviceManager::Initialize(_In_z_ LPCTSTR lpCaption
,
139 CAtlStringW szCaption
;
140 WNDCLASSEXW wc
= {0};
142 // Store the show window value
143 m_CmdShow
= nCmdShow
;
145 // Setup the window class struct
146 wc
.cbSize
= sizeof(WNDCLASSEXW
);
147 wc
.lpfnWndProc
= MainWndProc
;
148 wc
.hInstance
= g_hThisInstance
;
149 wc
.hIcon
= LoadIcon(g_hThisInstance
, MAKEINTRESOURCEW(IDI_MAIN_ICON
));
150 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
151 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
152 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDR_MAINMENU
);
153 wc
.lpszClassName
= m_szMainWndClass
;
154 wc
.hIconSm
= (HICON
)LoadImage(g_hThisInstance
,
155 MAKEINTRESOURCE(IDI_MAIN_ICON
),
161 // Register the window
162 if (RegisterClassExW(&wc
))
164 // Create the main window and store the object pointer
165 m_hMainWnd
= CreateWindowExW(WS_EX_WINDOWEDGE
,
168 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
179 // Return creation result
180 return !!(m_hMainWnd
);
184 CDeviceManager::Uninitialize(void)
186 // Unregister the window class
187 UnregisterClassW(m_szMainWndClass
, g_hThisInstance
);
191 CDeviceManager::Run(void)
195 // Pump the message queue
196 while (GetMessageW(&Msg
, NULL
, 0, 0 ) != 0)
198 TranslateMessage(&Msg
);
199 DispatchMessageW(&Msg
);
206 CDeviceManager::MainWndMenuHint(_In_ WORD CmdId
,
207 _In_
const MENU_HINT
*HintArray
,
208 _In_ DWORD HintsCount
,
212 const MENU_HINT
*LastHint
;
213 UINT HintId
= DefHintId
;
215 LastHint
= HintArray
+ HintsCount
;
216 while (HintArray
!= LastHint
)
218 if (HintArray
->CmdId
== CmdId
)
220 HintId
= HintArray
->HintId
;
227 StatusBarLoadString(m_hStatusBar
,
236 CDeviceManager::UpdateStatusBar(_In_
bool InMenuLoop
)
238 SendMessageW(m_hStatusBar
,
245 CDeviceManager::RefreshView(_In_ ViewType Type
)
249 // Refreshed the cached view
250 m_DeviceView
->Refresh(Type
, FALSE
, TRUE
, NULL
);
252 // Get the menu item id
255 case DevicesByType
: CheckId
= IDC_DEVBYTYPE
; break;
256 case DevicesByConnection
: CheckId
= IDC_DEVBYCONN
; break;
257 case ResourcesByType
: CheckId
= IDC_RESBYTYPE
; break;
258 case ResourcesByConnection
: CheckId
= IDC_RESBYCONN
; break;
259 default: ATLASSERT(FALSE
); break;
262 // Set the new check item
263 CheckMenuRadioItem(m_hMenu
,
273 CDeviceManager::CreateToolBar(void)
275 TBADDBITMAP TbAddBitmap
;
277 DWORD dwStyles
= WS_CHILDWINDOW
| TBSTYLE_FLAT
| TBSTYLE_WRAPABLE
| TBSTYLE_TOOLTIPS
| CCS_NODIVIDER
;
278 DWORD dwExStyles
= WS_EX_LEFT
| WS_EX_LTRREADING
| WS_EX_RIGHTSCROLLBAR
;
280 // Create the toolbar window
281 m_hToolBar
= CreateWindowExW(dwExStyles
,
290 if (m_hToolBar
== NULL
) return FALSE
;
292 // Don't show clipped buttons
293 SendMessageW(m_hToolBar
,
296 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
298 SendMessageW(m_hToolBar
, TB_SETBITMAPSIZE
, 0, MAKELONG(16, 16));
300 // Set the struct size, the toobar needs this...
301 SendMessageW(m_hToolBar
,
306 TbAddBitmap
.hInst
= g_hThisInstance
;
307 TbAddBitmap
.nID
= IDB_TOOLBAR
;
308 SendMessageW(m_hToolBar
, TB_ADDBITMAP
, _countof(TbButtons
), (LPARAM
)&TbAddBitmap
);
310 SendMessageW(m_hToolBar
, TB_ADDBUTTONSW
, _countof(TbButtons
), (LPARAM
)TbButtons
);
311 SendMessageW(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
315 ShowWindow(m_hToolBar
, SW_SHOW
);
322 CDeviceManager::CreateStatusBar(void)
324 int StatWidths
[] = {110, -1}; // widths of status bar
327 // Create the status bar
328 m_hStatusBar
= CreateWindowExW(0,
331 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
334 (HMENU
)IDC_STATUSBAR
,
340 bRet
= (SendMessageW(m_hStatusBar
,
342 sizeof(StatWidths
) / sizeof(int),
343 (LPARAM
)StatWidths
) != 0);
349 void CDeviceManager::UpdateToolbar()
353 CNode
*Node
= m_DeviceView
->GetSelectedNode();
356 if (Node
->HasProperties())
358 State
= TBSTATE_ENABLED
;
362 State
= TBSTATE_HIDDEN
;
364 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_PROPERTIES
, MAKELPARAM(State
, 0));
365 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UPDATE_DRV
, MAKELPARAM(State
, 0)); //hack
366 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UNINSTALL_DRV
, MAKELPARAM(State
, 0)); // hack
370 // enable driver button
371 if (Node
->GetNodeType() == DeviceNode
&&
372 dynamic_cast<CDeviceNode
*>(Node
)->IsDisabled())
374 State
= TBSTATE_ENABLED
;
378 State
= TBSTATE_HIDDEN
;
380 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_ENABLE_DRV
, MAKELPARAM(State
, 0));
382 // disable driver button
383 if (Node
->GetNodeType() == DeviceNode
&&
384 dynamic_cast<CDeviceNode
*>(Node
)->CanDisable() &&
385 !dynamic_cast<CDeviceNode
*>(Node
)->IsDisabled())
387 State
= TBSTATE_ENABLED
;
391 State
= TBSTATE_HIDDEN
;
393 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_DISABLE_DRV
, MAKELPARAM(State
, 0));
404 CDeviceManager::StatusBarLoadString(_In_ HWND hStatusBar
,
406 _In_ HINSTANCE hInstance
,
409 CAtlStringW szMessage
;
413 if (szMessage
.LoadStringW(hInstance
, uID
))
415 // Show the string on the status bar
416 bRet
= (SendMessageW(hStatusBar
,
419 (LPARAM
)szMessage
.GetBuffer()) != 0);
426 CDeviceManager::OnCreate(_In_ HWND hwnd
)
433 // Store a handle to the main menu
434 m_hMenu
= GetMenu(m_hMainWnd
);
436 // Create the toolbar and statusbar
437 if (CreateToolBar() && CreateStatusBar())
439 // Create the device view object
440 m_DeviceView
= new CDeviceView(m_hMainWnd
);
441 if (m_DeviceView
->Initialize())
443 // Do the initial scan
444 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
449 // Display the window according to the user request
450 ShowWindow(hwnd
, m_CmdShow
);
459 CDeviceManager::OnSize(void)
461 RECT rcClient
, rcTool
, rcStatus
;
462 INT lvHeight
, iToolHeight
, iStatusHeight
;
464 // Autosize the toolbar
465 SendMessage(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
467 // Get the toolbar rect and save the height
468 GetWindowRect(m_hToolBar
, &rcTool
);
469 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
471 // Resize the status bar
472 SendMessage(m_hStatusBar
, WM_SIZE
, 0, 0);
474 // Get the statusbar rect and save the height
475 GetWindowRect(m_hStatusBar
, &rcStatus
);
476 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
478 // Get the full client rect
479 GetClientRect(m_hMainWnd
, &rcClient
);
481 // Calculate the remaining height for the treeview
482 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
484 // Resize the device view
485 m_DeviceView
->OnSize(0,
494 CDeviceManager::OnNotify(_In_ LPARAM lParam
)
496 LPNMHDR NmHdr
= (LPNMHDR
)lParam
;
509 m_DeviceView
->DisplayPropertySheet();
515 Ret
= m_DeviceView
->OnRightClick(NmHdr
);
521 m_DeviceView
->DisplayPropertySheet();
525 case TTN_GETDISPINFO
:
527 LPTOOLTIPTEXT lpttt
= (LPTOOLTIPTEXT
)lParam
;
528 lpttt
->hinst
= g_hThisInstance
;
530 UINT_PTR idButton
= lpttt
->hdr
.idFrom
;
534 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_PROPERTIES
);
536 case IDC_SCAN_HARDWARE
:
537 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_SCAN
);
540 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_ENABLE
);
542 case IDC_DISABLE_DRV
:
543 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_DISABLE
);
546 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_UPDATE
);
548 case IDC_UNINSTALL_DRV
:
549 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_UNINSTALL
);
561 CDeviceManager::OnContext(_In_ LPARAM lParam
)
563 return m_DeviceView
->OnContextMenu(lParam
);
567 CDeviceManager::OnCommand(_In_ WPARAM wParam
,
568 _In_ LPARAM
/*lParam*/)
574 Msg
= LOWORD(wParam
);
579 case IDC_SCAN_HARDWARE
:
581 case IDC_DISABLE_DRV
:
583 case IDC_UNINSTALL_DRV
:
584 case IDC_ADD_HARDWARE
:
586 m_DeviceView
->OnAction(Msg
);
592 // Create a popup menu with all the actions for the selected node
593 HMENU hMenu
= CreatePopupMenu();
594 m_DeviceView
->CreateActionMenu(hMenu
, true);
596 // Calculate where to put the menu
598 GetMenuItemRect(m_hMainWnd
, m_hMenu
, 1, &rc
);
599 LONG Height
= rc
.bottom
- rc
.top
;
602 TrackPopupMenuEx(hMenu
,
615 RefreshView(DevicesByType
);
621 RefreshView(DevicesByConnection
);
627 // Get the current state
628 UINT CurCheckState
= GetMenuState(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
);
629 if (CurCheckState
== MF_CHECKED
)
631 m_DeviceView
->SetHiddenDevices(false);
632 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| MF_UNCHECKED
);
634 else if (CurCheckState
== MF_UNCHECKED
)
636 m_DeviceView
->SetHiddenDevices(true);
637 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| MF_CHECKED
);
639 // Refresh the device view
640 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
650 MessageBoxW(m_hMainWnd
,
651 L
"ReactOS Device Manager\r\nCopyright Ged Murphy 2015",
653 MB_OK
| MB_APPLMODAL
);
655 // Set focus back to the treeview
656 m_DeviceView
->SetFocus();
662 // Post a close message to the window
663 PostMessageW(m_hMainWnd
,
671 // We didn't handle it
680 CDeviceManager::OnDestroy(void)
682 // Uninitialize the device view
683 m_DeviceView
->Uninitialize();
689 // Clear the user data pointer
690 SetWindowLongPtr(m_hMainWnd
, GWLP_USERDATA
, 0);
692 // Break the message loop
699 CDeviceManager::MainWndProc(_In_ HWND hwnd
,
704 CDeviceManager
*This
;
707 // Get the object pointer from window context
708 This
= (CDeviceManager
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
711 // Check that this isn't a create message
712 if (msg
!= WM_CREATE
)
714 // Don't handle null info pointer
715 goto HandleDefaultMessage
;
723 // Get the object pointer from the create param
724 This
= (CDeviceManager
*)((LPCREATESTRUCT
)lParam
)->lpCreateParams
;
726 // Store the pointer in the window's global user data
727 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)This
);
729 // Call the create handler
730 RetCode
= This
->OnCreate(hwnd
);
736 RetCode
= This
->OnSize();
742 RetCode
= This
->OnNotify(lParam
);
748 RetCode
= This
->OnContext(lParam
);
754 if (This
->m_hStatusBar
!= NULL
)
756 if (!This
->MainWndMenuHint(LOWORD(wParam
),
758 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
761 This
->MainWndMenuHint(LOWORD(wParam
),
763 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
773 // Handle the command message
774 RetCode
= This
->OnCommand(wParam
, lParam
);
777 // Hand it off to the default message handler
778 goto HandleDefaultMessage
;
783 case WM_ENTERMENULOOP
:
785 This
->UpdateStatusBar(true);
789 case WM_EXITMENULOOP
:
791 This
->UpdateStatusBar(false);
797 // Destroy the main window
805 // Call the destroy handler
806 RetCode
= This
->OnDestroy();
812 HandleDefaultMessage
:
813 RetCode
= DefWindowProc(hwnd
, msg
, wParam
, lParam
);