summaryrefslogtreecommitdiff
path: root/iup/src/win/iupwin_colordlg.c
blob: f0baa79233c587a63c2256bb5424883962e3fcec (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
/** \file
 * \brief IupColorDlg 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"


#define IUP_COLOR_RED        706

static UINT_PTR winColorDlgHookProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
  (void)wParam;
  if (uiMsg == WM_INITDIALOG)
  {
    HWND hWndItem;
    CHOOSECOLOR* choosecolor = (CHOOSECOLOR*)lParam;
    Ihandle* ih = (Ihandle*)choosecolor->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_COLOR_RED);
    SetFocus(hWndItem);
  }
  return 0;
}

static char* winColorDlgColorsToString(COLORREF* lpCustColors)
{
  int i, inc, off = 0;
  char iup_str[20];
  char* str = iupStrGetMemory(300);
  for (i=0; i < 16; i++)
  {
    inc = sprintf(iup_str, "%d %d %d;", (int)GetRValue(*lpCustColors), (int)GetGValue(*lpCustColors), (int)GetBValue(*lpCustColors));
    memcpy(str+off, iup_str, inc);
    off += inc;
    lpCustColors++;
  }
  str[off-1] = 0; /* remove last separator */
  return str;
}

static void winColorDlgStringToColors(char* str, COLORREF* lpCustColors)
{
  int i = 0;
  unsigned char r, g, b;

  while (str && *str && i < 16)
  {
    if (iupStrToRGB(str, &r, &g, &b))
      *lpCustColors = RGB(r,g,b);

    str = strchr(str, ';');
    if (str) str++;
    i++;
    lpCustColors++;
  }
}

static int winColorDlgPopup(Ihandle* ih, int x, int y)
{
  InativeHandle* parent = iupDialogGetNativeParent(ih);
  CHOOSECOLOR choosecolor;
  unsigned char r, g, b;
  COLORREF lpCustColors[16];
  char* value;

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

  if (!parent)
    parent = GetActiveWindow();

  iupStrToRGB(iupAttribGet(ih, "VALUE"), &r, &g, &b);

  ZeroMemory(lpCustColors, 16*sizeof(COLORREF));

  value = iupAttribGetStr(ih, "COLORTABLE");
  if (value)
    winColorDlgStringToColors(value, lpCustColors);

  ZeroMemory(&choosecolor, sizeof(CHOOSECOLOR));
  choosecolor.lStructSize = sizeof(CHOOSECOLOR);
  choosecolor.hwndOwner = parent;
  choosecolor.rgbResult = RGB(r, g, b);
  choosecolor.lpCustColors = lpCustColors;
  choosecolor.lCustData = (LPARAM)ih;

  choosecolor.Flags = CC_RGBINIT|CC_FULLOPEN;
  if (IupGetCallback(ih, "HELP_CB"))
    choosecolor.Flags |= CC_SHOWHELP;

  choosecolor.Flags |= CC_ENABLEHOOK;
  choosecolor.lpfnHook = (LPCCHOOKPROC)winColorDlgHookProc;

  if (!ChooseColor(&choosecolor))
  {
    iupAttribSetStr(ih, "VALUE", NULL);
    iupAttribSetStr(ih, "COLORTABLE", NULL);
    iupAttribSetStr(ih, "STATUS", NULL);
    return IUP_NOERROR;
  }

  iupAttribSetStrf(ih, "VALUE", "%d %d %d", GetRValue(choosecolor.rgbResult),
                                            GetGValue(choosecolor.rgbResult),
                                            GetBValue(choosecolor.rgbResult));
  iupAttribSetStr(ih, "COLORTABLE", winColorDlgColorsToString(lpCustColors));
  iupAttribSetStr(ih, "STATUS", "1");

  return IUP_NOERROR;
}

void iupdrvColorDlgInitClass(Iclass* ic)
{
  ic->DlgPopup = winColorDlgPopup;
}