Szontágh Ferenc
2025-03-12 77bc7063534ab317a2ef96249a4f78198d01ef01
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
#include "BrowserFrame.h"
#include "handlers/CurlHttpHandler.h"
#include "handlers/CurlHttpsHandler.h"
 
BrowserFrame::BrowserFrame(wxWindow *parent) : wxBrowserBase(parent) {
  wxCommandEvent event;
  event.SetString("https://www.google.com");
  this->OnNewTab(event);
}
 
BrowserFrame::~BrowserFrame() { webViews.clear(); }
 
void BrowserFrame::OnPageClose(wxAuiNotebookEvent &event) { event.Skip(); }
void BrowserFrame::OnPageClosed(wxAuiNotebookEvent &event) {
 
  // get the closed tab index
  int closed = event.GetSelection();
  // remove the tab from the map
  this->webViewtabMap.erase(closed);
  // remove the webview from the map
  this->webViews.erase(this->webViewtabMap[closed]);
  if (this->currentWebView == this->webViewtabMap[closed]) {
    this->currentWebView = nullptr;
  }
  if (this->webViewtabMap.size() == 0) {
    wxCommandEvent event = wxCommandEvent();
    event.SetString("https://www.google.com");
    this->OnNewTab(event);
  }
  event.Skip();
}
void BrowserFrame::OnPageChanging(wxAuiNotebookEvent &event) { event.Skip(); }
void BrowserFrame::OnPageChanged(wxAuiNotebookEvent &event) { event.Skip(); }
 
void BrowserFrame::OnUrlEnter(wxCommandEvent &event) {
  wxString url = m_urlbar->GetValue();
  if (!url.StartsWith("http://") && !url.StartsWith("https://")) {
    wxRegEx domainRegex("^([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$");
    url = domainRegex.Matches(url) ? "https://" + url
                                   : "https://www.google.com/search?q=" + url;
    this->m_urlbar->SetValue(url);
  }
 
  if (this->currentWebView) {
    url.Prepend("p");
    this->currentWebView->LoadURL(url);
  }
}
void BrowserFrame::OnNewTab(wxCommandEvent &event) {
  wxPanel *panel = new wxPanel(m_auinotebook1);
  wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
 
  wxString url = "https://www.google.com";
  if (!event.GetString().IsEmpty()) {
    url = event.GetString();
  }
 
  wxWebView *webView = wxWebView::New(panel, wxID_ANY, url);
  webView->RegisterHandler(
      wxSharedPtr<wxWebViewHandler>(new CurlHttpHandler()));
  webView->RegisterHandler(
      wxSharedPtr<wxWebViewHandler>(new CurlHttpsHandler()));
  sizer->Add(webView, 1, wxEXPAND);
  panel->SetSizer(sizer);
  m_auinotebook1->AddPage(panel, "New Tab", true);
  m_auinotebook1->SetSelection(m_auinotebook1->GetPageCount() - 1);
  this->m_urlbar->SetValue(url);
  this->currentWebView = webView;
  this->webViews[webView] = std::make_shared<WebViewHandler>(
      webView, std::bind(&BrowserFrame::WebViewHandlerCallback, this,
                         std::placeholders::_1, std::placeholders::_2,
                         std::placeholders::_3));
  this->webViewtabMap[m_auinotebook1->GetPageCount() - 1] = webView;
}
void BrowserFrame::OnReload(wxCommandEvent &event) {
  if (this->currentWebView) {
    this->currentWebView->Reload();
  }
}
 
void BrowserFrame::OnBack(wxCommandEvent &event) {
  if (this->currentWebView) {
    this->currentWebView->GoBack();
  }
}
 
void BrowserFrame::OnForward(wxCommandEvent &event) {
  if (this->currentWebView) {
    this->currentWebView->GoForward();
  }
}
void BrowserFrame::OnHome(wxCommandEvent &event) {
  if (this->currentWebView) {
    this->currentWebView->LoadURL("https://www.google.com");
  }
}
void BrowserFrame::m_urlbarOnSetFocus(wxFocusEvent &event) {
  this->m_urlbar->SetSelection(0, this->m_urlbar->GetValue().size());
}
 
void BrowserFrame::m_urlbarOnLeftUp(wxMouseEvent &event) {
  this->m_urlbar->SetSelection(0, this->m_urlbar->GetValue().size());
}
 
size_t BrowserFrame::GetTabIndex(wxWebView *webView) {
  for (auto &pair : this->webViewtabMap) {
    if (pair.second == webView) {
      return pair.first;
    }
  }
  return wxNOT_FOUND;
}
 
void BrowserFrame::ChangeTabIndex(wxWebView *webView, int index) {
  auto it = this->webViewtabMap.find(index);
  if (it != this->webViewtabMap.end()) {
    this->webViewtabMap[index] = webView;
    this->webViewtabMap.erase(it);
  }
}
 
void BrowserFrame::WebViewHandlerCallback(WebViewHandler::CallbackType type,
                                          const wxString data,
                                          WebViewHandler *handler) {
  switch (type) {
  case WebViewHandler::CallbackType::OnTitleChanged: {
    const auto index = this->GetTabIndex(handler->GetwebView());
    if (index == wxNOT_FOUND) {
      return;
    }
    wxString title = data;
    if (data.size() > 50) {
      title = data.substr(0, 50) + "...";
      this->m_auinotebook1->SetPageToolTip(index, data);
    }
    this->m_auinotebook1->SetPageText(index, data);
 
  } break;
  case WebViewHandler::CallbackType::OnNavigated:
    this->m_urlbar->SetValue(data);
    break;
  case WebViewHandler::CallbackType::OnNewWindow: {
    wxCommandEvent event = wxCommandEvent();
    event.SetString(data);
    this->OnNewTab(event);
  } break;
  default:
    break;
  }
}