ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

MainWindow.cpp
Go to the documentation of this file.
00001 #include "StdAfx.h"
00002 #include "devmgmt.h"
00003 #include "MainWindow.h"
00004 
00005 /* menu hints */
00006 static const MENU_HINT MainMenuHintTable[] =
00007 {
00008     /* File Menu */
00009     {IDC_EXIT,     IDS_HINT_EXIT},
00010 
00011     /* Action Menu */
00012     {IDC_REFRESH,  IDS_HINT_REFRESH},
00013     {IDC_PROP,     IDS_HINT_PROP},
00014 
00015     {IDC_ABOUT,    IDS_HINT_ABOUT}
00016 };
00017 
00018 /* system menu hints */
00019 static const MENU_HINT SystemMenuHintTable[] =
00020 {
00021     {SC_RESTORE,    IDS_HINT_SYS_RESTORE},
00022     {SC_MOVE,       IDS_HINT_SYS_MOVE},
00023     {SC_SIZE,       IDS_HINT_SYS_SIZE},
00024     {SC_MINIMIZE,   IDS_HINT_SYS_MINIMIZE},
00025     {SC_MAXIMIZE,   IDS_HINT_SYS_MAXIMIZE},
00026     {SC_CLOSE,      IDS_HINT_SYS_CLOSE},
00027 };
00028 
00029 
00030 
00031 CMainWindow::CMainWindow(void) :
00032     m_hMainWnd(NULL),
00033     m_hStatusBar(NULL),
00034     m_hToolBar(NULL),
00035     m_CmdShow(0)
00036 {
00037     m_szMainWndClass = L"DevMgmtWndClass";
00038 }
00039 
00040 CMainWindow::~CMainWindow(void)
00041 {
00042 }
00043 
00044 BOOL
00045 CMainWindow::CreateToolBar()
00046 {
00047     HIMAGELIST hImageList;
00048     HBITMAP hBitmap;
00049     UINT StartResource, EndResource;
00050     INT NumButtons;
00051     BOOL bRet = FALSE;
00052 
00053     static TBBUTTON ToolbarButtons [] =
00054     {
00055         {TBICON_PROP,    IDC_PROP,    TBSTATE_INDETERMINATE, BTNS_BUTTON, {0}, 0, 0},
00056         {TBICON_REFRESH, IDC_REFRESH, TBSTATE_ENABLED,       BTNS_BUTTON, {0}, 0, 0},
00057 
00058         /* Add a seperator: First item for a seperator is its width in pixels */
00059         {15, 0, TBSTATE_ENABLED, BTNS_SEP, {0}, 0, 0},
00060     };
00061 
00062     /* Get the number of buttons */
00063     NumButtons = sizeof(ToolbarButtons) / sizeof(ToolbarButtons[0]);
00064 
00065     /* Create the toolbar window */
00066     m_hToolBar = CreateWindowExW(0,
00067                                  TOOLBARCLASSNAME,
00068                                  NULL,
00069                                  WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS,
00070                                  0, 0, 0, 0,
00071                                  m_hMainWnd,
00072                                  (HMENU)IDC_TOOLBAR,
00073                                  g_hInstance,
00074                                  NULL);
00075     if (m_hToolBar)
00076     {
00077         /* Don't show clipped buttons */
00078         SendMessageW(m_hToolBar,
00079                      TB_SETEXTENDEDSTYLE,
00080                      0,
00081                      TBSTYLE_EX_HIDECLIPPEDBUTTONS);
00082 
00083         /* Set the struct size, the toobar needs this... */
00084         SendMessageW(m_hToolBar,
00085                      TB_BUTTONSTRUCTSIZE,
00086                      sizeof(ToolbarButtons[0]),
00087                      0);
00088 
00089         /* Create the toolbar icon image list */
00090         hImageList = ImageList_Create(16,
00091                                       16,
00092                                       ILC_MASK | ILC_COLOR24,
00093                                       NumButtons,
00094                                       0);
00095         if (hImageList)
00096         {
00097             /* Set the index endpoints */
00098             StartResource = IDB_PROP;
00099             EndResource = IDB_REFRESH;
00100 
00101             /* Add all icons to the image list */
00102             for (UINT i = StartResource; i <= EndResource; i++)
00103             {
00104                 /* Load the image resource */
00105                 hBitmap = (HBITMAP)LoadImage(g_hInstance,
00106                                              MAKEINTRESOURCE(i),
00107                                              IMAGE_BITMAP,
00108                                              16,
00109                                              16,
00110                                              LR_LOADTRANSPARENT);
00111                 if (hBitmap)
00112                 {
00113                     /* Add it to the image list */
00114                     ImageList_AddMasked(hImageList,
00115                                         hBitmap,
00116                                         RGB(255, 0, 128));
00117 
00118                     /* Delete the bitmap */
00119                     DeleteObject(hBitmap);
00120                 }
00121             }
00122 
00123             /* Set the new image list */
00124             hImageList = (HIMAGELIST)SendMessageW(m_hToolBar,
00125                                                   TB_SETIMAGELIST,
00126                                                   0,
00127                                                   (LPARAM)hImageList);
00128 
00129             /* Destroy any previous list */
00130             if (hImageList) ImageList_Destroy(hImageList);
00131 
00132             /* Add the buttons */
00133             bRet = (BOOL)SendMessageW(m_hToolBar,
00134                                       TB_ADDBUTTONS,
00135                                       NumButtons,
00136                                       (LPARAM)ToolbarButtons);
00137         }
00138     }
00139 
00140     return bRet;
00141 }
00142 
00143 BOOL
00144 CMainWindow::CreateStatusBar()
00145 {
00146     INT StatWidths[] = {110, -1}; /* widths of status bar */
00147     BOOL bRet = FALSE;
00148 
00149     /* Create the status bar */
00150     m_hStatusBar = CreateWindowExW(0,
00151                                    STATUSCLASSNAME,
00152                                    NULL,
00153                                    WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
00154                                    0, 0, 0, 0,
00155                                    m_hMainWnd,
00156                                    (HMENU)IDC_STATUSBAR,
00157                                    g_hInstance,
00158                                    NULL);
00159     if (m_hStatusBar)
00160     {
00161         /* Set the width */
00162         bRet = (BOOL)SendMessage(m_hStatusBar,
00163                                  SB_SETPARTS,
00164                                  sizeof(StatWidths) / sizeof(INT),
00165                                  (LPARAM)StatWidths);
00166     }
00167 
00168     return bRet;
00169 }
00170 
00171 BOOL
00172 CMainWindow::StatusBarLoadString(IN HWND hStatusBar,
00173                                  IN INT PartId,
00174                                  IN HINSTANCE hInstance,
00175                                  IN UINT uID)
00176 {
00177     CAtlString szMessage;
00178     BOOL bRet = FALSE;
00179 
00180     /* Load the string */
00181     if (szMessage.LoadStringW(hInstance, uID))
00182     {
00183         /* Send the message to the status bar */
00184         bRet = (BOOL)SendMessageW(hStatusBar,
00185                                  SB_SETTEXT,
00186                                  (WPARAM)PartId,
00187                                  (LPARAM)szMessage.GetBuffer());
00188     }
00189 
00190     return bRet;
00191 }
00192 
00193 LRESULT
00194 CMainWindow::OnCreate(HWND hwnd)
00195 {
00196     LRESULT RetCode;
00197 
00198     /* Assume failure */
00199     RetCode = -1;
00200 
00201     /* Store the window handle */
00202     m_hMainWnd = hwnd;
00203 
00204     /* Create the toolbar */
00205     if (CreateToolBar())
00206     {
00207         /* Create the statusbar */
00208         if (CreateStatusBar())
00209         {
00210             /* Create the device view object */
00211             m_DeviceView = new CDeviceView(m_hMainWnd);
00212 
00213             /* Initialize it */
00214             if (m_DeviceView->Initialize())
00215             {
00216                 /* Display the window according to the user request */
00217                 ShowWindow(hwnd, m_CmdShow);
00218 
00219                 /* Set as handled */
00220                 RetCode = 0;
00221             }
00222         }
00223     }
00224 
00225     return RetCode;
00226 }
00227 
00228 LRESULT
00229 CMainWindow::OnSize()
00230 {
00231     RECT rcClient, rcTool, rcStatus;
00232     INT lvHeight, iToolHeight, iStatusHeight;
00233 
00234     /* Autosize the toolbar */
00235     SendMessage(m_hToolBar, TB_AUTOSIZE, 0, 0);
00236 
00237     /* Get the toolbar rect and save the height */
00238     GetWindowRect(m_hToolBar, &rcTool);
00239     iToolHeight = rcTool.bottom - rcTool.top;
00240 
00241     /* Resize the status bar */
00242     SendMessage(m_hStatusBar, WM_SIZE, 0, 0);
00243 
00244     /* Get the statusbar rect and save the height */
00245     GetWindowRect(m_hStatusBar, &rcStatus);
00246     iStatusHeight = rcStatus.bottom - rcStatus.top;
00247 
00248     /* Get the full client rect */
00249     GetClientRect(m_hMainWnd, &rcClient);
00250 
00251     /* Calculate the remaining height for the treeview */
00252     lvHeight = rcClient.bottom - iToolHeight - iStatusHeight;
00253 
00254     /* Resize the device view */
00255     m_DeviceView->Size(0,
00256                        iToolHeight,
00257                        rcClient.right,
00258                        lvHeight);
00259 
00260     return 0;
00261 }
00262 
00263 LRESULT
00264 CMainWindow::OnNotify(LPARAM lParam)
00265 {
00266 
00267     return 0;
00268 }
00269 
00270 LRESULT
00271 CMainWindow::OnContext(LPARAM lParam)
00272 {
00273     return 0;
00274 }
00275 
00276 LRESULT
00277 CMainWindow::OnCommand(WPARAM wParam,
00278                        LPARAM lParam)
00279 {
00280     LRESULT RetCode = 0;
00281     WORD Msg;
00282 
00283     /* Get the message */
00284     Msg = LOWORD(wParam);
00285 
00286     switch (Msg)
00287     {
00288         case IDC_PROP:
00289         {
00290             /* Display the device property sheet */
00291             m_DeviceView->DisplayPropertySheet();
00292             break;
00293         }
00294 
00295         case IDC_REFRESH:
00296         {
00297             /* Refresh the device list */
00298             m_DeviceView->Refresh();
00299             break;
00300         }
00301 
00302         case IDC_ABOUT:
00303         {
00304             /* Blow my own trumpet */
00305             MessageBoxW(m_hMainWnd,
00306                         L"ReactOS Device Manager\r\nCopyright Ged Murphy 2011",
00307                         L"About",
00308                         MB_OK);
00309 
00310             /* Set focus back to the treeview */
00311             m_DeviceView->SetFocus();
00312             break;
00313         }
00314 
00315         case IDC_EXIT:
00316         {
00317             /* Post a close message to the window */
00318             PostMessageW(m_hMainWnd,
00319                          WM_CLOSE,
00320                          0,
00321                          0);
00322             break;
00323         }
00324 
00325         default:
00326             break;
00327     }
00328 
00329     return RetCode;
00330 }
00331 
00332 LRESULT
00333 CMainWindow::OnDestroy()
00334 {
00335     /* Uninitialize the device view */
00336     m_DeviceView->Uninitialize();
00337 
00338     /* Kill the object */
00339     delete m_DeviceView;
00340     m_DeviceView = NULL;
00341 
00342     /* Clear the user data pointer */
00343     SetWindowLongPtr(m_hMainWnd, GWLP_USERDATA, 0);
00344 
00345     /* Break the message loop */
00346     PostQuitMessage(0);
00347 
00348     return 0;
00349 }
00350 
00351 LRESULT CALLBACK
00352 CMainWindow::MainWndProc(HWND hwnd,
00353                          UINT msg,
00354                          WPARAM wParam,
00355                          LPARAM lParam)
00356 {
00357     CMainWindow *pThis;
00358     LRESULT RetCode = 0;
00359 
00360     /* Get the object pointer from window context */
00361     pThis = (CMainWindow *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
00362 
00363     /* Check for an invalid pointer */
00364     if (!pThis)
00365     {
00366         /* Check that this isn't a create message */
00367         if (msg != WM_CREATE)
00368         {
00369             /* Don't handle null info pointer */
00370             goto HandleDefaultMessage;
00371         }
00372     }
00373 
00374     switch(msg)
00375     {
00376         case WM_CREATE:
00377         {
00378             /* Get the object pointer from the create param */
00379             pThis = (CMainWindow *)((LPCREATESTRUCT)lParam)->lpCreateParams;
00380 
00381             /* Store the info pointer in the window's global user data */
00382             SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
00383 
00384             /* Call the create handler */
00385             RetCode = pThis->OnCreate(hwnd);
00386             break;
00387         }
00388 
00389         case WM_SIZE:
00390         {
00391             RetCode = pThis->OnSize();
00392             break;
00393         }
00394 
00395         case WM_NOTIFY:
00396         {
00397             /* Handle the notify message */
00398             RetCode = pThis->OnNotify(lParam);
00399             break;
00400         }
00401 
00402         case WM_CONTEXTMENU:
00403         {
00404             /* Handle creating the context menu */
00405             RetCode = pThis->OnContext(lParam);
00406             break;
00407         }
00408 
00409         case WM_COMMAND:
00410         {
00411             /* Handle the command message */
00412             RetCode = pThis->OnCommand(wParam, lParam);
00413 
00414             /* Hand it off to the default message handler */
00415             goto HandleDefaultMessage;
00416         }
00417 
00418         case WM_CLOSE:
00419         {
00420             /* Destroy the main window */
00421             DestroyWindow(hwnd);
00422         }
00423         break;
00424 
00425         case WM_DESTROY:
00426         {
00427             /* Call the destroy handler */
00428             RetCode = pThis->OnDestroy();
00429             break;
00430         }
00431 
00432         default:
00433         {
00434 HandleDefaultMessage:
00435             RetCode = DefWindowProc(hwnd, msg, wParam, lParam);
00436             break;
00437         }
00438     }
00439 
00440     return RetCode;
00441 }
00442 
00443 BOOL
00444 CMainWindow::Initialize(LPCTSTR lpCaption,
00445                         int nCmdShow)
00446 {
00447     CAtlString szCaption;
00448     WNDCLASSEXW wc = {0};
00449 
00450     /* Store the show window value */
00451     m_CmdShow = nCmdShow;
00452 
00453     /* Setup the window class struct */
00454     wc.cbSize = sizeof(WNDCLASSEXW);
00455     wc.lpfnWndProc = MainWndProc;
00456     wc.hInstance = g_hInstance;
00457     wc.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCEW(IDI_MAIN_ICON));
00458     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00459     wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
00460     wc.lpszMenuName = MAKEINTRESOURCEW(IDR_MAINMENU);
00461     wc.lpszClassName = m_szMainWndClass;
00462     wc.hIconSm = (HICON)LoadImage(g_hInstance,
00463                                   MAKEINTRESOURCE(IDI_MAIN_ICON),
00464                                   IMAGE_ICON,
00465                                   16,
00466                                   16,
00467                                   LR_SHARED);
00468 
00469     /* Register the window */
00470     if (RegisterClassExW(&wc))
00471     {
00472         /* Create the main window and store the info pointer */
00473         m_hMainWnd = CreateWindowExW(WS_EX_WINDOWEDGE,
00474                                      m_szMainWndClass,
00475                                      lpCaption,
00476                                      WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
00477                                      CW_USEDEFAULT,
00478                                      CW_USEDEFAULT,
00479                                      600,
00480                                      450,
00481                                      NULL,
00482                                      NULL,
00483                                      g_hInstance,
00484                                      this);
00485     }
00486 
00487     /* Return creation result */
00488     return !!(m_hMainWnd);
00489 }
00490 
00491 VOID
00492 CMainWindow::Uninitialize()
00493 {
00494     /* Unregister the window class */
00495     UnregisterClassW(m_szMainWndClass, g_hInstance);
00496 }
00497 
00498 INT
00499 CMainWindow::Run()
00500 {
00501     MSG Msg;
00502 
00503     /* Pump the message queue */
00504     while (GetMessageW(&Msg, NULL, 0, 0 ) != 0)
00505     {
00506         TranslateMessage(&Msg);
00507         DispatchMessageW(&Msg);
00508     }
00509 
00510     return 0;
00511 }

Generated on Sat May 26 2012 04:15:52 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.