File: DeclarationSelectorDialog.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (155 lines) | stat: -rw-r--r-- 4,479 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "DeclarationSelectorDialog.h"

#include "DeclarationSelector.h"
#include <wx/button.h>
#include <wx/sizer.h>

#include "i18n.h"
#include "ideclmanager.h"
#include "iregistry.h"

namespace wxutil
{

DeclarationSelectorDialog::DeclarationSelectorDialog(decl::Type declType, 
    const std::string& title, const std::string& windowName, wxWindow* parent) :
    DialogBase(title, parent, windowName),
    _declType(declType),
    _selector(nullptr),
    _mainSizer(nullptr),
    _buttonSizer(nullptr),
    _restoreSelectionFromRegistry(true)
{
    SetSizer(new wxBoxSizer(wxVERTICAL));

    // Inner sizer with 12-pixel padding
    _mainSizer = new wxBoxSizer(wxVERTICAL);
    GetSizer()->Add(_mainSizer, 1, wxEXPAND | wxALL, 12);

    // Bottom row
    auto grid = new wxFlexGridSizer(1, 2, 0, 12);
    grid->AddGrowableCol(0);
    grid->AddGrowableCol(1);

    // Left half
    _bottomRowSizer = new wxBoxSizer(wxHORIZONTAL);
    grid->Add(_bottomRowSizer, 1, wxALIGN_LEFT);

    // Right half contains the buttons
    _buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);

    // Add a Reload Decls button
    _reloadDeclsButton = new wxButton(this, wxID_ANY, _("Reload Decls"));
    _reloadDeclsButton->Bind(wxEVT_BUTTON, &DeclarationSelectorDialog::onReloadDecls, this);
    _buttonSizer->Prepend(_reloadDeclsButton, 0, wxLEFT | wxALIGN_CENTER_VERTICAL | wxRIGHT, 12);

    grid->Add(_buttonSizer, 0, wxALIGN_RIGHT);

    _mainSizer->Add(grid, 0, wxEXPAND, 12);

    // Save the state of this dialog on close
    RegisterPersistableObject(this);
}

void DeclarationSelectorDialog::SetSelector(DeclarationSelector* selector)
{
    if (_selector != nullptr)
    {
        throw std::logic_error("There's already a selector attached to this dialog");
    }

    _selector = selector;
    _selector->Reparent(this);

    _mainSizer->Prepend(_selector, 1, wxEXPAND | wxBOTTOM, 12);

    // Update the affirmative button's sensitivity based on the selection
    _selector->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &DeclarationSelectorDialog::onDeclSelectionChanged, this);
    _selector->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &DeclarationSelectorDialog::onDeclItemActivated, this);

    // The selector state should be persisted on dialog close
    RegisterPersistableObject(_selector);
}

void DeclarationSelectorDialog::AddItemToBottomRow(wxWindow* widget)
{
    _bottomRowSizer->Prepend(widget, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
}

void DeclarationSelectorDialog::AddItemToBottomRow(wxSizer* sizer)
{
    _bottomRowSizer->Prepend(sizer, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
}

std::string DeclarationSelectorDialog::GetSelectedDeclName()
{
    return _selector->GetSelectedDeclName();
}

void DeclarationSelectorDialog::SetSelectedDeclName(const std::string& declName)
{
    _selector->SetSelectedDeclName(declName);
    _restoreSelectionFromRegistry = false; // prevent this selection from being overwritten
}

int DeclarationSelectorDialog::ShowModal()
{
    if (_selector == nullptr)
    {
        throw std::logic_error("Cannot run the DeclarationSelectorDialog without selector");
    }

    HandleTreeViewSelectionChanged();

    _selector->FocusTreeView();

    return DialogBase::ShowModal();
}

wxButton* DeclarationSelectorDialog::GetAffirmativeButton()
{
    return _buttonSizer->GetAffirmativeButton();
}

void DeclarationSelectorDialog::HandleTreeViewSelectionChanged()
{
    GetAffirmativeButton()->Enable(!_selector->GetSelectedDeclName().empty());
}

void DeclarationSelectorDialog::onDeclSelectionChanged(wxDataViewEvent&)
{
    HandleTreeViewSelectionChanged();
}

void DeclarationSelectorDialog::onDeclItemActivated(wxDataViewEvent&)
{
    // Double-clicking a valid decl item positively closes the dialog
    if (!_selector->GetSelectedDeclName().empty())
    {
        EndModal(wxID_OK);
    }
}

void DeclarationSelectorDialog::onReloadDecls(wxCommandEvent& ev)
{
    GlobalDeclarationManager().reloadDeclarations();
}

void DeclarationSelectorDialog::loadFromPath(const std::string& registryKey)
{
    if (!_restoreSelectionFromRegistry) return;

    auto lastSelectedDeclName = GlobalRegistry().getAttribute(registryKey, "lastSelectedDeclName");

    if (!lastSelectedDeclName.empty())
    {
        SetSelectedDeclName(lastSelectedDeclName);
    }
}

void DeclarationSelectorDialog::saveToPath(const std::string& registryKey)
{
    GlobalRegistry().setAttribute(registryKey, "lastSelectedDeclName", GetSelectedDeclName());
}

}