/* * Copyright (C) 2012-2017 Robin Haberkorn * * 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 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ requires 2.0.20 %ctop{ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #ifndef HAVE_GTK_FLOW_BOX_NEW #include "gtkflowbox.h" #endif #include "gtk-canonicalized-label.h" %} %h{ #include #include #include %} enum GTK_INFO_POPUP { PLAIN, FILE, DIRECTORY } Gtk:Info:Popup:Entry:Type; /* * NOTE: Deriving from GtkEventBox ensures that we can * set a background on the entire popup widget. */ class Gtk:Info:Popup from Gtk:Event:Box { public GtkAdjustment *hadjustment; public GtkAdjustment *vadjustment; private GtkWidget *flow_box; init(self) { GtkWidget *box, *viewport; /* * A box containing a viewport and scrollbar will * "emulate" a scrolled window. * We cannot use a scrolled window since it ignores * the preferred height of its viewport which breaks * height-for-width management. */ box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); self->hadjustment = gtk_adjustment_new(0, 0, 0, 0, 0, 0); self->vadjustment = gtk_adjustment_new(0, 0, 0, 0, 0, 0); GtkWidget *scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, self->vadjustment); /* show/hide the scrollbar dynamically */ g_signal_connect(self->vadjustment, "changed", G_CALLBACK(self_vadjustment_changed), scrollbar); self->_priv->flow_box = gtk_flow_box_new(); /* take as little height as necessary */ gtk_orientable_set_orientation(GTK_ORIENTABLE(self->_priv->flow_box), GTK_ORIENTATION_HORIZONTAL); //gtk_flow_box_set_homogeneous(GTK_FLOW_BOX(self->_priv->flow_box), TRUE); /* this for focus handling only, not for scrolling */ gtk_flow_box_set_hadjustment(GTK_FLOW_BOX(self->_priv->flow_box), self->hadjustment); gtk_flow_box_set_vadjustment(GTK_FLOW_BOX(self->_priv->flow_box), self->vadjustment); viewport = gtk_viewport_new(self->hadjustment, self->vadjustment); gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE); gtk_container_add(GTK_CONTAINER(viewport), self->_priv->flow_box); gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0); gtk_widget_show_all(box); /* * NOTE: Everything shown except the top-level container. * Therefore a gtk_widget_show() is enough to show our popup. */ gtk_container_add(GTK_CONTAINER(self), box); } /** * Allocate position in an overlay. * * This function can be used as the "get-child-position" signal * handler of a GtkOverlay in order to position the popup at the * bottom of the overlay's main child, spanning the entire width. * In contrast to the GtkOverlay's default allocation schemes, * this makes sure that the widget will not be larger than the * main child, so the popup properly scrolls when becoming too large * in height. * * @param user_data unused by this callback */ public gboolean get_position_in_overlay(Gtk:Overlay *overlay, Gtk:Widget *widget, Gdk:Rectangle *allocation, gpointer user_data) { GtkWidget *main_child = gtk_bin_get_child(GTK_BIN(overlay)); GtkAllocation main_child_alloc; gint natural_height; gtk_widget_get_allocation(main_child, &main_child_alloc); gtk_widget_get_preferred_height_for_width(widget, main_child_alloc.width, NULL, &natural_height); /* * FIXME: Probably due to some bug in the height-for-width * calculation of Gtk (at least in 3.10 or in the GtkFlowBox * fallback included with SciTECO), the natural height * is a bit too small to accommodate the entire GtkFlowBox, * resulting in the GtkViewport always scrolling. * This hack fixes it up in a NONPORTABLE manner. */ natural_height += 5; allocation->width = main_child_alloc.width; allocation->height = MIN(natural_height, main_child_alloc.height); allocation->x = 0; allocation->y = main_child_alloc.height - allocation->height; return TRUE; } /* * Adapted from GtkScrolledWindow's gtk_scrolled_window_scroll_event() * since the viewport does not react to scroll events. * This is registered for our container widget instead of only for * GtkViewport since this is what GtkScrolledWindow does. * FIXME: May need to handle non-delta scrolling, i.e. GDK_SCROLL_UP * and GDK_SCROLL_DOWN. */ override (Gtk:Widget) gboolean scroll_event(Gtk:Widget *widget, Gdk:Event:Scroll *event) { Self *self = SELF(widget); gdouble delta_x, delta_y; if (gdk_event_get_scroll_deltas((GdkEvent *)event, &delta_x, &delta_y)) { GtkAdjustment *adj = self->vadjustment; gdouble page_size = gtk_adjustment_get_page_size(adj); gdouble scroll_unit = pow(page_size, 2.0 / 3.0); gdouble new_value; new_value = CLAMP(gtk_adjustment_get_value(adj) + delta_y * scroll_unit, gtk_adjustment_get_lower(adj), gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj)); gtk_adjustment_set_value(adj, new_value); return TRUE; } return FALSE; } private void vadjustment_changed(Gtk:Adjustment *vadjustment, gpointer user_data) { GtkWidget *scrollbar = GTK_WIDGET(user_data); /* * This shows/hides the widget using opacity instead of using * gtk_widget_set_visibility() since the latter would influence * size allocations. A widget with opacity 0 keeps its size. */ gtk_widget_set_opacity(scrollbar, gtk_adjustment_get_upper(vadjustment) - gtk_adjustment_get_lower(vadjustment) > gtk_adjustment_get_page_size(vadjustment) ? 1 : 0); } public GtkWidget * new(void) { Self *widget = GET_NEW; return GTK_WIDGET(widget); } public GIcon * get_icon_for_path(const gchar *path, const gchar *fallback_name) { GFile *file; GFileInfo *info; GIcon *icon = NULL; file = g_file_new_for_path(path); info = g_file_query_info(file, "standard::icon", 0, NULL, NULL); if (info) { icon = g_file_info_get_icon(info); g_object_ref(icon); g_object_unref(info); } else { /* fall back to standard icon, but this can still return NULL! */ icon = g_icon_new_for_string(fallback_name, NULL); } g_object_unref(file); return icon; } public void add(self, Gtk:Info:Popup:Entry:Type type, const gchar *name, gboolean highlight) { GtkWidget *hbox; GtkWidget *label; hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5); if (highlight) gtk_style_context_add_class(gtk_widget_get_style_context(hbox), "highlight"); if (type == GTK_INFO_POPUP_FILE || type == GTK_INFO_POPUP_DIRECTORY) { const gchar *fallback = type == GTK_INFO_POPUP_FILE ? "text-x-generic" : "folder"; GIcon *icon; icon = self_get_icon_for_path(name, fallback); if (icon) { GtkWidget *image; image = gtk_image_new_from_gicon(icon, GTK_ICON_SIZE_MENU); g_object_unref(icon); gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0); } } label = gtk_canonicalized_label_new(name); gtk_widget_set_halign(label, GTK_ALIGN_START); gtk_widget_set_valign(label, GTK_ALIGN_CENTER); /* * FIXME: This makes little sense once we've got mouse support. * But for the time being, it's a useful setting. */ gtk_label_set_selectable(GTK_LABEL(label), TRUE); switch (type) { case GTK_INFO_POPUP_PLAIN: gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_START); break; case GTK_INFO_POPUP_FILE: case GTK_INFO_POPUP_DIRECTORY: gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_MIDDLE); break; } gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); gtk_widget_show_all(hbox); gtk_container_add(GTK_CONTAINER(self->_priv->flow_box), hbox); } public void scroll_page(self) { GtkAdjustment *adj = self->vadjustment; gdouble new_value; if (gtk_adjustment_get_value(adj) + gtk_adjustment_get_page_size(adj) == gtk_adjustment_get_upper(adj)) { /* wrap and scroll back to the top */ new_value = gtk_adjustment_get_lower(adj); } else { /* scroll one page */ GList *child_list; new_value = gtk_adjustment_get_value(adj) + gtk_adjustment_get_page_size(adj); /* * Adjust this so only complete entries are shown. * Effectively, this rounds down to the line height. */ child_list = gtk_container_get_children(GTK_CONTAINER(self->_priv->flow_box)); if (child_list) { new_value -= (gint)new_value % gtk_widget_get_allocated_height(GTK_WIDGET(child_list->data)); g_list_free(child_list); } /* clip to the maximum possible value */ new_value = MIN(new_value, gtk_adjustment_get_upper(adj)); } gtk_adjustment_set_value(adj, new_value); } public void clear(self) { GList *children; children = gtk_container_get_children(GTK_CONTAINER(self->_priv->flow_box)); for (GList *cur = g_list_first(children); cur != NULL; cur = g_list_next(cur)) gtk_widget_destroy(GTK_WIDGET(cur->data)); g_list_free(children); } }