原文: http://zetcode.com/gui/phpgtktutorial/customwidget/

工具箱通常仅提供最常见的窗口小部件,例如按钮,文本窗口小部件,滑块等。没有工具箱可以提供所有可能的窗口小部件。 如果需要更专业的小部件,我们必须自己创建它。

使用工具箱提供的绘图工具创建自定义窗口小部件。 有两种可能性。 程序员可以修改或增强现有的小部件。 或者,他可以从头开始创建自定义窗口小部件。

刻录小部件

这是我们从头开始创建的小部件的示例。 它基于最小的GtkWidget小部件。 可以在各种媒体刻录应用(例如 Nero Burning ROM)中找到此自定义窗口小部件。

  1. <?php
  2. /*
  3. ZetCode PHP GTK tutorial
  4. This example creates a burning
  5. custom widget.
  6. author: Jan Bodnar
  7. website: www.zetcode.com
  8. last modified: August 2011
  9. */
  10. class Burning extends GtkDrawingArea {
  11. public function __construct($par) {
  12. parent::__construct();
  13. $this->par = $par;
  14. $this->init_ui();
  15. }
  16. public function init_ui() {
  17. $this->num = array("75", "150", "225", "300",
  18. "375", "450", "525", "600", "675");
  19. $this->set_size_request(1, 30);
  20. $this->connect('expose_event', array($this, 'on_expose'));
  21. }
  22. public function on_expose() {
  23. $cr = $this->window->cairo_create();
  24. $this->draw_widget($cr);
  25. }
  26. public function draw_widget($cr) {
  27. $cr->SetLineWidth(0.8);
  28. $cr->SelectFontFace("Courier", CairoFontSlant::NORMAL,
  29. CairoFontWeight::NORMAL);
  30. $cr->SetFontSize(11);
  31. $width = $this->get_allocation()->width;
  32. $this->cur_width = $this->par->get_cur_value();
  33. $step = round($width / 10.0);
  34. $till = ($width / 750.0) * $this->cur_width;
  35. $full = ($width / 750.0) * 700;
  36. if ($this->cur_width >= 700) {
  37. $cr->SetSourceRgb(1.0, 1.0, 0.72);
  38. $cr->Rectangle(0, 0, $full, 30);
  39. $cr->Clip();
  40. $cr->Paint();
  41. $cr->ResetClip();
  42. $cr->SetSourceRgb(1.0, 0.68, 0.68).
  43. $cr->Rectangle($full, 0, $till-$full, 30);
  44. $cr->Clip();
  45. $cr->Paint();
  46. $cr->ResetClip();
  47. } else {
  48. $cr->SetSourceRgb(1.0, 1.0, 0.72);
  49. $cr->Rectangle(0, 0, $till, 30);
  50. $cr->Clip();
  51. $cr->Paint();
  52. $cr->ResetClip();
  53. }
  54. $cr->SetSourceRgb(0.35, 0.31, 0.24);
  55. $len = count($this->num);
  56. for ($i=1; $i <= $len; $i++) {
  57. $cr->MoveTo($i*$step, 0);
  58. $cr->LineTo($i*$step, 5);
  59. $cr->Stroke();
  60. $te = $cr->TextExtents($this->num[$i-1]);
  61. $cr->MoveTo($i*$step-$te['width']/2, 15);
  62. $cr->TextPath($this->num[$i-1]);
  63. $cr->Stroke();
  64. }
  65. }
  66. }
  67. class Example extends GtkWindow {
  68. public function __construct() {
  69. parent::__construct();
  70. $this->init_ui();
  71. }
  72. private function init_ui() {
  73. $this->set_title('Burning widget');
  74. $this->connect_simple('destroy', array('gtk', 'main_quit'));
  75. $this->cur_value = 0;
  76. $vbox = new GtkVBox(false, 2);
  77. $scale = new GtkHScale();
  78. $scale->set_range(0, 750);
  79. $scale->set_digits(0);
  80. $scale->set_size_request(160, 35);
  81. $scale->set_value($this->cur_value);
  82. $scale->connect('value-changed', array($this, 'on_changed'));
  83. $fixed = new GtkFixed();
  84. $fixed->put($scale, 50, 50);
  85. $vbox->pack_start($fixed);
  86. $this->burning = new Burning($this);
  87. $vbox->pack_start($this->burning, false, false, 0);
  88. $this->add($vbox);
  89. $this->set_default_size(350, 200);
  90. $this->set_position(GTK::WIN_POS_CENTER);
  91. $this->show_all();
  92. }
  93. public function on_changed($sender) {
  94. $this->cur_value = $sender->get_value();
  95. $this->burning->queue_draw();
  96. }
  97. public function get_cur_value() {
  98. return $this->cur_value;
  99. }
  100. }
  101. new Example();
  102. Gtk::main();
  103. ?>

我们在窗口底部放置一个GtkDrawingArea并手动绘制整个窗口小部件。 所有重要的代码都驻留在draw_widget()中,这是从 Burning 类的on_expose()方法调用的。 此小部件以图形方式显示了介质的总容量和可供我们使用的可用空间。 该小部件由比例小部件控制。 自定义窗口小部件的最小值为 0,最大值为 750。如果值达到 700,则开始绘制红色。 这通常表示过度燃烧。

  1. $this->num = array("75", "150", "225", "300",
  2. "375", "450", "525", "600", "675");

这些数字显示在刻录小部件上。 它们显示了介质的容量。

  1. $this->cur_width = $this->par->get_cur_value();

从父小部件中,我们获得了比例小部件的当前值。

  1. $till = ($width / 750.0) * $this->cur_width;
  2. $full = ($width / 750.0) * 700;

我们使用$width变量进行转换。 在比例尺值和自定义小部件的度量之间。 请注意,我们使用浮点值。 我们在绘图中获得了更高的精度。 $till参数确定要绘制的总大小。 该值来自滑块小部件。 它占整个面积的一部分。 $full参数确定了我们开始绘制红色的点。

  1. $cr->SetSourceRgb(1.0, 1.0, 0.72);
  2. $cr->Rectangle(0, 0, $full, 30);
  3. $cr->Clip();
  4. $cr->Paint();
  5. $cr->ResetClip();

我们绘制一个黄色矩形,直到介质充满为止。

  1. $te = $cr->TextExtents($this->num[$i-1]);
  2. $cr->MoveTo($i*$step-$te['width']/2, 15);
  3. $cr->TextPath($this->num[$i-1]);
  4. $cr->Stroke();

这里的代码在刻录小部件上绘制数字。 我们计算文本范围以正确定位文本。

  1. public function on_changed($sender) {
  2. $this->cur_value = $sender->get_value();
  3. $this->burning->queue_draw();
  4. }

我们从小部件中获取值,并将其存储在$this->cur_value变量中以备后用。 我们重新绘制刻录的小部件。

自定义小部件 - 图1

图:刻录小部件

在本章中,我们使用 GTK 和 PHP 编程语言创建了一个自定义窗口小部件。