Skip to content

Commit

Permalink
Filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
iguessthislldo committed Mar 9, 2018
1 parent cd3a54d commit ca1fdfb
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 37 deletions.
32 changes: 31 additions & 1 deletion Entry.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <stdlib.h>
#include <stdio.h>

#include "launcher.h"
#include "Entry.h"
#include "util.h"

Entry * Entry_new() {
Entry * entry = malloc(sizeof(Entry));
Expand All @@ -10,8 +12,9 @@ Entry * Entry_new() {
entry->image_path = NULL;
entry->exec = NULL;
entry->cd = NULL;
entry->image = NULL;
entry->steam_id = 0;
entry->image = NULL;
entry->event_box = NULL;
return entry;
}

Expand Down Expand Up @@ -51,6 +54,8 @@ void Entries_delete_all(Entries * entries) {
Entry * entry = node->entry;
g_free(entry->name);
g_free(entry->image_path);
if (entry->image) g_object_unref(entry->image);
if (entry->event_box) g_object_unref(entry->event_box);
if (entry->exec) g_free(entry->exec);
if (entry->cd) g_free(entry->cd);
free(entry);
Expand All @@ -60,3 +65,28 @@ void Entries_delete_all(Entries * entries) {
}
}

Entries * Entries_clear_container(GtkContainer * container, Entries * entries) {
Node * node = entries->head;
for (Node * next = node; next; node = next) {
gtk_container_remove(container, node->entry->event_box);
next = node->next;
}
}

Entries * Entries_filter(Entries * entries, const char * filter) {
char * uc_filter = g_utf8_strup(filter, -1);
Entries * filtered = Entries_new();
Node * node = entries->head;
for (Node * next = node; next; node = next) {
Entry * e = node->entry;
char * uc_entry_name = g_utf8_strup(e->name, -1);
if (starts_with(uc_entry_name, uc_filter)) {
if (debug) printf(" %s\n", e->name);
Entries_insert(filtered, e);
}
g_free(uc_entry_name);
next = node->next;
}
return filtered;
}

4 changes: 4 additions & 0 deletions Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Entry_struct {
char * cd;
char * steam_id;
GtkWidget * image;
GtkWidget * event_box;
};

Entry * Entry_new();
Expand All @@ -38,4 +39,7 @@ void Entries_insert(Entries * entries, Entry * entry);
void Entries_delete(Entries * entries);
void Entries_delete_all(Entries * entries);

Entries * Entries_filter(Entries * entries, const char * filter);
Entries * Entries_clear_container(GtkContainer * container, Entries * entries);

#endif
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ EXEC=banner_launcher

all: $(EXEC)

$(EXEC): launcher.c Entry.c steam.c
$(EXEC): launcher.c Entry.c steam.c util.c
gcc -g `pkg-config --cflags gtk+-3.0` -o $@ $^ `pkg-config --libs gtk+-3.0`

clean:
Expand Down
78 changes: 60 additions & 18 deletions launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include "launcher.h"
#include "steam.h"

GtkWidget * window;
GtkWidget * layout;
GtkWidget * filter;
GtkWidget * scroll;
GtkWidget * grid;

bool load_config(const gchar * path) {
bool error = false;
if (debug) printf("Loading config from %s\n", path);
Expand Down Expand Up @@ -55,6 +61,7 @@ bool load_entries(Entries * entries, const gchar * path) {
g_key_file_get_string(ini, groups[i], "image", NULL),
NULL);
entry->image = gtk_image_new_from_file(image_file);
g_object_ref(entry->image);
if (debug) {
printf(" Entry: %s\n \"%s\"\n",
groups[i], entry->name
Expand All @@ -74,6 +81,36 @@ void entry_click(GtkWidget * widget, GdkEvent * event, gpointer data) {
if (debug) printf("Run: %s\n", ((Entry *) data)->name);
}

void add_entries_to_grid(Entries * entries) {
int row = 0;
int col = 0;
int cols = GRID_WIDTH;
for (Node * n = entries->head; n; n = n->next) {
Entry * e = n->entry;
gtk_grid_attach(GTK_GRID(grid), e->event_box, col, row, 1, 1);
if (++col >= cols) {
row++;
col = 0;
}
}
}

void filter_changed(GtkEntryBuffer * b) {
const char * filter = gtk_entry_buffer_get_text(b);
if (debug) printf("Filter: %s\n", filter);
Entries_clear_container(GTK_CONTAINER(grid), visable_entries);
if (visable_entries != all_entries) {
Entries_delete(visable_entries);
}
if (filter[0]) {
visable_entries = Entries_filter(all_entries, filter);
} else {
if (debug) printf(" <RESET>\n");
visable_entries = all_entries;
}
add_entries_to_grid(visable_entries);
}

static void activate(GtkApplication * app, gpointer user_data) {
config_dir = g_build_filename(
g_get_user_config_dir(),
Expand All @@ -92,40 +129,46 @@ static void activate(GtkApplication * app, gpointer user_data) {
"config.ini",
NULL);
all_entries = Entries_new();
visable_entries = Entries_new();
visable_entries = NULL;
load_entries(all_entries, entries_file);
load_config(config_file);
steam_entries = Entries_new();
load_steam_entries(steam_path, steam_entries);

GtkWidget * window;
GtkWidget * layout;
GtkWidget * search;
GtkWidget * scroll;
GtkWidget * grid;

window = gtk_application_window_new(app);
gtk_window_set_resizable(GTK_WINDOW(window), false);
gtk_window_set_title(GTK_WINDOW(window), APP_NAME);
gtk_window_set_default_size(GTK_WINDOW(window), BANNER_WIDTH * GRID_WIDTH, BANNER_HIGHT * 4);
layout = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(window), layout);

search = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(layout), search);
GtkEntryBuffer * filter_buffer = gtk_entry_buffer_new("", -1);
filter = gtk_entry_new_with_buffer(filter_buffer);
g_signal_connect(
G_OBJECT(filter_buffer),
"inserted-text",
G_CALLBACK(filter_changed),
NULL
);
g_signal_connect(
G_OBJECT(filter_buffer),
"deleted-text",
G_CALLBACK(filter_changed),
NULL
);
gtk_container_add(GTK_CONTAINER(layout), filter);

scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll), BANNER_HIGHT * 4);
gtk_container_add(GTK_CONTAINER(layout), scroll);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(scroll), grid);

int row = 0;
int col = 0;
int cols = GRID_WIDTH;
for (Node * n = all_entries->head; n; n = n->next) {
Entry * e = n->entry;
GtkWidget * event_box = gtk_event_box_new();
e->event_box = event_box;
g_object_ref(event_box);
gtk_widget_set_events(event_box, GDK_BUTTON_RELEASE_MASK);
g_signal_connect(
G_OBJECT(event_box),
Expand All @@ -134,18 +177,17 @@ static void activate(GtkApplication * app, gpointer user_data) {
(gpointer) e
);
gtk_container_add(GTK_CONTAINER(event_box), e->image);
gtk_grid_attach(GTK_GRID(grid), event_box, col, row, 1, 1);
if (++col >= cols) {
row++;
col = 0;
}
}

visable_entries = all_entries;
add_entries_to_grid(visable_entries);

gtk_widget_show_all(window);
}

int main(int argc, char * argv[]) {
debug = true;
visable_entries = NULL;

GtkApplication * app;
int status;
Expand All @@ -155,7 +197,7 @@ int main(int argc, char * argv[]) {
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);

Entries_delete(visable_entries);
if (visable_entries != all_entries) Entries_delete(visable_entries);
Entries_delete_all(all_entries);
Entries_delete_all(steam_entries);
g_free(config_dir);
Expand Down
19 changes: 2 additions & 17 deletions steam.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,9 @@

#include <glib.h>

#include "launcher.h"
#include "util.h"
#include "steam.h"

bool starts_with(char * string, char * prefix) {
size_t i = 0;
char string_char, prefix_char;
while (true) {
string_char = string[i];
prefix_char = prefix[i];
if (!prefix_char) {
return true;
}
if (string_char != prefix_char) {
return false;
}
i++;
}
}
#include "launcher.h"

void load_steam_entries(char * steam_path, Entries * steam_entries) {

Expand Down
18 changes: 18 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "util.h"

bool starts_with(const char * string, const char * prefix) {
size_t i = 0;
char string_char, prefix_char;
while (true) {
string_char = string[i];
prefix_char = prefix[i];
if (!prefix_char) {
return true;
}
if (string_char != prefix_char) {
return false;
}
i++;
}
}

4 changes: 4 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stdbool.h>
#include <stdlib.h>

bool starts_with(const char * string, const char * prefix);

0 comments on commit ca1fdfb

Please sign in to comment.