Update to version 1.3
- Added `resources.c` and `resources.h` to the `.gitignore` file. - Modified the `execute_command` function to use `g_spawn_command_line_async` for asynchronous command execution. - Updated the `show_confirmation_dialog` function to display a GTK dialog with a warning icon and improved layout. - Added a margin to the buttons in the `create_button` function for better spacing. - Improved error handling for loading the window icon in the `activate` function. - Updated README.md with more information about manually compiling ssdd
This commit is contained in:
parent
152a0d648c
commit
65946c76fc
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
||||
ssdd
|
||||
resources.c
|
||||
resources.h
|
||||
|
13
README.md
13
README.md
@ -37,7 +37,14 @@ Edit the `Makefile` or use the following commands:
|
||||
|
||||
### Manual compilation
|
||||
|
||||
```shell
|
||||
First generate the resources.
|
||||
|
||||
```bash
|
||||
% glib-compile-resources resources.gresource.xml --generate-source --target=resources.c
|
||||
% glib-compile-resources resources.gresource.xml --generate-header --target=resources.h
|
||||
```
|
||||
|
||||
```bash
|
||||
# Using GCC:
|
||||
% gcc ssdd.c resources.c -o ssdd `pkg-config --cflags --libs gtk+-3.0`
|
||||
|
||||
@ -51,7 +58,7 @@ Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
|
||||
|
||||
1. Edit your Openbox menu:
|
||||
|
||||
```shell
|
||||
```bash
|
||||
% sudo nvim /etc/xdg/openbox/menu.xml
|
||||
```
|
||||
|
||||
@ -63,7 +70,7 @@ Place the `ssdd` binary in your `$PATH` (e.g., `~/bin`).
|
||||
|
||||
3. Reconfigure Openbox:
|
||||
|
||||
```shell
|
||||
```bash
|
||||
% openbox --reconfigure
|
||||
```
|
||||
|
||||
|
121
ssdd.c
121
ssdd.c
@ -1,43 +1,30 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <stdlib.h>
|
||||
#include "resources.h" // Include the generated resource header
|
||||
#include <glib.h>
|
||||
#include "resources.h"
|
||||
|
||||
// Function declarations (prototypes)
|
||||
static void execute_command(const gchar *command);
|
||||
static void show_about_dialog(GtkWidget *widget);
|
||||
static void button_clicked(GtkWidget *widget, gpointer data);
|
||||
static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data);
|
||||
static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command);
|
||||
static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos);
|
||||
|
||||
static void execute_command(const gchar *command) {
|
||||
int ret = system(command);
|
||||
if (ret != 0) {
|
||||
GError *error = NULL;
|
||||
gboolean ret = g_spawn_command_line_async(command, &error);
|
||||
if (!ret) {
|
||||
GtkWidget *dialog;
|
||||
dialog = gtk_message_dialog_new(NULL,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_ERROR,
|
||||
GTK_BUTTONS_CLOSE,
|
||||
"Error executing command: %s",
|
||||
command);
|
||||
"Error executing command: %s\n%s",
|
||||
command,
|
||||
error->message);
|
||||
gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command) {
|
||||
GtkWidget *dialog;
|
||||
gint response;
|
||||
|
||||
gchar *message = g_strdup_printf("Are you sure you want to %s?", label);
|
||||
|
||||
dialog = gtk_message_dialog_new(NULL,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_QUESTION,
|
||||
GTK_BUTTONS_NONE,
|
||||
"%s",
|
||||
message);
|
||||
gtk_dialog_add_button(GTK_DIALOG(dialog), "Yes", GTK_RESPONSE_YES);
|
||||
gtk_dialog_add_button(GTK_DIALOG(dialog), "No", GTK_RESPONSE_NO);
|
||||
|
||||
response = gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
g_free(message);
|
||||
|
||||
if (response == GTK_RESPONSE_YES) {
|
||||
execute_command(command);
|
||||
g_error_free(error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,7 +36,7 @@ static void show_about_dialog(GtkWidget *widget) {
|
||||
GtkWidget *box;
|
||||
const gchar *about_text =
|
||||
"\n<b>About Simple ShutDown Dialog</b>\n\n"
|
||||
"<b>Version:</b> 1.2\n"
|
||||
"<b>Version:</b> 1.3\n"
|
||||
"<b>Author:</b> kekePower\n"
|
||||
"<b>URL:</b> <a href=\"https://git.kekepower.com/kekePower/ssdd\">https://git.kekepower.com/kekePower/ssdd</a>\n"
|
||||
"<b>Description:</b> This is a simple Shutdown Dialog for Openbox.\n";
|
||||
@ -82,8 +69,8 @@ static void show_about_dialog(GtkWidget *widget) {
|
||||
}
|
||||
|
||||
static void button_clicked(GtkWidget *widget, gpointer data) {
|
||||
const gchar *command = (const gchar *) data;
|
||||
const gchar *label = gtk_button_get_label(GTK_BUTTON(widget));
|
||||
const gchar *command = (const gchar *)data;
|
||||
const gchar *label = g_object_get_data(G_OBJECT(widget), "label");
|
||||
|
||||
if (g_strcmp0(command, "exit") == 0) {
|
||||
g_application_quit(G_APPLICATION(g_object_get_data(G_OBJECT(widget), "app")));
|
||||
@ -93,7 +80,8 @@ static void button_clicked(GtkWidget *widget, gpointer data) {
|
||||
if (g_strcmp0(command, "about") == 0) {
|
||||
show_about_dialog(widget);
|
||||
} else {
|
||||
show_confirmation_dialog(widget, label, command);
|
||||
GtkWidget *window = gtk_widget_get_toplevel(widget);
|
||||
show_confirmation_dialog(window, label, command);
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,6 +93,51 @@ static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer dat
|
||||
return FALSE; // Event not handled
|
||||
}
|
||||
|
||||
static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *label, const gchar *command) {
|
||||
GtkWidget *dialog;
|
||||
gint response;
|
||||
|
||||
gchar *message = g_strdup_printf("Are you sure you want to %s?", label);
|
||||
|
||||
dialog = gtk_dialog_new();
|
||||
gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation");
|
||||
|
||||
// Set the transient parent correctly
|
||||
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent_window));
|
||||
|
||||
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
|
||||
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
|
||||
|
||||
GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
|
||||
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||
gtk_container_add(GTK_CONTAINER(content_area), box);
|
||||
|
||||
GtkWidget *image = gtk_image_new_from_icon_name("dialog-warning", GTK_ICON_SIZE_DIALOG);
|
||||
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
|
||||
|
||||
GtkWidget *label_widget = gtk_label_new(message);
|
||||
gtk_box_pack_start(GTK_BOX(box), label_widget, TRUE, TRUE, 0);
|
||||
|
||||
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
|
||||
"Yes", GTK_RESPONSE_YES,
|
||||
"No", GTK_RESPONSE_NO,
|
||||
NULL); // Add NULL to terminate the list of buttons
|
||||
|
||||
GtkWidget *button_box = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); // Get content area instead of action area
|
||||
gtk_box_set_spacing(GTK_BOX(button_box), 10); // Adjust spacing as needed
|
||||
gtk_widget_set_margin_top(button_box, 10);
|
||||
|
||||
gtk_widget_show_all(dialog); // Show the dialog and its children
|
||||
|
||||
response = gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
g_free(message);
|
||||
|
||||
if (response == GTK_RESPONSE_YES) {
|
||||
execute_command(command);
|
||||
}
|
||||
}
|
||||
|
||||
static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos) {
|
||||
GtkWidget *button;
|
||||
GtkWidget *box;
|
||||
@ -121,7 +154,11 @@ static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *lab
|
||||
label = gtk_label_new(label_text);
|
||||
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
|
||||
|
||||
//Add margin to the button
|
||||
gtk_widget_set_margin_top(button, 10);
|
||||
|
||||
g_object_set_data(G_OBJECT(button), "app", app);
|
||||
g_object_set_data(G_OBJECT(button), "label", (gpointer) label_text);
|
||||
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer) command);
|
||||
gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1);
|
||||
}
|
||||
@ -149,7 +186,7 @@ static void activate(GtkApplication *app, gpointer user_data) {
|
||||
"help-about",
|
||||
"application-exit"
|
||||
};
|
||||
const gchar *labels[] = {
|
||||
const gchar *labels[] = { // Define the labels array here
|
||||
"Logout",
|
||||
"Reboot",
|
||||
"Shutdown",
|
||||
@ -164,10 +201,16 @@ static void activate(GtkApplication *app, gpointer user_data) {
|
||||
gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog");
|
||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||
|
||||
// Load the icon from the resource and set it as the window icon
|
||||
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", NULL);
|
||||
// Load the icon with error handling
|
||||
GError *error = NULL;
|
||||
GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", &error);
|
||||
if (icon_pixbuf) {
|
||||
gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf);
|
||||
g_object_unref(icon_pixbuf); // Free the icon pixbuf after setting it
|
||||
g_object_unref(icon_pixbuf); // Free the pixbuf
|
||||
} else {
|
||||
g_warning("Failed to load icon: %s", error->message);
|
||||
g_error_free(error);
|
||||
}
|
||||
|
||||
g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), app);
|
||||
|
||||
@ -195,3 +238,5 @@ int main(int argc, char **argv) {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user