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

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

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

revision 2507 by persson, Sun Jan 12 19:37:55 2014 UTC revision 2921 by schoenebeck, Wed May 18 11:57:58 2016 UTC
# Line 17  Line 17 
17   * 02110-1301 USA.   * 02110-1301 USA.
18   */   */
19    
20    #include <glibmmconfig.h>
21    // threads.h must be included first to be able to build with
22    // G_DISABLE_DEPRECATED
23    #if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION == 31 && GLIBMM_MICRO_VERSION >= 2) || \
24        (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION > 31) || GLIBMM_MAJOR_VERSION > 2
25    #include <glibmm/threads.h>
26    #endif
27    
28  #include "dimensionmanager.h"  #include "dimensionmanager.h"
29    
 #include <gtkmm/stock.h>  
30  #include <gtkmm/messagedialog.h>  #include <gtkmm/messagedialog.h>
31  #include <gtkmm/dialog.h>  #include <gtkmm/dialog.h>
32  #include <gtkmm/comboboxtext.h>  #include <gtkmm/comboboxtext.h>
# Line 30  Line 37 
37  #include "compat.h"  #include "compat.h"
38    
39  // returns a human readable name of the given dimension type  // returns a human readable name of the given dimension type
40  static Glib::ustring __dimTypeAsString(gig::dimension_t d) {  Glib::ustring dimTypeAsString(gig::dimension_t d) {
41      char buf[32];      char buf[32];
42      switch (d) {      switch (d) {
43          case gig::dimension_none:          case gig::dimension_none:
# Line 183  static Glib::ustring __dimDescriptionAsS Line 190  static Glib::ustring __dimDescriptionAsS
190      }      }
191  }  }
192    
193    DimTypeCellRenderer::DimTypeCellRenderer() :
194        Glib::ObjectBase(typeid(DimTypeCellRenderer)),
195        Gtk::CellRendererText(),
196        m_propertyDimType(*this, "gigdimension_t", gig::dimension_none),
197        m_propertyUsageCount(*this, "intusagecount", 0),
198        m_propertyTotalRegions(*this, "inttotalregions", 0)
199    {
200        propertyDimType().signal_changed().connect(
201            sigc::mem_fun(*this, &DimTypeCellRenderer::typeChanged)
202        );
203        propertyUsageCount().signal_changed().connect(
204            sigc::mem_fun(*this, &DimTypeCellRenderer::statsChanged)
205        );
206        propertyTotalRegions().signal_changed().connect(
207            sigc::mem_fun(*this, &DimTypeCellRenderer::statsChanged)
208        );
209    }
210    
211    void DimTypeCellRenderer::typeChanged() {
212        gig::dimension_t type = propertyDimType();
213        Glib::ustring s = dimTypeAsString(type);
214        property_text() = s;
215    }
216    
217    void DimTypeCellRenderer::statsChanged() {
218        int usageCount   = propertyUsageCount();
219        int totalRegions = propertyTotalRegions();
220        bool bDimensionExistsOnAllRegions = (usageCount == totalRegions);
221        property_foreground() = ((bDimensionExistsOnAllRegions) ? "black" : "gray");
222    }
223    
224    IntSetCellRenderer::IntSetCellRenderer() :
225        Glib::ObjectBase(typeid(IntSetCellRenderer)),
226        Gtk::CellRendererText(),
227        m_propertyValue(*this, "stdintset", std::set<int>())
228    {
229        propertyValue().signal_changed().connect(
230            sigc::mem_fun(*this, &IntSetCellRenderer::valueChanged)
231        );
232    }
233    
234    void IntSetCellRenderer::valueChanged() {
235        Glib::ustring s;
236        std::set<int> v = propertyValue();
237        for (std::set<int>::const_iterator it = v.begin(); it != v.end(); ++it) {
238            s += ToString(*it);
239            if (*it != *v.rbegin()) s += "|";
240        }
241        property_text() = s;
242        property_foreground() = (v.size() > 1) ? "gray" : "black";
243    }
244    
245  DimensionManager::DimensionManager() :  DimensionManager::DimensionManager() :
246  addButton(Gtk::Stock::ADD), removeButton(Gtk::Stock::REMOVE)      addButton(_("_Add"), true),
247        removeButton(_("_Remove"), true),
248        allRegionsCheckBox(_("All Regions"))
249  {  {
250        ignoreColumnClicked = true;
251    
252      set_title(_("Dimensions of selected Region"));      set_title(_("Dimensions of selected Region"));
253      add(vbox);      add(vbox);
254      scrolledWindow.add(treeView);      scrolledWindow.add(treeView);
# Line 195  addButton(Gtk::Stock::ADD), removeButton Line 258  addButton(Gtk::Stock::ADD), removeButton
258      buttonBox.set_layout(Gtk::BUTTONBOX_END);      buttonBox.set_layout(Gtk::BUTTONBOX_END);
259      buttonBox.set_border_width(5);      buttonBox.set_border_width(5);
260      buttonBox.show();      buttonBox.show();
261        buttonBox.pack_start(allRegionsCheckBox, Gtk::PACK_EXPAND_PADDING);
262      buttonBox.pack_start(addButton, Gtk::PACK_SHRINK);      buttonBox.pack_start(addButton, Gtk::PACK_SHRINK);
263      buttonBox.pack_start(removeButton, Gtk::PACK_SHRINK);      buttonBox.pack_start(removeButton, Gtk::PACK_SHRINK);
264      addButton.show();      addButton.show();
265      removeButton.show();      removeButton.show();
266        allRegionsCheckBox.set_tooltip_text(
267            _("Enable this if you want to edit dimensions of all regions simultaniously.")
268        );
269    
270      // setup the table      // setup the table
271      refTableModel = Gtk::ListStore::create(tableModel);      refTableModel = Gtk::ListStore::create(tableModel);
272      treeView.set_model(refTableModel);      treeView.set_model(refTableModel);
273      treeView.append_column(_("Dimension Type"), tableModel.m_dim_type);      treeView.append_column(_("Dimension Type"), m_cellRendererDimType);
274      treeView.append_column(_("Bits"), tableModel.m_bits);      treeView.append_column(_("Bits"), m_cellRendererIntSet);
275      treeView.append_column(_("Zones"), tableModel.m_zones);      treeView.append_column(_("Zones"), m_cellRendererIntSet);
276      treeView.append_column(_("Description"), tableModel.m_description);      treeView.append_column(_("Description"), tableModel.m_description);
277        treeView.get_column(0)->add_attribute(m_cellRendererDimType.propertyDimType(), tableModel.m_type);
278        treeView.get_column(0)->add_attribute(m_cellRendererDimType.propertyUsageCount(), tableModel.m_usageCount);
279        treeView.get_column(0)->add_attribute(m_cellRendererDimType.propertyTotalRegions(), tableModel.m_totalRegions);
280        treeView.get_column(1)->add_attribute(m_cellRendererIntSet.propertyValue(), tableModel.m_bits);
281        treeView.get_column(2)->add_attribute(m_cellRendererIntSet.propertyValue(), tableModel.m_zones);
282      treeView.show();      treeView.show();
283    
284        treeView.signal_cursor_changed().connect(
285            sigc::mem_fun(*this, &DimensionManager::onColumnClicked)
286        );
287    
288      addButton.signal_clicked().connect(      addButton.signal_clicked().connect(
289          sigc::mem_fun(*this, &DimensionManager::addDimension)          sigc::mem_fun(*this, &DimensionManager::addDimension)
290      );      );
# Line 216  addButton(Gtk::Stock::ADD), removeButton Line 292  addButton(Gtk::Stock::ADD), removeButton
292      removeButton.signal_clicked().connect(      removeButton.signal_clicked().connect(
293          sigc::mem_fun(*this, &DimensionManager::removeDimension)          sigc::mem_fun(*this, &DimensionManager::removeDimension)
294      );      );
295        allRegionsCheckBox.signal_toggled().connect(
296            sigc::mem_fun(*this, &DimensionManager::onAllRegionsCheckBoxToggled)
297        );
298    
299      show_all_children();      show_all_children();
300        
301        resize(460,300);
302    }
303    
304    bool DimensionManager::allRegions() const {
305        return allRegionsCheckBox.get_active();
306  }  }
307    
308    void DimensionManager::onAllRegionsCheckBoxToggled() {
309        set_title(
310            allRegions() ? _("Dimensions of all Regions") :  _("Dimensions of selected Region")
311        );
312        treeView.set_tooltip_text(
313            allRegions()
314                ? _("Dimensions and numbers in gray indicates a difference among the individual regions.")
315                : _("You are currently only viewing dimensions of the currently selected region.")
316        );
317        refreshManager();
318    }
319    
320    // following two data types are just used in DimensionManager::refresManager(),
321    // due to the maps template nature however, they must be declared at global
322    // space to avoid compilation errors
323    struct _DimDef {
324        std::set<int> bits;
325        std::set<int> zones;
326        int usageCount;
327    };
328    typedef std::map<gig::dimension_t, _DimDef> _Dimensions;
329    
330  // update all GUI elements according to current gig::Region informations  // update all GUI elements according to current gig::Region informations
331  void DimensionManager::refreshManager() {  void DimensionManager::refreshManager() {
332        set_sensitive(false);
333      refTableModel->clear();      refTableModel->clear();
334      if (region) {      if (allRegions()) {
335          for (int i = 0; i < region->Dimensions; i++) {          if (region) {
336              gig::dimension_def_t* dim = &region->pDimensionDefinitions[i];              _Dimensions dims;
337              Gtk::TreeModel::Row row = *(refTableModel->append());              gig::Instrument* instr = (gig::Instrument*)region->GetParent();
338              row[tableModel.m_dim_type] = __dimTypeAsString(dim->dimension);              int iRegionsCount = 0;
339              row[tableModel.m_bits] = dim->bits;              for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion(), ++iRegionsCount) {
340              row[tableModel.m_zones] = dim->zones;                  for (uint i = 0; i < rgn->Dimensions; i++) {
341              row[tableModel.m_description] = __dimDescriptionAsString(dim->dimension);                      gig::dimension_def_t* dim = &rgn->pDimensionDefinitions[i];
342              row[tableModel.m_definition] = dim;                      dims[dim->dimension].bits.insert(dim->bits);
343                        dims[dim->dimension].zones.insert(dim->zones);
344                        dims[dim->dimension].usageCount++;
345                    }
346                }
347                for (_Dimensions::const_iterator it = dims.begin(); it != dims.end(); ++it) {
348                    Gtk::TreeModel::Row row = *(refTableModel->append());
349                    row[tableModel.m_type] = it->first;
350                    row[tableModel.m_bits] = it->second.bits;
351                    row[tableModel.m_zones] = it->second.zones;
352                    row[tableModel.m_description] = __dimDescriptionAsString(it->first);
353                    row[tableModel.m_usageCount] = it->second.usageCount;
354                    row[tableModel.m_totalRegions] = iRegionsCount;
355                }
356            }
357        } else {
358            if (region) {
359                for (uint i = 0; i < region->Dimensions; i++) {
360                    gig::dimension_def_t* dim = &region->pDimensionDefinitions[i];
361                    Gtk::TreeModel::Row row = *(refTableModel->append());
362                    std::set<int> vBits;
363                    vBits.insert(dim->bits);
364                    row[tableModel.m_bits] = vBits;
365                    std::set<int> vZones;
366                    vZones.insert(dim->zones);
367                    row[tableModel.m_zones] = vZones;
368                    row[tableModel.m_description] = __dimDescriptionAsString(dim->dimension);
369                    row[tableModel.m_type] = dim->dimension;
370                    row[tableModel.m_usageCount] = 1;
371                    row[tableModel.m_totalRegions] = 1;
372                }
373          }          }
374      }      }
375      set_sensitive(region);      set_sensitive(region);
376  }  }
377    
378  void DimensionManager::show(gig::Region* region) {  void DimensionManager::show(gig::Region* region) {
379        ignoreColumnClicked = true;
380      this->region = region;      this->region = region;
381      refreshManager();      refreshManager();
382      Gtk::Window::show();      Gtk::Window::show();
383      deiconify();      deiconify();
384        ignoreColumnClicked = false;
385  }  }
386    
387  void DimensionManager::set_region(gig::Region* region) {  void DimensionManager::set_region(gig::Region* region) {
388        ignoreColumnClicked = true;
389      this->region = region;      this->region = region;
390      refreshManager();      refreshManager();
391        ignoreColumnClicked = false;
392  }  }
393    
394  void DimensionManager::addDimension() {  void DimensionManager::onColumnClicked() {
395      try {      printf("DimensionManager::onColumnClicked()\n");
396          Gtk::Dialog dialog(_("New Dimension"), true /*modal*/);  
397          // add dimension type combo box to the dialog      //FIXME: BUG: this method is currently very unreliably called, it should actually be called when the user selects another column, it is ATM however also called when the table content changed programmatically causing the dialog below to popup at undesired times !
398    
399        //HACK: Prevents that onColumnClicked() gets called multiple times or at times where it is not desired
400        if (ignoreColumnClicked) {
401            ignoreColumnClicked = false;
402            return;
403        }
404    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 18) || GTKMM_MAJOR_VERSION > 2
405        // prevents app to crash if this dialog is closed
406        if (!get_visible())
407            return;
408    #else
409    # warning Your GTKMM version is too old; dimension manager dialog might crash when changing a dimension type !
410    #endif
411    
412        Gtk::TreeModel::Path path;
413        Gtk::TreeViewColumn* focus_column;
414        treeView.get_cursor(path, focus_column);
415        //const int row = path[0];
416        if (focus_column == treeView.get_column(0)) {
417            Gtk::TreeModel::iterator it = treeView.get_model()->get_iter(path);
418            Gtk::TreeModel::Row row = *it;
419            gig::dimension_t oldType = row[tableModel.m_type];
420    
421            Gtk::Dialog dialog(_("Change Dimension"), true /*modal*/);
422            int oldTypeIndex = -1;
423          Glib::RefPtr<Gtk::ListStore> refComboModel = Gtk::ListStore::create(comboModel);          Glib::RefPtr<Gtk::ListStore> refComboModel = Gtk::ListStore::create(comboModel);
424          for (int i = 0x01; i < 0xff; i++) {          for (int i = 0x01, count = 0; i < 0xff; i++) {
425              Glib::ustring sType =              Glib::ustring sType =
426                  __dimTypeAsString(static_cast<gig::dimension_t>(i));                  dimTypeAsString(static_cast<gig::dimension_t>(i));
427                if (i == oldType) oldTypeIndex = count;
428              if (sType.find("Unknown") != 0) {              if (sType.find("Unknown") != 0) {
429                  Gtk::TreeModel::Row row = *(refComboModel->append());                  Gtk::TreeModel::Row row = *(refComboModel->append());
430                  row[comboModel.m_type_id]   = i;                  row[comboModel.m_type_id]   = i;
431                  row[comboModel.m_type_name] = sType;                  row[comboModel.m_type_name] = sType;
432                    count++;
433              }              }
434          }          }
435          Gtk::Table table(2, 2);          Gtk::Table table(1, 2);
436          Gtk::Label labelDimType(_("Dimension:"), Gtk::ALIGN_START);          Gtk::Label labelDimType(_("Dimension:"), Gtk::ALIGN_START);
437          Gtk::ComboBox comboDimType;          Gtk::ComboBox comboDimType;
438          comboDimType.set_model(refComboModel);          comboDimType.set_model(refComboModel);
439          comboDimType.pack_start(comboModel.m_type_id);          comboDimType.pack_start(comboModel.m_type_id);
440          comboDimType.pack_start(comboModel.m_type_name);          comboDimType.pack_start(comboModel.m_type_name);
         Gtk::Label labelZones(_("Zones:"), Gtk::ALIGN_START);  
441          table.attach(labelDimType, 0, 1, 0, 1);          table.attach(labelDimType, 0, 1, 0, 1);
442          table.attach(comboDimType, 1, 2, 0, 1);          table.attach(comboDimType, 1, 2, 0, 1);
         table.attach(labelZones, 0, 1, 1, 2);  
443          dialog.get_vbox()->pack_start(table);          dialog.get_vbox()->pack_start(table);
444    
445          // number of zones: use a combo box with fix values for gig          dialog.add_button(_("_OK"), 0);
446          // v2 and a spin button for v3          dialog.add_button(_("_Cancel"), 1);
         Gtk::ComboBoxText comboZones;  
         Gtk::SpinButton spinZones;  
         bool version2 = false;  
         if (region) {  
             gig::File* file = (gig::File*)region->GetParent()->GetParent();  
             version2 = file->pVersion && file->pVersion->major == 2;  
         }  
         if (version2) {  
             for (int i = 1; i <= 5; i++) {  
                 char buf[3];  
                 sprintf(buf, "%d", 1 << i);  
 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 24) || GTKMM_MAJOR_VERSION < 2  
                 comboZones.append_text(buf);  
 #else  
                 comboZones.append(buf);  
 #endif  
             }  
             table.attach(comboZones, 1, 2, 1, 2);  
         } else {  
             spinZones.set_increments(1, 8);  
             spinZones.set_numeric(true);  
             spinZones.set_range(2, 128);  
             spinZones.set_value(2);  
             table.attach(spinZones, 1, 2, 1, 2);  
         }  
   
         dialog.add_button(Gtk::Stock::OK, 0);  
         dialog.add_button(Gtk::Stock::CANCEL, 1);  
447          dialog.show_all_children();          dialog.show_all_children();
448            
449            comboDimType.set_active(oldTypeIndex);
450    
451          if (!dialog.run()) { // OK selected ...          if (!dialog.run()) { // OK selected ...
452                ignoreColumnClicked = true;
453              Gtk::TreeModel::iterator iterType = comboDimType.get_active();              Gtk::TreeModel::iterator iterType = comboDimType.get_active();
454              if (!iterType) return;              if (!iterType) return;
455              Gtk::TreeModel::Row rowType = *iterType;              Gtk::TreeModel::Row rowType = *iterType;
456              if (!rowType) return;              if (!rowType) return;
             gig::dimension_def_t dim;  
457              int iTypeID = rowType[comboModel.m_type_id];              int iTypeID = rowType[comboModel.m_type_id];
458              dim.dimension = static_cast<gig::dimension_t>(iTypeID);              gig::dimension_t newType = static_cast<gig::dimension_t>(iTypeID);
459                if (newType == oldType) return;
460              if (version2) {              //printf("change 0x%x -> 0x%x\n", oldType, newType);
461                  if (comboZones.get_active_row_number() < 0) return;  
462                  dim.bits = comboZones.get_active_row_number() + 1;              // assemble the list of regions where the selected dimension type
463                  dim.zones = 1 << dim.bits;              // shall be changed
464              } else {              std::vector<gig::Region*> vRegions;
465                  dim.zones = spinZones.get_value_as_int();              if (allRegions()) {
466                  // Find the number of bits required to hold the                  gig::Instrument* instr = (gig::Instrument*)region->GetParent();
467                  // specified amount of zones.                  for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion()) {
468                  int zoneBits = dim.zones - 1;                      if (rgn->GetDimensionDefinition(oldType)) vRegions.push_back(rgn);
469                  for (dim.bits = 0; zoneBits > 1; dim.bits += 2, zoneBits >>= 2);                  }
470                  dim.bits += zoneBits;              } else vRegions.push_back(region);
471              }  
472              printf(              std::set<Glib::ustring> errors;
473                  "Adding dimension (type=0x%x, bits=%d, zones=%d)\n",  
474                  dim.dimension, dim.bits, dim.zones              for (uint iRgn = 0; iRgn < vRegions.size(); ++iRgn) {
475              );                  gig::Region* region = vRegions[iRgn];
476              // notify everybody that we're going to update the region                  try {
477              region_to_be_changed_signal.emit(region);                      // notify everybody that we're going to update the region
478              // add the new dimension to the region                      region_to_be_changed_signal.emit(region);
479              // (implicitly creates new dimension regions)                      // change the dimension type on that region
480              region->AddDimension(&dim);                      region->SetDimensionType(oldType, newType);
481              // let everybody know there was a change                      // let everybody know there was a change
482              region_changed_signal.emit(region);                      region_changed_signal.emit(region);
483                    } catch (RIFF::Exception e) {
484                        // notify that the changes are over (i.e. to avoid dead locks)
485                        region_changed_signal.emit(region);
486                        Glib::ustring txt = _("Could not alter dimension: ") + e.Message;
487                        if (vRegions.size() == 1) {
488                            // show error message directly
489                            Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
490                            msg.run();
491                        } else {
492                            // remember error, they are shown after all regions have been processed
493                            errors.insert(txt);
494                        }
495                    }
496                }
497              // update all GUI elements              // update all GUI elements
498              refreshManager();              refreshManager();
499    
500                if (!errors.empty()) {
501                    Glib::ustring txt = _(
502                        "The following errors occurred while trying to change the dimension type on all regions:"
503                    );
504                    txt += "\n\n";
505                    for (std::set<Glib::ustring>::const_iterator it = errors.begin();
506                        it != errors.end(); ++it)
507                    {
508                        txt += "-> " + *it + "\n";
509                    }
510                    txt += "\n";
511                    txt += _(
512                        "You might also want to check the console for further warnings and "
513                        "error messages."
514                    );
515                    Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
516                    msg.run();
517                }
518          }          }
     } catch (RIFF::Exception e) {  
         // notify that the changes are over (i.e. to avoid dead locks)  
         region_changed_signal.emit(region);  
         // show error message  
         Glib::ustring txt = _("Could not add dimension: ") + e.Message;  
         Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);  
         msg.run();  
519      }      }
520  }  }
521    
522  void DimensionManager::removeDimension() {  void DimensionManager::addDimension() {
523        Gtk::Dialog dialog(_("New Dimension"), true /*modal*/);
524        // add dimension type combo box to the dialog
525        Glib::RefPtr<Gtk::ListStore> refComboModel = Gtk::ListStore::create(comboModel);
526        for (int i = 0x01; i < 0xff; i++) {
527            Glib::ustring sType =
528                dimTypeAsString(static_cast<gig::dimension_t>(i));
529            if (sType.find("Unknown") != 0) {
530                Gtk::TreeModel::Row row = *(refComboModel->append());
531                row[comboModel.m_type_id]   = i;
532                row[comboModel.m_type_name] = sType;
533            }
534        }
535        Gtk::Table table(2, 2);
536        Gtk::Label labelDimType(_("Dimension:"), Gtk::ALIGN_START);
537        Gtk::ComboBox comboDimType;
538        comboDimType.set_model(refComboModel);
539        comboDimType.pack_start(comboModel.m_type_id);
540        comboDimType.pack_start(comboModel.m_type_name);
541        Gtk::Label labelZones(_("Zones:"), Gtk::ALIGN_START);
542        table.attach(labelDimType, 0, 1, 0, 1);
543        table.attach(comboDimType, 1, 2, 0, 1);
544        table.attach(labelZones, 0, 1, 1, 2);
545        dialog.get_vbox()->pack_start(table);
546    
547        // number of zones: use a combo box with fix values for gig
548        // v2 and a spin button for v3
549        Gtk::ComboBoxText comboZones;
550        Gtk::SpinButton spinZones;
551        bool version2 = false;
552        if (region) {
553            gig::File* file = (gig::File*)region->GetParent()->GetParent();
554            version2 = file->pVersion && file->pVersion->major == 2;
555        }
556        if (version2) {
557            for (int i = 1; i <= 5; i++) {
558                char buf[3];
559                sprintf(buf, "%d", 1 << i);
560    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 24) || GTKMM_MAJOR_VERSION < 2
561                comboZones.append_text(buf);
562    #else
563                comboZones.append(buf);
564    #endif
565            }
566            table.attach(comboZones, 1, 2, 1, 2);
567        } else {
568            spinZones.set_increments(1, 8);
569            spinZones.set_numeric(true);
570            spinZones.set_range(2, 128);
571            spinZones.set_value(2);
572            table.attach(spinZones, 1, 2, 1, 2);
573        }
574    
575        dialog.add_button(_("_OK"), 0);
576        dialog.add_button(_("_Cancel"), 1);
577        dialog.show_all_children();
578    
579        if (!dialog.run()) { // OK selected ...
580            Gtk::TreeModel::iterator iterType = comboDimType.get_active();
581            if (!iterType) return;
582            Gtk::TreeModel::Row rowType = *iterType;
583            if (!rowType) return;
584            int iTypeID = rowType[comboModel.m_type_id];
585            gig::dimension_t type = static_cast<gig::dimension_t>(iTypeID);
586            gig::dimension_def_t dim;
587            dim.dimension = type;
588    
589            if (version2) {
590                if (comboZones.get_active_row_number() < 0) return;
591                dim.bits = comboZones.get_active_row_number() + 1;
592                dim.zones = 1 << dim.bits;
593            } else {
594                dim.zones = spinZones.get_value_as_int();
595                dim.bits = zoneCountToBits(dim.zones);
596            }
597    
598            // assemble the list of regions where the selected dimension shall be
599            // added to
600            std::vector<gig::Region*> vRegions;
601            if (allRegions()) {
602                gig::Instrument* instr = (gig::Instrument*)region->GetParent();
603                for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion()) {
604                    if (!rgn->GetDimensionDefinition(type)) vRegions.push_back(rgn);
605                }
606            } else vRegions.push_back(region);
607                
608            std::set<Glib::ustring> errors;
609    
610            for (uint iRgn = 0; iRgn < vRegions.size(); ++iRgn) {
611                gig::Region* region = vRegions[iRgn];
612                try {
613                    printf(
614                        "Adding dimension (type=0x%x, bits=%d, zones=%d)\n",
615                        dim.dimension, dim.bits, dim.zones
616                    );
617                    // notify everybody that we're going to update the region
618                    region_to_be_changed_signal.emit(region);
619                    // add the new dimension to the region
620                    // (implicitly creates new dimension regions)
621                    region->AddDimension(&dim);
622                    // let everybody know there was a change
623                    region_changed_signal.emit(region);
624                } catch (RIFF::Exception e) {
625                    // notify that the changes are over (i.e. to avoid dead locks)
626                    region_changed_signal.emit(region);
627                    Glib::ustring txt = _("Could not add dimension: ") + e.Message;
628                    if (vRegions.size() == 1) {
629                        // show error message directly
630                        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
631                        msg.run();
632                    } else {
633                        // remember error, they are shown after all regions have been processed
634                        errors.insert(txt);
635                    }
636                }
637            }
638            // update all GUI elements
639            refreshManager();
640    
641            if (!errors.empty()) {
642                Glib::ustring txt = _(
643                    "The following errors occurred while trying to create the dimension on all regions:"
644                );
645                txt += "\n\n";
646                for (std::set<Glib::ustring>::const_iterator it = errors.begin();
647                     it != errors.end(); ++it)
648                {
649                    txt += "-> " + *it + "\n";
650                }
651                txt += "\n";
652                txt += _(
653                    "You might also want to check the console for further warnings and "
654                    "error messages."
655                );
656                Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
657                msg.run();
658            }
659        }
660    }
661    
662    void DimensionManager::removeDimension() {        
663      Glib::RefPtr<Gtk::TreeSelection> sel = treeView.get_selection();      Glib::RefPtr<Gtk::TreeSelection> sel = treeView.get_selection();
664      Gtk::TreeModel::iterator it = sel->get_selected();      Gtk::TreeModel::iterator it = sel->get_selected();
665      if (it) {      if (it) {
666          try {          Gtk::TreeModel::Row row = *it;
667              // notify everybody that we're going to update the region          gig::dimension_t type = row[tableModel.m_type];
668              region_to_be_changed_signal.emit(region);  
669              // remove selected dimension          // assemble the list of regions where the selected dimension shall be
670              Gtk::TreeModel::Row row = *it;          // added to
671              gig::dimension_def_t* dim = row[tableModel.m_definition];          std::vector<gig::Region*> vRegions;
672              region->DeleteDimension(dim);          if (allRegions()) {
673              // let everybody know there was a change              gig::Instrument* instr = (gig::Instrument*)region->GetParent();
674              region_changed_signal.emit(region);              for (gig::Region* rgn = instr->GetFirstRegion(); rgn; rgn = instr->GetNextRegion()) {
675              // update all GUI elements                  if (rgn->GetDimensionDefinition(type)) vRegions.push_back(rgn);
676              refreshManager();              }
677          } catch (RIFF::Exception e) {          } else vRegions.push_back(region);
678              // notify that the changes are over (i.e. to avoid dead locks)  
679              region_changed_signal.emit(region);          std::set<Glib::ustring> errors;
680              // show error message  
681              Glib::ustring txt = _("Could not remove dimension: ") + e.Message;          for (uint iRgn = 0; iRgn < vRegions.size(); ++iRgn) {
682                gig::Region* region = vRegions[iRgn];
683                gig::dimension_def_t* dim = region->GetDimensionDefinition(type);
684                try {
685                    // notify everybody that we're going to update the region
686                    region_to_be_changed_signal.emit(region);
687                    // remove selected dimension    
688                    region->DeleteDimension(dim);
689                    // let everybody know there was a change
690                    region_changed_signal.emit(region);
691                } catch (RIFF::Exception e) {
692                    // notify that the changes are over (i.e. to avoid dead locks)
693                    region_changed_signal.emit(region);
694                    Glib::ustring txt = _("Could not remove dimension: ") + e.Message;
695                    if (vRegions.size() == 1) {
696                        // show error message directly
697                        Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
698                        msg.run();
699                    } else {
700                        // remember error, they are shown after all regions have been processed
701                        errors.insert(txt);
702                    }
703                }
704            }
705            // update all GUI elements
706            refreshManager();
707    
708            if (!errors.empty()) {
709                Glib::ustring txt = _(
710                    "The following errors occurred while trying to remove the dimension from all regions:"
711                );
712                txt += "\n\n";
713                for (std::set<Glib::ustring>::const_iterator it = errors.begin();
714                     it != errors.end(); ++it)
715                {
716                    txt += "-> " + *it + "\n";
717                }
718                txt += "\n";
719                txt += _(
720                    "You might also want to check the console for further warnings and "
721                    "error messages."
722                );
723              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);              Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR);
724              msg.run();              msg.run();
725          }          }

Legend:
Removed from v.2507  
changed lines
  Added in v.2921

  ViewVC Help
Powered by ViewVC