olefont.c 64.7 KB
Newer Older
1 2 3 4 5 6 7
/*
 * OLE Font encapsulation implementation
 *
 * This file contains an implementation of the IFont
 * interface and the OleCreateFontIndirect API call.
 *
 * Copyright 1999 Francis Beaudet
8
 * Copyright 2006 (Google) Benjamin Arai
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24
 */
#include <assert.h>
25
#include <stdarg.h>
26
#include <string.h>
27

28
#define COBJMACROS
29 30
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
31

32
#include "winerror.h"
33
#include "windef.h"
34 35
#include "winbase.h"
#include "wingdi.h"
36
#include "winuser.h"
37
#include "wine/list.h"
38
#include "wine/unicode.h"
39
#include "objbase.h"
40
#include "oleauto.h"    /* for SysAllocString(....) */
41
#include "ole2.h"
42
#include "olectl.h"
43
#include "wine/debug.h"
44
#include "connpt.h" /* for CreateConnectionPoint */
45
#include "oaidl.h"
46

47
WINE_DEFAULT_DEBUG_CHANNEL(ole);
48

49
/***********************************************************************
50 51 52 53 54 55
 * Declaration of constants used when serializing the font object.
 */
#define FONTPERSIST_ITALIC        0x02
#define FONTPERSIST_UNDERLINE     0x04
#define FONTPERSIST_STRIKETHROUGH 0x08

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/***********************************************************************
 * List of the HFONTs it has given out, with each one having a separate
 * ref count.
 */
typedef struct _HFONTItem
{
  struct list entry;

  /* Reference count for that instance of the class. */
  LONG ref;

  /* Contain the font associated with this object. */
  HFONT gdiFont;

} HFONTItem, *PHFONTItem;

static struct list OLEFontImpl_hFontList = LIST_INIT(OLEFontImpl_hFontList);

/* Counts how many fonts contain at least one lock */
static LONG ifont_cnt = 0;

/***********************************************************************
 * Critical section for OLEFontImpl_hFontList
 */
static CRITICAL_SECTION OLEFontImpl_csHFONTLIST;
static CRITICAL_SECTION_DEBUG OLEFontImpl_csHFONTLIST_debug =
{
  0, 0, &OLEFontImpl_csHFONTLIST,
  { &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList,
    &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList },
    0, 0, { (DWORD_PTR)(__FILE__ ": OLEFontImpl_csHFONTLIST") }
};
static CRITICAL_SECTION OLEFontImpl_csHFONTLIST = { &OLEFontImpl_csHFONTLIST_debug, -1, 0, 0, 0, 0 };

static void HFONTItem_Delete(PHFONTItem item)
{
  DeleteObject(item->gdiFont);
  list_remove(&item->entry);
  HeapFree(GetProcessHeap(), 0, item);
}

98 99
/***********************************************************************
 * Declaration of the implementation class for the IFont interface
100 101 102 103 104 105
 */
typedef struct OLEFontImpl OLEFontImpl;

struct OLEFontImpl
{
  /*
106
   * This class supports many interfaces. IUnknown, IFont,
107 108 109
   * IDispatch, IDispFont IPersistStream and IConnectionPointContainer.
   * The first two are supported by the first vtable, the next two are
   * supported by the second table and the last two have their own.
110
   */
111 112 113 114 115 116
  const IFontVtbl*                     lpVtbl;
  const IDispatchVtbl*                 lpvtblIDispatch;
  const IPersistStreamVtbl*            lpvtblIPersistStream;
  const IConnectionPointContainerVtbl* lpvtblIConnectionPointContainer;
  const IPersistPropertyBagVtbl*       lpvtblIPersistPropertyBag;
  const IPersistStreamInitVtbl*        lpvtblIPersistStreamInit;
117 118 119
  /*
   * Reference count for that instance of the class.
   */
120
  LONG ref;
121 122 123 124 125

  /*
   * This structure contains the description of the class.
   */
  FONTDESC description;
126 127 128 129 130 131 132 133 134 135 136

  /*
   * Contain the font associated with this object.
   */
  HFONT gdiFont;

  /*
   * Size ratio
   */
  long cyLogical;
  long cyHimetric;
137

138 139
  IConnectionPoint *pPropertyNotifyCP;
  IConnectionPoint *pFontEventsCP;
140 141 142
};

/*
143
 * Here, I define utility macros to help with the casting of the
144
 * "this" parameter.
145
 * There is a version to accommodate all of the VTables implemented
146
 * by this object.
147
 */
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

static inline OLEFontImpl *impl_from_IDispatch( IDispatch *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIDispatch));
}

static inline OLEFontImpl *impl_from_IPersistStream( IPersistStream *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIPersistStream));
}

static inline OLEFontImpl *impl_from_IConnectionPointContainer( IConnectionPointContainer *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIConnectionPointContainer));
}

static inline OLEFontImpl *impl_from_IPersistPropertyBag( IPersistPropertyBag *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIPersistPropertyBag));
}

static inline OLEFontImpl *impl_from_IPersistStreamInit( IPersistStreamInit *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIPersistStreamInit));
}
173

174 175 176 177 178

/***********************************************************************
 * Prototypes for the implementation functions for the IFont
 * interface
 */
179
static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc);
180
static void         OLEFontImpl_Destroy(OLEFontImpl* fontDesc);
Marcus Meissner's avatar
Marcus Meissner committed
181
static ULONG        WINAPI OLEFontImpl_AddRef(IFont* iface);
182

183 184 185
/******************************************************************************
 *		OleCreateFontIndirect	[OLEAUT32.420]
 */
186
HRESULT WINAPI OleCreateFontIndirect(
187 188
  LPFONTDESC lpFontDesc,
  REFIID     riid,
189
  LPVOID*     ppvObj)
190 191 192 193
{
  OLEFontImpl* newFont = 0;
  HRESULT      hr      = S_OK;

194
  TRACE("(%p, %s, %p)\n", lpFontDesc, debugstr_guid(riid), ppvObj);
195 196 197 198 199 200 201 202
  /*
   * Sanity check
   */
  if (ppvObj==0)
    return E_POINTER;

  *ppvObj = 0;

203 204 205
  if (!lpFontDesc) {
    FONTDESC fd;

206
    static WCHAR fname[] = { 'S','y','s','t','e','m',0 };
207 208

    fd.cbSizeofstruct = sizeof(fd);
209
    fd.lpstrName      = fname;
210 211 212 213 214 215 216 217 218
    fd.cySize.s.Lo    = 80000;
    fd.cySize.s.Hi    = 0;
    fd.sWeight 	      = 0;
    fd.sCharset       = 0;
    fd.fItalic	      = 0;
    fd.fUnderline     = 0;
    fd.fStrikethrough = 0;
    lpFontDesc = &fd;
  }
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  /*
   * Try to construct a new instance of the class.
   */
  newFont = OLEFontImpl_Construct(lpFontDesc);

  if (newFont == 0)
    return E_OUTOFMEMORY;

  /*
   * Make sure it supports the interface required by the caller.
   */
  hr = IFont_QueryInterface((IFont*)newFont, riid, ppvObj);

  /*
   * Release the reference obtained in the constructor. If
   * the QueryInterface was unsuccessful, it will free the class.
   */
  IFont_Release((IFont*)newFont);

  return hr;
}


/***********************************************************************
 * Implementation of the OLEFontImpl class.
 */

247 248 249 250 251 252 253 254
/***********************************************************************
 *    OLEFont_SendNotify (internal)
 *
 * Sends notification messages of changed properties to any interested
 * connections.
 */
static void OLEFont_SendNotify(OLEFontImpl* this, DISPID dispID)
{
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  static const WCHAR wszName[] = {'N','a','m','e',0};
  static const WCHAR wszSize[] = {'S','i','z','e',0};
  static const WCHAR wszBold[] = {'B','o','l','d',0};
  static const WCHAR wszItalic[] = {'I','t','a','l','i','c',0};
  static const WCHAR wszUnder[] = {'U','n','d','e','r','l','i','n','e',0};
  static const WCHAR wszStrike[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
  static const WCHAR wszWeight[] = {'W','e','i','g','h','t',0};
  static const WCHAR wszCharset[] = {'C','h','a','r','s','s','e','t',0};
  static const LPCWSTR dispid_mapping[] =
  {
    wszName,
    NULL,
    wszSize,
    wszBold,
    wszItalic,
    wszUnder,
    wszStrike,
    wszWeight,
    wszCharset
  };

276 277
  IEnumConnections *pEnum;
  CONNECTDATA CD;
278
  HRESULT hres;
279

280
  this->gdiFont = 0;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  hres = IConnectionPoint_EnumConnections(this->pPropertyNotifyCP, &pEnum);
  if (SUCCEEDED(hres))
  {
    while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
      IPropertyNotifySink *sink;

      IUnknown_QueryInterface(CD.pUnk, &IID_IPropertyNotifySink, (LPVOID)&sink);
      IPropertyNotifySink_OnChanged(sink, dispID);
      IPropertyNotifySink_Release(sink);
      IUnknown_Release(CD.pUnk);
    }
    IEnumConnections_Release(pEnum);
  }

  hres = IConnectionPoint_EnumConnections(this->pFontEventsCP, &pEnum);
  if (SUCCEEDED(hres))
  {
    DISPPARAMS dispparams;
    VARIANTARG vararg;

    VariantInit(&vararg);
    V_VT(&vararg) = VT_BSTR;
    V_BSTR(&vararg) = SysAllocString(dispid_mapping[dispID]);
304

305 306 307 308
    dispparams.cArgs = 1;
    dispparams.cNamedArgs = 0;
    dispparams.rgdispidNamedArgs = NULL;
    dispparams.rgvarg = &vararg;
309

310 311 312 313 314 315 316 317 318 319 320 321 322
    while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
        IFontEventsDisp *disp;

        IUnknown_QueryInterface(CD.pUnk, &IID_IFontEventsDisp, (LPVOID)&disp);
        IDispatch_Invoke(disp, DISPID_FONT_CHANGED, &IID_NULL,
                         LOCALE_NEUTRAL, INVOKE_FUNC, &dispparams, NULL,
                         NULL, NULL);

        IDispatch_Release(disp);
        IUnknown_Release(CD.pUnk);
    }
    VariantClear(&vararg);
    IEnumConnections_Release(pEnum);
323 324
  }
}
325

326 327 328 329 330 331 332 333 334 335
/************************************************************************
 * OLEFontImpl_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
HRESULT WINAPI OLEFontImpl_QueryInterface(
  IFont*  iface,
  REFIID  riid,
  void**  ppvObject)
{
336
  OLEFontImpl *this = (OLEFontImpl *)iface;
337
  TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppvObject);
338 339 340 341 342 343

  /*
   * Perform a sanity check on the parameters.
   */
  if ( (this==0) || (ppvObject==0) )
    return E_INVALIDARG;
344

345 346 347 348
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
349

350 351 352
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
353
  if (IsEqualGUID(&IID_IUnknown, riid))
354
    *ppvObject = (IFont*)this;
355
  if (IsEqualGUID(&IID_IFont, riid))
356
    *ppvObject = (IFont*)this;
357
  if (IsEqualGUID(&IID_IDispatch, riid))
358
    *ppvObject = (IDispatch*)&(this->lpvtblIDispatch);
359
  if (IsEqualGUID(&IID_IFontDisp, riid))
360
    *ppvObject = (IDispatch*)&(this->lpvtblIDispatch);
361
  if (IsEqualIID(&IID_IPersist, riid) || IsEqualGUID(&IID_IPersistStream, riid))
362
    *ppvObject = (IPersistStream*)&(this->lpvtblIPersistStream);
363
  if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
364
    *ppvObject = (IConnectionPointContainer*)&(this->lpvtblIConnectionPointContainer);
365
  if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
366
    *ppvObject = (IPersistPropertyBag*)&(this->lpvtblIPersistPropertyBag);
367
  if (IsEqualGUID(&IID_IPersistStreamInit, riid))
368
    *ppvObject = (IPersistStreamInit*)&(this->lpvtblIPersistStreamInit);
369

370 371 372 373
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
374
  {
375
    FIXME("() : asking for unsupported interface %s\n",debugstr_guid(riid));
376
    return E_NOINTERFACE;
377
  }
378
  OLEFontImpl_AddRef((IFont*)this);
379
  return S_OK;
380
}
381

382 383 384 385 386
/************************************************************************
 * OLEFontImpl_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
387
ULONG WINAPI OLEFontImpl_AddRef(
388 389
  IFont* iface)
{
390
  OLEFontImpl *this = (OLEFontImpl *)iface;
391
  TRACE("(%p)->(ref=%d)\n", this, this->ref);
392
  return InterlockedIncrement(&this->ref);
393
}
394

395 396 397 398 399
/************************************************************************
 * OLEFontImpl_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
400
ULONG WINAPI OLEFontImpl_Release(
401 402
      IFont* iface)
{
403
  OLEFontImpl *this = (OLEFontImpl *)iface;
404
  ULONG ret;
405
  PHFONTItem ptr, next;
406
  TRACE("(%p)->(ref=%d)\n", this, this->ref);
407

408
  /* Decrease the reference count for current interface */
409
  ret = InterlockedDecrement(&this->ref);
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424
  /* If the reference count goes down to 0, destroy. */
  if (ret == 0)
  {
    ULONG fontlist_refs = InterlockedDecrement(&ifont_cnt);
    /* Check if all HFONT list refs are zero */
    if (fontlist_refs == 0)
    {
      EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
      LIST_FOR_EACH_ENTRY_SAFE(ptr, next, &OLEFontImpl_hFontList, HFONTItem, entry)
        HFONTItem_Delete(ptr);
      LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
    }
    OLEFontImpl_Destroy(this);
  }
425

426
  return ret;
427
}
428

429 430 431 432 433
/************************************************************************
 * OLEFontImpl_get_Name (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
434
static HRESULT WINAPI OLEFontImpl_get_Name(
435
  IFont*  iface,
436
  BSTR* pname)
437
{
438
  OLEFontImpl *this = (OLEFontImpl *)iface;
439
  TRACE("(%p)->(%p)\n", this, pname);
440 441 442 443 444 445 446
  /*
   * Sanity check.
   */
  if (pname==0)
    return E_POINTER;

  if (this->description.lpstrName!=0)
447
    *pname = SysAllocString(this->description.lpstrName);
448 449 450 451 452 453 454 455 456 457 458
  else
    *pname = 0;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Name (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
459
static HRESULT WINAPI OLEFontImpl_put_Name(
460
  IFont* iface,
461
  BSTR name)
462
{
463
  OLEFontImpl *this = (OLEFontImpl *)iface;
464
  TRACE("(%p)->(%p)\n", this, name);
465 466 467 468

  if (this->description.lpstrName==0)
  {
    this->description.lpstrName = HeapAlloc(GetProcessHeap(),
469
					    0,
470
					    (lstrlenW(name)+1) * sizeof(WCHAR));
471 472 473 474
  }
  else
  {
    this->description.lpstrName = HeapReAlloc(GetProcessHeap(),
475
					      0,
476
					      this->description.lpstrName,
477
					      (lstrlenW(name)+1) * sizeof(WCHAR));
478 479 480 481 482
  }

  if (this->description.lpstrName==0)
    return E_OUTOFMEMORY;

483
  strcpyW(this->description.lpstrName, name);
484 485
  TRACE("new name %s\n", debugstr_w(this->description.lpstrName));
  OLEFont_SendNotify(this, DISPID_FONT_NAME);
486 487 488 489 490 491 492 493
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Size (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
494
static HRESULT WINAPI OLEFontImpl_get_Size(
495
  IFont* iface,
496 497
  CY*    psize)
{
498
  OLEFontImpl *this = (OLEFontImpl *)iface;
499
  TRACE("(%p)->(%p)\n", this, psize);
500 501 502 503 504 505 506

  /*
   * Sanity check
   */
  if (psize==0)
    return E_POINTER;

507 508
  psize->s.Hi = 0;
  psize->s.Lo = this->description.cySize.s.Lo;
509 510 511 512 513 514 515 516 517

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Size (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
518
static HRESULT WINAPI OLEFontImpl_put_Size(
519
  IFont* iface,
520 521
  CY     size)
{
522
  OLEFontImpl *this = (OLEFontImpl *)iface;
523
  TRACE("(%p)->(%d)\n", this, size.s.Lo);
524
  this->description.cySize.s.Hi = 0;
525 526
  this->description.cySize.s.Lo = size.s.Lo;
  OLEFont_SendNotify(this, DISPID_FONT_SIZE);
527 528 529 530 531 532 533 534 535

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Bold (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
536
static HRESULT WINAPI OLEFontImpl_get_Bold(
537
  IFont*  iface,
538
  BOOL* pbold)
539
{
540
  OLEFontImpl *this = (OLEFontImpl *)iface;
541 542 543 544 545 546 547 548 549 550
  TRACE("(%p)->(%p)\n", this, pbold);
  /*
   * Sanity check
   */
  if (pbold==0)
    return E_POINTER;

  *pbold = this->description.sWeight > 550;

  return S_OK;
551 552 553 554 555 556 557
}

/************************************************************************
 * OLEFontImpl_put_Bold (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
558
static HRESULT WINAPI OLEFontImpl_put_Bold(
559
  IFont* iface,
560
  BOOL bold)
561
{
562
  OLEFontImpl *this = (OLEFontImpl *)iface;
563 564 565 566 567
  TRACE("(%p)->(%d)\n", this, bold);
  this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
  OLEFont_SendNotify(this, DISPID_FONT_BOLD);

  return S_OK;
568 569 570 571 572 573 574
}

/************************************************************************
 * OLEFontImpl_get_Italic (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
575
static HRESULT WINAPI OLEFontImpl_get_Italic(
576
  IFont*  iface,
577
  BOOL* pitalic)
578
{
579
  OLEFontImpl *this = (OLEFontImpl *)iface;
580
  TRACE("(%p)->(%p)\n", this, pitalic);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  /*
   * Sanity check
   */
  if (pitalic==0)
    return E_POINTER;

  *pitalic = this->description.fItalic;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Italic (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
597
static HRESULT WINAPI OLEFontImpl_put_Italic(
598
  IFont* iface,
599
  BOOL italic)
600
{
601
  OLEFontImpl *this = (OLEFontImpl *)iface;
602
  TRACE("(%p)->(%d)\n", this, italic);
603 604 605

  this->description.fItalic = italic;

606
  OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
607 608 609 610 611 612 613 614
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Underline (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
615
static HRESULT WINAPI OLEFontImpl_get_Underline(
616
  IFont*  iface,
617
  BOOL* punderline)
618
{
619
  OLEFontImpl *this = (OLEFontImpl *)iface;
620
  TRACE("(%p)->(%p)\n", this, punderline);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

  /*
   * Sanity check
   */
  if (punderline==0)
    return E_POINTER;

  *punderline = this->description.fUnderline;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Underline (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
638
static HRESULT WINAPI OLEFontImpl_put_Underline(
639
  IFont* iface,
640
  BOOL underline)
641
{
642
  OLEFontImpl *this = (OLEFontImpl *)iface;
643
  TRACE("(%p)->(%d)\n", this, underline);
644 645 646

  this->description.fUnderline = underline;

647
  OLEFont_SendNotify(this, DISPID_FONT_UNDER);
648 649 650 651 652 653 654 655
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Strikethrough (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
656
static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
657
  IFont*  iface,
658
  BOOL* pstrikethrough)
659
{
660
  OLEFontImpl *this = (OLEFontImpl *)iface;
661
  TRACE("(%p)->(%p)\n", this, pstrikethrough);
662 663 664 665 666 667 668

  /*
   * Sanity check
   */
  if (pstrikethrough==0)
    return E_POINTER;

669
  *pstrikethrough = this->description.fStrikethrough;
670 671 672 673 674 675 676 677 678

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Strikethrough (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
679
static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
680
 IFont* iface,
681
 BOOL strikethrough)
682
{
683
  OLEFontImpl *this = (OLEFontImpl *)iface;
684
  TRACE("(%p)->(%d)\n", this, strikethrough);
685

686
  this->description.fStrikethrough = strikethrough;
687
  OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
688 689 690 691 692 693 694 695 696

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Weight (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
697
static HRESULT WINAPI OLEFontImpl_get_Weight(
698
  IFont* iface,
699 700
  short* pweight)
{
701
  OLEFontImpl *this = (OLEFontImpl *)iface;
702
  TRACE("(%p)->(%p)\n", this, pweight);
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

  /*
   * Sanity check
   */
  if (pweight==0)
    return E_POINTER;

  *pweight = this->description.sWeight;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Weight (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
720
static HRESULT WINAPI OLEFontImpl_put_Weight(
721
  IFont* iface,
722 723
  short  weight)
{
724
  OLEFontImpl *this = (OLEFontImpl *)iface;
725
  TRACE("(%p)->(%d)\n", this, weight);
726 727 728

  this->description.sWeight = weight;

729
  OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
730 731 732 733 734 735 736 737
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Charset (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
738
static HRESULT WINAPI OLEFontImpl_get_Charset(
739
  IFont* iface,
740 741
  short* pcharset)
{
742
  OLEFontImpl *this = (OLEFontImpl *)iface;
743
  TRACE("(%p)->(%p)\n", this, pcharset);
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

  /*
   * Sanity check
   */
  if (pcharset==0)
    return E_POINTER;

  *pcharset = this->description.sCharset;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Charset (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
761
static HRESULT WINAPI OLEFontImpl_put_Charset(
762
  IFont* iface,
763 764
  short charset)
{
765
  OLEFontImpl *this = (OLEFontImpl *)iface;
766
  TRACE("(%p)->(%d)\n", this, charset);
767 768

  this->description.sCharset = charset;
769
  OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
770 771 772 773 774 775 776 777 778

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_hFont (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
779
static HRESULT WINAPI OLEFontImpl_get_hFont(
780
  IFont*   iface,
781
  HFONT* phfont)
782
{
783
  OLEFontImpl *this = (OLEFontImpl *)iface;
784
  TRACE("(%p)->(%p)\n", this, phfont);
785 786 787 788 789
  if (phfont==NULL)
    return E_POINTER;

  /*
   * Realize the font if necessary
790
 */
791
  if (this->gdiFont==0)
792
{
793 794 795
    LOGFONTW logFont;
    INT      fontHeight;
    CY       cySize;
796
    PHFONTItem newEntry;
797

798 799 800 801 802 803 804 805
    /*
     * The height of the font returned by the get_Size property is the
     * height of the font in points multiplied by 10000... Using some
     * simple conversions and the ratio given by the application, it can
     * be converted to a height in pixels.
     */
    IFont_get_Size(iface, &cySize);

806 807 808
    /* Standard ratio is 72 / 2540, or 18 / 635 in lowest terms. */
    /* Ratio is applied here relative to the standard. */
    fontHeight = MulDiv( cySize.s.Lo, this->cyLogical*635, this->cyHimetric*18 );
809 810 811

    memset(&logFont, 0, sizeof(LOGFONTW));

812 813
    logFont.lfHeight          = ((fontHeight%10000L)>5000L) ?	(-fontHeight/10000L)-1 :
 						    		(-fontHeight/10000L);
814 815
    logFont.lfItalic          = this->description.fItalic;
    logFont.lfUnderline       = this->description.fUnderline;
816
    logFont.lfStrikeOut       = this->description.fStrikethrough;
817 818 819 820 821 822
    logFont.lfWeight          = this->description.sWeight;
    logFont.lfCharSet         = this->description.sCharset;
    logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
    logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
    logFont.lfQuality         = DEFAULT_QUALITY;
    logFont.lfPitchAndFamily  = DEFAULT_PITCH;
823
    strcpyW(logFont.lfFaceName,this->description.lpstrName);
824 825

    this->gdiFont = CreateFontIndirectW(&logFont);
826 827 828 829 830 831 832 833

    /* Add font to the cache */
    newEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(HFONTItem));
    newEntry->ref = 1;
    newEntry->gdiFont = this->gdiFont;
    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
    list_add_tail(&OLEFontImpl_hFontList,&newEntry->entry);
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
834 835 836
  }

  *phfont = this->gdiFont;
837
  TRACE("Returning %p\n", *phfont);
838
  return S_OK;
839 840 841 842 843 844 845
}

/************************************************************************
 * OLEFontImpl_Clone (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
846
static HRESULT WINAPI OLEFontImpl_Clone(
847 848 849
  IFont*  iface,
  IFont** ppfont)
{
850
  OLEFontImpl* newObject = 0;
851 852 853
  LOGFONTW logFont;
  INT      fontHeight;
  CY       cySize;
854 855
  PHFONTItem newEntry;

856
  OLEFontImpl *this = (OLEFontImpl *)iface;
857
  TRACE("(%p)->(%p)\n", this, ppfont);
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

  if (ppfont == NULL)
    return E_POINTER;

  *ppfont = NULL;

  /*
   * Allocate space for the object.
   */
  newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));

  if (newObject==NULL)
    return E_OUTOFMEMORY;

  *newObject = *this;

874 875
  /* We need to alloc new memory for the string, otherwise
   * we free memory twice.
876
   */
877 878 879 880
  newObject->description.lpstrName = HeapAlloc(
	GetProcessHeap(),0,
	(1+strlenW(this->description.lpstrName))*2
  );
881
  strcpyW(newObject->description.lpstrName, this->description.lpstrName);
882 883 884
  /* We need to clone the HFONT too. This is just cut & paste from above */
  IFont_get_Size(iface, &cySize);

885
  fontHeight = MulDiv(cySize.s.Lo, this->cyLogical*635,this->cyHimetric*18);
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

  memset(&logFont, 0, sizeof(LOGFONTW));

  logFont.lfHeight          = ((fontHeight%10000L)>5000L) ? (-fontHeight/10000L)-1 :
							    (-fontHeight/10000L);
  logFont.lfItalic          = this->description.fItalic;
  logFont.lfUnderline       = this->description.fUnderline;
  logFont.lfStrikeOut       = this->description.fStrikethrough;
  logFont.lfWeight          = this->description.sWeight;
  logFont.lfCharSet         = this->description.sCharset;
  logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
  logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
  logFont.lfQuality         = DEFAULT_QUALITY;
  logFont.lfPitchAndFamily  = DEFAULT_PITCH;
  strcpyW(logFont.lfFaceName,this->description.lpstrName);

  newObject->gdiFont = CreateFontIndirectW(&logFont);

904 905 906 907 908 909 910 911 912
  /* Add font to the cache */
  InterlockedIncrement(&ifont_cnt);
  newEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(HFONTItem));
  newEntry->ref = 1;
  newEntry->gdiFont = newObject->gdiFont;
  EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
  list_add_tail(&OLEFontImpl_hFontList,&newEntry->entry);
  LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);

913 914 915 916 917 918 919 920 921 922 923
  /* create new connection points */
  newObject->pPropertyNotifyCP = NULL;
  newObject->pFontEventsCP = NULL;
  CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
  CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);

  if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
  {
    OLEFontImpl_Destroy(newObject);
    return E_OUTOFMEMORY;
  }
924 925

  /* The cloned object starts with a reference count of 1 */
926 927 928 929 930
  newObject->ref          = 1;

  *ppfont = (IFont*)newObject;

  return S_OK;
931 932 933 934 935 936 937
}

/************************************************************************
 * OLEFontImpl_IsEqual (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
938
static HRESULT WINAPI OLEFontImpl_IsEqual(
939
  IFont* iface,
940 941
  IFont* pFontOther)
{
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
  OLEFontImpl *left = (OLEFontImpl *)iface;
  OLEFontImpl *right = (OLEFontImpl *)pFontOther;
  HRESULT hres;
  INT left_len,right_len;

  if((iface == NULL) || (pFontOther == NULL))
    return E_POINTER;
  else if (left->description.cySize.s.Lo != right->description.cySize.s.Lo)
    return S_FALSE;
  else if (left->description.cySize.s.Hi != right->description.cySize.s.Hi)
    return S_FALSE;
  else if (left->description.sWeight != right->description.sWeight)
    return S_FALSE;
  else if (left->description.sCharset != right->description.sCharset)
    return S_FALSE;
  else if (left->description.fItalic != right->description.fItalic)
    return S_FALSE;
  else if (left->description.fUnderline != right->description.fUnderline)
    return S_FALSE;
  else if (left->description.fStrikethrough != right->description.fStrikethrough)
    return S_FALSE;

  /* Check from string */
  left_len = strlenW(left->description.lpstrName);
  right_len = strlenW(right->description.lpstrName);
  hres = CompareStringW(0,0,left->description.lpstrName, left_len,
    right->description.lpstrName, right_len);
  if (hres != CSTR_EQUAL)
    return S_FALSE;

  return S_OK;
973 974 975 976 977 978 979
}

/************************************************************************
 * OLEFontImpl_SetRatio (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
980
static HRESULT WINAPI OLEFontImpl_SetRatio(
981
  IFont* iface,
982 983
  LONG   cyLogical,
  LONG   cyHimetric)
984
{
985
  OLEFontImpl *this = (OLEFontImpl *)iface;
986
  TRACE("(%p)->(%d, %d)\n", this, cyLogical, cyHimetric);
987 988 989 990 991

  this->cyLogical  = cyLogical;
  this->cyHimetric = cyHimetric;

  return S_OK;
992 993 994 995 996 997 998
}

/************************************************************************
 * OLEFontImpl_QueryTextMetrics (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
999
static HRESULT      WINAPI OLEFontImpl_QueryTextMetrics(
1000
  IFont*         iface,
1001 1002
  TEXTMETRICOLE* ptm)
{
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
  HDC hdcRef;
  HFONT hOldFont, hNewFont;

  hdcRef = GetDC(0);
  OLEFontImpl_get_hFont(iface, &hNewFont);
  hOldFont = SelectObject(hdcRef, hNewFont);
  GetTextMetricsW(hdcRef, ptm);
  SelectObject(hdcRef, hOldFont);
  ReleaseDC(0, hdcRef);
  return S_OK;
1013 1014 1015 1016 1017 1018 1019
}

/************************************************************************
 * OLEFontImpl_AddRefHfont (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1020
static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1021
  IFont*  iface,
1022
  HFONT hfont)
1023
{
1024
  OLEFontImpl *this = (OLEFontImpl *)iface;
1025 1026
  PHFONTItem ptr, next;
  HRESULT hres = S_FALSE; /* assume not present */
1027

1028 1029 1030
  TRACE("(%p)->(%p)\n", this, hfont);

  if (!hfont)
1031 1032
    return E_INVALIDARG;

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  /* Check of the hFont is already in the list */
  EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
  LIST_FOR_EACH_ENTRY_SAFE(ptr, next, &OLEFontImpl_hFontList, HFONTItem, entry)
  {
    if (ptr->gdiFont == hfont)
    {
      ptr->ref++;
      hres = S_OK;
      break;
    }
  }
  LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
1045

1046
  return hres;
1047 1048 1049 1050 1051 1052 1053
}

/************************************************************************
 * OLEFontImpl_ReleaseHfont (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1054
static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1055
  IFont*  iface,
1056
  HFONT hfont)
1057
{
1058
  OLEFontImpl *this = (OLEFontImpl *)iface;
1059 1060
  PHFONTItem ptr, next;
  HRESULT hres = S_FALSE; /* assume not present */
1061

1062
  TRACE("(%p)->(%p)\n", this, hfont);
1063

1064 1065
  if (!hfont)
    return E_INVALIDARG;
1066

1067 1068 1069
  /* Check of the hFont is already in the list */
  EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
  LIST_FOR_EACH_ENTRY_SAFE(ptr, next, &OLEFontImpl_hFontList, HFONTItem, entry)
1070
  {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    if ((ptr->gdiFont == hfont) && ptr->ref)
    {
      /* Remove from cache and delete object if not referenced */
      if (!--ptr->ref)
      {
        if (ptr->gdiFont == this->gdiFont)
          this->gdiFont = NULL;
      }
      hres = S_OK;
      break;
    }
1082
  }
1083
  LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
1084

1085
  return hres;
1086 1087 1088 1089 1090 1091 1092
}

/************************************************************************
 * OLEFontImpl_SetHdc (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1093
static HRESULT WINAPI OLEFontImpl_SetHdc(
1094
  IFont* iface,
1095
  HDC  hdc)
1096
{
1097
  OLEFontImpl *this = (OLEFontImpl *)iface;
1098
  FIXME("(%p)->(%p): Stub\n", this, hdc);
1099 1100 1101
  return E_NOTIMPL;
}

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
/*
 * Virtual function tables for the OLEFontImpl class.
 */
static const IFontVtbl OLEFontImpl_VTable =
{
  OLEFontImpl_QueryInterface,
  OLEFontImpl_AddRef,
  OLEFontImpl_Release,
  OLEFontImpl_get_Name,
  OLEFontImpl_put_Name,
  OLEFontImpl_get_Size,
  OLEFontImpl_put_Size,
  OLEFontImpl_get_Bold,
  OLEFontImpl_put_Bold,
  OLEFontImpl_get_Italic,
  OLEFontImpl_put_Italic,
  OLEFontImpl_get_Underline,
  OLEFontImpl_put_Underline,
  OLEFontImpl_get_Strikethrough,
  OLEFontImpl_put_Strikethrough,
  OLEFontImpl_get_Weight,
  OLEFontImpl_put_Weight,
  OLEFontImpl_get_Charset,
  OLEFontImpl_put_Charset,
  OLEFontImpl_get_hFont,
  OLEFontImpl_Clone,
  OLEFontImpl_IsEqual,
  OLEFontImpl_SetRatio,
  OLEFontImpl_QueryTextMetrics,
  OLEFontImpl_AddRefHfont,
  OLEFontImpl_ReleaseHfont,
  OLEFontImpl_SetHdc
};

1136 1137 1138 1139 1140
/************************************************************************
 * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1141
static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1142 1143 1144 1145
  IDispatch* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1146
  OLEFontImpl *this = impl_from_IDispatch(iface);
1147

1148
  return IFont_QueryInterface((IFont *)this, riid, ppvoid);
1149 1150 1151 1152 1153 1154 1155
}

/************************************************************************
 * OLEFontImpl_IDispatch_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1156
static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1157 1158
  IDispatch* iface)
{
1159
  OLEFontImpl *this = impl_from_IDispatch(iface);
1160

1161
  return IFont_Release((IFont *)this);
1162 1163 1164 1165 1166 1167 1168
}

/************************************************************************
 * OLEFontImpl_IDispatch_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1169
static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1170 1171
  IDispatch* iface)
{
1172
  OLEFontImpl *this = impl_from_IDispatch(iface);
1173

1174
  return IFont_AddRef((IFont *)this);
1175 1176 1177 1178 1179 1180 1181
}

/************************************************************************
 * OLEFontImpl_GetTypeInfoCount (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1182
static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1183
  IDispatch*    iface,
1184 1185
  unsigned int* pctinfo)
{
1186
  OLEFontImpl *this = impl_from_IDispatch(iface);
1187 1188
  TRACE("(%p)->(%p)\n", this, pctinfo);
  *pctinfo = 1;
1189

1190
  return S_OK;
1191 1192 1193 1194 1195 1196 1197
}

/************************************************************************
 * OLEFontImpl_GetTypeInfo (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1198
static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1199
  IDispatch*  iface,
1200
  UINT      iTInfo,
1201
  LCID        lcid,
1202 1203
  ITypeInfo** ppTInfo)
{
1204
  static const WCHAR stdole2tlb[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
1205 1206
  ITypeLib *tl;
  HRESULT hres;
1207

1208
  OLEFontImpl *this = impl_from_IDispatch(iface);
1209
  TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1210 1211
  if (iTInfo != 0)
    return E_FAIL;
1212
  hres = LoadTypeLib(stdole2tlb, &tl);
1213
  if (FAILED(hres)) {
1214
    ERR("Could not load the stdole2.tlb?\n");
1215 1216
    return hres;
  }
1217
  hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1218
  if (FAILED(hres)) {
1219
    FIXME("Did not IDispatch typeinfo from typelib, hres %x\n",hres);
1220 1221
  }
  return hres;
1222 1223 1224 1225 1226 1227 1228
}

/************************************************************************
 * OLEFontImpl_GetIDsOfNames (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1229
static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1230
  IDispatch*  iface,
1231 1232 1233
  REFIID      riid,
  LPOLESTR* rgszNames,
  UINT      cNames,
1234 1235 1236
  LCID        lcid,
  DISPID*     rgDispId)
{
1237 1238 1239
  ITypeInfo * pTInfo;
  HRESULT hres;

1240
  OLEFontImpl *this = impl_from_IDispatch(iface);
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265

  TRACE("(%p,%s,%p,cNames=%d,lcid=%04x,%p)\n", this, debugstr_guid(riid),
        rgszNames, cNames, (int)lcid, rgDispId);

  if (cNames == 0)
  {
    return E_INVALIDARG;
  }
  else
  {
    /* retrieve type information */
    hres = OLEFontImpl_GetTypeInfo(iface, 0, lcid, &pTInfo);

    if (FAILED(hres))
    {
      ERR("GetTypeInfo failed.\n");
      return hres;
    }

    /* convert names to DISPIDs */
    hres = DispGetIDsOfNames (pTInfo, rgszNames, cNames, rgDispId);
    ITypeInfo_Release(pTInfo);

    return hres;
  }
1266 1267 1268 1269 1270 1271
}

/************************************************************************
 * OLEFontImpl_Invoke (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
1272 1273 1274 1275
 * 
 * Note: Do not call _put_Xxx methods, since setting things here
 * should not call notify functions as I found out debugging the generic
 * MS VB5 installer.
1276
 */
Marcus Meissner's avatar
Marcus Meissner committed
1277
static HRESULT WINAPI OLEFontImpl_Invoke(
1278
  IDispatch*  iface,
1279 1280 1281
  DISPID      dispIdMember,
  REFIID      riid,
  LCID        lcid,
1282 1283
  WORD        wFlags,
  DISPPARAMS* pDispParams,
1284
  VARIANT*    pVarResult,
1285
  EXCEPINFO*  pExepInfo,
1286
  UINT*     puArgErr)
1287
{
1288
  OLEFontImpl *this = impl_from_IDispatch(iface);
1289
  HRESULT hr;
1290

1291
  TRACE("%p->(%d,%s,0x%x,0x%x,%p,%p,%p,%p)\n", this, dispIdMember,
1292 1293 1294
    debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
    puArgErr);

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  /* validate parameters */

  if (!IsEqualIID(riid, &IID_NULL))
  {
    ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
    return DISP_E_UNKNOWNINTERFACE;
  }

  if (wFlags & DISPATCH_PROPERTYGET)
  {
    if (!pVarResult)
    {
      ERR("null pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
      return DISP_E_PARAMNOTOPTIONAL;
    }
  }
  else if (wFlags & DISPATCH_PROPERTYPUT)
  {
    if (!pDispParams)
    {
      ERR("null pDispParams not allowed when DISPATCH_PROPERTYPUT specified\n");
      return DISP_E_PARAMNOTOPTIONAL;
    }
    if (pDispParams->cArgs != 1)
    {
1320
      ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1321 1322 1323 1324 1325 1326 1327 1328 1329
      return DISP_E_BADPARAMCOUNT;
    }
  }
  else
  {
    ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
    return DISP_E_MEMBERNOTFOUND;
  }

1330 1331
  switch (dispIdMember) {
  case DISPID_FONT_NAME:
1332
    if (wFlags & DISPATCH_PROPERTYGET) {
1333
      V_VT(pVarResult) = VT_BSTR;
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
      return IFont_get_Name((IFont *)this, &V_BSTR(pVarResult));
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BSTR);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Name((IFont *)this, V_BSTR(&vararg));

      VariantClear(&vararg);
      return hr;
1347 1348 1349
    }
    break;
  case DISPID_FONT_BOLD:
1350 1351 1352
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Bold((IFont *)this, &value);
1353
      V_VT(pVarResult) = VT_BOOL;
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Bold((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1368 1369 1370
    }
    break;
  case DISPID_FONT_ITALIC:
1371 1372 1373
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Italic((IFont *)this, &value);
1374
      V_VT(pVarResult) = VT_BOOL;
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;
      HRESULT hr;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Italic((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1390 1391 1392
    }
    break;
  case DISPID_FONT_UNDER:
1393 1394 1395
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Underline((IFont *)this, &value);
1396
      V_VT(pVarResult) = VT_BOOL;
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;
      HRESULT hr;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Underline((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1412 1413 1414
    }
    break;
  case DISPID_FONT_STRIKE:
1415 1416 1417
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Strikethrough((IFont *)this, &value);
1418
      V_VT(pVarResult) = VT_BOOL;
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;
      HRESULT hr;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Strikethrough((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1434 1435 1436
    }
    break;
  case DISPID_FONT_SIZE:
1437
    if (wFlags & DISPATCH_PROPERTYGET) {
1438
      V_VT(pVarResult) = VT_CY;
1439
      return OLEFontImpl_get_Size((IFont *)this, &V_CY(pVarResult));
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
    } else {
      VARIANTARG vararg;
      HRESULT hr;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_CY);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Size((IFont *)this, V_CY(&vararg));

      VariantClear(&vararg);
      return hr;
1453 1454
    }
    break;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
  case DISPID_FONT_WEIGHT:
    if (wFlags & DISPATCH_PROPERTYGET) {
      V_VT(pVarResult) = VT_I2;
      return OLEFontImpl_get_Weight((IFont *)this, &V_I2(pVarResult));
    } else {
      VARIANTARG vararg;
      HRESULT hr;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Weight((IFont *)this, V_I2(&vararg));

      VariantClear(&vararg);
      return hr;
    }
    break;
1474
  case DISPID_FONT_CHARSET:
1475
    if (wFlags & DISPATCH_PROPERTYGET) {
1476
      V_VT(pVarResult) = VT_I2;
1477
      return OLEFontImpl_get_Charset((IFont *)this, &V_I2(pVarResult));
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
    } else {
      VARIANTARG vararg;
      HRESULT hr;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
      if (FAILED(hr))
        return hr;

      hr = IFont_put_Charset((IFont *)this, V_I2(&vararg));

      VariantClear(&vararg);
      return hr;
1491 1492
    }
    break;
1493
  default:
1494
    ERR("member not found for dispid 0x%x\n", dispIdMember);
1495
    return DISP_E_MEMBERNOTFOUND;
1496
  }
1497 1498
}

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
{
  OLEFontImpl_IDispatch_QueryInterface,
  OLEFontImpl_IDispatch_AddRef,
  OLEFontImpl_IDispatch_Release,
  OLEFontImpl_GetTypeInfoCount,
  OLEFontImpl_GetTypeInfo,
  OLEFontImpl_GetIDsOfNames,
  OLEFontImpl_Invoke
};

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
/************************************************************************
 * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
  IPersistStream* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1520
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1521

1522
  return IFont_QueryInterface((IFont *)this, riid, ppvoid);
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
}

/************************************************************************
 * OLEFontImpl_IPersistStream_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
  IPersistStream* iface)
{
1533
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1534

1535
  return IFont_Release((IFont *)this);
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
}

/************************************************************************
 * OLEFontImpl_IPersistStream_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
  IPersistStream* iface)
{
1546
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1547

1548
  return IFont_AddRef((IFont *)this);
1549 1550 1551 1552 1553 1554 1555 1556
}

/************************************************************************
 * OLEFontImpl_GetClassID (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_GetClassID(
1557
  IPersistStream* iface,
1558 1559
  CLSID*                pClassID)
{
1560
  TRACE("(%p,%p)\n",iface,pClassID);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
  if (pClassID==0)
    return E_POINTER;

  memcpy(pClassID, &CLSID_StdFont, sizeof(CLSID_StdFont));

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_IsDirty (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_IsDirty(
  IPersistStream*  iface)
{
1577
  TRACE("(%p)\n",iface);
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_Load (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 *
 * This is the format of the standard font serialization as far as I
 * know
 *
 * Offset   Type   Value           Comment
 * 0x0000   Byte   Unknown         Probably a version number, contains 0x01
 * 0x0001   Short  Charset         Charset value from the FONTDESC structure
 * 0x0003   Byte   Attributes      Flags defined as follows:
 *                                     00000010 - Italic
 *                                     00000100 - Underline
 *                                     00001000 - Strikethrough
 * 0x0004   Short  Weight          Weight value from FONTDESC structure
 * 0x0006   DWORD  size            "Low" portion of the cySize member of the FONTDESC
 *                                 structure/
 * 0x000A   Byte   name length     Length of the font name string (no null character)
 * 0x000B   String name            Name of the font (ASCII, no nul character)
 */
static HRESULT WINAPI OLEFontImpl_Load(
  IPersistStream*  iface,
  IStream*         pLoadStream)
{
  char  readBuffer[0x100];
  ULONG cbRead;
  BYTE  bVersion;
  BYTE  bAttributes;
  BYTE  bStringSize;
1611
  INT len;
1612

1613
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1614

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
  /*
   * Read the version byte
   */
  IStream_Read(pLoadStream, &bVersion, 1, &cbRead);

  if ( (cbRead!=1) ||
       (bVersion!=0x01) )
    return E_FAIL;

  /*
   * Charset
   */
  IStream_Read(pLoadStream, &this->description.sCharset, 2, &cbRead);

  if (cbRead!=2)
    return E_FAIL;

  /*
   * Attributes
   */
  IStream_Read(pLoadStream, &bAttributes, 1, &cbRead);

  if (cbRead!=1)
    return E_FAIL;

  this->description.fItalic        = (bAttributes & FONTPERSIST_ITALIC) != 0;
1641
  this->description.fStrikethrough = (bAttributes & FONTPERSIST_STRIKETHROUGH) != 0;
1642
  this->description.fUnderline     = (bAttributes & FONTPERSIST_UNDERLINE) != 0;
1643

1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
  /*
   * Weight
   */
  IStream_Read(pLoadStream, &this->description.sWeight, 2, &cbRead);

  if (cbRead!=2)
    return E_FAIL;

  /*
   * Size
   */
1655
  IStream_Read(pLoadStream, &this->description.cySize.s.Lo, 4, &cbRead);
1656 1657 1658 1659

  if (cbRead!=4)
    return E_FAIL;

1660
  this->description.cySize.s.Hi = 0;
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674

  /*
   * FontName
   */
  IStream_Read(pLoadStream, &bStringSize, 1, &cbRead);

  if (cbRead!=1)
    return E_FAIL;

  IStream_Read(pLoadStream, readBuffer, bStringSize, &cbRead);

  if (cbRead!=bStringSize)
    return E_FAIL;

1675
  HeapFree(GetProcessHeap(), 0, this->description.lpstrName);
1676

1677 1678 1679 1680
  len = MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, NULL, 0 );
  this->description.lpstrName = HeapAlloc( GetProcessHeap(), 0, (len+1) * sizeof(WCHAR) );
  MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, this->description.lpstrName, len );
  this->description.lpstrName[len] = 0;
1681

1682 1683 1684 1685
  /* Ensure use of this font causes a new one to be created @@@@ */
  DeleteObject(this->gdiFont);
  this->gdiFont = 0;

1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_Save (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_Save(
  IPersistStream*  iface,
  IStream*         pOutStream,
  BOOL             fClearDirty)
{
  char* writeBuffer = NULL;
  ULONG cbWritten;
  BYTE  bVersion = 0x01;
  BYTE  bAttributes;
  BYTE  bStringSize;
1704

1705
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730

  /*
   * Read the version byte
   */
  IStream_Write(pOutStream, &bVersion, 1, &cbWritten);

  if (cbWritten!=1)
    return E_FAIL;

  /*
   * Charset
   */
  IStream_Write(pOutStream, &this->description.sCharset, 2, &cbWritten);

  if (cbWritten!=2)
    return E_FAIL;

  /*
   * Attributes
   */
  bAttributes = 0;

  if (this->description.fItalic)
    bAttributes |= FONTPERSIST_ITALIC;

1731
  if (this->description.fStrikethrough)
1732
    bAttributes |= FONTPERSIST_STRIKETHROUGH;
1733

1734 1735 1736 1737 1738 1739 1740
  if (this->description.fUnderline)
    bAttributes |= FONTPERSIST_UNDERLINE;

  IStream_Write(pOutStream, &bAttributes, 1, &cbWritten);

  if (cbWritten!=1)
    return E_FAIL;
1741

1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
  /*
   * Weight
   */
  IStream_Write(pOutStream, &this->description.sWeight, 2, &cbWritten);

  if (cbWritten!=2)
    return E_FAIL;

  /*
   * Size
   */
1753
  IStream_Write(pOutStream, &this->description.cySize.s.Lo, 4, &cbWritten);
1754 1755 1756 1757 1758 1759 1760 1761

  if (cbWritten!=4)
    return E_FAIL;

  /*
   * FontName
   */
  if (this->description.lpstrName!=0)
1762 1763
    bStringSize = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
                                       strlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
  else
    bStringSize = 0;

  IStream_Write(pOutStream, &bStringSize, 1, &cbWritten);

  if (cbWritten!=1)
    return E_FAIL;

  if (bStringSize!=0)
  {
1774 1775 1776 1777
      if (!(writeBuffer = HeapAlloc( GetProcessHeap(), 0, bStringSize ))) return E_OUTOFMEMORY;
      WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
                           strlenW(this->description.lpstrName),
                           writeBuffer, bStringSize, NULL, NULL );
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

    IStream_Write(pOutStream, writeBuffer, bStringSize, &cbWritten);
    HeapFree(GetProcessHeap(), 0, writeBuffer);

    if (cbWritten!=bStringSize)
      return E_FAIL;
  }

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_GetSizeMax (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_GetSizeMax(
  IPersistStream*  iface,
  ULARGE_INTEGER*  pcbSize)
{
1798
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1799 1800 1801 1802

  if (pcbSize==NULL)
    return E_POINTER;

1803 1804
  pcbSize->u.HighPart = 0;
  pcbSize->u.LowPart = 0;
1805

1806 1807 1808 1809 1810 1811
  pcbSize->u.LowPart += sizeof(BYTE);  /* Version */
  pcbSize->u.LowPart += sizeof(WORD);  /* Lang code */
  pcbSize->u.LowPart += sizeof(BYTE);  /* Flags */
  pcbSize->u.LowPart += sizeof(WORD);  /* Weight */
  pcbSize->u.LowPart += sizeof(DWORD); /* Size */
  pcbSize->u.LowPart += sizeof(BYTE);  /* StrLength */
1812 1813

  if (this->description.lpstrName!=0)
1814
    pcbSize->u.LowPart += lstrlenW(this->description.lpstrName);
1815 1816 1817

  return S_OK;
}
1818

1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
static const IPersistStreamVtbl OLEFontImpl_IPersistStream_VTable =
{
  OLEFontImpl_IPersistStream_QueryInterface,
  OLEFontImpl_IPersistStream_AddRef,
  OLEFontImpl_IPersistStream_Release,
  OLEFontImpl_GetClassID,
  OLEFontImpl_IsDirty,
  OLEFontImpl_Load,
  OLEFontImpl_Save,
  OLEFontImpl_GetSizeMax
};

1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
  IConnectionPointContainer* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1841
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853

  return IFont_QueryInterface((IFont*)this, riid, ppvoid);
}

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_Release(
  IConnectionPointContainer* iface)
{
1854
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866

  return IFont_Release((IFont*)this);
}

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
  IConnectionPointContainer* iface)
{
1867
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881

  return IFont_AddRef((IFont*)this);
}

/************************************************************************
 * OLEFontImpl_EnumConnectionPoints (IConnectionPointContainer)
 *
 * See Windows documentation for more details on IConnectionPointContainer
 * methods.
 */
static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
  IConnectionPointContainer* iface,
  IEnumConnectionPoints **ppEnum)
{
1882
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898

  FIXME("(%p)->(%p): stub\n", this, ppEnum);
  return E_NOTIMPL;
}

/************************************************************************
 * OLEFontImpl_FindConnectionPoint (IConnectionPointContainer)
 *
 * See Windows documentation for more details on IConnectionPointContainer
 * methods.
 */
static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
   IConnectionPointContainer* iface,
   REFIID riid,
   IConnectionPoint **ppCp)
{
1899
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1900
  TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppCp);
1901

1902 1903 1904 1905 1906 1907 1908 1909
  if(IsEqualIID(riid, &IID_IPropertyNotifySink)) {
    return IConnectionPoint_QueryInterface(this->pPropertyNotifyCP,
                                           &IID_IConnectionPoint,
                                           (LPVOID)ppCp);
  } else if(IsEqualIID(riid, &IID_IFontEventsDisp)) {
    return IConnectionPoint_QueryInterface(this->pFontEventsCP,
                                           &IID_IConnectionPoint,
                                           (LPVOID)ppCp);
1910
  } else {
1911 1912
    FIXME("no connection point for %s\n", debugstr_guid(riid));
    return CONNECT_E_NOCONNECTION;
1913 1914 1915
  }
}

1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
static const IConnectionPointContainerVtbl
     OLEFontImpl_IConnectionPointContainer_VTable =
{
  OLEFontImpl_IConnectionPointContainer_QueryInterface,
  OLEFontImpl_IConnectionPointContainer_AddRef,
  OLEFontImpl_IConnectionPointContainer_Release,
  OLEFontImpl_EnumConnectionPoints,
  OLEFontImpl_FindConnectionPoint
};

1926 1927 1928 1929 1930 1931
/************************************************************************
 * OLEFontImpl implementation of IPersistPropertyBag.
 */
static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_QueryInterface(
   IPersistPropertyBag *iface, REFIID riid, LPVOID *ppvObj
) {
1932 1933
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
  return IFont_QueryInterface((IFont *)this,riid,ppvObj);
1934 1935 1936 1937 1938
}

static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_AddRef(
   IPersistPropertyBag *iface
) {
1939 1940
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
  return IFont_AddRef((IFont *)this);
1941 1942 1943 1944 1945
}

static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_Release(
   IPersistPropertyBag *iface
) {
1946 1947
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
  return IFont_Release((IFont *)this);
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_GetClassID(
   IPersistPropertyBag *iface, CLSID *classid
) {
  FIXME("(%p,%p), stub!\n", iface, classid);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_InitNew(
   IPersistPropertyBag *iface
) {
  FIXME("(%p), stub!\n", iface);
  return S_OK;
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Load(
   IPersistPropertyBag *iface, IPropertyBag* pPropBag, IErrorLog* pErrorLog
) {
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
/* (from Visual Basic 6 property bag)
         Name            =   "MS Sans Serif"
         Size            =   13.8
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
*/
    static const WCHAR sAttrName[] = {'N','a','m','e',0};
    static const WCHAR sAttrSize[] = {'S','i','z','e',0};
    static const WCHAR sAttrCharset[] = {'C','h','a','r','s','e','t',0};
    static const WCHAR sAttrWeight[] = {'W','e','i','g','h','t',0};
    static const WCHAR sAttrUnderline[] = {'U','n','d','e','r','l','i','n','e',0};
    static const WCHAR sAttrItalic[] = {'I','t','a','l','i','c',0};
    static const WCHAR sAttrStrikethrough[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
    VARIANT rawAttr;
    VARIANT valueAttr;
    HRESULT iRes = S_OK;
1986
    OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

    VariantInit(&rawAttr);
    VariantInit(&valueAttr);

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrName, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BSTR);
            if (iRes == S_OK)
1997
                iRes = IFont_put_Name((IFont *)this, V_BSTR(&valueAttr));
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrSize, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_CY);
            if (iRes == S_OK)
2011
                iRes = IFont_put_Size((IFont *)this, V_CY(&valueAttr));
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrCharset, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_I2);
            if (iRes == S_OK)
2025
                iRes = IFont_put_Charset((IFont *)this, V_I2(&valueAttr));
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrWeight, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_I2);
            if (iRes == S_OK)
2039
                iRes = IFont_put_Weight((IFont *)this, V_I2(&valueAttr));
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);

    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrUnderline, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BOOL);
            if (iRes == S_OK)
2054
                iRes = IFont_put_Underline((IFont *)this, V_BOOL(&valueAttr));
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrItalic, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BOOL);
            if (iRes == S_OK)
2068
                iRes = IFont_put_Italic((IFont *)this, V_BOOL(&valueAttr));
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrStrikethrough, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BOOL);
            if (iRes == S_OK)
2082
                IFont_put_Strikethrough((IFont *)this, V_BOOL(&valueAttr));
2083 2084 2085 2086 2087 2088 2089 2090
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (FAILED(iRes))
2091
        WARN("-- 0x%08x\n", iRes);
2092
    return iRes;
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Save(
   IPersistPropertyBag *iface, IPropertyBag* pPropBag, BOOL fClearDirty,
   BOOL fSaveAllProperties
) {
  FIXME("(%p,%p,%d,%d), stub!\n", iface, pPropBag, fClearDirty, fSaveAllProperties);
  return E_FAIL;
}

2103
static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable = 
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
{
  OLEFontImpl_IPersistPropertyBag_QueryInterface,
  OLEFontImpl_IPersistPropertyBag_AddRef,
  OLEFontImpl_IPersistPropertyBag_Release,

  OLEFontImpl_IPersistPropertyBag_GetClassID,
  OLEFontImpl_IPersistPropertyBag_InitNew,
  OLEFontImpl_IPersistPropertyBag_Load,
  OLEFontImpl_IPersistPropertyBag_Save
};

/************************************************************************
 * OLEFontImpl implementation of IPersistStreamInit.
 */
static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_QueryInterface(
   IPersistStreamInit *iface, REFIID riid, LPVOID *ppvObj
) {
2121 2122
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
  return IFont_QueryInterface((IFont *)this,riid,ppvObj);
2123 2124 2125 2126 2127
}

static ULONG WINAPI OLEFontImpl_IPersistStreamInit_AddRef(
   IPersistStreamInit *iface
) {
2128 2129
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
  return IFont_AddRef((IFont *)this);
2130 2131 2132 2133 2134
}

static ULONG WINAPI OLEFontImpl_IPersistStreamInit_Release(
   IPersistStreamInit *iface
) {
2135 2136
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
  return IFont_Release((IFont *)this);
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_GetClassID(
   IPersistStreamInit *iface, CLSID *classid
) {
  FIXME("(%p,%p), stub!\n", iface, classid);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_IsDirty(
   IPersistStreamInit *iface
) {
  FIXME("(%p), stub!\n", iface);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_Load(
   IPersistStreamInit *iface, LPSTREAM pStm
) {
  FIXME("(%p,%p), stub!\n", iface, pStm);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_Save(
   IPersistStreamInit *iface, LPSTREAM pStm, BOOL fClearDirty
) {
  FIXME("(%p,%p,%d), stub!\n", iface, pStm, fClearDirty);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_GetSizeMax(
   IPersistStreamInit *iface, ULARGE_INTEGER *pcbSize
) {
  FIXME("(%p,%p), stub!\n", iface, pcbSize);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_InitNew(
   IPersistStreamInit *iface
) {
  FIXME("(%p), stub!\n", iface);
  return S_OK;
}

2181
static const IPersistStreamInitVtbl OLEFontImpl_IPersistStreamInit_VTable = 
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
{
  OLEFontImpl_IPersistStreamInit_QueryInterface,
  OLEFontImpl_IPersistStreamInit_AddRef,
  OLEFontImpl_IPersistStreamInit_Release,

  OLEFontImpl_IPersistStreamInit_GetClassID,
  OLEFontImpl_IPersistStreamInit_IsDirty,
  OLEFontImpl_IPersistStreamInit_Load,
  OLEFontImpl_IPersistStreamInit_Save,
  OLEFontImpl_IPersistStreamInit_GetSizeMax,
  OLEFontImpl_IPersistStreamInit_InitNew
};

2195 2196 2197 2198 2199 2200 2201 2202 2203
/************************************************************************
 * OLEFontImpl_Construct
 *
 * This method will construct a new instance of the OLEFontImpl
 * class.
 *
 * The caller of this method must release the object when it's
 * done with it.
 */
2204
static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc)
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
{
  OLEFontImpl* newObject = 0;

  /*
   * Allocate space for the object.
   */
  newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));

  if (newObject==0)
    return newObject;

  /*
   * Initialize the virtual function table.
   */
  newObject->lpVtbl = &OLEFontImpl_VTable;
  newObject->lpvtblIDispatch = &OLEFontImpl_IDispatch_VTable;
  newObject->lpvtblIPersistStream = &OLEFontImpl_IPersistStream_VTable;
  newObject->lpvtblIConnectionPointContainer = &OLEFontImpl_IConnectionPointContainer_VTable;
  newObject->lpvtblIPersistPropertyBag = &OLEFontImpl_IPersistPropertyBag_VTable;
  newObject->lpvtblIPersistStreamInit = &OLEFontImpl_IPersistStreamInit_VTable;

  /*
   * Start with one reference count. The caller of this function
   * must release the interface pointer when it is done.
   */
  newObject->ref = 1;

  /*
   * Copy the description of the font in the object.
   */
  assert(fontDesc->cbSizeofstruct >= sizeof(FONTDESC));

  newObject->description.cbSizeofstruct = sizeof(FONTDESC);
  newObject->description.lpstrName = HeapAlloc(GetProcessHeap(),
					       0,
					       (lstrlenW(fontDesc->lpstrName)+1) * sizeof(WCHAR));
  strcpyW(newObject->description.lpstrName, fontDesc->lpstrName);
  newObject->description.cySize         = fontDesc->cySize;
  newObject->description.sWeight        = fontDesc->sWeight;
  newObject->description.sCharset       = fontDesc->sCharset;
  newObject->description.fItalic        = fontDesc->fItalic;
  newObject->description.fUnderline     = fontDesc->fUnderline;
  newObject->description.fStrikethrough = fontDesc->fStrikethrough;

  /*
   * Initializing all the other members.
   */
  newObject->gdiFont  = 0;
  newObject->cyLogical  = 72L;
  newObject->cyHimetric = 2540L;
  newObject->pPropertyNotifyCP = NULL;
  newObject->pFontEventsCP = NULL;

  CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
  CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);

  if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
  {
    OLEFontImpl_Destroy(newObject);
    return NULL;
  }

  InterlockedIncrement(&ifont_cnt);

  TRACE("returning %p\n", newObject);
  return newObject;
}

/************************************************************************
 * OLEFontImpl_Destroy
 *
 * This method is called by the Release method when the reference
 * count goes down to 0. It will free all resources used by
 * this object.
 */
static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc)
{
  TRACE("(%p)\n", fontDesc);

  HeapFree(GetProcessHeap(), 0, fontDesc->description.lpstrName);

  if (fontDesc->gdiFont!=0)
    DeleteObject(fontDesc->gdiFont);

  if (fontDesc->pPropertyNotifyCP)
      IConnectionPoint_Release(fontDesc->pPropertyNotifyCP);
  if (fontDesc->pFontEventsCP)
      IConnectionPoint_Release(fontDesc->pFontEventsCP);

  HeapFree(GetProcessHeap(), 0, fontDesc);
}

2297 2298 2299 2300 2301 2302
/*******************************************************************************
 * StdFont ClassFactory
 */
typedef struct
{
    /* IUnknown fields */
2303
    const IClassFactoryVtbl    *lpVtbl;
2304
    LONG                        ref;
2305 2306
} IClassFactoryImpl;

2307
static HRESULT WINAPI
2308
SFCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
2309
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2310 2311 2312 2313 2314 2315 2316

	FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
	return E_NOINTERFACE;
}

static ULONG WINAPI
SFCF_AddRef(LPCLASSFACTORY iface) {
2317
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2318
	return InterlockedIncrement(&This->ref);
2319 2320 2321
}

static ULONG WINAPI SFCF_Release(LPCLASSFACTORY iface) {
2322
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2323
	/* static class, won't be  freed */
2324
	return InterlockedDecrement(&This->ref);
2325 2326 2327 2328 2329
}

static HRESULT WINAPI SFCF_CreateInstance(
	LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
) {
2330
	return OleCreateFontIndirect(NULL,riid,ppobj);
2331 2332 2333 2334

}

static HRESULT WINAPI SFCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2335
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2336 2337 2338 2339
	FIXME("(%p)->(%d),stub!\n",This,dolock);
	return S_OK;
}

2340
static const IClassFactoryVtbl SFCF_Vtbl = {
2341 2342 2343 2344 2345 2346 2347 2348 2349
	SFCF_QueryInterface,
	SFCF_AddRef,
	SFCF_Release,
	SFCF_CreateInstance,
	SFCF_LockServer
};
static IClassFactoryImpl STDFONT_CF = {&SFCF_Vtbl, 1 };

void _get_STDFONT_CF(LPVOID *ppv) { *ppv = (LPVOID)&STDFONT_CF; }