entry.cc 6.37 KB
Newer Older
Murray Cumming's avatar
Murray Cumming committed
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.
Murray Cumming's avatar
Murray Cumming committed
19 20
 */

21
#include "entry.h"
22
#include <libglom/data_structure/glomconversions.h>
23
#include <gtkmm/messagedialog.h>
24
#include <glom/dialog_invalid_data.h>
25
#include <glom/appwindow.h>
26
#include <glom/utils_ui.h>
27 28
#include <iostream>   // for cout, endl

29 30 31
namespace Glom
{

32 33 34 35
namespace DataWidgetChildren
{

Entry::Entry(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& /* builder */)
36 37
:
  Gtk::Entry(cobject),
38
  m_glom_type(Field::glom_field_type::TEXT)
39
{
40
  init();
41
}
42

43
Entry::Entry(Field::glom_field_type glom_type)
44
: m_glom_type(glom_type)
Murray Cumming's avatar
Murray Cumming committed
45
{
46
  init();
Murray Cumming's avatar
Murray Cumming committed
47 48
}

49
void Entry::init()
50
{
51 52 53
#ifndef GLOM_ENABLE_CLIENT_ONLY
  setup_menu(this);
#endif // !GLOM_ENABLE_CLIENT_ONLY
54 55
}

56
void Entry::set_layout_item(const std::shared_ptr<LayoutItem>& layout_item, const Glib::ustring& table_name)
57 58
{
  LayoutWidgetField::set_layout_item(layout_item, table_name);
59
#ifdef GTKMM_ATKMM_ENABLED
60
  get_accessible()->set_name(layout_item->get_name());
61 62 63
#endif

  //Horizontal Alignment:
Murray Cumming's avatar
Murray Cumming committed
64
  Formatting::HorizontalAlignment alignment =
65
    Formatting::HorizontalAlignment::LEFT;
66
  auto layout_field =
67
    std::dynamic_pointer_cast<LayoutItem_Field>(get_layout_item());
68
  if(layout_field)
69
    alignment = layout_field->get_formatting_used_horizontal_alignment(true /* for details view */);
70

71
  const float x_align = (alignment == Formatting::HorizontalAlignment::LEFT ? 0.0 : 1.0);
72
  set_alignment(x_align);
73 74
}

75
void Entry::set_glom_type(Field::glom_field_type glom_type)
76 77 78 79
{
  m_glom_type = glom_type;
}

80
void Entry::check_for_change()
Murray Cumming's avatar
Murray Cumming committed
81 82 83 84
{
  Glib::ustring new_text = get_text();
  if(new_text != m_old_text)
  {
85 86
    //Validate the input:
    bool success = false;
87

88
    auto layout_item = std::dynamic_pointer_cast<const LayoutItem_Field>(get_layout_item());
89
    Gnome::Gda::Value value = Conversions::parse_value(m_glom_type, get_text(), layout_item->get_formatting_used().m_numeric_format, success);
90

91
    if(success)
92
    {
93 94
      //Actually show the canonical text:
      set_value(value);
95 96 97 98
      m_signal_edited.emit(); //The text was edited, so tell the client code.
    }
    else
    {
99
      //Tell the user and offer to revert or try again:
100
      bool revert = glom_show_dialog_invalid_data(m_glom_type);
101 102 103 104 105 106
      if(revert)
      {
        set_text(m_old_text);
      }
      else
        grab_focus(); //Force the user back into the same field, so that the field can be checked again and eventually corrected or reverted.
107
    }
Murray Cumming's avatar
Murray Cumming committed
108 109 110
  }
}

111
bool Entry::on_focus_out_event(GdkEventFocus* focus_event)
Murray Cumming's avatar
Murray Cumming committed
112
{
113
  const auto result = Gtk::Entry::on_focus_out_event(focus_event);
114

Murray Cumming's avatar
Murray Cumming committed
115 116
  //The user has finished editing.
  check_for_change();
117

Murray Cumming's avatar
Murray Cumming committed
118
  //Call base class:
119
  return result;
Murray Cumming's avatar
Murray Cumming committed
120 121
}

122
void Entry::on_activate()
123
{
Murray Cumming's avatar
Murray Cumming committed
124 125 126 127 128 129 130
  //Call base class:
  Gtk::Entry::on_activate();

  //The user has finished editing.
  check_for_change();
}

131
void Entry::on_changed()
Murray Cumming's avatar
Murray Cumming committed
132 133
{
  //The text is being edited, but the user has not finished yet.
134

Murray Cumming's avatar
Murray Cumming committed
135
  //Call base class:
136
  Gtk::Entry::on_changed();
Murray Cumming's avatar
Murray Cumming committed
137 138
}

139
void Entry::set_value(const Gnome::Gda::Value& value)
140
{
141
  auto layout_item = std::dynamic_pointer_cast<LayoutItem_Field>(get_layout_item());
142 143
  if(!layout_item)
    return;
144

Murray Cumming's avatar
Murray Cumming committed
145
  const auto text = Conversions::get_text_for_gda_value(m_glom_type, value, layout_item->get_formatting_used().m_numeric_format);
146
  set_text(text);
147

148
  //Show a different color if the value is numeric, if that's specified:
149
  if(layout_item->get_glom_type() == Field::glom_field_type::NUMERIC)
150
  {
Murray Cumming's avatar
Murray Cumming committed
151
    const Glib::ustring fg_color =
Murray Cumming's avatar
Murray Cumming committed
152
      layout_item->get_formatting_used().get_text_format_color_foreground_to_use(value);
153
    if(!fg_color.empty())
154 155 156
    {
      UiUtils::load_color_into_css_provider(*this, fg_color);
    }
157
    else
158 159 160
    {
      //TOOD: unset_color();
    }
161
  }
162 163
}

164
void Entry::set_text(const Glib::ustring& text)
Murray Cumming's avatar
Murray Cumming committed
165 166 167 168 169 170 171
{
  m_old_text = text;

  //Call base class:
  Gtk::Entry::set_text(text);
}

172
Gnome::Gda::Value Entry::get_value() const
173
{
174
  bool success = false;
175

176
  auto layout_item = std::dynamic_pointer_cast<const LayoutItem_Field>(get_layout_item());
177
  return Conversions::parse_value(m_glom_type, get_text(), layout_item->get_formatting_used().m_numeric_format, success);
178 179
}

180
#ifndef GLOM_ENABLE_CLIENT_ONLY
181
bool Entry::on_button_press_event(GdkEventButton *button_event)
182 183
{
  //Enable/Disable items.
184
  //We did this earlier, but get_appwindow is more likely to work now:
Murray Cumming's avatar
Murray Cumming committed
185
  auto pApp = get_appwindow();
186 187
  if(pApp)
  {
188 189 190 191
    pApp->add_developer_action(m_context_layout); //So that it can be disabled when not in developer mode.
    pApp->add_developer_action(m_context_add_field);
    pApp->add_developer_action(m_context_add_related_records);
    pApp->add_developer_action(m_context_add_group);
192

Murray Cumming's avatar
Murray Cumming committed
193
    pApp->update_userlevel_ui(); //Update our action's sensitivity.
194 195 196

    //Only show this popup in developer mode, so operators still see the default GtkEntry context menu.
    //TODO: It would be better to add it somehow to the standard context menu.
197
    if(pApp->get_userlevel() == AppState::userlevels::DEVELOPER)
198 199
    {
      GdkModifierType mods;
Murray Cumming's avatar
Murray Cumming committed
200
      gdk_window_get_device_position( gtk_widget_get_window (Gtk::Widget::gobj()), button_event->device, nullptr, nullptr, &mods );
201 202 203
      if(mods & GDK_BUTTON3_MASK)
      {
        //Give user choices of actions on this item:
204
        m_menu_popup->popup_at_pointer((GdkEvent*)button_event);
205 206 207 208 209 210
        return true; //We handled this event.
      }
    }

  }

211
  return Gtk::Entry::on_button_press_event(button_event);
212
}
213
#endif // !GLOM_ENABLE_CLIENT_ONLY
214

215
AppWindow* Entry::get_appwindow() const
216
{
Murray Cumming's avatar
Murray Cumming committed
217
  auto pWindow = const_cast<Gtk::Container*>(get_toplevel());
218 219
  //TODO: This only works when the child widget is already in its parent.

220
  return dynamic_cast<AppWindow*>(pWindow);
221
}
222

223
void Entry::set_read_only(bool read_only)
224 225 226
{
  set_editable(!read_only);
}
227

228
} //namespace DataWidetChildren
229
} //namespace Glom