summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-text-caption.c
blob: 8e4e853f0966c994ccc64d3610de9a0396e28239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-string-picker.c - A widget to pick a string from a list.

   Copyright (C) 1999, 2000 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>

#include "nautilus-text-caption.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-glib-extensions.h"

#include <gtk/gtklabel.h>
#include <gtk/gtkentry.h>

#include <gtk/gtksignal.h>

static const gint TEXT_CAPTION_INVALID = -1;
static const gint TEXT_CAPTION_SPACING = 10;

/* Signals */
typedef enum
{
	CHANGED,
	LAST_SIGNAL
} NautilusTextCaptionSignals;

struct _NautilusTextCaptionDetail
{
	GtkWidget		*text;
};

/* NautilusTextCaptionClass methods */
static void      nautilus_text_caption_initialize_class (NautilusTextCaptionClass *klass);
static void      nautilus_text_caption_initialize       (NautilusTextCaption      *text_caption);

/* GtkObjectClass methods */
static void      nautilus_text_caption_destroy          (GtkObject                 *object);

/* Editable (entry) callbacks */
static void      entry_changed_callback                  (GtkWidget                 *entry,
							  gpointer                   user_data);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusTextCaption, nautilus_text_caption, NAUTILUS_TYPE_CAPTION)

static guint text_caption_signals[LAST_SIGNAL] = { 0 };

/*
 * NautilusTextCaptionClass methods
 */
static void
nautilus_text_caption_initialize_class (NautilusTextCaptionClass *text_caption_class)
{
	GtkObjectClass *object_class;
	GtkWidgetClass *widget_class;
	
	object_class = GTK_OBJECT_CLASS (text_caption_class);
	widget_class = GTK_WIDGET_CLASS (text_caption_class);

	/* GtkObjectClass */
	object_class->destroy = nautilus_text_caption_destroy;
	
	/* Signals */
	text_caption_signals[CHANGED] =
		gtk_signal_new ("changed",
				GTK_RUN_LAST,
				object_class->type,
				0,
				gtk_marshal_NONE__NONE,
				GTK_TYPE_NONE, 
				0);

	gtk_object_class_add_signals (object_class, text_caption_signals, LAST_SIGNAL);
}

static void
nautilus_text_caption_initialize (NautilusTextCaption *text_caption)
{
	text_caption->detail = g_new (NautilusTextCaptionDetail, 1);

	gtk_box_set_homogeneous (GTK_BOX (text_caption), FALSE);
	gtk_box_set_spacing (GTK_BOX (text_caption), TEXT_CAPTION_SPACING);

	text_caption->detail->text = gtk_entry_new ();

	gtk_entry_set_editable (GTK_ENTRY (text_caption->detail->text), TRUE);

	nautilus_caption_set_child (NAUTILUS_CAPTION (text_caption),
				    text_caption->detail->text);

	gtk_signal_connect (GTK_OBJECT (text_caption->detail->text),
			    "changed",
			    GTK_SIGNAL_FUNC (entry_changed_callback),
			    (gpointer) text_caption);
	
	gtk_widget_show (text_caption->detail->text);
}

/*
 * GtkObjectClass methods
 */
static void
nautilus_text_caption_destroy(GtkObject* object)
{
	NautilusTextCaption * text_caption;
	
	g_return_if_fail (object != NULL);
	g_return_if_fail (NAUTILUS_IS_TEXT_CAPTION (object));
	
	text_caption = NAUTILUS_TEXT_CAPTION (object);

	g_free (text_caption->detail);

	/* Chain */
	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}

/*
 * Editable (entry) callbacks
 */
static void
entry_changed_callback (GtkWidget *entry, gpointer user_data)
{
	NautilusTextCaption *text_caption;

	g_assert (user_data != NULL);
	g_assert (NAUTILUS_IS_TEXT_CAPTION (user_data));

	text_caption = NAUTILUS_TEXT_CAPTION (user_data);

	gtk_signal_emit (GTK_OBJECT (text_caption), text_caption_signals[CHANGED]);
}

/*
 * NautilusTextCaption public methods
 */
GtkWidget *
nautilus_text_caption_new (void)
{
	return gtk_widget_new (nautilus_text_caption_get_type (), NULL);
}

/**
 * nautilus_text_caption_get_text
 * @text_caption: A NautilusTextCaption
 *
 * Returns: A copy of the currently selected text.  Need to g_free() it.
 */
char *
nautilus_text_caption_get_text (NautilusTextCaption *text_caption)
{
	const char *entry_text;

 	g_return_val_if_fail (text_caption != NULL, NULL);
	g_return_val_if_fail (NAUTILUS_IS_TEXT_CAPTION (text_caption), NULL);

	/* WATCHOUT: 
	 * As of gtk1.2, gtk_entry_get_text() returns a non const reference to
	 * the internal string.
	 */
	entry_text = (const char *) gtk_entry_get_text (GTK_ENTRY (text_caption->detail->text));

	return g_strdup (entry_text);
}

void
nautilus_text_caption_set_text (NautilusTextCaption	*text_caption,
				 const char		*text)
{
 	g_return_if_fail (text_caption != NULL);
	g_return_if_fail (NAUTILUS_IS_TEXT_CAPTION (text_caption));

	gtk_entry_set_text (GTK_ENTRY (text_caption->detail->text), text);
}