Support for non-ISO-8859-1 X keysyms. So in particular, pterm in a
[u/mdw/putty] / mac / macctrls.c
index 4cf3286..db454cf 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macctrls.c,v 1.30 2003/04/05 22:00:57 ben Exp $ */
+/* $Id: macctrls.c,v 1.39 2003/04/14 23:47:07 ben Exp $ */
 /*
  * Copyright (c) 2003 Ben Harris
  * All rights reserved.
 
 #include <MacTypes.h>
 #include <Appearance.h>
+#include <ColorPicker.h>
 #include <Controls.h>
 #include <ControlDefinitions.h>
 #include <Events.h>
+#include <Lists.h>
 #include <Menus.h>
 #include <Resources.h>
 #include <Script.h>
@@ -97,6 +99,7 @@ union macctrl {
     struct {
        struct macctrl_generic generic;
        ControlRef tbctrl;
+       ListHandle list;
        unsigned int nids;
        int *ids;
     } listbox;
@@ -148,6 +151,8 @@ static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
                                               ControlDefProcMessage, SInt32);
 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
                                               ControlDefProcMessage, SInt32);
+static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16, ControlRef,
+                                              ControlDefProcMessage, SInt32);
 #endif
 
 #if !TARGET_API_MAC_CARBON
@@ -176,6 +181,8 @@ static void macctrl_init()
     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
+    cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_ListBox);
+    (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_listbox_cdef);
     inited = 1;
 #endif
 }
@@ -207,6 +214,17 @@ static int macctrl_cmp_byctrl_find(void *av, void *bv)
        return 0;
 }
 
+static union control panellist;
+
+static void panellist_handler(union control *ctrl, void *dlg, void *data,
+                             int event)
+{
+    struct macctrls *mcs = dlg;
+
+    if (event == EVENT_SELCHANGE)
+       macctrl_switchtopanel(mcs, dlg_listbox_index(ctrl, dlg) + 1);
+}
+
 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
                       struct macctrls *mcs)
 {
@@ -216,16 +234,13 @@ void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
     Rect rect;
 
     macctrl_init();
+    if (mac_gestalts.apprvers >= 0x100)
+       CreateRootControl(window, &root);
 #if TARGET_API_MAC_CARBON
     GetPortBounds(GetWindowPort(window), &rect);
 #else
     rect = window->portRect;
 #endif
-    curstate.pos.h = rect.left + 13;
-    curstate.pos.v = rect.bottom - 33;
-    curstate.width = rect.right - rect.left - (13 * 2);
-    if (mac_gestalts.apprvers >= 0x100)
-       CreateRootControl(window, &root);
     mcs->window = window;
     mcs->byctrl = newtree234(macctrl_cmp_byctrl);
     mcs->focus = NULL;
@@ -239,17 +254,31 @@ void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
     mcs->panels = snewn(mcs->npanels, union macctrl *);
     memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
     curstate.panelnum = 0;
+
+    curstate.pos.h = rect.left + 13;
+    curstate.pos.v = rect.top + 13;
+    curstate.width = 160;
+    panellist.listbox.type = CTRL_LISTBOX;
+    panellist.listbox.handler = &panellist_handler;
+    panellist.listbox.height = 20;
+    panellist.listbox.percentwidth = 100;
+    macctrl_listbox(mcs, window, &curstate, &panellist);
+
+    curstate.pos.h = rect.left + 13 + 160 + 13;
+    curstate.pos.v = rect.bottom - 33;
+    curstate.width = rect.right - (rect.left + 13 + 160) - (13 * 2);
     for (i = 0; i < cb->nctrlsets; i++) {
        if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
                            cb->ctrlsets[i-1]->pathname)) {
            curstate.pos.v = rect.top + 13;
            curstate.panelnum++;
            assert(curstate.panelnum < mcs->npanels);
+           dlg_listbox_add(&panellist, mcs, cb->ctrlsets[i]->pathname);
        }
        macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
     }
-    macctrl_switchtopanel(mcs, 19);
-    /* 14 = proxies, 20 = SSH bugs */
+    macctrl_switchtopanel(mcs, 1);
+    /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
 }
 
 #define MAXCOLS 16
@@ -261,40 +290,17 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate,
     unsigned int i, j, ncols, colstart;
     struct mac_layoutstate cols[MAXCOLS];
 
-    fprintf(stderr, "--- begin set ---\n");
-    fprintf(stderr, "pathname = %s\n", s->pathname);
-    if (s->boxname && *s->boxname)
-       fprintf(stderr, "boxname = %s\n", s->boxname);
-    if (s->boxtitle)
-       fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
-
     cols[0] = *curstate;
     ncols = 1;
 
     for (i = 0; i < s->ncontrols; i++) {
        union control *ctrl = s->ctrls[i];
-       char const *s;
 
        colstart = COLUMN_START(ctrl->generic.column);
        switch (ctrl->generic.type) {
-         case CTRL_TEXT: s = "text"; break;
-         case CTRL_EDITBOX: s = "editbox"; break;
-         case CTRL_RADIO: s = "radio"; break;
-         case CTRL_CHECKBOX: s = "checkbox"; break;
-         case CTRL_BUTTON: s = "button"; break;
-         case CTRL_LISTBOX: s = "listbox"; break;
-         case CTRL_COLUMNS: s = "columns"; break;
-         case CTRL_FILESELECT: s = "fileselect"; break;
-         case CTRL_FONTSELECT: s = "fontselect"; break;
-         case CTRL_TABDELAY: s = "tabdelay"; break;
-         default: s = "unknown"; break;
-       }
-       fprintf(stderr, "  control: %s\n", s);
-       switch (ctrl->generic.type) {
          case CTRL_COLUMNS:
            if (ctrl->columns.ncols != 1) {
                ncols = ctrl->columns.ncols;
-               fprintf(stderr, "  split to %d\n", ncols);
                assert(ncols <= MAXCOLS);
                for (j = 0; j < ncols; j++) {
                    cols[j] = cols[0];
@@ -308,7 +314,6 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate,
                            ctrl->columns.percentages[j] / 100 - 6;
                }
            } else {
-               fprintf(stderr, "  join\n");
                for (j = 0; j < ncols; j++)
                    if (cols[j].pos.v > cols[0].pos.v)
                        cols[0].pos.v = cols[j].pos.v;
@@ -348,9 +353,6 @@ static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
 {
     unsigned int i, j;
     union macctrl *mc;
-    ListHandle list;
-    Size olen;
-
 
 #define hideshow(c) do {                                               \
     if (i == which) ShowControl(c); else HideControl(c);               \
@@ -391,17 +393,11 @@ static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
                 * At least under Mac OS 8.1, hiding a list box
                 * doesn't hide its scroll bars.
                 */
-               if (mac_gestalts.apprvers >= 0x100) {
-                   if (GetControlData(mc->listbox.tbctrl,
-                                      kControlEntireControl,
-                                      kControlListBoxListHandleTag,
-                                      sizeof(list), &list, &olen) == noErr)
 #if TARGET_API_MAC_CARBON
-                       hideshow(GetListVerticalScrollBar(list));
+               hideshow(GetListVerticalScrollBar(mc->listbox.list));
 #else
-                       hideshow((*list)->vScroll);
+               hideshow((*mc->listbox.list)->vScroll);
 #endif
-               }
                break;
              case MACCTRL_POPUP:
                hideshow(mc->popup.tbctrl);
@@ -439,6 +435,8 @@ static void macctrl_enfocus(union macctrl *mc)
 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
 {
 
+    if (mcs->focus == mc)
+       return;
     if (mcs->focus != NULL)
        macctrl_defocus(mcs->focus);
     mcs->focus = mc;
@@ -456,7 +454,6 @@ static void macctrl_text(struct macctrls *mcs, WindowPtr window,
     SInt16 height;
 
     assert(ctrl->text.label != NULL);
-    fprintf(stderr, "    label = %s\n", ctrl->text.label);
     mc->generic.type = MACCTRL_TEXT;
     mc->generic.ctrl = ctrl;
     mc->generic.privdata = NULL;
@@ -487,7 +484,6 @@ static void macctrl_text(struct macctrls *mcs, WindowPtr window,
        height = TEGetHeight(1, (*te)->nLines, te);
     }
 #endif
-    fprintf(stderr, "    height = %d\n", height);
     SizeControl(mc->text.tbctrl, curstate->width, height);
     curstate->pos.v += height + 6;
     add234(mcs->byctrl, mc);
@@ -502,11 +498,6 @@ static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
     union macctrl *mc = snew(union macctrl);
     Rect lbounds, bounds;
 
-    if (ctrl->editbox.label != NULL)
-       fprintf(stderr, "    label = %s\n", ctrl->editbox.label);
-    fprintf(stderr, "    percentwidth = %d\n", ctrl->editbox.percentwidth);
-    if (ctrl->editbox.password) fprintf(stderr, "    password\n");
-    if (ctrl->editbox.has_list) fprintf(stderr, "    has list\n");
     mc->generic.type = MACCTRL_EDITBOX;
     mc->generic.ctrl = ctrl;
     mc->generic.privdata = NULL;
@@ -602,6 +593,7 @@ static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
                FrameRect(&rect);
                InsetRect(&rect, 3, 3);
            }
+           EraseRect(&rect);
            (*(TEHandle)(*control)->contrlData)->viewRect = rect;
            TEUpdate(&rect, (TEHandle)(*control)->contrlData);
        }
@@ -644,8 +636,6 @@ static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
     Str255 title;
     unsigned int i, colwidth;
 
-    if (ctrl->radio.label != NULL)
-       fprintf(stderr, "    label = %s\n", ctrl->radio.label);
     mc->generic.type = MACCTRL_RADIO;
     mc->generic.ctrl = ctrl;
     mc->generic.privdata = NULL;
@@ -677,7 +667,6 @@ static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
        curstate->pos.v += 18;
     }
     for (i = 0; i < ctrl->radio.nbuttons; i++) {
-       fprintf(stderr, "    button = %s\n", ctrl->radio.buttons[i]);
        bounds.top = curstate->pos.v - 2;
        bounds.bottom = bounds.top + 18;
        bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
@@ -707,7 +696,6 @@ static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
     Str255 title;
 
     assert(ctrl->checkbox.label != NULL);
-    fprintf(stderr, "    label = %s\n", ctrl->checkbox.label);
     mc->generic.type = MACCTRL_CHECKBOX;
     mc->generic.ctrl = ctrl;
     mc->generic.privdata = NULL;
@@ -734,9 +722,6 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window,
     Str255 title;
 
     assert(ctrl->button.label != NULL);
-    fprintf(stderr, "    label = %s\n", ctrl->button.label);
-    if (ctrl->button.isdefault)
-       fprintf(stderr, "    is default\n");
     mc->generic.type = MACCTRL_BUTTON;
     mc->generic.ctrl = ctrl;
     mc->generic.privdata = NULL;
@@ -828,19 +813,8 @@ static void macctrl_listbox(struct macctrls *mcs, WindowPtr window,
 {
     union macctrl *mc = snew(union macctrl);
     Rect bounds;
-    ListHandle list;
     Size olen;
 
-    if (mac_gestalts.apprvers < 0x100)
-       return;
-    if (ctrl->listbox.label != NULL)
-       fprintf(stderr, "    label = %s\n", ctrl->listbox.label);
-    fprintf(stderr, "    height = %d\n", ctrl->listbox.height);
-    if (ctrl->listbox.draglist)
-       fprintf(stderr, "    draglist\n");
-    if (ctrl->listbox.multisel)
-       fprintf(stderr, "    multisel\n");
-    fprintf(stderr, "    ncols = %d\n", ctrl->listbox.ncols);
     assert(ctrl->listbox.percentwidth == 100);
     mc->generic.type = MACCTRL_LISTBOX;
     mc->generic.ctrl = ctrl;
@@ -851,29 +825,138 @@ static void macctrl_listbox(struct macctrls *mcs, WindowPtr window,
     bounds.left = curstate->pos.h;
     bounds.right = bounds.left + curstate->width;
     bounds.top = curstate->pos.v;
-    bounds.bottom = bounds.top + 20 * ctrl->listbox.height;
+    bounds.bottom = bounds.top + 16 * ctrl->listbox.height + 2;
 
-    mc->button.tbctrl = NewControl(window, &bounds, NULL, TRUE,
-                                  ldes_Default, 0, 0,
-                                  kControlListBoxProc, (long)mc);
-    if (!ctrl->listbox.multisel) {
+    if (mac_gestalts.apprvers >= 0x100) {
+       InsetRect(&bounds, 3, 3);
+       mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE,
+                                       ldes_Default, 0, 0,
+                                       kControlListBoxProc, (long)mc);
        if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                          kControlListBoxListHandleTag, sizeof(list), &list,
-                          &olen) != noErr)
+                          kControlListBoxListHandleTag,
+                          sizeof(mc->listbox.list), &mc->listbox.list,
+                          &olen) != noErr) {
+           DisposeControl(mc->listbox.tbctrl);
+           sfree(mc);
            return;
+       }
+    }
+#if !TARGET_API_MAC_CARBON
+    else {
+       InsetRect(&bounds, -3, -3);
+       mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE,
+                                       0, 0, 0,
+                                       SYS7_LISTBOX_PROC, (long)mc);
+       mc->listbox.list = (ListHandle)(*mc->listbox.tbctrl)->contrlData;
+       (*mc->listbox.list)->refCon = (long)mc;
+    }
+#endif
+    if (!ctrl->listbox.multisel) {
 #if TARGET_API_MAC_CARBON
-       SetListSelectionFlags(list, lOnlyOne);
+       SetListSelectionFlags(mc->listbox.list, lOnlyOne);
 #else
-       (*list)->selFlags = lOnlyOne;
+       (*mc->listbox.list)->selFlags = lOnlyOne;
 #endif
     }
     add234(mcs->byctrl, mc);
-    curstate->pos.v += 6 + 20 * ctrl->listbox.height;
+    curstate->pos.v += 6 + 16 * ctrl->listbox.height + 2;
     mc->generic.next = mcs->panels[curstate->panelnum];
     mcs->panels[curstate->panelnum] = mc;
     ctrlevent(mcs, mc, EVENT_REFRESH);
 }
 
+#if !TARGET_API_MAC_CARBON
+static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16 variant,
+                                              ControlRef control,
+                                              ControlDefProcMessage msg,
+                                              SInt32 param)
+{
+    RgnHandle rgn;
+    Rect rect;
+    ListHandle list;
+    long ssfs;
+    Point mouse;
+    ListBounds bounds;
+    Point csize;
+    short savefont;
+    short savesize;
+    GrafPtr curport;
+
+    switch (msg) {
+      case initCntl:
+       rect = (*control)->contrlRect;
+       InsetRect(&rect, 4, 4);
+       rect.right -= 15; /* scroll bar */
+       bounds.top = bounds.bottom = bounds.left = 0;
+       bounds.right = 1;
+       csize.h = csize.v = 0;
+       GetPort(&curport);
+       savefont = curport->txFont;
+       savesize = curport->txSize;
+       ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
+       TextFont(HiWord(ssfs));
+       TextSize(LoWord(ssfs));
+       list = LNew(&rect, &bounds, csize, 0, (*control)->contrlOwner,
+                   TRUE, FALSE, FALSE, TRUE);
+       SetControlReference((*list)->vScroll, (long)list);
+       (*control)->contrlData = (Handle)list;
+       TextFont(savefont);
+       TextSize(savesize);
+       return noErr;
+      case dispCntl:
+       /*
+        * If the dialogue box is being destroyed, the scroll bar
+        * might have gone already.  In our situation, this is the
+        * only time we destroy a control, so NULL out the scroll bar
+        * handle to prevent LDispose trying to free it.
+        */
+       list = (ListHandle)(*control)->contrlData;
+       (*list)->vScroll = NULL;
+       LDispose(list);
+       return 0;
+      case drawCntl:
+       if ((*control)->contrlVis) {
+           rect = (*control)->contrlRect;
+           /* XXX input focus highlighting? */
+           InsetRect(&rect, 3, 3);
+           PenNormal();
+           FrameRect(&rect);
+           list = (ListHandle)(*control)->contrlData;
+           LActivate((*control)->contrlHilite != kControlInactivePart, list);
+           GetPort(&curport);
+           LUpdate(curport->visRgn, list);
+       }
+       return 0;
+      case testCntl:
+       mouse.h = LoWord(param);
+       mouse.v = HiWord(param);
+       rect = (*control)->contrlRect;
+       InsetRect(&rect, 4, 4);
+       /*
+        * We deliberately exclude the scrollbar so that LClick() can see it.
+        */
+       rect.right -= 15;
+       return PtInRect(mouse, &rect) ? kControlListBoxPart : kControlNoPart;
+      case calcCRgns:
+       if (param & (1 << 31)) {
+           param &= ~(1 << 31);
+           goto calcthumbrgn;
+       }
+       /* FALLTHROUGH */
+      case calcCntlRgn:
+       rgn = (RgnHandle)param;
+       RectRgn(rgn, &(*control)->contrlRect);
+       return 0;
+      case calcThumbRgn:
+      calcthumbrgn:
+       rgn = (RgnHandle)param;
+       SetEmptyRgn(rgn);
+       return 0;
+    }
+
+    return 0;
+}
+#endif
 
 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
                          struct mac_layoutstate *curstate,
@@ -895,10 +978,6 @@ static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
     assert(!ctrl->listbox.draglist);
     assert(!ctrl->listbox.multisel);
 
-    if (ctrl->listbox.label != NULL)
-       fprintf(stderr, "    label = %s\n", ctrl->listbox.label);
-    fprintf(stderr, "    percentwidth = %d\n", ctrl->listbox.percentwidth);
-
     mc->generic.type = MACCTRL_POPUP;
     mc->generic.ctrl = ctrl;
     mc->generic.privdata = NULL;
@@ -999,7 +1078,7 @@ void macctrl_activate(WindowPtr window, EventRecord *event)
 void macctrl_click(WindowPtr window, EventRecord *event)
 {
     Point mouse;
-    ControlHandle control;
+    ControlHandle control, oldfocus;
     int part, trackresult;
     GrafPtr saveport;
     union macctrl *mc;
@@ -1013,11 +1092,25 @@ void macctrl_click(WindowPtr window, EventRecord *event)
     GlobalToLocal(&mouse);
     part = FindControl(mouse, window, &control);
     if (control != NULL) {
-       mc = (union macctrl *)GetControlReference(control);
+#if !TARGET_API_MAC_CARBON
+       /*
+        * Special magic for scroll bars in list boxes, whose refcon
+        * is the list.
+        */
+       if (part == kControlUpButtonPart || part == kControlDownButtonPart ||
+           part == kControlPageUpPart || part == kControlPageDownPart ||
+           part == kControlIndicatorPart)
+           mc = (union macctrl *)
+               (*(ListHandle)GetControlReference(control))->refCon;
+       else
+#endif
+           mc = (union macctrl *)GetControlReference(control);
        if (mac_gestalts.apprvers >= 0x100) {
            if (GetControlFeatures(control, &features) == noErr &&
                (features & kControlSupportsFocus) &&
-               (features & kControlGetsFocusOnClick))
+               (features & kControlGetsFocusOnClick) &&
+               GetKeyboardFocus(window, &oldfocus) == noErr &&
+               control != oldfocus)
                SetKeyboardFocus(window, control, part);
            trackresult = HandleControlClick(control, mouse, event->modifiers,
                                             (ControlActionUPP)-1);
@@ -1031,6 +1124,18 @@ void macctrl_click(WindowPtr window, EventRecord *event)
                TEClick(mouse, !!(event->modifiers & shiftKey), te);
                goto done;
            }
+           if (mc->generic.type == MACCTRL_LISTBOX &&
+               (control == mc->listbox.tbctrl ||
+                control == (*mc->listbox.list)->vScroll)) {
+
+               macctrl_setfocus(mcs, mc);
+               if (LClick(mouse, event->modifiers, mc->listbox.list))
+                   /* double-click */
+                   ctrlevent(mcs, mc, EVENT_ACTION);
+               else
+                   ctrlevent(mcs, mc, EVENT_SELCHANGE);
+               goto done;
+           }
 #endif
            trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
        }
@@ -1108,7 +1213,14 @@ void macctrl_key(WindowPtr window, EventRecord *event)
            HandleControlKey(control, (event->message & keyCodeMask) >> 8,
                             event->message & charCodeMask, event->modifiers);
            mc = (union macctrl *)GetControlReference(control);
-           ctrlevent(mcs, mc, EVENT_VALCHANGE);
+           switch (mc->generic.type) {
+             case MACCTRL_LISTBOX:
+               ctrlevent(mcs, mc, EVENT_SELCHANGE);
+               break;
+             default:
+               ctrlevent(mcs, mc, EVENT_VALCHANGE);
+               break;
+           }
        }
     }
 #if !TARGET_API_MAC_CARBON
@@ -1426,7 +1538,6 @@ void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
     }
 #endif
     buffer[olen] = '\0';
-    fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
 }
 
 
@@ -1442,7 +1553,6 @@ static void dlg_macpopup_clear(union control *ctrl, void *dlg)
     unsigned int i, n;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      popup_clear\n");
     n = CountMenuItems(menu);
     for (i = 0; i < n; i++)
        DeleteMenuItem(menu, n - i);
@@ -1456,16 +1566,9 @@ static void dlg_maclist_clear(union control *ctrl, void *dlg)
 {
     struct macctrls *mcs = dlg;
     union macctrl *mc = findbyctrl(mcs, ctrl);
-    ListHandle list;
-    Size olen;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      maclist_clear\n");
-    if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                      kControlListBoxListHandleTag, sizeof(list), &list,
-                      &olen) != noErr)
-       return;
-    LDelRow(0, 0, list);
+    LDelRow(0, 0, mc->listbox.list);
     mc->listbox.nids = 0;
     sfree(mc->listbox.ids);
     mc->listbox.ids = NULL;
@@ -1475,10 +1578,14 @@ static void dlg_maclist_clear(union control *ctrl, void *dlg)
 void dlg_listbox_clear(union control *ctrl, void *dlg)
 {
 
-    if (ctrl->listbox.height == 0)
-       dlg_macpopup_clear(ctrl, dlg);
-    else
-       dlg_maclist_clear(ctrl, dlg);
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           dlg_macpopup_clear(ctrl, dlg);
+       else
+           dlg_maclist_clear(ctrl, dlg);
+       break;
+    }
 }
 
 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
@@ -1488,7 +1595,6 @@ static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
     MenuRef menu = mc->popup.menu;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      popup_del %d\n", index);
     DeleteMenuItem(menu, index + 1);
     if (mc->popup.ids != NULL)
        memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
@@ -1500,16 +1606,9 @@ static void dlg_maclist_del(union control *ctrl, void *dlg, int index)
 {
     struct macctrls *mcs = dlg;
     union macctrl *mc = findbyctrl(mcs, ctrl);
-    ListHandle list;
-    Size olen;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      maclist_del %d\n", index);
-    if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                      kControlListBoxListHandleTag, sizeof(list), &list,
-                      &olen) != noErr)
-       return;
-    LDelRow(1, index, list);
+    LDelRow(1, index, mc->listbox.list);
     if (mc->listbox.ids != NULL)
        memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1,
               (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids));
@@ -1519,10 +1618,14 @@ static void dlg_maclist_del(union control *ctrl, void *dlg, int index)
 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
 {
 
-    if (ctrl->listbox.height == 0)
-       dlg_macpopup_del(ctrl, dlg, index);
-    else
-       dlg_maclist_del(ctrl, dlg, index);
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           dlg_macpopup_del(ctrl, dlg, index);
+       else
+           dlg_maclist_del(ctrl, dlg, index);
+       break;
+    }
 }
 
 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
@@ -1533,7 +1636,6 @@ static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
     Str255 itemstring;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      popup_add %s\n", text);
     assert(text[0] != '\0');
     c2pstrcpy(itemstring, text);
     AppendMenu(menu, "\pdummy");
@@ -1546,35 +1648,32 @@ static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text)
 {
     struct macctrls *mcs = dlg;
     union macctrl *mc = findbyctrl(mcs, ctrl);
-    ListHandle list;
-    Size olen;
     ListBounds bounds;
     Cell cell = { 0, 0 };
 
     if (mc == NULL) return;
-    fprintf(stderr, "      maclist_add %s\n", text);
-    if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                      kControlListBoxListHandleTag, sizeof(list), &list,
-                      &olen) != noErr)
-       return;
 #if TARGET_API_MAC_CARBON
-    GetListDataBounds(list, &bounds);
+    GetListDataBounds(mc->listbox.list, &bounds);
 #else
-    bounds = (*list)->dataBounds;
+    bounds = (*mc->listbox.list)->dataBounds;
 #endif
     cell.v = bounds.bottom;
-    LAddRow(1, cell.v, list);
-    LSetCell(text, strlen(text), cell, list);
+    LAddRow(1, cell.v, mc->listbox.list);
+    LSetCell(text, strlen(text), cell, mc->listbox.list);
     DrawOneControl(mc->listbox.tbctrl);
 }
 
 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
 {
 
-    if (ctrl->listbox.height == 0)
-       dlg_macpopup_add(ctrl, dlg, text);
-    else
-       dlg_maclist_add(ctrl, dlg, text);
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           dlg_macpopup_add(ctrl, dlg, text);
+       else
+           dlg_maclist_add(ctrl, dlg, text);
+       break;
+    }
 }
 
 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
@@ -1586,7 +1685,6 @@ static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
     unsigned int index;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      popup_addwthid %s, %d\n", text, id);
     dlg_macpopup_add(ctrl, dlg, text);
     index = CountMenuItems(menu) - 1;
     if (mc->popup.nids <= index) {
@@ -1601,22 +1699,15 @@ static void dlg_maclist_addwithid(union control *ctrl, void *dlg,
 {
     struct macctrls *mcs = dlg;
     union macctrl *mc = findbyctrl(mcs, ctrl);
-    ListHandle list;
-    Size olen;
     ListBounds bounds;
     int index;
 
     if (mc == NULL) return;
-    fprintf(stderr, "      maclist_addwithid %s %d\n", text, id);
     dlg_maclist_add(ctrl, dlg, text);
-    if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                      kControlListBoxListHandleTag, sizeof(list), &list,
-                      &olen) != noErr)
-       return;
 #if TARGET_API_MAC_CARBON
-    GetListDataBounds(list, &bounds);
+    GetListDataBounds(mc->listbox.list, &bounds);
 #else
-    bounds = (*list)->dataBounds;
+    bounds = (*mc->listbox.list)->dataBounds;
 #endif
     index = bounds.bottom;
     if (mc->listbox.nids <= index) {
@@ -1630,10 +1721,14 @@ void dlg_listbox_addwithid(union control *ctrl, void *dlg,
                           char const *text, int id)
 {
 
-    if (ctrl->listbox.height == 0)
-       dlg_macpopup_addwithid(ctrl, dlg, text, id);
-    else
-       dlg_maclist_addwithid(ctrl, dlg, text, id);
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           dlg_macpopup_addwithid(ctrl, dlg, text, id);
+       else
+           dlg_maclist_addwithid(ctrl, dlg, text, id);
+       break;
+    }
 }
 
 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
@@ -1642,69 +1737,57 @@ int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
     union macctrl *mc = findbyctrl(mcs, ctrl);
 
     assert(mc != NULL);
-    if (ctrl->listbox.height == 0) {
-       assert(mc->popup.ids != NULL && mc->popup.nids > index);
-       return mc->popup.ids[index];
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0) {
+           assert(mc->popup.ids != NULL && mc->popup.nids > index);
+           return mc->popup.ids[index];
+       } else {
+           assert(mc->listbox.ids != NULL && mc->listbox.nids > index);
+           return mc->listbox.ids[index];
+       }
     }
-    return 0;
-}
-
-static int dlg_maclist_index(union control *ctrl, void *dlg)
-{
-    struct macctrls *mcs = dlg;
-    union macctrl *mc = findbyctrl(mcs, ctrl);
-    ListHandle list;
-    Size olen;
-    Cell cell = { 0, 0 };
-
-    if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                      kControlListBoxListHandleTag, sizeof(list), &list,
-                      &olen) != noErr)
-       return -1;
-    if (LGetSelect(TRUE, &cell, list))
-       return cell.v;
-    else
-       return -1;
+    return -1;
 }
 
 int dlg_listbox_index(union control *ctrl, void *dlg)
 {
     struct macctrls *mcs = dlg;
-    union macctrl *mc;
-
-    if (ctrl->listbox.height == 0) {
-       mc  = findbyctrl(mcs, ctrl);
-       assert(mc != NULL);
-       return GetControlValue(mc->popup.tbctrl) - 1;
-    } else
-       return dlg_maclist_index(ctrl, dlg);
-}
-
-static int dlg_maclist_issel(union control *ctrl, void *dlg, int index)
-{
-    struct macctrls *mcs = dlg;
     union macctrl *mc = findbyctrl(mcs, ctrl);
-    ListHandle list;
-    Size olen;
     Cell cell = { 0, 0 };
 
-    if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
-                      kControlListBoxListHandleTag, sizeof(list), &list,
-                      &olen) != noErr)
-       return FALSE;
-    cell.v = index;
-    return LGetSelect(FALSE, &cell, list);
+    assert(mc != NULL);
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           return GetControlValue(mc->popup.tbctrl) - 1;
+       else {
+           if (LGetSelect(TRUE, &cell, mc->listbox.list))
+               return cell.v;
+           else
+               return -1;
+       }
+    }
+    return -1;
 }
 
 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
 {
     struct macctrls *mcs = dlg;
     union macctrl *mc = findbyctrl(mcs, ctrl);
+    Cell cell = { 0, 0 };
 
-    if (ctrl->listbox.height == 0)
-       return GetControlValue(mc->popup.tbctrl) - 1 == index;
-    else
-       return dlg_maclist_issel(ctrl, dlg, index);
+    assert(mc != NULL);
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           return GetControlValue(mc->popup.tbctrl) - 1 == index;
+       else {
+           cell.v = index;
+           return LGetSelect(FALSE, &cell, mc->listbox.list);
+       }
+    }
+    return FALSE;
 }
 
 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
@@ -1713,9 +1796,13 @@ void dlg_listbox_select(union control *ctrl, void *dlg, int index)
     union macctrl *mc = findbyctrl(mcs, ctrl);
 
     if (mc == NULL) return;
-    if (ctrl->listbox.height == 0)
-       SetControlValue(mc->popup.tbctrl, index + 1);
-};
+    switch (ctrl->generic.type) {
+      case CTRL_LISTBOX:
+       if (ctrl->listbox.height == 0)
+           SetControlValue(mc->popup.tbctrl, index + 1);
+       break;
+    }
+}
 
 
 /*
@@ -1799,14 +1886,34 @@ void printer_finish_enum(printer_enum *pe)
 void dlg_coloursel_start(union control *ctrl, void *dlg,
                         int r, int g, int b)
 {
-
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
+    Point where = {-1, -1}; /* Screen with greatest colour depth */
+    RGBColor incolour;
+
+    if (HAVE_COLOR_QD()) {
+       incolour.red = r * 0x0101;
+       incolour.green = g * 0x0101;
+       incolour.blue = b * 0x0101;
+       mcs->gotcolour = GetColor(where, "\pModify Colour:", &incolour,
+                                 &mcs->thecolour);
+       ctrlevent(mcs, mc, EVENT_CALLBACK);
+    } else
+       dlg_beep(dlg);
 }
 
 int dlg_coloursel_results(union control *ctrl, void *dlg,
                          int *r, int *g, int *b)
 {
+    struct macctrls *mcs = dlg;
 
-    return 0;
+    if (mcs->gotcolour) {
+       *r = mcs->thecolour.red >> 8;
+       *g = mcs->thecolour.green >> 8;
+       *b = mcs->thecolour.blue >> 8;
+       return 1;
+    } else
+       return 0;
 }
 
 /*