~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/ui/dialog/input.cpp

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
#include <map>
12
12
#include <set>
 
13
#include <list>
13
14
#include <glib/gprintf.h>
14
15
#include <glibmm/i18n.h>
15
16
#include <gtkmm/alignment.h>
331
332
class DeviceModelColumns : public Gtk::TreeModel::ColumnRecord
332
333
{
333
334
public:
 
335
    Gtk::TreeModelColumn<bool>                         toggler;
 
336
    Gtk::TreeModelColumn<Glib::ustring>                expander;
334
337
    Gtk::TreeModelColumn<Glib::ustring>                description;
335
 
    Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Pixbuf> >  thumbnail;
 
338
    Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> >   thumbnail;
336
339
    Gtk::TreeModelColumn<Glib::RefPtr<InputDevice const> > device;
337
340
    Gtk::TreeModelColumn<Gdk::InputMode>               mode;
338
341
 
339
 
    DeviceModelColumns() { add(description); add(thumbnail); add(device); add(mode); }
 
342
    DeviceModelColumns() { add(toggler), add(expander), add(description); add(thumbnail); add(device); add(mode); }
340
343
};
341
344
 
342
345
static std::map<Gdk::InputMode, Glib::ustring> &getModeToString()
344
347
    static std::map<Gdk::InputMode, Glib::ustring> mapping;
345
348
    if (mapping.empty()) {
346
349
        mapping[Gdk::MODE_DISABLED] = _("Disabled");
347
 
        mapping[Gdk::MODE_SCREEN]   = _("Screen");
 
350
        mapping[Gdk::MODE_SCREEN]   = C_("Input device", "Screen");
348
351
        mapping[Gdk::MODE_WINDOW]   = _("Window");
349
352
    }
350
353
 
390
393
        static void commitCellModeChange(Glib::ustring const &path, Glib::ustring const &newText, Glib::RefPtr<Gtk::TreeStore> store);
391
394
        static void setModeCellString(Gtk::CellRenderer *rndr, Gtk::TreeIter const &iter);
392
395
 
 
396
        static void commitCellStateChange(Glib::ustring const &path, Glib::RefPtr<Gtk::TreeStore> store);
 
397
        static void setCellStateToggle(Gtk::CellRenderer *rndr, Gtk::TreeIter const &iter);
 
398
 
393
399
        void saveSettings();
394
400
        void useExtToggled();
395
401
 
720
726
    show_all_children();
721
727
}
722
728
 
 
729
class TabletTmp {
 
730
public:
 
731
    TabletTmp() {}
 
732
 
 
733
    Glib::ustring name;
 
734
    std::list<Glib::RefPtr<InputDevice const> > devices;
 
735
};
 
736
 
 
737
static Glib::ustring getCommon( std::list<Glib::ustring> const &names )
 
738
{
 
739
    Glib::ustring result;
 
740
 
 
741
    if ( !names.empty() ) {
 
742
        size_t pos = 0;
 
743
        bool match = true;
 
744
        while ( match ) {
 
745
            if ( names.begin()->length() > pos ) {
 
746
                gunichar ch = (*names.begin())[pos];
 
747
                for ( std::list<Glib::ustring>::const_iterator it = names.begin(); it != names.end(); ++it ) {
 
748
                    if ( (pos >= it->length())
 
749
                         || ((*it)[pos] != ch) ) {
 
750
                        match = false;
 
751
                        break;
 
752
                    }
 
753
                }
 
754
                if (match) {
 
755
                    result += ch;
 
756
                    pos++;
 
757
                }
 
758
            } else {
 
759
                match = false;
 
760
            }
 
761
        }
 
762
    }
 
763
 
 
764
    return result;
 
765
}
 
766
 
723
767
void InputDialogImpl::setupTree( Glib::RefPtr<Gtk::TreeStore> store, Gtk::TreeIter &tablet )
724
768
{
725
769
    std::list<Glib::RefPtr<InputDevice const> > devList = Inkscape::DeviceManager::getManager().getDevices();
727
771
        Gtk::TreeModel::Row row = *(store->append());
728
772
        row[getCols().description] = _("Hardware");
729
773
 
730
 
        tablet = store->append(row.children());
731
 
        Gtk::TreeModel::Row childrow = *tablet;
732
 
        childrow[getCols().description] = _("Tablet");
733
 
        childrow[getCols().thumbnail] = getPix(PIX_TABLET);
 
774
        // Let's make some tablets!!!
 
775
        std::list<TabletTmp> tablets;
 
776
        std::set<Glib::ustring> consumed;
734
777
 
 
778
        // Phase 1 - figure out which tablets are present
735
779
        for ( std::list<Glib::RefPtr<InputDevice const> >::iterator it = devList.begin(); it != devList.end(); ++it ) {
736
780
            Glib::RefPtr<InputDevice const> dev = *it;
737
781
            if ( dev ) {
738
 
//                 g_message("device: name[%s] source[0x%x] mode[0x%x] cursor[%s] axis count[%d] key count[%d]", dev->getName().c_str(), dev->getSource(), dev->getMode(),
739
 
//                           dev->hasCursor() ? "Yes":"no", dev->getNumAxes(), dev->getNumKeys());
740
 
 
741
 
//                 if ( dev->getSource() != Gdk::SOURCE_MOUSE ) {
742
 
                if ( dev ) {
743
 
                    Gtk::TreeModel::Row deviceRow = *(store->append(childrow.children()));
744
 
                    deviceRow[getCols().description] = dev->getName();
745
 
                    deviceRow[getCols().device] = dev;
746
 
                    deviceRow[getCols().mode] = dev->getMode();
747
 
                    switch ( dev->getSource() ) {
748
 
                        case GDK_SOURCE_MOUSE:
749
 
                            deviceRow[getCols().thumbnail] = getPix(PIX_CORE);
750
 
                            break;
751
 
                        case GDK_SOURCE_PEN:
752
 
                            if (deviceRow[getCols().description] == _("pad")) {
753
 
                                deviceRow[getCols().thumbnail] = getPix(PIX_SIDEBUTTONS);
754
 
                            } else {
755
 
                                deviceRow[getCols().thumbnail] = getPix(PIX_TIP);
756
 
                            }
757
 
                            break;
758
 
                        case GDK_SOURCE_CURSOR:
759
 
                            deviceRow[getCols().thumbnail] = getPix(PIX_MOUSE);
760
 
                            break;
761
 
                        case GDK_SOURCE_ERASER:
762
 
                            deviceRow[getCols().thumbnail] = getPix(PIX_ERASER);
763
 
                            break;
764
 
                        default:
765
 
                            ; // nothing
 
782
                if ( dev->getSource() != Gdk::SOURCE_MOUSE ) {
 
783
                    consumed.insert( dev->getId() );
 
784
                    if ( tablets.empty() ) {
 
785
                        TabletTmp tmp;
 
786
                        tablets.push_back(tmp);
766
787
                    }
 
788
                    tablets.back().devices.push_back(dev);
767
789
                }
768
790
            } else {
769
791
                g_warning("Null device in list");
770
792
            }
771
793
        }
 
794
 
 
795
        // Phase 2 - build a UI for the present devices
 
796
        for ( std::list<TabletTmp>::iterator it = tablets.begin(); it != tablets.end(); ++it ) {
 
797
            tablet = store->append(row.children());
 
798
            Gtk::TreeModel::Row childrow = *tablet;
 
799
            if ( it->name.empty() ) {
 
800
                // Check to see if we can derive one
 
801
                std::list<Glib::ustring> names;
 
802
                for ( std::list<Glib::RefPtr<InputDevice const> >::iterator it2 = it->devices.begin(); it2 != it->devices.end(); ++it2 ) {
 
803
                    names.push_back( (*it2)->getName() );
 
804
                }
 
805
                Glib::ustring common = getCommon(names);
 
806
                if ( !common.empty() ) {
 
807
                    it->name = common;
 
808
                }
 
809
            }
 
810
            childrow[getCols().description] = it->name.empty() ? _("Tablet") : it->name ;
 
811
            childrow[getCols().thumbnail] = getPix(PIX_TABLET);
 
812
 
 
813
            // Check if there is an eraser we can link to a pen
 
814
            for ( std::list<Glib::RefPtr<InputDevice const> >::iterator it2 = it->devices.begin(); it2 != it->devices.end(); ++it2 ) {
 
815
                Glib::RefPtr<InputDevice const> dev = *it2;
 
816
                if ( dev->getSource() == Gdk::SOURCE_PEN ) {
 
817
                    for ( std::list<Glib::RefPtr<InputDevice const> >::iterator it3 = it->devices.begin(); it3 != it->devices.end(); ++it3 ) {
 
818
                        Glib::RefPtr<InputDevice const> dev2 = *it3;
 
819
                        if ( dev2->getSource() == Gdk::SOURCE_ERASER ) {
 
820
                            DeviceManager::getManager().setLinkedTo(dev->getId(), dev2->getId());                            
 
821
                            break; // only check the first eraser... for now
 
822
                        }
 
823
                        break; // only check the first pen... for now
 
824
                    }
 
825
                }
 
826
            }
 
827
 
 
828
            for ( std::list<Glib::RefPtr<InputDevice const> >::iterator it2 = it->devices.begin(); it2 != it->devices.end(); ++it2 ) {
 
829
                Glib::RefPtr<InputDevice const> dev = *it2;
 
830
                Gtk::TreeModel::Row deviceRow = *(store->append(childrow.children()));
 
831
                deviceRow[getCols().description] = dev->getName();
 
832
                deviceRow[getCols().device] = dev;
 
833
                deviceRow[getCols().mode] = dev->getMode();
 
834
                switch ( dev->getSource() ) {
 
835
                    case GDK_SOURCE_MOUSE:
 
836
                        deviceRow[getCols().thumbnail] = getPix(PIX_CORE);
 
837
                        break;
 
838
                    case GDK_SOURCE_PEN:
 
839
                        if (deviceRow[getCols().description] == _("pad")) {
 
840
                            deviceRow[getCols().thumbnail] = getPix(PIX_SIDEBUTTONS);
 
841
                        } else {
 
842
                            deviceRow[getCols().thumbnail] = getPix(PIX_TIP);
 
843
                        }
 
844
                        break;
 
845
                    case GDK_SOURCE_CURSOR:
 
846
                        deviceRow[getCols().thumbnail] = getPix(PIX_MOUSE);
 
847
                        break;
 
848
                    case GDK_SOURCE_ERASER:
 
849
                        deviceRow[getCols().thumbnail] = getPix(PIX_ERASER);
 
850
                        break;
 
851
                    default:
 
852
                        ; // nothing
 
853
                }
 
854
            }
 
855
        }
 
856
 
 
857
        for ( std::list<Glib::RefPtr<InputDevice const> >::iterator it = devList.begin(); it != devList.end(); ++it ) {
 
858
            Glib::RefPtr<InputDevice const> dev = *it;
 
859
            if ( dev && (consumed.find( dev->getId() ) == consumed.end()) ) {
 
860
                Gtk::TreeModel::Row deviceRow = *(store->append(row.children()));
 
861
                deviceRow[getCols().description] = dev->getName();
 
862
                deviceRow[getCols().device] = dev;
 
863
                deviceRow[getCols().mode] = dev->getMode();
 
864
                deviceRow[getCols().thumbnail] = getPix(PIX_CORE);
 
865
            }
 
866
        }
772
867
    } else {
773
868
        g_warning("No devices found");
774
869
    }
782
877
    tree(store),
783
878
    treeScroller(),
784
879
    watcher(*this),
785
 
    useExt(_("Use pressure-sensitive tablet (requires restart)")),
786
 
    save(_("Save"))
 
880
    useExt(_("_Use pressure-sensitive tablet (requires restart)"), true),
 
881
    save(_("_Save"), true)
787
882
{
788
883
    pack_start(treeScroller);
789
884
 
806
901
    row = *(poppers->append());
807
902
    row[foo.one] = getModeToString()[Gdk::MODE_WINDOW];
808
903
 
809
 
    Gtk::CellRendererCombo *rendr = new Gtk::CellRendererCombo();
810
 
    rendr->property_model().set_value(poppers);
811
 
    rendr->property_text_column().set_value(0);
812
 
    rendr->property_has_entry() = false;
813
 
 
814
904
    //Add the TreeView's view columns:
 
905
    {
 
906
        Gtk::CellRendererToggle *rendr = new Gtk::CellRendererToggle();
 
907
        Gtk::TreeViewColumn *col = new Gtk::TreeViewColumn("xx", *rendr);
 
908
        if (col) {
 
909
            tree.append_column(*col);
 
910
            col->set_cell_data_func(*rendr, sigc::ptr_fun(setCellStateToggle));
 
911
            rendr->signal_toggled().connect(sigc::bind(sigc::ptr_fun(commitCellStateChange), store));
 
912
        }
 
913
    }
 
914
 
 
915
    int expPos = tree.append_column("", getCols().expander);
 
916
 
815
917
    tree.append_column("I", getCols().thumbnail);
816
918
    tree.append_column("Bar", getCols().description);
817
 
    Gtk::TreeViewColumn *col = new Gtk::TreeViewColumn("X", *rendr);
818
 
    if (col) {
819
 
        tree.append_column(*col);
820
 
        col->set_cell_data_func(*rendr, sigc::ptr_fun(setModeCellString));
821
 
        rendr->signal_edited().connect(sigc::bind(sigc::ptr_fun(commitCellModeChange), store));
822
 
        rendr->property_editable() = true;
 
919
 
 
920
    {
 
921
        Gtk::CellRendererCombo *rendr = new Gtk::CellRendererCombo();
 
922
        rendr->property_model().set_value(poppers);
 
923
        rendr->property_text_column().set_value(0);
 
924
        rendr->property_has_entry() = false;
 
925
        Gtk::TreeViewColumn *col = new Gtk::TreeViewColumn("X", *rendr);
 
926
        if (col) {
 
927
            tree.append_column(*col);
 
928
            col->set_cell_data_func(*rendr, sigc::ptr_fun(setModeCellString));
 
929
            rendr->signal_edited().connect(sigc::bind(sigc::ptr_fun(commitCellModeChange), store));
 
930
            rendr->property_editable() = true;
 
931
        }
823
932
    }
824
933
 
825
934
    tree.set_enable_tree_lines();
826
935
    tree.set_headers_visible(false);
 
936
    tree.set_expander_column( *tree.get_column(expPos - 1) );
827
937
 
828
938
    setupTree( store, tabletIter );
829
939
 
874
984
    }
875
985
}
876
986
 
 
987
void InputDialogImpl::ConfPanel::setCellStateToggle(Gtk::CellRenderer *rndr, Gtk::TreeIter const &iter)
 
988
{
 
989
    if (iter) {
 
990
        Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(rndr);
 
991
        if (toggle) {
 
992
            Glib::RefPtr<InputDevice const> dev = (*iter)[getCols().device];
 
993
            if (dev) {
 
994
                Gdk::InputMode mode = (*iter)[getCols().mode];
 
995
                toggle->set_active(mode != Gdk::MODE_DISABLED);
 
996
            } else {
 
997
                toggle->set_active(false);
 
998
            }
 
999
        }
 
1000
    }
 
1001
}
 
1002
 
 
1003
void InputDialogImpl::ConfPanel::commitCellStateChange(Glib::ustring const &path, Glib::RefPtr<Gtk::TreeStore> store)
 
1004
{
 
1005
    Gtk::TreeIter iter = store->get_iter(path);
 
1006
    if (iter) {
 
1007
        Glib::RefPtr<InputDevice const> dev = (*iter)[getCols().device];
 
1008
        if (dev) {
 
1009
            Gdk::InputMode mode = (*iter)[getCols().mode];
 
1010
            if (mode == Gdk::MODE_DISABLED) {
 
1011
                Inkscape::DeviceManager::getManager().setMode( dev->getId(), Gdk::MODE_SCREEN );
 
1012
            } else {
 
1013
                Inkscape::DeviceManager::getManager().setMode( dev->getId(), Gdk::MODE_DISABLED );
 
1014
            }
 
1015
        }
 
1016
    }
 
1017
}
 
1018
 
877
1019
void InputDialogImpl::ConfPanel::saveSettings()
878
1020
{
879
1021
    Inkscape::DeviceManager::getManager().saveConfig();
1468
1610
  fill-column:99
1469
1611
  End:
1470
1612
*/
1471
 
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
 
1613
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :