File: documentsnavigation.cc

package info (click to toggle)
subtitleeditor 0.56.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 8,880 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (257 lines) | stat: -rw-r--r-- 9,408 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <debug.h>
#include <documents.h>
#include <extension/action.h>
#include <i18n.h>

#include <algorithm>

class DocumentsNavigationPlugin : public Action {
  public:
   DocumentsNavigationPlugin() {
      activate();
      update_ui();
   }

   ~DocumentsNavigationPlugin() {
      deactivate();
   }

   void activate() {
      se_dbg(SE_DBG_PLUGINS);

      // actions
      action_group = Gtk::ActionGroup::create("DocumentsNavigationPlugin");

      action_group->add(Gtk::Action::create("documentsnavigation", _("_Tabs"), _("Select a tab")));

      action_group->add(
         Gtk::Action::create("documentsnavigation-first-document", Gtk::Stock::GOTO_FIRST, _("_First Tab"), _("Select the first (left-most) tab")),
         sigc::bind<int>(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_select_document), FIRST));

      action_group->add(
         Gtk::Action::create("documentsnavigation-last-document", Gtk::Stock::GOTO_LAST, _("_Last Tab"), _("Select the last (right-most) tab")),
         sigc::bind<int>(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_select_document), LAST));

      action_group->add(Gtk::Action::create("documentsnavigation-previous-document",
                                            Gtk::Stock::GO_BACK,
                                            _("_Previous Tab"),
                                            _("Select the tab on the left (or cycle to the right-most one)")),
                        sigc::bind<int>(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_select_document), PREVIOUS));

      action_group->add(Gtk::Action::create("documentsnavigation-next-document",
                                            Gtk::Stock::GO_FORWARD,
                                            _("_Next Tab"),
                                            _("Select the tab on the right (or cycle to the left-most one)")),
                        sigc::bind<int>(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_select_document), NEXT));

      // ui
      Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();

      ui->insert_action_group(action_group);

      Glib::ustring submenu = R"(
      <ui>
        <menubar name='menubar'>
          <menu name='menu-extensions' action='menu-extensions'>
            <placeholder name='placeholder'>
              <menu name='documentsnavigation' action='documentsnavigation'>
                <menuitem action='documentsnavigation-first-document'/>
                <menuitem action='documentsnavigation-last-document'/>
                <separator/>
                <menuitem action='documentsnavigation-previous-document'/>
                <menuitem action='documentsnavigation-next-document'/>
                <separator/>
                <placeholder name='documentsnavigation-documents'/>
              </menu>
            </placeholder>
          </menu>
        </menubar>
      </ui>
    )";

      ui_id = ui->add_ui_from_string(submenu);

      // Update the documents menu when a document is created, deleted or changed
      m_create_document_connection =
         se::documents::signal_created().connect(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_document_create_or_delete));

      m_delete_document_connection =
         se::documents::signal_deleted().connect(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_document_create_or_delete));

      m_document_signals_connection = se::documents::signal_modified().connect(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_document_signals));

      rebuild_documents_menu();
   }

   void deactivate() {
      se_dbg(SE_DBG_PLUGINS);

      Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();

      m_create_document_connection.disconnect();
      m_delete_document_connection.disconnect();
      m_document_signals_connection.disconnect();

      if (action_group_documents) {
         get_ui_manager()->remove_ui(ui_id_documents);
         get_ui_manager()->remove_action_group(action_group_documents);
      }
      ui->remove_ui(ui_id);
      ui->remove_action_group(action_group);
   }

   void update_ui() {
      se_dbg(SE_DBG_PLUGINS);

      bool state = (get_current_document() != NULL);

      action_group->get_action("documentsnavigation-first-document")->set_sensitive(state);
      action_group->get_action("documentsnavigation-last-document")->set_sensitive(state);
      action_group->get_action("documentsnavigation-previous-document")->set_sensitive(state);
      action_group->get_action("documentsnavigation-next-document")->set_sensitive(state);
   }

   // Remove old ui_id and action_group and regenerate menu items.
   void rebuild_documents_menu() {
      if (action_group_documents) {
         get_ui_manager()->remove_ui(ui_id_documents);
         get_ui_manager()->remove_action_group(action_group_documents);
      }

      action_group_documents = Gtk::ActionGroup::create("DocumentsNavigationPluginDocuments");
      get_ui_manager()->insert_action_group(action_group_documents);

      ui_id_documents = get_ui_manager()->new_merge_id();

      guint count = 0;

      auto documents = se::documents::all();

      for (auto it = documents.begin(); it != documents.end(); ++it, ++count) {
         Glib::ustring action_name = Glib::ustring::compose("documentsnavigation-document-%1", count);
         Glib::ustring action_accel = (count < 10) ? Glib::ustring::compose("<alt>%1", (count + 1) % 10) : "";

         action_group_documents->add(Gtk::Action::create(action_name, (*it)->getName()),
                                     Gtk::AccelKey(action_accel),
                                     sigc::bind(sigc::mem_fun(*this, &DocumentsNavigationPlugin::on_documents_menu_activate), count));

         get_ui_manager()->add_ui(ui_id_documents,
                                  "/menubar/menu-extensions/placeholder/documentsnavigation/"
                                  "documentsnavigation-documents",
                                  action_name,
                                  action_name,
                                  Gtk::UI_MANAGER_MENUITEM,
                                  false);
      }
      get_ui_manager()->ensure_update();
   }

   enum { FIRST = 0, LAST = 1, PREVIOUS = 2, NEXT = 3 };

   void on_select_document(int value) {
      se_dbg_msg(SE_DBG_PLUGINS, "select %d", value);

      auto documents = se::documents::all();
      g_return_if_fail(!documents.empty());

      Document* doc = NULL;

      if (value == FIRST)
         doc = documents.front();
      else if (value == LAST)
         doc = documents.back();
      else if (value == PREVIOUS)
         doc = get_document(PREVIOUS);
      else
         doc = get_document(NEXT);

      g_return_if_fail(doc);
      se::documents::active(doc);
   }

   // We want to rebuild the documents menu each time a document is created or
   // delete
   void on_document_create_or_delete(Document* doc) {
      g_return_if_fail(doc);

      rebuild_documents_menu();
   }

   // We want to rebuild the documents menu each time the document property
   // change, like name of the document
   void on_document_signals(Document*, const std::string& signal) {
      if (signal == "document-property-changed")
         rebuild_documents_menu();
   }

   // PREVIOUS or NEXT
   Document* get_document(int value) {
      se_dbg(SE_DBG_PLUGINS);

      Document* current = get_current_document();
      g_return_val_if_fail(current, NULL);

      auto docs = se::documents::all();

      if (value == PREVIOUS)
         std::reverse(docs.begin(), docs.end());

      for (auto it = docs.begin(); it != docs.end(); ++it) {
         if (*it == current) {
            ++it;
            if (it == docs.end())
               return docs.front();
            else
               return *it;
         }
      }
      return NULL;
   }

   void on_documents_menu_activate(gint count) {
      se_dbg_msg(SE_DBG_PLUGINS, "activate document %d", count);

      auto docs = se::documents::all();
      g_return_if_fail(!docs.empty());

      auto it = docs.begin();

      std::advance(it, count);
      g_return_if_fail(it != docs.end());

      se::documents::active(*it);
   }

  protected:
   Gtk::UIManager::ui_merge_id ui_id;
   Glib::RefPtr<Gtk::ActionGroup> action_group;

   Gtk::UIManager::ui_merge_id ui_id_documents;
   Glib::RefPtr<Gtk::ActionGroup> action_group_documents;

   sigc::connection m_create_document_connection;
   sigc::connection m_delete_document_connection;
   sigc::connection m_document_signals_connection;
};

REGISTER_EXTENSION(DocumentsNavigationPlugin)