#include
#ifdef USE_HILDON
#include
#include
#define USE_LIBOSSO
#endif
#ifdef USE_LIBOSSO
#include
struct _instance
{
osso_context_t *osso;
GtkWidget *window;
};
typedef struct _instance instance;
static gint handler(const gchar *interface, const gchar *method,
GArray *arguments, gpointer data, osso_rpc_t *retval)
{
retval->type = DBUS_TYPE_INT32;
retval->value.i = OSSO_ERROR;
if (method != NULL && strcmp(method, "top_application") == 0)
{
instance *inst = (instance *) data;
gtk_window_present(GTK_WINDOW(inst->window));
retval->value.i = OSSO_OK;
}
return retval->value.i;
}
static instance g_inst;
static osso_init(GtkWidget *window)
{
g_inst.osso = osso_initialize("org.moblin.helloworld3", "", TRUE, NULL);
g_inst.window = window;
if(g_inst.osso == NULL)
g_print("osso_initialize error\n");
else
{
osso_return_t r;
r = osso_rpc_set_default_cb_f(g_inst.osso, (osso_rpc_cb_f *) handler,
&g_inst);
if (r != OSSO_OK)
g_print("osso_rpc_set_default_cb_f error\n");
}
}
static osso_exit(void)
{
if (g_inst.osso)
{
osso_deinitialize(g_inst.osso);
}
}
#else
#define osso_init(a)
#define osso_exit()
#endif
static gboolean quit1(GtkWidget *widget, GdkEvent *event)
{
g_print("quit1\n");
gtk_main_quit();
}
static gboolean quit2(GtkWidget *widget, GdkEvent *event)
{
g_print("quit2\n");
gtk_main_quit();
}
static void destroy(GtkWidget *widget, gpointer data)
{
g_print("destroy\n");
gtk_main_quit();
}
static void add_menu(GtkWidget *window, GtkWidget *box)
{
#ifdef USE_HILDON
GtkWidget *menubar = gtk_menu_new();
#else
GtkWidget *menubar = gtk_menu_bar_new();
gtk_box_pack_start(GTK_BOX(box), menubar, FALSE, FALSE, 0);
#endif
GtkAccelGroup *accel = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(window), accel);
GtkWidget *fileitem = gtk_menu_item_new_with_mnemonic("_File");
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileitem);
GtkWidget *filemenu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileitem), filemenu);
GtkWidget *q = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel);
gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), q);
g_signal_connect_swapped(G_OBJECT(q), "activate", G_CALLBACK(quit1), NULL);
#ifdef USE_HILDON
hildon_window_set_menu((HildonWindow *) window, GTK_MENU(menubar));
#endif
}
static void add_toolbar(GtkWidget *window, GtkWidget *box)
{
GtkWidget *toolbar = gtk_toolbar_new();
#ifdef USE_HILDON
hildon_window_add_toolbar((HildonWindow *) window, GTK_TOOLBAR(toolbar));
#else
gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
#endif
GtkToolItem *q = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
g_signal_connect_swapped(G_OBJECT(q), "clicked", G_CALLBACK(quit2), NULL);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), q, -1);
}
int main(int argc, char *argv[])
{
static char hello[] = "Hello World! (v 3.0)";
gtk_init (&argc, &argv);
#ifdef USE_HILDON
HildonProgram *program = hildon_program_get_instance();
HildonWindow *hildon_window = HILDON_WINDOW(hildon_window_new());
hildon_program_add_window(program, hildon_window);
GtkWidget *window = GTK_WIDGET(hildon_window);
#else
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
#endif
gtk_widget_set_size_request(window, 250, -1);
osso_init(window);
GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_window_set_title(GTK_WINDOW(window), hello);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
add_menu(window, vbox);
add_toolbar(window, vbox);
GtkWidget *label1 = gtk_label_new(hello);
gtk_box_pack_start_defaults(GTK_BOX(vbox), label1);
time_t current_time;
struct tm *local_time;
time(¤t_time);
local_time = localtime(¤t_time);
GtkWidget *label2 = gtk_label_new(asctime(local_time));
gtk_box_pack_start_defaults(GTK_BOX(vbox), label2);
gtk_widget_show_all(window);
gtk_main();
osso_exit();
return 0;
}
How to add a GtkUiManager menubar to a hildon window
Some time ago i found a very cool tip from this blog (http://blogs.igalia.com/svillar): a simple and easy function to convert from a gtkuimanager menubar to a gtkmenu. We need to do this because hildon_window_set_menu requires a GtkMenu. Let see the source:
static
GtkWidget *
menubar_to_menu (GtkUIManager *ui_manager) {
GtkWidget
*main_menu;
GtkWidget *menubar;
GList *iter;
/*
Create new main menu */
main_menu = gtk_menu_new();
/*
Get the menubar from the UI manager */
menubar =
gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
iter =
gtk_container_get_children (GTK_CONTAINER (menubar));
while (iter)
{
GtkWidget *menu;
menu = GTK_WIDGET (iter->data);
gtk_widget_reparent(menu,
main_menu);
iter = g_list_next (iter);
}
return
main_menu;
}
And you can use it this way:
ui = gtk_ui_manager_new ();
....
hildon_window_set_menu(HILDON_WINDOW(window), GTK_MENU(menubar_to_menu (ui)));
阅读(816) | 评论(0) | 转发(0) |