All pastes #211610 Raw Edit

the.golem

public cpp v1 · immutable
#211610 ·published 2006-10-20 12:04 UTC
rendered paste body
/*  Copyright (C) 2005-2006 Darrin Roenfanz (( the.golem ))  for any question or comment contact me at darrin.roenfanz@gmail.com.   This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.   This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.  You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#define WIN32_LEAN_AND_MEAN    #include <windows.h>           #include <commdlg.h>#include <string>#include <vector>#include <fstream>#include <iostream>#include <shlwapi.h>#include <map>using namespace std;const string sEvarText[13] = {        "FileManager", "TxtEditor", "CmdPrompt",        "AudioPlayer", "MediaPlayer", "GfxViewer",        "GfxEditor", "Browser", "DUN",        "Email", "IRC", "FTP",        "IM" };//=========================================================================================struct evarStruct{	int evarline;	string evarname;	string evarpath;	string whitespace;	string comment;};//=========================================================================================LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam);HINSTANCE hInstance;//================ FONT STUFFS ============================================================//=========================================================================================HFONT SetFont(HWND hwndCtrl, const char* chFont){    HFONT hFont = NULL;    if (hwndCtrl)    {        hFont = CreateFont(9, 4, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, chFont);        SendMessage(hwndCtrl, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(0, 0));    }    return hFont;}HFONT hFont[41];//================ TEXT CONTROL ===========================================================//=========================================================================================HWND Text(HWND hParent, const char* evarTEXT, int iX, int iY){    return CreateWindowEx(               0, "STATIC", evarTEXT,               WS_CHILD|WS_VISIBLE|SS_SIMPLE,               iX, iY, 60, 20,               hParent, NULL, hInstance, NULL);}//================ EDIT CONTROL ===========================================================//=========================================================================================HWND Edit(HWND hParent, LPCSTR cText ,int iX, int iY){    return CreateWindowEx(               WS_EX_CLIENTEDGE, "EDIT", cText,               WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,               iX, iY, 335, 20,               hParent, NULL, hInstance, NULL);}HWND hEdit[13];//================ BUTTON CONTROL =========================================================//=========================================================================================HWND Button(HWND hParent, LPCSTR cText, int iX, int iY, int iWidth, int iHeight, int nDefBtn ){    return CreateWindowEx(               0, "BUTTON", cText,               WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,               iX, iY, iWidth, iHeight,               hParent, reinterpret_cast<HMENU>(nDefBtn), hInstance, NULL);}//================ EVARS.RC LOCATION ======================================================//=========================================================================================string EvarRC(){    DWORD dwSize = 260;    char szPersonalDir[260];    DWORD result = SHGetValueA(                       HKEY_LOCAL_MACHINE,                       "SOFTWARE\\LOSI\\Installer",                       "PersonalDir",                       NULL,                       szPersonalDir,                       &dwSize );    string sData = szPersonalDir;    sData.append("\\evars.rc");    return sData;}////////////////////////////////////////////////////////////////////////////////////////////////// THE BELOW WILL BE IMPLEMENTED IN A LATER RELEASE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*================ SYSTEM DEFAULTS ==============================string sApplyDefaults( string strEvar ){    LPCSTR strSystemDefaults[13] = {      "folder\\shell\\open\\command",      "txtfile\\shell\\open\\command",      " ",      "mp3file\\shell\\open\\command",      "avifile\\shell\\open\\command",      "jpegfile\\shell\\open\\command",      "jpegfile\\shell\\edit\\command",      "http\\shell\\open\\command",      " ",      "mailto\\shell\\open\\command",      "irc\\shell\\open\\command",      "ftp\\shell\\open\\command",      " " };          DWORD dwDefaults = 260;    DWORD dwComspec = 260;    char szComspec[260];    char szDefaults[260];    string strSysPath;    int i = 0;    while ( strEvar != sEvarText[i] ){ ++i; }    DWORD result = SHGetValueA (                   HKEY_CLASSES_ROOT,                    strSystemDefaults[i],                   NULL,                   NULL,                   szDefaults,                   &dwDefaults );            if ( strEvar == "DUN" )          strSysPath = "...";    else if ( strEvar == "IM" )          strSysPath = "C:\\Path\\To\\IM_Client";    else if( strEvar == "CmdPrompt" )         {         DWORD result = ExpandEnvironmentStrings( "%comspec%", szComspec, dwComspec );         strSysPath = szComspec;         }    else    {        strSysPath = szDefaults;        if ( strSysPath[0] == '"' )           {           strSysPath = strSysPath.substr(strSysPath.find_first_not_of(" \t\""));           strSysPath = strSysPath.substr(0, strSysPath.find_first_of("\""));           }        else            strSysPath = strSysPath.substr(0, strSysPath.find_first_of(" "));                             }    return strSysPath;}*/////////////////////////////////////////////////////////////////////////////////////////////////// THE ABOVE WILL BE IMPLEMENTED IN A LATER RELEASE ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////================ FILE PARSER CODE =======================================================//=========================================================================================multimap<string, evarStruct> ParseFile(){    evarStruct eStruct; 	multimap<string, evarStruct> evarMap;     string filename = EvarRC();    ifstream file( filename.c_str() );     string line, sPath, sKey;     int evarline = 0;    while ( getline(file, line) )    {        ++evarline;         size_t first_token =  line.find_first_not_of(" \t");         if (first_token != string::npos)        {            if (line[first_token] != ';')            {                size_t separator = line.find_first_of(" \t", first_token);                 if (separator != string::npos)                {                    eStruct.evarname = line.substr(first_token, separator);                    separator = line.find_first_not_of(" \t\"", separator);                    sPath = line.substr(separator);                    sPath = sPath.substr(0, sPath.find_last_not_of(" \t\"")+1);                    eStruct.evarpath = sPath;                    sKey = eStruct.evarname;                }            }            else            {                eStruct.comment = line;                sKey = "comment";            }        }        else        {            eStruct.whitespace = line;            sKey = "whitespace";        }        eStruct.evarline = evarline;        evarMap.insert(pair<string , evarStruct>(sKey, eStruct));    }     file.close();    return evarMap;}//================ FILE WRITING CODE ======================================================//=========================================================================================void WriteFile(){    vector<string>  evarfile;    string sFileName = EvarRC();	ifstream OpenFile(sFileName.c_str());	string line, sEvarName, evarname;	char szString[260];	string sText[13] = {		"FileManager", "TxtEditor  ", "CmdPrompt  ",		"AudioPlayer", "MediaPlayer", "GfxViewer  ",		"GfxEditor  ", "Browser    ", "DUN        ",		"Email      ", "IRC        ", "FTP        ",		"IM         " };    while (getline(OpenFile, line))    {        size_t linebegin =  line.find_first_not_of(" \t");        if (linebegin != string::npos && line[linebegin] != ';')        {           for(int i = 0; i < 13; i++)           {                size_t separator = line.find_first_of(" \t", linebegin);                if (linebegin == line.find(sEvarText[i],linebegin))                {                    sEvarName = line.substr(linebegin, separator - linebegin);                    GetWindowText(hEdit[i], szString, 260);                    line = string(sText[i] + " \t" + '\"' + string (szString) + '\"' );                }           }        }        evarfile.push_back(line);    }    OpenFile.close();	ofstream CloseFile(sFileName.c_str());    for(int i = 0; i < evarfile.size(); i++)		CloseFile << evarfile[i] << endl;}//================ THE "BROWSE" DIALOGUE ==================================================//=========================================================================================string BrowseForFile(HWND hWnd){    OPENFILENAME ofn;       // common dialog box structure    char szFile[260];       // buffer for file name    HWND hwnd;              // owner window    HANDLE hf;              // file handle    hwnd = hWnd;    // Initialize OPENFILENAME    ZeroMemory(&ofn, sizeof(ofn));    ofn.lStructSize = sizeof(ofn);    ofn.hwndOwner = hwnd;    ofn.lpstrFile = szFile;    //    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not    // use the contents of szFile to initialize itself.    //    ofn.lpstrFile[0] = '\0';    ofn.nMaxFile = sizeof(szFile);    ofn.lpstrFilter = "Program Files (*.exe)\0*.exe\0All\0*.*\0";    ofn.nFilterIndex = 1;    ofn.lpstrFileTitle = NULL;    ofn.nMaxFileTitle = 0;    ofn.lpstrInitialDir = NULL;    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;    // Display the Open dialog box.    if (GetOpenFileName(&ofn)==TRUE){}    return string(szFile);}//================ MAIN WINDOW CODE =======================================================//=========================================================================================int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){    HWND    hwnd;    MSG     Msg;    HICON   hIcon;    HCURSOR hCursor;    TCHAR chClassName[]=TEXT("EvarConfig");    WNDCLASSEX wcx;    hIcon=(HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);    hCursor=(HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);    wcx.cbSize           = sizeof(WNDCLASSEX);    wcx.style            = CS_HREDRAW|CS_VREDRAW;    wcx.lpfnWndProc      = (WNDPROC)WndProc;    wcx.cbClsExtra       = 0;    wcx.cbWndExtra       = 0;    wcx.hInstance        = hInstance;    wcx.hIcon            = hIcon;    wcx.hCursor          = hCursor;    wcx.hbrBackground    = (HBRUSH)(COLOR_BTNFACE+1);    wcx.lpszMenuName     = NULL;    wcx.lpszClassName    = chClassName;    wcx.hIconSm          = NULL;    if (!RegisterClassEx(&wcx))    {        MessageBox( NULL,                    TEXT("Failed to register wnd class"),                    TEXT("ERROR"),                    MB_OK|MB_ICONERROR);        return FALSE;    }    hwnd = CreateWindowEx(               0, chClassName, TEXT("LOSI Evar Configurator"),               WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,               200, 200, 500, 400,               NULL, NULL, hInstance, NULL);    if (!hwnd)    {        MessageBox( NULL, TEXT("Failed to create wnd"), TEXT("ERROR"),                    MB_OK|MB_ICONERROR);        return FALSE;    }    ShowWindow(hwnd,nCmdShow);    UpdateWindow(hwnd);    while (GetMessage(&Msg,NULL,0,0)>0)    {        TranslateMessage(&Msg);        DispatchMessage(&Msg);    }    return Msg.wParam;}//================ CALLBACK PROCEDURES ====================================================//=========================================================================================LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam){    switch (Message)    {        case WM_COMMAND:        {            switch(LOWORD(wParam))            {                case 1101:                    WriteFile();                case 1102:                     DestroyWindow(hwnd);				break;				/* APPLY DEFAULTS BUTTON; TO BE IMPLEMENTED LATER                case 1103:                     for (int i = 0; i < 13; ++i)                     { SetWindowText( hEdit[i], sApplyDefaults( sEvarText[i] ).c_str() ); }                break;  */                default:                if ( wParam <= 12 )                {                    string sFile = BrowseForFile(hwnd).c_str() ;                    if (sFile != "")                        SetWindowText(hEdit[wParam], sFile.c_str() );                }                break;            }        }        break;        case WM_CREATE:        {            multimap<string, evarStruct> fileMap = ParseFile();            for (int i = 0; i < 13; ++i)            {                hEdit[i] = Edit( hwnd, fileMap.find( sEvarText[i] )->second.evarpath.c_str(), 75, i * 25 + 10);                hFont[i] = SetFont( hEdit[i] , "MS Sans Serif" ) ;                hFont[i+13] = SetFont( Text( hwnd, sEvarText[i].c_str() , 10, i * 25 + 10) , "MS Sans Serif" );                hFont[i+26] = SetFont( Button( hwnd, "Browse", 420, i * 25 + 10, 65, 20, i) , "MS Sans Serif" );            }            hFont[39] = SetFont( Button( hwnd, "Save", 10, 345, 65, 20, 1101) , "MS Sans Serif" ) ;            hFont[40] = SetFont( Button( hwnd, "Cancel", 420, 345, 65, 20, 1102) , "MS Sans Serif" ) ;            /* TO BE IMPLEMENTED LATER /////////////////               hFont[41] = SetFont( Button( hwnd, "Get Defaults", 85, 345, 85, 20, 1103) , "MS Sans Serif" ) ;            */            break;        }        case WM_DESTROY:			for ( int i = 0; i < 41; ++i )				DeleteObject( hFont[i] );			PostQuitMessage(0);    //signal end of application        break;        default:        return DefWindowProc( hwnd, Message, wParam, lParam );  //let system deal with msg    }}//================ END OF FILE!! ==========================================================//=========================================================================================