Skip to content

Commit

Permalink
implement gui-helper gtk
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan200101 committed Jul 27, 2024
1 parent 55c82b9 commit cce76f1
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ ifdef FLATPAK_ID
USER_LIB_PATH := /app/lib
endif

PKG_CONFIG := pkg-config

.PHONY: all
all: build

.PHONY: build
build:
install -Dm644 lib/linux/libiup.so -t $(USER_LIB_PATH)
gcc sgdboop.c curl-helper.c string-helpers.c crc.c -liup -lcurl -o linux-release/SGDBoop $(LDFLAGS)
gcc -g sgdboop.c curl-helper.c gui-helper-gtk.c string-helpers.c crc.c $(shell $(PKG_CONFIG) --cflags --libs gtk+-3.0) -lcurl -o linux-release/SGDBoop $(LDFLAGS)

ifdef FLATPAK_ID
.PHONY: install
Expand Down
153 changes: 153 additions & 0 deletions gui-helper-gtk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include "gui-helper.h"
#ifdef _WIN32
#include "Windows.h"
#else
#include <gtk/gtk.h>
#endif

int ShowMessageBox(const char* title, const char* message)
{
gtk_init(NULL, NULL);

//return SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, title, message, NULL);
GtkWidget* dialog = gtk_message_dialog_new (
NULL,
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
message
);
gtk_window_set_title(GTK_WINDOW(dialog), "title");

g_signal_connect_swapped (
dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
dialog
);

return gtk_dialog_run(GTK_DIALOG(dialog));
}

enum {
COLUMN_ITEM = 0,
COLUMN_INDEX = 1,
};

static void button_callback(GtkWidget *widget, gpointer *data)
{
GtkWidget* treeview = (GtkWidget*)data;

GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

GtkTreeModel *model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected(selection, &model, &iter))
{
int* selected_index = g_object_get_data(G_OBJECT(treeview), "selected_index");

gtk_tree_model_get (model, &iter, COLUMN_INDEX, selected_index, -1);

g_print ("selected row is: %i\n", *selected_index);

gtk_main_quit();
}
}

int SelectionDialog(const char* title, int count, const char** list, int selection)
{
gtk_init(NULL, NULL);

// Setup some stylesheets to match the original UI
const char* stylesheet = " \
#box { \
padding: 10px; \
} \
";

GtkCssProvider* provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(provider, stylesheet, -1, NULL);

GdkDisplay* display = gdk_display_get_default();
GdkScreen* screen = gdk_display_get_default_screen(display);
gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);

// Put list into a GtkListStore
GtkListStore *store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);

GtkTreeIter iter;
for (int i = 0; i < count; ++i) {
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
COLUMN_ITEM, list[i],
COLUMN_INDEX, i,
-1);
}

// Dervice a TreeView from the ListStore
GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
int selected_index = -1;
g_object_set_data(G_OBJECT(treeview), "selected_index", &selected_index);

GtkCellRenderer* renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (
GTK_TREE_VIEW(treeview),
-1,
"",
renderer,
"text", COLUMN_ITEM,
NULL
);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);

// Put the TreeView into a ScrolledWindow
GtkWidget* scrollable = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrollable), treeview);

// Put a nice frame around the window to match the original
GtkWidget* frame = gtk_frame_new(NULL);
gtk_container_add(GTK_CONTAINER(frame), scrollable);


GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_window_set_default_size(GTK_WINDOW(window), 454, 388);
g_signal_connect(G_OBJECT(window), "destroy", gtk_main_quit, NULL);

GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_widget_set_name(box, "box");
gtk_container_add(GTK_CONTAINER(window), box);
gtk_box_pack_start(
GTK_BOX(box),
frame,
TRUE,
TRUE,
0
);

GtkWidget* buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_pack_end(
GTK_BOX(box),
buttons,
FALSE,
FALSE,
0
);
gtk_widget_set_halign(buttons, GTK_ALIGN_END);
gtk_widget_set_valign(buttons, GTK_ALIGN_START);

GtkWidget* ok_button = gtk_button_new_with_label("OK");
gtk_widget_set_size_request(ok_button, 75, 40);
g_signal_connect(G_OBJECT(ok_button), "clicked", G_CALLBACK(button_callback), treeview);
gtk_container_add(GTK_CONTAINER(buttons), ok_button);

GtkWidget* cancel_button = gtk_button_new_with_label("Cancel");
gtk_widget_set_size_request(cancel_button, 75, 40);
g_signal_connect(G_OBJECT(cancel_button), "clicked", gtk_main_quit, NULL);
gtk_container_add(GTK_CONTAINER(buttons), cancel_button);

gtk_widget_show_all(window);
gtk_main();

return selected_index;
}

0 comments on commit cce76f1

Please sign in to comment.