[JEDI.NET-commits] main/run Jedi.Windows.Forms.Hmi.Leds.pas,1.2,1.3
Status: Pre-Alpha
Brought to you by:
jedi_mbe
From: Marcel B. <jed...@us...> - 2004-08-03 14:47:36
|
Update of /cvsroot/jedidotnet/main/run In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv930/main/run Modified Files: Jedi.Windows.Forms.Hmi.Leds.pas Log Message: * LedStateCollection.IndexOf overloaded to find a named state. * Changed design time behavior of LedState selection (interface changed and removed a class as a consequence). * Added AutoSize support to SingleLed control. Index: Jedi.Windows.Forms.Hmi.Leds.pas =================================================================== RCS file: /cvsroot/jedidotnet/main/run/Jedi.Windows.Forms.Hmi.Leds.pas,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Jedi.Windows.Forms.Hmi.Leds.pas 2 Aug 2004 16:19:50 -0000 1.2 --- Jedi.Windows.Forms.Hmi.Leds.pas 3 Aug 2004 14:47:27 -0000 1.3 *************** *** 75,79 **** function Contains(value: LedStateBase): Boolean; function get_Item(index: Integer): LedStateBase; ! function IndexOf(value: LedStateBase): Integer; procedure Insert(index: Integer; value: LedStateBase); procedure Remove(value: LedStateBase); --- 75,80 ---- function Contains(value: LedStateBase): Boolean; function get_Item(index: Integer): LedStateBase; ! function IndexOf(value: LedStateBase): Integer; overload; ! function IndexOf(value: string): Integer; overload; procedure Insert(index: Integer; value: LedStateBase); procedure Remove(value: LedStateBase); *************** *** 83,88 **** --- 84,95 ---- LedStateConverter = class (System.ComponentModel.TypeConverter) + strict protected + class function GetStateCollection(context: ITypeDescriptorContext): LedStateCollection; static; + class function IsCollectionItem(context: ITypeDescriptorContext): Boolean; static; public + function CanConvertFrom(context: ITypeDescriptorContext; sourceType: System.Type): Boolean; override; function CanConvertTo(context: ITypeDescriptorContext; destinationType: System.Type): Boolean; override; + function ConvertFrom(context: ITypeDescriptorContext; culture: CultureInfo; + value: System.Object): System.Object; override; function ConvertTo(context: ITypeDescriptorContext; culture: CultureInfo; value: System.Object; destinationType: System.Type): System.Object; override; *************** *** 90,93 **** --- 97,102 ---- attributes: array of Attribute): PropertyDescriptorCollection; override; function GetPropertiesSupported(context: ITypeDescriptorContext): Boolean; override; + function GetStandardValues(context: ITypeDescriptorContext): TypeConverter.StandardValuesCollection; override; + function GetStandardValuesSupported(context: ITypeDescriptorContext): Boolean; override; end; *************** *** 104,111 **** function GetInstDescArguments: ArrayList; virtual; function GetInstDescMemberInfo: MemberInfo; virtual; - function GetLedStyle: LedStyleBase; procedure OnColorChanged(e: EventArgs); virtual; procedure SetColor(value: System.Drawing.Color); protected { Marks the bitmap as invalid without notifying the owning LedStyle instance. } procedure MarkInvalid; --- 113,120 ---- function GetInstDescArguments: ArrayList; virtual; function GetInstDescMemberInfo: MemberInfo; virtual; procedure OnColorChanged(e: EventArgs); virtual; procedure SetColor(value: System.Drawing.Color); protected + function GetLedStyle: LedStyleBase; { Marks the bitmap as invalid without notifying the owning LedStyle instance. } procedure MarkInvalid; *************** *** 500,518 **** {$ENDREGION} - {$REGION 'Design support classes'} - type - LedStateSelectionConverter = class (System.ComponentModel.Int32Converter) - strict protected - function GetStateCollection(context: ITypeDescriptorContext): LedStateCollection; - public - function ConvertFrom(context: ITypeDescriptorContext; culture: CultureInfo; - value: System.Object): System.Object; override; - function ConvertTo(context: ITypeDescriptorContext; culture: CultureInfo; value: System.Object; - destinationType: System.Type): System.Object; override; - function GetStandardValues(context: ITypeDescriptorContext): TypeConverter.StandardValuesCollection; override; - function GetStandardValuesSupported(context: ITypeDescriptorContext): Boolean; override; - end; - {$ENDREGION} - {$REGION 'Led controls'} type --- 509,512 ---- *************** *** 528,539 **** {$ENDREGION} strict private FLedStyle: LedStyleBase; FLedStateIdx: Integer; - FInitializeCount: Integer; - FBlinkTimer: System.Timers.Timer; - FBlinkStateIdx: Integer; strict protected procedure BlinkTimer_Elapsed(sender: System.Object; e: ElapsedEventArgs); function GetLedStyleInstance: LedStyleBase; procedure OnBackColorChanged(e: EventArgs); override; procedure OnPaint(e: PaintEventArgs); override; --- 522,538 ---- {$ENDREGION} strict private + FAutoSize: Boolean; + FBlinkStateIdx: Integer; + FBlinkTimer: System.Timers.Timer; + FInitializeCount: Integer; FLedStyle: LedStyleBase; FLedStateIdx: Integer; strict protected + procedure BeginInit; procedure BlinkTimer_Elapsed(sender: System.Object; e: ElapsedEventArgs); + procedure EndInit; function GetLedStyleInstance: LedStyleBase; + function IsBlinking: Boolean; + function IsInitializing: Boolean; procedure OnBackColorChanged(e: EventArgs); override; procedure OnPaint(e: PaintEventArgs); override; *************** *** 542,554 **** procedure Style_StateAdded(sender: System.Object; e: StateChangedEventArgs); procedure Style_StateRemoved(sender: System.Object; e: StateChangedEventArgs); ! procedure BeginInit; ! procedure EndInit; ! function IsBlinking: Boolean; ! function IsInitializing: Boolean; public constructor Create; function get_BlinkInterval: Double; procedure set_BlinkInterval(value: Double); ! procedure set_LedState(value: Integer); procedure set_LedStyle(value: LedStyleBase); {$REGION 'SingleLed hiding inherited properties'} --- 541,552 ---- procedure Style_StateAdded(sender: System.Object; e: StateChangedEventArgs); procedure Style_StateRemoved(sender: System.Object; e: StateChangedEventArgs); ! procedure SetBoundsCore(x, y, width, height: Integer; specified: BoundsSpecified); override; public constructor Create; function get_BlinkInterval: Double; + function get_LedState: LedStateBase; + procedure set_AutoSize(value: Boolean); procedure set_BlinkInterval(value: Double); ! procedure set_LedState(value: LedStateBase); procedure set_LedStyle(value: LedStyleBase); {$REGION 'SingleLed hiding inherited properties'} *************** *** 571,580 **** {$ENDREGION} published ! [Category('Appearance'), Description('Style of led to use.')] ! property LedStyle: LedStyleBase read FLedStyle write set_LedStyle; [Category('Behavior'), Description('Blinking interval in ms. Use 0 to disable blinking.')] property BlinkInterval: Double read get_BlinkInterval write set_BlinkInterval; ! [Category('Behavior'), Description('State of the led.'), TypeConverter(TypeOf(LedStateSelectionConverter))] ! property LedState: Integer read FLedStateIdx write set_LedState; end; {$R '..\Resources\Jedi.Windows.Forms.Hmi.Leds.SingleLed.bmp'} --- 569,580 ---- {$ENDREGION} published ! [Category('Behavior'), Description('Specifies if the control resizes to the size of the led.')] ! property AutoSize: Boolean read FAutoSize write set_AutoSize; [Category('Behavior'), Description('Blinking interval in ms. Use 0 to disable blinking.')] property BlinkInterval: Double read get_BlinkInterval write set_BlinkInterval; ! [Category('Behavior'), Description('State of the led.')] ! property LedState: LedStateBase read get_LedState write set_LedState; ! [Category('Appearance'), Description('Style of led to use.')] ! property LedStyle: LedStyleBase read FLedStyle write set_LedStyle; end; {$R '..\Resources\Jedi.Windows.Forms.Hmi.Leds.SingleLed.bmp'} *************** *** 758,761 **** --- 758,768 ---- end; + function LedStateCollection.IndexOf(value: string): Integer; + begin + Result := Count - 1; + while (Result >= 0) and (System.String.Compare(Item[Result].Name, value, True) <> 0) do + Dec(Result); + end; + procedure LedStateCollection.Insert(index: Integer; value: LedStateBase); begin *************** *** 775,781 **** {$REGION 'LedStateConverter'} function LedStateConverter.CanConvertTo(context: ITypeDescriptorContext; destinationType: System.Type): Boolean; begin ! Result := (destinationType = TypeOf(InstanceDescriptor)) or inherited CanConvertTo(context, destinationType); end; --- 782,830 ---- {$REGION 'LedStateConverter'} + class function LedStateConverter.GetStateCollection(context: ITypeDescriptorContext): LedStateCollection; + var + mi: MethodInfo; + ledStyle: LedStyleBase; + begin + Result := nil; + if Assigned(context) and Assigned(context.Instance) then + begin + mi := context.Instance.GetType.GetMethod('GetLedStyleInstance', BindingFlags.Public or BindingFlags.NonPublic or + BindingFlags.Instance, nil, System.Type.EmptyTypes, nil); + if mi <> nil then + ledStyle := LedStyleBase(mi.Invoke(context.Instance, nil)) + else + ledStyle := nil; + if ledStyle <> nil then + Result := ledStyle.States; + end; + end; + + class function LedStateConverter.IsCollectionItem(context: ITypeDescriptorContext): Boolean; + begin + Result := (context <> nil) and (context.PropertyDescriptor <> nil) and + (context.PropertyDescriptor.ComponentType.IsAssignableFrom(TypeOf(LedStateCollection))); + end; + + function LedStateConverter.CanConvertFrom(context: ITypeDescriptorContext; sourceType: System.Type): Boolean; + begin + Result := ((sourceType = TypeOf(string)) and not IsCollectionItem(context)) or + inherited CanConvertFrom(context, sourceType); + end; + function LedStateConverter.CanConvertTo(context: ITypeDescriptorContext; destinationType: System.Type): Boolean; begin ! Result := (destinationType = TypeOf(InstanceDescriptor)) or ! ((destinationType = TypeOf(string)) and not IsCollectionItem(context)) or ! inherited CanConvertTo(context, destinationType); ! end; ! ! function LedStateConverter.ConvertFrom(context: ITypeDescriptorContext; culture: CultureInfo; ! value: System.Object): System.Object; ! begin ! if not IsCollectionItem(context) and (value.GetType = TypeOf(string)) then ! Result := GetStateCollection(context).Item[GetStateCollection(context).IndexOf(string(value))] ! else ! Result := inherited ConvertFrom(context, culture, value); end; *************** *** 786,789 **** --- 835,841 ---- Result := LedStateBase(value).GetInstanceDescriptor else + if (destinationType = TypeOf(string)) and not IsCollectionItem(context) then + Result := LedStateBase(value).Name + else Result := inherited ConvertTo(context, culture, value, destinationType); end; *************** *** 802,806 **** function LedStateConverter.GetPropertiesSupported(context: ITypeDescriptorContext): Boolean; begin ! Result := True; end; {$ENDREGION} --- 854,868 ---- function LedStateConverter.GetPropertiesSupported(context: ITypeDescriptorContext): Boolean; begin ! Result := IsCollectionItem(context); ! end; ! ! function LedStateConverter.GetStandardValues(context: ITypeDescriptorContext): TypeConverter.StandardValuesCollection; ! begin ! Result := TypeConverter.StandardValuesCollection.Create(GetStateCollection(context)); ! end; ! ! function LedStateConverter.GetStandardValuesSupported(context: ITypeDescriptorContext): Boolean; ! begin ! Result := not IsCollectionItem(context); end; {$ENDREGION} *************** *** 1612,1615 **** --- 1674,1680 ---- if (LedStyle <> nil) then LedStyle.InvalidateSize; + + if (LedStyle <> nil) and (LedStyle.CoverHeight * 2 > Math.Min(GetLedSize.Width, GetLedSize.Height)) then + LedStyle.CoverHeight := Math.Min(GetLedSize.Width, GetLedSize.Height) div 2; end; *************** *** 1800,1803 **** --- 1865,1869 ---- begin inherited Create; + Count := 2; end; *************** *** 1913,1916 **** --- 1979,1984 ---- procedure MultiLedShape.set_Count(value: Integer); begin + if value < 2 then + raise ArgumentOutOfRangeException.Create('value', 'Count should be at least 2.'); if Count <> value then begin *************** *** 2182,2270 **** {$ENDREGION} - {$REGION 'LedStateSelectionConverter'} - function LedStateSelectionConverter.GetStateCollection(context: ITypeDescriptorContext): LedStateCollection; - var - mi: MethodInfo; - ledStyle: LedStyleBase; - begin - Result := nil; - if Assigned(context) and Assigned(context.Instance) then - begin - mi := context.Instance.GetType.GetMethod('GetLedStyleInstance', BindingFlags.Public or BindingFlags.NonPublic or - BindingFlags.Instance, nil, System.Type.EmptyTypes, nil); - if mi <> nil then - ledStyle := LedStyleBase(mi.Invoke(context.Instance, nil)) - else - ledStyle := nil; - if ledStyle <> nil then - Result := ledStyle.States; - end; - end; - - function LedStateSelectionConverter.ConvertFrom(context: ITypeDescriptorContext; culture: CultureInfo; - value: System.Object): System.Object; - var - states: LedStateCollection; - str: string; - i: Integer; - begin - if value.GetType = TypeOf(string) then - begin - str := string(Value); - states := GetStateCollection(context); - if states <> nil then - begin - i := states.Count - 1; - while (i >= 0) and (System.String.Compare(str, states[i].Name, True) <> 0) do - Dec(i); - if i >= 0 then - Result := System.Object(i) - else - Result := inherited ConvertFrom(context, culture, value); - end - else - Result := inherited ConvertFrom(context, culture, value); - end - else - Result := inherited ConvertFrom(context, culture, value); - end; - - function LedStateSelectionConverter.ConvertTo(context: ITypeDescriptorContext; culture: CultureInfo; - value: System.Object; destinationType: System.Type): System.Object; - var - states: LedStateCollection; - begin - if destinationType = TypeOf(string) then - begin - states := GetStateCollection(context); - if states <> nil then - begin - if value.GetType.IsAssignableFrom(TypeOf(Integer)) then - Result := states[Integer(value)].Name - else - if value.GetType.IsSubclassOf(TypeOf(LedStateBase)) then - Result := LedStateBase(value).Name - else - Result := inherited ConvertTo(context, culture, value, destinationType); - end - else - Result := inherited ConvertTo(context, culture, value, destinationType); - end - else - Result := inherited ConvertTo(context, culture, value, destinationType); - end; - - function LedStateSelectionConverter.GetStandardValues( - context: ITypeDescriptorContext): TypeConverter.StandardValuesCollection; - begin - Result := TypeConverter.StandardValuesCollection.Create(GetStateCollection(context)); - end; - - function LedStateSelectionConverter.GetStandardValuesSupported(context: ITypeDescriptorContext): Boolean; - begin - Result := True; - end; - {$ENDREGION} - {$REGION 'SingleLed'} constructor SingleLed.Create; --- 2250,2253 ---- *************** *** 2273,2277 **** InitializeComponent; LedStyle := SingleColorNormalLedStyle.Create; ! LedState := 1; FBlinkTimer := System.Timers.Timer.Create; Include(FBlinkTimer.Elapsed, BlinkTimer_Elapsed); --- 2256,2260 ---- InitializeComponent; LedStyle := SingleColorNormalLedStyle.Create; ! LedState := LedStyle.States[1]; FBlinkTimer := System.Timers.Timer.Create; Include(FBlinkTimer.Elapsed, BlinkTimer_Elapsed); *************** *** 2296,2299 **** --- 2279,2289 ---- {$ENDREGION} + procedure SingleLed.BeginInit; + begin + Inc(FInitializeCount); + if Assigned(FLedStyle) then + FLedStyle.BeginInit; + end; + procedure SingleLed.BlinkTimer_Elapsed(sender: System.Object; e: ElapsedEventArgs); begin *************** *** 2302,2305 **** --- 2292,2318 ---- end; + procedure SingleLed.EndInit; + var + idx: Integer; + begin + if FInitializeCount > 0 then + begin + Dec(FInitializeCount); + if Assigned(FLedStyle) then + FLedStyle.EndInit; + if FInitializeCount = 0 then + begin + if Text <> '' then + begin + idx := LedStyle.States.IndexOf(Text); + Text := ''; + if (idx >= 0) and (idx < LedStyle.States.Count) then + FLedStateIdx := idx; + end; + Invalidate; + end; + end; + end; + function SingleLed.GetLedStyleInstance: LedStyleBase; begin *************** *** 2307,2310 **** --- 2320,2333 ---- end; + function SingleLed.IsBlinking: Boolean; + begin + Result := FBlinkTimer.Enabled; + end; + + function SingleLed.IsInitializing: Boolean; + begin + Result := FInitializeCount > 0; + end; + procedure SingleLed.OnBackColorChanged(e: EventArgs); begin *************** *** 2316,2320 **** procedure SingleLed.OnPaint(e: PaintEventArgs); var ! TmpBrush: Brush; begin if BackgroundImage <> nil then --- 2339,2344 ---- procedure SingleLed.OnPaint(e: PaintEventArgs); var ! tmpBrush: Brush; ! tmpBmp: Bitmap; begin if BackgroundImage <> nil then *************** *** 2322,2328 **** else begin ! TmpBrush := SolidBrush.Create(BackColor); try ! e.Graphics.FillRectangle(TmpBrush, DisplayRectangle); finally TmpBrush.Free; --- 2346,2352 ---- else begin ! tmpBrush := SolidBrush.Create(BackColor); try ! e.Graphics.FillRectangle(tmpBrush, DisplayRectangle); finally TmpBrush.Free; *************** *** 2330,2336 **** end; if IsBlinking then ! e.Graphics.DrawImage(LedStyle.States[FBlinkStateIdx].GetBitmap, 0, 0) else ! e.Graphics.DrawImage(LedStyle.States[LedState].GetBitmap, 0, 0); end; --- 2354,2374 ---- end; if IsBlinking then ! tmpBmp := LedStyle.States[FBlinkStateIdx].GetBitmap else ! tmpBmp := LedState.GetBitmap; ! ! e.Graphics.DrawImage(tmpBmp, (DisplayRectangle.Width - tmpBmp.Width) div 2, ! (DisplayRectangle.Height - tmpBmp.Height) div 2); ! end; ! ! procedure SingleLed.SetBoundsCore(x, y, width, height: Integer; specified: BoundsSpecified); ! begin ! if FAutoSize and (Size = LedStyle.LedSize) then ! begin ! width := Self.Width; ! height := Self.Height; ! end; ! inherited SetBoundsCore(x, y, width, height, specified); ! Invalidate; end; *************** *** 2342,2346 **** procedure SingleLed.Style_SizeChanged(sender: System.Object; e: EventArgs); begin ! Size := LedStyle.LedSize; end; --- 2380,2385 ---- procedure SingleLed.Style_SizeChanged(sender: System.Object; e: EventArgs); begin ! if FAutoSize then ! Size := LedStyle.LedSize; end; *************** *** 2367,2405 **** end; ! procedure SingleLed.BeginInit; ! begin ! Inc(FInitializeCount); ! if Assigned(FLedStyle) then ! FLedStyle.BeginInit; ! end; ! ! procedure SingleLed.EndInit; ! begin ! if FInitializeCount > 0 then ! begin ! Dec(FInitializeCount); ! if Assigned(FLedStyle) then ! FLedStyle.EndInit; ! end; ! if FInitializeCount > 0 then ! Invalidate; ! end; ! ! function SingleLed.IsBlinking: Boolean; begin ! Result := FBlinkTimer.Enabled; end; ! function SingleLed.IsInitializing: Boolean; begin ! Result := FInitializeCount > 0; end; ! function SingleLed.get_BlinkInterval: Double; begin ! if FBlinkTimer.Enabled then ! Result := FBlinkTimer.Interval ! else ! Result := 0.0; end; --- 2406,2433 ---- end; ! function SingleLed.get_BlinkInterval: Double; begin ! if FBlinkTimer.Enabled then ! Result := FBlinkTimer.Interval ! else ! Result := 0.0; end; ! function SingleLed.get_LedState: LedStateBase; begin ! if LedStyle <> nil then ! Result := LedStyle.States.Item[fLedStateIdx] ! else ! Result := nil; end; ! procedure SingleLed.set_AutoSize(value: Boolean); begin ! if value <> FAutoSize then ! begin ! FAutoSize := value; ! if not IsInitializing and FAutoSize then ! Size := LedStyle.LedSize; ! end; end; *************** *** 2418,2427 **** end; ! procedure SingleLed.set_LedState(value: Integer); begin ! if (value <> FLedStateIdx) and (value >= 0) and (IsInitializing or (value < FLedStyle.States.Count)) then begin ! FLedStateIdx := value; ! Invalidate; end; end; --- 2446,2467 ---- end; ! procedure SingleLed.set_LedState(value: LedStateBase); ! var ! idx: Integer; begin ! if value = nil then ! raise ArgumentNullException.Create; ! if IsInitializing then ! Text := value.Name ! else begin ! idx := LedStyle.States.IndexOf(value.Name); ! if idx < 0 then ! raise ArgumentOutOfRangeException.Create('value', 'Specified led state does not belong to the current style.'); ! if (idx <> FLedStateIdx) and (IsInitializing or (idx < FLedStyle.States.Count)) then ! begin ! FLedStateIdx := idx; ! Invalidate; ! end; end; end; |