indexing description: "The application's main window including the data" class MainWindow inherit WEL_FRAME_WINDOW redefine on_paint, on_control_command end creation make feature {NONE} -- Widgets and other display related stuff dc: WEL_CLIENT_DC; radio1, radio2, radio3: WEL_RADIO_BUTTON; reset_button, finish_button, insert_button, abort_button: WEL_PUSH_BUTTON; add_2_button, add_5_button, add_10_button, add_20_button: WEL_PUSH_BUTTON; -- The top left corner of the machine mach_x: Integer is 400; mach_y: Integer is 20; -- The data: one machine and three cards mach: Machine; c1, c2, c3: Card; -- Reset the values of all cards reset_cards is do c1.set_money (700); c2.set_money (1895); c3.set_money (2200); end; -- Format an integer representing a money value special_format (i: Integer): String is local a, b: Integer; do a := i // 100; b := i \\ 100; Result := ""; if a < 10 then Result.append ("0"); end; Result.append_integer (a); Result.append ("."); if b < 10 then Result.append ("0"); end; Result.append_integer (b); end; -- Draw a rectangle that is _not_ filled with the background color frame (pdc: WEL_DC; x1, y1, x2, y2: Integer) is do pdc.line (x1, y1, x2, y1); pdc.line (x2, y1, x2, y2); pdc.line (x1, y2, x2, y2); pdc.line (x1, y1, x1, y2); end; -- Paint one card draw_card (pdc: WEL_DC; xx, yy: Integer; name: String; value: Integer) is local s: String; do s := ""; s.append (name); s.append (": "); s.append (special_format (value)); pdc.rectangle (xx, yy, xx + 100, yy + 20); pdc.text_out (xx + 5, yy + 2, s); end; -- Paint all cards draw_cards (pdc: WEL_DC) is do draw_card (pdc, 20, 20, "Card 1", c1.money); draw_card (pdc, 20, 50, "Card 2", c2.money); draw_card (pdc, 20, 80, "Card 3", c3.money); end; -- Paint the machine draw_machine (pdc: WEL_DC) is local s: String; c: Card; xx, yy: Integer; do xx := mach_x; yy := mach_y; frame (pdc, xx, yy, xx + 100, yy + 170); pdc.text_out (xx + 10, yy + 5, "The Machine"); c := mach.get_card; -- test for card if c = void then s := "--.--"; else s := special_format (mach.money + c.money); end; pdc.rectangle (xx + 10, yy + 30, xx + 60, yy + 50); pdc.text_out (xx + 15, yy + 32, s); end; -- Repaint the machine repaint_machine is do dc.get; draw_machine (dc); dc.release; end; -- Repaint the cards repaint_cards is do dc.get; draw_cards (dc); dc.release; end; -- Wrapper for mach.add_money which additionally updates the display add_money (m: Integer) is do mach.add_money (m); repaint_machine; end; -- Update the display after a card has been ejected card_ejected is do insert_button.enable; reset_button.enable; repaint_machine; repaint_cards; end; -- Update the display after a card has been inserted card_inserted is do insert_button.disable; reset_button.disable; repaint_machine; end; -- Handle button events on_control_command (a_control: WEL_CONTROL) is local c: Card; do if a_control = reset_button then reset_cards; repaint_cards; elseif a_control = insert_button then if radio1.checked then c := c1; elseif radio2.checked then c := c2; else c := c3; end; mach.insert_card (c); if mach.get_card /= void then card_inserted; end; elseif a_control = finish_button then mach.finish; card_ejected; elseif a_control = abort_button then mach.abort; card_ejected; elseif a_control = add_2_button then add_money (200); elseif a_control = add_5_button then add_money (500); elseif a_control = add_10_button then add_money (1000); elseif a_control = add_20_button then add_money (2000); end; end; -- Handle repaint requests on_paint (pdc: WEL_PAINT_DC; invalid_rect: WEL_RECT) is do draw_cards (pdc); draw_machine (pdc); end; -- Create all widgets that are part of the machine create_machine is local xx, yy: Integer; do xx := mach_x; yy := mach_y; !! finish_button.make (current, "Finish", xx + 10, yy + 115, 80, 20, -1); !! abort_button.make (current, "Abort", xx + 10, yy + 140, 80, 20, -1); !! add_2_button.make (current, "+2", xx + 10, yy + 60, 37, 20, -1); !! add_5_button.make (current, "+5", xx + 53, yy + 60, 37, 20, -1); !! add_10_button.make (current, "+10", xx + 10, yy + 85, 37, 20, -1); !! add_20_button.make (current, "+20", xx + 53, yy + 85, 37, 20, -1); end; feature {ANY} make is do -- Create and configure the window make_top ("State Example"); resize (520, 230); -- Create the data objects !! mach.make; !! c1.make (0); !! c2.make (0); !! c3.make (0); reset_cards; -- Create the widgets !! dc.make (current); !! radio1.make (current, "Select Card 1", 140, 15, 120, 30, -1); !! radio2.make (current, "Select Card 2", 140, 45, 120, 30, -1); !! radio3.make (current, "Select Card 3", 140, 75, 120, 30, -1); radio1.set_checked; !! reset_button.make (current, "Reset Cards", 20, 120, 100, 40, -1); !! insert_button.make (current, ">> Insert >>", 280, 50, 100, 20, -1); create_machine; end; end -- class MainWindow