Advertisement
Combreal

pathSelector01.cpp

Sep 20th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.01 KB | None | 0 0
  1. #include <gtk-3.0\gtk\gtk.h>
  2. #include <windows.h>
  3. #include "bu.h"
  4. #include "folder.h"
  5. #include <string>
  6.  
  7. enum
  8. {
  9.     LIST_ITEM = 0, N_COLUMNS
  10. };
  11.  
  12. typedef struct t_WINDOWSCBSD
  13. {
  14.     GtkWidget *window, *sourceEntry;
  15. } WINDOWSCBSD;
  16.  
  17.  
  18. GtkWidget *list;
  19.  
  20. static void browseFile(GtkWidget *button, gpointer data)
  21. {
  22.     WINDOWSCBSD *myData = (struct t_WINDOWSCBSD *)data;
  23.     std::string path;
  24.     GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
  25.     GtkWidget *dialog = gtk_file_chooser_dialog_new("Open File", GTK_WINDOW(myData->window), action, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL);
  26.     gint res = gtk_dialog_run(GTK_DIALOG(dialog));
  27.     if(res == GTK_RESPONSE_ACCEPT)
  28.     {
  29.         char *filename;
  30.         GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
  31.         filename = gtk_file_chooser_get_filename(chooser);
  32.         path = filename;
  33.         gtk_entry_set_text(GTK_ENTRY(myData->sourceEntry), path.c_str());
  34.         g_free(filename);
  35.     }
  36.     gtk_widget_destroy(dialog);
  37. }
  38.  
  39.  
  40. void appendItem(GtkWidget *widget, gpointer entry)
  41. {  
  42.     GtkListStore *store;
  43.     GtkTreeIter iter;
  44.     const gchar *str = gtk_entry_get_text(GTK_ENTRY(entry));
  45.     store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
  46.     gtk_list_store_append(store, &iter);
  47.     gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);
  48.     gtk_entry_set_text(GTK_ENTRY(entry), "");
  49. }
  50.  
  51. void removeItem(GtkWidget *widget, gpointer selection)
  52. {  
  53.     GtkListStore *store;
  54.     GtkTreeModel *model;
  55.     GtkTreeIter  iter;
  56.     store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
  57.     model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
  58.     if(gtk_tree_model_get_iter_first(model, &iter) == FALSE)
  59.     {
  60.         return;
  61.     }
  62.     if(gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection), &model, &iter))
  63.     {
  64.         gtk_list_store_remove(store, &iter);
  65.     }
  66. }
  67.  
  68. void removeAllItem(GtkWidget *widget, gpointer selection)
  69. {
  70.     GtkListStore *store;
  71.     GtkTreeModel *model;
  72.     GtkTreeIter iter;
  73.     store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
  74.     model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
  75.     if(gtk_tree_model_get_iter_first(model, &iter) == FALSE)
  76.     {
  77.         return;
  78.     }
  79.     gtk_list_store_clear(store);
  80. }
  81.  
  82. void initlist(GtkWidget *list)
  83. {
  84.     GtkCellRenderer *renderer;
  85.     GtkTreeViewColumn *column;
  86.     GtkListStore *store;
  87.     renderer = gtk_cell_renderer_text_new();
  88.     column = gtk_tree_view_column_new_with_attributes("List Item", renderer, "text", LIST_ITEM, NULL);
  89.     gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
  90.     store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);
  91.     gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
  92.     g_object_unref(store);
  93. }
  94.  
  95. int main(int argc, char *argv[])
  96. {
  97.     WINDOWSCBSD data;
  98.     GtkWidget *window, *sw, *grid, *label, *sourceEntry, *browse, *buttonBrowse, *add, *buttonLabelAdd, *remove, *buttonLabelRemove, *removeAll, *buttonLabelRemoveAll, *img;
  99.     GtkTreeSelection *selection;
  100.     GdkPixbuf *iconTitle, *iconButton;
  101.     GtkImage *imgButton=NULL;
  102.  
  103.     gtk_init(&argc, &argv);
  104.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  105.     gtk_window_set_title(GTK_WINDOW(window), "Backup");
  106.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  107.     gtk_container_set_border_width(GTK_CONTAINER (window), 10);
  108.     gtk_widget_set_size_request(window, 500, 260);
  109.     gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
  110.     iconTitle = gdk_pixbuf_new_from_inline(-1, icon_1, false, NULL);
  111.     gtk_window_set_icon(GTK_WINDOW(window), iconTitle);
  112.     data.window = window;
  113.     g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  114.  
  115.     grid = gtk_grid_new();
  116.     gtk_grid_set_row_spacing(GTK_GRID(grid), 7);
  117.     gtk_container_add(GTK_CONTAINER(window), grid);//window
  118.  
  119.     list = gtk_tree_view_new();  
  120.     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
  121.     gtk_widget_set_size_request(list, 485, 150);
  122.     initlist(list);
  123.     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
  124.     gtk_grid_attach(GTK_GRID(grid), list, 0, 0, 6, 1);
  125.  
  126.     label = gtk_label_new(NULL);
  127.     gtk_label_set_markup(GTK_LABEL(label), "<b>Select source : </b>");
  128.     gtk_widget_set_halign(label, GTK_ALIGN_START);
  129.     gtk_grid_attach(GTK_GRID(grid), label, 0, 1, 1, 1);
  130.     sourceEntry = gtk_entry_new();
  131.     gtk_widget_set_size_request(sourceEntry, 75, 18);
  132.     gtk_grid_attach_next_to(GTK_GRID(grid), sourceEntry, label, GTK_POS_RIGHT, 4, 1);
  133.     data.sourceEntry = sourceEntry;
  134.     browse = gtk_button_new_with_label(NULL);
  135.     gtk_widget_set_size_request(browse, 32, 32);
  136.     iconButton = gdk_pixbuf_new_from_inline(-1, icon_2, false, NULL);
  137.     img = gtk_image_new_from_pixbuf(iconButton);
  138.     gtk_button_set_image(GTK_BUTTON(browse), img);
  139.     gtk_button_set_relief(GTK_BUTTON(browse), GTK_RELIEF_NONE);
  140.     gtk_widget_set_tooltip_text(browse, "Select source directory");
  141.     gtk_grid_attach_next_to(GTK_GRID(grid), browse, sourceEntry, GTK_POS_RIGHT, 1, 1);
  142.     g_signal_connect(G_OBJECT(browse), "clicked", G_CALLBACK(browseFile), &data);
  143.  
  144.     add = gtk_button_new_with_label((gchar *)"Add");
  145.     buttonLabelAdd = gtk_bin_get_child(GTK_BIN(add));
  146.     gtk_label_set_markup(GTK_LABEL(buttonLabelAdd), "<b>Add</b>");
  147.     gtk_widget_set_halign(add, GTK_ALIGN_END);
  148.     gtk_grid_attach(GTK_GRID(grid), add, 1, 2, 1, 1);
  149.     g_signal_connect(G_OBJECT(add), "clicked", G_CALLBACK(appendItem), sourceEntry);
  150.     remove = gtk_button_new_with_label((gchar *)"Remove");
  151.     buttonLabelRemove = gtk_bin_get_child(GTK_BIN(remove));
  152.     gtk_label_set_markup(GTK_LABEL(buttonLabelRemove), "<b>Remove</b>");
  153.     gtk_grid_attach_next_to(GTK_GRID(grid), remove, add, GTK_POS_RIGHT, 1, 1);
  154.     g_signal_connect(G_OBJECT(remove), "clicked", G_CALLBACK(removeItem), selection);
  155.     removeAll = gtk_button_new_with_label((gchar *)"Remove All");
  156.     buttonLabelRemoveAll = gtk_bin_get_child(GTK_BIN(removeAll));
  157.     gtk_label_set_markup(GTK_LABEL(buttonLabelRemoveAll), "<b>Remove All</b>");
  158.     gtk_grid_attach_next_to(GTK_GRID(grid), removeAll, remove, GTK_POS_RIGHT, 1, 1);
  159.     g_signal_connect(G_OBJECT(removeAll), "clicked", G_CALLBACK(removeAllItem), selection);
  160.  
  161.     gtk_widget_grab_focus(sourceEntry);
  162.     gtk_widget_show_all(window);
  163.     gtk_main();
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement