Chinaunix首页 | 论坛 | 博客
  • 博客访问: 833163
  • 博文数量: 117
  • 博客积分: 2595
  • 博客等级: 少校
  • 技术积分: 1204
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-18 18:16
文章分类

全部博文(117)

文章存档

2013年(1)

2012年(2)

2011年(18)

2010年(95)

2009年(1)

分类: C/C++

2010-04-14 11:45:06

    GNUstep是苹果系统下开发语言Objective-C在windows或linux下进行开发的一种平台,为我们这些暂时没有苹果电脑,却想要学习的人提供了方便,但是毕竟是不同,没有太多教程,靠我们自己摸索了.
    网上倒是有许多关于GNUstep的第一个helloworld,但是是命令行的,图形界面,第一个视窗程序也有,,是英文的,也有对应的繁体版本,在,但是非常难懂,我看了许久才明白是怎么回事,其实很简单,但是除了这个,网上就没有对应的gui helloworld的教程了,我就在这里发一个!
    前提:你必须安装 GNUstep的core和system,安装一个应用程序app,在,除了必须的,还有一些app,如下,选择Gorm,下载!
    接下来我们开始!
一,不用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程序吧.
    附件:如果你懒得自己copy或者自己写,我传个附件:Helloworld1

二,使用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崩溃.






阅读(5392) | 评论(5) | 转发(0) |
给主人留下些什么吧!~~

bombless2013-01-29 17:23:16

我晕,我用的Ubuntu带的GNUstep有这个bug,但是Windows版的就没有……

bombless2013-01-29 17:19:10

悲剧,2年过去了,现在的GNUstep仍然有这个bug:点那个“Info Panel...”每次都崩溃……

chinaunix网友2010-08-25 14:38:48

呵呵,谢谢你的回答哦,我现在公司里没macOS,想在windows下先过把隐

a525274592010-08-20 13:04:16

你好,到哪里你不会用了? 比如安装后,不知道如何打开sh,(其实就再安装目录下一个批处理); 建议你去看看 http://www.nongnu.org/gstutorial/zh_TW/官方教程。 我这个是作为补充吧。。(其实现在我已经不做这个了,还是在macOS 下好)

chinaunix网友2010-08-18 10:47:02

我下了你给的Gorm,但不会用,能说具体点吗?谢谢