You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 rivejä
3.6 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. void HAdjust(object sender, EventArgs args)
  24. {
  25. QueueDraw();
  26. }
  27. void VAdjust(object sender, EventArgs args)
  28. {
  29. QueueDraw();
  30. }
  31. protected override bool OnExposeEvent(Gdk.EventExpose e)
  32. {
  33. bool ret = base.OnExposeEvent(e);
  34. using (Gdk.GC gc = new Gdk.GC(GdkWindow))
  35. {
  36. int w = e.Area.Width;
  37. int h = e.Area.Height;
  38. int x = (int)_hadj.Value + e.Area.X;
  39. int y = (int)_vadj.Value + e.Area.Y;
  40. if(x + w > _p.Width)
  41. w = _p.Width - x < 0 ? 0 : _p.Width - x;
  42. if(y + h > _p.Height)
  43. h = _p.Height - y < 0 ? 0 : _p.Height - y;
  44. byte[] a = _p.GetPixels(w, h, x, y);
  45. GdkWindow.DrawRgb32Image(gc, e.Area.X, e.Area.Y, w, h,
  46. 0 /* no dithering */, a, w * 4);
  47. }
  48. return ret;
  49. }
  50. protected override void OnRealized()
  51. {
  52. base.OnRealized();
  53. }
  54. protected override void OnSetScrollAdjustments(Adjustment hadj,
  55. Adjustment vadj)
  56. {
  57. _hadj = hadj;
  58. _vadj = vadj;
  59. if(hadj != null)
  60. hadj.ValueChanged += HAdjust;
  61. if(vadj != null)
  62. vadj.ValueChanged += VAdjust;
  63. }
  64. protected override void OnSizeAllocated(Gdk.Rectangle alloc)
  65. {
  66. base.OnSizeAllocated(alloc);
  67. if(_hadj != null)
  68. {
  69. _hadj.SetBounds(0, _p.Width, 1, alloc.Width, alloc.Width);
  70. if(_hadj.Value + alloc.Width > _p.Width)
  71. {
  72. _hadj.Value = _p.Width - alloc.Width;
  73. _hadj.ChangeValue();
  74. }
  75. }
  76. if(_vadj != null)
  77. {
  78. _vadj.SetBounds(0, _p.Height, 1, alloc.Height, alloc.Height);
  79. if(_vadj.Value + alloc.Height > _p.Height)
  80. {
  81. _vadj.Value = _p.Height - alloc.Height;
  82. _vadj.ChangeValue();
  83. }
  84. }
  85. }
  86. protected override void OnDestroyed()
  87. {
  88. if(_hadj != null)
  89. {
  90. _hadj.ValueChanged -= HAdjust;
  91. _hadj = null;
  92. }
  93. if(_vadj != null)
  94. {
  95. _vadj.ValueChanged -= VAdjust;
  96. _vadj = null;
  97. }
  98. }
  99. public PictureArea(Picture p)
  100. {
  101. _p = p;
  102. }
  103. }
  104. public partial class PictureView : Gtk.ScrolledWindow
  105. {
  106. public readonly Picture Picture;
  107. public PictureView(Picture p)
  108. {
  109. Picture = p;
  110. Add(new PictureArea(p));
  111. ShowAll();
  112. }
  113. }
  114. }