backport
[AROS.git] / workbench / demos / gtmultiselect.c
blobfdea98f0cb9b2ff9648795ee1297137429f330d6
1 #include <intuition/intuition.h>
2 #include <libraries/gadtools.h>
3 #include <graphics/gfx.h>
4 #include <graphics/gfxmacros.h>
5 #include <aros/asmcall.h>
6 #include <proto/exec.h>
7 #include <proto/intuition.h>
8 #include <proto/graphics.h>
9 #include <proto/gadtools.h>
10 #include <proto/alib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <ctype.h>
17 #define SHOW_CHECKMARK 1
18 #define SHOW_SELECTFILL 0
20 #define WINWIDTH 200
21 #define WINHEIGHT 200
22 #define LVENTRIES 40
24 struct IntuitionBase *IntuitionBase;
25 struct GfxBase *GfxBase;
26 struct Library *GadToolsBase;
27 struct Screen *scr;
28 struct DrawInfo *dri;
29 APTR vi;
30 struct Window *win;
31 struct Gadget *gadlist, *gad, *lvgad;
32 struct RastPort *rp;
33 struct Node lvnode[LVENTRIES];
34 struct List lvlist;
35 struct Hook myrenderhook;
36 WORD greenpen = -1;
38 #define IS_SELECTED(node) (node->ln_Pri == 1)
39 #define DO_SELECT(node) node->ln_Pri = 1
40 #define DO_UNSELECT(node) node->ln_Pri = 0
42 static void cleanup(char *msg)
44 if(msg) printf("gtmultiselect: %s\n", msg);
46 if (win) CloseWindow(win);
47 if (gadlist) FreeGadgets(gadlist);
49 if (greenpen != -1) ReleasePen(scr->ViewPort.ColorMap, greenpen);
51 if (vi) FreeVisualInfo(vi);
52 if (dri) FreeScreenDrawInfo(scr, dri);
53 if (scr) UnlockPubScreen(0, scr);
55 if (GadToolsBase) CloseLibrary(GadToolsBase);
56 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
57 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
59 exit(0);
62 static void openlibs(void)
64 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
66 cleanup("Can't open intuition.library!");
69 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
71 cleanup("Can't open graphics.library!");
74 if (!(GadToolsBase = OpenLibrary("gadtools.library", 39)))
76 cleanup("Can't open gadtools.library!");
80 static void getvisual(void)
82 if (!(scr = LockPubScreen(0))) cleanup("Can't lock pub screen!");
83 if (!(dri = GetScreenDrawInfo(scr))) cleanup("Can't get drawinfo!");
84 if (!(vi = GetVisualInfoA(scr, NULL))) cleanup("Can' get visualinfo!");
86 greenpen = ObtainBestPen(scr->ViewPort.ColorMap, 0, 0x76767676, 0, OBP_FailIfBad, FALSE, TAG_DONE);
90 AROS_UFH3(IPTR, MyRenderFunc,
91 AROS_UFHA(struct Hook *, hook, A0),
92 AROS_UFHA(struct Node *, node, A2),
93 AROS_UFHA(struct LVDrawMsg *, msg, A1)
96 AROS_USERFUNC_INIT
98 IPTR retval;
100 if (msg->lvdm_MethodID == LV_DRAW)
102 struct DrawInfo *dri = msg->lvdm_DrawInfo;
103 struct RastPort *rp = msg->lvdm_RastPort;
104 struct TextExtent te;
105 WORD numfit;
107 WORD min_x = msg->lvdm_Bounds.MinX;
108 WORD min_y = msg->lvdm_Bounds.MinY;
109 WORD max_x = msg->lvdm_Bounds.MaxX;
110 WORD max_y = msg->lvdm_Bounds.MaxY;
111 WORD width, height;
113 UWORD erasepen;
115 if (msg->lvdm_State == LVR_SELECTED)
117 if (IS_SELECTED(node))
119 DO_UNSELECT(node);
121 else
123 DO_SELECT(node);
127 #if SHOW_SELECTFILL
128 erasepen = IS_SELECTED(node) ? FILLPEN : BACKGROUNDPEN;
129 #else
130 erasepen = BACKGROUNDPEN;
131 #endif
133 SetDrMd(rp, JAM1);
135 SetAPen(rp, dri->dri_Pens[erasepen]);
136 RectFill(rp, min_x, min_y, max_x, max_y);
138 #if SHOW_CHECKMARK
139 numfit = TextFit(rp, node->ln_Name, strlen(node->ln_Name),
140 &te, NULL, 1, max_x - min_x + 1 - 16, max_y - min_y + 1);
141 #else
142 numfit = TextFit(rp, node->ln_Name, strlen(node->ln_Name),
143 &te, NULL, 1, max_x - min_x + 1, max_y - min_y + 1);
144 #endif
146 SetAPen(rp, dri->dri_Pens[TEXTPEN]);
148 /* Render text */
149 #if SHOW_CHECKMARK
150 Move(rp, min_x + 16, min_y + rp->Font->tf_Baseline);
151 #else
152 Move(rp, min_x, min_y + rp->Font->tf_Baseline);
153 #endif
154 Text(rp, node->ln_Name, numfit);
156 #if SHOW_CHECKMARK
157 if (IS_SELECTED(node))
159 min_y += 1;
160 max_y -= 1;
161 min_x += 1;
162 height = max_y - min_y + 1;
163 width = height;
164 max_x = min_x + width - 1;
166 SetAPen(rp, greenpen);
167 Move(rp, min_x, min_y + height / 3); Draw(rp, min_x, max_y);
168 Move(rp, min_x + 1, min_y + height / 3); Draw(rp, min_x + 1, max_y);
170 SetAPen(rp, dri->dri_Pens[SHADOWPEN]);
171 Move(rp, min_x + 2, min_y + height / 3); Draw(rp, min_x + 2, max_y);
173 SetAPen(rp, greenpen);
174 Move(rp, min_x + 1, max_y); Draw(rp, max_x - 1, min_y);
175 Move(rp, min_x + 2, max_y); Draw(rp, max_x, min_y);
177 SetAPen(rp, dri->dri_Pens[SHADOWPEN]);
178 Move(rp, min_x + 3, max_y); Draw(rp, max_x + 1, min_y);
181 #endif
183 retval = LVCB_OK;
185 else
187 retval = LVCB_UNKNOWN;
190 return TRUE;
192 AROS_USERFUNC_EXIT
195 static void makegadgets(void)
197 struct NewGadget ng;
198 WORD i;
200 gad = CreateContext(&gadlist);
202 memset(&ng, 0, sizeof(ng));
204 ng.ng_VisualInfo = vi;
205 ng.ng_LeftEdge = scr->WBorLeft + 4;
206 ng.ng_TopEdge = scr->WBorTop + scr->Font->ta_YSize + 1 + 4;
207 ng.ng_Width = WINWIDTH - scr->WBorRight - scr->WBorLeft - 8;
208 ng.ng_Height = WINHEIGHT - scr->WBorTop - scr->Font->ta_YSize - 1 - scr->WBorBottom - 8;
210 NewList(&lvlist);
211 for(i = 0; i < LVENTRIES; i++)
213 char s[30];
215 sprintf(s, "Item %d", i);
217 lvnode[i].ln_Name = strdup(s);
218 AddTail(&lvlist, &lvnode[i]);
222 struct TagItem lv_tags[] =
224 {GTLV_Labels , (IPTR)&lvlist },
225 {GTLV_CallBack , (IPTR)&myrenderhook },
226 {GTLV_ItemHeight, dri->dri_Font->tf_YSize + 4 },
227 {TAG_DONE }
230 myrenderhook.h_Entry = AROS_ASMSYMNAME(MyRenderFunc);
232 gad = lvgad = CreateGadgetA(LISTVIEW_KIND, gad, &ng, lv_tags);
235 if (!gad) cleanup("Can't create gadgets!");
238 static void makewin(void)
240 win = OpenWindowTags(0, WA_PubScreen, (IPTR)scr,
241 WA_Left, 20,
242 WA_Top, 20,
243 WA_Width, WINWIDTH,
244 WA_Height, WINHEIGHT,
245 WA_Title, (IPTR)"GadTools MultiSelect Listview",
246 WA_CloseGadget, TRUE,
247 WA_DragBar, TRUE,
248 WA_DepthGadget, TRUE,
249 WA_IDCMP, IDCMP_CLOSEWINDOW | LISTVIEWIDCMP,
250 WA_Activate, TRUE,
251 WA_Gadgets, (IPTR)gadlist,
252 TAG_DONE);
255 if (!win) cleanup("Can't create parent window!");
257 GT_RefreshWindow(win, NULL);
260 static void handleall(void)
262 struct IntuiMessage *imsg;
263 BOOL quitme = FALSE;
265 while(!quitme)
267 WaitPort(win->UserPort);
268 while((imsg = GT_GetIMsg(win->UserPort)))
270 switch(imsg->Class)
272 case IDCMP_CLOSEWINDOW:
273 quitme = TRUE;
274 break;
276 GT_ReplyIMsg(imsg);
283 int main(void)
285 openlibs();
286 getvisual();
287 makegadgets();
288 makewin();
289 handleall();
290 cleanup(0);
291 return 0;