(C++) MessageBox для Linux как в MS Windows

В Windows Explorer, правой кнопкой мыши, перетаскивают файл от того, где это - туда, где Вы хотите его. После выпуска правой кнопки мыши Вы будете видеть, что контекстное меню с опциями, такими как "Перемещение SVN присвоило версию файлу здесь".

http://tortoisesvn.net/most-forgotten-feature

7
задан karx11erx 6 September 2009 в 22:10
поделиться

4 ответа

Я лично использую Qt4 QMessageBox .

пример:

QMessageBox mb(QMessageBox::Question, "Title", "Message",  QMessageBox::Ok | QMessageBox::Cancel);
if(mb.exec() == QMessageBox::Ok) { do_stuff(); }
3
ответ дан 6 December 2019 в 15:24
поделиться

Похоже, вам нужно будет создать окно X11 / Motif верхнего уровня. Вот код для начала:

#include <Xm/Xm.h> 
#include <Xm/PushB.h>

/* Prototype Callback function */

void pushed_fn(Widget , XtPointer , 
               XmPushButtonCallbackStruct *);


main(int argc, char **argv) 

{   Widget top_wid, button;
    XtAppContext  app;

    top_wid = XtVaAppInitialize(&app, "Push", NULL, 0,
        &argc, argv, NULL, NULL);

    button = XmCreatePushButton(top_wid, "Push_me", NULL, 0);

    /* tell Xt to manage button */
                XtManageChild(button);

                /* attach fn to widget */
    XtAddCallback(button, XmNactivateCallback, pushed_fn, NULL);

    XtRealizeWidget(top_wid); /* display widget hierarchy */
    XtAppMainLoop(app); /* enter processing loop */ 

}

void pushed_fn(Widget w, XtPointer client_data, 
               XmPushButtonCallbackStruct *cbs) 
  {   
     printf("Don't Push Me!!\n");
  }

Он был скопирован из здесь , который может дать вам еще несколько указателей по этому поводу.

2
ответ дан 6 December 2019 в 15:24
поделиться

Я бы посоветовал вам изучить одну из библиотек GUI, которые поддерживают SDL в качестве бэкэнда. Одной из таких библиотек будет GG, у которой есть класс ThreeButtonDlg . Когда его Run () возвращается, вы можете посмотреть его Result (). См. Метод Initial в их примере минимальный .

1
ответ дан 6 December 2019 в 15:24
поделиться

Вот мое решение. Я решил использовать Motif (OpenMotif), поскольку для этого требуется сравнительно немного дополнительных библиотек (Xm, Xt, X11). В зависимости от размера сообщения моя реализация открывает простое окно сообщения или более сложный диалог с не редактируемым, прокручиваемым текстом (последний взят из руководства программиста Motif и адаптирован для моих целей).

Включить файлы и глобальные данные:

#include <Xm/Xm.h>
#include <Xm/MwmUtil.h>
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/PushBG.h>
#include <Xm/LabelG.h>
#include <Xm/PanedW.h>
#include <Xm/Text.h>
#include <Xm/DialogS.h>
#include <Xm/Command.h>

static XtAppContext appShell;

Вспомогательная функция для определить ряды и макс. столбцы текстового сообщения:

static int MsgSize (char* pszMsg, int& nCols)
{
if (!(pszMsg && *pszMsg))
   return 0;
int nRows = 1;
nCols = 0;
for (char* p = pszMsg; *p && (pszMsg = strchr (p, '\n')); nRows++, p = ++pszMsg) {
   if (nCols < pszMsg - p)
      nCols = pszMsg - p;
   }
return nRows;
}

Функция обратного вызова для кнопки закрытия диалогового окна сообщения:

void DestroyShell (Widget widget, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget) clientData;
XtDestroyWidget (shell);
// tell the application event loop to terminate w/o terminating the application
XtAppSetExitFlag (appShell);
}

Создание диалогового окна, содержащего прокручиваемый, не редактируемый текстовый виджет и кнопку закрытия. Взято из руководства программиста Motif и немного адаптировано (без значка, одна кнопка), минимальное оформление окна).

void XmMessageDialog (const char* pszMsg, int nRows, int nCols, bool bError)
{
    Widget       msgBox, pane, msgText, form, widget;
    void         DestroyShell(Widget, XtPointer, XtPointer);
    Arg          args [10];
    int          n = 0;
    int          i;
    Dimension    h;

// Set up a DialogShell as a popup window. Set the delete window protocol response to XmDESTROY to make sure that
// the window goes away appropriately. Otherwise, it's XmUNMAP which means it'd be lost forever, since we're not storing
// the widget globally or statically to this function.
Widget topWid = XtVaAppInitialize (&appShell, "D2X-XL", NULL, 0, &argc, argv, NULL, NULL);
XtSetArg (args [0], XmNdeleteResponse, XmDESTROY);
msgBox = XmCreateDialogShell (topWid, bError ? const_cast<char*>("Error") : const_cast<char*>("Warning"), args, 1);
XtVaGetValues (msgBox, XmNmwmDecorations, &i, NULL);
i &= ~(MWM_DECOR_ALL | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE | MWM_DECOR_MENU);
XtVaSetValues (msgBox, XmNmwmDecorations, i, NULL);
XtVaGetValues (msgBox, XmNmwmFunctions, &i, NULL);
i &= ~(MWM_FUNC_ALL | MWM_FUNC_MINIMIZE | MWM_FUNC_MAXIMIZE | MWM_FUNC_CLOSE);
XtVaSetValues (msgBox, XmNmwmFunctions, i, NULL);
// Create a PanedWindow to manage the stuff in this dialog. PanedWindow won't let us set these to 0!
XtSetArg (args [0], XmNsashWidth, 1);
// Make small so user doesn't try to resize
XtSetArg (args [1], XmNsashHeight, 1);
pane = XmCreatePanedWindow (msgBox, const_cast<char*>("pane"), args, 2);
// Create a RowColumn in the form for Label and Text widgets. This is the control area.
form = XmCreateForm (pane, const_cast<char*>("form1"), NULL, 0);
// prepare the text for display in the ScrolledText object we are about to create.
n = 0;
XtSetArg (args [n], XmNscrollVertical, True); n++;
XtSetArg (args [n], XmNscrollHorizontal, False); n++;
XtSetArg (args [n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
XtSetArg (args [n], XmNeditable, False); n++;
XtSetArg (args [n], XmNcursorPositionVisible, False); n++;
XtSetArg (args [n], XmNwordWrap, True); n++;
XtSetArg (args [n], XmNvalue, pszMsg); n++;
XtSetArg (args [n], XmNrows, min (nRows, 30)); n++;
XtSetArg (args [n], XmNcolumns, min (nCols, 120)); n++;
msgText = XmCreateScrolledText (form, const_cast<char*>("help_text"), args, n);
// Attachment values must be set on the Text widget's PARENT, the ScrolledWindow. This  is the object that is positioned.
XtVaSetValues (XtParent (msgText),
               XmNleftAttachment, XmATTACH_FORM,
               XmNtopAttachment, XmATTACH_FORM,
               XmNrightAttachment, XmATTACH_FORM,
               XmNbottomAttachment, XmATTACH_FORM,
               NULL);
XtManageChild (msgText);
XtManageChild (form);
// Create another form to act as the action area for the dialog
XtSetArg (args [0], XmNfractionBase, 5);
form = XmCreateForm (pane, const_cast<char*>("form2"), args, 1);
// The OK button is under the pane's separator and is attached to the left edge of the form. It spreads from
// position 0 to 1 along the bottom (the form is split into 5 separate grids via XmNfractionBase upon creation).
widget = XmCreatePushButtonGadget (form, const_cast<char*>("Close"), NULL, 0);
XtVaSetValues (widget,
               XmNtopAttachment, XmATTACH_FORM,
               XmNbottomAttachment, XmATTACH_FORM,
               XmNleftAttachment, XmATTACH_POSITION,
               XmNleftPosition, 2,
               XmNrightAttachment, XmATTACH_POSITION,
               XmNrightPosition, 3,
               XmNshowAsDefault, True,
               XmNdefaultButtonShadowThickness, 1,
               NULL);
XtManageChild (widget);
XtAddCallback (widget, XmNactivateCallback, DestroyShell, (XtPointer) msgBox);
// Fix the action area pane to its current height -- never let it resize
XtManageChild (form);
XtVaGetValues (widget, XmNheight, &h, NULL);
XtVaSetValues (form, XmNpaneMaximum, h, XmNpaneMinimum, h, NULL);
// This also pops up the dialog, as it is the child of a DialogShell
XtManageChild (pane);
}

Функция обратного вызова для окна сообщения 'Кнопка Ok

void XmCloseMsgBox (Widget w, XtPointer clientData, XtPointer callData)
{
XtAppSetExitFlag (appShell);
}

Решите, использовать ли простое или расширенное окно сообщения, отобразите либо из них, и удаляйте их, когда пользователь нажимает кнопку закрытия / ОК.

void XmMessageBox (const char* pszMsg, bool bError)
{
   Widget   topWid;
   int      nRows, nCols;

nRows = MsgSize (const_cast<char*>(pszMsg), nCols);
if ((nRows > 3) || (nCols > 360))
   XmMessageDialog (pszMsg, nRows, nCols, bError);
else { // use the built-in message box
   topWid = XtVaAppInitialize (&appShell, "D2X-XL", NULL, 0, &argC, argv, NULL, NULL);
   // setup message box text
   Arg args [1];
   XmString xmString = XmStringCreateLocalized (const_cast<char*>(pszMsg));
   XtSetArg (args [0], XmNmessageString, xmString);
   // create and label message box
   Widget xMsgBox = bError
                    ? XmCreateErrorDialog (topWid, const_cast<char*>("Error"), args, 1)
                    : XmCreateWarningDialog (topWid, const_cast<char*>("Warning"), args, 1);
   // remove text resource
   XmStringFree (xmString);
   // remove help and cancel buttons
   XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_CANCEL_BUTTON));
   XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_HELP_BUTTON));
   // add callback to the "close" button that signals closing of the message box
   XtAddCallback (xMsgBox, XmNokCallback, XmCloseMsgBox, NULL);
   XtManageChild (xMsgBox);
   XtRealizeWidget (topWid);
   }
XtAppMainLoop (appShell);
XtUnrealizeWidget (topWid);
XtDestroyApplicationContext (appShell);
}
2
ответ дан 6 December 2019 в 15:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: