.\" Copyright (c) 2002-2023 Julien Nadeau Carriere .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES .\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .Dd December 21, 2022 .Dt AG_COMBO 3 .Os Agar 1.7 .Sh NAME .Nm AG_Combo .Nd agar canned text input + drop-down menu widget .Sh SYNOPSIS .Bd -literal #include #include .Ed .Sh DESCRIPTION .\" IMAGE(/widgets/AG_ComboExpanded.png, "Expanded AG_Combo(3) widget") .\" IMAGE(/widgets/AG_Combo.png, "Collapsed AG_Combo(3) widget") The .Nm widget packs a .Xr AG_Textbox 3 widget next to a .Xr AG_Button 3 which triggers the expansion of a drop-down menu (a window containing an .Xr AG_Tlist 3 ) when pressed. The drop-down menu collapses if the user selects an item, or clicks outside of the .Nm area. .Pp .Nm provides the same functionality as .Xr AG_UCombo 3 , but also adds support for text search and arbitrary text input. .Sh INHERITANCE HIERARCHY .Xr AG_Object 3 -> .Xr AG_Widget 3 -> .Nm . .Sh INITIALIZATION .nr nS 1 .Ft "AG_Combo *" .Fn AG_ComboNew "AG_Widget *parent" "Uint flags" "const char *format" "..." .Pp .Ft "AG_Combo *" .Fn AG_ComboNewS "AG_Widget *parent" "Uint flags" "const char *label" .Pp .Ft "AG_Combo *" .Fn AG_ComboNewFn "AG_Widget *parent" "Uint flags" "const char *label" "AG_EventFn fn" "const char *fnArgs" "..." .Pp .Ft "void" .Fn AG_ComboSizeHint "AG_Combo *combo" "const char *text" "int nitems" .Pp .Ft "void" .Fn AG_ComboSizeHintPixels "AG_Combo *combo" "int w" "int nitems" .Pp .nr nS 0 The .Fn AG_ComboNew function allocates, initializes, and attaches a new .Nm widget. The string argument specifies an optional text label to be displayed at the left of the textbox. The .Fn AG_ComboNewFn variant sets the "combo-expanded" event handler to .Fa fn with optional arguments .Fa fnArgs . .Pp Acceptable .Fa flags include: .Bl -tag -width "AG_COMBO_SCROLLTOSEL " .It AG_COMBO_POLL List contents are dynamic (pass the .Dv AG_TLIST_POLL flag to the .Va tlist ) . .It AG_COMBO_ANY_TEXT Allow user to enter text that does not match any item in the list. .It AG_COMBO_SCROLLTOSEL Scroll to initial selection if it is not visible. .It AG_COMBO_HFILL Expand horizontally in parent container. .It AG_COMBO_VFILL Expand vertically in parent container. .It AG_COMBO_EXPAND Shorthand for .Dv AG_COMBO_HFILL | AG_COMBO_VFILL . .El .Pp The .Fn AG_ComboSizeHint function arranges for the .Xr AG_Tlist 3 widget displayed on popup to request a size large enough to display the given number of items. The .Fn AG_ComboSizeHintPixels variant specifies the width in number of pixels. .Sh ITEM SELECTION .nr nS 1 .Ft void .Fn AG_ComboSelect "AG_Combo *combo" "AG_TlistItem *item" .Pp .Ft "AG_TlistItem *" .Fn AG_ComboSelectPointer "AG_Combo *combo" "void *ptr" .Pp .Ft "AG_TlistItem *" .Fn AG_ComboSelectText "AG_Combo *combo" "const char *text" .Pp .nr nS 0 The .Fn AG_ComboSelect function sets the selection flag on the given .Fa item . .Pp The .Fn AG_ComboSelectPointer function selects the first item with a user pointer value matching .Fa ptr . Similarly, .Fn AG_ComboSelectText selects the first item with a text string equal to .Fa text . .Pp If the .Dv AG_COMBO_POLL option is set, both .Fn AG_ComboSelectPointer and .Fn AG_ComboSelectText will raise a .Sq tlist-poll event prior to making the selection. .Sh EVENTS The .Nm widget generates the following events: .Pp .Bl -tag -compact -width 2n .It Fn combo-selected "AG_TlistItem *item" An item was selected. .It Fn combo-expanded "void" The drop-down menu is now visible. May be used to populate .Va list . .It Fn combo-collapsed "void" The drop-down menu is now hidden. .It Fn combo-text-entry "const char *text" The .Dv AG_COMBO_ANY_TEXT option is set and the user has entered a string .Fa text which does not match any item in the list. .It Fn combo-text-unknown "const char *text" The .Dv AG_COMBO_ANY_TEXT flag is not set and the user has entered a string .Fa text which does not match any item in the list. .El .Sh STRUCTURE DATA For the .Ft AG_Combo object: .Pp .Bl -tag -compact -width "AG_Textbox *tbox " .It Ft AG_Tlist *list The .Xr AG_Tlist 3 displayed by .Nm when expanded, or NULL if collapsed (RO). .It Ft AG_Textbox *tbox The input .Xr AG_Textbox 3 (RO). .It Ft AG_Button *button The .Xr AG_Button 3 which triggers expansion (RO). .It Ft int nVisItems Initial number of items to show in expanded list. .El .Sh EXAMPLES The following code fragment generates a drop-down menu and reacts to a selection event by displaying a text dialog: .Bd -literal -offset indent .\" SYNTAX(c) static void ExpandItems(AG_Event *event) { AG_Combo *com = AG_COMBO_SELF(); AG_Tlist *tl = com->list; AG_TlistAdd(tl, NULL, "Foo"); AG_TlistAdd(tl, NULL, "Bar"); } static void SelectItem(AG_Event *event) { AG_TlistItem *item = AG_TLISTITEM_PTR(1); AG_TextMsg(AG_MSG_INFO, "Selected item: %s", item->text); } AG_Combo *com; com = AG_ComboNew(NULL, 0, "My combo: "); AG_SetEvent(com, "combo-expanded", ExpandItems, NULL); AG_SetEvent(com, "combo-selected", SelectItem, NULL); .Ed .Pp The following code fragment generates a drop-down menu displaying a tree: .Bd -literal -offset indent .\" SYNTAX(c) static void ExpandTreeItems(AG_Event *event) { AG_Combo *com = AG_COMBO_SELF(); AG_Tlist *tl = com->list; AG_TlistItem *it; it = AG_TlistAdd(tl, NULL, "Foo"); it->depth = 1; it->flags |= AG_TLIST_HAS_CHILDREN; { it = AG_TlistAdd(tl, NULL, "Bar"); it->depth = 2; it = AG_TlistAdd(tl, NULL, "Baz"); it->depth = 2; { it = AG_TlistAdd(tl, NULL, "Bezo"); it->depth = 3; } } } AG_ComboNewFn(NULL, 0, "My tree: ", ExpandTreeItems, NULL); .Ed .Sh SEE ALSO .Xr AG_Button 3 , .Xr AG_Intro 3 , .Xr AG_Textbox 3 , .Xr AG_Tlist 3 , .Xr AG_UCombo 3 , .Xr AG_Widget 3 , .Xr AG_Window 3 .Sh HISTORY The .Nm widget first appeared in Agar 1.0. As of Agar 1.6.0, .Dv AG_COMBO_TREE is a deprecated no-op. "combo-expanded" and "combo-selected" appeared in Agar 1.7.0. .Fn AG_ComboNewFn appeared in Agar 1.7.1.