Ticket #7340: showimage-new-from-clipboard-001.diff

File showimage-new-from-clipboard-001.diff, 4.3 KB (added by mmu_man, 13 years ago)

Unfinished attempt at adding a "New from clipboard" menu item

  • src/apps/showimage/ShowImageWindow.cpp

     
    118118
    119119
    120120ShowImageWindow::ShowImageWindow(BRect frame, const entry_ref& ref,
    121     const BMessenger& trackerMessenger)
     121    const BMessenger& trackerMessenger, bool fromClipboard)
    122122    :
    123123    BWindow(frame, "", B_DOCUMENT_WINDOW, 0),
    124124    fNavigator(ref, trackerMessenger),
     
    187187
    188188    SetSizeLimits(250, 100000, 100, 100000);
    189189
    190     // finish creating the window
    191     if (_LoadImage() != B_OK) {
     190    if (fromClipboard) {
     191printf("fromClipboard\n");
     192        status_t status = B_ERROR;
     193        //XXX: needs cleanup
     194        BBitmap *bitmap = NULL;
     195        if (be_clipboard->Lock()) {
     196            BMessage *clip = be_clipboard->Data();
     197            BMessage archivedBitmap;
     198            if (clip->FindMessage("image/bitmap", &archivedBitmap) == B_OK
     199                || clip->FindMessage("image/bitmap", &archivedBitmap) == B_OK) {
     200                bitmap = new(std::nothrow) BBitmap(&archivedBitmap);
     201printf("bitmap %p\n", bitmap);
     202            }
     203            be_clipboard->Unlock();
     204        }
     205        if (bitmap)
     206            status = fImageView->SetImage(NULL, bitmap);
     207printf("error 0x%08lx\n", status);
     208        if (status != B_OK) {
     209            //XXX
     210            entry_ref ref;
     211            _LoadError(ref);
     212            Quit();
     213            return;
     214        }
     215        fImageView->FitToBounds();
     216        fImageView->MakeFocus(true);
     217        SetTitle("Untitled");
     218        Show();
     219    } else if (_LoadImage() != B_OK) { // finish creating the window
    192220        _LoadError(ref);
    193221        Quit();
    194222        return;
    195223    }
     224printf("done\n");
    196225
    197226    // add View menu here so it can access ShowImageView methods
    198227    BMenu* menu = new BMenu(B_TRANSLATE_WITH_CONTEXT("View", "Menus"));
     
    211240    // and tell this window if it contains interesting data or not
    212241    be_app_messenger.SendMessage(B_CLIPBOARD_CHANGED);
    213242
     243printf("Run()\n");
    214244    // The window will be shown on screen automatically
    215245    Run();
    216246}
     
    320350{
    321351    BMenu* menu = new BMenu(B_TRANSLATE("File"));
    322352
     353    _AddItemMenu(menu, B_TRANSLATE("New from clipboard"), B_PASTE, 'N', 0, this);
    323354    // Add recent files to "Open File" entry as sub-menu.
    324355    BMenuItem* item = new BMenuItem(BRecentFilesList::NewFileListMenu(
    325356        B_TRANSLATE("Open"B_UTF8_ELLIPSIS), NULL, NULL, be_app, 10, true,
     
    711742            fImageView->CopySelectionToClipboard();
    712743            break;
    713744
     745        case B_PASTE:
     746            be_app->PostMessage(message);
     747            break;
     748
    714749        case MSG_SELECTION_MODE:
    715750            fImageView->SetSelectionMode(_ToggleMenuItem(MSG_SELECTION_MODE));
    716751            break;
  • src/apps/showimage/ShowImageWindow.h

     
    4747public:
    4848                                ShowImageWindow(BRect frame,
    4949                                    const entry_ref& ref,
    50                                     const BMessenger& trackerMessenger);
     50                                    const BMessenger& trackerMessenger,
     51                                    bool fromClipboard=false);
    5152    virtual                     ~ShowImageWindow();
    5253
    5354            void                BuildContextMenu(BMenu* menu);
  • src/apps/showimage/ShowImageApp.cpp

     
    125125            _StartPulse();
    126126            break;
    127127
     128        case B_PASTE:
     129            _CreateFromClipboard();
     130            break;
     131
    128132        case B_CLIPBOARD_CHANGED:
    129133            _CheckClipboard();
    130134            break;
     
    237241
    238242
    239243void
     244ShowImageApp::_CreateFromClipboard()
     245{
     246    fLastWindowFrame.OffsetBy(20, 20);
     247    if (!BScreen(B_MAIN_SCREEN_ID).Frame().Contains(fLastWindowFrame))
     248        fLastWindowFrame.OffsetTo(50, 50);
     249   
     250    entry_ref ref;
     251    BMessenger messenger;
     252    new ShowImageWindow(fLastWindowFrame, ref, messenger, true);
     253}
     254
     255
     256void
    240257ShowImageApp::_CheckClipboard()
    241258{
    242259    // Determines if the contents of the clipboard contain
  • src/apps/showimage/ShowImageApp.h

     
    4343            void                _Open(const entry_ref& ref,
    4444                                    const BMessenger& trackerMessenger);
    4545            void                _BroadcastToWindows(BMessage* message);
     46            void                _CreateFromClipboard();
    4647            void                _CheckClipboard();
    4748            void                _UpdateLastWindowFrame();
    4849