樹心幽徑

« 20181128讀取檔名文字檔產生影像陣列,再以column major合併(mtile6e.c) | Main | 20181210在Ubuntu1604-Linux裝Anjuta c/c++/gtk IDE »

20181204可按+或-鍵來縮放基底影像並捲動的gtk-X視窗程式(gtk_scrolling_canvas.c)
2018/12/04,10:25

(1)編譯

$ gcc gtk_scrolling_canvas.c -Wall `pkg-config --cflags --libs gtk+-3.0` -export-dynamic -lm -o gtk_scrolling_canvas

(2a)執行
$ ./gtk_scrolling_canvas trkmap.jpg
map=trkmap.jpg
keyval=32

SPACE KEY PRESSED!
scwin minh=10.00, minw=10.00 vcurrent=1906.20 vupper=3804.00  vlower=0.00 v_step_inc=76.60 v_page_inc=689.40 v_page_size=766.00 v_min_inc=76.60
:

:

keyval=65453
KEY - PRESSED!
keyval=65451
KEY + PRESSED!

:

CTRL+HOME KEY PRESSED!
keyval=65367
CTRL+END KEY PRESSED!
keyval=65360
CTRL+HOME KEY PRESSED!
keyval=65367
CTRL+END KEY PRESSED!
keyval=65360
CTRL+HOME KEY PRESSED!
keyval=65506
keyval=65360
:

SHIFT+HOME KEY PRESSED!
keyval=65514
:

keyval=115
keyval=98
ALT+b KEY PRESSED!

(2b)畫面

alt

(3)程式碼:


http://oldwww.kmvs.km.edu.tw/lf/index.php?op=ViewResource&blogId=70&resource=gtk_scrolling_canvas.c

treehrt@hrt:~/mysqlc/gtk$ cat gtk_scrolling_canvas.c
// $ gcc gtk_scrolling_canvas.c -Wall `pkg-config --cflags --libs gtk+-3.0` -export-dynamic -lm -o gtk_scrolling_canvas
// $ ./gtk_scrolling_canvas trkmap.jpg
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <cairo.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static double rab=1;
GtkWidget *window;
GtkWidget *scwin;
GtkWidget *canvas;
GdkPixbuf *pix,*pixC;
char fname[100];

static gboolean on_window_draw (GtkWidget *da, GdkEvent *event, gpointer data)
{
    (void)event; (void)data;
    cairo_t *cr;
    cr = gdk_cairo_create (gtk_widget_get_window(da));
    gdk_cairo_set_source_pixbuf(cr, pixC, 0, 0);
    cairo_paint(cr);
    cairo_destroy (cr);
    return TRUE;
}

void showPixbuf(GdkPixbuf *p){
    cairo_t *cr;
    cr = gdk_cairo_create (gtk_widget_get_window(canvas));
    gdk_cairo_set_source_pixbuf(cr, p, 0, 0);
    cairo_paint(cr);
    cairo_destroy (cr);
}

gboolean on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer data) {
    printf("keyval=%d\n",event->keyval);
    if ((event->keyval == 65360) && (event->state & GDK_SHIFT_MASK)){
        printf("SHIFT+HOME KEY PRESSED!\n");
        return TRUE;
    }
    if ((event->keyval == 65360) && (event->state & GDK_CONTROL_MASK)){
        printf("CTRL+HOME KEY PRESSED!\n");
        return TRUE;
    }
    if ((event->keyval == 65367) && (event->state & GDK_CONTROL_MASK)){
        printf("CTRL+END KEY PRESSED!\n");
        return TRUE;
    }
    if ((event->keyval == 'a') && (event->state & GDK_CONTROL_MASK)){
        printf("CTRL+a KEY PRESSED!\n");
        return TRUE;
    }
    if ((event->keyval == 'b') && (event->state & GDK_MOD1_MASK)){
        printf("ALT+b KEY PRESSED!\n");
        return TRUE;
    }

    if (event->keyval == GDK_KEY_space){
      gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scwin),10*rab);
      gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scwin),10*rab);

        printf("SPACE KEY PRESSED!\n");
      double scwin_minh=gtk_scrolled_window_get_min_content_height(GTK_SCROLLED_WINDOW(scwin));
      double scwin_minw=gtk_scrolled_window_get_min_content_width(GTK_SCROLLED_WINDOW(scwin));
        GtkAdjustment *vad=gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scwin));
    printf("scwin minh=%.2f, minw=%.2lf vcurrent=%.2lf vupper=%.2lf  vlower=%.2lf v_step_inc=%.2lf v_page_inc=%.2lf v_page_size=%.2lf v_min_inc=%.2lf\n",
        scwin_minh,scwin_minw,
        gtk_adjustment_get_value(vad),
        gtk_adjustment_get_upper(vad),
        gtk_adjustment_get_lower(vad),
        gtk_adjustment_get_step_increment(vad),
        gtk_adjustment_get_page_increment(vad),
        gtk_adjustment_get_page_size(vad),
        gtk_adjustment_get_minimum_increment(vad)
        );

        return TRUE;
    }


    if (event->keyval == GDK_KEY_KP_Add){
    rab=rab*1.01;
        printf("KEY + PRESSED!\n");
      int zw=gdk_pixbuf_get_width(pix)*rab;
    int zh=gdk_pixbuf_get_height(pix)*rab;
    g_object_unref(pixC);
    pixC=gdk_pixbuf_scale_simple(pix,zw,zh, GDK_INTERP_BILINEAR);
    showPixbuf(pixC);
        return TRUE;
    }
    if (event->keyval == GDK_KEY_KP_Subtract){
    rab=rab*0.99;
        printf("KEY - PRESSED!\n");
      int zw=gdk_pixbuf_get_width(pix)*rab;
    int zh=gdk_pixbuf_get_height(pix)*rab;
    g_object_unref(pixC);
    pixC=gdk_pixbuf_scale_simple(pix,zw,zh, GDK_INTERP_BILINEAR);
    showPixbuf(pixC);
        return TRUE;
    }
    return FALSE;
}

int main (int argc, char *argv[])
{
    if (argc != 2) {
    g_print("Syntax Example :  ./a.out a.jpg");
    exit(-1);
    }
    strcpy(fname,argv[1]);
    g_print("map=%s \n",fname);

    GtkWidget *center_vbox;

  gtk_init (&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_add_events(window, GDK_KEY_PRESS_MASK);
  center_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  GError *err = NULL;
  pix = gdk_pixbuf_new_from_file(fname, &err);
  pixC = gdk_pixbuf_new_from_file(fname, &err);
  if(err)
    {
        printf("Error : %s\n", err->message);
        g_error_free(err);
        return FALSE;
    }
  int w=gdk_pixbuf_get_width(pix);
  int h=gdk_pixbuf_get_height(pix);

  canvas = gtk_drawing_area_new ();
  scwin = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(scwin), canvas);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  gtk_box_pack_start(GTK_BOX(center_vbox), scwin, TRUE, TRUE, 1);

  gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
  gtk_widget_add_events(canvas, GDK_BUTTON_PRESS_MASK);

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit) , NULL);
  g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (on_key_press), NULL);
  g_signal_connect (canvas, "draw", (GCallback) on_window_draw, NULL);
  gtk_widget_set_app_paintable(canvas, TRUE);

  gtk_container_add (GTK_CONTAINER (window), center_vbox);
  gtk_widget_set_size_request (window, 1024,768);
  gtk_widget_set_size_request (center_vbox, 1020,760);
  gtk_widget_set_size_request (canvas, w,h);
  gtk_widget_show_all (window);
  gtk_main();
  return 0;
}

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType