Cocoa: Drag & Drop

February 28, 2009 | Filed Under Cocoa | 1 Comment

Today a simple example of Drag & Drop with Cocoa and NSView. The screenshot below shows two groups. In the first one, you can drop the photos on your filesystem, taken from Desktop or Finder. In the second group, you can only drop the first group image.

Cocoa Drag & Drop Test App

You can find the source code here: Drag & Drop Test Source Code.

WebKit: Google Maps

February 1, 2009 | Filed Under Cocoa | No Comments

Following the Google StreetView post, another example on How Cocoa Application can Interact with Google. Today the WebKit shows Google Maps.
Cocoa Google Maps - Aliso Viejo
Like iPhoto ‘09 you can select you Map Type (Terrain, Satellite, Hybrid) and you can Zoom In or Zoom Out using controls of your Application.
Cocoa Google Maps - Red Wood
You can Find the Source Code Here: Cocoa Google Maps.

WebKit: Google StreetView

January 17, 2009 | Filed Under Cocoa | 2 Comments

Last iPhoto ‘09 presented at MacWorld contains a couple of interesting Features: Faces and Places. Today, I’ve a little bit of free time to play with my Mac, and the topic is How to embed Google StreetView into a Cocoa Application.
Cocoa Streetview Cuppertino
The Solution is using WebKit and Google Maps API. We’ve a simple HTML file that contains StreetView “Canvas” and a couple of javascript methods that interact with GStreetviewPanorama Object.
Cocoa Streetview San Francisco
The Source Code is Available Here: Cocoa StreetView Source.

WebKit: Dynamic Content

December 6, 2008 | Filed Under Cocoa | No Comments

Today I’ve played a bit with WebKit. I Want do something like Cocoa VBox View and NSScrollView but with more flexibility. Below you can see the example result.

WebKit Dynamic Content

The interesting part of this example is How to manage WebKit content, and how to add something dynamically.

I use webView:decidePolicyForNavigationAction:request:frame:decisionListener: of WebPolicyDelegate Protocol to handle my custom requests, in this way…

- (void)webView:(WebView *)sender
    decidePolicyForNavigationAction:(NSDictionary *)actionInformation
   request:(NSURLRequest *)request frame:(WebFrame *)frame
   decisionListener:(id)listener
{
    if ([[[request URL] path] isEqualToString:@"PATH to Handle"]) {
        [listener ignore];
        // Handle Request
    } else {
         [listener use];
    }
}

…Then with a little bit of JavaScript I’ll set my dynamic content loaded from Database or somewhere else.

NSArray *args = [NSArray arrayWithObjects:@"Item1Content",
                                  @"Hi, I'm Test 1 Content", nil];
[[webKit windowScriptObject] callWebScriptMethod:@"fillElement"
                               withArguments:args];

The fillElement method is a simple JS function like this document.getElementById(elem).innerHTML = data;

Here you can find the Source Code.

Cocoa: Sidebar with Badges

December 2, 2008 | Filed Under Cocoa | 8 Comments

Yesterday, I’ve written a simple Sidebar controller that can saves you some time and trouble with Cocoa Source List. Nodes and Folders can be drag and dropped into other folders or near other items and you can easily set badges on nodes.

Cocoa Sidebar

Cocoa Sidebar

You can find the example Source Code Here.

And below you can see hot to add folders, nodes and set badges.

- (void)populateOutlineContents:(id)inObject {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  [sidebar addRootFolder:@"1" name:@"DEVICES"];
  [sidebar addChild:@"1.1" name:@"Machintosh HD"
               icon:[NSImage imageNamed:NSImageNameComputer]
             action:@selector(buttonPres:) target:self];

  [sidebar addRootFolder:@"2" name:@"PLACES"];
  [sidebar addChild:@"2.1"
                url:NSHomeDirectory()
             action:@selector(buttonPres:) target:self];

  [sidebar addRootFolder:@"3" name:@"OTHER."];
  [sidebar addChild:@"3.1" name:@"Bonjour"
               icon:[NSImage imageNamed:NSImageNameBonjour]
             action:@selector(buttonPres:) target:self];
  [sidebar addChild:@"3.2" name:@"Users"
               icon:[NSImage imageNamed:NSImageNameUserGroup]
             action:@selector(buttonPres:) target:self];

  [sidebar clearSelection];

  // Add Badge to Node with Key
  [sidebar setBadge:@"2.1" count:5];
  [sidebar setBadge:@"3.2" count:7];

  [pool release];
}

- (void)buttonPres:(id)sender {
  NSLog(@"Button Press %@", [sender nodeTitle]);
}

Objective-C: SQLite Wrapper

November 28, 2008 | Filed Under Mac OS X, iPhone | 8 Comments

I’ve written a simple SQLite Wrapper with two Examples one for Mac and one for the iPhone. The Wrapper class has the same source code for both platforms.

Here you can find the Mac Example Source Code and the iPhone Example Source Code.

iPhone Test SQLite App

This is a simple usage example:

Sqlite *sqlite = [[Sqlite alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                             NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
                        stringByAppendingPathComponent:@"SQLiteTest.db"];
if (![sqlite open:writableDBPath])
  return;

[sqlite executeNonQuery:@"CREATE TABLE test (key TEXT NOT NULL, value TEXT);"];
[sqlite executeNonQuery:@"DELETE FROM test;"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
                        [Sqlite createUuid], @"PROVA"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
                         [Sqlite createUuid], @"PROVA 2"];
[sqlite executeNonQuery:@"INSERT INTO test VALUES (?, ?);",
                         [Sqlite createUuid], @"PROVA 3"];

NSArray *results = [sqlite executeQuery:@"SELECT * FROM test;"];
for (NSDictionary *dictionary in results) {
  NSLog(@"Row");
  for (NSString *key in [dictionary keyEnumerator])
      NSLog(@" - %@ %@", key, [dictionary objectForKey:key]);
}

[results release];
[sqlite release];

Cocoa: VBox View and NSScrollView

November 16, 2008 | Filed Under Cocoa | 1 Comment

Following the post of yesterday, today I’ve implemented a simple VBoxView (Vertical Box Layout) that allow you to lines up NSViews vertically.

Cocoa Scrollable NSViews VBox

Cocoa Scrollable NSViews VBox


Here you can find the example Source Code.

Cocoa: Drawing Images, Texts and Shadows

November 15, 2008 | Filed Under Cocoa | No Comments

Today I’m playing a bit with Quartz and Cocoa Drawing system (that is based on Quartz).

I’ve made a simple example that you can see below where I’ve used custom NSView that draws an Image and a Bezier Path with some text inside, and around the Bezier Path there’s a Shadow.

Cocoa Drawing Example

Cocoa Drawing Example

…And this is the drawRect method source code of the custom NSView.

- (void)drawRect:(NSRect)frameRect {
  [NSGraphicsContext saveGraphicsState];

  [[NSColor colorWithCalibratedRed:0.64 green:0.66 blue:0.71 alpha:1.0] set];
  NSRectFill(frameRect);

  /* Draw Shadow */
  NSShadow *shadow = [[NSShadow alloc] init];
  [shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.5]];
  [shadow setShadowOffset:NSMakeSize(4.0, -4.0)];
  [shadow setShadowBlurRadius:3.0];
  [shadow set];

  /* Draw Control */
  NSRect myRect = NSMakeRect(80, 50, frameRect.size.width - 120, 60);
  NSBezierPath *path = [NSBezierPath bezierPath];
  [path setLineJoinStyle:NSRoundLineJoinStyle];
  [path appendBezierPathWithRoundedRect:myRect xRadius:8.0 yRadius:8.0];    

  [path moveToPoint:NSMakePoint(80, 75)];
  [path lineToPoint:NSMakePoint(65, 85)];
  [path lineToPoint:NSMakePoint(80, 95)];

  NSColor *startingColor = [NSColor colorWithCalibratedRed:0.90
                                             green:0.92 blue:0.85 alpha:1.0];
  NSColor *endingColor = [NSColor colorWithCalibratedRed:0.81
                                           green:0.83 blue:0.76 alpha:1.0];
  NSGradient *gradient = [[[NSGradient alloc] initWithStartingColor:startingColor
                                 endingColor:endingColor] autorelease];
  [path fill];
  [gradient drawInBezierPath:path angle:90];

  [NSGraphicsContext restoreGraphicsState];

  [shadow release];

  NSImage *image = [NSImage imageNamed:NSImageNameUser];
  [image drawInRect:NSMakeRect(15, 50, 50, 50) fromRect:NSZeroRect
                operation:NSCompositeSourceOver fraction:1.0];

  NSString *string = [NSString stringWithString:@"Text\nMore Text"];
  [string drawInRect:NSMakeRect(90, 60, frameRect.size.width - 140, 45)
                        withAttributes:nil];
}
« Previous PageNext Page »