source: libpipi/trunk/ThePimp/src/PictureView.cs @ 3145

Last change on this file since 3145 was 3039, checked in by Sam Hocevar, 14 years ago

ThePimp?: move source files to src/.

File size: 5.4 KB
Line 
1//
2//  The Pimp      The Pathetic Image Manipulation Program
3//  Copyright (c) 2008 Sam Hocevar <sam@zoy.org>
4//                All Rights Reserved
5//
6//  $Id$
7//
8//  This library is free software. It comes without any warranty, to
9//  the extent permitted by applicable law. You can redistribute it
10//  and/or modify it under the terms of the Do What The Fuck You Want
11//  To Public License, Version 2, as published by Sam Hocevar. See
12//  http://sam.zoy.org/wtfpl/COPYING for more details.
13//
14
15using System;
16using Gtk;
17using Pipi;
18
19namespace ThePimp
20{
21    public class PictureArea : Gtk.DrawingArea
22    {
23        private Pipi.Picture _p;
24        private Adjustment _hadj = null, _vadj = null;
25        private bool _drag = false;
26        private double _xdrag, _ydrag;
27
28        void HAdjust(object sender, EventArgs args)
29        {
30            QueueDraw();
31        }
32
33        void VAdjust(object sender, EventArgs args)
34        {
35            QueueDraw();
36        }
37
38        protected override bool OnExposeEvent(Gdk.EventExpose e)
39        {
40            bool ret = base.OnExposeEvent(e);
41
42            if(_hadj == null || _vadj == null)
43                return ret;
44
45            using (Gdk.GC gc = new Gdk.GC(GdkWindow))
46            {
47                int w = e.Area.Width;
48                int h = e.Area.Height;
49                int x = (int)_hadj.Value + e.Area.X;
50                int y = (int)_vadj.Value + e.Area.Y;
51                if(x + w > _p.Width)
52                    w = _p.Width - x < 0 ? 0 : _p.Width - x;
53                if(y + h > _p.Height)
54                    h = _p.Height - y < 0 ? 0 : _p.Height - y;
55
56                byte[] a = _p.GetPixels(w, h, x, y);
57                GdkWindow.DrawRgb32Image(gc, e.Area.X, e.Area.Y, w, h,
58                                         0 /* no dithering */, a, w * 4);
59            }
60
61            return ret;
62        }
63
64        protected override void OnRealized()
65        {
66            base.OnRealized();
67        }
68
69        protected override bool OnButtonPressEvent(Gdk.EventButton e)
70        {
71            if(e.Button == 2)
72            {
73                GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Hand1);
74                _drag = true;
75                _xdrag = e.X;
76                _ydrag = e.Y;
77            }
78
79            return base.OnButtonPressEvent(e);
80        }
81
82        protected override bool OnButtonReleaseEvent(Gdk.EventButton e)
83        {
84            if(e.Button == 2)
85            {
86                GdkWindow.Cursor = null;
87                _drag = false;
88            }
89
90            return base.OnButtonReleaseEvent(e);
91        }
92
93        protected override bool OnMotionNotifyEvent(Gdk.EventMotion e)
94        {
95            if(_drag)
96            {
97                if(_hadj != null && e.X != _xdrag)
98                {
99                    _hadj.Value += _xdrag - e.X;
100                    _xdrag = e.X;
101                    if(_hadj.Value + Allocation.Width > _p.Width)
102                        _hadj.Value = _p.Width - Allocation.Width;
103                    _hadj.ChangeValue();
104                }
105                if(_vadj != null && e.Y != _ydrag)
106                {
107                    _vadj.Value += _ydrag - e.Y;
108                    _ydrag = e.Y;
109                    if(_vadj.Value + Allocation.Height > _p.Height)
110                        _vadj.Value = _p.Height - Allocation.Height;
111                    _vadj.ChangeValue();
112                }
113            }
114
115            return base.OnMotionNotifyEvent(e);
116        }
117
118        protected override void OnSetScrollAdjustments(Adjustment hadj,
119                                                       Adjustment vadj)
120        {
121            _hadj = hadj;
122            _vadj = vadj;
123            if(hadj != null)
124                hadj.ValueChanged += HAdjust;
125            if(vadj != null)
126                vadj.ValueChanged += VAdjust;
127        }
128
129        protected override void OnSizeAllocated(Gdk.Rectangle alloc)
130        {
131            base.OnSizeAllocated(alloc);
132
133            if(_hadj != null)
134            {
135                _hadj.SetBounds(0, _p.Width, 1, alloc.Width, alloc.Width);
136                if(_hadj.Value + alloc.Width > _p.Width)
137                {
138                    _hadj.Value = _p.Width - alloc.Width;
139                    _hadj.ChangeValue();
140                }
141            }
142
143            if(_vadj != null)
144            {
145                _vadj.SetBounds(0, _p.Height, 1, alloc.Height, alloc.Height);
146                if(_vadj.Value + alloc.Height > _p.Height)
147                {
148                    _vadj.Value = _p.Height - alloc.Height;
149                    _vadj.ChangeValue();
150                }
151            }
152        }
153
154        protected override void OnDestroyed()
155        {
156            if(_hadj != null)
157            {
158                _hadj.ValueChanged -= HAdjust;
159                _hadj = null;
160            }
161
162            if(_vadj != null)
163            {
164                _vadj.ValueChanged -= VAdjust;
165                _vadj = null;
166            }
167        }
168
169        public PictureArea(Picture p)
170        {
171            _p = p;
172
173            AddEvents((int)Gdk.EventMask.ButtonPressMask);
174            AddEvents((int)Gdk.EventMask.ButtonReleaseMask);
175            AddEvents((int)Gdk.EventMask.PointerMotionMask);
176        }
177    }
178
179    public partial class PictureView : Gtk.ScrolledWindow
180    {
181        public readonly Picture Picture;
182
183        public PictureView(Picture p)
184        {
185            Picture = p;
186            Add(new PictureArea(p));
187            ShowAll();
188        }
189    }
190}
Note: See TracBrowser for help on using the repository browser.