Sample11: List Widget.

The List Widget allows the user to pick up a text item from a list. Items can be kept sorted by providing the object with a sorting callback. A void pointer can be associated to each item for application convenience. Such user data pointer is passed to the application code as a member in structures LIST_SEL and LIST_ACT.

/* C version */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mgui.h" void ExitCB(MOBJECT obj, void *a, void *b) { MShellDestroy((MOBJECT)b); MGUITerm(); exit(0); } void SelectionListCB(MOBJECT p, LIST_SEL * la, void *label) { static int nn; char str[128]; if (la->pos >= 0) sprintf(str, "Focused row # %ld\n<%s>\nuser data: %ld", la->pos, la->item, (long) la->u_data); else strcpy(str, "No focused item in the list !"); MObjectSetText((MOBJECT) label, str); } void ScrolledListCB(MOBJECT p, LIST_ACT * la, void *v) { char str[128]; sprintf(str, "Activated row # %ld\n<%s>\nuser data: %ld", la->pos, la->item, (long) la->u_data); MMessageDialog("", str, "Ok", NULL); } void MGUIMain(int argc, char **argv) { MOBJECT pbutton, shell, form; MOBJECT slist, label; MTColor seagreen, white; int i; char str[512]; shell = MCreateShell("Scrolled List", SF_NO_CLOSE); form = MCreateRowForm(shell); MObjectSetBackgroundImageFile(form, "tile5.bmp", BI_TILED); slist = MCreateSList(form, FIXED_MEDIUM, 16, 10, 12); MSListSetCallback(slist, ScrolledListCB, slist); MSListSetSortCallback(slist, strcmp); MObjectSetTransparency(slist, 255); seagreen = MAllocColor(90, 170, 160); white = MAllocColor(255, 255, 255); MSBLSetHead(slist, "Col 1  Column 2"); MSBLHeadSetColor(slist, seagreen, white); for (i = 0; i < 50; i++) { sprintf(str, "row %2d item%04d", i, i); MSListAddItem(slist, str, (void *) i); if (i == 10) MSListSetItemCheckStatus(slist, str, True); } label = MCreateLabel(form, "\n\n", HELV_MEDIUM); MSListSetSelectionCallback(slist, SelectionListCB, label); pbutton = MCreatePButton(shell, "Quit", TIMES_MEDIUM); MPButtonSetCallback(pbutton, ExitCB, shell); MShellRealize(shell); MMainLoop(); }
Callback SelectionListCB() provides the application a notification each time the input focus moves through items. Callback ScrolledListCB() is the item activation callback. The List object calls this function when the user chooses an item via a mouse button double click or the 'Enter' key press. Items in the list are sorted using the standard 'strcmp' function which is set as the sorting callback by the MSListSetSortCallback() function call.

// C++ version #include <stdio.h> #include <string.h> #include "mguipp.h" class CaAppl : public CmAppl { public: void start(int, char **); }; CaAppl appInstance; class CaMainWindow : public CmShell { int perc; CmSList *slist; CmLabel *label; public: CaMainWindow(const char *title, int flags); void selectionListCB(LIST_SEL * la); void scrolledListCB(LIST_ACT * la); void exitCB(void); }; void CaMainWindow::exitCB(void) { delete this; CmAppl::end(0); } void CaAppl::start(int argc, char **argv) { CaMainWindow *win = new CaMainWindow("Example 10", SF_NO_CLOSE); win->realize(); mainLoop(); } void CaMainWindow::selectionListCB(LIST_SEL * la) { static int nn; char str[128]; if (la->pos >= 0) sprintf(str, "Focused row # %ld\n<%s>\nuser data: %ld", la->pos, la->item, (long) la->u_data); else strcpy(str, "No focused item in the list !"); label->setText(str); } void CaMainWindow::scrolledListCB(LIST_ACT * la) { char str[128]; sprintf(str, "Activated row # %ld\n<%s>\nuser data: %ld", la->pos, la->item, (long) la->u_data); messageDialog("", str, "Ok", NULL); } CaMainWindow::CaMainWindow(const char *title, int flags) : CmShell(title, flags) { CmSharedColor seagreen(80, 140, 120); CmSharedColor white(255, 255, 255); setBackgroundImageFile("tile5.bmp", BI_TILED); slist = new CmSList(this, FIXED_MEDIUM, 16, 10, 12); slist->setCallback(this, (LISTACT_CB)scrolledListCB); slist->setSortCallback(strcmp); slist->setTransparency(255); slist->setHead("Col 1  Column 2"); slist->setHeadColor(&seagreen, &white); for (int i = 0; i < 50; i++) { char str[128]; sprintf(str, "row %2d item%04d", i, i); slist->addItem(str, (void *) i); if (i == 10) slist->setItemCheckStatus(str, True); } label = new CmLabel(this, "\n\n", HELV_MEDIUM); slist->setSelectionCallback(this, (LISTSEL_CB)selectionListCB); CmPushButton *pb = new CmPushButton(this, " Ok ", TIMES_MEDIUM); pb->setCallback(this, (VOID_CB)exitCB); }
Screenshot

Back