Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright 2012 Andrew Higginson
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- */
- public class ContainerMenuItem : Gtk.MenuItem {
- public Gtk.Box box;
- public ContainerMenuItem(Gtk.Orientation orientation, int spacing) {
- this.add_events(Gdk.EventMask.KEY_PRESS_MASK);
- this.box = new Gtk.Box(orientation, spacing);
- this.box.set_name("box");
- this.add(this.box);
- this.connect_signals();
- }
- public void pack_start(Gtk.Widget child, bool expand, bool fill, int padding = 0) {
- this.box.pack_start(child, expand, fill, padding);
- }
- public void pack_end(Gtk.Widget child, bool expand, bool fill, int padding = 0) {
- this.box.pack_end(child, expand, fill, padding);
- }
- private void connect_signals() {
- this.parent_set.connect(this.on_parent_set);
- this.button_release_event.connect(this.on_button_event);
- this.button_press_event.connect(this.on_button_event);
- this.enter_notify_event.connect(this.on_enter_notify_event);
- this.leave_notify_event.connect(this.on_leave_notify_event);
- this.motion_notify_event.connect(this.on_motion_notify_event);
- }
- public Gtk.Widget[] get_all_child_widgets(Gtk.Widget? parent = null) {
- if (parent == null) { parent = this; }
- Gtk.Widget[] widgets = {};
- foreach (var child in (parent as Gtk.Container).get_children()) {
- widgets += child;
- if (child is Gtk.Container) {
- var child_widgets = this.get_all_child_widgets(child);
- foreach (var child_widget in child_widgets) {
- widgets += child_widget;
- }
- }
- }
- return widgets;
- }
- private void on_parent_set(Gtk.Widget? parent_old) {
- if (parent_old != null)
- parent_old.key_press_event.disconnect(this.on_parent_key_press_event);
- var parent_new = this.get_parent();
- if (parent_new != null)
- parent_new.key_press_event.connect(this.on_parent_key_press_event);
- }
- private bool on_button_event(Gdk.EventButton event) {
- foreach (var widget in this.get_all_child_widgets()) {
- Gtk.Allocation allocation;
- widget.get_allocation(out allocation);
- if (event.x >= allocation.x && event.y >= allocation.y &&
- event.x <= allocation.x + allocation.width &&
- event.y <= allocation.y + allocation.height)
- {
- foreach (var window in widget.get_window().get_children()) {
- int x_window, y_window;
- window.get_origin(out x_window, out y_window);
- if (event.x_root >= x_window && event.y_root >= y_window &&
- event.x_root <= x_window + window.get_width() &&
- event.y_root <= y_window + window.get_height())
- {
- var event_new = event;
- event_new.window = window;
- event_new.x = event.x_root - x_window;
- event_new.y = event.y_root - y_window;
- if (event.type == Gdk.EventType.BUTTON_PRESS) {
- widget.button_press_event(event_new);
- }
- else if (event.type == Gdk.EventType.BUTTON_RELEASE) {
- widget.button_release_event(event_new);
- }
- }
- }
- }
- }
- return true; // Don't appear as a menuitem
- }
- private bool on_motion_notify_event(Gtk.Widget parent, Gdk.EventMotion event) {
- foreach (var widget in this.get_all_child_widgets()) {
- Gtk.Allocation allocation;
- widget.get_allocation(out allocation);
- foreach (var window in widget.get_window().get_children()) {
- int x_window, y_window;
- window.get_origin(out x_window, out y_window);
- bool widget_in_event = (event.x >= allocation.x && event.y >= allocation.y &&
- event.x <= allocation.x + allocation.width &&
- event.y <= allocation.y + allocation.height);
- bool window_in_event = (event.x_root >= x_window && event.y_root >= y_window &&
- event.x_root <= x_window + window.get_width() &&
- event.y_root <= y_window + window.get_height());
- if (widget_in_event && window_in_event) {
- var event_crossing = Gdk.EventCrossing();
- var event_motion = Gdk.EventMotion();
- event_crossing.type = Gdk.EventType.ENTER_NOTIFY;
- event_crossing.window = event_motion.window = window;
- event_crossing.x = event_motion.x = event.x_root - x_window;
- event_crossing.y = event_motion.y = event.y_root - y_window;
- widget.enter_notify_event(event_crossing);
- widget.motion_notify_event(event_motion);
- } else {
- var event_new = Gdk.EventCrossing();
- event_new.type = Gdk.EventType.LEAVE_NOTIFY;
- event_new.window = window;
- event_new.x = event.x_root - x_window;
- event_new.y = event.y_root - y_window;
- widget.leave_notify_event(event_new);
- }
- }
- }
- return true;
- }
- private bool on_enter_notify_event(Gdk.EventCrossing event) { return true; }
- private bool on_leave_notify_event(Gdk.EventCrossing event) {
- foreach (var widget in this.get_children()) {
- widget.set_data("over", "false");
- foreach (var window in widget.get_window().get_children()) {
- int x_window, y_window;
- window.get_origin(out x_window, out y_window);
- var event_new = event;
- event_new.window = window;
- event_new.x = event.x_root - x_window;
- event_new.y = event.y_root - y_window;
- widget.leave_notify_event(event_new);
- }
- }
- return true;
- }
- private bool on_parent_key_press_event(Gdk.EventKey event) {
- foreach (var child in this.get_all_child_widgets()) {
- if (child.is_focus) {
- child.key_press_event(event);
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment