=?utf-8?q?=5Balmanah=5D_Bug_662016_=E2=80=94_Create_a_toolbar_menu_item_w?= =?utf-8?q?ith_the_fonts_style?=



commit e573eb06c5391a9fa6e4d1029a9957ab44d81589
Author: Ãlvaro PeÃa <alvaropg gmail com>
Date:   Mon Oct 24 18:39:31 2011 +0200

    Bug 662016 â Create a toolbar menu item with the fonts style

 data/almanah.ui                      |   25 ++++++++-
 src/Makefile.am                      |   74 +++++++++++++------------
 src/main-window.c                    |   99 ++++++++++++++++++++++++++++++++++
 src/widgets/font-style-menu-action.c |   58 ++++++++++++++++++++
 src/widgets/font-style-menu-action.h |   47 ++++++++++++++++
 5 files changed, 264 insertions(+), 39 deletions(-)
---
diff --git a/data/almanah.ui b/data/almanah.ui
index d449c54..d1ff490 100644
--- a/data/almanah.ui
+++ b/data/almanah.ui
@@ -196,6 +196,21 @@
 						<signal name="activate" handler="mw_jump_to_today_activate_cb"/>
 					</object>
 				</child>
+				<child>
+					<object class="AlmanahFontStyleMenuAction" id="almanah_ui_font_style">
+						<property name="stock-id">gtk-select-font</property>
+						<property name="name">format-font-style</property>
+						<property name="label" translatable="yes">Font Style</property>
+						<property name="tooltip" translatable="yes">Change font style for selected text</property>
+						<signal name="activate" handler="mw_font_style_activate_cb"/>
+					</object>
+				</child>
+				<child>
+					<object class="GtkAction" id="almanah_ui_font_style_menu">
+						<property name="name">font-style</property>
+						<property name="label" translatable="yes">_Font Style</property>
+					</object>
+				</child>
 			</object>
 		</child>
 		<ui>
@@ -237,12 +252,16 @@
 				</menu>
 			</menubar>
 			<toolbar name="almanah_mw_toolbar">
+				<toolitem action="almanah_ui_font_style">
+					<menu action="almanah_ui_font_style_menu">
+						<menuitem action="almanah_ui_bold"/>
+						<menuitem action="almanah_ui_italic"/>
+						<menuitem action="almanah_ui_underline"/>
+					</menu>
+				</toolitem>
 				<toolitem action="almanah_ui_jump_to_today"/>
 				<toolitem action="almanah_ui_important"/>
 				<separator/>
-				<toolitem action="almanah_ui_bold"/>
-				<toolitem action="almanah_ui_italic"/>
-				<toolitem action="almanah_ui_underline"/>
 				<toolitem action="almanah_ui_hyperlink"/>
 			</toolbar>
 		</ui>
diff --git a/src/Makefile.am b/src/Makefile.am
index 2f0de84..425e4bd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,42 +1,44 @@
 bin_PROGRAMS = almanah
 
 almanah_SOURCES = \
-	$(ALMANAH_MARSHAL_FILES)	\
-	$(ALMANAH_ENUM_FILES)		\
-	main.c				\
-	application.c			\
-	application.h			\
-	interface.c			\
-	interface.h			\
-	main-window.c			\
-	main-window.h			\
-	storage-manager.c		\
-	storage-manager.h		\
-	search-dialog.c			\
-	search-dialog.h			\
-	printing.c			\
-	printing.h			\
-	entry.c				\
-	entry.h				\
-	event.c				\
-	event.h				\
-	event-factory.c			\
-	event-factory.h			\
-	event-manager.c			\
-	event-manager.h			\
-	date-entry-dialog.c		\
-	date-entry-dialog.h		\
-	import-export-dialog.c		\
-	import-export-dialog.h		\
-	import-operation.c		\
-	import-operation.h		\
-	export-operation.c		\
-	export-operation.h		\
-	uri-entry-dialog.c		\
-	uri-entry-dialog.h		\
-	widgets/calendar.c		\
-	widgets/calendar.h		\
-	widgets/hyperlink-tag.c		\
+	$(ALMANAH_MARSHAL_FILES)		\
+	$(ALMANAH_ENUM_FILES)			\
+	main.c					\
+	application.c				\
+	application.h				\
+	interface.c				\
+	interface.h				\
+	main-window.c				\
+	main-window.h				\
+	storage-manager.c			\
+	storage-manager.h			\
+	search-dialog.c				\
+	search-dialog.h				\
+	printing.c				\
+	printing.h				\
+	entry.c					\
+	entry.h					\
+	event.c					\
+	event.h					\
+	event-factory.c				\
+	event-factory.h				\
+	event-manager.c				\
+	event-manager.h				\
+	date-entry-dialog.c			\
+	date-entry-dialog.h			\
+	import-export-dialog.c			\
+	import-export-dialog.h			\
+	import-operation.c			\
+	import-operation.h			\
+	export-operation.c			\
+	export-operation.h			\
+	uri-entry-dialog.c			\
+	uri-entry-dialog.h			\
+	widgets/calendar.c			\
+	widgets/calendar.h			\
+	widgets/font-style-menu-action.h	\
+	widgets/font-style-menu-action.c	\
+	widgets/hyperlink-tag.c			\
 	widgets/hyperlink-tag.h
 
 if HAVE_EVO
diff --git a/src/main-window.c b/src/main-window.c
index 5c63548..912a54c 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -40,6 +40,7 @@
 #include "import-export-dialog.h"
 #include "uri-entry-dialog.h"
 #include "widgets/calendar.h"
+#include "widgets/font-style-menu-action.h"
 #include "widgets/hyperlink-tag.h"
 
 static void almanah_main_window_dispose (GObject *object);
@@ -61,6 +62,8 @@ static void mw_hyperlink_toggled_cb (GtkToggleAction *action, AlmanahMainWindow
 static void mw_events_updated_cb (AlmanahEventManager *event_manager, AlmanahEventFactoryType type_id, AlmanahMainWindow *main_window);
 static void mw_events_selection_changed_cb (GtkTreeSelection *tree_selection, AlmanahMainWindow *main_window);
 static void mw_events_value_data_cb (GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data);
+static void mw_font_style_menu_position_func (GtkMenu *menu, int *x, int *y, gboolean *push_in, GtkMenuToolButton *button);
+static GtkMenuToolButton *mw_get_font_style_tool_button_from_action (GtkAction *action);
 
 /* GtkBuilder callbacks */
 void mw_calendar_day_selected_cb (GtkCalendar *calendar, AlmanahMainWindow *main_window);
@@ -83,6 +86,7 @@ void mw_about_activate_cb (GtkAction *action, AlmanahMainWindow *main_window);
 void mw_jump_to_today_activate_cb (GtkAction *action, AlmanahMainWindow *main_window);
 void mw_events_tree_view_row_activated_cb (GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, AlmanahMainWindow *main_window);
 void mw_view_button_clicked_cb (GtkButton *button, AlmanahMainWindow *main_window);
+void mw_font_style_activate_cb (AlmanahFontStyleMenuAction *action, AlmanahMainWindow *main_window);
 
 struct _AlmanahMainWindowPrivate {
 	GtkTextView *entry_view;
@@ -102,6 +106,7 @@ struct _AlmanahMainWindowPrivate {
 	GtkAction *copy_action;
 	GtkAction *delete_action;
 	GtkAction *important_action;
+	GtkMenuToolButton *font_style_tool_button;
 
 	gboolean updating_formatting;
 	gboolean pending_bold_active;
@@ -245,6 +250,7 @@ almanah_main_window_new (AlmanahApplication *application)
 	priv->copy_action = GTK_ACTION (gtk_builder_get_object (builder, "almanah_ui_copy"));
 	priv->delete_action = GTK_ACTION (gtk_builder_get_object (builder, "almanah_ui_delete"));
 	priv->important_action = GTK_ACTION (gtk_builder_get_object (builder, "almanah_ui_important"));
+	priv->font_style_tool_button = mw_get_font_style_tool_button_from_action (GTK_ACTION (gtk_builder_get_object (builder, "almanah_ui_font_style")));
 
 #ifdef ENABLE_SPELL_CHECKING
 	/* Set up spell checking, if it's enabled */
@@ -1198,6 +1204,99 @@ mw_jump_to_today_activate_cb (GtkAction *action, AlmanahMainWindow *main_window)
 	almanah_main_window_select_date (main_window, &current_date);
 }
 
+/**
+ * Just copied from GtkMenuToolButton (menu_position_func),
+ * but using the button widget (not the arrow widget) for
+ * menu position calc, and removing the code for a vertical
+ * toolbar situation.
+ *
+ * Copyright (C) 2003 Ricardo Fernandez Pascual
+ * Copyright (C) 2004 Paolo Borelli
+ * Licensed under the GPL v2
+ */
+static void
+mw_font_style_menu_position_func (GtkMenu *menu, int *x, int *y, gboolean *push_in, GtkMenuToolButton *button)
+{
+	GtkAllocation arrow_allocation;
+	GtkWidget *widget = GTK_WIDGET (button);
+	GtkRequisition menu_req;
+	GtkTextDirection direction;
+	GdkRectangle monitor;
+	gint monitor_num;
+	GdkScreen *screen;
+	GdkWindow *window;
+	GtkAllocation allocation;
+
+	gtk_widget_get_preferred_size (GTK_WIDGET (menu), &menu_req, NULL);
+	direction = gtk_widget_get_direction (widget);
+	window = gtk_widget_get_window (widget);
+
+	screen = gtk_widget_get_screen (GTK_WIDGET (menu));
+	monitor_num = gdk_screen_get_monitor_at_window (screen, window);
+	if (monitor_num < 0)
+		monitor_num = 0;
+	gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
+
+	gtk_widget_get_allocation (widget, &allocation);
+
+	gdk_window_get_origin (window, x, y);
+	*x += allocation.x;
+	*y += allocation.y;
+
+	if (direction == GTK_TEXT_DIR_LTR)
+		*x += MAX (allocation.width - menu_req.width, 0);
+	else if (menu_req.width > allocation.width)
+		*x -= menu_req.width - allocation.width;
+
+	if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
+		*y += allocation.height;
+	else if ((*y - menu_req.height) >= monitor.y)
+		*y -= menu_req.height;
+	else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
+		*y += allocation.height;
+	else
+		*y -= menu_req.height;
+
+	*push_in = FALSE;
+}
+
+void
+mw_font_style_activate_cb (AlmanahFontStyleMenuAction *action, AlmanahMainWindow *main_window)
+{
+	AlmanahMainWindowPrivate *priv = ALMANAH_MAIN_WINDOW_GET_PRIVATE(main_window);
+	GtkMenu *menu = NULL;
+
+	if (GTK_IS_MENU_TOOL_BUTTON (priv->font_style_tool_button)) {
+		menu = GTK_MENU (gtk_menu_tool_button_get_menu (priv->font_style_tool_button));
+	}
+
+	if (GTK_IS_MENU (menu)) {
+		gtk_menu_popup (menu, NULL, NULL,
+				(GtkMenuPositionFunc) mw_font_style_menu_position_func,
+				priv->font_style_tool_button,
+				0,
+				gtk_get_current_event_time());
+	}
+}
+
+static GtkMenuToolButton *
+mw_get_font_style_tool_button_from_action (GtkAction *action)
+{
+	GSList *proxies;
+	GtkMenuToolButton *tool_button = NULL;
+
+	proxies = gtk_action_get_proxies (GTK_ACTION (action));
+	while (proxies != NULL) {
+		if (GTK_IS_MENU_TOOL_BUTTON (proxies->data)) {
+			tool_button = GTK_MENU_TOOL_BUTTON (proxies->data);
+		}
+
+		proxies = proxies->next;
+	}
+
+	return tool_button;
+}
+
 static void
 clear_factory_events (AlmanahMainWindow *self, AlmanahEventFactoryType type_id)
 {
diff --git a/src/widgets/font-style-menu-action.c b/src/widgets/font-style-menu-action.c
new file mode 100644
index 0000000..d8e4825
--- /dev/null
+++ b/src/widgets/font-style-menu-action.c
@@ -0,0 +1,58 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ * Almanah
+ * Copyright (C) Ãlvaro PeÃa 2011 <alvaropg gmail com>
+ *
+ * Almanah 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.
+ *
+ * Almanah 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 Almanah.	If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtk/gtk.h>
+
+#include "font-style-menu-action.h"
+
+G_DEFINE_TYPE (AlmanahFontStyleMenuAction, almanah_font_style_menu_action, GTK_TYPE_ACTION)
+
+static void
+almanah_font_style_menu_action_class_init (AlmanahFontStyleMenuActionClass *klass)
+{
+	GtkActionClass *action_class = GTK_ACTION_CLASS (klass);
+
+	/* This class only has been created to allow a menu into a toolitem
+	 * in the data/almanah.ui file (see the unique toolbar) in the way
+	 * described in the GtkUIManager documentation ("Note that toolitem
+	 * elements may contain a menu element, but only if their associated
+	 * action specifies a GtkMenuToolButton as proxy")
+	 *
+	 * See: https://bugzilla.gnome.org/show_bug.cgi?id=590335
+	 */
+	action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
+}
+
+static void
+almanah_font_style_menu_action_init (AlmanahFontStyleMenuAction *self)
+{
+}
+
+GtkAction *
+almanah_font_style_menu_action_new (const gchar *name, const gchar *label, const gchar *tooltip, const gchar *stock_id)
+{
+	g_return_val_if_fail (name != NULL, NULL);
+
+	return g_object_new (ALMANAH_TYPE_FONT_STYLE_MENU_ACTION,
+			     "name", name,
+			     "label", label,
+			     "tooltip", tooltip,
+			     "stock-id", stock_id,
+			     NULL);
+}
diff --git a/src/widgets/font-style-menu-action.h b/src/widgets/font-style-menu-action.h
new file mode 100644
index 0000000..bf417ae
--- /dev/null
+++ b/src/widgets/font-style-menu-action.h
@@ -0,0 +1,47 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ * Almanah
+ * Copyright (C) Ãlvaro PeÃa 2011 <alvaropg gmail com>
+ *
+ * Almanah 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.
+ *
+ * Almanah 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 Almanah.	If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef ALMANAH_FONT_STYLE_MENU_ACTION_H
+#define ALMANAH_FONT_STYLE_MENU_ACTION_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define ALMANAH_TYPE_FONT_STYLE_MENU_ACTION	    (almanah_font_style_menu_action_get_type ())
+#define ALMANAH_FONT_STYLE_MENU_ACTION(o)	    (G_TYPE_CHECK_INSTANCE_CAST ((o), ALMANAH_TYPE_FONT_STYLE_MENU_ACTION, AlmanahFontStyleMenuAction))
+#define ALMANAH_FONT_STYLE_MENU_ACTION_CLASS(k)	    (G_TYPE_CHECK_CLASS_CAST ((k), ALMANAH_TYPE_FONT_STYLE_MENU_ACTION, AlmanahFontStyleMenuActionClass))
+#define ALMANAH_IS_FONT_STYLE_MENU_ACTION(o)	    (G_TYPE_CHECK_INSTANCE_TYPE ((o), ALMANAH_TYPE_FONT_STYLE_MENU_ACTION))
+#define ALMANAH_IS_FONT_STYLE_MENU_ACTION_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), ALMANAH_TYPE_FONT_STYLE_MENU_ACTION))
+#define ALMANAH_FONT_STYLE_MENU_ACTION_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), ALMANAH_TYPE_FONT_STYLE_MENU_ACTION, AlmanahFontStyleMenuAction))
+
+typedef struct {
+	GtkAction parent;
+} AlmanahFontStyleMenuAction;
+
+typedef struct {
+	GtkActionClass parent_class;
+} AlmanahFontStyleMenuActionClass;
+
+GType almanah_font_style_menu_action_get_type (void) G_GNUC_CONST;
+GtkAction *almanah_font_style_menu_action_new (const gchar *name, const gchar *label, const gchar *tooltip, const gchar *stock_id) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+
+G_END_DECLS
+
+#endif /* !ALMANAH_FONT_STYLE_MENU_ACTION_H__ */



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