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