Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_combobox_wheel.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
40
41#define ALLEGRO_UNSTABLE 1
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46
47#include "nilorea/n_common.h"
48#include "nilorea/n_gui.h"
49#include "nilorea/n_log.h"
50
52static int failures = 0;
53
55#define CB_X 10
57#define CB_Y 10
59#define CB_W 200
61#define CB_H 30
62
64#define ITEMS 9
65
71static void expect_true(const char* label, int cond) {
72 if (!cond) {
73 n_log(LOG_ERR, "%s: FAILED", label);
74 failures++;
75 } else {
76 n_log(LOG_INFO, "%s: OK", label);
77 }
78}
79
88static ALLEGRO_EVENT mouse_event(int type, int x, int y, int dz) {
89 ALLEGRO_EVENT ev;
90 memset(&ev, 0, sizeof(ev));
91 ev.type = (unsigned int)type;
92 ev.mouse.x = x;
93 ev.mouse.y = y;
94 ev.mouse.dz = dz;
95 ev.mouse.button = 1;
96 return ev;
97}
98
105static void click_at(N_GUI_CTX* gui, int x, int y) {
106 n_gui_process_event(gui, mouse_event(ALLEGRO_EVENT_MOUSE_AXES, x, y, 0));
107 n_gui_process_event(gui, mouse_event(ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, x, y, 0));
108 n_gui_process_event(gui, mouse_event(ALLEGRO_EVENT_MOUSE_BUTTON_UP, x, y, 0));
109}
110
116static int row_y(int row) {
117 return CB_Y + CB_H + row * CB_H + CB_H / 2;
118}
119
128static int wheel_then_click(N_GUI_CTX* gui, int cb, int row, int notches) {
129 int i = 0;
130 int x = CB_X + CB_W / 3;
131
132 click_at(gui, x, CB_Y + CB_H / 2); /* open it */
133 n_gui_process_event(gui, mouse_event(ALLEGRO_EVENT_MOUSE_AXES, x, row_y(row), 0));
134 for (i = 0; i < abs(notches); i++)
135 n_gui_process_event(gui, mouse_event(ALLEGRO_EVENT_MOUSE_AXES, x, row_y(row), notches > 0 ? -1 : 1));
136 click_at(gui, x, row_y(row));
138}
139
140int main(void) {
141 ALLEGRO_FONT* font;
142 N_GUI_CTX* gui;
143 int win, cb, i;
144 char name[16];
145
147 if (!al_init() || !al_init_font_addon()) {
148 n_log(LOG_ERR, "allegro init failed");
149 return 1;
150 }
151 font = al_create_builtin_font(); /* no display needed */
152 if (!font) {
153 n_log(LOG_ERR, "al_create_builtin_font failed");
154 return 1;
155 }
157 if (!gui) {
158 n_log(LOG_ERR, "n_gui_new_ctx failed");
159 al_destroy_font(font);
160 return 1;
161 }
162 n_gui_set_display_size(gui, 800.0f, 600.0f);
163 win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 500.0f);
165 cb = n_gui_add_combobox(gui, win, (float)CB_X, (float)CB_Y, (float)CB_W, (float)CB_H, NULL, NULL);
166 for (i = 0; i < ITEMS; i++) {
167 snprintf(name, sizeof(name), "item %d", i);
168 n_gui_combobox_add_item(gui, cb, name);
169 }
171
172 expect_true("with no wheel, a row selects the item it shows", wheel_then_click(gui, cb, 1, 0) == 1);
174 expect_true("three notches down scroll the list by three items", wheel_then_click(gui, cb, 1, 3) == 4);
176 expect_true("the wheel reaches the last item and stops there", wheel_then_click(gui, cb, 5, 20) == ITEMS - 1);
178 expect_true("and comes back up to the first", wheel_then_click(gui, cb, 0, -20) == 0);
179
181 al_destroy_font(font);
182 if (failures) {
183 n_log(LOG_ERR, "ex_gui_combobox_wheel: %d failure(s)", failures);
184 return 1;
185 }
186 n_log(LOG_NOTICE, "ex_gui_combobox_wheel: all checks passed");
187 return 0;
188}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static ALLEGRO_EVENT mouse_event(int type, int x, int y, int dz)
Build a left-button mouse event at (x, y).
#define CB_W
Where the combobox is and how tall one row of it is.
static int wheel_then_click(N_GUI_CTX *gui, int cb, int row, int notches)
Open the list, turn the wheel over one row, then click that row.
static int row_y(int row)
The screen y of the middle of one row of the open list.
#define CB_Y
Where the combobox is and how tall one row of it is.
static void click_at(N_GUI_CTX *gui, int x, int y)
Move to (x, y) and click there.
#define CB_H
Where the combobox is and how tall one row of it is.
#define CB_X
Where the combobox is and how tall one row of it is.
#define ITEMS
How many items: more than the six it shows.
static N_GUI_CTX * gui
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static const char * row(N_GUI_TREE *tree, int index)
the text of a visible row, "" past the end
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:13611
int n_gui_combobox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a combo box
Definition n_gui.c:6012
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:276
int n_gui_add_combobox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a combo box widget (dropdown selector).
Definition n_gui.c:3547
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
int n_gui_combobox_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected item index in a combobox widget
Definition n_gui.c:6034
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:2515
void n_gui_combobox_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a combobox widget
Definition n_gui.c:6116
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.