diff --git a/API Documentation.en.md b/API Documentation.en.md deleted file mode 100644 index a9a8735..0000000 --- a/API Documentation.en.md +++ /dev/null @@ -1,2028 +0,0 @@ -# API Documentation - -[中文 API ](API 文档.md) - -Below is the API documentation for the main classes and functions of the **StellarX GUI Framework**. Detailed information such as the purpose, interface, parameters, and return values for each class are as follows: - -## Control Class (Abstract Base Class) - -**Description:** `Control` is the abstract base class for all controls, defining common properties and interfaces, including basic functionalities like position, size, and dirty flag. It provides drawing state save and restore mechanisms to ensure that a control's drawing operations do not affect the global drawing state. It also declares a series of pure virtual functions (such as `draw()` and `handleEvent()`) to be implemented by subclasses and prohibits copying (supports move semantics) to prevent accidental copying overhead. Generally, `Control` is not instantiated directly but serves as the parent class for other specific controls. - -**Features:** - -- Defines the basic properties of a control (coordinates, size, visibility, dirty flag, etc.) and provides corresponding accessor methods. -- Provides drawing state management interfaces (`saveStyle()`/`restoreStyle()`, etc.) for saving and restoring the global pen state before and after control drawing. -- Declares pure virtual interfaces, such as `draw()` (draw the control) and `handleEvent()` (handle events), which all concrete controls must implement. -- Supports move construction and move assignment to prevent controls from being accidentally copied. - -### Public Member Functions - -#### Control::updateBackground() - -- **Purpose:** Releases the previously saved background snapshot and recaptures the current background. Used to update the control's background cache after its size changes or content changes. Calling this function ensures the correctness of the control's background, avoiding misalignment due to size changes. - -- **Parameters:** None. - -- **Return Value:** None. - -- **Behavior Details:** In the default implementation, `updateBackground()` first discards the old background bitmap (equivalent to calling the internal `discardBackground()`), then resaves a background snapshot within the control's current position and new size range. This is usually called automatically when the control size changes or a redraw is needed, and generally does not require manual user calls. - -- **Example:** Assuming a custom control needs to refresh its background upon resizing, it can be called after adjusting the size: - - c++ - - ``` - myControl.setWidth(newW); - myControl.setHeight(newH); - myControl.updateBackground(); // Update the background snapshot to match the new size - ``` - - - -#### Control::onWindowResize() - -- **Purpose:** An event function called to notify the control to handle corresponding actions when the parent window's size changes. Used to adjust the control's state when the window size changes, for example, invalidating outdated background caches. - -- **Parameters:** None. - -- **Return Value:** None. - -- **Behavior Details:** In the default implementation, `onWindowResize()` calls `discardBackground()` to discard the background bitmap saved by the control, preventing background misalignment after the window size changes. Container controls override this method (e.g., `Canvas` will call the child controls' `onWindowResize()` after handling its own). Generally, users do not need to call this method actively—the framework automatically triggers the call when the window or container size changes. - -- **Example:** If a custom control needs to execute specific logic when the window size changes, it can override this function: - - c++ - - ``` - void MyControl::onWindowResize() - { - Control::onWindowResize(); // Call base class to discard background, etc. - // Custom logic, e.g., adjust position based on new window size - repositionBasedOnNewWindowSize(); - } - ``` - - - -#### Control::getX() / getY() / getWidth() / getHeight() - -- **Purpose:** Gets the control's global coordinate position (top-left corner `x, y`) or current size (width, height). - -- **Parameters:** None. - -- **Return Value:** Returns the control's global X coordinate, global Y coordinate, current width, and height respectively, all of type `int`. - -- **Behavior Details:** These methods provide the control's position and size in the window coordinate system, typically used for layout calculations or debugging. Note that global coordinates are relative to the origin (0,0) of the entire application window. - -- **Example:** - - c++ - - ``` - int x = ctrl.getX(); - int h = ctrl.getHeight(); - std::cout << "Control at X = " << x << ", height = " << h << std::endl; - ``` - - - -#### Control::getRight() / getBottom() - -- **Purpose:** Gets the X coordinate of the control's right edge (`getRight()`) and the Y coordinate of the control's bottom edge (`getBottom()`), calculated in global coordinates. - -- **Parameters:** None. - -- **Return Value:** The X coordinate of the control's right edge and the Y coordinate of the bottom edge, type `int`. - -- **Behavior Details:** These two methods are equivalent to `getX() + getWidth()` and `getY() + getHeight()`, used to conveniently obtain the boundary positions covered by the control in the window coordinate system. - -- **Example:** - - c++ - - ``` - if (cursorX < ctrl.getRight() && cursorY < ctrl.getBottom()) - { - // Cursor is within the control's rectangular area - } - ``` - - - -#### Control::getLocalX() / getLocalY() / getLocalWidth() / getLocalHeight() - -- **Purpose:** Gets the control's position relative to the parent container's coordinate system (`LocalX, LocalY`) and its size within the parent container (`LocalWidth, LocalHeight`). - -- **Parameters:** None. - -- **Return Value:** The control's X coordinate, Y coordinate, width, and height relative to the parent container, all of type `int`. - -- **Behavior Details:** When a control is added to a container (e.g., `Canvas`), its local coordinates use the container's top-left corner as the origin. If the control has no parent container (directly belongs to the window), the local coordinates are the same as the global coordinates. These methods are particularly useful for laying out controls inside a container. - -- **Example:** Assuming a container `canvas` contains a child control `childCtrl`: - - c++ - - ``` - int localX = childCtrl.getLocalX(); - int globalX = childCtrl.getX(); - std::cout << "Child global X: " << globalX - << ", local X in canvas: " << localX << std::endl; - ``` - - - -#### Control::getLocalRight() / getLocalBottom() - -- **Purpose:** Gets the coordinates of the control's right and bottom edges relative to the parent container's coordinate system. - -- **Parameters:** None. - -- **Return Value:** The control's right-side coordinate and bottom coordinate within the parent container, type `int`. - -- **Behavior Details:** Equivalent to `getLocalX() + getLocalWidth()` and `getLocalY() + getLocalHeight()` respectively. These methods provide convenience when needing to determine the control's boundary positions inside the container. - -- **Example:** Can be used to check if a child control exceeds the container's bounds: - - c++ - - ``` - if (childCtrl.getLocalRight() > canvas.getWidth()) - { - // The child control's right edge within the container exceeds the container's width - } - ``` - - - -#### Control::setX(int x) / setY(int y) / setWidth(int w) / setHeight(int h) - -- **Purpose:** Sets the control's global coordinate position (top-left X or Y) or adjusts the control's size (width or height). - -- **Parameters:** `x` or `y` is the new global coordinate; `w` is the new width value; `h` is the new height value (all of type `int`). - -- **Return Value:** None. - -- **Behavior Details:** Calling these methods directly modifies the control's position or size and automatically marks the control as "needing redraw" (internally sets the `dirty` flag to `true`), causing the control to be drawn at the new position or with the new size in the next refresh cycle. Note that if the control is in a container, setting the global coordinates will change its position in the window, but its position relative to the parent container will also be updated accordingly. - -- **Example:** Move control `ctrl` to window coordinates (100,100) and adjust the width to 200: - - c++ - - ``` - ctrl.setX(100); - ctrl.setY(100); - ctrl.setWidth(200); - // Height remains unchanged; if needed, call ctrl.setHeight(newHeight); - ``` - - - -#### Control::draw() (Pure Virtual Function) - -- **Purpose:** Abstract interface for drawing the control, implemented by concrete control classes with actual drawing logic. - -- **Parameters:** None (drawing information is determined by the control's internal state). - -- **Return Value:** None. - -- **Behavior Details:** `draw()` is called by the system during framework redraws; specific controls should complete their own drawing work in the implementation. Typically, a `draw()` implementation needs to first call `Control::saveStyle()` to save the current drawing state, then set drawing colors/fonts/etc. and draw, followed by calling `Control::restoreStyle()` to restore the state. **Users should not call this method directly**; to force a redraw, mark the control as dirty (`setDirty(true)`) and wait for the system call. - -- **Example:** The following is example pseudocode for a custom control implementing `draw()`: - - c++ - - ``` - void MyControl::draw() - { - saveStyle(); - setfillcolor(backgroundColor); - bar(x, y, x+width, y+height); // Draw background rectangle - // ... Other drawing ... - restoreStyle(); - } - ``` - - - -#### Control::handleEvent(const ExMessage& msg) (Pure Virtual Function) - -- **Purpose:** Abstract interface for event handling, implemented by concrete controls, used to handle interactive events like mouse and keyboard. - -- **Parameters:** `msg` is the EasyX framework's event message (`ExMessage`), containing information such as event type, mouse coordinates, and key presses. - -- **Return Value:** Boolean value, `true` indicates the event has been handled ("consumed") by the control and does not need further propagation; `false` indicates the control did not handle the event, and the event can be passed to other objects or default logic. - -- **Behavior Details:** The `handleEvent()` method is also automatically called by the system in the event loop, allowing controls to respond to user input. Different controls handle different types of events as needed (e.g., buttons handle mouse clicks, text boxes handle keyboard input). If a control recognizes and handles an event (e.g., a mouse click within the button area), it should return `true`, indicating the event stops here and does not propagate further. For unhandled events, it returns `false`. - -- **Example:** Simplified event handling logic for a button control: - - c++ - - ``` - bool Button::handleEvent(const ExMessage& msg) - { - if(msg.message == WM_LBUTTONDOWN && isPointInButton(msg.x, msg.y)) { - // Handle button click - onClickCallback(); // Call the registered callback - return true; - } - return false; - } - ``` - - - -#### Control::setIsVisible(bool show) - -- **Purpose:** Sets the control's visible state, dynamically showing or hiding the control. Passing `false` hides the control; passing `true` shows it. - -- **Parameters:** `show` is a boolean value specifying whether the control should be visible. `true` means visible, `false` means hidden. - -- **Return Value:** None. - -- **Behavior Details:** This method allows switching the control's display state at runtime. After calling, the control's `IsVisible()` state changes and the redraw logic is automatically triggered: when hidden, the control's content is no longer drawn; when shown again, the control is marked as needing redraw. For container controls (like `Canvas`), this method is overridden to **recursively affect its child controls**: hiding a container also hides all its child controls; showing the container again also shows the child controls. This simplifies the visibility control of entire interface groups. - -- **Example:** - - c++ - - ``` - button.setIsVisible(false); // Hide a button - // ... After some operations ... - button.setIsVisible(true); // Show the button again - ``` - - - -#### Control::setParent(Control* parent) - -- **Purpose:** Sets the control's parent container pointer. Usually called by the container when adding the control; manual calls are less common. - -- **Parameters:** `parent` is a pointer to the new parent container control (`Canvas`, etc.). Passing `nullptr` clears the parent container association. - -- **Return Value:** None. - -- **Behavior Details:** This method sets the internal `parent` pointer to the specified container and adjusts the control's coordinate interpretation accordingly (local coordinates are relative to the parent container when one exists). The framework automatically calls this function when adding a control to a container; user intervention is not required. **Note:** When moving a control from one container to another, besides calling `setParent()`, you must also ensure the control is removed from the old container and added to the new one (using the container's `addControl()`, etc.), otherwise the interface state will be inconsistent. - -- **Example:** - - c++ - - ``` - canvas1.addControl(std::move(ctrl)); // Internally automatically calls setParent(&canvas1) - // To transfer from canvas1 to canvas2: - canvas1.removeControl(ctrlPtr); - canvas2.addControl(std::move(ctrlPtr)); // Internally calls setParent(&canvas2) - ``` - - - -#### Control::setDirty(bool dirty) - -- **Purpose:** Manually sets the control's "needs redraw" flag. Call this function when the control's content or properties change and you want to refresh the display immediately. - -- **Parameters:** `dirty` boolean value, `true` marks the control as needing redraw, `false` clears the redraw flag. - -- **Return Value:** None. - -- **Behavior Details:** After setting `dirty` to `true`, the control's `draw()` will be called to redraw during the next screen refresh. Usually, the framework internally sets this flag when control properties change, e.g., after calling methods like `setX()/setWidth()` that modify position or size, the control is already marked dirty. Developers can also call it in custom scenarios, for example, when the control's internal state changes but there is no corresponding setter method. Generally, there is no need to manually set `dirty` to `false` because the framework automatically clears the flag after redrawing. - -- **Example:** - - c++ - - ``` - ctrl.setDirty(true); // Request the ctrl control to redraw - ``` - - - -#### Control::IsVisible() const - -- **Purpose:** Checks if the control is currently in a visible state. - -- **Parameters:** None. - -- **Return Value:** `bool`, whether the control is currently visible. `true` means visible, `false` means hidden. - -- **Behavior Details:** This method returns the state of the control's internal `show` flag. Initially, controls are visible by default (`true`). If `setIsVisible(false)` is called or the control's parent container is hidden, this method returns `false`. When the control is not visible, its `draw()` will not be called. - -- **Example:** - - c++ - - ``` - if (!ctrl.IsVisible()) - { - ctrl.setIsVisible(true); // Ensure the control is visible - } - ``` - - - -## Window Class (Application Main Window) - -**Description:** The `Window` class represents the application's main window. It manages window creation, the message loop, and the global control container. `Window` is responsible for initializing the graphics window (based on EasyX) and also serves as the carrier for all top-level controls (acting as their parent container). It handles system event distribution, timed redraws, etc., allowing users to focus on adding controls to the window and responding to events. An application typically creates only one `Window` instance. - -**Features:** - -- Provides multiple window initialization modes (e.g., with or without double buffering, whether to show the console, etc.) to suit different application needs. -- Supports setting window background color or background image, as well as window title. -- Built-in **dialog management** system: tracks popped-up modal/non-modal dialogs to avoid repeating the same dialog. -- Integrates a complete message processing loop, automatically dispatching events to each control's `handleEvent()` method and scheduling redraws of each control's `draw()`. -- Manages the lifecycle of all controls and dialogs added to the window, automatically cleaning up when the window is destroyed. - -### Public Member Functions - -#### Window(int width, int height, int mode, COLORREF bkColor = BLACK, std::string headline = "Window") (Constructor) - -- **Purpose:** Creates an application window of the specified size and performs necessary graphics initialization. You can specify the window mode, background color, and window title. - -- **Parameters:** - - - `width`: Window width (pixels). - - `height`: Window height (pixels). - - `mode`: Window mode flags (int type). Different values configure window properties, such as whether to use **double buffering** for drawing, whether to show the console window, etc. Typically, 0 represents the default (window mode, hide console), or you can use constants provided by EasyX to combine this parameter, e.g., `EW_SHOWCONSOLE` to show the console, `EW_NOCLOSE` to disable the close button, etc. - - `bkColor` *(optional)*: Window background color, defaults to black (`BLACK`). - - `headline` *(optional)*: Window title text, defaults to "Window". - -- **Return Value:** None (constructor). - -- **Behavior Details:** When constructing a `Window` object, it calls EasyX's graphics initialization function `initgraph(width, height, mode)` to create the form, automatically applies the provided background color, and sets the window title. If the mode enables double buffering, the framework will use a manual refresh mechanism. After creating the window, **you must call** `runEventLoop()` to enter the message loop before the window starts responding to events and drawing. - -- **Example:** - - c++ - - ``` - // Create an 800x600 window, double-buffered and showing the console, background white, titled "Example" - int mode = EW_SHOWCONSOLE | EW_NOBORDER; // Example mode combination only - Window mainWin(800, 600, mode, WHITE, "Example"); - mainWin.draw(); - mainWin.runEventLoop(); - ``` - - - -#### Window::~Window() (Destructor) - -- **Purpose:** Destroys the window object and cleans up resources. - -- **Parameters:** None. - -- **Return Value:** None. - -- **Behavior Details:** When the `Window` object is destroyed, the framework automatically closes the graphics window, releases EasyX graphics resources, and cleans up the controls and dialog objects hosted in the window. Usually does not require explicit calls; simply let the `Window` object go out of scope or be reclaimed by the system when the program ends. - -- **Example:** - - c++ - - ``` - { - Window win(400, 300, 0); - // ... Use win ... - } // Scope ends, automatically destructs, window closes - ``` - - - -#### Window::runEventLoop() - -- **Purpose:** Enters the window message processing loop, starting the GUI main event loop. After calling this function, the window begins responding to user interactions and continues running until the window is closed. - -- **Parameters:** None. - -- **Return Value:** `int`, the exit code of the window message loop (can typically be returned directly to `main()`). - -- **Behavior Details:** This function internally implements a typical event loop: continuously calls EasyX's `peekmessage()` to get event messages and dispatches them to the `handleEvent()` methods of the controls within the window. Simultaneously, it handles redraws as needed: each frame traverses all controls and calls their `draw()` methods. In the loop, the window also checks for the existence of modal dialogs and prevents interaction with underlying controls, etc. **Must** be called after creating the window and adding controls; otherwise, the window will be blank and unresponsive. - -- **Example:** - - c++ - - ``` - Window win(640, 480, EW_SHOWCONSOLE, LIGHTGRAY, "Demo"); - // ... Add controls and other initialization ... - wni.draw(); - return win.runEventLoop(); // Enter event loop, blocks until window closes - ``` - - - -#### Window::addControl(std::unique_ptr control) - -- **Purpose:** Adds a new control to the window for management and display. Can add any control inheriting from `Control`, such as buttons, text boxes, containers, etc. - -- **Parameters:** `control` is a `unique_ptr` smart pointer pointing to the control to be added. The function internally takes ownership of this pointer. - -- **Return Value:** None. - -- **Behavior Details:** When adding a control, the window includes the control in its internally maintained control list and calls the control's `setParent()`. Thereafter, the framework considers this control during each redraw and event handling. The control's initial position and size in the window coordinate system depend on the control's own properties. If you need to remove a control, currently it can be done via `clearAllControls()` (in `Canvas`) or by destroying the window. - -- **Example:** - - c++ - - ``` - auto btn = std::make_unique