gui-util.c 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * gnumeric-util.c:  Various GUI utility functions. 
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 *
 */
#include <config.h>
#include <gnome.h>
10
#include <string.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "gnumeric.h"
#include "gnumeric-util.h"

void
gnumeric_notice (char *str)
{
	GtkWidget *dialog;
	GtkWidget *label;

	label = gtk_label_new (str);
	dialog = gnome_dialog_new (_("Notice"), GNOME_STOCK_BUTTON_OK, NULL);
	gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox), label, TRUE, TRUE, 0);
	gtk_widget_show (label);
	gnome_dialog_run_modal (GNOME_DIALOG (dialog));
	gtk_object_destroy (GTK_OBJECT (dialog));
}

28
29
30
int
gtk_radio_group_get_selected (GSList *radio_group)
{
31
32
	GSList *l;
	int i, c;
33
34
35

	g_return_val_if_fail (radio_group != NULL, 0);
	
36
37
38
	c = g_slist_length (radio_group);
		
	for (i = 0, l = radio_group; l; l = l->next, i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
39
		GtkRadioButton *button = l->data;
40

Arturo Espinosa's avatar
Arturo Espinosa committed
41
		if (GTK_TOGGLE_BUTTON (button)->active)
42
			return c - i - 1;
43
	}
44
45

	return 0;
46
}
47
48
49
50
51
52
53

void
gtk_radio_button_select (GSList *group, int n)
{
	GSList *l;
	int len = g_slist_length (group);
	
Arturo Espinosa's avatar
Arturo Espinosa committed
54
55
	l = g_slist_nth (group, len - n - 1);
	gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (l->data), 1);
56
}
57
58
59
60
61
62
63
64
65
66
67

static void
popup_menu_item_activated (GtkWidget *item, void *value)
{
	int *dest = gtk_object_get_user_data (GTK_OBJECT (item));

	*dest = GPOINTER_TO_INT (value);
	gtk_main_quit ();
}

int
68
run_popup_menu (GdkEvent *event, int button, char **strings)
69
70
71
72
{
	GtkWidget *menu;
	int i;

73
74
75
	g_return_val_if_fail (event != NULL, -1);
	g_return_val_if_fail (strings != NULL, -1);

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
	/* Create the popup menu */
	menu = gtk_menu_new ();
	for (i = 0;*strings; strings++, i++){
		GtkWidget *item;

		item = gtk_menu_item_new_with_label (_(*strings));
		
		gtk_widget_show (item);
		gtk_signal_connect (
			GTK_OBJECT (item), "activate",
			GTK_SIGNAL_FUNC (popup_menu_item_activated), GINT_TO_POINTER (i));

		/* Pass a pointer where we want the result stored */
		gtk_object_set_user_data (GTK_OBJECT (item), &i);
		
		gtk_menu_append (GTK_MENU (menu), item);
	}

	i = -1;
	
	/* Configure it: */
	gtk_signal_connect (GTK_OBJECT (menu), "deactivate",
			    GTK_SIGNAL_FUNC (gtk_main_quit), NULL);

	/* popup the menu */
	gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
			event->button.button, event->button.time);
	gtk_main ();

	gtk_widget_destroy (menu);
	
	return i;
}
109
110
111
112
113
114
115
116
117
118
119
120

gboolean
range_contains (Range *range, int col, int row)
{
	if ((col >= range->start_col) &&
	    (col <= range->end_col)   &&
	    (row >= range->start_row) &&
	    (row <= range->end_row))
		return TRUE;

	return FALSE;
}
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

char *
font_change_component (char *fontname, int idx, char *newvalue)
{
	char *components [15];
	char *new = g_strdup (fontname), *res;
	char *p = new;
	int  n = 0, i, len;

	/* split the font name */
	for (;*p; p++){
		if (*p == '-'){
			*p = 0;
			p++;
			components [n++] = p;
		}
	}

	/* Change the value */
	components [idx] = newvalue;

	/* reassemble */
	len = 1;
	for (i = 0; i < n; i++){
		len += strlen (components [i]) + 1;
	}
	len++;
	res = g_malloc (len);
	res [0] = '-';
	res [1] = 0;
	
	for (i = 0; i < n; i++){
		strcat (res, components [i]);
		if (i + 1 != n)
			strcat (res, "-");
	}
	g_free (new);

	return res;
}

char *
font_get_bold_name (char *fontname)
{
	char *f;
	
	f = font_change_component (fontname, 2, "bold");

	return f;
}