[glabels/vala] Added initial FieldButton implementation.



commit 666c2c3c0322b3ed43fde043246a02b3f2e3b721
Author: Jim Evins <evins snaught com>
Date:   Thu Mar 8 23:26:08 2012 -0500

    Added initial FieldButton implementation.

 .gitignore                          |    3 +-
 glabels/Makefile.am                 |    3 +
 glabels/field_button.vala           |  182 +++++++++++++++++++++++++++++++++++
 glabels/field_button_menu.vala      |   80 +++++++++++++++
 glabels/field_button_menu_item.vala |   41 ++++++++
 5 files changed, 308 insertions(+), 1 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 11de99a..683c249 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,7 +54,8 @@ glabels-*.tar.gz
 /glabels/*.c
 /glabels/*.stamp
 /glabels/test
-/glabels/glabels
+/glabels/glabels-4
+/glabels/cursors/cursor_pixdata.h
 
 /src/marshal.[ch]
 /src/stock-pixmaps/stockpixbufs.h
diff --git a/glabels/Makefile.am b/glabels/Makefile.am
index 1942e61..b3f06dd 100644
--- a/glabels/Makefile.am
+++ b/glabels/Makefile.am
@@ -18,6 +18,9 @@ glabels_4_SOURCES = \
 	color_swatch.vala \
 	cursor.vapi \
 	enum_util.vala \
+	field_button.vala \
+	field_button_menu.vala \
+	field_button_menu_item.vala \
 	file.vala \
 	file_util.vala \
 	font_button.vala \
diff --git a/glabels/field_button.vala b/glabels/field_button.vala
new file mode 100644
index 0000000..0eb5031
--- /dev/null
+++ b/glabels/field_button.vala
@@ -0,0 +1,182 @@
+/*  field_button.vala
+ *
+ *  Copyright (C) 2012  Jim Evins <evins snaught com>
+ *
+ *  This file is part of gLabels.
+ *
+ *  gLabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  gLabels 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+using GLib;
+
+namespace glabels
+{
+
+	public class FieldButton : Gtk.ToggleButton
+	{
+		private bool   klabel_is_key;
+		private string key;
+
+		private Gtk.Label       klabel;
+		private FieldButtonMenu menu;
+
+		public  signal void key_selected( string key );
+		public  signal void changed();
+
+
+		public FieldButton( string? name )
+		{
+			Gtk.HBox hbox = new Gtk.HBox( false, 3 );
+			this.add( hbox );
+
+			klabel = new Gtk.Label( "" );
+			klabel.set_alignment( 0, 0.5f );
+			hbox.pack_start( klabel, true, true, 0 );
+
+			Gtk.Arrow arrow = new Gtk.Arrow( Gtk.ArrowType.DOWN, Gtk.ShadowType.IN );
+			hbox.pack_end( arrow, false, false, 0 );
+
+			if ( name != null )
+			{
+				klabel.set_text( name );
+				klabel_is_key = false;
+			}
+			else
+			{
+				klabel_is_key = true;
+			}
+
+			button_press_event.connect( on_button_press_event );
+			menu.key_selected.connect( on_menu_key_selected );
+			menu.selection_done.connect( on_menu_selection_done );
+		}
+
+
+		public void set_keys( List<string> key_list )
+		{
+			menu.set_keys( key_list );
+			key = key_list.first().data;
+			if ( klabel_is_key )
+			{
+				klabel.set_text( key );
+			}
+
+			menu.show_all();
+		}
+
+
+		public void set_key( string key )
+		{
+			this.key = key;
+			if ( klabel_is_key )
+			{
+				klabel.set_text( key );
+			}
+		}
+
+
+		public string get_key()
+		{
+			return key;
+		}
+
+
+		private void menu_position_function( Gtk.Menu menu,
+		                                     out int  x,
+		                                     out int  y,
+		                                     out bool push_in )
+		{
+			Gdk.Screen screen = this.get_screen();
+			int w_screen = screen.get_width();
+			int h_screen = screen.get_height();
+
+			Gdk.Window window = this.get_window();
+			int x_window, y_window;
+			window.get_origin( out x_window, out y_window );
+
+			Gtk.Allocation allocation;
+			this.get_allocation( out allocation );
+			int x_this = allocation.x;
+			int y_this = allocation.y;
+			int h_this = allocation.height;
+
+			int w_menu, h_menu;
+			menu.get_size_request( out w_menu, out h_menu );
+
+			x = x_window + x_this;
+			y = y_window + y_this + h_this;
+
+			if ( (y + h_menu) > h_screen )
+			{
+				y = y_window + y_this - h_menu;
+
+				if ( y < 0 )
+				{
+					y = h_screen - h_menu;
+				}
+			}
+
+			if ( (x + w_menu) > w_screen )
+			{
+				x = w_screen - w_menu;
+			}
+
+			push_in = true;
+		}
+
+
+		private bool on_button_press_event( Gdk.EventButton event )
+		{
+			switch (event.button)
+			{
+
+			case 1:
+				set_active( true );
+				menu.popup( null, null, menu_position_function, event.button, event.time );
+				break;
+
+			default:
+				break;
+
+			}
+
+			return false;
+		}
+
+
+		private void on_menu_key_selected( string key )
+		{
+			if ( klabel_is_key )
+			{
+				klabel.set_text( key );
+			}
+			this.key = key;
+
+			key_selected( key );
+			changed();
+		}
+
+
+		private void on_menu_selection_done()
+		{
+			this.set_active( false );
+		}
+
+
+	}
+
+}
+
+
diff --git a/glabels/field_button_menu.vala b/glabels/field_button_menu.vala
new file mode 100644
index 0000000..504f550
--- /dev/null
+++ b/glabels/field_button_menu.vala
@@ -0,0 +1,80 @@
+/*  field_button_menu.vala
+ *
+ *  Copyright (C) 2012  Jim Evins <evins snaught com>
+ *
+ *  This file is part of gLabels.
+ *
+ *  gLabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  gLabels 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+using GLib;
+
+namespace glabels
+{
+
+	public class FieldButtonMenu : Gtk.Menu
+	{
+		public  signal void key_selected( string key );
+
+		private List<unowned FieldButtonMenuItem> menu_items;
+
+		private const int MAX_MENU_ROWS = 25;
+
+
+		public void set_keys( List<string> key_list )
+		{
+			/* Remove old menu items and cleanup list. */
+			unowned List<FieldButtonMenuItem> p;
+			unowned List<FieldButtonMenuItem> p_next = null;
+			for ( p = menu_items; p != null; p = p_next )
+			{
+				remove( p.first().data );
+
+				p_next = p.next;
+				menu_items.delete_link( p );
+			}
+			unrealize();
+
+			/* Load up new keys. */
+			int i = 0;
+			foreach ( string key in key_list )
+			{
+				FieldButtonMenuItem menu_item = new FieldButtonMenuItem( key );
+				menu_item.show();
+				menu_item.activate.connect( on_menu_item_activated );
+
+				menu_items.append( menu_item );
+
+				int i_row = i % MAX_MENU_ROWS;
+				int i_col = i / MAX_MENU_ROWS;
+				attach( menu_item, i_col, i_col+1, i_row, i_row+1 );
+
+				i++;
+			}
+		}
+
+
+		private void on_menu_item_activated( Gtk.MenuItem menu_item )
+		{
+			FieldButtonMenuItem fb_menu_item = (FieldButtonMenuItem)menu_item;
+			key_selected( fb_menu_item.key );
+		}
+
+
+	}
+
+}
+
+
diff --git a/glabels/field_button_menu_item.vala b/glabels/field_button_menu_item.vala
new file mode 100644
index 0000000..7149c1c
--- /dev/null
+++ b/glabels/field_button_menu_item.vala
@@ -0,0 +1,41 @@
+/*  field_button_menu_item.vala
+ *
+ *  Copyright (C) 2012  Jim Evins <evins snaught com>
+ *
+ *  This file is part of gLabels.
+ *
+ *  gLabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  gLabels 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+using GLib;
+
+namespace glabels
+{
+
+	public class FieldButtonMenuItem : Gtk.MenuItem
+	{
+		public string key { get; private set; }
+
+		public FieldButtonMenuItem( string key )
+		{
+			this.key = key;
+
+			set_label( key );
+		}
+
+	}
+
+}
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]