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.
 
 
 
 
 
 

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