From: Olivier S. <ob...@us...> - 2004-04-23 10:11:57
|
Update of /cvsroot/jvcl/dev/JVCL3/run In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3283/dev/JVCL3/run Modified Files: JvMenus.pas Log Message: Fixed measurement of items Fixed use of canvas Index: JvMenus.pas =================================================================== RCS file: /cvsroot/jvcl/dev/JVCL3/run/JvMenus.pas,v retrieving revision 1.43 retrieving revision 1.44 diff -C2 -d -r1.43 -r1.44 *** JvMenus.pas 29 Mar 2004 23:47:22 -0000 1.43 --- JvMenus.pas 23 Apr 2004 10:11:48 -0000 1.44 *************** *** 717,720 **** --- 717,724 ---- Separator = '-'; + // The space between a menu item text and its shortcut + ShortcutSpacing = ' '; + + function CreateMenuItemPainterFromStyle(Style: TJvMenuStyle; Menu: TMenu): TJvCustomMenuItemPainter; begin *************** *** 831,835 **** begin inherited Create(AOwner); - FCanvas := TControlCanvas.Create; RegisterWndProcHook(FindForm, NewWndProc, hoAfterMsg); FStyle := msStandard; --- 835,838 ---- *************** *** 860,865 **** UnregisterWndProcHook(FindForm, NewWndProc, hoAfterMsg); inherited Destroy; - // (rom) destroy Canvas AFTER inherited Destroy - FCanvas.Free; end; --- 863,866 ---- *************** *** 1092,1115 **** (FindItem(Item.Command, fkCommand) = Item) then begin ! SaveIndex := SaveDC(hDC); try ! Canvas.Handle := hDC; ! SetDefaultMenuFont(Canvas.Font); ! Canvas.Font.Color := clMenuText; ! Canvas.Brush.Color := clMenu; ! if mdDefault in State then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! if (mdSelected in State) and not ! (Style in [msBtnLowered, msBtnRaised]) then ! begin ! Canvas.Brush.Color := clHighlight; ! Canvas.Font.Color := clHighlightText; end; - with rcItem do - IntersectClipRect(Canvas.Handle, Left, Top, Right, Bottom); - DrawItem(Item, rcItem, State); - Canvas.Handle := 0; finally ! RestoreDC(hDC, SaveIndex); end; end; --- 1093,1121 ---- (FindItem(Item.Command, fkCommand) = Item) then begin ! FCanvas := TControlCanvas.Create; try ! SaveIndex := SaveDC(hDC); ! try ! Canvas.Handle := hDC; ! SetDefaultMenuFont(Canvas.Font); ! Canvas.Font.Color := clMenuText; ! Canvas.Brush.Color := clMenu; ! if mdDefault in State then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! if (mdSelected in State) and not ! (Style in [msBtnLowered, msBtnRaised]) then ! begin ! Canvas.Brush.Color := clHighlight; ! Canvas.Font.Color := clHighlightText; ! end; ! with rcItem do ! IntersectClipRect(Canvas.Handle, Left, Top, Right, Bottom); ! DrawItem(Item, rcItem, State); ! Canvas.Handle := 0; ! finally ! RestoreDC(hDC, SaveIndex); end; finally ! Canvas.Free; end; end; *************** *** 1120,1142 **** var Item: TMenuItem; DC: HDC; begin with Message.MeasureItemStruct^ do begin ! Item := TMenuItem(Pointer(itemData)); ! if Assigned(Item) and (FindItem(Item.Command, fkCommand) = Item) then begin ! DC := GetDC(0); try ! Canvas.Handle := DC; ! SetDefaultMenuFont(Canvas.Font); ! if Item.Default then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! GetActiveItemPainter.Menu := Self; ! GetActiveItemPainter.Measure(Item, Integer(itemWidth), Integer(itemHeight)); ! MeasureItem(Item, Integer(itemWidth), Integer(itemHeight)); finally ! Canvas.Handle := 0; ! ReleaseDC(0, DC); end; end; --- 1126,1159 ---- var Item: TMenuItem; + SaveIndex: Integer; DC: HDC; begin with Message.MeasureItemStruct^ do begin ! Item := FindItem(itemID, fkCommand); ! if Assigned(Item) then begin ! DC := GetWindowDC(0); try ! FCanvas := TControlCanvas.Create; ! try ! SaveIndex := SaveDC(DC); ! try ! FCanvas.Handle := DC; ! FCanvas.Font := Screen.MenuFont; ! if Item.Default then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! GetActiveItemPainter.Menu := Self; ! GetActiveItemPainter.Measure(Item, Integer(itemWidth), Integer(itemHeight)); ! //MeasureItem(Item, Integer(itemWidth), Integer(itemHeight)); ! finally ! FCanvas.Handle := 0; ! RestoreDC(DC, SaveIndex); ! end; ! finally ! Canvas.Free; ! end; finally ! ReleaseDC(DC, 0); end; end; *************** *** 1170,1178 **** end; - {function TJvMainMenu.GetItems : TJvMenuItem; - begin - Result := TJvMenuItem(TMainMenu(Self).Items); - end;} - { TJvPopupList } --- 1187,1190 ---- *************** *** 1334,1338 **** FStyle := msStandard; FStyleItemPainter := CreateMenuItemPainterFromStyle(FStyle, Self); - FCanvas := TControlCanvas.Create; FCursor := crDefault; FImageMargin := TJvImageMargin.Create; --- 1346,1349 ---- *************** *** 1361,1366 **** PopupList.Remove(Self); inherited Destroy; - // (rom) destroy Canvas AFTER inherited Destroy - FCanvas.Free; end; --- 1372,1375 ---- *************** *** 1618,1641 **** (FindItem(Item.Command, fkCommand) = Item) then begin ! SaveIndex := SaveDC(hDC); try ! Canvas.Handle := hDC; ! SetDefaultMenuFont(Canvas.Font); ! Canvas.Font.Color := clMenuText; ! Canvas.Brush.Color := clMenu; ! if mdDefault in State then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! if (mdSelected in State) and ! not (Style in [msBtnLowered, msBtnRaised]) then ! begin ! Canvas.Brush.Color := clHighlight; ! Canvas.Font.Color := clHighlightText; end; - with rcItem do - IntersectClipRect(Canvas.Handle, Left, Top, Right, Bottom); - DrawItem(Item, rcItem, State); - Canvas.Handle := 0; finally ! RestoreDC(hDC, SaveIndex); end; end; --- 1627,1655 ---- (FindItem(Item.Command, fkCommand) = Item) then begin ! FCanvas := TControlCanvas.Create; try ! SaveIndex := SaveDC(hDC); ! try ! Canvas.Handle := hDC; ! SetDefaultMenuFont(Canvas.Font); ! Canvas.Font.Color := clMenuText; ! Canvas.Brush.Color := clMenu; ! if mdDefault in State then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! if (mdSelected in State) and ! not (Style in [msBtnLowered, msBtnRaised]) then ! begin ! Canvas.Brush.Color := clHighlight; ! Canvas.Font.Color := clHighlightText; ! end; ! with rcItem do ! IntersectClipRect(Canvas.Handle, Left, Top, Right, Bottom); ! DrawItem(Item, rcItem, State); ! Canvas.Handle := 0; ! finally ! RestoreDC(hDC, SaveIndex); end; finally ! Canvas.Free; end; end; *************** *** 1646,1666 **** var Item: TMenuItem; begin with Message.MeasureItemStruct^ do begin ! Item := TMenuItem(Pointer(itemData)); ! if Assigned(Item) and (FindItem(Item.Command, fkCommand) = Item) then begin ! Canvas.Handle := GetDC(0); try ! SetDefaultMenuFont(Canvas.Font); ! if Item.Default then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! GetActiveItemPainter.Menu := Self; ! GetActiveItemPainter.Measure(Item, Integer(itemWidth), Integer(itemHeight)); ! MeasureItem(Item, Integer(itemWidth), Integer(itemHeight)); finally ! ReleaseDC(0, Canvas.Handle); ! Canvas.Handle := 0; end; end; --- 1660,1693 ---- var Item: TMenuItem; + SaveIndex: Integer; + DC: HDC; begin with Message.MeasureItemStruct^ do begin ! Item := FindItem(itemID, fkCommand); ! if Assigned(Item) then begin ! DC := GetWindowDC(0); try ! FCanvas := TControlCanvas.Create; ! try ! SaveIndex := SaveDC(DC); ! try ! FCanvas.Handle := DC; ! FCanvas.Font := Screen.MenuFont; ! if Item.Default then ! Canvas.Font.Style := Canvas.Font.Style + [fsBold]; ! GetActiveItemPainter.Menu := Self; ! GetActiveItemPainter.Measure(Item, Integer(itemWidth), Integer(itemHeight)); ! //MeasureItem(Item, Integer(itemWidth), Integer(itemHeight)); ! finally ! FCanvas.Handle := 0; ! RestoreDC(DC, SaveIndex); ! end; ! finally ! Canvas.Free; ! end; finally ! ReleaseDC(DC, 0); end; end; *************** *** 2077,2084 **** DrawCheckImage(CheckMarkRect); ! // if we have a valid image to use for this item if UseImages then begin ! // then draw the corresponding back of an item // if the item is to be drawn checked or not if Item.Checked and not ShowCheckMarks then --- 2104,2111 ---- DrawCheckImage(CheckMarkRect); ! // if we have a valid image from the list to use for this item if UseImages then begin ! // Draw the corresponding back of an item // if the item is to be drawn checked or not if Item.Checked and not ShowCheckMarks then *************** *** 2087,2091 **** DrawNotCheckedImageBack(ImageAndMarginRect); ! // finally, draw the correct image, according to the state // of the item if (mdDisabled in State) then --- 2114,2118 ---- DrawNotCheckedImageBack(ImageAndMarginRect); ! // then, draw the correct image, according to the state // of the item if (mdDisabled in State) then *************** *** 2102,2105 **** --- 2129,2139 ---- (Item.Caption <> Separator) then begin + // Draw the corresponding back of an item + // if the item is to be drawn checked or not + if Item.Checked and not ShowCheckMarks then + DrawCheckedImageBack(ImageAndMarginRect) + else + DrawNotCheckedImageBack(ImageAndMarginRect); + if FGlyph is TBitmap then begin *************** *** 2151,2160 **** begin // find the largest text element ! MaxWidth := Canvas.TextWidth(DelChars(Item.Caption, '&') + Tab + Tab); if (Item.Parent <> nil) and (Item.ShortCut <> scNone) then begin for I := 0 to Item.Parent.Count - 1 do MaxWidth := Max(Canvas.TextWidth(DelChars(Item.Parent.Items[I].Caption, ! '&') + Tab + Tab), MaxWidth); end; --- 2185,2194 ---- begin // find the largest text element ! MaxWidth := Canvas.TextWidth(DelChars(Item.Caption, '&') + ShortcutSpacing{Tab + Tab}); if (Item.Parent <> nil) and (Item.ShortCut <> scNone) then begin for I := 0 to Item.Parent.Count - 1 do MaxWidth := Max(Canvas.TextWidth(DelChars(Item.Parent.Items[I].Caption, ! '&') + ShortcutSpacing { Tab + Tab}), MaxWidth); end; *************** *** 2226,2229 **** --- 2260,2265 ---- MaxWidth: Integer; tmpWidth: Integer; + ShortcutWidth: Integer; + OneItemHasChildren: Boolean; begin if IsPopup then *************** *** 2235,2262 **** MaxWidth := Canvas.TextWidth(DelChars(Item.Caption, '&')); // Find the widest item in the menu being displayed if Item.Parent <> nil then for I := 0 to Item.Parent.Count - 1 do begin ! tmpWidth := Canvas.TextWidth(Item.Parent.Items[I].Caption); if tmpWidth > MaxWidth then MaxWidth := tmpWidth; ! // if the item has childs, then add then we add the required // width for an arrow. It is considered to be the width of // two spaces. if Item.Parent.Items[I].Count > 0 then ! Inc(MaxWidth, Canvas.TextWidth(' ')); end; Result := MaxWidth; ! // If there is a shortcut, add its width to the current width ! if Item.ShortCut <> scNone then begin ! // It has been found that there are always one tab ! // in front of the text for the shortcut. ! Inc(Result, Canvas.TextWidth(ShortcutToText(Item.ShortCut))); ! Inc(Result, Canvas.TextWidth(Tab)); ! end; end else --- 2271,2321 ---- MaxWidth := Canvas.TextWidth(DelChars(Item.Caption, '&')); + ShortcutWidth := 0; + OneItemHasChildren := False; // Find the widest item in the menu being displayed if Item.Parent <> nil then + + // If the current item is the first one and it's not + // alone, then discard its width because for some reason + // the canvas is never correct. + if Item = Item.Parent.Items[0] then + begin + if Item.Parent.Count > 1 then + Result := 0 + else + Result := MaxWidth; + exit; + end; + for I := 0 to Item.Parent.Count - 1 do begin ! tmpWidth := Canvas.TextWidth(DelChars(Item.Parent.Items[I].Caption, '&')); if tmpWidth > MaxWidth then MaxWidth := tmpWidth; ! // if the item has childs, then add the required // width for an arrow. It is considered to be the width of // two spaces. if Item.Parent.Items[I].Count > 0 then ! OneItemHasChildren := True; ! ! if Item.Parent.Items[I].ShortCut <> scNone then ! begin ! tmpWidth := Canvas.TextWidth(ShortcutToText(Item.Parent.Items[I].ShortCut)); ! if tmpWidth > ShortcutWidth then ! ShortcutWidth := tmpWidth; ! end; end; Result := MaxWidth; ! // If there was a shortcut in any of the items, ! if ShortcutWidth <> 0 then begin ! // add its width to the current width, plus the spacing ! Inc(Result, ShortcutWidth); ! Inc(Result, Canvas.TextWidth(ShortcutSpacing)); ! end ! else if OneItemHasChildren then ! Inc(Result, Canvas.TextWidth(' ')); end else *************** *** 2380,2383 **** --- 2439,2444 ---- if Assigned(Images) then Result := Images.Height + else if Assigned(FGlyph) then + Result := FGlyph.Height else Result := ImageSize.Height; *************** *** 2388,2391 **** --- 2449,2454 ---- if Assigned(Images) then Result := Images.Width + else if Assigned(FGlyph) then + Result := FGlyph.Width else Result := ImageSize.Width; |