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 BOOL
pCreateToolbar(PMAIN_WND_INFO Info
);
14 static const TCHAR szMainWndClass
[] = TEXT("DevMgmtWndClass");
19 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
20 {TBICON_PROP
, IDC_PROP
, TBSTATE_INDETERMINATE
, BTNS_BUTTON
, {0}, 0, 0}, /* properties */
21 {TBICON_REFRESH
, IDC_REFRESH
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0}, /* refresh */
23 /* Note: First item for a seperator is its width in pixels */
24 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0}, /* separator */
26 {TBICON_HELP
, IDC_PROGHELP
,TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }, /* help */
27 {TBICON_EXIT
, IDC_EXIT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0 }, /* exit */
33 static const MENU_HINT MainMenuHintTable
[] = {
35 {IDC_EXIT
, IDS_HINT_EXIT
},
38 {IDC_REFRESH
, IDS_HINT_REFRESH
},
39 {IDC_PROP
, IDS_HINT_PROP
},
42 {IDC_PROGHELP
, IDS_HINT_HELP
},
43 {IDC_ABOUT
, IDS_HINT_ABOUT
}
46 /* system menu hints */
47 static const MENU_HINT SystemMenuHintTable
[] = {
48 {SC_RESTORE
, IDS_HINT_SYS_RESTORE
},
49 {SC_MOVE
, IDS_HINT_SYS_MOVE
},
50 {SC_SIZE
, IDS_HINT_SYS_SIZE
},
51 {SC_MINIMIZE
, IDS_HINT_SYS_MINIMIZE
},
52 {SC_MAXIMIZE
, IDS_HINT_SYS_MAXIMIZE
},
53 {SC_CLOSE
, IDS_HINT_SYS_CLOSE
},
58 MainWndMenuHint(PMAIN_WND_INFO Info
,
60 const MENU_HINT
*HintArray
,
65 const MENU_HINT
*LastHint
;
66 UINT HintId
= DefHintId
;
68 LastHint
= HintArray
+ HintsCount
;
69 while (HintArray
!= LastHint
)
71 if (HintArray
->CmdId
== CmdId
)
73 HintId
= HintArray
->HintId
;
80 StatusBarLoadString(Info
->hStatus
,
90 UpdateMainStatusBar(PMAIN_WND_INFO Info
)
92 if (Info
->hStatus
!= NULL
)
94 SendMessage(Info
->hStatus
,
96 (WPARAM
)Info
->InMenuLoop
,
103 pCreateToolbar(PMAIN_WND_INFO Info
)
105 INT NumButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
107 Info
->hTool
= CreateWindowEx(0,
110 WS_CHILD
| WS_VISIBLE
| TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
,
116 if(Info
->hTool
!= NULL
)
118 HIMAGELIST hImageList
;
120 SendMessage(Info
->hTool
,
123 TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
125 SendMessage(Info
->hTool
,
130 hImageList
= InitImageList(IDB_PROP
,
134 if (hImageList
== NULL
)
137 ImageList_Destroy((HIMAGELIST
)SendMessage(Info
->hTool
,
140 (LPARAM
)hImageList
));
142 SendMessage(Info
->hTool
,
155 CreateTreeView(PMAIN_WND_INFO Info
)
157 Info
->hTreeView
= CreateWindowEx(WS_EX_CLIENTEDGE
,
160 WS_CHILD
| WS_VISIBLE
| WS_BORDER
| TVS_HASLINES
|
161 TVS_HASBUTTONS
| TVS_SHOWSELALWAYS
| TVS_LINESATROOT
,
167 if (Info
->hTreeView
== NULL
)
169 DisplayString(_T("Could not create TreeView."));
177 CreateStatusBar(PMAIN_WND_INFO Info
)
179 INT StatWidths
[] = {110, -1}; /* widths of status bar */
181 Info
->hStatus
= CreateWindowEx(0,
184 WS_CHILD
| WS_VISIBLE
| SBARS_SIZEGRIP
,
187 (HMENU
)IDC_STATUSBAR
,
190 if(Info
->hStatus
== NULL
)
194 SendMessage(Info
->hStatus
,
196 sizeof(StatWidths
) / sizeof(INT
),
204 DeviceEnumThread(LPVOID lpParameter
)
209 hTreeView
= (HWND
*)lpParameter
;
212 FreeDeviceStrings(*hTreeView
);
214 hRoot
= InitTreeView(*hTreeView
);
217 ListDevicesByType(*hTreeView
, hRoot
);
226 InitMainWnd(PMAIN_WND_INFO Info
)
228 HANDLE DevEnumThread
;
231 if (!pCreateToolbar(Info
))
232 DisplayString(_T("error creating toolbar"));
234 if (!CreateTreeView(Info
))
236 DisplayString(_T("error creating list view"));
240 if (!CreateStatusBar(Info
))
241 DisplayString(_T("error creating status bar"));
243 /* make 'properties' bold */
244 hMenu
= GetMenu(Info
->hMainWnd
);
245 hMenu
= GetSubMenu(hMenu
, 1);
246 SetMenuDefaultItem(hMenu
, IDC_PROP
, FALSE
);
248 /* Create Popup Menu */
249 Info
->hShortcutMenu
= LoadMenu(hInstance
,
250 MAKEINTRESOURCE(IDR_POPUP
));
251 Info
->hShortcutMenu
= GetSubMenu(Info
->hShortcutMenu
,
253 SetMenuDefaultItem(Info
->hShortcutMenu
, IDC_PROP
, FALSE
);
255 /* create seperate thread to emum devices */
256 DevEnumThread
= CreateThread(NULL
,
264 DisplayString(_T("Failed to enumerate devices"));
268 CloseHandle(DevEnumThread
);
274 OnContext(PMAIN_WND_INFO Info
,
281 INT xPos
= GET_X_LPARAM(lParam
);
282 INT yPos
= GET_Y_LPARAM(lParam
);
284 hSelected
= TreeView_GetSelection(Info
->hTreeView
);
286 if (TreeView_GetItemRect(Info
->hTreeView
,
291 if (GetCursorPos(&pt
) &&
292 ScreenToClient(Info
->hTreeView
, &pt
) &&
295 TrackPopupMenuEx(Info
->hShortcutMenu
,
307 OnNotify(PMAIN_WND_INFO Info
,
310 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
312 switch (pnmhdr
->code
)
316 LPNM_TREEVIEW pnmtv
= (LPNM_TREEVIEW
)lParam
;
318 if (!TreeView_GetChild(Info
->hTreeView
,
319 pnmtv
->itemNew
.hItem
))
321 SendMessage(Info
->hTool
,
324 (LPARAM
)MAKELONG(TBSTATE_ENABLED
, 0));
326 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_ENABLED
);
327 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_ENABLED
);
331 SendMessage(Info
->hTool
,
334 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
336 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
337 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
344 HTREEITEM hSelected
= TreeView_GetSelection(Info
->hTreeView
);
345 TV_HITTESTINFO HitTest
;
347 if (!TreeView_GetChild(Info
->hTreeView
,
350 if (GetCursorPos(&HitTest
.pt
) &&
351 ScreenToClient(Info
->hTreeView
, &HitTest
.pt
))
353 if (TreeView_HitTest(Info
->hTreeView
, &HitTest
))
355 if (HitTest
.hItem
== hSelected
)
356 OpenPropSheet(Info
->hTreeView
,
366 TV_HITTESTINFO HitTest
;
368 if (GetCursorPos(&HitTest
.pt
) &&
369 ScreenToClient(Info
->hTreeView
, &HitTest
.pt
))
371 if (TreeView_HitTest(Info
->hTreeView
, &HitTest
))
372 (void)TreeView_SelectItem(Info
->hTreeView
, HitTest
.hItem
);
377 case TTN_GETDISPINFO
:
382 lpttt
= (LPTOOLTIPTEXT
)lParam
;
384 idButton
= (UINT
)lpttt
->hdr
.idFrom
;
388 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PROP
);
392 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_REFRESH
);
396 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_HELP
);
400 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_EXIT
);
410 MainWndCommand(PMAIN_WND_INFO Info
,
414 UNREFERENCED_PARAMETER(hControl
);
420 HTREEITEM hSelected
= TreeView_GetSelection(Info
->hTreeView
);
421 OpenPropSheet(Info
->hTreeView
,
428 HANDLE DevEnumThread
;
430 SendMessage(Info
->hTool
,
433 (LPARAM
)MAKELONG(TBSTATE_INDETERMINATE
, 0));
435 EnableMenuItem(GetMenu(Info
->hMainWnd
), IDC_PROP
, MF_GRAYED
);
436 EnableMenuItem(Info
->hShortcutMenu
, IDC_PROP
, MF_GRAYED
);
438 /* create seperate thread to emum devices */
439 DevEnumThread
= CreateThread(NULL
,
447 DisplayString(_T("Failed to enumerate devices"));
451 CloseHandle(DevEnumThread
);
457 DisplayString(_T("Help is not yet implemented\n"));
458 SetFocus(Info
->hTreeView
);
464 PostMessage(Info
->hMainWnd
,
474 MAKEINTRESOURCE(IDD_ABOUTBOX
),
478 SetFocus(Info
->hTreeView
);
487 MainWndResize(PMAIN_WND_INFO Info
,
491 RECT rcClient
, rcTool
, rcStatus
;
492 int lvHeight
, iToolHeight
, iStatusHeight
;
494 /* Size toolbar and get height */
495 SendMessage(Info
->hTool
, TB_AUTOSIZE
, 0, 0);
496 GetWindowRect(Info
->hTool
, &rcTool
);
497 iToolHeight
= rcTool
.bottom
- rcTool
.top
;
499 /* Size status bar and get height */
500 SendMessage(Info
->hStatus
, WM_SIZE
, 0, 0);
501 GetWindowRect(Info
->hStatus
, &rcStatus
);
502 iStatusHeight
= rcStatus
.bottom
- rcStatus
.top
;
504 /* Calculate remaining height and size list view */
505 GetClientRect(Info
->hMainWnd
, &rcClient
);
506 lvHeight
= rcClient
.bottom
- iToolHeight
- iStatusHeight
;
507 SetWindowPos(Info
->hTreeView
,
517 static LRESULT CALLBACK
518 MainWndProc(HWND hwnd
,
526 /* Get the window context */
527 Info
= (PMAIN_WND_INFO
)GetWindowLongPtr(hwnd
,
529 if (Info
== NULL
&& msg
!= WM_CREATE
)
531 goto HandleDefaultMessage
;
538 Info
= (PMAIN_WND_INFO
)(((LPCREATESTRUCT
)lParam
)->lpCreateParams
);
540 /* Initialize the main window context */
541 Info
->hMainWnd
= hwnd
;
543 SetWindowLongPtr(hwnd
,
547 if (!InitMainWnd(Info
))
548 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
550 /* Show the window */
567 OnNotify(Info
, lParam
);
573 OnContext(Info
, lParam
);
582 goto HandleDefaultMessage
;
587 if (Info
->hStatus
!= NULL
)
589 if (!MainWndMenuHint(Info
,
592 sizeof(MainMenuHintTable
) / sizeof(MainMenuHintTable
[0]),
595 MainWndMenuHint(Info
,
598 sizeof(SystemMenuHintTable
) / sizeof(SystemMenuHintTable
[0]),
605 case WM_ENTERMENULOOP
:
607 Info
->InMenuLoop
= TRUE
;
608 UpdateMainStatusBar(Info
);
612 case WM_EXITMENULOOP
:
614 Info
->InMenuLoop
= FALSE
;
615 UpdateMainStatusBar(Info
);
621 FreeDeviceStrings(Info
->hTreeView
);
622 DestroyMenu(Info
->hShortcutMenu
);
629 HeapFree(ProcessHeap
,
632 SetWindowLongPtr(hwnd
,
636 /* Break the message queue loop */
643 HandleDefaultMessage
:
645 Ret
= DefWindowProc(hwnd
,
658 CreateMainWindow(LPCTSTR lpCaption
,
662 HWND hMainWnd
= NULL
;
664 Info
= (PMAIN_WND_INFO
)HeapAlloc(ProcessHeap
,
666 sizeof(MAIN_WND_INFO
));
670 Info
->nCmdShow
= nCmdShow
;
672 hMainWnd
= CreateWindowEx(WS_EX_WINDOWEDGE
,
675 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,
684 if (hMainWnd
== NULL
)
687 HeapFree(ProcessHeap
,
697 InitMainWindowImpl(VOID
)
701 wc
.cbSize
= sizeof(WNDCLASSEX
);
702 wc
.lpfnWndProc
= MainWndProc
;
703 wc
.hInstance
= hInstance
;
704 wc
.hIcon
= LoadIcon(hInstance
,
705 MAKEINTRESOURCE(IDI_MAIN_ICON
));
706 wc
.hCursor
= LoadCursor(NULL
,
708 wc
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
709 wc
.lpszMenuName
= MAKEINTRESOURCE(IDR_MAINMENU
);
710 wc
.lpszClassName
= szMainWndClass
;
711 wc
.hIconSm
= (HICON
)LoadImage(hInstance
,
712 MAKEINTRESOURCE(IDI_MAIN_ICON
),
718 return RegisterClassEx(&wc
) != (ATOM
)0;
723 UninitMainWindowImpl(VOID
)
725 UnregisterClass(szMainWndClass
,