Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 770:429ff505c450
prev765:4cd066048203
next773:146c3210768b
author nkeynes
date Mon Jul 28 03:41:25 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Implement key-binding configuration pane for Cocoa UI
Minor tweaks for consistency and static-correctness
file annotate diff log raw
nkeynes@681
     1
/**
nkeynes@681
     2
 * $Id$
nkeynes@681
     3
 *
nkeynes@681
     4
 * Core Cocoa-based user interface
nkeynes@681
     5
 *
nkeynes@725
     6
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@681
     7
 *
nkeynes@681
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@681
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@681
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@681
    11
 * (at your option) any later version.
nkeynes@681
    12
 *
nkeynes@681
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@681
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@681
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@681
    16
 * GNU General Public License for more details.
nkeynes@681
    17
 */
nkeynes@681
    18
nkeynes@681
    19
#include <AppKit/AppKit.h>
nkeynes@681
    20
#include <stdio.h>
nkeynes@681
    21
#include <stdlib.h>
nkeynes@685
    22
#include <string.h>
nkeynes@681
    23
#include <sys/time.h>
nkeynes@681
    24
#include "lxdream.h"
nkeynes@718
    25
#include "dream.h"
nkeynes@681
    26
#include "dreamcast.h"
nkeynes@718
    27
#include "config.h"
nkeynes@718
    28
#include "display.h"
nkeynes@681
    29
#include "gui.h"
nkeynes@681
    30
#include "cocoaui/cocoaui.h"
nkeynes@681
    31
nkeynes@681
    32
void cocoa_gui_update( void );
nkeynes@681
    33
void cocoa_gui_start( void );
nkeynes@681
    34
void cocoa_gui_stop( void );
nkeynes@681
    35
void cocoa_gui_run_later( void );
nkeynes@681
    36
uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
nkeynes@681
    37
nkeynes@681
    38
struct dreamcast_module cocoa_gui_module = { "gui", NULL,
nkeynes@736
    39
        cocoa_gui_update, 
nkeynes@736
    40
        cocoa_gui_start, 
nkeynes@736
    41
        cocoa_gui_run_slice, 
nkeynes@736
    42
        cocoa_gui_stop, 
nkeynes@736
    43
        NULL, NULL };
nkeynes@681
    44
nkeynes@681
    45
/**
nkeynes@681
    46
 * Count of running nanoseconds - used to cut back on the GUI runtime
nkeynes@681
    47
 */
nkeynes@681
    48
static uint32_t cocoa_gui_nanos = 0;
nkeynes@681
    49
static uint32_t cocoa_gui_ticks = 0;
nkeynes@681
    50
static struct timeval cocoa_gui_lasttv;
nkeynes@681
    51
static BOOL cocoa_gui_autorun = NO;
nkeynes@693
    52
static BOOL cocoa_gui_is_running = NO;
nkeynes@681
    53
nkeynes@681
    54
@interface NSApplication (PrivateAdditions)
nkeynes@681
    55
- (void) setAppleMenu:(NSMenu *)aMenu;
nkeynes@681
    56
@end
nkeynes@681
    57
nkeynes@681
    58
static void cocoa_gui_create_menu(void)
nkeynes@681
    59
{
nkeynes@681
    60
    NSMenu *appleMenu, *services;
nkeynes@681
    61
    NSMenuItem *menuItem;
nkeynes@681
    62
    NSString *title;
nkeynes@681
    63
    NSString *appName;
nkeynes@736
    64
nkeynes@681
    65
    appName = @"Lxdream";
nkeynes@681
    66
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
nkeynes@681
    67
nkeynes@681
    68
    /* Add menu items */
nkeynes@681
    69
    title = [@"About " stringByAppendingString:appName];
nkeynes@725
    70
    [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
nkeynes@738
    71
    
nkeynes@765
    72
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@765
    73
    [appleMenu addItemWithTitle: NS_("Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
nkeynes@736
    74
nkeynes@725
    75
    // Services Menu
nkeynes@681
    76
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@681
    77
    services = [[[NSMenu alloc] init] autorelease];
nkeynes@681
    78
    [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
nkeynes@681
    79
    [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
nkeynes@681
    80
nkeynes@681
    81
    // Hide AppName
nkeynes@681
    82
    title = [@"Hide " stringByAppendingString:appName];
nkeynes@681
    83
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
nkeynes@681
    84
nkeynes@681
    85
    // Hide Others
nkeynes@681
    86
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
nkeynes@736
    87
                              action:@selector(hideOtherApplications:) 
nkeynes@736
    88
                              keyEquivalent:@"h"];
nkeynes@681
    89
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@681
    90
nkeynes@681
    91
    // Show All
nkeynes@681
    92
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
nkeynes@681
    93
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@681
    94
nkeynes@681
    95
    // Quit AppName
nkeynes@681
    96
    title = [@"Quit " stringByAppendingString:appName];
nkeynes@681
    97
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
nkeynes@681
    98
nkeynes@681
    99
    /* Put menu into the menubar */
nkeynes@681
   100
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
nkeynes@681
   101
    [menuItem setSubmenu: appleMenu];
nkeynes@681
   102
    NSMenu *menu = [NSMenu new];
nkeynes@681
   103
    [menu addItem: menuItem];
nkeynes@736
   104
nkeynes@691
   105
    NSMenu *gdromMenu = cocoa_gdrom_menu_new();
nkeynes@736
   106
nkeynes@681
   107
    NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
nkeynes@681
   108
    [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
nkeynes@691
   109
    [[fileMenu addItemWithTitle: NS_("GD-Rom") action: nil keyEquivalent: @""]
nkeynes@691
   110
      setSubmenu: gdromMenu];
nkeynes@681
   111
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@681
   112
    [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
nkeynes@681
   113
      setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@681
   114
    [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
nkeynes@681
   115
    [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
nkeynes@681
   116
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@681
   117
    [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
nkeynes@681
   118
    [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
nkeynes@736
   119
nkeynes@681
   120
    menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
nkeynes@681
   121
    [menuItem setSubmenu: fileMenu];
nkeynes@681
   122
    [menu addItem: menuItem];
nkeynes@736
   123
nkeynes@681
   124
    /* Tell the application object that this is now the application menu */
nkeynes@681
   125
    [NSApp setMainMenu: menu];
nkeynes@681
   126
    [NSApp setAppleMenu: appleMenu];
nkeynes@681
   127
    [NSApp setServicesMenu: services];
nkeynes@681
   128
nkeynes@681
   129
    /* Finally give up our references to the objects */
nkeynes@681
   130
    [appleMenu release];
nkeynes@681
   131
    [menuItem release];
nkeynes@681
   132
    [menu release];
nkeynes@681
   133
}
nkeynes@681
   134
nkeynes@681
   135
@interface LxdreamDelegate : NSObject
nkeynes@681
   136
@end
nkeynes@681
   137
nkeynes@681
   138
@implementation LxdreamDelegate
nkeynes@681
   139
- (void)windowWillClose: (NSNotification *)notice
nkeynes@681
   140
{
nkeynes@681
   141
    dreamcast_shutdown();
nkeynes@681
   142
    exit(0);
nkeynes@681
   143
}
nkeynes@681
   144
- (void)windowDidBecomeMain: (NSNotification *)notice
nkeynes@681
   145
{
nkeynes@681
   146
    if( cocoa_gui_autorun ) {
nkeynes@681
   147
        cocoa_gui_autorun = NO;
nkeynes@681
   148
        cocoa_gui_run_later();
nkeynes@681
   149
    }
nkeynes@681
   150
}
nkeynes@725
   151
- (void) about_action: (id)sender
nkeynes@725
   152
{
nkeynes@725
   153
    NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
nkeynes@738
   154
    NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright),  nil];
nkeynes@725
   155
nkeynes@725
   156
    NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
nkeynes@725
   157
nkeynes@725
   158
    [NSApp orderFrontStandardAboutPanelWithOptions: options];
nkeynes@725
   159
}
nkeynes@725
   160
- (void) preferences_action: (id)sender
nkeynes@725
   161
{
nkeynes@765
   162
    cocoa_gui_show_preferences();
nkeynes@725
   163
}
nkeynes@681
   164
- (void) load_action: (id)sender
nkeynes@681
   165
{
nkeynes@681
   166
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@718
   167
    const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
nkeynes@718
   168
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@681
   169
    NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
nkeynes@718
   170
    int result = [panel runModalForDirectory: path file: nil types: fileTypes];
nkeynes@681
   171
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@681
   172
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@681
   173
        dreamcast_load_state( [filename UTF8String] );
nkeynes@681
   174
    }
nkeynes@681
   175
}
nkeynes@681
   176
- (void) save_action: (id)sender
nkeynes@681
   177
{
nkeynes@681
   178
    NSSavePanel *panel = [NSSavePanel savePanel];
nkeynes@718
   179
    const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
nkeynes@718
   180
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@681
   181
    [panel setRequiredFileType: @"dst"];
nkeynes@718
   182
    int result = [panel runModalForDirectory: path file:@""];
nkeynes@681
   183
    if( result == NSOKButton ) {
nkeynes@681
   184
        NSString *filename = [panel filename];
nkeynes@681
   185
        dreamcast_save_state( [filename UTF8String] );
nkeynes@681
   186
    }
nkeynes@681
   187
}
nkeynes@681
   188
- (void) load_binary_action: (id)sender
nkeynes@681
   189
{
nkeynes@681
   190
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@718
   191
    const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
nkeynes@718
   192
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@718
   193
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@681
   194
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@681
   195
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@681
   196
        file_load_magic( [filename UTF8String] );
nkeynes@681
   197
    }
nkeynes@681
   198
}
nkeynes@681
   199
- (void) mount_action: (id)sender
nkeynes@681
   200
{
nkeynes@681
   201
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@718
   202
    const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
nkeynes@718
   203
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@718
   204
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@681
   205
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@681
   206
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@681
   207
        gdrom_mount_image( [filename UTF8String] );
nkeynes@681
   208
    }
nkeynes@681
   209
}
nkeynes@681
   210
- (void) pause_action: (id)sender
nkeynes@681
   211
{
nkeynes@681
   212
    dreamcast_stop();
nkeynes@681
   213
}
nkeynes@681
   214
nkeynes@681
   215
- (void) reset_action: (id)sender
nkeynes@681
   216
{
nkeynes@681
   217
    dreamcast_reset();
nkeynes@681
   218
}
nkeynes@681
   219
- (void) run_action: (id)sender
nkeynes@681
   220
{
nkeynes@681
   221
    cocoa_gui_run_later();
nkeynes@681
   222
}
nkeynes@681
   223
- (void) run_immediate
nkeynes@681
   224
{
nkeynes@681
   225
    dreamcast_run();
nkeynes@681
   226
}
nkeynes@691
   227
- (void) gdrom_list_action: (id)sender
nkeynes@691
   228
{
nkeynes@691
   229
    gdrom_list_set_selection( [sender tag] );
nkeynes@691
   230
}
nkeynes@681
   231
@end
nkeynes@681
   232
nkeynes@681
   233
nkeynes@681
   234
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@681
   235
{
nkeynes@685
   236
    /* If started from the finder, the first (and only) arg will look something like 
nkeynes@736
   237
     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
nkeynes@736
   238
     * normally
nkeynes@736
   239
     */
nkeynes@685
   240
    if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
nkeynes@685
   241
        *argc = 1;
nkeynes@685
   242
    }
nkeynes@685
   243
    return TRUE;
nkeynes@681
   244
}
nkeynes@681
   245
nkeynes@681
   246
gboolean gui_init( gboolean withDebug )
nkeynes@681
   247
{
nkeynes@681
   248
    dreamcast_register_module( &cocoa_gui_module );
nkeynes@736
   249
nkeynes@681
   250
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
nkeynes@681
   251
    [NSApplication sharedApplication];
nkeynes@736
   252
nkeynes@681
   253
    LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
nkeynes@681
   254
    [NSApp setDelegate: delegate];
nkeynes@686
   255
    NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
nkeynes@686
   256
    NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
nkeynes@725
   257
    [iconImage setName: @"NSApplicationIcon"];
nkeynes@686
   258
    [NSApp setApplicationIconImage: iconImage];   
nkeynes@681
   259
    cocoa_gui_create_menu();
nkeynes@681
   260
    NSWindow *window = cocoa_gui_create_main_window();
nkeynes@681
   261
    [window makeKeyAndOrderFront: nil];
nkeynes@681
   262
    [NSApp activateIgnoringOtherApps: YES];   
nkeynes@736
   263
nkeynes@681
   264
    [pool release];
nkeynes@681
   265
}
nkeynes@681
   266
nkeynes@681
   267
void gui_main_loop( gboolean run )
nkeynes@681
   268
{
nkeynes@681
   269
    if( run ) {
nkeynes@681
   270
        cocoa_gui_autorun = YES;
nkeynes@681
   271
    }
nkeynes@693
   272
    cocoa_gui_is_running = YES;
nkeynes@736
   273
    [NSApp run];
nkeynes@736
   274
    cocoa_gui_is_running = NO;
nkeynes@681
   275
}
nkeynes@681
   276
nkeynes@681
   277
void gui_update_state(void)
nkeynes@681
   278
{
nkeynes@681
   279
    cocoa_gui_update();
nkeynes@681
   280
}
nkeynes@681
   281
nkeynes@681
   282
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@681
   283
{
nkeynes@693
   284
    if( cocoa_gui_is_running ) {
nkeynes@693
   285
        NSString *error_string;
nkeynes@693
   286
nkeynes@693
   287
        va_list args;
nkeynes@693
   288
        va_start(args, msg);
nkeynes@693
   289
        error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
nkeynes@693
   290
        NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
nkeynes@693
   291
        va_end(args);
nkeynes@693
   292
        return TRUE;
nkeynes@693
   293
    } else {
nkeynes@693
   294
        return FALSE;
nkeynes@693
   295
    }
nkeynes@681
   296
}
nkeynes@681
   297
nkeynes@681
   298
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@681
   299
{
nkeynes@681
   300
nkeynes@681
   301
}
nkeynes@681
   302
nkeynes@681
   303
nkeynes@681
   304
uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
nkeynes@681
   305
{
nkeynes@681
   306
    NSEvent *event;
nkeynes@681
   307
    NSAutoreleasePool *pool;
nkeynes@681
   308
nkeynes@681
   309
    cocoa_gui_nanos += nanosecs;
nkeynes@681
   310
    if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@681
   311
        cocoa_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@681
   312
        cocoa_gui_ticks ++;
nkeynes@681
   313
        uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
nkeynes@681
   314
nkeynes@681
   315
        // Run the event loop
nkeynes@681
   316
        pool = [NSAutoreleasePool new];
nkeynes@681
   317
        while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
nkeynes@681
   318
                         inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
nkeynes@681
   319
            [NSApp sendEvent: event];
nkeynes@681
   320
        }
nkeynes@681
   321
        [pool release];
nkeynes@681
   322
nkeynes@681
   323
        struct timeval tv;
nkeynes@681
   324
        gettimeofday(&tv,NULL);
nkeynes@681
   325
        uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@681
   326
        (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
nkeynes@681
   327
        if( (ns * 1.05) < current_period ) {
nkeynes@681
   328
            // We've gotten ahead - sleep for a little bit
nkeynes@681
   329
            struct timespec tv;
nkeynes@681
   330
            tv.tv_sec = 0;
nkeynes@681
   331
            tv.tv_nsec = current_period - ns;
nkeynes@681
   332
            nanosleep(&tv, &tv);
nkeynes@681
   333
        }
nkeynes@681
   334
nkeynes@681
   335
        /* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@681
   336
         * save the current tv value */
nkeynes@681
   337
        if( cocoa_gui_ticks > 10 ) {
nkeynes@681
   338
            gchar buf[32];
nkeynes@681
   339
            cocoa_gui_ticks -= 10;
nkeynes@681
   340
nkeynes@681
   341
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@681
   342
            cocoa_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@681
   343
            cocoa_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@681
   344
            snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
nkeynes@681
   345
            [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
nkeynes@681
   346
nkeynes@681
   347
        }
nkeynes@681
   348
    }
nkeynes@681
   349
    return nanosecs;
nkeynes@681
   350
}
nkeynes@681
   351
nkeynes@681
   352
void cocoa_gui_update( void )
nkeynes@681
   353
{
nkeynes@736
   354
nkeynes@681
   355
}
nkeynes@681
   356
nkeynes@681
   357
void cocoa_gui_start( void )
nkeynes@681
   358
{
nkeynes@681
   359
    LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
nkeynes@681
   360
    [win setRunning: YES];
nkeynes@681
   361
    cocoa_gui_nanos = 0;
nkeynes@681
   362
    gettimeofday(&cocoa_gui_lasttv,NULL);
nkeynes@681
   363
}
nkeynes@681
   364
nkeynes@681
   365
void cocoa_gui_stop( void )
nkeynes@681
   366
{
nkeynes@681
   367
    LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
nkeynes@681
   368
    [win setRunning: NO];
nkeynes@681
   369
}
nkeynes@681
   370
nkeynes@681
   371
/**
nkeynes@681
   372
 * Queue a dreamcast_run() to execute after the currently event(s)
nkeynes@681
   373
 */
nkeynes@681
   374
void cocoa_gui_run_later( void )
nkeynes@681
   375
{
nkeynes@681
   376
    [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
nkeynes@736
   377
     target: [NSApp delegate] argument: nil order: 1 
nkeynes@736
   378
     modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
nkeynes@718
   379
}
nkeynes@718
   380
nkeynes@770
   381
/*************************** Convenience methods ***************************/
nkeynes@770
   382
nkeynes@718
   383
NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
nkeynes@718
   384
{
nkeynes@718
   385
    NSBitmapImageRep *rep = 
nkeynes@718
   386
        [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
nkeynes@718
   387
         pixelsWide: buffer->width  pixelsHigh: buffer->height
nkeynes@736
   388
         bitsPerSample: 8 samplesPerPixel: 3
nkeynes@736
   389
         hasAlpha: NO isPlanar: NO
nkeynes@736
   390
         colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
nkeynes@736
   391
         bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
nkeynes@736
   392
nkeynes@718
   393
    NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
nkeynes@718
   394
    [image addRepresentation: rep];
nkeynes@718
   395
    return image;
nkeynes@718
   396
}
nkeynes@770
   397
nkeynes@770
   398
nkeynes@770
   399
NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
nkeynes@770
   400
{
nkeynes@770
   401
    NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
nkeynes@770
   402
    [label setStringValue: text];
nkeynes@770
   403
    [label setBordered: NO];
nkeynes@770
   404
    [label setDrawsBackground: NO];
nkeynes@770
   405
    [label setEditable: NO];
nkeynes@770
   406
    [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
nkeynes@770
   407
    if( parent != NULL ) {
nkeynes@770
   408
        [parent addSubview: label];
nkeynes@770
   409
    }
nkeynes@770
   410
    return label;
nkeynes@770
   411
}
.