void SludgeApplication::on_open()
{
	if (!reallyClose()) return;
	GtkWidget *dialog;
	GtkFileFilter *filter;

	dialog = gtk_file_chooser_dialog_new ("Open File",
				      NULL,
				      GTK_FILE_CHOOSER_ACTION_OPEN,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				      NULL);

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, getFilterName());
	gtk_file_filter_add_pattern(filter, getFilterPattern());
	gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);

	if (currentFolder[0] != 0)
	{
		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), currentFolder);
	}

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename;
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		flipBackslashes(&filename);
		open(filename);
		g_free (filename);
	}
	gtk_widget_destroy (dialog);
}
void SludgeFloorMaker::on_filechooserbutton_file_set(GtkFileChooser *theChooser)
{	
	char *filename;
	gboolean success = 0;

    filename = gtk_file_chooser_get_filename(theChooser);
	if (filename == NULL) return;

	flipBackslashes(&filename);

	if (strlen(filename) > 4) {
		char * extension = filename + strlen(filename) - 4;
		if        (!strcmp(extension, ".png") || !strcmp(extension, ".PNG")) {
			success = loadSpriteFromPNG(filename, &backdrop, 0);
		} else if (!strcmp(extension, ".tga") || !strcmp(extension, ".TGA")) {
			success = loadSpriteFromTGA(filename, &backdrop, 0);
		}
	} else {
		errorBox("Can't load image", "I don't recognise the file type. TGA and PNG are the supported file types.");
	}

	if (success) {
		setFolderFromFilename(filename);
		backdrop.sprites[0].height = -backdrop.sprites[0].height;
		activateZoomButtons(backdrop.sprites[0].width, -backdrop.sprites[0].height);
		showStatusbar(backdrop.sprites[0].width, -backdrop.sprites[0].height);
		on_zoom_fit_clicked();
		reshape();
		render_timer_event(theDrawingarea);
	}

	g_free(filename);
}
void SludgeZBufferMaker::postNew()
{
	GtkWidget *dialog;

	dialog = gtk_file_chooser_dialog_new("Load file to zBuffer",
				      NULL,
				      GTK_FILE_CHOOSER_ACTION_OPEN,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				      NULL);

	setFileChooserFilters(GTK_FILE_CHOOSER (dialog), FALSE, TRUE);

	if (currentFolder[0] != 0)
	{
		gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (dialog), currentFolder);
	}

	if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename;
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
		flipBackslashes(&filename);

		if (!loadZBufferFromTGA(filename, &backdrop))
		{
			errorBox("Error", "Loading TGA file failed.");
		} else {
			setFolderFromFilename(filename);
			setFilename(filename);
			int i, lastSlash;
			for (i = 0; filename[i] != 0; i++)
			{
				if (filename[i] == '/')
				lastSlash = i;
			}		
			currentShortname[i-lastSlash-4] = 'z';
			currentShortname[i-lastSlash-3] = 'b';
			currentShortname[i-lastSlash-2] = 'u';
			currentFilename[0] = 0;
			gtk_window_set_title(GTK_WINDOW(theWindow), getWindowTitle());
			setFileChanged();
			postOpen();
		}

		g_free(filename);
	}
	gtk_widget_destroy(dialog);

	setupButtons();
}
void SludgeProjectManager::postNew()
{
	GtkWidget *dialog;
	GtkFileFilter *filter;
	gboolean success = FALSE;

	dialog = gtk_file_chooser_dialog_new("New SLUDGE Project",
				      NULL,
				      GTK_FILE_CHOOSER_ACTION_SAVE,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
				      NULL);
	gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER (dialog), TRUE);

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, getFilterName());
	gtk_file_filter_add_pattern(filter, getFilterPattern());
	gtk_file_chooser_set_filter(GTK_FILE_CHOOSER (dialog), filter);

	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), getUntitledFilename());

	if (currentFolder[0] != 0)
	{
		gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (dialog), currentFolder);
	}

	if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename;
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
		flipBackslashes(&filename);

		doNewProject(filename, fileList, &fileListNum);
		listChanged(FILE_TREEVIEW);
		listChanged(RESOURCE_TREEVIEW);
		gtk_list_store_clear(errorsListStore);
		gtk_widget_set_sensitive(projectRunGameItem, FALSE);

		setFilename(filename);
		setFolderFromFilename(filename);

		g_free(filename);
		success = TRUE;
	}
	gtk_widget_destroy(dialog);

	if (success)
		on_project_settings();

	setupButtons();
}
void SludgeApplication::saveToFile()
{
	GtkWidget *dialog;
	GtkFileFilter *filter;

	dialog = gtk_file_chooser_dialog_new ("Save File",
				      NULL,
				      GTK_FILE_CHOOSER_ACTION_SAVE,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
				      NULL);
	gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, getFilterName());
	gtk_file_filter_add_pattern(filter, getFilterPattern());
	gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);

	if (currentShortname[0] != 0)
	{
		gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), currentShortname);
	}
	if (currentFolder[0] != 0)
	{
		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), currentFolder);
	}

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename;
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		flipBackslashes(&filename);

		if (!saveFile (filename))
		{
			errorBox ("Error", "Saving file failed.");
		}
		else
		{
			setFilename(filename);
			setFolderFromFilename(filename);
		}

		g_free (filename);
	}
	gtk_widget_destroy (dialog);
}
void SludgeProjectManager::on_add_file_clicked()
{
	GtkWidget *dialog;
	GtkFileFilter *filter, *slufilter, *sldfilter, *trafilter;

	dialog = gtk_file_chooser_dialog_new("Add file to SLUDGE project",
				      NULL,
				      GTK_FILE_CHOOSER_ACTION_OPEN,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				      NULL);

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, "SLU/SLD/TRA files");
	gtk_file_filter_add_pattern(filter, "*.[sS][lL][uU]");
	gtk_file_filter_add_pattern(filter, "*.[sS][lL][dD]");
	gtk_file_filter_add_pattern(filter, "*.[tT][rR][aA]");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);

	slufilter = gtk_file_filter_new();
	gtk_file_filter_set_name(slufilter, "SLUDGE scripts (*.slu)");
	gtk_file_filter_add_pattern(slufilter, "*.[sS][lL][uU]");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), slufilter);

	sldfilter = gtk_file_filter_new();
	gtk_file_filter_set_name(sldfilter, "SLUDGE constant definition files (*.sld)");
	gtk_file_filter_add_pattern(sldfilter, "*.[sS][lL][dD]");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), sldfilter);

	trafilter = gtk_file_filter_new();
	gtk_file_filter_set_name(trafilter, "SLUDGE translation files (*.tra)");
	gtk_file_filter_add_pattern(trafilter, "*.[tT][rR][aA]");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), trafilter);

	gtk_file_chooser_set_filter(GTK_FILE_CHOOSER (dialog), filter);

	gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER (dialog), TRUE);

	if (currentFolder[0] != 0)
	{
		gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (dialog), currentFolder);
	}

	if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		GSList *filenames;
		char *filename;
		filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER (dialog));

		for (int j = 0; j < g_slist_length(filenames); j++) {
			filename = (char *) g_slist_nth(filenames, j)->data;
			flipBackslashes(&filename);

			getSourceDirFromName(currentFilename);
			addFileToProject(filename, sourceDirectory, fileList, &fileListNum);
			setFileChanged();
		
			g_free(filename);
		}
		g_slist_free(filenames);
	}
	gtk_widget_destroy(dialog);

	listChanged(FILE_TREEVIEW);
}