doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AlphaPopup.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #include "../../sys/win32/win_local.h"
33 #include "ColorButton.h"
34 #include "MaskEdit.h"
35 #include "../../sys/win32/rc/guied_resource.h"
36 
37 static HHOOK gAlphaHook = NULL;
38 static HWND gAlphaDlg = NULL;
39 
40 /*
41 ================
42 AlphaSlider_DrawArrow
43 
44 Draws the arrow under alpha slider
45 ================
46 */
47 static void AlphaSlider_DrawArrow ( HDC hDC, RECT* pRect, COLORREF color )
48 {
49  POINT ptsArrow[3];
50 
51  ptsArrow[0].x = pRect->left;
52  ptsArrow[0].y = pRect->bottom;
53  ptsArrow[1].x = (pRect->left + pRect->right)/2;
54  ptsArrow[1].y = pRect->top;
55  ptsArrow[2].x = pRect->right;
56  ptsArrow[2].y = pRect->bottom;
57 
58  HBRUSH arrowBrush = CreateSolidBrush ( color );
59  HPEN arrowPen = CreatePen ( PS_SOLID, 1, color );
60 
61  HGDIOBJ oldBrush = SelectObject ( hDC, arrowBrush );
62  HGDIOBJ oldPen = SelectObject ( hDC, arrowPen );
63 
64  SetPolyFillMode(hDC, WINDING);
65  Polygon(hDC, ptsArrow, 3);
66 
67  SelectObject ( hDC, oldBrush );
68  SelectObject ( hDC, oldPen );
69 
70  DeleteObject ( arrowBrush );
71  DeleteObject ( arrowPen );
72 }
73 
74 /*
75 ================
76 AlphaSlider_WndProc
77 
78 Window procedure for the alpha slider control
79 ================
80 */
81 LRESULT CALLBACK AlphaSlider_WndProc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
82 {
83  switch ( msg )
84  {
85  case WM_LBUTTONDOWN:
86  {
87  RECT rClient;
88  float v;
89 
90  GetClientRect ( hwnd, &rClient );
91  v = (float)((short)LOWORD(lParam)-5) / (float)(rClient.right - rClient.left - 10);
92  if ( v < 0 ) v = 0;
93  if ( v > 1.0f ) v = 1.0f;
94  SetWindowLong ( hwnd, GWL_USERDATA, MAKELONG(0x8000,(unsigned short)(255.0f * v)) );
95  InvalidateRect ( hwnd, NULL, FALSE );
96 
97  SetCapture ( hwnd );
98 
99  break;
100  }
101 
102  case WM_MOUSEMOVE:
103  if ( LOWORD(GetWindowLong ( hwnd, GWL_USERDATA ) ) & 0x8000 )
104  {
105  RECT rClient;
106  float v;
107 
108  GetClientRect ( hwnd, &rClient );
109  v = (float)((short)LOWORD(lParam)-5) / (float)(rClient.right - rClient.left - 10);
110  if ( v < 0 ) v = 0;
111  if ( v > 1.0f ) v = 1.0f;
112  SetWindowLong ( hwnd, GWL_USERDATA, MAKELONG(0x8000,(unsigned short)(255.0f * v)) );
113  InvalidateRect ( hwnd, NULL, FALSE );
114  }
115  break;
116 
117  case WM_LBUTTONUP:
118  if ( LOWORD(GetWindowLong ( hwnd, GWL_USERDATA ) ) & 0x8000 )
119  {
120  RECT rClient;
121  float v;
122 
123  GetClientRect ( hwnd, &rClient );
124  v = (float)((short)LOWORD(lParam)-5) / (float)(rClient.right - rClient.left - 10);
125  if ( v < 0 ) v = 0;
126  if ( v > 1.0f ) v = 1.0f;
127  SetWindowLong ( hwnd, GWL_USERDATA, MAKELONG(0x8000,(unsigned short)(255.0f * v)) );
128  InvalidateRect ( hwnd, NULL, FALSE );
129  ReleaseCapture ( );
130  SendMessage ( GetParent ( hwnd ), WM_COMMAND, MAKELONG(GetWindowLong (hwnd,GWL_ID),0), 0 );
131  }
132  break;
133 
134  case WM_PAINT:
135  {
136  PAINTSTRUCT ps;
137  HDC hDC = BeginPaint ( hwnd, &ps );
138 
139  RECT rDraw;
140  RECT rClient;
141  GetClientRect ( hwnd, &rClient );
142 
143  // Setup the gradient rect
144  CopyRect ( &rDraw, &rClient );
145  rDraw.left += 5;
146  rDraw.right -= 5;
147  rDraw.bottom -= 6;
148 
149  // Draw the gradient
150  int parts = 20;
151  RECT rColor;
152  float step = (float)(rDraw.right-rDraw.left) / (float)parts;
153  CopyRect ( &rColor, &rDraw );
154  for ( int i = 0; i < parts; i ++ )
155  {
156  float color = ((float)i / (float)parts) * 255.0f;
157 
158  rColor.left = rDraw.left + i * step;
159  rColor.right = rColor.left + step + 1;
160 
161  HBRUSH brush = CreateSolidBrush ( RGB((int)color,(int)color,(int)color) );
162  FillRect ( hDC, &rColor, brush );
163  DeleteObject ( brush );
164  }
165 
166  // Draw a frame around the gradient
167  FrameRect (hDC, &rDraw, (HBRUSH)GetStockObject ( BLACK_BRUSH ) );
168 
169  // Make sure the area below the graident is filled in
170  rClient.top = rDraw.bottom;
171  FillRect ( hDC, &rClient, GetSysColorBrush ( COLOR_3DFACE ) );
172 
173  // Draw the thumb
174  RECT rThumb;
175  short s = HIWORD(GetWindowLong ( hwnd, GWL_USERDATA ));
176  float thumb = (float)(short)s;
177  thumb /= 255.0f;
178  thumb *= (float)(rDraw.right-rDraw.left);
179  rThumb.left = rDraw.left - 5 + thumb;
180  rThumb.right = rThumb.left + 10;
181  rThumb.top = rDraw.bottom + 1;
182  rThumb.bottom = rThumb.top + 5;
183  AlphaSlider_DrawArrow ( hDC, &rThumb, RGB(0,0,0) );
184 
185  EndPaint ( hwnd, &ps );
186  return 0;
187  }
188  }
189 
190  return DefWindowProc ( hwnd, msg, wParam, lParam );
191 }
192 
193 /*
194 ================
195 AlphaSelectDlg_GetMsgProc
196 
197 Ensures normal dialog functions work in the alpha select dialog
198 ================
199 */
200 LRESULT FAR PASCAL AlphaSelectDlg_GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
201 {
202  LPMSG lpMsg = (LPMSG) lParam;
203 
204  if ( nCode >= 0 && PM_REMOVE == wParam )
205  {
206  // Don't translate non-input events.
207  if ( (lpMsg->message >= WM_KEYFIRST && lpMsg->message <= WM_KEYLAST) )
208  {
209  if ( IsDialogMessage( gAlphaDlg, lpMsg) )
210  {
211  // The value returned from this hookproc is ignored,
212  // and it cannot be used to tell Windows the message has been handled.
213  // To avoid further processing, convert the message to WM_NULL
214  // before returning.
215  lpMsg->message = WM_NULL;
216  lpMsg->lParam = 0;
217  lpMsg->wParam = 0;
218  }
219  }
220  }
221 
222  return CallNextHookEx(gAlphaHook, nCode, wParam, lParam);
223 }
224 
225 /*
226 ================
227 AlphaSelectDlg_WndProc
228 
229 Window procedure for the alpha select dialog
230 ================
231 */
232 INT_PTR CALLBACK AlphaSelectDlg_WndProc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
233 {
234  switch ( msg )
235  {
236  case WM_INITDIALOG:
237  {
238  int color;
239 
240  gAlphaDlg = hwnd;
241  gAlphaHook = SetWindowsHookEx( WH_GETMESSAGE, AlphaSelectDlg_GetMsgProc, NULL, GetCurrentThreadId() );
242  color = GetRValue(ColorButton_GetColor ((HWND)lParam));
243 
244  // The lParam for the alpha select dialog is the window handle of the button pressed
245  SetWindowLong ( hwnd, GWL_USERDATA, lParam );
246 
247  // Subclass the alpha
248  SetWindowLong ( GetDlgItem ( hwnd, IDC_GUIED_ALPHASLIDER ), GWL_USERDATA, MAKELONG(0,color) );
249 
250  // Numbers only on the edit box and start it with the current alpha value.
251  NumberEdit_Attach ( GetDlgItem ( hwnd, IDC_GUIED_ALPHA ) );
252  SetWindowText ( GetDlgItem ( hwnd, IDC_GUIED_ALPHA ), va("%.3f", ((float)color / 255.0f) ) );
253  break;
254  }
255 
256  case WM_DESTROY:
257  UnhookWindowsHookEx( gAlphaHook );
258  ReleaseCapture ( );
259  gAlphaDlg = NULL;
260  break;
261 
262  case WM_ACTIVATE:
263  if ( !LOWORD(wParam) )
264  {
265  EndDialog ( hwnd, 0 );
266  }
267  break;
268 
269  case WM_COMMAND:
270  switch ( LOWORD(wParam) )
271  {
272  case IDC_GUIED_ALPHA:
273  {
274  char temp[64];
275  float value;
276 
277  // Get the current text in the window and convert it to a float
278  GetDlgItemText ( hwnd, IDC_GUIED_ALPHA, temp, 64 );
279  value = atof ( temp );
280 
281  if ( value < 0.0f )
282  {
283  value = 0.0f;
284  }
285  else if ( value > 1.0f )
286  {
287  value = 1.0f;
288  }
289 
290  // Set the current alpha value in the slider
291  SetWindowLong ( GetDlgItem ( hwnd, IDC_GUIED_ALPHASLIDER ), GWL_USERDATA, MAKELONG(0,(255.0f * value)) );
292  break;
293  }
294 
296  case IDOK:
297  {
298  int color = (short)HIWORD(GetWindowLong ( GetDlgItem ( hwnd, IDC_GUIED_ALPHASLIDER ), GWL_USERDATA ));
299  ColorButton_SetColor ( (HWND)GetWindowLong ( hwnd, GWL_USERDATA ), RGB(color,color,color) );
300  EndDialog ( hwnd, 0 );
301  break;
302  }
303 
304  case IDCANCEL:
305  EndDialog ( hwnd, 0 );
306  break;
307  }
308  break;
309  }
310 
311  return FALSE;
312 }
313 
314 /*
315 ================
316 AlphaButton_OpenPopup
317 
318 Opens the popup window under the alpha button
319 ================
320 */
321 void AlphaButton_OpenPopup ( HWND button )
322 {
323  RECT rWindow;
324  WNDCLASSEX wndClass;
325  HWND dlg;
326 
327  // Make sure the alpha slider window class is registered
328  memset ( &wndClass, 0, sizeof(wndClass) );
329  wndClass.cbSize = sizeof(WNDCLASSEX);
330  wndClass.lpszClassName = "GUIED_ALPHASLIDER";
331  wndClass.lpfnWndProc = AlphaSlider_WndProc;
332  wndClass.hInstance = win32.hInstance;
333  RegisterClassEx ( &wndClass );
334 
335  GetWindowRect ( button, &rWindow );
336  dlg = CreateDialogParam ( win32.hInstance, MAKEINTRESOURCE(IDD_GUIED_ALPHA), GetParent(button), AlphaSelectDlg_WndProc, (LPARAM)button );
337 
338  SetWindowPos ( dlg, NULL, rWindow.left, rWindow.bottom + 1, 0, 0, SWP_NOSIZE|SWP_NOZORDER );
339  ShowWindow ( dlg, SW_SHOW );
340  UpdateWindow ( dlg );
341  SetFocus ( dlg );
342 }
byte color[4]
Definition: MegaTexture.cpp:54
#define IDD_GUIED_ALPHA
GLsizei const GLfloat * value
Definition: glext.h:3614
CONST PIXELFORMATDESCRIPTOR UINT
Definition: win_qgl.cpp:47
const GLdouble * v
Definition: glext.h:2936
void AlphaButton_OpenPopup(HWND button)
Definition: AlphaPopup.cpp:321
case const float
Definition: Callbacks.cpp:62
LRESULT FAR PASCAL AlphaSelectDlg_GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
Definition: AlphaPopup.cpp:200
GLdouble s
Definition: glext.h:2935
#define IDC_GUIED_ALPHA
int i
Definition: process.py:33
#define NULL
Definition: Lib.h:88
HDC hDC
Definition: wglext.h:383
#define FAR
Definition: jmorecfg.h:205
#define IDC_GUIED_ALPHASLIDER
typedef HDC(WINAPI *PFNWGLGETCURRENTREADDCARBPROC)(void)
LRESULT CALLBACK AlphaSlider_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: AlphaPopup.cpp:81
tuple f
Definition: idal.py:89
MFnDagNode * GetParent(MFnDagNode *joint)
Definition: maya_main.cpp:350
void NumberEdit_Attach(HWND hWnd)
Definition: MaskEdit.cpp:93
void ColorButton_SetColor(HWND hWnd, COLORREF color)
Definition: ColorButton.cpp:44
#define FALSE
Definition: mprintf.c:70
HINSTANCE hInstance
Definition: win_local.h:102
char * va(const char *fmt,...)
Definition: Str.cpp:1568
Win32Vars_t win32
Definition: win_main.cpp:65
COLORREF ColorButton_GetColor(HWND hWnd)
Definition: ColorButton.cpp:95
INT_PTR CALLBACK AlphaSelectDlg_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: AlphaPopup.cpp:232