2 * PROJECT: ReactOS Device Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/devmgr/devmgr/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
26 static const MENU_HINT MainMenuHintTable
[] =
29 { IDC_EXIT
, IDS_HINT_EXIT
},
32 { IDC_PROPERTIES
, IDS_HINT_PROPERTIES
},
33 { IDC_SCAN_HARDWARE
, IDS_HINT_SCAN
},
34 { IDC_ENABLE_DRV
, IDS_HINT_ENABLE
},
35 { IDC_DISABLE_DRV
, IDS_HINT_DISABLE
},
36 { IDC_UPDATE_DRV
, IDS_HINT_UPDATE
},
37 { IDC_UNINSTALL_DRV
, IDS_HINT_UNINSTALL
},
38 { IDC_ADD_HARDWARE
, IDS_HINT_ADD
},
42 { IDC_DEVBYTYPE
, IDS_HINT_DEV_BY_TYPE
},
43 { IDC_DEVBYCONN
, IDS_HINT_DEV_BY_CONN
},
44 { IDC_RESBYTYPE
, IDS_HINT_RES_BY_TYPE
},
45 { IDC_RESBYCONN
, IDS_HINT_RES_BY_TYPE
},
46 { IDC_SHOWHIDDEN
, IDS_HINT_SHOW_HIDDEN
},
48 { IDC_ABOUT
, IDS_HINT_ABOUT
}
54 static const MENU_HINT SystemMenuHintTable
[] =
56 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
57 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
58 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
59 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
60 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
61 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
},
64 static TBBUTTON TbButtons
[] =
66 { BTN_PROPERTIES
, IDC_PROPERTIES
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
67 { BTN_SCAN_HARDWARE
, IDC_SCAN_HARDWARE
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
68 { 2, IDC_STATIC
, TBSTATE_ENABLED
, BTNS_SEP
, 0, 0 },
69 { BTN_ENABLE_DRV
, IDC_ENABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
70 { BTN_DISABLE_DRV
, IDC_DISABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
71 { BTN_UPDATE_DRV
, IDC_UPDATE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
72 { BTN_UNINSTALL_DRV
, IDC_UNINSTALL_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 }
76 /* PUBLIC METHODS **********************************************/
78 CMainWindow::CMainWindow(void) :
79 m_ToolbarhImageList(NULL
),
85 m_szMainWndClass
= L
"DevMgmtWndClass";
88 CMainWindow::~CMainWindow(void)
90 // Destroy any previous list
91 if (m_ToolbarhImageList
) ImageList_Destroy(m_ToolbarhImageList
);
95 CMainWindow::Initialize(LPCTSTR lpCaption
,
98 CAtlStringW szCaption
;
101 // Store the show window value
102 m_CmdShow
= nCmdShow
;
104 // Setup the window class struct
105 wc
.cbSize
= sizeof(WNDCLASSEXW
);
106 wc
.lpfnWndProc
= MainWndProc
;
107 wc
.hInstance
= g_hInstance
;
108 wc
.hIcon
= LoadIcon(g_hInstance
, MAKEINTRESOURCEW(IDI_MAIN_ICON
));
109 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
110 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
111 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDR_MAINMENU
);
112 wc
.lpszClassName
= m_szMainWndClass
;
113 wc
.hIconSm
= (HICON
)LoadImage(g_hInstance
,
114 MAKEINTRESOURCE(IDI_MAIN_ICON
),
120 // Register the window
121 if (RegisterClassExW(&wc
))
123 // Create the main window and store the object pointer
124 m_hMainWnd
= CreateWindowExW(WS_EX_WINDOWEDGE
,
127 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
138 // Return creation result
139 return !!(m_hMainWnd
);
143 CMainWindow::Uninitialize()
145 // Unregister the window class
146 UnregisterClassW(m_szMainWndClass
, g_hInstance
);
154 // Pump the message queue
155 while (GetMessageW(&Msg
, NULL
, 0, 0 ) != 0)
157 TranslateMessage(&Msg
);
158 DispatchMessageW(&Msg
);
165 /* PRIVATE METHODS **********************************************/
168 CMainWindow::MainWndMenuHint(WORD CmdId
,
169 const MENU_HINT
*HintArray
,
174 const MENU_HINT
*LastHint
;
175 UINT HintId
= DefHintId
;
177 LastHint
= HintArray
+ HintsCount
;
178 while (HintArray
!= LastHint
)
180 if (HintArray
->CmdId
== CmdId
)
182 HintId
= HintArray
->HintId
;
189 StatusBarLoadString(m_hStatusBar
,
198 CMainWindow::UpdateStatusBar(
202 SendMessageW(m_hStatusBar
,
209 CMainWindow::RefreshView(ViewType Type
)
214 // Refreshed the cached view
215 m_DeviceView
->Refresh(Type
, FALSE
, TRUE
, NULL
);
217 // Get the menu item id
220 case DevicesByType
: CheckId
= IDC_DEVBYTYPE
; break;
221 case DevicesByConnection
: CheckId
= IDC_DEVBYCONN
; break;
222 case ResourcesByType
: CheckId
= IDC_RESBYTYPE
; break;
223 case ResourcesByConnection
: CheckId
= IDC_RESBYCONN
; break;
224 default: ATLASSERT(FALSE
); break;
227 // Set the new check item
228 bSuccess
= CheckMenuRadioItem(m_hMenu
,
238 CMainWindow::ScanForHardwareChanges()
240 // Refresh the cache and and display
241 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
249 CMainWindow::CreateToolBar()
251 TBADDBITMAP TbAddBitmap
;
254 DWORD dwStyles
= WS_CHILDWINDOW
| TBSTYLE_FLAT
| TBSTYLE_WRAPABLE
| TBSTYLE_TOOLTIPS
| CCS_NODIVIDER
;
255 DWORD dwExStyles
= WS_EX_LEFT
| WS_EX_LTRREADING
| WS_EX_RIGHTSCROLLBAR
;
257 // Create the toolbar window
258 m_hToolBar
= CreateWindowExW(dwExStyles
,
267 if (m_hToolBar
== NULL
) return FALSE
;
269 // Don't show clipped buttons
270 SendMessageW(m_hToolBar
,
273 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
275 SendMessageW(m_hToolBar
, TB_SETBITMAPSIZE
, 0, MAKELONG(16, 16));
277 // Set the struct size, the toobar needs this...
278 SendMessageW(m_hToolBar
,
283 TbAddBitmap
.hInst
= g_hInstance
;
284 TbAddBitmap
.nID
= IDB_TOOLBAR
;
285 Index
= SendMessageW(m_hToolBar
, TB_ADDBITMAP
, _countof(TbButtons
), (LPARAM
)&TbAddBitmap
);
287 SendMessageW(m_hToolBar
, TB_ADDBUTTONSW
, _countof(TbButtons
), (LPARAM
)TbButtons
);
288 SendMessageW(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
292 ShowWindow(m_hToolBar
, SW_SHOW
);
299 CMainWindow::CreateStatusBar()
301 int StatWidths
[] = {110, -1}; // widths of status bar
304 // Create the status bar
305 m_hStatusBar
= CreateWindowExW(0,
308 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
311 (HMENU
)IDC_STATUSBAR
,
317 bRet
= (SendMessageW(m_hStatusBar
,
319 sizeof(StatWidths
) / sizeof(int),
320 (LPARAM
)StatWidths
) != 0);
326 void CMainWindow::UpdateUiContext(_In_ LPTV_ITEMW TvItem
)
331 if (m_DeviceView
->HasProperties(TvItem
))
333 State
= TBSTATE_ENABLED
;
337 State
= TBSTATE_HIDDEN
;
339 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_PROPERTIES
, MAKELPARAM(State
, 0));
340 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UPDATE_DRV
, MAKELPARAM(State
, 0)); //hack
341 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UNINSTALL_DRV
, MAKELPARAM(State
, 0)); // hack
343 // enable driver button
344 if (m_DeviceView
->IsDisabled(TvItem
))
346 State
= TBSTATE_ENABLED
;
350 State
= TBSTATE_HIDDEN
;
352 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_ENABLE_DRV
, MAKELPARAM(State
, 0));
354 // disable driver button
355 if (m_DeviceView
->CanDisable(TvItem
) && !m_DeviceView
->IsDisabled(TvItem
))
357 State
= TBSTATE_ENABLED
;
361 State
= TBSTATE_HIDDEN
;
363 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_DISABLE_DRV
, MAKELPARAM(State
, 0));
374 CMainWindow::StatusBarLoadString(IN HWND hStatusBar
,
376 IN HINSTANCE hInstance
,
379 CAtlStringW szMessage
;
383 if (szMessage
.LoadStringW(hInstance
, uID
))
385 // Show the string on the status bar
386 bRet
= (SendMessageW(hStatusBar
,
389 (LPARAM
)szMessage
.GetBuffer()) != 0);
396 CMainWindow::OnCreate(HWND hwnd
)
403 // Store a handle to the main menu
404 m_hMenu
= GetMenu(m_hMainWnd
);
406 // Create the toolbar and statusbar
407 if (CreateToolBar() && CreateStatusBar())
409 // Create the device view object
410 m_DeviceView
= new CDeviceView(m_hMainWnd
);
411 if (m_DeviceView
->Initialize())
413 // Do the initial scan
414 ScanForHardwareChanges();
416 // Display the window according to the user request
417 ShowWindow(hwnd
, m_CmdShow
);
426 CMainWindow::OnSize()
428 RECT rcClient
, rcTool
, rcStatus
;
429 INT lvHeight
, iToolHeight
, iStatusHeight
;
431 // Autosize the toolbar
432 SendMessage(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
434 // Get the toolbar rect and save the height
435 GetWindowRect(m_hToolBar
, &rcTool
);
436 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
438 // Resize the status bar
439 SendMessage(m_hStatusBar
, WM_SIZE
, 0, 0);
441 // Get the statusbar rect and save the height
442 GetWindowRect(m_hStatusBar
, &rcStatus
);
443 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
445 // Get the full client rect
446 GetClientRect(m_hMainWnd
, &rcClient
);
448 // Calculate the remaining height for the treeview
449 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
451 // Resize the device view
452 m_DeviceView
->OnSize(0,
461 CMainWindow::OnNotify(LPARAM lParam
)
463 LPNMHDR NmHdr
= (LPNMHDR
)lParam
;
470 LPNMTREEVIEW NmTreeView
= (LPNMTREEVIEW
)lParam
;
471 UpdateUiContext(&NmTreeView
->itemNew
);
477 LPNMTREEVIEW NmTreeView
= (LPNMTREEVIEW
)lParam
;
478 m_DeviceView
->DisplayPropertySheet();
484 Ret
= m_DeviceView
->OnRightClick(NmHdr
);
490 m_DeviceView
->DisplayPropertySheet();
494 case TTN_GETDISPINFO
:
496 LPTOOLTIPTEXT lpttt
= (LPTOOLTIPTEXT
)lParam
;
498 UINT_PTR idButton
= lpttt
->hdr
.idFrom
;
502 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_PROPERTIES
);
504 case IDC_SCAN_HARDWARE
:
505 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_SCAN
);
508 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ENABLE
);
510 case IDC_DISABLE_DRV
:
511 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_DIABLE
);
514 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_UPDATE
);
516 case IDC_UNINSTALL_DRV
:
517 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_UNINSTALL
);
528 CMainWindow::OnContext(LPARAM lParam
)
530 return m_DeviceView
->OnContextMenu(lParam
);
534 CMainWindow::OnCommand(WPARAM wParam
,
541 Msg
= LOWORD(wParam
);
547 m_DeviceView
->DisplayPropertySheet();
551 case IDC_SCAN_HARDWARE
:
553 ScanForHardwareChanges();
560 if (m_DeviceView
->EnableSelectedDevice(true, NeedsReboot
) &&
563 MessageBox(m_hMainWnd
, L
"Rebooting", L
"Enable", MB_OK
);
568 case IDC_DISABLE_DRV
:
571 m_DeviceView
->EnableSelectedDevice(false, NeedsReboot
);
577 MessageBox(m_hMainWnd
, L
"Not yet implemented", L
"Update Driver", MB_OK
);
581 case IDC_UNINSTALL_DRV
:
583 MessageBox(m_hMainWnd
, L
"Not yet implemented", L
"Uninstall Driver", MB_OK
);
587 case IDC_ADD_HARDWARE
:
589 MessageBox(m_hMainWnd
, L
"Not yet implemented", L
"Add Hardware", MB_OK
);
595 RefreshView(DevicesByType
);
601 RefreshView(DevicesByConnection
);
607 // Get the current state
608 UINT CurCheckState
= GetMenuState(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
);
609 if (CurCheckState
== MF_CHECKED
)
611 m_DeviceView
->SetHiddenDevices(false);
612 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| MF_UNCHECKED
);
614 else if (CurCheckState
== MF_UNCHECKED
)
616 m_DeviceView
->SetHiddenDevices(true);
617 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| MF_CHECKED
);
619 // Refresh the device view
620 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
630 MessageBoxW(m_hMainWnd
,
631 L
"ReactOS Device Manager\r\nCopyright Ged Murphy 2015",
633 MB_OK
| MB_APPLMODAL
);
635 // Set focus back to the treeview
636 m_DeviceView
->SetFocus();
642 // Post a close message to the window
643 PostMessageW(m_hMainWnd
,
651 // We didn't handle it
660 CMainWindow::OnDestroy()
662 // Uninitialize the device view
663 m_DeviceView
->Uninitialize();
669 // Clear the user data pointer
670 SetWindowLongPtr(m_hMainWnd
, GWLP_USERDATA
, 0);
672 // Break the message loop
679 CMainWindow::MainWndProc(HWND hwnd
,
687 // Get the object pointer from window context
688 This
= (CMainWindow
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
691 // Check that this isn't a create message
692 if (msg
!= WM_CREATE
)
694 // Don't handle null info pointer
695 goto HandleDefaultMessage
;
703 // Get the object pointer from the create param
704 This
= (CMainWindow
*)((LPCREATESTRUCT
)lParam
)->lpCreateParams
;
706 // Store the pointer in the window's global user data
707 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)This
);
709 // Call the create handler
710 RetCode
= This
->OnCreate(hwnd
);
716 RetCode
= This
->OnSize();
722 RetCode
= This
->OnNotify(lParam
);
728 RetCode
= This
->OnContext(lParam
);
734 if (This
->m_hStatusBar
!= NULL
)
736 if (!This
->MainWndMenuHint(LOWORD(wParam
),
738 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
741 This
->MainWndMenuHint(LOWORD(wParam
),
743 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
753 // Handle the command message
754 RetCode
= This
->OnCommand(wParam
, lParam
);
757 // Hand it off to the default message handler
758 goto HandleDefaultMessage
;
763 case WM_ENTERMENULOOP
:
765 This
->UpdateStatusBar(true);
769 case WM_EXITMENULOOP
:
771 This
->UpdateStatusBar(false);
777 // Destroy the main window
785 // Call the destroy handler
786 RetCode
= This
->OnDestroy();
792 HandleDefaultMessage
:
793 RetCode
= DefWindowProc(hwnd
, msg
, wParam
, lParam
);
802 //////// MOVE ME ////////////////
804 HINSTANCE g_hInstance
= NULL
;
805 HANDLE ProcessHeap
= NULL
;
809 DeviceManager_ExecuteW(HWND hWndParent
,
811 LPCWSTR lpMachineName
,
814 CMainWindow MainWindow
;
815 INITCOMMONCONTROLSEX icex
;
816 CAtlStringW szAppName
;
819 // Store the global values
821 ProcessHeap
= GetProcessHeap();
823 // Initialize common controls
824 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
825 icex
.dwICC
= ICC_BAR_CLASSES
| ICC_COOL_CLASSES
;
826 InitCommonControlsEx(&icex
);
828 // Load the application name
829 if (szAppName
.LoadStringW(g_hInstance
, IDS_APPNAME
))
831 // Initialize the main window
832 if (MainWindow
.Initialize(szAppName
, nCmdShow
))
834 // Run the application
835 Ret
= MainWindow
.Run();
837 // Uninitialize the main window
838 MainWindow
.Uninitialize();