diff --git a/Debug/src/subdir.mk b/Debug/src/subdir.mk index d6d8325..4be839f 100644 --- a/Debug/src/subdir.mk +++ b/Debug/src/subdir.mk @@ -7,6 +7,7 @@ C_SRCS += \ ../src/config.c \ ../src/main.c \ ../src/map.c \ +../src/property_label.c \ ../src/utils.c \ ../src/wofi.c @@ -14,6 +15,7 @@ OBJS += \ ./src/config.o \ ./src/main.o \ ./src/map.o \ +./src/property_label.o \ ./src/utils.o \ ./src/wofi.o @@ -21,6 +23,7 @@ C_DEPS += \ ./src/config.d \ ./src/main.d \ ./src/map.d \ +./src/property_label.d \ ./src/utils.d \ ./src/wofi.d diff --git a/Release/src/subdir.mk b/Release/src/subdir.mk index 2fef766..ccec8f9 100644 --- a/Release/src/subdir.mk +++ b/Release/src/subdir.mk @@ -7,6 +7,7 @@ C_SRCS += \ ../src/config.c \ ../src/main.c \ ../src/map.c \ +../src/property_label.c \ ../src/utils.c \ ../src/wofi.c @@ -14,6 +15,7 @@ OBJS += \ ./src/config.o \ ./src/main.o \ ./src/map.o \ +./src/property_label.o \ ./src/utils.o \ ./src/wofi.o @@ -21,6 +23,7 @@ C_DEPS += \ ./src/config.d \ ./src/main.d \ ./src/map.d \ +./src/property_label.d \ ./src/utils.d \ ./src/wofi.d diff --git a/inc/property_label.h b/inc/property_label.h new file mode 100644 index 0000000..c8c3f61 --- /dev/null +++ b/inc/property_label.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2019 Scoopta + * This file is part of Wofi + * Wofi is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Wofi is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Wofi. If not, see . + */ + +#ifndef PROPERTY_LABEL_H +#define PROPERTY_LABEL_H +#include + +#include + +#define WOFI_TYPE_PROPERTY_LABEL wofi_property_label_get_type() +G_DECLARE_FINAL_TYPE(WofiPropertyLabel, wofi_property_label, WOFI, PROPERTY_LABEL, GtkLabel); + +GtkWidget* wofi_property_label_new(const gchar* str); + +void wofi_property_label_add_property(WofiPropertyLabel* this, const gchar* key, gchar* value); + +const gchar* wofi_property_label_get_property(WofiPropertyLabel* this, const gchar* key); + +#endif diff --git a/inc/wofi.h b/inc/wofi.h index 5185d6f..602e4b4 100644 --- a/inc/wofi.h +++ b/inc/wofi.h @@ -19,6 +19,7 @@ #include #include +#include #include #include diff --git a/src/property_label.c b/src/property_label.c new file mode 100644 index 0000000..6e3bee4 --- /dev/null +++ b/src/property_label.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2019 Scoopta + * This file is part of Wofi + * Wofi is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Wofi is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Wofi. If not, see . + */ + +#include + +struct _WofiPropertyLabel { + GtkLabel super; + struct map* properties; +}; + +G_DEFINE_TYPE(WofiPropertyLabel, wofi_property_label, GTK_TYPE_LABEL); + +static void wofi_property_label_init(WofiPropertyLabel* this) { + this->properties = map_init(); +} + +static void finalize(GObject* obj) { + WofiPropertyLabel* this = WOFI_PROPERTY_LABEL(obj); + map_free(this->properties); + G_OBJECT_CLASS(wofi_property_label_parent_class)->finalize(obj); +} + +static void wofi_property_label_class_init(WofiPropertyLabelClass* class) { + GObjectClass* g_class = G_OBJECT_CLASS(class); + g_class->finalize = finalize; +} + +GtkWidget* wofi_property_label_new(const gchar* str) { + return g_object_new(WOFI_TYPE_PROPERTY_LABEL, "label", str, NULL); +} + +void wofi_property_label_add_property(WofiPropertyLabel* this, const gchar* key, gchar* value) { + map_put(this->properties, key, value); +} + +const gchar* wofi_property_label_get_property(WofiPropertyLabel* this, const gchar* key) { + return map_get(this->properties, key); +} diff --git a/src/wofi.c b/src/wofi.c index c7801b6..c9a608e 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -25,8 +25,9 @@ static const gchar* filter; static char* mode; static time_t filter_time; static int64_t filter_rate; + struct node { - char* label, *tooltip; + char* text, *action; GtkContainer* container; }; @@ -72,22 +73,21 @@ static void get_search(GtkSearchEntry* entry, gpointer data) { gtk_list_box_invalidate_filter(GTK_LIST_BOX(inner_box)); } -static GtkWidget* create_label(const char* text, char* tooltip) { - GtkWidget* label = gtk_label_new(text); +static GtkWidget* create_label(const char* text, char* action) { + GtkWidget* label = wofi_property_label_new(text); gtk_widget_set_name(label, "unselected"); - gtk_widget_set_tooltip_text(label, tooltip); - gtk_widget_set_has_tooltip(label, FALSE); + wofi_property_label_add_property(WOFI_PROPERTY_LABEL(label), "action", action); gtk_label_set_xalign(GTK_LABEL(label), 0); return label; } static gboolean insert_widget(gpointer data) { struct node* node = data; - GtkWidget* label = create_label(node->label, node->tooltip); + GtkWidget* label = create_label(node->text, node->action); gtk_container_add(node->container, label); gtk_widget_show(label); - free(node->label); - free(node->tooltip); + free(node->text); + free(node->action); free(node); return FALSE; } @@ -149,15 +149,15 @@ static void* do_run(void* data) { wl_list_for_each_reverse_safe(node, tmp, cache, link) { struct node* label = malloc(sizeof(struct node)); char* text = strrchr(node->line, '/'); - char* tooltip = strchr(node->line, ' ') + 1; + char* action = strchr(node->line, ' ') + 1; if(text == NULL) { - text = tooltip; + text = action; } else { ++text; } - map_put(cached, tooltip, "true"); - label->label = strdup(text); - label->tooltip = strdup(tooltip); + map_put(cached, action, "true"); + label->text = strdup(text); + label->action = strdup(action); label->container = GTK_CONTAINER(inner_box); g_idle_add(insert_widget, label); utils_sleep_millis(1); @@ -185,8 +185,8 @@ static void* do_run(void* data) { stat(full_path, &info); if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) { struct node* node = malloc(sizeof(struct node)); - node->label = strdup(entry->d_name); - node->tooltip = strdup(full_path); + node->text = strdup(entry->d_name); + node->action = strdup(full_path); node->container = GTK_CONTAINER(inner_box); g_idle_add(insert_widget, node); utils_sleep_millis(1); @@ -212,8 +212,8 @@ static void* do_dmenu(void* data) { *lf = 0; } struct node* node = malloc(sizeof(struct node)); - node->label = strdup(line); - node->tooltip = strdup(line); + node->text = strdup(line); + node->action = strdup(line); node->container = GTK_CONTAINER(inner_box); g_idle_add(insert_widget, node); utils_sleep_millis(1); @@ -262,8 +262,8 @@ static void* do_drun(void* data) { continue; } struct node* node = malloc(sizeof(struct node)); - node->label = strdup(name); - node->tooltip = strdup(full_path); + node->text = strdup(name); + node->action = strdup(full_path); node->container = GTK_CONTAINER(inner_box); g_idle_add(insert_widget, node); utils_sleep_millis(1); @@ -346,7 +346,7 @@ static void activate_item(GtkListBox* box, GtkListBoxRow* row, gpointer data) { (void) box; (void) data; GtkWidget* label = gtk_bin_get_child(GTK_BIN(row)); - execute_action(mode, gtk_widget_get_tooltip_text(label)); + execute_action(mode, wofi_property_label_get_property(WOFI_PROPERTY_LABEL(label), "action")); } static void select_item(GtkListBox* box, GtkListBoxRow* row, gpointer data) { @@ -367,7 +367,7 @@ static void activate_search(GtkEntry* entry, gpointer data) { } else { GtkListBoxRow* row = gtk_list_box_get_row_at_y(GTK_LIST_BOX(inner_box), 0); GtkWidget* label = gtk_bin_get_child(GTK_BIN(row)); - execute_action(mode, gtk_widget_get_tooltip_text(label)); + execute_action(mode, wofi_property_label_get_property(WOFI_PROPERTY_LABEL(label), "action")); } }