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>
12 static const TCHAR szMainWndClass
[] = TEXT("DevMgmtWndClass");
17 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
18 {TBICON_PROP
, IDC_PROP
, TBSTATE_INDETERMINATE
, BTNS_BUTTON
, {0}, 0, 0}, /* properties */
19 {TBICON_REFRESH
, IDC_REFRESH
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0}, /* refresh */
21 /* Note: First item for a seperator is its width in pixels */
22 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0}, /* separator */
24 {TBICON_HELP
, IDC_PROGHELP
,TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }, /* help */
25 {TBICON_EXIT
, IDC_EXIT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }, /* exit */
31 static const MENU_HINT MainMenuHintTable
[] = {
33 {IDC_EXIT
, IDS_HINT_EXIT
},
36 {IDC_REFRESH
, IDS_HINT_REFRESH
},
37 {IDC_PROP
, IDS_HINT_PROP
},
40 {IDC_PROGHELP
, IDS_HINT_HELP
},
41 {IDC_ABOUT
, IDS_HINT_ABOUT
}
44 /* system menu hints */
45 static const MENU_HINT SystemMenuHintTable
[] = {
46 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
47 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
48 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
49 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
50 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
51 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
},
56 MainWndMenuHint(PMAIN_WND_INFO Info
,
58 const MENU_HINT
*HintArray
,
63 const MENU_HINT
*LastHint
;
64 UINT HintId
= DefHintId
;
66 LastHint
= HintArray
+ HintsCount
;
67 while (HintArray
!= LastHint
)
69 if (HintArray
->CmdId
== CmdId
)
71 HintId
= HintArray
->HintId
;
78 StatusBarLoadString(Info
->hStatus
,
88 UpdateMainStatusBar(PMAIN_WND_INFO Info
)
90 if (Info
->hStatus
!= NULL
)
92 SendMessage(Info
->hStatus
,
94 (WPARAM
)Info
->InMenuLoop
,
101 CreateToolbar(PMAIN_WND_INFO Info
)
103 INT NumButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
105 Info
->hTool
= CreateWindowEx(0,
108 WS_CHILD
| WS_VISIBLE
| TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
,
114 if(Info
->hTool
!= NULL
)
116 HIMAGELIST hImageList
;
118 SendMessage(Info
->hTool
,
121 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
123 SendMessage(Info
->hTool
,
128 hImageList
= InitImageList(IDB_PROP
,
132 if (hImageList
== NULL
)
135 ImageList_Destroy((HIMAGELIST
)SendMessage(Info
->hTool
,
138 (LPARAM
)hImageList
));
140 SendMessage(Info
->hTool
,
153 CreateTreeView(PMAIN_WND_INFO Info
)
155 Info
->hTreeView
= CreateWindowEx(0,
158 WS_CHILD
| WS_VISIBLE
| WS_BORDER
| TVS_HASLINES
|
159 TVS_HASBUTTONS
| TVS_SHOWSELALWAYS
| TVS_LINESATROOT
,
165 if (Info
->hTreeView
== NULL
)
167 DisplayString(_T("Could not create TreeView."));
175 CreateStatusBar(PMAIN_WND_INFO Info
)
177 INT StatWidths
[] = {110, -1}; /* widths of status bar */
179 Info
->hStatus
= CreateWindowEx(0,
182 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
185 (HMENU
)IDC_STATUSBAR
,
188 if(Info
->hStatus
== NULL
)
192 SendMessage(Info
->hStatus
,
194 sizeof(StatWidths
) / sizeof(INT
),
202 DeviceEnumThread(LPVOID lpParameter
)
207 hTreeView
= (HWND
*)lpParameter
;
210 FreeDeviceStrings(*hTreeView
);
212 hRoot
= InitTreeView(*hTreeView
);
215 ListDevicesByType(*hTreeView
, hRoot
);
224 InitMainWnd(PMAIN_WND_INFO Info
)
226 HANDLE DevEnumThread
;
229 if (!CreateToolbar(Info
))
230 DisplayString(_T("error creating toolbar"));
232 if (!CreateTreeView(Info
))
234 DisplayString(_T("error creating list view"));
238 if (!CreateStatusBar(Info
))
239 DisplayString(_T("error creating status bar"));
241 /* make 'properties' bold */
242 hMenu
= GetMenu(Info
->hMainWnd
);
243 hMenu
= GetSubMenu(hMenu
, 1);
244 SetMenuDefaultItem(hMenu
, IDC_PROP
, FALSE
);
246 /* Create Popup Menu */
247 Info
->hShortcutMenu
= LoadMenu(hInstance
,
248 MAKEINTRESOURCE(IDR_POPUP
));
249 Info
->hShortcutMenu
= GetSubMenu(Info
->hShortcutMenu
,
251 SetMenuDefaultItem(Info
->hShortcutMenu
, IDC_PROP
, FALSE
);
253 /* create seperate thread to emum devices */
254 DevEnumThread
= CreateThread(NULL
,
262 DisplayString(_T("Failed to enumerate devices"));
266 CloseHandle(DevEnumThread
);
272 OnContext(PMAIN_WND_INFO Info
,
279 INT xPos
= GET_X_LPARAM(lParam
);
280 INT yPos
= GET_Y_LPARAM(lParam
);
282 hSelected
= TreeView_GetSelection(Info
->hTreeView
);
284 if (TreeView_GetItemRect(Info
->hTreeView
,
289 if (GetCursorPos(&pt
) &&
290 ScreenToClient(Info
->hTreeView
, &pt
) &&
293 TrackPopupMenuEx(Info
->hShortcutMenu
,
305 OnNotify(PMAIN_WND_INFO Info
,
308 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
310 switch (pnmhdr
->code
)
314 LPNM_TREEVIEW pnmtv
= (LPNM_TREEVIEW
)lParam
;
316 if (!TreeView_GetChild(Info
->hTreeView
,
317 pnmtv
->itemNew
.hItem
))
319 SendMessage(Info
->hTool
,
322 (LPARAM
)MAKELONG(TBSTATE_ENABLED
, 0));
324 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_ENABLED
);
325 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_ENABLED
);
329 SendMessage(Info
->hTool
,
332 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
334 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
335 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
342 HTREEITEM hSelected
= TreeView_GetSelection(Info
->hTreeView
);
344 if (!TreeView_GetChild(Info
->hTreeView
,
347 OpenPropSheet(Info
->hTreeView
,
355 TV_HITTESTINFO HitTest
;
357 if (GetCursorPos(&HitTest
.pt
) &&
358 ScreenToClient(Info
->hTreeView
, &HitTest
.pt
))
360 if (TreeView_HitTest(Info
->hTreeView
, &HitTest
))
361 (void)TreeView_SelectItem(Info
->hTreeView
, HitTest
.hItem
);
366 case TTN_GETDISPINFO
:
371 lpttt
= (LPTOOLTIPTEXT
)lParam
;
373 idButton
= (UINT
)lpttt
->hdr
.idFrom
;
377 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PROP
);
381 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_REFRESH
);
385 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_HELP
);
389 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_EXIT
);
399 MainWndCommand(PMAIN_WND_INFO Info
,
403 UNREFERENCED_PARAMETER(hControl
);
409 HTREEITEM hSelected
= TreeView_GetSelection(Info
->hTreeView
);
410 OpenPropSheet(Info
->hTreeView
,
417 HANDLE DevEnumThread
;
419 SendMessage(Info
->hTool
,
422 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
424 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
425 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
427 /* create seperate thread to emum devices */
428 DevEnumThread
= CreateThread(NULL
,
436 DisplayString(_T("Failed to enumerate devices"));
440 CloseHandle(DevEnumThread
);
446 DisplayString(_T("Help is not yet implemented\n"));
447 SetFocus(Info
->hTreeView
);
453 PostMessage(Info
->hMainWnd
,
463 MAKEINTRESOURCE(IDD_ABOUTBOX
),
465 (DLGPROC
)AboutDialogProc
);
467 SetFocus(Info
->hTreeView
);
476 MainWndResize(PMAIN_WND_INFO Info
,
480 RECT rcClient
, rcTool
, rcStatus
;
481 int lvHeight
, iToolHeight
, iStatusHeight
;
483 /* Size toolbar and get height */
484 SendMessage(Info
->hTool
, TB_AUTOSIZE
, 0, 0);
485 GetWindowRect(Info
->hTool
, &rcTool
);
486 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
488 /* Size status bar and get height */
489 SendMessage(Info
->hStatus
, WM_SIZE
, 0, 0);
490 GetWindowRect(Info
->hStatus
, &rcStatus
);
491 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
493 /* Calculate remaining height and size list view */
494 GetClientRect(Info
->hMainWnd
, &rcClient
);
495 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
496 SetWindowPos(Info
->hTreeView
,
506 static LRESULT CALLBACK
507 MainWndProc(HWND hwnd
,
515 /* Get the window context */
516 Info
= (PMAIN_WND_INFO
)GetWindowLongPtr(hwnd
,
518 if (Info
== NULL
&& msg
!= WM_CREATE
)
520 goto HandleDefaultMessage
;
527 Info
= (PMAIN_WND_INFO
)(((LPCREATESTRUCT
)lParam
)->lpCreateParams
);
529 /* Initialize the main window context */
530 Info
->hMainWnd
= hwnd
;
532 SetWindowLongPtr(hwnd
,
536 if (!InitMainWnd(Info
))
537 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
539 /* Show the window */
556 OnNotify(Info
, lParam
);
562 OnContext(Info
, lParam
);
571 goto HandleDefaultMessage
;
576 if (Info
->hStatus
!= NULL
)
578 if (!MainWndMenuHint(Info
,
581 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
584 MainWndMenuHint(Info
,
587 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
594 case WM_ENTERMENULOOP
:
596 Info
->InMenuLoop
= TRUE
;
597 UpdateMainStatusBar(Info
);
601 case WM_EXITMENULOOP
:
603 Info
->InMenuLoop
= FALSE
;
604 UpdateMainStatusBar(Info
);
610 FreeDeviceStrings(Info
->hTreeView
);
611 DestroyMenu(Info
->hShortcutMenu
);
618 HeapFree(ProcessHeap
,
621 SetWindowLongPtr(hwnd
,
625 /* Break the message queue loop */
632 HandleDefaultMessage
:
634 Ret
= DefWindowProc(hwnd
,
647 CreateMainWindow(LPCTSTR lpCaption
,
651 HWND hMainWnd
= NULL
;
653 Info
= (PMAIN_WND_INFO
)HeapAlloc(ProcessHeap
,
655 sizeof(MAIN_WND_INFO
));
659 Info
->nCmdShow
= nCmdShow
;
661 hMainWnd
= CreateWindowEx(WS_EX_WINDOWEDGE
,
664 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
673 if (hMainWnd
== NULL
)
676 HeapFree(ProcessHeap
,
686 InitMainWindowImpl(VOID
)
690 wc
.cbSize
= sizeof(WNDCLASSEX
);
691 wc
.lpfnWndProc
= MainWndProc
;
692 wc
.hInstance
= hInstance
;
693 wc
.hIcon
= LoadIcon(hInstance
,
694 MAKEINTRESOURCE(IDI_MAIN_ICON
));
695 wc
.hCursor
= LoadCursor(NULL
,
697 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
698 wc
.lpszMenuName
= MAKEINTRESOURCE(IDR_MAINMENU
);
699 wc
.lpszClassName
= szMainWndClass
;
700 wc
.hIconSm
= (HICON
)LoadImage(hInstance
,
701 MAKEINTRESOURCE(IDI_MAIN_ICON
),
707 return RegisterClassEx(&wc
) != (ATOM
)0;
712 UninitMainWindowImpl(VOID
)
714 UnregisterClass(szMainWndClass
,