Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PictureView.cs 5.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // The Pimp The Pathetic Image Manipulation Program
  3. // Copyright (c) 2004-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. using System;
  15. using Gtk;
  16. using Pipi;
  17. namespace ThePimp
  18. {
  19. public class PictureArea : Gtk.DrawingArea
  20. {
  21. private Pipi.Picture _p;
  22. private Adjustment _hadj = null, _vadj = null;
  23. private bool _drag = false;
  24. private double _xdrag, _ydrag;
  25. void HAdjust(object sender, EventArgs args)
  26. {
  27. QueueDraw();
  28. }
  29. void VAdjust(object sender, EventArgs args)
  30. {
  31. QueueDraw();
  32. }
  33. protected override bool OnExposeEvent(Gdk.EventExpose e)
  34. {
  35. bool ret = base.OnExposeEvent(e);
  36. if(_hadj == null || _vadj == null)
  37. return ret;
  38. using (Gdk.GC gc = new Gdk.GC(GdkWindow))
  39. {
  40. int w = e.Area.Width;
  41. int h = e.Area.Height;
  42. int x = (int)_hadj.Value + e.Area.X;
  43. int y = (int)_vadj.Value + e.Area.Y;
  44. if(x + w > _p.Width)
  45. w = _p.Width - x < 0 ? 0 : _p.Width - x;
  46. if(y + h > _p.Height)
  47. h = _p.Height - y < 0 ? 0 : _p.Height - y;
  48. byte[] a = _p.GetPixels(w, h, x, y);
  49. GdkWindow.DrawRgb32Image(gc, e.Area.X, e.Area.Y, w, h,
  50. 0 /* no dithering */, a, w * 4);
  51. }
  52. return ret;
  53. }
  54. protected override void OnRealized()
  55. {
  56. base.OnRealized();
  57. }
  58. protected override bool OnButtonPressEvent(Gdk.EventButton e)
  59. {
  60. if(e.Button == 2)
  61. {
  62. GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Hand1);
  63. _drag = true;
  64. _xdrag = e.X;
  65. _ydrag = e.Y;
  66. }
  67. return base.OnButtonPressEvent(e);
  68. }
  69. protected override bool OnButtonReleaseEvent(Gdk.EventButton e)
  70. {
  71. if(e.Button == 2)
  72. {
  73. GdkWindow.Cursor = null;
  74. _drag = false;
  75. }
  76. return base.OnButtonReleaseEvent(e);
  77. }
  78. protected override bool OnMotionNotifyEvent(Gdk.EventMotion e)
  79. {
  80. if(_drag)
  81. {
  82. if(_hadj != null && e.X != _xdrag)
  83. {
  84. _hadj.Value += _xdrag - e.X;
  85. _xdrag = e.X;
  86. if(_hadj.Value + Allocation.Width > _p.Width)
  87. _hadj.Value = _p.Width - Allocation.Width;
  88. _hadj.ChangeValue();
  89. }
  90. if(_vadj != null && e.Y != _ydrag)
  91. {
  92. _vadj.Value += _ydrag - e.Y;
  93. _ydrag = e.Y;
  94. if(_vadj.Value + Allocation.Height > _p.Height)
  95. _vadj.Value = _p.Height - Allocation.Height;
  96. _vadj.ChangeValue();
  97. }
  98. }
  99. return base.OnMotionNotifyEvent(e);
  100. }
  101. protected override void OnSetScrollAdjustments(Adjustment hadj,
  102. Adjustment vadj)
  103. {
  104. _hadj = hadj;
  105. _vadj = vadj;
  106. if(hadj != null)
  107. hadj.ValueChanged += HAdjust;
  108. if(vadj != null)
  109. vadj.ValueChanged += VAdjust;
  110. }
  111. protected override void OnSizeAllocated(Gdk.Rectangle alloc)
  112. {
  113. base.OnSizeAllocated(alloc);
  114. if(_hadj != null)
  115. {
  116. _hadj.SetBounds(0, _p.Width, 1, alloc.Width, alloc.Width);
  117. if(_hadj.Value + alloc.Width > _p.Width)
  118. {
  119. _hadj.Value = _p.Width - alloc.Width;
  120. _hadj.ChangeValue();
  121. }
  122. }
  123. if(_vadj != null)
  124. {
  125. _vadj.SetBounds(0, _p.Height, 1, alloc.Height, alloc.Height);
  126. if(_vadj.Value + alloc.Height > _p.Height)
  127. {
  128. _vadj.Value = _p.Height - alloc.Height;
  129. _vadj.ChangeValue();
  130. }
  131. }
  132. }
  133. protected override void OnDestroyed()
  134. {
  135. if(_hadj != null)
  136. {
  137. _hadj.ValueChanged -= HAdjust;
  138. _hadj = null;
  139. }
  140. if(_vadj != null)
  141. {
  142. _vadj.ValueChanged -= VAdjust;
  143. _vadj = null;
  144. }
  145. }
  146. public PictureArea(Picture p)
  147. {
  148. _p = p;
  149. AddEvents((int)Gdk.EventMask.ButtonPressMask);
  150. AddEvents((int)Gdk.EventMask.ButtonReleaseMask);
  151. AddEvents((int)Gdk.EventMask.PointerMotionMask);
  152. }
  153. }
  154. public partial class PictureView : Gtk.ScrolledWindow
  155. {
  156. public readonly Picture Picture;
  157. public PictureView(Picture p)
  158. {
  159. Picture = p;
  160. Add(new PictureArea(p));
  161. ShowAll();
  162. }
  163. }
  164. }