Skip to content

Commit 7da05d3

Browse files
committed
Improvements to utils.editbox and utils.inputbox
1 parent c5f8ef7 commit 7da05d3

File tree

6 files changed

+303
-19
lines changed

6 files changed

+303
-19
lines changed

dialogs/EditMultiLine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ END_MESSAGE_MAP()
7272

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

7878

dialogs/LuaInputBox.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void CLuaInputBox::DoDataExchange(CDataExchange* pDX)
4949

5050
BEGIN_MESSAGE_MAP(CLuaInputBox, CDialog)
5151
//{{AFX_MSG_MAP(CLuaInputBox)
52+
ON_WM_SIZE()
5253
//}}AFX_MSG_MAP
5354

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

6768
PostMessage (WM_COMMAND, CLEAR_SELECTION);
6869

70+
WINDOWPLACEMENT wndpl;
71+
72+
GetWindowPlacement (&wndpl);
73+
74+
if (m_iBoxWidth < 180) // need room for OK and Cancel buttons
75+
m_iBoxWidth = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
76+
77+
if (m_iBoxHeight < 125) // need room for prompt and reply
78+
m_iBoxHeight = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
79+
80+
MoveWindow(wndpl.rcNormalPosition.left, wndpl.rcNormalPosition.top, m_iBoxWidth, m_iBoxHeight);
81+
82+
if (m_iMaxReplyLength > 0)
83+
::SendMessage(m_ctlReply, EM_LIMITTEXT, m_iMaxReplyLength, 0);
84+
85+
6986
return TRUE; // return TRUE unless you set the focus to a control
7087
// EXCEPTION: OCX Property Pages should return FALSE
7188
}
@@ -76,3 +93,115 @@ void CLuaInputBox::OnRemoveSelection()
7693
// m_ctlReply.SetSel (m_strReply.GetLength (), m_strReply.GetLength ());
7794

7895
}
96+
97+
// helpful macro for adjusting button positions
98+
#define ADJUST_BUTTON(ctl, item) \
99+
(ctl).MoveWindow (iBorder + (iWidth * (item - 1)) + (iGap * (item - 1)), \
100+
iTopOfRow, iWidth, iHeight)
101+
102+
103+
void CLuaInputBox::OnSize(UINT nType, int cx, int cy)
104+
{
105+
CDialog::OnSize(nType, cx, cy);
106+
107+
CWnd * ctlOK = GetDlgItem (IDOK);
108+
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
109+
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);
110+
111+
if (ctlCancel &&
112+
ctlCancel->m_hWnd &&
113+
ctlOK &&
114+
ctlOK->m_hWnd &&
115+
ctlMessage &&
116+
ctlMessage->m_hWnd
117+
118+
)
119+
{
120+
// move OK and Cancel buttons
121+
int iHeight;
122+
int iWidth;
123+
int iBorder = 10;
124+
125+
const int iButtonCount = 2; // how many buttons
126+
127+
// -----------------------
128+
// where is OK button?
129+
GetButtonSize (*ctlOK, iHeight, iWidth);
130+
131+
int iTopOfRow = cy - iHeight - 10;
132+
133+
// ------------------------
134+
135+
// calculate gaps for middle buttons - I will assume all buttons are the same size here
136+
137+
// gap (between OK and cancel buttons) will be the width of the dialog
138+
// less the gaps on the side of those buttons, less the width of the iButtonCount buttons themselves
139+
140+
int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);
141+
142+
// we need (iButtonCount - 1) gaps: OK --1-- Cancel
143+
iGap /= iButtonCount - 1;
144+
145+
// -----------------------
146+
147+
// OK button (1)
148+
ADJUST_BUTTON (*ctlOK, 1);
149+
150+
// Cancel Button (2)
151+
ADJUST_BUTTON (*ctlCancel, 2);
152+
153+
154+
WINDOWPLACEMENT promptwndpl; // where prompt is
155+
WINDOWPLACEMENT cancelwndpl; // where cancel button is
156+
157+
// where is prompt?
158+
ctlMessage->GetWindowPlacement (&promptwndpl);
159+
160+
// where it he cancel button now?
161+
ctlCancel->GetWindowPlacement (&cancelwndpl);
162+
163+
// if prompt too small, make it width of window
164+
if (m_iPromptWidth <= 0)
165+
m_iPromptWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;
166+
167+
if (m_iPromptWidth < 10)
168+
m_iPromptWidth = 10;
169+
170+
if (m_iPromptHeight <= 0)
171+
m_iPromptHeight = promptwndpl.rcNormalPosition.bottom - promptwndpl.rcNormalPosition.top;
172+
173+
if (m_iPromptHeight < 12)
174+
m_iPromptHeight = 12;
175+
176+
// new prompt size
177+
ctlMessage->MoveWindow(promptwndpl.rcNormalPosition.left, promptwndpl.rcNormalPosition.top, m_iPromptWidth, m_iPromptHeight);
178+
179+
// where is the prompt now?
180+
ctlMessage->GetWindowPlacement (&promptwndpl);
181+
182+
// default reply width is from left of prompt to right of cancel button
183+
int iReplyWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;
184+
185+
// default reply height is from bottom of prompt (+ gap) to top of cancel button (+ gap)
186+
int iReplyHeight = cancelwndpl.rcNormalPosition.top - promptwndpl.rcNormalPosition.bottom - iBorder * 2;
187+
188+
// override with user-supplied if smaller
189+
if (m_iReplyWidth < iReplyWidth && m_iReplyWidth > 10)
190+
iReplyWidth = m_iReplyWidth;
191+
192+
// ditto for height
193+
if (m_iReplyHeight < iReplyHeight && m_iReplyHeight > 10)
194+
iReplyHeight = m_iReplyHeight;
195+
196+
// move reply area to fit
197+
m_ctlReply.MoveWindow (promptwndpl.rcNormalPosition.left, // left
198+
promptwndpl.rcNormalPosition.bottom + iBorder, // top
199+
iReplyWidth, // width
200+
iReplyHeight); // height
201+
202+
203+
204+
205+
} // end of controls available
206+
207+
} // end of CLuaInputBox::OnSize

dialogs/LuaInputBox.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class CLuaInputBox : public CDialog
3333

3434
CFont * m_font;
3535

36+
int m_iBoxWidth;
37+
int m_iBoxHeight;
38+
int m_iPromptWidth;
39+
int m_iPromptHeight;
40+
int m_iReplyWidth;
41+
int m_iReplyHeight;
42+
int m_iMaxReplyLength;
43+
3644
// Overrides
3745
// ClassWizard generated virtual function overrides
3846
//{{AFX_VIRTUAL(CLuaInputBox)
@@ -46,6 +54,7 @@ class CLuaInputBox : public CDialog
4654
// Generated message map functions
4755
//{{AFX_MSG(CLuaInputBox)
4856
virtual BOOL OnInitDialog();
57+
afx_msg void OnSize(UINT nType, int cx, int cy);
4958
//}}AFX_MSG
5059
afx_msg void OnRemoveSelection();
5160

dialogs/LuaInputEditDlg.cpp

Lines changed: 102 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ BOOL CLuaInputEditDlg::OnInitDialog()
7070

7171
PostMessage (WM_COMMAND, CLEAR_SELECTION);
7272

73+
WINDOWPLACEMENT wndpl;
74+
75+
GetWindowPlacement (&wndpl);
76+
77+
if (m_iBoxWidth < 180) // need room for OK and Cancel buttons
78+
m_iBoxWidth = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
79+
80+
if (m_iBoxHeight < 125) // need room for prompt and reply
81+
m_iBoxHeight = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
82+
83+
MoveWindow(wndpl.rcNormalPosition.left, wndpl.rcNormalPosition.top, m_iBoxWidth, m_iBoxHeight);
84+
85+
if (m_iMaxReplyLength > 0)
86+
::SendMessage(m_ctlReply, EM_LIMITTEXT, m_iMaxReplyLength, 0);
87+
7388
return TRUE; // return TRUE unless you set the focus to a control
7489
// EXCEPTION: OCX Property Pages should return FALSE
7590
}
@@ -81,42 +96,113 @@ void CLuaInputEditDlg::OnRemoveSelection()
8196

8297
}
8398

99+
// helpful macro for adjusting button positions
100+
#define ADJUST_BUTTON(ctl, item) \
101+
(ctl).MoveWindow (iBorder + (iWidth * (item - 1)) + (iGap * (item - 1)), \
102+
iTopOfRow, iWidth, iHeight)
103+
84104
void CLuaInputEditDlg::OnSize(UINT nType, int cx, int cy)
85105
{
86106
CDialog::OnSize(nType, cx, cy);
87107

88-
if (m_ctlReply.m_hWnd && m_ctlCancel.m_hWnd && m_ctlOK)
108+
CWnd * ctlOK = GetDlgItem (IDOK);
109+
CWnd * ctlCancel = GetDlgItem (IDCANCEL);
110+
CWnd * ctlMessage = GetDlgItem (IDC_INPUT_BOX_MESSAGE);
111+
112+
if (ctlCancel &&
113+
ctlCancel->m_hWnd &&
114+
ctlOK &&
115+
ctlOK->m_hWnd &&
116+
ctlMessage &&
117+
ctlMessage->m_hWnd
118+
119+
)
89120
{
90121
// move OK and Cancel buttons
91-
WINDOWPLACEMENT wndpl;
92122
int iHeight;
93123
int iWidth;
94124
int iBorder = 10;
95125

126+
const int iButtonCount = 2; // how many buttons
127+
96128
// -----------------------
97129
// where is OK button?
98-
GetButtonSize (m_ctlOK, iHeight, iWidth);
130+
GetButtonSize (*ctlOK, iHeight, iWidth);
99131

100-
// move to near bottom
132+
int iTopOfRow = cy - iHeight - 10;
101133

102-
m_ctlOK.MoveWindow (iBorder, cy - iHeight - 10, iWidth, iHeight);
134+
// ------------------------
103135

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

108-
// move to near bottom
138+
// gap (between OK and cancel buttons) will be the width of the dialog
139+
// less the gaps on the side of those buttons, less the width of the iButtonCount buttons themselves
109140

110-
m_ctlCancel.MoveWindow (cx - iWidth - iBorder, cy - iHeight - 10, iWidth, iHeight);
141+
int iGap = cx - (iBorder * 2) - (iWidth * iButtonCount);
142+
143+
// we need (iButtonCount - 1) gaps: OK --1-- Cancel
144+
iGap /= iButtonCount - 1;
111145

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

116-
const int iTop = 32;
148+
// OK button (1)
149+
ADJUST_BUTTON (*ctlOK, 1);
150+
151+
// Cancel Button (2)
152+
ADJUST_BUTTON (*ctlCancel, 2);
153+
154+
155+
WINDOWPLACEMENT promptwndpl; // where prompt is
156+
WINDOWPLACEMENT cancelwndpl; // where cancel button is
157+
158+
// where is prompt?
159+
ctlMessage->GetWindowPlacement (&promptwndpl);
160+
161+
// where it he cancel button now?
162+
ctlCancel->GetWindowPlacement (&cancelwndpl);
163+
164+
// if prompt too small, make it width of window
165+
if (m_iPromptWidth <= 0)
166+
m_iPromptWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;
167+
168+
if (m_iPromptWidth < 10)
169+
m_iPromptWidth = 10;
170+
171+
if (m_iPromptHeight <= 0)
172+
m_iPromptHeight = promptwndpl.rcNormalPosition.bottom - promptwndpl.rcNormalPosition.top;
173+
174+
if (m_iPromptHeight < 12)
175+
m_iPromptHeight = 12;
176+
177+
// new prompt size
178+
ctlMessage->MoveWindow(promptwndpl.rcNormalPosition.left, promptwndpl.rcNormalPosition.top, m_iPromptWidth, m_iPromptHeight);
179+
180+
// where is the prompt now?
181+
ctlMessage->GetWindowPlacement (&promptwndpl);
182+
183+
// default reply width is from left of prompt to right of cancel button
184+
int iReplyWidth = cancelwndpl.rcNormalPosition.right - promptwndpl.rcNormalPosition.left;
185+
186+
// default reply height is from bottom of prompt (+ gap) to top of cancel button (+ gap)
187+
int iReplyHeight = cancelwndpl.rcNormalPosition.top - promptwndpl.rcNormalPosition.bottom - iBorder * 2;
188+
189+
// override with user-supplied if smaller
190+
if (m_iReplyWidth < iReplyWidth && m_iReplyWidth > 10)
191+
iReplyWidth = m_iReplyWidth;
192+
193+
// ditto for height
194+
if (m_iReplyHeight < iReplyHeight && m_iReplyHeight > 10)
195+
iReplyHeight = m_iReplyHeight;
196+
197+
// move reply area to fit
198+
m_ctlReply.MoveWindow (promptwndpl.rcNormalPosition.left, // left
199+
promptwndpl.rcNormalPosition.bottom + iBorder, // top
200+
iReplyWidth, // width
201+
iReplyHeight); // height
202+
203+
204+
117205

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

122208
}

dialogs/LuaInputEditDlg.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class CLuaInputEditDlg : public CDialog
3434

3535
CFont * m_font;
3636

37+
int m_iBoxWidth;
38+
int m_iBoxHeight;
39+
int m_iPromptWidth;
40+
int m_iPromptHeight;
41+
int m_iReplyWidth;
42+
int m_iReplyHeight;
43+
int m_iMaxReplyLength;
44+
3745
// Overrides
3846
// ClassWizard generated virtual function overrides
3947
//{{AFX_VIRTUAL(CLuaInputEditDlg)

0 commit comments

Comments
 (0)