/**
*
* gtk.c: A simple exemplar GTK program which uses all features of the
* GtkPaperTape widget in one window. It does not implement any
* extra features, except of:
*
* * handling of command line options by using the glib parser, thus
* * being able to read from stdin or any files
* * an additional menu item to open files in the GUI
*
* rewritten from gtkprogram.c on 21.06.2008
* Started 05.06.2008 13:13 (the first new project in the SVN)
* Copyright (C) 2008 Sven Köppel
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program; if not, see
* .
*
**/
#include
#include "gtkpapertape.h"
#define _(a) (a)
gboolean open_file_dialog(GtkWidget *menuitem, GtkPaperTape* papertape) {
/**
* displays an "open file" dialog which runs gtk_paper_tape_read_from_file() if success,
* is run by the menu item "open file"
**/
GtkWidget *chooser;
chooser = gtk_file_chooser_dialog_new(
_("Select (binary) file to display as a paper tape"),
GTK_WINDOW(papertape->parent_window),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);
if(gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_OK) {
char *filename;
FILE *file;
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (chooser));
if(filename == NULL) {
g_printf(_("No file selected to open.\n"));
gtk_widget_destroy(GTK_WIDGET(chooser));
return FALSE;
} else {
gtk_paper_tape_read_from_file(papertape, filename);
g_free(filename);
gtk_widget_destroy(GTK_WIDGET(chooser));
return TRUE;
}
}
gtk_widget_destroy(chooser);
return FALSE;
} // open_file_dialog
int main(int argc, char *argv[]) {
GtkWidget *window, *mainbox, *statusbar;
GtkPaperTape *papertape;
gtk_init(&argc, &argv);
// main window generation
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 600);
gtk_window_set_title(GTK_WINDOW(window), "GtkPaperTape Demo");
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
// generate main box (menubar + papertape whole widget)
mainbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), mainbox);
gtk_widget_show(mainbox);
// main widget generation
papertape = GTK_PAPER_TAPE(gtk_paper_tape_new(window));
// menus start
{
GtkWidget *menu, *menubar, *menuitem;
// menus
menubar = gtk_menu_bar_new();
gtk_box_pack_start(GTK_BOX(mainbox), menubar, FALSE, TRUE, 0);
gtk_widget_show(menubar);
// FIRST menu: File {{{
menu = gtk_menu_new();
// Oeffnen
g_signal_connect(G_OBJECT(fast_stock_menuitem(menu, GTK_STOCK_OPEN)),
"activate", G_CALLBACK(open_file_dialog), papertape);
fast_menu_seperator(menu);
gtk_paper_tape_menu_export(papertape, menu);
fast_menu_seperator(menu);
g_signal_connect(G_OBJECT(fast_stock_menuitem(menu, GTK_STOCK_QUIT)),
"activate", G_CALLBACK(gtk_main_quit), NULL);
menuitem = gtk_menu_item_new_with_mnemonic("_Datei");
gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), menu);
gtk_menu_shell_append(GTK_MENU_SHELL (menubar), menuitem);
gtk_widget_show (menuitem);
// Zweites Menue: Ansicht
menu = gtk_menu_new();
gtk_paper_tape_menu_view(papertape, menu);
menuitem = gtk_menu_item_new_with_mnemonic("_Ansicht");
gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), menu);
gtk_menu_shell_append(GTK_MENU_SHELL (menubar), menuitem);
gtk_widget_show (menuitem);
// Drittes Menue: Farben
menu = gtk_menu_new();
gtk_paper_tape_menu_colors(papertape, menu);
menuitem = gtk_menu_item_new_with_mnemonic("_Farben");
gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), menu);
gtk_menu_shell_append(GTK_MENU_SHELL (menubar), menuitem);
gtk_widget_show (menuitem);
}
// menus end
// attach papertape whole widget to mainbox
gtk_box_pack_start(GTK_BOX(mainbox),
GTK_WIDGET(gtk_paper_tape_get_whole_box(GTK_PAPER_TAPE(papertape))),
TRUE, TRUE, 0);
// parse command options
{
char *filename = NULL;
gboolean read_from_stdin = FALSE;
GError *error = NULL;
GOptionContext *context;
byte_t show_mystart = 0; // eigene Startprozedur anzeigen?
GOptionEntry option_entries[] = {
{ "filename", 'f', 0, G_OPTION_ARG_FILENAME, &filename, _("file to open"), NULL },
{ "stdin", 's', 0, G_OPTION_ARG_NONE, &read_from_stdin, _("read from standard input"), NULL },
{ NULL }
};
context = g_option_context_new(_(" - GtkPaperTape visualisation"));
g_option_context_add_main_entries(context, option_entries, NULL);
g_option_context_add_group(context, gtk_get_option_group(TRUE));
g_option_context_parse(context, &argc, &argv, &error);
if(read_from_stdin) {
printf(_("%s: Reading data from standard input, will open window after EOF.\n"), argv[0]);
byte_t *data;
int length = file_get_contents(stdin, &data);
gtk_paper_tape_set_data(papertape, length, data);
} else if(filename != NULL) {
// eine Datei einlesen
printf(_("Reading from file '%s'\n"), filename);
if(!gtk_paper_tape_read_from_file(papertape, argv[1])) {
printf(_("Failed to read from file!\n"));
return 1;
}
}
}
// end of parsing command options
gtk_widget_show(window);
gtk_main();
return 0;
}