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 #define REFRESH_TIMER 1
26 HINSTANCE g_hThisInstance
= NULL
;
27 HINSTANCE g_hParentInstance
= NULL
;
30 static const MENU_HINT MainMenuHintTable
[] =
33 { IDC_EXIT
, IDS_HINT_EXIT
},
36 { IDC_PROPERTIES
, IDS_HINT_PROPERTIES
},
37 { IDC_SCAN_HARDWARE
, IDS_HINT_SCAN
},
38 { IDC_ENABLE_DRV
, IDS_HINT_ENABLE
},
39 { IDC_DISABLE_DRV
, IDS_HINT_DISABLE
},
40 { IDC_UPDATE_DRV
, IDS_HINT_UPDATE
},
41 { IDC_UNINSTALL_DRV
, IDS_HINT_UNINSTALL
},
42 { IDC_ADD_HARDWARE
, IDS_HINT_ADD
},
46 { IDC_DEVBYTYPE
, IDS_HINT_DEV_BY_TYPE
},
47 { IDC_DEVBYCONN
, IDS_HINT_DEV_BY_CONN
},
48 { IDC_RESBYTYPE
, IDS_HINT_RES_BY_TYPE
},
49 { IDC_RESBYCONN
, IDS_HINT_RES_BY_TYPE
},
50 { IDC_SHOWHIDDEN
, IDS_HINT_SHOW_HIDDEN
},
52 { IDC_ABOUT
, IDS_HINT_ABOUT
}
58 static const MENU_HINT SystemMenuHintTable
[] =
60 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
61 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
62 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
63 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
64 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
65 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
}
68 static TBBUTTON TbButtons
[] =
70 { BTN_PROPERTIES
, IDC_PROPERTIES
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
71 { BTN_SCAN_HARDWARE
, IDC_SCAN_HARDWARE
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
72 { 2, IDC_STATIC
, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0 },
73 { BTN_ENABLE_DRV
, IDC_ENABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
74 { BTN_DISABLE_DRV
, IDC_DISABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
75 { BTN_UPDATE_DRV
, IDC_UPDATE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 },
76 { BTN_UNINSTALL_DRV
, IDC_UNINSTALL_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }
80 /* PUBLIC METHODS **********************************************/
82 CDeviceManager::CDeviceManager(void) :
87 m_RefreshPending(false)
89 m_szMainWndClass
= L
"DevMgmtWndClass";
92 CDeviceManager::~CDeviceManager(void)
98 CDeviceManager::Create(_In_ HWND
/*hWndParent*/,
100 _In_opt_z_ LPCWSTR
/*lpMachineName*/,
103 CDeviceManager MainWindow
;
104 INITCOMMONCONTROLSEX icex
;
105 CAtlStringW szAppName
;
108 // Store the instances
109 g_hParentInstance
= hInst
;
110 g_hThisInstance
= GetModuleHandleW(L
"devmgr.dll");
112 // Initialize common controls
113 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
114 icex
.dwICC
= ICC_BAR_CLASSES
| ICC_COOL_CLASSES
;
115 InitCommonControlsEx(&icex
);
117 // Load the application name
118 if (szAppName
.LoadStringW(g_hThisInstance
, IDS_APPNAME
))
120 // Initialize the main window
121 if (MainWindow
.Initialize(szAppName
, nCmdShow
))
123 // Run the application
124 Ret
= MainWindow
.Run();
126 // Uninitialize the main window
127 MainWindow
.Uninitialize();
136 /* PRIVATE METHODS **********************************************/
139 CDeviceManager::Initialize(_In_z_ LPCTSTR lpCaption
,
142 CAtlStringW szCaption
;
143 WNDCLASSEXW wc
= {0};
145 // Store the show window value
146 m_CmdShow
= nCmdShow
;
148 // Setup the window class struct
149 wc
.cbSize
= sizeof(WNDCLASSEXW
);
150 wc
.lpfnWndProc
= MainWndProc
;
151 wc
.hInstance
= g_hThisInstance
;
152 wc
.hIcon
= LoadIcon(g_hThisInstance
, MAKEINTRESOURCEW(IDI_MAIN_ICON
));
153 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
154 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
155 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDR_MAINMENU
);
156 wc
.lpszClassName
= m_szMainWndClass
;
157 wc
.hIconSm
= (HICON
)LoadImage(g_hThisInstance
,
158 MAKEINTRESOURCE(IDI_MAIN_ICON
),
164 // Register the window
165 if (RegisterClassExW(&wc
))
167 // Create the main window and store the object pointer
168 m_hMainWnd
= CreateWindowExW(WS_EX_WINDOWEDGE
,
171 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
182 // Return creation result
183 return !!(m_hMainWnd
);
187 CDeviceManager::Uninitialize(void)
189 // Unregister the window class
190 UnregisterClassW(m_szMainWndClass
, g_hThisInstance
);
194 CDeviceManager::Run(void)
198 // Pump the message queue
199 while (GetMessageW(&Msg
, NULL
, 0, 0 ) != 0)
201 TranslateMessage(&Msg
);
202 DispatchMessageW(&Msg
);
209 CDeviceManager::MainWndMenuHint(_In_ WORD CmdId
,
210 _In_
const MENU_HINT
*HintArray
,
211 _In_ DWORD HintsCount
,
215 const MENU_HINT
*LastHint
;
216 UINT HintId
= DefHintId
;
218 LastHint
= HintArray
+ HintsCount
;
219 while (HintArray
!= LastHint
)
221 if (HintArray
->CmdId
== CmdId
)
223 HintId
= HintArray
->HintId
;
230 StatusBarLoadString(m_hStatusBar
,
239 CDeviceManager::UpdateStatusBar(_In_
bool InMenuLoop
)
241 SendMessageW(m_hStatusBar
,
248 CDeviceManager::RefreshView(_In_ ViewType Type
)
252 // Refreshed the cached view
253 m_DeviceView
->Refresh(Type
, FALSE
, TRUE
, NULL
);
255 // Get the menu item id
258 case DevicesByType
: CheckId
= IDC_DEVBYTYPE
; break;
259 case DevicesByConnection
: CheckId
= IDC_DEVBYCONN
; break;
260 case ResourcesByType
: CheckId
= IDC_RESBYTYPE
; break;
261 case ResourcesByConnection
: CheckId
= IDC_RESBYCONN
; break;
262 default: ATLASSERT(FALSE
); break;
265 // Set the new check item
266 CheckMenuRadioItem(m_hMenu
,
276 CDeviceManager::CreateToolBar(void)
278 TBADDBITMAP TbAddBitmap
;
280 DWORD dwStyles
= WS_CHILDWINDOW
| TBSTYLE_FLAT
| TBSTYLE_WRAPABLE
| TBSTYLE_TOOLTIPS
| CCS_NODIVIDER
;
281 DWORD dwExStyles
= WS_EX_LEFT
| WS_EX_LTRREADING
| WS_EX_RIGHTSCROLLBAR
;
283 // Create the toolbar window
284 m_hToolBar
= CreateWindowExW(dwExStyles
,
293 if (m_hToolBar
== NULL
) return FALSE
;
295 // Don't show clipped buttons
296 SendMessageW(m_hToolBar
,
299 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
301 SendMessageW(m_hToolBar
, TB_SETBITMAPSIZE
, 0, MAKELONG(16, 16));
303 // Set the struct size, the toobar needs this...
304 SendMessageW(m_hToolBar
,
309 TbAddBitmap
.hInst
= g_hThisInstance
;
310 TbAddBitmap
.nID
= IDB_TOOLBAR
;
311 SendMessageW(m_hToolBar
, TB_ADDBITMAP
, _countof(TbButtons
), (LPARAM
)&TbAddBitmap
);
313 SendMessageW(m_hToolBar
, TB_ADDBUTTONSW
, _countof(TbButtons
), (LPARAM
)TbButtons
);
314 SendMessageW(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
318 ShowWindow(m_hToolBar
, SW_SHOW
);
325 CDeviceManager::CreateStatusBar(void)
327 int StatWidths
[] = {110, -1}; // widths of status bar
330 // Create the status bar
331 m_hStatusBar
= CreateWindowExW(0,
334 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
337 (HMENU
)IDC_STATUSBAR
,
343 bRet
= (SendMessageW(m_hStatusBar
,
345 sizeof(StatWidths
) / sizeof(int),
346 (LPARAM
)StatWidths
) != 0);
352 void CDeviceManager::UpdateToolbar()
356 CNode
*Node
= m_DeviceView
->GetSelectedNode();
359 if (Node
->HasProperties())
361 State
= TBSTATE_ENABLED
;
365 State
= TBSTATE_HIDDEN
;
367 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_PROPERTIES
, MAKELPARAM(State
, 0));
368 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UPDATE_DRV
, MAKELPARAM(State
, 0)); //hack
369 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UNINSTALL_DRV
, MAKELPARAM(State
, 0)); // hack
371 // enable driver button
372 if (Node
->GetNodeType() == DeviceNode
&&
373 dynamic_cast<CDeviceNode
*>(Node
)->IsDisabled())
375 State
= TBSTATE_ENABLED
;
379 State
= TBSTATE_HIDDEN
;
381 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_ENABLE_DRV
, MAKELPARAM(State
, 0));
383 // disable driver button
384 if (Node
->GetNodeType() == DeviceNode
&&
385 dynamic_cast<CDeviceNode
*>(Node
)->CanDisable() &&
386 !dynamic_cast<CDeviceNode
*>(Node
)->IsDisabled())
388 State
= TBSTATE_ENABLED
;
392 State
= TBSTATE_HIDDEN
;
394 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_DISABLE_DRV
, MAKELPARAM(State
, 0));
400 CDeviceManager::StatusBarLoadString(_In_ HWND hStatusBar
,
402 _In_ HINSTANCE hInstance
,
405 CAtlStringW szMessage
;
409 if (szMessage
.LoadStringW(hInstance
, uID
))
411 // Show the string on the status bar
412 bRet
= (SendMessageW(hStatusBar
,
415 (LPARAM
)szMessage
.GetBuffer()) != 0);
422 CDeviceManager::OnCreate(_In_ HWND hwnd
)
429 // Store a handle to the main menu
430 m_hMenu
= GetMenu(m_hMainWnd
);
432 // Create the toolbar and statusbar
433 if (CreateToolBar() && CreateStatusBar())
435 // Create the device view object
436 m_DeviceView
= new CDeviceView(m_hMainWnd
);
437 if (m_DeviceView
->Initialize())
439 // Do the initial scan
440 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
445 // Display the window according to the user request
446 ShowWindow(hwnd
, m_CmdShow
);
455 CDeviceManager::OnSize(void)
457 RECT rcClient
, rcTool
, rcStatus
;
458 INT lvHeight
, iToolHeight
, iStatusHeight
;
460 // Autosize the toolbar
461 SendMessage(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
463 // Get the toolbar rect and save the height
464 GetWindowRect(m_hToolBar
, &rcTool
);
465 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
467 // Resize the status bar
468 SendMessage(m_hStatusBar
, WM_SIZE
, 0, 0);
470 // Get the statusbar rect and save the height
471 GetWindowRect(m_hStatusBar
, &rcStatus
);
472 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
474 // Get the full client rect
475 GetClientRect(m_hMainWnd
, &rcClient
);
477 // Calculate the remaining height for the treeview
478 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
480 // Resize the device view
481 m_DeviceView
->OnSize(0,
490 CDeviceManager::OnNotify(_In_ LPARAM lParam
)
492 LPNMHDR NmHdr
= (LPNMHDR
)lParam
;
505 m_DeviceView
->DisplayPropertySheet();
511 Ret
= m_DeviceView
->OnRightClick(NmHdr
);
517 m_DeviceView
->DisplayPropertySheet();
521 case TTN_GETDISPINFO
:
523 LPTOOLTIPTEXT lpttt
= (LPTOOLTIPTEXT
)lParam
;
524 lpttt
->hinst
= g_hThisInstance
;
526 UINT_PTR idButton
= lpttt
->hdr
.idFrom
;
530 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_PROPERTIES
);
532 case IDC_SCAN_HARDWARE
:
533 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_SCAN
);
536 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_ENABLE
);
538 case IDC_DISABLE_DRV
:
539 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_DISABLE
);
542 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_UPDATE
);
544 case IDC_UNINSTALL_DRV
:
545 lpttt
->lpszText
= MAKEINTRESOURCEW(IDS_TOOLTIP_UNINSTALL
);
557 CDeviceManager::OnContext(_In_ LPARAM lParam
)
559 return m_DeviceView
->OnContextMenu(lParam
);
563 CDeviceManager::OnCommand(_In_ WPARAM wParam
,
564 _In_ LPARAM
/*lParam*/)
570 Msg
= LOWORD(wParam
);
575 case IDC_SCAN_HARDWARE
:
577 case IDC_DISABLE_DRV
:
579 case IDC_UNINSTALL_DRV
:
580 case IDC_ADD_HARDWARE
:
582 m_DeviceView
->OnAction(Msg
);
588 // Create a popup menu with all the actions for the selected node
589 HMENU hMenu
= CreatePopupMenu();
590 m_DeviceView
->CreateActionMenu(hMenu
, true);
592 // Calculate where to put the menu
594 GetMenuItemRect(m_hMainWnd
, m_hMenu
, 1, &rc
);
595 LONG Height
= rc
.bottom
- rc
.top
;
598 TrackPopupMenuEx(hMenu
,
611 RefreshView(DevicesByType
);
617 RefreshView(DevicesByConnection
);
623 // Get the current state
624 UINT CurCheckState
= GetMenuState(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
);
625 if (CurCheckState
== MF_CHECKED
)
627 m_DeviceView
->SetHiddenDevices(false);
628 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| MF_UNCHECKED
);
630 else if (CurCheckState
== MF_UNCHECKED
)
632 m_DeviceView
->SetHiddenDevices(true);
633 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| MF_CHECKED
);
635 // Refresh the device view
636 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
646 MessageBoxW(m_hMainWnd
,
647 L
"ReactOS Device Manager\r\nCopyright Ged Murphy 2015",
649 MB_OK
| MB_APPLMODAL
);
651 // Set focus back to the treeview
652 m_DeviceView
->SetFocus();
658 // Post a close message to the window
659 PostMessageW(m_hMainWnd
,
667 // We didn't handle it
676 CDeviceManager::OnDestroy(void)
678 // Uninitialize the device view
679 m_DeviceView
->Uninitialize();
685 // Clear the user data pointer
686 SetWindowLongPtr(m_hMainWnd
, GWLP_USERDATA
, 0);
688 // Break the message loop
695 CDeviceManager::MainWndProc(_In_ HWND hwnd
,
700 CDeviceManager
*This
;
703 // Get the object pointer from window context
704 This
= (CDeviceManager
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
707 // Check that this isn't a create message
708 if (msg
!= WM_CREATE
)
710 // Don't handle null info pointer
711 goto HandleDefaultMessage
;
719 // Get the object pointer from the create param
720 This
= (CDeviceManager
*)((LPCREATESTRUCT
)lParam
)->lpCreateParams
;
722 // Store the pointer in the window's global user data
723 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)This
);
725 // Call the create handler
726 RetCode
= This
->OnCreate(hwnd
);
732 RetCode
= This
->OnSize();
738 RetCode
= This
->OnNotify(lParam
);
744 RetCode
= This
->OnContext(lParam
);
750 if (This
->m_hStatusBar
!= NULL
)
752 if (!This
->MainWndMenuHint(LOWORD(wParam
),
754 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
757 This
->MainWndMenuHint(LOWORD(wParam
),
759 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
769 // Handle the command message
770 RetCode
= This
->OnCommand(wParam
, lParam
);
773 // Hand it off to the default message handler
774 goto HandleDefaultMessage
;
779 case WM_DEVICECHANGE
:
781 if (wParam
== DBT_DEVNODES_CHANGED
)
784 // The OS can send multiple change messages in quick sucsession. To avoid
785 // refreshing multiple times (and to avoid waiting in the message thread)
786 // we set a timer to run in 500ms, which should leave enough time for all
787 // the messages to come through. Wrap so we don't set multiple timers
789 if (InterlockedCompareExchange((LONG
*)&This
->m_RefreshPending
, TRUE
, FALSE
) == FALSE
)
791 SetTimer(hwnd
, REFRESH_TIMER
, 500, NULL
);
798 if (wParam
== REFRESH_TIMER
)
800 // Schedule a refresh (this just creates a thread and returns)
801 This
->m_DeviceView
->Refresh(This
->m_DeviceView
->GetCurrentView(),
807 KillTimer(hwnd
, REFRESH_TIMER
);
809 // Allow more change notifications
810 InterlockedExchange((LONG
*)&This
->m_RefreshPending
, FALSE
);
815 case WM_ENTERMENULOOP
:
817 This
->UpdateStatusBar(true);
821 case WM_EXITMENULOOP
:
823 This
->UpdateStatusBar(false);
829 // Destroy the main window
837 // Call the destroy handler
838 RetCode
= This
->OnDestroy();
844 HandleDefaultMessage
:
845 RetCode
= DefWindowProc(hwnd
, msg
, wParam
, lParam
);