Created
January 19, 2023 18:22
-
-
Save lynxluna/de359fe12ceb9ff36e44d9fb34e9dd0b to your computer and use it in GitHub Desktop.
OpenFile GTK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtk/gtk.h> | |
#define UNUSED_PARAMETER(param) (void) (param) | |
// Application State Data | |
struct app_data_t { | |
GtkWidget *parent_window; | |
GtkWidget *text_edit_view; | |
}; | |
// App Handlers | |
static void | |
on_app_activate(GtkApplication *app, gpointer user_data); | |
static void | |
on_app_startup(GtkApplication *app, gpointer user_data); | |
// Menu Handlers | |
static void | |
on_quit(GSimpleAction *act, GVariant*param, gpointer user_data); | |
static void | |
on_open(GSimpleAction *act, GVariant *param, gpointer user_data); | |
// Dialog handlers | |
static void | |
on_open_response(GtkNativeDialog *dialog, int response, gpointer user_data); | |
void | |
on_app_activate(GtkApplication *app, gpointer user_data) { | |
GtkWidget *wnd; | |
GSimpleAction *act_open; | |
struct app_data_t *app_data = (struct app_data_t *)user_data; | |
UNUSED_PARAMETER(user_data); | |
wnd = gtk_application_window_new(app); | |
gtk_window_set_title(GTK_WINDOW(wnd), "Text Editor"); | |
gtk_window_set_default_size(GTK_WINDOW(wnd), 800, 600); | |
gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (wnd), TRUE); | |
gtk_window_present(GTK_WINDOW(wnd)); | |
act_open = g_simple_action_new("open", NULL); | |
g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act_open)); | |
g_object_unref(act_open); | |
app_data->parent_window = wnd; | |
g_signal_connect(act_open, "activate", G_CALLBACK(on_open), app_data); | |
} | |
void | |
on_quit(GSimpleAction *act, GVariant*param, gpointer user_data) { | |
UNUSED_PARAMETER(param); | |
UNUSED_PARAMETER(act); | |
if (NULL == user_data) { | |
return; | |
} | |
g_application_quit(G_APPLICATION(user_data)); | |
} | |
void | |
on_open_response(GtkNativeDialog *dialog, int response, gpointer user_data) { | |
struct app_data_t *app_data = user_data; | |
GtkFileChooser *chooser; | |
char *fname; | |
GFile* file; | |
if (response != GTK_RESPONSE_ACCEPT) { | |
return; | |
} | |
chooser = GTK_FILE_CHOOSER(dialog); | |
file = gtk_file_chooser_get_file(chooser); | |
fname = g_file_get_path(file); | |
gtk_window_set_title(GTK_WINDOW(app_data->parent_window), fname); | |
g_object_unref(dialog); | |
g_object_unref(file); | |
} | |
void | |
on_open(GSimpleAction *act, GVariant *param, gpointer user_data) { | |
GtkFileChooserNative *dialog; | |
GtkWindow *wnd; | |
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
struct app_data_t *app_data = (struct app_data_t *)user_data; | |
wnd = GTK_WINDOW(app_data->parent_window); | |
UNUSED_PARAMETER(act); | |
UNUSED_PARAMETER(param); | |
dialog = gtk_file_chooser_native_new("Open File", wnd, action, "_Open", "_Cancel"); | |
g_signal_connect(dialog, "response", G_CALLBACK(on_open_response), app_data); | |
gtk_native_dialog_show(GTK_NATIVE_DIALOG(dialog)); | |
} | |
void | |
on_app_startup(GtkApplication *app, gpointer user_data) { | |
UNUSED_PARAMETER(user_data); | |
// setup menu bar | |
GMenu *menubar; | |
GMenuItem *file_op_item; | |
GMenu *file_menu; | |
// setup file op section | |
GMenu *file_op_section; | |
GMenuItem *file_open_item; | |
// setup file_quit_section | |
GMenu *file_quit_section; | |
GMenuItem *file_quit_item; | |
GSimpleAction *act_quit; | |
// Bottom Up setup | |
// File Quit | |
file_quit_section = g_menu_new(); | |
file_quit_item = g_menu_item_new("Quit", "app.quit"); | |
act_quit = g_simple_action_new("quit", NULL); | |
g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act_quit)); | |
g_object_unref(act_quit); | |
g_signal_connect(act_quit, "activate", G_CALLBACK(on_quit), app); | |
g_menu_append_item(file_quit_section, file_quit_item); | |
g_object_unref(file_quit_item); | |
// File Ops | |
file_op_section = g_menu_new(); | |
file_open_item = g_menu_item_new("Open", "app.open"); | |
g_menu_append_item(file_op_section, file_open_item); | |
g_object_unref(file_open_item); | |
// File Menu | |
file_menu = g_menu_new(); | |
g_menu_append_section(file_menu, NULL, G_MENU_MODEL(file_op_section)); | |
g_menu_append_section(file_menu, NULL, G_MENU_MODEL(file_quit_section)); | |
g_object_unref(file_op_section); | |
g_object_unref(file_quit_section); | |
// Root File Item | |
file_op_item = g_menu_item_new("File", NULL); | |
g_menu_item_set_submenu(file_op_item, G_MENU_MODEL(file_menu)); | |
g_object_unref(file_menu); | |
menubar = g_menu_new(); | |
g_menu_append_item(menubar, file_op_item); | |
g_object_unref(file_op_item); | |
gtk_application_set_menubar(GTK_APPLICATION(app), G_MENU_MODEL(menubar)); | |
g_object_unref(menubar); | |
} | |
int | |
main(int argc, char **argv) { | |
GtkApplication *app; | |
struct app_data_t app_data; | |
int status; | |
// if not defined | |
#ifndef G_APPLICATION_DEFAULT_FLAGS | |
#define G_APPLICATION_DEFAULT_FLAGS G_APPLICATION_FLAGS_NONE | |
#endif | |
app = gtk_application_new("net.retrocoding.texted", G_APPLICATION_DEFAULT_FLAGS); | |
g_signal_connect(app, "activate", G_CALLBACK(on_app_activate), &app_data); | |
g_signal_connect(app, "startup", G_CALLBACK(on_app_startup), &app_data); | |
status = g_application_run(G_APPLICATION(app), argc, argv); | |
g_object_unref(app); | |
return status; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment