Re: [PATCH] Manage note sidepane by nautilus-clipboard



Am Donnerstag, den 26.05.2005, 10:52 +0200 schrieb Christian Neumair:
> Attaching a new patch which separates out some code...turns out that
> both GtkEditables and GtkTextViews share selection signals (except for
> select_all :/).

Should have attached it.

-- 
Christian Neumair <chris gnome-de org>
Index: libnautilus-private/nautilus-clipboard.c
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-clipboard.c,v
retrieving revision 1.4
diff -u -p -r1.4 nautilus-clipboard.c
--- libnautilus-private/nautilus-clipboard.c	12 May 2005 19:20:43 -0000	1.4
+++ libnautilus-private/nautilus-clipboard.c	26 May 2005 08:50:34 -0000
@@ -38,101 +38,101 @@
 #include <gtk/gtkmain.h>
 #include <gtk/gtksignal.h>
 #include <gtk/gtktext.h>
+#include <gtk/gtktextview.h>
 #include <string.h>
 
-typedef void (* EditableFunction) (GtkEditable *editable);
+typedef void (* SelectAllCallback) (gpointer target);
 
 static void selection_changed_callback            (GtkWidget *widget,
 						   gpointer callback_data);
 static void owner_change_callback (GtkClipboard        *clipboard,
 				   GdkEventOwnerChange *event,
 				   gpointer callback_data);
-     
+typedef struct {
+	GtkUIManager *ui_manager;
+	GtkActionGroup *action_group;
+	gboolean shares_selection_changes;
+
+	SelectAllCallback select_all_callback;
+} TargetCallbackData;
+
 static void
-action_cut_callback (GtkAction *action,
-		     gpointer callback_data)
+cut_callback (gpointer target)
 {
-	gtk_editable_cut_clipboard (GTK_EDITABLE (callback_data));
+	g_return_if_fail (target != NULL);
+
+	g_signal_emit_by_name (target, "cut-clipboard");
 }
 
 static void
-action_copy_callback (GtkAction *action,
-		      gpointer callback_data)
+copy_callback (gpointer target)
 {
-	gtk_editable_copy_clipboard (GTK_EDITABLE (callback_data));
+	g_return_if_fail (target != NULL);
+
+	g_signal_emit_by_name (target, "copy-clipboard");
 }
 
 static void
-action_paste_callback (GtkAction *action,
-		       gpointer callback_data)
+paste_callback (gpointer target)
 {
-	gtk_editable_paste_clipboard (GTK_EDITABLE (callback_data));
+	g_return_if_fail (target != NULL);
+
+	g_signal_emit_by_name (target, "paste-clipboard");
 }
 
 static void
-select_all (GtkEditable *editable)
-{	
+editable_select_all_callback (gpointer target)
+{
+	GtkEditable *editable;
+
+	editable = GTK_EDITABLE (target);
+	g_return_if_fail (editable != NULL);
+
 	gtk_editable_set_position (editable, -1);
 	gtk_editable_select_region (editable, 0, -1);
 }
 
-
 static void
-idle_source_destroy_callback (gpointer data,
-			      GObject *where_the_object_was)
+text_view_select_all_callback (gpointer target)
 {
-	g_source_destroy (data);
+	g_return_if_fail (GTK_IS_TEXT_VIEW (target));
+
+	g_signal_emit_by_name (target, "select-all", TRUE);
 }
 
-static gboolean
-select_all_idle_callback (gpointer callback_data)
+static void
+action_cut_callback (GtkAction *action,
+		     gpointer callback_data)
 {
-	GtkEditable *editable;
-	GSource *source;
-
-	editable = GTK_EDITABLE (callback_data);
-
-	source = g_object_get_data (G_OBJECT (editable), 
-				    "clipboard-select-all-source");
-
-	g_object_weak_unref (G_OBJECT (editable), 
-			     idle_source_destroy_callback,
-			     source);
-	
-	g_object_set_data (G_OBJECT (editable), 
-			   "clipboard-select-all-source",
-			   NULL);
+	cut_callback (callback_data);
+}
 
-	select_all (editable);
+static void
+action_copy_callback (GtkAction *action,
+		      gpointer callback_data)
+{
+	copy_callback (callback_data);
+}
 
-	return FALSE;
+static void
+action_paste_callback (GtkAction *action,
+		       gpointer callback_data)
+{
+	paste_callback (callback_data);
 }
 
 static void
 action_select_all_callback (GtkAction *action,
 			    gpointer callback_data)
 {
-	GSource *source;
-	GtkEditable *editable;
+	TargetCallbackData *target_data;
 
-	editable = GTK_EDITABLE (callback_data);
+	g_return_if_fail (callback_data != NULL);
 
-	if (g_object_get_data (G_OBJECT (editable), 
-			       "clipboard-select-all-source")) {
-		return;
-	}
+	target_data = g_object_get_data (callback_data, "Nautilus:clipboard_target_data");
+	g_return_if_fail (target_data != NULL);
 
-	source = g_idle_source_new ();
-	g_source_set_callback (source, select_all_idle_callback, editable, NULL);
-	g_object_weak_ref (G_OBJECT (editable),
-			   idle_source_destroy_callback,
-			   source);
-	g_source_attach (source, NULL);
-	g_source_unref (source);
-
-	g_object_set_data (G_OBJECT (editable),
-			   "clipboard-select-all-source", 
-			   source);
+	target_data->select_all_callback (callback_data);
 }
 
 static void
@@ -199,12 +199,6 @@ set_clipboard_menu_items_insensitive (Gt
 	gtk_action_set_sensitive (action, FALSE);
 }
 
-typedef struct {
-	GtkUIManager *ui_manager;
-	GtkActionGroup *action_group;
-	gboolean editable_shares_selection_changes;
-} TargetCallbackData;
-
 static gboolean
 clipboard_items_are_merged_in (GtkWidget *widget)
 {
@@ -229,7 +223,7 @@ merge_in_clipboard_menu_items (GObject *
 
 	g_assert (target_data != NULL);
 	
-	add_selection_callback = target_data->editable_shares_selection_changes;
+	add_selection_callback = target_data->shares_selection_changes;
 
 	gtk_ui_manager_insert_action_group (target_data->ui_manager,
 					    target_data->action_group, 0);
@@ -269,7 +263,7 @@ merge_out_clipboard_menu_items (GObject 
 					      G_CALLBACK (owner_change_callback),
 					      target_data);
 	
-	selection_callback_was_added = target_data->editable_shares_selection_changes;
+	selection_callback_was_added = target_data->shares_selection_changes;
 
 	if (selection_callback_was_added) {
 		g_signal_handlers_disconnect_matched (widget_as_object,
@@ -312,6 +306,7 @@ selection_changed_callback (GtkWidget *w
 	g_assert (target_data != NULL);
 
 	editable = GTK_EDITABLE (widget);
+	g_return_if_fail (editable != NULL);
 
 	if (gtk_editable_get_selection_bounds (editable, &start, &end) && start != end) {
 		set_clipboard_menu_items_sensitive (target_data->action_group);
@@ -376,7 +371,8 @@ static GtkActionEntry clipboard_entries[
 static TargetCallbackData *
 initialize_clipboard_component_with_callback_data (GtkEditable *target,
 						   GtkUIManager *ui_manager,
-						   gboolean shares_selection_changes)
+						   gboolean shares_selection_changes,
+						   SelectAllCallback select_all_callback)
 {
 	GtkActionGroup *action_group;
 	TargetCallbackData *target_data;
@@ -394,25 +390,25 @@ initialize_clipboard_component_with_call
 	target_data = g_new (TargetCallbackData, 1);
 	target_data->ui_manager = ui_manager;
 	target_data->action_group = action_group;
-	target_data->editable_shares_selection_changes = shares_selection_changes;
+	target_data->shares_selection_changes = shares_selection_changes;
+	target_data->select_all_callback = select_all_callback;
 	
 	return target_data;
 }
 
-void
-nautilus_clipboard_set_up_editable (GtkEditable *target,
-				    GtkUIManager *ui_manager,
-				    gboolean shares_selection_changes)
+static void
+nautilus_clipboard_real_set_up (gpointer target,
+				GtkUIManager *ui_manager,
+				gboolean shares_selection_changes,
+				SelectAllCallback select_all_callback)
 {
 	TargetCallbackData *target_data;
-	
-	g_return_if_fail (GTK_IS_EDITABLE (target));
-	g_return_if_fail (ui_manager != NULL);
 
 	target_data = initialize_clipboard_component_with_callback_data
 		(target, 
 		 ui_manager,
-		 shares_selection_changes);
+		 shares_selection_changes,
+		 select_all_callback);
 
 	g_signal_connect (target, "focus_in_event",
 			  G_CALLBACK (focus_changed_callback), target_data);
@@ -421,10 +417,39 @@ nautilus_clipboard_set_up_editable (GtkE
 	g_signal_connect (target, "destroy",
 			  G_CALLBACK (target_destroy_callback), target_data);
 
-	g_object_weak_ref (G_OBJECT (target), (GWeakNotify) target_data_free, target_data);
-	
+	g_object_set_data_full (G_OBJECT (target), "Nautilus:clipboard_target_data",
+				target_data, (GDestroyNotify) target_data_free);
+
 	/* Call the focus changed callback once to merge if the window is
 	 * already in focus.
 	 */
 	focus_changed_callback (GTK_WIDGET (target), NULL, target_data);
 }
+
+void
+nautilus_clipboard_set_up_editable (GtkEditable *target,
+				    GtkUIManager *ui_manager,
+				    gboolean shares_selection_changes)
+{
+	g_return_if_fail (GTK_IS_EDITABLE (target));
+	g_return_if_fail (GTK_IS_UI_MANAGER (ui_manager));
+
+	nautilus_clipboard_real_set_up (target, ui_manager,
+					shares_selection_changes,
+					editable_select_all_callback);
+}
+
+void
+nautilus_clipboard_set_up_text_view (GtkTextView *target,
+				     GtkUIManager *ui_manager,
+				     gboolean shares_selection_changes)
+{
+	g_return_if_fail (GTK_IS_TEXT_VIEW (target));
+	g_return_if_fail (GTK_IS_UI_MANAGER (ui_manager));
+
+	nautilus_clipboard_real_set_up (target, ui_manager,
+					shares_selection_changes,
+					text_view_select_all_callback);
+}
+
+
Index: libnautilus-private/nautilus-clipboard.h
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-clipboard.h,v
retrieving revision 1.2
diff -u -p -r1.2 nautilus-clipboard.h
--- libnautilus-private/nautilus-clipboard.h	22 Nov 2004 15:24:35 -0000	1.2
+++ libnautilus-private/nautilus-clipboard.h	26 May 2005 08:50:34 -0000
@@ -27,19 +27,24 @@
 #define NAUTILUS_CLIPBOARD_H
 
 #include <gtk/gtkeditable.h>
+#include <gtk/gtktextview.h>
 #include <gtk/gtkuimanager.h>
 
-/* This makes this editable put clipboard commands into the passed UI
- * manager when the editable is in focus. Callers in Nautilus
- * normally get the UI manager from
+/* This makes this editable or text view put clipboard commands into
+ * the passed UI manager when the editable/text view is in focus.
+ * Callers in Nautilus normally get the UI manager from
  * nautilus_window_get_ui_manager. */
 /* The shares selection changes argument should be set to true if the
- * editable is a widget that uses the signal "selection_changed" to
- * tell others about text selection changes.  The NautilusEntry widget
+ * widget uses the signal "selection_changed" to tell others about
+ * text selection changes.  The NautilusEntry widget
  * is currently the only widget in nautilus that shares selection
- * changes.  */
+ * changes. This feature currently only works for GtkEditables. */
 void nautilus_clipboard_set_up_editable            (GtkEditable        *target,
 						    GtkUIManager       *ui_manager,
 						    gboolean            shares_selection_changes);
+void nautilus_clipboard_set_up_text_view           (GtkTextView        *target,
+						    GtkUIManager       *ui_manager,
+						    gboolean            shares_selection_changes);
+
 
 #endif /* NAUTILUS_CLIPBOARD_H */
Index: src/nautilus-notes-viewer.c
===================================================================
RCS file: /cvs/gnome/nautilus/src/nautilus-notes-viewer.c,v
retrieving revision 1.5
diff -u -p -r1.5 nautilus-notes-viewer.c
--- src/nautilus-notes-viewer.c	17 May 2005 14:17:57 -0000	1.5
+++ src/nautilus-notes-viewer.c	26 May 2005 08:50:35 -0000
@@ -430,14 +430,11 @@ nautilus_notes_viewer_set_parent_window 
         sidebar->details->uri = nautilus_window_info_get_current_location (window);
         notes_load_metainfo (sidebar);
 
-        /* handle selections */
-#ifdef GNOME2_CONVERSION_COMPLETE
         /* note_text_field is a text-view, not a GtkEditable */
-	nautilus_clipboard_set_up_editable
-                (GTK_EDITABLE (sidebar->details->note_text_field),
+	nautilus_clipboard_set_up_text_view
+		(GTK_TEXT_VIEW (sidebar->details->note_text_field),
 		 nautilus_window_info_get_ui_manager (window),
 		 FALSE);
-#endif
 }
 
 static NautilusSidebar *


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