Skip to content

Commit

Permalink
Improvements to utils.editbox and utils.inputbox
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Oct 1, 2010
1 parent c5f8ef7 commit 7da05d3
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dialogs/EditMultiLine.cpp
Expand Up @@ -72,7 +72,7 @@ END_MESSAGE_MAP()

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


Expand Down
129 changes: 129 additions & 0 deletions dialogs/LuaInputBox.cpp
Expand Up @@ -49,6 +49,7 @@ void CLuaInputBox::DoDataExchange(CDataExchange* pDX)

BEGIN_MESSAGE_MAP(CLuaInputBox, CDialog)
//{{AFX_MSG_MAP(CLuaInputBox)
ON_WM_SIZE()
//}}AFX_MSG_MAP

ON_COMMAND(CLEAR_SELECTION, OnRemoveSelection)
Expand All @@ -66,6 +67,22 @@ BOOL CLuaInputBox::OnInitDialog()

PostMessage (WM_COMMAND, CLEAR_SELECTION);

WINDOWPLACEMENT wndpl;

GetWindowPlacement (&wndpl);

if (m_iBoxWidth < 180) // need room for OK and Cancel buttons
m_iBoxWidth = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;

if (m_iBoxHeight < 125) // need room for prompt and reply
m_iBoxHeight = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;

MoveWindow(wndpl.rcNormalPosition.left, wndpl.rcNormalPosition.top, m_iBoxWidth, m_iBoxHeight);

if (m_iMaxReplyLength > 0)
::SendMessage(m_ctlReply, EM_LIMITTEXT, m_iMaxReplyLength, 0);


return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Expand All @@ -76,3 +93,115 @@ void CLuaInputBox::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)


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);

if (ctlCancel &&
ctlCancel->m_hWnd &&
ctlOK &&
ctlOK->m_hWnd &&
ctlMessage &&
ctlMessage->m_hWnd

)
{
// move OK and Cancel buttons
int iHeight;
int iWidth;
int iBorder = 10;

const int iButtonCount = 2; // how many buttons

// -----------------------
// where is OK button?
GetButtonSize (*ctlOK, iHeight, iWidth);

int iTopOfRow = cy - iHeight - 10;

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

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

// 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);

// we need (iButtonCount - 1) gaps: OK --1-- Cancel
iGap /= iButtonCount - 1;

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

// OK button (1)
ADJUST_BUTTON (*ctlOK, 1);

// Cancel Button (2)
ADJUST_BUTTON (*ctlCancel, 2);


WINDOWPLACEMENT promptwndpl; // where prompt is
WINDOWPLACEMENT cancelwndpl; // where cancel button is

// where is prompt?
ctlMessage->GetWindowPlacement (&promptwndpl);

// where it he cancel button now?
ctlCancel->GetWindowPlacement (&cancelwndpl);

// if prompt too small, make it width of window
if (m_iPromptWidth <= 0)
m_iPromptWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;

if (m_iPromptWidth < 10)
m_iPromptWidth = 10;

if (m_iPromptHeight <= 0)
m_iPromptHeight = promptwndpl.rcNormalPosition.bottom - promptwndpl.rcNormalPosition.top;

if (m_iPromptHeight < 12)
m_iPromptHeight = 12;

// new prompt size
ctlMessage->MoveWindow(promptwndpl.rcNormalPosition.left, promptwndpl.rcNormalPosition.top, m_iPromptWidth, m_iPromptHeight);

// where is the prompt now?
ctlMessage->GetWindowPlacement (&promptwndpl);

// default reply width is from left of prompt to right of cancel button
int iReplyWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;

// default reply height is from bottom of prompt (+ gap) to top of cancel button (+ gap)
int iReplyHeight = cancelwndpl.rcNormalPosition.top - promptwndpl.rcNormalPosition.bottom - iBorder * 2;

// override with user-supplied if smaller
if (m_iReplyWidth < iReplyWidth && m_iReplyWidth > 10)
iReplyWidth = m_iReplyWidth;

// ditto for height
if (m_iReplyHeight < iReplyHeight && m_iReplyHeight > 10)
iReplyHeight = m_iReplyHeight;

// move reply area to fit
m_ctlReply.MoveWindow (promptwndpl.rcNormalPosition.left, // left
promptwndpl.rcNormalPosition.bottom + iBorder, // top
iReplyWidth, // width
iReplyHeight); // height




} // end of controls available

} // end of CLuaInputBox::OnSize
9 changes: 9 additions & 0 deletions dialogs/LuaInputBox.h
Expand Up @@ -33,6 +33,14 @@ class CLuaInputBox : public CDialog

CFont * m_font;

int m_iBoxWidth;
int m_iBoxHeight;
int m_iPromptWidth;
int m_iPromptHeight;
int m_iReplyWidth;
int m_iReplyHeight;
int m_iMaxReplyLength;

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLuaInputBox)
Expand All @@ -46,6 +54,7 @@ class CLuaInputBox : public CDialog
// Generated message map functions
//{{AFX_MSG(CLuaInputBox)
virtual BOOL OnInitDialog();
afx_msg void OnSize(UINT nType, int cx, int cy);
//}}AFX_MSG
afx_msg void OnRemoveSelection();

Expand Down
118 changes: 102 additions & 16 deletions dialogs/LuaInputEditDlg.cpp
Expand Up @@ -70,6 +70,21 @@ BOOL CLuaInputEditDlg::OnInitDialog()

PostMessage (WM_COMMAND, CLEAR_SELECTION);

WINDOWPLACEMENT wndpl;

GetWindowPlacement (&wndpl);

if (m_iBoxWidth < 180) // need room for OK and Cancel buttons
m_iBoxWidth = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;

if (m_iBoxHeight < 125) // need room for prompt and reply
m_iBoxHeight = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;

MoveWindow(wndpl.rcNormalPosition.left, wndpl.rcNormalPosition.top, m_iBoxWidth, m_iBoxHeight);

if (m_iMaxReplyLength > 0)
::SendMessage(m_ctlReply, EM_LIMITTEXT, m_iMaxReplyLength, 0);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Expand All @@ -81,42 +96,113 @@ void CLuaInputEditDlg::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 CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

if (m_ctlReply.m_hWnd && m_ctlCancel.m_hWnd && m_ctlOK)
CWnd * ctlOK = GetDlgItem (IDOK);
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);

if (ctlCancel &&
ctlCancel->m_hWnd &&
ctlOK &&
ctlOK->m_hWnd &&
ctlMessage &&
ctlMessage->m_hWnd

)
{
// move OK and Cancel buttons
WINDOWPLACEMENT wndpl;
int iHeight;
int iWidth;
int iBorder = 10;

const int iButtonCount = 2; // how many buttons

// -----------------------
// where is OK button?
GetButtonSize (m_ctlOK, iHeight, iWidth);
GetButtonSize (*ctlOK, iHeight, iWidth);

// move to near bottom
int iTopOfRow = cy - iHeight - 10;

m_ctlOK.MoveWindow (iBorder, cy - iHeight - 10, iWidth, iHeight);
// ------------------------

// -----------------------
// where is Cancel button?
GetButtonSize (m_ctlCancel, iHeight, iWidth);
// calculate gaps for middle buttons - I will assume all buttons are the same size here

// move to near bottom
// 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

m_ctlCancel.MoveWindow (cx - iWidth - iBorder, cy - iHeight - 10, iWidth, iHeight);
int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);

// we need (iButtonCount - 1) gaps: OK --1-- Cancel
iGap /= iButtonCount - 1;

// -----------------------
// where is Cancel button now?
m_ctlCancel.GetWindowPlacement (&wndpl);

const int iTop = 32;
// OK button (1)
ADJUST_BUTTON (*ctlOK, 1);

// Cancel Button (2)
ADJUST_BUTTON (*ctlCancel, 2);


WINDOWPLACEMENT promptwndpl; // where prompt is
WINDOWPLACEMENT cancelwndpl; // where cancel button is

// where is prompt?
ctlMessage->GetWindowPlacement (&promptwndpl);

// where it he cancel button now?
ctlCancel->GetWindowPlacement (&cancelwndpl);

// if prompt too small, make it width of window
if (m_iPromptWidth <= 0)
m_iPromptWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;

if (m_iPromptWidth < 10)
m_iPromptWidth = 10;

if (m_iPromptHeight <= 0)
m_iPromptHeight = promptwndpl.rcNormalPosition.bottom - promptwndpl.rcNormalPosition.top;

if (m_iPromptHeight < 12)
m_iPromptHeight = 12;

// new prompt size
ctlMessage->MoveWindow(promptwndpl.rcNormalPosition.left, promptwndpl.rcNormalPosition.top, m_iPromptWidth, m_iPromptHeight);

// where is the prompt now?
ctlMessage->GetWindowPlacement (&promptwndpl);

// default reply width is from left of prompt to right of cancel button
int iReplyWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;

// default reply height is from bottom of prompt (+ gap) to top of cancel button (+ gap)
int iReplyHeight = cancelwndpl.rcNormalPosition.top - promptwndpl.rcNormalPosition.bottom - iBorder * 2;

// override with user-supplied if smaller
if (m_iReplyWidth < iReplyWidth && m_iReplyWidth > 10)
iReplyWidth = m_iReplyWidth;

// ditto for height
if (m_iReplyHeight < iReplyHeight && m_iReplyHeight > 10)
iReplyHeight = m_iReplyHeight;

// move reply area to fit
m_ctlReply.MoveWindow (promptwndpl.rcNormalPosition.left, // left
promptwndpl.rcNormalPosition.bottom + iBorder, // top
iReplyWidth, // width
iReplyHeight); // height




// move text to just above it
m_ctlReply.MoveWindow(iBorder, iTop, cx - (iBorder * 2), wndpl.rcNormalPosition.top - 10 - iTop);
}
} // end of controls available

}
8 changes: 8 additions & 0 deletions dialogs/LuaInputEditDlg.h
Expand Up @@ -34,6 +34,14 @@ class CLuaInputEditDlg : public CDialog

CFont * m_font;

int m_iBoxWidth;
int m_iBoxHeight;
int m_iPromptWidth;
int m_iPromptHeight;
int m_iReplyWidth;
int m_iReplyHeight;
int m_iMaxReplyLength;

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLuaInputEditDlg)
Expand Down

0 comments on commit 7da05d3

Please sign in to comment.