discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Gnustep for windowns version problem


From: David
Subject: Re: Gnustep for windowns version problem
Date: Mon, 15 Nov 2004 14:33:43 GMT
User-agent: Mozilla Thunderbird 0.7.1 (Windows/20040626)

Donnie Luis wrote:
I installed Gnustep with windows installer and MinGW was installed.
When I program console code which print "hello world", it works fine.
But ***libmingw32.a(main.o)(.text+0x97):main.c: undefined reference to
`WinMain@16'*** error occur if I program gui code. Need I add library
information for link with WinMain to somewhere?
What's problem and how can I solve it?

// File test1.m
/*
*/
#define WINVER 0x0500
#include <windows.h>
#include <Foundation/foundation.h>

#include "DSWinApp.h"


//--------------------------------

void readConfiguration(void)
{
}

void saveConfiguration(void)
{
}

//--------------------------------
//---------------------

int APIENTRY WinMain(HINSTANCE hInstance,
                                         HINSTANCE hPrevInstance,
                                         LPSTR lpszCmdLine,
                                         int nCmdShow
                                        )
{
        int result = 0;

        CREATE_AUTORELEASE_POOL(pool);
        readConfiguration();

        GSDebugAllocationActive(TRUE);

        id application = [DSWinApp newWithInst: hInstance
                                   withPrevInst: hPrevInstance
                                                       withCommandLine: 
lpszCmdLine
                                                           withShowCommand: 
nCmdShow];

        printf(GSDebugAllocationList(TRUE));

        if (application)
                result = [application runAppWithWindow:
                                  [DSWindow newForApp: application
                                                withInstance: hInstance
                                                                        withTitle: 
"Window Title"]];

        printf(GSDebugAllocationList(TRUE));

        saveConfiguration();
        RELEASE(pool);

        return result;
}

// end file test1.m

// File test2.m
/*
*/
#include <stdio.h>
#include <objc/objc.h>

int main(int argc, const char* argv[])
{
        printf("Hello, World!\n");
        
        return 0;
}


// end file test2.m

// File makefile

#
#
#


SOURCES = \
 DSApp.m \
 DSWindow.m

OBJS = $(SOURCES:.m=.o)
HDRS = $(SOURCES:.m=.h)

%.o : %.m
        gcc -c -O4 -mwindows -Wall -o $@ $<

test1.exe: Makefile test1.m $(OBJS) $(HDRS)
        gcc -v -O4 -mwindows -Wall -o test1.exe test1.m $(OBJS) -lobjc

test2.exe: test2.m
        gcc -o test2.exe test2.m -lobjc

clean:
        rm -fr $(OBJS) test1.exe test2.exe

// end file makefile

// File DSApp.h
#ifndef DSAPP_H
#define DSAPP_H
/*
*/
//#include <objc/objc.h>
//#include <objc/Object.h>

#include <windows.h>
#include <Foundation/foundation.h>

@interface DSApp : NSObject
{
@private
        HINSTANCE hinst;
        HWND hwndMain;
}

+ newWithInst: (HINSTANCE) instance mainWindow: (HWND) window;

- init;
- initWithInst: (HINSTANCE) instance mainWindow: (HWND) window;

- (HINSTANCE) instance;
- (HWND) window;

- (void) setInstance: (HINSTANCE) newInstance;
- (void) setWindow: (HWND) newWindow;

- (LRESULT) processMessage: (UINT) msg withWindow: (HWND) window withWParam: (WPARAM) wP withLParam: (LPARAM) lP;

@end

#endif  // DSAPP_H

// end file DSApp.h

// File DSApp.m
/*
*/
#include "DSApp.h"

@implementation DSApp

+ newWithInst: (HINSTANCE) instance mainWindow: (HWND) window
{
        return [[self alloc] initWithInst: instance mainWindow: window];
}

- init
{
        hinst = 0;
        hwndMain = 0;
        return self;
}

- initWithInst: (HINSTANCE) instance mainWindow: (HWND) window
{
        self = [super init];
        if (self != nil)
        {
                hinst = instance;
                hwndMain = window;
        }
        return self;
}

- (HINSTANCE) instance
{
        return hinst;
}

- (HWND) window
{
        return hwndMain;
}

- (void) setInstance: (HINSTANCE) newInstance
{
        hinst = newInstance;
}

- (void) setWindow: (HWND) newWindow
{
        hwndMain = newWindow;
}

- (LRESULT) processMessage: (UINT) msg withWindow: (HWND) window withWParam: (WPARAM) wP withLParam: (LPARAM) lP
{
        switch (msg)
        {
                case WM_ACTIVATE:
                        break;

                case WM_ACTIVATEAPP:
                        break;

                case WM_CREATE:
                        break;

                case WM_PAINT:
                        break;

                case WM_DESTROY:
                        PostQuitMessage(0);
                        return 0;
        }
        
        return DefWindowProc(window, msg, wP, lP);
}
@end

// end file DSApp.m

// File GNUmakefile
#
# makefile
#
include $(GNUSTEP_MAKEFILES)/common.make

OPTFLAG = -O4

TOOL_NAME = WinAppTest

WinAppTest_HEADERS = DSWinApp.h DSWindow.h DSMenu.h
WinAppTest_OBJC_FILES = test1.m DSWinApp.m DSWindow.m DSMenu.m

ADDITIONAL_LDFLAGS = -mwindows

include $(GNUSTEP_MAKEFILES)/tool.make

// end file GNUmakefile

// File DSWinApp.h
#ifndef DSWINAPP_H
#define DSWINAPP_H
/*
*/
#include <windows.h>
#include <Foundation/foundation.h>

#include "DSWindow.h"

@interface DSWinApp : NSObject
{
@private
        HINSTANCE instance;
        HINSTANCE prevInstance;
        LPSTR commandLine;
        int showCommand;
}

+ newWithInst: (HINSTANCE) hInstance
  withPrevInst: (HINSTANCE) hPrevInstance
  withCommandLine: (LPSTR) lpszCmdLine
  withShowCommand: (int) nCmdShow;

- (void) dealloc;

- init;
- initWithInst: (HINSTANCE) hInstance
  withPrevInst: (HINSTANCE) hPrevInstance
  withCommandLine: (LPSTR) lpszCmdLine
  withShowCommand: (int) nCmdShow;

- (int) runAppWithWindow: (DSWindow*) window;

- (LRESULT) processMessage: (UINT) msg
            withWindow: (HWND) window
                        withWParam: (WPARAM) wP
                        withLParam: (LPARAM) lP;

@end

#endif  // DSWINAPP_H
// end file DSWinApp.h
// File DSWinApp.m
/*
*/
#define WINVER 0x0500
#include "DSWinApp.h"

@implementation DSWinApp

// Single, allocated application instance.
static id theApp = nil;

LRESULT CALLBACK DSWinAppWndProc(HWND window, UINT msg, WPARAM wP, LPARAM lP)
{
        // Initialise the return value
        LRESULT result = 0;

        // Has the window got an embedded window class?
        id win = (id) GetWindowLong(window, 0);
        if (win)
        {
                // Process the message, return zero for further processing.
result = [win processMessage: msg withWindow: window withWParam: wP withLParam: lP];
        }

        // Did the window process the message?
        if (!result)
        {
                // Has the window got an embedded application class?
                id app = (id) GetClassLong(window, 0);
                if (app)
                {
                        // Yes, maybe the application can process the message
                        // Process the message, return zero for further 
processing.
result = [app processMessage: msg withWindow: window withWParam: wP withLParam: lP];
                }
                else
                {
                        // No, the is the static reference valid yet?
                        if (theApp)
                        {
// Yes, the have the application process the message through the static pointer. // this only happens during window creation, before the embedded pointers are initialised.
                                // @see: [DSWindow 
-initForApp:withInstance:withTitle:].
                                // Process the message, return zero for further 
processing.
result = [theApp processMessage: msg withWindow: window withWParam: wP withLParam: lP];
                        }
                }
        }

        // Did anyone process the message yet?
        if (!result)
        {
                // No, then use the default processor
                result = DefWindowProc(window, msg, wP, lP);
        }

        return result;
}

+ newWithInst: (HINSTANCE) hInstance
  withPrevInst: (HINSTANCE) hPrevInstance
  withCommandLine: (LPSTR) lpszCmdLine
  withShowCommand: (int) nCmdShow
{
        DSWinApp* app;

        if (!theApp)
                app = [self alloc];

        app = [app initWithInst: hInstance
                   withPrevInst: hPrevInstance
               withCommandLine: lpszCmdLine
                       withShowCommand: nCmdShow];

        theApp = app;

        return AUTORELEASE(app);
}

- init
{
        return [self initWithInst: 0
                     withPrevInst: 0
                                 withCommandLine: 0
                                 withShowCommand: 0];
}

- initWithInst: (HINSTANCE) hInstance
  withPrevInst: (HINSTANCE) hPrevInstance
  withCommandLine: (LPSTR) lpszCmdLine
  withShowCommand: (int) nCmdShow
{
        self = [super init];
        if (self != nil)
        {
                instance = hInstance;
                prevInstance = hPrevInstance;
                commandLine = lpszCmdLine;
                showCommand = nCmdShow;

                if (instance)
                {
                        if (!prevInstance)
                        {
                                WNDCLASSEX appWindowClass;

                                appWindowClass.cbSize            = 
sizeof(appWindowClass);
                                appWindowClass.style         = 0;
                                appWindowClass.lpfnWndProc   = (WNDPROC) 
DSWinAppWndProc;
                                appWindowClass.cbClsExtra    = sizeof(id);
                                appWindowClass.cbWndExtra    = sizeof(id);
                                appWindowClass.hInstance     = instance;
appWindowClass.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
                                appWindowClass.hCursor       = 
LoadCursor((HINSTANCE) NULL, IDC_ARROW);
                                appWindowClass.hbrBackground = 
GetStockObject(WHITE_BRUSH);
                                appWindowClass.lpszMenuName  = 0;
                                appWindowClass.lpszClassName = 
DSWINAPP_WINDOWCLASS_NAME;
                                appWindowClass.hIconSm           = 0;

                                RegisterClassEx(&appWindowClass);
                        }
                }
        }

        return self;
}

- (void) dealloc
{
        // free resources
        // unregister class
        UnregisterClass(DSWINAPP_WINDOWCLASS_NAME, instance);

        // clean up
        [super dealloc];

        theApp = nil;
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

- (int) runAppWithWindow: (DSWindow*) window
{
    MSG msg;
    BOOL bRet;

    // Show the window and paint its contents.
        [window showAndPaint: showCommand];

    // Start the message loop.
    while( (bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (bRet == (BOOL)-1)
        {
            // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    // Return the exit code to the system.
    return msg.wParam;
}

//-------------------------------------------------------------------------

- (LRESULT) processMessage: (UINT) msg
            withWindow: (HWND) window
                        withWParam: (WPARAM) wP
                        withLParam: (LPARAM) lP
{
        switch (msg)
        {
                case WM_CREATE:
MessageBox(0, "DSWinApp: WM_CREATE activated", "Daves Error Service", MB_OK);
                        break;

                case WM_CLOSE:
MessageBox(0, "DSWinApp: WM_CLOSE activated", "Daves Error Service", MB_OK);
                        break;

                case WM_DESTROY:
MessageBox(0, "DSWinApp: WM_DESTROY activated", "Daves Error Service", MB_OK);
                        PostQuitMessage(0);
                        return 1;
        }

        // return 0 for default processing to handle message
        // return 1 for no further processing
        return 0;
}
@end
// end file DSWinApp.m
// File DSWindow.h
#ifndef DSWINDOW_H
#define DSWINDOW_H
/*
*/

#include <windows.h>
#include <Foundation/foundation.h>

#include "DSMenu.h"

#define DSWINAPP_WINDOWCLASS_NAME "OBJC_WINDOW_CLASS"

@interface DSWindow : NSObject
{
@private
        HINSTANCE theApplicationInstance;
        HWND      theWindow;
        id        theMenu;
}

+ newForApp: (id) app
  withInstance: (HINSTANCE) inst
  withTitle: (char*) title;

- (void) dealloc;

- init;
- initForApp: (id) app
  withInstance: (HINSTANCE) inst
  withTitle: (char*) title;

- (void) showAndPaint: (int) showCmd;

- (LRESULT) processMessage: (UINT) msg
            withWindow: (HWND) window
                        withWParam: (WPARAM) wP
                        withLParam: (LPARAM) lP;

@end

#endif  // DSWINDOW_H
// end file DSWindow.h
// File DSWindow.m
/*
*/
#define WINVER 0x0500
#include "DSWindow.h"

@implementation DSWindow

+ newForApp: (id) app
  withInstance: (HINSTANCE) inst
  withTitle: (char*) title
{
        DSWindow* w = [[self alloc] initForApp: app
                                    withInstance: inst
                                                                withTitle: 
title];
        return AUTORELEASE(w);
}

- init
{
        return [self initForApp: nil
                     withInstance: 0
                                 withTitle: 0];
}

- initForApp: (id) app
  withInstance: (HINSTANCE) inst
  withTitle: (char*) title
{
        self = [super init];
        if (self != nil)
        {
                if (app != nil && inst != 0)
                {
                        DWORD styles = WS_OVERLAPPEDWINDOW;
                        DWORD extendedStyles = 0;

                        theApplicationInstance = inst;

                        theWindow = CreateWindowEx(extendedStyles,
                                DSWINAPP_WINDOWCLASS_NAME, title, styles,
                                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
CW_USEDEFAULT,
                                (HWND) NULL, (HMENU) NULL,
                                theApplicationInstance, (LPVOID) NULL);

                        if (theWindow)
                        {
                                // store a reference for the non-class windows 
function
                                SetClassLong(theWindow, 0, (LONG) app);
                                SetWindowLong(theWindow, 0, (LONG) self);
                        }
                }
        }

        return self;
}

- (void) dealloc
{
        // free resources
        // clean up
        [super dealloc];
}

- (void) showAndPaint: (int) showCmd
{
        // Add menu
        theMenu = [DSMenu newMainMenuForWindow: theWindow];

        // Add additional controls here

    ShowWindow(theWindow, showCmd);
    UpdateWindow(theWindow);
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//
// The WM_COMMAND message is sent when the user selects a command
// item from a menu, when a control sends a notification message
// to its parent window, or when an accelerator keystroke is translated.
- (LRESULT) onCommand: (HWND) windowFrom withWp: (WPARAM) wP withLp: (LPARAM) lP
{
        // wParam - The high-order word specifies the notification code
        //          if the message is from a control. If the message is
        //          from an accelerator, this value is 1. If the message
        //          is from a menu, this value is zero.
        //          The low-order word specifies the identifier of the
        //          menu item, control, or accelerator.
        //
        // lParam - Handle to the control sending the message if the
        //          message is from a control. Otherwise, this parameter
        //          is NULL.
        //
        // If an application processes this message, it should return zero.
        //
        // menu command and valid menu handle?
        if (HIWORD(wP) == 0 && theMenu)
                return [theMenu processMenuSelection: LOWORD(wP)];

        // accelerator command
        if (HIWORD(wP) == 1)
                return 0;

        // control command
        return 0;
}

//-------------------------------------------------------------------------

- (LRESULT) processMessage: (UINT) msg
            withWindow: (HWND) window
                        withWParam: (WPARAM) wP
                        withLParam: (LPARAM) lP
{
        switch (msg)
        {
                case WM_COMMAND:
                        return [self onCommand: window withWp: wP withLp: lP];

                // when window gains focus
                case WM_ACTIVATE:
                        break;

                // when application gains focus
                case WM_ACTIVATEAPP:
                        break;

                case WM_CREATE:
MessageBox(0, "DSWindow: WM_CREATE activated", "Daves Error Service", MB_OK);
                        break;

                case WM_PAINT:
                        break;

                case WM_CLOSE:
MessageBox(0, "DSWindow: WM_CLOSE activated", "Daves Error Service", MB_OK);
                        break;

                case WM_DESTROY:
MessageBox(0, "DSWindow: WM_DESTROY activated", "Daves Error Service", MB_OK);
                        break;
        }

        // return 0 for application to handle message
        // return 1 for no further processing
        return 0;
}

@end
// end file DSWindow.m
// File DSMenu.h
#ifndef DSMENU_H
#define DSMENU_H
/*
*/
#include <windows.h>
#include <Foundation/foundation.h>

@interface DSMenu : NSObject
{
@private
        HWND  parent;
        HMENU menuBar;
}

+ newMainMenuForWindow: (HWND) theWindow;

- init;
- initMainMenuForWindow: (HWND) theWindow;

- (void) dealloc;

- (int) processMenuSelection: (int) itemCode;

/*
- (LRESULT) processMessage: (UINT) msg
            withWindow: (HWND) window
                        withWParam: (WPARAM) wP
                        withLParam: (LPARAM) lP;
*/
@end

#endif  // DSMENU_H
// end file DSMenu.h
// File DSMenu.m
/*
*/
#define WINVER 0x0500
#include "DSMenu.h"

@implementation DSMenu

+ newMainMenuForWindow: (HWND) theWindow
{
        DSMenu* menu = [[self alloc] initMainMenuForWindow: theWindow];
        return AUTORELEASE(menu);
}

- init
{
        return [self initMainMenuForWindow: 0];
}

- (MENUITEMINFO*) makeMenuSeparator
{
        static MENUITEMINFO minfo;

        minfo.cbSize        = sizeof(minfo);
        minfo.fMask         = (UINT) MIIM_TYPE;
        minfo.fType         = (UINT) MFT_SEPARATOR;
        minfo.fState        = (UINT) MFS_ENABLED;
        minfo.wID           = (UINT) 0;
        minfo.hSubMenu      = (HMENU) 0;
        minfo.hbmpChecked   = (HBITMAP) 0;
        minfo.hbmpUnchecked = (HBITMAP) 0;
        minfo.dwItemData    = (ULONG_PTR) 0;
        minfo.dwTypeData    = (LPTSTR) 0;
        minfo.cch           = (UINT) 0;
        minfo.hbmpItem      = (HBITMAP) 0;

        return &minfo;
}

- (MENUITEMINFO*) makeMenuItem: (char*) text withItemNum: (int) num withSubMenu: (HMENU) sub
{
        static MENUITEMINFO minfo;

        minfo.cbSize        = sizeof(minfo);
minfo.fMask = (UINT) MIIM_FTYPE | MIIM_STRING | MIIM_SUBMENU | MIIM_STATE | MIIM_ID;
        minfo.fType         = (UINT) MFT_STRING;
        minfo.fState        = (UINT) MFS_ENABLED;
        minfo.wID           = (UINT) num;
        minfo.hSubMenu      = sub;
        minfo.hbmpChecked   = (HBITMAP) 0;
        minfo.hbmpUnchecked = (HBITMAP) 0;
        minfo.dwItemData    = (ULONG_PTR) 0;
        minfo.dwTypeData    = (LPTSTR) text;
        minfo.cch           = (UINT) strlen(text);
        minfo.hbmpItem      = (HBITMAP) 0;

        return &minfo;
}

- initMainMenuForWindow: (HWND) theWindow
{
        HMENU mnu;

        self = [super init];
        if (self != nil)
        {
                // create the menu
                menuBar = CreateMenu();

                mnu = CreatePopupMenu();
InsertMenuItem(menuBar, 0, TRUE, [self makeMenuItem: "&File" withItemNum: 1 withSubMenu: mnu]); InsertMenuItem(mnu, 0, TRUE, [self makeMenuItem: "&Open" withItemNum: 1 withSubMenu: 0]); InsertMenuItem(mnu, 1, TRUE, [self makeMenuItem: "&Close" withItemNum: 2 withSubMenu: 0]);
                InsertMenuItem(mnu, 2, TRUE, [self makeMenuSeparator]);
InsertMenuItem(mnu, 3, TRUE, [self makeMenuItem: "E&xit" withItemNum: 3 withSubMenu: 0]);

                mnu = CreatePopupMenu();
InsertMenuItem(menuBar, 1, TRUE, [self makeMenuItem: "&Edit" withItemNum: 2 withSubMenu: mnu]); InsertMenuItem(mnu, 0, TRUE, [self makeMenuItem: "&Cut" withItemNum: 1 withSubMenu: 0]); InsertMenuItem(mnu, 1, TRUE, [self makeMenuItem: "&Copy" withItemNum: 2 withSubMenu: 0]); InsertMenuItem(mnu, 2, TRUE, [self makeMenuItem: "&Paste" withItemNum: 3 withSubMenu: 0]);

                // attach the menu
                SetMenu(theWindow, menuBar);
        }
        return self;
}

- (void) dealloc
{
        [super dealloc];
}

- (int) processMenuSelection: (int) itemCode
{
        char buf[128];
        sprintf(buf, "[DSMenu processMenuSelection: %08x]", itemCode);
        MessageBox(0, buf, "Daves Error Service", MB_OK);
        return 1;
}

/*
LRESULT CALLBACK DSMenuWndProc(HWND window, UINT msg, WPARAM wP, LPARAM lP)
{
        LRESULT result = 0;

        // use stored reference to locate handler class
        // if no class set, use the default handler

        DSWinApp* app = (DSWinApp*) GetClassLong(window, 0);
        DSWindow* win = (DSWindow*) GetWindowLong(window, 0);

//      if (win && msg != WM_DESTROY)
        if (win)
result = [win processMessage: msg withWindow: window withWParam: wP withLParam: lP];

        if (!result)
                if (app)
result = [app processMessage: msg withWindow: window withWParam: wP withLParam: lP];
                else if (theApp)
result = [theApp processMessage: msg withWindow: window withWParam: wP withLParam: lP];

        if (!result)
                result = DefWindowProc(window, msg, wP, lP);

        return result;
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

- (LRESULT) processMessage: (UINT) msg
            withWindow: (HWND) window
                        withWParam: (WPARAM) wP
                        withLParam: (LPARAM) lP
{
        switch (msg)
        {
                case WM_CREATE:
MessageBox(0, "DSWinApp: WM_CREATE activated", "Daves Error Service", MB_OK);
                        break;

                case WM_CLOSE:
MessageBox(0, "DSWinApp: WM_CLOSE activated", "Daves Error Service", MB_OK);
                        break;

                case WM_DESTROY:
MessageBox(0, "DSWinApp: WM_DESTROY activated", "Daves Error Service", MB_OK);
                        PostQuitMessage(0);
                        return 1;
        }

        // return 0 for default processing to handle message
        // return 1 for no further processing
        return 0;
}
*/
@end
// end file DSMenu.m


reply via email to

[Prev in Thread] Current Thread [Next in Thread]