Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Reading and writing global menu settings

  • Loading branch information...
commit d73c5c4be4ac2691470020284ce24957eb5d368b 1 parent b34eb32
rainwoodman authored November 06, 2009
95  libsettings/settings.vala
... ...
@@ -0,0 +1,95 @@
  1
+public class Gnomenu.Settings : Object {
  2
+	private Gdk.Screen screen;
  3
+	private Gdk.Window window;
  4
+
  5
+	private Gdk.Atom atom = Gdk.Atom.intern("_NET_GLOBALMENU_SETTINGS", false);
  6
+
  7
+	public KeyFile keyfile = new KeyFile();
  8
+
  9
+	public bool show_local_menu { get; set; default = true; }
  10
+	public bool use_global_menu { get; set; default = true; }
  11
+	
  12
+	public Settings(Gdk.Screen screen) {
  13
+		this.screen = screen;
  14
+		this.window = screen.get_root_window();
  15
+		assert(window != null);
  16
+		window.add_filter(this.event_filter);
  17
+		var events = window.get_events();
  18
+		window.set_events(events | Gdk.EventMask.PROPERTY_CHANGE_MASK);
  19
+		pull();
  20
+	}
  21
+	~Settings() {
  22
+		window.remove_filter(this.event_filter);
  23
+	}
  24
+
  25
+	[CCode (instance_pos = -1)]
  26
+	private Gdk.FilterReturn event_filter(Gdk.XEvent xevent, Gdk.Event event) {
  27
+		/* This weird extra level of calling is to avoid a type cast in Vala
  28
+		 * which will cause the loss of delegate target. */
  29
+		return real_event_filter(&xevent, event);
  30
+	}
  31
+	[CCode (instance_pos = -1)]
  32
+	private Gdk.FilterReturn real_event_filter(X.Event* xevent, Gdk.Event event) {
  33
+		switch(xevent->type) {
  34
+			case X.EventType.PropertyNotify:
  35
+			if(atom == Gdk.x11_xatom_to_atom(xevent->xproperty.atom)) {
  36
+				pull();
  37
+			}
  38
+			break;
  39
+		}
  40
+		return Gdk.FilterReturn.CONTINUE;
  41
+	}
  42
+
  43
+	public string to_string() {
  44
+		keyfile.set_boolean("GlobalMenu:Client", "show-local-menu", this.show_local_menu);
  45
+		keyfile.set_boolean("GlobalMenu:Client", "use-global-menu", this.use_global_menu);
  46
+		return keyfile.to_data(null);
  47
+	}
  48
+
  49
+	public void pull() {
  50
+		var data = get_by_atom(atom);
  51
+		if(data == null) return;
  52
+		keyfile.load_from_data(data, data.length, KeyFileFlags.NONE);
  53
+		this.show_local_menu = keyfile.get_boolean("GlobalMenu:Client", "show-local-menu");
  54
+		this.use_global_menu = keyfile.get_boolean("GlobalMenu:Client", "use-global-menu");
  55
+	}
  56
+
  57
+	public void push() {
  58
+		var str = this.to_string();
  59
+		set_by_atom(atom, str);
  60
+	}
  61
+
  62
+	public string? get_by_atom(Gdk.Atom atom) {
  63
+		string context = null;
  64
+		Gdk.Atom actual_type;
  65
+		Gdk.Atom type = Gdk.Atom.intern("STRING", false);
  66
+		int actual_format;
  67
+		int actual_length;
  68
+		Gdk.property_get(window,
  69
+			atom,
  70
+			type,
  71
+			0, (ulong) long.MAX, false, 
  72
+			out actual_type, 
  73
+			out actual_format, 
  74
+			out actual_length, 
  75
+			out context);
  76
+		return context;
  77
+	}
  78
+
  79
+	public void set_by_atom(Gdk.Atom atom, string? value) {
  80
+		if(value != null) {
  81
+			Gdk.Atom type = Gdk.Atom.intern("STRING", false);
  82
+			Gdk.property_change(window,
  83
+				atom, type,
  84
+				8,
  85
+				Gdk.PropMode.REPLACE,
  86
+				value, 
  87
+				(int) value.size() + 1
  88
+			);
  89
+		} else {
  90
+			Gdk.property_delete(window, atom);
  91
+		}
  92
+	}
  93
+}
  94
+
  95
+}
33  libsettings/tests/Makefile.am
... ...
@@ -0,0 +1,33 @@
  1
+include $(top_srcdir)/Makefile.gtest
  2
+
  3
+if ENABLE_TESTS
  4
+TEST_PROGS +=  $(noinst_PROGRAMS)
  5
+noinst_PROGRAMS =$(VALASOURCES:.vala=)
  6
+endif
  7
+
  8
+VALASOURCES = \
  9
+	test-write-settings.vala \
  10
+	test-watch-settings.vala
  11
+
  12
+STAMP = $(VALASOURCES:.vala=.c)
  13
+SUFFIXES = .vala .c
  14
+
  15
+.vala.c:
  16
+	$(VALAC) $(VALA_DEFAULT_PKGS) -C \
  17
+		--vapidir=.. \
  18
+		--pkg gtk+-2.0 \
  19
+		--pkg globalmenu-settings $^
  20
+	touch $@
  21
+
  22
+AM_CPPFLAGS = -I.. \
  23
+	$(GTK_CFLAGS) \
  24
+	$(GLIB_CFLAGS) \
  25
+	-include $(CONFIG_HEADER) 
  26
+
  27
+BUILT_SOURCES = $(STAMP)
  28
+LDADD = $(GTK_LIBS) \
  29
+	$(GLIB_LIBS) \
  30
+	$(WNCK_LIBS) \
  31
+	../libsettings.la
  32
+
  33
+EXTRA_DIST += $(STAMP) $(VALASOURCES)
12  libsettings/tests/test-watch-settings.vala
... ...
@@ -0,0 +1,12 @@
  1
+public int main(string[] args) {
  2
+	Gtk.init(ref args);
  3
+
  4
+	Gnomenu.Settings settings = new Gnomenu.Settings(Gdk.Screen.get_default());
  5
+
  6
+	settings.notify["use-global-menu"] += () => {
  7
+		message("use-global-menu changed");
  8
+	};
  9
+
  10
+	Gtk.main();
  11
+	return 0;
  12
+}
13  libsettings/tests/test-write-settings.vala
... ...
@@ -0,0 +1,13 @@
  1
+public int main(string[] args) {
  2
+	Gtk.init(ref args);
  3
+	Gnomenu.Settings settings = new Gnomenu.Settings(Gdk.Screen.get_default());
  4
+	settings.notify["use-global-menu"] += () => {
  5
+		message("use-global-menu changed");
  6
+	};
  7
+	settings.use_global_menu = ! settings.use_global_menu;
  8
+	settings.push();
  9
+	Gdk.flush();
  10
+	Gtk.main();
  11
+
  12
+	return 0;
  13
+}

0 notes on commit d73c5c4

Please sign in to comment.
Something went wrong with that request. Please try again.