ホームページ>開発ツール>Xojo / Real Studio Trial and Error・CocoaのDeclareでのメモリーリークに対処する - 別稿
【ソースコードのコピー&ペーストについて】
ソースコード(グレー背景部分の全文)をコピーし、指定のウィンドウ/クラスにペーストすると、(新規作成して名前等を個別にコピー&ペーストしなくても)復元されます。
- Window1のメソッドを以下の通り書き換え
Private Sub PrintContent() // 文字列を指定してクラスオブジェクト/セレクタを取得する。最初に一回宣言しておけばよい。 Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr Declare Function NSSelectorFromString Lib "Cocoa" (aSelName As CFStringRef) As Ptr // NSPrintInfoの取得とセット(ポインターの代入だと元のPrintDialog.printInfoが書き換わってしまうので、コピーを作る) Declare Function dictionary Lib "Cocoa" selector "dictionary" (class_id As Ptr) As Ptr Dim dict As Ptr = dictionary(PrintSheet.printInfo) // PrintDialog.printInfoからdictionaryを取得 Dim pinfo As Ptr = NSClassFromString("NSPrintInfo") // クラスメソッドなので、まずNSPrintInfoクラスを取得 Declare Function alloc Lib "Cocoa" selector "alloc" (receiver As Ptr) As Ptr Declare Function initWithDictionary Lib "Cocoa" selector "initWithDictionary:" (receiver As Ptr, path As Ptr) As Ptr pinfo = initWithDictionary(alloc(pinfo), dict) // dictionaryからNSPrintInfoを生成 // パラメータのカスタマイズ Declare Sub setHorizontallyCentered Lib "Cocoa" selector "setHorizontallyCentered:" (obj_id As Ptr, flg As Boolean) setHorizontallyCentered(pinfo, false) // セット Declare Sub setVerticallyCentered Lib "Cocoa" selector "setVerticallyCentered:" (obj_id As Ptr, flg As Boolean) setVerticallyCentered(pinfo, false) // セット Declare Function paperSize Lib "Cocoa" selector "paperSize" (obj_id As Ptr) As NSSize Dim paperSize As NSSize = paperSize(pinfo) Dim margin As GFMargin Declare Function leftMargin Lib "Cocoa" selector "leftMargin" (obj_id As Ptr) As Single margin.Left = leftMargin(pinfo) Declare Function topMargin Lib "Cocoa" selector "topMargin" (obj_id As Ptr) As Single margin.Top = topMargin(pinfo) Declare Function rightMargin Lib "Cocoa" selector "rightMargin" (obj_id As Ptr) As Single margin.Right = rightMargin(pinfo) Declare Function bottomMargin Lib "Cocoa" selector "bottomMargin" (obj_id As Ptr) As Single margin.Bottom = bottomMargin(pinfo) // myNSTextViewPrintの生成。引数は順に、生成したインスタンスへのポインタ(戻り値)、用紙サイズ、マージン、ウィンドウのタイトル Dim textView As Ptr Dim a As NSTextViewPrint = new NSTextViewPrint(textView,paperSize,margin,me.Title) // myNSTextViewPrintにTextArea1の内容をセット PrintContent2(textView) // Bottomが上辺の設定になっている Declare Sub setBottomMargin Lib "Cocoa" selector "setBottomMargin:" (obj_id As Ptr, mgn As Single) setBottomMargin(pinfo, margin.Bottom+17) // ヘッダー領域分を差し引く // Topが下辺の設定になっている Declare Sub setTopMargin Lib "Cocoa" selector "setTopMargin:" (obj_id As Ptr, mgn As Single) setTopMargin(pinfo, margin.Top+15) // フッター領域分を差し引く // PrintOperationの生成 Dim pope As Ptr = NSClassFromString("NSPrintOperation") Declare Function printOperation Lib "Cocoa" selector "printOperationWithView:printInfo:" (class_id As Ptr, view As Ptr, pinfo As Ptr) As Ptr pope = printOperation(pope, textView, pinfo) // clean up Declare Sub release Lib "Cocoa" Selector "release" (receiver As Ptr) release(pinfo) release(textView) // PrintOperationの実行(シート形式のダイアログ生成を伴う。実行をPrintOperation側に任せる場合は、delegate,didRunSelectorは設定不要?っぽい) Declare Sub runOperationModalForWindow Lib "Cocoa" Selector "runOperationModalForWindow:delegate:didRunSelector:contextInfo:" (receiver As Ptr, win As Integer, dlgt As Ptr, sel As Ptr, cntx As Ptr) runOperationModalForWindow(pope, Window1.Handle, nil, nil, nil) End Sub
- NSTextViewPrintのメソッドを以下の通り書き換え
Protected Sub DrawPageBorderWithSize(size As NSSize) // 文字列を指定してクラスオブジェクトを取得する。最初に一回宣言しておけばよい。 Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr // ヘッダー(左)用スタイル付きテキスト生成 Dim stHL As Ptr = GetHeaderString(pTitle, 0) // 0 = NSLeftTextAlignment // ヘッダー(右)用スタイル付きテキスト生成 Dim d As new date // 現在日時の取得 Dim stHR As Ptr = GetHeaderString(d.LongDate+" "+d.LongTime, 1) // 1 = NSRightTextAlignment // ヘッダーのポジション Dim rectH As NSRect = NSMakeRect(pMargin.Left, pMargin.Top-3, pPaperSize.Width-pMargin.Left-pMargin.Right, 22) // フッター用スタイル付きテキスト生成 Dim stF As Ptr = GetFooterSrring() // フッターのポジション Dim rectF As NSRect = NSMakeRect(0, pPaperSize.Height-pMargin.Bottom-10, pPaperSize.Width, 22) // ロックフォーカス(これをしないと、文字が上下反転する) Declare Sub lockFocus Lib "Cocoa" selector "lockFocus" (class_id As Ptr) lockFocus(NSTextViewInst) // ヘッダー・フッターの描画 Declare Sub drawInRect Lib "Cocoa" selector "drawInRect:" (class_id As Ptr, rect As NSRect) drawInRect(stHL, rectH) drawInRect(stHR, rectH) drawInRect(stF, rectF) // アンロックフォーカス Declare Sub unlockFocus Lib "Cocoa" selector "unlockFocus" (class_id As Ptr) unlockFocus(NSTextViewInst) // clean up Declare Sub release Lib "Cocoa" Selector "release" (receiver As Ptr) release(stHL) release(stHR) release(stF) End Sub
- NSTextViewPrintのメソッドを以下の通り書き換え
Protected Function GetFooterSrring() as Ptr // 文字列を指定してクラスオブジェクトを取得する。最初に一回宣言しておけばよい。 Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr Declare Function alloc Lib "Cocoa" selector "alloc" (class_id As Ptr) As Ptr Declare Sub setObject Lib "Cocoa" Selector "setObject:forKey:" (receiver As Ptr, obj As Ptr, key As CFStringRef) // アトリビュート用Dictionaryの生成 Dim option As Ptr = NSClassFromString("NSMutableDictionary") Declare Function dictionary Lib "Cocoa" Selector "dictionary" (receiver As Ptr) As Ptr option = dictionary(option) // フォントの設定 Dim font As Ptr = NSClassFromString("NSFont") Declare Function systemFontOfSize Lib "Cocoa" Selector "systemFontOfSize:" (receiver As Ptr, size As Single) As Ptr font = systemFontOfSize(font, 9.0) // フォントをDictionaryにセット setObject(option, font, "NSFont") // ParagraphStyleの生成 Dim pgf As Ptr = NSClassFromString("NSMutableParagraphStyle") Declare Function init Lib "Cocoa" selector "init" (obj_id As Ptr) As Ptr pgf = init(alloc(pgf)) // 揃えの設定(ここではセンタリングを指定) Declare Sub setAlignment Lib "Cocoa" Selector "setAlignment:" (receiver As Ptr, type As Integer) setAlignment(pgf, 2) // 2 = NSCenterTextAlignment // ParagraphStyleをDictionaryにセット setObject(option, pgf, "NSParagraphStyle") // clean up Declare Sub release Lib "Cocoa" Selector "release" (receiver As Ptr) release(pgf) // 現在印刷中のページ番号を取得 Dim pope As Ptr = NSClassFromString("NSPrintOperation") Declare Function currentOperation Lib "Cocoa" selector "currentOperation" (class_id As Ptr) As Ptr pope = currentOperation(pope) Declare Function currentPage Lib "Cocoa" selector "currentPage" (class_id As Ptr) As Integer Dim page As Integer = currentPage(pope) // スタイル付きテキストを生成 Dim attr As Ptr = NSClassFromString("NSAttributedString") attr = alloc(attr) Declare Function initWithString Lib "Cocoa" Selector "initWithString:attributes:" (receiver As Ptr, str As CFStringRef, attr As Ptr) As Ptr attr = initWithString(attr, "- "+Str(page)+" -", option) // スタイル付きテキストを返す return attr End Function
- NSTextViewPrintのメソッドを以下の通り書き換え
Protected Function GetHeaderString(txt As String, align As Integer) as Ptr // 文字列を指定してクラスオブジェクトを取得する。最初に一回宣言しておけばよい。 Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr Declare Function alloc Lib "Cocoa" selector "alloc" (class_id As Ptr) As Ptr Declare Sub setObject Lib "Cocoa" Selector "setObject:forKey:" (receiver As Ptr, obj As Ptr, key As CFStringRef) // アトリビュート用Dictionaryの生成 Dim option As Ptr = NSClassFromString("NSMutableDictionary") Declare Function dictionary Lib "Cocoa" Selector "dictionary" (receiver As Ptr) As Ptr option = dictionary(option) // フォントの設定 Dim font As Ptr = NSClassFromString("NSFont") Declare Function systemFontOfSize Lib "Cocoa" Selector "systemFontOfSize:" (receiver As Ptr, size As Single) As Ptr font = systemFontOfSize(font, 9.0) // フォントをDictionaryにセット setObject(option, font, "NSFont") // ParagraphStyleの生成 Dim pgf As Ptr = NSClassFromString("NSMutableParagraphStyle") Declare Function init Lib "Cocoa" selector "init" (obj_id As Ptr) As Ptr pgf = init(alloc(pgf)) // 揃えの設定(ここでは外部からのパラメータを指定) Declare Sub setAlignment Lib "Cocoa" Selector "setAlignment:" (receiver As Ptr, type As Integer) setAlignment(pgf, align) // ParagraphStyleをDictionaryにセット setObject(option, pgf, "NSParagraphStyle") // clean up Declare Sub release Lib "Cocoa" Selector "release" (receiver As Ptr) release(pgf) // スタイル付きテキストを生成 Dim attr As Ptr = NSClassFromString("NSAttributedString") attr = alloc(attr) Declare Function initWithString Lib "Cocoa" Selector "initWithString:attributes:" (receiver As Ptr, str As CFStringRef, attr As Ptr) As Ptr attr = initWithString(attr, txt, option) // スタイル付きテキストを返す return attr End Function
[Home] [MacSoft] [Donation] [History] [Privacy Policy] [Affiliate Policy]