Updated file dialogs.

This commit is contained in:
leo 2012-12-01 02:45:20 +00:00
parent 345fa3c11f
commit 13b48e77a8

View file

@ -1,13 +1,3 @@
//
// This file holds all the dialogs that are called
// from the 'File' submenu:
//
// - File Open Dialog
// - File Save Dialog
// - Save Picture Dialog
// - Piece Library Manager
//
#include "lc_global.h" #include "lc_global.h"
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <unistd.h> #include <unistd.h>
@ -22,14 +12,13 @@
// ============================================================================= // =============================================================================
// Open Project Dialog // Open Project Dialog
static void openprojectdlg_select (GtkCList *clist, gint row, gint col, GdkEvent *event, GtkPreview *preview) static void openprojectdlg_preview(GtkFileChooser* dlg, GtkPreview* preview)
{ {
GtkWidget *parent = gtk_widget_get_toplevel (GTK_WIDGET (clist)); char *filename, *p;
const char *filename, *p;
bool loaded = false; bool loaded = false;
Image image; Image image;
filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (parent)); filename = gtk_file_chooser_get_preview_filename(dlg);
p = strrchr (filename, '.'); p = strrchr (filename, '.');
if ((p != NULL) && (g_strcasecmp (p+1, "lcd") == 0)) if ((p != NULL) && (g_strcasecmp (p+1, "lcd") == 0))
@ -87,9 +76,11 @@ static void openprojectdlg_select (GtkCList *clist, gint row, gint col, GdkEvent
file.Close(); file.Close();
} }
g_free(filename);
if (loaded == false) if (loaded == false)
{ {
GtkWidget *w = GTK_WIDGET (preview); GtkWidget *w = GTK_WIDGET(preview);
guchar row[360]; guchar row[360];
for (int x = 0; x < 120; x++) for (int x = 0; x < 120; x++)
@ -100,8 +91,8 @@ static void openprojectdlg_select (GtkCList *clist, gint row, gint col, GdkEvent
} }
for (int y = 0; y < 100; y++) for (int y = 0; y < 100; y++)
gtk_preview_draw_row (preview, row, 0, y, 120); gtk_preview_draw_row(preview, row, 0, y, 120);
gtk_widget_draw (w, NULL); gtk_widget_draw(w, NULL);
} }
else else
{ {
@ -109,70 +100,55 @@ static void openprojectdlg_select (GtkCList *clist, gint row, gint col, GdkEvent
gtk_preview_draw_row (preview, image.GetData ()+y*360, 0, y, 120); gtk_preview_draw_row (preview, image.GetData ()+y*360, 0, y, 120);
gtk_widget_draw (GTK_WIDGET (preview), NULL); gtk_widget_draw (GTK_WIDGET (preview), NULL);
} }
gtk_file_chooser_set_preview_widget_active(dlg, loaded);
} }
int openprojectdlg_execute (char* filename) int openprojectdlg_execute (char* filename)
{ {
GtkWidget *dlg, *preview, *vbox, *frame, *frame2, *hbox; GtkWidget* dlg;
int ret = LC_CANCEL, loop = 1, len; int ret;
dlg = gtk_file_selection_new ("Open Project"); dlg = gtk_file_chooser_dialog_new("Open Project", GTK_WINDOW(((GtkWidget*)*main_window)), GTK_FILE_CHOOSER_ACTION_OPEN,
gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (((GtkWidget*)(*main_window)))); GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
gtk_signal_connect (GTK_OBJECT (dlg), "delete_event",
GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (dlg)->ok_button), "clicked",
GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (LC_OK));
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (dlg)->cancel_button), "clicked",
GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (LC_CANCEL));
gtk_object_set_data (GTK_OBJECT (dlg), "loop", &loop);
gtk_object_set_data (GTK_OBJECT (dlg), "ret", &ret);
// add preview support gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dlg), filename);
hbox = GTK_FILE_SELECTION (dlg)->file_list->parent->parent;
vbox = gtk_vbox_new (FALSE, 0); GtkFileFilter* filter = gtk_file_filter_new();
gtk_widget_show (vbox); gtk_file_filter_add_pattern(filter, "*.lcd");
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, TRUE, 0); gtk_file_filter_set_name(filter, "LeoCAD Files");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dlg), filter);
frame = gtk_frame_new ("Preview"); filter = gtk_file_filter_new();
gtk_widget_show (frame); gtk_file_filter_add_pattern(filter, "*.dat");
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, FALSE, 0); gtk_file_filter_add_pattern(filter, "*.ldr");
gtk_file_filter_set_name(filter, "LDraw Files");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
frame2 = gtk_frame_new (NULL); filter = gtk_file_filter_new();
gtk_widget_show (frame2); gtk_file_filter_add_pattern(filter, "*");
gtk_container_add (GTK_CONTAINER (frame), frame2); gtk_file_filter_set_name(filter, "All Files");
gtk_container_border_width (GTK_CONTAINER (frame2), 5); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_NONE);
preview = gtk_preview_new (GTK_PREVIEW_COLOR); GtkWidget* preview = gtk_preview_new(GTK_PREVIEW_COLOR);
gtk_container_add (GTK_CONTAINER (frame2), preview); gtk_preview_size(GTK_PREVIEW(preview), 120, 100);
gtk_preview_size (GTK_PREVIEW (preview), 120, 100); gtk_widget_show(preview);
gtk_widget_show (preview); gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dlg), preview);
g_signal_connect(dlg, "update-preview", G_CALLBACK(openprojectdlg_preview), preview);
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (dlg)->file_list), "select_row", if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK)
GTK_SIGNAL_FUNC (openprojectdlg_select), preview);
len = strlen (filename);
if (len != 0)
{ {
if (filename[len-1] != '/') char* dlgfilename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
strcat (filename, "/"); strcpy(filename, dlgfilename);
gtk_file_selection_set_filename (GTK_FILE_SELECTION (dlg), filename); g_free(dlgfilename);
ret = LC_OK;
} }
else
ret = LC_CANCEL;
gtk_widget_show (dlg); gtk_widget_destroy(dlg);
gtk_grab_add (dlg);
while (loop)
gtk_main_iteration ();
if (ret == LC_OK)
strcpy (filename, gtk_file_selection_get_filename (GTK_FILE_SELECTION (dlg)));
gtk_grab_remove (dlg);
gtk_widget_destroy (dlg);
return ret; return ret;
} }
@ -180,12 +156,62 @@ int openprojectdlg_execute (char* filename)
// ============================================================================= // =============================================================================
// Save Project Dialog // Save Project Dialog
static void saveprojectdlg_preview (GtkToggleButton *button, gpointer data) int saveprojectdlg_execute(char* filename)
{ {
Sys_ProfileSaveInt ("Default", "Save Preview", gtk_toggle_button_get_active (button)); GtkWidget* dlg;
int ret;
dlg = gtk_file_chooser_dialog_new("Save Project", GTK_WINDOW(((GtkWidget*)*main_window)), GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, NULL);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dlg), TRUE);
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dlg), filename);
GtkFileFilter* filter = gtk_file_filter_new();
gtk_file_filter_add_pattern(filter, "*.lcd");
gtk_file_filter_set_name(filter, "LeoCAD Files");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dlg), filter);
filter = gtk_file_filter_new();
gtk_file_filter_add_pattern(filter, "*.dat");
gtk_file_filter_add_pattern(filter, "*.ldr");
gtk_file_filter_set_name(filter, "LDraw Files");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
filter = gtk_file_filter_new();
gtk_file_filter_add_pattern(filter, "*");
gtk_file_filter_set_name(filter, "All Files");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter);
GtkWidget* check = gtk_check_button_new_with_label("Save Preview");
gtk_widget_show(check);
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dlg), check);
if (Sys_ProfileLoadInt("Default", "Save Preview", 0))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK)
{
Sys_ProfileSaveInt("Default", "Save Preview", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check)));
char* dlgfilename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
strcpy(filename, dlgfilename);
g_free(dlgfilename);
ret = LC_OK;
}
else
ret = LC_CANCEL;
gtk_widget_destroy(dlg);
return ret;
} }
// used by the save project and save picture dialogs // =============================================================================
// Save Picture Dialog
static void savefiledlg_ok (GtkWidget *widget, gpointer data) static void savefiledlg_ok (GtkWidget *widget, gpointer data)
{ {
GtkWidget *parent; GtkWidget *parent;
@ -204,54 +230,6 @@ static void savefiledlg_ok (GtkWidget *widget, gpointer data)
*ret = GPOINTER_TO_INT (data); *ret = GPOINTER_TO_INT (data);
} }
int saveprojectdlg_execute (char* filename)
{
GtkWidget *dlg, *check;
int ret = LC_CANCEL, loop = 1;
dlg = gtk_file_selection_new ("Save Project");
gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (((GtkWidget*)(*main_window))));
gtk_signal_connect (GTK_OBJECT (dlg), "delete_event",
GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (dlg)->ok_button), "clicked",
GTK_SIGNAL_FUNC (savefiledlg_ok), GINT_TO_POINTER (LC_OK));
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (dlg)->cancel_button), "clicked",
GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (LC_CANCEL));
gtk_object_set_data (GTK_OBJECT (dlg), "loop", &loop);
gtk_object_set_data (GTK_OBJECT (dlg), "ret", &ret);
// add preview checkbox
check = gtk_check_button_new_with_label ("Save Preview");
gtk_widget_show (check);
gtk_box_pack_start (GTK_BOX (GTK_FILE_SELECTION (dlg)->main_vbox), check, FALSE, FALSE, 0);
int i = Sys_ProfileLoadInt ("Default", "Save Preview", 0);
if (i != 0)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
gtk_signal_connect (GTK_OBJECT (check), "toggled", GTK_SIGNAL_FUNC (saveprojectdlg_preview), NULL);
gtk_file_selection_set_filename (GTK_FILE_SELECTION (dlg), filename);
gtk_widget_show (dlg);
gtk_grab_add (dlg);
while (loop)
gtk_main_iteration ();
if (ret == LC_OK)
strcpy (filename, gtk_file_selection_get_filename (GTK_FILE_SELECTION (dlg)));
gtk_grab_remove (dlg);
gtk_widget_destroy (dlg);
return ret;
}
// =============================================================================
// Save Picture Dialog
static void savepicturedlg_options (GtkWidget *widget, gpointer data) static void savepicturedlg_options (GtkWidget *widget, gpointer data)
{ {
imageoptsdlg_execute (gtk_widget_get_toplevel(widget), data, false); imageoptsdlg_execute (gtk_widget_get_toplevel(widget), data, false);
@ -340,7 +318,3 @@ int savepicturedlg_execute (void* param)
return ret; return ret;
} }
// =============================================================================
// Piece Library Manager