您的位置:首页 > 编程语言 > Go语言

GNUstep Gorm第一个helloworld视窗程序

2010-08-18 21:34 423 查看
GNUstep是苹果系统下开发语言Objective-C在windows或linux下进行开发的一种平台,有许多关于GNUstep的第一个helloworld,但是是命令行的,图形界面,第一个视窗程序也有,在一个页面的GNUstep教程,是英文的,也有对应的繁体版本,在GNUstep 入門(官方的吧),但是非常难懂,我看了许久才明白是怎么回事,其实很简单,但是除了这个,网上就没有对应的gui helloworld的教程了,我就在这里发一个!
    前提:你必须安装 GNUstep的coresystem,在GNUstep Windows Installer页面下方,除了必须的,还有一些,比如

GNUstep Devel(包括gcc编译器)

Cairo Backend(可选)

SystemPreferences 1.1.0
Gorm 1.2.10
    接下来我们开始!
一,不用Gorm的第一个图形界面,helloworld!
    这个helloworld程序共有五个文件:main.m、AppController.h、AppController.m、helloworldInfo.plist和GNUmakefile。图形界面的设计全部在代码中,和第二个程序不一样,第二个程序的图形界面设计在.gorm的二进制文件中,如果是.nib文件,不是二进制,是xml格式的.既然在Gorm下开发,就用.gorm文件吧

Main.m

#include "AppController.h"
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool;
AppController *delegate;

pool = [[NSAutoreleasePool alloc] init];
delegate = [[AppController alloc] init];

[NSApplication sharedApplication];
[NSApp setDelegate: delegate];

RELEASE(pool);
return NSApplicationMain (argc, argv);
}


AppController.h
#ifndef _AppController_H_
#define _AppController_H_
#include <Foundation/NSObject.h>
@class NSWindow;
@class NSTextField;
@class NSNotification;

@interface AppController : NSObject
{
NSWindow *window;
NSTextField *label;
}

- (void)applicationWillFinishLaunching:(NSNotification *) not;
- (void)applicationDidFinishLaunching:(NSNotification *) not;

@end

#endif /* _AppController_H_ */


 AppController.m

#include "AppController.h"
#include <AppKit/AppKit.h>

@implementation AppController
- (void) applicationWillFinishLaunching: (NSNotification *) not
{
/* Create Menu */
NSMenu *menu;
NSMenu *info;

menu = [NSMenu new];
[menu addItemWithTitle: @"Info"
action: NULL
keyEquivalent: @""];
[menu addItemWithTitle: @"Hide"
action: @selector(hide:)
keyEquivalent: @"h"];
[menu addItemWithTitle: @"Quit"
action: @selector(terminate:)
keyEquivalent: @"q"];

info = [NSMenu new];
[info addItemWithTitle: @"Info Panel..."
action: @selector(orderFrontStandardInfoPanel:)
keyEquivalent: @""];
[info addItemWithTitle: @"Preferences"
action: NULL
keyEquivalent: @""];
[info addItemWithTitle: @"Help"
action: @selector (orderFrontHelpPanel:)
keyEquivalent: @"?"];

[menu setSubmenu: info
forItem: [menu itemWithTitle:@"Info"]];
RELEASE(info);

[NSApp setMainMenu:menu];
RELEASE(menu);

/* Create Window */
window = [[NSWindow alloc] initWithContentRect: NSMakeRect(300, 300, 200, 100)
styleMask: (NSTitledWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask)
backing: NSBackingStoreBuffered
defer: YES];
[window setTitle: @"Hello World"];

/* Create Label */
label = [[NSTextField alloc] initWithFrame: NSMakeRect(30, 30, 80, 30)];
[label setSelectable: NO];
[label setBezeled: NO];
[label setDrawsBackground: NO];
[label setStringValue: @"Hello World"];

[[window contentView] addSubview: label];
RELEASE(label);
}

- (void) applicationDidFinishLaunching: (NSNotification *) not
{
[window makeKeyAndOrderFront: self];
}

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

@end



接下来是helloworldInfo.plist文件:

{
ApplicationDescription = "Hello World Tutorial";
ApplicationIcon = "";
ApplicationName = HelloWorld;
ApplicationRelease = 0.1;
Authors = "";
Copyright = "Copyright (C) 200x by ...";
CopyrightDescription = "Released under...";
FullVersionID = 0.1;
URL = "";
}


最后是比较重要的GNUmakefile:

GNUSTEP_MAKEFILES=/GNUstep/System/Library/Makefiles

include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME = HelloWorld
HelloWorld_HEADERS = AppController.h
HelloWorld_OBJC_FILES = main.m AppController.m
HelloWorld_RESOURCE_FILES = HelloWorldInfo.plist

include $(GNUSTEP_MAKEFILES)/application.make


注意:在GNUmakefile当中,第一行为GNUstep的Makefile文件夹的变量,本来应该在路径中的,但是我运行的时候找不到这些make文件,所以我手工添加.
    接下来,把这些文件放到一个文件夹下,如helloworld1,然后运行sh,或者从GNUstep的shell,cd到这里,运行make就ok了!!如果已经运行过,会什么也不干,你需要 make clean;make就ok了!
接下来运行./HelloWorld.app/HelloWorld.exe,欣赏你的gui 的helloworld程序吧.
二,使用Gorm的第一个图形界面,helloworld!
    打开Gorm,你不会?如果你安装了Gorm,打开sh,然后运行/GNUstep/Local/Tools/Gorm,此时sh处于占用状态,Ctrl+C则会关闭Gorm.其实这个Gorm是个脚本,真正的程序在/GNUstep/Local/Applications/Gorm.app/Gorm.exe
#! /bin/sh

exec openapp "Gorm" "$@"


 下面我们开始,打开Gorm后,在主菜单,一般位于左上角,点击Document->New Application就会出现一个简单的窗口叫My Window,除了菜单,重要的设计窗口还有Untitled-7(你的程序名称,不知道怎么称呼这个窗口),Palette窗口,一般的控件都是从这里面拖出来的;Inspect窗口,设置各种属性。



接下来我们开始设计.
    1.改变My Window的大小和设置标题,拖拉此窗口右下角(鼠标形状不变,让我崩溃),让窗口变小点;标题必须在Inspect手动设置,先点击下My Window,Inspect就会出现My Window的属性,在Title文本域输入你要的标题:Hello GNUstep Window,同时标题也变了,至于窗口大小也可以在Inspect窗口改变,点击Attributes,出现下拉菜单,选择size,改变即可,这个就不贴图了.



 2.添加一个Title控件,在palettes窗口中,拖动Title到你的窗口中.



再双击这个Title,直接输入你要的内容:HelloWorld
3.再你的窗口菜单中添加info子菜单:

 有个莫民奇妙的bug,运行后的菜单info->info panel有时候无法显示,选择两次,app崩溃.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息