win_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "texpaint.h"
  19. HINSTANCE main_instance;
  20. int screen_width, screen_height;
  21. HWND mainwindow;
  22. HWND camerawindow;
  23. HWND palettewindow;
  24. HWND skinwindow;
  25. /*
  26. =================
  27. Sys_Error
  28. For abnormal program terminations
  29. =================
  30. */
  31. void Sys_Error (char *error, ...)
  32. {
  33. va_list argptr;
  34. char text[1024];
  35. char text2[1024];
  36. int err;
  37. err = GetLastError ();
  38. va_start (argptr,error);
  39. vsprintf (text, error,argptr);
  40. va_end (argptr);
  41. sprintf (text2, "%s\nGetLastError() = %i", text, err);
  42. MessageBox(mainwindow, text2, "Error", 0 /* MB_OK */ );
  43. exit (1);
  44. }
  45. /*
  46. ======================================================================
  47. FILE DIALOGS
  48. ======================================================================
  49. */
  50. qboolean modified;
  51. qboolean modified_past_autosave;
  52. qboolean ConfirmModified (void)
  53. {
  54. if (!modified)
  55. return true;
  56. if (MessageBox (mainwindow, "This will lose changes to the skin"
  57. , "warning", MB_OKCANCEL) == IDCANCEL)
  58. return false;
  59. return true;
  60. }
  61. OPENFILENAME ofn; /* common dialog box structure */
  62. char szDirName[MAX_PATH]; /* directory string */
  63. char szFile[260]; /* filename string */
  64. char szFileTitle[260]; /* file title string */
  65. char szSkinFilter[260] = /* filter string */
  66. "Skin texture (*.lbm *.pcx)\0*.lbm;*.pcx\0\0";
  67. char szFrameFilter[260] = /* filter string */
  68. "Model frame (*.tri)\0*.tri\0\0";
  69. char chReplace; /* string separator for szFilter */
  70. int i, cbString; /* integer count variables */
  71. HANDLE hf; /* file handle */
  72. void OpenSkinDialog (void)
  73. {
  74. // strcpy (szDirName, ValueForKey (project_entity, "basepath") );
  75. // strcat (szDirName, "\\maps");
  76. /* Place the terminating null character in the szFile. */
  77. szFile[0] = '\0';
  78. /* Set the members of the OPENFILENAME structure. */
  79. ofn.lStructSize = sizeof(OPENFILENAME);
  80. ofn.hwndOwner = mainwindow;
  81. ofn.lpstrFilter = szSkinFilter;
  82. ofn.nFilterIndex = 1;
  83. ofn.lpstrFile = szFile;
  84. ofn.nMaxFile = sizeof(szFile);
  85. ofn.lpstrFileTitle = szFileTitle;
  86. ofn.nMaxFileTitle = sizeof(szFileTitle);
  87. ofn.lpstrInitialDir = szDirName;
  88. ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  89. OFN_FILEMUSTEXIST;
  90. /* Display the Open dialog box. */
  91. if (!GetOpenFileName(&ofn))
  92. return; // canceled
  93. Skin_LoadFile (ofn.lpstrFile);
  94. }
  95. void OpenFrameDialog (void)
  96. {
  97. // strcpy (szDirName, ValueForKey (project_entity, "basepath") );
  98. // strcat (szDirName, "\\maps");
  99. /* Place the terminating null character in the szFile. */
  100. szFile[0] = '\0';
  101. /* Set the members of the OPENFILENAME structure. */
  102. ofn.lStructSize = sizeof(OPENFILENAME);
  103. ofn.hwndOwner = mainwindow;
  104. ofn.lpstrFilter = szFrameFilter;
  105. ofn.nFilterIndex = 1;
  106. ofn.lpstrFile = szFile;
  107. ofn.nMaxFile = sizeof(szFile);
  108. ofn.lpstrFileTitle = szFileTitle;
  109. ofn.nMaxFileTitle = sizeof(szFileTitle);
  110. ofn.lpstrInitialDir = szDirName;
  111. ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  112. OFN_FILEMUSTEXIST;
  113. /* Display the Open dialog box. */
  114. if (!GetOpenFileName(&ofn))
  115. return; // canceled
  116. LoadTriFile (ofn.lpstrFile);
  117. }
  118. void SaveSkinDialog (void)
  119. {
  120. // strcpy (szDirName, ValueForKey (project_entity, "basepath") );
  121. // strcat (szDirName, "\\maps");
  122. /* Place the terminating null character in the szFile. */
  123. szFile[0] = '\0';
  124. /* Set the members of the OPENFILENAME structure. */
  125. ofn.lStructSize = sizeof(OPENFILENAME);
  126. ofn.hwndOwner = mainwindow;
  127. ofn.lpstrFilter = szSkinFilter;
  128. ofn.nFilterIndex = 1;
  129. ofn.lpstrFile = szFile;
  130. ofn.nMaxFile = sizeof(szFile);
  131. ofn.lpstrFileTitle = szFileTitle;
  132. ofn.nMaxFileTitle = sizeof(szFileTitle);
  133. ofn.lpstrInitialDir = szDirName;
  134. ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  135. OFN_FILEMUSTEXIST;
  136. /* Display the Open dialog box. */
  137. if (!GetSaveFileName(&ofn))
  138. return; // canceled
  139. DefaultExtension (ofn.lpstrFile, ".lbm");
  140. Skin_SaveFile (ofn.lpstrFile);
  141. strcpy (skin_filename, ofn.lpstrFile);
  142. }
  143. //==========================================================================
  144. BOOL bSetupPixelFormat(HDC hDC)
  145. {
  146. static PIXELFORMATDESCRIPTOR pfd = {
  147. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  148. 1, // version number
  149. PFD_DRAW_TO_WINDOW | // support window
  150. PFD_SUPPORT_OPENGL | // support OpenGL
  151. PFD_DOUBLEBUFFER, // double buffered
  152. PFD_TYPE_RGBA, // RGBA type
  153. 24, // 24-bit color depth
  154. 0, 0, 0, 0, 0, 0, // color bits ignored
  155. 0, // no alpha buffer
  156. 0, // shift bit ignored
  157. 0, // no accumulation buffer
  158. 0, 0, 0, 0, // accum bits ignored
  159. 32, // 32-bit z-buffer
  160. 0, // no stencil buffer
  161. 0, // no auxiliary buffer
  162. PFD_MAIN_PLANE, // main layer
  163. 0, // reserved
  164. 0, 0, 0 // layer masks ignored
  165. };
  166. int pixelformat = 0;
  167. PIXELFORMATDESCRIPTOR newp;
  168. if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
  169. {
  170. printf("%d",GetLastError());
  171. Error ("ChoosePixelFormat failed");
  172. }
  173. if (!SetPixelFormat(hDC, pixelformat, &pfd))
  174. Error ("SetPixelFormat failed");
  175. return TRUE;
  176. }
  177. /*
  178. ==============================================================================
  179. MENU
  180. ==============================================================================
  181. */
  182. /* handle all WM_COMMAND messages here */
  183. LONG WINAPI CommandHandler (
  184. HWND hWnd,
  185. WPARAM wParam,
  186. LPARAM lParam)
  187. {
  188. unsigned short cmd;
  189. cmd = LOWORD(wParam);
  190. switch (cmd)
  191. {
  192. //
  193. // file menu
  194. //
  195. case ID_FILE_RESAMPLESKIN:
  196. ResampleSkin ();
  197. break;
  198. case ID_FILE_NEWSKIN:
  199. NewSkin ();
  200. break;
  201. case ID_FILE_OPENFRAME:
  202. OpenFrameDialog ();
  203. break;
  204. case ID_FILE_OPENSKIN:
  205. if (!ConfirmModified())
  206. break;
  207. OpenSkinDialog ();
  208. break;
  209. case ID_FILE_RELOADSKIN:
  210. if (!ConfirmModified())
  211. break;
  212. Skin_LoadFile (skin_filename);
  213. break;
  214. case ID_FILE_SAVESKIN:
  215. Skin_SaveFile (skin_filename);
  216. break;
  217. case ID_FILE_SAVESKINAS:
  218. SaveSkinDialog ();
  219. break;
  220. case ID_FILE_EXIT:
  221. if (!ConfirmModified())
  222. break;
  223. PostQuitMessage (0);
  224. break;
  225. //
  226. // edit menu
  227. //
  228. case ID_EDIT_UNDO:
  229. Undo();
  230. break;
  231. case ID_EDIT_REDO:
  232. Redo();
  233. break;
  234. //
  235. // view menu
  236. //
  237. case ID_VIEW_MODELLINES:
  238. model_lines ^= 1;
  239. CheckMenuItem ( GetSubMenu (GetMenu(mainwindow), MENU_VIEW)
  240. , ID_VIEW_MODELLINES
  241. , MF_BYCOMMAND | (model_lines ? MF_CHECKED : MF_UNCHECKED) );
  242. InvalidateRect (camerawindow, NULL, false);
  243. break;
  244. case ID_VIEW_TEXTURELINES:
  245. skin_lines ^= 1;
  246. CheckMenuItem ( GetSubMenu (GetMenu(mainwindow), MENU_VIEW)
  247. , ID_VIEW_TEXTURELINES
  248. , MF_BYCOMMAND | (skin_lines ? MF_CHECKED : MF_UNCHECKED) );
  249. InvalidateRect (skinwindow, NULL, false);
  250. break;
  251. default:
  252. return FALSE;
  253. }
  254. return TRUE;
  255. }
  256. /*
  257. ============
  258. WMAIN_WndProc
  259. ============
  260. */
  261. LONG WINAPI WMAIN_WndProc (
  262. HWND hWnd,
  263. UINT uMsg,
  264. WPARAM wParam,
  265. LPARAM lParam)
  266. {
  267. LONG lRet = 1;
  268. RECT rect;
  269. HDC maindc;
  270. GetClientRect(hWnd, &rect);
  271. switch (uMsg)
  272. {
  273. case WM_CREATE:
  274. maindc = GetDC(hWnd);
  275. bSetupPixelFormat(maindc);
  276. break;
  277. case WM_COMMAND:
  278. lRet = CommandHandler (hWnd, wParam, lParam);
  279. break;
  280. case WM_CLOSE:
  281. if (!ConfirmModified())
  282. break;
  283. PostQuitMessage (0);
  284. break;
  285. default:
  286. /* pass all unhandled messages to DefWindowProc */
  287. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  288. break;
  289. }
  290. /* return 1 if handled message, 0 if not */
  291. return lRet;
  292. }
  293. /*
  294. ==============
  295. Main_Create
  296. ==============
  297. */
  298. void Main_Create (HINSTANCE hInstance)
  299. {
  300. WNDCLASS wc;
  301. /* Register the class */
  302. memset (&wc, 0, sizeof(wc));
  303. wc.style = 0;
  304. wc.lpfnWndProc = (WNDPROC)WMAIN_WndProc;
  305. wc.cbClsExtra = 0;
  306. wc.cbWndExtra = 0;
  307. wc.hInstance = hInstance;
  308. wc.hIcon = 0;
  309. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  310. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  311. wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU2);
  312. wc.lpszClassName = "TEXPAINT_MAIN";
  313. if (!RegisterClass (&wc) )
  314. Error ("WCam_Register: failed");
  315. mainwindow = CreateWindow ("TEXPAINT_MAIN" ,
  316. "Texpaint",
  317. WS_OVERLAPPEDWINDOW |
  318. WS_CLIPSIBLINGS |
  319. WS_CLIPCHILDREN,
  320. 0,0,screen_width,screen_height, // size
  321. 0,
  322. NULL, // no menu
  323. hInstance,
  324. NULL);
  325. if (!mainwindow)
  326. Error ("Couldn't create main window");
  327. // GetWindowInfo("mainwindow", &SavedInfo, NULL);
  328. ShowWindow (mainwindow, SW_SHOWDEFAULT);
  329. }
  330. BOOL SaveWindowInfo(const char *pszName, void *pvBuf, long lSize)
  331. {
  332. LONG lres;
  333. DWORD dwDisp;
  334. HKEY hKeyId;
  335. lres = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\id\\Texpaint", 0, NULL,
  336. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyId, &dwDisp);
  337. if (lres != ERROR_SUCCESS)
  338. return FALSE;
  339. lres = RegSetValueEx(hKeyId, pszName, 0, REG_BINARY, pvBuf, lSize);
  340. RegCloseKey(hKeyId);
  341. if (lres != ERROR_SUCCESS)
  342. return FALSE;
  343. return TRUE;
  344. }
  345. BOOL GetWindowInfo(const char *pszName, void *pvBuf, long *plSize)
  346. {
  347. HKEY hKey;
  348. long lres, lType, lSize;
  349. if (plSize == NULL)
  350. plSize = &lSize;
  351. lres = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\id\\Texpaint", 0, KEY_READ, &hKey);
  352. if (lres != ERROR_SUCCESS)
  353. return FALSE;
  354. lres = RegQueryValueEx(hKey, pszName, NULL, &lType, pvBuf, plSize);
  355. RegCloseKey(hKey);
  356. if (lres != ERROR_SUCCESS)
  357. return FALSE;
  358. return TRUE;
  359. }
  360. BOOL SaveWindowState(HWND hWnd, const char *pszName)
  361. {
  362. RECT rc;
  363. GetWindowRect(hWnd, &rc);
  364. MapWindowPoints(NULL, mainwindow, (POINT *)&rc, 2);
  365. return SaveWindowInfo(pszName, &rc, sizeof(rc));
  366. }
  367. BOOL RestoreWindowState(HWND hWnd, const char *pszName)
  368. {
  369. RECT rc;
  370. LONG lSize = sizeof(rc);
  371. if (GetWindowInfo(pszName, &rc, &lSize))
  372. {
  373. if (rc.left < 0)
  374. rc.left = 0;
  375. if (rc.top < 0)
  376. rc.top = 0;
  377. if (rc.right < rc.left + 16)
  378. rc.right = rc.left + 16;
  379. if (rc.bottom < rc.top + 16)
  380. rc.bottom = rc.top + 16;
  381. MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left,
  382. rc.bottom - rc.top, FALSE);
  383. return TRUE;
  384. }
  385. return FALSE;
  386. }