2 * PROJECT: ReactOS Device Managment
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/devmgmt/mainwnd.c
5 * PURPOSE: Main window message handler
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
14 static BOOL
pCreateToolbar(PMAIN_WND_INFO Info
);
16 static const TCHAR szMainWndClass
[] = TEXT("DevMgmtWndClass");
21 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
22 {TBICON_PROP
, IDC_PROP
, TBSTATE_INDETERMINATE
, BTNS_BUTTON
, {0}, 0, 0}, /* properties */
23 {TBICON_REFRESH
, IDC_REFRESH
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0}, /* refresh */
25 /* Note: First item for a seperator is its width in pixels */
26 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0}, /* separator */
28 {TBICON_HELP
, IDC_PROGHELP
,TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }, /* help */
29 {TBICON_EXIT
, IDC_EXIT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }, /* exit */
35 static const MENU_HINT MainMenuHintTable
[] = {
37 {IDC_EXIT
, IDS_HINT_EXIT
},
40 {IDC_REFRESH
, IDS_HINT_REFRESH
},
41 {IDC_PROP
, IDS_HINT_PROP
},
44 {IDC_PROGHELP
, IDS_HINT_HELP
},
45 {IDC_ABOUT
, IDS_HINT_ABOUT
}
48 /* system menu hints */
49 static const MENU_HINT SystemMenuHintTable
[] = {
50 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
51 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
52 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
53 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
54 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
55 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
},
60 MainWndMenuHint(PMAIN_WND_INFO Info
,
62 const MENU_HINT
*HintArray
,
67 const MENU_HINT
*LastHint
;
68 UINT HintId
= DefHintId
;
70 LastHint
= HintArray
+ HintsCount
;
71 while (HintArray
!= LastHint
)
73 if (HintArray
->CmdId
== CmdId
)
75 HintId
= HintArray
->HintId
;
82 StatusBarLoadString(Info
->hStatus
,
92 UpdateMainStatusBar(PMAIN_WND_INFO Info
)
94 if (Info
->hStatus
!= NULL
)
96 SendMessage(Info
->hStatus
,
98 (WPARAM
)Info
->InMenuLoop
,
105 pCreateToolbar(PMAIN_WND_INFO Info
)
107 INT NumButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
109 Info
->hTool
= CreateWindowEx(0,
112 WS_CHILD
| WS_VISIBLE
| TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
,
118 if(Info
->hTool
!= NULL
)
120 HIMAGELIST hImageList
;
122 SendMessage(Info
->hTool
,
125 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
127 SendMessage(Info
->hTool
,
132 hImageList
= InitImageList(IDB_PROP
,
136 if (hImageList
== NULL
)
139 ImageList_Destroy((HIMAGELIST
)SendMessage(Info
->hTool
,
142 (LPARAM
)hImageList
));
144 SendMessage(Info
->hTool
,
157 CreateTreeView(PMAIN_WND_INFO Info
)
159 Info
->hTreeView
= CreateWindowEx(WS_EX_CLIENTEDGE
,
162 WS_CHILD
| WS_VISIBLE
| WS_BORDER
| TVS_HASLINES
|
163 TVS_HASBUTTONS
| TVS_SHOWSELALWAYS
| TVS_LINESATROOT
,
169 if (Info
->hTreeView
== NULL
)
171 DisplayString(_T("Could not create TreeView."));
179 CreateStatusBar(PMAIN_WND_INFO Info
)
181 INT StatWidths
[] = {110, -1}; /* widths of status bar */
183 Info
->hStatus
= CreateWindowEx(0,
186 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
189 (HMENU
)IDC_STATUSBAR
,
192 if(Info
->hStatus
== NULL
)
196 SendMessage(Info
->hStatus
,
198 sizeof(StatWidths
) / sizeof(INT
),
206 DeviceEnumThread(LPVOID lpParameter
)
211 Info
= (PMAIN_WND_INFO
)lpParameter
;
214 FreeDeviceStrings(Info
->hTreeView
);
216 hRoot
= InitTreeView(Info
->hTreeView
);
219 switch (Info
->Display
)
222 ListDevicesByType(Info
->hTreeView
, hRoot
, Info
->bShowHidden
);
225 case DevicesByConnection
:
226 ListDevicesByConnection(Info
->hTreeView
, hRoot
, Info
->bShowHidden
);
240 UpdateViewMenu(PMAIN_WND_INFO Info
)
242 UINT id
= IDC_DEVBYTYPE
;
245 hMenu
= GetMenu(Info
->hMainWnd
);
247 switch (Info
->Display
)
252 case DevicesByConnection
:
255 case RessourcesByType
:
258 case RessourcesByConnection
:
263 CheckMenuRadioItem(hMenu
,
271 MF_BYCOMMAND
| (Info
->bShowHidden
) ? MF_CHECKED
: MF_UNCHECKED
);
276 InitMainWnd(PMAIN_WND_INFO Info
)
278 HANDLE DevEnumThread
;
281 if (!pCreateToolbar(Info
))
282 DisplayString(_T("error creating toolbar"));
284 if (!CreateTreeView(Info
))
286 DisplayString(_T("error creating list view"));
290 if (!CreateStatusBar(Info
))
291 DisplayString(_T("error creating status bar"));
293 UpdateViewMenu(Info
);
295 /* make 'properties' bold */
296 hMenu
= GetMenu(Info
->hMainWnd
);
297 hMenu
= GetSubMenu(hMenu
, 1);
298 SetMenuDefaultItem(hMenu
, IDC_PROP
, FALSE
);
300 /* Create Popup Menu */
301 Info
->hShortcutMenu
= LoadMenu(hInstance
,
302 MAKEINTRESOURCE(IDR_POPUP
));
303 Info
->hShortcutMenu
= GetSubMenu(Info
->hShortcutMenu
,
305 SetMenuDefaultItem(Info
->hShortcutMenu
, IDC_PROP
, FALSE
);
307 /* create seperate thread to emum devices */
308 DevEnumThread
= CreateThread(NULL
,
316 DisplayString(_T("Failed to enumerate devices"));
320 CloseHandle(DevEnumThread
);
326 OnContext(PMAIN_WND_INFO Info
,
333 INT xPos
= GET_X_LPARAM(lParam
);
334 INT yPos
= GET_Y_LPARAM(lParam
);
336 hSelected
= TreeView_GetSelection(Info
->hTreeView
);
338 if (TreeView_GetItemRect(Info
->hTreeView
,
343 if (GetCursorPos(&pt
) &&
344 ScreenToClient(Info
->hTreeView
, &pt
) &&
347 TrackPopupMenuEx(Info
->hShortcutMenu
,
359 OnNotify(PMAIN_WND_INFO Info
,
362 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
365 switch (pnmhdr
->code
)
369 LPNM_TREEVIEW pnmtv
= (LPNM_TREEVIEW
)lParam
;
371 if (Info
->Display
== DevicesByType
)
373 if (!TreeView_GetChild(Info
->hTreeView
,
374 pnmtv
->itemNew
.hItem
))
376 SendMessage(Info
->hTool
,
379 (LPARAM
)MAKELONG(TBSTATE_ENABLED
, 0));
381 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_ENABLED
);
382 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_ENABLED
);
386 SendMessage(Info
->hTool
,
389 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
391 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
392 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
395 else if (Info
->Display
== DevicesByConnection
)
397 if (pnmtv
->itemNew
.hItem
== TreeView_GetRoot(Info
->hTreeView
))
399 SendMessage(Info
->hTool
,
402 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
404 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
405 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
409 SendMessage(Info
->hTool
,
412 (LPARAM
)MAKELONG(TBSTATE_ENABLED
, 0));
414 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_ENABLED
);
415 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_ENABLED
);
423 HTREEITEM hSelected
= TreeView_GetSelection(Info
->hTreeView
);
424 TV_HITTESTINFO HitTest
;
426 if (Info
->Display
== DevicesByType
)
428 if (!TreeView_GetChild(Info
->hTreeView
,
431 if (GetCursorPos(&HitTest
.pt
) &&
432 ScreenToClient(Info
->hTreeView
, &HitTest
.pt
))
434 if (TreeView_HitTest(Info
->hTreeView
, &HitTest
))
436 if (HitTest
.hItem
== hSelected
)
438 OpenPropSheet(Info
->hTreeView
,
446 else if (Info
->Display
== DevicesByConnection
)
448 if (hSelected
!= TreeView_GetRoot(Info
->hTreeView
))
450 if (GetCursorPos(&HitTest
.pt
) &&
451 ScreenToClient(Info
->hTreeView
, &HitTest
.pt
))
453 if (TreeView_HitTest(Info
->hTreeView
, &HitTest
))
455 if (HitTest
.hItem
== hSelected
)
457 OpenPropSheet(Info
->hTreeView
,
470 TV_HITTESTINFO HitTest
;
472 if (GetCursorPos(&HitTest
.pt
) &&
473 ScreenToClient(Info
->hTreeView
, &HitTest
.pt
))
475 if (TreeView_HitTest(Info
->hTreeView
, &HitTest
))
476 (void)TreeView_SelectItem(Info
->hTreeView
, HitTest
.hItem
);
481 case TTN_GETDISPINFO
:
486 lpttt
= (LPTOOLTIPTEXT
)lParam
;
488 idButton
= (UINT
)lpttt
->hdr
.idFrom
;
492 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PROP
);
496 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_REFRESH
);
500 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_HELP
);
504 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_EXIT
);
516 OnRefresh(PMAIN_WND_INFO Info
)
518 HANDLE DevEnumThread
;
520 SendMessage(Info
->hTool
,
523 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
525 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
526 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
528 /* create seperate thread to emum devices */
529 DevEnumThread
= CreateThread(NULL
,
537 DisplayString(_T("Failed to enumerate devices"));
541 CloseHandle(DevEnumThread
);
546 MainWndCommand(PMAIN_WND_INFO Info
,
550 UNREFERENCED_PARAMETER(hControl
);
556 HTREEITEM hSelected
= TreeView_GetSelection(Info
->hTreeView
);
557 OpenPropSheet(Info
->hTreeView
,
570 DisplayString(_T("Help is not yet implemented\n"));
571 SetFocus(Info
->hTreeView
);
577 PostMessage(Info
->hMainWnd
,
587 MAKEINTRESOURCE(IDD_ABOUTBOX
),
591 SetFocus(Info
->hTreeView
);
597 Info
->Display
= DevicesByType
;
598 UpdateViewMenu(Info
);
605 Info
->Display
= DevicesByConnection
;
606 UpdateViewMenu(Info
);
613 Info
->bShowHidden
= !Info
->bShowHidden
;
614 UpdateViewMenu(Info
);
623 MainWndResize(PMAIN_WND_INFO Info
,
627 RECT rcClient
, rcTool
, rcStatus
;
628 int lvHeight
, iToolHeight
, iStatusHeight
;
630 /* Size toolbar and get height */
631 SendMessage(Info
->hTool
, TB_AUTOSIZE
, 0, 0);
632 GetWindowRect(Info
->hTool
, &rcTool
);
633 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
635 /* Size status bar and get height */
636 SendMessage(Info
->hStatus
, WM_SIZE
, 0, 0);
637 GetWindowRect(Info
->hStatus
, &rcStatus
);
638 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
640 /* Calculate remaining height and size list view */
641 GetClientRect(Info
->hMainWnd
, &rcClient
);
642 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
643 SetWindowPos(Info
->hTreeView
,
653 static LRESULT CALLBACK
654 MainWndProc(HWND hwnd
,
662 /* Get the window context */
663 Info
= (PMAIN_WND_INFO
)GetWindowLongPtr(hwnd
,
665 if (Info
== NULL
&& msg
!= WM_CREATE
)
667 goto HandleDefaultMessage
;
674 Info
= (PMAIN_WND_INFO
)(((LPCREATESTRUCT
)lParam
)->lpCreateParams
);
676 /* Initialize the main window context */
677 Info
->hMainWnd
= hwnd
;
679 SetWindowLongPtr(hwnd
,
683 if (!InitMainWnd(Info
))
684 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
686 /* Show the window */
694 if (Info
->hTreeView
!= NULL
)
695 SetFocus(Info
->hTreeView
);
709 Ret
= OnNotify(Info
, lParam
);
715 OnContext(Info
, lParam
);
724 goto HandleDefaultMessage
;
729 if (Info
->hStatus
!= NULL
)
731 if (!MainWndMenuHint(Info
,
734 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
737 MainWndMenuHint(Info
,
740 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
747 case WM_ENTERMENULOOP
:
749 Info
->InMenuLoop
= TRUE
;
750 UpdateMainStatusBar(Info
);
754 case WM_EXITMENULOOP
:
756 Info
->InMenuLoop
= FALSE
;
757 UpdateMainStatusBar(Info
);
763 FreeDeviceStrings(Info
->hTreeView
);
764 DestroyMenu(Info
->hShortcutMenu
);
771 HeapFree(ProcessHeap
,
774 SetWindowLongPtr(hwnd
,
778 /* Break the message queue loop */
785 HandleDefaultMessage
:
787 Ret
= DefWindowProc(hwnd
,
800 CreateMainWindow(LPCTSTR lpCaption
,
804 HWND hMainWnd
= NULL
;
806 Info
= (PMAIN_WND_INFO
)HeapAlloc(ProcessHeap
,
808 sizeof(MAIN_WND_INFO
));
812 Info
->nCmdShow
= nCmdShow
;
813 Info
->Display
= DevicesByType
;
814 Info
->bShowHidden
= TRUE
;
816 hMainWnd
= CreateWindowEx(WS_EX_WINDOWEDGE
,
819 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
828 if (hMainWnd
== NULL
)
831 HeapFree(ProcessHeap
,
841 InitMainWindowImpl(VOID
)
845 wc
.cbSize
= sizeof(WNDCLASSEX
);
846 wc
.lpfnWndProc
= MainWndProc
;
847 wc
.hInstance
= hInstance
;
848 wc
.hIcon
= LoadIcon(hInstance
,
849 MAKEINTRESOURCE(IDI_MAIN_ICON
));
850 wc
.hCursor
= LoadCursor(NULL
,
852 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
853 wc
.lpszMenuName
= MAKEINTRESOURCE(IDR_MAINMENU
);
854 wc
.lpszClassName
= szMainWndClass
;
855 wc
.hIconSm
= (HICON
)LoadImage(hInstance
,
856 MAKEINTRESOURCE(IDI_MAIN_ICON
),
862 return RegisterClassEx(&wc
) != (ATOM
)0;
867 UninitMainWindowImpl(VOID
)
869 UnregisterClass(szMainWndClass
,