Skip to content

Commit f942c66

Browse files
committed
Refactor floating tool management.
1 parent 37ae07e commit f942c66

File tree

2 files changed

+131
-39
lines changed

2 files changed

+131
-39
lines changed

src/ToolDock.cpp

Lines changed: 105 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ void REHex::ToolDock::OnMotion(wxMouseEvent &event)
518518
if(dest_notebook->GetPageCount() == 1)
519519
{
520520
ResetNotebookSize(dest_notebook);
521-
dest_notebook->Show();
522521
}
523522

524523
if(frame != NULL)
@@ -550,45 +549,64 @@ void REHex::ToolDock::OnMotion(wxMouseEvent &event)
550549
frame->SetPosition(frame_pos);
551550
frame->Show();
552551

553-
ToolPanel *tool = m_left_down_tool;
552+
frame->Bind(wxEVT_CLOSE_WINDOW, &REHex::ToolDock::OnFrameClose, this);
554553

555-
#if 0
556-
frame->Bind(wxEVT_ICONIZE, [this, frame, tool](wxIconizeEvent &event)
554+
m_tool_frames.emplace(m_left_down_tool, frame);
555+
}
556+
}
557+
}
558+
559+
event.Skip();
560+
}
561+
562+
void REHex::ToolDock::OnFrameClose(wxCloseEvent &event)
563+
{
564+
ToolFrame *frame = (ToolFrame*)(event.GetEventObject());
565+
566+
ToolPanel *tool = frame->GetTool();
567+
if(tool != NULL)
568+
{
569+
frame->RemoveTool(tool);
570+
571+
ToolNotebook *dest_notebook = NULL;
572+
573+
switch(tool->shape())
574+
{
575+
case ToolPanel::Shape::TPS_WIDE:
576+
if(m_bottom_notebook->GetPageCount() > 0 || m_top_notebook->GetPageCount() == 0)
557577
{
558-
if(event.IsIconized())
559-
{
560-
tool->Reparent(m_right_notebook);
561-
m_right_notebook->AddPage(tool, tool->name(), true);
562-
563-
m_tool_frames.erase(tool);
564-
frame->Destroy();
565-
}
566-
});
567-
#endif
578+
dest_notebook = m_bottom_notebook;
579+
}
580+
else{
581+
dest_notebook = m_top_notebook;
582+
}
583+
584+
break;
568585

569-
frame->Bind(wxEVT_CLOSE_WINDOW, [this, frame, tool](wxCloseEvent &event)
586+
case ToolPanel::Shape::TPS_TALL:
587+
if(m_right_notebook->GetPageCount() > 0 || m_left_notebook->GetPageCount() == 0)
570588
{
571-
frame->GetSizer()->Detach(tool);
572-
tool->Reparent(m_right_notebook);
573-
574-
m_right_notebook->AddPage(tool, tool->name(), true);
575-
576-
if(m_right_notebook->GetPageCount() == 1)
577-
{
578-
ResetNotebookSize(m_right_notebook);
579-
m_right_notebook->Show();
580-
}
581-
582-
m_tool_frames.erase(tool);
583-
frame->Destroy();
584-
});
589+
dest_notebook = m_right_notebook;
590+
}
591+
else{
592+
dest_notebook = m_left_notebook;
593+
}
585594

586-
m_tool_frames.emplace(m_left_down_tool, frame);
587-
}
595+
break;
596+
}
597+
598+
tool->Reparent(dest_notebook);
599+
dest_notebook->AddPage(tool, tool->label(), true);
600+
601+
if(dest_notebook->GetPageCount() == 1)
602+
{
603+
ResetNotebookSize(dest_notebook);
588604
}
605+
606+
m_tool_frames.erase(tool);
589607
}
590608

591-
event.Skip();
609+
frame->Destroy();
592610
}
593611

594612
BEGIN_EVENT_TABLE(REHex::ToolDock::ToolNotebook, wxNotebook)
@@ -603,6 +621,11 @@ bool REHex::ToolDock::ToolNotebook::AddPage(wxWindow *page, const wxString &text
603621
bool res = wxNotebook::AddPage(page, text, select, imageId);
604622
UpdateToolVisibility();
605623

624+
if(GetPageCount() == 1)
625+
{
626+
Show();
627+
}
628+
606629
return res;
607630
}
608631

@@ -611,6 +634,11 @@ bool REHex::ToolDock::ToolNotebook::DeletePage(size_t page)
611634
bool res = wxNotebook::DeletePage(page);
612635
UpdateToolVisibility();
613636

637+
if(GetPageCount() == 0)
638+
{
639+
Hide();
640+
}
641+
614642
return res;
615643
}
616644

@@ -619,6 +647,11 @@ bool REHex::ToolDock::ToolNotebook::InsertPage(size_t index, wxWindow *page, con
619647
bool res = wxNotebook::InsertPage(index, page, text, select, imageId);
620648
UpdateToolVisibility();
621649

650+
if(GetPageCount() == 1)
651+
{
652+
Show();
653+
}
654+
622655
return res;
623656
}
624657

@@ -627,6 +660,11 @@ bool REHex::ToolDock::ToolNotebook::RemovePage(size_t page)
627660
bool res = wxNotebook::RemovePage(page);
628661
UpdateToolVisibility();
629662

663+
if(GetPageCount() == 0)
664+
{
665+
Hide();
666+
}
667+
630668
return res;
631669
}
632670

@@ -721,15 +759,44 @@ void REHex::ToolDock::ToolNotebook::OnPageChanged(wxNotebookEvent& event)
721759
}
722760

723761
REHex::ToolDock::ToolFrame::ToolFrame(wxWindow *parent, ToolPanel *tool):
724-
wxFrame(parent, wxID_ANY, tool->name(), wxDefaultPosition, wxDefaultSize,
725-
(wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER | wxFRAME_TOOL_WINDOW | wxFRAME_FLOAT_ON_PARENT))
762+
wxFrame(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
763+
(wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER | wxFRAME_TOOL_WINDOW | wxFRAME_FLOAT_ON_PARENT)),
764+
m_tool(NULL)
726765
{
727-
SetClientSize(tool->GetSize());
766+
m_sizer = new wxBoxSizer(wxHORIZONTAL);
767+
SetSizer(m_sizer);
768+
769+
if(tool != NULL)
770+
{
771+
AdoptTool(tool);
772+
}
773+
}
728774

775+
void REHex::ToolDock::ToolFrame::AdoptTool(ToolPanel *tool)
776+
{
777+
assert(m_tool == NULL);
778+
779+
m_tool = tool;
780+
781+
SetClientSize(tool->GetSize());
782+
729783
tool->Reparent(this);
730784
tool->Show();
785+
786+
SetTitle(tool->label());
787+
m_sizer->Add(tool, 1, wxEXPAND);
788+
}
789+
790+
void REHex::ToolDock::ToolFrame::RemoveTool(ToolPanel *tool)
791+
{
792+
assert(m_tool == tool);
793+
794+
m_tool = NULL;
795+
796+
m_sizer->Detach(tool);
797+
}
731798

732-
wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
733-
sizer->Add(tool, 1, wxEXPAND);
734-
SetSizer(sizer);
799+
REHex::ToolPanel *REHex::ToolDock::ToolFrame::GetTool() const
800+
{
801+
return m_tool;
735802
}

src/ToolDock.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <wx/config.h>
2424
#include <wx/event.h>
2525
#include <wx/notebook.h>
26+
#include <wx/sizer.h>
2627

2728
#include "MultiSplitter.hpp"
2829
#include "ToolPanel.hpp"
@@ -47,6 +48,9 @@ namespace REHex
4748
void LoadTools(wxConfig *config, SharedDocumentPointer &document, DocumentCtrl *document_ctrl);
4849

4950
private:
51+
/**
52+
* @brief wxNotebook specialisation for holding any tools docked to the main window.
53+
*/
5054
class ToolNotebook: public wxNotebook
5155
{
5256
public:
@@ -76,10 +80,30 @@ namespace REHex
7680
DECLARE_EVENT_TABLE()
7781
};
7882

83+
/**
84+
* @brief wxFrame specialisation for holding a detached/floating tool.
85+
*/
7986
class ToolFrame: public wxFrame
8087
{
8188
public:
82-
ToolFrame(wxWindow *parent, ToolPanel *tool);
89+
ToolFrame(wxWindow *parent, ToolPanel *tool = NULL);
90+
91+
void AdoptTool(ToolPanel *tool);
92+
93+
/**
94+
* @brief Remove the owned tool.
95+
*
96+
* This method must be called to detach the tool from the
97+
* floating frame before it can be re-inserted elsewhere in
98+
* the window heierarchy.
99+
*/
100+
void RemoveTool(ToolPanel *tool);
101+
102+
ToolPanel *GetTool() const;
103+
104+
private:
105+
wxBoxSizer *m_sizer;
106+
ToolPanel *m_tool;
83107
};
84108

85109
wxWindow *m_main_panel;
@@ -117,6 +141,7 @@ namespace REHex
117141
void OnLeftUp(wxMouseEvent &event);
118142
void OnMouseCaptureLost(wxMouseCaptureLostEvent &event);
119143
void OnMotion(wxMouseEvent &event);
144+
void OnFrameClose(wxCloseEvent &event);
120145

121146
DECLARE_EVENT_TABLE()
122147
};

0 commit comments

Comments
 (0)