/[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 3299 by schoenebeck, Sun Jul 9 12:44:59 2017 UTC revision 3300 by schoenebeck, Sun Jul 9 18:15:02 2017 UTC
# Line 796  CombineInstrumentsDialog::CombineInstrum Line 796  CombineInstrumentsDialog::CombineInstrum
796      get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK);      get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK);
797      get_vbox()->pack_start(m_tableDimCombo, Gtk::PACK_SHRINK);      get_vbox()->pack_start(m_tableDimCombo, Gtk::PACK_SHRINK);
798      get_vbox()->pack_start(m_scrolledWindow);      get_vbox()->pack_start(m_scrolledWindow);
799        get_vbox()->pack_start(m_labelOrder, Gtk::PACK_SHRINK);
800        get_vbox()->pack_start(m_iconView, Gtk::PACK_SHRINK);
801      get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK);      get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK);
802    
803  #if GTKMM_MAJOR_VERSION >= 3  #if GTKMM_MAJOR_VERSION >= 3
# Line 872  CombineInstrumentsDialog::CombineInstrum Line 874  CombineInstrumentsDialog::CombineInstrum
874          row[m_columns.m_col_instr] = instr;          row[m_columns.m_col_instr] = instr;
875      }      }
876    
877        m_refOrderModel = Gtk::ListStore::create(m_orderColumns);
878        m_iconView.set_model(m_refOrderModel);
879        m_iconView.set_tooltip_text(_("Use drag & drop to change the order."));
880        m_iconView.set_markup_column(1);
881        m_iconView.set_selection_mode(Gtk::SELECTION_SINGLE);
882        // force background to retain white also on selections
883        // (this also fixes a bug with GTK 2 which often causes visibility issue
884        //  with the text of the selected item)
885        {
886            Gdk::Color white;
887            white.set("#ffffff");
888            m_iconView.modify_base(Gtk::STATE_SELECTED, white);
889            m_iconView.modify_base(Gtk::STATE_ACTIVE, white);
890            m_iconView.modify_bg(Gtk::STATE_SELECTED, white);
891            m_iconView.modify_bg(Gtk::STATE_ACTIVE, white);
892        }
893    
894        m_labelOrder.set_text(_("Order of the instruments to be combined:"));
895    
896        // establish drag&drop within the instrument tree view, allowing to reorder
897        // the sequence of instruments within the gig file
898        {
899            std::vector<Gtk::TargetEntry> drag_target_instrument;
900            drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument"));
901            m_iconView.drag_source_set(drag_target_instrument);
902            m_iconView.drag_dest_set(drag_target_instrument);
903            m_iconView.signal_drag_begin().connect(
904                sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drag_begin)
905            );
906            m_iconView.signal_drag_data_get().connect(
907                sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drag_data_get)
908            );
909            m_iconView.signal_drag_data_received().connect(
910                sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drop_drag_data_received)
911            );
912        }
913    
914      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);      m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
915      m_buttonBox.set_border_width(5);      m_buttonBox.set_border_width(5);
916      m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK);      m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK);
# Line 905  CombineInstrumentsDialog::CombineInstrum Line 944  CombineInstrumentsDialog::CombineInstrum
944      }      }
945  }  }
946    
947    void CombineInstrumentsDialog::on_order_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
948    {
949        printf("Drag begin\n");
950        first_call_to_drag_data_get = true;
951    }
952    
953    void CombineInstrumentsDialog::on_order_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context,
954                                                           Gtk::SelectionData& selection_data, guint, guint)
955    {
956        printf("Drag data get\n");
957        if (!first_call_to_drag_data_get) return;
958        first_call_to_drag_data_get = false;
959    
960        // get selected source instrument
961        gig::Instrument* src = NULL;
962        {
963            std::vector<Gtk::TreeModel::Path> rows = m_iconView.get_selected_items();
964            if (!rows.empty()) {
965                Gtk::TreeModel::iterator it = m_refOrderModel->get_iter(rows[0]);
966                if (it) {
967                    Gtk::TreeModel::Row row = *it;
968                    src = row[m_orderColumns.m_col_instr];
969                }
970            }
971        }
972        if (!src) {
973            printf("Drag data get: !src\n");
974            return;
975        }
976        printf("src=%ld\n", (size_t)src);
977    
978        // pass the source gig::Instrument as pointer
979        selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src,
980                           sizeof(src)/*length of data in bytes*/);
981    }
982    
983    void CombineInstrumentsDialog::on_order_drop_drag_data_received(
984        const Glib::RefPtr<Gdk::DragContext>& context, int x, int y,
985        const Gtk::SelectionData& selection_data, guint, guint time)
986    {
987        printf("Drag data received\n");
988        if (&selection_data == NULL) {
989            printf("!selection_data\n");
990            return;
991        }
992        if (!selection_data.get_data()) {
993            printf("selection_data.get_data() == NULL\n");
994            return;
995        }
996    
997        gig::Instrument* src = *((gig::Instrument**) selection_data.get_data());
998        if (!src || selection_data.get_length() != sizeof(gig::Instrument*)) {
999            printf("!src\n");
1000            return;
1001        }
1002        printf("src=%d\n", src);
1003    
1004        gig::Instrument* dst = NULL;
1005        {
1006            Gtk::TreeModel::Path path = m_iconView.get_path_at_pos(x, y);
1007            if (!path) return;
1008    
1009            Gtk::TreeModel::iterator iter = m_refOrderModel->get_iter(path);
1010            if (!iter) return;
1011            Gtk::TreeModel::Row row = *iter;
1012            dst = row[m_orderColumns.m_col_instr];
1013        }
1014        if (!dst) {
1015            printf("!dst\n");
1016            return;
1017        }
1018    
1019        printf("dragdrop received src='%s' dst='%s'\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str());
1020    
1021        // swap the two items
1022        typedef Gtk::TreeModel::Children Children;
1023        Children children = m_refOrderModel->children();
1024        Children::iterator itSrc, itDst;
1025        int i = 0, iSrc = -1, iDst = -1;
1026        for (Children::iterator iter = children.begin();
1027             iter != children.end(); ++iter, ++i)
1028        {
1029            Gtk::TreeModel::Row row = *iter;
1030            if (row[m_orderColumns.m_col_instr] == src) {
1031                itSrc = iter;
1032                iSrc  = i;
1033            } else if (row[m_orderColumns.m_col_instr] == dst) {
1034                itDst = iter;
1035                iDst  = i;
1036            }
1037        }
1038        if (itSrc && itDst) {
1039            // swap elements
1040            m_refOrderModel->iter_swap(itSrc, itDst);
1041            // update markup
1042            Gtk::TreeModel::Row rowSrc = *itSrc;
1043            Gtk::TreeModel::Row rowDst = *itDst;
1044            {
1045                Glib::ustring name = rowSrc[m_orderColumns.m_col_name];
1046                Glib::ustring markup =
1047                    "<span foreground='black' background='white'>" + ToString(iDst+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>";
1048                rowSrc[m_orderColumns.m_col_markup] = markup;
1049            }
1050            {
1051                Glib::ustring name = rowDst[m_orderColumns.m_col_name];
1052                Glib::ustring markup =
1053                    "<span foreground='black' background='white'>" + ToString(iSrc+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>";
1054                rowDst[m_orderColumns.m_col_markup] = markup;
1055            }
1056        }
1057    }
1058    
1059  void CombineInstrumentsDialog::setSelectedInstruments(const std::set<int>& instrumentIndeces) {  void CombineInstrumentsDialog::setSelectedInstruments(const std::set<int>& instrumentIndeces) {
1060      typedef Gtk::TreeModel::Children Children;      typedef Gtk::TreeModel::Children Children;
1061      Children children = m_refTreeModel->children();      Children children = m_refTreeModel->children();
# Line 920  void CombineInstrumentsDialog::setSelect Line 1071  void CombineInstrumentsDialog::setSelect
1071    
1072  void CombineInstrumentsDialog::combineSelectedInstruments() {  void CombineInstrumentsDialog::combineSelectedInstruments() {
1073      std::vector<gig::Instrument*> instruments;      std::vector<gig::Instrument*> instruments;
1074      std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows();      {
1075      for (uint i = 0; i < v.size(); ++i) {          typedef Gtk::TreeModel::Children Children;
1076          Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(v[i]);          int i = 0;
1077          Gtk::TreeModel::Row row = *it;          Children selection = m_refOrderModel->children();
1078          Glib::ustring name = row[m_columns.m_col_name];          for (Children::iterator it = selection.begin();
1079          gig::Instrument* instrument = row[m_columns.m_col_instr];               it != selection.end(); ++it, ++i)
1080          #if DEBUG_COMBINE_INSTRUMENTS          {
1081          printf("Selection '%s' 0x%lx\n\n", name.c_str(), int64_t((void*)instrument));              Gtk::TreeModel::Row row = *it;
1082          #endif              Glib::ustring name = row[m_orderColumns.m_col_name];
1083          instruments.push_back(instrument);              gig::Instrument* instrument = row[m_orderColumns.m_col_instr];
1084                #if DEBUG_COMBINE_INSTRUMENTS
1085                printf("Selection %d. '%s' %p\n\n", (i+1), name.c_str(), instrument));
1086                #endif
1087                instruments.push_back(instrument);
1088            }
1089      }      }
1090    
1091      g_warnings.clear();      g_warnings.clear();
# Line 946  void CombineInstrumentsDialog::combineSe Line 1102  void CombineInstrumentsDialog::combineSe
1102              mainDimension = static_cast<gig::dimension_t>(iTypeID);              mainDimension = static_cast<gig::dimension_t>(iTypeID);
1103          }          }
1104    
1105          // now start the actual cobination task ...          // now start the actual combination task ...
1106          combineInstruments(instruments, m_gig, m_newCombinedInstrument, mainDimension);          combineInstruments(instruments, m_gig, m_newCombinedInstrument, mainDimension);
1107      } catch (RIFF::Exception e) {;      } catch (RIFF::Exception e) {;
1108          Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);          Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR);
# Line 986  void CombineInstrumentsDialog::combineSe Line 1142  void CombineInstrumentsDialog::combineSe
1142  void CombineInstrumentsDialog::onSelectionChanged() {  void CombineInstrumentsDialog::onSelectionChanged() {
1143      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();
1144      m_OKButton.set_sensitive(v.size() >= 2);      m_OKButton.set_sensitive(v.size() >= 2);
1145    
1146        typedef Gtk::TreeModel::Children Children;
1147    
1148        // update horizontal selection list (icon view) ...
1149    
1150        // remove items which are not part of the new selection anymore
1151        {
1152            Children allOrdered = m_refOrderModel->children();
1153            for (Children::iterator itOrder = allOrdered.begin();
1154                 itOrder != allOrdered.end(); ++itOrder)
1155            {
1156                Gtk::TreeModel::Row rowOrder = *itOrder;
1157                gig::Instrument* instr = rowOrder[m_orderColumns.m_col_instr];
1158                for (uint i = 0; i < v.size(); ++i) {
1159                    Gtk::TreeModel::iterator itSel = m_refTreeModel->get_iter(v[i]);
1160                    Gtk::TreeModel::Row rowSel = *itSel;
1161                    if (rowSel[m_columns.m_col_instr] == instr)
1162                        goto nextOrderedItem;
1163                }
1164                goto removeOrderedItem;
1165            nextOrderedItem:
1166                continue;
1167            removeOrderedItem:
1168                m_refOrderModel->erase(itOrder);
1169            }
1170        }
1171    
1172        // add items newly added to the selection
1173        for (uint i = 0; i < v.size(); ++i) {
1174            Gtk::TreeModel::iterator itSel = m_refTreeModel->get_iter(v[i]);
1175            Gtk::TreeModel::Row rowSel = *itSel;
1176            gig::Instrument* instr = rowSel[m_columns.m_col_instr];
1177            Children allOrdered = m_refOrderModel->children();
1178            for (Children::iterator itOrder = allOrdered.begin();
1179                 itOrder != allOrdered.end(); ++itOrder)
1180            {
1181                Gtk::TreeModel::Row rowOrder = *itOrder;
1182                if (rowOrder[m_orderColumns.m_col_instr] == instr)
1183                    goto nextSelectionItem;
1184            }
1185            goto addNewSelectionItem;
1186        nextSelectionItem:
1187            continue;
1188        addNewSelectionItem:
1189            Glib::ustring name = gig_to_utf8(instr->pInfo->Name);
1190            Gtk::TreeModel::iterator iterOrder = m_refOrderModel->append();
1191            Gtk::TreeModel::Row rowOrder = *iterOrder;
1192            rowOrder[m_orderColumns.m_col_name] = name;
1193            rowOrder[m_orderColumns.m_col_instr] = instr;
1194        }
1195    
1196        // update markup
1197        {
1198            int i = 0;
1199            Children allOrdered = m_refOrderModel->children();
1200            for (Children::iterator itOrder = allOrdered.begin();
1201                 itOrder != allOrdered.end(); ++itOrder, ++i)
1202            {
1203                Gtk::TreeModel::Row rowOrder = *itOrder;
1204                Glib::ustring name = rowOrder[m_orderColumns.m_col_name];
1205                Glib::ustring markup =
1206                    "<span foreground='black' background='white'>" + ToString(i+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>";
1207                rowOrder[m_orderColumns.m_col_markup] = markup;
1208            }
1209        }
1210  }  }
1211    
1212  bool CombineInstrumentsDialog::fileWasChanged() const {  bool CombineInstrumentsDialog::fileWasChanged() const {

Legend:
Removed from v.3299  
changed lines
  Added in v.3300

  ViewVC Help
Powered by ViewVC