My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
CodeSnippets  
this page contains code snippets to help beginners starts with Pharo and learn cool things
Updated Sep 7, 2011 by marcus.d...@gmail.com

How to shrink the changes file?

Smalltalk condenseChanges

This will free up some space in the changes file at the cost of removing version history in your image.

Examples to build a GUI

UITheme exampleBasicControls.
UITheme exampleColorControls.
UITheme exampleDialogs.
UITheme exampleGroups.
UITheme exampleOtherControls.
UITheme exampleWindowWithToolbars.

Disabling features for more speed

The following statements can be run in a Pharo image to increase the speed of the IDE. The script selects the OBSystemBrowser (code browser without package view), it disables the virtual method categories, removes the annotation pane above the source code, and enables fast window dragging.

SystemBrowser default: OBSystemBrowserAdaptor.
Preferences disable: #dynamicProtocolActivation.
Preferences disable: #annotationPanes.
Preferences enable: #fastDragWindowForMorphic.

How to set demo mode

In Pharo 1.0

Preferences setDemoFonts

In Pharo 1.1

From the setting browser:

Style button (top button bar) then "load" then choose "DemoMode" or

DemoSettingStyle new load

Playing with fonts

The following script sets the fonts in Pharo. Feel free to change the font family names and sizes to what suits you best. You will have to reopen each window to see the change.

In Pharo 1.0

|font codeFont|
font := LogicalFont familyName: 'DejaVu Serif' pointSize: 10.
codeFont := LogicalFont familyName: 'DejaVu Sans Mono' pointSize: 9.
Preferences setListFontTo: font.
Preferences setMenuFontTo: font.
Preferences setCodeFontTo: codeFont.
Preferences setButtonFontTo: font.
Preferences setSystemFontTo: font.
font := LogicalFont familyName: 'DejaVu Serif' pointSize: 11.
Preferences setWindowTitleFontTo: font.

You can also set the fonts using the graphical interface in the Preferences menu.

In Pharo 1.1

|font codeFont|
font := LogicalFont familyName: 'DejaVu Serif' pointSize: 10.
codeFont := LogicalFont familyName: 'DejaVu Sans Mono' pointSize: 9.
StandardFonts listFont: font.
StandardFonts menuFont: font.
StandardFonts codeFont: codeFont.
StandardFonts buttonFont: font.
StandardFonts defaultFont: font.
font := LogicalFont familyName: 'DejaVu Serif' pointSize: 11.
StandardFonts windowTitleFont: font.

or

StandardFonts setFontsFromSpec:
        #( #(#defaultFont: 'Bitmap DejaVu' 9 )
            #(#codeFont: 'Bitmap DejaVu' 9 )
            #(#listFont: 'Bitmap DejaVu' 9 )
            #(#menuFont: 'Bitmap DejaVu' 9 )
            #(#windowTitleFont: 'Bitmap DejaVu' 12 )
            #(#balloonFont:  'Bitmap DejaVu' 10 )
            #(#haloFont:  'Bitmap DejaVu' 10 )
            #(#buttonFont:  'Bitmap DejaVu' 9 ) ).

You can also set the fonts using the Settings tool: <world Menu>/System/Settings/Appearance/Standard fonts.

Double-buffering using Display and BitBlt

Compare these two:

1 to: 1000 do:[:i|
	Display getCanvas line: i@0 to: i@500 width: 1 color: Color black.
].

vs. (after doing a restore display):

Display deferUpdates: true.
1 to: 1000 do:[:i|
	Display getCanvas line: i@0 to: i@500 width: 1 color: Color black.
].
Display forceToScreen: Display boundingBox. "way too much but hey..."

(Submitted by Andreas Raab on the Squeak mailing list)

Knowing the list of modules

The #listLoadedModules are the plugins (internal and external) that your image has actually used since you last restarted it. When you inspect the list of loaded modules, you will notice either '(i)' or '(e)' to indicate whether the loaded module is an internal plugin or and external plugin.

SmalltalkImage current listLoadedModules

The available plugins are those that appear in #listBuiltinModules, plus any external plugins that the VM can find in shared libraries. It doesn't matter if they were loaded or not.

SmalltalkImage current listBuiltinModules

To load the FFI module

To install FFI in Pharo, evaluate:

Gofer new
    squeaksource: 'MetacelloRepository';
    package: 'ConfigurationOfFFI';
    load.

((Smalltalk at: #ConfigurationOfFFI) project version: '1.3') load.

Searching for unsaved packages (advanced)

To refresh all the packages of your image and see which ones have not been saved, execute the following

|pharoRepository|
pharoRepository := MCWorkingCopyBrowser new repositories
			detect: [:ea | (ea isKindOf: MCHttpRepository)
					and: [ea locationWithTrailingSlash  endsWith: 'Pharo/']].

UIManager default informUserDuring: [:bar |
	MCWorkingCopyBrowser new workingCopies do: [:workingCopy| |repository patch|
		bar value: workingCopy printString.
		(workingCopy repositoryGroup repositories size = 1)
			ifTrue: ["The working copy is only associated with the cache, that won''t help us, so choose the Pharo repository"
				repository := pharoRepository]
			ifFalse: ["Arbitrarily selects the last repository of this working copy"
				repository := workingCopy repositoryGroup repositories last.
				"If the choosen is PharoInbox then selects the Pharo repository"
				(repository locationWithTrailingSlash endsWith: 'PharoInbox/')
					ifTrue: [repository := pharoRepository]].
		patch := workingCopy changesRelativeToRepository: repository.
		patch isEmpty ifFalse: [workingCopy modified: true]]]

Why an object is not garbage collected?

To see pointers to that object

PointerExplorer openOn: anObject

Or you can do something like this (example with HandMorph):

HandMorph allInstancesDo: [:e | PointerExplorer openOn: e]

How to create a tab with an icon?

TabGroupMorph/TabSelectorMorph support arbitrary morphs as well as text labels.

An example:

|dialog|
dialog := (UITheme builder newPluggableDialogWindow: 'Example tabs') 
useDefaultOKButton.
dialog contentMorph: (
 dialog newTabGroup: {
  (dialog newRow: {dialog newImage: MenuIcons smallFindIcon. dialog 
newLabel: 'Page 1'})->
   dialog newPanel.
  'Page 2'->dialog newPanel}).
dialog model: nil.
World openModal: dialog

How to fix underscore assigment with RB ?

Since the refactoring engine and OmniBrowser has no overrides and cleanly loads into Pharo 1.0 and 1.1 images this can conveniently be done from the GUI.

Alternatively the following script works without GUI:

1. Load the code:

   Gofer new
       squeaksource: 'rb';
       package: 'AST-Core';
       package: 'Refactoring-Core';
       load.

2. Select the code (packages) you want to fix:

   environment := BrowserEnvironment new
       forPackageNames: #('PackageName1' 'PackageName2').

3. Create the transformation rule:

   rule := RBUnderscoreAssignmentRule new.

4. Perform the search:

   SmalllintChecker runRule: rule onEnvironment: environment.

5. Perform the transformation:

   change := CompositeRefactoryChange new
   change changes: rule changes.
   change execute
Comment by project member marianopeck, May 20, 2009

How to know the package of a class?

PackageOrganizer? default packageOfClass: aClass

Comment by project member tudor.gi...@gmail.com, Jul 8, 2009

How to reinitialize the font system?

FreeTypeFontProvider current updateFromSystem

Comment by jimg1...@gmail.com, Sep 21, 2009

Chinese Fonts

menuFont := LogicalFont familyName: 'SimSun' pointSize: 12. 
titleFont := LogicalFont familyName: 'SimHei' pointSize: 13.
codeFont := LogicalFont familyName: 'PMinLiU' pointSize: 13. 
defaultFont := LogicalFont familyName: 'SimSun' pointSize: 13. 
Preferences setSystemFontTo: defaultFont. 
Preferences setListFontTo: defaultFont. 
Preferences setFlapsFontTo: menuFont. 
Preferences setHaloLabelFontTo: defaultFont. 
Preferences setMenuFontTo: menuFont. 
Preferences setWindowTitleFontTo: titleFont. 
Preferences setBalloonHelpFontTo: defaultFont. 
Preferences setCodeFontTo: codeFont. 
Preferences setButtonFontTo: defaultFont.
Comment by project member tudor.gi...@gmail.com, Nov 26, 2009

How to set the user to squeaksource.com Monticello Repositories

repos := (MCWorkingCopy allManagers flatCollect: [:e | e repositoryGroup repositories]) asSet.
squeaksourceRepos := repos select: [:each | 
	(each isKindOf: MCHttpRepository) and: [
		each locationWithTrailingSlash beginsWith: 'http://www.squeaksource.com'] ].
squeaksourceRepos do: [:each | each user: 'YOURUSER'; password: 'YOURPASSWORD']
Comment by project member adrian.l...@gmail.com, Jun 24, 2010

How to add a nice background wallpaper

Nice wallpaper (from Ubuntu) that fits well with the pharo logo, polymorph and seaside:

|form|
form := (Form fromBinaryStream: (HTTPSocket httpGet: 		 
     'http://ubuntu.ecchi.ca/wallpapers/10.04/' , 'Maraetaibeforesunrise.jpg')).
World backgroundImage: form layout: #scaled

(contributed by Torsten)

Comment by daliot...@gmail.com, Apr 9, 2011

<H1>How to get all package names(major category names)</H1>

(SystemOrganizer? allInstances anyOne categories asSet collect: [:each| each readStream upTo: $-]) asSortedCollection


Sign in to add a comment
Powered by Google Project Hosting