1 /* 2 * this file is part of the oxygen gtk engine 3 * Copyright (c) 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr> 4 * Copyright (c) 2010 Ruslan Kabatsayev <b7.10110111@gmail.com> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or( at your option ) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free 18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 19 * MA 02110-1301, USA. 20 */ 21 22 #include "oxygenhook.h" 23 #include "../config.h" 24 25 #include <cassert> 26 #include <iostream> 27 28 namespace Oxygen 29 { 30 31 #if OXYGEN_DEBUG 32 static int counter( 0 ); 33 #endif 34 35 //__________________________________________________________________ connect(const std::string & signal,GType typeId,GSignalEmissionHook hookFunction,gpointer data)36 bool Hook::connect( const std::string& signal, GType typeId, GSignalEmissionHook hookFunction, gpointer data ) 37 { 38 #if DISABLE_SIGNAL_HOOKS 39 return false; 40 #endif 41 // make sure that signal is not already connected 42 assert( _signalId == 0 && _hookId == 0 ); 43 44 // check type id 45 if( !g_type_class_peek( typeId ) ) 46 { 47 48 #if OXYGEN_DEBUG 49 std::cerr << "Oxygen::Hook::connect - typeId " << g_type_name(typeId) << " not yet installed" << std::endl; 50 #endif 51 52 g_type_class_ref( typeId ); 53 54 } 55 56 // store signal id 57 _signalId = g_signal_lookup( signal.c_str(), typeId ); 58 if( !_signalId ) 59 { 60 61 #if OXYGEN_DEBUG 62 std::cerr << "Oxygen::Hook::connect - signal " << signal << " not installed." << std::endl; 63 #endif 64 65 return false; 66 67 } 68 69 // store attributes and create connection 70 _hookId = g_signal_add_emission_hook( 71 _signalId, 72 (GQuark)0L, 73 hookFunction, 74 data, 0L); 75 76 #if OXYGEN_DEBUG 77 ++counter; 78 std::cerr << "Oxygen::Hook::connect - hook: " << _hookId << " counter: " << counter << std::endl; 79 #endif 80 81 return true; 82 83 } 84 85 //____________________________________________________________________ disconnect(void)86 void Hook::disconnect( void ) 87 { 88 #if DISABLE_SIGNAL_HOOKS 89 return; 90 #endif 91 92 // disconnect signal 93 if( _signalId > 0 && _hookId > 0 ) 94 { 95 96 #if OXYGEN_DEBUG 97 --counter; 98 std::cerr << "Oxygen::Hook::disconnect - hook: " << _hookId << " counter: " << counter << std::endl; 99 #endif 100 101 g_signal_remove_emission_hook( _signalId, _hookId ); 102 103 } 104 105 _signalId = 0; 106 _hookId = 0; 107 108 } 109 110 } 111