Skip to content

Commit

Permalink
Add tooltip parameter to the add_button function
Browse files Browse the repository at this point in the history
The tooltip needs to display the file's path, but will be unavailable if
the label is passed as the basename only. If the tooltip value is null,
default to the label input.
  • Loading branch information
JedBurke committed Dec 27, 2024
1 parent 5b48527 commit 9c80b7e
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions dragon.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void add_uri(char *uri) {
}
}

GtkButton *add_button(char *label, struct draggable_thing *dragdata, int type) {
GtkButton *add_button(char *label, struct draggable_thing *dragdata, int type, char *tooltip) {
GtkWidget *button;

if (icons_only) {
Expand All @@ -168,8 +168,9 @@ GtkButton *add_button(char *label, struct draggable_thing *dragdata, int type) {
button = gtk_button_new_with_label(label);
}

// Show a tooltip with the filename. Should perhaps show the full path.
gtk_widget_set_tooltip_text(button, label);
// Show a tooltip based on `tooltip` or the button's intended label.
// Ideally, the value is the file's full path.
gtk_widget_set_tooltip_text(button, tooltip != NULL ? tooltip : label);

GtkTargetList *targetlist = gtk_drag_source_get_target_list(GTK_WIDGET(button));
if (targetlist)
Expand Down Expand Up @@ -220,14 +221,8 @@ void add_file_button(GFile *file) {
// be either the basename or the full path. However, the full path is
// necessary to get the pixel buffer for image as well as for the button's
// tooltip.
char *filename;
char *path = g_file_get_path(file);

if (filename_only) {
filename = g_path_get_basename(path);
} else {
filename = g_file_get_path(file);
}
char *filename = filename_only ? g_path_get_basename(path) : g_file_get_path(file);

if(!g_file_query_exists(file, NULL)) {
fprintf(stderr, "The file `%s' does not exist.\n",
Expand All @@ -243,7 +238,7 @@ void add_file_button(GFile *file) {
dragdata->text = filename;
dragdata->uri = uri;

GtkButton *button = add_button(filename, dragdata, TARGET_TYPE_URI);
GtkButton *button = add_button(filename, dragdata, TARGET_TYPE_URI, path);
GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_size(path, thumb_size, thumb_size, NULL);
if (pb) {
GtkWidget *image = gtk_image_new_from_pixbuf(pb);
Expand Down Expand Up @@ -289,7 +284,7 @@ void add_uri_button(char *uri) {
struct draggable_thing *dragdata = malloc(sizeof(struct draggable_thing));
dragdata->text = uri;
dragdata->uri = uri;
GtkButton *button = add_button(uri, dragdata, TARGET_TYPE_URI);
GtkButton *button = add_button(uri, dragdata, TARGET_TYPE_URI, NULL);
left_align_button(button);
}

Expand Down

0 comments on commit 9c80b7e

Please sign in to comment.