選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

191 行
5.4 KiB

  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. }