| 1 | /* Copyright 2024 Maxim Iorsh <[email protected]> |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "dialog_base.hpp" |
| 29 | |
| 30 | extern "C" { |
| 31 | #include "gutils.h" |
| 32 | } |
| 33 | |
| 34 | #include "intl.h" |
| 35 | #include "gimage.h" |
| 36 | #include "utils.hpp" |
| 37 | |
| 38 | typedef struct gevent GEvent; |
| 39 | |
| 40 | namespace ff::dlg { |
| 41 | |
| 42 | static GdkWindow* get_toplevel_gdk_window(GWindow gwin) { |
| 43 | // Redeclare GWindow to avoid dependency on the legacy headers. |
| 44 | struct ggdkwindow_local { /* :GWindow */ |
| 45 | // Inherit GWindow start |
| 46 | void* ggc; |
| 47 | void* display; |
| 48 | int (*eh)(GWindow, GEvent*); |
| 49 | GRect pos; |
| 50 | struct ggdkwindow_local* parent; |
| 51 | void* user_data; |
| 52 | void* widget_data; |
| 53 | GdkWindow* w; |
| 54 | }; |
| 55 | |
| 56 | if (gwin) { |
| 57 | GdkWindow* gdk_win = ((ggdkwindow_local*)gwin)->w; |
| 58 | return gdk_window_get_effective_toplevel(gdk_win); |
| 59 | } else { |
| 60 | return nullptr; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | DialogBase::DialogBase(GWindow parent_gwin) : parent_gwindow_(parent_gwin) { |
| 65 | auto ok_button = add_button(_("_OK"), Gtk::RESPONSE_OK); |
| 66 | ok_button->set_name("ok"); |
| 67 | |
| 68 | auto cancel_button = add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); |
| 69 | cancel_button->set_name("cancel"); |
| 70 | |
| 71 | set_default_response(Gtk::RESPONSE_OK); |
| 72 | set_position(Gtk::WIN_POS_CENTER); |
| 73 | } |
| 74 | |
| 75 | DialogBase::~DialogBase() { |
| 76 | if (parent_gwindow_) { |
| 77 | // Unblock the parent GDraw window |
| 78 | GdkWindow* parent_gdk_window = get_toplevel_gdk_window(parent_gwindow_); |
| 79 | g_object_set_data(G_OBJECT(parent_gdk_window), "GTKModalBlock", NULL); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Gtk::ResponseType DialogBase::run() { |
| 84 | if (parent_gwindow_) { |
| 85 | GdkWindow* parent_gdk_window = get_toplevel_gdk_window(parent_gwindow_); |
| 86 | GdkWindow* this_window = get_window().get()->gobj(); |
| 87 | gdk_window_set_transient_for(this_window, parent_gdk_window); |
| 88 | |
| 89 | // Mark the parent window as blocked, this would cause GDraw to discard |
| 90 | // its events. |
| 91 | g_object_set_data(G_OBJECT(parent_gdk_window), "GTKModalBlock", |
| 92 | (gpointer) true); |
| 93 | } |
| 94 | |
| 95 | return (Gtk::ResponseType)Gtk::Dialog::run(); |
| 96 | } |
| 97 | |
| 98 | void DialogBase::set_help_context(const std::string& file, |
| 99 | const std::string& section) { |
| 100 | help_file_ = file; |
| 101 | help_section_ = section; |
| 102 | |
| 103 | if (!help_file_.empty()) { |
| 104 | signal_key_press_event().connect( |
| 105 | sigc::mem_fun(*this, &DialogBase::on_help_key_press), false); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void DialogBase::set_hints_horizontal_resize_only() { |
| 110 | Gdk::Geometry geometry; |
| 111 | // Height can't grow beyond the necessary minimum |
| 112 | geometry.min_height = 1; |
| 113 | geometry.max_height = 1; |
| 114 | // Width is not limited |
| 115 | geometry.min_width = 1; |
| 116 | geometry.max_width = 10000; |
| 117 | set_geometry_hints(*this, geometry, |
| 118 | Gdk::HINT_MIN_SIZE | Gdk::HINT_MAX_SIZE); |
| 119 | } |
| 120 | |
| 121 | void DialogBase::remove_cancel_button() { |
| 122 | auto cancel_button = get_widget_for_response(Gtk::RESPONSE_CANCEL); |
| 123 | if (cancel_button) { |
| 124 | get_action_area()->remove(*cancel_button); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | bool DialogBase::on_help_key_press(GdkEventKey* event) { |
| 129 | if (event->keyval == GDK_KEY_F1 || event->keyval == GDK_KEY_Help) { |
| 130 | help(help_file_.c_str(), help_section_.c_str()); |
| 131 | return true; |
| 132 | } |
| 133 | return false; // let other keys be processed normally |
| 134 | } |
| 135 | |
| 136 | } // namespace ff::dlg |