notebooklabelglom.cc 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Glom
 *
 * Copyright (C) 2001-2004 Murray Cumming
 *
 * 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 2 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, write to the
17 18
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
19 20 21
 */

#include "notebooklabelglom.h"
22
#include <glom/appwindow.h>
23 24
#include <glibmm/i18n.h>

David King's avatar
David King committed
25 26
#include <iostream>

27 28 29
namespace Glom
{

Murray Cumming's avatar
Murray Cumming committed
30
NotebookLabel::NotebookLabel(NotebookGlom* notebook)
31
: m_notebook(notebook),
32
  m_popup_menu(nullptr)
33 34 35 36
{
  init();
}

37
NotebookLabel::NotebookLabel(const Glib::ustring& label, NotebookGlom* notebook)
38 39
: m_label(label),
  m_notebook (notebook),
40
  m_popup_menu(nullptr)
41 42 43 44
{
  init();
}

45
void NotebookLabel::init()
46 47 48 49 50
{
  add(m_label);
  m_label.show();
  set_events (Gdk::ALL_EVENTS_MASK);
  set_visible_window (false);
51
  setup_menu(this);
52 53
}

54
void NotebookLabel::set_label (const Glib::ustring& title)
55
{
Murray Cumming's avatar
Murray Cumming committed
56
  m_label.set_label (title);
57 58
}

59
AppWindow* NotebookLabel::get_appwindow()
60
{
Murray Cumming's avatar
Murray Cumming committed
61
  auto pWindow = get_toplevel();
62 63
  //TODO: This only works when the child widget is already in its parent.

64
  return dynamic_cast<AppWindow*>(pWindow);
65 66
}

67
void NotebookLabel::on_menu_new_group_activate()
68
{
69
  auto group = std::make_shared<LayoutGroup>();
70
  group->set_title_original(_("New Group"));
71
  group->set_name(_("Group"));
Murray Cumming's avatar
Murray Cumming committed
72

73
  auto notebook_group = std::dynamic_pointer_cast<LayoutGroup>(m_notebook->get_layout_item());
74
  notebook_group->add_item(group);
Murray Cumming's avatar
Murray Cumming committed
75

76 77 78
  m_notebook->signal_layout_changed().emit();
}

79
void NotebookLabel::on_menu_delete_activate()
80 81
{
  Glib::ustring message;
Murray Cumming's avatar
Murray Cumming committed
82
  const auto notebook_title = item_get_title(m_notebook->get_layout_item());
83
  if(!notebook_title.empty())
84 85
  {
    message = Glib::ustring::compose (_("Delete whole notebook \"%1\"?"),
86
                                      notebook_title);
87 88 89 90 91
  }
  else
  {
    message = _("Delete whole notebook?");
  }
92

93 94
  Gtk::MessageDialog dlg (message, false, Gtk::MESSAGE_QUESTION,
                          Gtk::BUTTONS_YES_NO, true);
95
  switch(dlg.run())
96 97 98 99 100 101 102 103 104
  {
    case Gtk::RESPONSE_YES:
      m_notebook->delete_from_layout();
      break;
    default:
      return;
  }
}

105
void NotebookLabel::setup_menu(Gtk::Widget* /* widget */)
106
{
107
  m_action_group = Gio::SimpleActionGroup::create();
108

109
  m_new_group = m_action_group->add_action("new-group",
110
    sigc::mem_fun(*this, &NotebookLabel::on_menu_new_group_activate) );
111
  m_deelete = m_action_group->add_action("delete",
112
    sigc::mem_fun(*this, &NotebookLabel::on_menu_delete_activate) );
Murray Cumming's avatar
Murray Cumming committed
113

114
  insert_action_group("context", m_action_group);
115

116
  auto menu = Gio::Menu::create();
117 118
  menu->append(_("New Group"), "context.new-group");
  menu->append(_("_Delete"), "context.delete");
119

120 121
  m_popup_menu = std::make_unique<Gtk::Menu>(menu);
  m_popup_menu->attach_to_widget(*this);
122 123
}

124
bool NotebookLabel::on_button_press_event(GdkEventButton *button_event)
125
{
Murray Cumming's avatar
Murray Cumming committed
126
  auto pApp = get_appwindow();
127
  if(pApp && pApp->get_userlevel() == AppState::userlevels::DEVELOPER)
128 129
  {
    GdkModifierType mods;
Murray Cumming's avatar
Murray Cumming committed
130
    gdk_window_get_device_position( gtk_widget_get_window (Gtk::Widget::gobj()), button_event->device, nullptr, nullptr, &mods );
131 132 133
    if(mods & GDK_BUTTON3_MASK)
    {
      //Give user choices of actions on this item:
134
      m_popup_menu->popup_at_pointer((GdkEvent*)button_event);
135 136 137
      return true; //We handled this event.
    }
  }
138 139

  return Gtk::EventBox::on_button_press_event(button_event);
140 141 142
}

} //namespace Glom