imageglom.cc 26.4 KB
Newer Older
1
2
/* Glom
 *
3
 * Copyright (C) 2001-2011 Murray Cumming
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * 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
22
 */

#include "imageglom.h"
#include <glibmm/i18n.h>
23
#include <glom/appwindow.h>
24
#include <glom/utils_ui.h>
25
#include <glom/glade_utils.h>
26
#include <libglom/data_structure/glomconversions.h>
27
28
#include <glom/utility_widgets/dialog_image_load_progress.h>
#include <glom/utility_widgets/dialog_image_save_progress.h>
29
30
#include <gtkmm/appchooserdialog.h>
#include <gtkmm/filechooserdialog.h>
31
32
#include <giomm/file.h>
#include <giomm/contenttype.h>
33
#include <giomm/menu.h>
34
#include <libgda/gda-blob-op.h>
35
#include <glibmm/convert.h>
36

37
38
39
40
#ifdef G_OS_WIN32
#include <windows.h>
#endif

41
42
#include <iostream>   // for cout, endl

43
44
45
namespace Glom
{

46
ImageGlom::type_vec_ustrings ImageGlom::m_evince_supported_mime_types;
47
ImageGlom::type_vec_ustrings ImageGlom::m_gdkpixbuf_supported_mime_types;
48

49
ImageGlom::ImageGlom()
Murray Cumming's avatar
Murray Cumming committed
50
51
52
: m_ev_view(nullptr),
  m_ev_document_model(nullptr),
  m_pMenuPopup_UserMode(nullptr)
53
{
54
55
56
  init();
}

57
ImageGlom::ImageGlom(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& /* builder */)
58
: Gtk::EventBox(cobject),
Murray Cumming's avatar
Murray Cumming committed
59
60
61
  m_ev_view(nullptr),
  m_ev_document_model(nullptr),
  m_pMenuPopup_UserMode(nullptr)
62
63
64
65
66
67
{
  init();
}

void ImageGlom::init()
{
68
  m_ev_view = EV_VIEW(ev_view_new());
69
70
71
72
73
74
75
76
  //gtk_widget_add_events(GTK_WIDGET(m_ev_view), GDK_BUTTON_PRESS_MASK);

  //Connect the the EvView's button-press-event signal, 
  //because we don't get it otherwise.
  //For some reason this is not necessary with the GtkImage.
  Gtk::Widget* cppEvView = Glib::wrap(GTK_WIDGET(m_ev_view));
  cppEvView->signal_button_press_event().connect(
    sigc::mem_fun(*this, &ImageGlom::on_button_press_event), false);
77

78
79
  m_read_only = false;

80
#ifndef GLOM_ENABLE_CLIENT_ONLY
81
  setup_menu(this);
82
83
#endif // !GLOM_ENABLE_CLIENT_ONLY

84
  setup_menu_usermode();
85

86
  //m_image.set_size_request(150, 150);
87

88

89
90
  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.show();
91

92
  add(m_frame);
93
94
}

95
96


97
98
ImageGlom::~ImageGlom()
{
99
  delete m_pMenuPopup_UserMode;
100
101
}

102
void ImageGlom::set_layout_item(const std::shared_ptr<LayoutItem>& layout_item, const Glib::ustring& table_name)
103
104
{
  LayoutWidgetField::set_layout_item(layout_item, table_name);
105
#ifdef GTKMM_ATKMM_ENABLED
106
  get_accessible()->set_name(layout_item->get_name());
107
#endif  
108
}
109

110
bool ImageGlom::on_button_press_event(GdkEventButton *button_event)
111
112
{
  GdkModifierType mods;
113
  gdk_window_get_device_position( gtk_widget_get_window (Gtk::Widget::gobj()), button_event->device, 0, 0, &mods );
114

115
  //Enable/Disable items.
116
117
  //We did this earlier, but get_appwindow is more likely to work now:
  AppWindow* pApp = get_appwindow();
118
119
  if(pApp)
  {
120
#ifndef GLOM_ENABLE_CLIENT_ONLY
121
122
123
124
125
126
    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.
127
#endif // !GLOM_ENABLE_CLIENT_ONLY
128
129
130

    //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.
131
#ifndef GLOM_ENABLE_CLIENT_ONLY
132
    if(pApp->get_userlevel() == AppState::userlevels::DEVELOPER)
133
134
135
136
    {
      if(mods & GDK_BUTTON3_MASK)
      {
        //Give user choices of actions on this item:
137
        popup_menu(button_event->button, button_event->time);
138
       
139
140
141
        return true; //We handled this event.
      }
    }
142
    else
143
#endif // !GLOM_ENABLE_CLIENT_ONLY
144
    {
145
      // We cannot be in developer mode in client only mode.
146
147
148
      if(mods & GDK_BUTTON3_MASK)
      {
        //Give user choices of actions on this item:
149
        popup_menu(button_event->button, button_event->time);
150

151
152
153
        return true; //We handled this event.
      }
    }
154

155
156
    //Single-click to select file:
    if(mods & GDK_BUTTON1_MASK)
157
    {
158
      on_menupopup_activate_select_file();
159
160
      return true; //We handled this event.

161
162
163
    }
  }

164
  return Gtk::EventBox::on_button_press_event(button_event);
165
166
}

167
AppWindow* ImageGlom::get_appwindow() const
168
{
169
  Gtk::Container* pWindow = const_cast<Gtk::Container*>(get_toplevel());
170
171
  //TODO: This only works when the child widget is already in its parent.

172
  return dynamic_cast<AppWindow*>(pWindow);
173
174
}

175
176
177
178
bool ImageGlom::get_has_original_data() const
{
  return true; //TODO.
}
179

180
/*
181
182
183
void ImageGlom::set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
{
  m_pixbuf_original = pixbuf;
184
  show_image_data();
185
}
186
*/
187

188
void ImageGlom::set_value(const Gnome::Gda::Value& value)
189
{
Armin Burgmeier's avatar
Armin Burgmeier committed
190
191
192
  // Remember original data 
  m_original_data = Gnome::Gda::Value();
  m_original_data = value;
193
  show_image_data();
194
195
196
197
}

Gnome::Gda::Value ImageGlom::get_value() const
{
198
  return m_original_data;
199
}
200

201
void ImageGlom::on_size_allocate(Gtk::Allocation& allocation)
202
{
203
  Gtk::EventBox::on_size_allocate(allocation);
204

205
  //Resize the GtkImage if necessary:
Murray Cumming's avatar
Murray Cumming committed
206
  if(m_pixbuf_original)
207
  {
Murray Cumming's avatar
Murray Cumming committed
208
    const auto pixbuf_scaled = get_scaled_image();
209
210
    m_image.set(pixbuf_scaled);
  }
211
212
}

213
static void image_glom_ev_job_finished(EvJob* job, void* user_data)
214
{
215
216
217
218
219
220
221
222
223
224
  g_assert(job);
  
  ImageGlom* self = (ImageGlom*)user_data;
  g_assert(self);
  
  self->on_ev_job_finished(job);
}
  
void ImageGlom::on_ev_job_finished(EvJob* job)
{
225
  if(ev_job_is_failed (job)) {
Murray Cumming's avatar
Murray Cumming committed
226
227
228
229
230
231
232
233
234
235
236
237
    g_warning ("%s", job->error->message);
    g_object_unref (job);

    return;
  }

  ev_document_model_set_document(m_ev_document_model, job->document);
  ev_document_model_set_page(m_ev_document_model, 1);
  g_object_unref (job);
  
  //TODO: Show that we are no longer loading.
  //ev_view_set_loading(m_ev_view, FALSE);
238
}
239

240
241
const GdaBinary* ImageGlom::get_binary() const
{
Murray Cumming's avatar
Murray Cumming committed
242
  const GdaBinary* gda_binary = nullptr;
243
244
  if(m_original_data.get_value_type() == GDA_TYPE_BINARY)
    gda_binary = gda_value_get_binary(m_original_data.gobj());
245
246
  else if(m_original_data.get_value_type() == GDA_TYPE_BLOB)
  {
Murray Cumming's avatar
Murray Cumming committed
247
    const auto gda_blob = gda_value_get_blob(m_original_data.gobj());
248
249
250
    if(gda_blob && gda_blob_op_read_all(const_cast<GdaBlobOp*>(gda_blob->op), const_cast<GdaBlob*>(gda_blob)))
      gda_binary = &(gda_blob->data);
  }
251
252
253
254
  
  return gda_binary;
}

255
256
Glib::ustring ImageGlom::get_mime_type() const
{
Murray Cumming's avatar
Murray Cumming committed
257
  const auto gda_binary = get_binary();
258

259
260
261
262
263
264
265
  if(!gda_binary)
    return Glib::ustring();
    
  if(!gda_binary->data)
    return Glib::ustring();

  bool uncertain = false;
Murray Cumming's avatar
Murray Cumming committed
266
  const auto result = Gio::content_type_guess(std::string(),
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    gda_binary->data, gda_binary->binary_length,
    uncertain);

  //std::cout << G_STRFUNC << ": mime_type=" << result << ", uncertain=" << uncertain << std::endl;
  return result;  
}

void ImageGlom::fill_evince_supported_mime_types()
{
  //Fill the static list if it has not already been filled:
  if(!m_evince_supported_mime_types.empty())
    return;

  //Discover what mime types libevview can support.
  //Older versions supported image types too, via GdkPixbuf,
  //but that support was then removed.  
  GList* types_list = ev_backends_manager_get_all_types_info();
  if(!types_list)
  {
    return;
  }
  
  for(GList* l = types_list; l; l = g_list_next(l))
  {
Murray Cumming's avatar
Murray Cumming committed
291
292
293
294
    EvTypeInfo *info = (EvTypeInfo *)l->data;
    if(!info)
      continue;

Murray Cumming's avatar
Murray Cumming committed
295
    const char* mime_type = nullptr;
Murray Cumming's avatar
Murray Cumming committed
296
297
298
299
300
301
302
303
    int i = 0;
    while((mime_type = info->mime_types[i++]))
    {
      if(mime_type)
        m_evince_supported_mime_types.push_back(mime_type);
      //std::cout << "evince supported mime_type=" << mime_type << std::endl; 
    }
  }  
304
305
}

306
307
308
309
310
void ImageGlom::fill_gdkpixbuf_supported_mime_types()
{
  //Fill the static list if it has not already been filled:
  if(!m_gdkpixbuf_supported_mime_types.empty())
    return;
Murray Cumming's avatar
Murray Cumming committed
311

312
  for(const auto& format : Gdk::Pixbuf::get_formats())
313
  {
Murray Cumming's avatar
Murray Cumming committed
314
    const auto mime_types = format.get_mime_types();
315
316
317
318
319
320
    m_gdkpixbuf_supported_mime_types.insert(
      m_gdkpixbuf_supported_mime_types.end(),
      mime_types.begin(), mime_types.end());
  }
}

321
322
323
324
void ImageGlom::show_image_data()
{
  bool use_evince = false;
  
Murray Cumming's avatar
Murray Cumming committed
325
  const auto mime_type = get_mime_type();
326
327

  //std::cout << "mime_type=" << mime_type << std::endl; 
328
  
329
  fill_evince_supported_mime_types();
Murray Cumming's avatar
Murray Cumming committed
330
  const auto iterFind = 
331
332
333
334
    std::find(m_evince_supported_mime_types.begin(),
      m_evince_supported_mime_types.end(),
      mime_type);
  if(iterFind != m_evince_supported_mime_types.end())
335
  {
336
337
    use_evince = true;
  }  
338
  
339
340
  m_frame.remove();
    
341
342
  //Clear all possible display widgets:
  m_pixbuf_original.reset();
343
  m_image.set(Glib::RefPtr<Gdk::Pixbuf>()); //TODO: Add an unset() to gtkmm.
344
345
346
347
  
  if(m_ev_document_model)
  {
    g_object_unref(m_ev_document_model);
Murray Cumming's avatar
Murray Cumming committed
348
    m_ev_document_model = nullptr;
349
350
351
352
353
354
  }

  if(use_evince)
  {
    //Use EvView:
    m_image.hide();
355
    
356
357
358
    gtk_widget_show(GTK_WIDGET(m_ev_view));
    gtk_container_add(GTK_CONTAINER(m_frame.gobj()), GTK_WIDGET(m_ev_view));

359
360
361
    // Try loading from data in memory:
    // TODO: Uncomment this if this API is added: https://bugzilla.gnome.org/show_bug.cgi?id=654832
    /*
Murray Cumming's avatar
Murray Cumming committed
362
    const auto gda_binary = get_binary();
363
364
365
366
367
368
369
370
371
372
373
    if(!gda_binary || !gda_binary->data || !gda_binary->binary_length)
    {
       std::cerr << G_STRFUNC << "Data was null or empty." << std::endl;
      return;
    }
    
    EvJob *job = ev_job_load_new_with_data(
      (char*)gda_binary->data, gda_binary->binary_length);
    */
    //TODO: Test failure asynchronously.
    
Murray Cumming's avatar
Murray Cumming committed
374
    const auto uri = save_to_temp_file(false /* don't show progress */);
375
376
377
378
379
380
381
382
383
    if(uri.empty())
    {
      std::cerr << G_STRFUNC << "Could not save temp file to show in the EvView." << std::endl;
    }
  
    EvJob *job = ev_job_load_new(uri.c_str());
  
    m_ev_document_model = ev_document_model_new();
    ev_view_set_model(m_ev_view, m_ev_document_model);
384
385
386
387
    ev_document_model_set_continuous(m_ev_document_model, FALSE); //Show only one page.

    //TODO: Show that we are loading.
    //ev_view_set_loading(m_ev_view, TRUE);
Murray Cumming's avatar
Murray Cumming committed
388

389
    g_signal_connect (job, "finished",
390
391
      G_CALLBACK (image_glom_ev_job_finished), this);
    ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
392
393
394
395
  }
  else
  {
    //Use GtkImage instead:
396
    gtk_widget_hide(GTK_WIDGET(m_ev_view));  
397
398
    m_image.show();
    m_frame.add(m_image);
399
400
    
    Glib::RefPtr<const Gio::Icon> icon;
401
402
403
      
    bool use_gdkpixbuf = false;
    fill_gdkpixbuf_supported_mime_types();
Murray Cumming's avatar
Murray Cumming committed
404
    const auto iter_find_mime_type = 
405
406
407
      std::find(m_gdkpixbuf_supported_mime_types.begin(),
        m_gdkpixbuf_supported_mime_types.end(),
        mime_type);
Murray Cumming's avatar
Murray Cumming committed
408
    if(iter_find_mime_type != m_gdkpixbuf_supported_mime_types.end())
409
410
411
412
413
414
415
    {
      use_gdkpixbuf = true;
    }
    
    if(use_gdkpixbuf)
    {
      //Try to use GdkPixbuf's loader:
416
      m_pixbuf_original = UiUtils::get_pixbuf_for_gda_value(m_original_data);
417
418
419
    }
    else
    {
420
421
      //Get an icon for the file type;
      icon = Gio::content_type_get_icon(mime_type);
422
    }
423
    
424
    if(m_pixbuf_original)
425
426
427
428
    {
      Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = get_scaled_image();
      m_image.set(pixbuf_scaled);
    }
429
430
431
432
    else if(icon)
    {
      m_image.set(icon, Gtk::ICON_SIZE_DIALOG);
    }
433
    else
434
    {
435
      m_image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_DIALOG);
436
    }
437
438
439
  }
}

440
Glib::RefPtr<Gdk::Pixbuf> ImageGlom::get_scaled_image()
441
442
443
444
{
  Glib::RefPtr<Gdk::Pixbuf> pixbuf = m_pixbuf_original;

  if(!pixbuf)
445
    return pixbuf;
446
 
Murray Cumming's avatar
Murray Cumming committed
447
448
449
  const auto allocation = m_image.get_allocation();
  const auto pixbuf_height = pixbuf->get_height();
  const auto pixbuf_width = pixbuf->get_width();
450
    
Murray Cumming's avatar
Murray Cumming committed
451
452
  const auto allocation_height = allocation.get_height();
  const auto allocation_width = allocation.get_width();
453
      
Murray Cumming's avatar
Murray Cumming committed
454
455
  //std::cout << "pixbuf_height=" << pixbuf_height << ", pixbuf_width=" << pixbuf_width << std::endl;
  //std::cout << "allocation_height=" << allocation.get_height() << ", allocation_width=" << allocation.get_width() << std::endl;
456

457
  if( (pixbuf_height > allocation_height) ||
458
459
      (pixbuf_width > allocation_width) )
  {
460
    if(true) //allocation_height > 10 || allocation_width > 10)
461
    {
462
      Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = UiUtils::image_scale_keeping_ratio(pixbuf, allocation_height, allocation_width);
463
464
      
      //Don't set a new pixbuf if the dimensions have not changed:
465
      Glib::RefPtr<Gdk::Pixbuf> pixbuf_in_image;
466
467
468
469
470

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

      if( !pixbuf_in_image || !pixbuf_scaled || (pixbuf_in_image->get_height() != pixbuf_scaled->get_height()) || (pixbuf_in_image->get_width() != pixbuf_scaled->get_width()) )
471
      {
Murray Cumming's avatar
Murray Cumming committed
472
        /*
473
474
475
476
477
        std::cout << "get_scale(): returning scaled" << std::endl;
        if(pixbuf_scaled)
        {
          std::cout << "scaled height=" << pixbuf_scaled->get_height() << ", scaled width=" << pixbuf_scaled->get_width() << std::endl;
        }
Murray Cumming's avatar
Murray Cumming committed
478
        */
479
480
        
        return pixbuf_scaled;
481
      }
482
      else
483
      {
484
485
486
487
        //Return the existing one, 
        //instead of a new one with the same contents,
        //so no unnecessary changes will be triggered.
        return pixbuf_in_image;
488
      }
489
    }
490
  }
491
  
Murray Cumming's avatar
Murray Cumming committed
492
  //std::cout << "get_scaled(): returning original" << std::endl;
493
  return pixbuf;
494
495
}

496
void ImageGlom::on_menupopup_activate_open_file()
497
{
498
499
500
  open_with();
}

501
void ImageGlom::on_menupopup_activate_open_file_with()
502
{
503
  AppWindow* pApp = get_appwindow();
504
505

  //Offer the user a choice of suitable applications:
Murray Cumming's avatar
Murray Cumming committed
506
  const auto mime_type = get_mime_type();
507
508
509
510
511
512
  if(mime_type.empty())
  {
    std::cerr << G_STRFUNC << ": mime_type is empty." << std::endl;
  }
  
  Gtk::AppChooserDialog dialog(mime_type);
513
514
515
516
  if(pApp)
    dialog.set_transient_for(*pApp);

  if(dialog.run() != Gtk::RESPONSE_OK)
517
    return;
518
519
520
521
522
523
524
525
526
  
  Glib::RefPtr<Gio::AppInfo> app_info = dialog.get_app_info();
  if(!app_info)
  {
    std::cerr << G_STRFUNC << ": app_info was null." << std::endl;
  }
  
  open_with(app_info);
}
527

528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
static void make_file_read_only(const Glib::ustring& uri)
{
  std::string filepath;

  try
  {
    filepath = Glib::filename_from_uri(uri);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << G_STRFUNC << "Exception: " << ex.what() << std::endl;
    return;
  }
  
  if(filepath.empty())
  {
    std::cerr << G_STRFUNC << ": filepath is empty." << std::endl;
  }
  
Murray Cumming's avatar
Murray Cumming committed
547
  const auto result = chmod(filepath.c_str(), S_IRUSR);
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  if(result != 0)
  {
    std::cerr << G_STRFUNC << ": chmod() failed." << std::endl;
  }
  
  //Setting the attribute via gio gives us this exception:
  //"Setting attribute access::can-write not supported"
  /*
  Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(uri);

  Glib::RefPtr<Gio::FileInfo> file_info;

  try
  {
    file_info = file->query_info(G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << G_STRFUNC << ": query_info() failed: " << ex.what() << std::endl;
    return;
  }
  
  if(!file_info)
  {
572
    std::cerr << G_STRFUNC << ": : file_info is null" << std::endl;
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    return;
  }
  
  const bool can_write =
    file_info->get_attribute_boolean(G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
  if(!can_write)
    return;
    
  file_info->set_attribute_boolean(G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, false);
  
  try
  {
    file->set_attributes_from_info(file_info);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << G_STRFUNC << ": set_attributes_from_info() failed: " << ex.what() << std::endl;
  }
  */
}

594
Glib::ustring ImageGlom::save_to_temp_file(bool show_progress)
595
{
596
597
  Glib::ustring uri = Utils::get_temp_file_uri("glom_image");
  if(uri.empty())
598
  {
599
    std::cerr << G_STRFUNC << ": : uri is empty." << std::endl;
600
  }
601
  
602
603
604
605
606
  bool saved = false;
  if(show_progress)
    saved = save_file(uri);
  else
    saved = save_file_sync(uri);
607
  
608
609
610
611
612
  if(!saved)
  {
    uri = Glib::ustring();
    std::cerr << G_STRFUNC << ": save_file() failed." << std::endl;
  }
613
614
615
616
617
618
619
  else
  {
    //Don't let people easily edit the saved file,
    //because they would lose data when it is automatically deleted later.
    //Also they might think that editing it will change it in the database.
    make_file_read_only(uri);
  }
620
621
622
623
624
625

  return uri;
}

void ImageGlom::open_with(const Glib::RefPtr<Gio::AppInfo>& app_info)
{
Murray Cumming's avatar
Murray Cumming committed
626
  const auto uri = save_to_temp_file();
627
  if(uri.empty())
628
629
630
631
    return;

  if(app_info)
  {
Murray Cumming's avatar
Murray Cumming committed
632
    app_info->launch_uri(uri); //TODO: Get a GdkAppLaunchContext?
633
634
635
636
637
638
639
640
  }
  else
  {
    //TODO: Avoid duplication in xsl_utils.cc, by moving this into a utility function:  
#ifdef G_OS_WIN32
    // gtk_show_uri doesn't seem to work on Win32, at least not for local files
    // We use Windows API instead.
    // TODO: Check it again and file a bug if necessary.
641
642
    // TODO: and this might not be necessary with Gio::AppInfo::launch_default_for_uri().
    //   Previously we used gtk_show_uri().
643
644
    ShellExecute(0, "open", uri.c_str(), 0, 0, SW_SHOW);
#else
645
    Gio::AppInfo::launch_default_for_uri(uri);
646
647
648
#endif //G_OS_WIN32
  }
}
649

650

651
static void set_file_filter_images(Gtk::FileChooser& file_chooser)
652
{
653
  //Get image formats only:
654
655
656
  Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
  filter->set_name(_("Images"));
  filter->add_pixbuf_formats();
657
  file_chooser.add_filter(filter);
658
  
659
660
  ev_document_factory_add_filters(GTK_WIDGET(file_chooser.gobj()), 0);
  
661
662
663
  //Make Images the currently-selected one:
  file_chooser.set_filter(filter);
  
664
  /*  ev_document_factory_add_filters() add this already:
665
666
667
668
  filter = Gtk::FileFilter::create();
  filter->set_name(_("All Files"));
  filter->add_pattern("*");
  file_chooser.add_filter(filter);
669
  */
670
671
}

672
void ImageGlom::on_menupopup_activate_save_file()
673
{
674
  AppWindow* pApp = get_appwindow();
675
676
677
678
679

  Gtk::FileChooserDialog dialog(_("Save Image"), Gtk::FILE_CHOOSER_ACTION_SAVE);
  if(pApp)
    dialog.set_transient_for(*pApp);
          
680
  set_file_filter_images(dialog);
681

682
683
  dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
  dialog.add_button(_("_Save"), Gtk::RESPONSE_OK);
Murray Cumming's avatar
Murray Cumming committed
684
  const auto response = dialog.run();
685
686
687
688
  dialog.hide();
  if(response != Gtk::RESPONSE_OK)
    return;
    
Murray Cumming's avatar
Murray Cumming committed
689
  const auto uri = dialog.get_uri();
690
691
692
693
694
695
  if(uri.empty())
    return;
    
  save_file(uri);
}

696
697
698
699
700
701
702
bool ImageGlom::save_file_sync(const Glib::ustring& uri)
{
  //TODO: We should still do this asynchronously, 
  //even when we don't use the dialog's run() to do that
  //because we don't want to offer feedback.
  //Ideally, EvView would just load from data anyway.
  
Murray Cumming's avatar
Murray Cumming committed
703
  const auto gda_binary = get_binary();
704
705
706
707
708
709
710
711
712
713
714
715
716
717
  if(!gda_binary)
  {
    std::cerr << G_STRFUNC << ": GdaBinary is null" << std::endl;
    return false;
  }
    
  if(!gda_binary->data)
  {
    std::cerr << G_STRFUNC << ": GdaBinary::data is null" << std::endl;
    return false;
  }

  try
  {
Murray Cumming's avatar
Murray Cumming committed
718
    const auto filepath = Glib::filename_from_uri(uri);
719
720
721
722
723
724
725
726
727
728
729
    Glib::file_set_contents(filepath, (const char*)gda_binary->data, gda_binary->binary_length);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << G_STRFUNC << "Exception: " << ex.what() << std::endl;
    return false;
  }
  
  return true;
}

730
731
bool ImageGlom::save_file(const Glib::ustring& uri)
{
Murray Cumming's avatar
Murray Cumming committed
732
  DialogImageSaveProgress* dialog_save = nullptr;
733
734
735
736
737
  Utils::get_glade_widget_derived_with_warning(dialog_save);
  if(!dialog_save)
    return false;
    
  // Automatically delete the dialog when we no longer need it:
738
  std::shared_ptr<Gtk::Dialog> dialog_keeper(dialog_save);
739

740
  AppWindow* pApp = get_appwindow();
741
742
743
  if(pApp)
    dialog_save->set_transient_for(*pApp);

Murray Cumming's avatar
Murray Cumming committed
744
  const auto gda_binary = get_binary();
745
746
747
748
  if(!gda_binary)
    return false;

  dialog_save->set_image_data(*gda_binary);
749
750
  dialog_save->save(uri);

751
  dialog_save->run();
752

753
754
755
  return true;
}

756
void ImageGlom::on_menupopup_activate_select_file()
757
758
759
760
{
  if(m_read_only)
    return;
    
761
  AppWindow* pApp = get_appwindow();
762
763
764
765
766

  Gtk::FileChooserDialog dialog(_("Choose Image"), Gtk::FILE_CHOOSER_ACTION_OPEN);
  if(pApp)
    dialog.set_transient_for(*pApp);
          
767
  set_file_filter_images(dialog);
768

769
  dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
770
771
772
  dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
  int response = dialog.run();
  dialog.hide();
773

774
  if((response != Gtk::RESPONSE_CANCEL) && (response != Gtk::RESPONSE_DELETE_EVENT))
775
  {
Murray Cumming's avatar
Murray Cumming committed
776
    const auto uri = dialog.get_uri();
777
    if(!uri.empty())
778
    {
Murray Cumming's avatar
Murray Cumming committed
779
780
781
      DialogImageLoadProgress* dialog_progress = nullptr;
      Utils::get_glade_widget_derived_with_warning(dialog_progress);
      if(dialog_progress)
782
      {
783
        // Automatically delete the dialog when we no longer need it:
Murray Cumming's avatar
Murray Cumming committed
784
        std::shared_ptr<Gtk::Dialog> dialog_keeper(dialog_progress);
Armin Burgmeier's avatar
Armin Burgmeier committed
785

786
        if(pApp)
Murray Cumming's avatar
Murray Cumming committed
787
          dialog_progress->set_transient_for(*pApp);
788

Murray Cumming's avatar
Murray Cumming committed
789
        dialog_progress->load(uri);
Armin Burgmeier's avatar
Armin Burgmeier committed
790

Murray Cumming's avatar
Murray Cumming committed
791
        if(dialog_progress->run() == Gtk::RESPONSE_ACCEPT)
Armin Burgmeier's avatar
Armin Burgmeier committed
792
        {
793
          GdaBinary* bin = g_new(GdaBinary, 1);
Murray Cumming's avatar
Murray Cumming committed
794
          std::shared_ptr<GdaBinary> image_data = dialog_progress->get_image_data();
795
796
797
798
          bin->data = image_data->data;
          bin->binary_length = image_data->binary_length;

          m_original_data = Gnome::Gda::Value();
799
800
801
          
          g_value_unset(m_original_data.gobj());
          g_value_init(m_original_data.gobj(), GDA_TYPE_BINARY);
802
803
          gda_value_take_binary(m_original_data.gobj(), bin);

804
          show_image_data();
Armin Burgmeier's avatar
Armin Burgmeier committed
805
806
          signal_edited().emit();
        }
807
808
      }
    }
809
  }
810
811
}

812
813
814
815
816
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.

Murray Cumming's avatar
Murray Cumming committed
817
  const auto target = selection_data.get_target(); 
818

Murray Cumming's avatar
Murray Cumming committed
819
  const auto mime_type = get_mime_type();
820
821
822
823
824
825
  if(mime_type.empty())
  {
    std::cerr << G_STRFUNC << ": mime_type is empty." << std::endl;
  }
  
  if(target == mime_type)
826
  {
Murray Cumming's avatar
Murray Cumming committed
827
    const auto gda_binary = get_binary();
828
829
830
831
832
833
834
835
    if(!gda_binary)
      return;
    
    if(!gda_binary->data)
      return;
    
    selection_data.set(mime_type, 8, gda_binary->data, gda_binary->binary_length);

836
    // This set() override uses an 8-bit text format for the data.
837
    //selection_data.set_pixbuf(m_pixbuf_clipboard);
838
839
840
  }
  else
  {
841
    std::cout << "ExampleWindow::on_clipboard_get(): Unexpected clipboard target format. expected: " << mime_type << std::endl;
842
  }
843
844
845
846
}

void ImageGlom::on_clipboard_clear()
{
847
848
849
  if(m_read_only)
    return;

850
  m_pixbuf_clipboard.reset();
851
852
}

853
void ImageGlom::on_menupopup_activate_copy()
854
{
855
856
857
858
859
860
  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
861
    m_pixbuf_clipboard.reset();
862

863
864
865
  Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();

  //Targets:
Murray Cumming's avatar
Murray Cumming committed
866
  const auto mime_type = get_mime_type();
867
868
869
870
871
  if(mime_type.empty())
  {
    std::cerr << G_STRFUNC << ": mime_type is empty." << std::endl;
  }
  
872
  std::vector<Gtk::TargetEntry> listTargets;
873
  listTargets.push_back( Gtk::TargetEntry(mime_type) );
874

875
876
  refClipboard->set( listTargets, sigc::mem_fun(*this, &ImageGlom::on_clipboard_get), sigc::mem_fun(*this, &ImageGlom::on_clipboard_clear) );
}
877

878
879
void ImageGlom::on_clipboard_received_image(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
{
880
881
882
  if(m_read_only)
    return;

883
884
  if(pixbuf)
  {
Armin Burgmeier's avatar
Armin Burgmeier committed
885
886
887
    // Clear original data of previous image
    m_original_data = Gnome::Gda::Value();

888
    m_pixbuf_original = pixbuf;
889
    show_image_data();
890

891
892
    signal_edited().emit();
  }
893
894
}

895

896
void ImageGlom::on_menupopup_activate_paste()
897
{
898
899
900
  if(m_read_only)
    return;

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

904
905
  if(refClipboard)
    refClipboard->request_image( sigc::mem_fun(*this, &ImageGlom::on_clipboard_received_image) );
906
907
}

908
void ImageGlom::on_menupopup_activate_clear()
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
909
{
910
911
912
  if(m_read_only)
    return;

913
  m_original_data = Gnome::Gda::Value();
914
  show_image_data();
915
  signal_edited().emit();
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
916
917
}

918
919
void ImageGlom::setup_menu_usermode()
{
920
921
  //Create the Gio::ActionGroup and associate it with this widget:
  m_refActionGroup_UserModePopup = Gio::SimpleActionGroup::create();
922

923
  m_refActionOpenFile = m_refActionGroup_UserModePopup->add_action("open-file",
924
925
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_open_file) );

926
  m_refActionOpenFileWith = m_refActionGroup_UserModePopup->add_action("open-fil-ewith",
927
928
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_open_file_with) );
    
929
  m_refActionSaveFile = m_refActionGroup_UserModePopup->add_action("save-file",
930
931
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_save_file) );
    
932
  m_refActionSelectFile = m_refActionGroup_UserModePopup->add_action("select-file",
933
934
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_select_file) );

935
  m_refActionCopy = m_refActionGroup_UserModePopup->add_action("copy",
936
937
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_copy) );

938
  m_refActionPaste = m_refActionGroup_UserModePopup->add_action("paste",
939
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_paste) );
940

941
  m_refActionClear = m_refActionGroup_UserModePopup->add_action("clear",
Murray Cumming's avatar
0.8.34:    
Murray Cumming committed
942
    sigc::mem_fun(*this, &ImageGlom::on_menupopup_activate_clear) );
943

944
  insert_action_group("imagecontext", m_refActionGroup_UserModePopup);
945
946


947
948
  //Create the UI for the menu whose items will activate the actions,
  //when this UI (a GtkMenu) is added and shown:
949

950
951
952
  Glib::RefPtr<Gio::Menu> menu = Gio::Menu::create();
  menu->append(_("_Open File"), "context.open-file");
  menu->append(_("Open File With"), "context.open-file-with");
953
  menu->append(_("Select File"), "context.select-file");
954
955
956
  menu->append(_("_Copy"), "context.copy");
  menu->append(_("_Paste"), "context.paste");
  menu->append(_("_Clear"), "context.clear");
957

958
  m_pMenuPopup_UserMode = new Gtk::Menu(menu);
959
  m_pMenuPopup_UserMode->attach_to_widget(*this);
960
}
961
962
963

void ImageGlom::do_choose_image()
{
964
  on_menupopup_activate_select_file();
965
966
967
968
969
970
971
}

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

972
973
974
975
976
977
978
979
980
981
982
983
984
void ImageGlom::popup_menu(guint button, guint32 activate_time)
{
  if(!m_pMenuPopup_UserMode)
  {
    std::cerr << G_STRFUNC << ": m_pMenuPopup_UserMode is null" << std::endl;
    return;
  }

  m_pMenuPopup_UserMode->popup(button, activate_time);

  m_refActionSelectFile->set_enabled();
}

985
986

} //namespace Glom