Ember
Loading...
Searching...
No Matches
SidePanel.cpp
Go to the documentation of this file.
2#include "App/MainFrame.h"
8#include "Utils/Logger.h"
10#include <algorithm>
11#include <ctime>
12#include <fstream>
13#include <typeinfo>
14#include <wx/artprov.h>
15#include <wx/bmpbuttn.h>
16#include <wx/filename.h>
17#include <wx/menu.h>
18#include <wx/sizer.h>
19
20#ifdef HAVE_WXSVG
21#include <wxSVG/imagsvg.h>
22#include <wxSVG/svg.h>
23#endif
24
25// Event table for the side panel base class
27 EVT_AUINOTEBOOK_PAGE_CLOSE(SidePanel::ID_BASE_NOTEBOOK, SidePanel::OnTabClosed)
30
31 SidePanel::SidePanel(wxWindow *parent, MainFrame *mainFrame, const wxString &panelName,
32 EmberForge::PanelType panelType, EmberForge::PanelDescriptor *parentDescriptor)
33 : Panel(parent, panelName), m_mainFrame(mainFrame), m_notebook(nullptr), m_activeTabIndex(-1),
34 m_panelName(panelName), m_tabBarButtonsEnabled(true), m_isInitializing(true),
35 m_descriptor(panelType, parentDescriptor ? parentDescriptor->GetHierarchyLevel() + 1 : 0, parentDescriptor) {
36 LOG_INFO("Panel", std::string("Creating side panel: ") + m_panelName.ToStdString() +
37 " (Type: " + m_descriptor.GetDisplayName().ToStdString() +
38 ", Level: " + std::to_string(m_descriptor.GetHierarchyLevel()) + ")");
39 DoCreateLayout(); // Non-virtual: safe to call from constructor
40}
41
43 LOG_INFO("Panel", std::string("Destroying side panel: ") + m_panelName.ToStdString());
44
45 // CRITICAL: Remove all pages from the notebook BEFORE m_tabs is destroyed.
46 // We own the tabs (m_tabs), and the notebook also references them. If we don't
47 // remove them first, m_tabs destructor deletes them, then the notebook destructor
48 // tries to access them -> use-after-free. RemovePage() removes from the notebook
49 // without deleting; we then delete via m_tabs.
50 if (m_notebook) {
51 while (m_notebook->GetPageCount() > 0) {
52 m_notebook->RemovePage(0);
53 }
54 }
55}
56
58 DoCreateLayout(); // Virtual dispatch works when called at runtime (e.g. from Panel::Initialize)
59}
60
62 // Create main sizer
63 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
64
65 // Create components using template method pattern (non-virtual calls - safe in ctor)
68
69 // Add notebook to sizer (takes most space)
70 // The notebook now has integrated buttons in the tab bar (no separate toolbar)
71 if (m_notebook) {
72 mainSizer->Add(m_notebook, 1, wxEXPAND | wxALL, 2);
73 }
74
75 SetSizer(mainSizer);
76
77 // NOTE: OnPanelSpecificSetup() is NOT called here because virtual dispatch
78 // doesn't work during construction. Call FinishSetup() after construction instead.
79
80 LOG_INFO("Panel", std::string("Layout created for side panel: ") + m_panelName.ToStdString());
81}
82
84 // This method should be called AFTER the derived class is fully constructed
85 // to ensure proper virtual dispatch for OnPanelSpecificSetup()
86 LOG_INFO("Panel", std::string("Calling OnPanelSpecificSetup() for ") + m_panelName.ToStdString() +
87 " (type: " + std::string(typeid(*this).name()) + ")");
89 LOG_INFO("Panel", std::string("OnPanelSpecificSetup() completed for ") + m_panelName.ToStdString());
90
91 // Debug: Check if any tabs were created
92 if (m_notebook) {
93 LOG_INFO("Panel", std::string("Notebook has ") + std::to_string(m_notebook->GetPageCount()) +
94 " pages after OnPanelSpecificSetup()");
95 }
96}
97
99 DoCreateNotebook(); // Virtual dispatch works when called at runtime
100}
101
103 // Create notebook with disabled tab dragging (as per original LeftSidePanel)
104 long style = wxAUI_NB_DEFAULT_STYLE;
105 style &= ~wxAUI_NB_TAB_MOVE; // Disable tab moving
106 style &= ~wxAUI_NB_TAB_EXTERNAL_MOVE; // Disable external tab moving
107 style &= ~wxAUI_NB_TAB_SPLIT; // Disable tab splitting
108
109 // Use CustomAuiNotebook which has integrated buttons in the tab bar
110 m_notebook = new EmberForge::CustomAuiNotebook(this, ID_BASE_NOTEBOOK, wxDefaultPosition, wxDefaultSize, style);
111
112 // Bind to the custom tab bar button events
113 Bind(EmberForge::EVT_TAB_ART_ADD_BUTTON, &SidePanel::OnTabBarAddButtonClicked, this);
114 Bind(EmberForge::EVT_TAB_ART_MENU_BUTTON, &SidePanel::OnTabBarMenuButtonClicked, this);
115
116 LOG_TRACE("Panel", std::string("Created CustomAuiNotebook for ") + m_panelName.ToStdString() +
117 " with integrated tab bar buttons");
118}
119
121 DoSetupEventHandlers(); // Virtual dispatch works when called at runtime
122}
123
125 // Event handlers are set up via the event table and Bind() in DoCreateNotebook()
126 // This method is available for derived classes to add additional handlers
127 LOG_TRACE("Panel", std::string("Event handlers set up for %s") + m_panelName.ToStdString());
128}
129
131 // Default implementation - derived classes can override this for additional setup
132 LOG_INFO("Panel", std::string("Base OnPanelSpecificSetup() called for %s") + m_panelName.ToStdString());
133}
134
136 LOG_INFO("Panel", std::string("*** RestoreAndEnsureTabs() called for ") + m_panelName.ToStdString() + " ***");
137
138 // Try to restore tabs from saved state if rememberLastTab is enabled
139 if (GetRememberLastTab()) {
140 const auto &lastOpenTabs = GetLastOpenTabs();
141 if (!lastOpenTabs.empty()) {
142 LOG_INFO("Panel", wxString::Format("Restoring %zu saved tabs", lastOpenTabs.size()).ToStdString());
143 for (const auto &tabTypeStr : lastOpenTabs) {
145 CreateAndAddTab(tabType);
146 }
147 }
148 }
149
150 // Ensure at least one default tab exists (solves the empty tab bar problem)
151 // If the preferred default tab fails (e.g., due to global instance limits), try alternatives
152 if (!GetNotebook() || GetNotebook()->GetPageCount() == 0) {
153 LOG_INFO("Panel", "No tabs exist, trying to create a default tab");
154
155 auto supportedTypes = GetSupportedTabTypes();
156 for (const auto &tabType : supportedTypes) {
157 size_t countBefore = GetNotebook() ? GetNotebook()->GetPageCount() : 0;
158 CreateAndAddTab(tabType);
159 size_t countAfter = GetNotebook() ? GetNotebook()->GetPageCount() : 0;
160
161 if (countAfter > countBefore) {
162 LOG_INFO("Panel", wxString::Format("Created fallback tab: %s",
164 .ToStdString());
165 break;
166 }
167 }
168
169 if (GetNotebook() && GetNotebook()->GetPageCount() == 0) {
170 LOG_ERROR("Panel", std::string("Failed to create any default tab for ") + m_panelName.ToStdString() +
171 " - all supported types at max instances");
172 }
173 }
174
175 // Apply panel-specific preferences
177
178 // Restore the active tab based on preferences
179 if (GetNotebook() && GetNotebook()->GetPageCount() > 0) {
180 std::string tabToActivate = GetRememberLastTab() ? GetLastActiveTab() : GetDefaultActiveTab();
181
183 wxString targetTabName = EmberForge::TabFactory::GetTabDisplayName(targetTabType);
184
185 int targetIndex = 0;
186 for (size_t i = 0; i < GetNotebook()->GetPageCount(); ++i) {
187 if (GetNotebook()->GetPageText(i) == targetTabName) {
188 targetIndex = static_cast<int>(i);
189 break;
190 }
191 }
192
193 GetNotebook()->SetSelection(targetIndex);
194 LOG_INFO("Panel", wxString::Format("Set active tab to '%s' (index %d) [rememberLastTab=%s], total tabs: %d",
195 targetTabName, targetIndex, GetRememberLastTab() ? "true" : "false",
196 static_cast<int>(GetNotebook()->GetPageCount()))
197 .ToStdString());
198 }
199
200 LOG_INFO("Panel", std::string("RestoreAndEnsureTabs() completed for ") + m_panelName.ToStdString());
201}
202
203// Tab management implementation
205 if (!tab || !m_notebook) {
206 LOG_ERROR("Panel", std::string("Cannot add null tab to %s") + m_panelName.ToStdString());
207 return -1;
208 }
209
210 // Add tab to notebook
211 wxWindow *tabWindow = tab->GetWidget();
212 wxString tabTitle = tab->GetTitle();
213
214 // AddPage returns bool, not index. We need to get the index after adding.
215 bool success = m_notebook->AddPage(tabWindow, tabTitle, true);
216 int index = success ? static_cast<int>(m_notebook->GetPageCount()) - 1 : -1;
217
218 if (success) {
219 // Initialize the tab after adding it to the notebook
220 tab->Initialize();
221
222 m_tabs.push_back(std::move(tab));
223 m_activeTabIndex = index;
224 LOG_INFO("Panel", std::string("Added tab '") + tabTitle.ToStdString() + "' to " + m_panelName.ToStdString() +
225 " at index " + std::to_string(index));
226
227 // Save the updated tab state immediately so it persists
228 SaveState();
229
230 // Persist preferences to disk
232 std::string configPath = EmberForge::AppPreferences::GetDefaultConfigPath();
233
234 if (prefManager.GetPreferences().SaveToFile(configPath)) {
235 LOG_INFO("Panel", "Saved tab state to disk after tab addition");
236 } else {
237 LOG_ERROR("Panel", "Failed to save tab state to disk after tab addition");
238 }
239 } else {
240 LOG_ERROR("Panel",
241 std::string("Failed to add tab '") + tabTitle.ToStdString() + "' to " + m_panelName.ToStdString());
242 }
243 return index;
244}
245
247 if (index < 0 || index >= static_cast<int>(m_tabs.size()) || !m_notebook) {
248 LOG_ERROR("Panel",
249 std::string("Invalid tab index ") + std::to_string(index) + " for " + m_panelName.ToStdString());
250 return nullptr;
251 }
252
253 // Remove from notebook
254 if (m_notebook->RemovePage(index)) {
255 // Remove from our vector and return it
256 ITabPtr removedTab = std::move(m_tabs[index]);
257 m_tabs.erase(m_tabs.begin() + index);
258
259 // Call OnClosed() to allow the tab to clean up (remove observers, notify MainFrame, etc.)
260 // This is critical for tabs like SceneHierarchyTab and PropertiesTab which need to notify
261 // MainFrame to null out their pointers, and LogTab which needs to remove its observer.
262 if (removedTab) {
263 removedTab->OnClosed();
264 }
265
266 // Update active tab index
267 if (m_activeTabIndex == index) {
268 m_activeTabIndex = m_tabs.empty() ? -1 : std::min(m_activeTabIndex, static_cast<int>(m_tabs.size()) - 1);
269 } else if (m_activeTabIndex > index) {
271 }
272
273 LOG_INFO("Panel",
274 std::string("Removed tab at index ") + std::to_string(index) + " from " + m_panelName.ToStdString());
275 return removedTab;
276 }
277
278 LOG_ERROR("Panel", std::string("Failed to remove tab at index ") + std::to_string(index) + " from " +
279 m_panelName.ToStdString());
280 return nullptr;
281}
282
283ITab *SidePanel::GetTab(int index) const {
284 if (index < 0 || index >= static_cast<int>(m_tabs.size())) {
285 return nullptr;
286 }
287 return m_tabs[index].get();
288}
289
290bool SidePanel::SetActiveTab(int index) {
291 if (index < 0 || index >= static_cast<int>(m_tabs.size()) || !m_notebook) {
292 return false;
293 }
294
295 m_notebook->SetSelection(index);
296 m_activeTabIndex = index;
297
298 LOG_TRACE("Panel",
299 std::string("Set active tab to index ") + std::to_string(index) + " in " + m_panelName.ToStdString());
300 return true;
301}
302
304 if (m_activeTabIndex < 0 || m_activeTabIndex >= static_cast<int>(m_tabs.size())) {
305 return nullptr;
306 }
307 return m_tabs[m_activeTabIndex].get();
308}
309
311 if (!m_notebook) {
312 return;
313 }
314
315 wxString defaultTabName = EmberForge::TabFactory::GetTabDisplayName(defaultTabType);
316 LOG_INFO("Panel", std::string("Resetting ") + m_panelName.ToStdString() +
317 " to default tab: " + defaultTabName.ToStdString());
318
319 // Find if the default tab already exists - track by window pointer, not index
320 // (indices shift as tabs are removed, but the window pointer remains stable)
321 wxWindow *defaultTabWindow = nullptr;
322 for (size_t i = 0; i < m_notebook->GetPageCount(); ++i) {
323 if (m_notebook->GetPageText(i) == defaultTabName) {
324 defaultTabWindow = m_notebook->GetPage(i);
325 break;
326 }
327 }
328
329 // Close all tabs except the default one (if it exists)
330 // Work backwards to avoid issues with index shifting during removal
331 for (int i = static_cast<int>(m_notebook->GetPageCount()) - 1; i >= 0; --i) {
332 if (m_notebook->GetPage(i) != defaultTabWindow) {
333 RemoveTab(i);
334 }
335 }
336
337 // If default tab didn't exist, create it
338 if (defaultTabWindow == nullptr) {
339 CreateAndAddTab(defaultTabType);
340 }
341
342 // Ensure the default tab is selected
343 if (m_notebook->GetPageCount() > 0) {
344 m_notebook->SetSelection(0);
346 }
347
348 LOG_INFO("Panel", std::string("Reset ") + m_panelName.ToStdString() +
349 " complete, tab count: " + std::to_string(m_notebook->GetPageCount()));
350}
351
352void SidePanel::ResetToDefaultTabs(const std::vector<EmberForge::TabType> &defaultTabTypes) {
353 if (!m_notebook || defaultTabTypes.empty()) {
354 return;
355 }
356
357 LOG_INFO("Panel", std::string("Resetting ") + m_panelName.ToStdString() + " to " +
358 std::to_string(defaultTabTypes.size()) + " default tabs");
359
360 // Close all existing tabs
361 while (m_notebook->GetPageCount() > 0) {
362 RemoveTab(0);
363 }
364
365 // Create all the default tabs
366 for (const auto &tabType : defaultTabTypes) {
367 CreateAndAddTab(tabType);
368 }
369
370 // Select the first tab
371 if (m_notebook->GetPageCount() > 0) {
372 m_notebook->SetSelection(0);
374 }
375
376 LOG_INFO("Panel", std::string("Reset ") + m_panelName.ToStdString() +
377 " complete, tab count: " + std::to_string(m_notebook->GetPageCount()));
378}
379
381 if (!m_notebook) {
382 return;
383 }
384
385 LOG_INFO("Panel", std::string("Clearing all tabs from ") + m_panelName.ToStdString());
386
387 while (m_notebook->GetPageCount() > 0) {
388 RemoveTab(0);
389 }
390
391 m_activeTabIndex = -1;
392}
393
394// Event handlers
395void SidePanel::OnTabChanged(wxAuiNotebookEvent &event) {
396 int newSelection = event.GetSelection();
397 if (newSelection != wxNOT_FOUND) {
398 m_activeTabIndex = newSelection;
399 LOG_TRACE("Panel", std::string("Tab changed to index ") + std::to_string(newSelection) + " in " +
400 m_panelName.ToStdString());
401
402 // Save the updated active tab state immediately
403 SaveState();
404
405 // Don't save to disk during initialization
406 if (!m_isInitializing) {
407 // Persist preferences to disk
409 std::string configPath = EmberForge::AppPreferences::GetDefaultConfigPath();
410 if (prefManager.GetPreferences().SaveToFile(configPath)) {
411 LOG_TRACE("Panel", "Saved active tab state to disk after tab change");
412 } else {
413 LOG_ERROR("Panel", "Failed to save active tab state to disk after tab change");
414 }
415 }
416 }
417 event.Skip();
418}
419
420void SidePanel::OnTabClosed(wxAuiNotebookEvent &event) {
421 int closedIndex = event.GetSelection();
422 LOG_INFO("Panel", std::string("Tab at index ") + std::to_string(closedIndex) + " closed");
423
424 // IMPORTANT: Clean up the tab BEFORE releasing ownership!
425 // This ensures observers, callbacks, etc. are unregistered before the object is destroyed.
426 if (closedIndex >= 0 && closedIndex < static_cast<int>(m_tabs.size())) {
427 // Call OnClosed() to allow the tab to clean up (remove observers, etc.)
428 if (m_tabs[closedIndex]) {
429 m_tabs[closedIndex]->OnClosed();
430 }
431 // Release ownership - wxAuiNotebook will delete the window
432 m_tabs[closedIndex].release();
433 }
434
435 // Use CallAfter to clean up the vector entry after wxWidgets finishes processing
436 CallAfter([this, closedIndex]() {
437 if (closedIndex >= 0 && closedIndex < static_cast<int>(m_tabs.size())) {
438 m_tabs.erase(m_tabs.begin() + closedIndex);
439
440 // Update active tab index
441 if (m_activeTabIndex == closedIndex) {
443 m_tabs.empty() ? -1 : std::min(m_activeTabIndex, static_cast<int>(m_tabs.size()) - 1);
444 } else if (m_activeTabIndex > closedIndex) {
446 }
447
448 // Save the updated tab state immediately so it persists
449 SaveState();
450
451 // Persist preferences to disk
453 std::string configPath = EmberForge::AppPreferences::GetDefaultConfigPath();
454 if (prefManager.GetPreferences().SaveToFile(configPath)) {
455 LOG_INFO("Panel", "Saved tab state to disk after tab removal");
456 } else {
457 LOG_ERROR("Panel", "Failed to save tab state to disk after tab removal");
458 }
459 }
460 });
461
462 event.Skip();
463}
464void SidePanel::OnAddTabButtonClickedInternal(wxCommandEvent &event) {
465 LOG_INFO("Panel", std::string("Add tab button clicked in %s") + m_panelName.ToStdString());
466
467 // Call derived class implementation first
469
470 // If event was skipped (not handled by derived class), show default menu
471 if (event.GetSkipped()) {
473 }
474}
475
476void SidePanel::OnTabBarAddButtonClicked(wxCommandEvent &event) {
477 LOG_INFO("Panel", std::string("Tab bar add button clicked in %s") + m_panelName.ToStdString());
478
479 // Reuse the existing add tab logic
480 wxCommandEvent addEvent;
482}
483
484void SidePanel::OnTabBarMenuButtonClicked(wxCommandEvent &event) {
485 LOG_INFO("Panel", std::string("Tab bar menu button clicked in %s") + m_panelName.ToStdString());
486
487 // Reuse the existing menu logic
488 wxCommandEvent menuEvent;
489 OnMenuButtonClicked(menuEvent);
490}
491
492void SidePanel::OnMenuButtonClicked(wxCommandEvent &event) {
493 LOG_INFO("Panel", std::string("Menu button clicked in %s") + m_panelName.ToStdString());
494
495 // Create the main popup menu
496 wxMenu *mainMenu = new wxMenu();
497
498 // Add "Hide Panel" option
499 mainMenu->Append(ID_BASE_HIDE_PANEL_MENU_ITEM, "Hide Panel", "Hide this panel from view");
500
501 // Add separator
502 mainMenu->AppendSeparator();
503
504 // Add "Preferences" option
505 mainMenu->Append(ID_BASE_PREFERENCES_MENU_ITEM, "Preferences...", "Open preferences for this panel");
506
507 // Show the popup menu at the button location
508 PopupMenu(mainMenu);
509
510 // Clean up
511 delete mainMenu;
512}
513
514void SidePanel::OnHidePanelClicked(wxCommandEvent &event) {
515 LOG_INFO("Panel", std::string("Hide panel clicked in %s") + m_panelName.ToStdString());
516
517 if (!m_mainFrame) {
518 LOG_ERROR("Panel", "Cannot hide panel - no MainFrame reference");
519 return;
520 }
521
522 // Get the AUI manager from MainFrame and hide this panel
523 wxAuiManager *auiManager = m_mainFrame->GetAuiManager();
524 if (!auiManager) {
525 LOG_ERROR("Panel", "Cannot hide panel - no AUI manager");
526 return;
527 }
528
529 // Get the pane name from the descriptor
530 wxString paneName = m_descriptor.GetPaneName();
531
532 wxAuiPaneInfo &pane = auiManager->GetPane(paneName);
533 if (pane.IsOk()) {
534 pane.Hide();
535 auiManager->Update();
536
537 // Update the toolbar toggle button icons to reflect the new state
538 m_mainFrame->UpdatePanelToggleButtons();
539
540 LOG_INFO("Panel", std::string("Panel hidden: ") + m_panelName.ToStdString());
541 }
542}
543
544void SidePanel::OnPanelPreferencesClicked(wxCommandEvent &event) {
545 LOG_INFO("Panel", std::string("Preferences clicked in %s") + m_panelName.ToStdString());
546
547 if (!m_mainFrame) {
548 LOG_ERROR("Panel", "Cannot open preferences - no MainFrame reference");
549 return;
550 }
551
552 // Get the display name from the descriptor (matches the tree section names)
553 wxString sectionName = m_descriptor.GetDisplayName();
554
555 // Open the preferences dialog and navigate to the panel's settings
557 dialog.SelectSection(sectionName);
558
559 int result = dialog.ShowModal();
560 if (result == wxID_OK) {
561 LOG_INFO("Panel", std::string("Preferences applied for ") + m_panelName.ToStdString());
562 // Notify MainFrame to apply preferences
563 m_mainFrame->RefreshPreferences();
564 }
565}
566
567// Helper methods
568bool SidePanel::HasTabWithName(const wxString &tabName) const {
569 for (const auto &tab : m_tabs) {
570 if (tab && tab->GetTitle() == tabName) {
571 return true;
572 }
573 }
574 return false;
575}
576
578 if (!m_notebook || !m_mainFrame) {
579 LOG_ERROR("Panel",
580 std::string("Cannot create tab - missing notebook or mainframe in %s") + m_panelName.ToStdString());
581 return;
582 }
583
584 wxString tabName = EmberForge::TabFactory::GetTabDisplayName(tabType);
585 EmberForge::PanelType panelType = m_descriptor.GetType();
586
587 // Check if this tab type is allowed in this panel
588 if (!EmberForge::TabFactory::IsTabAllowedInPanel(tabType, panelType)) {
589 LOG_WARNING("Panel",
590 std::string("Tab '") + tabName.ToStdString() + "' is not allowed in " + m_panelName.ToStdString());
591 return;
592 }
593
594 // Check if tab already exists in this panel (per-panel check)
596 LOG_WARNING("Panel",
597 std::string("Tab '") + tabName.ToStdString() + "' already exists in " + m_panelName.ToStdString());
598 return;
599 }
600
601 // Check global instance limit
602 int maxInstances = EmberForge::TabFactory::GetMaxGlobalInstances(tabType);
603 if (maxInstances > 0) {
604 int currentCount = m_mainFrame->CountTabGlobally(tabName);
605 if (currentCount >= maxInstances) {
606 LOG_WARNING("Panel", std::string("Tab '") + tabName.ToStdString() + "' has reached max instances (" +
607 std::to_string(maxInstances) + ")");
608 return;
609 }
610 }
611
612 // Create and add the tab
614 if (tab) {
615 AddTab(std::move(tab));
616 LOG_INFO("Panel",
617 std::string("Created and added tab '") + tabName.ToStdString() + "' to " + m_panelName.ToStdString());
618 } else {
619 LOG_ERROR("Panel",
620 std::string("Failed to create tab '") + tabName.ToStdString() + "' for " + m_panelName.ToStdString());
621 }
622}
623
625 auto supportedTypes = GetSupportedTabTypes();
626 if (supportedTypes.empty()) {
627 LOG_WARNING("Panel", std::string("No supported tab types for %s") + m_panelName.ToStdString());
628 return;
629 }
630
631 wxMenu menu;
632 EmberForge::PanelType panelType = m_descriptor.GetType();
633 int validItems = 0;
634
635 // Add menu items for each supported tab type
636 for (size_t i = 0; i < supportedTypes.size(); ++i) {
637 EmberForge::TabType tabType = supportedTypes[i];
638
639 // Skip tabs not allowed in this panel type
640 if (!EmberForge::TabFactory::IsTabAllowedInPanel(tabType, panelType)) {
641 continue;
642 }
643
644 wxString displayName = EmberForge::TabFactory::GetTabDisplayName(tabType);
645 wxString tooltip = EmberForge::TabFactory::GetTabTooltip(tabType);
646
647 int menuId = ID_BASE_LAST + static_cast<int>(i);
648 wxMenuItem *item = menu.Append(menuId, displayName, tooltip);
649 validItems++;
650
651 // Check if tab already exists in this panel
652 bool existsInThisPanel =
654
655 // Check global instance limit
656 int maxInstances = EmberForge::TabFactory::GetMaxGlobalInstances(tabType);
657 int currentCount = m_mainFrame->CountTabGlobally(displayName);
658 bool atMaxInstances = (maxInstances > 0) && (currentCount >= maxInstances);
659
660 if (existsInThisPanel || atMaxInstances) {
661 item->Enable(false);
662 if (atMaxInstances && !existsInThisPanel) {
663 if (maxInstances == 1) {
664 item->SetItemLabel(displayName + " (open in another panel)");
665 } else {
666 item->SetItemLabel(displayName + " (max " + std::to_string(maxInstances) + " reached)");
667 }
668 }
669 }
670
671 // Bind the event to the menu object, not to 'this'. When the menu goes out
672 // of scope after PopupMenu returns, its event handlers are automatically
673 // cleaned up, preventing handler accumulation across invocations.
674 menu.Bind(
675 wxEVT_COMMAND_MENU_SELECTED, [this, tabType](wxCommandEvent &) { CreateAndAddTab(tabType); }, menuId);
676 }
677
678 if (validItems == 0) {
679 wxMenuItem *item = menu.Append(wxID_ANY, "(No tabs available for this panel)");
680 item->Enable(false);
681 }
682
683 // Show the menu (blocks until menu is dismissed, then menu is destroyed)
684 PopupMenu(&menu);
685
686 LOG_TRACE("Panel", std::string("Showed tab creation menu for ") + m_panelName.ToStdString() + " with " +
687 std::to_string(validItems) + " options");
688}
689
690// Tab bar button control methods
692 if (m_tabBarButtonsEnabled == enabled) {
693 return; // No change needed
694 }
695
696 m_tabBarButtonsEnabled = enabled;
697
698 if (m_notebook) {
699 m_notebook->SetTabBarButtonsEnabled(enabled);
700 LOG_INFO("Panel", std::string("Tab bar buttons ") + (enabled ? "enabled" : "disabled") + " for " +
701 m_panelName.ToStdString());
702 }
703}
704
707 LOG_INFO("Panel", std::string("Tab bar buttons toggled to ") + (m_tabBarButtonsEnabled ? "enabled" : "disabled") +
708 " for " + m_panelName.ToStdString());
709}
710
712 // Base implementation does nothing - SidePanelSettings is now reserved for global settings
713 // Derived classes (LeftSidePanel, RightSidePanel, BottomPanel) override this to use their specific settings
714 LOG_INFO("SidePanel", std::string("Base ApplyPreferences called for ") + m_panelName.ToStdString() +
715 " - derived class should override this");
716}
717
719 // Base implementation does nothing - SidePanelSettings is now reserved for global settings
720 // Derived classes (LeftSidePanel, RightSidePanel, BottomPanel) override this to use their specific settings
721 LOG_INFO("SidePanel", std::string("Base SaveState called for ") + m_panelName.ToStdString() +
722 " - derived class should override this");
723}
724
726 // Base implementation does nothing - SidePanelSettings is now reserved for global settings
727 // Derived classes (LeftSidePanel, RightSidePanel, BottomPanel) override this to use their specific settings
728 LOG_INFO("SidePanel", std::string("Base RestoreState called for ") + m_panelName.ToStdString() +
729 " - derived class should override this");
730}
BehaviorTreeProjectDialog::OnProjectNameChanged BehaviorTreeProjectDialog::OnRemoveFiles wxEND_EVENT_TABLE() BehaviorTreeProjectDialog
std::unique_ptr< ITab > ITabPtr
Smart pointer type for ITab instances.
Definition ITab.h:145
#define LOG_ERROR(category, message)
Definition Logger.h:116
#define LOG_TRACE(category, message)
Definition Logger.h:113
#define LOG_WARNING(category, message)
Definition Logger.h:115
#define LOG_INFO(category, message)
Definition Logger.h:114
MainFrame::OnExit EVT_MENU(wxID_ABOUT, MainFrame::OnAbout) EVT_MENU(ID_NewProject
Centralized resource path management for EmberForge.
wxBEGIN_EVENT_TABLE(SidePanel, Panel) EVT_AUINOTEBOOK_PAGE_CHANGED(SidePanel
Definition SidePanel.cpp:26
static AppPreferencesManager & GetInstance()
static EmberCore::String GetDefaultConfigPath()
Custom wxAuiNotebook that integrates Add Tab and Menu buttons into the tab bar.
static wxString GetTabDisplayName(TabType type)
Get the display name for a tab type.
static wxString GetTabTooltip(TabType type)
Get the tooltip text for a tab type.
static std::unique_ptr< ITab > CreateTab(TabType type, wxWindow *parent, MainFrame *mainFrame=nullptr)
Create a tab of the specified type.
static int GetMaxGlobalInstances(TabType type)
Get the maximum number of instances allowed globally across all panels.
static bool IsTabAllowedInPanel(TabType tabType, EmberForge::PanelType panelType)
Check if a tab type is allowed in a specific panel type.
static bool AllowMultipleInstances(TabType type)
Check if a tab type can be created multiple times within a single panel.
static TabType StringToTabType(const std::string &str)
Convert string identifier to tab type.
Abstract interface for individual tabs that can be moved between panels.
Definition ITab.h:28
Main application window for EmberForge.
Definition MainFrame.h:65
Base implementation of IPanel interface.
Definition Panel.h:28
Preferences dialog for configuring EmberForge application settings.
bool SelectSection(const wxString &sectionName)
Select a section in the preferences tree by name.
Base class for sidebar panels with shared tab management backend.
Definition SidePanel.h:33
void SetTabBarButtonsEnabled(bool enabled)
Enable or disable the tab bar buttons (+ and menu)
@ ID_BASE_NOTEBOOK
Definition SidePanel.h:246
@ ID_BASE_PREFERENCES_MENU_ITEM
Definition SidePanel.h:248
@ ID_BASE_HIDE_PANEL_MENU_ITEM
Definition SidePanel.h:247
void FinishSetup()
Complete panel setup after construction.
Definition SidePanel.cpp:83
virtual void ApplyPreferences()
Apply preferences to the side panel Virtual to allow derived classes to use their specific settings.
void DoCreateNotebook()
ITabPtr RemoveTab(int index)
Remove a tab by index.
void OnTabChanged(wxAuiNotebookEvent &event)
virtual const std::vector< std::string > & GetLastOpenTabs() const =0
std::vector< ITabPtr > m_tabs
Definition SidePanel.h:237
void ClearAllTabs()
Clear all tabs from the panel Removes all tabs without creating any new ones.
void OnHidePanelClicked(wxCommandEvent &event)
bool HasTabWithName(const wxString &tabName) const
Check if this panel has a tab with the given name.
int AddTab(ITabPtr tab)
Add a tab to this panel.
void DoCreateLayout()
Definition SidePanel.cpp:61
void ResetToDefaultTab(EmberForge::TabType defaultTabType)
Reset the panel to a single default tab Closes all tabs and creates only the specified default tab.
void RestoreAndEnsureTabs()
bool m_tabBarButtonsEnabled
Definition SidePanel.h:240
virtual void CreateNotebook()
Definition SidePanel.cpp:98
virtual void SetupEventHandlers()
ITab * GetActiveTab() const
Get the currently active tab.
virtual ~SidePanel()
Definition SidePanel.cpp:42
void OnPanelPreferencesClicked(wxCommandEvent &event)
virtual void OnMenuButtonClicked(wxCommandEvent &event)
virtual void OnPanelSpecificSetup()
void DoSetupEventHandlers()
void OnAddTabButtonClickedInternal(wxCommandEvent &event)
MainFrame * m_mainFrame
Definition SidePanel.h:235
void OnTabClosed(wxAuiNotebookEvent &event)
virtual std::string GetLastActiveTab() const =0
int m_activeTabIndex
Definition SidePanel.h:238
EmberForge::CustomAuiNotebook * GetNotebook() const
Get the notebook widget for direct access.
Definition SidePanel.h:98
void ShowTabCreationMenu()
wxString m_panelName
Definition SidePanel.h:239
void ToggleTabBarButtons()
Toggle tab bar buttons visibility.
virtual void OnAddTabButtonClicked(wxCommandEvent &event)
Definition SidePanel.h:211
virtual void RestoreState()
Restore state from preferences Virtual to allow derived classes to use their specific settings.
void OnTabBarMenuButtonClicked(wxCommandEvent &event)
bool SetActiveTab(int index)
Set the active tab by index.
virtual bool GetRememberLastTab() const =0
void CreateAndAddTab(EmberForge::TabType tabType)
virtual void SaveState()
Save the current state (last active tab, etc.) Virtual to allow derived classes to use their specific...
EmberForge::PanelDescriptor m_descriptor
Definition SidePanel.h:242
void ResetToDefaultTabs(const std::vector< EmberForge::TabType > &defaultTabTypes)
Reset the panel to multiple default tabs Closes all tabs and creates the specified default tabs.
bool m_isInitializing
Definition SidePanel.h:241
virtual std::vector< EmberForge::TabType > GetSupportedTabTypes() const
Definition SidePanel.h:212
EmberForge::CustomAuiNotebook * m_notebook
Definition SidePanel.h:236
void OnTabBarAddButtonClicked(wxCommandEvent &event)
void CreateLayout() override
Create the panel's UI layout Called during initialization.
Definition SidePanel.cpp:57
virtual std::string GetDefaultActiveTab() const =0
ITab * GetTab(int index) const
Get a tab by index.
PanelType
Defines the type of panel in the UI hierarchy.
TabType
Enumeration of available tab types.
Definition TabFactory.h:17