• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

antonvw / wex / 26306568010

22 May 2026 07:01PM UTC coverage: 64.787% (+0.01%) from 64.774%
26306568010

push

github

web-flow
notebook improvements (#1258)

18952 of 32142 branches covered (58.96%)

Branch coverage included in aggregate %.

15142 of 20483 relevant lines covered (73.92%)

1529.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.58
/src/ui/notebook.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
// Name:      notebook.cpp
3
// Purpose:   Implementation of class wex::notebook
4
// Author:    Anton van Wezenbeek
5
// Copyright: (c) 2021-2026 Anton van Wezenbeek
6
////////////////////////////////////////////////////////////////////////////////
7

8
#include <wex/core/log.h>
9
#include <wex/ui/item-dialog.h>
10
#include <wex/ui/item-vector.h>
11
#include <wex/ui/notebook.h>
12
#include <wx/settings.h>
13

14
#define PAGE_DATA                                                              \
15
  data.page(), (data.caption().empty() ? data.key() : data.caption()),         \
16
    data.select(), data.bitmap()
17

18
namespace wex
19
{
20
const std::vector<item> notebook_config_items()
45✔
21
{
22
  return std::vector<item>({
23
    {_("tab.Font"),
45✔
24
     item::FONTPICKERCTRL,
25
     wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)},
90✔
26
  });
270!
27
};
90!
28
} // namespace wex
29

30
wex::notebook::notebook(const data::window& data)
45✔
31
  : wxAuiNotebook(
32
      data.parent(),
33
      data.id(),
34
      data.pos(),
35
      data.size(),
36
      data.style() == data::NUMBER_NOT_SET ? default_style_t : data.style())
44✔
37
  , m_frame(dynamic_cast<frame*>(wxTheApp->GetTopWindow()))
89!
38
{
39
  SetArtProvider(new wxAuiDefaultTabArt);
45!
40

41
  config_get();
45✔
42

43
  Bind(
45✔
44
    wxEVT_AUINOTEBOOK_PAGE_CHANGED,
45
    [=, this](wxAuiNotebookEvent& event)
45✔
46
    {
47
      event.Skip(); // call base
71✔
48
      if (m_frame != nullptr)
71!
49
      {
50
        m_frame->on_notebook(GetId(), GetPage(event.GetSelection()));
71✔
51
      }
52
    });
71✔
53

54
  Bind(
45✔
55
    wxEVT_AUINOTEBOOK_PAGE_CLOSE,
56
    [=, this](wxAuiNotebookEvent& event)
45✔
57
    {
58
      if (const auto sel = event.GetSelection(); sel != wxNOT_FOUND)
×
59
      {
60
        if (m_frame != nullptr && !m_frame->allow_close(GetId(), GetPage(sel)))
×
61
        {
62
          event.Veto();
×
63
        }
64
        else
65
        {
66
          erase_keys(GetPage(sel));
×
67

68
          if (m_frame != nullptr && m_keys.empty())
×
69
          {
70
            m_frame->sync_close_all(GetId());
×
71
          }
72

73
          event.Skip(); // call base
×
74
        }
75
      }
76
    });
×
77
}
45✔
78

79
wxWindow* wex::notebook::add_page(const data::notebook& data)
403✔
80
{
81
  if (!AddPage(PAGE_DATA))
403!
82
  {
83
    return nullptr;
×
84
  }
85

86
  m_keys[data.key()]     = data.page();
403✔
87
  m_windows[data.page()] = data.key();
403✔
88

89
  return data.page();
403✔
90
}
91

92
const std::string wex::notebook::change_selection(const std::string& key)
3✔
93
{
94
  int previous;
95

96
  if (
3✔
97
    const auto index = page_index_by_key(key);
3✔
98
    index != wxNOT_FOUND &&
5✔
99
    ((previous = wxAuiNotebook::ChangeSelection(index))) >= 0)
2!
100
  {
101
    auto* page      = m_keys[key];
2✔
102
    m_keys[key]     = page;
2✔
103
    m_windows[page] = key;
2✔
104
    return key_by_page(GetPage(previous));
2✔
105
  }
106

107
  return std::string();
1✔
108
}
109

110
int wex::notebook::config_dialog(const data::window& par)
×
111
{
112
  const data::window data(data::window(par).title(_("Tab Options")));
×
113

114
  if (m_config_dialog == nullptr)
×
115
  {
116
    m_config_dialog = new item_dialog(notebook_config_items(), data);
×
117
  }
118

119
  return (data.button() & wxAPPLY) ? m_config_dialog->Show() :
×
120
                                     m_config_dialog->ShowModal();
×
121
}
×
122

123
void wex::notebook::config_get()
45✔
124
{
125
  const auto&       ci(notebook_config_items());
45✔
126
  const item_vector iv(&ci);
45✔
127

128
  SetFont(iv.find<wxFont>(_("tab.Font")));
45✔
129
}
45✔
130

131
bool wex::notebook::delete_page(const std::string& key)
6✔
132
{
133
  if (
6✔
134
    const auto index = page_index_by_key(key);
6✔
135
    index != wxNOT_FOUND && DeletePage(index))
6!
136
  {
137
    erase_keys(key);
5✔
138

139
    if (m_frame != nullptr && m_keys.empty())
5!
140
    {
141
      m_frame->sync_close_all(GetId());
×
142
    }
143

144
    return true;
5✔
145
  }
146

147
  return false;
1✔
148
}
149

150
const std::string wex::notebook::current_page_key()
6✔
151
{
152
  return key_by_page(GetCurrentPage());
6✔
153
}
154

155
void wex::notebook::erase_keys(const std::variant<std::string, wxWindow*>& v)
8✔
156
{
157
  std::string str;
8✔
158
  wxWindow*   win;
159

160
  if (std::holds_alternative<std::string>(v))
8✔
161
  {
162
    str = get<std::string>(v);
5✔
163
    win = m_keys[str];
5✔
164
  }
165
  else if (std::holds_alternative<wxWindow*>(v))
3!
166
  {
167
    win = get<wxWindow*>(v);
3✔
168
    str = m_windows[win];
3✔
169
  }
170
  else
171
  {
172
    assert("unexpected variant");
173
  }
174

175
  m_keys.erase(str);
8✔
176
  m_windows.erase(win);
8✔
177
}
8✔
178

179
wxWindow* wex::notebook::insert_page(const data::notebook& data)
2✔
180
{
181
  if (!InsertPage(data.index(), PAGE_DATA))
2!
182
  {
183
    return nullptr;
×
184
  }
185

186
  m_keys[data.key()]     = data.page();
2✔
187
  m_windows[data.page()] = data.key();
2✔
188

189
  return data.page();
2✔
190
}
191

192
void wex::notebook::rearrange(int direction)
2✔
193
{
194
  for (size_t i = 0; i < GetPageCount(); ++i)
8✔
195
  {
196
    wxAuiNotebook::Split(i, direction);
6✔
197
  }
198
}
2✔
199

200
bool wex::notebook::set_page_text(
1✔
201
  const std::string&    key,
202
  const std::string&    new_key,
203
  const std::string&    caption,
204
  const wxBitmapBundle& bitmap)
205
{
206
  const auto index = page_index_by_key(key);
1✔
207
  if (index == wxNOT_FOUND || !SetPageText(index, caption))
1!
208
  {
209
    return false;
×
210
  }
211

212
  auto* page = m_keys[key];
1✔
213
  m_keys.erase(key);
1✔
214
  m_keys[new_key] = page;
1✔
215
  m_windows[page] = new_key;
1✔
216

217
  if (bitmap.IsOk())
1!
218
  {
219
    SetPageBitmap(index, bitmap);
×
220
  }
221

222
  return true;
1✔
223
}
224

225
wxWindow* wex::notebook::set_selection(const std::string& key)
3✔
226
{
227
  const auto index = page_index_by_key(key);
3✔
228
  if (index == wxNOT_FOUND)
3✔
229
  {
230
    return nullptr;
1✔
231
  }
232

233
  wxAuiNotebook::SetSelection(index);
2✔
234
  auto* page = GetPage(index);
2✔
235
  page->SetFocus();
2✔
236
  return page;
2✔
237
}
238

239
bool wex::notebook::split(const std::string& key, int direction)
3✔
240
{
241
  const auto index = page_index_by_key(key);
3✔
242

243
  if (index == wxNOT_FOUND)
3✔
244
  {
245
    return false;
1✔
246
  }
247

248
  wxAuiNotebook::Split(index, direction);
2✔
249

250
  return true;
2✔
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc