2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "windowobservers.h"
25 #include <gdk/gdkevents.h>
26 #include "generic/bitfield.h"
30 ModifierFlags g_modifier_state = c_modifierNone;
33 typedef std::vector<WindowObserver*> WindowObservers;
35 inline void WindowObservers_OnModifierDown(WindowObservers& observers, ModifierFlags type)
37 g_modifier_state = bitfield_enable(g_modifier_state, type);
38 for(WindowObservers::iterator i = observers.begin(); i != observers.end(); ++i)
40 (*i)->onModifierDown(type);
44 inline void WindowObservers_OnModifierUp(WindowObservers& observers, ModifierFlags type)
46 g_modifier_state = bitfield_disable(g_modifier_state, type);
47 for(WindowObservers::iterator i = observers.begin(); i != observers.end(); ++i)
49 (*i)->onModifierUp(type);
53 #include <gdk/gdkkeysyms.h>
55 gboolean selection_modifier_key_press(GtkWidget* widget, GdkEventKey* event, WindowObservers& observers)
61 //globalOutputStream() << "Alt PRESSED\n";
62 WindowObservers_OnModifierDown(observers, c_modifierAlt);
66 //globalOutputStream() << "Shift PRESSED\n";
67 WindowObservers_OnModifierDown(observers, c_modifierShift);
71 //globalOutputStream() << "Control PRESSED\n";
72 WindowObservers_OnModifierDown(observers, c_modifierControl);
78 gboolean selection_modifier_key_release(GtkWidget* widget, GdkEventKey* event, WindowObservers& observers)
84 //globalOutputStream() << "Alt RELEASED\n";
85 WindowObservers_OnModifierUp(observers, c_modifierAlt);
89 //globalOutputStream() << "Shift RELEASED\n";
90 WindowObservers_OnModifierUp(observers, c_modifierShift);
94 //globalOutputStream() << "Control RELEASED\n";
95 WindowObservers_OnModifierUp(observers, c_modifierControl);
101 void WindowObservers_UpdateModifier(WindowObservers& observers, ModifierFlags modifiers, ModifierFlags modifier)
103 if(!bitfield_enabled(g_modifier_state, modifier) && bitfield_enabled(modifiers, modifier))
105 WindowObservers_OnModifierDown(observers, modifier);
107 if(bitfield_enabled(g_modifier_state, modifier) && !bitfield_enabled(modifiers, modifier))
109 WindowObservers_OnModifierUp(observers, modifier);
113 void WindowObservers_UpdateModifiers(WindowObservers& observers, ModifierFlags modifiers)
115 WindowObservers_UpdateModifier(observers, modifiers, c_modifierAlt);
116 WindowObservers_UpdateModifier(observers, modifiers, c_modifierShift);
117 WindowObservers_UpdateModifier(observers, modifiers, c_modifierControl);
120 gboolean modifiers_button_press(GtkWidget* widget, GdkEventButton* event, WindowObservers* observers)
122 if(event->type == GDK_BUTTON_PRESS)
124 WindowObservers_UpdateModifiers(*observers, modifiers_for_state(event->state));
129 gboolean modifiers_button_release(GtkWidget* widget, GdkEventButton* event, WindowObservers* observers)
131 if(event->type == GDK_BUTTON_RELEASE)
133 WindowObservers_UpdateModifiers(*observers, modifiers_for_state(event->state));
138 gboolean modifiers_motion(GtkWidget *widget, GdkEventMotion *event, WindowObservers* observers)
140 WindowObservers_UpdateModifiers(*observers, modifiers_for_state(event->state));
145 WindowObservers g_window_observers;
147 void GlobalWindowObservers_updateModifiers(ModifierFlags modifiers)
149 WindowObservers_UpdateModifiers(g_window_observers, modifiers);
152 void GlobalWindowObservers_add(WindowObserver* observer)
154 g_window_observers.push_back(observer);
157 void GlobalWindowObservers_connectTopLevel(GtkWindow* window)
159 g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(selection_modifier_key_press), &g_window_observers);
160 g_signal_connect(G_OBJECT(window), "key_release_event", G_CALLBACK(selection_modifier_key_release), &g_window_observers);
163 void GlobalWindowObservers_connectWidget(GtkWidget* widget)
165 g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(modifiers_button_press), &g_window_observers);
166 g_signal_connect(G_OBJECT(widget), "button_release_event", G_CALLBACK(modifiers_button_release), &g_window_observers);
167 g_signal_connect(G_OBJECT(widget), "motion_notify_event", G_CALLBACK(modifiers_motion), &g_window_observers);