Skip to content

Commit

Permalink
Allowed OK and cancel buttons sizes to change, and no_default option
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed May 3, 2011
1 parent 84486cc commit d33099e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 47 deletions.
101 changes: 77 additions & 24 deletions dialogs/LuaInputBox.cpp
Expand Up @@ -131,6 +131,36 @@ BOOL CLuaInputBox::OnInitDialog()
if (!m_strCancelbuttonLabel.IsEmpty ())
GetDlgItem (IDCANCEL)->SetWindowText (m_strCancelbuttonLabel);

CWnd * ctlOK = GetDlgItem (IDOK);
CWnd * ctlCancel = GetDlgItem (IDCANCEL);

int iHeight;
int iWidth;
WINDOWPLACEMENT buttonwndpl; // where button is

// make OK button requested size
if (m_iOKbuttonWidth > 0)
{
GetButtonSize (*ctlOK, iHeight, iWidth);
ctlOK->GetWindowPlacement (&buttonwndpl);
ctlOK->MoveWindow (buttonwndpl.rcNormalPosition.left,
buttonwndpl.rcNormalPosition.top,
m_iOKbuttonWidth, iHeight);
}

// make Cancel button requested size
if (m_iCancelbuttonWidth > 0)
{
GetButtonSize (*ctlCancel, iHeight, iWidth);
ctlCancel->GetWindowPlacement (&buttonwndpl);
ctlCancel->MoveWindow (buttonwndpl.rcNormalPosition.left,
buttonwndpl.rcNormalPosition.top,
m_iCancelbuttonWidth, iHeight);
}

// layout dialog based on new button etc. sizes
Calculate_Button_Positions ();

if (m_bReadOnly)
{
m_ctlReply.SetReadOnly (TRUE);
Expand All @@ -139,25 +169,28 @@ BOOL CLuaInputBox::OnInitDialog()
}
else
return TRUE; // return TRUE unless you set the focus to a control
}
} // end of CLuaInputBox::OnInitDialog

void CLuaInputBox::OnRemoveSelection()
{

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

}
} // end of CLuaInputBox::OnRemoveSelection

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

void CLuaInputBox::Calculate_Button_Positions ()
{

int cx, cy;

CRect rect;

GetClientRect(&rect);

cx = rect.right - rect.left;
cy = rect.bottom - rect.top;

void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

CWnd * ctlOK = GetDlgItem (IDOK);
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);
Expand All @@ -172,37 +205,36 @@ void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
)
{
// move OK and Cancel buttons
int iHeight;
int iWidth;
int iOKHeight;
int iOKWidth;
int iCancelHeight;
int iCancelWidth;
int iBorder = 10;

const int iButtonCount = 2; // how many buttons

// -----------------------
// where is OK button?
GetButtonSize (*ctlOK, iHeight, iWidth);
GetButtonSize (*ctlOK, iOKHeight, iOKWidth);
GetButtonSize (*ctlCancel, iCancelHeight, iCancelWidth);

int iTopOfRow = cy - iHeight - 10;
int iTopOfRow = cy - iOKHeight - 10;

// ------------------------

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

// gap (between OK and cancel buttons) will be the width of the dialog
// less the gaps on the side of those buttons, less the width of the iButtonCount buttons themselves

int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);
// less the gaps on the side of those buttons, less the width of the buttons themselves

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

// -----------------------


// OK button (1)
ADJUST_BUTTON (*ctlOK, 1);
ctlOK->MoveWindow (iBorder, iTopOfRow, iOKWidth, iOKHeight);

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


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


} // end of controls available


} // end of CLuaInputBox::Calculate_Button_Positions

void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

Calculate_Button_Positions ();


} // end of CLuaInputBox::OnSize

BOOL CLuaInputBox::PreTranslateMessage(MSG* pMsg)
{
// if no default button wanted, throw away <enter> key
if( pMsg->message==WM_KEYDOWN &&
pMsg->wParam==VK_RETURN &&
m_bNoDefault)
return TRUE;

return CDialog::PreTranslateMessage(pMsg);
} // end of CLuaInputBox::PreTranslateMessage
7 changes: 7 additions & 0 deletions dialogs/LuaInputBox.h
Expand Up @@ -43,13 +43,20 @@ class CLuaInputBox : public CDialog
bool m_bReadOnly;
CString m_strOKbuttonLabel;
CString m_strCancelbuttonLabel;
int m_iOKbuttonWidth;
int m_iCancelbuttonWidth;
bool m_bNoDefault;

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

void Calculate_Button_Positions ();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLuaInputBox)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
Expand Down
100 changes: 77 additions & 23 deletions dialogs/LuaInputEditDlg.cpp
Expand Up @@ -134,6 +134,36 @@ BOOL CLuaInputEditDlg::OnInitDialog()
if (!m_strCancelbuttonLabel.IsEmpty ())
GetDlgItem (IDCANCEL)->SetWindowText (m_strCancelbuttonLabel);

CWnd * ctlOK = GetDlgItem (IDOK);
CWnd * ctlCancel = GetDlgItem (IDCANCEL);

int iHeight;
int iWidth;
WINDOWPLACEMENT buttonwndpl; // where button is

// make OK button requested size
if (m_iOKbuttonWidth > 0)
{
GetButtonSize (*ctlOK, iHeight, iWidth);
ctlOK->GetWindowPlacement (&buttonwndpl);
ctlOK->MoveWindow (buttonwndpl.rcNormalPosition.left,
buttonwndpl.rcNormalPosition.top,
m_iOKbuttonWidth, iHeight);
}

// make Cancel button requested size
if (m_iCancelbuttonWidth > 0)
{
GetButtonSize (*ctlCancel, iHeight, iWidth);
ctlCancel->GetWindowPlacement (&buttonwndpl);
ctlCancel->MoveWindow (buttonwndpl.rcNormalPosition.left,
buttonwndpl.rcNormalPosition.top,
m_iCancelbuttonWidth, iHeight);
}

// layout dialog based on new button etc. sizes
Calculate_Button_Positions ();

if (m_bReadOnly)
{
m_ctlReply.SetReadOnly (TRUE);
Expand All @@ -142,24 +172,35 @@ BOOL CLuaInputEditDlg::OnInitDialog()
}
else
return TRUE; // return TRUE unless you set the focus to a control
}
} // end of CLuaInputEditDlg::OnInitDialog

void CLuaInputEditDlg::OnRemoveSelection()
{

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

}

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

void CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

Calculate_Button_Positions ();

} // end of CLuaInputEditDlg::OnSize

void CLuaInputEditDlg::Calculate_Button_Positions ()
{

int cx, cy;

CRect rect;

GetClientRect(&rect);

cx = rect.right - rect.left;
cy = rect.bottom - rect.top;

CWnd * ctlOK = GetDlgItem (IDOK);
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);
Expand All @@ -174,37 +215,36 @@ void CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
)
{
// move OK and Cancel buttons
int iHeight;
int iWidth;
int iOKHeight;
int iOKWidth;
int iCancelHeight;
int iCancelWidth;
int iBorder = 10;

const int iButtonCount = 2; // how many buttons

// -----------------------
// where is OK button?
GetButtonSize (*ctlOK, iHeight, iWidth);
GetButtonSize (*ctlOK, iOKHeight, iOKWidth);
GetButtonSize (*ctlCancel, iCancelHeight, iCancelWidth);

int iTopOfRow = cy - iHeight - 10;
int iTopOfRow = cy - iOKHeight - 10;

// ------------------------

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

// gap (between OK and cancel buttons) will be the width of the dialog
// less the gaps on the side of those buttons, less the width of the iButtonCount buttons themselves

int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);
// less the gaps on the side of those buttons, less the width of the buttons themselves

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

// -----------------------


// OK button (1)
ADJUST_BUTTON (*ctlOK, 1);
ctlOK->MoveWindow (iBorder, iTopOfRow, iOKWidth, iOKHeight);

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


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


} // end of controls available

}


} // end of CLuaInputEditDlg::Calculate_Button_Positions


BOOL CLuaInputEditDlg::PreTranslateMessage(MSG* pMsg)
{
// if no default button wanted, throw away <enter> key
if( pMsg->message==WM_KEYDOWN &&
pMsg->wParam==VK_RETURN &&
m_bNoDefault &&
m_bReadOnly)
return TRUE;

return CDialog::PreTranslateMessage(pMsg);
} // end of CLuaInputEditDlg::PreTranslateMessage
7 changes: 7 additions & 0 deletions dialogs/LuaInputEditDlg.h
Expand Up @@ -44,13 +44,20 @@ class CLuaInputEditDlg : public CDialog
bool m_bReadOnly;
CString m_strOKbuttonLabel;
CString m_strCancelbuttonLabel;
int m_iOKbuttonWidth;
int m_iCancelbuttonWidth;
bool m_bNoDefault;

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

void Calculate_Button_Positions ();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLuaInputEditDlg)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
Expand Down
15 changes: 15 additions & 0 deletions scripting/lua_utils.cpp
Expand Up @@ -314,6 +314,11 @@ static CString get_extra_string (lua_State *L, const int narg, const char * name
// validate --> validation function
// ok_button --> what to put on the OK button (default: OK)
// cancel_button --> what to put on the Cancel button (default: Cancel)
// read_only --> text cannot be changed
// ok_button_width --> width of OK button in pixels
// cancel_button_width --> width of Cancel button in pixels
// no_default --> there is no default button

//
// returns: what they typed, or nil if cancelled

Expand All @@ -336,8 +341,12 @@ static int gen_inputbox (lua_State *L, T & msg)
int iReplyHeight = 0;
int iMaxReplyLength = 0;
bool bReadOnly = false;
bool bNoDefault = false;
CString strOKbuttonLabel;
CString strCancelbuttonLabel;
int iOKbuttonWidth = 0;
int iCancelbuttonWidth = 0;


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


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

lua_settop (L, 0);

Expand Down

0 comments on commit d33099e

Please sign in to comment.