Skip to content

Instantly share code, notes, and snippets.

@rkulla
Created August 4, 2011 06:39
Show Gist options
  • Save rkulla/1124611 to your computer and use it in GitHub Desktop.
Save rkulla/1124611 to your computer and use it in GitHub Desktop.
GTK+ front-end wrapper around the UNIX which(1) command
/* gwhich.c -by Ryan Kulla
* GTK+ front-end wrapper around the which(1) command.
* Compile with: gcc gwhich.c -o gwhich `pkg-config --cflags --libs gtk+-2.0`
* Usage: gwhich
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <gtk/gtk.h>
GtkWidget *get_window(void);
void build_window(GtkWidget *window, char *title);
GtkWidget *get_vbox(GtkWidget *window);
void show_vbox(GtkWidget *window, GtkWidget *vbox);
GtkWidget *get_entry(void);
void build_entry(GtkWidget *vbox, GtkWidget *entry);
void enter_callback(GtkWidget *widget, GtkWidget *entry);
GtkWidget *get_frame(void);
void build_label_frame(GtkWidget *vbox, GtkWidget *frame);
GtkWidget *get_hbox(void);
void show_hbox(GtkWidget *vbox, GtkWidget *hbox);
GtkWidget *get_button(char *msg);
GtkWidget *build_button(GtkWidget *window, GtkWidget *vbox, GtkWidget *button);
GtkWidget *label;
gchar *result_path;
GtkWidget *get_window(void) {
return gtk_window_new(GTK_WINDOW_TOPLEVEL);
}
void build_window(GtkWidget *window, char *title) {
gtk_widget_set_usize(GTK_WIDGET(window), 255, 200);
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_signal_connect(GTK_OBJECT(window), "delete_event",
(GtkSignalFunc) gtk_exit, NULL);
}
GtkWidget *get_vbox(GtkWidget *window) {
return gtk_vbox_new(FALSE, 0);
}
void show_vbox(GtkWidget *window, GtkWidget *vbox) {
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show(vbox);
}
GtkWidget *get_entry(void) {
return gtk_entry_new_with_max_length(256);
}
void build_entry(GtkWidget *vbox, GtkWidget *entry) {
gtk_signal_connect(GTK_OBJECT(entry), "activate", GTK_SIGNAL_FUNC(enter_callback), entry);
gtk_entry_set_text(GTK_ENTRY(entry), "some");
gtk_entry_append_text(GTK_ENTRY(entry), " command");
gtk_entry_select_region(GTK_ENTRY(entry), 0, GTK_ENTRY(entry)->text_length);
gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 0);
}
void enter_callback(GtkWidget *widget, GtkWidget *entry) {
char buf[256], *cp, command[256], no_match[100];
FILE *p;
result_path = gtk_entry_get_text(GTK_ENTRY(entry));
sprintf(command, "which %s", result_path);
sprintf(no_match, "Can't find %s", result_path);
p = popen(cp = command, "r");
while (fgets(buf, sizeof buf, p) != 0);
if (buf[0] != '/') {
gtk_label_set_text(GTK_LABEL(label), no_match);
} else {
gtk_label_set_text(GTK_LABEL(label), buf);
}
pclose(p);
}
GtkWidget *get_frame(void) {
return gtk_frame_new("Path: ");
}
void build_label_frame(GtkWidget *vbox, GtkWidget *frame) {
label = gtk_label_new(result_path);
gtk_container_add(GTK_CONTAINER(frame), label);
gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
}
GtkWidget *get_hbox(void) {
return gtk_hbox_new(FALSE, 0);
}
void show_hbox(GtkWidget *vbox, GtkWidget *hbox) {
gtk_container_add(GTK_CONTAINER(vbox), hbox);
gtk_widget_show(hbox);
}
GtkWidget *get_button(char *msg) {
return gtk_button_new_with_label(msg);
}
GtkWidget *build_button(GtkWidget *window, GtkWidget *vbox, GtkWidget *button) {
gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(gtk_exit), GTK_OBJECT(window));
gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(button);
}
int main(int argc, char *argv[]) {
GtkWidget *window, *vbox, *hbox, *entry, *button, *frame;
gtk_init(&argc, &argv);
window = get_window();
build_window(window, "which(1) GUI");
vbox = get_vbox(window);
show_vbox(window, vbox);
entry = get_entry();
build_entry(vbox, entry);
gtk_widget_show(entry);
frame = get_frame();
build_label_frame(vbox, frame);
gtk_widget_show(label);
gtk_widget_show(frame);
hbox = get_hbox();
show_hbox(vbox, hbox);
button = get_button("Close");
build_button(window, vbox, button);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment