leocad/linux/dlgfile.cpp

297 lines
8.6 KiB
C++
Raw Normal View History

2012-03-23 01:53:05 +01:00
#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "system.h"
#include "dialogs.h"
2012-03-23 01:53:05 +01:00
#include "lc_file.h"
2011-09-07 23:06:51 +02:00
#include "image.h"
#include "main.h"
// =============================================================================
// Open Project Dialog
2012-12-01 03:45:20 +01:00
static void openprojectdlg_preview(GtkFileChooser* dlg, GtkPreview* preview)
2011-09-07 23:06:51 +02:00
{
char *filename, *p = NULL;
2012-12-01 03:45:20 +01:00
bool loaded = false;
Image image;
filename = gtk_file_chooser_get_preview_filename(dlg);
if (filename)
p = strrchr(filename, '.');
2013-01-06 20:24:25 +01:00
if ((p != NULL) && (g_ascii_strcasecmp(p+1, "lcd") == 0))
2012-12-01 03:45:20 +01:00
{
float fv;
char id[32];
lcDiskFile file;
file.Open(filename, "rb");
file.ReadBuffer(id, 32);
sscanf(strchr(id, ' '), "%f", &fv);
2012-12-01 03:45:20 +01:00
if (fv > 0.4f)
{
file.ReadFloats(&fv, 1);
if (fv > 0.7f)
{
lcuint32 dwPosition;
file.Seek(-4, SEEK_END);
file.ReadU32(&dwPosition, 1);
file.Seek(dwPosition, SEEK_SET);
2012-12-01 03:45:20 +01:00
if (dwPosition != 0)
{
if (fv < 1.0f)
{
file.Seek(54, SEEK_CUR);
2012-12-01 03:45:20 +01:00
image.Allocate(120, 100, false);
file.ReadBuffer(image.GetData(), 36000);
2012-12-01 03:45:20 +01:00
for (int y = 0; y < 50; y++)
for (int x = 0; x < 120; x++)
{
unsigned char *from = image.GetData() + x*3 + y*360;
unsigned char *to = image.GetData() + x*3 + (100-y-1)*360;
unsigned char tmp[3] = { from[0], from[1], from[2] };
from[0] = to[2];
from[1] = to[1];
from[2] = to[0];
to[0] = tmp[2];
to[1] = tmp[1];
to[2] = tmp[0];
}
loaded = true;
}
else
{
loaded = image.FileLoad(file);
2012-12-01 03:45:20 +01:00
}
}
}
}
file.Close();
}
g_free(filename);
if (loaded == false)
{
GtkWidget *w = GTK_WIDGET(preview);
guchar row[360];
for (int x = 0; x < 120; x++)
{
row[x*3] = w->style->bg[0].red/0xFF;
row[x*3+1] = w->style->bg[0].green/0xFF;
row[x*3+2] = w->style->bg[0].blue/0xFF;
}
for (int y = 0; y < 100; y++)
gtk_preview_draw_row(preview, row, 0, y, 120);
gtk_widget_draw(w, NULL);
}
else
{
for (int y = 0; y < 100; y++)
gtk_preview_draw_row(preview, image.GetData()+y*360, 0, y, 120);
gtk_widget_draw(GTK_WIDGET(preview), NULL);
2012-12-01 03:45:20 +01:00
}
gtk_file_chooser_set_preview_widget_active(dlg, loaded);
2011-09-07 23:06:51 +02:00
}
int openprojectdlg_execute(char* filename)
2011-09-07 23:06:51 +02:00
{
2012-12-01 03:45:20 +01:00
GtkWidget* dlg;
int ret;
dlg = gtk_file_chooser_dialog_new("Open Project", GTK_WINDOW(((GtkWidget*)*main_window)), GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
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* preview = gtk_preview_new(GTK_PREVIEW_COLOR);
gtk_preview_size(GTK_PREVIEW(preview), 120, 100);
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);
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK)
{
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;
}
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
// =============================================================================
// Save Project Dialog
int saveprojectdlg_execute(char* filename)
{
GtkWidget* dlg;
int ret;
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
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);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dlg), TRUE);
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dlg), filename);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
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);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
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);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
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);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
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);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
if (Sys_ProfileLoadInt("Default", "Save Preview", 0))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK)
{
Sys_ProfileSaveInt("Default", "Save Preview", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check)));
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
char* dlgfilename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
strcpy(filename, dlgfilename);
g_free(dlgfilename);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
ret = LC_OK;
}
else
ret = LC_CANCEL;
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
gtk_widget_destroy(dlg);
2011-09-07 23:06:51 +02:00
2012-12-01 03:45:20 +01:00
return ret;
2011-09-07 23:06:51 +02:00
}
// =============================================================================
2012-12-01 03:45:20 +01:00
// Save Picture Dialog
2011-09-07 23:06:51 +02:00
2012-12-08 00:25:36 +01:00
static void savepicturedlg_options(GtkWidget *widget, gpointer data)
2011-09-07 23:06:51 +02:00
{
2012-12-08 00:25:36 +01:00
imageoptsdlg_execute(gtk_widget_get_toplevel(widget), data, false);
}
2011-09-07 23:06:51 +02:00
2012-12-08 00:25:36 +01:00
int savepicturedlg_execute(void* param)
{
GtkWidget* dlg;
int ret;
2011-09-07 23:06:51 +02:00
2012-12-08 00:25:36 +01:00
dlg = gtk_file_chooser_dialog_new("Save Picture", GTK_WINDOW(((GtkWidget*)*main_window)), GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, NULL);
2011-09-07 23:06:51 +02:00
2012-12-08 00:25:36 +01:00
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dlg), TRUE);
// gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dlg), filename);
GtkWidget* hbox = gtk_hbox_new(FALSE, 10);
gtk_widget_show(hbox);
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dlg), hbox);
GtkWidget* button = gtk_button_new_with_label ("Image Options");
gtk_widget_show(button);
// GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(savepicturedlg_options), param);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
unsigned long image = Sys_ProfileLoadInt ("Default", "Image Options", 1|LC_IMAGE_TRANSPARENT);
LC_IMAGEDLG_OPTS* opts = (LC_IMAGEDLG_OPTS*)param;
opts->width = Sys_ProfileLoadInt ("Default", "Image Width", gdk_screen_width ());
opts->height = Sys_ProfileLoadInt ("Default", "Image Height", gdk_screen_height ());
opts->imopts.quality = Sys_ProfileLoadInt ("Default", "JPEG Quality", 70);
opts->imopts.interlaced = (image & LC_IMAGE_PROGRESSIVE) != 0;
opts->imopts.transparent = (image & LC_IMAGE_TRANSPARENT) != 0;
opts->imopts.truecolor = (image & LC_IMAGE_HIGHCOLOR) != 0;
opts->imopts.pause = (float)Sys_ProfileLoadInt ("Default", "AVI Pause", 100)/100;
opts->imopts.format = (unsigned char)(image & ~(LC_IMAGE_MASK));
2011-09-07 23:06:51 +02:00
2012-12-08 00:25:36 +01:00
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK)
{
char* dlgfilename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
strcpy(opts->filename, dlgfilename);
g_free(dlgfilename);
2011-09-07 23:06:51 +02:00
2012-12-08 00:25:36 +01:00
char ext[5], *p;
if (strlen (opts->filename) == 0)
ret = LC_CANCEL;
p = strrchr(opts->filename, '.');
if (p != NULL)
{
strcpy (ext, p+1);
strlwr (ext);
}
else
ext[0] = '\0';
if ((strcmp (ext, "jpg") != 0) && (strcmp (ext, "jpeg") != 0) &&
(strcmp (ext, "bmp") != 0) && (strcmp (ext, "gif") != 0) &&
(strcmp (ext, "png") != 0) && (strcmp (ext, "avi") != 0))
{
switch (opts->imopts.format)
{
case LC_IMAGE_BMP: strcat(opts->filename, ".bmp"); break;
case LC_IMAGE_GIF: strcat(opts->filename, ".gif"); break;
case LC_IMAGE_JPG: strcat(opts->filename, ".jpg"); break;
case LC_IMAGE_PNG: strcat(opts->filename, ".png"); break;
case LC_IMAGE_AVI: strcat(opts->filename, ".avi"); break;
}
}
ret = LC_OK;
}
else
ret = LC_CANCEL;
gtk_widget_destroy(dlg);
return ret;
2011-09-07 23:06:51 +02:00
}