Advertisement
Kyosaur

Simple Convert (Old - My first c++ application)

Jul 22nd, 2011
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.46 KB | None | 0 0
  1. /*
  2.     Simple Convert is an application which converts map files into a
  3.     format suitible for San Andreas Multiplayer. Copyright (C) 2010
  4.     Kyoshiro aka Kyosaur.
  5.  
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, version 3 of the license.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19.  
  20.  
  21.  
  22. #include <iostream>
  23. #include <fstream>
  24. #include <string>
  25. #include <vector>
  26. #include <map>
  27. #include <windows.h>
  28. #include <tchar.h>
  29.  
  30. using namespace std;
  31.  
  32. //type def incase i want to use it
  33. typedef const char cchar;
  34.  
  35. //window dimensions (Both windows)
  36. #define WINDOW_WIDTH    300
  37. #define WINDOW_HEIGHT   300
  38. #define CUSTOM_WIDTH    340
  39. #define CUSTOM_HEIGHT   250
  40.  
  41. //function overloading for itos
  42. inline void itos(char str[], float number) { sprintf(str, "%f", number); }
  43. inline void itos(char str[], int number) { sprintf(str, "%d", number); }
  44. inline void itos(TCHAR str[], float number) { wsprintf(str, _T("%f"), number); }
  45. inline void itos(TCHAR str[], int number) { wsprintf(str, _T("%d"), number); }
  46.  
  47. //macro to just shorten the code...
  48. #define SPECIFIER_CHECK(p) if(!str.compare(pos,(end-pos)+1,"{" #p "}")){ itos(tmp,p); str.replace(pos,(end-pos)+1,tmp); continue;}
  49.  
  50. //enum for our menu's and "events"
  51. enum E_EVENT_MENU
  52. {
  53.     E_MENU_MENUBAR,
  54.     E_MENU_FILE,
  55.     E_MENU_OPEN,
  56.     E_MENU_SAVE,
  57.     E_MENU_QUIT,
  58.     E_MENU_OBJECTS,
  59.     E_MENU_VEHICLES,
  60.     E_MENU_FO_CREATEOBJECT,
  61.     E_MENU_FO_INCOGNITO,
  62.     E_MENU_FO_XSTREAMER,
  63.     E_MENU_FO_XOBJECTS,
  64.     E_MENU_FO_CUSTOM,
  65.     E_MENU_FO_NONE,
  66.     E_MENU_FO_ADDSTATICV,
  67.     E_MENU_FO_ADDSTATICVEX,
  68.     E_MENU_FO_CREATEVEHICLE,
  69.     E_MENU_FO_VCUSTOM,
  70.     E_MENU_FI_MTARACE,
  71.     E_MENU_FI_MTA1,
  72.     E_EVENT_TEXTBOX,
  73.     E_EVENT_CONVERT,
  74.     E_EVENT_OBJFORMAT,
  75.     E_EVENT_VEHFORMAT,
  76.     E_EVENT_CONTINUE
  77. };
  78.  
  79. //structer for our g_Selected array
  80. struct hSelect
  81. {
  82.     HMENU menu;
  83.     int id;
  84. };
  85.  
  86. //Array for menu selection tracking
  87. hSelect g_Selected[3] = {NULL};
  88.  
  89. //Handles for our two windows
  90. HWND
  91.     Main,
  92.     Other;
  93.  
  94.  
  95. //map for out custom formats
  96. map<int,wstring> g_Formats;
  97.  
  98.  
  99. //This tracks our textbox message (assign a start message)
  100. wstring
  101.     g_TextMessage = _T("Please paste your MTA map here, or open it with the file menu!\
  102.                         \r\n- You can chose your input and output format by changing them in the options menu.\
  103.                         \r\n- This is just a beta version, i will be making improvements!");
  104.  
  105.  
  106. //Our class for converting
  107. class Convert
  108. {
  109. private:
  110.     int
  111.         Model,
  112.         Interior,
  113.         Dimension;
  114.     float
  115.         X,
  116.         Y,
  117.         Z,
  118.         RX,
  119.         RY,
  120.         RZ;
  121. public:
  122.     Convert();
  123.  
  124.     void ParseInput(string,string,string,bool);
  125.     string FormatOutput(string);
  126. };
  127.  
  128. //Convert constructor  
  129. Convert::Convert()
  130. {
  131.     Model = Interior = Dimension = X = Y = Z = RX = RY = RZ = NULL;
  132. }
  133.  
  134. //Function to parse data from our textbox
  135. void Convert::ParseInput(string Input, string ObjFormat, string VehFormat, bool Race)
  136. {
  137.  
  138. //Defines for sscanf (replacement string specifier)
  139. #define SP_s "%[a-z-A-Z-0-9-_-.-(-)- ]"
  140. #define SP_S "%*[a-z-A-Z-0-9-_-.-(-)- ]"
  141.  
  142.     //Create our pointer to track tokens, and 2 string objects
  143.     char* tok = NULL;
  144.     string str[2];
  145.  
  146.     /*  Since strtok asks for a char pointer, we have to dynamically
  147.         allocate some memory of equal size to our string size, plus
  148.         room for the terminating null character ('\0'). Then i copy
  149.         the Input string to the newly created buf.
  150.     */
  151.  
  152.     char* buf = new char[Input.size()+1];
  153.     strcpy(buf,Input.c_str());
  154.  
  155.     //Strtok to split our map into tokens (using < and > as delimiters)
  156.     tok = strtok(buf, "<>");
  157.  
  158.     //While there are tokens left
  159.     while(tok)
  160.     {
  161.         //Chose the parser based on our race variable
  162.  
  163.         if(Race)
  164.         {
  165.             //Race parser comming soon
  166.  
  167.             /*
  168.               <object name="object (16)">
  169.                 <position>-2252.4368 -1656.4774 455.7871</position>
  170.                 <rotation>-0.196349540625 0.69000021102425 0</rotation>
  171.                 <model>18450</model>
  172.               </object>
  173.             */
  174.  
  175.         }
  176.         else
  177.         {
  178.             //We use sscanf to parse our object data, if the format matches
  179.             int Obj = sscanf(tok,"object id=\"" SP_S "\" model=\"%d\" interior=\"%d\" dimension=\"%d\" posX=\"%f\" posY=\"%f\" posZ=\"%f\" rotX=\"%f\" rotY=\"%f\" rotZ=\"%f\" /",&Model,&Interior,&Dimension,&X,&Y,&Z,&RX,&RY,&RZ);
  180.            
  181.             //if it does match (9 = number of specifiers that ARENT "quiet")
  182.             if(Obj == 9)
  183.             {
  184.                 //Format the parsed data into the user's format and add it to str[0].
  185.                 str[0] += FormatOutput(ObjFormat) += "\r\n";
  186.             }
  187.             else
  188.             {
  189.                 /*
  190.                     if our sscanf check failed, its not an appropriate object.
  191.                     We then check if the user specified if they wanted any vehicles
  192.                     by checking our vehicle menu tracker.
  193.                 */
  194.  
  195.                 if(g_Selected[2].id != E_MENU_FO_NONE)
  196.                 {
  197.                     //If they wanted vehicles, we check the token again to see if its a vehicle.
  198.                     int veh = sscanf(tok,"vehicle id=\"" SP_S "\" paintjob=\"%*d\" model=\"%d\" plate=\"" SP_S "\" interior=\"%d\" dimension=\"%d\" posX=\"%f\" posY=\"%f\" posZ=\"%f\" rotX=\"%f\" rotY=\"%f\" rotZ=\"%f\" /",&Model,&Interior,&Dimension,&X,&Y,&Z,&RX,&RY,&RZ);  
  199.                
  200.                     //if it matches (again)
  201.                     if(veh == 9)
  202.                     {
  203.                         //Format the parsed vehicle data into the user's format.
  204.                         str[1] += FormatOutput(VehFormat) += "\r\n";
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.         /*
  210.             When strtok's str param is NULL, it continues going through
  211.             the tokens, which is exactly what we want to do in order to
  212.             convert the entire map.
  213.         */
  214.         tok = strtok(NULL, "<>");
  215.     }
  216.  
  217.     //When the conrting process is over we delete our allocated memory (buf)
  218.     delete []buf;
  219.    
  220.     //Empty our global string that tracks E_EVENT_TEXTBOX since its about to change
  221.     g_TextMessage.clear();
  222.  
  223.     //Add the vehicle string to out object string
  224.     str[0] += "\r\n";
  225.     str[0] += str[1];
  226.  
  227.     //Assign the object string to our E_EVENT_TEXTBOX tracking string.
  228.     //We only add the object string since it now contains the vehicles too.
  229.     g_TextMessage.assign(str[0].begin(),str[0].end());
  230.  
  231.     //Update our edit control E_EVENT_TEXTBOX
  232.     SetDlgItemText(Main,E_EVENT_TEXTBOX,g_TextMessage.c_str());
  233. }
  234.  
  235. //Yucky FormatOutput for converting
  236. string Convert::FormatOutput(string format)
  237. {
  238.     //Create a temporary string so we dont mod format.
  239.     string str  = format;
  240.    
  241.     //Create 2 cars for tracking positions of specifiers.
  242.     size_t
  243.         pos     = NULL,
  244.         end     = NULL;
  245.  
  246.     //char used for itos (in the macros below).
  247.     char tmp[41];
  248.  
  249.     //search the string for "{"
  250.     while((pos = str.find("{",pos+1)) != string::npos)
  251.     {
  252.         //while there's a "{", search for a "}"
  253.         if((end = str.find("}",pos)) != string::npos)
  254.         {
  255.             /*
  256.                 if we found a closing brace we need
  257.                 to check if the content between matches
  258.                 one of our specifiers. If it does replace
  259.                 that part of the string with the correct
  260.                 value.
  261.  
  262.                 I use a macro to cut down the code drastically.
  263.             */
  264.  
  265.             SPECIFIER_CHECK(Model);
  266.             SPECIFIER_CHECK(X);
  267.             SPECIFIER_CHECK(Y);
  268.             SPECIFIER_CHECK(Z);
  269.             SPECIFIER_CHECK(RX);
  270.             SPECIFIER_CHECK(RY);
  271.             SPECIFIER_CHECK(RZ);
  272.             SPECIFIER_CHECK(Interior);
  273.             SPECIFIER_CHECK(Dimension);
  274.         }
  275.     }
  276.     return str;
  277. }
  278.  
  279. //function prototype for our Window procedures (aka "forwards")
  280. LRESULT CALLBACK MainProc(HWND, UINT, WPARAM, LPARAM);
  281. LRESULT CALLBACK OtherProc(HWND, UINT, WPARAM, LPARAM);
  282.  
  283. //function prototype for saving/opening files (aka a "forward")
  284. int OpenFileDialog(HWND hWnd, int item, const TCHAR* filter, bool save = false, bool append = false);
  285. string WstrToStr(wstring);
  286.  
  287.  
  288. //Heart of the code (main)
  289. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  290. {
  291.     //Creating our WNDCLASS and MSG structures.
  292.     MSG msg             = {NULL};
  293.     WNDCLASS wc         = {NULL};
  294.  
  295.     //Fill out information for our windows
  296.     wc.lpszClassName    = _T("Main");
  297.     wc.lpfnWndProc      = (WNDPROC) MainProc;
  298.     wc.hInstance        = hInstance;
  299.     wc.hbrBackground    = GetSysColorBrush(COLOR_3DFACE);
  300.     wc.hCursor          = LoadCursor(hInstance,IDC_ARROW);
  301.     wc.hIcon            = (HICON) LoadImage(NULL, _T("Resources/Icon/icon.ico"), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  302.    
  303.  
  304.     //register and create our first window structure
  305.     RegisterClass(&wc);
  306.     Main = CreateWindow(wc.lpszClassName,_T("Simple Convert"),WS_VISIBLE | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,100,100,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);
  307.  
  308.     //Change the class name and procedure pointer for the second window
  309.     wc.lpszClassName    = _T("Other");
  310.     wc.lpfnWndProc      = (WNDPROC) OtherProc;
  311.  
  312.     //Regester and create our second window
  313.     RegisterClass(&wc);
  314.     Other = CreateWindow(wc.lpszClassName,_T("Simple Covert"),WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,100,100,CUSTOM_WIDTH,CUSTOM_HEIGHT,Main,NULL,hInstance,NULL);
  315.  
  316.     //Here we get messages from windows, translate them,
  317.     //and then send them to the window procedure(s).
  318.    
  319.     while(GetMessage(&msg,NULL,NULL,NULL))
  320.     {
  321.         TranslateMessage(&msg);
  322.         DispatchMessage(&msg);
  323.     }
  324.  
  325.     return (int) msg.wParam;
  326. }
  327.  
  328. //Callback for our second window
  329. LRESULT CALLBACK OtherProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  330. {
  331.     switch(Msg)
  332.     {
  333.         case WM_CREATE:
  334.         {
  335.             //Text for our static control
  336.             const TCHAR* Text = _T("Please enter or load a custom format! For help \r\nand available specifiers see the help menu.");
  337.  
  338.             //Create our formats for our g_Formats map
  339.             g_Formats[E_MENU_FO_CREATEOBJECT]   = _T("CreateObject({Model},{X},{Y},{Z},{RX},{RY},{RZ});");
  340.             g_Formats[E_MENU_FO_INCOGNITO]      = _T("CreateDynamicObject({Model},{X},{Y},{Z},{RY},{RY},{RZ},{Dimension},{Interior},-1,100.0);");
  341.             g_Formats[E_MENU_FO_XOBJECTS]       = _T("{{Model},{X},{Y},{Z},{RX},{RY},{RZ},100.0},");
  342.             g_Formats[E_MENU_FO_XSTREAMER]      = _T("CreateStreamedObject({Model},{X},{Y},{Z},{RX},{RY},{RZ},100.0);");
  343.             g_Formats[E_MENU_FO_CUSTOM]         = _T("Please enter your object format!");
  344.             g_Formats[E_MENU_FO_ADDSTATICV]     = _T("AddStaticVehicle({Model},{X},{Y},{Z},{RZ},-1,-1);");
  345.             g_Formats[E_MENU_FO_ADDSTATICVEX]   = _T("AddStaticVehicleEx({Model},{X},{Y},{Z},{RZ},-1,-1,60);");
  346.             g_Formats[E_MENU_FO_CREATEVEHICLE]  = _T("CreateVehicle({Model},{X},{Y},{Z},{RZ},-1,-1,60);");
  347.             g_Formats[E_MENU_FO_VCUSTOM]        = _T("Please enter your vehicle format!");
  348.  
  349.             //create the static text control
  350.             CreateWindow(_T("static"),Text, WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 5,CUSTOM_WIDTH-5,100,hWnd,NULL,NULL,NULL);
  351.            
  352.             //Our static text and edit controls for the second window
  353.             CreateWindow(_T("static"),_T("Object Data:"), WS_VISIBLE | WS_CHILD | SS_LEFT,10,50,120,20,hWnd,NULL,NULL,NULL);
  354.             CreateWindow(_T("Edit"),g_Formats[g_Selected[0].id].c_str(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,10,70,310,20,hWnd,(HMENU) E_EVENT_OBJFORMAT,NULL,NULL);
  355.  
  356.             //Second static text and edit control for the second window
  357.             CreateWindow(_T("static"),_T("Vehicle Data:"), WS_VISIBLE | WS_CHILD | SS_LEFT,10,105,120,20,hWnd,NULL,NULL,NULL);
  358.             CreateWindow(_T("Edit"),g_Formats[g_Selected[2].id].c_str(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,10,125,310,20,hWnd,(HMENU) E_EVENT_VEHFORMAT,NULL,NULL);
  359.  
  360.             //The continue button
  361.             CreateWindow(_T("Button"),_T("Continue"), WS_VISIBLE | WS_CHILD | WS_BORDER,10, 170, 310, 35,hWnd, (HMENU) E_EVENT_CONTINUE,NULL,NULL);
  362.             return 0;
  363.         }
  364.         case WM_COMMAND:
  365.         {
  366.             switch(LOWORD(wParam))
  367.             {
  368.                 case E_EVENT_CONTINUE:
  369.                 {
  370.                     //Create our "Convert" object
  371.                     Convert Map;
  372.  
  373.                     //Create 2 wide strings, and 3 ints
  374.                     wstring Str[2];
  375.                     int len[3];        
  376.                
  377.                     //Store the length of our 3 windows
  378.                     //(E_EVENT_TEXTBOX,E_EVENT_OBJFORMAT, and E_EVENT_VEHFORMAT)
  379.                    
  380.                     len[0]  = GetWindowTextLength(GetDlgItem(Main, E_EVENT_TEXTBOX));
  381.                     len[1]  = GetWindowTextLength(GetDlgItem(Other, E_EVENT_OBJFORMAT));
  382.                     len[2]  = GetWindowTextLength(GetDlgItem(Other, E_EVENT_VEHFORMAT));
  383.  
  384.                     //Resize our string object to a size thats appropriate for storing
  385.                     //Our three window messages.
  386.                     g_TextMessage.clear();
  387.                     g_TextMessage.resize(len[0], NULL);
  388.  
  389.                     Str[0].resize(len[1], NULL);
  390.                     Str[1].resize(len[2], NULL);
  391.  
  392.                     /*
  393.                         Here we get our three window messages. Since the third parameter
  394.                         wants a LPWSTR (long pointer to a wide string) and .c_str() returns
  395.                         a LPCWSTR (long pointer to a constant wide string) we have to type
  396.                         cast.
  397.                     */
  398.                     GetDlgItemText(Main,E_EVENT_TEXTBOX, (LPWSTR) g_TextMessage.c_str(), len[0]+1);
  399.                     GetDlgItemText(Other,E_EVENT_OBJFORMAT, (LPWSTR) Str[0].c_str(), len[1]+1);
  400.                     GetDlgItemText(Other,E_EVENT_VEHFORMAT, (LPWSTR) Str[1].c_str(), len[2]+1);
  401.  
  402.                     /*
  403.                         We use our "Map" object's member function ParseInput to convert our map
  404.                         that is now inside g_TextMessage. We pass the map, and the object and
  405.                         vehicle formats, and finally a bool to check if its a Race map or a 1.0 map
  406.                     */
  407.                     Map.ParseInput(WstrToStr(g_TextMessage), WstrToStr(Str[0]), WstrToStr(Str[1]), (g_Selected[1].id == E_MENU_FI_MTARACE)?(true):(false));
  408.                    
  409.                     //After the map is done we hide our second window
  410.                     ShowWindow(Other, SW_HIDE);
  411.                     return 0;
  412.                 }
  413.             }
  414.             return 0;
  415.         }
  416.         case WM_CLOSE:
  417.         {
  418.             //Hide our second window if someone closes it
  419.             ShowWindow(Other,SW_HIDE);
  420.             return 0;
  421.         }
  422.     }
  423.     return DefWindowProc(hWnd,Msg,wParam,lParam);
  424. }
  425.  
  426. //callback for our first window
  427. LRESULT CALLBACK MainProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  428. {
  429.     switch(Msg)
  430.     {
  431.         case WM_CREATE:
  432.         {
  433.  
  434.             //Creating the menus
  435.             HMENU hMenuBar      = CreateMenu();
  436.             HMENU hFile         = CreateMenu();
  437.             HMENU hOptions      = CreateMenu();
  438.             HMENU hVehOrObj     = CreateMenu();
  439.             HMENU hVehFormat    = CreateMenu();
  440.             HMENU hObjFormat    = CreateMenu();
  441.             HMENU hInputFormat  = CreateMenu();
  442.  
  443.             //Appending itemps to the hFile menu
  444.             AppendMenu(hFile, MF_STRING, E_MENU_OPEN, _T("Open Map"));
  445.             AppendMenu(hFile, MF_STRING, E_MENU_SAVE, _T("Save Map"));
  446.             AppendMenu(hFile, MF_SEPARATOR, NULL, NULL);
  447.             AppendMenu(hFile, MF_STRING, E_MENU_QUIT, _T("Quit"));
  448.  
  449.             //Append items to hInputFormat
  450.             AppendMenu(hInputFormat, MF_STRING, E_MENU_FI_MTA1, _T("MTA 1.0"));
  451.             AppendMenu(hInputFormat, MF_STRING, E_MENU_FI_MTARACE, _T("MTA Race"));
  452.            
  453.             //Appending items to hObjFormat
  454.             AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_CREATEOBJECT, _T("CreateObject"));
  455.             AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_INCOGNITO, _T("Streamer plugin (incognito)"));
  456.             AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_XSTREAMER, _T("xStreamer plugin"));
  457.             AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_XOBJECTS, _T("xObjects"));
  458.             AppendMenu(hObjFormat, MF_SEPARATOR, NULL, NULL);
  459.             AppendMenu(hObjFormat, MF_STRING, E_MENU_FO_CUSTOM, _T("Custom format"));
  460.  
  461.             //Appending itemps to hVehFormat
  462.             AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_NONE, _T("No vehicles"));
  463.             AppendMenu(hVehFormat, MF_SEPARATOR, NULL, NULL);
  464.             AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_ADDSTATICV, _T("AddStaticVehicle"));
  465.             AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_ADDSTATICVEX, _T("AddStaticVehicleEx"));
  466.             AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_CREATEVEHICLE, _T("CreateVehicle"));
  467.             AppendMenu(hVehFormat, MF_SEPARATOR, NULL, NULL);
  468.             AppendMenu(hVehFormat, MF_STRING, E_MENU_FO_VCUSTOM, _T("Custom format"));
  469.  
  470.             //Appending items to menu hVehOrObj
  471.             AppendMenu(hVehOrObj, MF_POPUP, (UINT_PTR) hObjFormat, _T("Objects"));
  472.             AppendMenu(hVehOrObj, MF_POPUP, (UINT_PTR) hVehFormat, _T("Vehicles"));
  473.  
  474.             //Append the hInputFormat and hVehOrObj menus to hOptions
  475.             AppendMenu(hOptions, MF_POPUP, (UINT_PTR) hInputFormat, _T("Input"));
  476.             AppendMenu(hOptions, MF_POPUP, (UINT_PTR) hVehOrObj, _T("Output"));
  477.  
  478.             //Append hFile and hOptions to the hMenuBar
  479.             AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hFile, _T("File"));
  480.             AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR) hOptions, _T("Options"));
  481.  
  482.             //Apply the menu to our winodw
  483.             SetMenu(hWnd,hMenuBar);
  484.  
  485.             //Assign our starting menu selection data to our tracking arrays
  486.             g_Selected[0].menu      = hObjFormat;
  487.             g_Selected[0].id        = E_MENU_FO_CREATEOBJECT;
  488.  
  489.             g_Selected[1].menu      = hInputFormat;
  490.             g_Selected[1].id        = E_MENU_FI_MTA1;
  491.  
  492.             g_Selected[2].menu      = hVehFormat;
  493.             g_Selected[2].id        = E_MENU_FO_ADDSTATICV;
  494.  
  495.             //Actually "select" our menu items
  496.             CheckMenuItem(hObjFormat,E_MENU_FO_CREATEOBJECT, MF_CHECKED);
  497.             CheckMenuItem(hInputFormat,E_MENU_FI_MTA1, MF_CHECKED);
  498.             CheckMenuItem(hVehFormat,E_MENU_FO_ADDSTATICV, MF_CHECKED);
  499.  
  500.             //Create our textbox and button
  501.             CreateWindow(_T("Edit"),g_TextMessage.c_str(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_MULTILINE|ES_WANTRETURN|WS_VSCROLL,0,0,WINDOW_WIDTH-5,200,hWnd,(HMENU) E_EVENT_TEXTBOX,NULL,NULL);
  502.             CreateWindow(_T("Button"),_T("Convert!"), WS_CHILD | WS_VISIBLE | WS_BORDER,0,200,WINDOW_WIDTH-4,53,hWnd,(HMENU) E_EVENT_CONVERT,NULL,NULL);
  503.             return 1;
  504.         }
  505.  
  506.         case WM_COMMAND:
  507.         {
  508.             int val     = LOWORD(wParam);
  509.  
  510.             switch(val)
  511.             {
  512.  
  513.                 case E_MENU_FO_CREATEOBJECT:
  514.                 case E_MENU_FO_INCOGNITO:
  515.                 case E_MENU_FO_XSTREAMER:
  516.                 case E_MENU_FO_XOBJECTS:
  517.  
  518.                 case E_MENU_FO_CUSTOM:
  519.                 {
  520.                     CheckMenuItem(g_Selected[0].menu, g_Selected[0].id, MF_UNCHECKED);
  521.                     CheckMenuItem(g_Selected[0].menu, val, MF_CHECKED);
  522.  
  523.                     SetDlgItemText(Other,E_EVENT_OBJFORMAT,g_Formats[val].c_str());
  524.  
  525.                     g_Selected[0].id    = val;
  526.                     return 0;
  527.                 }
  528.  
  529.                 case E_MENU_FO_NONE:
  530.                 case E_MENU_FO_ADDSTATICV:
  531.                 case E_MENU_FO_ADDSTATICVEX:
  532.                 case E_MENU_FO_VCUSTOM:
  533.  
  534.                 case E_MENU_FO_CREATEVEHICLE:
  535.                 {
  536.                     CheckMenuItem(g_Selected[2].menu, g_Selected[2].id, MF_UNCHECKED);
  537.                     CheckMenuItem(g_Selected[2].menu, val, MF_CHECKED);
  538.  
  539.                     SetDlgItemText(Other,E_EVENT_VEHFORMAT,g_Formats[val].c_str());
  540.  
  541.                     g_Selected[2].id    = val;
  542.                     return 0;
  543.                 }
  544.  
  545.                 case E_MENU_FI_MTARACE:
  546.                 case E_MENU_FI_MTA1:
  547.                 {
  548.                     CheckMenuItem(g_Selected[1].menu, g_Selected[1].id, MF_UNCHECKED);
  549.                     CheckMenuItem(g_Selected[1].menu, val, MF_CHECKED);
  550.                    
  551.                     g_Selected[1].id    = val;
  552.                     return 0;
  553.                 }
  554.  
  555.                 case E_MENU_QUIT:
  556.                 {
  557.                     PostQuitMessage(0);
  558.                     return 0;
  559.                 }
  560.  
  561.                 case E_EVENT_TEXTBOX:
  562.                 {
  563.                     if(HIWORD(wParam) == EN_CHANGE)
  564.                     {
  565.                         //change save variable and give a quit warning
  566.                     }
  567.                     return 0;
  568.                 }
  569.  
  570.                 case E_EVENT_CONVERT:
  571.                 {
  572.                     //Show second window, hide main window
  573.                     ShowWindow(Other,SW_SHOW);
  574.                     return 0;
  575.                 }
  576.                
  577.                 case E_MENU_SAVE:
  578.                 {
  579.                     OpenFileDialog(Main, E_EVENT_TEXTBOX, _T("All files (*.*)\0*.*\0\0"),true,false);
  580.                     return 0;
  581.                 }
  582.  
  583.                 case E_MENU_OPEN:
  584.                 {
  585.                     OpenFileDialog(Main, E_EVENT_TEXTBOX, _T("MTA Map files (*.map)\0*.map\0\0"));
  586.                     return 0;
  587.                 }
  588.             }
  589.             return 0;
  590.         }
  591.  
  592.         case WM_DESTROY:
  593.         {
  594.             PostQuitMessage(0);
  595.             return 0;
  596.         }
  597.     }
  598.  
  599.     return DefWindowProc(hWnd,Msg,wParam,lParam);
  600. }
  601.  
  602.  
  603. int OpenFileDialog(HWND hWnd, int item, const TCHAR filter[], bool save, bool append)
  604. {
  605.     //Declare our OPENFILENAME object and fill it out
  606.     OPENFILENAME OFN            = {0};
  607.     TCHAR szFileName[MAX_PATH]  = {0};
  608.  
  609.     OFN.lStructSize             = sizeof(OPENFILENAME);
  610.     OFN.Flags                   = OFN_EXPLORER;
  611.     OFN.lpstrFilter             = filter;
  612.     OFN.lpstrFile               = szFileName;
  613.     OFN.nMaxFile                = MAX_PATH;
  614.     OFN.hwndOwner               = hWnd;
  615.  
  616.     wifstream iFile;
  617.     wofstream oFile;
  618.  
  619.     if(save)
  620.     {
  621.         //If save is true, open the save dialog (passing our OFN object's address)
  622.         if(GetSaveFileName(&OFN))
  623.         {
  624.             //Open the file that the user specified in the save dialog
  625.             iFile.open(szFileName);
  626.  
  627.             //Get the size of the window
  628.             int len     = GetWindowTextLength(GetDlgItem(hWnd,item));
  629.  
  630.             wstring Text;
  631.             Text.resize(len+2);
  632.  
  633.             //Get the specified item's text.
  634.             GetDlgItemText(hWnd, item, (LPWSTR) Text.c_str(),len+1);
  635.  
  636.             //Check if its open (to see if it exists or not)
  637.             if(iFile.is_open())
  638.             {
  639.                 //close the file, cause we dont need it
  640.                 iFile.close();
  641.  
  642.                 //if user doesnt want append to the existing file
  643.                 if(!append)
  644.                 {
  645.                     //Ask if he wants to overwrite the existing file
  646.                     int YON     = MessageBox(hWnd,_T("This file already exists! Do you want to overwrite it?"),_T("Warning!"),MB_YESNO);
  647.                
  648.                     switch(YON)
  649.                     {
  650.                         //If he doesnt, return false and get out.
  651.                         case IDNO:
  652.                         {
  653.                             return 0;
  654.                         }
  655.  
  656.                         //If he does
  657.                         case IDYES:
  658.                         {
  659.                             //Open our file for writing, write the text, and close it.
  660.                             oFile.open(szFileName);
  661.                             oFile << Text;
  662.                             oFile.close();
  663.                             return 1;
  664.                         }
  665.                     }
  666.                 }
  667.                 else
  668.                 {
  669.                     //We open a file for appending, append text to it, and close it
  670.                     oFile.open(szFileName, ios::app);
  671.                     oFile << Text;
  672.                     oFile.close();
  673.                     return 1;
  674.                 }
  675.  
  676.             }
  677.             else
  678.             {
  679.                 //close iFile, we dont need it anymore
  680.                 iFile.close();
  681.  
  682.                 //Open our file, write to it, and close it
  683.                 oFile.open(szFileName, ios::out);
  684.                 oFile << Text;
  685.                 oFile.close();
  686.                 return 1;
  687.             }
  688.         }
  689.     }
  690.     else
  691.     {
  692.         OFN.Flags = OFN_PATHMUSTEXIST | OFN_READONLY;
  693.  
  694.         if(GetOpenFileName(&OFN))
  695.         {
  696.             iFile.open(szFileName);
  697.  
  698.             wstring
  699.                 tmp,
  700.                 Text;
  701.  
  702.  
  703.             while(!iFile.eof())
  704.             {
  705.                 getline(iFile, tmp);
  706.                 Text += tmp;
  707.             }
  708.  
  709.             SetDlgItemText(hWnd,item,Text.c_str());
  710.             return 1;
  711.         }
  712.     }
  713.     return 0;
  714. }
  715.  
  716. //Convert a wide string (wstring) to string
  717. string WstrToStr(wstring str)
  718. {
  719.     string tmp;
  720.  
  721.     tmp.resize(str.size(),NULL);
  722.     tmp.assign(str.begin(),str.end());
  723.    
  724.     return tmp;
  725. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement