Compare commits

...

3 Commits

Author SHA1 Message Date
be6f3dfe99
Invoke a file open dialog on C-o/o 2021-09-17 20:08:56 +02:00
ddcc878424
Make the background black, center the image
It might be a good idea to make the colour adjustable, at least being
able to switch to white, for certain partly-transparent pictures.
2021-09-16 20:14:06 +02:00
a6982bcc3b
Make <q> and <Escape> quit, update README 2021-09-16 19:43:36 +02:00
3 changed files with 75 additions and 12 deletions

View File

@ -29,6 +29,9 @@ Low priority:
- display 16-bit pictures smoothly, using the 30-bit depth under X.org
- make RAW as fast as it can possibly be
- load everything that resembles a picture, potentially even play video
- port to something less hostile than the current GNOME stack, such as SDL,
although it may involve a lot of reimplemented code,
or result in reduced functionality
Non-goals:

View File

@ -88,8 +88,25 @@ fastiv_view_draw(GtkWidget *widget, cairo_t *cr)
if (!self->surface)
return TRUE;
// TODO(p): Make this adjustable later.
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
int w = cairo_image_surface_get_width(self->surface);
int h = cairo_image_surface_get_height(self->surface);
double x = 0;
double y = 0;
if (w < allocation.width)
x = (allocation.width - w) / 2;
if (h < allocation.height)
y = (allocation.height - h) / 2;
// TODO(p): Times the zoom.
cairo_set_source_surface(cr, self->surface, 0, 0);
cairo_set_source_surface(cr, self->surface, x, y);
cairo_paint(cr);
return TRUE;
}

View File

@ -48,10 +48,44 @@ exit_fatal(const gchar *format, ...)
// --- Main --------------------------------------------------------------------
struct {
// TODO(p): Add some state variables.
// - Directory filenames, index within the list.
// TODO(p): Directory filenames, index within the list, ...
GtkWidget *window;
GtkWidget *view;
} g;
static void
open(const gchar *path)
{
GError *error = NULL;
if (!fastiv_view_open(FASTIV_VIEW(g.view), path, &error)) {
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(g.window),
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
gtk_window_set_title(GTK_WINDOW(g.window), path);
}
static void
on_open(void)
{
GtkWidget *dialog = gtk_file_chooser_dialog_new("Open file",
GTK_WINDOW(g.window), GTK_FILE_CHOOSER_ACTION_OPEN,
"_Cancel", GTK_RESPONSE_CANCEL,
"_Open", GTK_RESPONSE_ACCEPT, NULL);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
gchar *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
open(path);
g_free(path);
}
gtk_widget_destroy(dialog);
}
int
main(int argc, char *argv[])
{
@ -79,10 +113,22 @@ main(int argc, char *argv[])
gtk_window_set_default_icon_name(PROJECT_NAME);
GtkWidget *view = g_object_new(FASTIV_TYPE_VIEW, NULL);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(window), view);
g.view = g_object_new(FASTIV_TYPE_VIEW, NULL);
g.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(g.window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(g.window), g.view);
// The references to closures are initially floating and sunk on connect.
GtkAccelGroup *accel_group = gtk_accel_group_new();
gtk_accel_group_connect(accel_group, GDK_KEY_Escape, 0, 0,
g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
gtk_accel_group_connect(accel_group, GDK_KEY_q, 0, 0,
g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
gtk_accel_group_connect(accel_group, GDK_KEY_o, 0, 0,
g_cclosure_new(G_CALLBACK(on_open), NULL, NULL));
gtk_accel_group_connect(accel_group, GDK_KEY_o, GDK_CONTROL_MASK, 0,
g_cclosure_new(G_CALLBACK(on_open), NULL, NULL));
gtk_window_add_accel_group(GTK_WINDOW(g.window), accel_group);
// TODO(p): Load directory entries, store in `g`.
// - Only when there's just one filename.
@ -104,13 +150,10 @@ main(int argc, char *argv[])
g_dir_close(dir);
}
if (!fastiv_view_open(FASTIV_VIEW(view), files[0], &error))
g_printerr("error: %s\n", error->message);
else
gtk_window_set_title(GTK_WINDOW(window), files[0]);
open(files[0]);
}
gtk_widget_show_all(window);
gtk_widget_show_all(g.window);
gtk_main();
return 0;
}