File: CurrencyConverter.m

package info (click to toggle)
gnustep-examples 1%3A1.0.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,952 kB
  • ctags: 280
  • sloc: objc: 15,345; makefile: 64
file content (326 lines) | stat: -rw-r--r-- 9,503 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 *  CurrencyConverter.m: A mini over-commented sample GNUstep app 
 *
 *  Copyright (c) 1999 Free Software Foundation, Inc.
 *  
 *  Author: Nicola Pero
 *  Date: November 1999
 *
 *  This sample program is part of GNUstep.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * This mini sample program documents using text fields. 
 *
 * Layout is done with GSHbox, GSVbox.  
 * See Calculator.app for an example of window layout done without 
 * using GSVbox & GSHbox.
 */

/*
 *  I took the idea of writing this example code from an old GNUstep project 
 *  by Michael S. Hanni, but everything was coded from scratch. 
 */

// Include the definition of our custom class.
#include "CurrencyConverter.h"

// Include GNUstep layout extension classes
#include <GNUstepGUI/GSHbox.h>
#include <GNUstepGUI/GSVbox.h>


// Text to be displayed in the labels
static NSString* fieldString[3] = {
  @"Amount in other currency:",
  @"EUROs to convert:", 
  @"Exchange rate per 1 EURO:"
};

// Implementation of our custom class.
@implementation CurrencyConverter
{
  // See the @interface declaration in CurrencyConverter.h 
  // for the listing of instance variables
}
//
// Methods implementation
//

// Initialize an instance object of our class. 
- (id) init
{
  GSVbox* windowVbox;
  GSVbox* formVbox;
  GSHbox* hbox;
  NSTextField* label;
  NSButton* convertButton;
  NSRect winFrame;
  int i;
  NSSize size;

  self = [super init];

  // Create a vertical box (NB: Things are packed in the box 
  // from bottom to top)
  windowVbox = AUTORELEASE ([GSVbox new]);
  [windowVbox setBorder: 0];
  [windowVbox setDefaultMinYMargin: 0];
  
  //
  // Result field
  //
  hbox = AUTORELEASE ([GSHbox new]);
  [hbox setDefaultMinXMargin: 10];
  [hbox setBorder: 10];

  label = AUTORELEASE ([NSTextField new]);
  [label setSelectable: NO];
  [label setBezeled: NO];
  [label setDrawsBackground: NO];
  [label setStringValue: fieldString[0]];
  [label sizeToFit];
  [label setAutoresizingMask: NSViewHeightSizable];
  [hbox addView: label
	enablingXResizing: NO];
  
  field[0] = AUTORELEASE ([NSTextField new]);
  [field[0] setSelectable: YES];
  [field[0] setEditable: NO];
  [field[0] setBezeled: YES];
  [field[0] setBackgroundColor: [NSColor controlBackgroundColor]];
  [field[0] setDrawsBackground: YES];
  // Use automatic height
  [field[0] sizeToFit];
  // But set width to 100
  size = [field[0] frame].size;
  size.width = 100;
  [field[0] setFrameSize: size];
  [field[0] setAutoresizingMask: NSViewWidthSizable];
  // Saying nothing means enablingXResizing: YES
  [hbox addView: field[0]];

  [hbox setAutoresizingMask: NSViewWidthSizable];
  [windowVbox addView: hbox];

  //
  // Separator
  //
  [windowVbox addSeparator];
  
  //
  // Upper part of the window
  //
  formVbox = AUTORELEASE ([GSVbox new]);
  [formVbox setBorder: 10];
  [formVbox setDefaultMinYMargin: 10];

  // The two editable fields
  for (i = 1; i < 3; i++)
    {
      // We are doing it the hard way, without NSForm, to show how to do
      // more generally to pack things and objects
      hbox = AUTORELEASE ([GSHbox new]);
      [hbox setDefaultMinXMargin: 10];

      label = AUTORELEASE ([NSTextField new]);
      [label setSelectable: NO];
      [label setBezeled: NO];
      [label setDrawsBackground: NO];
      [label setStringValue: fieldString[i]];
      [label sizeToFit];
      [label setAutoresizingMask: NSViewHeightSizable];
      [hbox addView: label
	    enablingXResizing: NO];

      field[i] = AUTORELEASE ([NSTextField new]);
      [field[i] setEditable: YES];
      [field[i] setBezeled: YES];
      [field[i] setDrawsBackground: YES];
      // Use automatic height
      [field[i] sizeToFit];
      // But set width to 100
      size = [field[i] frame].size;
      size.width = 100;
      [field[i] setFrameSize: size];
      [field[i] setAutoresizingMask: NSViewWidthSizable];
      [hbox addView: field[i]];

      [hbox setAutoresizingMask: NSViewWidthSizable];
      [formVbox addView: hbox];
    }

  // Link the editable fields so the user may move between them
  // pressing TAB (Important: Remember to always send these messages
  // *after* you have created the objects you are referring to)
  [field[1] setNextText: field[2]];
  [field[2] setNextText: field[1]];

  // Ask to receive interesting messages concerning what's happening 
  // to the fields.  We are interested only in [-controlTextDidEndEditing:]
  [field[1] setDelegate: self];
  [field[2] setDelegate: self];

  [formVbox setAutoresizingMask: NSViewWidthSizable];
  [windowVbox addView: formVbox];

  //
  // Window
  //
  winFrame.size = [windowVbox frame].size;
  winFrame.origin = NSMakePoint (100, 100);

  // Now we can make the window of the exact size  
  // NB: Note that we do not autorelease the window
  window = [[NSWindow alloc] 
	     initWithContentRect: winFrame
	     styleMask: (NSTitledWindowMask | NSMiniaturizableWindowMask 
			 | NSResizableWindowMask)
	     backing: NSBackingStoreBuffered
	     defer: YES];
  [window setTitle: @"CurrencyConverter.app"];
  [window setContentView: windowVbox];
  [window setMinSize: [NSWindow frameRectForContentRect: winFrame
				styleMask: [window styleMask]].size];

  // Trick to forbid vertical resizing
  [window setResizeIncrements: NSMakeSize (1, 100000)];
  return self;
}
- (void)dealloc
{
  // Releasing the window releases all its views in cascade
  RELEASE (window);
  [super dealloc];
}

// Received upon ending of editing in one of the two fields.
- (void)controlTextDidEndEditing: (NSNotification *)aNotification
{
  float euros, rate, total;

  // Read values
  euros = [field[2] floatValue];
  rate = [field[1] floatValue];

  // Compute total
  total = euros * rate;
  
  // Display total
  [field[0] setFloatValue: total];
}

// As app delegate, we receive this message from the app
- (void)applicationDidFinishLaunching: (NSNotification *)aNotification;
{
  [window orderFront: self];
}

// Execution starts from here. 
int main (void)
{
   NSAutoreleasePool *pool;
   NSApplication *app;
   NSMenu *mainMenu;
   NSMenu *menu;
   NSMenuItem *menuItem;
   CurrencyConverter *converter;
   int i;

   // We need to explicitly create this object only in the main function;     
   // instead, while the app is running, the gui library creates these objects
   // automatically for us.
   pool = [NSAutoreleasePool new];

   // Get the object representing our application.
   app = [NSApplication sharedApplication];
  
   //
   // Create the Menu 
   //

   // Main Menu
   mainMenu = AUTORELEASE ([NSMenu new]);

   // Info Item
   // The object receiving this message is determined at run time;
   // it will be the NSApplication
   [mainMenu addItemWithTitle: @"Info..." 
	     action: @selector (orderFrontStandardInfoPanel:) 
	     keyEquivalent: @""];

   // Edit Submenu
   menuItem = [mainMenu addItemWithTitle: @"Edit" 
			action: NULL 
			keyEquivalent: @""];
   menu = AUTORELEASE ([NSMenu new]);
   [mainMenu setSubmenu: menu forItem: menuItem];

   // The object which should receive the messages cut:, copy:, paste: is not 
   // specified, so that the library will have to determine it at run time. 
   // At first, it will (try to) send them to the 'first responder' 
   // -- the object which is receiving keyboard input.  
   // In our case that is precisely what we want, since the first responder 
   // is the NSText being edited (which knows how to handle cut:, copy:, 
   // paste:), if any.  
   [menu addItemWithTitle: @"Cut" 
	 action: @selector (cut:) 
	 keyEquivalent: @"x"];

   [menu addItemWithTitle: @"Copy" 
	 action: @selector (copy:) 
	 keyEquivalent: @"c"];

   [menu addItemWithTitle: @"Paste" 
	 action: @selector (paste:) 
	 keyEquivalent: @"v"];

   [menu addItemWithTitle: @"SelectAll" 
	 action: @selector (selectAll:) 
	 keyEquivalent: @"a"];

   // Hide MenuItem
   [mainMenu addItemWithTitle: @"Hide" 
	     action: @selector (hide:) 
	     keyEquivalent: @""];
   // Quit MenuItem
   [mainMenu addItemWithTitle: @"Quit" 
	     action: @selector (terminate:)
	     keyEquivalent: @"q"];	

   [app setMainMenu: mainMenu];
   // The default title @"Currency Converter" is a bit too long
   [mainMenu setTitle: @"CurrConv"];


   // Create and initializes an instance of our custom object.
   converter = [[CurrencyConverter alloc] init];
   
   // Set our custom object instance as the application delegate. 
   // This means that 'converter' will receive certain messages 
   // (documented in the doc) before/after important events for the app 
   // life, such as starting, ending, closing last window, etc.
   // In this context, we are interested in receiving the 
   // [-applicationDidFinishLaunching:] message.
   [app setDelegate: converter];

   // Finally, all is ready to run our application.
   [app run];
   return 0;
}