dialog-stf-format-page.c 12 KB
Newer Older
1
/*
Morten Welinder's avatar
Morten Welinder committed
2
 * dialog-stf-format-page.c : Controls the widgets on the format page of the dialog
3
 *
4
 * Copyright (C) Almer S. Tigelaar <almer@gnome.org>
5 6 7
 *
 */

8 9
#include <gnumeric-config.h>
#include <gnumeric.h>
10
#include "dialog-stf.h"
11 12 13
#include <format.h>
#include <formats.h>
#include <gui-util.h>
14 15 16 17 18 19

/*************************************************************************************************
 * MISC UTILITY FUNCTIONS
 *************************************************************************************************/

/**
20 21
 * format_page_update_preview
 * @pagedata : mother struct
22
 *
23 24
 * Will simply utilize the preview rendering functions to update
 * the preview
25
 *
26 27
 * returns : nothing
 **/
28 29 30 31
static void
format_page_update_preview (DruidPageData_t *pagedata)
{
	FormatInfo_t *info = pagedata->format_info;
32
	GList *list;
33
	GSList *iterator;
34

35 36 37 38 39 40
	stf_preview_colformats_clear (info->format_run_renderdata);
	iterator = info->format_run_list;
	while (iterator) {
		stf_preview_colformats_add (info->format_run_renderdata, iterator->data);
		iterator = g_slist_next (iterator);
	}
41

42
	list = stf_parse_general (info->format_run_parseoptions, pagedata->cur);
43

44
	stf_preview_render (info->format_run_renderdata, list,
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
			    info->format_run_displayrows,
			    pagedata->colcount);

}

/*************************************************************************************************
 * SIGNAL HANDLERS
 *************************************************************************************************/

/**
 * format_page_scroll_value_changed
 * @adjustment : The gtkadjustment that emitted the signal
 * @data : a mother struct
 *
 * This signal responds to changes in the scrollbar and
 * will force a redraw of the preview
 *
 * returns : nothing
 **/
static void
format_page_scroll_value_changed (GtkAdjustment *adjustment, DruidPageData_t *data)
{
	FormatInfo_t *info = data->format_info;

	stf_preview_set_startrow (info->format_run_renderdata, adjustment->value);
	format_page_update_preview (data);
}

/**
 * format_page_canvas_button_press_event
 * @canvas : gnome canvas that emitted the signal
 * @event : a button click event
 * @data : a mother struct
 *
 * This signal responds to a click on the canvas, in fact it will find out what
80
 * column the user clicked on and then select that column in the column list
81 82 83 84 85 86 87 88
 *
 * returns : always TRUE
 **/
static gboolean
format_page_canvas_button_press_event (GnomeCanvas *canvas, GdkEventButton *event, DruidPageData_t *data)
{
	FormatInfo_t *info = data->format_info;
	double worldx, worldy;
89
	int column;
90 91

	gnome_canvas_window_to_world (canvas, event->x, event->y, &worldx, &worldy);
92 93 94 95

	column = stf_preview_get_column_at_x (info->format_run_renderdata, worldx);
	gtk_clist_select_row (info->format_collist, column, 0);
	gnumeric_clist_moveto (info->format_collist, column);
96

97 98
	return TRUE;
}
99

100 101
/**
 * format_page_collist_select_row
Jody Goldberg's avatar
Jody Goldberg committed
102
 * @clist : GtkCList which emitted the signal
103 104 105 106 107 108 109 110 111 112 113 114 115 116
 * @row : row the user selected
 * @column : column the user selected
 * @event : some info on the button the user clicked (unused)
 * @data : Dialog "mother" record
 *
 * this will simply set the gtkentry info->format_format's text to the (char*) format associated
 * with @row (@row is actually the column in the @data->src->sheet *confusing*)
 *
 * returns : nothing
 **/
static void
format_page_collist_select_row (GtkCList *clist, int row, int column, GdkEventButton *event, DruidPageData_t *data)
{
	FormatInfo_t *info = data->format_info;
Jody Goldberg's avatar
Jody Goldberg committed
117 118 119 120 121
	StyleFormat const *colformat = g_slist_nth_data (info->format_run_list, row);
	char *fmt;

	if (!colformat)
		return;
122 123

	stf_preview_set_activecolumn (info->format_run_renderdata, row);
124

125
	gnumeric_clist_moveto (info->format_collist, row);
126

127 128 129 130
	if (info->format_run_manual_change) {
		info->format_run_manual_change = FALSE;
		return;
	}
131

132
	info->format_run_index = row;
Jody Goldberg's avatar
Jody Goldberg committed
133 134 135
	fmt = style_format_as_XL (colformat, TRUE);
	gtk_entry_set_text (info->format_format, fmt);
	g_free (fmt);
136 137 138 139
}

/**
 * format_page_sublist_select_row
Jody Goldberg's avatar
Jody Goldberg committed
140
 * @clist : GtkCList which emitted the signal
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 * @row : row the user selected
 * @column : column the user selected
 * @event : some info on the button the user clicked (unused)
 * @data : Dialog "mother" record
 *
 * If the user selects a different format from @clist, the caption of info->format_format will
 * change to the entry in the @clist the user selected
 *
 * returns : nothing
 **/
static void
format_page_sublist_select_row (GtkCList *clist, int row, int column, GdkEventButton *event, DruidPageData_t *data)
{
	FormatInfo_t *info = data->format_info;
	char *t[1];

	/* User did not select, it was done in the code with gtk_clist_select_row */
	if (info->format_run_manual_change) {
		info->format_run_manual_change = FALSE;
		return;
	}
162

163 164 165
	/* WEIRD THING : when scrolling with keys it will give the right row, but always -1 as column,
	   because we have only one column, always set "column" to 0 for now */
	column = 0;
166

167 168
	gtk_clist_get_text (clist, row, column, t);

169
	info->format_run_sublist_select = FALSE;
170
	if (strcmp (t[0], _("Custom")) != 0)
171
		gtk_entry_set_text (info->format_format, t[0]);
172
	info->format_run_sublist_select = TRUE;
173 174 175 176 177 178
}

/**
 * format_page_format_changed
 * @entry : GtkEntry which emitted the signal
 * @data : Dialog "mother" record
179
 *
180 181 182 183 184 185 186 187 188
 * Updates the selected column on the sheet with the new
 * format the user choose/entered.
 *
 * returns : nothing
 **/
static void
format_page_format_changed (GtkEntry *entry, DruidPageData_t *data)
{
	FormatInfo_t *info = data->format_info;
189

190
	if (info->format_run_index >= 0) {
191 192 193
		int i, found;
		char *t[1];
		GSList *listitem;
Jody Goldberg's avatar
Jody Goldberg committed
194
		char *new_fmt = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1);
195
		StyleFormat *sf;
196 197 198

		listitem = g_slist_nth (info->format_run_list, info->format_run_index);
		g_return_if_fail (listitem != NULL);
199

200
		sf = listitem->data;
201
		if (sf)
202
			style_format_unref (sf);
Jody Goldberg's avatar
Jody Goldberg committed
203

Jody Goldberg's avatar
Jody Goldberg committed
204
		listitem->data = style_format_new_XL (new_fmt, TRUE);
205

Jody Goldberg's avatar
Jody Goldberg committed
206
		gtk_clist_set_text (info->format_collist, info->format_run_index, 1, new_fmt);
207

208 209 210
		gtk_clist_set_column_width (info->format_collist,
					    1,
					    gtk_clist_optimal_column_width (info->format_collist, 1));
211

212 213 214 215
		if (info->format_run_sublist_select) {
			found = 0;
			for (i = 0; i < info->format_sublist->rows; i++) {
				gtk_clist_get_text (info->format_sublist, i, 0, t);
Jody Goldberg's avatar
Jody Goldberg committed
216
				if (strcmp (t[0], new_fmt)==0) {
217 218 219 220
					found = i;
					break;
				}
			}
221

222 223 224 225
			info->format_run_manual_change = TRUE;
			gtk_clist_select_row (info->format_sublist, found, 0);
			gnumeric_clist_moveto (info->format_sublist, found);
		}
226 227

		g_free (new_fmt);
228
	}
229

230 231 232 233 234 235 236 237
	format_page_update_preview (data);
}

/*************************************************************************************************
 * FORMAT EXPORTED FUNCTIONS
 *************************************************************************************************/

/**
238
 * stf_dialog_format_page_prepare
239 240 241 242 243 244 245 246
 * @page : format page
 * @druid : gnome druid hosting @page
 * @data : mother struct
 *
 * This will prepare the widgets on the format page before
 * the page gets displayed
 *
 * returns : nothing
247 248
 **/
void
249
stf_dialog_format_page_prepare (GnomeDruidPage *page, GnomeDruid *druid, DruidPageData_t *data)
250 251 252 253 254
{
	FormatInfo_t *info = data->format_info;
	int listcount, i;

	data->colcount = stf_parse_get_colcount (info->format_run_parseoptions, data->cur);
255

256
	listcount = g_slist_length (info->format_run_list);
257

258 259
	/* If necessary add new items (non-visual) */
	while (listcount <= data->colcount) {
260
		info->format_run_list = g_slist_append (info->format_run_list,
Jody Goldberg's avatar
Jody Goldberg committed
261
							style_format_new_XL (cell_formats[0][0], FALSE));
262 263
		listcount++;
	}
264

265 266
	/* Add new items visual */
	gtk_clist_clear (info->format_collist);
267

268
	for (i = 0; i <= data->colcount; i++) {
269 270 271 272
		StyleFormat *sf;
		char *t[2];

		sf = g_slist_nth_data (info->format_run_list, i);
273
		t[0] = g_strdup_printf ("%d", i);
Jody Goldberg's avatar
Jody Goldberg committed
274
		t[1] = style_format_as_XL (sf, TRUE);
275
		gtk_clist_append (info->format_collist, t);
Jody Goldberg's avatar
Jody Goldberg committed
276
		g_free (t[1]);
277 278 279 280 281
		g_free (t[0]);
	}

	gtk_clist_columns_autosize (info->format_collist);

282
	if (data->lines > LINE_DISPLAY_LIMIT) {
283
		GTK_RANGE (info->format_scroll)->adjustment->upper = LINE_DISPLAY_LIMIT;
284 285
		stf_parse_options_set_lines_to_parse (info->format_run_parseoptions, LINE_DISPLAY_LIMIT);
	} else {
286
		GTK_RANGE (info->format_scroll)->adjustment->upper = data->lines + 1;
287 288
		stf_parse_options_set_lines_to_parse (info->format_run_parseoptions, data->importlines);
	}
289 290 291 292 293 294

	stf_preview_colwidths_clear (info->format_run_renderdata);
	for (i = 0; i < data->colcount + 1; i++) {
		stf_preview_colwidths_add (info->format_run_renderdata, stf_parse_get_colwidth (info->format_run_parseoptions, data->cur, i));
	}

295 296
	info->format_run_manual_change = TRUE;
	gtk_clist_select_row (info->format_collist, 0, 0);
297
	gnumeric_clist_moveto (info->format_collist, 0);
298

299
	info->format_run_index = 0;
300

301
	{
Jody Goldberg's avatar
Jody Goldberg committed
302 303
		StyleFormat const *sf;
		char *fmt;
304 305

		sf = g_slist_nth_data (info->format_run_list, 0);
Jody Goldberg's avatar
Jody Goldberg committed
306 307 308
		fmt = style_format_as_XL (sf, TRUE);
		gtk_entry_set_text (info->format_format, fmt);
		g_free (fmt);
309
	}
310 311 312
}

/**
313
 * stf_dialog_format_page_cleanup
314 315 316 317 318 319 320 321
 * @pagedata : mother struct
 *
 * This should be called when the druid has finished to clean up resources
 * used. In this case the format_run_list data pointers and the format_run_list
 * itself will be freed
 *
 * returns : nothing
 **/
322 323
void
stf_dialog_format_page_cleanup (DruidPageData_t *pagedata)
324
{
325
	FormatInfo_t *info = pagedata->format_info;
326

327
	stf_preview_free (info->format_run_renderdata);
328 329 330
}

/**
331
 * stf_dialog_format_page_init
332 333 334 335 336 337 338 339 340
 * @gui : The glade gui of the dialog
 * @pagedata : pagedata mother struct passed to signal handlers etc.
 *
 * This routine prepares/initializes all widgets on the format Page of the
 * Druid.
 *
 * returns : nothing
 **/
void
341
stf_dialog_format_page_init (GladeXML *gui, DruidPageData_t *pagedata)
342 343
{
	FormatInfo_t *info;
344 345
	char const * const * const * mainiterator = cell_formats;
	char const * const * subiterator;
346 347 348 349 350 351 352 353
	char *temp[1];
	int rownumber;

	g_return_if_fail (gui != NULL);
	g_return_if_fail (pagedata != NULL);
	g_return_if_fail (pagedata->format_info != NULL);

	info = pagedata->format_info;
354

355 356 357
        /* Create/get object and fill information struct */
	info->format_collist       = GTK_CLIST (glade_xml_get_widget (gui, "format_collist"));
	info->format_sublist       = GTK_CLIST (glade_xml_get_widget (gui, "format_sublist"));
358
	info->format_sublistholder = GTK_SCROLLED_WINDOW (glade_xml_get_widget (gui, "format_sublistholder"));
359 360 361 362 363 364
	info->format_format        = GTK_ENTRY (glade_xml_get_widget (gui, "format_format"));

	info->format_canvas = GNOME_CANVAS   (glade_xml_get_widget (gui, "format_canvas"));
	info->format_scroll = GTK_VSCROLLBAR (glade_xml_get_widget (gui, "format_scroll"));

	/* Set properties */
365
	info->format_run_renderdata    = stf_preview_new (info->format_canvas, TRUE);
366 367 368
	info->format_run_list          = NULL;
	info->format_run_index         = -1;
	info->format_run_manual_change = FALSE;
369
	info->format_run_sublist_select = TRUE;
370
	info->format_run_displayrows   = stf_preview_get_displayed_rowcount (info->format_run_renderdata);
371
	info->format_run_parseoptions  = NULL; /*  stf_parse_options_new (); */
372 373 374

        gtk_clist_column_titles_passive (info->format_sublist);

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	rownumber = 0;
	temp[0] = _("Custom");
	gtk_clist_append (info->format_sublist, temp);
	while (*mainiterator) {
		subiterator = *mainiterator;
		while (*subiterator) {
			temp[0] = (char*) *subiterator;
			gtk_clist_append (info->format_sublist, temp);
			subiterator++;
			rownumber++;
		}
		mainiterator++;
	}

	gtk_clist_set_column_justification (info->format_collist, 0, GTK_JUSTIFY_RIGHT);
390

391
	/* Connect signals */
Jody Goldberg's avatar
Jody Goldberg committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	g_signal_connect (G_OBJECT (info->format_format),
		"changed",
		G_CALLBACK (format_page_format_changed), pagedata);
	g_signal_connect (G_OBJECT (info->format_collist),
		"select_row",
		G_CALLBACK (format_page_collist_select_row), pagedata);
	g_signal_connect (G_OBJECT (info->format_sublist),
		"select_row",
		G_CALLBACK (format_page_sublist_select_row), pagedata);
	g_signal_connect (G_OBJECT (info->format_canvas),
		"button_press_event",
		G_CALLBACK (format_page_canvas_button_press_event), pagedata);

	g_signal_connect (G_OBJECT (GTK_RANGE (info->format_scroll)->adjustment),
		"value_changed",
		G_CALLBACK (format_page_scroll_value_changed), pagedata);
408
}