Back to index

texmacs  1.0.7.15
TMButtonsController.m
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : TMButtonsController.m
00004 * DESCRIPTION: Controller for the widget bar
00005 * COPYRIGHT  : (C) 2007  Massimiliano Gubinelli
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #import "TMButtonsController.h"
00013 
00014 @implementation TMButtonsController
00015 
00016 - (id) init
00017 {
00018        self = [super init];
00019        if (self != nil) {
00020               menuArray = [[NSMutableArray alloc] initWithCapacity:10];
00021               barArray = [[NSMutableArray alloc] initWithCapacity:10];
00022               view = [[NSView alloc] init];
00023        }
00024        return self;
00025 }
00026 - (void) dealloc
00027 {
00028        [menuArray release];
00029        [barArray release];
00030        [view release];
00031        [super dealloc];
00032 }
00033 
00034 @protocol TMMenuItemDoit
00035 - (void) doit;
00036 @end
00037 
00038 - (void) buttonsAction:(id) sc
00039 {
00040   unsigned idx = [sc selectedSegment];
00041   NSArray *arr = [[sc cell] representedObject];
00042   NSMenuItem *mi = [arr objectAtIndex:idx];
00043   NSMenu *sm = [mi submenu];
00044   if (sm)
00045     [NSMenu popUpContextMenu:sm withEvent:[NSApp currentEvent] forView:sc];
00046   else if ([mi respondsToSelector:@selector(doit)]) [(id)mi doit];
00047 }
00048 
00049 - (NSSegmentedControl*) newSegment
00050 {
00051   NSSegmentedControl *ret =  [[[NSSegmentedControl alloc] init] autorelease];
00052   [ret setSegmentStyle: NSSegmentStyleTexturedSquare];
00053   return ret; 
00054 }
00055 
00056 - (void)setMenu:(NSMenu *)menu forRow:(unsigned) idx
00057 {
00058        unsigned count = [menuArray count];
00059        while (count <= idx) { 
00060               [menuArray addObject:[[[NSMenu alloc] init] autorelease]];  
00061       NSMutableArray *arr =  [[NSMutableArray alloc] initWithCapacity:10];
00062       [barArray addObject:arr];  
00063               count++; 
00064        }
00065        [menuArray replaceObjectAtIndex: idx withObject: menu];
00066        {
00067       NSMutableArray *arr = [barArray objectAtIndex:idx];
00068 //      [arr removeAllObjects];
00069       unsigned nn = [arr count], mm = 0;
00070       unsigned  i,j;
00071       [menu addItem:[NSMenuItem separatorItem]];
00072       unsigned c = [menu numberOfItems];
00073       NSMutableArray *segs =  [[[NSMutableArray alloc] initWithCapacity:10] autorelease];
00074       for(i = 0; i < c; i++)
00075       {
00076         NSMenuItem *mi = [menu itemAtIndex:i];
00077         if (![mi isSeparatorItem]) [segs addObject:mi];
00078         else  if ([segs count]>0) {
00079           while (mm >= nn) {
00080             [arr addObject:[self newSegment]];
00081             nn++;
00082           }
00083           NSSegmentedControl *sc = [arr objectAtIndex:mm];
00084           mm++;
00085           [sc setSegmentCount:[segs count]];
00086           for(j=0; j<[segs count]; j++)
00087           {
00088             mi = [segs objectAtIndex:j];
00089             [sc setEnabled:YES forSegment:j];
00090             [sc setImage:[mi representedObject] forSegment:j];
00091             [sc setMenu:[mi submenu] forSegment:j];
00092             [mi setMenu:nil];
00093             [sc setLabel:nil forSegment:j];
00094             [sc setWidth:25.0 forSegment:j];
00095             [(NSSegmentedCell*)[sc cell] setToolTip:[mi toolTip] forSegment:j];
00096             
00097           }
00098           [(NSCell*)[sc cell] setRepresentedObject:segs];
00099           [[sc cell] setTrackingMode: NSSegmentSwitchTrackingMomentary];
00100           [sc sizeToFit];
00101           [sc setTarget: self];
00102           [sc setAction:@selector(buttonsAction:)];
00103           segs =  [[[NSMutableArray alloc] initWithCapacity:10] autorelease];
00104         }
00105         
00106       }
00107       while (mm < nn) {
00108         [(NSSegmentedControl*)[arr objectAtIndex:mm] removeFromSuperview];
00109         mm++;
00110       }
00111        }
00112   [self layout];
00113 }
00114 
00115 - (void) layout
00116 {
00117   NSRect r = [view bounds];
00118   float totalHeight = 0.0;
00119   float totalWidth = 0.0;
00120   float baseX = r.origin.x;
00121   int i,j;
00122   //   r.origin.y += r.size.height;
00123   for (i=[barArray count]-1; i>= 0; i--) {
00124     r.origin.x = baseX;
00125     NSArray *arr = [barArray objectAtIndex:i];
00126     float currHeight = 0.0;
00127     for(j=0; j<((int) [arr count]); j++) {
00128       NSSegmentedControl *sc = [arr objectAtIndex:j];
00129       if (![sc superview]) [view addSubview:sc];
00130       r.size = [sc frame].size;
00131          [sc setFrame:r];
00132       r = NSOffsetRect(r, r.size.width + 5.0, 0);            
00133       if (NSMaxX(r) > totalWidth) totalWidth = NSMaxX(r);
00134       if (r.size.height > currHeight) currHeight = r.size.height; 
00135     }
00136     r = NSOffsetRect(r, 0, currHeight);
00137     totalHeight += currHeight;    
00138   }
00139   r = [view frame];
00140   r.size.height = totalHeight;
00141   r.size.width = totalWidth;
00142   
00143   [view setFrame:r];
00144   [view setNeedsDisplay:YES];
00145 }
00146 
00147 - (NSView*) bar
00148 {
00149        return view;
00150 }
00151 
00152 @end