tictactoe.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. {
  2. Converted from C to Pascal by Frank Loemker
  3. <[email protected]>
  4. }
  5. unit tictactoe;
  6. interface
  7. uses
  8. glib,gdk,gtk;
  9. type
  10. PTictactoe = ^TTictactoe;
  11. TTictactoe = record
  12. vbox : TGtkVBox ;
  13. buttons : array [0..2 , 0..2] of pGtkWidget;
  14. end;
  15. PTictactoeClass = ^TTictactoeClass;
  16. TTictactoeClass = record
  17. parent_class: TGtkVBoxClass ;
  18. tictactoe: Procedure (ttt : pTictactoe); cdecl;
  19. end;
  20. Function tictactoe_get_type : guint;
  21. Function tictactoe_new : pGtkWidget;
  22. procedure tictactoe_clear (ttt : pTictactoe);
  23. implementation
  24. const
  25. ANZ_SIGNAL = 1;
  26. type
  27. TTT_Signals = (TICTACTOE_SIGNAL);
  28. const
  29. tictactoe_signals: array[TTT_Signals] of guint = (0);
  30. Procedure tictactoe_toggle (widget : pGtkWidget ; ttt: pTictactoe); cdecl;
  31. const rwins: array[0..7,0..2] of integer =
  32. ( ( 0, 0, 0 ), ( 1, 1, 1 ), ( 2, 2, 2 ),
  33. ( 0, 1, 2 ), ( 0, 1, 2 ), ( 0, 1, 2 ),
  34. ( 0, 1, 2 ), ( 0, 1, 2 ) );
  35. cwins:array [0..7,0..2] of integer =
  36. ( ( 0, 1, 2 ), ( 0, 1, 2 ), ( 0, 1, 2 ),
  37. ( 0, 0, 0 ), ( 1, 1, 1 ), ( 2, 2, 2 ),
  38. ( 0, 1, 2 ), ( 2, 1, 0 ) );
  39. var i, k : integer;
  40. success, found : boolean;
  41. begin
  42. for k:=0 to 7 do
  43. begin
  44. success := TRUE;
  45. found := FALSE;
  46. for i:=0 to 2 do
  47. begin
  48. success := success and
  49. boolean(active(pGTKTOGGLEBUTTON(ttt^.buttons[rwins[k,i],cwins[k,i]])^));
  50. found := found or
  51. (ttt^.buttons[rwins[k,i],cwins[k,i]] = widget);
  52. end;
  53. if (success and found) then
  54. begin
  55. gtk_signal_emit (pGTKOBJECT (ttt),
  56. tictactoe_signals[TICTACTOE_SIGNAL]);
  57. break;
  58. end;
  59. end;
  60. end;
  61. Procedure gtk_signal_default_marshallerT(theobject : pGtkObject;
  62. func : GTK_SIGNAL_FUNC;
  63. func_data : gpointer;
  64. args : pGtkArg); cdecl;
  65. begin
  66. gtk_marshal_NONE__NONE (theobject,func,func_data,args);
  67. end;
  68. Procedure tictactoe_class_init (theclass : pTictactoeClass );
  69. var object_class : pGtkObjectClass ;
  70. begin
  71. object_class := pGtkObjectClass (theclass);
  72. tictactoe_signals[TICTACTOE_SIGNAL] :=gtk_signal_new ('tictactoe',
  73. GTK_RUN_FIRST,
  74. object_class^.thetype,
  75. @theclass^.tictactoe - pointer(theclass),
  76. @gtk_signal_default_marshallerT, GTK_TYPE_NONE, 0);
  77. gtk_object_class_add_signals (object_class, pguint(@tictactoe_signals), ANZ_SIGNAL);
  78. theclass^.tictactoe := NIL;
  79. end;
  80. Procedure tictactoe_init (ttt : pTictactoe );
  81. var table : pGtkWidget ;
  82. i,j : gint;
  83. begin
  84. table := gtk_table_new (3, 3, true);
  85. gtk_container_add (pGTKCONTAINER(ttt), table);
  86. gtk_widget_show (table);
  87. for i:=0 to 2 do
  88. for j:=0 to 2 do
  89. begin
  90. ttt^.buttons[i][j] := gtk_toggle_button_new ();
  91. gtk_table_attach_defaults (pGTKTABLE(table), ttt^.buttons[i][j],
  92. i, i+1, j, j+1);
  93. gtk_signal_connect (pGTKOBJECT (ttt^.buttons[i][j]), 'toggled',
  94. GTK_SIGNAL_FUNC (@tictactoe_toggle), ttt);
  95. gtk_widget_set_usize (ttt^.buttons[i][j], 20, 20);
  96. gtk_widget_show (ttt^.buttons[i][j]);
  97. end;
  98. end;
  99. Procedure tictactoe_class_init2 (theclass : gpointer ); cdecl;
  100. begin
  101. tictactoe_class_init (theclass);
  102. end;
  103. Procedure tictactoe_init2 (ttt : gpointer; klass:gpointer); cdecl;
  104. begin
  105. tictactoe_init (ttt);
  106. end;
  107. Function tictactoe_get_type:guint;
  108. const ttt_type : guint = 0;
  109. ttt_info: TGtkTypeInfo = (
  110. type_name : 'Tictactoe';
  111. object_size : sizeof (TTictactoe);
  112. class_size : sizeof (TTictactoeClass);
  113. class_init_func : @tictactoe_class_init2;
  114. object_init_func : @tictactoe_init2;
  115. );
  116. begin
  117. if (ttt_type = 0) then
  118. ttt_type := gtk_type_unique (gtk_vbox_get_type (), @ttt_info);
  119. tictactoe_get_type:= ttt_type;
  120. end;
  121. Function tictactoe_new:pGtkWidget;
  122. begin
  123. tictactoe_new:= pGTKWIDGET ( gtk_type_new (tictactoe_get_type ()));
  124. end;
  125. Procedure tictactoe_clear (ttt : pTictactoe );
  126. var i,j : integer;
  127. begin
  128. for i:=0 to 2 do
  129. for j:=0 to 2 do
  130. begin
  131. gtk_signal_handler_block_by_data (pGTKOBJECT(ttt^.buttons[i][j]), ttt);
  132. gtk_toggle_button_set_active (pGTKTOGGLEBUTTON (ttt^.buttons[i][j]),
  133. false);
  134. gtk_signal_handler_unblock_by_data (pGTKOBJECT(ttt^.buttons[i][j]), ttt);
  135. end;
  136. end;
  137. end.