File indexing completed on 2024-04-14 05:40:00

0001 /*****************************************************************************
0002  *   Copyright 2003 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
0003  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0004  *                                                                           *
0005  *   This program is free software; you can redistribute it and/or modify    *
0006  *   it under the terms of the GNU Lesser General Public License as          *
0007  *   published by the Free Software Foundation; either version 2.1 of the    *
0008  *   License, or (at your option) version 3, or any later version accepted   *
0009  *   by the membership of KDE e.V. (or its successor approved by the         *
0010  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0011  *   Section 6 of version 3 of the license.                                  *
0012  *                                                                           *
0013  *   This program is distributed in the hope that it will be useful,         *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0016  *   Lesser General Public License for more details.                         *
0017  *                                                                           *
0018  *   You should have received a copy of the GNU Lesser General Public        *
0019  *   License along with this library. If not,                                *
0020  *   see <http://www.gnu.org/licenses/>.                                     *
0021  *****************************************************************************/
0022 
0023 #include "scrollbar.h"
0024 
0025 #include <qtcurve-utils/gtkprops.h>
0026 
0027 namespace QtCurve {
0028 namespace Scrollbar {
0029 
0030 static void
0031 cleanup(GtkWidget *widget)
0032 {
0033     GtkWidgetProps props(widget);
0034     if (widget && props->scrollBarHacked) {
0035         props->scrollBarDestroy.disconn();
0036         props->scrollBarUnrealize.disconn();
0037         props->scrollBarStyleSet.disconn();
0038         props->scrollBarValueChanged.disconn();
0039         props->scrollBarHacked = false;
0040     }
0041 }
0042 
0043 static gboolean
0044 styleSet(GtkWidget *widget, GtkStyle*, void*)
0045 {
0046     cleanup(widget);
0047     return false;
0048 }
0049 
0050 static gboolean
0051 destroy(GtkWidget *widget, GdkEvent*, void*)
0052 {
0053     cleanup(widget);
0054     return false;
0055 }
0056 
0057 static GtkScrolledWindow*
0058 parentScrolledWindow(GtkWidget *widget)
0059 {
0060     GtkWidget *parent = widget;
0061 
0062     while (parent && (parent = gtk_widget_get_parent(parent))) {
0063         if (GTK_IS_SCROLLED_WINDOW(parent)) {
0064             return GTK_SCROLLED_WINDOW(parent);
0065         }
0066     }
0067     return nullptr;
0068 }
0069 
0070 static gboolean
0071 valueChanged(GtkWidget *widget, GdkEventMotion*, void*)
0072 {
0073     if (GTK_IS_SCROLLBAR(widget)) {
0074         GtkScrolledWindow *sw = parentScrolledWindow(widget);
0075 
0076         if (sw) {
0077             gtk_widget_queue_draw(GTK_WIDGET(sw));
0078         }
0079     }
0080     return false;
0081 }
0082 
0083 static void
0084 setupSlider(GtkWidget *widget)
0085 {
0086     GtkWidgetProps props(widget);
0087     if (widget && !props->scrollBarHacked) {
0088         props->scrollBarHacked = true;
0089         props->scrollBarDestroy.conn("destroy-event", destroy);
0090         props->scrollBarUnrealize.conn("unrealize", destroy);
0091         props->scrollBarStyleSet.conn("style-set", styleSet);
0092         props->scrollBarValueChanged.conn("value-changed", valueChanged);
0093     }
0094 }
0095 
0096 void
0097 setup(GtkWidget *widget)
0098 {
0099     GtkScrolledWindow *sw = parentScrolledWindow(widget);
0100 
0101     if (sw) {
0102         GtkWidget *slider;
0103 
0104         if ((slider = gtk_scrolled_window_get_hscrollbar(sw))) {
0105             setupSlider(slider);
0106         }
0107         if ((slider = gtk_scrolled_window_get_vscrollbar(sw))) {
0108             setupSlider(slider);
0109         }
0110     }
0111 }
0112 
0113 }
0114 }