libxputty 0.1
Loading...
Searching...
No Matches
xcombobox.c
Go to the documentation of this file.
1/*
2 * 0BSD
3 *
4 * BSD Zero Clause License
5 *
6 * Copyright (c) 2019 Hermann Meyer
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted.
10
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21#include "xcombobox.h"
22#include "xcombobox_private.h"
23#include "xtooltip.h"
24#include "xslider.h"
25
26
27void combobox_set_active_entry(Widget_t *w, int active) {
28 float value = (float)active;
29 if (value>w->adj->max_value) value = w->adj->max_value;
30 else if (value<w->adj->min_value) value = w->adj->min_value;
31 adj_set_value(w->adj, value);
32}
33
34void combobox_rename_entry(Widget_t *w, int active, const char* label) {
35 float value = (float)active;
36 if ((value>w->adj->max_value) || (value<w->adj->min_value)) return;
37 Widget_t * menu = w->childlist->childs[1];
38 Widget_t* view_port = menu->childlist->childs[0];
39 ComboBox_t *comboboxlist = (ComboBox_t*)view_port->parent_struct;
40 //fprintf(stderr,"%s %s\n", comboboxlist->list_names[active], label);
41 free(comboboxlist->list_names[active]);
42 comboboxlist->list_names[active] = NULL;
43 asprintf(&comboboxlist->list_names[active],"%s",label);
44 assert(comboboxlist->list_names != NULL);
45}
46
47void combobox_mem_free(void *w_, void* user_data) {
48 Widget_t *w = (Widget_t*)w_;
49 ComboBox_t *comboboxlist = (ComboBox_t*)w->parent_struct;
50 unsigned int j = 0;
51 for(; j<comboboxlist->list_size;j++) {
52 free(comboboxlist->list_names[j]);
53 comboboxlist->list_names[j] = NULL;
54 }
55 free(comboboxlist->list_names);
56 free(comboboxlist);
57}
58
59void combobox_set_menu_size(Widget_t *combobox, int v) {
60 Widget_t * menu = combobox->childlist->childs[1];
61 Widget_t* view_port = menu->childlist->childs[0];
62 ComboBox_t *comboboxlist = (ComboBox_t*)view_port->parent_struct;
63 comboboxlist->show_items = v;
64 _configure_combobox_menu(combobox, menu, comboboxlist->show_items, true);
65}
66
68 Widget_t * menu = combobox->childlist->childs[1];
69 Widget_t* view_port = menu->childlist->childs[0];
70 ComboBox_t *comboboxlist = (ComboBox_t*)view_port->parent_struct;
71 unsigned int j = 0;
72 for(; j<comboboxlist->list_size;j++) {
73 free(comboboxlist->list_names[j]);
74 comboboxlist->list_names[j] = NULL;
75 }
76 comboboxlist->list_size = 0;
77 set_adjustment(combobox->adj,0.0, 0.0, 0.0, -1.0,1.0, CL_ENUM);
78 set_adjustment(view_port->adj,0.0, 0.0, 0.0, -6.0,1.0, CL_ENUM);
79 set_adjustment(comboboxlist->slider->adj,0.0, 0.0, 0.0, 1.0,0.0085, CL_VIEWPORTSLIDER);
80}
81
82void pop_combobox_menu_show(Widget_t *parent, Widget_t *menu, bool above) {
83 if (!childlist_has_child(menu->childlist)) return;
84 Widget_t* view_port = menu->childlist->childs[0];
85 ComboBox_t *comboboxlist = (ComboBox_t*)view_port->parent_struct;
86 if (!comboboxlist->list_size) return;
87 _configure_combobox_menu(parent, menu, comboboxlist->show_items, above);
89 int err = os_grab_pointer(menu);
90 menu->app->hold_grab = menu;
91
92 if (err) debug_print("Error grap pointer\n");
93}
94
95
96Widget_t* create_combobox_viewport(Widget_t *parent, int elem, int width, int height) {
97 Widget_t *wid = create_widget(parent->app, parent, 0, 0, width, height);
99 wid->scale.gravity = CENTER;
100 ComboBox_t *comboboxlist;
101 comboboxlist = (ComboBox_t*)malloc(sizeof(ComboBox_t));
102 comboboxlist->show_items = elem;
103 comboboxlist->prelight_item = 0;
104 comboboxlist->active_item = 0;
105 comboboxlist->list_names = NULL;
106 comboboxlist->list_size = 0;
107 wid->flags |= HAS_MEM;
108 wid->parent_struct = comboboxlist;
109 float max_value = -elem;
110 wid->adj_y = add_adjustment(wid,0.0, 0.0, 0.0, max_value,1.0, CL_VIEWPORT);
111 wid->adj = wid->adj_y;
120 return wid;
121}
122
124
125 int x1, y1;
126 os_translate_coords( parent, parent->widget, os_get_root_window(parent->app, IS_WIDGET), 0, 0, &x1, &y1);
127 Widget_t *wid = create_window(parent->app, os_get_root_window(parent->app, IS_WIDGET), x1, y1, 10, height);
128 Widget_t *viewport = create_combobox_viewport(wid, 6, 10, 5*height);
129 ComboBox_t *comboboxlist = (ComboBox_t*)viewport->parent_struct;
130 comboboxlist->combobox = parent;
131
133 os_set_transient_for_hint(parent, wid);
135 wid->flags |= IS_POPUP;
136 wid->scale.gravity = NONE;
137 childlist_add_child(parent->childlist, wid);
138
139 comboboxlist->slider = add_vslider(wid, "", 0, 0, 10, height);
141 set_adjustment(comboboxlist->slider->adj_y,0.0, 0.0, 0.0, 1.0,0.0085, CL_VIEWPORTSLIDER);
142 comboboxlist->slider->adj = comboboxlist->slider->adj_y;
144 comboboxlist->slider->scale.gravity = NORTHWEST;
145 comboboxlist->slider->flags &= ~USE_TRANSPARENCY;
146 comboboxlist->slider->flags |= NO_AUTOREPEAT | NO_PROPAGATE;
147 comboboxlist->slider->parent_struct = viewport;
148
149 return wid;
150}
151
152Widget_t* add_combobox(Widget_t *parent, const char * label, int x, int y, int width, int height) {
153
154 Widget_t *wid = create_widget(parent->app, parent, x, y, width, height);
155 wid->label = label;
156 wid->scale.gravity = CENTER;
157 wid->adj_y = add_adjustment(wid,0.0, 0.0, 0.0, -1.0,1.0, CL_ENUM);
158 wid->adj = wid->adj_y;
164
165 Widget_t* button = add_button(wid, "", width-20, 0, 20, height);
168
169 Widget_t* menu = create_combobox_menu(wid, 25);
171
172 return wid;
173}
174
175void combobox_add_entry(Widget_t *wid, const char * label) {
176 Widget_t *menu = wid->childlist->childs[1];
177 Widget_t* view_port = menu->childlist->childs[0];
178 ComboBox_t *comboboxlist = (ComboBox_t*)view_port->parent_struct;
179 comboboxlist->list_size++;
180 comboboxlist->list_names = (char **)realloc(comboboxlist->list_names,
181 comboboxlist->list_size * sizeof(char *));
182 asprintf(&comboboxlist->list_names[comboboxlist->list_size-1],"%s",label);
183 assert(comboboxlist->list_names != NULL);
184 float max_value = wid->adj->max_value+1.0;
185 set_adjustment(wid->adj,0.0, max_value, 0.0, max_value,1.0, CL_ENUM);
186
187}
188
189void combobox_add_numeric_entrys(Widget_t *wid, int imin, int imax) {
190 int i = imin;
191 int o = imax+1;
192 for (;i<o;i++) {
193 char s[32];
194 snprintf(s, 31, "%i",i);
195 combobox_add_entry(wid, s);
196 }
197}
float min_value
Definition xadjustment.h:90
float max_value
Definition xadjustment.h:92
Widget_t ** childs
Definition xchildlist.h:51
ComboBox_t - struct to hold information for the combobox.
Definition xcombobox.h:45
int active_item
Definition xcombobox.h:49
Widget_t * combobox
Definition xcombobox.h:47
char ** list_names
Definition xcombobox.h:52
int prelight_item
Definition xcombobox.h:48
unsigned int list_size
Definition xcombobox.h:51
Widget_t * slider
Definition xcombobox.h:46
int show_items
Definition xcombobox.h:50
xevfunc configure_notify_callback
Definition xwidget.h:93
evfunc button_release_callback
Definition xwidget.h:101
xevfunc expose_callback
Definition xwidget.h:85
evfunc key_press_callback
Definition xwidget.h:104
xevfunc leave_callback
Definition xwidget.h:88
xevfunc adj_callback
Definition xwidget.h:89
xevfunc value_changed_callback
Definition xwidget.h:90
xevfunc mem_free_callback
Definition xwidget.h:92
evfunc motion_callback
Definition xwidget.h:103
xevfunc enter_callback
Definition xwidget.h:87
Gravity gravity
Definition xwidget.h:347
Widget_t - struct to hold the basic Widget_t info.
Definition xwidget.h:457
Resize_t scale
Definition xwidget.h:525
Adjustment_t * adj_y
Definition xwidget.h:495
Adjustment_t * adj
Definition xwidget.h:497
Window widget
Definition xwidget.h:469
Childlist_t * childlist
Definition xwidget.h:499
long long flags
Definition xwidget.h:461
const char * label
Definition xwidget.h:463
void * parent_struct
Definition xwidget.h:473
Func_t func
Definition xwidget.h:481
Xputty * app
Definition xwidget.h:465
Widget_t * hold_grab
Definition xputty.h:238
void adj_set_value(Adjustment_t *adj, float value)
adj_set_value - set the current value to the Adjustment_t
Adjustment_t * add_adjustment(Widget_t *w, float std_value, float value, float min_value, float max_value, float step, CL_type type)
*add_adjustment - adding a adjustment to a Widget_t
Definition xadjustment.c:25
void set_adjustment(Adjustment_t *adj, float std_value, float value, float min_value, float max_value, float step, CL_type type)
*set_adjustment - set a new range to a existing Adjustment_t or create if it not exists yet
Definition xadjustment.c:80
@ CL_VIEWPORT
Definition xadjustment.h:57
@ CL_ENUM
Definition xadjustment.h:55
@ CL_VIEWPORTSLIDER
Definition xadjustment.h:65
int asprintf(char *strp[], const char *fmt,...)
Definition xasprintf.c:36
Widget_t * add_button(Widget_t *parent, const char *label, int x, int y, int width, int height)
add_button - add a button to a Widget_t connect to func.value_changed_callback to implement your acti...
Definition xbutton.c:26
void childlist_add_child(Childlist_t *childlist, Widget_t *child)
childlist_add_child - internal use to add a child to the Childlist_t You usually didn't need to cal...
Definition xchildlist.c:42
int childlist_has_child(Childlist_t *childlist)
childlist_has_child - check if a Widget_t Childlist_t contain a child
Definition xchildlist.c:89
void pop_combobox_menu_show(Widget_t *parent, Widget_t *menu, bool above)
Definition xcombobox.c:82
void combobox_set_active_entry(Widget_t *w, int active)
combobox_set_active_entry - set the active combobox entry
Definition xcombobox.c:27
void combobox_add_numeric_entrys(Widget_t *wid, int imin, int imax)
combobox_add_numeric_items - add numeric items from imin to imax to wid
Definition xcombobox.c:189
void combobox_delete_entrys(Widget_t *combobox)
combobox_delete_entrys - free the list hold the combobox entrys
Definition xcombobox.c:67
void combobox_rename_entry(Widget_t *w, int active, const char *label)
combobox_rename_entry - rename a entry in the combobox
Definition xcombobox.c:34
Widget_t * create_combobox_menu(Widget_t *parent, int height)
Definition xcombobox.c:123
void combobox_mem_free(void *w_, void *user_data)
combobox_mem_free - release additional used memory when destroy the Widget_t
Definition xcombobox.c:47
void combobox_set_menu_size(Widget_t *combobox, int v)
combobox_set_menu_size - set the number of entrys shown in the pop menu
Definition xcombobox.c:59
void combobox_add_entry(Widget_t *wid, const char *label)
combobox_add_entry - add a entry to the combobox
Definition xcombobox.c:175
Widget_t * create_combobox_viewport(Widget_t *parent, int elem, int width, int height)
Definition xcombobox.c:96
Widget_t * add_combobox(Widget_t *parent, const char *label, int x, int y, int width, int height)
add_combobox - add a combobox
Definition xcombobox.c:152
void _set_combobox_menu_viewport(void *w_, void *user_data)
_set_combobox_menu_viewport - set the viewport position from slider state
void _draw_combobox(void *w_, void *user_data)
_draw_combobox - internal draw the combobox to the buffer
void _leave_combobox(void *w_, void *user_data)
_leave_combobox - mouse pointer leave the combobox
void _draw_combobox_entrys(void *w_, void *user_data)
_draw_combobox_entrys - internal draw the combobox entrys to the buffer
void _combobox_motion(void *w_, void *xmotion_, void *user_data)
_combobox_motion - mouse pointer move in viewport
void _set_combobox_viewpoint(void *w_, void *user_data)
_set_combobox_viewpoint - set the slider state from viewport position
void _draw_combobox_menu_slider(void *w_, void *user_data)
_draw_combobox_menu_slider - internal draw the combobox menu slider
void _combobox_entry_released(void *w_, void *button_, void *user_data)
_combobox_entry_released - viewport entry released mouse button
void _draw_combobox_menu(void *w_, void *user_data)
_draw_combobox_menu - internal draw the combobox menu to the buffer
void _combobox_button_released(void *w_, void *button_, void *user_data)
_combobox_button_released - popup menu on right click
void _reconfigure_combobox_viewport(void *w_, void *user_data)
_reconfigure_combobox_viewport - set slider scale and step to match viewport entrys
void _draw_combobox_button(void *w_, void *user_data)
_draw_combobox_button - internal draw the combobox button to the buffer
void _configure_combobox_menu(Widget_t *parent, Widget_t *menu, int elem, bool above)
_configure_combobox_menu - set final size and position of menu to a Widget_t
void _button_combobox_released(void *w_, void *button_, void *user_data)
_button_combobox_released - popup the combobox menu
void _combobox_key_pressed(void *w_, void *xkey_, void *user_data)
_combobox_key_released - viewport entry released key
void _entry_released(void *w_, void *item_, void *user_data)
_entry_released - the combobox menu release func
void _set_entry(void *w_, void *user_data)
_set_entry - set the active combobox entry on adjustment change
Widget_t * add_vslider(Widget_t *parent, const char *label, int x, int y, int width, int height)
add_vslider - add a vertical slider to a Widget_t connect to func.value_changed_callback to implement...
Definition xslider.c:30
int os_grab_pointer(Widget_t *w)
os_grab_pointer - grab the mouse pointer. Works only on linux
Window os_get_root_window(Xputty *main, int flag)
os_get_root_window - get a pointer to the root window (desktop)
void os_translate_coords(Widget_t *w, Window from_window, Window to_window, int from_x, int from_y, int *to_x, int *to_y)
os_translate_coords - get x,y related to destination Window
void os_set_input_mask(Widget_t *w)
os_set_input_mask - set the Event mask to a Widget_t only work on linux, stub on Windows
void os_set_transient_for_hint(Widget_t *parent, Widget_t *w)
os_set_ transient_for_hint - set a Widget_t transient to a other Widget_t only work on linux,...
void os_transparent_draw(void *w_, void *user_data)
os_transparent_draw - Draw the Widget_t to the back buffer
void os_set_window_attrb(Widget_t *w)
os_set_window_attrb - set the attribute mask to a Widget_t only work on linux, stub on Windows
Widget_t * create_widget(Xputty *app, Widget_t *win, int x, int y, int width, int height)
*create_widget - create a widget A Widget_t could only be created as child of a other Widget_t To...
Definition xwidget.c:265
Widget_t * create_window(Xputty *app, Window win, int x, int y, int width, int height)
*create_window - create a Window You need to create as least minimun one Window to get started....
Definition xwidget.c:163
@ CENTER
Definition xwidget.h:320
@ NONE
Definition xwidget.h:328
@ NORTHWEST
Definition xwidget.h:300
@ IS_WIDGET
Definition xwidget.h:388
@ NO_AUTOREPEAT
Definition xwidget.h:408
@ HAS_MEM
Definition xwidget.h:406
@ NO_PROPAGATE
Definition xwidget.h:416
@ IS_POPUP
Definition xwidget.h:392
void pop_widget_show_all(Widget_t *w)
pop_widget_show_all - map/show popup widget with all it's childs
Definition xwidget.c:400