1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * PROJECT:     ReactOS Applications
 * LICENSE:     LGPL - See COPYING in the top level directory
 * FILE:        base/applications/msconfig_new/msconfig.c
 * PURPOSE:     MSConfig main dialog
 * COPYRIGHT:   Copyright 2005-2006 Christoph von Wittich <Christoph@ApiViewer.de>
 */

#include "precomp.h"
#include "fileutils.h"
#include "utils.h"

#include "generalpage.h"
#include "systempage.h"
#include "freeldrpage.h"
#include "srvpage.h"
// #include "startuppage.h"
#include "toolspage.h"

/* Allow only for a single instance of MSConfig */
#ifdef _MSC_VER
    #pragma data_seg("MSConfigInstance")
    HWND hSingleWnd = NULL;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:MSConfigInstance,RWS")
#else
    HWND hSingleWnd __attribute__((section ("MSConfigInstance"), shared)) = NULL;
#endif

/* Defaults for ReactOS */
BOOL bIsWindows = FALSE;
BOOL bIsPreVistaOSVersion = TRUE;

/* Language-independent Vendor strings */
const LPCWSTR IDS_REACTOS   = L"ReactOS";
const LPCWSTR IDS_MICROSOFT = L"Microsoft";
const LPCWSTR IDS_WINDOWS   = L"Windows";

HINSTANCE hInst = NULL;
LPWSTR szAppName = NULL;
HWND hMainWnd;                   /* Main Window */

HWND  hTabWnd;                   /* Tab Control Window */
HICON hIcon = NULL, hIconSm = NULL;
WNDPROC wpOrigEditProc = NULL;


/* About Box dialog */
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;
            }
            break;
    }
    return (INT_PTR)FALSE;
}


/* Message handler for dialog box */
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
    switch (uMessage)
    {
        case WM_SYSCOMMAND:
        {
            switch (LOWORD(wParam) /*GET_WM_COMMAND_ID(wParam, lParam)*/)
            {
                case IDM_ABOUT:
                    DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_ABOUTBOX), hWnd, About);
                    // break;
                    return TRUE;
            }

            // break;
            return FALSE;
        }

        case WM_COMMAND:
        {
            switch (LOWORD(wParam) /*GET_WM_COMMAND_ID(wParam, lParam)*/)
            {
                case IDM_ABOUT:
                    DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_ABOUTBOX), hWnd, About);
                    // break;
                    return TRUE;
            }

            break;
            // return FALSE;
        }

        case WM_DESTROY:
        {
            if (wpOrigEditProc)
                SetWindowLongPtr(hWnd, DWLP_DLGPROC, (LONG_PTR)wpOrigEditProc);

            if (hIcon)   DestroyIcon(hIcon);
            if (hIconSm) DestroyIcon(hIconSm);
        }

        default:
            break;
    }

    /* Return */
    if (wpOrigEditProc)
        return CallWindowProc(wpOrigEditProc, hWnd, uMessage, wParam, lParam);
    else
        return FALSE;
}


#include <pshpack1.h>
typedef struct DLGTEMPLATEEX
{
    WORD dlgVer;
    WORD signature;
    DWORD helpID;
    DWORD exStyle;
    DWORD style;
    WORD cDlgItems;
    short x;
    short y;
    short cx;
    short cy;
} DLGTEMPLATEEX, *LPDLGTEMPLATEEX;
#include <poppack.h>

int CALLBACK PropSheetCallback(HWND hDlg, UINT message, LPARAM lParam)
{
    switch (message)
    {
        case PSCB_PRECREATE:
        {
            LPDLGTEMPLATE   dlgTemplate   =   (LPDLGTEMPLATE)lParam;
            LPDLGTEMPLATEEX dlgTemplateEx = (LPDLGTEMPLATEEX)lParam;

            // MFC : DS_MODALFRAME | DS_3DLOOK | DS_CONTEXTHELP | DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION;
            DWORD style   = DS_SHELLFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU;
                         // DS_SHELLFONT | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME;
            DWORD exStyle = WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;

            /* Hide the dialog by default; we will center it on screen later, and then show it */
            style &= ~WS_VISIBLE;

            /* Set the styles of the property sheet dialog */
            if (dlgTemplateEx->signature == 0xFFFF)
            {
                dlgTemplateEx->style   = style;
                dlgTemplateEx->exStyle = exStyle;
            }
            else
            {
                dlgTemplate->style           = style;
                dlgTemplate->dwExtendedStyle = exStyle;
            }

            break;
        }

        case PSCB_INITIALIZED:
        {
            /* Customize the window's system menu, add items before the "Close" item */
            LPWSTR szMenuString;
            HMENU hSysMenu = GetSystemMenu(hDlg, FALSE);
            assert(hSysMenu);

            szMenuString = LoadResourceString(hInst, IDS_ABOUT);
            if (szMenuString)
            {
                /* "About" menu */
                InsertMenuW(hSysMenu, SC_CLOSE, MF_BYCOMMAND | MF_ENABLED | MF_STRING, IDM_ABOUT, szMenuString);
                /* Separator */
                InsertMenuW(hSysMenu, SC_CLOSE, MF_BYCOMMAND | MF_SEPARATOR          , 0 , NULL);

                MemFree(szMenuString);
            }
            DrawMenuBar(hDlg);

            /* Set the dialog icons */
            hIcon   = (HICON)LoadImageW(hInst, MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
            hIconSm = (HICON)CopyImage(hIcon, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_COPYFROMRESOURCE);
            SendMessageW(hDlg, WM_SETICON, ICON_BIG,   (LPARAM)hIcon);
            SendMessageW(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);

            /* Sub-class the property sheet window procedure */
            wpOrigEditProc = (WNDPROC)SetWindowLongPtr(hDlg, DWLP_DLGPROC, (LONG_PTR)MainWndProc);

            break;
        }

        default:
            break;
    }

    return FALSE;
}

HWND CreatePropSheet(HINSTANCE hInstance, HWND hwndOwner, LPCTSTR lpszTitle)
{
    HWND hPropSheet;
    PROPSHEETHEADERW psh;
    PROPSHEETPAGEW   psp[7];
    unsigned int nPages = 0;

    /* Header */
    psh.dwSize      = sizeof(psh);
    psh.dwFlags     = PSH_PROPSHEETPAGE | PSH_MODELESS | /*PSH_USEICONID |*/ PSH_HASHELP | /*PSH_NOCONTEXTHELP |*/ PSH_USECALLBACK;
    psh.hInstance   = hInstance;
    psh.hwndParent  = hwndOwner;
    // psh.pszIcon     = MAKEINTRESOURCEW(IDI_APPICON); // Disabled because it only sets the small icon; the big icon is a stretched version of the small one.
    psh.pszCaption  = lpszTitle;
    psh.nStartPage  = 0;
    psh.ppsp        = psp;
    psh.pfnCallback = PropSheetCallback;

    /* General page */
    psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
    psp[nPages].dwFlags     = PSP_HASHELP;
    psp[nPages].hInstance   = hInstance;
    psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_GENERAL_PAGE);
    psp[nPages].pfnDlgProc  = GeneralPageWndProc;
    ++nPages;

    // if (bIsPreVistaOSVersion)
    {
        /* SYSTEM.INI page */
        if (MyFileExists(lpszSystemIni, NULL))
        {
            psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
            psp[nPages].dwFlags     = PSP_HASHELP | PSP_USETITLE;
            psp[nPages].hInstance   = hInstance;
            psp[nPages].pszTitle    = MAKEINTRESOURCEW(IDS_TAB_SYSTEM);
            psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_SYSTEM_PAGE);
            psp[nPages].pfnDlgProc  = SystemPageWndProc;
            psp[nPages].lParam      = (LPARAM)lpszSystemIni;
            ++nPages;

            BackupIniFile(lpszSystemIni);
        }

        /* WIN.INI page */
        if (MyFileExists(lpszWinIni, NULL))
        {
            psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
            psp[nPages].dwFlags     = PSP_HASHELP | PSP_USETITLE;
            psp[nPages].hInstance   = hInstance;
            psp[nPages].pszTitle    = MAKEINTRESOURCEW(IDS_TAB_WIN);
            psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_SYSTEM_PAGE);
            psp[nPages].pfnDlgProc  = WinPageWndProc;
            psp[nPages].lParam      = (LPARAM)lpszWinIni;
            ++nPages;

            BackupIniFile(lpszWinIni);
        }
    }

    /* FreeLdr page */
    // TODO: Program the interface for Vista: "light" BCD editor...
    if (!bIsWindows || (bIsWindows && bIsPreVistaOSVersion))
    {
        LPCWSTR lpszLoaderIniFile = NULL;
        DWORD   dwTabNameId       = 0;
        if (bIsWindows)
        {
            lpszLoaderIniFile = lpszBootIni;
            dwTabNameId       = IDS_TAB_BOOT;
        }
        else
        {
            lpszLoaderIniFile = lpszFreeLdrIni;
            dwTabNameId       = IDS_TAB_FREELDR;
        }

        if (MyFileExists(lpszLoaderIniFile, NULL))
        {
            psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
            psp[nPages].dwFlags     = PSP_HASHELP | PSP_USETITLE;
            psp[nPages].hInstance   = hInstance;
            psp[nPages].pszTitle    = MAKEINTRESOURCEW(dwTabNameId);
            psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_FREELDR_PAGE);
            psp[nPages].pfnDlgProc  = FreeLdrPageWndProc;
            psp[nPages].lParam      = (LPARAM)lpszLoaderIniFile;
            ++nPages;

            BackupIniFile(lpszLoaderIniFile);
        }
    }

    /* Services page */
    psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
    psp[nPages].dwFlags     = PSP_HASHELP;
    psp[nPages].hInstance   = hInstance;
    psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_SERVICES_PAGE);
    psp[nPages].pfnDlgProc  = ServicesPageWndProc;
    ++nPages;

#if 0
    /* Startup page */
    psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
    psp[nPages].dwFlags     = PSP_HASHELP;
    psp[nPages].hInstance   = hInstance;
    psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_STARTUP_PAGE);
    psp[nPages].pfnDlgProc  = StartupPageWndProc;
    ++nPages;
#endif

    /* Tools page */
    psp[nPages].dwSize      = sizeof(PROPSHEETPAGEW);
    psp[nPages].dwFlags     = PSP_HASHELP;
    psp[nPages].hInstance   = hInstance;
    psp[nPages].pszTemplate = MAKEINTRESOURCEW(IDD_TOOLS_PAGE);
    psp[nPages].pfnDlgProc  = ToolsPageWndProc;
    ++nPages;

    /* Set the total number of created pages */
    psh.nPages = nPages;

    /* Create the property sheet */
    hPropSheet = (HWND)PropertySheetW(&psh);
    if (hPropSheet)
    {
        /* Center the property sheet on the desktop and show it */
        ClipOrCenterWindowToMonitor(hPropSheet, MONITOR_WORKAREA | MONITOR_CENTER);
        ShowWindow(hPropSheet, SW_SHOWNORMAL);
    }

    return hPropSheet;
}

BOOL Initialize(HINSTANCE hInstance)
{
    BOOL Success = TRUE;
    LPWSTR lpszVistaAppName = NULL;
    HANDLE hSemaphore;
    INITCOMMONCONTROLSEX InitControls;

    /* Initialize our global version flags */
    bIsWindows = IsWindowsOS();
    bIsPreVistaOSVersion = IsPreVistaOSVersion();

    /* Initialize global strings */
    szAppName = LoadResourceString(hInstance, IDS_MSCONFIG);
    if (!bIsPreVistaOSVersion)
        lpszVistaAppName = LoadResourceString(hInstance, IDS_MSCONFIG_2);

    /* We use a semaphore in order to have a single-instance application */
    hSemaphore = CreateSemaphoreW(NULL, 0, 1, L"MSConfigRunning");
    if (!hSemaphore || GetLastError() == ERROR_ALREADY_EXISTS)
    {
        CloseHandle(hSemaphore);

        /*
         * A semaphore with the same name already exist. It should have been
         * created by another instance of MSConfig. Try to find its window
         * and bring it to front.
         */
        if ( (hSingleWnd && IsWindow(hSingleWnd))                         ||
             ( (hSingleWnd = FindWindowW(L"#32770", szAppName)) != NULL ) ||
             (!bIsPreVistaOSVersion ? ( (hSingleWnd = FindWindowW(L"#32770", lpszVistaAppName)) != NULL ) : FALSE) )
        {
            /* Found it. Show the window. */
            ShowWindow(hSingleWnd, SW_SHOWNORMAL);
            SetForegroundWindow(hSingleWnd);
        }

        /* Quit this instance of MSConfig */
        Success = FALSE;
    }
    if (!bIsPreVistaOSVersion) MemFree(lpszVistaAppName);

    /* Quit now if we failed */
    if (!Success)
    {
        MemFree(szAppName);
        return FALSE;
    }

    /* Initialize the common controls */
    InitControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitControls.dwICC  = ICC_LISTVIEW_CLASSES | ICC_TREEVIEW_CLASSES | ICC_UPDOWN_CLASS /* | ICC_PROGRESS_CLASS | ICC_HOTKEY_CLASS*/;
    InitCommonControlsEx(&InitControls);

    hInst = hInstance;

    return Success;
}

VOID Cleanup(VOID)
{
    MemFree(szAppName);

    // // Close the sentry semaphore.
    // CloseHandle(hSemaphore);
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    MSG msg;
    HACCEL hAccelTable;

    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    UNREFERENCED_PARAMETER(nCmdShow);

    /*
     * Initialize this instance of MSConfig. Quit if we have
     * another instance already running.
     */
    if (!Initialize(hInstance))
        return -1;

    // hInst = hInstance;

    hMainWnd = CreatePropSheet(hInstance, NULL, szAppName);
    if (!hMainWnd)
    {
        /* We failed, cleanup and bail out */
        Cleanup();
        return -1;
    }

    hAccelTable = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(IDR_MSCONFIG));

    /* Message loop */
    while (IsWindow(hMainWnd) && GetMessageW(&msg, NULL, 0, 0))
    {
        /*
         * PropSheet_GetCurrentPageHwnd returns NULL when the user clicks the OK or Cancel button
         * and after all of the pages have been notified. Apply button doesn't cause this to happen.
         * We can then use the DestroyWindow function to destroy the property sheet.
         */
        if (PropSheet_GetCurrentPageHwnd(hMainWnd) == NULL)
            break;

        /* Process the accelerator table */
        if (!TranslateAcceleratorW(hMainWnd, hAccelTable, &msg))
        {
            /*
             * If e.g. an item on the tree view is being edited,
             * we cannot pass the event to PropSheet_IsDialogMessage.
             * Doing so causes the property sheet to be closed at Enter press
             * (instead of completing edit operation).
             */
            if (/*g_bDisableDialogDispatch ||*/ !PropSheet_IsDialogMessage(hMainWnd, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessageW(&msg);
            }
        }
    }

    // FIXME: Process the results of MSConfig !!

    /* Destroy the accelerator table and the window */
    if (hAccelTable != NULL)
        DestroyAcceleratorTable(hAccelTable);

    DestroyWindow(hMainWnd);

    /* Finish cleanup and return */
    Cleanup();
    return (int)msg.wParam;
}

/* EOF */