GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Jul 30, 2015 12:12 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Gtk Image Cropper
PostPosted: Thu Jul 16, 2015 2:00 pm 
Offline
Familiar Face

Joined: Wed May 06, 2015 1:33 pm
Posts: 24
I have a gtk application working almost entirely with libfm. I am using fm_standart_view_new to display items in my dialog.

Is there any way to change the size of the images? or create a cropper to crop the images?

this is my code:

Code:
static void
ppd_content_new(GtkPhotoPickerDialog *dialog)
{
    GtkWidget *eb, *hbox, *b;
   
    fm_gtk_init(NULL);
    dialog->fv = (FmFolderView*)fm_standard_view_new(FM_FV_DENSE_VIEW,
        pp_update_files_popup, pp_open_folder_func);
    fm_folder_view_set_show_hidden(dialog->fv, FALSE);
    fm_folder_view_set_selection_mode(dialog->fv, GTK_SELECTION_SINGLE);
    g_signal_connect(dialog->fv, "clicked",
        G_CALLBACK(pp_on_file_clicked), dialog);
    g_signal_connect(dialog->fv, "sel-changed",
        G_CALLBACK(pp_on_sel_changed), dialog);

    eb = gtk_event_box_new();
    gtk_widget_set_name(eb, "toolbar");
    hbox = gtk_hbox_new(FALSE, 0);
    b = mk_button("Sort", "sr-sort", 55, "some tooltip");
    g_signal_connect(b, "button-press-event",
        (GCallback) on_sort_pressed, dialog);
    gtk_box_pack_start(GTK_BOX(hbox), b, FALSE, FALSE, 0);

    b = mk_button("Up", "sr-up", 55, "some tooltip");
    g_signal_connect(b, "button-press-event",
        (GCallback) on_up_pressed, dialog);
    gtk_box_pack_start(GTK_BOX(hbox), b, FALSE, FALSE, 0);
    dialog->upb = b;
   
    gtk_container_add(GTK_CONTAINER(eb), hbox);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
        eb, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
        GTK_WIDGET(dialog->fv), TRUE, TRUE, 5);
    gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);

    dialog->act_grp = gtk_action_group_new("View");
    mk_radio_actions(dialog->act_grp, view_sort_type_radio_actions,
        G_N_ELEMENTS(view_sort_type_radio_actions),
        (GCallback) on_view_sort_type_action, dialog);
    mk_radio_actions(dialog->act_grp, view_sort_dir_radio_actions,
        G_N_ELEMENTS(view_sort_dir_radio_actions),
        (GCallback) on_view_sort_dir_action, dialog);
}



Code:
static gboolean
pp_open_folder_func(GAppLaunchContext* ctx, GList* l,
    gpointer user_data, GError** err)
{
    FmIcon* fi = (FmIcon*)l->data;
    GtkPhotoPickerDialog *dialog = user_data;

    ENTER;
    //ppd_chdir(dialog, fm_file_info_get_path(fi));
    return TRUE;
}


Code:
static void
pp_update_files_popup(FmFolderView* fv, GtkWindow* win,
    GtkUIManager* ui, GtkActionGroup* act_grp,
    FmFileInfoList* files)
{
    ENTER;
}


Top
 Profile  
 
 Post subject: Re: Gtk Image Cropper
PostPosted: Sat Jul 18, 2015 9:29 pm 
Offline
GTK+ Guru

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 164
I am unfamiliar with libfm and can't tell what kind of images you might have from your code or how they are being displayed. GTK can crop images easy enough with a pixbuf though. You can try the following out and see if it is helpful.

Replace the jpeg for the pixbuf in main and test out.

Code:
//gcc -Wall image_crop1.c -o image_crop1 `pkg-config --cflags --libs gtk+-3.0`

#include <gtk/gtk.h>

static void display_pixbuf(GtkComboBox *combo, gpointer data[])
  {
    g_print("Crop Image %i\n", gtk_combo_box_get_active(combo));

    GdkPixbuf *pixbuf=NULL;
    if(gtk_combo_box_get_active(combo)==0)
      {
        pixbuf=gdk_pixbuf_new_subpixbuf(GDK_PIXBUF(data[0]), 0, 0, 400, 400);
      }
    else if(gtk_combo_box_get_active(combo)==1)
      {
        pixbuf=gdk_pixbuf_new_subpixbuf(GDK_PIXBUF(data[0]), 0, 0, 300, 300);
      }
    else
      {
        pixbuf=gdk_pixbuf_new_subpixbuf(GDK_PIXBUF(data[0]), 0, 0, 200, 200);
      }

    GdkWindow *win=gtk_widget_get_window(GTK_WIDGET(data[1]));
    cairo_t *cr=gdk_cairo_create(win);
    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_paint(cr);
    gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
    cairo_paint(cr);
    //Decrease reference count by one.
    g_object_unref(pixbuf);
    gtk_widget_show(GTK_WIDGET(data[1]));
  }
static void da_draw(GtkWidget *widget, cairo_t *cr, gpointer data[])
  {
    g_print("Draw\n");
    GdkPixbuf *pixbuf=NULL;
    if(gtk_combo_box_get_active(GTK_COMBO_BOX(data[2]))==0)
      {
        pixbuf=gdk_pixbuf_new_subpixbuf(GDK_PIXBUF(data[0]), 0, 0, 400, 400);
      }
    else if(gtk_combo_box_get_active(GTK_COMBO_BOX(data[2]))==1)
      {
        pixbuf=gdk_pixbuf_new_subpixbuf(GDK_PIXBUF(data[0]), 0, 0, 300, 300);
      }
    else
      {
        pixbuf=gdk_pixbuf_new_subpixbuf(GDK_PIXBUF(data[0]), 0, 0, 200, 200);
      }
    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_paint(cr);
    gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
    cairo_paint(cr);
    //Decrease reference count by one.
    g_object_unref(pixbuf);
    gtk_widget_show(widget);
  }
int main(int argc, char **argv)
  {
    gtk_init(&argc, &argv);
    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Crop Image");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 500, 500);
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);

    GError *error=NULL;
    GdkPixbuf *pixbuf=NULL;
    //Scale the image file.
    pixbuf=gdk_pixbuf_new_from_file_at_scale("picture.jpeg", 400, 400, FALSE, &error);
    if(error)
      {
        g_print("%s\n", error->message);
      }
    if(error!=NULL)g_error_free(error);

    GtkWidget *drawing_area=gtk_drawing_area_new();
    gtk_widget_set_hexpand(drawing_area, TRUE);
    gtk_widget_set_vexpand(drawing_area, TRUE);

    GtkWidget *combo=gtk_combo_box_text_new();
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 0, "1", "400X400");
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 1, "2", "300X300");
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 2, "3", "200X200");
    gtk_combo_box_set_active_id(GTK_COMBO_BOX(combo), "1");

    gpointer picture[]={pixbuf, drawing_area, combo};
    g_signal_connect(combo, "changed", G_CALLBACK(display_pixbuf), picture);
    g_signal_connect(drawing_area, "draw", G_CALLBACK(da_draw), picture);

    GtkWidget *grid=gtk_grid_new();
    gtk_grid_attach(GTK_GRID(grid), drawing_area, 0, 0, 4, 4);
    gtk_grid_attach(GTK_GRID(grid), combo, 0, 4, 1, 1);

    gtk_container_add(GTK_CONTAINER(window), grid);

    gtk_widget_show_all(GTK_WIDGET(window));
    gtk_main();
   
    return 0;
  }


Top
 Profile  
 
 Post subject: Re: Gtk Image Cropper
PostPosted: Sun Jul 19, 2015 6:49 am 
Offline
Familiar Face

Joined: Wed May 06, 2015 1:33 pm
Posts: 24
Thank you :)

This was very helpful.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group