summaryrefslogtreecommitdiffstats
path: root/tdegtk/tdegtk-widgetlookup.cpp
blob: 79ed0e9382f0551879ed06e4465309e72045225d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* this file was largely taken from the oxygen gtk engine
* Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
* Copyright (c) 2010 Ruslan Kabatsayev <b7.10110111@gmail.com>
*
* Hook-setup code provided by Ruslan
*
* This  library is free  software; you can  redistribute it and/or
* modify it  under  the terms  of the  GNU Lesser  General  Public
* License  as published  by the Free  Software  Foundation; either
* version 2 of the License, or( at your option ) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License  along  with  this library;  if not,  write to  the Free
* Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#include "tdegtk-widgetlookup.h"
#include "config.h"

#include <iostream>
#include <cairo/cairo-gobject.h>

// #define TDEGTK_DEBUG 1

    //__________________________________________________________________
    WidgetLookup::WidgetLookup( void ):
        _hooksInitialized( false ),
        _context( 0L )
    {}


    //_____________________________________________________
    WidgetLookup::~WidgetLookup( void )
    {

        // disconnect hooks
        _drawHook.disconnect();

    }

    //_____________________________________________________
    void WidgetLookup::initializeHooks( void )
    {

        #if TDEGTK_DEBUG
        std::cerr << "Oxygen::WidgetLookup::initializeHooks" << std::endl;
        #endif

        if( _hooksInitialized ) return;

        // install hook and test
        if( !_drawHook.connect( "draw", (GSignalEmissionHook)drawHook, this ) ) return;

        // set initialization flag
        _hooksInitialized = true;

        return;

    }

    //_____________________________________________________
    GtkWidget* WidgetLookup::find( cairo_t* context, const GtkWidgetPath* path ) const
    {

        // check path
        if( !path ) return 0L;

        // get length and check
        const gint length( gtk_widget_path_length( path ) );
        if( length < 1 ) return 0L;

        // lookup last type
        return find( context, gtk_widget_path_iter_get_object_type( path, length-1 ) );

    }

    //_____________________________________________________
    GtkWidget* WidgetLookup::find( cairo_t* context, GType type ) const
    {
        // check context
        if( context != _context )
        {
            #if TDEGTK_DEBUG
            std::cerr << "Oxygen::WidgetLookup::find - invalid context: " << context << std::endl;
            #endif
            return 0L;
        }

        #if TDEGTK_DEBUG
        std::cerr
            << "Oxygen::WidgetLookup::find -"
            << " context: " << context
            << " type: " << g_type_name( type )
            << std::endl;
        #endif

        // look for type in stored widgets
        /* we loop backward, since last added widgets are more likely to be looked up */
        for( WidgetList::const_reverse_iterator iter = _widgets.rbegin(); iter != _widgets.rend(); ++iter )
        {
            // compare types and return if matched
            if( G_OBJECT_TYPE( *iter ) == type )
            {
                #if TDEGTK_DEBUG
                std::cerr
                    << "Oxygen::WidgetLookup::find -"
                    << " context: " << context
                    << " type: " << g_type_name( type )
                    << " found: " << *iter
                    << std::endl;
                #endif
                return *iter;
            }
        }

        #if TDEGTK_DEBUG
        std::cerr
            << "Oxygen::WidgetLookup::find -"
            << " context: " << context
            << " type: " << g_type_name( type )
            << " - no match found"
            << std::endl;
        #endif

        return 0L;

    }

    //_____________________________________________________
    void WidgetLookup::bind( GtkWidget* widget, cairo_t* context )
    {
        // check if context has changed and clear widgets if yes
        if( context != _context )
        {

            #if TDEGTK_DEBUG
            std::cerr
                << "Oxygen::WidgetLookup::bind -"
                << " context: " << _context
                << " widgets: " << _widgets.size()
                << std::endl;
            #endif

            _context = context;
            _widgets.clear();
        }

        _widgets.push_back( widget );

        // add to all widgets map
        if( _allWidgets.find( widget ) == _allWidgets.end() )
        {
            Signal destroyId;
            destroyId.connect( G_OBJECT( widget ), "destroy", G_CALLBACK( destroyNotifyEvent ), this );
            _allWidgets.insert( std::make_pair( widget, destroyId ) );
        }

    }

    //____________________________________________________________________________________________
    void WidgetLookup::unregisterWidget( GtkWidget* widget )
    {

        #if TDEGTK_DEBUG
        std::cerr << "Oxygen::WidgetLookup::unregisterWidget - " << widget << " (" << G_OBJECT_TYPE_NAME( widget ) << ")" << std::endl;
        #endif

        // find in map
        WidgetMap::iterator iter( _allWidgets.find( widget ) );
        assert( iter != _allWidgets.end() );

        // disconnect signal
        iter->second.disconnect();

        // erase from lists
        _allWidgets.erase( widget );
        _widgets.remove( widget );

    }

    //_____________________________________________________
    gboolean WidgetLookup::drawHook( GSignalInvocationHint*, guint numParams, const GValue* params, gpointer data )
    {

        // check number of parameters
        if( numParams < 2 ) return FALSE;

        // get widget from params
        GtkWidget* widget( GTK_WIDGET( g_value_get_object( params ) ) );

        // check type
        if( !GTK_IS_WIDGET( widget ) ) return FALSE;

        // check second parameter type.
        if( !G_VALUE_HOLDS( params+1, CAIRO_GOBJECT_TYPE_CONTEXT ) )
        { return FALSE; }

        // retrieve context and cast
        cairo_t* context( static_cast<cairo_t*>( g_value_get_boxed(params+1) ) );

        #if TDEGTK_DEBUG
        std::cerr
            << "Oxygen::WidgetLookup::drawHook -"
            << " widget: " << widget << " (" << G_OBJECT_TYPE_NAME( widget ) << ")"
            << " context: " << context
            << std::endl;
        #endif

        // bind widget and context
        static_cast<WidgetLookup*>( data )->bind( widget, context );

        return TRUE;

    }


    //____________________________________________________________________________________________
    gboolean WidgetLookup::destroyNotifyEvent( GtkWidget* widget, gpointer data )
    {
        static_cast<WidgetLookup*>(data)->unregisterWidget( widget );
        return FALSE;
    }