2 * PROJECT: ReactOS Device Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/devmgmt/deviceview.cpp
5 * PURPOSE: Implements main window
6 * COPYRIGHT: Copyright 2014 Ged Murphy <gedmurphy@gmail.com>
11 #include "MainWindow.h"
14 /* DATA *****************************************************/
17 static const MENU_HINT MainMenuHintTable
[] =
20 {IDC_EXIT
, IDS_HINT_EXIT
},
23 {IDC_REFRESH
, IDS_HINT_REFRESH
},
24 {IDC_PROP
, IDS_HINT_PROP
},
26 {IDC_ABOUT
, IDS_HINT_ABOUT
},
28 {IDC_DEVBYTYPE
, IDS_HINT_DEV_BY_TYPE
},
29 {IDC_DEVBYCONN
, IDS_HINT_DEV_BY_CONN
},
30 {IDC_RESBYTYPE
, IDS_HINT_RES_BY_TYPE
},
31 {IDC_RESBYCONN
, IDS_HINT_RES_BY_TYPE
}
35 /* system menu hints */
36 static const MENU_HINT SystemMenuHintTable
[] =
38 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
39 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
40 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
41 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
42 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
43 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
},
47 /* PUBLIC METHODS **********************************************/
49 CMainWindow::CMainWindow(void) :
53 m_ToolbarhImageList(NULL
),
56 m_szMainWndClass
= L
"DevMgmtWndClass";
59 CMainWindow::~CMainWindow(void)
61 /* Destroy any previous list */
62 if (m_ToolbarhImageList
) ImageList_Destroy(m_ToolbarhImageList
);
66 CMainWindow::Initialize(LPCTSTR lpCaption
,
71 /* Store the show window value */
74 /* Setup the window class struct */
75 wc
.cbSize
= sizeof(WNDCLASSEXW
);
76 wc
.lpfnWndProc
= MainWndProc
;
77 wc
.hInstance
= g_hInstance
;
78 wc
.hIcon
= LoadIcon(g_hInstance
, MAKEINTRESOURCEW(IDI_MAIN_ICON
));
79 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
80 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
81 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDR_MAINMENU
);
82 wc
.lpszClassName
= m_szMainWndClass
;
83 wc
.hIconSm
= (HICON
)LoadImage(g_hInstance
,
84 MAKEINTRESOURCE(IDI_MAIN_ICON
),
90 /* Register the window */
91 if (RegisterClassExW(&wc
))
93 /* Create the main window and store the info pointer */
94 m_hMainWnd
= CreateWindowExW(WS_EX_WINDOWEDGE
,
97 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
108 /* Return creation result */
109 return !!(m_hMainWnd
);
113 CMainWindow::Uninitialize()
115 /* Unregister the window class */
116 UnregisterClassW(m_szMainWndClass
, g_hInstance
);
124 /* Pump the message queue */
125 while (GetMessageW(&Msg
, NULL
, 0, 0 ) != 0)
127 TranslateMessage(&Msg
);
128 DispatchMessageW(&Msg
);
135 /* PRIVATE METHODS **********************************************/
139 CMainWindow::MainWndMenuHint(WORD CmdId
,
140 const MENU_HINT
*HintArray
,
145 const MENU_HINT
*LastHint
;
146 UINT HintId
= DefHintId
;
148 LastHint
= HintArray
+ HintsCount
;
149 while (HintArray
!= LastHint
)
151 if (HintArray
->CmdId
== CmdId
)
153 HintId
= HintArray
->HintId
;
160 StatusBarLoadString(m_hStatusBar
,
169 CMainWindow::UpdateDevicesDisplay(ListDevices List
)
173 /* Set the new type*/
174 m_DeviceView
->SetDeviceListType(List
);
176 /* Get the menu item id */
179 case DevicesByType
: CheckId
= IDC_DEVBYTYPE
; break;
180 case DevicesByConnection
: CheckId
= IDC_DEVBYCONN
; break;
181 case ResourcesByType
: CheckId
= IDC_RESBYTYPE
; break;
182 case ResourcesByConnection
: CheckId
= IDC_RESBYCONN
; break;
183 default: ATLASSERT(FALSE
); break;
186 /* Set the new check item */
187 CheckMenuRadioItem(m_hMenu
,
193 /* Refresh the view */
194 m_DeviceView
->Refresh();
200 CMainWindow::CreateToolBar()
202 HIMAGELIST hImageList
;
204 UINT StartResource
, EndResource
;
208 static TBBUTTON ToolbarButtons
[] =
210 {TBICON_PROP
, IDC_PROP
, TBSTATE_INDETERMINATE
, BTNS_BUTTON
, {0}, 0, 0},
211 {TBICON_REFRESH
, IDC_REFRESH
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
213 /* Add a seperator: First item for a seperator is its width in pixels */
214 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
217 /* Calculate the number of buttons */
218 NumButtons
= sizeof(ToolbarButtons
) / sizeof(ToolbarButtons
[0]);
220 /* Create the toolbar window */
221 m_hToolBar
= CreateWindowExW(0,
224 WS_CHILD
| WS_VISIBLE
| TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
,
230 if (m_hToolBar
== NULL
) return FALSE
;
232 /* Don't show clipped buttons */
233 SendMessageW(m_hToolBar
,
236 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
238 /* Set the struct size, the toobar needs this... */
239 SendMessageW(m_hToolBar
,
241 sizeof(ToolbarButtons
[0]),
244 /* Create the toolbar icon image list */
245 m_ToolbarhImageList
= ImageList_Create(16,
247 ILC_MASK
| ILC_COLOR24
,
250 if (m_ToolbarhImageList
== NULL
) return FALSE
;
252 /* Set the index endpoints */
253 StartResource
= IDB_PROP
;
254 EndResource
= IDB_REFRESH
;
256 /* Add all icons to the image list */
257 for (UINT i
= StartResource
; i
<= EndResource
; i
++)
259 /* Load the image resource */
260 hBitmap
= (HBITMAP
)LoadImage(g_hInstance
,
268 /* Add it to the image list */
269 ImageList_AddMasked(m_ToolbarhImageList
,
273 /* Delete the bitmap */
274 DeleteObject(hBitmap
);
278 /* Set the new image list */
279 hImageList
= (HIMAGELIST
)SendMessageW(m_hToolBar
,
282 (LPARAM
)m_ToolbarhImageList
);
284 /* Destroy any previous list */
285 if (hImageList
) ImageList_Destroy(hImageList
);
287 /* Add the buttons */
288 bRet
= (BOOL
)SendMessageW(m_hToolBar
,
291 (LPARAM
)ToolbarButtons
);
297 CMainWindow::CreateStatusBar()
299 INT StatWidths
[] = {110, -1}; /* widths of status bar */
302 /* Create the status bar */
303 m_hStatusBar
= CreateWindowExW(0,
306 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
309 (HMENU
)IDC_STATUSBAR
,
315 bRet
= (BOOL
)SendMessageW(m_hStatusBar
,
317 sizeof(StatWidths
) / sizeof(INT
),
325 CMainWindow::StatusBarLoadString(IN HWND hStatusBar
,
327 IN HINSTANCE hInstance
,
330 WCHAR szMessage
[4096];
333 /* Load the string */
334 if (LoadStringW(hInstance
, uID
, szMessage
, _countof(szMessage
)))
336 /* Send the message to the status bar */
337 bRet
= (BOOL
)SendMessageW(hStatusBar
,
347 CMainWindow::OnCreate(HWND hwnd
)
354 /* Store the window handle */
357 /* Get the menu handle */
358 m_hMenu
= GetMenu(m_hMainWnd
);
360 /* Create the toolbar */
361 if (CreateToolBar() && CreateStatusBar())
363 /* Create the device view object */
364 m_DeviceView
= new CDeviceView(m_hMainWnd
, DevicesByType
);
367 if (m_DeviceView
->Initialize())
369 UpdateDevicesDisplay(DevicesByType
);
371 /* Display the window according to the user request */
372 ShowWindow(hwnd
, m_CmdShow
);
383 CMainWindow::OnSize()
385 RECT rcClient
, rcTool
, rcStatus
;
386 INT lvHeight
, iToolHeight
, iStatusHeight
;
388 /* Autosize the toolbar */
389 SendMessage(m_hToolBar
, TB_AUTOSIZE
, 0, 0);
391 /* Get the toolbar rect and save the height */
392 GetWindowRect(m_hToolBar
, &rcTool
);
393 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
395 /* Resize the status bar */
396 SendMessage(m_hStatusBar
, WM_SIZE
, 0, 0);
398 /* Get the statusbar rect and save the height */
399 GetWindowRect(m_hStatusBar
, &rcStatus
);
400 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
402 /* Get the full client rect */
403 GetClientRect(m_hMainWnd
, &rcClient
);
405 /* Calculate the remaining height for the treeview */
406 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
408 /* Resize the device view */
409 m_DeviceView
->Size(0,
418 CMainWindow::OnNotify(LPARAM lParam
)
420 LPNMHDR NmHdr
= (LPNMHDR
)lParam
;
424 case TVN_DELETEITEMW
:
426 LPNMTREEVIEW NmTreeView
= (LPNMTREEVIEW
)lParam
;
428 NmTreeView
->action
= NmTreeView
->action
;
435 m_DeviceView
->DisplayPropertySheet();
441 m_DeviceView
->DisplayPropertySheet();
450 CMainWindow::OnContext(LPARAM lParam
)
452 INT xPos
= GET_X_LPARAM(lParam
);
453 INT yPos
= GET_Y_LPARAM(lParam
);
455 m_DeviceView
->ShowContextMenu(xPos
, yPos
);
460 CMainWindow::OnCommand(WPARAM wParam
,
466 /* Get the message */
467 Msg
= LOWORD(wParam
);
473 /* Display the device property sheet */
474 m_DeviceView
->DisplayPropertySheet();
480 /* Refresh the device list */
481 m_DeviceView
->Refresh();
487 UpdateDevicesDisplay(DevicesByType
);
493 UpdateDevicesDisplay(DevicesByConnection
);
499 UINT CurCheckState
, NewCheckState
;
501 /* Get the current state */
502 CurCheckState
= GetMenuState(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
);
504 /* Inform the device view of the change */
505 if (CurCheckState
== MF_CHECKED
)
507 NewCheckState
= MF_UNCHECKED
;
508 m_DeviceView
->ShowHiddenDevices(FALSE
);
510 else if (CurCheckState
== MF_UNCHECKED
)
512 NewCheckState
= MF_CHECKED
;
513 m_DeviceView
->ShowHiddenDevices(TRUE
);
521 /* Set the new check state */
522 CheckMenuItem(m_hMenu
, IDC_SHOWHIDDEN
, MF_BYCOMMAND
| NewCheckState
);
524 /* Refresh the device view */
525 m_DeviceView
->Refresh();
531 /* Apportion blame */
532 MessageBoxW(m_hMainWnd
,
533 L
"ReactOS Device Manager\r\nCopyright Ged Murphy 2014",
535 MB_OK
| MB_APPLMODAL
);
537 /* Set focus back to the treeview */
538 m_DeviceView
->SetFocus();
544 /* Post a close message to the window */
545 PostMessageW(m_hMainWnd
,
560 CMainWindow::OnDestroy()
562 /* Uninitialize the device view */
563 m_DeviceView
->Uninitialize();
565 /* Kill the object */
569 /* Clear the user data pointer */
570 SetWindowLongPtr(m_hMainWnd
, GWLP_USERDATA
, 0);
572 /* Break the message loop */
579 CMainWindow::MainWndProc(HWND hwnd
,
587 /* Get the object pointer from window context */
588 pThis
= (CMainWindow
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
590 /* Check for an invalid pointer */
593 /* Check that this isn't a create message */
594 if (msg
!= WM_CREATE
)
596 /* Don't handle null info pointer */
597 goto HandleDefaultMessage
;
605 /* Get the object pointer from the create param */
606 pThis
= (CMainWindow
*)((LPCREATESTRUCT
)lParam
)->lpCreateParams
;
608 /* Store the info pointer in the window's global user data */
609 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)pThis
);
611 /* Call the create handler */
612 RetCode
= pThis
->OnCreate(hwnd
);
618 RetCode
= pThis
->OnSize();
624 /* Handle the notify message */
625 RetCode
= pThis
->OnNotify(lParam
);
631 /* Handle creating the context menu */
632 RetCode
= pThis
->OnContext(lParam
);
638 if (pThis
->m_hStatusBar
!= NULL
)
640 if (!pThis
->MainWndMenuHint(LOWORD(wParam
),
642 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
645 pThis
->MainWndMenuHint(LOWORD(wParam
),
647 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
657 /* Handle the command message */
658 RetCode
= pThis
->OnCommand(wParam
, lParam
);
660 /* Hand it off to the default message handler */
661 goto HandleDefaultMessage
;
666 /* Destroy the main window */
673 /* Call the destroy handler */
674 RetCode
= pThis
->OnDestroy();
680 HandleDefaultMessage
:
681 RetCode
= DefWindowProc(hwnd
, msg
, wParam
, lParam
);