Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
LedItMainFrame.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5#include "Stroika/Foundation/StroikaPreComp.h"
6
7#include <afxadv.h>
8
9#include "ColorMenu.h"
10#include "LedItResources.h"
11#include "LedItView.h"
12#include "Options.h"
13
14#if qStroika_Foundation_Common_Platform_Windows
15#include "LedItApplication.h"
16#endif
17
18#include "LedItMainFrame.h"
19
20using namespace Stroika::Foundation;
21using namespace Stroika::Frameworks::Led;
22using namespace Stroika::Frameworks::Led::Platform;
23using namespace Stroika::Frameworks::Led::StyledTextIO;
24
25inline LedItView* GetActiveLedItView ()
26{
27// For MDI
28#if 0
29 CMDIFrameWnd* mainFrame = dynamic_cast<CMDIFrameWnd*> (::AfxGetMainWnd ());
30 if (mainFrame != NULL) {
31 CMDIChildWnd* childFrame = mainFrame->MDIGetActive ();
32 if (childFrame != NULL) {
33 return dynamic_cast<LedItView*> (childFrame->GetActiveView ());
34 }
35 }
36#endif
37 // For SDI
38 CFrameWnd* mainFrame = dynamic_cast<CFrameWnd*> (::AfxGetMainWnd ());
39 if (mainFrame != NULL) {
40 return dynamic_cast<LedItView*> (mainFrame->GetActiveView ());
41 }
42 return NULL;
43}
44
45/*
46 ********************************************************************************
47 ******************************** LedItMainFrame ********************************
48 ********************************************************************************
49 */
50
51IMPLEMENT_DYNCREATE (LedItMainFrame, CFrameWnd)
52
53BEGIN_MESSAGE_MAP (LedItMainFrame, CFrameWnd)
54ON_WM_CREATE ()
55ON_WM_CLOSE ()
56ON_WM_INITMENUPOPUP ()
57ON_COMMAND (ID_HELP_FINDER, OnHelpFinder)
58ON_COMMAND (ID_HELP, OnHelp)
59ON_COMMAND (ID_CONTEXT_HELP, OnContextHelp)
60ON_COMMAND (ID_DEFAULT_HELP, OnHelpFinder)
61ON_COMMAND (ID_CHAR_COLOR, OnCharColor)
62ON_COMMAND_EX (ID_VIEW_FORMATBAR, OnBarCheck)
63ON_UPDATE_COMMAND_UI (ID_VIEW_FORMATBAR, OnUpdateControlBarMenu)
64ON_COMMAND_EX (ID_VIEW_RULER, OnBarCheck)
65ON_UPDATE_COMMAND_UI (ID_VIEW_RULER, OnUpdateControlBarMenu)
66END_MESSAGE_MAP ()
67
68LedItMainFrame::LedItMainFrame ()
69 : inherited ()
70 , fStatusBar ()
71 , fToolBar ()
72 , fFormatBar (NULL)
73{
74 fFormatBar = new FormatToolbar (*this);
75}
76
77LedItMainFrame::~LedItMainFrame ()
78{
79 delete fFormatBar;
80}
81
82int LedItMainFrame::OnCreate (LPCREATESTRUCT lpCreateStruct)
83{
84 if (inherited::OnCreate (lpCreateStruct) == -1) {
85 return -1;
86 }
87
88 Led_Size desiredSize = Led_Size (Led_CvtScreenPixelsFromTWIPSV (TWIPS (1440 * 11)),
89 //Led_CvtScreenPixelsFromTWIPSH (TWIPS (1440 * 8.5))
90 Led_CvtScreenPixelsFromTWIPSH (TWIPS (static_cast<long> (1440 * 7.5))));
91 Led_Rect newBounds = Led_Rect (lpCreateStruct->y, lpCreateStruct->x, desiredSize.v, desiredSize.h);
92 newBounds = EnsureRectOnScreen (newBounds);
93 MoveWindow (CRect (AsRECT (newBounds)));
94
95 CMenu* menuBar = GetMenu ();
96 LedItApplication::Get ().FixupFontMenu (menuBar->GetSubMenu (4)->GetSubMenu (0));
97
98 if (!fToolBar.Create (this) || !fToolBar.LoadToolBar (IDR_MAINFRAME)) {
99 TRACE0 ("Failed to create toolbar\n");
100 return -1; // fail to create
101 }
102
103 AssertNotNull (fFormatBar);
104 if (!fFormatBar->Create (this, WS_CHILD | WS_VISIBLE | CBRS_TOP, ID_VIEW_FORMATBAR)) {
105 TRACE0 ("Failed to create FormatBar\n");
106 return -1; // fail to create
107 }
108
109 const UINT indicators[] = {
110 ID_SEPARATOR, // status line indicator
111 ID_INDICATOR_CAPS,
112 ID_INDICATOR_NUM,
113 ID_INDICATOR_SCRL,
114 };
115 if (!fStatusBar.Create (this) || !fStatusBar.SetIndicators (indicators, sizeof (indicators) / sizeof (UINT))) {
116 TRACE0 ("Failed to create status bar\n");
117 return -1; // fail to create
118 }
119
120 // TODO: Remove this if you don't want tool tips or a resizeable toolbar
121 fToolBar.SetBarStyle (fToolBar.GetBarStyle () | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
122
123 EnableDocking (CBRS_ALIGN_ANY);
124
125 if (!fRulerBar.Create (this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_HIDE_INPLACE, ID_VIEW_RULER)) {
126 TRACE0 ("Failed to create ruler\n");
127 return -1; // fail to create
128 }
129
130 // TODO: Delete these three lines if you don't want the toolbar to
131 // be dockable
132 fToolBar.EnableDocking (CBRS_ALIGN_ANY);
133 AssertNotNull (fFormatBar);
134 fFormatBar->EnableDocking (CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
135 DockControlBar (&fToolBar);
136 DockControlBar (fFormatBar);
137 SetDockState (Options{}.GetDocBarState ());
138
139 DragAcceptFiles ();
140
141 return 0;
142}
143
144void LedItMainFrame::OnClose ()
145{
146 CDockState dockState;
147 GetDockState (dockState);
148 Options{}.SetDocBarState (dockState);
149 inherited::OnClose ();
150}
151
152BOOL LedItMainFrame::PreCreateWindow (CREATESTRUCT& cs)
153{
154 return inherited::PreCreateWindow (cs);
155}
156
157IncrementalFontSpecification LedItMainFrame::GetCurFont () const
158{
159 IncrementalFontSpecification ifsp;
160 LedItView* liv = GetActiveLedItView ();
161 if (liv != NULL) {
162 ifsp = liv->GetCurSelFontSpec ();
163 }
164 return ifsp;
165}
166
167void LedItMainFrame::SetCurFont (const IncrementalFontSpecification& fsp)
168{
169 LedItView* liv = GetActiveLedItView ();
170 if (liv != NULL) {
171 liv->InteractiveSetFont (fsp);
172 }
173}
174
175void LedItMainFrame::OnCharColor ()
176{
177 ColorMenu colorMenu;
178 CRect rc;
179 AssertNotNull (fFormatBar);
180 int index = fFormatBar->CommandToIndex (ID_CHAR_COLOR);
181 fFormatBar->GetItemRect (index, &rc);
182 fFormatBar->ClientToScreen (rc);
183 colorMenu.TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON, rc.left, rc.bottom, this);
184}
185
186static bool IsPopupInOwningMenu (HMENU popup, HMENU potentialOwner)
187{
188 RequireNotNull (popup);
189 if (potentialOwner != NULL) {
190 int nIndexMax = ::GetMenuItemCount (potentialOwner);
191 for (int nIndex = 0; nIndex < nIndexMax; ++nIndex) {
192 HMENU itsSubMenu = ::GetSubMenu (potentialOwner, nIndex);
193 if (itsSubMenu == popup) {
194 return true;
195 }
196 else if (itsSubMenu != NULL) {
197 // See if its inside sub menu.
198 if (IsPopupInOwningMenu (popup, itsSubMenu)) {
199 return true;
200 }
201 }
202 }
203 }
204 return false;
205}
206void LedItMainFrame::OnInitMenuPopup (CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
207{
208 // Disable inappropriate commands (done by inherited version)
209 inherited::OnInitMenuPopup (pPopupMenu, nIndex, bSysMenu);
210
211 // Check if this is our context menu, or the systemmenu, or one of the
212 // applications main window menus.
213 // We only do our remove-disabled hack for the context menu...
214 if (bSysMenu or IsPopupInOwningMenu (pPopupMenu->m_hMenu, ::GetMenu (m_hWnd))) {
215 return;
216 }
217
218 // Remove disabled menu items from the popup (to save space).
219 // Remove consecutive (or leading) separators as useless.
220 CCmdUI state;
221 state.m_pSubMenu = NULL;
222 state.m_pMenu = pPopupMenu;
223 Assert (state.m_pOther == NULL);
224 Assert (state.m_pParentMenu == NULL);
225
226 state.m_nIndexMax = pPopupMenu->GetMenuItemCount ();
227
228 // Remove disabled items (and unneeded separators)
229 {
230 bool prevItemSep = true; // prevent initial separators
231 for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;) {
232 state.m_nID = pPopupMenu->GetMenuItemID (state.m_nIndex);
233 if (state.m_nID == 0 and prevItemSep) {
234 pPopupMenu->RemoveMenu (state.m_nIndex, MF_BYPOSITION);
235 --state.m_nIndexMax;
236 continue;
237 }
238 if (state.m_nID != 0) {
239 MENUITEMINFO mInfo;
240 memset (&mInfo, 0, sizeof (mInfo));
241 mInfo.cbSize = sizeof (mInfo);
242 mInfo.fMask = MIIM_STATE;
243 Verify (::GetMenuItemInfo (pPopupMenu->GetSafeHmenu (), state.m_nIndex, true, &mInfo));
244 if (mInfo.fState & MFS_DISABLED) {
245 pPopupMenu->RemoveMenu (state.m_nIndex, MF_BYPOSITION);
246 --state.m_nIndexMax;
247 continue;
248 }
249 }
250 prevItemSep = bool (state.m_nID == 0);
251 state.m_nIndex++;
252 }
253 // if LAST item is a separator - remove THAT
254 if (prevItemSep) {
255 pPopupMenu->RemoveMenu (state.m_nIndexMax - 1, MF_BYPOSITION);
256 }
257 }
258}
259
260#ifdef _DEBUG
261void LedItMainFrame::AssertValid () const
262{
263 inherited::AssertValid ();
264}
265
266void LedItMainFrame::Dump (CDumpContext& dc) const
267{
268 inherited::Dump (dc);
269}
270#endif //_DEBUG
#define AssertNotNull(p)
Definition Assertions.h:333
#define RequireNotNull(p)
Definition Assertions.h:347
#define Verify(c)
Definition Assertions.h:419