Created an open button

Clicking the button will open a file chooser to select a file to play.
This commit is contained in:
Bryan Schumaker 2011-08-23 08:26:23 -04:00
parent 7d3769a1a2
commit 20756aac14
5 changed files with 67 additions and 15 deletions

View File

@ -1,23 +1,10 @@
#include <ocarina/button.h>
#include <ocarina/chooser.h>
#include <libsaria/audio.h>
#include <libsaria/libsaria.h>
static void on_click_play(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria_get()->play();
}
static void on_click_pause(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria_get()->pause();
}
static void on_click_stop(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria_get()->stop();
}
static GtkWidget *make_button(const gchar *stockid, GtkIconSize size,
void (* func)(GtkWidget *, GdkEvent *, gpointer))
{
@ -33,17 +20,43 @@ static GtkWidget *make_button(const gchar *stockid, GtkIconSize size,
return button;
}
static void on_click_play(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria_get()->play();
}
GtkWidget *make_play_button(GtkIconSize size)
{
return make_button(GTK_STOCK_MEDIA_PLAY, size, on_click_play);
}
static void on_click_pause(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria_get()->pause();
}
GtkWidget *make_pause_button(GtkIconSize size)
{
return make_button(GTK_STOCK_MEDIA_PAUSE, size, on_click_pause);
}
static void on_click_stop(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria_get()->stop();
}
GtkWidget *make_stop_button(GtkIconSize size)
{
return make_button(GTK_STOCK_MEDIA_STOP, size, on_click_stop);
}
static void on_click_open_file(GtkWidget *b, GdkEvent *e, gpointer d)
{
string file = ocarina_choose_file();
print("Playing file: " + file);
}
GtkWidget *make_open_button(GtkIconSize size)
{
return make_button(GTK_STOCK_OPEN, size, on_click_open_file);
}

26
gui/chooser.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <ocarina/gtk.h>
#include <ocarina/chooser.h>
string ocarina_choose_file()
{
GtkWidget *chooser = gtk_file_chooser_dialog_new(
"Open File",
NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
char *c_file;
string filename = "";
if ( gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_ACCEPT) {
c_file = gtk_file_chooser_get_filename(
GTK_FILE_CHOOSER(chooser));
filename = c_file;
g_free(c_file);
}
gtk_widget_destroy(chooser);
return filename;
}

View File

@ -7,6 +7,7 @@
void ocarina_init(int argc, char **argv)
{
GtkWidget *open;
GtkWidget *play;
GtkWidget *pause;
GtkWidget *stop;
@ -17,10 +18,12 @@ void ocarina_init(int argc, char **argv)
window_icon("images/ocarina.png");
hbox = gtk_hbox_new(FALSE, 0);
open = make_open_button(GTK_ICON_SIZE_MENU);
play = make_play_button(GTK_ICON_SIZE_MENU);
pause = make_pause_button(GTK_ICON_SIZE_MENU);
stop = make_stop_button(GTK_ICON_SIZE_MENU);
box_pack_start(hbox, open, FALSE, FALSE, 0);
box_pack_start(hbox, play, FALSE, FALSE, 0);
box_pack_start(hbox, pause, FALSE, FALSE, 0);
box_pack_start(hbox, stop, FALSE, FALSE, 0);

View File

@ -9,5 +9,6 @@ using namespace std;
GtkWidget *make_play_button(GtkIconSize);
GtkWidget *make_pause_button(GtkIconSize);
GtkWidget *make_stop_button(GtkIconSize);
GtkWidget *make_open_button(GtkIconSize);
#endif /* OCARINA_BUTTON_H */

View File

@ -0,0 +1,9 @@
#ifndef OCARINA_CHOOSER_H
#define OCARINA_CHOOSER_H
#include <string>
using namespace std;
string ocarina_choose_file();
#endif