/[svn]/gigedit/trunk/src/gigedit/CombineInstrumentsDialog.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/CombineInstrumentsDialog.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3225 by schoenebeck, Fri May 26 22:10:16 2017 UTC revision 3456 by persson, Sun Jan 27 10:07:56 2019 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (c) 2014-2017 Christian Schoenebeck      Copyright (c) 2014-2019 Christian Schoenebeck
3            
4      This file is part of "gigedit" and released under the terms of the      This file is part of "gigedit" and released under the terms of the
5      GNU General Public License version 2.      GNU General Public License version 2.
# Line 20  Line 20 
20  #include <string.h>  #include <string.h>
21    
22  #include <glibmm/ustring.h>  #include <glibmm/ustring.h>
23  #include <gtkmm/stock.h>  #if HAS_GTKMM_STOCK
24    # include <gtkmm/stock.h>
25    #endif
26  #include <gtkmm/messagedialog.h>  #include <gtkmm/messagedialog.h>
27  #include <gtkmm/label.h>  #include <gtkmm/label.h>
28    #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()
29    
30  Glib::ustring dimTypeAsString(gig::dimension_t d);  Glib::ustring dimTypeAsString(gig::dimension_t d);
31    
# Line 781  static void combineInstruments(std::vect Line 784  static void combineInstruments(std::vect
784  CombineInstrumentsDialog::CombineInstrumentsDialog(Gtk::Window& parent, gig::File* gig)  CombineInstrumentsDialog::CombineInstrumentsDialog(Gtk::Window& parent, gig::File* gig)
785      : ManagedDialog(_("Combine Instruments"), parent, true),      : ManagedDialog(_("Combine Instruments"), parent, true),
786        m_gig(gig), m_fileWasChanged(false), m_newCombinedInstrument(NULL),        m_gig(gig), m_fileWasChanged(false), m_newCombinedInstrument(NULL),
787    #if HAS_GTKMM_STOCK
788        m_cancelButton(Gtk::Stock::CANCEL), m_OKButton(Gtk::Stock::OK),        m_cancelButton(Gtk::Stock::CANCEL), m_OKButton(Gtk::Stock::OK),
789        m_descriptionLabel(), m_tableDimCombo(2, 2), m_comboDimType(),  #else
790          m_cancelButton(_("_Cancel"), true), m_OKButton(_("_OK"), true),
791    #endif
792          m_descriptionLabel(),
793    #if USE_GTKMM_GRID
794          m_tableDimCombo(),
795    #else
796          m_tableDimCombo(2, 2),
797    #endif
798          m_comboDimType(),
799        m_labelDimType(Glib::ustring(_("Combine by Dimension:")) + "  ", Gtk::ALIGN_END)        m_labelDimType(Glib::ustring(_("Combine by Dimension:")) + "  ", Gtk::ALIGN_END)
800  {  {
801      if (!Settings::singleton()->autoRestoreWindowDimension) {      if (!Settings::singleton()->autoRestoreWindowDimension) {
# Line 793  CombineInstrumentsDialog::CombineInstrum Line 806  CombineInstrumentsDialog::CombineInstrum
806      m_scrolledWindow.add(m_treeView);      m_scrolledWindow.add(m_treeView);
807      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
808    
809    #if USE_GTKMM_BOX
810        get_content_area()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK);
811        get_content_area()->pack_start(m_tableDimCombo, Gtk::PACK_SHRINK);
812        get_content_area()->pack_start(m_scrolledWindow);
813        get_content_area()->pack_start(m_labelOrder, Gtk::PACK_SHRINK);
814        get_content_area()->pack_start(m_iconView, Gtk::PACK_SHRINK);
815        get_content_area()->pack_start(m_buttonBox, Gtk::PACK_SHRINK);
816    #else
817      get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK);      get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK);
818      get_vbox()->pack_start(m_tableDimCombo, Gtk::PACK_SHRINK);      get_vbox()->pack_start(m_tableDimCombo, Gtk::PACK_SHRINK);
819      get_vbox()->pack_start(m_scrolledWindow);      get_vbox()->pack_start(m_scrolledWindow);
820        get_vbox()->pack_start(m_labelOrder, Gtk::PACK_SHRINK);
821        get_vbox()->pack_start(m_iconView, Gtk::PACK_SHRINK);
822      get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK);      get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK);
823    #endif
824    
825  #if GTKMM_MAJOR_VERSION >= 3  #if GTKMM_MAJOR_VERSION >= 3
826      m_descriptionLabel.set_line_wrap();      m_descriptionLabel.set_line_wrap();
# Line 840  CombineInstrumentsDialog::CombineInstrum Line 864  CombineInstrumentsDialog::CombineInstrum
864          "Use SHIFT + left click or CTRL + left click to select the instruments "          "Use SHIFT + left click or CTRL + left click to select the instruments "
865          "you want to combine."          "you want to combine."
866      ));      ));
867      m_treeView.append_column("Instrument", m_columns.m_col_name);      m_treeView.append_column(_("Nr"), m_columns.m_col_index);
868      m_treeView.set_headers_visible(false);      m_treeView.append_column(_("Instrument"), m_columns.m_col_name);
869        m_treeView.set_headers_visible(true);
870      m_treeView.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);      m_treeView.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
871      m_treeView.get_selection()->signal_changed().connect(      m_treeView.get_selection()->signal_changed().connect(
872          sigc::mem_fun(*this, &CombineInstrumentsDialog::onSelectionChanged)          sigc::mem_fun(*this, &CombineInstrumentsDialog::onSelectionChanged)
# Line 866  CombineInstrumentsDialog::CombineInstrum Line 891  CombineInstrumentsDialog::CombineInstrum
891          Glib::ustring name(gig_to_utf8(instr->pInfo->Name));          Glib::ustring name(gig_to_utf8(instr->pInfo->Name));
892          Gtk::TreeModel::iterator iter = m_refTreeModel->append();          Gtk::TreeModel::iterator iter = m_refTreeModel->append();
893          Gtk::TreeModel::Row row = *iter;          Gtk::TreeModel::Row row = *iter;
894            row[m_columns.m_col_index] = i;
895          row[m_columns.m_col_name] = name;          row[m_columns.m_col_name] = name;
896          row[m_columns.m_col_instr] = instr;          row[m_columns.m_col_instr] = instr;
897      }      }
898    
899        m_refOrderModel = Gtk::ListStore::create(m_orderColumns);
900        m_iconView.set_model(m_refOrderModel);
901        m_iconView.set_tooltip_text(_("Use drag & drop to change the order."));
902        m_iconView.set_markup_column(1);
903        m_iconView.set_selection_mode(Gtk::SELECTION_SINGLE);
904        // force background to retain white also on selections
905        // (this also fixes a bug with GTK 2 which often causes visibility issue
906        //  with the text of the selected item)
907        {
908    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
909            Gdk::Color white;
910    #else
911            Gdk::RGBA white;
912    #endif
913            white.set("#ffffff");
914            GtkWidget* widget = (GtkWidget*) m_iconView.gobj();
915    #if GTK_MAJOR_VERSION < 3
916            gtk_widget_modify_base(widget, GTK_STATE_SELECTED, white.gobj());
917            gtk_widget_modify_base(widget, GTK_STATE_ACTIVE, white.gobj());
918            gtk_widget_modify_bg(widget, GTK_STATE_SELECTED, white.gobj());
919            gtk_widget_modify_bg(widget, GTK_STATE_ACTIVE, white.gobj());
920    #endif
921        }
922    
923        m_labelOrder.set_text(_("Order of the instruments to be combined:"));
924    
925        // establish drag&drop within the instrument tree view, allowing to reorder
926        // the sequence of instruments within the gig file
927        {
928            std::vector<Gtk::TargetEntry> drag_target_instrument;
929            drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument"));
930            m_iconView.drag_source_set(drag_target_instrument);
931            m_iconView.drag_dest_set(drag_target_instrument);
932            m_iconView.signal_drag_begin().connect(
933                sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drag_begin)
934            );
935            m_iconView.signal_drag_data_get().connect(
936                sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drag_data_get)
937            );
938            m_iconView.signal_drag_data_received().connect(
939                sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drop_drag_data_received)
940            );
941        }
942    
943      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
944    #if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION > 24)
945        m_buttonBox.set_margin(5);
946    #else
947      m_buttonBox.set_border_width(5);      m_buttonBox.set_border_width(5);
948    #endif
949      m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK);      m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK);
950      m_buttonBox.pack_start(m_OKButton, Gtk::PACK_SHRINK);      m_buttonBox.pack_start(m_OKButton, Gtk::PACK_SHRINK);
951      m_buttonBox.show();      m_buttonBox.show();
# Line 888  CombineInstrumentsDialog::CombineInstrum Line 962  CombineInstrumentsDialog::CombineInstrum
962          sigc::mem_fun(*this, &CombineInstrumentsDialog::combineSelectedInstruments)          sigc::mem_fun(*this, &CombineInstrumentsDialog::combineSelectedInstruments)
963      );      );
964    
965    #if HAS_GTKMM_SHOW_ALL_CHILDREN
966      show_all_children();      show_all_children();
967    #endif
968    
969        Settings::singleton()->showTooltips.get_proxy().signal_changed().connect(
970            sigc::mem_fun(*this, &CombineInstrumentsDialog::on_show_tooltips_changed)
971        );
972        on_show_tooltips_changed();
973    
974      // show a warning to user if he uses a .gig in v2 format      // show a warning to user if he uses a .gig in v2 format
975      if (gig->pVersion->major < 3) {      if (gig->pVersion->major < 3) {
# Line 901  CombineInstrumentsDialog::CombineInstrum Line 982  CombineInstrumentsDialog::CombineInstrum
982          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_WARNING);          Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_WARNING);
983          msg.run();          msg.run();
984      }      }
985    
986        // OK button should have focus by default for quick combining with Return key
987        m_OKButton.grab_focus();
988    }
989    
990    void CombineInstrumentsDialog::on_order_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
991    {
992        printf("Drag begin\n");
993        first_call_to_drag_data_get = true;
994    }
995    
996    void CombineInstrumentsDialog::on_order_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context,
997                                                           Gtk::SelectionData& selection_data, guint, guint)
998    {
999        printf("Drag data get\n");
1000        if (!first_call_to_drag_data_get) return;
1001        first_call_to_drag_data_get = false;
1002    
1003        // get selected source instrument
1004        gig::Instrument* src = NULL;
1005        {
1006            std::vector<Gtk::TreeModel::Path> rows = m_iconView.get_selected_items();
1007            if (!rows.empty()) {
1008                Gtk::TreeModel::iterator it = m_refOrderModel->get_iter(rows[0]);
1009                if (it) {
1010                    Gtk::TreeModel::Row row = *it;
1011                    src = row[m_orderColumns.m_col_instr];
1012                }
1013            }
1014        }
1015        if (!src) {
1016            printf("Drag data get: !src\n");
1017            return;
1018        }
1019        printf("src=%ld\n", (size_t)src);
1020    
1021        // pass the source gig::Instrument as pointer
1022        selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src,
1023                           sizeof(src)/*length of data in bytes*/);
1024    }
1025    
1026    void CombineInstrumentsDialog::on_order_drop_drag_data_received(
1027        const Glib::RefPtr<Gdk::DragContext>& context, int x, int y,
1028        const Gtk::SelectionData& selection_data, guint, guint time)
1029    {
1030        printf("Drag data received\n");
1031        if (!selection_data.get_data()) {
1032            printf("selection_data.get_data() == NULL\n");
1033            return;
1034        }
1035    
1036        gig::Instrument* src = *((gig::Instrument**) selection_data.get_data());
1037        if (!src || selection_data.get_length() != sizeof(gig::Instrument*)) {
1038            printf("!src\n");
1039            return;
1040        }
1041        printf("src=%ld\n", (size_t)src);
1042    
1043        gig::Instrument* dst = NULL;
1044        {
1045            Gtk::TreeModel::Path path = m_iconView.get_path_at_pos(x, y);
1046            if (!path) return;
1047    
1048            Gtk::TreeModel::iterator iter = m_refOrderModel->get_iter(path);
1049            if (!iter) return;
1050            Gtk::TreeModel::Row row = *iter;
1051            dst = row[m_orderColumns.m_col_instr];
1052        }
1053        if (!dst) {
1054            printf("!dst\n");
1055            return;
1056        }
1057    
1058        printf("dragdrop received src='%s' dst='%s'\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str());
1059    
1060        // swap the two items
1061        typedef Gtk::TreeModel::Children Children;
1062        Children children = m_refOrderModel->children();
1063        Children::iterator itSrc, itDst;
1064        int i = 0, iSrc = -1, iDst = -1;
1065        for (Children::iterator iter = children.begin();
1066             iter != children.end(); ++iter, ++i)
1067        {
1068            Gtk::TreeModel::Row row = *iter;
1069            if (row[m_orderColumns.m_col_instr] == src) {
1070                itSrc = iter;
1071                iSrc  = i;
1072            } else if (row[m_orderColumns.m_col_instr] == dst) {
1073                itDst = iter;
1074                iDst  = i;
1075            }
1076        }
1077        if (itSrc && itDst) {
1078            // swap elements
1079            m_refOrderModel->iter_swap(itSrc, itDst);
1080            // update markup
1081            Gtk::TreeModel::Row rowSrc = *itSrc;
1082            Gtk::TreeModel::Row rowDst = *itDst;
1083            {
1084                Glib::ustring name = rowSrc[m_orderColumns.m_col_name];
1085                Glib::ustring markup =
1086                    "<span foreground='black' background='white'>" + ToString(iDst+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>";
1087                rowSrc[m_orderColumns.m_col_markup] = markup;
1088            }
1089            {
1090                Glib::ustring name = rowDst[m_orderColumns.m_col_name];
1091                Glib::ustring markup =
1092                    "<span foreground='black' background='white'>" + ToString(iSrc+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>";
1093                rowDst[m_orderColumns.m_col_markup] = markup;
1094            }
1095        }
1096    }
1097    
1098    void CombineInstrumentsDialog::setSelectedInstruments(const std::set<int>& instrumentIndeces) {
1099        typedef Gtk::TreeModel::Children Children;
1100        Children children = m_refTreeModel->children();
1101        for (Children::iterator iter = children.begin();
1102             iter != children.end(); ++iter)
1103        {
1104            Gtk::TreeModel::Row row = *iter;
1105            int index = row[m_columns.m_col_index];
1106            if (instrumentIndeces.count(index))
1107                m_treeView.get_selection()->select(iter);
1108        }
1109        // hack: OK button lost focus after doing the above, it should have focus by default for quick combining with Return key
1110        m_OKButton.grab_focus();
1111  }  }
1112    
1113  void CombineInstrumentsDialog::combineSelectedInstruments() {  void CombineInstrumentsDialog::combineSelectedInstruments() {
1114      std::vector<gig::Instrument*> instruments;      std::vector<gig::Instrument*> instruments;
1115      std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows();      {
1116      for (uint i = 0; i < v.size(); ++i) {          typedef Gtk::TreeModel::Children Children;
1117          Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(v[i]);          int i = 0;
1118          Gtk::TreeModel::Row row = *it;          Children selection = m_refOrderModel->children();
1119          Glib::ustring name = row[m_columns.m_col_name];          for (Children::iterator it = selection.begin();
1120          gig::Instrument* instrument = row[m_columns.m_col_instr];               it != selection.end(); ++it, ++i)
1121          #if DEBUG_COMBINE_INSTRUMENTS          {
1122          printf("Selection '%s' 0x%lx\n\n", name.c_str(), int64_t((void*)instrument));              Gtk::TreeModel::Row row = *it;
1123          #endif              Glib::ustring name = row[m_orderColumns.m_col_name];
1124          instruments.push_back(instrument);              gig::Instrument* instrument = row[m_orderColumns.m_col_instr];
1125                #if DEBUG_COMBINE_INSTRUMENTS
1126                printf("Selection %d. '%s' %p\n\n", (i+1), name.c_str(), instrument));
1127                #endif
1128                instruments.push_back(instrument);
1129            }
1130      }      }
1131    
1132      g_warnings.clear();      g_warnings.clear();
# Line 931  void CombineInstrumentsDialog::combineSe Line 1143  void CombineInstrumentsDialog::combineSe
1143              mainDimension = static_cast<gig::dimension_t>(iTypeID);              mainDimension = static_cast<gig::dimension_t>(iTypeID);
1144          }          }
1145    
1146          // now start the actual cobination task ...          // now start the actual combination task ...
1147          combineInstruments(instruments, m_gig, m_newCombinedInstrument, mainDimension);          combineInstruments(instruments, m_gig, m_newCombinedInstrument, mainDimension);
1148      } catch (RIFF::Exception e) {;      } catch (RIFF::Exception e) {;
1149          Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);          Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
# Line 971  void CombineInstrumentsDialog::combineSe Line 1183  void CombineInstrumentsDialog::combineSe
1183  void CombineInstrumentsDialog::onSelectionChanged() {  void CombineInstrumentsDialog::onSelectionChanged() {
1184      std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows();      std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows();
1185      m_OKButton.set_sensitive(v.size() >= 2);      m_OKButton.set_sensitive(v.size() >= 2);
1186    
1187        typedef Gtk::TreeModel::Children Children;
1188    
1189        // update horizontal selection list (icon view) ...
1190    
1191        // remove items which are not part of the new selection anymore
1192        {
1193            Children allOrdered = m_refOrderModel->children();
1194            for (Children::iterator itOrder = allOrdered.begin();
1195                 itOrder != allOrdered.end(); ++itOrder)
1196            {
1197                Gtk::TreeModel::Row rowOrder = *itOrder;
1198                gig::Instrument* instr = rowOrder[m_orderColumns.m_col_instr];
1199                for (uint i = 0; i < v.size(); ++i) {
1200                    Gtk::TreeModel::iterator itSel = m_refTreeModel->get_iter(v[i]);
1201                    Gtk::TreeModel::Row rowSel = *itSel;
1202                    if (rowSel[m_columns.m_col_instr] == instr)
1203                        goto nextOrderedItem;
1204                }
1205                goto removeOrderedItem;
1206            nextOrderedItem:
1207                continue;
1208            removeOrderedItem:
1209                m_refOrderModel->erase(itOrder);
1210            }
1211        }
1212    
1213        // add items newly added to the selection
1214        for (uint i = 0; i < v.size(); ++i) {
1215            Gtk::TreeModel::iterator itSel = m_refTreeModel->get_iter(v[i]);
1216            Gtk::TreeModel::Row rowSel = *itSel;
1217            gig::Instrument* instr = rowSel[m_columns.m_col_instr];
1218            Children allOrdered = m_refOrderModel->children();
1219            for (Children::iterator itOrder = allOrdered.begin();
1220                 itOrder != allOrdered.end(); ++itOrder)
1221            {
1222                Gtk::TreeModel::Row rowOrder = *itOrder;
1223                if (rowOrder[m_orderColumns.m_col_instr] == instr)
1224                    goto nextSelectionItem;
1225            }
1226            goto addNewSelectionItem;
1227        nextSelectionItem:
1228            continue;
1229        addNewSelectionItem:
1230            Glib::ustring name = gig_to_utf8(instr->pInfo->Name);
1231            Gtk::TreeModel::iterator iterOrder = m_refOrderModel->append();
1232            Gtk::TreeModel::Row rowOrder = *iterOrder;
1233            rowOrder[m_orderColumns.m_col_name] = name;
1234            rowOrder[m_orderColumns.m_col_instr] = instr;
1235        }
1236    
1237        // update markup
1238        {
1239            int i = 0;
1240            Children allOrdered = m_refOrderModel->children();
1241            for (Children::iterator itOrder = allOrdered.begin();
1242                 itOrder != allOrdered.end(); ++itOrder, ++i)
1243            {
1244                Gtk::TreeModel::Row rowOrder = *itOrder;
1245                Glib::ustring name = rowOrder[m_orderColumns.m_col_name];
1246                Glib::ustring markup =
1247                    "<span foreground='black' background='white'>" + ToString(i+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>";
1248                rowOrder[m_orderColumns.m_col_markup] = markup;
1249            }
1250        }
1251    }
1252    
1253    void CombineInstrumentsDialog::on_show_tooltips_changed() {
1254        const bool b = Settings::singleton()->showTooltips;
1255    
1256        m_treeView.set_has_tooltip(b);
1257        m_iconView.set_has_tooltip(b);
1258    
1259        set_has_tooltip(b);
1260  }  }
1261    
1262  bool CombineInstrumentsDialog::fileWasChanged() const {  bool CombineInstrumentsDialog::fileWasChanged() const {

Legend:
Removed from v.3225  
changed lines
  Added in v.3456

  ViewVC Help
Powered by ViewVC