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_SEPERATOR -1
20 #define BTN_ENABLE_DRV 2
21 #define BTN_DISABLE_DRV 3
22 #define BTN_UPDATE_DRV 4
23 #define BTN_UNINSTALL_DRV 5
27 static const MENU_HINT MainMenuHintTable
[] =
30 { IDC_EXIT
, IDS_HINT_EXIT
},
33 { IDC_UPDATE_DRV
, NULL
},
34 { IDC_DISABLE_DRV
, NULL
},
35 { IDC_UNINSTALL_DRV
, NULL
},
36 { IDC_SCAN_HARDWARE
, IDS_HINT_REFRESH
},
37 { IDC_ADD_HARDWARE
, NULL
},
38 { IDC_PROPERTIES
, IDS_HINT_PROP
},
41 { IDC_DEVBYTYPE
, IDS_HINT_DEV_BY_TYPE
},
42 { IDC_DEVBYCONN
, IDS_HINT_DEV_BY_CONN
},
43 { IDC_RESBYTYPE
, IDS_HINT_RES_BY_TYPE
},
44 { IDC_RESBYCONN
, IDS_HINT_RES_BY_TYPE
},
46 { IDC_ABOUT
, IDS_HINT_ABOUT
}
51 static const MENU_HINT SystemMenuHintTable
[] =
53 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
54 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
55 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
56 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
57 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
58 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
},
61 static TBBUTTON TbButtons
[] =
63 { BTN_PROPERTIES
, IDC_PROPERTIES
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
64 { BTN_SCAN_HARDWARE
, IDC_SCAN_HARDWARE
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
65 { 2, IDC_STATIC
, TBSTATE_ENABLED
, BTNS_SEP
, 0, 0 },
66 { BTN_ENABLE_DRV
, IDC_ENABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
67 { BTN_DISABLE_DRV
, IDC_DISABLE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
68 { BTN_UPDATE_DRV
, IDC_UPDATE_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 },
69 { BTN_UNINSTALL_DRV
, IDC_UNINSTALL_DRV
, TBSTATE_ENABLED
, BTNS_BUTTON
, 0, 0 }
73 /* PUBLIC METHODS **********************************************/
75 CMainWindow::CMainWindow(void) :
76 m_ToolbarhImageList(NULL
),
82 m_szMainWndClass
= L
"DevMgmtWndClass";
85 CMainWindow::~CMainWindow(void)
87 // Destroy any previous list
88 if (m_ToolbarhImageList
) ImageList_Destroy(m_ToolbarhImageList
);
92 CMainWindow::Initialize(LPCTSTR lpCaption
,
95 CAtlStringW szCaption
;
98 // Store the show window value
101 // Setup the window class struct
102 wc
.cbSize
= sizeof(WNDCLASSEXW
);
103 wc
.lpfnWndProc
= MainWndProc
;
104 wc
.hInstance
= g_hInstance
;
105 wc
.hIcon
= LoadIcon(g_hInstance
, MAKEINTRESOURCEW(IDI_MAIN_ICON
));
106 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
107 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
108 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDR_MAINMENU
);
109 wc
.lpszClassName
= m_szMainWndClass
;
110 wc
.hIconSm
= (HICON
)LoadImage(g_hInstance
,
111 MAKEINTRESOURCE(IDI_MAIN_ICON
),
117 // Register the window
118 if (RegisterClassExW(&wc
))
120 // Create the main window and store the object pointer
121 m_hMainWnd
= CreateWindowExW(WS_EX_WINDOWEDGE
,
124 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
135 // Return creation result
136 return !!(m_hMainWnd
);
140 CMainWindow::Uninitialize()
142 // Unregister the window class
143 UnregisterClassW(m_szMainWndClass
, g_hInstance
);
151 // Pump the message queue
152 while (GetMessageW(&Msg
, NULL
, 0, 0 ) != 0)
154 TranslateMessage(&Msg
);
155 DispatchMessageW(&Msg
);
162 /* PRIVATE METHODS **********************************************/
165 CMainWindow::MainWndMenuHint(WORD CmdId
,
166 const MENU_HINT
*HintArray
,
171 const MENU_HINT
*LastHint
;
172 UINT HintId
= DefHintId
;
174 LastHint
= HintArray
+ HintsCount
;
175 while (HintArray
!= LastHint
)
177 if (HintArray
->CmdId
== CmdId
)
179 HintId
= HintArray
->HintId
;
186 StatusBarLoadString(m_hStatusBar
,
195 CMainWindow::RefreshView(ViewType Type
)
200 // Refreshed the cached view
201 m_DeviceView
->Refresh(Type
, FALSE
, TRUE
);
203 // Get the menu item id
206 case DevicesByType
: CheckId
= IDC_DEVBYTYPE
; break;
207 case DevicesByConnection
: CheckId
= IDC_DEVBYCONN
; break;
208 case ResourcesByType
: CheckId
= IDC_RESBYTYPE
; break;
209 case ResourcesByConnection
: CheckId
= IDC_RESBYCONN
; break;
210 default: ATLASSERT(FALSE
); break;
213 // Set the new check item
214 bSuccess
= CheckMenuRadioItem(m_hMenu
,
224 CMainWindow::ScanForHardwareChanges()
226 // Refresh the cache and and display
227 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
237 CMainWindow::CreateToolBar()
239 TBADDBITMAP TbAddBitmap
;
242 DWORD dwStyles
= WS_CHILDWINDOW
| TBSTYLE_FLAT
| TBSTYLE_WRAPABLE
| TBSTYLE_TOOLTIPS
| CCS_NODIVIDER
;
243 DWORD dwExStyles
= WS_EX_LEFT
| WS_EX_LTRREADING
| WS_EX_RIGHTSCROLLBAR
;
245 // Create the toolbar window
246 m_hToolBar
= CreateWindowExW(dwExStyles
,
255 if (m_hToolBar
== NULL
) return FALSE
;
257 // Don't show clipped buttons
258 SendMessageW(m_hToolBar
,
261 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
263 SendMessageW(m_hToolBar
, TB_SETBITMAPSIZE
, 0, MAKELONG(16, 16));
265 // Set the struct size, the toobar needs this...
266 SendMessageW(m_hToolBar
,
271 TbAddBitmap
.hInst
= g_hInstance
;
272 TbAddBitmap
.nID
= IDB_TOOLBAR
;
273 Index
= SendMessageW(m_hToolBar
, TB_ADDBITMAP
, _countof(TbButtons
), (LPARAM
)&TbAddBitmap
);
275 SendMessageW(m_hToolBar
, TB_ADDBUTTONSW
, _countof(TbButtons
), (LPARAM
)TbButtons
);
276 SendMessageW(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
280 ShowWindow(m_hToolBar
, SW_SHOW
);
287 CMainWindow::CreateStatusBar()
289 int StatWidths
[] = {110, -1}; // widths of status bar
292 // Create the status bar
293 m_hStatusBar
= CreateWindowExW(0,
296 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
299 (HMENU
)IDC_STATUSBAR
,
305 bRet
= (SendMessageW(m_hStatusBar
,
307 sizeof(StatWidths
) / sizeof(int),
308 (LPARAM
)StatWidths
) != 0);
314 void CMainWindow::UpdateContext(_In_ LPTV_ITEMW TvItem
)
319 if (m_DeviceView
->HasProperties(TvItem
))
321 State
= TBSTATE_ENABLED
;
325 State
= TBSTATE_HIDDEN
;
327 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_PROPERTIES
, MAKELPARAM(State
, 0));
328 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UPDATE_DRV
, MAKELPARAM(State
, 0)); //hack
329 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_UNINSTALL_DRV
, MAKELPARAM(State
, 0)); // hack
331 // enable driver button
332 if (m_DeviceView
->IsDisabled(TvItem
))
334 State
= TBSTATE_ENABLED
;
338 State
= TBSTATE_HIDDEN
;
340 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_ENABLE_DRV
, MAKELPARAM(State
, 0));
342 // disable driver button
343 if (m_DeviceView
->CanDisable(TvItem
) && !m_DeviceView
->IsDisabled(TvItem
))
345 State
= TBSTATE_ENABLED
;
349 State
= TBSTATE_HIDDEN
;
351 SendMessageW(m_hToolBar
, TB_SETSTATE
, IDC_DISABLE_DRV
, MAKELPARAM(State
, 0));
362 CMainWindow::StatusBarLoadString(IN HWND hStatusBar
,
364 IN HINSTANCE hInstance
,
367 CAtlStringW szMessage
;
371 if (szMessage
.LoadStringW(hInstance
, uID
))
373 // Show the string on the status bar
374 bRet
= (SendMessageW(hStatusBar
,
377 (LPARAM
)szMessage
.GetBuffer()) != 0);
384 CMainWindow::OnCreate(HWND hwnd
)
391 // Store a handle to the main menu
392 m_hMenu
= GetMenu(m_hMainWnd
);
394 // Create the toolbar and statusbar
395 if (CreateToolBar() && CreateStatusBar())
397 // Create the device view object
398 m_DeviceView
= new CDeviceView(m_hMainWnd
);
399 if (m_DeviceView
->Initialize())
401 // Do the initial scan
402 ScanForHardwareChanges();
404 // Display the window according to the user request
405 ShowWindow(hwnd
, m_CmdShow
);
414 CMainWindow::OnSize()
416 RECT rcClient
, rcTool
, rcStatus
;
417 INT lvHeight
, iToolHeight
, iStatusHeight
;
419 // Autosize the toolbar
420 SendMessage(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
422 // Get the toolbar rect and save the height
423 GetWindowRect(m_hToolBar
, &rcTool
);
424 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
426 // Resize the status bar
427 SendMessage(m_hStatusBar
, WM_SIZE
, 0, 0);
429 // Get the statusbar rect and save the height
430 GetWindowRect(m_hStatusBar
, &rcStatus
);
431 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
433 // Get the full client rect
434 GetClientRect(m_hMainWnd
, &rcClient
);
436 // Calculate the remaining height for the treeview
437 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
439 // Resize the device view
440 m_DeviceView
->Size(0,
449 CMainWindow::OnNotify(LPARAM lParam
)
451 LPNMHDR NmHdr
= (LPNMHDR
)lParam
;
457 LPNMTREEVIEW NmTreeView
= (LPNMTREEVIEW
)lParam
;
458 UpdateContext(&NmTreeView
->itemNew
);
462 case TVN_DELETEITEMW
:
464 LPNMTREEVIEW NmTreeView
= (LPNMTREEVIEW
)lParam
;
465 NmTreeView
->action
= NmTreeView
->action
;
472 m_DeviceView
->DisplayPropertySheet();
478 m_DeviceView
->DisplayPropertySheet();
487 CMainWindow::OnContext(LPARAM lParam
)
493 CMainWindow::OnCommand(WPARAM wParam
,
500 Msg
= LOWORD(wParam
);
506 m_DeviceView
->DisplayPropertySheet();
510 case IDC_SCAN_HARDWARE
:
512 ScanForHardwareChanges();
518 RefreshView(DevicesByType
);
524 RefreshView(DevicesByConnection
);
530 UINT CurCheckState
, NewCheckState
;
532 // Get the current state
533 CurCheckState
= GetMenuState(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
);
535 if (CurCheckState
== MF_CHECKED
)
537 // Inform the device view of the change
538 m_DeviceView
->ShowHiddenDevices(false);
539 NewCheckState
= MF_UNCHECKED
;
541 else if (CurCheckState
== MF_UNCHECKED
)
543 m_DeviceView
->ShowHiddenDevices(true);
544 NewCheckState
= MF_CHECKED
;
552 // Set the new check state
553 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| NewCheckState
);
555 // Refresh the device view
556 m_DeviceView
->Refresh(m_DeviceView
->GetCurrentView(),
565 MessageBoxW(m_hMainWnd
,
566 L
"ReactOS Device Manager\r\nCopyright Ged Murphy 2015",
568 MB_OK
| MB_APPLMODAL
);
570 // Set focus back to the treeview
571 m_DeviceView
->SetFocus();
577 // Post a close message to the window
578 PostMessageW(m_hMainWnd
,
586 // We didn't handle it
595 CMainWindow::OnDestroy()
597 // Uninitialize the device view
598 m_DeviceView
->Uninitialize();
604 // Clear the user data pointer
605 SetWindowLongPtr(m_hMainWnd
, GWLP_USERDATA
, 0);
607 // Break the message loop
614 CMainWindow::MainWndProc(HWND hwnd
,
622 // Get the object pointer from window context
623 pThis
= (CMainWindow
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
626 // Check that this isn't a create message
627 if (msg
!= WM_CREATE
)
629 // Don't handle null info pointer
630 goto HandleDefaultMessage
;
638 // Get the object pointer from the create param
639 pThis
= (CMainWindow
*)((LPCREATESTRUCT
)lParam
)->lpCreateParams
;
641 // Store the pointer in the window's global user data
642 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)pThis
);
644 // Call the create handler
645 RetCode
= pThis
->OnCreate(hwnd
);
651 RetCode
= pThis
->OnSize();
657 RetCode
= pThis
->OnNotify(lParam
);
663 RetCode
= pThis
->OnContext(lParam
);
669 if (pThis
->m_hStatusBar
!= NULL
)
671 if (!pThis
->MainWndMenuHint(LOWORD(wParam
),
673 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
676 pThis
->MainWndMenuHint(LOWORD(wParam
),
678 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
688 // Handle the command message
689 RetCode
= pThis
->OnCommand(wParam
, lParam
);
692 // Hand it off to the default message handler
693 goto HandleDefaultMessage
;
699 // Destroy the main window
706 // Call the destroy handler
707 RetCode
= pThis
->OnDestroy();
713 HandleDefaultMessage
:
714 RetCode
= DefWindowProc(hwnd
, msg
, wParam
, lParam
);
723 //////// MOVE ME ////////////////
725 HINSTANCE g_hInstance
= NULL
;
726 HANDLE ProcessHeap
= NULL
;
730 DeviceManager_ExecuteW(HWND hWndParent
,
732 LPCWSTR lpMachineName
,
735 CMainWindow MainWindow
;
736 INITCOMMONCONTROLSEX icex
;
737 CAtlStringW szAppName
;
740 // Store the global values
742 ProcessHeap
= GetProcessHeap();
744 // Initialize common controls
745 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
746 icex
.dwICC
= ICC_BAR_CLASSES
| ICC_COOL_CLASSES
;
747 InitCommonControlsEx(&icex
);
749 // Load the application name
750 if (szAppName
.LoadStringW(g_hInstance
, IDS_APPNAME
))
752 // Initialize the main window
753 if (MainWindow
.Initialize(szAppName
, nCmdShow
))
755 // Run the application
756 Ret
= MainWindow
.Run();
758 // Uninitialize the main window
759 MainWindow
.Uninitialize();