summaryrefslogtreecommitdiff
path: root/misc/pylib/robofab/interface/all/dialogs_fontlab_legacy2.py
blob: 460b73f1e810f627382c1edf527b25c8af482735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""

    Dialogs for FontLab 5.1.
    This might work in future versions of FontLab as well. 
    This is basically a butchered version of vanilla.dialogs.
    No direct import of, or dependency on Vanilla
    
    March 7 2012
    It seems only the dialogs that deal with the file system
    need to be replaced, the other dialogs still work. 
    As we're not entirely sure whether it is worth to maintain
    these dialogs, let's fix the imports in dialogs.py.
    
    This is the phenolic aldehyde version of dialogs.

"""

#__import__("FL")
from FL import *

from Foundation import NSObject
from AppKit import NSApplication, NSInformationalAlertStyle, objc, NSAlert, NSAlertFirstButtonReturn, NSAlertSecondButtonReturn, NSAlertThirdButtonReturn, NSSavePanel, NSOKButton, NSOpenPanel

NSApplication.sharedApplication()

__all__ = [
#    "AskString",
    "AskYesNoCancel",
#    "FindGlyph",
    "GetFile",
    "GetFolder",
	"GetFileOrFolder",
    "Message",
#    "OneList",
    "PutFile",
#    "SearchList",
#    "SelectFont",
#    "SelectGlyph",
#    "TwoChecks",
#    "TwoFields",
    "ProgressBar",
]


class BaseMessageDialog(NSObject):

    def initWithMessageText_informativeText_alertStyle_buttonTitlesValues_window_resultCallback_(self,
            messageText="",
            informativeText="", 
            alertStyle=NSInformationalAlertStyle,
            buttonTitlesValues=None,
            parentWindow=None,
            resultCallback=None):
        if buttonTitlesValues is None:
            buttonTitlesValues = []
        self = super(BaseMessageDialog, self).init()
        self.retain()
        self._resultCallback = resultCallback
        self._buttonTitlesValues = buttonTitlesValues
        #
        alert = NSAlert.alloc().init()
        alert.setMessageText_(messageText)
        alert.setInformativeText_(informativeText)
        alert.setAlertStyle_(alertStyle)
        for buttonTitle, value in buttonTitlesValues:
            alert.addButtonWithTitle_(buttonTitle)
        self._value = None
        code = alert.runModal()
        self._translateValue(code)
        return self

    def _translateValue(self, code):
        if code == NSAlertFirstButtonReturn:
            value = 1
        elif code == NSAlertSecondButtonReturn:
            value = 2
        elif code == NSAlertThirdButtonReturn:
            value = 3
        else:
            value = code - NSAlertThirdButtonReturn + 3
        self._value = self._buttonTitlesValues[value-1][1]

    def windowWillClose_(self, notification):
        self.autorelease()


class BasePutGetPanel(NSObject):

    def initWithWindow_resultCallback_(self, parentWindow=None, resultCallback=None):
        self = super(BasePutGetPanel, self).init()
        self.retain()
        self._parentWindow = parentWindow
        self._resultCallback = resultCallback
        return self

    def windowWillClose_(self, notification):
        self.autorelease()


class PutFilePanel(BasePutGetPanel):

    def initWithWindow_resultCallback_(self, parentWindow=None, resultCallback=None):
        self = super(PutFilePanel, self).initWithWindow_resultCallback_(parentWindow, resultCallback)
        self.messageText = None
        self.title = None
        self.fileTypes = None
        self.directory = None
        self.fileName = None
        self.canCreateDirectories = True
        self.accessoryView = None
        self._result = None
        return self

    def run(self):
        panel = NSSavePanel.alloc().init()
        if self.messageText:
            panel.setMessage_(self.messageText)
        if self.title:
            panel.setTitle_(self.title)
        if self.directory:
            panel.setDirectory_(self.directory)
        if self.fileTypes:
            panel.setAllowedFileTypes_(self.fileTypes)
        panel.setCanCreateDirectories_(self.canCreateDirectories)
        panel.setCanSelectHiddenExtension_(True)
        panel.setAccessoryView_(self.accessoryView)
        if self._parentWindow is not None:
            panel.beginSheetForDirectory_file_modalForWindow_modalDelegate_didEndSelector_contextInfo_(
                    self.directory, self.fileName, self._parentWindow, self, "savePanelDidEnd:returnCode:contextInfo:", 0)
        else:
            isOK = panel.runModalForDirectory_file_(self.directory, self.fileName)
            if isOK == NSOKButton:
                self._result = panel.filename()

    def savePanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
        panel.close()
        if returnCode:
            self._result = panel.filename()
            if self._resultCallback is not None:
                self._resultCallback(self._result)

    savePanelDidEnd_returnCode_contextInfo_ = objc.selector(savePanelDidEnd_returnCode_contextInfo_, signature="v@:@ii")


class GetFileOrFolderPanel(BasePutGetPanel):

    def initWithWindow_resultCallback_(self, parentWindow=None, resultCallback=None):
        self = super(GetFileOrFolderPanel, self).initWithWindow_resultCallback_(parentWindow, resultCallback)
        self.messageText = None
        self.title = None
        self.directory = None
        self.fileName = None
        self.fileTypes = None
        self.allowsMultipleSelection = False
        self.canChooseDirectories = True
        self.canChooseFiles = True
        self.resolvesAliases = True
        self._result = None
        return self

    def run(self):
        panel = NSOpenPanel.alloc().init()
        if self.messageText:
            panel.setMessage_(self.messageText)
        if self.title:
            panel.setTitle_(self.title)
        if self.directory:
            panel.setDirectory_(self.directory)
        if self.fileTypes:
            panel.setAllowedFileTypes_(self.fileTypes)
        panel.setCanChooseDirectories_(self.canChooseDirectories)
        panel.setCanChooseFiles_(self.canChooseFiles)
        panel.setAllowsMultipleSelection_(self.allowsMultipleSelection)
        panel.setResolvesAliases_(self.resolvesAliases)
        if self._parentWindow is not None:
            panel.beginSheetForDirectory_file_types_modalForWindow_modalDelegate_didEndSelector_contextInfo_(
                    self.directory, self.fileName, self.fileTypes, self._parentWindow, self, "openPanelDidEnd:returnCode:contextInfo:", 0)
        else:
            isOK = panel.runModalForDirectory_file_types_(self.directory, self.fileName, self.fileTypes)
            if isOK == NSOKButton:
                self._result = panel.filenames()

    def openPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
        panel.close()
        if returnCode:
            self._result = panel.filenames()
            if self._resultCallback is not None:
                self._resultCallback(self._result)

    openPanelDidEnd_returnCode_contextInfo_ = objc.selector(openPanelDidEnd_returnCode_contextInfo_, signature="v@:@ii")


def Message(message="", title='noLongerUsed', informativeText=""):
    """Legacy robofab dialog compatible wrapper."""
    #def _message(messageText="", informativeText="", alertStyle=NSInformationalAlertStyle, parentWindow=None, resultCallback=None):
    resultCallback = None
    alert = BaseMessageDialog.alloc().initWithMessageText_informativeText_alertStyle_buttonTitlesValues_window_resultCallback_(
                messageText=message,
                informativeText=informativeText,
                alertStyle=NSInformationalAlertStyle,
                buttonTitlesValues=[("OK", 1)],
                parentWindow=None,
                resultCallback=None)
    if resultCallback is None:
        return 1


def AskYesNoCancel(message, title='noLongerUsed', default=None, informativeText=""):
    """
        AskYesNoCancel Dialog
        
        message             the string
        title*              a title of the window
                            (may not be supported everywhere)
        default*            index number of which button should be default
                            (i.e. respond to return)
        informativeText*    A string with secundary information

        * may not be supported everywhere
    """
    parentWindow = None
    alert = BaseMessageDialog.alloc().initWithMessageText_informativeText_alertStyle_buttonTitlesValues_window_resultCallback_(
        messageText=message,
        informativeText=informativeText,
        alertStyle=NSInformationalAlertStyle,
        buttonTitlesValues=[("Cancel", -1), ("Yes", 1), ("No", 0)],
        parentWindow=None,
        resultCallback=None)
    return alert._value

def _askYesNo(messageText="", informativeText="", alertStyle=NSInformationalAlertStyle, parentWindow=None, resultCallback=None):
    parentWindow = None
    alert = BaseMessageDialog.alloc().initWithMessageText_informativeText_alertStyle_buttonTitlesValues_window_resultCallback_(
        messageText=messageText, informativeText=informativeText, alertStyle=alertStyle, buttonTitlesValues=[("Yes", 1), ("No", 0)], parentWindow=parentWindow, resultCallback=resultCallback)
    if resultCallback is None:
        return alert._value

def GetFile(message=None, title=None, directory=None, fileName=None, allowsMultipleSelection=False, fileTypes=None):
    """ Legacy robofab dialog compatible wrapper.
        This will select UFO on OSX 10.7, FL5.1
    """
    parentWindow = None
    resultCallback=None
    basePanel = GetFileOrFolderPanel.alloc().initWithWindow_resultCallback_(parentWindow, resultCallback)
    basePanel.messageText = message
    basePanel.title = title
    basePanel.directory = directory
    basePanel.fileName = fileName
    basePanel.fileTypes = fileTypes
    basePanel.allowsMultipleSelection = allowsMultipleSelection
    basePanel.canChooseDirectories = False
    basePanel.canChooseFiles = True
    basePanel.run()
    if basePanel._result is None:
        return None
    if not allowsMultipleSelection:
        # compatibly return only one as we expect
        return str(list(basePanel._result)[0])
    else:
        # return more if we explicitly expect
        return [str(n) for n in list(basePanel._result)]

def GetFolder(message=None, title=None, directory=None, allowsMultipleSelection=False):
    parentWindow = None
    resultCallback = None
    basePanel = GetFileOrFolderPanel.alloc().initWithWindow_resultCallback_(parentWindow, resultCallback)
    basePanel.messageText = message
    basePanel.title = title
    basePanel.directory = directory
    basePanel.allowsMultipleSelection = allowsMultipleSelection
    basePanel.canChooseDirectories = True
    basePanel.canChooseFiles = False
    basePanel.run()
    if basePanel._result is None:
        return None
    if not allowsMultipleSelection:
        # compatibly return only one as we expect
        return str(list(basePanel._result)[0])
    else:
        # return more if we explicitly expect
        return [str(n) for n in list(basePanel._result)]

def GetFileOrFolder(message=None, title=None, directory=None, fileName=None, allowsMultipleSelection=False, fileTypes=None, parentWindow=None, resultCallback=None):
    parentWindow = None
    basePanel = GetFileOrFolderPanel.alloc().initWithWindow_resultCallback_(parentWindow, resultCallback)
    basePanel.messageText = message
    basePanel.title = title
    basePanel.directory = directory
    basePanel.fileName = fileName
    basePanel.fileTypes = fileTypes
    basePanel.allowsMultipleSelection = allowsMultipleSelection
    basePanel.canChooseDirectories = True
    basePanel.canChooseFiles = True
    basePanel.run()
    if basePanel._result is None:
        return None
    if not allowsMultipleSelection:
        # compatibly return only one as we expect
        return str(list(basePanel._result)[0])
    else:
        # return more if we explicitly expect
        return [str(n) for n in list(basePanel._result)]

def PutFile(message=None, title=None, directory=None, fileName=None, canCreateDirectories=True, fileTypes=None):
    parentWindow = None
    resultCallback=None
    accessoryView=None
    basePanel = PutFilePanel.alloc().initWithWindow_resultCallback_(parentWindow, resultCallback)
    basePanel.messageText = message
    basePanel.title = title
    basePanel.directory = directory
    basePanel.fileName = fileName
    basePanel.fileTypes = fileTypes
    basePanel.canCreateDirectories = canCreateDirectories
    basePanel.accessoryView = accessoryView
    basePanel.run()
    return str(basePanel._result)


class ProgressBar(object):

    def __init__(self, title="RoboFab...", ticks=0, label=""):
        self._tickValue = 1
        fl.BeginProgress(title, ticks)

    def getCurrentTick(self):
        return self._tickValue

    def tick(self, tickValue=None):
        if not tickValue:
            tickValue = self._tickValue
        fl.TickProgress(tickValue)
        self._tickValue = tickValue + 1

    def label(self, label):
        pass

    def close(self):
        fl.EndProgress()


# we seem to have problems importing from here.
# so let's see what happens if we make the robofab compatible wrappers here as well.

# start with all the defaults. 

#def AskString(message, value='', title='RoboFab'):
#    raise NotImplementedError

#def FindGlyph(aFont, message="Search for a glyph:", title='RoboFab'):
#    raise NotImplementedError

#def OneList(list, message="Select an item:", title='RoboFab'):
#    raise NotImplementedError
    
#def PutFile(message=None, fileName=None):
#    raise NotImplementedError

#def SearchList(list, message="Select an item:", title='RoboFab'):
#    raise NotImplementedError

#def SelectFont(message="Select a font:", title='RoboFab'):
#    raise NotImplementedError

#def SelectGlyph(font, message="Select a glyph:", title='RoboFab'):
#    raise NotImplementedError

#def TwoChecks(title_1="One",  title_2="Two", value1=1, value2=1, title='RoboFab'):
#    raise NotImplementedError

#def TwoFields(title_1="One:", value_1="0", title_2="Two:", value_2="0", title='RoboFab'):
#    raise NotImplementedError