Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammond committed Sep 1, 1999
1 parent 267eb67 commit bb3e220
Show file tree
Hide file tree
Showing 259 changed files with 62,135 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Pythonwin/Win32app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

//
// Python Thread object
//
class PyCWinThread : public PyCCmdTarget {
protected:
PyCWinThread();
~PyCWinThread();
public:
static PyObject *create(PyObject *self, PyObject *args);
static ui_type_CObject type;
MAKE_PY_CTOR(PyCWinThread)
};

//
// Application Object.
//

class PyCWinApp : public PyCWinThread {
protected:
PyCWinApp();
~PyCWinApp();
public:
static PyObject *pExistingAppObject;

static ui_type_CObject type;
MAKE_PY_CTOR(PyCWinApp)
static void cleanup();
};


/////////////////////////////////////////////////////////////////////
//
// Hack Application objects
//
// These objects are purely to get access to protected members.
// It is never instantiated. Therefore, it must not have virtual
// functions or data items.
// It is used purely so C++ casts can override protection.
class CProtectedDocManager : public CDocManager
{
public:
CPtrList &GetTemplateList() {return m_templateList;}
};

class PYW_EXPORT CProtectedWinApp : public CWinApp {
public:
// how do I change from protected to public?
CString GetRecentFileName(int index);
void RemoveRecentFile(int index);
// Get main window - usually (but not always!) a CMDIFrameWnd
CWnd *GetMainFrame () {return m_pMainWnd;}
void SetMainFrame (CWnd *pWnd) {m_pMainWnd = pWnd;}
CDocument *FindOpenDocument (const char *lpszFileName);
BOOL Enable3dControls() {return CWinApp::Enable3dControls();}
void SetDialogBkColor(COLORREF clrCtlBk, COLORREF clrCtlText) { CWinApp::SetDialogBkColor(clrCtlBk, clrCtlText);}
BOOL HaveLoadStdProfileSettings() {return m_pRecentFileList!=NULL;}
void LoadStdProfileSettings(UINT max) {CWinApp::LoadStdProfileSettings(max);}
void SetRegistryKey(LPCTSTR key) {CWinApp::SetRegistryKey(key);}
void OnFileNew(void) {CWinApp::OnFileNew();}
void OnFileOpen(void) {CWinApp::OnFileOpen();}
CProtectedDocManager *GetDocManager();
PyObject *MakePyDocTemplateList(void);
};

class CProtectedWinThread : public CWinThread {
public:
void PumpIdle();
void PumpWaitingMessages(UINT firstMsg, UINT lastMsg);
void PumpMessages();
};

inline CWinApp *GetApp() {CWinApp *ret = AfxGetApp(); if (ret==NULL) RETURN_ERR("There is no application object"); return ret;}
inline CProtectedWinApp *GetProtectedApp() {return (CProtectedWinApp *)GetApp();}
inline CProtectedWinThread *GetProtectedThread() {return (CProtectedWinThread *)GetApp();}
//////////////////////////////////////////////////////////////////////
249 changes: 249 additions & 0 deletions Pythonwin/Win32uiHostGlue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Win32uiHostGlue.h : Defines a connection between win32ui and its
// application object.

// Sometimes I break this at the binary level - ie, all components must
// be in synch! Use a version number to check this.
#define WIN32UIHOSTGLUE_VERSION 3

class Win32uiHostGlue : public CObject {
public:
Win32uiHostGlue();
~Win32uiHostGlue();

#ifndef LINK_WITH_WIN32UI
// This will dynamically attach to win32ui.pyd.
BOOL DynamicApplicationInit(const char *cmd = NULL, const char *additionalPaths = NULL);
#else
BOOL ApplicationInit(const char *cmd = NULL, const char *additionalPaths = NULL);
#endif
// placeholder in case application want to provide custom status text.
virtual void SetStatusText(const char * /*cmd*/, int /*bForce*/) {return;}
// Helper class, to register _any_ HMODULE as a module name.
// This allows modules built into .EXE's, or in differently
// named DLL's etc. This requires admin priveliges on some machines, so
// a program should not refuse to start if this fails, but calling it
// each time means the app is guaranteed to work when moved.
// REMOVED - See below!!!
// BOOL RegisterModule(HMODULE hModule, const char *moduleName);
// or if you know the file name
// BOOL RegisterModule(const char *fileName, const char *moduleName);

// These must be called by Host Application at the relevant time.
BOOL InitInstance()
{return pfnInitInstance ? (*pfnInitInstance)() : FALSE;}
int ExitInstance(void) \
{return pfnExitInstance ? (*pfnExitInstance)() : -1;}
BOOL OnCmdMsg(CCmdTarget *pT, UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO*pHandlerInfo ) \
{return pfnOnCmdMsg ? (*pfnOnCmdMsg)(pT, nID, nCode, pExtra, pHandlerInfo) : FALSE;}
BOOL PreTranslateMessage(MSG *pMsg) \
{return pfnPreTranslateMessage ? (*pfnPreTranslateMessage)(pMsg) : FALSE;}
BOOL OnIdle( LONG lCount ) \
{return pfnOnIdle ? (*pfnOnIdle)(lCount) : FALSE;}

// This can be used as the main application "Run" method
// if you want Python to have this level of control.
int Run()
{ return pfnRun ? (*pfnRun)() : -1;}

// Must be the last thing called, ever!
void ApplicationFinalize()
{if (pfnFinalize) (*pfnFinalize)();}


// some helpers for this class.
HKEY GetRegistryRootKey();

// function pointers.
BOOL (*pfnInitInstance)();
int (*pfnExitInstance)(void);
BOOL (*pfnOnCmdMsg)(CCmdTarget *, UINT, int, void*, AFX_CMDHANDLERINFO* );
BOOL (*pfnPreTranslateMessage)(MSG *pMsg);
BOOL (*pfnOnIdle)( LONG lCount );
int (*pfnRun)();
void (*pfnFinalize)();
bool bShouldFinalizePython; // Should win32ui shut down Python?
bool bShouldAbandonThreadState; // Should win32ui abandon the thread state as it initializes?
int versionNo; // version ID of the creator of the structure.
bool bDebugBuild; // If the creator of the structure in a debug build?
bool bWantStatusBarText; // The app should want this if it wants to override the status bar.

};

inline Win32uiHostGlue::Win32uiHostGlue()
{
versionNo = WIN32UIHOSTGLUE_VERSION;
pfnInitInstance = NULL;
pfnExitInstance = NULL;
pfnOnCmdMsg = NULL;
pfnPreTranslateMessage = NULL;
pfnOnIdle = NULL;
pfnRun = NULL;
pfnFinalize = NULL;
bShouldFinalizePython = false;
bShouldAbandonThreadState = true; // Depends on how embedded.
bWantStatusBarText = false; // We can handle it by default.
bDebugBuild =
#ifdef _DEBUG
true;
#else
false;
#endif
}
inline Win32uiHostGlue::~Win32uiHostGlue()
{
}

inline HKEY Win32uiHostGlue::GetRegistryRootKey()
{
// different for win32s.
OSVERSIONINFO ver;
ver.dwOSVersionInfoSize = sizeof(ver);
GetVersionEx(&ver);
return ver.dwPlatformId == VER_PLATFORM_WIN32s ? HKEY_CLASSES_ROOT : HKEY_LOCAL_MACHINE;
}

/******
If we really need to reinstate this, we should get the current version of Python
by calling "LoadLibrary" for Python15.dll, and get the version from that!
inline BOOL Win32uiHostGlue::RegisterModule(HMODULE hModule, const char *moduleName)
{
char fname[MAX_PATH];
if (GetModuleFileName(hModule, fname, sizeof(fname))==0) {
TRACE("RegisterModule failed due to GetModuleFileName() error %d\n", GetLastError());
return FALSE;
}
return RegisterModule(fname, moduleName);
}
inline BOOL Win32uiHostGlue::RegisterModule(const char *fname, const char *moduleName)
{
const char *keyRootName = "Software\\Python\\PythonCore\\";
char keyBuf[256];
char version[10];
strcpy(keyBuf, keyRootName);
// Indirect through "CurrentVersion" - must read it.
strcat(keyBuf, "CurrentVersion");
long retSize = sizeof(version);
HKEY hkey = GetRegistryRootKey();
if (RegQueryValue(hkey, keyBuf, version, &retSize)!=ERROR_SUCCESS) {
TRACE("RegisterModule failed due to RegQueryValue() error %d\n", GetLastError());
return FALSE;
}
// Build key to write.
strcpy(keyBuf, keyRootName);
strcat(keyBuf, version);
strcat(keyBuf, "\\Modules\\");
strcat(keyBuf, moduleName);
#ifdef _DEBUG
strcat(keyBuf, "\\Debug");
#endif
if (RegSetValue(hkey,
keyBuf, REG_SZ,
fname, strlen(fname))!=ERROR_SUCCESS) {
TRACE("RegisterModule failed due to RegSetValue() error %d\n", GetLastError());
return FALSE;
}
return TRUE;
}
****/
#ifndef LINK_WITH_WIN32UI
inline BOOL Win32uiHostGlue::DynamicApplicationInit(const char *cmd, const char *additionalPaths)
{
char keyName[256];
char version[32] = "";

const char *keyRootName = "Software\\Python\\PythonCore\\";
HKEY hkey = GetRegistryRootKey();
HMODULE hModWin32ui = NULL;

#ifdef _DEBUG
HMODULE hModCore = LoadLibrary("Python15_d.dll"); // ?? variable?
char *szWinui_Name = "win32ui_d.pyd";
#else
HMODULE hModCore = LoadLibrary("Python15.dll"); // ?? variable?
char *szWinui_Name = "win32ui.pyd";
#endif
// Get the version string from the DLL.
LoadString(hModCore, 1000, version, sizeof(version));
if (GetModuleHandle(szWinui_Name)==0) { // win32ui is not yet loaded - load it.
strcpy(keyName, keyRootName);
strcat(keyName, version);
strcat(keyName, "\\Modules\\win32ui");
#ifdef _DEBUG
strcat(keyName, "\\Debug");
#endif
char path[_MAX_PATH];
long retSize = sizeof(path);
if (RegQueryValue(hkey, keyName, path, &retSize)==ERROR_SUCCESS) {
TRACE("Located registered win32ui at %s.\n", path);
hModWin32ui = LoadLibrary(path);
if (hModWin32ui==NULL) {
char buf[80];
wsprintf(buf,"Warning - win32ui registered, but module load failed (%d)\n", GetLastError());
OutputDebugString(buf);
}
}
}
// Now the modules are loaded, call the Python init functions.
int (__cdecl *pfnIsInit)(void);
pfnIsInit = (int (__cdecl *)(void))GetProcAddress(hModCore, "Py_IsInitialized");
BOOL bShouldInitPython;
if (pfnIsInit)
bShouldFinalizePython = bShouldInitPython = !(*pfnIsInit)();
else {
bShouldFinalizePython = FALSE; // Dont cleanup if we cant tell (this wont happen - Im paranoid :-)
bShouldInitPython = TRUE;
}

void (__cdecl *pfnPyInit)(void);
pfnPyInit = (void (__cdecl *)(void))GetProcAddress(hModCore, "Py_Initialize");
if (pfnPyInit && bShouldInitPython) {
(*pfnPyInit)();
}

if (hModWin32ui==NULL)
hModWin32ui = LoadLibrary(szWinui_Name);
if (hModCore) // free my temp instance - must be after win32ui, else it is unloaded!
FreeLibrary(hModCore);
if (hModWin32ui==NULL) {
char buf[256];
sprintf(buf,"The application can not locate %s (%d)\n", szWinui_Name, GetLastError());
int len = strlen(buf);
int bufLeft = sizeof(buf) - len;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL), buf+len, bufLeft, NULL);
AfxMessageBox(buf);
return FALSE;
}
BOOL (__cdecl *pfnWin32uiInit)(Win32uiHostGlue *, char *, const char *);

pfnWin32uiInit = (BOOL (__cdecl *)(Win32uiHostGlue *, char *, const char *))GetProcAddress(hModWin32ui, "Win32uiApplicationInit");
BOOL rc;
if (pfnWin32uiInit)
rc = (*pfnWin32uiInit)(this, (char *)cmd, additionalPaths);
else {
OutputDebugString("WARNING - win32uiHostGlue could not load the entry point for ApplicationInit\n");
rc = FALSE;
}
FreeLibrary(hModWin32ui);
return rc;
}
#else

extern "C" __declspec(dllimport) BOOL Win32uiApplicationInit(Win32uiHostGlue *pGlue, char *cmd, const char *addnPaths);
extern "C" void initwin32ui();

inline BOOL Win32uiHostGlue::ApplicationInit(const char *cmd, const char *additionalPaths)
{
if (!Py_IsInitialized()) {
bShouldFinalizePython = TRUE;
Py_Initialize();
}
// Make sure the statically linked win32ui is the one Python sees
// (and doesnt go searching for a new one)
initwin32ui();
return Win32uiApplicationInit(this, (char *)cmd,additionalPaths);
}

#endif
Loading

0 comments on commit bb3e220

Please sign in to comment.