Slightly improve radio button layout to more closely match the Mac OS 8 HI
[sgt/putty] / mac / macctrls.c
index 7015c67..f86efe6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macctrls.c,v 1.7 2003/03/20 23:15:25 ben Exp $ */
+/* $Id: macctrls.c,v 1.10 2003/03/24 21:55:51 ben Exp $ */
 /*
  * Copyright (c) 2003 Ben Harris
  * All rights reserved.
 #include <Appearance.h>
 #include <Controls.h>
 #include <ControlDefinitions.h>
+#include <Menus.h>
 #include <Resources.h>
 #include <Sound.h>
 #include <TextUtils.h>
 #include <Windows.h>
 
 #include <assert.h>
+#include <string.h>
 
 #include "putty.h"
 #include "mac.h"
 #include "dialog.h"
 #include "tree234.h"
 
+/* Range of menu IDs for popup menus */
+#define MENU_MIN       1024
+#define MENU_MAX       2048
+
+
 union macctrl {
     struct macctrl_generic {
        enum {
            MACCTRL_TEXT,
            MACCTRL_RADIO,
            MACCTRL_CHECKBOX,
-           MACCTRL_BUTTON
+           MACCTRL_BUTTON,
+           MACCTRL_POPUP
        } type;
        /* Template from which this was generated */
        union control *ctrl;
+       /* Next control in this panel */
+       union macctrl *next;
     } generic;
     struct {
        struct macctrl_generic generic;
@@ -69,11 +79,20 @@ union macctrl {
        struct macctrl_generic generic;
        ControlRef tbctrl;
     } button;
+    struct {
+       struct macctrl_generic generic;
+       ControlRef tbctrl;
+       MenuRef menu;
+       int menuid;
+       unsigned int nids;
+       int *ids;
+    } popup;
 };
 
 struct mac_layoutstate {
     Point pos;
     unsigned int width;
+    unsigned int panelnum;
 };
 
 #define ctrlevent(mcs, mc, event) do {                                 \
@@ -87,6 +106,7 @@ struct mac_layoutstate {
 
 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, 
                              WindowPtr, struct macctrls *);
+static void macctrl_switchtopanel(struct macctrls *, unsigned int);
 static void macctrl_text(struct macctrls *, WindowPtr,
                         struct mac_layoutstate *, union control *);
 static void macctrl_radio(struct macctrls *, WindowPtr,
@@ -95,6 +115,8 @@ static void macctrl_checkbox(struct macctrls *, WindowPtr,
                             struct mac_layoutstate *, union control *);
 static void macctrl_button(struct macctrls *, WindowPtr,
                           struct mac_layoutstate *, union control *);
+static void macctrl_popup(struct macctrls *, WindowPtr,
+                         struct mac_layoutstate *, union control *);
 #if !TARGET_API_MAC_CARBON
 static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef,
                                            ControlDefProcMessage, SInt32);
@@ -174,13 +196,29 @@ void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
     rect = window->portRect;
 #endif
     curstate.pos.h = rect.left + 13;
-    curstate.pos.v = rect.top + 13;
+    curstate.pos.v = rect.bottom - 59;
     curstate.width = rect.right - rect.left - (13 * 2);
     if (mac_gestalts.apprvers >= 0x100)
        CreateRootControl(window, &root);
     mcs->byctrl = newtree234(macctrl_cmp_byctrl);
-    for (i = 0; i < cb->nctrlsets; i++)
+    /* Count the number of panels */
+    mcs->npanels = 1;
+    for (i = 1; i < cb->nctrlsets; i++)
+       if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
+           mcs->npanels++;
+    mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels);
+    memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
+    curstate.panelnum = 0;
+    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);
+       }
        macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
+    }
+    macctrl_switchtopanel(mcs, 2);
 }
 
 static void macctrl_layoutset(struct mac_layoutstate *curstate,
@@ -228,11 +266,52 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate,
          case CTRL_BUTTON:
            macctrl_button(mcs, window, curstate, ctrl);
            break;
-
+         case CTRL_LISTBOX:
+           if (ctrl->listbox.height == 0)
+               macctrl_popup(mcs, window, curstate, ctrl);
+           break;
        }
     }
 }
 
+static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
+{
+    unsigned int i, j;
+    union macctrl *mc;
+
+    /* Panel 0 is special and always visible. */
+    for (i = 1; i < mcs->npanels; i++)
+       for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next)
+           switch (mc->generic.type) {
+             case MACCTRL_TEXT:
+               if (i == which)
+                   ShowControl(mc->text.tbctrl);
+               else
+                   HideControl(mc->text.tbctrl);
+               break;
+             case MACCTRL_RADIO:
+               for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
+                   if (i == which)
+                       ShowControl(mc->radio.tbctrls[j]);
+                   else
+                       HideControl(mc->radio.tbctrls[j]);
+               break;
+             case MACCTRL_CHECKBOX:
+               if (i == which)
+                   ShowControl(mc->checkbox.tbctrl);
+               else
+                   HideControl(mc->checkbox.tbctrl);
+               break;
+             case MACCTRL_BUTTON:
+               if (i == which)
+                   ShowControl(mc->button.tbctrl);
+               else
+                   HideControl(mc->button.tbctrl);
+               break;
+
+           }
+}
+
 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
                         struct mac_layoutstate *curstate,
                         union control *ctrl)
@@ -270,6 +349,8 @@ static void macctrl_text(struct macctrls *mcs, WindowPtr window,
                                     SYS7_TEXT_PROC, (long)mc);
     }
     add234(mcs->byctrl, mc);
+    mc->generic.next = mcs->panels[curstate->panelnum];
+    mcs->panels[curstate->panelnum] = mc;
 }
 
 #if !TARGET_API_MAC_CARBON
@@ -322,20 +403,23 @@ static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
     colwidth = (curstate->width + 13) /        ctrl->radio.ncolumns;
     for (i = 0; i < ctrl->radio.nbuttons; i++) {
        fprintf(stderr, "    button = %s\n", ctrl->radio.buttons[i]);
-       bounds.top = curstate->pos.v;
-       bounds.bottom = bounds.top + 16;
+       bounds.top = curstate->pos.v - 2;
+       bounds.bottom = bounds.top + 18;
        bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
        if (i == ctrl->radio.nbuttons - 1 ||
            i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
            bounds.right = curstate->pos.h + curstate->width;
-           curstate->pos.v += 22;
+           curstate->pos.v += 18;
        } else
            bounds.right = bounds.left + colwidth - 13;
        c2pstrcpy(title, ctrl->radio.buttons[i]);
        mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
                                          0, 0, 1, radioButProc, (long)mc);
     }
+    curstate->pos.v += 4;
     add234(mcs->byctrl, mc);
+    mc->generic.next = mcs->panels[curstate->panelnum];
+    mcs->panels[curstate->panelnum] = mc;
     ctrlevent(mcs, mc, EVENT_REFRESH);
 }
 
@@ -359,6 +443,8 @@ static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
                                     checkBoxProc, (long)mc);
     add234(mcs->byctrl, mc);
     curstate->pos.v += 22;
+    mc->generic.next = mcs->panels[curstate->panelnum];
+    mcs->panels[curstate->panelnum] = mc;
     ctrlevent(mcs, mc, EVENT_REFRESH);
 }
 
@@ -401,6 +487,8 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window,
                       sizeof(iscancel), &iscancel);
     }
     add234(mcs->byctrl, mc);
+    mc->generic.next = mcs->panels[curstate->panelnum];
+    mcs->panels[curstate->panelnum] = mc;
     curstate->pos.v += 26;
 }
 
@@ -445,6 +533,63 @@ static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
 }
 #endif
 
+static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
+                         struct mac_layoutstate *curstate,
+                         union control *ctrl)
+{
+    union macctrl *mc = smalloc(sizeof *mc);
+    Rect bounds;
+    Str255 title;
+    unsigned int labelwidth;
+    static int nextmenuid;
+    int menuid;
+    MenuRef menu;
+
+    /* 
+     * <http://developer.apple.com/qa/tb/tb42.html> explains how to
+     * create a popup menu with dynamic content.
+     */
+    assert(ctrl->listbox.height == 0);
+    assert(!ctrl->listbox.draglist);
+    assert(!ctrl->listbox.multisel);
+
+    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;
+    c2pstrcpy(title, ctrl->button.label);
+
+    /* Find a spare menu ID and create the menu */
+    while (GetMenuHandle(nextmenuid) != NULL)
+       if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
+    menuid = nextmenuid++;
+    menu = NewMenu(menuid, "\pdummy");
+    if (menu == NULL) return;
+    mc->popup.menu = menu;
+    mc->popup.menuid = menuid;
+    InsertMenu(menu, kInsertHierarchicalMenu);
+
+    /* The menu starts off empty */
+    mc->popup.nids = 0;
+    mc->popup.ids = NULL;
+
+    bounds.left = curstate->pos.h;
+    bounds.right = bounds.left + curstate->width;
+    bounds.top = curstate->pos.v;
+    bounds.bottom = bounds.top + 20;
+    /* XXX handle percentwidth == 100 */
+    labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
+    mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
+                                 popupTitleLeftJust, menuid, labelwidth,
+                                 popupMenuProc + popupFixedWidth, (long)mc);
+    add234(mcs->byctrl, mc);
+    curstate->pos.v += 26;
+    mc->generic.next = mcs->panels[curstate->panelnum];
+    mcs->panels[curstate->panelnum] = mc;
+    ctrlevent(mcs, mc, EVENT_REFRESH);
+}
+
 
 void macctrl_activate(WindowPtr window, EventRecord *event)
 {
@@ -485,30 +630,36 @@ void macctrl_click(WindowPtr window, EventRecord *event)
     mouse = event->where;
     GlobalToLocal(&mouse);
     part = FindControl(mouse, window, &control);
-    if (control != NULL)
-       if (TrackControl(control, mouse, NULL) != 0) {
-           mc = (union macctrl *)GetControlReference(control);
-           switch (mc->generic.type) {
-             case MACCTRL_RADIO:
-               for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++) {
+    if (control != NULL) {
+       mc = (union macctrl *)GetControlReference(control);
+       switch (mc->generic.type) {
+         case MACCTRL_POPUP:
+           TrackControl(control, mouse, (ControlActionUPP)-1);
+           ctrlevent(mcs, mc, EVENT_SELCHANGE);
+         case MACCTRL_RADIO:
+           if (TrackControl(control, mouse, NULL) != 0) {
+               for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
                    if (mc->radio.tbctrls[i] == control)
                        SetControlValue(mc->radio.tbctrls[i],
                                        kControlRadioButtonCheckedValue);
                    else
                        SetControlValue(mc->radio.tbctrls[i],
                                        kControlRadioButtonUncheckedValue);
-               }
                ctrlevent(mcs, mc, EVENT_VALCHANGE);
-               break;
-             case MACCTRL_CHECKBOX:
+           }
+           break;
+         case MACCTRL_CHECKBOX:
+           if (TrackControl(control, mouse, NULL) != 0) {
                SetControlValue(control, !GetControlValue(control));
                ctrlevent(mcs, mc, EVENT_VALCHANGE);
-               break;
-             case MACCTRL_BUTTON:
-               ctrlevent(mcs, mc, EVENT_ACTION);
-               break;
            }
+           break;
+         case MACCTRL_BUTTON:
+           if (TrackControl(control, mouse, NULL) != 0)
+               ctrlevent(mcs, mc, EVENT_ACTION);
+           break;
        }
+    }
     SetPort(saveport);
 }
 
@@ -567,19 +718,29 @@ void macctrl_close(WindowPtr window)
     struct macctrls *mcs = mac_winctrls(window);
     union macctrl *mc;
 
+    /*
+     * Mostly, we don't bother disposing of the Toolbox controls,
+     * since that will happen automatically when the window is
+     * disposed of.  Popup menus are an exception, because we have to
+     * dispose of the menu ourselves, and doing that while the control
+     * still holds a reference to it seems rude.
+     */
     while ((mc = index234(mcs->byctrl, 0)) != NULL) {
+       switch (mc->generic.type) {
+         case MACCTRL_POPUP:
+           DisposeControl(mc->popup.tbctrl);
+           DeleteMenu(mc->popup.menuid);
+           DisposeMenu(mc->popup.menu);
+           break;
+       }
        del234(mcs->byctrl, mc);
        sfree(mc);
     }
 
     freetree234(mcs->byctrl);
     mcs->byctrl = NULL;
-
-/* XXX
-    DisposeWindow(window);
-    if (s->window == NULL)
-       sfree(s);
-*/
+    sfree(mcs->panels);
+    mcs->panels = NULL;
 }
 
 void dlg_update_start(union control *ctrl, void *dlg)
@@ -736,48 +897,139 @@ void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
  * List Box control
  */
 
+static void dlg_macpopup_clear(union control *ctrl, void *dlg)
+{
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
+    MenuRef menu = mc->popup.menu;
+    unsigned int i, n;
+
+    fprintf(stderr, "      popup_clear\n");
+    n = CountMItems(menu);
+    for (i = 0; i < n; i++)
+       DeleteMenuItem(menu, n - i);
+    mc->popup.nids = 0;
+    sfree(mc->popup.ids);
+    mc->popup.ids = NULL;
+    SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
+}
+
 void dlg_listbox_clear(union control *ctrl, void *dlg)
 {
 
-};
+    if (ctrl->listbox.height == 0)
+       dlg_macpopup_clear(ctrl, dlg);
+}
+
+static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
+{
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
+    MenuRef menu = mc->popup.menu;
+
+    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,
+              (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
+    SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
+}
 
 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
 {
 
-};
+    if (ctrl->listbox.height == 0)
+       dlg_macpopup_del(ctrl, dlg, index);
+}
+
+static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
+{
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
+    MenuRef menu = mc->popup.menu;
+    Str255 itemstring;
+
+    fprintf(stderr, "      popup_add %s\n", text);
+    assert(text[0] != '\0');
+    c2pstrcpy(itemstring, text);
+    AppendMenu(menu, "\pdummy");
+    SetMenuItemText(menu, CountMItems(menu), itemstring);
+    SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
+}
 
 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
 {
 
-};
+    if (ctrl->listbox.height == 0)
+       dlg_macpopup_add(ctrl, dlg, text);
+}
+
+static void dlg_macpopup_addwithindex(union control *ctrl, void *dlg,
+                                     char const *text, int id)
+{
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
+    MenuRef menu = mc->popup.menu;
+    unsigned int index;
+
+    fprintf(stderr, "      popup_addwthindex %s, %d\n", text, id);
+    dlg_macpopup_add(ctrl, dlg, text);
+    index = CountMItems(menu) - 1;
+    if (mc->popup.nids <= index) {
+       mc->popup.nids = index + 1;
+       mc->popup.ids = srealloc(mc->popup.ids,
+                                mc->popup.nids * sizeof(*mc->popup.ids));
+    }
+    mc->popup.ids[index] = id;
+}
 
 void dlg_listbox_addwithindex(union control *ctrl, void *dlg,
                              char const *text, int id)
 {
 
-};
+    if (ctrl->listbox.height == 0)
+       dlg_macpopup_addwithindex(ctrl, dlg, text, id);
+}
 
 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
 {
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
 
+    if (ctrl->listbox.height == 0) {
+       assert(mc->popup.ids != NULL && mc->popup.nids > index);
+       return mc->popup.ids[index];
+    }
     return 0;
-};
+}
 
 int dlg_listbox_index(union control *ctrl, void *dlg)
 {
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
 
+    if (ctrl->listbox.height == 0)
+       return GetControlValue(mc->popup.tbctrl) - 1;
     return 0;
 };
 
 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
 {
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
 
+    if (ctrl->listbox.height == 0)
+       return GetControlValue(mc->popup.tbctrl) - 1 == index;
     return 0;
 };
 
 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
 {
+    struct macctrls *mcs = dlg;
+    union macctrl *mc = findbyctrl(mcs, ctrl);
 
+    if (ctrl->listbox.height == 0)
+       SetControlValue(mc->popup.tbctrl, index + 1);
 };