summaryrefslogtreecommitdiff
path: root/src/gui_gtk.ccbak
blob: fac8886ae19124ec9fc36f754c3f7251d963cd44 (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// -*- mode: c++; c-basic-offset: 2; -*-

#include "common.hh"

#include <gtk/gtk.h>
#include <memory>
#include <unordered_map>
#include <vector>

#include "gui_about.hh"
#include "gui_main.hh"
#include "gui_menu.hh"
#include "observers.hh"

namespace {

template<typename T>
class shared_gobject {
public:
  shared_gobject()
    : ptr_(nullptr) {
  }
  shared_gobject(std::nullptr_t)
    : ptr_(nullptr) {
  }
  explicit shared_gobject(T* ptr)
    : ptr_(ptr) {
  }
  shared_gobject(shared_gobject const& obj)
    : ptr_(obj.ptr_) {
    if (ptr_) g_object_ref(ptr_);
  }
  shared_gobject(shared_gobject&& obj)
    : ptr_(obj.ptr_) {
    obj.ptr = nullptr;
  }

  ~shared_gobject() {
    reset();
  }

  shared_gobject& operator=(shared_gobject const& obj) {
    reset(obj.ptr_);
    if (ptr_) g_object_ref(ptr_);
    return *this;
  }
  shared_gobject& operator=(shared_gobject&& obj) {
    ptr_ = obj.ptr_;
    obj.ptr_ = nullptr;
    return *this;
  }

  void swap(shared_gobject& obj) {
    auto x = ptr_;
    ptr_ = obj.ptr_;
    obj.ptr_ = x;
  }

  void reset() {
    if (ptr_) {
      g_object_unref(ptr_);
      ptr_ = nullptr;
    }
  }

  void reset(T* ptr) {
    if (ptr_) g_object_unref(ptr_);
    ptr_ = ptr;
  }

  T* get() const {
    return ptr_;
  }

  T& operator*() const {
    return *ptr_;
  }

  T* operator->() const {
    return ptr_;
  }

  explicit operator bool() const {
    return ptr_ != nullptr;
  }

private:
  T* ptr_;
};

#define MAIN_APP_TYPE main_app_get_type()

G_DECLARE_FINAL_TYPE(MainApp, main_app, MAIN, APP, GtkApplication)

#define MAIN_APP_WINDOW_TYPE main_app_window_get_type()
G_DECLARE_FINAL_TYPE(MainAppWindow, main_app_window, MAIN, APP_WINDOW,
                     GtkApplicationWindow)

class GtkGuiWindow : public GuiWindow {
public:
  GtkWindow* window() const {
    return reinterpret_cast<GtkWindow*>(impl());
  }

  void set_title(std::string const& title) override;
};

class GtkGuiMain : public virtual GuiMain, public GtkGuiWindow {
public:
  GtkGuiMain(std::string const& title, uint32_t width, uint32_t height)
    : title_(title), width_(width), height_(height) {
  }

  void set_menu(GuiMenu* menu) override {
    assert(menu);
    menu_ = menu;
  }

  GuiMenu* menu() const override {
    return menu_;
  }

  bool run(int argc, char** argv) override;

  bool exit() override;

  void set_title(std::string const& title) override;

  void show(GuiWindow* window) override;

  uint32_t default_width() const {
    return width_;
  }

  uint32_t default_height() const {
    return height_;
  }

  void add_listener(GuiMain::Listener* listener) override {
    observers_.insert(listener);
  }

  void remove_listener(GuiMain::Listener* listener) override {
    observers_.erase(listener);
  }

  void* impl() const override;

private:
  bool notify_about_to_exit() {
    auto it = observers_.notify();
    while (it.has_next()) {
      if (!it.next()->about_to_exit()) return false;
    }
    return true;
  }

  std::string title_;
  uint32_t width_;
  uint32_t height_;

  shared_gobject<MainApp> app_;
  Observers<GuiMain::Listener*> observers_;
  GuiMenu* menu_;
};

class GtkGuiMenu : public GuiMenu {
public:
  GtkGuiMenu()
    : parent_(nullptr), menu_(g_menu_new()), map_(nullptr) {
  }

  ~GtkGuiMenu() override {
  }

  void add_listener(Listener* listener) override {
    if (parent_) {
      parent_->add_listener(listener);
      return;
    }
    observers_.insert(listener);
  }

  void remove_listener(Listener* listener) override {
    if (parent_) {
      parent_->remove_listener(listener);
      return;
    }
    observers_.erase(listener);
  }

  GMenuModel* model() const {
    return G_MENU_MODEL(menu_.get());
  }

  void set_map(GActionMap* map);
  void activate(GSimpleAction* action);

  void add_item(std::string const& id, std::string const& label) override;
  GuiMenu* add_menu(std::string const& label) override;
  bool enable_item(std::string const& id, bool enable) override;

private:
  explicit GtkGuiMenu(GtkGuiMenu* parent)
    : parent_(parent), menu_(g_menu_new()), map_(nullptr) {
  }

  void notify_item_activated(std::string const& id) {
    auto it = observers_.notify();
    while (it.has_next()) {
      it.next()->item_activated(id);
    }
  }

  std::string add_action(std::string const& id);
  std::string escape(std::string const& id);
  std::string unescape(std::string const& action);

  GtkGuiMenu* const parent_;
  shared_gobject<GMenu> menu_;
  std::unordered_map<std::string, shared_gobject<GSimpleAction>> action_;
  GActionMap* map_;
  std::vector<std::unique_ptr<GtkGuiMenu>> submenus_;
  Observers<Listener*> observers_;
};

void close_about_dialog(GtkAboutDialog* about) {
  // TODO: Restore dialog state
  gtk_widget_hide(GTK_WIDGET(about));
}

class GtkGuiAbout : public virtual GuiAbout, public GtkGuiWindow {
public:
  GtkGuiAbout(std::string const& title, std::string const& version,
              std::vector<std::string> const& authors)
    : about_(GTK_ABOUT_DIALOG(gtk_about_dialog_new())) {
    gtk_about_dialog_set_program_name(about_, title.c_str());
    gtk_about_dialog_set_version(about_, version.c_str());
    std::vector<std::string> tmp;
    tmp.reserve(authors.size() / 2);
    for (size_t i = 0; i < authors.size(); i += 2) {
      if (authors[i + 1].empty()) {
        tmp.push_back(authors[i]);
      } else {
        tmp.push_back(authors[i] + " <" + authors[i + 1] + ">");
      }
    }
    std::vector<gchar const*> tmp2;
    tmp2.reserve(tmp.size() + 1);
    for (auto const& str : tmp) tmp2.push_back(str.c_str());
    tmp2.push_back(nullptr);
    gtk_about_dialog_set_authors(about_, tmp2.data());

    g_signal_connect(about_, "delete-event",
                     G_CALLBACK(gtk_widget_hide_on_delete), nullptr);
    g_signal_connect(about_, "response",
                     G_CALLBACK(close_about_dialog), nullptr);
  }

  ~GtkGuiAbout() override {
    gtk_widget_destroy(GTK_WIDGET(about_));
  }

  void* impl() const override {
    return about_;
  }

  void set_title(std::string const& title) override {
    GtkGuiWindow::set_title(title);
  }

  void add_listener(GuiAbout::Listener* listener) override {
    observers_.insert(listener);
  }

  void remove_listener(GuiAbout::Listener* listener) override {
    observers_.erase(listener);
  }

private:
  GtkAboutDialog* about_;
  Observers<GuiAbout::Listener*> observers_;
};

struct _MainApp
{
  GtkApplication parent;
  GtkGuiMain* main;
};

struct _MainAppWindow
{
  GtkApplicationWindow parent;
};

G_DEFINE_TYPE(MainApp, main_app, GTK_TYPE_APPLICATION);

G_DEFINE_TYPE(MainAppWindow, main_app_window, GTK_TYPE_APPLICATION_WINDOW);

MainAppWindow* main_app_window_new(MainApp *app) {
  auto ret = MAIN_APP_WINDOW(g_object_new(MAIN_APP_WINDOW_TYPE,
                                          "application", app, nullptr));
  gtk_window_set_default_size(GTK_WINDOW(ret), app->main->default_width(),
                              app->main->default_height());
  return ret;
}

void main_app_activate(GApplication* g_app) {
  auto app = MAIN_APP(g_app);
  auto menu = static_cast<GtkGuiMenu*>(app->main->menu());
  if (menu) {
    menu->set_map(G_ACTION_MAP(app));
    gtk_application_set_menubar(GTK_APPLICATION(app), menu->model());
  }
  auto win = main_app_window_new(app);
  gtk_window_present(GTK_WINDOW(win));
}

void main_app_window_open(MainAppWindow* win, GFile* file) {
}

void main_app_open(GApplication* app, GFile** files, gint n_files,
                   gchar const* hint) {
  auto windows = gtk_application_get_windows(GTK_APPLICATION(app));
  auto win = windows ? MAIN_APP_WINDOW(windows->data)
    : main_app_window_new(MAIN_APP(app));

  for (auto i = 0; i < n_files; i++) {
    main_app_window_open(win, files[i]);
  }

  gtk_window_present(GTK_WINDOW(win));
}

void main_app_class_init(MainAppClass* clazz) {
  G_APPLICATION_CLASS(clazz)->activate = main_app_activate;
  G_APPLICATION_CLASS(clazz)->open = main_app_open;
}

void main_app_window_init(MainAppWindow* app) {
}

void main_app_window_class_init(MainAppWindowClass* clazz) {
}

MainApp* main_app_new(GtkGuiMain* main) {
  auto ret = MAIN_APP(g_object_new(MAIN_APP_TYPE,
                                   "application-id", "org.jk.tp",
                                   "flags", G_APPLICATION_HANDLES_OPEN,
                                   nullptr));
  ret->main = main;
  return ret;
}

void GtkGuiWindow::set_title(std::string const& title) {
  auto win = window();
  if (!win) return;
  gtk_window_set_title(win, title.c_str());
}

bool GtkGuiMain::run(int argc, char** argv) {
  app_.reset(main_app_new(this));
  return g_application_run(G_APPLICATION(app_.get()), argc, argv);
}

void* GtkGuiMain::impl() const {
  if (!app_) return nullptr;
  auto windows = gtk_application_get_windows(GTK_APPLICATION(app_.get()));
  if (!windows) return nullptr;
  return windows->data;
}

void GtkGuiMain::set_title(std::string const& title) {
  title_ = title;
  GtkGuiWindow::set_title(title);
}

bool GtkGuiMain::exit() {
  if (!app_) {
    assert(false);
    return true;
  }
  if (!notify_about_to_exit()) return false;
  g_application_quit(G_APPLICATION(app_.get()));
  return true;
}

void GtkGuiMain::show(GuiWindow* window) {
  auto wnd = reinterpret_cast<GtkWindow*>(window->impl());
  if (!wnd) {
    assert(false);
    return;
  }
  gtk_window_set_transient_for(wnd, this->window());
  gtk_window_present(wnd);
}

void GtkGuiMenu::add_item(std::string const& id, std::string const& label) {
  auto action = add_action(id);
  g_menu_append(menu_.get(), label.c_str(), action.c_str());
}

GuiMenu* GtkGuiMenu::add_menu(std::string const& label) {
  auto ret = new GtkGuiMenu(this);
  g_menu_append_submenu(menu_.get(), label.c_str(), ret->model());
  submenus_.emplace_back(ret);
  return ret;
}

void GtkGuiMenu::set_map(GActionMap* map) {
  assert(!parent_);
  assert(!map_);
  assert(map);
  map_ = map;
  for (auto const& pair : action_) {
    g_action_map_add_action(map_, G_ACTION(pair.second.get()));
  }
}

std::string GtkGuiMenu::escape(std::string const& action) {
  // TODO
  return action;
}

std::string GtkGuiMenu::unescape(std::string const& action) {
  return action;
}

void menu_item_activate(GSimpleAction* action, GVariant* parameter,
                        gpointer* data) {
  auto menu = reinterpret_cast<GtkGuiMenu*>(data);
  menu->activate(action);
}

void GtkGuiMenu::activate(GSimpleAction* action) {
  notify_item_activated(unescape(g_action_get_name(G_ACTION(action))));
}

std::string GtkGuiMenu::add_action(std::string const& id) {
  if (parent_) return parent_->add_action(id);
  std::string name = escape(id);
  auto action = g_simple_action_new(name.c_str(), nullptr);
  action_.emplace(id, action);
  g_signal_connect(action, "activate", G_CALLBACK(menu_item_activate), this);
  if (map_) g_action_map_add_action(map_, G_ACTION(action));
  return "app." + name;
}

bool GtkGuiMenu::enable_item(std::string const& id, bool enable) {
  if (parent_) return parent_->enable_item(id, enable);
  auto pair = action_.find(id);
  if (pair == action_.end()) return false;
  g_simple_action_set_enabled(pair->second.get(), enable);
  return true;
}

}  // namespace

// static
GuiMain* GuiMain::create(std::string const& title, uint32_t width,
                         uint32_t height) {
  return new GtkGuiMain(title, width, height);
}

// static
GuiMenu* GuiMenu::create() {
  return new GtkGuiMenu();
}

// static
GuiAbout* GuiAbout::create(std::string const& title,
                           std::string const& version,
                           char const* author_name,
                           char const* author_email,
                           ...) {
  std::vector<std::string> authors;
  authors.push_back(author_name);
  authors.push_back(author_email);
  va_list args;
  va_start(args, author_email);
  while (true) {
    auto name = va_arg(args, char const*);
    if (!name) break;
    auto email = va_arg(args, char const*);
    authors.push_back(name);
    authors.push_back(email);
  }
  va_end(args);
  return new GtkGuiAbout(title, version, authors);
}