Added widget builder API
This commit is contained in:
92
src/widget_builder.c
Normal file
92
src/widget_builder.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <widget_builder.h>
|
||||
|
||||
#include <wofi.h>
|
||||
#include <utils.h>
|
||||
|
||||
struct widget_builder* wofi_create_widget_builder(struct mode* mode, size_t actions) {
|
||||
struct widget_builder* builder = calloc(actions, sizeof(struct widget_builder));
|
||||
|
||||
for(size_t count = 0; count < actions; ++count) {
|
||||
builder[count].mode = mode;
|
||||
builder[count].box = WOFI_PROPERTY_BOX(wofi_property_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
|
||||
|
||||
if(count == 0) {
|
||||
builder->actions = actions;
|
||||
}
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
void wofi_widget_builder_set_search_text(struct widget_builder* builder, char* search_text) {
|
||||
wofi_property_box_add_property(builder->box, "filter", search_text);
|
||||
}
|
||||
|
||||
void wofi_widget_builder_set_action(struct widget_builder* builder, char* action) {
|
||||
wofi_property_box_add_property(builder->box, "action", action);
|
||||
}
|
||||
|
||||
void wofi_widget_builder_insert_text(struct widget_builder* builder, char* text, char* css_name) {
|
||||
GtkWidget* label = gtk_label_new(text);
|
||||
gtk_container_add(GTK_CONTAINER(builder->box), label);
|
||||
if(css_name != NULL) {
|
||||
char* tmp = utils_concat(3, builder->mode->name, "-", css_name);
|
||||
gtk_widget_set_name(label, tmp);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void wofi_widget_builder_insert_image(struct widget_builder* builder, GdkPixbuf* pixbuf, char* css_name) {
|
||||
GtkWidget* img = gtk_image_new_from_pixbuf(pixbuf);
|
||||
gtk_container_add(GTK_CONTAINER(builder->box), img);
|
||||
if(css_name != NULL) {
|
||||
char* tmp = utils_concat(3, builder->mode->name, "-", css_name);
|
||||
gtk_widget_set_name(img, tmp);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
struct widget_builder* wofi_widget_builder_get_idx(struct widget_builder* builder, size_t idx) {
|
||||
return builder + idx;
|
||||
}
|
||||
|
||||
struct widget* wofi_widget_builder_get_widget(struct widget_builder* builder) {
|
||||
if(builder->actions == 0) {
|
||||
fprintf(stderr, "%s: This is not the 0 index of the widget_builder array\n", builder->mode->name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(builder->widget == NULL) {
|
||||
builder->widget = malloc(sizeof(struct widget));
|
||||
builder->widget->builder = builder;
|
||||
builder->widget->action_count = builder->actions;
|
||||
}
|
||||
|
||||
for(size_t count = 0; count < builder->actions; ++count) {
|
||||
}
|
||||
|
||||
return builder->widget;
|
||||
}
|
||||
|
||||
void wofi_free_widget_builder(struct widget_builder* builder) {
|
||||
if(builder->widget != NULL) {
|
||||
free(builder->widget);
|
||||
}
|
||||
free(builder);
|
||||
}
|
61
src/wofi.c
61
src/wofi.c
@@ -30,11 +30,11 @@
|
||||
#include <utils.h>
|
||||
#include <config.h>
|
||||
#include <property_box.h>
|
||||
#include <widget_builder.h>
|
||||
|
||||
#include <xdg-output-unstable-v1-client-protocol.h>
|
||||
#include <wlr-layer-shell-unstable-v1-client-protocol.h>
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <pango/pango.h>
|
||||
#include <gdk/gdkwayland.h>
|
||||
|
||||
@@ -109,18 +109,6 @@ static struct wl_list outputs;
|
||||
static struct zxdg_output_manager_v1* output_manager;
|
||||
static struct zwlr_layer_surface_v1* wlr_surface;
|
||||
|
||||
struct mode {
|
||||
void (*mode_exec)(const gchar* cmd);
|
||||
struct widget* (*mode_get_widget)(void);
|
||||
char* name, *dso;
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
struct widget {
|
||||
size_t action_count;
|
||||
char* mode, **text, *search_text, **actions;
|
||||
};
|
||||
|
||||
struct output_node {
|
||||
char* name;
|
||||
struct wl_output* output;
|
||||
@@ -502,11 +490,17 @@ static gboolean _insert_widget(gpointer data) {
|
||||
if(node == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
GtkWidget* parent;
|
||||
if(node->action_count > 1 && !no_actions) {
|
||||
parent = gtk_expander_new("");
|
||||
g_signal_connect(parent, "activate", G_CALLBACK(expand), NULL);
|
||||
GtkWidget* box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
|
||||
GtkWidget* box;
|
||||
if(node->builder == NULL) {
|
||||
box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
|
||||
} else {
|
||||
box = GTK_WIDGET(node->builder->box);
|
||||
}
|
||||
gtk_expander_set_label_widget(GTK_EXPANDER(parent), box);
|
||||
|
||||
GtkWidget* exp_box = gtk_list_box_new();
|
||||
@@ -514,7 +508,11 @@ static gboolean _insert_widget(gpointer data) {
|
||||
g_signal_connect(exp_box, "row-activated", G_CALLBACK(activate_item), NULL);
|
||||
gtk_container_add(GTK_CONTAINER(parent), exp_box);
|
||||
for(size_t count = 1; count < node->action_count; ++count) {
|
||||
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]);
|
||||
if(node->builder == NULL) {
|
||||
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]);
|
||||
} else {
|
||||
box = GTK_WIDGET(node->builder[count].box);
|
||||
}
|
||||
|
||||
GtkWidget* row = gtk_list_box_row_new();
|
||||
gtk_widget_set_name(row, "entry");
|
||||
@@ -523,8 +521,13 @@ static gboolean _insert_widget(gpointer data) {
|
||||
gtk_container_add(GTK_CONTAINER(exp_box), row);
|
||||
}
|
||||
} else {
|
||||
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
|
||||
if(node->builder == NULL) {
|
||||
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
|
||||
} else {
|
||||
parent = GTK_WIDGET(node->builder->box);
|
||||
}
|
||||
}
|
||||
|
||||
gtk_widget_set_halign(parent, content_halign);
|
||||
GtkWidget* child = gtk_flow_box_child_new();
|
||||
gtk_widget_set_name(child, "entry");
|
||||
@@ -546,17 +549,21 @@ static gboolean _insert_widget(gpointer data) {
|
||||
gtk_widget_set_visible(box, FALSE);
|
||||
}
|
||||
|
||||
free(node->mode);
|
||||
for(size_t count = 0; count < node->action_count; ++count) {
|
||||
free(node->text[count]);
|
||||
if(node->builder != NULL) {
|
||||
wofi_free_widget_builder(node->builder);
|
||||
} else {
|
||||
free(node->mode);
|
||||
for(size_t count = 0; count < node->action_count; ++count) {
|
||||
free(node->text[count]);
|
||||
}
|
||||
free(node->text);
|
||||
free(node->search_text);
|
||||
for(size_t count = 0; count < node->action_count; ++count) {
|
||||
free(node->actions[count]);
|
||||
}
|
||||
free(node->actions);
|
||||
free(node);
|
||||
}
|
||||
free(node->text);
|
||||
free(node->search_text);
|
||||
for(size_t count = 0; count < node->action_count; ++count) {
|
||||
free(node->actions[count]);
|
||||
}
|
||||
free(node->actions);
|
||||
free(node);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -774,7 +781,7 @@ struct wl_list* wofi_read_cache(struct mode* mode) {
|
||||
}
|
||||
|
||||
struct widget* wofi_create_widget(struct mode* mode, char* text[], char* search_text, char* actions[], size_t action_count) {
|
||||
struct widget* widget = malloc(sizeof(struct widget));
|
||||
struct widget* widget = calloc(1, sizeof(struct widget));
|
||||
widget->mode = strdup(mode->name);
|
||||
widget->text = malloc(action_count * sizeof(char*));
|
||||
for(size_t count = 0; count < action_count; ++count) {
|
||||
|
Reference in New Issue
Block a user