imageglom.cc 15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "imageglom.h"
#include <glibmm/i18n.h>
23
#include <glom/application.h>
24
#include <glom/utils_ui.h>
25
#include <glom/glade_utils.h>
26
#include <libglom/data_structure/glomconversions.h>
27
28
29
30
//#include <sstream> //For stringstream

#include <iostream>   // for cout, endl

31
32
33
namespace Glom
{

34
ImageGlom::ImageGlom()
35
36
: m_image(Gtk::Stock::MISSING_IMAGE, Gtk::ICON_SIZE_DIALOG), //The widget is invisible if we don't specify an image.
  m_pMenuPopup_UserMode(0)
37
{
38
39
40
  init();
}

41
ImageGlom::ImageGlom(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& /* builder */)
42
43
44
: Gtk::EventBox(cobject),
  m_image(Gtk::Stock::MISSING_IMAGE, Gtk::ICON_SIZE_DIALOG), //The widget is invisible if we don't specify an image.
  m_pMenuPopup_UserMode(0)
45
46
47
48
49
50
51
52
{
  init();
}

void ImageGlom::init()
{
  m_read_only = false;

53
#ifndef GLOM_ENABLE_CLIENT_ONLY
54
  setup_menu();
55
56
#endif // !GLOM_ENABLE_CLIENT_ONLY

57
  setup_menu_usermode();
58

59
  m_image.set_size_request(150, 150);
60
  m_image.show();
61

62
63
64
  m_frame.set_shadow_type(Gtk::SHADOW_ETCHED_IN); //Without this, the image widget has no borders and is completely invisible when empty.
  m_frame.add(m_image);
  m_frame.show();
65

66
  add(m_frame);
67
68
}

69
70


71
72
73
74
ImageGlom::~ImageGlom()
{
}

75
76
77
void ImageGlom::set_layout_item(const sharedptr<LayoutItem>& layout_item, const Glib::ustring& table_name)
{
  LayoutWidgetField::set_layout_item(layout_item, table_name);
78
#ifdef GTKMM_ATKMM_ENABLED
79
  get_accessible()->set_name(layout_item->get_name());
80
#endif  
81
}
82
83
84
85

bool ImageGlom::on_button_press_event(GdkEventButton *event)
{
  GdkModifierType mods;
86
  gdk_window_get_pointer( gtk_widget_get_window (Gtk::Widget::gobj()), 0, 0, &mods );
87

88
89
  //Enable/Disable items.
  //We did this earlier, but get_application is more likely to work now:
90
  Application* pApp = get_application();
91
92
  if(pApp)
  {
93
#ifndef GLOM_ENABLE_CLIENT_ONLY
94
95
96
97
98
99
    pApp->add_developer_action(m_refContextLayout); //So that it can be disabled when not in developer mode.
    pApp->add_developer_action(m_refContextAddField);
    pApp->add_developer_action(m_refContextAddRelatedRecords);
    pApp->add_developer_action(m_refContextAddGroup);

    pApp->update_userlevel_ui(); //Update our action's sensitivity.
100
#endif // !GLOM_ENABLE_CLIENT_ONLY
101
102
103

    //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.
104
#ifndef GLOM_ENABLE_CLIENT_ONLY
105
106
107
108
109
110
111
112
113
    if(pApp->get_userlevel() == AppState::USERLEVEL_DEVELOPER)
    {
      if(mods & GDK_BUTTON3_MASK)
      {
        //Give user choices of actions on this item:
        m_pMenuPopup->popup(event->button, event->time);
        return true; //We handled this event.
      }
    }
114
    else
115
#endif // !GLOM_ENABLE_CLIENT_ONLY
116
    {
117
      // We cannot be in developer mode in client only mode.
118
119
120
121
122
123
124
      if(mods & GDK_BUTTON3_MASK)
      {
        //Give user choices of actions on this item:
        m_pMenuPopup_UserMode->popup(event->button, event->time);
        return true; //We handled this event.
      }
    }
125

126
127
    //Single-click to select file:
    if(mods & GDK_BUTTON1_MASK)
128
    {
129
130
131
      on_menupopup_activate_select_file();
      return true; //We handled this event.

132
133
134
135
136
137
    }
  }

  return Gtk::EventBox::on_button_press_event(event);
}

138
Application* ImageGlom::get_application()
139
140
141
142
{
  Gtk::Container* pWindow = get_toplevel();
  //TODO: This only works when the child widget is already in its parent.

143
  return dynamic_cast<Application*>(pWindow);
144
145
}

146
147
148
149
bool ImageGlom::get_has_original_data() const
{
  return true; //TODO.
}
150

151
152
153
154
155
156
157
158
void ImageGlom::set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
{
  m_pixbuf_original = pixbuf;
  m_image.set(m_pixbuf_original);

  scale();
}

159
void ImageGlom::set_value(const Gnome::Gda::Value& value)
160
{
Armin Burgmeier's avatar
Armin Burgmeier committed
161
162
163
  // Remember original data 
  m_original_data = Gnome::Gda::Value();
  m_original_data = value;
164
  Glib::RefPtr<Gdk::Pixbuf> pixbuf = Utils::get_pixbuf_for_gda_value(value);
Armin Burgmeier's avatar
Armin Burgmeier committed
165

166
  if(pixbuf)
167
  {
168
169
    set_pixbuf(pixbuf);
    scale();
170
  }
171
  else
172
  {
173
174
175
176
177
178
179
180
    /*
    std::cout << "Debug: Setting MISSING_IMAGE" << std::endl;
    
    //Check that this stock icon size is really available,
    //though it would be a distro error if it is not.
    Glib::RefPtr<Gtk::Style> style = get_style();
    if(style)
    {
181
      /std::cout << "Debug: Setting MISSING_IMAGE 3" << std::endl;
182
183
184
185
186
187
188
189
190
191
192
193

      const Gtk::IconSet iconset = style->lookup_icon_set(Gtk::Stock::MISSING_IMAGE);

      std::cout << "Debug: Setting MISSING_IMAGE 4" << std::endl;

      typedef std::vector<Gtk::IconSize> type_vecSizes;
      type_vecSizes sizes = iconset.get_sizes();
      type_vecSizes::iterator iterFind = std::find(sizes.begin(), sizes.end(), Gtk::ICON_SIZE_DIALOG);
      if(iterFind != sizes.end())
      {
     */
        m_image.set(Gtk::Stock::MISSING_IMAGE, Gtk::ICON_SIZE_DIALOG);
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
      }
      else
      {
        std::cerr << "Glom: The current theme does not seem to havae the Gtk::Stock::MISSING_IMAGE icon in size Gtk::ICON_SIZE_DIALOG" << std::endl;

        if(!sizes.empty() && (sizes[0] > 0))
        {
          std::cerr << "  Using alternative stock icon size." << std::endl;
          m_image.set(Gtk::Stock::MISSING_IMAGE, sizes[0]);
        }
        else
        {
          std::cerr << "  No alternative stock icon size available either, for this stock icon." << std::endl;
          m_image.set("");
        }
      }
    }
    else
    {
      std::cerr << "Glom: No Gtk::Style available for this widget (yet), so not setting MISSING_IMAGE icon." << std::endl;
      m_image.set("");
    }
    */

    m_pixbuf_original = Glib::RefPtr<Gdk::Pixbuf>();
220
  }
221
222
223
224
}

Gnome::Gda::Value ImageGlom::get_value() const
{
225
226
  //TODO: Return the data from the file that was just chosen.
  //Don't store the original here any longer than necessary,
Armin Burgmeier's avatar
Armin Burgmeier committed
227
228
  if(m_original_data.get_value_type() != G_TYPE_NONE)
    return m_original_data;
229
  
230
  if(m_pixbuf_original)
231
232
233
234
235
  {
    try
    {
      gchar* buffer = 0;
      gsize buffer_size = 0;
236
237
238
239
      std::list<Glib::ustring> list_keys;
      std::list<Glib::ustring> list_values;
      //list_keys.push_back("quality"); //For jpeg only.
      //list_values.push_back("95");
240

241
      m_pixbuf_original->save_to_buffer(buffer, buffer_size, GLOM_IMAGE_FORMAT, list_keys, list_values); //Always store images as the standard format in the database.
242

243
244
245
      //g_warning("ImageGlom::get_value(): debug: to db: ");
      //for(int i = 0; i < 10; ++i)
      //  g_warning("%02X (%c), ", (guint8)buffer[i], buffer[i]);
246

Armin Burgmeier's avatar
Armin Burgmeier committed
247
248
249
250
251
252
253
      GdaBinary* bin = g_new(GdaBinary, 1);
      bin->data = reinterpret_cast<guchar*>(buffer);
      bin->binary_length = buffer_size;

      m_original_data = Gnome::Gda::Value();
      m_original_data.Glib::ValueBase::init(GDA_TYPE_BINARY);
      gda_value_take_binary(m_original_data.gobj(), bin);
254

255
      buffer = 0;
Armin Burgmeier's avatar
Armin Burgmeier committed
256
      return m_original_data;
257
    }
258
    catch(const Glib::Exception& ex)
259
    {
260
      std::cerr << G_STRFUNC << ": " << ex.what() << std::endl;
261
262
    }
  }
263

Armin Burgmeier's avatar
Armin Burgmeier committed
264
  return Gnome::Gda::Value();
265
}
266

267
268
269
bool ImageGlom::on_expose_event(GdkEventExpose* event)
{
  const bool result = Gtk::EventBox::on_expose_event(event);
270

271
272
273
274
  scale();
  return result;
}

275
276
void ImageGlom::scale()
{
277
  Glib::RefPtr<Gdk::Pixbuf> pixbuf = m_pixbuf_original;
278

279
  if(pixbuf)
280
  {
281
282
283
    const Gtk::Allocation allocation = m_image.get_allocation();
    const int pixbuf_height = pixbuf->get_height();
    const int pixbuf_width = pixbuf->get_width();
284

285
286
287
    if( (pixbuf_height > allocation.get_height()) ||
        (pixbuf_width > allocation.get_width()) )
    {
288
289
      if(allocation.get_height() > 10 || allocation.get_width() > 10)
      {
290
        Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = Utils::image_scale_keeping_ratio(pixbuf, allocation.get_height(), allocation.get_width());
291
292
        if(!pixbuf_scaled)
        {
293
          std::cerr << "Utils::image_scale_keeping_ratio() returned NULL pixbuf." << std::endl;
294
295
296
297
        }
        else 
        {
          //Don't set a new pixbuf if the dimenstions have not changed:
298
299
300
301
302
          Glib::RefPtr<const Gdk::Pixbuf> pixbuf_in_image;

          if(m_image.get_storage_type() == Gtk::IMAGE_PIXBUF) //Prevent warning.
            pixbuf_in_image = m_image.get_pixbuf();

303
304
305
306
          if( !pixbuf_in_image || (pixbuf_in_image->get_height() != pixbuf_scaled->get_height()) || (pixbuf_in_image->get_width() != pixbuf_scaled->get_width()) )
            m_image.set(pixbuf_scaled);
        }
      }
307
    }
308
  }
309
310
  //else
  //  g_warning("ImageGlom::scale(): attempt to scale a null pixbuf.");
311
312
}

313
314
void ImageGlom::on_menupopup_activate_select_file()
{
315
316
317
  if(m_read_only)
    return;

318
  //TODO: Use Hildon::FileChooser for Maemo.
Murray Cumming's avatar
Murray Cumming committed
319
  Gtk::FileChooserDialog dialog(_("Choose Image"), Gtk::FILE_CHOOSER_ACTION_OPEN);
320

321
322
  //Get image formats only:
  Gtk::FileFilter filter;
323
  filter.set_name(_("Images"));
324
325
  filter.add_pixbuf_formats();
  dialog.add_filter(filter);
326

327
328
329
330
  dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
  dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
  int response = dialog.run();
  dialog.hide();
331

332
  if((response != Gtk::RESPONSE_CANCEL) && (response != Gtk::RESPONSE_DELETE_EVENT))
333
  {
334
335
    const Glib::ustring uri = dialog.get_uri();
    if(!uri.empty())
336
    {
337
      Dialog_Image_Progress* dialog;
338
      Utils::get_glade_widget_derived_with_warning(dialog);
339
      if(dialog)
340
      {
341
342
        // Automatically delete the dialog when we no longer need it:
        std::auto_ptr<Gtk::Dialog> dialog_keeper(dialog);
Armin Burgmeier's avatar
Armin Burgmeier committed
343

344
        Application* pApp = get_application();
345
346
347
        if(pApp)
          dialog->set_transient_for(*pApp);

348
        dialog->load(uri);
Armin Burgmeier's avatar
Armin Burgmeier committed
349

350
        if(dialog->run() == Gtk::RESPONSE_ACCEPT)
Armin Burgmeier's avatar
Armin Burgmeier committed
351
        {
352
353
354
355
356
357
358
359
360
361
          GdaBinary* bin = g_new(GdaBinary, 1);
          std::auto_ptr<GdaBinary> image_data = dialog->get_image_data();
          bin->data = image_data->data;
          bin->binary_length = image_data->binary_length;

          m_original_data = Gnome::Gda::Value();
          m_original_data.Glib::ValueBase::init(GDA_TYPE_BINARY);
          gda_value_take_binary(m_original_data.gobj(), bin);

          m_pixbuf_original = dialog->get_pixbuf();
Armin Burgmeier's avatar
Armin Burgmeier committed
362
363
364
365
          m_image.set(m_pixbuf_original); //Load the image.
          scale();
          signal_edited().emit();
        }
366
367
      }
    }
368
  }
369
370
}

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
void ImageGlom::on_clipboard_get(Gtk::SelectionData& selection_data, guint /* info */)
{
  //info is meant to indicate the target, but it seems to be always 0,
  //so we use the selection_data's target instead.

  const std::string target = selection_data.get_target(); 

  if(target == GLOM_IMAGE_FORMAT_MIME_TYPE)
  {
    // This set() override uses an 8-bit text format for the data.
    selection_data.set_pixbuf(m_pixbuf_clipboard);
  }
  else
  {
    g_warning("ExampleWindow::on_clipboard_get(): Unexpected clipboard target format.");
386
  }
387
388
389
390
}

void ImageGlom::on_clipboard_clear()
{
391
392
393
  if(m_read_only)
    return;

394
  m_pixbuf_clipboard.reset();
395
396
}

397
398
void ImageGlom::on_menupopup_activate_copy()
{
399
400
401
402
403
404
  if(m_pixbuf_original)
  {
    //When copy is used, store it here until it is pasted.
    m_pixbuf_clipboard = m_pixbuf_original->copy(); //TODO: Get it from the DB, when we stop storing the original here instead of just the preview.
  }
  else
405
    m_pixbuf_clipboard.reset();
406

407
408
409
410
411
412
  Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();

  //Targets:
  std::list<Gtk::TargetEntry> listTargets;

  listTargets.push_back( Gtk::TargetEntry(GLOM_IMAGE_FORMAT_MIME_TYPE) );
413

414
415
  refClipboard->set( listTargets, sigc::mem_fun(*this, &ImageGlom::on_clipboard_get), sigc::mem_fun(*this, &ImageGlom::on_clipboard_clear) );
}
416

417
418
void ImageGlom::on_clipboard_received_image(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
{
419
420
421
  if(m_read_only)
    return;

422
423
  if(pixbuf)
  {
Armin Burgmeier's avatar
Armin Burgmeier committed
424
425
426
    // Clear original data of previous image
    m_original_data = Gnome::Gda::Value();

427
    m_pixbuf_original = pixbuf;
428

429
430
431
432
    m_image.set(m_pixbuf_original); //Load the image.
    scale();
    signal_edited().emit();
  }
433
434
}

435

436
437
void ImageGlom::on_menupopup_activate_paste()
{
438
439
440
  if(m_read_only)
    return;

441
442
  //Tell the clipboard to call our method when it is ready:
  Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
443

444
445
  if(refClipboard)
    refClipboard->request_image( sigc::mem_fun(*this, &ImageGlom::on_clipboard_received_image) );
446
447
}

Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
448
449
void ImageGlom::on_menupopup_activate_clear()
{
450
451
452
  if(m_read_only)
    return;

453
  m_pixbuf_original.reset();
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
454
  m_image.set(Gtk::Stock::MISSING_IMAGE, Gtk::ICON_SIZE_DIALOG);
455
  signal_edited().emit();
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
456
457
}

458
459
460
void ImageGlom::setup_menu_usermode()
{
  m_refActionGroup_UserModePopup = Gtk::ActionGroup::create();
461

462
463
464
465
  m_refActionGroup_UserModePopup->add(Gtk::Action::create("ContextMenu_UserMode", "Context Menu") );
  m_refActionSelectFile =  Gtk::Action::create("ContextSelectFile", Gtk::Stock::EDIT, _("Choose File"));
  m_refActionCopy = Gtk::Action::create("ContextCopy", Gtk::Stock::COPY);
  m_refActionPaste = Gtk::Action::create("ContextPaste", Gtk::Stock::PASTE);
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
466
  m_refActionClear = Gtk::Action::create("ContextClear", Gtk::Stock::CLEAR);
467

468
469
470
471
472
473
474
475
  m_refActionGroup_UserModePopup->add(m_refActionSelectFile,
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_select_file) );

  m_refActionGroup_UserModePopup->add(m_refActionCopy,
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_copy) );

  m_refActionGroup_UserModePopup->add(m_refActionPaste,
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_paste) );
476

Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
477
478
  m_refActionGroup_UserModePopup->add(m_refActionClear,
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_clear) );
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

  m_refUIManager_UserModePopup = Gtk::UIManager::create();

  m_refUIManager_UserModePopup->insert_action_group(m_refActionGroup_UserModePopup);

  //TODO: add_accel_group(m_refUIManager_UserModePopup->get_accel_group());

  try
  {
    Glib::ustring ui_info = 
        "<ui>"
        "  <popup name='ContextMenu_UserMode'>"
        "    <menuitem action='ContextSelectFile'/>"
        "    <menuitem action='ContextCopy'/>"
        "    <menuitem action='ContextPaste'/>"
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
494
        "    <menuitem action='ContextClear'/>"
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
        "  </popup>"
        "</ui>";

    m_refUIManager_UserModePopup->add_ui_from_string(ui_info);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "building menus failed: " <<  ex.what();
  }

  //Get the menu:
  m_pMenuPopup_UserMode = dynamic_cast<Gtk::Menu*>( m_refUIManager_UserModePopup->get_widget("/ContextMenu_UserMode") ); 
  if(!m_pMenuPopup_UserMode)
    g_warning("menu not found");
}
510
511
512
513
514
515
516
517
518
519
520

void ImageGlom::do_choose_image()
{
  on_menupopup_activate_select_file();
}

void ImageGlom::set_read_only(bool read_only)
{
  m_read_only = read_only;
}

521
522

} //namespace Glom