add input directory

This commit is contained in:
2025-12-25 00:09:59 +05:00
parent 53044d2362
commit df37b4c8f2

65
main.c
View File

@@ -5,28 +5,31 @@
typedef struct{
GtkApplication *app;
GtkWidget *window;
GtkWidget *input;
GtkWidget* box;
GtkWidget* grid;
char dir[256];
} Appdata;
static void buttons (GtkApplication *app, Appdata *data, char *directory);
// remove child(grid buttons)
static void remove_child(Appdata *data){
gtk_box_remove(data->box,GTK_WIDGET(data->grid));
}
// action on clicked button
static void on_button_clicked(GtkButton *btn, gpointer user_data){
Appdata *data = (Appdata *)user_data;
const char *label = gtk_button_get_label(btn);
g_print("Button %s clicked\n",label);
// remove child(grid buttons)
GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(data->window));
while (child != NULL) {
GtkWidget *next_child = gtk_widget_get_next_sibling(child);
gtk_window_set_child(GTK_WINDOW(data->window), NULL);
child = next_child;
}
remove_child(data);
// get into char directory label of button with previos dir name
char directory[256];
snprintf(directory, sizeof(directory), "%s/%s", data->dir,label);
// save full path
snprintf(data->dir, sizeof(directory), "%s", directory);
gtk_editable_set_text(GTK_EDITABLE(data->input), ("%s", data->dir));
buttons(data->app,data,directory);
}
// gets number of items and return value in arrays
@@ -38,8 +41,7 @@ int *check_dir(int *arr, DIR *d, char *directory){
if (d) {
while ((dir = readdir(d)) != NULL){
char *name = dir->d_name;
// temporary make them ' ' for get into previos dir
if (*name == ' '){
if (*name == '.'){
printf("directory starts with . : %s\n", name);
}
else{
@@ -55,6 +57,23 @@ int *check_dir(int *arr, DIR *d, char *directory){
}
return 0;
}
// activates after enter dir in input
void on_entry_activate(GtkEntry *entry, Appdata *data){
const gchar *user_text;
GtkTextIter end_iter;
user_text = gtk_editable_get_text(GTK_EDITABLE(entry));
snprintf(data->dir, sizeof(data->dir), "%s", user_text);
remove_child(data);
buttons(data->app,data,data->dir);
}
// make input line
static void input_dir(Appdata *data){
data->input = gtk_entry_new();
gtk_editable_set_text(GTK_EDITABLE(data->input), ("%s", data->dir));
g_signal_connect(data->input, "activate", G_CALLBACK(on_entry_activate), data);
g_print("Set input line\n");
gtk_box_append (GTK_BOX (data->box), data->input);
}
// present buttons
static void buttons (GtkApplication *app, Appdata *data,char *directory){
@@ -64,9 +83,8 @@ static void buttons (GtkApplication *app, Appdata *data,char *directory){
int j = 0;
DIR *d;
struct dirent *dir;
// char directory[100];
d = opendir(directory);
GtkWidget* grid = gtk_grid_new();
data->grid = gtk_grid_new();
check_dir(p, d, directory);
printf("directory: %s\n",directory);
printf("%i %i\n",p[0],p[1]);
@@ -74,8 +92,7 @@ static void buttons (GtkApplication *app, Appdata *data,char *directory){
while ((dir = readdir(d)) != NULL && i <=p[1] && j < g){
if (d) {
char *name = dir->d_name;
// temporary make them ' ' for get into previos dir
if (*name == ' '){
if (*name == '.'){
printf("directory starts with . : %s\n", name);
}
else{
@@ -85,12 +102,12 @@ static void buttons (GtkApplication *app, Appdata *data,char *directory){
}
printf("set directory label: %s\n", name);
GtkWidget* btn = gtk_button_new();
gtk_grid_set_row_spacing(GTK_GRID (grid), 10);
gtk_grid_set_column_spacing(GTK_GRID (grid), 10);
gtk_grid_set_column_homogeneous(GTK_GRID (grid), TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID (grid), TRUE);
gtk_grid_set_row_spacing(GTK_GRID (data->grid), 10);
gtk_grid_set_column_spacing(GTK_GRID (data->grid), 10);
gtk_grid_set_column_homogeneous(GTK_GRID (data->grid), TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID (data->grid), TRUE);
gtk_button_set_label(GTK_BUTTON(btn),name);
gtk_grid_attach (GTK_GRID(grid), btn, j, i, 1,1);
gtk_grid_attach (GTK_GRID(data->grid), btn, j, i, 1,1);
gtk_widget_set_halign(btn, GTK_ALIGN_CENTER);
gtk_widget_set_valign(btn, GTK_ALIGN_CENTER);
g_signal_connect(btn, "clicked",G_CALLBACK(on_button_clicked),data);
@@ -103,14 +120,12 @@ static void buttons (GtkApplication *app, Appdata *data,char *directory){
}
}
closedir(d);
gtk_window_set_child(GTK_WINDOW(data->window),grid);
gtk_window_present(GTK_WINDOW(data->window));
printf("set child and present window %d\n", data->window);
gtk_box_append(GTK_BOX(data->box),data->grid);
}
static void app_activate (GtkApplication *app, gpointer *user_data){
Appdata *data = (Appdata *)user_data;
scanf("%256[^\n]", &data->dir);
snprintf(data->dir, sizeof(data->dir), "/");
// create window if not exist
if (!data->window){
data->window = gtk_application_window_new (GTK_APPLICATION(app));
@@ -119,7 +134,13 @@ static void app_activate (GtkApplication *app, gpointer *user_data){
gtk_window_set_application(GTK_WINDOW(data->window),app);
gtk_window_set_icon_name(GTK_WINDOW(data->window), "filemanager-icon");
}
data->box = gtk_box_new(GTK_ORIENTATION_VERTICAL,10);
gtk_widget_set_valign(data->box,GTK_ALIGN_START);
gtk_window_set_child(GTK_WINDOW(data->window),data->box);
input_dir(data);
buttons(app,data,data->dir);
gtk_window_present(GTK_WINDOW(data->window));
printf("set child and present window %d\n", data->window);
g_print("Started\n");
}