如何在dialog_choose中放置多个选定的文件gtk_tree_view

时间:2016-05-07 14:31:46

标签: gtk3

此代码从dialogo_chosser获取文件并添加gtk_tree_view ... 我会从一个gtkfilechosser中获取更多文件并将它们添加到gtk_tree_view中... 我看到你必须输入这个功能:

gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE)

但我不知道如何选择所选文件:( 我礼貌地问及#34;这个解决方案的一个小例子

我的代码是:

#include "header.h"
#include <string.h>
#include <gtk/gtk.h>

GtkWidget *list;
GtkWidget *win;
char *filename;

void Add_Items_List(GtkWidget *widget, gpointer data)
{
  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));

  gtk_list_store_append(store, &iter);

  gtk_list_store_set(store, &iter, 0, filename, -1);

}

void Dialog_Chooser(GtkWidget *widget, gpointer gst)
{
  GtkWidget *dialog;
  GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
  GtkFileChooser *chooser;
  gint res;

  dialog = gtk_file_chooser_dialog_new("Open File", GTK_WINDOW(win), action,
                       "Cancel", GTK_RESPONSE_CANCEL,
                       "Open", GTK_RESPONSE_ACCEPT, NULL);

  res = gtk_dialog_run(GTK_DIALOG(dialog));

  if(res == GTK_RESPONSE_ACCEPT){
    chooser = GTK_FILE_CHOOSER(dialog);

    filename = gtk_file_chooser_get_filename(chooser);

    Add_Items_List(NULL, NULL);

    g_free(filename);
  }

  gtk_widget_destroy(dialog);
}

void Delete_Item_List(GtkWidget *widget, gpointer selection)
{
  GtkTreeModel *model;
  GtkTreeIter iter;

  model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));

  if(gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection), &model, &iter)){
    gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
  }

}

void Delete_All_Items_List(GtkWidget *widget, gpointer selection)
{
  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));

  gtk_list_store_clear(store);
}

void Inizializes_The_List(GtkWidget *list)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkListStore *store;

  renderer = gtk_cell_renderer_text_new();

  column = gtk_tree_view_column_new_with_attributes("Lenguages", renderer, "text", 0, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

  store = gtk_list_store_new(1, G_TYPE_STRING);

  gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));

  g_object_unref(store);
}

void Add_Lista(GtkWidget *list, const gchar *str)
{
  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));

  gtk_list_store_append(store, &iter);

  gtk_list_store_set(store, &iter, 0, str, -1);
}

int main(int argc, char **argv)
{
  GtkWidget *Button_remove;
  GtkWidget *button_remove_all;
  GtkWidget *button_chosser;
  GtkWidget *sw;
  GtkWidget *hbox;
  GtkWidget *vbox;
  GtkTreeSelection *selection;

  gtk_init(&argc, &argv);

  win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(win), "Simple chosser list");
  gtk_window_set_default_size(GTK_WINDOW(win), 200, 200);
  gtk_container_set_border_width(GTK_CONTAINER(win), 10);

  sw = gtk_scrolled_window_new(NULL, NULL);

  list = gtk_tree_view_new();

  gtk_container_add(GTK_CONTAINER(sw), list);

  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN);

  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);

  vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 5);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);

  Button_remove = gtk_button_new_with_label("Delete");
  button_remove_all = gtk_button_new_with_label("Delte All");
  button_chosser = gtk_button_new_with_label("List");

  gtk_box_pack_start(GTK_BOX(hbox), Button_remove, FALSE, TRUE, 3);
  gtk_box_pack_start(GTK_BOX(hbox), button_remove_all, FALSE, TRUE, 3);
  gtk_box_pack_start(GTK_BOX(hbox), button_chosser, FALSE, TRUE, 3);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);

  gtk_container_add(GTK_CONTAINER(win), vbox);

  Inizializes_The_List(list);

  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));

  g_signal_connect(G_OBJECT(button_chosser), "clicked", G_CALLBACK(Dialog_Chooser), NULL);
  g_signal_connect(Button_remove, "clicked", G_CALLBACK(Delete_Item_List), selection);
  g_signal_connect(button_remove_all, "clicked", G_CALLBACK(Delete_All_Items_List), selection);

  g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(destroy), NULL);

  gtk_widget_show_all(win);

  gtk_main();

  return 0;
}

ps:在ubuntu 16.04上使用gtk + 3

1 个答案:

答案 0 :(得分:1)

目前,我已经找到了这个解决方案:

<!DOCTYPE html>
    <html>
    <head>
        <style>
            body{
                font-family:Arial, sans-serif;
                font-size:14px;
            }
            table{
                border-spacing:0;
                padding:0;
            }
            th{
              text-align:left;
              font-weight:normal !important;
              border-top:1px solid #ddd;
              border-left:1px solid #ddd;
              border-bottom:1px solid #ddd;
                height:25px;
                padding-left:5px;
                width: 50px;
            }
            td{
              border:1px solid #ddd;
                width:30px !important;
                height:25px;
                padding-left:5px;
            }
            tr.row-odd,
            .row-odd{
                background: #eee;
            }
        </style>
        <script src="http://d3js.org/d3.v3.min.js"></script>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>

    </head>
    <body>
        <select size="1" id="sections" type="select" name="style">
        <option>SELECT</option>
        <option value="a">Section A</option>
        <option value="b">Section B</option>
        </select>

        <div id="content">
        </div>

    <script>

    function filterJSON(json, key, value) {
      var result = [];
      for (var foo in json) {
        if (json[foo][key] === value) {
          result.push(json[foo]);
        }
      }

      return result;
    }

    definitions = {
        "a" : "a: footnote 1",
        "b" : "b: footnote 2",
        "c" : "c: footnote 3",
        "d" : "d: footnote 4"
    };

    var notes="";
    var added=[];

    d3.json("data3.json", function(json) {
    d3.json("footnotes.json", function(foot){
                json.forEach(function(d) {
                  d.year = +d.year;
                });

                $('#sections')

                    .on("change", function () {
                        var section = $(this).val();

                        data1 = filterJSON(json, 'set', section);
                        filterFootnotes = filterJSON(foot, 'set',  section);

                        //make an object with footnotes
                            function merge(data1, filterFootnotes) {

                                function makeObj(a) {
                                    obj[a.state] = obj[a.state] || {};
                                    obj[a.state][a.year] = obj[a.state][a.year] || {};
                                    Object.keys(a).forEach(function (k) {
                                        obj[a.state][a.year][k] = a[k];
                                    });
                                }
                                var array = [],
                                    obj = {};

                                data1.forEach(makeObj);
                                filterFootnotes.forEach(makeObj);
                                Object.keys(obj).forEach(function (k) {
                                    Object.keys(obj[k]).forEach(function (kk) {
                                        array.push(obj[k][kk]);
                                    });
                                });
                                return array;
                            }
                            var fullData = merge(data1, filterFootnotes);

                        // add years for select indicator
                        var nestyr = d3.nest()
                            .key(function(d) { return d.year; })
                            .sortKeys(d3.ascending)
                            .map(data1);

                        var yearstring = Object.keys(nestyr);

                        var tableData = [],
                            states = {};
                            fullData.forEach(function (d) {
                            var state = states[d.state];
                            if (!state) {
                                tableData.push(state = states[d.state] = {});
                                }
                                    if ( d.footnote_id ){
                                        state[d.year] = d.value + " <sup class='footnote'>" + d.footnote_id + "</sup>";
                                        if(added.indexOf(d.footnote_id)==-1){
                                          states[d.state]['footnote'] = d.footnote_id;
                                            if(undefined!=definitions[d.footnote_id]){
                                              notes+=definitions[d.footnote_id]+"<br />";
                                              added.push(d.footnote_id);
                                            }
                                            }
                                        } else{
                                            state[d.year] = d.value;
                                        }
                            states[d.state].State = d.state;
                        });

                        yearstring.unshift("State");

                        // render the table
                        tabulate(tableData, yearstring);

                        // add footnotes
                        d3.select("#footnotes").remove();
                        var fnotes = d3.selectAll('#content').append('div').attr("id", "footnotes").html(notes);

                });

                var width = 200, height = 25;
                var minInd = d3.min(json, function(d) { return d.value;} )
                var maxInd = d3.max(json, function(d) { return d.value;} )

                xScale = d3.scale.linear().range([0, width - 10]).domain(d3.extent(json, function(d) { return d.year; })),
                yScale = d3.scale.linear().range([height, 0]).domain([minInd,maxInd]),

                xAxis = d3.svg.axis().scale(xScale).tickFormat(d3.format('0f')),
                yAxis = d3.svg.axis().scale(yScale).orient("left");

        }); // close footnotes json
    }); // close json


    function tabulate(newData, columns) {

                d3.select("#table").remove();

                var table = d3.select('#content').append('table').attr("id", "table")
                var thead = table.append('thead')
                var tbody = table.append('tbody');

                thead.append('tr')
                  .selectAll('th')
                  .data(columns).enter()
                  .append('th')
                    .text(function (column) { return column; });

                var rows = tbody.selectAll('tr')
                  .data(newData)
                  .enter()
                  .append('tr');

                rows.attr("class", function(d, i){ if (i++ % 2 === 0){return 'row-even'}else {return 'row-odd'}});

                var cells = rows.selectAll('td')
                  .data(function (row) {
                    return columns.map(function (column) {
                      return {column: column, value: row[column]};
                    });
                  })
                  .enter()
                  .append('td')
                        .attr("class", function (d,i) { return columns[i]; })
                    .html(function (d) { return d.value; });

              return table;
    };
    </script>
    </body>
    </html>

如果有人知道更好的解决方案,我仍然希望听到它。