# Implementation Steps - Incremental Calculator for AROS

Each step delivers a working, testable application with both UI and logic implemented.

---

## Step 1: Project Skeleton & Basic Window

**Goal:** Create a compilable project that opens a window and can be closed

### UI Components
- Makefile with cross-compiler setup
- Main source file with library initialization
- Basic window with close gadget
- Event loop handling `IDCMP_CLOSEWINDOW`

### Logic Components
- Library open/close functions (intuition, gadtools, graphics)
- Window open/close
- Main event loop
- Proper cleanup on exit

### Test Criteria
- [ ] Compile without errors
- [ ] Window opens
- [ ] Click close button → Window closes
- [ ] No crashes

---

## Step 2: Display Area (String Gadget)

**Goal:** Add a display area that shows "0" initially

### UI Components
- Visual info creation with `GetVisualInfoA()`
- Gadget context with `CreateContext()`
- One STRING_KIND gadget at top for display
- Display is read-only (non-editable)

### Logic Components
- Calculator state structure (initialized to 0)
- Display buffer management
- `UpdateDisplay()` function to refresh display text

### Test Criteria
- [ ] Window opens
- [ ] Display area shows "0"
- [ ] Display cannot be edited by user

---

## Step 3: Digit Input (0-9 Buttons)

**Goal:** Can click digit buttons to build numbers

### UI Components
- 10 button gadgets (BUTTON_KIND) for digits 0-9
- Grid layout:
  ```
  Row 2: [7] [8] [9]
  Row 3: [4] [5] [6]
  Row 4: [1] [2] [3]
  Row 5: [0]
  ```

### Logic Components
- Digit handling function
- Accumulate digits into current value
- STATE_IDLE → STATE_NUMBER transition
- Handle multiple digits (e.g., "123")

### Test Criteria
- [ ] Click 1, 2, 3 → Display shows "123"
- [ ] Click 0 → Display shows "0"
- [ ] Multiple clicks accumulate digits

---

## Step 4: Clear Functions (C and CE Buttons)

**Goal:** Can reset the calculator

### UI Components
- Two buttons: C (Clear All) and CE (Clear Entry)
- Row 1 layout: [C] [CE] [_] [_]

### Logic Components
- **C button**: Reset everything to initial state (accumulator=0, display="0", state=STATE_IDLE)
- **CE button**: Clear current entry only (display="0", keep accumulator and operator)
- State transitions back to STATE_IDLE (for C)

### Test Criteria
- [ ] Enter 123 → Click CE → Display shows "0" (accumulator kept)
- [ ] Enter 456 → Click C → Everything reset to "0"
- [ ] Both clear buttons work correctly

---

## Step 5: Basic Operations (+ and - Buttons)

**Goal:** Can perform addition and subtraction

### UI Components
- Two operator buttons: + and - on right column
- Row layout: [_] [_] [_] [+/]

### Logic Components
- Operator handling function
- STATE_NUMBER → STATE_OP_PENDING transition
- Store current value as accumulator
- Store operator (+ or -)
- Prepare for second number entry
- Clear display for next input

### Test Criteria
- [ ] Click 5 → Click + → Display still shows "5"
- [ ] Click 3 → Display shows "3"
- [ ] Operator is stored for later calculation
- [ ] Subtraction (-) works the same way

---

## Step 6: More Operations (* and / Buttons)

**Goal:** Can perform multiplication and division

### UI Components
- Two more operator buttons: * and / on right column
- Complete right column: Row 1 [/], Row 2 [*], Row 3 [-], Row 4 [+]

### Logic Components
- Extend operator handling for * and /
- Same state machine transitions as Step 5
- All four operators work identically

### Test Criteria
- [ ] Click 6 → Click * → Click 2 → Display shows "2"
- [ ] Click 8 → Click / → Click 2 → Display shows "2"
- [ ] All 4 operators (+, -, *, /) can be queued

---

## Step 7: Equals Button (=)

**Goal:** Complete calculation cycle

### UI Components
- Equals button (=) spanning two columns at bottom
- Row 5 layout: [0] [.] [=====]

### Logic Components
- STATE_OP_PENDING → STATE_RESULT transition
- Perform operation: accumulator (op) current value
- Store result in accumulator
- Display result with proper formatting
- Handle division by zero (display "Error")
- State returns to STATE_RESULT

### Test Criteria
- [ ] Click 2 → Click + → Click 3 → Click = → Display shows "5"
- [ ] Click 10 → Click - → Click 4 → Click = → Display shows "6"
- [ ] Click 6 → Click * → Click 7 → Click = → Display shows "42"
- [ ] Click 8 → Click / → Click 2 → Click = → Display shows "4"
- [ ] Click 5 → Click / → Click 0 → Click = → Display shows "Error"

---

## Step 8: Decimal Point Support

**Goal:** Can enter decimal numbers

### UI Components
- Decimal point button (.) next to 0
- Row 5 layout: [0] [.] [=====]

### Logic Components
- Handle decimal point input
- Track decimal places count
- Prevent multiple decimal points in same number
- Format display with decimal
- Proper state handling after operator press

### Test Criteria
- [ ] Click 1 → Click . → Click 5 → Display shows "1.5"
- [ ] Click 2 → Click . → Click Click . (prevented) → Only one decimal
- [ ] Click 1.5 → Click + → Click 2.3 → Display shows "2.3"
- [ ] 1.5 + 2.3 = 3.8 works correctly

---

## Step 9: Backspace Button

**Goal:** Can delete last digit

### UI Components
- Backspace button (<-) on row 1
- Row 1 layout: [C] [CE] [<-] [/]

### Logic Components
- Remove last digit from current value string
- Handle empty value (reset to "0")
- Handle decimal point removal
- Update decimal places count
- Re-render display

### Test Criteria
- [ ] Click 1, 2, 3 → Click <- → Display shows "12"
- [ ] Click 1, 2, 3 → Click <- twice → Display shows "1"
- [ ] Click 1, 2, 3 → Click <- three times → Display shows "0"
- [ ] Click 1, ., 5 → Click <- → Display shows "1."

---

## Step 10: Keyboard Support

**Goal:** Can use keyboard to operate calculator

### UI Components
- Enable `IDCMP_VANILLAKEY` in window IDCMP flags
- Handle `IDCMP_VANILLAKEY` events

### Logic Components
- Key mapping:
  - `0-9` → Digit buttons
  - `+` → Add
  - `-` → Subtract
  - `*` → Multiply
  - `/` → Divide
  - `=` or Enter → Equals
  - `.` → Decimal
  - Backspace → Backspace
  - Escape → Clear (C)
  - Delete → Clear Entry (CE)
- Call same button handler functions

### Test Criteria
- [ ] Type "2+3=" using keyboard → Display shows "5"
- [ ] Type "123" using keyboard → Display shows "123"
- [ ] Type ".5" → Display shows "0.5"
- [ ] Escape clears everything
- [ ] Backspace deletes digits

---

## Step 11: Menu Bar

**Goal:** Add standard application menu

### UI Components
- Menu bar with "Project" menu
- Menu items:
  - About
  - ---
  - Quit
- Use `CreateMenusA()` and `LayoutMenusA()`
- Use `SetMenuStrip()` to attach menu

### Logic Components
- Handle `IDCMP_MENUPICK` events
- About: Show simple requester with app name and version
- Quit: Signal application to close cleanly

### Test Criteria
- [ ] Menu bar appears in window
- [ ] Can open Project menu
- [ ] Click About → Requester appears
- [ ] Click Quit → Application closes

---

## Step 12: Polish & Final Testing

**Goal:** Complete application with all finishing touches

### UI Components
- Version string in window title: "$VER: Calculator 1.0 (DD.MM.YYYY) AROS Dev Team"
- Proper window minimum/maximum size
- Consistent button sizing and spacing
- Clean visual layout

### Logic Components
- Error handling improvements (overflow, invalid input)
- Chained operations support (e.g., 2+3= displays 5, then +4= displays 9)
- Memory leak verification
- Code cleanup and comments

### Test Criteria
- [ ] Full calculation: 12.5 * 4 = 50
- [ ] Chained operations: 2 + 3 = shows 5, then + 4 = shows 9
- [ ] Division by zero: 5 / 0 = "Error"
- [ ] Memory test: Run 10 times, no memory leak
- [ ] All keyboard shortcuts work
- [ ] Menu Quit works
- [ ] Clean exit every time

---

## Step Summary Table

| Step | Feature | UI Added | Logic Added | Deliverable |
|------|---------|----------|-------------|-------------|
| 1 | Basic Window | Window, close gadget | Libraries, event loop | Opens/closes without crash |
| 2 | Display | String gadget | State struct, UpdateDisplay() | Shows "0" |
| 3 | Digits | 10 digit buttons | Digit accumulation | Can enter "123" |
| 4 | Clear | C, CE buttons | Reset functions | Resets properly |
| 5 | Add/Sub | +, - buttons | Op storage, state transition | Can queue operations |
| 6 | Mul/Div | *, / buttons | Extended op handling | All 4 ops work |
| 7 | Equals | = button | Calculate function | 2+3=5 works |
| 8 | Decimal | . button | Decimal handling | Can enter 1.5 |
| 9 | Backspace | <- button | Digit removal | Can delete digits |
| 10 | Keyboard | - | Key mapping | Keyboard input works |
| 11 | Menu | Menu bar | Menu handlers | Menu items work |
| 12 | Polish | Title, layout | Error handling, docs | All features working |

---

## Implementation Notes

### Starting Point for Each Step
- Each step begins by copying the previous working step
- Implement only the new additions for that step
- Test before moving to next step
- Each step should build and run successfully

### Testing Approach
1. Build: `make clean && make`
2. Run: Test functionality manually
3. If broken: Debug and fix before proceeding
4. If working: Commit changes, proceed to next step

### File Changes Per Step

| Step | Files Modified |
|------|---------------|
| 1 | `Makefile`, `calculator.c` |
| 2 | `calculator.c` (add display gadget) |
| 3 | `calculator.c` (add digit buttons) |
| 4 | `calculator.c` (add C, CE buttons) |
| 5 | `calculator.c` (add +, - buttons) |
| 6 | `calculator.c` (add *, / buttons) |
| 7 | `calculator.c` (add = button, calculation) |
| 8 | `calculator.c` (add . button) |
| 9 | `calculator.c` (add backspace) |
| 10 | `calculator.c` (add keyboard handling) |
| 11 | `calculator.c` (add menu bar) |
| 12 | `calculator.c` (polish, test) |
