summaryrefslogtreecommitdiff
path: root/iup/src/win/iupwin_open.c
blob: 7357cde51f42f6fa4ee5534e5c2788a39932038d (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
/** \file
 * \brief Windows Driver Core
 *
 * See Copyright Notice in "iup.h"
 */

#include <stdio.h>              
#include <stdlib.h>
#include <string.h>             

#include <windows.h>
#include <commctrl.h>

#include "iup.h"

#include "iup_object.h"
#include "iup_str.h"
#include "iup_drv.h"
#include "iup_drvfont.h"
#include "iup_globalattrib.h"

#include "iupwin_drv.h"
#include "iupwin_info.h"
#include "iupwin_handle.h"
#include "iupwin_brush.h"
#include "iupwin_draw.h"


HINSTANCE iupwin_hinstance = 0;    
int       iupwin_comctl32ver6 = 0;

void* iupdrvGetDisplay(void)
{
  return NULL;
}

void iupwinShowLastError(void)
{
  int error = GetLastError();
  if (error)
  {
    LPVOID lpMsgBuf;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL, error, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
    MessageBox(NULL, (LPCTSTR)lpMsgBuf, "GetLastError:", MB_OK|MB_ICONERROR);
    LocalFree(lpMsgBuf);
  }
}

static void winSetGlobalColor(int index, const char* name)
{
  COLORREF color = GetSysColor(index);
  iupGlobalSetDefaultColorAttrib(name, (int)GetRValue(color), 
                                       (int)GetGValue(color), 
                                       (int)GetBValue(color));
}

int iupdrvOpen(int *argc, char ***argv)
{
  (void)argc; /* unused in the Windows driver */
  (void)argv;

  if (iupwinGetSystemMajorVersion() < 5) /* older than Windows 2000 */
    return IUP_ERROR;

  IupSetGlobal("DRIVER",  "Win32");

  {
#ifdef __MINGW32__
    /* MingW fails to create windows if using a console and HINSTANCE is not from the console */
    HWND win = GetConsoleWindow();
    if (win)
      iupwin_hinstance = (HINSTANCE)GetWindowLongPtr(win, GWLP_HINSTANCE);
    else
#endif
      iupwin_hinstance = GetModuleHandle(NULL);
    IupSetGlobal("HINSTANCE", (char*)iupwin_hinstance);
  }

  CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

  {
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCtrls.dwICC = ICC_WIN95_CLASSES;  /* trackbar, tooltips, updown, tab, progress */
    InitCommonControlsEx(&InitCtrls);  
  }

  iupwin_comctl32ver6 = (iupwinGetComCtl32Version() >= 0x060000)? 1: 0;
  if (iupwin_comctl32ver6 && !iupwinIsAppThemed())  /* When the user seleted the Windows Classic theme */
    iupwin_comctl32ver6 = 0;

  IupSetGlobal("SYSTEMLANGUAGE", iupwinGetSystemLanguage());

  /* default colors */
  winSetGlobalColor(COLOR_BTNFACE, "DLGBGCOLOR");
  winSetGlobalColor(COLOR_BTNTEXT, "DLGFGCOLOR");
  winSetGlobalColor(COLOR_WINDOW,     "TXTBGCOLOR");
  winSetGlobalColor(COLOR_WINDOWTEXT, "TXTFGCOLOR");
  winSetGlobalColor(COLOR_MENU,     "MENUBGCOLOR");
  winSetGlobalColor(COLOR_MENUTEXT, "MENUFGCOLOR");

  iupwinHandleInit();
  iupwinBrushInit();
  iupwinDrawInit();

#ifdef __WATCOMC__ 
  {
    /* this is used to force Watcom to link the winmain.c module. */
    void iupwinMainDummy(void);
    iupwinMainDummy();
  }
#endif

  return IUP_NOERROR;
}

void iupdrvClose(void)
{
  iupwinHandleFinish();
  iupwinBrushFinish();

  CoUninitialize();
}