summaryrefslogtreecommitdiff
path: root/iup/src/win/iupwin_fontdlg.c
blob: 0602441c0b8c793e16b05d7be4a0c31491256ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/** \file
 * \brief IupFontDlg pre-defined dialog
 *
 * See Copyright Notice in "iup.h"
 */

#include <windows.h>
#include <stdio.h>

#include "iup.h"

#include "iup_object.h"
#include "iup_attrib.h"
#include "iup_str.h"
#include "iup_dialog.h"
#include "iup_drvfont.h"

#include "iupwin_drv.h"


#define IUP_FONTFAMILYCOMBOBOX        0x0470

static UINT_PTR winFontDlgHookProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
  (void)wParam;
  if (uiMsg == WM_INITDIALOG)
  {
    HWND hWndItem;
    CHOOSEFONT* choosefont = (CHOOSEFONT*)lParam;
    Ihandle* ih = (Ihandle*)choosefont->lCustData;

    char* value = iupAttribGet(ih, "TITLE");
    if (value)
      SetWindowText(hWnd, value);

    ih->handle = hWnd;
    iupDialogUpdatePosition(ih);
    ih->handle = NULL;  /* reset handle */
    iupAttribSetStr(ih, "HWND", (char*)hWnd);  /* used by HELP_CB in winDialogBaseProc */

    hWndItem = GetDlgItem(hWnd, IUP_FONTFAMILYCOMBOBOX);
    SetFocus(hWndItem);
  }
  return 0;
}

static int winFontDlgPopup(Ihandle* ih, int x, int y)
{
  InativeHandle* parent = iupDialogGetNativeParent(ih);
  unsigned char r, g, b;
  CHOOSEFONT choosefont;
  LOGFONT logfont;
  char* standardfont;
  int height_pixels;
  char typeface[50] = "";
  int height = 8;
  int is_bold = 0,
    is_italic = 0, 
    is_underline = 0,
    is_strikeout = 0;
  int res = iupwinGetScreenRes();

  iupAttribSetInt(ih, "_IUPDLG_X", x);   /* used in iupDialogUpdatePosition */
  iupAttribSetInt(ih, "_IUPDLG_Y", y);

  if (!parent)
    parent = GetActiveWindow();

  standardfont = iupAttribGet(ih, "VALUE");
  if (!standardfont)
    return IUP_ERROR;

  /* parse the old format first */
  if (!iupFontParseWin(standardfont, typeface, &height, &is_bold, &is_italic, &is_underline, &is_strikeout))
  {
    if (!iupFontParsePango(standardfont, typeface, &height, &is_bold, &is_italic, &is_underline, &is_strikeout))
      return IUP_ERROR;
  }

  /* get size in pixels */
  if (height < 0)
    height_pixels = height;  /* already in pixels */
  else
    height_pixels = -IUPWIN_PT2PIXEL(height, res);

  if (height_pixels == 0)
    return IUP_ERROR;

  ZeroMemory(&choosefont, sizeof(CHOOSEFONT));
  choosefont.lStructSize = sizeof(CHOOSEFONT);

  if (iupStrToRGB(iupAttribGet(ih, "COLOR"), &r, &g, &b))
    choosefont.rgbColors = RGB(r, g, b);
  
  choosefont.hwndOwner = parent;
  choosefont.lpLogFont = &logfont;
  choosefont.Flags = CF_SCREENFONTS | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_ENABLEHOOK;
  choosefont.lCustData = (LPARAM)ih;
  choosefont.lpfnHook = (LPCFHOOKPROC)winFontDlgHookProc;

  if (IupGetCallback(ih, "HELP_CB"))
    choosefont.Flags |= CF_SHOWHELP;

  strcpy(logfont.lfFaceName, typeface);
  logfont.lfHeight = height_pixels;
  logfont.lfWeight = (is_bold)? FW_BOLD: FW_NORMAL;
  logfont.lfItalic = (BYTE)is_italic;
  logfont.lfUnderline = (BYTE)is_underline;
  logfont.lfStrikeOut = (BYTE)is_strikeout;

  logfont.lfCharSet = DEFAULT_CHARSET;
  logfont.lfEscapement = 0; 
  logfont.lfOrientation = 0;
  logfont.lfWidth = 0; 
  logfont.lfOutPrecision = OUT_TT_PRECIS; 
  logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS; 
  logfont.lfQuality = DEFAULT_QUALITY; 
  logfont.lfPitchAndFamily = FF_DONTCARE|DEFAULT_PITCH; 

  if (!ChooseFont(&choosefont))
  {
    iupAttribSetStr(ih, "VALUE", NULL);
    iupAttribSetStr(ih, "COLOR", NULL);
    iupAttribSetStr(ih, "STATUS", NULL);
    return IUP_NOERROR;
  }

  is_bold = (logfont.lfWeight == FW_NORMAL)? 0: 1;
  is_italic = logfont.lfItalic;
  is_underline = logfont.lfUnderline;
  is_strikeout = logfont.lfStrikeOut;
  height_pixels = logfont.lfHeight;

  if (height < 0) /* not an error, use old value as a reference for the units */
    height = height_pixels;   /* return in pixels */
  else
    height = IUPWIN_PIXEL2PT(-height_pixels, res);   /* return in points */

  iupAttribSetStrf(ih, "VALUE", "%s, %s%s%s%s %d", logfont.lfFaceName, 
                                                    is_bold?"Bold ":"", 
                                                    is_italic?"Italic ":"", 
                                                    is_underline?"Underline ":"", 
                                                    is_strikeout?"Strikeout ":"", 
                                                    height);

  iupAttribSetStrf(ih, "COLOR", "%d %d %d", GetRValue(choosefont.rgbColors),
                                            GetGValue(choosefont.rgbColors),
                                            GetBValue(choosefont.rgbColors));
  iupAttribSetStr(ih, "STATUS", "1");

  return IUP_NOERROR;
}

void iupdrvFontDlgInitClass(Iclass* ic)
{
  ic->DlgPopup = winFontDlgPopup;

  /* IupFontDialog Windows Only */
  iupClassRegisterAttribute(ic, "COLOR", NULL, NULL, NULL, NULL, IUPAF_NO_INHERIT);
}