"Fossies" - the Fresh Open Source Software Archive

Member "filezilla-3.48.1/src/interface/dialogex.cpp" (8 May 2020, 8599 Bytes) of package /linux/misc/FileZilla_3.48.1_src.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dialogex.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.48.0_vs_3.48.1.

    1 #include <filezilla.h>
    2 #include "dialogex.h"
    3 #include "msgbox.h"
    4 #include "xrc_helper.h"
    5 
    6 #include <wx/statline.h>
    7 #include <wx/gbsizer.h>
    8 
    9 #ifdef __WXMAC__
   10 #include "filezillaapp.h"
   11 #endif
   12 
   13 BEGIN_EVENT_TABLE(wxDialogEx, wxDialog)
   14 EVT_CHAR_HOOK(wxDialogEx::OnChar)
   15 END_EVENT_TABLE()
   16 
   17 std::vector<wxDialogEx*> wxDialogEx::shown_dialogs_;
   18 
   19 #ifdef __WXMAC__
   20 std::vector<void*> wxDialogEx::shown_dialogs_creation_events_;
   21 
   22 static int const pasteId = wxNewId();
   23 static int const selectAllId = wxNewId();
   24 
   25 extern wxTextEntry* GetSpecialTextEntry(wxWindow*, wxChar);
   26 
   27 bool wxDialogEx::ProcessEvent(wxEvent& event)
   28 {
   29     if (event.GetEventType() != wxEVT_MENU) {
   30         return wxDialog::ProcessEvent(event);
   31     }
   32 
   33     wxTextEntry* e = GetSpecialTextEntry(FindFocus(), 'V');
   34     if (e && event.GetId() == pasteId) {
   35         e->Paste();
   36         return true;
   37     }
   38     else if (e && event.GetId() == selectAllId) {
   39         e->SelectAll();
   40         return true;
   41     }
   42     else {
   43         return wxDialog::ProcessEvent(event);
   44     }
   45 }
   46 #endif
   47 
   48 bool wxDialogEx::Create(wxWindow* parent, int id, wxString const& title, wxPoint const& pos, wxSize const& size, long style)
   49 {
   50     bool ret = wxDialog::Create(parent, id, title, pos, size, style);
   51 #ifdef __WXMAC__
   52     if (ret) {
   53         FixPasswordPaste(acceleratorTable_);
   54     }
   55 #endif
   56     return ret;
   57 }
   58 
   59 void wxDialogEx::OnChar(wxKeyEvent& event)
   60 {
   61     if (event.GetKeyCode() == WXK_ESCAPE) {
   62         wxCommandEvent cmdEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
   63         ProcessEvent(cmdEvent);
   64     }
   65     else {
   66         event.Skip();
   67     }
   68 }
   69 
   70 bool wxDialogEx::Load(wxWindow* pParent, wxString const& name, std::wstring const& file)
   71 {
   72     SetParent(pParent);
   73 
   74     InitXrc(file);
   75     if (!wxXmlResource::Get()->LoadDialog(this, pParent, name)) {
   76         return false;
   77     }
   78 
   79 #ifdef __WCMAC__
   80     FixPasswordPaste(acceleratorTable_);
   81 #endif
   82 
   83     return true;
   84 }
   85 
   86 bool wxDialogEx::SetChildLabel(int id, const wxString& label, unsigned long maxLength)
   87 {
   88     wxWindow* pText = FindWindow(id);
   89     if (!pText) {
   90         return false;
   91     }
   92 
   93     if (!maxLength) {
   94         pText->SetLabel(label);
   95     }
   96     else {
   97         wxString wrapped = label;
   98         WrapText(this, wrapped, maxLength);
   99         pText->SetLabel(wrapped);
  100     }
  101 
  102     return true;
  103 }
  104 
  105 bool wxDialogEx::SetChildLabel(char const* id, const wxString& label, unsigned long maxLength)
  106 {
  107     return SetChildLabel(XRCID(id), label, maxLength);
  108 }
  109 
  110 wxString wxDialogEx::GetChildLabel(int id)
  111 {
  112     auto pText = dynamic_cast<wxStaticText*>(FindWindow(id));
  113     if (!pText) {
  114         return wxString();
  115     }
  116 
  117     return pText->GetLabel();
  118 }
  119 
  120 int wxDialogEx::ShowModal()
  121 {
  122     CenterOnParent();
  123 
  124 #ifdef __WXMSW__
  125     // All open menus need to be closed or app will become unresponsive.
  126     ::EndMenu();
  127 
  128     // For same reason release mouse capture.
  129     // Could happen during drag&drop with notification dialogs.
  130     ::ReleaseCapture();
  131 #endif
  132 
  133     shown_dialogs_.push_back(this);
  134 #ifdef __WXMAC__
  135     shown_dialogs_creation_events_.push_back(wxGetApp().MacGetCurrentEvent());
  136 #endif
  137 
  138     if (acceleratorTable_.empty()) {
  139         SetAcceleratorTable(wxNullAcceleratorTable);
  140     }
  141     else {
  142         SetAcceleratorTable(wxAcceleratorTable(acceleratorTable_.size(), acceleratorTable_.data()));
  143     }
  144 
  145     int ret = wxDialog::ShowModal();
  146 
  147 #ifdef __WXMAC__
  148     shown_dialogs_creation_events_.pop_back();
  149 #endif
  150     shown_dialogs_.pop_back();
  151 
  152     return ret;
  153 }
  154 
  155 bool wxDialogEx::ReplaceControl(wxWindow* old, wxWindow* wnd)
  156 {
  157     if (!GetSizer()->Replace(old, wnd, true)) {
  158         return false;
  159     }
  160     old->Destroy();
  161 
  162     return true;
  163 }
  164 
  165 bool wxDialogEx::CanShowPopupDialog(wxTopLevelWindow * parent)
  166 {
  167     if (IsShowingMessageBox()) {
  168         // There already a message box showing
  169         return false;
  170     }
  171 
  172     if (!shown_dialogs_.empty() && shown_dialogs_.back() != parent) {
  173         // There is an open dialog which isn't the expected parent
  174         return false;
  175     }
  176 
  177     wxMouseState mouseState = wxGetMouseState();
  178     if (mouseState.LeftIsDown() || mouseState.MiddleIsDown() || mouseState.RightIsDown()) {
  179         // Displaying a dialog while the user is clicking is extremely confusing, don't do it.
  180         return false;
  181     }
  182 #ifdef __WXMSW__
  183     // During a drag & drop we cannot show a dialog. Doing so can render the program unresponsive
  184     if (GetCapture()) {
  185         return false;
  186     }
  187 #endif
  188 
  189 #ifdef __WXMAC__
  190     void* ev = wxGetApp().MacGetCurrentEvent();
  191     if (ev && (shown_dialogs_creation_events_.empty() || ev != shown_dialogs_creation_events_.back())) {
  192         // We're inside an event handler for a native mac event, such as a popup menu
  193         return false;
  194     }
  195 #endif
  196 
  197     return true;
  198 }
  199 
  200 void wxDialogEx::InitDialog()
  201 {
  202 #ifdef __WXGTK__
  203     // Some controls only report proper size after the call to Show(), e.g.
  204     // wxStaticBox::GetBordersForSizer is affected by this.
  205     // Re-fit window to compensate.
  206     wxSizer* s = GetSizer();
  207     if (s) {
  208         wxSize min = GetMinClientSize();
  209         wxSize smin = s->GetMinSize();
  210         if( min.x < smin.x || min.y < smin.y ) {
  211             s->Fit(this);
  212             SetMinSize(GetSize());
  213         }
  214     }
  215 #endif
  216 
  217     wxDialog::InitDialog();
  218 }
  219 
  220 
  221 DialogLayout const& wxDialogEx::layout()
  222 {
  223     if (!layout_) {
  224         layout_ = std::make_unique<DialogLayout>(this);
  225     }
  226 
  227     return *layout_;
  228 }
  229 
  230 wxSizerFlags const DialogLayout::grow(wxSizerFlags().Expand());
  231 wxSizerFlags const DialogLayout::valign(wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
  232 wxSizerFlags const DialogLayout::halign(wxSizerFlags().Align(wxALIGN_CENTER_HORIZONTAL));
  233 wxSizerFlags const DialogLayout::valigng(wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL).Expand());
  234 wxSizerFlags const DialogLayout::ralign(wxSizerFlags().Align(wxALIGN_RIGHT));
  235 
  236 DialogLayout::DialogLayout(wxTopLevelWindow * parent)
  237     : parent_(parent)
  238 {
  239     gap = dlgUnits(3);
  240     border = dlgUnits(3);
  241     indent = dlgUnits(10);
  242 }
  243 
  244 int DialogLayout::dlgUnits(int num) const
  245 {
  246     return wxDLG_UNIT(parent_, wxPoint(0, num)).y;
  247 }
  248 
  249 wxFlexGridSizer* DialogLayout::createMain(wxWindow* parent, int cols, int rows) const
  250 {
  251     auto outer = new wxBoxSizer(wxVERTICAL);
  252     parent->SetSizer(outer);
  253 
  254     auto main = createFlex(cols, rows);
  255     outer->Add(main, 1, wxALL|wxGROW, border);
  256 
  257     return main;
  258 }
  259 
  260 wxFlexGridSizer* DialogLayout::createFlex(int cols, int rows) const
  261 {
  262     int const g = gap;
  263     return new wxFlexGridSizer(rows, cols, g, g);
  264 }
  265 
  266 wxGridSizer* DialogLayout::createGrid(int cols, int rows) const
  267 {
  268     int const g = gap;
  269     return new wxGridSizer(rows, cols, g, g);
  270 }
  271 
  272 wxGridBagSizer* DialogLayout::createGridBag(int cols, int rows) const
  273 {
  274     int const g = gap;
  275     auto bag = new wxGridBagSizer(g, g);
  276     bag->SetCols(cols);
  277     bag->SetRows(rows);
  278     return bag;
  279 }
  280 
  281 wxStdDialogButtonSizer * DialogLayout::createButtonSizer(wxWindow* parent, wxSizer * sizer, bool hline) const
  282 {
  283     if (hline) {
  284         sizer->Add(new wxStaticLine(parent), grow);
  285     }
  286     auto btns = new wxStdDialogButtonSizer();
  287     sizer->Add(btns, grow);
  288 
  289     return btns;
  290 }
  291 
  292 wxSizerItem* DialogLayout::gbAddRow(wxGridBagSizer * gb, wxWindow* wnd, wxSizerFlags const& flags) const
  293 {
  294     int row = gb->GetRows();
  295     gb->SetRows(row + 1);
  296 
  297     return gb->Add(wnd, wxGBPosition(row, 0), wxGBSpan(1, gb->GetCols()), flags.GetFlags(), flags.GetBorderInPixels());
  298 }
  299 
  300 void DialogLayout::gbNewRow(wxGridBagSizer * gb) const
  301 {
  302     gb->SetRows(gb->GetRows() + 1);
  303 }
  304 
  305 wxSizerItem* DialogLayout::gbAdd(wxGridBagSizer* gb, wxWindow* wnd, wxSizerFlags const& flags) const
  306 {
  307     int const row = gb->GetRows() - 1;
  308     int col;
  309     for (col = 0; col < gb->GetCols(); ++col) {
  310         if (!gb->FindItemAtPosition(wxGBPosition(row, col))) {
  311             break;
  312         }
  313     }
  314 
  315     auto item = gb->Add(wnd, wxGBPosition(row, col), wxGBSpan(), flags.GetFlags(), flags.GetBorderInPixels());
  316     return item;
  317 }
  318 
  319 wxSizerItem* DialogLayout::gbAdd(wxGridBagSizer* gb, wxSizer* sizer, wxSizerFlags const& flags) const
  320 {
  321     int const row = gb->GetRows() - 1;
  322     int col;
  323     for (col = 0; col < gb->GetCols(); ++col) {
  324         if (!gb->FindItemAtPosition(wxGBPosition(row, col))) {
  325             break;
  326         }
  327     }
  328 
  329     auto item = gb->Add(sizer, wxGBPosition(row, col), wxGBSpan(), flags.GetFlags(), flags.GetBorderInPixels());
  330     return item;
  331 }
  332 
  333 std::tuple<wxStaticBox*, wxFlexGridSizer*> DialogLayout::createStatBox(wxSizer* parent, wxString const& title, int cols, int rows) const
  334 {
  335     auto* boxSizer = new wxStaticBoxSizer(wxHORIZONTAL, parent->GetContainingWindow(), title);
  336     auto* box = boxSizer->GetStaticBox();
  337     parent->Add(boxSizer, 1, wxGROW);
  338 
  339     auto* flex = createFlex(cols, rows);
  340 #ifdef __WXMSW__
  341     int style = wxGROW | wxLEFT | wxBOTTOM | wxRIGHT;
  342     int const b = dlgUnits(2);
  343 #else
  344     int style = wxGROW | wxALL;
  345     int const b = border;
  346 #endif
  347     boxSizer->Add(flex, 1, style, b);
  348 
  349     return std::make_tuple(box, flex);
  350 }
  351 
  352 std::wstring LabelEscape(std::wstring const& label)
  353 {
  354     return fz::replaced_substrings(label, L"&", L"&&");
  355 }
  356 
  357 #ifdef __WXMAC__
  358 void FixPasswordPaste(std::vector<wxAcceleratorEntry> & entries)
  359 {
  360     entries.emplace_back(wxACCEL_CMD, 'V', pasteId);
  361     entries.emplace_back(wxACCEL_CMD, 'A', selectAllId);
  362 }
  363 #endif