Skip to content

Commit d33099e

Browse files
committed
Allowed OK and cancel buttons sizes to change, and no_default option
1 parent 84486cc commit d33099e

File tree

5 files changed

+183
-47
lines changed

5 files changed

+183
-47
lines changed

dialogs/LuaInputBox.cpp

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ BOOL CLuaInputBox::OnInitDialog()
131131
if (!m_strCancelbuttonLabel.IsEmpty ())
132132
GetDlgItem (IDCANCEL)->SetWindowText (m_strCancelbuttonLabel);
133133

134+
CWnd * ctlOK = GetDlgItem (IDOK);
135+
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
136+
137+
int iHeight;
138+
int iWidth;
139+
WINDOWPLACEMENT buttonwndpl; // where button is
140+
141+
// make OK button requested size
142+
if (m_iOKbuttonWidth > 0)
143+
{
144+
GetButtonSize (*ctlOK, iHeight, iWidth);
145+
ctlOK->GetWindowPlacement (&buttonwndpl);
146+
ctlOK->MoveWindow (buttonwndpl.rcNormalPosition.left,
147+
buttonwndpl.rcNormalPosition.top,
148+
m_iOKbuttonWidth, iHeight);
149+
}
150+
151+
// make Cancel button requested size
152+
if (m_iCancelbuttonWidth > 0)
153+
{
154+
GetButtonSize (*ctlCancel, iHeight, iWidth);
155+
ctlCancel->GetWindowPlacement (&buttonwndpl);
156+
ctlCancel->MoveWindow (buttonwndpl.rcNormalPosition.left,
157+
buttonwndpl.rcNormalPosition.top,
158+
m_iCancelbuttonWidth, iHeight);
159+
}
160+
161+
// layout dialog based on new button etc. sizes
162+
Calculate_Button_Positions ();
163+
134164
if (m_bReadOnly)
135165
{
136166
m_ctlReply.SetReadOnly (TRUE);
@@ -139,25 +169,28 @@ BOOL CLuaInputBox::OnInitDialog()
139169
}
140170
else
141171
return TRUE; // return TRUE unless you set the focus to a control
142-
}
172+
} // end of CLuaInputBox::OnInitDialog
143173

144174
void CLuaInputBox::OnRemoveSelection()
145175
{
146176

147177
// m_ctlReply.SetSel (m_strReply.GetLength (), m_strReply.GetLength ());
148178

149-
}
179+
} // end of CLuaInputBox::OnRemoveSelection
150180

151-
// helpful macro for adjusting button positions
152-
#define ADJUST_BUTTON(ctl, item) \
153-
(ctl).MoveWindow (iBorder + (iWidth * (item - 1)) + (iGap * (item - 1)), \
154-
iTopOfRow, iWidth, iHeight)
155181

182+
void CLuaInputBox::Calculate_Button_Positions ()
183+
{
184+
185+
int cx, cy;
186+
187+
CRect rect;
188+
189+
GetClientRect(&rect);
190+
191+
cx = rect.right - rect.left;
192+
cy = rect.bottom - rect.top;
156193

157-
void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
158-
{
159-
CDialog::OnSize(nType, cx, cy);
160-
161194
CWnd * ctlOK = GetDlgItem (IDOK);
162195
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
163196
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);
@@ -172,37 +205,36 @@ void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
172205
)
173206
{
174207
// move OK and Cancel buttons
175-
int iHeight;
176-
int iWidth;
208+
int iOKHeight;
209+
int iOKWidth;
210+
int iCancelHeight;
211+
int iCancelWidth;
177212
int iBorder = 10;
178213

179-
const int iButtonCount = 2; // how many buttons
180-
181214
// -----------------------
182215
// where is OK button?
183-
GetButtonSize (*ctlOK, iHeight, iWidth);
216+
GetButtonSize (*ctlOK, iOKHeight, iOKWidth);
217+
GetButtonSize (*ctlCancel, iCancelHeight, iCancelWidth);
184218

185-
int iTopOfRow = cy - iHeight - 10;
219+
int iTopOfRow = cy - iOKHeight - 10;
186220

187221
// ------------------------
188222

189-
// calculate gaps for middle buttons - I will assume all buttons are the same size here
223+
// calculate gaps for middle buttons
190224

191225
// gap (between OK and cancel buttons) will be the width of the dialog
192-
// less the gaps on the side of those buttons, less the width of the iButtonCount buttons themselves
193-
194-
int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);
226+
// less the gaps on the side of those buttons, less the width of the buttons themselves
195227

196-
// we need (iButtonCount - 1) gaps: OK --1-- Cancel
197-
iGap /= iButtonCount - 1;
228+
int iGap = cx - (iBorder * 2) - (iOKWidth + iCancelWidth);
198229

199230
// -----------------------
200231

232+
201233
// OK button (1)
202-
ADJUST_BUTTON (*ctlOK, 1);
234+
ctlOK->MoveWindow (iBorder, iTopOfRow, iOKWidth, iOKHeight);
203235

204236
// Cancel Button (2)
205-
ADJUST_BUTTON (*ctlCancel, 2);
237+
ctlCancel->MoveWindow (iBorder + iOKWidth + iGap, iTopOfRow, iCancelWidth, iCancelHeight);
206238

207239

208240
WINDOWPLACEMENT promptwndpl; // where prompt is
@@ -257,5 +289,26 @@ void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
257289

258290

259291
} // end of controls available
292+
293+
294+
} // end of CLuaInputBox::Calculate_Button_Positions
295+
296+
void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
297+
{
298+
CDialog::OnSize(nType, cx, cy);
299+
300+
Calculate_Button_Positions ();
301+
260302

261303
} // end of CLuaInputBox::OnSize
304+
305+
BOOL CLuaInputBox::PreTranslateMessage(MSG* pMsg)
306+
{
307+
// if no default button wanted, throw away <enter> key
308+
if( pMsg->message==WM_KEYDOWN &&
309+
pMsg->wParam==VK_RETURN &&
310+
m_bNoDefault)
311+
return TRUE;
312+
313+
return CDialog::PreTranslateMessage(pMsg);
314+
} // end of CLuaInputBox::PreTranslateMessage

dialogs/LuaInputBox.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ class CLuaInputBox : public CDialog
4343
bool m_bReadOnly;
4444
CString m_strOKbuttonLabel;
4545
CString m_strCancelbuttonLabel;
46+
int m_iOKbuttonWidth;
47+
int m_iCancelbuttonWidth;
48+
bool m_bNoDefault;
4649

4750
lua_State *m_L; // for validating
4851
int m_iValidationIndex; // where validation function is
4952

53+
void Calculate_Button_Positions ();
54+
5055
// Overrides
5156
// ClassWizard generated virtual function overrides
5257
//{{AFX_VIRTUAL(CLuaInputBox)
58+
public:
59+
virtual BOOL PreTranslateMessage(MSG* pMsg);
5360
protected:
5461
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
5562
//}}AFX_VIRTUAL

dialogs/LuaInputEditDlg.cpp

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ BOOL CLuaInputEditDlg::OnInitDialog()
134134
if (!m_strCancelbuttonLabel.IsEmpty ())
135135
GetDlgItem (IDCANCEL)->SetWindowText (m_strCancelbuttonLabel);
136136

137+
CWnd * ctlOK = GetDlgItem (IDOK);
138+
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
139+
140+
int iHeight;
141+
int iWidth;
142+
WINDOWPLACEMENT buttonwndpl; // where button is
143+
144+
// make OK button requested size
145+
if (m_iOKbuttonWidth > 0)
146+
{
147+
GetButtonSize (*ctlOK, iHeight, iWidth);
148+
ctlOK->GetWindowPlacement (&buttonwndpl);
149+
ctlOK->MoveWindow (buttonwndpl.rcNormalPosition.left,
150+
buttonwndpl.rcNormalPosition.top,
151+
m_iOKbuttonWidth, iHeight);
152+
}
153+
154+
// make Cancel button requested size
155+
if (m_iCancelbuttonWidth > 0)
156+
{
157+
GetButtonSize (*ctlCancel, iHeight, iWidth);
158+
ctlCancel->GetWindowPlacement (&buttonwndpl);
159+
ctlCancel->MoveWindow (buttonwndpl.rcNormalPosition.left,
160+
buttonwndpl.rcNormalPosition.top,
161+
m_iCancelbuttonWidth, iHeight);
162+
}
163+
164+
// layout dialog based on new button etc. sizes
165+
Calculate_Button_Positions ();
166+
137167
if (m_bReadOnly)
138168
{
139169
m_ctlReply.SetReadOnly (TRUE);
@@ -142,24 +172,35 @@ BOOL CLuaInputEditDlg::OnInitDialog()
142172
}
143173
else
144174
return TRUE; // return TRUE unless you set the focus to a control
145-
}
175+
} // end of CLuaInputEditDlg::OnInitDialog
146176

147177
void CLuaInputEditDlg::OnRemoveSelection()
148178
{
149179

150180
m_ctlReply.SetSel (m_strReply.GetLength (), m_strReply.GetLength ());
151181

152-
}
153-
154-
// helpful macro for adjusting button positions
155-
#define ADJUST_BUTTON(ctl, item) \
156-
(ctl).MoveWindow (iBorder + (iWidth * (item - 1)) + (iGap * (item - 1)), \
157-
iTopOfRow, iWidth, iHeight)
182+
} // end of CLuaInputEditDlg::OnRemoveSelection
158183

159184
void CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
160185
{
161186
CDialog::OnSize(nType, cx, cy);
162187

188+
Calculate_Button_Positions ();
189+
190+
} // end of CLuaInputEditDlg::OnSize
191+
192+
void CLuaInputEditDlg::Calculate_Button_Positions ()
193+
{
194+
195+
int cx, cy;
196+
197+
CRect rect;
198+
199+
GetClientRect(&rect);
200+
201+
cx = rect.right - rect.left;
202+
cy = rect.bottom - rect.top;
203+
163204
CWnd * ctlOK = GetDlgItem (IDOK);
164205
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
165206
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);
@@ -174,37 +215,36 @@ void CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
174215
)
175216
{
176217
// move OK and Cancel buttons
177-
int iHeight;
178-
int iWidth;
218+
int iOKHeight;
219+
int iOKWidth;
220+
int iCancelHeight;
221+
int iCancelWidth;
179222
int iBorder = 10;
180223

181-
const int iButtonCount = 2; // how many buttons
182-
183224
// -----------------------
184225
// where is OK button?
185-
GetButtonSize (*ctlOK, iHeight, iWidth);
226+
GetButtonSize (*ctlOK, iOKHeight, iOKWidth);
227+
GetButtonSize (*ctlCancel, iCancelHeight, iCancelWidth);
186228

187-
int iTopOfRow = cy - iHeight - 10;
229+
int iTopOfRow = cy - iOKHeight - 10;
188230

189231
// ------------------------
190232

191-
// calculate gaps for middle buttons - I will assume all buttons are the same size here
233+
// calculate gaps for middle buttons
192234

193235
// gap (between OK and cancel buttons) will be the width of the dialog
194-
// less the gaps on the side of those buttons, less the width of the iButtonCount buttons themselves
195-
196-
int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);
236+
// less the gaps on the side of those buttons, less the width of the buttons themselves
197237

198-
// we need (iButtonCount - 1) gaps: OK --1-- Cancel
199-
iGap /= iButtonCount - 1;
238+
int iGap = cx - (iBorder * 2) - (iOKWidth + iCancelWidth);
200239

201240
// -----------------------
202241

242+
203243
// OK button (1)
204-
ADJUST_BUTTON (*ctlOK, 1);
244+
ctlOK->MoveWindow (iBorder, iTopOfRow, iOKWidth, iOKHeight);
205245

206246
// Cancel Button (2)
207-
ADJUST_BUTTON (*ctlCancel, 2);
247+
ctlCancel->MoveWindow (iBorder + iOKWidth + iGap, iTopOfRow, iCancelWidth, iCancelHeight);
208248

209249

210250
WINDOWPLACEMENT promptwndpl; // where prompt is
@@ -259,5 +299,19 @@ void CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
259299

260300

261301
} // end of controls available
262-
263-
}
302+
303+
304+
} // end of CLuaInputEditDlg::Calculate_Button_Positions
305+
306+
307+
BOOL CLuaInputEditDlg::PreTranslateMessage(MSG* pMsg)
308+
{
309+
// if no default button wanted, throw away <enter> key
310+
if( pMsg->message==WM_KEYDOWN &&
311+
pMsg->wParam==VK_RETURN &&
312+
m_bNoDefault &&
313+
m_bReadOnly)
314+
return TRUE;
315+
316+
return CDialog::PreTranslateMessage(pMsg);
317+
} // end of CLuaInputEditDlg::PreTranslateMessage

dialogs/LuaInputEditDlg.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ class CLuaInputEditDlg : public CDialog
4444
bool m_bReadOnly;
4545
CString m_strOKbuttonLabel;
4646
CString m_strCancelbuttonLabel;
47+
int m_iOKbuttonWidth;
48+
int m_iCancelbuttonWidth;
49+
bool m_bNoDefault;
4750

4851
lua_State *m_L; // for validating
4952
int m_iValidationIndex; // where validation function is
5053

54+
void Calculate_Button_Positions ();
55+
5156
// Overrides
5257
// ClassWizard generated virtual function overrides
5358
//{{AFX_VIRTUAL(CLuaInputEditDlg)
59+
public:
60+
virtual BOOL PreTranslateMessage(MSG* pMsg);
5461
protected:
5562
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
5663
//}}AFX_VIRTUAL

scripting/lua_utils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ static CString get_extra_string (lua_State *L, const int narg, const char * name
314314
// validate --> validation function
315315
// ok_button --> what to put on the OK button (default: OK)
316316
// cancel_button --> what to put on the Cancel button (default: Cancel)
317+
// read_only --> text cannot be changed
318+
// ok_button_width --> width of OK button in pixels
319+
// cancel_button_width --> width of Cancel button in pixels
320+
// no_default --> there is no default button
321+
317322
//
318323
// returns: what they typed, or nil if cancelled
319324

@@ -336,8 +341,12 @@ static int gen_inputbox (lua_State *L, T & msg)
336341
int iReplyHeight = 0;
337342
int iMaxReplyLength = 0;
338343
bool bReadOnly = false;
344+
bool bNoDefault = false;
339345
CString strOKbuttonLabel;
340346
CString strCancelbuttonLabel;
347+
int iOKbuttonWidth = 0;
348+
int iCancelbuttonWidth = 0;
349+
341350

342351
// if arg6 present, and a table, grab extra stuff
343352
if (lua_istable (L, nExtraStuffArg))
@@ -352,6 +361,9 @@ static int gen_inputbox (lua_State *L, T & msg)
352361
bReadOnly = get_extra_boolean (L, nExtraStuffArg, "read_only");
353362
strOKbuttonLabel = get_extra_string (L, nExtraStuffArg, "ok_button");
354363
strCancelbuttonLabel = get_extra_string (L, nExtraStuffArg, "cancel_button");
364+
iOKbuttonWidth = get_extra_integer (L, nExtraStuffArg, "ok_button_width");
365+
iCancelbuttonWidth = get_extra_integer (L, nExtraStuffArg, "cancel_button_width");
366+
bNoDefault = get_extra_boolean (L, nExtraStuffArg, "no_default");
355367

356368

357369
// see if validation function there - do this last so we can leave it on the stack
@@ -399,6 +411,9 @@ static int gen_inputbox (lua_State *L, T & msg)
399411
msg.m_bReadOnly = bReadOnly;
400412
msg.m_strOKbuttonLabel = strOKbuttonLabel ;
401413
msg.m_strCancelbuttonLabel = strCancelbuttonLabel;
414+
msg.m_iOKbuttonWidth = iOKbuttonWidth ;
415+
msg.m_iCancelbuttonWidth = iCancelbuttonWidth;
416+
msg.m_bNoDefault = bNoDefault;
402417

403418
lua_settop (L, 0);
404419

0 commit comments

Comments
 (0)