Missing assert.
[sgt/putty] / windows / winctrls.c
index 9e8e0e7..72d462e 100644 (file)
@@ -448,6 +448,8 @@ char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
 
     if (lines) *lines = nlines;
 
+    sfree(pwidths);
+
     return ret;
 }
 
@@ -1291,6 +1293,7 @@ void winctrl_remove(struct winctrls *wc, struct winctrl *c)
 {
     struct winctrl *ret;
     ret = del234(wc->byctrl, c);
+    assert(ret == c);
     ret = del234(wc->byid, c);
     assert(ret == c);
 }
@@ -1639,8 +1642,8 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
            shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
            statictext(&pos, escaped, 1, base_id);
            staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
+            data = fontspec_new("", 0, 0, 0);
            sfree(escaped);
-           data = snew(FontSpec);
            break;
          default:
            assert(!"Can't happen");
@@ -1665,7 +1668,9 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
            winctrl_add_shortcuts(dp, c);
            if (actual_base_id == base_id)
                base_id += num_ids;
-       }
+       } else {
+            sfree(data);
+        }
 
        if (colstart >= 0) {
            /*
@@ -1934,38 +1939,36 @@ int winctrl_handle_command(struct dlgparam *dp, UINT msg,
            CHOOSEFONT cf;
            LOGFONT lf;
            HDC hdc;
-           FontSpec fs = *(FontSpec *)c->data;
-           
+           FontSpec *fs = (FontSpec *)c->data;
+
            hdc = GetDC(0);
-           lf.lfHeight = -MulDiv(fs.height,
+           lf.lfHeight = -MulDiv(fs->height,
                                  GetDeviceCaps(hdc, LOGPIXELSY), 72);
            ReleaseDC(0, hdc);
            lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
            lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
-           lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
-           lf.lfCharSet = fs.charset;
+           lf.lfWeight = (fs->isbold ? FW_BOLD : 0);
+           lf.lfCharSet = fs->charset;
            lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
            lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
            lf.lfQuality = DEFAULT_QUALITY;
            lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
-           strncpy(lf.lfFaceName, fs.name,
+           strncpy(lf.lfFaceName, fs->name,
                    sizeof(lf.lfFaceName) - 1);
            lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
 
            cf.lStructSize = sizeof(cf);
            cf.hwndOwner = dp->hwnd;
            cf.lpLogFont = &lf;
-           cf.Flags = CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST |
-               CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
+           cf.Flags = (dp->fixed_pitch_fonts ? CF_FIXEDPITCHONLY : 0) |
+                CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
 
            if (ChooseFont(&cf)) {
-               strncpy(fs.name, lf.lfFaceName,
-                       sizeof(fs.name) - 1);
-               fs.name[sizeof(fs.name) - 1] = '\0';
-               fs.isbold = (lf.lfWeight == FW_BOLD);
-               fs.charset = lf.lfCharSet;
-               fs.height = cf.iPointSize / 10;
+                fs = fontspec_new(lf.lfFaceName, (lf.lfWeight == FW_BOLD),
+                                  cf.iPointSize / 10, lf.lfCharSet);
                dlg_fontsel_set(ctrl, dp, fs);
+                fontspec_free(fs);
+
                ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
            }
        }
@@ -2100,13 +2103,12 @@ void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
     SetDlgItemText(dp->hwnd, c->base_id+1, text);
 }
 
-void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
+char *dlg_editbox_get(union control *ctrl, void *dlg)
 {
     struct dlgparam *dp = (struct dlgparam *)dlg;
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
     assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
-    GetDlgItemText(dp->hwnd, c->base_id+1, buffer, length);
-    buffer[length-1] = '\0';
+    return GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
 }
 
 /* The `listbox' functions can also apply to combo boxes. */
@@ -2194,8 +2196,13 @@ int dlg_listbox_index(union control *ctrl, void *dlg)
     struct dlgparam *dp = (struct dlgparam *)dlg;
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
     int msg, ret;
-    assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
-          !c->ctrl->listbox.multisel);
+    assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
+    if (c->ctrl->listbox.multisel) {
+       assert(c->ctrl->listbox.height != 0); /* not combo box */
+       ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSELCOUNT, 0, 0);
+       if (ret == LB_ERR || ret > 1)
+           return -1;
+    }
     msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
     ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
     if (ret == LB_ERR)
@@ -2281,49 +2288,56 @@ void dlg_label_change(union control *ctrl, void *dlg, char const *text)
     }
 }
 
-void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
+void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
 {
     struct dlgparam *dp = (struct dlgparam *)dlg;
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
     assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
-    SetDlgItemText(dp->hwnd, c->base_id+1, fn.path);
+    SetDlgItemText(dp->hwnd, c->base_id+1, fn->path);
 }
 
-void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
+Filename *dlg_filesel_get(union control *ctrl, void *dlg)
 {
     struct dlgparam *dp = (struct dlgparam *)dlg;
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+    char *tmp;
+    Filename *ret;
     assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
-    GetDlgItemText(dp->hwnd, c->base_id+1, fn->path, lenof(fn->path));
-    fn->path[lenof(fn->path)-1] = '\0';
+    tmp = GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
+    ret = filename_from_str(tmp);
+    sfree(tmp);
+    return ret;
 }
 
-void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
+void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
 {
     char *buf, *boldstr;
     struct dlgparam *dp = (struct dlgparam *)dlg;
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
     assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
 
-    *(FontSpec *)c->data = fs;        /* structure copy */
+    fontspec_free((FontSpec *)c->data);
+    c->data = fontspec_copy(fs);
 
-    boldstr = (fs.isbold ? "bold, " : "");
-    if (fs.height == 0)
-       buf = dupprintf("Font: %s, %sdefault height", fs.name, boldstr);
+    boldstr = (fs->isbold ? "bold, " : "");
+    if (fs->height == 0)
+       buf = dupprintf("Font: %s, %sdefault height", fs->name, boldstr);
     else
-       buf = dupprintf("Font: %s, %s%d-%s", fs.name, boldstr,
-                       (fs.height < 0 ? -fs.height : fs.height),
-                       (fs.height < 0 ? "pixel" : "point"));
+       buf = dupprintf("Font: %s, %s%d-%s", fs->name, boldstr,
+                       (fs->height < 0 ? -fs->height : fs->height),
+                       (fs->height < 0 ? "pixel" : "point"));
     SetDlgItemText(dp->hwnd, c->base_id+1, buf);
     sfree(buf);
+
+    dlg_auto_set_fixed_pitch_flag(dp);
 }
 
-void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
+FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
 {
     struct dlgparam *dp = (struct dlgparam *)dlg;
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
     assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
-    *fs = *(FontSpec *)c->data;               /* structure copy */
+    return fontspec_copy((FontSpec *)c->data);
 }
 
 /*
@@ -2357,6 +2371,8 @@ void dlg_set_focus(union control *ctrl, void *dlg)
     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
     int id;
     HWND ctl;
+    if (!c)
+        return;
     switch (ctrl->generic.type) {
       case CTRL_EDITBOX: id = c->base_id + 1; break;
       case CTRL_RADIO:
@@ -2461,6 +2477,62 @@ int dlg_coloursel_results(union control *ctrl, void *dlg,
        return 0;
 }
 
+void dlg_auto_set_fixed_pitch_flag(void *dlg)
+{
+    struct dlgparam *dp = (struct dlgparam *)dlg;
+    Conf *conf = (Conf *)dp->data;
+    FontSpec *fs;
+    int quality;
+    HFONT hfont;
+    HDC hdc;
+    TEXTMETRIC tm;
+    int is_var;
+
+    /*
+     * Attempt to load the current font, and see if it's
+     * variable-pitch. If so, start off the fixed-pitch flag for the
+     * dialog box as false.
+     *
+     * We assume here that any client of the dlg_* mechanism which is
+     * using font selectors at all is also using a normal 'Conf *'
+     * as dp->data.
+     */
+
+    quality = conf_get_int(conf, CONF_font_quality);
+    fs = conf_get_fontspec(conf, CONF_font);
+
+    hfont = CreateFont(0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
+                       DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
+                       CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
+                       FIXED_PITCH | FF_DONTCARE, fs->name);
+    hdc = GetDC(NULL);
+    if (hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
+        /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
+        is_var = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH);
+    } else {
+        is_var = FALSE;                /* assume it's basically normal */
+    }
+    if (hdc)
+        ReleaseDC(NULL, hdc);
+    if (hfont)
+        DeleteObject(hfont);
+
+    if (is_var)
+        dp->fixed_pitch_fonts = FALSE;
+}
+
+int dlg_get_fixed_pitch_flag(void *dlg)
+{
+    struct dlgparam *dp = (struct dlgparam *)dlg;
+    return dp->fixed_pitch_fonts;
+}
+
+void dlg_set_fixed_pitch_flag(void *dlg, int flag)
+{
+    struct dlgparam *dp = (struct dlgparam *)dlg;
+    dp->fixed_pitch_fonts = flag;
+}
+
 struct perctrl_privdata {
     union control *ctrl;
     void *data;
@@ -2488,6 +2560,7 @@ void dp_init(struct dlgparam *dp)
     dp->hwnd = NULL;
     dp->wintitle = dp->errtitle = NULL;
     dp->privdata = newtree234(perctrl_privdata_cmp);
+    dp->fixed_pitch_fonts = TRUE;
 }
 
 void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)