/* created by Stephen Nestinger
This program is modified from itemfactory.c.
The program is in public domain.
*/
#include
#include
#include
#include
#include
#include "common.h"
static GtkWidget *spinner;
static COMMAND_STRUCT command;
static int command_fd;
static int status_fd;
/* Obligatory basic callback */
void send_receive ( void )
{
STATUS_STRUCT status;
command.command_num++;
if (sizeof(command) != write(command_fd, &command, sizeof(command)))
{
g_message ( "Can't write command\n" );
}
if (sizeof(status) == read(status_fd, &status, sizeof(status)))
{
g_message ("status: %d %d\n", status.command_num_echo, status.heartbeat);
}
}
void turn_sound_off ( GtkWidget *w, gpointer data)
{
command.command = SOUND_OFF;
send_receive();
}
void turn_sound_on ( GtkWidget *w, gpointer data)
{
command.command = SOUND_ON;
send_receive();
}
void update ( GtkWidget *w, gpointer data)
{
GtkSpinButton *spin;
spin = GTK_SPIN_BUTTON (spinner);
command.command = SOUND_FREQ;
command.freq = gtk_spin_button_get_value_as_int (spin);
send_receive();
}
/* This is the GtkItemFactoryEntry structure used to generate new menus.
Item 1: The menu path. The letter after the underscore indicates an
accelerator key once the menu is open.
Item 2: The accelerator key for the entry
Item 3: The callback function.
Item 4: The callback action. This changes the parameters with
which the function is called. The default is 0.
Item 5: The item type, used to define what kind of an item it is.
Here are the possible values:
NULL -> "- "
"" -> "
- "
"" -> create a title item
"
- " -> create a simple item
"" -> create a check item
"" -> create a toggle item
"" -> create a radio item
-> path of a radio item to link against
"" -> create a separator
"" -> create an item to hold sub items (optional)
"" -> create a right justified branch
*/
static GtkItemFactoryEntry menu_items[] = {
{ "/_File", NULL, NULL, 0, "" },
{ "/File/_Quit", "Q", gtk_main_quit, 0, NULL },
{ "/_Sound", NULL, NULL, 0, "" },
{ "/_Sound/_On", NULL, turn_sound_on, 0, NULL },
{ "/_Sound/_Off", NULL, turn_sound_off, 0, NULL },
/*
{ "/_File", NULL, NULL, 0, "" },
{ "/File/_New", "N", print_hello, 0, NULL },
{ "/File/_Open", "O", print_hello, 0, NULL },
{ "/File/_Save", "S", print_hello, 0, NULL },
{ "/File/Save _As", NULL, NULL, 0, NULL },
{ "/File/sep1", NULL, NULL, 0, "" },
{ "/File/Quit", "Q", gtk_main_quit, 0, NULL },
{ "/_Options", NULL, NULL, 0, "" },
{ "/Options/Test", NULL, NULL, 0, NULL },
{ "/_Help", NULL, NULL, 0, "" },
{ "/_Help/About", NULL, NULL, 0, NULL },
*/
};
void get_main_menu( GtkWidget *window, GtkWidget **menubar )
{
GtkItemFactory *item_factory;
GtkAccelGroup *accel_group;
gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
accel_group = gtk_accel_group_new ();
/* This function initializes the item factory.
Param 1: The type of menu - can be GTK_TYPE_MENU_BAR, GTK_TYPE_MENU,
or GTK_TYPE_OPTION_MENU.
Param 2: The path of the menu.
Param 3: A pointer to a gtk_accel_group. The item factory sets up
the accelerator table while generating menus.
*/
//item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "", accel_group);
item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "", accel_group);
/* This function generates the menu items. Pass the item factory,
the number of items in the array, the array itself, and any
callback data for the the menu items. */
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
/* Attach the new accelerator group to the window. */
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
if (menubar)
/* Finally, return the actual menu bar created by the item factory. */
*menubar = gtk_item_factory_get_widget (item_factory, "");
}
int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *main_vbox;
GtkWidget *menubar;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *on_button;
GtkWidget *off_button;
GtkWidget *update_button;
GtkWidget *label;
GtkAdjustment *adj;
COMMAND_STRUCT command;
command.command_num = 0;
/*
Open the command and status FIFOs.
*/
if ((command_fd = open(RTF_COMMAND_DEV, O_WRONLY)) < 0)
{
fprintf(stderr, "error opening %s\n", RTF_COMMAND_DEV);
return 1;
}
if ((status_fd = open(RTF_STATUS_DEV, O_RDONLY)) < 0)
{
fprintf(stderr, "error opening %s\n", RTF_STATUS_DEV);
return 1;
}
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (gtk_main_quit), "WM destroy");
gtk_window_set_title (GTK_WINDOW(window), "Frequency Modulator");
gtk_widget_set_usize (GTK_WIDGET(window), 140, 110);
main_vbox = gtk_vbox_new (FALSE, 1);
gtk_container_border_width (GTK_CONTAINER (main_vbox), 1);
gtk_container_add (GTK_CONTAINER (window), main_vbox);
gtk_widget_show (main_vbox);
get_main_menu (window, &menubar);
gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, FALSE, 0);
gtk_widget_show (menubar);
vbox = gtk_vbox_new(FALSE,0);
gtk_box_pack_start (GTK_BOX (main_vbox), vbox, TRUE, TRUE, 0);
gtk_widget_show (vbox);
/*
hbox = gtk_hbox_new(FALSE,0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
gtk_widget_show (hbox);
on_button = gtk_button_new_with_label ("ON");
gtk_signal_connect (GTK_OBJECT (on_button), "clicked", GTK_SIGNAL_FUNC (turn_sound_on), &command);
gtk_box_pack_start(GTK_BOX (hbox), on_button, TRUE, TRUE, 0);
gtk_widget_show (on_button);
off_button = gtk_button_new_with_label ("OFF");
gtk_signal_connect (GTK_OBJECT (off_button), "clicked", GTK_SIGNAL_FUNC (turn_sound_off), &command);
gtk_box_pack_start(GTK_BOX (hbox), off_button, TRUE, TRUE, 0);
gtk_widget_show (off_button);
*/
label = gtk_label_new( "Frequency:");
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 1);
gtk_widget_show (label);
adj = (GtkAdjustment *) gtk_adjustment_new ( 0.0, 0.0, 1000.0, 1.0, 100.0, 0.0);
spinner = gtk_spin_button_new (adj, 1.0, 2);
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), TRUE);
gtk_box_pack_start (GTK_BOX (vbox), spinner, FALSE, FALSE, 5);
gtk_widget_show (spinner);
update_button = gtk_button_new_with_label ("UPDATE");
gtk_signal_connect (GTK_OBJECT (update_button), "clicked", GTK_SIGNAL_FUNC (update), (gpointer) "update button");
gtk_box_pack_start(GTK_BOX (vbox), update_button, TRUE, TRUE, 0);
gtk_widget_show (update_button);
gtk_widget_show (window);
gtk_main ();
return(0);
}
/* example-end */