Skip to content

Commit 055a84a

Browse files
committed
Initial commit
1 parent 161bd0a commit 055a84a

File tree

11 files changed

+2870
-1
lines changed

11 files changed

+2870
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
tutorial-framework
22
==================
33

4-
Framework to serve as a starting point for tutorials
4+
Framework to serve as a starting point for Awesomium tutorials.
5+
6+
Please see: http://wiki.awesomium.com/tutorials
7+
8+
## Setting Up on Windows
9+
10+
You should create a new Visual Studio project with the files located in the "src" directory. Only filenames ending in "_win" or those that do not specify a platform should be added to your project.
11+
12+
## Setting Up on Mac OSX
13+
14+
You should create a new XCode Cocoa project with the files located in the "src" directory. Only filenames ending in "_mac" or those that do not specify a platform should be added to your project.

src/application.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef TUTORIAL_APPLICATION_H_
2+
#define TUTORIAL_APPLICATION_H_
3+
4+
class View;
5+
6+
namespace Awesomium {
7+
class WebCore;
8+
}
9+
10+
// Common class that sets up an application, creates the WebCore, handles
11+
// the Run loop, and abstracts platform-specific details.
12+
class Application {
13+
public:
14+
// Listener interface to be used to handle various application events.
15+
class Listener {
16+
public:
17+
virtual ~Listener() {}
18+
19+
// Event is fired when app (and WebCore) have been loaded.
20+
virtual void OnLoaded() = 0;
21+
22+
// Event is fired for each iteration of the Run loop.
23+
virtual void OnUpdate() = 0;
24+
25+
// Event is fired when the app is shutting down.
26+
virtual void OnShutdown() = 0;
27+
};
28+
29+
virtual ~Application() {}
30+
31+
// Platform-specific factory constructor
32+
static Application* Create();
33+
34+
// Begin the Run loop.
35+
virtual void Run() = 0;
36+
37+
// Ends the Run loop.
38+
virtual void Quit() = 0;
39+
40+
// Create a platform-specific, windowed View
41+
virtual View* CreateView(int width, int height) = 0;
42+
43+
// Destroy a View
44+
virtual void DestroyView(View* view) = 0;
45+
46+
// Show a modal message box
47+
virtual void ShowMessage(const char* message) = 0;
48+
49+
// Get the WebCore
50+
virtual Awesomium::WebCore* web_core() { return web_core_; }
51+
52+
// Get the Listener.
53+
Listener* listener() { return listener_; }
54+
55+
// Set the Listener for various app events.
56+
void set_listener(Listener* listener) { listener_ = listener; }
57+
58+
protected:
59+
Application() { }
60+
61+
virtual void Load() = 0;
62+
63+
Listener* listener_;
64+
Awesomium::WebCore* web_core_;
65+
};
66+
67+
#endif // TUTORIAL_APPLICATION_H_

src/application_mac.mm

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "application.h"
2+
#include "view.h"
3+
#import <Cocoa/Cocoa.h>
4+
#include <Awesomium/WebCore.h>
5+
#include <string>
6+
7+
@interface AppDelegate : NSObject<NSApplicationDelegate> {
8+
NSTimer *timer;
9+
}
10+
- (void)startUpdateTimer;
11+
- (void)applicationWillTerminate:(NSNotification *)aNotification;
12+
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
13+
- (void)updateTimer:(NSTimer *)timer;
14+
@end
15+
16+
using namespace Awesomium;
17+
18+
class ApplicationMac : public Application {
19+
NSAutoreleasePool* pool;
20+
NSApplication* app;
21+
AppDelegate* appDelegate;
22+
public:
23+
ApplicationMac() {
24+
listener_ = NULL;
25+
web_core_ = NULL;
26+
}
27+
28+
virtual ~ApplicationMac() {
29+
[NSApp setDelegate:nil];
30+
[appDelegate release];
31+
[pool release];
32+
33+
if (listener())
34+
listener()->OnShutdown();
35+
36+
if (web_core_)
37+
web_core_->Shutdown();
38+
}
39+
40+
virtual void Run() {
41+
Load();
42+
43+
[NSApp activateIgnoringOtherApps:YES];
44+
[appDelegate startUpdateTimer];
45+
[NSApp run];
46+
}
47+
48+
virtual void Quit() {
49+
[app terminate:app];
50+
}
51+
52+
virtual void Load() {
53+
pool = [[NSAutoreleasePool alloc] init];
54+
app = [NSApplication sharedApplication];
55+
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
56+
appDelegate = [[AppDelegate alloc] init];
57+
[NSApp setDelegate:appDelegate];
58+
59+
web_core_ = WebCore::Initialize(WebConfig());
60+
61+
if (listener())
62+
listener()->OnLoaded();
63+
}
64+
65+
virtual View* CreateView(int width, int height) {
66+
return View::Create(width, height);
67+
}
68+
69+
virtual void DestroyView(View* view) {
70+
delete view;
71+
}
72+
73+
virtual void ShowMessage(const char* message) {
74+
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
75+
[alert setMessageText:[[NSString alloc] initWithUTF8String:message]];
76+
[alert runModal];
77+
}
78+
};
79+
80+
Application* Application::Create() {
81+
return new ApplicationMac();
82+
}
83+
84+
@implementation AppDelegate
85+
86+
- (void)startUpdateTimer {
87+
timer = [NSTimer timerWithTimeInterval:(1.0f/60.0f)
88+
target:self
89+
selector:@selector(updateTimer:)
90+
userInfo:nil
91+
repeats:YES];
92+
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
93+
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
94+
}
95+
96+
- (void)applicationWillTerminate:(NSNotification *)aNotification {
97+
if (Awesomium::WebCore::instance())
98+
Awesomium::WebCore::instance()->Shutdown();
99+
}
100+
101+
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
102+
return YES;
103+
}
104+
105+
- (void)updateTimer:(NSTimer *)timer {
106+
if (Awesomium::WebCore::instance())
107+
Awesomium::WebCore::instance()->Update();
108+
}
109+
110+
@end

src/application_win.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "application.h"
2+
#include "view.h"
3+
#include <Awesomium/WebCore.h>
4+
#include <string>
5+
6+
using namespace Awesomium;
7+
8+
class ApplicationWin : public Application {
9+
bool is_running_;
10+
public:
11+
ApplicationWin() {
12+
is_running_ = true;
13+
listener_ = NULL;
14+
web_core_ = NULL;
15+
}
16+
17+
virtual ~ApplicationWin() {
18+
if (listener())
19+
listener()->OnShutdown();
20+
21+
if (web_core_)
22+
web_core_->Shutdown();
23+
}
24+
25+
virtual void Run() {
26+
Load();
27+
28+
// Main message loop:
29+
MSG msg;
30+
while (GetMessage(&msg, NULL, 0, 0) && is_running_) {
31+
web_core_->Update();
32+
TranslateMessage(&msg);
33+
DispatchMessage(&msg);
34+
if (listener())
35+
listener()->OnUpdate();
36+
}
37+
}
38+
39+
virtual void Quit() {
40+
is_running_ = false;
41+
}
42+
43+
virtual void Load() {
44+
web_core_ = WebCore::Initialize(WebConfig());
45+
46+
if (listener())
47+
listener()->OnLoaded();
48+
}
49+
50+
virtual View* CreateView(int width, int height) {
51+
return View::Create(width, height);
52+
}
53+
54+
virtual void DestroyView(View* view) {
55+
delete view;
56+
}
57+
58+
virtual void ShowMessage(const char* message) {
59+
std::wstring message_str(message, message + strlen(message));
60+
MessageBox(0, message_str.c_str(), message_str.c_str(), NULL);
61+
}
62+
};
63+
64+
Application* Application::Create() {
65+
return new ApplicationWin();
66+
}

0 commit comments

Comments
 (0)